]> git.proxmox.com Git - qemu.git/blame - hw/hd-geometry.c
hd-geometry: Move disk geometry guessing back from block.c
[qemu.git] / hw / hd-geometry.c
CommitLineData
9db1c0f7
MA
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
36struct partition {
37 uint8_t boot_ind; /* 0x80 - active */
38 uint8_t head; /* starting head */
39 uint8_t sector; /* starting sector */
40 uint8_t cyl; /* starting cylinder */
41 uint8_t sys_ind; /* What partition type */
42 uint8_t end_head; /* end head */
43 uint8_t end_sector; /* end sector */
44 uint8_t end_cyl; /* end cylinder */
45 uint32_t start_sect; /* starting sector counting from 0 */
46 uint32_t nr_sects; /* nr of sectors in partition */
47} QEMU_PACKED;
48
49/* try to guess the disk logical geometry from the MSDOS partition table.
50 Return 0 if OK, -1 if could not guess */
51static int guess_disk_lchs(BlockDriverState *bs,
52 int *pcylinders, int *pheads, int *psectors)
53{
54 uint8_t buf[BDRV_SECTOR_SIZE];
55 int i, heads, sectors, cylinders;
56 struct partition *p;
57 uint32_t nr_sects;
58 uint64_t nb_sectors;
59
60 bdrv_get_geometry(bs, &nb_sectors);
61
62 /**
63 * The function will be invoked during startup not only in sync I/O mode,
64 * but also in async I/O mode. So the I/O throttling function has to
65 * be disabled temporarily here, not permanently.
66 */
67 if (bdrv_read_unthrottled(bs, 0, buf, 1) < 0) {
68 return -1;
69 }
70 /* test msdos magic */
71 if (buf[510] != 0x55 || buf[511] != 0xaa) {
72 return -1;
73 }
74 for (i = 0; i < 4; i++) {
75 p = ((struct partition *)(buf + 0x1be)) + i;
76 nr_sects = le32_to_cpu(p->nr_sects);
77 if (nr_sects && p->end_head) {
78 /* We make the assumption that the partition terminates on
79 a cylinder boundary */
80 heads = p->end_head + 1;
81 sectors = p->end_sector & 63;
82 if (sectors == 0) {
83 continue;
84 }
85 cylinders = nb_sectors / (heads * sectors);
86 if (cylinders < 1 || cylinders > 16383) {
87 continue;
88 }
89 *pheads = heads;
90 *psectors = sectors;
91 *pcylinders = cylinders;
92#if 0
93 printf("guessed geometry: LCHS=%d %d %d\n",
94 cylinders, heads, sectors);
95#endif
96 return 0;
97 }
98 }
99 return -1;
100}
101
102void hd_geometry_guess(BlockDriverState *bs,
103 int *pcyls, int *pheads, int *psecs)
104{
105 int translation, lba_detected = 0;
106 int cylinders, heads, secs;
107 uint64_t nb_sectors;
108
109 /* if a geometry hint is available, use it */
110 bdrv_get_geometry(bs, &nb_sectors);
111 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
112 translation = bdrv_get_translation_hint(bs);
113 if (cylinders != 0) {
114 *pcyls = cylinders;
115 *pheads = heads;
116 *psecs = secs;
117 } else {
118 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
119 if (heads > 16) {
120 /* if heads > 16, it means that a BIOS LBA
121 translation was active, so the default
122 hardware geometry is OK */
123 lba_detected = 1;
124 goto default_geometry;
125 } else {
126 *pcyls = cylinders;
127 *pheads = heads;
128 *psecs = secs;
129 /* disable any translation to be in sync with
130 the logical geometry */
131 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
132 bdrv_set_translation_hint(bs,
133 BIOS_ATA_TRANSLATION_NONE);
134 }
135 }
136 } else {
137 default_geometry:
138 /* if no geometry, use a standard physical disk geometry */
139 cylinders = nb_sectors / (16 * 63);
140
141 if (cylinders > 16383) {
142 cylinders = 16383;
143 } else if (cylinders < 2) {
144 cylinders = 2;
145 }
146 *pcyls = cylinders;
147 *pheads = 16;
148 *psecs = 63;
149 if ((lba_detected == 1)
150 && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
151 if ((*pcyls * *pheads) <= 131072) {
152 bdrv_set_translation_hint(bs,
153 BIOS_ATA_TRANSLATION_LARGE);
154 } else {
155 bdrv_set_translation_hint(bs,
156 BIOS_ATA_TRANSLATION_LBA);
157 }
158 }
159 }
160 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
161 }
162}