]> git.proxmox.com Git - mirror_qemu.git/blame - tests/fdc-test.c
fdc-test: split test_media_change() test, so insert part can be reused
[mirror_qemu.git] / tests / fdc-test.c
CommitLineData
93e9eb68
KW
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
39enum {
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
49enum {
98272dbb
PH
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,
93e9eb68
KW
55};
56
57enum {
58 RQM = 0x80,
59 DIO = 0x40,
60
61 DSKCHG = 0x80,
62};
63
64char 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
7cd33161
PH
69static uint8_t base = 0x70;
70
71enum {
72 CMOS_FLOPPY = 0x10,
73};
74
93e9eb68
KW
75static 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
86static 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
c3cdc1b0
KW
96/* pcn: Present Cylinder Number */
97static void ack_irq(uint8_t *pcn)
93e9eb68 98{
98272dbb
PH
99 uint8_t ret;
100
93e9eb68
KW
101 g_assert(get_irq(FLOPPY_IRQ));
102 floppy_send(CMD_SENSE_INT);
103 floppy_recv();
c3cdc1b0 104
98272dbb 105 ret = floppy_recv();
c3cdc1b0
KW
106 if (pcn != NULL) {
107 *pcn = ret;
108 }
98272dbb 109
c3cdc1b0 110 g_assert(!get_irq(FLOPPY_IRQ));
93e9eb68
KW
111}
112
8b9ef60d
PH
113static 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();
b3ce604e 155 if (st0 != 0x60) {
8b9ef60d
PH
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
c3cdc1b0 169static void send_seek(int cyl)
93e9eb68
KW
170{
171 int drive = 0;
172 int head = 0;
93e9eb68
KW
173
174 floppy_send(CMD_SEEK);
175 floppy_send(head << 2 | drive);
176 g_assert(!get_irq(FLOPPY_IRQ));
177 floppy_send(cyl);
c3cdc1b0 178 ack_irq(NULL);
93e9eb68
KW
179}
180
7cd33161 181static uint8_t cmos_read(uint8_t reg)
93e9eb68 182{
7cd33161
PH
183 outb(base + 0, reg);
184 return inb(base + 1);
185}
93e9eb68 186
7cd33161
PH
187static void test_cmos(void)
188{
189 uint8_t cmos;
93e9eb68 190
7cd33161
PH
191 cmos = cmos_read(CMOS_FLOPPY);
192 g_assert(cmos == 0x40);
193}
93e9eb68 194
7cd33161
PH
195static 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. */
93e9eb68
KW
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);
c3cdc1b0 205 send_seek(1);
93e9eb68
KW
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);
7cd33161
PH
210}
211
8b9ef60d
PH
212static void test_read_without_media(void)
213{
214 uint8_t ret;
215
216 ret = send_read_command();
217 g_assert(ret == 0);
218}
219
1f507913 220static void test_media_insert(void)
7cd33161
PH
221{
222 uint8_t dir;
93e9eb68 223
7cd33161 224 /* Insert media in drive. DSKCHK should not be reset until a step pulse
93e9eb68
KW
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
c3cdc1b0 236 send_seek(0);
59240c34
PH
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. */
c3cdc1b0 243 send_seek(1);
93e9eb68
KW
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);
1f507913
HP
248}
249
250static void test_media_change(void)
251{
252 uint8_t dir;
253
254 test_media_insert();
7cd33161
PH
255
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 */
260
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);
265
c3cdc1b0 266 send_seek(0);
59240c34
PH
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);
271
c3cdc1b0 272 send_seek(1);
7cd33161
PH
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);
93e9eb68
KW
277}
278
b3ce604e
PH
279static void test_sense_interrupt(void)
280{
281 int drive = 0;
282 int head = 0;
283 int cyl = 0;
284 int ret = 0;
285
286 floppy_send(CMD_SENSE_INT);
287 ret = floppy_recv();
288 g_assert(ret == 0x80);
289
290 floppy_send(CMD_SEEK);
291 floppy_send(head << 2 | drive);
292 g_assert(!get_irq(FLOPPY_IRQ));
293 floppy_send(cyl);
294
295 floppy_send(CMD_SENSE_INT);
296 ret = floppy_recv();
297 g_assert(ret == 0x20);
298 floppy_recv();
299}
300
98272dbb
PH
301static void test_relative_seek(void)
302{
303 uint8_t drive = 0;
304 uint8_t head = 0;
305 uint8_t cyl = 1;
c3cdc1b0 306 uint8_t pcn;
98272dbb
PH
307
308 /* Send seek to track 0 */
c3cdc1b0 309 send_seek(0);
98272dbb
PH
310
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);
316
c3cdc1b0
KW
317 ack_irq(&pcn);
318 g_assert(pcn == 1);
98272dbb
PH
319
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);
325
c3cdc1b0
KW
326 ack_irq(&pcn);
327 g_assert(pcn == 0);
98272dbb
PH
328}
329
3359847e
BS
330/* success if no crash or abort */
331static void fuzz_registers(void)
332{
333 unsigned int i;
334
335 for (i = 0; i < 1000; i++) {
336 uint8_t reg, val;
337
338 reg = (uint8_t)g_test_rand_int_range(0, 8);
339 val = (uint8_t)g_test_rand_int_range(0, 256);
340
341 outb(FLOPPY_BASE + reg, val);
342 inb(FLOPPY_BASE + reg);
343 }
344}
345
93e9eb68
KW
346int main(int argc, char **argv)
347{
348 const char *arch = qtest_get_arch();
349 char *cmdline;
350 int fd;
351 int ret;
352
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;
357 }
358
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);
365
366 /* Run the tests */
367 g_test_init(&argc, &argv, NULL);
368
7cd33161 369 cmdline = g_strdup_printf("-vnc none ");
93e9eb68
KW
370
371 qtest_start(cmdline);
372 qtest_irq_intercept_in(global_qtest, "ioapic");
7cd33161
PH
373 qtest_add_func("/fdc/cmos", test_cmos);
374 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start);
8b9ef60d 375 qtest_add_func("/fdc/read_without_media", test_read_without_media);
93e9eb68 376 qtest_add_func("/fdc/media_change", test_media_change);
b3ce604e 377 qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt);
98272dbb 378 qtest_add_func("/fdc/relative_seek", test_relative_seek);
3359847e 379 qtest_add_func("/fdc/fuzz-registers", fuzz_registers);
93e9eb68
KW
380
381 ret = g_test_run();
382
383 /* Cleanup */
384 qtest_quit(global_qtest);
385 unlink(test_image);
386
387 return ret;
388}