4 * Copyright (c) 2012 Kevin Wolf <kwolf@redhat.com>
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 "qemu-common.h"
31 #define TEST_IMAGE_SIZE 1440 * 1024
33 #define FLOPPY_BASE 0x3f0
52 CMD_RELATIVE_SEEK_OUT
= 0x8f,
53 CMD_RELATIVE_SEEK_IN
= 0xcf,
65 static char test_image
[] = "/tmp/qtest.XXXXXX";
67 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
68 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
70 static uint8_t base
= 0x70;
76 static void floppy_send(uint8_t byte
)
80 msr
= inb(FLOPPY_BASE
+ reg_msr
);
81 assert_bit_set(msr
, RQM
);
82 assert_bit_clear(msr
, DIO
);
84 outb(FLOPPY_BASE
+ reg_fifo
, byte
);
87 static uint8_t floppy_recv(void)
91 msr
= inb(FLOPPY_BASE
+ reg_msr
);
92 assert_bit_set(msr
, RQM
| DIO
);
94 return inb(FLOPPY_BASE
+ reg_fifo
);
97 /* pcn: Present Cylinder Number */
98 static void ack_irq(uint8_t *pcn
)
102 g_assert(get_irq(FLOPPY_IRQ
));
103 floppy_send(CMD_SENSE_INT
);
111 g_assert(!get_irq(FLOPPY_IRQ
));
114 static uint8_t send_read_command(uint8_t cmd
)
119 uint8_t sect_addr
= 1;
120 uint8_t sect_size
= 2;
131 floppy_send(head
<< 2 | drive
);
132 g_assert(!get_irq(FLOPPY_IRQ
));
135 floppy_send(sect_addr
);
136 floppy_send(sect_size
);
144 msr
= inb(FLOPPY_BASE
+ reg_msr
);
170 static uint8_t send_read_no_dma_command(int nb_sect
, uint8_t expected_st0
)
175 uint8_t sect_addr
= 1;
176 uint8_t sect_size
= 2;
177 uint8_t eot
= nb_sect
;
186 floppy_send(CMD_READ
);
187 floppy_send(head
<< 2 | drive
);
188 g_assert(!get_irq(FLOPPY_IRQ
));
191 floppy_send(sect_addr
);
192 floppy_send(sect_size
);
200 msr
= inb(FLOPPY_BASE
+ reg_msr
);
201 if (msr
== (BUSY
| NONDMA
| DIO
| RQM
)) {
212 for (i
= 0; i
< 512 * 2 * nb_sect
; i
++) {
213 msr
= inb(FLOPPY_BASE
+ reg_msr
);
214 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
215 inb(FLOPPY_BASE
+ reg_fifo
);
218 msr
= inb(FLOPPY_BASE
+ reg_msr
);
219 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
220 g_assert(get_irq(FLOPPY_IRQ
));
223 if (st0
!= expected_st0
) {
232 g_assert(get_irq(FLOPPY_IRQ
));
235 /* Check that we're back in command phase */
236 msr
= inb(FLOPPY_BASE
+ reg_msr
);
237 assert_bit_clear(msr
, BUSY
| DIO
);
238 assert_bit_set(msr
, RQM
);
239 g_assert(!get_irq(FLOPPY_IRQ
));
244 static void send_seek(int cyl
)
249 floppy_send(CMD_SEEK
);
250 floppy_send(head
<< 2 | drive
);
251 g_assert(!get_irq(FLOPPY_IRQ
));
256 static uint8_t cmos_read(uint8_t reg
)
259 return inb(base
+ 1);
262 static void test_cmos(void)
266 cmos
= cmos_read(CMOS_FLOPPY
);
267 g_assert(cmos
== 0x40 || cmos
== 0x50);
270 static void test_no_media_on_start(void)
274 /* Media changed bit must be set all time after start if there is
275 * no media in drive. */
276 dir
= inb(FLOPPY_BASE
+ reg_dir
);
277 assert_bit_set(dir
, DSKCHG
);
278 dir
= inb(FLOPPY_BASE
+ reg_dir
);
279 assert_bit_set(dir
, DSKCHG
);
281 dir
= inb(FLOPPY_BASE
+ reg_dir
);
282 assert_bit_set(dir
, DSKCHG
);
283 dir
= inb(FLOPPY_BASE
+ reg_dir
);
284 assert_bit_set(dir
, DSKCHG
);
287 static void test_read_without_media(void)
291 ret
= send_read_command(CMD_READ
);
295 static void test_media_insert(void)
299 /* Insert media in drive. DSKCHK should not be reset until a step pulse
301 qmp_discard_response("{'execute':'blockdev-change-medium', 'arguments':{"
302 " 'id':'floppy0', 'filename': %s, 'format': 'raw' }}",
305 dir
= inb(FLOPPY_BASE
+ reg_dir
);
306 assert_bit_set(dir
, DSKCHG
);
307 dir
= inb(FLOPPY_BASE
+ reg_dir
);
308 assert_bit_set(dir
, DSKCHG
);
311 dir
= inb(FLOPPY_BASE
+ reg_dir
);
312 assert_bit_set(dir
, DSKCHG
);
313 dir
= inb(FLOPPY_BASE
+ reg_dir
);
314 assert_bit_set(dir
, DSKCHG
);
316 /* Step to next track should clear DSKCHG bit. */
318 dir
= inb(FLOPPY_BASE
+ reg_dir
);
319 assert_bit_clear(dir
, DSKCHG
);
320 dir
= inb(FLOPPY_BASE
+ reg_dir
);
321 assert_bit_clear(dir
, DSKCHG
);
324 static void test_media_change(void)
330 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
332 qmp_discard_response("{'execute':'eject', 'arguments':{"
333 " 'id':'floppy0' }}");
335 dir
= inb(FLOPPY_BASE
+ reg_dir
);
336 assert_bit_set(dir
, DSKCHG
);
337 dir
= inb(FLOPPY_BASE
+ reg_dir
);
338 assert_bit_set(dir
, DSKCHG
);
341 dir
= inb(FLOPPY_BASE
+ reg_dir
);
342 assert_bit_set(dir
, DSKCHG
);
343 dir
= inb(FLOPPY_BASE
+ reg_dir
);
344 assert_bit_set(dir
, DSKCHG
);
347 dir
= inb(FLOPPY_BASE
+ reg_dir
);
348 assert_bit_set(dir
, DSKCHG
);
349 dir
= inb(FLOPPY_BASE
+ reg_dir
);
350 assert_bit_set(dir
, DSKCHG
);
353 static void test_sense_interrupt(void)
360 floppy_send(CMD_SENSE_INT
);
362 g_assert(ret
== 0x80);
364 floppy_send(CMD_SEEK
);
365 floppy_send(head
<< 2 | drive
);
366 g_assert(!get_irq(FLOPPY_IRQ
));
369 floppy_send(CMD_SENSE_INT
);
371 g_assert(ret
== 0x20);
375 static void test_relative_seek(void)
382 /* Send seek to track 0 */
385 /* Send relative seek to increase track by 1 */
386 floppy_send(CMD_RELATIVE_SEEK_IN
);
387 floppy_send(head
<< 2 | drive
);
388 g_assert(!get_irq(FLOPPY_IRQ
));
394 /* Send relative seek to decrease track by 1 */
395 floppy_send(CMD_RELATIVE_SEEK_OUT
);
396 floppy_send(head
<< 2 | drive
);
397 g_assert(!get_irq(FLOPPY_IRQ
));
404 static void test_read_id(void)
412 /* Seek to track 0 and check with READ ID */
415 floppy_send(CMD_READ_ID
);
416 g_assert(!get_irq(FLOPPY_IRQ
));
417 floppy_send(head
<< 2 | drive
);
419 msr
= inb(FLOPPY_BASE
+ reg_msr
);
420 if (!get_irq(FLOPPY_IRQ
)) {
421 assert_bit_set(msr
, BUSY
);
422 assert_bit_clear(msr
, RQM
);
425 while (!get_irq(FLOPPY_IRQ
)) {
426 /* qemu involves a timer with READ ID... */
427 clock_step(1000000000LL / 50);
430 msr
= inb(FLOPPY_BASE
+ reg_msr
);
431 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
437 head
= floppy_recv();
439 g_assert(get_irq(FLOPPY_IRQ
));
441 g_assert(!get_irq(FLOPPY_IRQ
));
443 g_assert_cmpint(cyl
, ==, 0);
444 g_assert_cmpint(head
, ==, 0);
445 g_assert_cmpint(st0
, ==, head
<< 2);
447 /* Seek to track 8 on head 1 and check with READ ID */
451 floppy_send(CMD_SEEK
);
452 floppy_send(head
<< 2 | drive
);
453 g_assert(!get_irq(FLOPPY_IRQ
));
455 g_assert(get_irq(FLOPPY_IRQ
));
458 floppy_send(CMD_READ_ID
);
459 g_assert(!get_irq(FLOPPY_IRQ
));
460 floppy_send(head
<< 2 | drive
);
462 msr
= inb(FLOPPY_BASE
+ reg_msr
);
463 if (!get_irq(FLOPPY_IRQ
)) {
464 assert_bit_set(msr
, BUSY
);
465 assert_bit_clear(msr
, RQM
);
468 while (!get_irq(FLOPPY_IRQ
)) {
469 /* qemu involves a timer with READ ID... */
470 clock_step(1000000000LL / 50);
473 msr
= inb(FLOPPY_BASE
+ reg_msr
);
474 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
480 head
= floppy_recv();
482 g_assert(get_irq(FLOPPY_IRQ
));
484 g_assert(!get_irq(FLOPPY_IRQ
));
486 g_assert_cmpint(cyl
, ==, 8);
487 g_assert_cmpint(head
, ==, 1);
488 g_assert_cmpint(st0
, ==, head
<< 2);
491 static void test_read_no_dma_1(void)
495 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
497 ret
= send_read_no_dma_command(1, 0x04);
501 static void test_read_no_dma_18(void)
505 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
507 ret
= send_read_no_dma_command(18, 0x04);
511 static void test_read_no_dma_19(void)
515 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
517 ret
= send_read_no_dma_command(19, 0x20);
521 static void test_verify(void)
525 ret
= send_read_command(CMD_VERIFY
);
529 /* success if no crash or abort */
530 static void fuzz_registers(void)
534 for (i
= 0; i
< 1000; i
++) {
537 reg
= (uint8_t)g_test_rand_int_range(0, 8);
538 val
= (uint8_t)g_test_rand_int_range(0, 256);
540 outb(FLOPPY_BASE
+ reg
, val
);
541 inb(FLOPPY_BASE
+ reg
);
545 int main(int argc
, char **argv
)
547 const char *arch
= qtest_get_arch();
551 /* Check architecture */
552 if (strcmp(arch
, "i386") && strcmp(arch
, "x86_64")) {
553 g_test_message("Skipping test for non-x86\n");
557 /* Create a temporary raw image */
558 fd
= mkstemp(test_image
);
560 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
565 g_test_init(&argc
, &argv
, NULL
);
567 qtest_start("-device floppy,id=floppy0");
568 qtest_irq_intercept_in(global_qtest
, "ioapic");
569 qtest_add_func("/fdc/cmos", test_cmos
);
570 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start
);
571 qtest_add_func("/fdc/read_without_media", test_read_without_media
);
572 qtest_add_func("/fdc/media_change", test_media_change
);
573 qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt
);
574 qtest_add_func("/fdc/relative_seek", test_relative_seek
);
575 qtest_add_func("/fdc/read_id", test_read_id
);
576 qtest_add_func("/fdc/verify", test_verify
);
577 qtest_add_func("/fdc/media_insert", test_media_insert
);
578 qtest_add_func("/fdc/read_no_dma_1", test_read_no_dma_1
);
579 qtest_add_func("/fdc/read_no_dma_18", test_read_no_dma_18
);
580 qtest_add_func("/fdc/read_no_dma_19", test_read_no_dma_19
);
581 qtest_add_func("/fdc/fuzz-registers", fuzz_registers
);