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"
28 #include "libqtest-single.h"
29 #include "qapi/qmp/qdict.h"
31 /* TODO actually test the results and get rid of this */
32 #define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__))
34 #define DRIVE_FLOPPY_BLANK \
35 "-drive if=floppy,file=null-co://,file.read-zeroes=on,format=raw,size=1440k"
37 #define TEST_IMAGE_SIZE 1440 * 1024
39 #define FLOPPY_BASE 0x3f0
58 CMD_RELATIVE_SEEK_OUT
= 0x8f,
59 CMD_RELATIVE_SEEK_IN
= 0xcf,
71 static char *test_image
;
73 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
74 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
76 static uint8_t base
= 0x70;
82 static void floppy_send(uint8_t byte
)
86 msr
= inb(FLOPPY_BASE
+ reg_msr
);
87 assert_bit_set(msr
, RQM
);
88 assert_bit_clear(msr
, DIO
);
90 outb(FLOPPY_BASE
+ reg_fifo
, byte
);
93 static uint8_t floppy_recv(void)
97 msr
= inb(FLOPPY_BASE
+ reg_msr
);
98 assert_bit_set(msr
, RQM
| DIO
);
100 return inb(FLOPPY_BASE
+ reg_fifo
);
103 /* pcn: Present Cylinder Number */
104 static void ack_irq(uint8_t *pcn
)
108 g_assert(get_irq(FLOPPY_IRQ
));
109 floppy_send(CMD_SENSE_INT
);
117 g_assert(!get_irq(FLOPPY_IRQ
));
120 static uint8_t send_read_command(uint8_t cmd
)
125 uint8_t sect_addr
= 1;
126 uint8_t sect_size
= 2;
137 floppy_send(head
<< 2 | drive
);
138 g_assert(!get_irq(FLOPPY_IRQ
));
141 floppy_send(sect_addr
);
142 floppy_send(sect_size
);
150 msr
= inb(FLOPPY_BASE
+ reg_msr
);
176 static uint8_t send_read_no_dma_command(int nb_sect
, uint8_t expected_st0
)
181 uint8_t sect_addr
= 1;
182 uint8_t sect_size
= 2;
183 uint8_t eot
= nb_sect
;
192 floppy_send(CMD_READ
);
193 floppy_send(head
<< 2 | drive
);
194 g_assert(!get_irq(FLOPPY_IRQ
));
197 floppy_send(sect_addr
);
198 floppy_send(sect_size
);
206 msr
= inb(FLOPPY_BASE
+ reg_msr
);
207 if (msr
== (BUSY
| NONDMA
| DIO
| RQM
)) {
218 for (i
= 0; i
< 512 * 2 * nb_sect
; i
++) {
219 msr
= inb(FLOPPY_BASE
+ reg_msr
);
220 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
221 inb(FLOPPY_BASE
+ reg_fifo
);
224 msr
= inb(FLOPPY_BASE
+ reg_msr
);
225 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
226 g_assert(get_irq(FLOPPY_IRQ
));
229 if (st0
!= expected_st0
) {
238 g_assert(get_irq(FLOPPY_IRQ
));
241 /* Check that we're back in command phase */
242 msr
= inb(FLOPPY_BASE
+ reg_msr
);
243 assert_bit_clear(msr
, BUSY
| DIO
);
244 assert_bit_set(msr
, RQM
);
245 g_assert(!get_irq(FLOPPY_IRQ
));
250 static void send_seek(int cyl
)
255 floppy_send(CMD_SEEK
);
256 floppy_send(head
<< 2 | drive
);
257 g_assert(!get_irq(FLOPPY_IRQ
));
262 static uint8_t cmos_read(uint8_t reg
)
265 return inb(base
+ 1);
268 static void test_cmos(void)
272 cmos
= cmos_read(CMOS_FLOPPY
);
273 g_assert(cmos
== 0x40 || cmos
== 0x50);
276 static void test_no_media_on_start(void)
280 /* Media changed bit must be set all time after start if there is
281 * no media in drive. */
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
);
287 dir
= inb(FLOPPY_BASE
+ reg_dir
);
288 assert_bit_set(dir
, DSKCHG
);
289 dir
= inb(FLOPPY_BASE
+ reg_dir
);
290 assert_bit_set(dir
, DSKCHG
);
293 static void test_read_without_media(void)
297 ret
= send_read_command(CMD_READ
);
301 static void test_media_insert(void)
305 /* Insert media in drive. DSKCHK should not be reset until a step pulse
307 qmp_discard_response("{'execute':'blockdev-change-medium', 'arguments':{"
308 " 'id':'floppy0', 'filename': %s, 'format': 'raw' }}",
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
);
317 dir
= inb(FLOPPY_BASE
+ reg_dir
);
318 assert_bit_set(dir
, DSKCHG
);
319 dir
= inb(FLOPPY_BASE
+ reg_dir
);
320 assert_bit_set(dir
, DSKCHG
);
322 /* Step to next track should clear DSKCHG bit. */
324 dir
= inb(FLOPPY_BASE
+ reg_dir
);
325 assert_bit_clear(dir
, DSKCHG
);
326 dir
= inb(FLOPPY_BASE
+ reg_dir
);
327 assert_bit_clear(dir
, DSKCHG
);
330 static void test_media_change(void)
336 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
338 qmp_discard_response("{'execute':'eject', 'arguments':{"
339 " 'id':'floppy0' }}");
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 dir
= inb(FLOPPY_BASE
+ reg_dir
);
354 assert_bit_set(dir
, DSKCHG
);
355 dir
= inb(FLOPPY_BASE
+ reg_dir
);
356 assert_bit_set(dir
, DSKCHG
);
359 static void test_sense_interrupt(void)
366 floppy_send(CMD_SENSE_INT
);
368 g_assert(ret
== 0x80);
370 floppy_send(CMD_SEEK
);
371 floppy_send(head
<< 2 | drive
);
372 g_assert(!get_irq(FLOPPY_IRQ
));
375 floppy_send(CMD_SENSE_INT
);
377 g_assert(ret
== 0x20);
381 static void test_relative_seek(void)
388 /* Send seek to track 0 */
391 /* Send relative seek to increase track by 1 */
392 floppy_send(CMD_RELATIVE_SEEK_IN
);
393 floppy_send(head
<< 2 | drive
);
394 g_assert(!get_irq(FLOPPY_IRQ
));
400 /* Send relative seek to decrease track by 1 */
401 floppy_send(CMD_RELATIVE_SEEK_OUT
);
402 floppy_send(head
<< 2 | drive
);
403 g_assert(!get_irq(FLOPPY_IRQ
));
410 static void test_read_id(void)
418 /* Seek to track 0 and check with READ ID */
421 floppy_send(CMD_READ_ID
);
422 g_assert(!get_irq(FLOPPY_IRQ
));
423 floppy_send(head
<< 2 | drive
);
425 msr
= inb(FLOPPY_BASE
+ reg_msr
);
426 if (!get_irq(FLOPPY_IRQ
)) {
427 assert_bit_set(msr
, BUSY
);
428 assert_bit_clear(msr
, RQM
);
431 while (!get_irq(FLOPPY_IRQ
)) {
432 /* qemu involves a timer with READ ID... */
433 clock_step(1000000000LL / 50);
436 msr
= inb(FLOPPY_BASE
+ reg_msr
);
437 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
443 head
= floppy_recv();
445 g_assert(get_irq(FLOPPY_IRQ
));
447 g_assert(!get_irq(FLOPPY_IRQ
));
449 g_assert_cmpint(cyl
, ==, 0);
450 g_assert_cmpint(head
, ==, 0);
451 g_assert_cmpint(st0
, ==, head
<< 2);
453 /* Seek to track 8 on head 1 and check with READ ID */
457 floppy_send(CMD_SEEK
);
458 floppy_send(head
<< 2 | drive
);
459 g_assert(!get_irq(FLOPPY_IRQ
));
461 g_assert(get_irq(FLOPPY_IRQ
));
464 floppy_send(CMD_READ_ID
);
465 g_assert(!get_irq(FLOPPY_IRQ
));
466 floppy_send(head
<< 2 | drive
);
468 msr
= inb(FLOPPY_BASE
+ reg_msr
);
469 if (!get_irq(FLOPPY_IRQ
)) {
470 assert_bit_set(msr
, BUSY
);
471 assert_bit_clear(msr
, RQM
);
474 while (!get_irq(FLOPPY_IRQ
)) {
475 /* qemu involves a timer with READ ID... */
476 clock_step(1000000000LL / 50);
479 msr
= inb(FLOPPY_BASE
+ reg_msr
);
480 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
486 head
= floppy_recv();
488 g_assert(get_irq(FLOPPY_IRQ
));
490 g_assert(!get_irq(FLOPPY_IRQ
));
492 g_assert_cmpint(cyl
, ==, 8);
493 g_assert_cmpint(head
, ==, 1);
494 g_assert_cmpint(st0
, ==, head
<< 2);
497 static void test_read_no_dma_1(void)
501 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
503 ret
= send_read_no_dma_command(1, 0x04);
507 static void test_read_no_dma_18(void)
511 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
513 ret
= send_read_no_dma_command(18, 0x04);
517 static void test_read_no_dma_19(void)
521 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
523 ret
= send_read_no_dma_command(19, 0x20);
527 static void test_verify(void)
531 ret
= send_read_command(CMD_VERIFY
);
535 /* success if no crash or abort */
536 static void fuzz_registers(void)
540 for (i
= 0; i
< 1000; i
++) {
543 reg
= (uint8_t)g_test_rand_int_range(0, 8);
544 val
= (uint8_t)g_test_rand_int_range(0, 256);
546 outb(FLOPPY_BASE
+ reg
, val
);
547 inb(FLOPPY_BASE
+ reg
);
551 static bool qtest_check_clang_sanitizer(void)
553 #ifdef QEMU_SANITIZE_ADDRESS
556 g_test_skip("QEMU not configured using --enable-sanitizers");
560 static void test_cve_2021_20196(void)
564 if (!qtest_check_clang_sanitizer()) {
568 s
= qtest_initf("-nographic -m 32M -nodefaults " DRIVE_FLOPPY_BLANK
);
570 qtest_outw(s
, 0x3f4, 0x0500);
571 qtest_outb(s
, 0x3f5, 0x00);
572 qtest_outb(s
, 0x3f5, 0x00);
573 qtest_outw(s
, 0x3f4, 0x0000);
574 qtest_outb(s
, 0x3f5, 0x00);
575 qtest_outw(s
, 0x3f1, 0x0400);
576 qtest_outw(s
, 0x3f4, 0x0000);
577 qtest_outw(s
, 0x3f4, 0x0000);
578 qtest_outb(s
, 0x3f5, 0x00);
579 qtest_outb(s
, 0x3f5, 0x01);
580 qtest_outw(s
, 0x3f1, 0x0500);
581 qtest_outb(s
, 0x3f5, 0x00);
585 static void test_cve_2021_3507(void)
589 s
= qtest_initf("-nographic -m 32M -nodefaults "
590 "-drive file=%s,format=raw,if=floppy,snapshot=on",
592 qtest_outl(s
, 0x9, 0x0a0206);
593 qtest_outw(s
, 0x3f4, 0x1600);
594 qtest_outw(s
, 0x3f4, 0x0000);
595 qtest_outw(s
, 0x3f4, 0x0000);
596 qtest_outw(s
, 0x3f4, 0x0000);
597 qtest_outw(s
, 0x3f4, 0x0200);
598 qtest_outw(s
, 0x3f4, 0x0200);
599 qtest_outw(s
, 0x3f4, 0x0000);
600 qtest_outw(s
, 0x3f4, 0x0000);
601 qtest_outw(s
, 0x3f4, 0x0000);
605 int main(int argc
, char **argv
)
610 /* Create a temporary raw image */
611 fd
= g_file_open_tmp("qtest.XXXXXX", &test_image
, NULL
);
613 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
618 g_test_init(&argc
, &argv
, NULL
);
620 qtest_start("-machine pc -device floppy,id=floppy0");
621 qtest_irq_intercept_in(global_qtest
, "ioapic");
622 qtest_add_func("/fdc/cmos", test_cmos
);
623 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start
);
624 qtest_add_func("/fdc/read_without_media", test_read_without_media
);
625 qtest_add_func("/fdc/media_change", test_media_change
);
626 qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt
);
627 qtest_add_func("/fdc/relative_seek", test_relative_seek
);
628 qtest_add_func("/fdc/read_id", test_read_id
);
629 qtest_add_func("/fdc/verify", test_verify
);
630 qtest_add_func("/fdc/media_insert", test_media_insert
);
631 qtest_add_func("/fdc/read_no_dma_1", test_read_no_dma_1
);
632 qtest_add_func("/fdc/read_no_dma_18", test_read_no_dma_18
);
633 qtest_add_func("/fdc/read_no_dma_19", test_read_no_dma_19
);
634 qtest_add_func("/fdc/fuzz-registers", fuzz_registers
);
635 qtest_add_func("/fdc/fuzz/cve_2021_20196", test_cve_2021_20196
);
636 qtest_add_func("/fdc/fuzz/cve_2021_3507", test_cve_2021_3507
);