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"
29 #include "qapi/qmp/qdict.h"
30 #include "qemu-common.h"
32 /* TODO actually test the results and get rid of this */
33 #define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__))
35 #define TEST_IMAGE_SIZE 1440 * 1024
37 #define FLOPPY_BASE 0x3f0
56 CMD_RELATIVE_SEEK_OUT = 0x8f,
57 CMD_RELATIVE_SEEK_IN = 0xcf,
69 static char test_image[] = "/tmp/qtest.XXXXXX";
71 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
72 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
74 static uint8_t base = 0x70;
80 static void floppy_send(uint8_t byte)
84 msr = inb(FLOPPY_BASE + reg_msr);
85 assert_bit_set(msr, RQM);
86 assert_bit_clear(msr, DIO);
88 outb(FLOPPY_BASE + reg_fifo, byte);
91 static uint8_t floppy_recv(void)
95 msr = inb(FLOPPY_BASE + reg_msr);
96 assert_bit_set(msr, RQM | DIO);
98 return inb(FLOPPY_BASE + reg_fifo);
101 /* pcn: Present Cylinder Number */
102 static void ack_irq(uint8_t *pcn)
106 g_assert(get_irq(FLOPPY_IRQ));
107 floppy_send(CMD_SENSE_INT);
115 g_assert(!get_irq(FLOPPY_IRQ));
118 static uint8_t send_read_command(uint8_t cmd)
123 uint8_t sect_addr = 1;
124 uint8_t sect_size = 2;
135 floppy_send(head << 2 | drive);
136 g_assert(!get_irq(FLOPPY_IRQ));
139 floppy_send(sect_addr);
140 floppy_send(sect_size);
148 msr = inb(FLOPPY_BASE + reg_msr);
174 static uint8_t send_read_no_dma_command(int nb_sect, uint8_t expected_st0)
179 uint8_t sect_addr = 1;
180 uint8_t sect_size = 2;
181 uint8_t eot = nb_sect;
190 floppy_send(CMD_READ);
191 floppy_send(head << 2 | drive);
192 g_assert(!get_irq(FLOPPY_IRQ));
195 floppy_send(sect_addr);
196 floppy_send(sect_size);
204 msr = inb(FLOPPY_BASE + reg_msr);
205 if (msr == (BUSY | NONDMA | DIO | RQM)) {
216 for (i = 0; i < 512 * 2 * nb_sect; i++) {
217 msr = inb(FLOPPY_BASE + reg_msr);
218 assert_bit_set(msr, BUSY | RQM | DIO);
219 inb(FLOPPY_BASE + reg_fifo);
222 msr = inb(FLOPPY_BASE + reg_msr);
223 assert_bit_set(msr, BUSY | RQM | DIO);
224 g_assert(get_irq(FLOPPY_IRQ));
227 if (st0 != expected_st0) {
236 g_assert(get_irq(FLOPPY_IRQ));
239 /* Check that we're back in command phase */
240 msr = inb(FLOPPY_BASE + reg_msr);
241 assert_bit_clear(msr, BUSY | DIO);
242 assert_bit_set(msr, RQM);
243 g_assert(!get_irq(FLOPPY_IRQ));
248 static void send_seek(int cyl)
253 floppy_send(CMD_SEEK);
254 floppy_send(head << 2 | drive);
255 g_assert(!get_irq(FLOPPY_IRQ));
260 static uint8_t cmos_read(uint8_t reg)
263 return inb(base + 1);
266 static void test_cmos(void)
270 cmos = cmos_read(CMOS_FLOPPY);
271 g_assert(cmos == 0x40 || cmos == 0x50);
274 static void test_no_media_on_start(void)
278 /* Media changed bit must be set all time after start if there is
279 * no media in drive. */
280 dir = inb(FLOPPY_BASE + reg_dir);
281 assert_bit_set(dir, DSKCHG);
282 dir = inb(FLOPPY_BASE + reg_dir);
283 assert_bit_set(dir, DSKCHG);
285 dir = inb(FLOPPY_BASE + reg_dir);
286 assert_bit_set(dir, DSKCHG);
287 dir = inb(FLOPPY_BASE + reg_dir);
288 assert_bit_set(dir, DSKCHG);
291 static void test_read_without_media(void)
295 ret = send_read_command(CMD_READ);
299 static void test_media_insert(void)
303 /* Insert media in drive. DSKCHK should not be reset until a step pulse
305 qmp_discard_response("{'execute':'blockdev-change-medium', 'arguments':{"
306 " 'id':'floppy0', 'filename': %s, 'format': 'raw' }}",
309 dir = inb(FLOPPY_BASE + reg_dir);
310 assert_bit_set(dir, DSKCHG);
311 dir = inb(FLOPPY_BASE + reg_dir);
312 assert_bit_set(dir, DSKCHG);
315 dir = inb(FLOPPY_BASE + reg_dir);
316 assert_bit_set(dir, DSKCHG);
317 dir = inb(FLOPPY_BASE + reg_dir);
318 assert_bit_set(dir, DSKCHG);
320 /* Step to next track should clear DSKCHG bit. */
322 dir = inb(FLOPPY_BASE + reg_dir);
323 assert_bit_clear(dir, DSKCHG);
324 dir = inb(FLOPPY_BASE + reg_dir);
325 assert_bit_clear(dir, DSKCHG);
328 static void test_media_change(void)
334 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
336 qmp_discard_response("{'execute':'eject', 'arguments':{"
337 " 'id':'floppy0' }}");
339 dir = inb(FLOPPY_BASE + reg_dir);
340 assert_bit_set(dir, DSKCHG);
341 dir = inb(FLOPPY_BASE + reg_dir);
342 assert_bit_set(dir, DSKCHG);
345 dir = inb(FLOPPY_BASE + reg_dir);
346 assert_bit_set(dir, DSKCHG);
347 dir = inb(FLOPPY_BASE + reg_dir);
348 assert_bit_set(dir, DSKCHG);
351 dir = inb(FLOPPY_BASE + reg_dir);
352 assert_bit_set(dir, DSKCHG);
353 dir = inb(FLOPPY_BASE + reg_dir);
354 assert_bit_set(dir, DSKCHG);
357 static void test_sense_interrupt(void)
364 floppy_send(CMD_SENSE_INT);
366 g_assert(ret == 0x80);
368 floppy_send(CMD_SEEK);
369 floppy_send(head << 2 | drive);
370 g_assert(!get_irq(FLOPPY_IRQ));
373 floppy_send(CMD_SENSE_INT);
375 g_assert(ret == 0x20);
379 static void test_relative_seek(void)
386 /* Send seek to track 0 */
389 /* Send relative seek to increase track by 1 */
390 floppy_send(CMD_RELATIVE_SEEK_IN);
391 floppy_send(head << 2 | drive);
392 g_assert(!get_irq(FLOPPY_IRQ));
398 /* Send relative seek to decrease track by 1 */
399 floppy_send(CMD_RELATIVE_SEEK_OUT);
400 floppy_send(head << 2 | drive);
401 g_assert(!get_irq(FLOPPY_IRQ));
408 static void test_read_id(void)
416 /* Seek to track 0 and check with READ ID */
419 floppy_send(CMD_READ_ID);
420 g_assert(!get_irq(FLOPPY_IRQ));
421 floppy_send(head << 2 | drive);
423 msr = inb(FLOPPY_BASE + reg_msr);
424 if (!get_irq(FLOPPY_IRQ)) {
425 assert_bit_set(msr, BUSY);
426 assert_bit_clear(msr, RQM);
429 while (!get_irq(FLOPPY_IRQ)) {
430 /* qemu involves a timer with READ ID... */
431 clock_step(1000000000LL / 50);
434 msr = inb(FLOPPY_BASE + reg_msr);
435 assert_bit_set(msr, BUSY | RQM | DIO);
441 head = floppy_recv();
443 g_assert(get_irq(FLOPPY_IRQ));
445 g_assert(!get_irq(FLOPPY_IRQ));
447 g_assert_cmpint(cyl, ==, 0);
448 g_assert_cmpint(head, ==, 0);
449 g_assert_cmpint(st0, ==, head << 2);
451 /* Seek to track 8 on head 1 and check with READ ID */
455 floppy_send(CMD_SEEK);
456 floppy_send(head << 2 | drive);
457 g_assert(!get_irq(FLOPPY_IRQ));
459 g_assert(get_irq(FLOPPY_IRQ));
462 floppy_send(CMD_READ_ID);
463 g_assert(!get_irq(FLOPPY_IRQ));
464 floppy_send(head << 2 | drive);
466 msr = inb(FLOPPY_BASE + reg_msr);
467 if (!get_irq(FLOPPY_IRQ)) {
468 assert_bit_set(msr, BUSY);
469 assert_bit_clear(msr, RQM);
472 while (!get_irq(FLOPPY_IRQ)) {
473 /* qemu involves a timer with READ ID... */
474 clock_step(1000000000LL / 50);
477 msr = inb(FLOPPY_BASE + reg_msr);
478 assert_bit_set(msr, BUSY | RQM | DIO);
484 head = floppy_recv();
486 g_assert(get_irq(FLOPPY_IRQ));
488 g_assert(!get_irq(FLOPPY_IRQ));
490 g_assert_cmpint(cyl, ==, 8);
491 g_assert_cmpint(head, ==, 1);
492 g_assert_cmpint(st0, ==, head << 2);
495 static void test_read_no_dma_1(void)
499 outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
501 ret = send_read_no_dma_command(1, 0x04);
505 static void test_read_no_dma_18(void)
509 outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
511 ret = send_read_no_dma_command(18, 0x04);
515 static void test_read_no_dma_19(void)
519 outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
521 ret = send_read_no_dma_command(19, 0x20);
525 static void test_verify(void)
529 ret = send_read_command(CMD_VERIFY);
533 /* success if no crash or abort */
534 static void fuzz_registers(void)
538 for (i = 0; i < 1000; i++) {
541 reg = (uint8_t)g_test_rand_int_range(0, 8);
542 val = (uint8_t)g_test_rand_int_range(0, 256);
544 outb(FLOPPY_BASE + reg, val);
545 inb(FLOPPY_BASE + reg);
549 int main(int argc, char **argv)
554 /* Create a temporary raw image */
555 fd = mkstemp(test_image);
557 ret = ftruncate(fd, TEST_IMAGE_SIZE);
562 g_test_init(&argc, &argv, NULL);
564 qtest_start("-device floppy,id=floppy0");
565 qtest_irq_intercept_in(global_qtest, "ioapic");
566 qtest_add_func("/fdc/cmos", test_cmos);
567 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start);
568 qtest_add_func("/fdc/read_without_media", test_read_without_media);
569 qtest_add_func("/fdc/media_change", test_media_change);
570 qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt);
571 qtest_add_func("/fdc/relative_seek", test_relative_seek);
572 qtest_add_func("/fdc/read_id", test_read_id);
573 qtest_add_func("/fdc/verify", test_verify);
574 qtest_add_func("/fdc/media_insert", test_media_insert);
575 qtest_add_func("/fdc/read_no_dma_1", test_read_no_dma_1);
576 qtest_add_func("/fdc/read_no_dma_18", test_read_no_dma_18);
577 qtest_add_func("/fdc/read_no_dma_19", test_read_no_dma_19);
578 qtest_add_func("/fdc/fuzz-registers", fuzz_registers);