]> git.proxmox.com Git - qemu.git/blob - tests/fdc-test.c
Merge remote-tracking branch 'kraxel/usb.68' into staging
[qemu.git] / tests / fdc-test.c
1 /*
2 * Floppy test cases.
3 *
4 * Copyright (c) 2012 Kevin Wolf <kwolf@redhat.com>
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 #include <stdint.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include <glib.h>
30
31 #include "libqtest.h"
32 #include "qemu-common.h"
33
34 #define TEST_IMAGE_SIZE 1440 * 1024
35
36 #define FLOPPY_BASE 0x3f0
37 #define FLOPPY_IRQ 6
38
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,
47 };
48
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,
55 };
56
57 enum {
58 RQM = 0x80,
59 DIO = 0x40,
60
61 DSKCHG = 0x80,
62 };
63
64 char test_image[] = "/tmp/qtest.XXXXXX";
65
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)
68
69 static uint8_t base = 0x70;
70
71 enum {
72 CMOS_FLOPPY = 0x10,
73 };
74
75 static void floppy_send(uint8_t byte)
76 {
77 uint8_t msr;
78
79 msr = inb(FLOPPY_BASE + reg_msr);
80 assert_bit_set(msr, RQM);
81 assert_bit_clear(msr, DIO);
82
83 outb(FLOPPY_BASE + reg_fifo, byte);
84 }
85
86 static uint8_t floppy_recv(void)
87 {
88 uint8_t msr;
89
90 msr = inb(FLOPPY_BASE + reg_msr);
91 assert_bit_set(msr, RQM | DIO);
92
93 return inb(FLOPPY_BASE + reg_fifo);
94 }
95
96 /* pcn: Present Cylinder Number */
97 static void ack_irq(uint8_t *pcn)
98 {
99 uint8_t ret;
100
101 g_assert(get_irq(FLOPPY_IRQ));
102 floppy_send(CMD_SENSE_INT);
103 floppy_recv();
104
105 ret = floppy_recv();
106 if (pcn != NULL) {
107 *pcn = ret;
108 }
109
110 g_assert(!get_irq(FLOPPY_IRQ));
111 }
112
113 static uint8_t send_read_command(void)
114 {
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;
123
124 uint8_t msr = 0;
125 uint8_t st0;
126
127 uint8_t ret = 0;
128
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);
139
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;
146 }
147 sleep(1);
148 }
149
150 if (i >= n) {
151 return 1;
152 }
153
154 st0 = floppy_recv();
155 if (st0 != 0x60) {
156 ret = 1;
157 }
158
159 floppy_recv();
160 floppy_recv();
161 floppy_recv();
162 floppy_recv();
163 floppy_recv();
164 floppy_recv();
165
166 return ret;
167 }
168
169 static void send_seek(int cyl)
170 {
171 int drive = 0;
172 int head = 0;
173
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);
179 }
180
181 static uint8_t cmos_read(uint8_t reg)
182 {
183 outb(base + 0, reg);
184 return inb(base + 1);
185 }
186
187 static void test_cmos(void)
188 {
189 uint8_t cmos;
190
191 cmos = cmos_read(CMOS_FLOPPY);
192 g_assert(cmos == 0x40);
193 }
194
195 static void test_no_media_on_start(void)
196 {
197 uint8_t dir;
198
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);
210 }
211
212 static void test_read_without_media(void)
213 {
214 uint8_t ret;
215
216 ret = send_read_command();
217 g_assert(ret == 0);
218 }
219
220 static void test_media_change(void)
221 {
222 uint8_t dir;
223
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 */
230
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);
235
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);
241
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);
248
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 */
253
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);
258
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);
264
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);
270 }
271
272 static void test_sense_interrupt(void)
273 {
274 int drive = 0;
275 int head = 0;
276 int cyl = 0;
277 int ret = 0;
278
279 floppy_send(CMD_SENSE_INT);
280 ret = floppy_recv();
281 g_assert(ret == 0x80);
282
283 floppy_send(CMD_SEEK);
284 floppy_send(head << 2 | drive);
285 g_assert(!get_irq(FLOPPY_IRQ));
286 floppy_send(cyl);
287
288 floppy_send(CMD_SENSE_INT);
289 ret = floppy_recv();
290 g_assert(ret == 0x20);
291 floppy_recv();
292 }
293
294 static void test_relative_seek(void)
295 {
296 uint8_t drive = 0;
297 uint8_t head = 0;
298 uint8_t cyl = 1;
299 uint8_t pcn;
300
301 /* Send seek to track 0 */
302 send_seek(0);
303
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);
309
310 ack_irq(&pcn);
311 g_assert(pcn == 1);
312
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);
318
319 ack_irq(&pcn);
320 g_assert(pcn == 0);
321 }
322
323 /* success if no crash or abort */
324 static void fuzz_registers(void)
325 {
326 unsigned int i;
327
328 for (i = 0; i < 1000; i++) {
329 uint8_t reg, val;
330
331 reg = (uint8_t)g_test_rand_int_range(0, 8);
332 val = (uint8_t)g_test_rand_int_range(0, 256);
333
334 outb(FLOPPY_BASE + reg, val);
335 inb(FLOPPY_BASE + reg);
336 }
337 }
338
339 int main(int argc, char **argv)
340 {
341 const char *arch = qtest_get_arch();
342 char *cmdline;
343 int fd;
344 int ret;
345
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;
350 }
351
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);
358
359 /* Run the tests */
360 g_test_init(&argc, &argv, NULL);
361
362 cmdline = g_strdup_printf("-vnc none ");
363
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);
373
374 ret = g_test_run();
375
376 /* Cleanup */
377 qtest_quit(global_qtest);
378 unlink(test_image);
379
380 return ret;
381 }