]>
Commit | Line | Data |
---|---|---|
0e8a8c8f MA |
1 | /* |
2 | * Hard disk geometry test cases. | |
3 | * | |
4 | * Copyright (c) 2012 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Markus Armbruster <[email protected]>, | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
10 | * See the COPYING file in the top-level directory. | |
11 | */ | |
12 | ||
13 | /* | |
14 | * Covers only IDE and tests only CMOS contents. Better than nothing. | |
15 | * Improvements welcome. | |
16 | */ | |
17 | ||
18 | #include <glib.h> | |
19 | #include <stdlib.h> | |
20 | #include <string.h> | |
21 | #include <unistd.h> | |
22 | #include "qemu-common.h" | |
23 | #include "libqtest.h" | |
24 | ||
0e8a8c8f MA |
25 | static char *create_test_img(int secs) |
26 | { | |
27 | char *template = strdup("/tmp/qtest.XXXXXX"); | |
28 | int fd, ret; | |
29 | ||
30 | fd = mkstemp(template); | |
31 | g_assert(fd >= 0); | |
32 | ret = ftruncate(fd, (off_t)secs * 512); | |
33 | g_assert(ret == 0); | |
34 | close(fd); | |
35 | return template; | |
36 | } | |
37 | ||
38 | typedef struct { | |
39 | int cyls, heads, secs, trans; | |
40 | } CHST; | |
41 | ||
42 | typedef enum { | |
43 | mbr_blank, mbr_lba, mbr_chs, | |
44 | mbr_last | |
45 | } MBRcontents; | |
46 | ||
47 | typedef enum { | |
48 | /* order is relevant */ | |
49 | backend_small, backend_large, backend_empty, | |
50 | backend_last | |
51 | } Backend; | |
52 | ||
53 | static const int img_secs[backend_last] = { | |
54 | [backend_small] = 61440, | |
55 | [backend_large] = 8388608, | |
56 | [backend_empty] = -1, | |
57 | }; | |
58 | ||
59 | static const CHST hd_chst[backend_last][mbr_last] = { | |
60 | [backend_small] = { | |
61 | [mbr_blank] = { 60, 16, 63, 0 }, | |
62 | [mbr_lba] = { 60, 16, 63, 2 }, | |
63 | [mbr_chs] = { 60, 16, 63, 0 } | |
64 | }, | |
65 | [backend_large] = { | |
66 | [mbr_blank] = { 8322, 16, 63, 1 }, | |
67 | [mbr_lba] = { 8322, 16, 63, 1 }, | |
68 | [mbr_chs] = { 8322, 16, 63, 0 } | |
69 | }, | |
70 | }; | |
71 | ||
72 | static const char *img_file_name[backend_last]; | |
73 | ||
74 | static const CHST *cur_ide[4]; | |
75 | ||
76 | static bool is_hd(const CHST *expected_chst) | |
77 | { | |
78 | return expected_chst && expected_chst->cyls; | |
79 | } | |
80 | ||
81 | static void test_cmos_byte(int reg, int expected) | |
82 | { | |
83 | enum { cmos_base = 0x70 }; | |
84 | int actual; | |
85 | ||
86 | outb(cmos_base + 0, reg); | |
87 | actual = inb(cmos_base + 1); | |
88 | g_assert(actual == expected); | |
89 | } | |
90 | ||
91 | static void test_cmos_bytes(int reg0, int n, uint8_t expected[]) | |
92 | { | |
93 | int i; | |
94 | ||
95 | for (i = 0; i < 9; i++) { | |
96 | test_cmos_byte(reg0 + i, expected[i]); | |
97 | } | |
98 | } | |
99 | ||
100 | static void test_cmos_disk_data(void) | |
101 | { | |
102 | test_cmos_byte(0x12, | |
103 | (is_hd(cur_ide[0]) ? 0xf0 : 0) | | |
104 | (is_hd(cur_ide[1]) ? 0x0f : 0)); | |
105 | } | |
106 | ||
107 | static void test_cmos_drive_cyl(int reg0, const CHST *expected_chst) | |
108 | { | |
109 | if (is_hd(expected_chst)) { | |
110 | int c = expected_chst->cyls; | |
111 | int h = expected_chst->heads; | |
112 | int s = expected_chst->secs; | |
113 | uint8_t expected_bytes[9] = { | |
114 | c & 0xff, c >> 8, h, 0xff, 0xff, 0xc0 | ((h > 8) << 3), | |
115 | c & 0xff, c >> 8, s | |
116 | }; | |
117 | test_cmos_bytes(reg0, 9, expected_bytes); | |
118 | } else { | |
119 | int i; | |
120 | ||
121 | for (i = 0; i < 9; i++) { | |
122 | test_cmos_byte(reg0 + i, 0); | |
123 | } | |
124 | } | |
125 | } | |
126 | ||
127 | static void test_cmos_drive1(void) | |
128 | { | |
129 | test_cmos_byte(0x19, is_hd(cur_ide[0]) ? 47 : 0); | |
130 | test_cmos_drive_cyl(0x1b, cur_ide[0]); | |
131 | } | |
132 | ||
133 | static void test_cmos_drive2(void) | |
134 | { | |
135 | test_cmos_byte(0x1a, is_hd(cur_ide[1]) ? 47 : 0); | |
136 | test_cmos_drive_cyl(0x24, cur_ide[1]); | |
137 | } | |
138 | ||
139 | static void test_cmos_disktransflag(void) | |
140 | { | |
141 | int val, i; | |
142 | ||
143 | val = 0; | |
144 | for (i = 0; i < ARRAY_SIZE(cur_ide); i++) { | |
145 | if (is_hd(cur_ide[i])) { | |
146 | val |= cur_ide[i]->trans << (2 * i); | |
147 | } | |
148 | } | |
149 | test_cmos_byte(0x39, val); | |
150 | } | |
151 | ||
152 | static void test_cmos(void) | |
153 | { | |
154 | test_cmos_disk_data(); | |
155 | test_cmos_drive1(); | |
156 | test_cmos_drive2(); | |
157 | test_cmos_disktransflag(); | |
158 | } | |
159 | ||
160 | static int append_arg(int argc, char *argv[], int argv_sz, char *arg) | |
161 | { | |
162 | g_assert(argc + 1 < argv_sz); | |
163 | argv[argc++] = arg; | |
164 | argv[argc] = NULL; | |
165 | return argc; | |
166 | } | |
167 | ||
168 | static int setup_common(char *argv[], int argv_sz) | |
169 | { | |
170 | memset(cur_ide, 0, sizeof(cur_ide)); | |
171 | return append_arg(0, argv, argv_sz, | |
2ad645d2 | 172 | g_strdup("-nodefaults")); |
0e8a8c8f MA |
173 | } |
174 | ||
175 | static void setup_mbr(int img_idx, MBRcontents mbr) | |
176 | { | |
177 | static const uint8_t part_lba[16] = { | |
178 | /* chs 0,1,1 (lba 63) to chs 0,127,63 (8001 sectors) */ | |
179 | 0x80, 1, 1, 0, 6, 127, 63, 0, 63, 0, 0, 0, 0x41, 0x1F, 0, 0, | |
180 | }; | |
181 | static const uint8_t part_chs[16] = { | |
182 | /* chs 0,1,1 (lba 63) to chs 7,15,63 (8001 sectors) */ | |
183 | 0x80, 1, 1, 0, 6, 15, 63, 7, 63, 0, 0, 0, 0x41, 0x1F, 0, 0, | |
184 | }; | |
185 | uint8_t buf[512]; | |
186 | int fd, ret; | |
187 | ||
188 | memset(buf, 0, sizeof(buf)); | |
189 | ||
190 | if (mbr != mbr_blank) { | |
191 | buf[0x1fe] = 0x55; | |
192 | buf[0x1ff] = 0xAA; | |
193 | memcpy(buf + 0x1BE, mbr == mbr_lba ? part_lba : part_chs, 16); | |
194 | } | |
195 | ||
196 | fd = open(img_file_name[img_idx], O_WRONLY); | |
197 | g_assert(fd >= 0); | |
198 | ret = write(fd, buf, sizeof(buf)); | |
199 | g_assert(ret == sizeof(buf)); | |
200 | close(fd); | |
201 | } | |
202 | ||
203 | static int setup_ide(int argc, char *argv[], int argv_sz, | |
204 | int ide_idx, const char *dev, int img_idx, | |
205 | MBRcontents mbr, const char *opts) | |
206 | { | |
207 | char *s1, *s2, *s3; | |
208 | ||
b8e665e4 | 209 | s1 = g_strdup_printf("-drive id=drive%d,if=%s,format=raw", |
0e8a8c8f MA |
210 | ide_idx, dev ? "none" : "ide"); |
211 | s2 = dev ? g_strdup("") : g_strdup_printf(",index=%d", ide_idx); | |
212 | ||
213 | if (img_secs[img_idx] >= 0) { | |
214 | setup_mbr(img_idx, mbr); | |
215 | s3 = g_strdup_printf(",file=%s", img_file_name[img_idx]); | |
216 | } else { | |
217 | s3 = g_strdup(",media=cdrom"); | |
218 | } | |
219 | argc = append_arg(argc, argv, argv_sz, | |
220 | g_strdup_printf("%s%s%s%s", s1, s2, s3, opts)); | |
221 | g_free(s1); | |
222 | g_free(s2); | |
223 | g_free(s3); | |
224 | ||
225 | if (dev) { | |
226 | argc = append_arg(argc, argv, argv_sz, | |
227 | g_strdup_printf("-device %s,drive=drive%d," | |
228 | "bus=ide.%d,unit=%d", | |
229 | dev, ide_idx, | |
230 | ide_idx / 2, ide_idx % 2)); | |
231 | } | |
232 | return argc; | |
233 | } | |
234 | ||
235 | /* | |
236 | * Test case: no IDE devices | |
237 | */ | |
238 | static void test_ide_none(void) | |
239 | { | |
240 | char *argv[256]; | |
241 | ||
242 | setup_common(argv, ARRAY_SIZE(argv)); | |
243 | qtest_start(g_strjoinv(" ", argv)); | |
244 | test_cmos(); | |
1d9358e6 | 245 | qtest_end(); |
0e8a8c8f MA |
246 | } |
247 | ||
248 | static void test_ide_mbr(bool use_device, MBRcontents mbr) | |
249 | { | |
250 | char *argv[256]; | |
251 | int argc; | |
252 | Backend i; | |
253 | const char *dev; | |
254 | ||
255 | argc = setup_common(argv, ARRAY_SIZE(argv)); | |
256 | for (i = 0; i < backend_last; i++) { | |
257 | cur_ide[i] = &hd_chst[i][mbr]; | |
258 | dev = use_device ? (is_hd(cur_ide[i]) ? "ide-hd" : "ide-cd") : NULL; | |
259 | argc = setup_ide(argc, argv, ARRAY_SIZE(argv), i, dev, i, mbr, ""); | |
260 | } | |
261 | qtest_start(g_strjoinv(" ", argv)); | |
262 | test_cmos(); | |
1d9358e6 | 263 | qtest_end(); |
0e8a8c8f MA |
264 | } |
265 | ||
266 | /* | |
267 | * Test case: IDE devices (if=ide) with blank MBRs | |
268 | */ | |
269 | static void test_ide_drive_mbr_blank(void) | |
270 | { | |
271 | test_ide_mbr(false, mbr_blank); | |
272 | } | |
273 | ||
274 | /* | |
275 | * Test case: IDE devices (if=ide) with MBRs indicating LBA is in use | |
276 | */ | |
277 | static void test_ide_drive_mbr_lba(void) | |
278 | { | |
279 | test_ide_mbr(false, mbr_lba); | |
280 | } | |
281 | ||
282 | /* | |
283 | * Test case: IDE devices (if=ide) with MBRs indicating CHS is in use | |
284 | */ | |
285 | static void test_ide_drive_mbr_chs(void) | |
286 | { | |
287 | test_ide_mbr(false, mbr_chs); | |
288 | } | |
289 | ||
290 | /* | |
291 | * Test case: IDE devices (if=none) with blank MBRs | |
292 | */ | |
293 | static void test_ide_device_mbr_blank(void) | |
294 | { | |
295 | test_ide_mbr(true, mbr_blank); | |
296 | } | |
297 | ||
298 | /* | |
299 | * Test case: IDE devices (if=none) with MBRs indicating LBA is in use | |
300 | */ | |
301 | static void test_ide_device_mbr_lba(void) | |
302 | { | |
303 | test_ide_mbr(true, mbr_lba); | |
304 | } | |
305 | ||
306 | /* | |
307 | * Test case: IDE devices (if=none) with MBRs indicating CHS is in use | |
308 | */ | |
309 | static void test_ide_device_mbr_chs(void) | |
310 | { | |
311 | test_ide_mbr(true, mbr_chs); | |
312 | } | |
313 | ||
314 | static void test_ide_drive_user(const char *dev, bool trans) | |
315 | { | |
316 | char *argv[256], *opts; | |
317 | int argc; | |
318 | int secs = img_secs[backend_small]; | |
319 | const CHST expected_chst = { secs / (4 * 32) , 4, 32, trans }; | |
320 | ||
321 | argc = setup_common(argv, ARRAY_SIZE(argv)); | |
856dcba2 MA |
322 | opts = g_strdup_printf("%s,%s%scyls=%d,heads=%d,secs=%d", |
323 | dev ?: "", | |
324 | trans && dev ? "bios-chs-" : "", | |
325 | trans ? "trans=lba," : "", | |
0e8a8c8f | 326 | expected_chst.cyls, expected_chst.heads, |
856dcba2 | 327 | expected_chst.secs); |
0e8a8c8f MA |
328 | cur_ide[0] = &expected_chst; |
329 | argc = setup_ide(argc, argv, ARRAY_SIZE(argv), | |
856dcba2 MA |
330 | 0, dev ? opts : NULL, backend_small, mbr_chs, |
331 | dev ? "" : opts); | |
0e8a8c8f MA |
332 | g_free(opts); |
333 | qtest_start(g_strjoinv(" ", argv)); | |
334 | test_cmos(); | |
1d9358e6 | 335 | qtest_end(); |
0e8a8c8f MA |
336 | } |
337 | ||
338 | /* | |
339 | * Test case: IDE device (if=ide) with explicit CHS | |
340 | */ | |
341 | static void test_ide_drive_user_chs(void) | |
342 | { | |
343 | test_ide_drive_user(NULL, false); | |
344 | } | |
345 | ||
346 | /* | |
347 | * Test case: IDE device (if=ide) with explicit CHS and translation | |
348 | */ | |
349 | static void test_ide_drive_user_chst(void) | |
350 | { | |
351 | test_ide_drive_user(NULL, true); | |
352 | } | |
353 | ||
354 | /* | |
355 | * Test case: IDE device (if=none) with explicit CHS | |
356 | */ | |
357 | static void test_ide_device_user_chs(void) | |
358 | { | |
359 | test_ide_drive_user("ide-hd", false); | |
360 | } | |
361 | ||
362 | /* | |
363 | * Test case: IDE device (if=none) with explicit CHS and translation | |
364 | */ | |
365 | static void test_ide_device_user_chst(void) | |
366 | { | |
367 | test_ide_drive_user("ide-hd", true); | |
368 | } | |
369 | ||
4e4e6e31 MA |
370 | /* |
371 | * Test case: IDE devices (if=ide), but use index=0 for CD-ROM | |
372 | */ | |
373 | static void test_ide_drive_cd_0(void) | |
374 | { | |
375 | char *argv[256]; | |
376 | int argc, ide_idx; | |
377 | Backend i; | |
378 | ||
379 | argc = setup_common(argv, ARRAY_SIZE(argv)); | |
380 | for (i = 0; i <= backend_empty; i++) { | |
381 | ide_idx = backend_empty - i; | |
382 | cur_ide[ide_idx] = &hd_chst[i][mbr_blank]; | |
383 | argc = setup_ide(argc, argv, ARRAY_SIZE(argv), | |
384 | ide_idx, NULL, i, mbr_blank, ""); | |
385 | } | |
386 | qtest_start(g_strjoinv(" ", argv)); | |
387 | test_cmos(); | |
1d9358e6 | 388 | qtest_end(); |
4e4e6e31 MA |
389 | } |
390 | ||
0e8a8c8f MA |
391 | int main(int argc, char **argv) |
392 | { | |
393 | Backend i; | |
394 | int ret; | |
395 | ||
396 | g_test_init(&argc, &argv, NULL); | |
397 | ||
398 | for (i = 0; i < backend_last; i++) { | |
399 | if (img_secs[i] >= 0) { | |
400 | img_file_name[i] = create_test_img(img_secs[i]); | |
401 | } else { | |
402 | img_file_name[i] = NULL; | |
403 | } | |
404 | } | |
405 | ||
406 | qtest_add_func("hd-geo/ide/none", test_ide_none); | |
407 | qtest_add_func("hd-geo/ide/drive/mbr/blank", test_ide_drive_mbr_blank); | |
408 | qtest_add_func("hd-geo/ide/drive/mbr/lba", test_ide_drive_mbr_lba); | |
409 | qtest_add_func("hd-geo/ide/drive/mbr/chs", test_ide_drive_mbr_chs); | |
410 | qtest_add_func("hd-geo/ide/drive/user/chs", test_ide_drive_user_chs); | |
411 | qtest_add_func("hd-geo/ide/drive/user/chst", test_ide_drive_user_chst); | |
4e4e6e31 | 412 | qtest_add_func("hd-geo/ide/drive/cd_0", test_ide_drive_cd_0); |
0e8a8c8f MA |
413 | qtest_add_func("hd-geo/ide/device/mbr/blank", test_ide_device_mbr_blank); |
414 | qtest_add_func("hd-geo/ide/device/mbr/lba", test_ide_device_mbr_lba); | |
415 | qtest_add_func("hd-geo/ide/device/mbr/chs", test_ide_device_mbr_chs); | |
416 | qtest_add_func("hd-geo/ide/device/user/chs", test_ide_device_user_chs); | |
417 | qtest_add_func("hd-geo/ide/device/user/chst", test_ide_device_user_chst); | |
418 | ||
419 | ret = g_test_run(); | |
420 | ||
421 | for (i = 0; i < backend_last; i++) { | |
422 | unlink(img_file_name[i]); | |
423 | } | |
424 | ||
425 | return ret; | |
426 | } |