]>
Commit | Line | Data |
---|---|---|
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 | ||
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 { | |
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 | ||
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 | ||
7cd33161 PH |
69 | static uint8_t base = 0x70; |
70 | ||
71 | enum { | |
72 | CMOS_FLOPPY = 0x10, | |
73 | }; | |
74 | ||
93e9eb68 KW |
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 | ||
98272dbb | 96 | static uint8_t ack_irq(void) |
93e9eb68 | 97 | { |
98272dbb PH |
98 | uint8_t ret; |
99 | ||
93e9eb68 KW |
100 | g_assert(get_irq(FLOPPY_IRQ)); |
101 | floppy_send(CMD_SENSE_INT); | |
102 | floppy_recv(); | |
98272dbb | 103 | ret = floppy_recv(); |
93e9eb68 | 104 | g_assert(!get_irq(FLOPPY_IRQ)); |
98272dbb PH |
105 | |
106 | return ret; | |
93e9eb68 KW |
107 | } |
108 | ||
8b9ef60d PH |
109 | static uint8_t send_read_command(void) |
110 | { | |
111 | uint8_t drive = 0; | |
112 | uint8_t head = 0; | |
113 | uint8_t cyl = 0; | |
114 | uint8_t sect_addr = 1; | |
115 | uint8_t sect_size = 2; | |
116 | uint8_t eot = 1; | |
117 | uint8_t gap = 0x1b; | |
118 | uint8_t gpl = 0xff; | |
119 | ||
120 | uint8_t msr = 0; | |
121 | uint8_t st0; | |
122 | ||
123 | uint8_t ret = 0; | |
124 | ||
125 | floppy_send(CMD_READ); | |
126 | floppy_send(head << 2 | drive); | |
127 | g_assert(!get_irq(FLOPPY_IRQ)); | |
128 | floppy_send(cyl); | |
129 | floppy_send(head); | |
130 | floppy_send(sect_addr); | |
131 | floppy_send(sect_size); | |
132 | floppy_send(eot); | |
133 | floppy_send(gap); | |
134 | floppy_send(gpl); | |
135 | ||
136 | uint8_t i = 0; | |
137 | uint8_t n = 2; | |
138 | for (; i < n; i++) { | |
139 | msr = inb(FLOPPY_BASE + reg_msr); | |
140 | if (msr == 0xd0) { | |
141 | break; | |
142 | } | |
143 | sleep(1); | |
144 | } | |
145 | ||
146 | if (i >= n) { | |
147 | return 1; | |
148 | } | |
149 | ||
150 | st0 = floppy_recv(); | |
b3ce604e | 151 | if (st0 != 0x60) { |
8b9ef60d PH |
152 | ret = 1; |
153 | } | |
154 | ||
155 | floppy_recv(); | |
156 | floppy_recv(); | |
157 | floppy_recv(); | |
158 | floppy_recv(); | |
159 | floppy_recv(); | |
160 | floppy_recv(); | |
161 | ||
162 | return ret; | |
163 | } | |
164 | ||
59240c34 | 165 | static void send_step_pulse(int cyl) |
93e9eb68 KW |
166 | { |
167 | int drive = 0; | |
168 | int head = 0; | |
93e9eb68 KW |
169 | |
170 | floppy_send(CMD_SEEK); | |
171 | floppy_send(head << 2 | drive); | |
172 | g_assert(!get_irq(FLOPPY_IRQ)); | |
173 | floppy_send(cyl); | |
174 | ack_irq(); | |
93e9eb68 KW |
175 | } |
176 | ||
7cd33161 | 177 | static uint8_t cmos_read(uint8_t reg) |
93e9eb68 | 178 | { |
7cd33161 PH |
179 | outb(base + 0, reg); |
180 | return inb(base + 1); | |
181 | } | |
93e9eb68 | 182 | |
7cd33161 PH |
183 | static void test_cmos(void) |
184 | { | |
185 | uint8_t cmos; | |
93e9eb68 | 186 | |
7cd33161 PH |
187 | cmos = cmos_read(CMOS_FLOPPY); |
188 | g_assert(cmos == 0x40); | |
189 | } | |
93e9eb68 | 190 | |
7cd33161 PH |
191 | static void test_no_media_on_start(void) |
192 | { | |
193 | uint8_t dir; | |
194 | ||
195 | /* Media changed bit must be set all time after start if there is | |
196 | * no media in drive. */ | |
93e9eb68 KW |
197 | dir = inb(FLOPPY_BASE + reg_dir); |
198 | assert_bit_set(dir, DSKCHG); | |
199 | dir = inb(FLOPPY_BASE + reg_dir); | |
200 | assert_bit_set(dir, DSKCHG); | |
59240c34 | 201 | send_step_pulse(1); |
93e9eb68 KW |
202 | dir = inb(FLOPPY_BASE + reg_dir); |
203 | assert_bit_set(dir, DSKCHG); | |
204 | dir = inb(FLOPPY_BASE + reg_dir); | |
205 | assert_bit_set(dir, DSKCHG); | |
7cd33161 PH |
206 | } |
207 | ||
8b9ef60d PH |
208 | static void test_read_without_media(void) |
209 | { | |
210 | uint8_t ret; | |
211 | ||
212 | ret = send_read_command(); | |
213 | g_assert(ret == 0); | |
214 | } | |
215 | ||
7cd33161 PH |
216 | static void test_media_change(void) |
217 | { | |
218 | uint8_t dir; | |
93e9eb68 | 219 | |
7cd33161 | 220 | /* Insert media in drive. DSKCHK should not be reset until a step pulse |
93e9eb68 KW |
221 | * is sent. */ |
222 | qmp("{'execute':'change', 'arguments':{ 'device':'floppy0', " | |
223 | "'target': '%s' }}", test_image); | |
224 | qmp(""); /* ignore event (FIXME open -> open transition?!) */ | |
225 | qmp(""); /* ignore event */ | |
226 | ||
227 | dir = inb(FLOPPY_BASE + reg_dir); | |
228 | assert_bit_set(dir, DSKCHG); | |
229 | dir = inb(FLOPPY_BASE + reg_dir); | |
230 | assert_bit_set(dir, DSKCHG); | |
231 | ||
59240c34 PH |
232 | send_step_pulse(0); |
233 | dir = inb(FLOPPY_BASE + reg_dir); | |
234 | assert_bit_set(dir, DSKCHG); | |
235 | dir = inb(FLOPPY_BASE + reg_dir); | |
236 | assert_bit_set(dir, DSKCHG); | |
237 | ||
238 | /* Step to next track should clear DSKCHG bit. */ | |
239 | send_step_pulse(1); | |
93e9eb68 KW |
240 | dir = inb(FLOPPY_BASE + reg_dir); |
241 | assert_bit_clear(dir, DSKCHG); | |
242 | dir = inb(FLOPPY_BASE + reg_dir); | |
243 | assert_bit_clear(dir, DSKCHG); | |
7cd33161 PH |
244 | |
245 | /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't | |
246 | * reset the bit. */ | |
247 | qmp("{'execute':'eject', 'arguments':{ 'device':'floppy0' }}"); | |
248 | qmp(""); /* ignore event */ | |
249 | ||
250 | dir = inb(FLOPPY_BASE + reg_dir); | |
251 | assert_bit_set(dir, DSKCHG); | |
252 | dir = inb(FLOPPY_BASE + reg_dir); | |
253 | assert_bit_set(dir, DSKCHG); | |
254 | ||
59240c34 PH |
255 | send_step_pulse(0); |
256 | dir = inb(FLOPPY_BASE + reg_dir); | |
257 | assert_bit_set(dir, DSKCHG); | |
258 | dir = inb(FLOPPY_BASE + reg_dir); | |
259 | assert_bit_set(dir, DSKCHG); | |
260 | ||
261 | send_step_pulse(1); | |
7cd33161 PH |
262 | dir = inb(FLOPPY_BASE + reg_dir); |
263 | assert_bit_set(dir, DSKCHG); | |
264 | dir = inb(FLOPPY_BASE + reg_dir); | |
265 | assert_bit_set(dir, DSKCHG); | |
93e9eb68 KW |
266 | } |
267 | ||
b3ce604e PH |
268 | static void test_sense_interrupt(void) |
269 | { | |
270 | int drive = 0; | |
271 | int head = 0; | |
272 | int cyl = 0; | |
273 | int ret = 0; | |
274 | ||
275 | floppy_send(CMD_SENSE_INT); | |
276 | ret = floppy_recv(); | |
277 | g_assert(ret == 0x80); | |
278 | ||
279 | floppy_send(CMD_SEEK); | |
280 | floppy_send(head << 2 | drive); | |
281 | g_assert(!get_irq(FLOPPY_IRQ)); | |
282 | floppy_send(cyl); | |
283 | ||
284 | floppy_send(CMD_SENSE_INT); | |
285 | ret = floppy_recv(); | |
286 | g_assert(ret == 0x20); | |
287 | floppy_recv(); | |
288 | } | |
289 | ||
98272dbb PH |
290 | static void test_relative_seek(void) |
291 | { | |
292 | uint8_t drive = 0; | |
293 | uint8_t head = 0; | |
294 | uint8_t cyl = 1; | |
295 | uint8_t ret; | |
296 | ||
297 | /* Send seek to track 0 */ | |
298 | send_step_pulse(0); | |
299 | ||
300 | /* Send relative seek to increase track by 1 */ | |
301 | floppy_send(CMD_RELATIVE_SEEK_IN); | |
302 | floppy_send(head << 2 | drive); | |
303 | g_assert(!get_irq(FLOPPY_IRQ)); | |
304 | floppy_send(cyl); | |
305 | ||
306 | ret = ack_irq(); | |
307 | g_assert(ret == 1); | |
308 | ||
309 | /* Send relative seek to decrease track by 1 */ | |
310 | floppy_send(CMD_RELATIVE_SEEK_OUT); | |
311 | floppy_send(head << 2 | drive); | |
312 | g_assert(!get_irq(FLOPPY_IRQ)); | |
313 | floppy_send(cyl); | |
314 | ||
315 | ret = ack_irq(); | |
316 | g_assert(ret == 0); | |
317 | } | |
318 | ||
3359847e BS |
319 | /* success if no crash or abort */ |
320 | static void fuzz_registers(void) | |
321 | { | |
322 | unsigned int i; | |
323 | ||
324 | for (i = 0; i < 1000; i++) { | |
325 | uint8_t reg, val; | |
326 | ||
327 | reg = (uint8_t)g_test_rand_int_range(0, 8); | |
328 | val = (uint8_t)g_test_rand_int_range(0, 256); | |
329 | ||
330 | outb(FLOPPY_BASE + reg, val); | |
331 | inb(FLOPPY_BASE + reg); | |
332 | } | |
333 | } | |
334 | ||
93e9eb68 KW |
335 | int main(int argc, char **argv) |
336 | { | |
337 | const char *arch = qtest_get_arch(); | |
338 | char *cmdline; | |
339 | int fd; | |
340 | int ret; | |
341 | ||
342 | /* Check architecture */ | |
343 | if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) { | |
344 | g_test_message("Skipping test for non-x86\n"); | |
345 | return 0; | |
346 | } | |
347 | ||
348 | /* Create a temporary raw image */ | |
349 | fd = mkstemp(test_image); | |
350 | g_assert(fd >= 0); | |
351 | ret = ftruncate(fd, TEST_IMAGE_SIZE); | |
352 | g_assert(ret == 0); | |
353 | close(fd); | |
354 | ||
355 | /* Run the tests */ | |
356 | g_test_init(&argc, &argv, NULL); | |
357 | ||
7cd33161 | 358 | cmdline = g_strdup_printf("-vnc none "); |
93e9eb68 KW |
359 | |
360 | qtest_start(cmdline); | |
361 | qtest_irq_intercept_in(global_qtest, "ioapic"); | |
7cd33161 PH |
362 | qtest_add_func("/fdc/cmos", test_cmos); |
363 | qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start); | |
8b9ef60d | 364 | qtest_add_func("/fdc/read_without_media", test_read_without_media); |
93e9eb68 | 365 | qtest_add_func("/fdc/media_change", test_media_change); |
b3ce604e | 366 | qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt); |
98272dbb | 367 | qtest_add_func("/fdc/relative_seek", test_relative_seek); |
3359847e | 368 | qtest_add_func("/fdc/fuzz-registers", fuzz_registers); |
93e9eb68 KW |
369 | |
370 | ret = g_test_run(); | |
371 | ||
372 | /* Cleanup */ | |
373 | qtest_quit(global_qtest); | |
374 | unlink(test_image); | |
375 | ||
376 | return ret; | |
377 | } |