nbd: enable use of TLS with nbd-server-start command
[qemu.git] / tests / fdc-test.c
blobdbabf50a9a504aed2d8186b52875119bb8ae69f1
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_READ_ID = 0x0a,
52 CMD_SEEK = 0x0f,
53 CMD_VERIFY = 0x16,
54 CMD_READ = 0xe6,
55 CMD_RELATIVE_SEEK_OUT = 0x8f,
56 CMD_RELATIVE_SEEK_IN = 0xcf,
59 enum {
60 BUSY = 0x10,
61 NONDMA = 0x20,
62 RQM = 0x80,
63 DIO = 0x40,
65 DSKCHG = 0x80,
68 static char test_image[] = "/tmp/qtest.XXXXXX";
70 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
71 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
73 static uint8_t base = 0x70;
75 enum {
76 CMOS_FLOPPY = 0x10,
79 static void floppy_send(uint8_t byte)
81 uint8_t msr;
83 msr = inb(FLOPPY_BASE + reg_msr);
84 assert_bit_set(msr, RQM);
85 assert_bit_clear(msr, DIO);
87 outb(FLOPPY_BASE + reg_fifo, byte);
90 static uint8_t floppy_recv(void)
92 uint8_t msr;
94 msr = inb(FLOPPY_BASE + reg_msr);
95 assert_bit_set(msr, RQM | DIO);
97 return inb(FLOPPY_BASE + reg_fifo);
100 /* pcn: Present Cylinder Number */
101 static void ack_irq(uint8_t *pcn)
103 uint8_t ret;
105 g_assert(get_irq(FLOPPY_IRQ));
106 floppy_send(CMD_SENSE_INT);
107 floppy_recv();
109 ret = floppy_recv();
110 if (pcn != NULL) {
111 *pcn = ret;
114 g_assert(!get_irq(FLOPPY_IRQ));
117 static uint8_t send_read_command(uint8_t cmd)
119 uint8_t drive = 0;
120 uint8_t head = 0;
121 uint8_t cyl = 0;
122 uint8_t sect_addr = 1;
123 uint8_t sect_size = 2;
124 uint8_t eot = 1;
125 uint8_t gap = 0x1b;
126 uint8_t gpl = 0xff;
128 uint8_t msr = 0;
129 uint8_t st0;
131 uint8_t ret = 0;
133 floppy_send(cmd);
134 floppy_send(head << 2 | drive);
135 g_assert(!get_irq(FLOPPY_IRQ));
136 floppy_send(cyl);
137 floppy_send(head);
138 floppy_send(sect_addr);
139 floppy_send(sect_size);
140 floppy_send(eot);
141 floppy_send(gap);
142 floppy_send(gpl);
144 uint8_t i = 0;
145 uint8_t n = 2;
146 for (; i < n; i++) {
147 msr = inb(FLOPPY_BASE + reg_msr);
148 if (msr == 0xd0) {
149 break;
151 sleep(1);
154 if (i >= n) {
155 return 1;
158 st0 = floppy_recv();
159 if (st0 != 0x40) {
160 ret = 1;
163 floppy_recv();
164 floppy_recv();
165 floppy_recv();
166 floppy_recv();
167 floppy_recv();
168 floppy_recv();
170 return ret;
173 static uint8_t send_read_no_dma_command(int nb_sect, uint8_t expected_st0)
175 uint8_t drive = 0;
176 uint8_t head = 0;
177 uint8_t cyl = 0;
178 uint8_t sect_addr = 1;
179 uint8_t sect_size = 2;
180 uint8_t eot = nb_sect;
181 uint8_t gap = 0x1b;
182 uint8_t gpl = 0xff;
184 uint8_t msr = 0;
185 uint8_t st0;
187 uint8_t ret = 0;
189 floppy_send(CMD_READ);
190 floppy_send(head << 2 | drive);
191 g_assert(!get_irq(FLOPPY_IRQ));
192 floppy_send(cyl);
193 floppy_send(head);
194 floppy_send(sect_addr);
195 floppy_send(sect_size);
196 floppy_send(eot);
197 floppy_send(gap);
198 floppy_send(gpl);
200 uint16_t i = 0;
201 uint8_t n = 2;
202 for (; i < n; i++) {
203 msr = inb(FLOPPY_BASE + reg_msr);
204 if (msr == (BUSY | NONDMA | DIO | RQM)) {
205 break;
207 sleep(1);
210 if (i >= n) {
211 return 1;
214 /* Non-DMA mode */
215 for (i = 0; i < 512 * 2 * nb_sect; i++) {
216 msr = inb(FLOPPY_BASE + reg_msr);
217 assert_bit_set(msr, BUSY | RQM | DIO);
218 inb(FLOPPY_BASE + reg_fifo);
221 msr = inb(FLOPPY_BASE + reg_msr);
222 assert_bit_set(msr, BUSY | RQM | DIO);
223 g_assert(get_irq(FLOPPY_IRQ));
225 st0 = floppy_recv();
226 if (st0 != expected_st0) {
227 ret = 1;
230 floppy_recv();
231 floppy_recv();
232 floppy_recv();
233 floppy_recv();
234 floppy_recv();
235 g_assert(get_irq(FLOPPY_IRQ));
236 floppy_recv();
238 /* Check that we're back in command phase */
239 msr = inb(FLOPPY_BASE + reg_msr);
240 assert_bit_clear(msr, BUSY | DIO);
241 assert_bit_set(msr, RQM);
242 g_assert(!get_irq(FLOPPY_IRQ));
244 return ret;
247 static void send_seek(int cyl)
249 int drive = 0;
250 int head = 0;
252 floppy_send(CMD_SEEK);
253 floppy_send(head << 2 | drive);
254 g_assert(!get_irq(FLOPPY_IRQ));
255 floppy_send(cyl);
256 ack_irq(NULL);
259 static uint8_t cmos_read(uint8_t reg)
261 outb(base + 0, reg);
262 return inb(base + 1);
265 static void test_cmos(void)
267 uint8_t cmos;
269 cmos = cmos_read(CMOS_FLOPPY);
270 g_assert(cmos == 0x40 || cmos == 0x50);
273 static void test_no_media_on_start(void)
275 uint8_t dir;
277 /* Media changed bit must be set all time after start if there is
278 * no media in drive. */
279 dir = inb(FLOPPY_BASE + reg_dir);
280 assert_bit_set(dir, DSKCHG);
281 dir = inb(FLOPPY_BASE + reg_dir);
282 assert_bit_set(dir, DSKCHG);
283 send_seek(1);
284 dir = inb(FLOPPY_BASE + reg_dir);
285 assert_bit_set(dir, DSKCHG);
286 dir = inb(FLOPPY_BASE + reg_dir);
287 assert_bit_set(dir, DSKCHG);
290 static void test_read_without_media(void)
292 uint8_t ret;
294 ret = send_read_command(CMD_READ);
295 g_assert(ret == 0);
298 static void test_media_insert(void)
300 uint8_t dir;
302 /* Insert media in drive. DSKCHK should not be reset until a step pulse
303 * is sent. */
304 qmp_discard_response("{'execute':'change', 'arguments':{"
305 " 'device':'floppy0', 'target': %s, 'arg': 'raw' }}",
306 test_image);
308 dir = inb(FLOPPY_BASE + reg_dir);
309 assert_bit_set(dir, DSKCHG);
310 dir = inb(FLOPPY_BASE + reg_dir);
311 assert_bit_set(dir, DSKCHG);
313 send_seek(0);
314 dir = inb(FLOPPY_BASE + reg_dir);
315 assert_bit_set(dir, DSKCHG);
316 dir = inb(FLOPPY_BASE + reg_dir);
317 assert_bit_set(dir, DSKCHG);
319 /* Step to next track should clear DSKCHG bit. */
320 send_seek(1);
321 dir = inb(FLOPPY_BASE + reg_dir);
322 assert_bit_clear(dir, DSKCHG);
323 dir = inb(FLOPPY_BASE + reg_dir);
324 assert_bit_clear(dir, DSKCHG);
327 static void test_media_change(void)
329 uint8_t dir;
331 test_media_insert();
333 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
334 * reset the bit. */
335 qmp_discard_response("{'execute':'eject', 'arguments':{"
336 " 'device':'floppy0' }}");
338 dir = inb(FLOPPY_BASE + reg_dir);
339 assert_bit_set(dir, DSKCHG);
340 dir = inb(FLOPPY_BASE + reg_dir);
341 assert_bit_set(dir, DSKCHG);
343 send_seek(0);
344 dir = inb(FLOPPY_BASE + reg_dir);
345 assert_bit_set(dir, DSKCHG);
346 dir = inb(FLOPPY_BASE + reg_dir);
347 assert_bit_set(dir, DSKCHG);
349 send_seek(1);
350 dir = inb(FLOPPY_BASE + reg_dir);
351 assert_bit_set(dir, DSKCHG);
352 dir = inb(FLOPPY_BASE + reg_dir);
353 assert_bit_set(dir, DSKCHG);
356 static void test_sense_interrupt(void)
358 int drive = 0;
359 int head = 0;
360 int cyl = 0;
361 int ret = 0;
363 floppy_send(CMD_SENSE_INT);
364 ret = floppy_recv();
365 g_assert(ret == 0x80);
367 floppy_send(CMD_SEEK);
368 floppy_send(head << 2 | drive);
369 g_assert(!get_irq(FLOPPY_IRQ));
370 floppy_send(cyl);
372 floppy_send(CMD_SENSE_INT);
373 ret = floppy_recv();
374 g_assert(ret == 0x20);
375 floppy_recv();
378 static void test_relative_seek(void)
380 uint8_t drive = 0;
381 uint8_t head = 0;
382 uint8_t cyl = 1;
383 uint8_t pcn;
385 /* Send seek to track 0 */
386 send_seek(0);
388 /* Send relative seek to increase track by 1 */
389 floppy_send(CMD_RELATIVE_SEEK_IN);
390 floppy_send(head << 2 | drive);
391 g_assert(!get_irq(FLOPPY_IRQ));
392 floppy_send(cyl);
394 ack_irq(&pcn);
395 g_assert(pcn == 1);
397 /* Send relative seek to decrease track by 1 */
398 floppy_send(CMD_RELATIVE_SEEK_OUT);
399 floppy_send(head << 2 | drive);
400 g_assert(!get_irq(FLOPPY_IRQ));
401 floppy_send(cyl);
403 ack_irq(&pcn);
404 g_assert(pcn == 0);
407 static void test_read_id(void)
409 uint8_t drive = 0;
410 uint8_t head = 0;
411 uint8_t cyl;
412 uint8_t st0;
413 uint8_t msr;
415 /* Seek to track 0 and check with READ ID */
416 send_seek(0);
418 floppy_send(CMD_READ_ID);
419 g_assert(!get_irq(FLOPPY_IRQ));
420 floppy_send(head << 2 | drive);
422 msr = inb(FLOPPY_BASE + reg_msr);
423 if (!get_irq(FLOPPY_IRQ)) {
424 assert_bit_set(msr, BUSY);
425 assert_bit_clear(msr, RQM);
428 while (!get_irq(FLOPPY_IRQ)) {
429 /* qemu involves a timer with READ ID... */
430 clock_step(1000000000LL / 50);
433 msr = inb(FLOPPY_BASE + reg_msr);
434 assert_bit_set(msr, BUSY | RQM | DIO);
436 st0 = floppy_recv();
437 floppy_recv();
438 floppy_recv();
439 cyl = floppy_recv();
440 head = floppy_recv();
441 floppy_recv();
442 g_assert(get_irq(FLOPPY_IRQ));
443 floppy_recv();
444 g_assert(!get_irq(FLOPPY_IRQ));
446 g_assert_cmpint(cyl, ==, 0);
447 g_assert_cmpint(head, ==, 0);
448 g_assert_cmpint(st0, ==, head << 2);
450 /* Seek to track 8 on head 1 and check with READ ID */
451 head = 1;
452 cyl = 8;
454 floppy_send(CMD_SEEK);
455 floppy_send(head << 2 | drive);
456 g_assert(!get_irq(FLOPPY_IRQ));
457 floppy_send(cyl);
458 g_assert(get_irq(FLOPPY_IRQ));
459 ack_irq(NULL);
461 floppy_send(CMD_READ_ID);
462 g_assert(!get_irq(FLOPPY_IRQ));
463 floppy_send(head << 2 | drive);
465 msr = inb(FLOPPY_BASE + reg_msr);
466 if (!get_irq(FLOPPY_IRQ)) {
467 assert_bit_set(msr, BUSY);
468 assert_bit_clear(msr, RQM);
471 while (!get_irq(FLOPPY_IRQ)) {
472 /* qemu involves a timer with READ ID... */
473 clock_step(1000000000LL / 50);
476 msr = inb(FLOPPY_BASE + reg_msr);
477 assert_bit_set(msr, BUSY | RQM | DIO);
479 st0 = floppy_recv();
480 floppy_recv();
481 floppy_recv();
482 cyl = floppy_recv();
483 head = floppy_recv();
484 floppy_recv();
485 g_assert(get_irq(FLOPPY_IRQ));
486 floppy_recv();
487 g_assert(!get_irq(FLOPPY_IRQ));
489 g_assert_cmpint(cyl, ==, 8);
490 g_assert_cmpint(head, ==, 1);
491 g_assert_cmpint(st0, ==, head << 2);
494 static void test_read_no_dma_1(void)
496 uint8_t ret;
498 outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
499 send_seek(0);
500 ret = send_read_no_dma_command(1, 0x04);
501 g_assert(ret == 0);
504 static void test_read_no_dma_18(void)
506 uint8_t ret;
508 outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
509 send_seek(0);
510 ret = send_read_no_dma_command(18, 0x04);
511 g_assert(ret == 0);
514 static void test_read_no_dma_19(void)
516 uint8_t ret;
518 outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
519 send_seek(0);
520 ret = send_read_no_dma_command(19, 0x20);
521 g_assert(ret == 0);
524 static void test_verify(void)
526 uint8_t ret;
528 ret = send_read_command(CMD_VERIFY);
529 g_assert(ret == 0);
532 /* success if no crash or abort */
533 static void fuzz_registers(void)
535 unsigned int i;
537 for (i = 0; i < 1000; i++) {
538 uint8_t reg, val;
540 reg = (uint8_t)g_test_rand_int_range(0, 8);
541 val = (uint8_t)g_test_rand_int_range(0, 256);
543 outb(FLOPPY_BASE + reg, val);
544 inb(FLOPPY_BASE + reg);
548 int main(int argc, char **argv)
550 const char *arch = qtest_get_arch();
551 int fd;
552 int ret;
554 /* Check architecture */
555 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
556 g_test_message("Skipping test for non-x86\n");
557 return 0;
560 /* Create a temporary raw image */
561 fd = mkstemp(test_image);
562 g_assert(fd >= 0);
563 ret = ftruncate(fd, TEST_IMAGE_SIZE);
564 g_assert(ret == 0);
565 close(fd);
567 /* Run the tests */
568 g_test_init(&argc, &argv, NULL);
570 qtest_start(NULL);
571 qtest_irq_intercept_in(global_qtest, "ioapic");
572 qtest_add_func("/fdc/cmos", test_cmos);
573 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start);
574 qtest_add_func("/fdc/read_without_media", test_read_without_media);
575 qtest_add_func("/fdc/media_change", test_media_change);
576 qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt);
577 qtest_add_func("/fdc/relative_seek", test_relative_seek);
578 qtest_add_func("/fdc/read_id", test_read_id);
579 qtest_add_func("/fdc/verify", test_verify);
580 qtest_add_func("/fdc/media_insert", test_media_insert);
581 qtest_add_func("/fdc/read_no_dma_1", test_read_no_dma_1);
582 qtest_add_func("/fdc/read_no_dma_18", test_read_no_dma_18);
583 qtest_add_func("/fdc/read_no_dma_19", test_read_no_dma_19);
584 qtest_add_func("/fdc/fuzz-registers", fuzz_registers);
586 ret = g_test_run();
588 /* Cleanup */
589 qtest_end();
590 unlink(test_image);
592 return ret;