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