6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
30 #include "qemu-common.h"
32 #define TEST_IMAGE_SIZE 1440 * 1024
34 #define FLOPPY_BASE 0x3f0
53 CMD_RELATIVE_SEEK_OUT = 0x8f,
54 CMD_RELATIVE_SEEK_IN = 0xcf,
66 static char test_image[] = "/tmp/qtest.XXXXXX";
68 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
69 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
71 static uint8_t base = 0x70;
77 static void floppy_send(uint8_t byte)
81 msr = inb(FLOPPY_BASE + reg_msr);
82 assert_bit_set(msr, RQM);
83 assert_bit_clear(msr, DIO);
85 outb(FLOPPY_BASE + reg_fifo, byte);
88 static uint8_t floppy_recv(void)
92 msr = inb(FLOPPY_BASE + reg_msr);
93 assert_bit_set(msr, RQM | DIO);
95 return inb(FLOPPY_BASE + reg_fifo);
98 /* pcn: Present Cylinder Number */
99 static void ack_irq(uint8_t *pcn)
103 g_assert(get_irq(FLOPPY_IRQ));
104 floppy_send(CMD_SENSE_INT);
112 g_assert(!get_irq(FLOPPY_IRQ));
115 static uint8_t send_read_command(uint8_t cmd)
120 uint8_t sect_addr = 1;
121 uint8_t sect_size = 2;
132 floppy_send(head << 2 | drive);
133 g_assert(!get_irq(FLOPPY_IRQ));
136 floppy_send(sect_addr);
137 floppy_send(sect_size);
145 msr = inb(FLOPPY_BASE + reg_msr);
171 static uint8_t send_read_no_dma_command(int nb_sect, uint8_t expected_st0)
176 uint8_t sect_addr = 1;
177 uint8_t sect_size = 2;
178 uint8_t eot = nb_sect;
187 floppy_send(CMD_READ);
188 floppy_send(head << 2 | drive);
189 g_assert(!get_irq(FLOPPY_IRQ));
192 floppy_send(sect_addr);
193 floppy_send(sect_size);
201 msr = inb(FLOPPY_BASE + reg_msr);
202 if (msr == (BUSY | NONDMA | DIO | RQM)) {
213 for (i = 0; i < 512 * 2 * nb_sect; i++) {
214 msr = inb(FLOPPY_BASE + reg_msr);
215 assert_bit_set(msr, BUSY | RQM | DIO);
216 inb(FLOPPY_BASE + reg_fifo);
219 msr = inb(FLOPPY_BASE + reg_msr);
220 assert_bit_set(msr, BUSY | RQM | DIO);
221 g_assert(get_irq(FLOPPY_IRQ));
224 if (st0 != expected_st0) {
233 g_assert(get_irq(FLOPPY_IRQ));
236 /* Check that we're back in command phase */
237 msr = inb(FLOPPY_BASE + reg_msr);
238 assert_bit_clear(msr, BUSY | DIO);
239 assert_bit_set(msr, RQM);
240 g_assert(!get_irq(FLOPPY_IRQ));
245 static void send_seek(int cyl)
250 floppy_send(CMD_SEEK);
251 floppy_send(head << 2 | drive);
252 g_assert(!get_irq(FLOPPY_IRQ));
257 static uint8_t cmos_read(uint8_t reg)
260 return inb(base + 1);
263 static void test_cmos(void)
267 cmos = cmos_read(CMOS_FLOPPY);
268 g_assert(cmos == 0x40 || cmos == 0x50);
271 static void test_no_media_on_start(void)
275 /* Media changed bit must be set all time after start if there is
276 * no media in drive. */
277 dir = inb(FLOPPY_BASE + reg_dir);
278 assert_bit_set(dir, DSKCHG);
279 dir = inb(FLOPPY_BASE + reg_dir);
280 assert_bit_set(dir, DSKCHG);
282 dir = inb(FLOPPY_BASE + reg_dir);
283 assert_bit_set(dir, DSKCHG);
284 dir = inb(FLOPPY_BASE + reg_dir);
285 assert_bit_set(dir, DSKCHG);
288 static void test_read_without_media(void)
292 ret = send_read_command(CMD_READ);
296 static void test_media_insert(void)
300 /* Insert media in drive. DSKCHK should not be reset until a step pulse
302 qmp_discard_response("{'execute':'change', 'arguments':{"
303 " 'device':'floppy0', 'target': %s, 'arg': 'raw' }}",
306 dir = inb(FLOPPY_BASE + reg_dir);
307 assert_bit_set(dir, DSKCHG);
308 dir = inb(FLOPPY_BASE + reg_dir);
309 assert_bit_set(dir, DSKCHG);
312 dir = inb(FLOPPY_BASE + reg_dir);
313 assert_bit_set(dir, DSKCHG);
314 dir = inb(FLOPPY_BASE + reg_dir);
315 assert_bit_set(dir, DSKCHG);
317 /* Step to next track should clear DSKCHG bit. */
319 dir = inb(FLOPPY_BASE + reg_dir);
320 assert_bit_clear(dir, DSKCHG);
321 dir = inb(FLOPPY_BASE + reg_dir);
322 assert_bit_clear(dir, DSKCHG);
325 static void test_media_change(void)
331 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
333 qmp_discard_response("{'execute':'eject', 'arguments':{"
334 " 'device':'floppy0' }}");
336 dir = inb(FLOPPY_BASE + reg_dir);
337 assert_bit_set(dir, DSKCHG);
338 dir = inb(FLOPPY_BASE + reg_dir);
339 assert_bit_set(dir, DSKCHG);
342 dir = inb(FLOPPY_BASE + reg_dir);
343 assert_bit_set(dir, DSKCHG);
344 dir = inb(FLOPPY_BASE + reg_dir);
345 assert_bit_set(dir, DSKCHG);
348 dir = inb(FLOPPY_BASE + reg_dir);
349 assert_bit_set(dir, DSKCHG);
350 dir = inb(FLOPPY_BASE + reg_dir);
351 assert_bit_set(dir, DSKCHG);
354 static void test_sense_interrupt(void)
361 floppy_send(CMD_SENSE_INT);
363 g_assert(ret == 0x80);
365 floppy_send(CMD_SEEK);
366 floppy_send(head << 2 | drive);
367 g_assert(!get_irq(FLOPPY_IRQ));
370 floppy_send(CMD_SENSE_INT);
372 g_assert(ret == 0x20);
376 static void test_relative_seek(void)
383 /* Send seek to track 0 */
386 /* Send relative seek to increase track by 1 */
387 floppy_send(CMD_RELATIVE_SEEK_IN);
388 floppy_send(head << 2 | drive);
389 g_assert(!get_irq(FLOPPY_IRQ));
395 /* Send relative seek to decrease track by 1 */
396 floppy_send(CMD_RELATIVE_SEEK_OUT);
397 floppy_send(head << 2 | drive);
398 g_assert(!get_irq(FLOPPY_IRQ));
405 static void test_read_id(void)
413 /* Seek to track 0 and check with READ ID */
416 floppy_send(CMD_READ_ID);
417 g_assert(!get_irq(FLOPPY_IRQ));
418 floppy_send(head << 2 | drive);
420 msr = inb(FLOPPY_BASE + reg_msr);
421 if (!get_irq(FLOPPY_IRQ)) {
422 assert_bit_set(msr, BUSY);
423 assert_bit_clear(msr, RQM);
426 while (!get_irq(FLOPPY_IRQ)) {
427 /* qemu involves a timer with READ ID... */
428 clock_step(1000000000LL / 50);
431 msr = inb(FLOPPY_BASE + reg_msr);
432 assert_bit_set(msr, BUSY | RQM | DIO);
438 head = floppy_recv();
440 g_assert(get_irq(FLOPPY_IRQ));
442 g_assert(!get_irq(FLOPPY_IRQ));
444 g_assert_cmpint(cyl, ==, 0);
445 g_assert_cmpint(head, ==, 0);
446 g_assert_cmpint(st0, ==, head << 2);
448 /* Seek to track 8 on head 1 and check with READ ID */
452 floppy_send(CMD_SEEK);
453 floppy_send(head << 2 | drive);
454 g_assert(!get_irq(FLOPPY_IRQ));
456 g_assert(get_irq(FLOPPY_IRQ));
459 floppy_send(CMD_READ_ID);
460 g_assert(!get_irq(FLOPPY_IRQ));
461 floppy_send(head << 2 | drive);
463 msr = inb(FLOPPY_BASE + reg_msr);
464 if (!get_irq(FLOPPY_IRQ)) {
465 assert_bit_set(msr, BUSY);
466 assert_bit_clear(msr, RQM);
469 while (!get_irq(FLOPPY_IRQ)) {
470 /* qemu involves a timer with READ ID... */
471 clock_step(1000000000LL / 50);
474 msr = inb(FLOPPY_BASE + reg_msr);
475 assert_bit_set(msr, BUSY | RQM | DIO);
481 head = floppy_recv();
483 g_assert(get_irq(FLOPPY_IRQ));
485 g_assert(!get_irq(FLOPPY_IRQ));
487 g_assert_cmpint(cyl, ==, 8);
488 g_assert_cmpint(head, ==, 1);
489 g_assert_cmpint(st0, ==, head << 2);
492 static void test_read_no_dma_1(void)
496 outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
498 ret = send_read_no_dma_command(1, 0x04);
502 static void test_read_no_dma_18(void)
506 outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
508 ret = send_read_no_dma_command(18, 0x04);
512 static void test_read_no_dma_19(void)
516 outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
518 ret = send_read_no_dma_command(19, 0x20);
522 static void test_verify(void)
526 ret = send_read_command(CMD_VERIFY);
530 /* success if no crash or abort */
531 static void fuzz_registers(void)
535 for (i = 0; i < 1000; i++) {
538 reg = (uint8_t)g_test_rand_int_range(0, 8);
539 val = (uint8_t)g_test_rand_int_range(0, 256);
541 outb(FLOPPY_BASE + reg, val);
542 inb(FLOPPY_BASE + reg);
546 int main(int argc, char **argv)
548 const char *arch = qtest_get_arch();
552 /* Check architecture */
553 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
554 g_test_message("Skipping test for non-x86\n");
558 /* Create a temporary raw image */
559 fd = mkstemp(test_image);
561 ret = ftruncate(fd, TEST_IMAGE_SIZE);
566 g_test_init(&argc, &argv, NULL);
569 qtest_irq_intercept_in(global_qtest, "ioapic");
570 qtest_add_func("/fdc/cmos", test_cmos);
571 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start);
572 qtest_add_func("/fdc/read_without_media", test_read_without_media);
573 qtest_add_func("/fdc/media_change", test_media_change);
574 qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt);
575 qtest_add_func("/fdc/relative_seek", test_relative_seek);
576 qtest_add_func("/fdc/read_id", test_read_id);
577 qtest_add_func("/fdc/verify", test_verify);
578 qtest_add_func("/fdc/media_insert", test_media_insert);
579 qtest_add_func("/fdc/read_no_dma_1", test_read_no_dma_1);
580 qtest_add_func("/fdc/read_no_dma_18", test_read_no_dma_18);
581 qtest_add_func("/fdc/read_no_dma_19", test_read_no_dma_19);
582 qtest_add_func("/fdc/fuzz-registers", fuzz_registers);