]> git.proxmox.com Git - qemu.git/blob - tests/fdc-test.c
Merge remote-tracking branch 'pmaydell/target-arm.for-upstream' into staging
[qemu.git] / tests / fdc-test.c
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 {
50 CMD_SENSE_INT = 0x08,
51 CMD_SEEK = 0x0f,
52 };
53
54 enum {
55 RQM = 0x80,
56 DIO = 0x40,
57
58 DSKCHG = 0x80,
59 };
60
61 char test_image[] = "/tmp/qtest.XXXXXX";
62
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)
65
66 static void floppy_send(uint8_t byte)
67 {
68 uint8_t msr;
69
70 msr = inb(FLOPPY_BASE + reg_msr);
71 assert_bit_set(msr, RQM);
72 assert_bit_clear(msr, DIO);
73
74 outb(FLOPPY_BASE + reg_fifo, byte);
75 }
76
77 static uint8_t floppy_recv(void)
78 {
79 uint8_t msr;
80
81 msr = inb(FLOPPY_BASE + reg_msr);
82 assert_bit_set(msr, RQM | DIO);
83
84 return inb(FLOPPY_BASE + reg_fifo);
85 }
86
87 static void ack_irq(void)
88 {
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));
94 }
95
96 static void send_step_pulse(void)
97 {
98 int drive = 0;
99 int head = 0;
100 static int cyl = 0;
101
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();
107
108 cyl = (cyl + 1) % 4;
109 }
110
111 static void test_media_change(void)
112 {
113 uint8_t dir;
114
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);
121
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 */
126
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);
131
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);
137
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 */
144
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);
149
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);
155 }
156
157 int main(int argc, char **argv)
158 {
159 const char *arch = qtest_get_arch();
160 char *cmdline;
161 int fd;
162 int ret;
163
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;
168 }
169
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);
176
177 /* Run the tests */
178 g_test_init(&argc, &argv, NULL);
179
180 cmdline = g_strdup_printf("-vnc none "
181 "-drive file=%s,if=floppy,cache=writeback ",
182 test_image);
183
184 qtest_start(cmdline);
185 qtest_irq_intercept_in(global_qtest, "ioapic");
186 qtest_add_func("/fdc/media_change", test_media_change);
187
188 ret = g_test_run();
189
190 /* Cleanup */
191 qtest_quit(global_qtest);
192 unlink(test_image);
193
194 return ret;
195 }