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,
64 char test_image
[] = "/tmp/qtest.XXXXXX";
66 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
67 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
69 static uint8_t base
= 0x70;
75 static void floppy_send(uint8_t byte
)
79 msr
= inb(FLOPPY_BASE
+ reg_msr
);
80 assert_bit_set(msr
, RQM
);
81 assert_bit_clear(msr
, DIO
);
83 outb(FLOPPY_BASE
+ reg_fifo
, byte
);
86 static uint8_t floppy_recv(void)
90 msr
= inb(FLOPPY_BASE
+ reg_msr
);
91 assert_bit_set(msr
, RQM
| DIO
);
93 return inb(FLOPPY_BASE
+ reg_fifo
);
96 /* pcn: Present Cylinder Number */
97 static void ack_irq(uint8_t *pcn
)
101 g_assert(get_irq(FLOPPY_IRQ
));
102 floppy_send(CMD_SENSE_INT
);
110 g_assert(!get_irq(FLOPPY_IRQ
));
113 static uint8_t send_read_command(void)
118 uint8_t sect_addr
= 1;
119 uint8_t sect_size
= 2;
129 floppy_send(CMD_READ
);
130 floppy_send(head
<< 2 | drive
);
131 g_assert(!get_irq(FLOPPY_IRQ
));
134 floppy_send(sect_addr
);
135 floppy_send(sect_size
);
143 msr
= inb(FLOPPY_BASE
+ reg_msr
);
169 static void send_seek(int cyl
)
174 floppy_send(CMD_SEEK
);
175 floppy_send(head
<< 2 | drive
);
176 g_assert(!get_irq(FLOPPY_IRQ
));
181 static uint8_t cmos_read(uint8_t reg
)
184 return inb(base
+ 1);
187 static void test_cmos(void)
191 cmos
= cmos_read(CMOS_FLOPPY
);
192 g_assert(cmos
== 0x40);
195 static void test_no_media_on_start(void)
199 /* Media changed bit must be set all time after start if there is
200 * no media in drive. */
201 dir
= inb(FLOPPY_BASE
+ reg_dir
);
202 assert_bit_set(dir
, DSKCHG
);
203 dir
= inb(FLOPPY_BASE
+ reg_dir
);
204 assert_bit_set(dir
, DSKCHG
);
206 dir
= inb(FLOPPY_BASE
+ reg_dir
);
207 assert_bit_set(dir
, DSKCHG
);
208 dir
= inb(FLOPPY_BASE
+ reg_dir
);
209 assert_bit_set(dir
, DSKCHG
);
212 static void test_read_without_media(void)
216 ret
= send_read_command();
220 static void test_media_change(void)
224 /* Insert media in drive. DSKCHK should not be reset until a step pulse
226 qmp("{'execute':'change', 'arguments':{ 'device':'floppy0', "
227 "'target': '%s' }}", test_image
);
228 qmp(""); /* ignore event (FIXME open -> open transition?!) */
229 qmp(""); /* ignore event */
231 dir
= inb(FLOPPY_BASE
+ reg_dir
);
232 assert_bit_set(dir
, DSKCHG
);
233 dir
= inb(FLOPPY_BASE
+ reg_dir
);
234 assert_bit_set(dir
, DSKCHG
);
237 dir
= inb(FLOPPY_BASE
+ reg_dir
);
238 assert_bit_set(dir
, DSKCHG
);
239 dir
= inb(FLOPPY_BASE
+ reg_dir
);
240 assert_bit_set(dir
, DSKCHG
);
242 /* Step to next track should clear DSKCHG bit. */
244 dir
= inb(FLOPPY_BASE
+ reg_dir
);
245 assert_bit_clear(dir
, DSKCHG
);
246 dir
= inb(FLOPPY_BASE
+ reg_dir
);
247 assert_bit_clear(dir
, DSKCHG
);
249 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
251 qmp("{'execute':'eject', 'arguments':{ 'device':'floppy0' }}");
252 qmp(""); /* ignore event */
254 dir
= inb(FLOPPY_BASE
+ reg_dir
);
255 assert_bit_set(dir
, DSKCHG
);
256 dir
= inb(FLOPPY_BASE
+ reg_dir
);
257 assert_bit_set(dir
, DSKCHG
);
260 dir
= inb(FLOPPY_BASE
+ reg_dir
);
261 assert_bit_set(dir
, DSKCHG
);
262 dir
= inb(FLOPPY_BASE
+ reg_dir
);
263 assert_bit_set(dir
, DSKCHG
);
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
);
272 static void test_sense_interrupt(void)
279 floppy_send(CMD_SENSE_INT
);
281 g_assert(ret
== 0x80);
283 floppy_send(CMD_SEEK
);
284 floppy_send(head
<< 2 | drive
);
285 g_assert(!get_irq(FLOPPY_IRQ
));
288 floppy_send(CMD_SENSE_INT
);
290 g_assert(ret
== 0x20);
294 static void test_relative_seek(void)
301 /* Send seek to track 0 */
304 /* Send relative seek to increase track by 1 */
305 floppy_send(CMD_RELATIVE_SEEK_IN
);
306 floppy_send(head
<< 2 | drive
);
307 g_assert(!get_irq(FLOPPY_IRQ
));
313 /* Send relative seek to decrease track by 1 */
314 floppy_send(CMD_RELATIVE_SEEK_OUT
);
315 floppy_send(head
<< 2 | drive
);
316 g_assert(!get_irq(FLOPPY_IRQ
));
323 /* success if no crash or abort */
324 static void fuzz_registers(void)
328 for (i
= 0; i
< 1000; i
++) {
331 reg
= (uint8_t)g_test_rand_int_range(0, 8);
332 val
= (uint8_t)g_test_rand_int_range(0, 256);
334 outb(FLOPPY_BASE
+ reg
, val
);
335 inb(FLOPPY_BASE
+ reg
);
339 int main(int argc
, char **argv
)
341 const char *arch
= qtest_get_arch();
346 /* Check architecture */
347 if (strcmp(arch
, "i386") && strcmp(arch
, "x86_64")) {
348 g_test_message("Skipping test for non-x86\n");
352 /* Create a temporary raw image */
353 fd
= mkstemp(test_image
);
355 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
360 g_test_init(&argc
, &argv
, NULL
);
362 cmdline
= g_strdup_printf("-vnc none ");
364 qtest_start(cmdline
);
365 qtest_irq_intercept_in(global_qtest
, "ioapic");
366 qtest_add_func("/fdc/cmos", test_cmos
);
367 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start
);
368 qtest_add_func("/fdc/read_without_media", test_read_without_media
);
369 qtest_add_func("/fdc/media_change", test_media_change
);
370 qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt
);
371 qtest_add_func("/fdc/relative_seek", test_relative_seek
);
372 qtest_add_func("/fdc/fuzz-registers", fuzz_registers
);
377 qtest_quit(global_qtest
);