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
32 #include "qemu-common.h"
34 #define TEST_IMAGE_SIZE 1440 * 1024
36 #define FLOPPY_BASE 0x3f0
55 CMD_RELATIVE_SEEK_OUT
= 0x8f,
56 CMD_RELATIVE_SEEK_IN
= 0xcf,
68 char test_image
[] = "/tmp/qtest.XXXXXX";
70 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
71 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
73 static uint8_t base
= 0x70;
79 static void floppy_send(uint8_t byte
)
83 msr
= inb(FLOPPY_BASE
+ reg_msr
);
84 assert_bit_set(msr
, RQM
);
85 assert_bit_clear(msr
, DIO
);
87 outb(FLOPPY_BASE
+ reg_fifo
, byte
);
90 static uint8_t floppy_recv(void)
94 msr
= inb(FLOPPY_BASE
+ reg_msr
);
95 assert_bit_set(msr
, RQM
| DIO
);
97 return inb(FLOPPY_BASE
+ reg_fifo
);
100 /* pcn: Present Cylinder Number */
101 static void ack_irq(uint8_t *pcn
)
105 g_assert(get_irq(FLOPPY_IRQ
));
106 floppy_send(CMD_SENSE_INT
);
114 g_assert(!get_irq(FLOPPY_IRQ
));
117 static uint8_t send_read_command(uint8_t cmd
)
122 uint8_t sect_addr
= 1;
123 uint8_t sect_size
= 2;
134 floppy_send(head
<< 2 | drive
);
135 g_assert(!get_irq(FLOPPY_IRQ
));
138 floppy_send(sect_addr
);
139 floppy_send(sect_size
);
147 msr
= inb(FLOPPY_BASE
+ reg_msr
);
173 static uint8_t send_read_no_dma_command(int nb_sect
, uint8_t expected_st0
)
178 uint8_t sect_addr
= 1;
179 uint8_t sect_size
= 2;
180 uint8_t eot
= nb_sect
;
189 floppy_send(CMD_READ
);
190 floppy_send(head
<< 2 | drive
);
191 g_assert(!get_irq(FLOPPY_IRQ
));
194 floppy_send(sect_addr
);
195 floppy_send(sect_size
);
203 msr
= inb(FLOPPY_BASE
+ reg_msr
);
204 if (msr
== (BUSY
| NONDMA
| DIO
| RQM
)) {
215 for (i
= 0; i
< 512 * 2 * nb_sect
; i
++) {
216 msr
= inb(FLOPPY_BASE
+ reg_msr
);
217 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
218 inb(FLOPPY_BASE
+ reg_fifo
);
222 if (st0
!= expected_st0
) {
236 static void send_seek(int cyl
)
241 floppy_send(CMD_SEEK
);
242 floppy_send(head
<< 2 | drive
);
243 g_assert(!get_irq(FLOPPY_IRQ
));
248 static uint8_t cmos_read(uint8_t reg
)
251 return inb(base
+ 1);
254 static void test_cmos(void)
258 cmos
= cmos_read(CMOS_FLOPPY
);
259 g_assert(cmos
== 0x40);
262 static void test_no_media_on_start(void)
266 /* Media changed bit must be set all time after start if there is
267 * no media in drive. */
268 dir
= inb(FLOPPY_BASE
+ reg_dir
);
269 assert_bit_set(dir
, DSKCHG
);
270 dir
= inb(FLOPPY_BASE
+ reg_dir
);
271 assert_bit_set(dir
, DSKCHG
);
273 dir
= inb(FLOPPY_BASE
+ reg_dir
);
274 assert_bit_set(dir
, DSKCHG
);
275 dir
= inb(FLOPPY_BASE
+ reg_dir
);
276 assert_bit_set(dir
, DSKCHG
);
279 static void test_read_without_media(void)
283 ret
= send_read_command(CMD_READ
);
287 static void test_media_insert(void)
291 /* Insert media in drive. DSKCHK should not be reset until a step pulse
293 qmp_discard_response("{'execute':'change', 'arguments':{"
294 " 'device':'floppy0', 'target': '%s' }}",
296 qmp_discard_response(""); /* ignore event
297 (FIXME open -> open transition?!) */
298 qmp_discard_response(""); /* ignore event */
300 dir
= inb(FLOPPY_BASE
+ reg_dir
);
301 assert_bit_set(dir
, DSKCHG
);
302 dir
= inb(FLOPPY_BASE
+ reg_dir
);
303 assert_bit_set(dir
, DSKCHG
);
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
);
311 /* Step to next track should clear DSKCHG bit. */
313 dir
= inb(FLOPPY_BASE
+ reg_dir
);
314 assert_bit_clear(dir
, DSKCHG
);
315 dir
= inb(FLOPPY_BASE
+ reg_dir
);
316 assert_bit_clear(dir
, DSKCHG
);
319 static void test_media_change(void)
325 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
327 qmp_discard_response("{'execute':'eject', 'arguments':{"
328 " 'device':'floppy0' }}");
329 qmp_discard_response(""); /* ignore event */
331 dir
= inb(FLOPPY_BASE
+ reg_dir
);
332 assert_bit_set(dir
, DSKCHG
);
333 dir
= inb(FLOPPY_BASE
+ reg_dir
);
334 assert_bit_set(dir
, DSKCHG
);
337 dir
= inb(FLOPPY_BASE
+ reg_dir
);
338 assert_bit_set(dir
, DSKCHG
);
339 dir
= inb(FLOPPY_BASE
+ reg_dir
);
340 assert_bit_set(dir
, DSKCHG
);
343 dir
= inb(FLOPPY_BASE
+ reg_dir
);
344 assert_bit_set(dir
, DSKCHG
);
345 dir
= inb(FLOPPY_BASE
+ reg_dir
);
346 assert_bit_set(dir
, DSKCHG
);
349 static void test_sense_interrupt(void)
356 floppy_send(CMD_SENSE_INT
);
358 g_assert(ret
== 0x80);
360 floppy_send(CMD_SEEK
);
361 floppy_send(head
<< 2 | drive
);
362 g_assert(!get_irq(FLOPPY_IRQ
));
365 floppy_send(CMD_SENSE_INT
);
367 g_assert(ret
== 0x20);
371 static void test_relative_seek(void)
378 /* Send seek to track 0 */
381 /* Send relative seek to increase track by 1 */
382 floppy_send(CMD_RELATIVE_SEEK_IN
);
383 floppy_send(head
<< 2 | drive
);
384 g_assert(!get_irq(FLOPPY_IRQ
));
390 /* Send relative seek to decrease track by 1 */
391 floppy_send(CMD_RELATIVE_SEEK_OUT
);
392 floppy_send(head
<< 2 | drive
);
393 g_assert(!get_irq(FLOPPY_IRQ
));
400 static void test_read_id(void)
407 /* Seek to track 0 and check with READ ID */
410 floppy_send(CMD_READ_ID
);
411 g_assert(!get_irq(FLOPPY_IRQ
));
412 floppy_send(head
<< 2 | drive
);
414 while (!get_irq(FLOPPY_IRQ
)) {
415 /* qemu involves a timer with READ ID... */
416 clock_step(1000000000LL / 50);
423 head
= floppy_recv();
427 g_assert_cmpint(cyl
, ==, 0);
428 g_assert_cmpint(head
, ==, 0);
429 g_assert_cmpint(st0
, ==, head
<< 2);
431 /* Seek to track 8 on head 1 and check with READ ID */
435 floppy_send(CMD_SEEK
);
436 floppy_send(head
<< 2 | drive
);
437 g_assert(!get_irq(FLOPPY_IRQ
));
439 g_assert(get_irq(FLOPPY_IRQ
));
442 floppy_send(CMD_READ_ID
);
443 g_assert(!get_irq(FLOPPY_IRQ
));
444 floppy_send(head
<< 2 | drive
);
446 while (!get_irq(FLOPPY_IRQ
)) {
447 /* qemu involves a timer with READ ID... */
448 clock_step(1000000000LL / 50);
455 head
= floppy_recv();
459 g_assert_cmpint(cyl
, ==, 8);
460 g_assert_cmpint(head
, ==, 1);
461 g_assert_cmpint(st0
, ==, head
<< 2);
464 static void test_read_no_dma_1(void)
468 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
470 ret
= send_read_no_dma_command(1, 0x04);
474 static void test_read_no_dma_18(void)
478 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
480 ret
= send_read_no_dma_command(18, 0x04);
484 static void test_read_no_dma_19(void)
488 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
490 ret
= send_read_no_dma_command(19, 0x20);
494 static void test_verify(void)
498 ret
= send_read_command(CMD_VERIFY
);
502 /* success if no crash or abort */
503 static void fuzz_registers(void)
507 for (i
= 0; i
< 1000; i
++) {
510 reg
= (uint8_t)g_test_rand_int_range(0, 8);
511 val
= (uint8_t)g_test_rand_int_range(0, 256);
513 outb(FLOPPY_BASE
+ reg
, val
);
514 inb(FLOPPY_BASE
+ reg
);
518 int main(int argc
, char **argv
)
520 const char *arch
= qtest_get_arch();
525 /* Check architecture */
526 if (strcmp(arch
, "i386") && strcmp(arch
, "x86_64")) {
527 g_test_message("Skipping test for non-x86\n");
531 /* Create a temporary raw image */
532 fd
= mkstemp(test_image
);
534 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
539 g_test_init(&argc
, &argv
, NULL
);
541 cmdline
= g_strdup_printf("-vnc none ");
543 qtest_start(cmdline
);
544 qtest_irq_intercept_in(global_qtest
, "ioapic");
545 qtest_add_func("/fdc/cmos", test_cmos
);
546 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start
);
547 qtest_add_func("/fdc/read_without_media", test_read_without_media
);
548 qtest_add_func("/fdc/media_change", test_media_change
);
549 qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt
);
550 qtest_add_func("/fdc/relative_seek", test_relative_seek
);
551 qtest_add_func("/fdc/read_id", test_read_id
);
552 qtest_add_func("/fdc/verify", test_verify
);
553 qtest_add_func("/fdc/media_insert", test_media_insert
);
554 qtest_add_func("/fdc/read_no_dma_1", test_read_no_dma_1
);
555 qtest_add_func("/fdc/read_no_dma_18", test_read_no_dma_18
);
556 qtest_add_func("/fdc/read_no_dma_19", test_read_no_dma_19
);
557 qtest_add_func("/fdc/fuzz-registers", fuzz_registers
);