fdc-test: insert media before fuzzing registers
[qemu/ar7.git] / tests / fdc-test.c
blob67bfb22174b8b0893dd4edbcc0a454c3e021b412
1 /*
2 * Floppy test cases.
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
22 * THE SOFTWARE.
25 #include <stdint.h>
26 #include <string.h>
27 #include <stdio.h>
29 #include <glib.h>
31 #include "libqtest.h"
32 #include "qemu-common.h"
34 #define TEST_IMAGE_SIZE 1440 * 1024
36 #define FLOPPY_BASE 0x3f0
37 #define FLOPPY_IRQ 6
39 enum {
40 reg_sra = 0x0,
41 reg_srb = 0x1,
42 reg_dor = 0x2,
43 reg_msr = 0x4,
44 reg_dsr = 0x4,
45 reg_fifo = 0x5,
46 reg_dir = 0x7,
49 enum {
50 CMD_SENSE_INT = 0x08,
51 CMD_SEEK = 0x0f,
52 CMD_READ = 0xe6,
53 CMD_RELATIVE_SEEK_OUT = 0x8f,
54 CMD_RELATIVE_SEEK_IN = 0xcf,
57 enum {
58 RQM = 0x80,
59 DIO = 0x40,
61 DSKCHG = 0x80,
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;
71 enum {
72 CMOS_FLOPPY = 0x10,
75 static void floppy_send(uint8_t byte)
77 uint8_t msr;
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)
88 uint8_t msr;
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)
99 uint8_t ret;
101 g_assert(get_irq(FLOPPY_IRQ));
102 floppy_send(CMD_SENSE_INT);
103 floppy_recv();
105 ret = floppy_recv();
106 if (pcn != NULL) {
107 *pcn = ret;
110 g_assert(!get_irq(FLOPPY_IRQ));
113 static uint8_t send_read_command(void)
115 uint8_t drive = 0;
116 uint8_t head = 0;
117 uint8_t cyl = 0;
118 uint8_t sect_addr = 1;
119 uint8_t sect_size = 2;
120 uint8_t eot = 1;
121 uint8_t gap = 0x1b;
122 uint8_t gpl = 0xff;
124 uint8_t msr = 0;
125 uint8_t st0;
127 uint8_t ret = 0;
129 floppy_send(CMD_READ);
130 floppy_send(head << 2 | drive);
131 g_assert(!get_irq(FLOPPY_IRQ));
132 floppy_send(cyl);
133 floppy_send(head);
134 floppy_send(sect_addr);
135 floppy_send(sect_size);
136 floppy_send(eot);
137 floppy_send(gap);
138 floppy_send(gpl);
140 uint8_t i = 0;
141 uint8_t n = 2;
142 for (; i < n; i++) {
143 msr = inb(FLOPPY_BASE + reg_msr);
144 if (msr == 0xd0) {
145 break;
147 sleep(1);
150 if (i >= n) {
151 return 1;
154 st0 = floppy_recv();
155 if (st0 != 0x60) {
156 ret = 1;
159 floppy_recv();
160 floppy_recv();
161 floppy_recv();
162 floppy_recv();
163 floppy_recv();
164 floppy_recv();
166 return ret;
169 static void send_seek(int cyl)
171 int drive = 0;
172 int head = 0;
174 floppy_send(CMD_SEEK);
175 floppy_send(head << 2 | drive);
176 g_assert(!get_irq(FLOPPY_IRQ));
177 floppy_send(cyl);
178 ack_irq(NULL);
181 static uint8_t cmos_read(uint8_t reg)
183 outb(base + 0, reg);
184 return inb(base + 1);
187 static void test_cmos(void)
189 uint8_t cmos;
191 cmos = cmos_read(CMOS_FLOPPY);
192 g_assert(cmos == 0x40);
195 static void test_no_media_on_start(void)
197 uint8_t dir;
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);
205 send_seek(1);
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)
214 uint8_t ret;
216 ret = send_read_command();
217 g_assert(ret == 0);
220 static void test_media_insert(void)
222 uint8_t dir;
224 /* Insert media in drive. DSKCHK should not be reset until a step pulse
225 * is sent. */
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);
236 send_seek(0);
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. */
243 send_seek(1);
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);
250 static void test_media_change(void)
252 uint8_t dir;
254 test_media_insert();
256 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
257 * reset the bit. */
258 qmp("{'execute':'eject', 'arguments':{ 'device':'floppy0' }}");
259 qmp(""); /* ignore event */
261 dir = inb(FLOPPY_BASE + reg_dir);
262 assert_bit_set(dir, DSKCHG);
263 dir = inb(FLOPPY_BASE + reg_dir);
264 assert_bit_set(dir, DSKCHG);
266 send_seek(0);
267 dir = inb(FLOPPY_BASE + reg_dir);
268 assert_bit_set(dir, DSKCHG);
269 dir = inb(FLOPPY_BASE + reg_dir);
270 assert_bit_set(dir, DSKCHG);
272 send_seek(1);
273 dir = inb(FLOPPY_BASE + reg_dir);
274 assert_bit_set(dir, DSKCHG);
275 dir = inb(FLOPPY_BASE + reg_dir);
276 assert_bit_set(dir, DSKCHG);
279 static void test_sense_interrupt(void)
281 int drive = 0;
282 int head = 0;
283 int cyl = 0;
284 int ret = 0;
286 floppy_send(CMD_SENSE_INT);
287 ret = floppy_recv();
288 g_assert(ret == 0x80);
290 floppy_send(CMD_SEEK);
291 floppy_send(head << 2 | drive);
292 g_assert(!get_irq(FLOPPY_IRQ));
293 floppy_send(cyl);
295 floppy_send(CMD_SENSE_INT);
296 ret = floppy_recv();
297 g_assert(ret == 0x20);
298 floppy_recv();
301 static void test_relative_seek(void)
303 uint8_t drive = 0;
304 uint8_t head = 0;
305 uint8_t cyl = 1;
306 uint8_t pcn;
308 /* Send seek to track 0 */
309 send_seek(0);
311 /* Send relative seek to increase track by 1 */
312 floppy_send(CMD_RELATIVE_SEEK_IN);
313 floppy_send(head << 2 | drive);
314 g_assert(!get_irq(FLOPPY_IRQ));
315 floppy_send(cyl);
317 ack_irq(&pcn);
318 g_assert(pcn == 1);
320 /* Send relative seek to decrease track by 1 */
321 floppy_send(CMD_RELATIVE_SEEK_OUT);
322 floppy_send(head << 2 | drive);
323 g_assert(!get_irq(FLOPPY_IRQ));
324 floppy_send(cyl);
326 ack_irq(&pcn);
327 g_assert(pcn == 0);
330 /* success if no crash or abort */
331 static void fuzz_registers(void)
333 unsigned int i;
335 for (i = 0; i < 1000; i++) {
336 uint8_t reg, val;
338 reg = (uint8_t)g_test_rand_int_range(0, 8);
339 val = (uint8_t)g_test_rand_int_range(0, 256);
341 outb(FLOPPY_BASE + reg, val);
342 inb(FLOPPY_BASE + reg);
346 int main(int argc, char **argv)
348 const char *arch = qtest_get_arch();
349 char *cmdline;
350 int fd;
351 int ret;
353 /* Check architecture */
354 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
355 g_test_message("Skipping test for non-x86\n");
356 return 0;
359 /* Create a temporary raw image */
360 fd = mkstemp(test_image);
361 g_assert(fd >= 0);
362 ret = ftruncate(fd, TEST_IMAGE_SIZE);
363 g_assert(ret == 0);
364 close(fd);
366 /* Run the tests */
367 g_test_init(&argc, &argv, NULL);
369 cmdline = g_strdup_printf("-vnc none ");
371 qtest_start(cmdline);
372 qtest_irq_intercept_in(global_qtest, "ioapic");
373 qtest_add_func("/fdc/cmos", test_cmos);
374 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start);
375 qtest_add_func("/fdc/read_without_media", test_read_without_media);
376 qtest_add_func("/fdc/media_change", test_media_change);
377 qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt);
378 qtest_add_func("/fdc/relative_seek", test_relative_seek);
379 qtest_add_func("/fdc/media_insert", test_media_insert);
380 qtest_add_func("/fdc/fuzz-registers", fuzz_registers);
382 ret = g_test_run();
384 /* Cleanup */
385 qtest_quit(global_qtest);
386 unlink(test_image);
388 return ret;