chardev: Use timer instead of bottom-half to postpone open event
[qemu/ar7.git] / tests / fdc-test.c
blobfa7441110dfd1194a202a50ae8909e7e237cb29b
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_change(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);
249 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
250 * reset the bit. */
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);
259 send_seek(0);
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);
265 send_seek(1);
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)
274 int drive = 0;
275 int head = 0;
276 int cyl = 0;
277 int ret = 0;
279 floppy_send(CMD_SENSE_INT);
280 ret = floppy_recv();
281 g_assert(ret == 0x80);
283 floppy_send(CMD_SEEK);
284 floppy_send(head << 2 | drive);
285 g_assert(!get_irq(FLOPPY_IRQ));
286 floppy_send(cyl);
288 floppy_send(CMD_SENSE_INT);
289 ret = floppy_recv();
290 g_assert(ret == 0x20);
291 floppy_recv();
294 static void test_relative_seek(void)
296 uint8_t drive = 0;
297 uint8_t head = 0;
298 uint8_t cyl = 1;
299 uint8_t pcn;
301 /* Send seek to track 0 */
302 send_seek(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));
308 floppy_send(cyl);
310 ack_irq(&pcn);
311 g_assert(pcn == 1);
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));
317 floppy_send(cyl);
319 ack_irq(&pcn);
320 g_assert(pcn == 0);
323 /* success if no crash or abort */
324 static void fuzz_registers(void)
326 unsigned int i;
328 for (i = 0; i < 1000; i++) {
329 uint8_t reg, val;
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();
342 char *cmdline;
343 int fd;
344 int ret;
346 /* Check architecture */
347 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
348 g_test_message("Skipping test for non-x86\n");
349 return 0;
352 /* Create a temporary raw image */
353 fd = mkstemp(test_image);
354 g_assert(fd >= 0);
355 ret = ftruncate(fd, TEST_IMAGE_SIZE);
356 g_assert(ret == 0);
357 close(fd);
359 /* Run the tests */
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);
374 ret = g_test_run();
376 /* Cleanup */
377 qtest_quit(global_qtest);
378 unlink(test_image);
380 return ret;