2 * Hard disk geometry test cases.
4 * Copyright (c) 2012 Red Hat Inc.
7 * Markus Armbruster <armbru@redhat.com>,
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.
14 * Covers only IDE and tests only CMOS contents. Better than nothing.
15 * Improvements welcome.
22 #include "qemu-common.h"
25 static char *create_test_img(int secs
)
27 char *template = strdup("/tmp/qtest.XXXXXX");
30 fd
= mkstemp(template);
32 ret
= ftruncate(fd
, (off_t
)secs
* 512);
39 int cyls
, heads
, secs
, trans
;
43 mbr_blank
, mbr_lba
, mbr_chs
,
48 /* order is relevant */
49 backend_small
, backend_large
, backend_empty
,
53 static const int img_secs
[backend_last
] = {
54 [backend_small
] = 61440,
55 [backend_large
] = 8388608,
59 static const CHST hd_chst
[backend_last
][mbr_last
] = {
61 [mbr_blank
] = { 60, 16, 63, 0 },
62 [mbr_lba
] = { 60, 16, 63, 2 },
63 [mbr_chs
] = { 60, 16, 63, 0 }
66 [mbr_blank
] = { 8322, 16, 63, 1 },
67 [mbr_lba
] = { 8322, 16, 63, 1 },
68 [mbr_chs
] = { 8322, 16, 63, 0 }
72 static const char *img_file_name
[backend_last
];
74 static const CHST
*cur_ide
[4];
76 static bool is_hd(const CHST
*expected_chst
)
78 return expected_chst
&& expected_chst
->cyls
;
81 static void test_cmos_byte(int reg
, int expected
)
83 enum { cmos_base
= 0x70 };
86 outb(cmos_base
+ 0, reg
);
87 actual
= inb(cmos_base
+ 1);
88 g_assert(actual
== expected
);
91 static void test_cmos_bytes(int reg0
, int n
, uint8_t expected
[])
95 for (i
= 0; i
< 9; i
++) {
96 test_cmos_byte(reg0
+ i
, expected
[i
]);
100 static void test_cmos_disk_data(void)
103 (is_hd(cur_ide
[0]) ? 0xf0 : 0) |
104 (is_hd(cur_ide
[1]) ? 0x0f : 0));
107 static void test_cmos_drive_cyl(int reg0
, const CHST
*expected_chst
)
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),
117 test_cmos_bytes(reg0
, 9, expected_bytes
);
121 for (i
= 0; i
< 9; i
++) {
122 test_cmos_byte(reg0
+ i
, 0);
127 static void test_cmos_drive1(void)
129 test_cmos_byte(0x19, is_hd(cur_ide
[0]) ? 47 : 0);
130 test_cmos_drive_cyl(0x1b, cur_ide
[0]);
133 static void test_cmos_drive2(void)
135 test_cmos_byte(0x1a, is_hd(cur_ide
[1]) ? 47 : 0);
136 test_cmos_drive_cyl(0x24, cur_ide
[1]);
139 static void test_cmos_disktransflag(void)
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
);
149 test_cmos_byte(0x39, val
);
152 static void test_cmos(void)
154 test_cmos_disk_data();
157 test_cmos_disktransflag();
160 static int append_arg(int argc
, char *argv
[], int argv_sz
, char *arg
)
162 g_assert(argc
+ 1 < argv_sz
);
168 static int setup_common(char *argv
[], int argv_sz
)
170 memset(cur_ide
, 0, sizeof(cur_ide
));
171 return append_arg(0, argv
, argv_sz
,
172 g_strdup("-nodefaults"));
175 static void setup_mbr(int img_idx
, MBRcontents mbr
)
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,
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,
188 memset(buf
, 0, sizeof(buf
));
190 if (mbr
!= mbr_blank
) {
193 memcpy(buf
+ 0x1BE, mbr
== mbr_lba
? part_lba
: part_chs
, 16);
196 fd
= open(img_file_name
[img_idx
], O_WRONLY
);
198 ret
= write(fd
, buf
, sizeof(buf
));
199 g_assert(ret
== sizeof(buf
));
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
)
209 s1
= g_strdup_printf("-drive id=drive%d,if=%s,format=raw",
210 ide_idx
, dev
? "none" : "ide");
211 s2
= dev
? g_strdup("") : g_strdup_printf(",index=%d", ide_idx
);
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
]);
217 s3
= g_strdup(",media=cdrom");
219 argc
= append_arg(argc
, argv
, argv_sz
,
220 g_strdup_printf("%s%s%s%s", s1
, s2
, s3
, opts
));
226 argc
= append_arg(argc
, argv
, argv_sz
,
227 g_strdup_printf("-device %s,drive=drive%d,"
228 "bus=ide.%d,unit=%d",
230 ide_idx
/ 2, ide_idx
% 2));
236 * Test case: no IDE devices
238 static void test_ide_none(void)
242 setup_common(argv
, ARRAY_SIZE(argv
));
243 qtest_start(g_strjoinv(" ", argv
));
248 static void test_ide_mbr(bool use_device
, MBRcontents mbr
)
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
, "");
261 qtest_start(g_strjoinv(" ", argv
));
267 * Test case: IDE devices (if=ide) with blank MBRs
269 static void test_ide_drive_mbr_blank(void)
271 test_ide_mbr(false, mbr_blank
);
275 * Test case: IDE devices (if=ide) with MBRs indicating LBA is in use
277 static void test_ide_drive_mbr_lba(void)
279 test_ide_mbr(false, mbr_lba
);
283 * Test case: IDE devices (if=ide) with MBRs indicating CHS is in use
285 static void test_ide_drive_mbr_chs(void)
287 test_ide_mbr(false, mbr_chs
);
291 * Test case: IDE devices (if=none) with blank MBRs
293 static void test_ide_device_mbr_blank(void)
295 test_ide_mbr(true, mbr_blank
);
299 * Test case: IDE devices (if=none) with MBRs indicating LBA is in use
301 static void test_ide_device_mbr_lba(void)
303 test_ide_mbr(true, mbr_lba
);
307 * Test case: IDE devices (if=none) with MBRs indicating CHS is in use
309 static void test_ide_device_mbr_chs(void)
311 test_ide_mbr(true, mbr_chs
);
314 static void test_ide_drive_user(const char *dev
, bool trans
)
316 char *argv
[256], *opts
;
318 int secs
= img_secs
[backend_small
];
319 const CHST expected_chst
= { secs
/ (4 * 32) , 4, 32, trans
};
321 argc
= setup_common(argv
, ARRAY_SIZE(argv
));
322 opts
= g_strdup_printf("%s,%s%scyls=%d,heads=%d,secs=%d",
324 trans
&& dev
? "bios-chs-" : "",
325 trans
? "trans=lba," : "",
326 expected_chst
.cyls
, expected_chst
.heads
,
328 cur_ide
[0] = &expected_chst
;
329 argc
= setup_ide(argc
, argv
, ARRAY_SIZE(argv
),
330 0, dev
? opts
: NULL
, backend_small
, mbr_chs
,
333 qtest_start(g_strjoinv(" ", argv
));
339 * Test case: IDE device (if=ide) with explicit CHS
341 static void test_ide_drive_user_chs(void)
343 test_ide_drive_user(NULL
, false);
347 * Test case: IDE device (if=ide) with explicit CHS and translation
349 static void test_ide_drive_user_chst(void)
351 test_ide_drive_user(NULL
, true);
355 * Test case: IDE device (if=none) with explicit CHS
357 static void test_ide_device_user_chs(void)
359 test_ide_drive_user("ide-hd", false);
363 * Test case: IDE device (if=none) with explicit CHS and translation
365 static void test_ide_device_user_chst(void)
367 test_ide_drive_user("ide-hd", true);
371 * Test case: IDE devices (if=ide), but use index=0 for CD-ROM
373 static void test_ide_drive_cd_0(void)
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
, "");
386 qtest_start(g_strjoinv(" ", argv
));
391 int main(int argc
, char **argv
)
396 g_test_init(&argc
, &argv
, NULL
);
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
]);
402 img_file_name
[i
] = NULL
;
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
);
412 qtest_add_func("hd-geo/ide/drive/cd_0", test_ide_drive_cd_0
);
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
);
421 for (i
= 0; i
< backend_last
; i
++) {
422 unlink(img_file_name
[i
]);