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 if (blk_pread(blk, 0, buf, BDRV_SECTOR_SIZE) < 0) {
67 return -1;
68 }
69
70 if (buf[510] != 0x55 || buf[511] != 0xaa) {
71 return -1;
72 }
73 for (i = 0; i < 4; i++) {
74 p = ((struct partition *)(buf + 0x1be)) + i;
75 nr_sects = le32_to_cpu(p->nr_sects);
76 if (nr_sects && p->end_head) {
77
78
79 heads = p->end_head + 1;
80 sectors = p->end_sector & 63;
81 if (sectors == 0) {
82 continue;
83 }
84 cylinders = nb_sectors / (heads * sectors);
85 if (cylinders < 1 || cylinders > 16383) {
86 continue;
87 }
88 *pheads = heads;
89 *psectors = sectors;
90 *pcylinders = cylinders;
91 trace_hd_geometry_lchs_guess(blk, cylinders, heads, sectors);
92 return 0;
93 }
94 }
95 return -1;
96}
97
98static void guess_chs_for_size(BlockBackend *blk,
99 uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs)
100{
101 uint64_t nb_sectors;
102 int cylinders;
103
104 blk_get_geometry(blk, &nb_sectors);
105
106 cylinders = nb_sectors / (16 * 63);
107 if (cylinders > 16383) {
108 cylinders = 16383;
109 } else if (cylinders < 2) {
110 cylinders = 2;
111 }
112 *pcyls = cylinders;
113 *pheads = 16;
114 *psecs = 63;
115}
116
117void hd_geometry_guess(BlockBackend *blk,
118 uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs,
119 int *ptrans)
120{
121 int cylinders, heads, secs, translation;
122 HDGeometry geo;
123
124
125
126 if (blk_probe_geometry(blk, &geo) == 0) {
127 *pcyls = geo.cylinders;
128 *psecs = geo.sectors;
129 *pheads = geo.heads;
130 translation = BIOS_ATA_TRANSLATION_NONE;
131 } else if (guess_disk_lchs(blk, &cylinders, &heads, &secs) < 0) {
132
133 guess_chs_for_size(blk, pcyls, pheads, psecs);
134 translation = hd_bios_chs_auto_trans(*pcyls, *pheads, *psecs);
135 } else if (heads > 16) {
136
137
138
139 guess_chs_for_size(blk, pcyls, pheads, psecs);
140 translation = *pcyls * *pheads <= 131072
141 ? BIOS_ATA_TRANSLATION_LARGE
142 : BIOS_ATA_TRANSLATION_LBA;
143 } else {
144
145 *pcyls = cylinders;
146 *pheads = heads;
147 *psecs = secs;
148
149
150 translation = BIOS_ATA_TRANSLATION_NONE;
151 }
152 if (ptrans) {
153 *ptrans = translation;
154 }
155 trace_hd_geometry_guess(blk, *pcyls, *pheads, *psecs, translation);
156}
157
158int hd_bios_chs_auto_trans(uint32_t cyls, uint32_t heads, uint32_t secs)
159{
160 return cyls <= 1024 && heads <= 16 && secs <= 63
161 ? BIOS_ATA_TRANSLATION_NONE
162 : BIOS_ATA_TRANSLATION_LBA;
163}
164