cocoa: Suppress Cocoa frontend for -qtest
[qemu/wangdongxu.git] / tests / fdc-test.c
blob5b5dd7481f47ae530ed6b7955d4d163ee5918d95
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_SEEK = 0x0f,
54 enum {
55 RQM = 0x80,
56 DIO = 0x40,
58 DSKCHG = 0x80,
61 char test_image[] = "/tmp/qtest.XXXXXX";
63 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
64 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
66 static void floppy_send(uint8_t byte)
68 uint8_t msr;
70 msr = inb(FLOPPY_BASE + reg_msr);
71 assert_bit_set(msr, RQM);
72 assert_bit_clear(msr, DIO);
74 outb(FLOPPY_BASE + reg_fifo, byte);
77 static uint8_t floppy_recv(void)
79 uint8_t msr;
81 msr = inb(FLOPPY_BASE + reg_msr);
82 assert_bit_set(msr, RQM | DIO);
84 return inb(FLOPPY_BASE + reg_fifo);
87 static void ack_irq(void)
89 g_assert(get_irq(FLOPPY_IRQ));
90 floppy_send(CMD_SENSE_INT);
91 floppy_recv();
92 floppy_recv();
93 g_assert(!get_irq(FLOPPY_IRQ));
96 static void send_step_pulse(void)
98 int drive = 0;
99 int head = 0;
100 static int cyl = 0;
102 floppy_send(CMD_SEEK);
103 floppy_send(head << 2 | drive);
104 g_assert(!get_irq(FLOPPY_IRQ));
105 floppy_send(cyl);
106 ack_irq();
108 cyl = (cyl + 1) % 4;
111 static void test_media_change(void)
113 uint8_t dir;
115 /* Media changed bit must be up-to-date after step pulse. Do two SEEKs
116 * because we may already happen to be on the right cylinder initially. */
117 send_step_pulse();
118 send_step_pulse();
119 dir = inb(FLOPPY_BASE + reg_dir);
120 assert_bit_clear(dir, DSKCHG);
122 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
123 * reset the bit. */
124 qmp("{'execute':'eject', 'arguments':{ 'device':'floppy0' }}");
125 qmp(""); /* ignore event */
127 dir = inb(FLOPPY_BASE + reg_dir);
128 assert_bit_set(dir, DSKCHG);
129 dir = inb(FLOPPY_BASE + reg_dir);
130 assert_bit_set(dir, DSKCHG);
132 send_step_pulse();
133 dir = inb(FLOPPY_BASE + reg_dir);
134 assert_bit_set(dir, DSKCHG);
135 dir = inb(FLOPPY_BASE + reg_dir);
136 assert_bit_set(dir, DSKCHG);
138 /* And then insert it again. DSKCHK should not be reset until a step pulse
139 * is sent. */
140 qmp("{'execute':'change', 'arguments':{ 'device':'floppy0', "
141 "'target': '%s' }}", test_image);
142 qmp(""); /* ignore event (FIXME open -> open transition?!) */
143 qmp(""); /* ignore event */
145 dir = inb(FLOPPY_BASE + reg_dir);
146 assert_bit_set(dir, DSKCHG);
147 dir = inb(FLOPPY_BASE + reg_dir);
148 assert_bit_set(dir, DSKCHG);
150 send_step_pulse();
151 dir = inb(FLOPPY_BASE + reg_dir);
152 assert_bit_clear(dir, DSKCHG);
153 dir = inb(FLOPPY_BASE + reg_dir);
154 assert_bit_clear(dir, DSKCHG);
157 int main(int argc, char **argv)
159 const char *arch = qtest_get_arch();
160 char *cmdline;
161 int fd;
162 int ret;
164 /* Check architecture */
165 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
166 g_test_message("Skipping test for non-x86\n");
167 return 0;
170 /* Create a temporary raw image */
171 fd = mkstemp(test_image);
172 g_assert(fd >= 0);
173 ret = ftruncate(fd, TEST_IMAGE_SIZE);
174 g_assert(ret == 0);
175 close(fd);
177 /* Run the tests */
178 g_test_init(&argc, &argv, NULL);
180 cmdline = g_strdup_printf("-vnc none "
181 "-drive file=%s,if=floppy,cache=writeback ",
182 test_image);
184 qtest_start(cmdline);
185 qtest_irq_intercept_in(global_qtest, "ioapic");
186 qtest_add_func("/fdc/media_change", test_media_change);
188 ret = g_test_run();
190 /* Cleanup */
191 qtest_quit(global_qtest);
192 unlink(test_image);
194 return ret;