]> git.proxmox.com Git - qemu.git/blame - tests/fdc-test.c
Merge remote-tracking branch 'sstabellini/compile-xs' into staging
[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 {
50 CMD_SENSE_INT = 0x08,
51 CMD_SEEK = 0x0f,
8b9ef60d 52 CMD_READ = 0xe6,
93e9eb68
KW
53};
54
55enum {
56 RQM = 0x80,
57 DIO = 0x40,
58
59 DSKCHG = 0x80,
60};
61
62char test_image[] = "/tmp/qtest.XXXXXX";
63
64#define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
65#define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
66
7cd33161
PH
67static uint8_t base = 0x70;
68
69enum {
70 CMOS_FLOPPY = 0x10,
71};
72
93e9eb68
KW
73static void floppy_send(uint8_t byte)
74{
75 uint8_t msr;
76
77 msr = inb(FLOPPY_BASE + reg_msr);
78 assert_bit_set(msr, RQM);
79 assert_bit_clear(msr, DIO);
80
81 outb(FLOPPY_BASE + reg_fifo, byte);
82}
83
84static uint8_t floppy_recv(void)
85{
86 uint8_t msr;
87
88 msr = inb(FLOPPY_BASE + reg_msr);
89 assert_bit_set(msr, RQM | DIO);
90
91 return inb(FLOPPY_BASE + reg_fifo);
92}
93
94static void ack_irq(void)
95{
96 g_assert(get_irq(FLOPPY_IRQ));
97 floppy_send(CMD_SENSE_INT);
98 floppy_recv();
99 floppy_recv();
100 g_assert(!get_irq(FLOPPY_IRQ));
101}
102
8b9ef60d
PH
103static uint8_t send_read_command(void)
104{
105 uint8_t drive = 0;
106 uint8_t head = 0;
107 uint8_t cyl = 0;
108 uint8_t sect_addr = 1;
109 uint8_t sect_size = 2;
110 uint8_t eot = 1;
111 uint8_t gap = 0x1b;
112 uint8_t gpl = 0xff;
113
114 uint8_t msr = 0;
115 uint8_t st0;
116
117 uint8_t ret = 0;
118
119 floppy_send(CMD_READ);
120 floppy_send(head << 2 | drive);
121 g_assert(!get_irq(FLOPPY_IRQ));
122 floppy_send(cyl);
123 floppy_send(head);
124 floppy_send(sect_addr);
125 floppy_send(sect_size);
126 floppy_send(eot);
127 floppy_send(gap);
128 floppy_send(gpl);
129
130 uint8_t i = 0;
131 uint8_t n = 2;
132 for (; i < n; i++) {
133 msr = inb(FLOPPY_BASE + reg_msr);
134 if (msr == 0xd0) {
135 break;
136 }
137 sleep(1);
138 }
139
140 if (i >= n) {
141 return 1;
142 }
143
144 st0 = floppy_recv();
145 if (st0 != 0x40) {
146 ret = 1;
147 }
148
149 floppy_recv();
150 floppy_recv();
151 floppy_recv();
152 floppy_recv();
153 floppy_recv();
154 floppy_recv();
155
156 return ret;
157}
158
93e9eb68
KW
159static void send_step_pulse(void)
160{
161 int drive = 0;
162 int head = 0;
163 static int cyl = 0;
164
165 floppy_send(CMD_SEEK);
166 floppy_send(head << 2 | drive);
167 g_assert(!get_irq(FLOPPY_IRQ));
168 floppy_send(cyl);
169 ack_irq();
170
171 cyl = (cyl + 1) % 4;
172}
173
7cd33161 174static uint8_t cmos_read(uint8_t reg)
93e9eb68 175{
7cd33161
PH
176 outb(base + 0, reg);
177 return inb(base + 1);
178}
93e9eb68 179
7cd33161
PH
180static void test_cmos(void)
181{
182 uint8_t cmos;
93e9eb68 183
7cd33161
PH
184 cmos = cmos_read(CMOS_FLOPPY);
185 g_assert(cmos == 0x40);
186}
93e9eb68 187
7cd33161
PH
188static void test_no_media_on_start(void)
189{
190 uint8_t dir;
191
192 /* Media changed bit must be set all time after start if there is
193 * no media in drive. */
93e9eb68
KW
194 dir = inb(FLOPPY_BASE + reg_dir);
195 assert_bit_set(dir, DSKCHG);
196 dir = inb(FLOPPY_BASE + reg_dir);
197 assert_bit_set(dir, DSKCHG);
7cd33161 198 send_step_pulse();
93e9eb68
KW
199 send_step_pulse();
200 dir = inb(FLOPPY_BASE + reg_dir);
201 assert_bit_set(dir, DSKCHG);
202 dir = inb(FLOPPY_BASE + reg_dir);
203 assert_bit_set(dir, DSKCHG);
7cd33161
PH
204}
205
8b9ef60d
PH
206static void test_read_without_media(void)
207{
208 uint8_t ret;
209
210 ret = send_read_command();
211 g_assert(ret == 0);
212}
213
7cd33161
PH
214static void test_media_change(void)
215{
216 uint8_t dir;
93e9eb68 217
7cd33161 218 /* Insert media in drive. DSKCHK should not be reset until a step pulse
93e9eb68
KW
219 * is sent. */
220 qmp("{'execute':'change', 'arguments':{ 'device':'floppy0', "
221 "'target': '%s' }}", test_image);
222 qmp(""); /* ignore event (FIXME open -> open transition?!) */
223 qmp(""); /* ignore event */
224
225 dir = inb(FLOPPY_BASE + reg_dir);
226 assert_bit_set(dir, DSKCHG);
227 dir = inb(FLOPPY_BASE + reg_dir);
228 assert_bit_set(dir, DSKCHG);
229
230 send_step_pulse();
231 dir = inb(FLOPPY_BASE + reg_dir);
232 assert_bit_clear(dir, DSKCHG);
233 dir = inb(FLOPPY_BASE + reg_dir);
234 assert_bit_clear(dir, DSKCHG);
7cd33161
PH
235
236 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
237 * reset the bit. */
238 qmp("{'execute':'eject', 'arguments':{ 'device':'floppy0' }}");
239 qmp(""); /* ignore event */
240
241 dir = inb(FLOPPY_BASE + reg_dir);
242 assert_bit_set(dir, DSKCHG);
243 dir = inb(FLOPPY_BASE + reg_dir);
244 assert_bit_set(dir, DSKCHG);
245
246 send_step_pulse();
247 dir = inb(FLOPPY_BASE + reg_dir);
248 assert_bit_set(dir, DSKCHG);
249 dir = inb(FLOPPY_BASE + reg_dir);
250 assert_bit_set(dir, DSKCHG);
93e9eb68
KW
251}
252
3359847e
BS
253/* success if no crash or abort */
254static void fuzz_registers(void)
255{
256 unsigned int i;
257
258 for (i = 0; i < 1000; i++) {
259 uint8_t reg, val;
260
261 reg = (uint8_t)g_test_rand_int_range(0, 8);
262 val = (uint8_t)g_test_rand_int_range(0, 256);
263
264 outb(FLOPPY_BASE + reg, val);
265 inb(FLOPPY_BASE + reg);
266 }
267}
268
93e9eb68
KW
269int main(int argc, char **argv)
270{
271 const char *arch = qtest_get_arch();
272 char *cmdline;
273 int fd;
274 int ret;
275
276 /* Check architecture */
277 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
278 g_test_message("Skipping test for non-x86\n");
279 return 0;
280 }
281
282 /* Create a temporary raw image */
283 fd = mkstemp(test_image);
284 g_assert(fd >= 0);
285 ret = ftruncate(fd, TEST_IMAGE_SIZE);
286 g_assert(ret == 0);
287 close(fd);
288
289 /* Run the tests */
290 g_test_init(&argc, &argv, NULL);
291
7cd33161 292 cmdline = g_strdup_printf("-vnc none ");
93e9eb68
KW
293
294 qtest_start(cmdline);
295 qtest_irq_intercept_in(global_qtest, "ioapic");
7cd33161
PH
296 qtest_add_func("/fdc/cmos", test_cmos);
297 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start);
8b9ef60d 298 qtest_add_func("/fdc/read_without_media", test_read_without_media);
93e9eb68 299 qtest_add_func("/fdc/media_change", test_media_change);
3359847e 300 qtest_add_func("/fdc/fuzz-registers", fuzz_registers);
93e9eb68
KW
301
302 ret = g_test_run();
303
304 /* Cleanup */
305 qtest_quit(global_qtest);
306 unlink(test_image);
307
308 return ret;
309}