2 * Hard disk geometry test cases.
4 * Copyright (c) 2012 Red Hat Inc.
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.
18 #include "qemu/osdep.h"
20 #include "qemu-common.h"
23 static char *create_test_img(int secs)
25 char *template = strdup("/tmp/qtest.XXXXXX");
28 fd = mkstemp(template);
30 ret = ftruncate(fd, (off_t)secs * 512);
37 int cyls, heads, secs, trans;
41 mbr_blank, mbr_lba, mbr_chs,
46 /* order is relevant */
47 backend_small, backend_large, backend_empty,
51 static const int img_secs[backend_last] = {
52 [backend_small] = 61440,
53 [backend_large] = 8388608,
57 static const CHST hd_chst[backend_last][mbr_last] = {
59 [mbr_blank] = { 60, 16, 63, 0 },
60 [mbr_lba] = { 60, 16, 63, 2 },
61 [mbr_chs] = { 60, 16, 63, 0 }
64 [mbr_blank] = { 8322, 16, 63, 1 },
65 [mbr_lba] = { 8322, 16, 63, 1 },
66 [mbr_chs] = { 8322, 16, 63, 0 }
70 static const char *img_file_name[backend_last];
72 static const CHST *cur_ide[4];
74 static bool is_hd(const CHST *expected_chst)
76 return expected_chst && expected_chst->cyls;
79 static void test_cmos_byte(int reg, int expected)
81 enum { cmos_base = 0x70 };
84 outb(cmos_base + 0, reg);
85 actual = inb(cmos_base + 1);
86 g_assert(actual == expected);
89 static void test_cmos_bytes(int reg0, int n, uint8_t expected[])
93 for (i = 0; i < 9; i++) {
94 test_cmos_byte(reg0 + i, expected[i]);
98 static void test_cmos_disk_data(void)
101 (is_hd(cur_ide[0]) ? 0xf0 : 0) |
102 (is_hd(cur_ide[1]) ? 0x0f : 0));
105 static void test_cmos_drive_cyl(int reg0, const CHST *expected_chst)
107 if (is_hd(expected_chst)) {
108 int c = expected_chst->cyls;
109 int h = expected_chst->heads;
110 int s = expected_chst->secs;
111 uint8_t expected_bytes[9] = {
112 c & 0xff, c >> 8, h, 0xff, 0xff, 0xc0 | ((h > 8) << 3),
115 test_cmos_bytes(reg0, 9, expected_bytes);
119 for (i = 0; i < 9; i++) {
120 test_cmos_byte(reg0 + i, 0);
125 static void test_cmos_drive1(void)
127 test_cmos_byte(0x19, is_hd(cur_ide[0]) ? 47 : 0);
128 test_cmos_drive_cyl(0x1b, cur_ide[0]);
131 static void test_cmos_drive2(void)
133 test_cmos_byte(0x1a, is_hd(cur_ide[1]) ? 47 : 0);
134 test_cmos_drive_cyl(0x24, cur_ide[1]);
137 static void test_cmos_disktransflag(void)
142 for (i = 0; i < ARRAY_SIZE(cur_ide); i++) {
143 if (is_hd(cur_ide[i])) {
144 val |= cur_ide[i]->trans << (2 * i);
147 test_cmos_byte(0x39, val);
150 static void test_cmos(void)
152 test_cmos_disk_data();
155 test_cmos_disktransflag();
158 static int append_arg(int argc, char *argv[], int argv_sz, char *arg)
160 g_assert(argc + 1 < argv_sz);
166 static int setup_common(char *argv[], int argv_sz)
168 memset(cur_ide, 0, sizeof(cur_ide));
169 return append_arg(0, argv, argv_sz,
170 g_strdup("-nodefaults"));
173 static void setup_mbr(int img_idx, MBRcontents mbr)
175 static const uint8_t part_lba[16] = {
176 /* chs 0,1,1 (lba 63) to chs 0,127,63 (8001 sectors) */
177 0x80, 1, 1, 0, 6, 127, 63, 0, 63, 0, 0, 0, 0x41, 0x1F, 0, 0,
179 static const uint8_t part_chs[16] = {
180 /* chs 0,1,1 (lba 63) to chs 7,15,63 (8001 sectors) */
181 0x80, 1, 1, 0, 6, 15, 63, 7, 63, 0, 0, 0, 0x41, 0x1F, 0, 0,
186 memset(buf, 0, sizeof(buf));
188 if (mbr != mbr_blank) {
191 memcpy(buf + 0x1BE, mbr == mbr_lba ? part_lba : part_chs, 16);
194 fd = open(img_file_name[img_idx], O_WRONLY);
196 ret = write(fd, buf, sizeof(buf));
197 g_assert(ret == sizeof(buf));
201 static int setup_ide(int argc, char *argv[], int argv_sz,
202 int ide_idx, const char *dev, int img_idx,
203 MBRcontents mbr, const char *opts)
207 s1 = g_strdup_printf("-drive id=drive%d,if=%s",
208 ide_idx, dev ? "none" : "ide");
209 s2 = dev ? g_strdup("") : g_strdup_printf(",index=%d", ide_idx);
211 if (img_secs[img_idx] >= 0) {
212 setup_mbr(img_idx, mbr);
213 s3 = g_strdup_printf(",format=raw,file=%s", img_file_name[img_idx]);
215 s3 = g_strdup(",media=cdrom");
217 argc = append_arg(argc, argv, argv_sz,
218 g_strdup_printf("%s%s%s%s", s1, s2, s3, opts));
224 argc = append_arg(argc, argv, argv_sz,
225 g_strdup_printf("-device %s,drive=drive%d,"
226 "bus=ide.%d,unit=%d",
228 ide_idx / 2, ide_idx % 2));
234 * Test case: no IDE devices
236 static void test_ide_none(void)
240 setup_common(argv, ARRAY_SIZE(argv));
241 qtest_start(g_strjoinv(" ", argv));
246 static void test_ide_mbr(bool use_device, MBRcontents mbr)
253 argc = setup_common(argv, ARRAY_SIZE(argv));
254 for (i = 0; i < backend_last; i++) {
255 cur_ide[i] = &hd_chst[i][mbr];
256 dev = use_device ? (is_hd(cur_ide[i]) ? "ide-hd" : "ide-cd") : NULL;
257 argc = setup_ide(argc, argv, ARRAY_SIZE(argv), i, dev, i, mbr, "");
259 qtest_start(g_strjoinv(" ", argv));
265 * Test case: IDE devices (if=ide) with blank MBRs
267 static void test_ide_drive_mbr_blank(void)
269 test_ide_mbr(false, mbr_blank);
273 * Test case: IDE devices (if=ide) with MBRs indicating LBA is in use
275 static void test_ide_drive_mbr_lba(void)
277 test_ide_mbr(false, mbr_lba);
281 * Test case: IDE devices (if=ide) with MBRs indicating CHS is in use
283 static void test_ide_drive_mbr_chs(void)
285 test_ide_mbr(false, mbr_chs);
289 * Test case: IDE devices (if=none) with blank MBRs
291 static void test_ide_device_mbr_blank(void)
293 test_ide_mbr(true, mbr_blank);
297 * Test case: IDE devices (if=none) with MBRs indicating LBA is in use
299 static void test_ide_device_mbr_lba(void)
301 test_ide_mbr(true, mbr_lba);
305 * Test case: IDE devices (if=none) with MBRs indicating CHS is in use
307 static void test_ide_device_mbr_chs(void)
309 test_ide_mbr(true, mbr_chs);
312 static void test_ide_drive_user(const char *dev, bool trans)
314 char *argv[256], *opts;
316 int secs = img_secs[backend_small];
317 const CHST expected_chst = { secs / (4 * 32) , 4, 32, trans };
319 argc = setup_common(argv, ARRAY_SIZE(argv));
320 opts = g_strdup_printf("%s,%s%scyls=%d,heads=%d,secs=%d",
322 trans && dev ? "bios-chs-" : "",
323 trans ? "trans=lba," : "",
324 expected_chst.cyls, expected_chst.heads,
326 cur_ide[0] = &expected_chst;
327 argc = setup_ide(argc, argv, ARRAY_SIZE(argv),
328 0, dev ? opts : NULL, backend_small, mbr_chs,
331 qtest_start(g_strjoinv(" ", argv));
337 * Test case: IDE device (if=ide) with explicit CHS
339 static void test_ide_drive_user_chs(void)
341 test_ide_drive_user(NULL, false);
345 * Test case: IDE device (if=ide) with explicit CHS and translation
347 static void test_ide_drive_user_chst(void)
349 test_ide_drive_user(NULL, true);
353 * Test case: IDE device (if=none) with explicit CHS
355 static void test_ide_device_user_chs(void)
357 test_ide_drive_user("ide-hd", false);
361 * Test case: IDE device (if=none) with explicit CHS and translation
363 static void test_ide_device_user_chst(void)
365 test_ide_drive_user("ide-hd", true);
369 * Test case: IDE devices (if=ide), but use index=0 for CD-ROM
371 static void test_ide_drive_cd_0(void)
377 argc = setup_common(argv, ARRAY_SIZE(argv));
378 for (i = 0; i <= backend_empty; i++) {
379 ide_idx = backend_empty - i;
380 cur_ide[ide_idx] = &hd_chst[i][mbr_blank];
381 argc = setup_ide(argc, argv, ARRAY_SIZE(argv),
382 ide_idx, NULL, i, mbr_blank, "");
384 qtest_start(g_strjoinv(" ", argv));
389 int main(int argc, char **argv)
394 g_test_init(&argc, &argv, NULL);
396 for (i = 0; i < backend_last; i++) {
397 if (img_secs[i] >= 0) {
398 img_file_name[i] = create_test_img(img_secs[i]);
400 img_file_name[i] = NULL;
404 qtest_add_func("hd-geo/ide/none", test_ide_none);
405 qtest_add_func("hd-geo/ide/drive/mbr/blank", test_ide_drive_mbr_blank);
406 qtest_add_func("hd-geo/ide/drive/mbr/lba", test_ide_drive_mbr_lba);
407 qtest_add_func("hd-geo/ide/drive/mbr/chs", test_ide_drive_mbr_chs);
408 qtest_add_func("hd-geo/ide/drive/user/chs", test_ide_drive_user_chs);
409 qtest_add_func("hd-geo/ide/drive/user/chst", test_ide_drive_user_chst);
410 qtest_add_func("hd-geo/ide/drive/cd_0", test_ide_drive_cd_0);
411 qtest_add_func("hd-geo/ide/device/mbr/blank", test_ide_device_mbr_blank);
412 qtest_add_func("hd-geo/ide/device/mbr/lba", test_ide_device_mbr_lba);
413 qtest_add_func("hd-geo/ide/device/mbr/chs", test_ide_device_mbr_chs);
414 qtest_add_func("hd-geo/ide/device/user/chs", test_ide_device_user_chs);
415 qtest_add_func("hd-geo/ide/device/user/chst", test_ide_device_user_chst);
419 for (i = 0; i < backend_last; i++) {
420 unlink(img_file_name[i]);