xref: /openbmc/qemu/hw/block/block.c (revision 407ba084)
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 "sysemu/blockdev.h"
11 #include "hw/block/block.h"
12 #include "qemu/error-report.h"
13 
14 void blkconf_serial(BlockConf *conf, char **serial)
15 {
16     DriveInfo *dinfo;
17 
18     if (!*serial) {
19         /* try to fall back to value set with legacy -drive serial=... */
20         dinfo = drive_get_by_blockdev(conf->bs);
21         *serial = g_strdup(dinfo->serial);
22     }
23 }
24 
25 void blkconf_geometry(BlockConf *conf, int *ptrans,
26                       unsigned cyls_max, unsigned heads_max, unsigned secs_max,
27                       Error **errp)
28 {
29     DriveInfo *dinfo;
30 
31     if (!conf->cyls && !conf->heads && !conf->secs) {
32         /* try to fall back to value set with legacy -drive cyls=... */
33         dinfo = drive_get_by_blockdev(conf->bs);
34         conf->cyls  = dinfo->cyls;
35         conf->heads = dinfo->heads;
36         conf->secs  = dinfo->secs;
37         if (ptrans) {
38             *ptrans = dinfo->trans;
39         }
40     }
41     if (!conf->cyls && !conf->heads && !conf->secs) {
42         hd_geometry_guess(conf->bs,
43                           &conf->cyls, &conf->heads, &conf->secs,
44                           ptrans);
45     } else if (ptrans && *ptrans == BIOS_ATA_TRANSLATION_AUTO) {
46         *ptrans = hd_bios_chs_auto_trans(conf->cyls, conf->heads, conf->secs);
47     }
48     if (conf->cyls || conf->heads || conf->secs) {
49         if (conf->cyls < 1 || conf->cyls > cyls_max) {
50             error_setg(errp, "cyls must be between 1 and %u", cyls_max);
51             return;
52         }
53         if (conf->heads < 1 || conf->heads > heads_max) {
54             error_setg(errp, "heads must be between 1 and %u", heads_max);
55             return;
56         }
57         if (conf->secs < 1 || conf->secs > secs_max) {
58             error_setg(errp, "secs must be between 1 and %u", secs_max);
59             return;
60         }
61     }
62 }
63