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