1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include "qemu/osdep.h"
34#include "sysemu/block-backend.h"
35#include "hw/block/block.h"
36#include "trace.h"
37
38struct partition {
39 uint8_t boot_ind;
40 uint8_t head;
41 uint8_t sector;
42 uint8_t cyl;
43 uint8_t sys_ind;
44 uint8_t end_head;
45 uint8_t end_sector;
46 uint8_t end_cyl;
47 uint32_t start_sect;
48 uint32_t nr_sects;
49} QEMU_PACKED;
50
51
52
53static int guess_disk_lchs(BlockBackend *blk,
54 int *pcylinders, int *pheads, int *psectors)
55{
56 uint8_t buf[BDRV_SECTOR_SIZE];
57 int i, heads, sectors, cylinders;
58 struct partition *p;
59 uint32_t nr_sects;
60 uint64_t nb_sectors;
61
62 blk_get_geometry(blk, &nb_sectors);
63
64
65
66
67
68
69 if (blk_read_unthrottled(blk, 0, buf, 1) < 0) {
70 return -1;
71 }
72
73 if (buf[510] != 0x55 || buf[511] != 0xaa) {
74 return -1;
75 }
76 for (i = 0; i < 4; i++) {
77 p = ((struct partition *)(buf + 0x1be)) + i;
78 nr_sects = le32_to_cpu(p->nr_sects);
79 if (nr_sects && p->end_head) {
80
81
82 heads = p->end_head + 1;
83 sectors = p->end_sector & 63;
84 if (sectors == 0) {
85 continue;
86 }
87 cylinders = nb_sectors / (heads * sectors);
88 if (cylinders < 1 || cylinders > 16383) {
89 continue;
90 }
91 *pheads = heads;
92 *psectors = sectors;
93 *pcylinders = cylinders;
94 trace_hd_geometry_lchs_guess(blk, cylinders, heads, sectors);
95 return 0;
96 }
97 }
98 return -1;
99}
100
101static void guess_chs_for_size(BlockBackend *blk,
102 uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs)
103{
104 uint64_t nb_sectors;
105 int cylinders;
106
107 blk_get_geometry(blk, &nb_sectors);
108
109 cylinders = nb_sectors / (16 * 63);
110 if (cylinders > 16383) {
111 cylinders = 16383;
112 } else if (cylinders < 2) {
113 cylinders = 2;
114 }
115 *pcyls = cylinders;
116 *pheads = 16;
117 *psecs = 63;
118}
119
120void hd_geometry_guess(BlockBackend *blk,
121 uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs,
122 int *ptrans)
123{
124 int cylinders, heads, secs, translation;
125 HDGeometry geo;
126
127
128
129 if (blk_probe_geometry(blk, &geo) == 0) {
130 *pcyls = geo.cylinders;
131 *psecs = geo.sectors;
132 *pheads = geo.heads;
133 translation = BIOS_ATA_TRANSLATION_NONE;
134 } else if (guess_disk_lchs(blk, &cylinders, &heads, &secs) < 0) {
135
136 guess_chs_for_size(blk, pcyls, pheads, psecs);
137 translation = hd_bios_chs_auto_trans(*pcyls, *pheads, *psecs);
138 } else if (heads > 16) {
139
140
141
142 guess_chs_for_size(blk, pcyls, pheads, psecs);
143 translation = *pcyls * *pheads <= 131072
144 ? BIOS_ATA_TRANSLATION_LARGE
145 : BIOS_ATA_TRANSLATION_LBA;
146 } else {
147
148 *pcyls = cylinders;
149 *pheads = heads;
150 *psecs = secs;
151
152
153 translation = BIOS_ATA_TRANSLATION_NONE;
154 }
155 if (ptrans) {
156 *ptrans = translation;
157 }
158 trace_hd_geometry_guess(blk, *pcyls, *pheads, *psecs, translation);
159}
160
161int hd_bios_chs_auto_trans(uint32_t cyls, uint32_t heads, uint32_t secs)
162{
163 return cyls <= 1024 && heads <= 16 && secs <= 63
164 ? BIOS_ATA_TRANSLATION_NONE
165 : BIOS_ATA_TRANSLATION_LBA;
166}
167