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 #define DRIVE_FLOPPY_BLANK \
32 "-drive if=floppy,file=null-co://,file.read-zeroes=on,format=raw,size=1440k"
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 static char *test_image
;
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
);
221 msr
= inb(FLOPPY_BASE
+ reg_msr
);
222 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
223 g_assert(get_irq(FLOPPY_IRQ
));
226 if (st0
!= expected_st0
) {
235 g_assert(get_irq(FLOPPY_IRQ
));
238 /* Check that we're back in command phase */
239 msr
= inb(FLOPPY_BASE
+ reg_msr
);
240 assert_bit_clear(msr
, BUSY
| DIO
);
241 assert_bit_set(msr
, RQM
);
242 g_assert(!get_irq(FLOPPY_IRQ
));
247 static void send_seek(int cyl
)
252 floppy_send(CMD_SEEK
);
253 floppy_send(head
<< 2 | drive
);
254 g_assert(!get_irq(FLOPPY_IRQ
));
259 static uint8_t cmos_read(uint8_t reg
)
262 return inb(base
+ 1);
265 static void test_cmos(void)
269 cmos
= cmos_read(CMOS_FLOPPY
);
270 g_assert(cmos
== 0x40 || cmos
== 0x50);
273 static void test_no_media_on_start(void)
277 /* Media changed bit must be set all time after start if there is
278 * no media in drive. */
279 dir
= inb(FLOPPY_BASE
+ reg_dir
);
280 assert_bit_set(dir
, DSKCHG
);
281 dir
= inb(FLOPPY_BASE
+ reg_dir
);
282 assert_bit_set(dir
, DSKCHG
);
284 dir
= inb(FLOPPY_BASE
+ reg_dir
);
285 assert_bit_set(dir
, DSKCHG
);
286 dir
= inb(FLOPPY_BASE
+ reg_dir
);
287 assert_bit_set(dir
, DSKCHG
);
290 static void test_read_without_media(void)
294 ret
= send_read_command(CMD_READ
);
298 static void test_media_insert(void)
302 /* Insert media in drive. DSKCHK should not be reset until a step pulse
304 qtest_qmp_assert_success(global_qtest
,
305 "{'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 qtest_qmp_assert_success(global_qtest
,
337 "{'execute':'eject', 'arguments':{"
338 " 'id':'floppy0' }}");
340 dir
= inb(FLOPPY_BASE
+ reg_dir
);
341 assert_bit_set(dir
, DSKCHG
);
342 dir
= inb(FLOPPY_BASE
+ reg_dir
);
343 assert_bit_set(dir
, DSKCHG
);
346 dir
= inb(FLOPPY_BASE
+ reg_dir
);
347 assert_bit_set(dir
, DSKCHG
);
348 dir
= inb(FLOPPY_BASE
+ reg_dir
);
349 assert_bit_set(dir
, DSKCHG
);
352 dir
= inb(FLOPPY_BASE
+ reg_dir
);
353 assert_bit_set(dir
, DSKCHG
);
354 dir
= inb(FLOPPY_BASE
+ reg_dir
);
355 assert_bit_set(dir
, DSKCHG
);
358 static void test_sense_interrupt(void)
365 floppy_send(CMD_SENSE_INT
);
367 g_assert(ret
== 0x80);
369 floppy_send(CMD_SEEK
);
370 floppy_send(head
<< 2 | drive
);
371 g_assert(!get_irq(FLOPPY_IRQ
));
374 floppy_send(CMD_SENSE_INT
);
376 g_assert(ret
== 0x20);
380 static void test_relative_seek(void)
387 /* Send seek to track 0 */
390 /* Send relative seek to increase track by 1 */
391 floppy_send(CMD_RELATIVE_SEEK_IN
);
392 floppy_send(head
<< 2 | drive
);
393 g_assert(!get_irq(FLOPPY_IRQ
));
399 /* Send relative seek to decrease track by 1 */
400 floppy_send(CMD_RELATIVE_SEEK_OUT
);
401 floppy_send(head
<< 2 | drive
);
402 g_assert(!get_irq(FLOPPY_IRQ
));
409 static void test_read_id(void)
417 /* Seek to track 0 and check with READ ID */
420 floppy_send(CMD_READ_ID
);
421 g_assert(!get_irq(FLOPPY_IRQ
));
422 floppy_send(head
<< 2 | drive
);
424 msr
= inb(FLOPPY_BASE
+ reg_msr
);
425 if (!get_irq(FLOPPY_IRQ
)) {
426 assert_bit_set(msr
, BUSY
);
427 assert_bit_clear(msr
, RQM
);
430 while (!get_irq(FLOPPY_IRQ
)) {
431 /* qemu involves a timer with READ ID... */
432 clock_step(1000000000LL / 50);
435 msr
= inb(FLOPPY_BASE
+ reg_msr
);
436 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
442 head
= floppy_recv();
444 g_assert(get_irq(FLOPPY_IRQ
));
446 g_assert(!get_irq(FLOPPY_IRQ
));
448 g_assert_cmpint(cyl
, ==, 0);
449 g_assert_cmpint(head
, ==, 0);
450 g_assert_cmpint(st0
, ==, head
<< 2);
452 /* Seek to track 8 on head 1 and check with READ ID */
456 floppy_send(CMD_SEEK
);
457 floppy_send(head
<< 2 | drive
);
458 g_assert(!get_irq(FLOPPY_IRQ
));
460 g_assert(get_irq(FLOPPY_IRQ
));
463 floppy_send(CMD_READ_ID
);
464 g_assert(!get_irq(FLOPPY_IRQ
));
465 floppy_send(head
<< 2 | drive
);
467 msr
= inb(FLOPPY_BASE
+ reg_msr
);
468 if (!get_irq(FLOPPY_IRQ
)) {
469 assert_bit_set(msr
, BUSY
);
470 assert_bit_clear(msr
, RQM
);
473 while (!get_irq(FLOPPY_IRQ
)) {
474 /* qemu involves a timer with READ ID... */
475 clock_step(1000000000LL / 50);
478 msr
= inb(FLOPPY_BASE
+ reg_msr
);
479 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
485 head
= floppy_recv();
487 g_assert(get_irq(FLOPPY_IRQ
));
489 g_assert(!get_irq(FLOPPY_IRQ
));
491 g_assert_cmpint(cyl
, ==, 8);
492 g_assert_cmpint(head
, ==, 1);
493 g_assert_cmpint(st0
, ==, head
<< 2);
496 static void test_read_no_dma_1(void)
500 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
502 ret
= send_read_no_dma_command(1, 0x04);
506 static void test_read_no_dma_18(void)
510 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
512 ret
= send_read_no_dma_command(18, 0x04);
516 static void test_read_no_dma_19(void)
520 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
522 ret
= send_read_no_dma_command(19, 0x20);
526 static void test_verify(void)
530 ret
= send_read_command(CMD_VERIFY
);
534 /* success if no crash or abort */
535 static void fuzz_registers(void)
539 for (i
= 0; i
< 1000; i
++) {
542 reg
= (uint8_t)g_test_rand_int_range(0, 8);
543 val
= (uint8_t)g_test_rand_int_range(0, 256);
545 outb(FLOPPY_BASE
+ reg
, val
);
546 inb(FLOPPY_BASE
+ reg
);
550 static bool qtest_check_clang_sanitizer(void)
552 #ifdef QEMU_SANITIZE_ADDRESS
555 g_test_skip("QEMU not configured using --enable-sanitizers");
559 static void test_cve_2021_20196(void)
563 if (!qtest_check_clang_sanitizer()) {
567 s
= qtest_initf("-nographic -m 32M -nodefaults " DRIVE_FLOPPY_BLANK
);
569 qtest_outw(s
, 0x3f4, 0x0500);
570 qtest_outb(s
, 0x3f5, 0x00);
571 qtest_outb(s
, 0x3f5, 0x00);
572 qtest_outw(s
, 0x3f4, 0x0000);
573 qtest_outb(s
, 0x3f5, 0x00);
574 qtest_outw(s
, 0x3f1, 0x0400);
575 qtest_outw(s
, 0x3f4, 0x0000);
576 qtest_outw(s
, 0x3f4, 0x0000);
577 qtest_outb(s
, 0x3f5, 0x00);
578 qtest_outb(s
, 0x3f5, 0x01);
579 qtest_outw(s
, 0x3f1, 0x0500);
580 qtest_outb(s
, 0x3f5, 0x00);
584 static void test_cve_2021_3507(void)
588 s
= qtest_initf("-nographic -m 32M -nodefaults "
589 "-drive file=%s,format=raw,if=floppy,snapshot=on",
591 qtest_outl(s
, 0x9, 0x0a0206);
592 qtest_outw(s
, 0x3f4, 0x1600);
593 qtest_outw(s
, 0x3f4, 0x0000);
594 qtest_outw(s
, 0x3f4, 0x0000);
595 qtest_outw(s
, 0x3f4, 0x0000);
596 qtest_outw(s
, 0x3f4, 0x0200);
597 qtest_outw(s
, 0x3f4, 0x0200);
598 qtest_outw(s
, 0x3f4, 0x0000);
599 qtest_outw(s
, 0x3f4, 0x0000);
600 qtest_outw(s
, 0x3f4, 0x0000);
604 int main(int argc
, char **argv
)
609 /* Create a temporary raw image */
610 fd
= g_file_open_tmp("qtest.XXXXXX", &test_image
, NULL
);
612 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
617 g_test_init(&argc
, &argv
, NULL
);
619 qtest_start("-machine pc -device floppy,id=floppy0");
620 qtest_irq_intercept_in(global_qtest
, "ioapic");
621 qtest_add_func("/fdc/cmos", test_cmos
);
622 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start
);
623 qtest_add_func("/fdc/read_without_media", test_read_without_media
);
624 qtest_add_func("/fdc/media_change", test_media_change
);
625 qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt
);
626 qtest_add_func("/fdc/relative_seek", test_relative_seek
);
627 qtest_add_func("/fdc/read_id", test_read_id
);
628 qtest_add_func("/fdc/verify", test_verify
);
629 qtest_add_func("/fdc/media_insert", test_media_insert
);
630 qtest_add_func("/fdc/read_no_dma_1", test_read_no_dma_1
);
631 qtest_add_func("/fdc/read_no_dma_18", test_read_no_dma_18
);
632 qtest_add_func("/fdc/read_no_dma_19", test_read_no_dma_19
);
633 qtest_add_func("/fdc/fuzz-registers", fuzz_registers
);
634 qtest_add_func("/fdc/fuzz/cve_2021_20196", test_cve_2021_20196
);
635 qtest_add_func("/fdc/fuzz/cve_2021_3507", test_cve_2021_3507
);