]>
git.proxmox.com Git - qemu.git/blob - tests/fdc-test.c
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
53 CMD_RELATIVE_SEEK_OUT
= 0x8f,
54 CMD_RELATIVE_SEEK_IN
= 0xcf,
66 char test_image
[] = "/tmp/qtest.XXXXXX";
68 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
69 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
71 static uint8_t base
= 0x70;
77 static void floppy_send(uint8_t byte
)
81 msr
= inb(FLOPPY_BASE
+ reg_msr
);
82 assert_bit_set(msr
, RQM
);
83 assert_bit_clear(msr
, DIO
);
85 outb(FLOPPY_BASE
+ reg_fifo
, byte
);
88 static uint8_t floppy_recv(void)
92 msr
= inb(FLOPPY_BASE
+ reg_msr
);
93 assert_bit_set(msr
, RQM
| DIO
);
95 return inb(FLOPPY_BASE
+ reg_fifo
);
98 /* pcn: Present Cylinder Number */
99 static void ack_irq(uint8_t *pcn
)
103 g_assert(get_irq(FLOPPY_IRQ
));
104 floppy_send(CMD_SENSE_INT
);
112 g_assert(!get_irq(FLOPPY_IRQ
));
115 static uint8_t send_read_command(void)
120 uint8_t sect_addr
= 1;
121 uint8_t sect_size
= 2;
131 floppy_send(CMD_READ
);
132 floppy_send(head
<< 2 | drive
);
133 g_assert(!get_irq(FLOPPY_IRQ
));
136 floppy_send(sect_addr
);
137 floppy_send(sect_size
);
145 msr
= inb(FLOPPY_BASE
+ reg_msr
);
171 static uint8_t send_read_no_dma_command(int nb_sect
, uint8_t expected_st0
)
176 uint8_t sect_addr
= 1;
177 uint8_t sect_size
= 2;
178 uint8_t eot
= nb_sect
;
187 floppy_send(CMD_READ
);
188 floppy_send(head
<< 2 | drive
);
189 g_assert(!get_irq(FLOPPY_IRQ
));
192 floppy_send(sect_addr
);
193 floppy_send(sect_size
);
201 msr
= inb(FLOPPY_BASE
+ reg_msr
);
202 if (msr
== (BUSY
| NONDMA
| DIO
| RQM
)) {
213 for (i
= 0; i
< 512 * 2 * nb_sect
; i
++) {
214 msr
= inb(FLOPPY_BASE
+ reg_msr
);
215 assert_bit_set(msr
, BUSY
| RQM
| DIO
);
216 inb(FLOPPY_BASE
+ reg_fifo
);
220 if (st0
!= expected_st0
) {
234 static void send_seek(int cyl
)
239 floppy_send(CMD_SEEK
);
240 floppy_send(head
<< 2 | drive
);
241 g_assert(!get_irq(FLOPPY_IRQ
));
246 static uint8_t cmos_read(uint8_t reg
)
249 return inb(base
+ 1);
252 static void test_cmos(void)
256 cmos
= cmos_read(CMOS_FLOPPY
);
257 g_assert(cmos
== 0x40);
260 static void test_no_media_on_start(void)
264 /* Media changed bit must be set all time after start if there is
265 * no media in drive. */
266 dir
= inb(FLOPPY_BASE
+ reg_dir
);
267 assert_bit_set(dir
, DSKCHG
);
268 dir
= inb(FLOPPY_BASE
+ reg_dir
);
269 assert_bit_set(dir
, DSKCHG
);
271 dir
= inb(FLOPPY_BASE
+ reg_dir
);
272 assert_bit_set(dir
, DSKCHG
);
273 dir
= inb(FLOPPY_BASE
+ reg_dir
);
274 assert_bit_set(dir
, DSKCHG
);
277 static void test_read_without_media(void)
281 ret
= send_read_command();
285 static void test_media_insert(void)
289 /* Insert media in drive. DSKCHK should not be reset until a step pulse
291 qmp("{'execute':'change', 'arguments':{ 'device':'floppy0', "
292 "'target': '%s' }}", test_image
);
293 qmp(""); /* ignore event (FIXME open -> open transition?!) */
294 qmp(""); /* ignore event */
296 dir
= inb(FLOPPY_BASE
+ reg_dir
);
297 assert_bit_set(dir
, DSKCHG
);
298 dir
= inb(FLOPPY_BASE
+ reg_dir
);
299 assert_bit_set(dir
, DSKCHG
);
302 dir
= inb(FLOPPY_BASE
+ reg_dir
);
303 assert_bit_set(dir
, DSKCHG
);
304 dir
= inb(FLOPPY_BASE
+ reg_dir
);
305 assert_bit_set(dir
, DSKCHG
);
307 /* Step to next track should clear DSKCHG bit. */
309 dir
= inb(FLOPPY_BASE
+ reg_dir
);
310 assert_bit_clear(dir
, DSKCHG
);
311 dir
= inb(FLOPPY_BASE
+ reg_dir
);
312 assert_bit_clear(dir
, DSKCHG
);
315 static void test_media_change(void)
321 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
323 qmp("{'execute':'eject', 'arguments':{ 'device':'floppy0' }}");
324 qmp(""); /* ignore event */
326 dir
= inb(FLOPPY_BASE
+ reg_dir
);
327 assert_bit_set(dir
, DSKCHG
);
328 dir
= inb(FLOPPY_BASE
+ reg_dir
);
329 assert_bit_set(dir
, DSKCHG
);
332 dir
= inb(FLOPPY_BASE
+ reg_dir
);
333 assert_bit_set(dir
, DSKCHG
);
334 dir
= inb(FLOPPY_BASE
+ reg_dir
);
335 assert_bit_set(dir
, DSKCHG
);
338 dir
= inb(FLOPPY_BASE
+ reg_dir
);
339 assert_bit_set(dir
, DSKCHG
);
340 dir
= inb(FLOPPY_BASE
+ reg_dir
);
341 assert_bit_set(dir
, DSKCHG
);
344 static void test_sense_interrupt(void)
351 floppy_send(CMD_SENSE_INT
);
353 g_assert(ret
== 0x80);
355 floppy_send(CMD_SEEK
);
356 floppy_send(head
<< 2 | drive
);
357 g_assert(!get_irq(FLOPPY_IRQ
));
360 floppy_send(CMD_SENSE_INT
);
362 g_assert(ret
== 0x20);
366 static void test_relative_seek(void)
373 /* Send seek to track 0 */
376 /* Send relative seek to increase track by 1 */
377 floppy_send(CMD_RELATIVE_SEEK_IN
);
378 floppy_send(head
<< 2 | drive
);
379 g_assert(!get_irq(FLOPPY_IRQ
));
385 /* Send relative seek to decrease track by 1 */
386 floppy_send(CMD_RELATIVE_SEEK_OUT
);
387 floppy_send(head
<< 2 | drive
);
388 g_assert(!get_irq(FLOPPY_IRQ
));
395 static void test_read_no_dma_1(void)
399 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
401 ret
= send_read_no_dma_command(1, 0x24); /* FIXME: should be 0x04 */
405 static void test_read_no_dma_18(void)
409 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
411 ret
= send_read_no_dma_command(18, 0x24); /* FIXME: should be 0x04 */
415 static void test_read_no_dma_19(void)
419 outb(FLOPPY_BASE
+ reg_dor
, inb(FLOPPY_BASE
+ reg_dor
) & ~0x08);
421 ret
= send_read_no_dma_command(19, 0x20);
425 /* success if no crash or abort */
426 static void fuzz_registers(void)
430 for (i
= 0; i
< 1000; i
++) {
433 reg
= (uint8_t)g_test_rand_int_range(0, 8);
434 val
= (uint8_t)g_test_rand_int_range(0, 256);
436 outb(FLOPPY_BASE
+ reg
, val
);
437 inb(FLOPPY_BASE
+ reg
);
441 int main(int argc
, char **argv
)
443 const char *arch
= qtest_get_arch();
448 /* Check architecture */
449 if (strcmp(arch
, "i386") && strcmp(arch
, "x86_64")) {
450 g_test_message("Skipping test for non-x86\n");
454 /* Create a temporary raw image */
455 fd
= mkstemp(test_image
);
457 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
462 g_test_init(&argc
, &argv
, NULL
);
464 cmdline
= g_strdup_printf("-vnc none ");
466 qtest_start(cmdline
);
467 qtest_irq_intercept_in(global_qtest
, "ioapic");
468 qtest_add_func("/fdc/cmos", test_cmos
);
469 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start
);
470 qtest_add_func("/fdc/read_without_media", test_read_without_media
);
471 qtest_add_func("/fdc/media_change", test_media_change
);
472 qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt
);
473 qtest_add_func("/fdc/relative_seek", test_relative_seek
);
474 qtest_add_func("/fdc/media_insert", test_media_insert
);
475 qtest_add_func("/fdc/read_no_dma_1", test_read_no_dma_1
);
476 qtest_add_func("/fdc/read_no_dma_18", test_read_no_dma_18
);
477 qtest_add_func("/fdc/read_no_dma_19", test_read_no_dma_19
);
478 qtest_add_func("/fdc/fuzz-registers", fuzz_registers
);
483 qtest_quit(global_qtest
);