xref: /openbmc/qemu/hw/block/block.c (revision 4a09d0bb)
1 /*
2  * Common code for block device models
3  *
4  * Copyright (C) 2012 Red Hat, Inc.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or
7  * later.  See the COPYING file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "sysemu/blockdev.h"
12 #include "sysemu/block-backend.h"
13 #include "hw/block/block.h"
14 #include "qapi/error.h"
15 #include "qemu/error-report.h"
16 
17 void blkconf_serial(BlockConf *conf, char **serial)
18 {
19     DriveInfo *dinfo;
20 
21     if (!*serial) {
22         /* try to fall back to value set with legacy -drive serial=... */
23         dinfo = blk_legacy_dinfo(conf->blk);
24         if (dinfo) {
25             *serial = g_strdup(dinfo->serial);
26         }
27     }
28 }
29 
30 void blkconf_blocksizes(BlockConf *conf)
31 {
32     BlockBackend *blk = conf->blk;
33     BlockSizes blocksizes;
34     int backend_ret;
35 
36     backend_ret = blk_probe_blocksizes(blk, &blocksizes);
37     /* fill in detected values if they are not defined via qemu command line */
38     if (!conf->physical_block_size) {
39         if (!backend_ret) {
40            conf->physical_block_size = blocksizes.phys;
41         } else {
42             conf->physical_block_size = BDRV_SECTOR_SIZE;
43         }
44     }
45     if (!conf->logical_block_size) {
46         if (!backend_ret) {
47             conf->logical_block_size = blocksizes.log;
48         } else {
49             conf->logical_block_size = BDRV_SECTOR_SIZE;
50         }
51     }
52 }
53 
54 void blkconf_apply_backend_options(BlockConf *conf)
55 {
56     BlockBackend *blk = conf->blk;
57     BlockdevOnError rerror, werror;
58     bool wce;
59 
60     switch (conf->wce) {
61     case ON_OFF_AUTO_ON:    wce = true; break;
62     case ON_OFF_AUTO_OFF:   wce = false; break;
63     case ON_OFF_AUTO_AUTO:  wce = blk_enable_write_cache(blk); break;
64     default:
65         abort();
66     }
67 
68     rerror = conf->rerror;
69     if (rerror == BLOCKDEV_ON_ERROR_AUTO) {
70         rerror = blk_get_on_error(blk, true);
71     }
72 
73     werror = conf->werror;
74     if (werror == BLOCKDEV_ON_ERROR_AUTO) {
75         werror = blk_get_on_error(blk, false);
76     }
77 
78     blk_set_enable_write_cache(blk, wce);
79     blk_set_on_error(blk, rerror, werror);
80 }
81 
82 void blkconf_geometry(BlockConf *conf, int *ptrans,
83                       unsigned cyls_max, unsigned heads_max, unsigned secs_max,
84                       Error **errp)
85 {
86     DriveInfo *dinfo;
87 
88     if (!conf->cyls && !conf->heads && !conf->secs) {
89         /* try to fall back to value set with legacy -drive cyls=... */
90         dinfo = blk_legacy_dinfo(conf->blk);
91         if (dinfo) {
92             conf->cyls  = dinfo->cyls;
93             conf->heads = dinfo->heads;
94             conf->secs  = dinfo->secs;
95             if (ptrans) {
96                 *ptrans = dinfo->trans;
97             }
98         }
99     }
100     if (!conf->cyls && !conf->heads && !conf->secs) {
101         hd_geometry_guess(conf->blk,
102                           &conf->cyls, &conf->heads, &conf->secs,
103                           ptrans);
104     } else if (ptrans && *ptrans == BIOS_ATA_TRANSLATION_AUTO) {
105         *ptrans = hd_bios_chs_auto_trans(conf->cyls, conf->heads, conf->secs);
106     }
107     if (conf->cyls || conf->heads || conf->secs) {
108         if (conf->cyls < 1 || conf->cyls > cyls_max) {
109             error_setg(errp, "cyls must be between 1 and %u", cyls_max);
110             return;
111         }
112         if (conf->heads < 1 || conf->heads > heads_max) {
113             error_setg(errp, "heads must be between 1 and %u", heads_max);
114             return;
115         }
116         if (conf->secs < 1 || conf->secs > secs_max) {
117             error_setg(errp, "secs must be between 1 and %u", secs_max);
118             return;
119         }
120     }
121 }
122