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
61 char test_image[] = "/tmp/qtest.XXXXXX";
63 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
64 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
66 static void floppy_send(uint8_t byte)
70 msr = inb(FLOPPY_BASE + reg_msr);
71 assert_bit_set(msr, RQM);
72 assert_bit_clear(msr, DIO);
74 outb(FLOPPY_BASE + reg_fifo, byte);
77 static uint8_t floppy_recv(void)
81 msr = inb(FLOPPY_BASE + reg_msr);
82 assert_bit_set(msr, RQM | DIO);
84 return inb(FLOPPY_BASE + reg_fifo);
87 static void ack_irq(void)
89 g_assert(get_irq(FLOPPY_IRQ));
90 floppy_send(CMD_SENSE_INT);
93 g_assert(!get_irq(FLOPPY_IRQ));
96 static void send_step_pulse(void)
102 floppy_send(CMD_SEEK);
103 floppy_send(head << 2 | drive);
104 g_assert(!get_irq(FLOPPY_IRQ));
111 static void test_media_change(void)
115 /* Media changed bit must be up-to-date after step pulse. Do two SEEKs
116 * because we may already happen to be on the right cylinder initially. */
119 dir = inb(FLOPPY_BASE + reg_dir);
120 assert_bit_clear(dir, DSKCHG);
122 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
124 qmp("{'execute':'eject', 'arguments':{ 'device':'floppy0' }}");
125 qmp(""); /* ignore event */
127 dir = inb(FLOPPY_BASE + reg_dir);
128 assert_bit_set(dir, DSKCHG);
129 dir = inb(FLOPPY_BASE + reg_dir);
130 assert_bit_set(dir, DSKCHG);
133 dir = inb(FLOPPY_BASE + reg_dir);
134 assert_bit_set(dir, DSKCHG);
135 dir = inb(FLOPPY_BASE + reg_dir);
136 assert_bit_set(dir, DSKCHG);
138 /* And then insert it again. DSKCHK should not be reset until a step pulse
140 qmp("{'execute':'change', 'arguments':{ 'device':'floppy0', "
141 "'target': '%s' }}", test_image);
142 qmp(""); /* ignore event (FIXME open -> open transition?!) */
143 qmp(""); /* ignore event */
145 dir = inb(FLOPPY_BASE + reg_dir);
146 assert_bit_set(dir, DSKCHG);
147 dir = inb(FLOPPY_BASE + reg_dir);
148 assert_bit_set(dir, DSKCHG);
151 dir = inb(FLOPPY_BASE + reg_dir);
152 assert_bit_clear(dir, DSKCHG);
153 dir = inb(FLOPPY_BASE + reg_dir);
154 assert_bit_clear(dir, DSKCHG);
157 int main(int argc, char **argv)
159 const char *arch = qtest_get_arch();
164 /* Check architecture */
165 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
166 g_test_message("Skipping test for non-x86\n");
170 /* Create a temporary raw image */
171 fd = mkstemp(test_image);
173 ret = ftruncate(fd, TEST_IMAGE_SIZE);
178 g_test_init(&argc, &argv, NULL);
180 cmdline = g_strdup_printf("-vnc none "
181 "-drive file=%s,if=floppy,cache=writeback ",
184 qtest_start(cmdline);
185 qtest_irq_intercept_in(global_qtest, "ioapic");
186 qtest_add_func("/fdc/media_change", test_media_change);
191 qtest_quit(global_qtest);