]> Git Repo - qemu.git/blob - hw/hd-geometry.c
hd-geometry: Factor out guess_chs_for_size()
[qemu.git] / hw / hd-geometry.c
1 /*
2  * Hard disk geometry utilities
3  *
4  * Copyright (C) 2012 Red Hat, Inc.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  *
9  * This file incorporates work covered by the following copyright and
10  * permission notice:
11  *
12  * Copyright (c) 2003 Fabrice Bellard
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this software and associated documentation files (the "Software"), to deal
16  * in the Software without restriction, including without limitation the rights
17  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18  * copies of the Software, and to permit persons to whom the Software is
19  * furnished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30  * THE SOFTWARE.
31  */
32
33 #include "block.h"
34 #include "hw/block-common.h"
35 #include "trace.h"
36
37 struct partition {
38         uint8_t boot_ind;           /* 0x80 - active */
39         uint8_t head;               /* starting head */
40         uint8_t sector;             /* starting sector */
41         uint8_t cyl;                /* starting cylinder */
42         uint8_t sys_ind;            /* What partition type */
43         uint8_t end_head;           /* end head */
44         uint8_t end_sector;         /* end sector */
45         uint8_t end_cyl;            /* end cylinder */
46         uint32_t start_sect;        /* starting sector counting from 0 */
47         uint32_t nr_sects;          /* nr of sectors in partition */
48 } QEMU_PACKED;
49
50 /* try to guess the disk logical geometry from the MSDOS partition table.
51    Return 0 if OK, -1 if could not guess */
52 static int guess_disk_lchs(BlockDriverState *bs,
53                            int *pcylinders, int *pheads, int *psectors)
54 {
55     uint8_t buf[BDRV_SECTOR_SIZE];
56     int i, heads, sectors, cylinders;
57     struct partition *p;
58     uint32_t nr_sects;
59     uint64_t nb_sectors;
60
61     bdrv_get_geometry(bs, &nb_sectors);
62
63     /**
64      * The function will be invoked during startup not only in sync I/O mode,
65      * but also in async I/O mode. So the I/O throttling function has to
66      * be disabled temporarily here, not permanently.
67      */
68     if (bdrv_read_unthrottled(bs, 0, buf, 1) < 0) {
69         return -1;
70     }
71     /* test msdos magic */
72     if (buf[510] != 0x55 || buf[511] != 0xaa) {
73         return -1;
74     }
75     for (i = 0; i < 4; i++) {
76         p = ((struct partition *)(buf + 0x1be)) + i;
77         nr_sects = le32_to_cpu(p->nr_sects);
78         if (nr_sects && p->end_head) {
79             /* We make the assumption that the partition terminates on
80                a cylinder boundary */
81             heads = p->end_head + 1;
82             sectors = p->end_sector & 63;
83             if (sectors == 0) {
84                 continue;
85             }
86             cylinders = nb_sectors / (heads * sectors);
87             if (cylinders < 1 || cylinders > 16383) {
88                 continue;
89             }
90             *pheads = heads;
91             *psectors = sectors;
92             *pcylinders = cylinders;
93             trace_hd_geometry_lchs_guess(bs, cylinders, heads, sectors);
94             return 0;
95         }
96     }
97     return -1;
98 }
99
100 static void guess_chs_for_size(BlockDriverState *bs,
101                                int *pcyls, int *pheads, int *psecs)
102 {
103     uint64_t nb_sectors;
104     int cylinders;
105
106     bdrv_get_geometry(bs, &nb_sectors);
107
108     cylinders = nb_sectors / (16 * 63);
109     if (cylinders > 16383) {
110         cylinders = 16383;
111     } else if (cylinders < 2) {
112         cylinders = 2;
113     }
114     *pcyls = cylinders;
115     *pheads = 16;
116     *psecs = 63;
117 }
118
119 void hd_geometry_guess(BlockDriverState *bs,
120                        int *pcyls, int *pheads, int *psecs)
121 {
122     int translation, lba_detected = 0;
123     int cylinders, heads, secs;
124
125     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
126     translation = bdrv_get_translation_hint(bs);
127
128     if (cylinders != 0) {
129         /* already got a geometry hint: use it */
130         *pcyls = cylinders;
131         *pheads = heads;
132         *psecs = secs;
133         return;
134     }
135
136     if (guess_disk_lchs(bs, &cylinders, &heads, &secs) < 0) {
137         /* no LCHS guess: use a standard physical disk geometry  */
138     default_geometry:
139         guess_chs_for_size(bs, pcyls, pheads, psecs);
140         if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
141             if ((*pcyls * *pheads) <= 131072) {
142                 bdrv_set_translation_hint(bs,
143                                           BIOS_ATA_TRANSLATION_LARGE);
144             } else {
145                 bdrv_set_translation_hint(bs,
146                                           BIOS_ATA_TRANSLATION_LBA);
147             }
148         }
149     } else if (heads > 16) {
150         /* LCHS guess with heads > 16 means that a BIOS LBA
151            translation was active, so a standard physical disk
152            geometry is OK */
153         lba_detected = 1;
154         goto default_geometry;
155     } else {
156         /* LCHS guess with heads <= 16: use as physical geometry */
157         *pcyls = cylinders;
158         *pheads = heads;
159         *psecs = secs;
160         /* disable any translation to be in sync with
161            the logical geometry */
162         if (translation == BIOS_ATA_TRANSLATION_AUTO) {
163             bdrv_set_translation_hint(bs,
164                                       BIOS_ATA_TRANSLATION_NONE);
165         }
166     }
167     bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
168     trace_hd_geometry_guess(bs, *pcyls, *pheads, *psecs, translation);
169 }
This page took 0.035017 seconds and 4 git commands to generate.