xref: /qemu/hw/block/block.c (revision 72ac97cd)
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 int blkconf_geometry(BlockConf *conf, int *ptrans,
26                      unsigned cyls_max, unsigned heads_max, unsigned secs_max)
27 {
28     DriveInfo *dinfo;
29 
30     if (!conf->cyls && !conf->heads && !conf->secs) {
31         /* try to fall back to value set with legacy -drive cyls=... */
32         dinfo = drive_get_by_blockdev(conf->bs);
33         conf->cyls  = dinfo->cyls;
34         conf->heads = dinfo->heads;
35         conf->secs  = dinfo->secs;
36         if (ptrans) {
37             *ptrans = dinfo->trans;
38         }
39     }
40     if (!conf->cyls && !conf->heads && !conf->secs) {
41         hd_geometry_guess(conf->bs,
42                           &conf->cyls, &conf->heads, &conf->secs,
43                           ptrans);
44     } else if (ptrans && *ptrans == BIOS_ATA_TRANSLATION_AUTO) {
45         *ptrans = hd_bios_chs_auto_trans(conf->cyls, conf->heads, conf->secs);
46     }
47     if (conf->cyls || conf->heads || conf->secs) {
48         if (conf->cyls < 1 || conf->cyls > cyls_max) {
49             error_report("cyls must be between 1 and %u", cyls_max);
50             return -1;
51         }
52         if (conf->heads < 1 || conf->heads > heads_max) {
53             error_report("heads must be between 1 and %u", heads_max);
54             return -1;
55         }
56         if (conf->secs < 1 || conf->secs > secs_max) {
57             error_report("secs must be between 1 and %u", secs_max);
58             return -1;
59         }
60     }
61     return 0;
62 }
63