]> Git Repo - qemu.git/blob - tests/fdc-test.c
Merge remote-tracking branch 'sstabellini/for_1.1_rc3' into staging
[qemu.git] / tests / fdc-test.c
1 /*
2  * Floppy test cases.
3  *
4  * Copyright (c) 2012 Kevin Wolf <[email protected]>
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 uint8_t base = 0x70;
67
68 enum {
69     CMOS_FLOPPY     = 0x10,
70 };
71
72 static void floppy_send(uint8_t byte)
73 {
74     uint8_t msr;
75
76     msr = inb(FLOPPY_BASE + reg_msr);
77     assert_bit_set(msr, RQM);
78     assert_bit_clear(msr, DIO);
79
80     outb(FLOPPY_BASE + reg_fifo, byte);
81 }
82
83 static uint8_t floppy_recv(void)
84 {
85     uint8_t msr;
86
87     msr = inb(FLOPPY_BASE + reg_msr);
88     assert_bit_set(msr, RQM | DIO);
89
90     return inb(FLOPPY_BASE + reg_fifo);
91 }
92
93 static void ack_irq(void)
94 {
95     g_assert(get_irq(FLOPPY_IRQ));
96     floppy_send(CMD_SENSE_INT);
97     floppy_recv();
98     floppy_recv();
99     g_assert(!get_irq(FLOPPY_IRQ));
100 }
101
102 static void send_step_pulse(void)
103 {
104     int drive = 0;
105     int head = 0;
106     static int cyl = 0;
107
108     floppy_send(CMD_SEEK);
109     floppy_send(head << 2 | drive);
110     g_assert(!get_irq(FLOPPY_IRQ));
111     floppy_send(cyl);
112     ack_irq();
113
114     cyl = (cyl + 1) % 4;
115 }
116
117 static uint8_t cmos_read(uint8_t reg)
118 {
119     outb(base + 0, reg);
120     return inb(base + 1);
121 }
122
123 static void test_cmos(void)
124 {
125     uint8_t cmos;
126
127     cmos = cmos_read(CMOS_FLOPPY);
128     g_assert(cmos == 0x40);
129 }
130
131 static void test_no_media_on_start(void)
132 {
133     uint8_t dir;
134
135     /* Media changed bit must be set all time after start if there is
136      * no media in drive. */
137     dir = inb(FLOPPY_BASE + reg_dir);
138     assert_bit_set(dir, DSKCHG);
139     dir = inb(FLOPPY_BASE + reg_dir);
140     assert_bit_set(dir, DSKCHG);
141     send_step_pulse();
142     send_step_pulse();
143     dir = inb(FLOPPY_BASE + reg_dir);
144     assert_bit_set(dir, DSKCHG);
145     dir = inb(FLOPPY_BASE + reg_dir);
146     assert_bit_set(dir, DSKCHG);
147 }
148
149 static void test_media_change(void)
150 {
151     uint8_t dir;
152
153     /* Insert media in drive. DSKCHK should not be reset until a step pulse
154      * is sent. */
155     qmp("{'execute':'change', 'arguments':{ 'device':'floppy0', "
156         "'target': '%s' }}", test_image);
157     qmp(""); /* ignore event (FIXME open -> open transition?!) */
158     qmp(""); /* ignore event */
159
160     dir = inb(FLOPPY_BASE + reg_dir);
161     assert_bit_set(dir, DSKCHG);
162     dir = inb(FLOPPY_BASE + reg_dir);
163     assert_bit_set(dir, DSKCHG);
164
165     send_step_pulse();
166     dir = inb(FLOPPY_BASE + reg_dir);
167     assert_bit_clear(dir, DSKCHG);
168     dir = inb(FLOPPY_BASE + reg_dir);
169     assert_bit_clear(dir, DSKCHG);
170
171     /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
172      * reset the bit. */
173     qmp("{'execute':'eject', 'arguments':{ 'device':'floppy0' }}");
174     qmp(""); /* ignore event */
175
176     dir = inb(FLOPPY_BASE + reg_dir);
177     assert_bit_set(dir, DSKCHG);
178     dir = inb(FLOPPY_BASE + reg_dir);
179     assert_bit_set(dir, DSKCHG);
180
181     send_step_pulse();
182     dir = inb(FLOPPY_BASE + reg_dir);
183     assert_bit_set(dir, DSKCHG);
184     dir = inb(FLOPPY_BASE + reg_dir);
185     assert_bit_set(dir, DSKCHG);
186 }
187
188 int main(int argc, char **argv)
189 {
190     const char *arch = qtest_get_arch();
191     char *cmdline;
192     int fd;
193     int ret;
194
195     /* Check architecture */
196     if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
197         g_test_message("Skipping test for non-x86\n");
198         return 0;
199     }
200
201     /* Create a temporary raw image */
202     fd = mkstemp(test_image);
203     g_assert(fd >= 0);
204     ret = ftruncate(fd, TEST_IMAGE_SIZE);
205     g_assert(ret == 0);
206     close(fd);
207
208     /* Run the tests */
209     g_test_init(&argc, &argv, NULL);
210
211     cmdline = g_strdup_printf("-vnc none ");
212
213     qtest_start(cmdline);
214     qtest_irq_intercept_in(global_qtest, "ioapic");
215     qtest_add_func("/fdc/cmos", test_cmos);
216     qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start);
217     qtest_add_func("/fdc/media_change", test_media_change);
218
219     ret = g_test_run();
220
221     /* Cleanup */
222     qtest_quit(global_qtest);
223     unlink(test_image);
224
225     return ret;
226 }
This page took 0.036672 seconds and 4 git commands to generate.