Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 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, |
Pavel Hrdina | 8b9ef60 | 2012-06-13 15:43:12 +0200 | [diff] [blame] | 52 | CMD_READ = 0xe6, |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | enum { |
| 56 | RQM = 0x80, |
| 57 | DIO = 0x40, |
| 58 | |
| 59 | DSKCHG = 0x80, |
| 60 | }; |
| 61 | |
| 62 | char 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 | |
Pavel Hrdina | 7cd3316 | 2012-05-24 11:02:30 +0200 | [diff] [blame] | 67 | static uint8_t base = 0x70; |
| 68 | |
| 69 | enum { |
| 70 | CMOS_FLOPPY = 0x10, |
| 71 | }; |
| 72 | |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 73 | static 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 | |
| 84 | static 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 | |
| 94 | static 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 | |
Pavel Hrdina | 8b9ef60 | 2012-06-13 15:43:12 +0200 | [diff] [blame] | 103 | static 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(); |
Pavel Hrdina | b3ce604 | 2012-07-04 11:18:35 +0200 | [diff] [blame^] | 145 | if (st0 != 0x60) { |
Pavel Hrdina | 8b9ef60 | 2012-06-13 15:43:12 +0200 | [diff] [blame] | 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 | |
Pavel Hrdina | 59240c3 | 2012-07-04 16:26:04 +0200 | [diff] [blame] | 159 | static void send_step_pulse(int cyl) |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 160 | { |
| 161 | int drive = 0; |
| 162 | int head = 0; |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 163 | |
| 164 | floppy_send(CMD_SEEK); |
| 165 | floppy_send(head << 2 | drive); |
| 166 | g_assert(!get_irq(FLOPPY_IRQ)); |
| 167 | floppy_send(cyl); |
| 168 | ack_irq(); |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Pavel Hrdina | 7cd3316 | 2012-05-24 11:02:30 +0200 | [diff] [blame] | 171 | static uint8_t cmos_read(uint8_t reg) |
| 172 | { |
| 173 | outb(base + 0, reg); |
| 174 | return inb(base + 1); |
| 175 | } |
| 176 | |
| 177 | static void test_cmos(void) |
| 178 | { |
| 179 | uint8_t cmos; |
| 180 | |
| 181 | cmos = cmos_read(CMOS_FLOPPY); |
| 182 | g_assert(cmos == 0x40); |
| 183 | } |
| 184 | |
| 185 | static void test_no_media_on_start(void) |
| 186 | { |
| 187 | uint8_t dir; |
| 188 | |
| 189 | /* Media changed bit must be set all time after start if there is |
| 190 | * no media in drive. */ |
| 191 | dir = inb(FLOPPY_BASE + reg_dir); |
| 192 | assert_bit_set(dir, DSKCHG); |
| 193 | dir = inb(FLOPPY_BASE + reg_dir); |
| 194 | assert_bit_set(dir, DSKCHG); |
Pavel Hrdina | 59240c3 | 2012-07-04 16:26:04 +0200 | [diff] [blame] | 195 | send_step_pulse(1); |
Pavel Hrdina | 7cd3316 | 2012-05-24 11:02:30 +0200 | [diff] [blame] | 196 | dir = inb(FLOPPY_BASE + reg_dir); |
| 197 | assert_bit_set(dir, DSKCHG); |
| 198 | dir = inb(FLOPPY_BASE + reg_dir); |
| 199 | assert_bit_set(dir, DSKCHG); |
| 200 | } |
| 201 | |
Pavel Hrdina | 8b9ef60 | 2012-06-13 15:43:12 +0200 | [diff] [blame] | 202 | static void test_read_without_media(void) |
| 203 | { |
| 204 | uint8_t ret; |
| 205 | |
| 206 | ret = send_read_command(); |
| 207 | g_assert(ret == 0); |
| 208 | } |
| 209 | |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 210 | static void test_media_change(void) |
| 211 | { |
| 212 | uint8_t dir; |
| 213 | |
Pavel Hrdina | 7cd3316 | 2012-05-24 11:02:30 +0200 | [diff] [blame] | 214 | /* Insert media in drive. DSKCHK should not be reset until a step pulse |
| 215 | * is sent. */ |
| 216 | qmp("{'execute':'change', 'arguments':{ 'device':'floppy0', " |
| 217 | "'target': '%s' }}", test_image); |
| 218 | qmp(""); /* ignore event (FIXME open -> open transition?!) */ |
| 219 | qmp(""); /* ignore event */ |
| 220 | |
| 221 | dir = inb(FLOPPY_BASE + reg_dir); |
| 222 | assert_bit_set(dir, DSKCHG); |
| 223 | dir = inb(FLOPPY_BASE + reg_dir); |
| 224 | assert_bit_set(dir, DSKCHG); |
| 225 | |
Pavel Hrdina | 59240c3 | 2012-07-04 16:26:04 +0200 | [diff] [blame] | 226 | send_step_pulse(0); |
| 227 | dir = inb(FLOPPY_BASE + reg_dir); |
| 228 | assert_bit_set(dir, DSKCHG); |
| 229 | dir = inb(FLOPPY_BASE + reg_dir); |
| 230 | assert_bit_set(dir, DSKCHG); |
| 231 | |
| 232 | /* Step to next track should clear DSKCHG bit. */ |
| 233 | send_step_pulse(1); |
Pavel Hrdina | 7cd3316 | 2012-05-24 11:02:30 +0200 | [diff] [blame] | 234 | dir = inb(FLOPPY_BASE + reg_dir); |
| 235 | assert_bit_clear(dir, DSKCHG); |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 236 | dir = inb(FLOPPY_BASE + reg_dir); |
| 237 | assert_bit_clear(dir, DSKCHG); |
| 238 | |
| 239 | /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't |
| 240 | * reset the bit. */ |
| 241 | qmp("{'execute':'eject', 'arguments':{ 'device':'floppy0' }}"); |
| 242 | qmp(""); /* ignore event */ |
| 243 | |
| 244 | dir = inb(FLOPPY_BASE + reg_dir); |
| 245 | assert_bit_set(dir, DSKCHG); |
| 246 | dir = inb(FLOPPY_BASE + reg_dir); |
| 247 | assert_bit_set(dir, DSKCHG); |
| 248 | |
Pavel Hrdina | 59240c3 | 2012-07-04 16:26:04 +0200 | [diff] [blame] | 249 | send_step_pulse(0); |
| 250 | dir = inb(FLOPPY_BASE + reg_dir); |
| 251 | assert_bit_set(dir, DSKCHG); |
| 252 | dir = inb(FLOPPY_BASE + reg_dir); |
| 253 | assert_bit_set(dir, DSKCHG); |
| 254 | |
| 255 | send_step_pulse(1); |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 256 | dir = inb(FLOPPY_BASE + reg_dir); |
| 257 | assert_bit_set(dir, DSKCHG); |
| 258 | dir = inb(FLOPPY_BASE + reg_dir); |
| 259 | assert_bit_set(dir, DSKCHG); |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Pavel Hrdina | b3ce604 | 2012-07-04 11:18:35 +0200 | [diff] [blame^] | 262 | static void test_sense_interrupt(void) |
| 263 | { |
| 264 | int drive = 0; |
| 265 | int head = 0; |
| 266 | int cyl = 0; |
| 267 | int ret = 0; |
| 268 | |
| 269 | floppy_send(CMD_SENSE_INT); |
| 270 | ret = floppy_recv(); |
| 271 | g_assert(ret == 0x80); |
| 272 | |
| 273 | floppy_send(CMD_SEEK); |
| 274 | floppy_send(head << 2 | drive); |
| 275 | g_assert(!get_irq(FLOPPY_IRQ)); |
| 276 | floppy_send(cyl); |
| 277 | |
| 278 | floppy_send(CMD_SENSE_INT); |
| 279 | ret = floppy_recv(); |
| 280 | g_assert(ret == 0x20); |
| 281 | floppy_recv(); |
| 282 | } |
| 283 | |
Blue Swirl | 3359847 | 2012-05-17 18:55:58 +0000 | [diff] [blame] | 284 | /* success if no crash or abort */ |
| 285 | static void fuzz_registers(void) |
| 286 | { |
| 287 | unsigned int i; |
| 288 | |
| 289 | for (i = 0; i < 1000; i++) { |
| 290 | uint8_t reg, val; |
| 291 | |
| 292 | reg = (uint8_t)g_test_rand_int_range(0, 8); |
| 293 | val = (uint8_t)g_test_rand_int_range(0, 256); |
| 294 | |
| 295 | outb(FLOPPY_BASE + reg, val); |
| 296 | inb(FLOPPY_BASE + reg); |
| 297 | } |
| 298 | } |
| 299 | |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 300 | int main(int argc, char **argv) |
| 301 | { |
| 302 | const char *arch = qtest_get_arch(); |
| 303 | char *cmdline; |
| 304 | int fd; |
| 305 | int ret; |
| 306 | |
| 307 | /* Check architecture */ |
| 308 | if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) { |
| 309 | g_test_message("Skipping test for non-x86\n"); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | /* Create a temporary raw image */ |
| 314 | fd = mkstemp(test_image); |
| 315 | g_assert(fd >= 0); |
| 316 | ret = ftruncate(fd, TEST_IMAGE_SIZE); |
| 317 | g_assert(ret == 0); |
| 318 | close(fd); |
| 319 | |
| 320 | /* Run the tests */ |
| 321 | g_test_init(&argc, &argv, NULL); |
| 322 | |
Pavel Hrdina | 7cd3316 | 2012-05-24 11:02:30 +0200 | [diff] [blame] | 323 | cmdline = g_strdup_printf("-vnc none "); |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 324 | |
| 325 | qtest_start(cmdline); |
| 326 | qtest_irq_intercept_in(global_qtest, "ioapic"); |
Pavel Hrdina | 7cd3316 | 2012-05-24 11:02:30 +0200 | [diff] [blame] | 327 | qtest_add_func("/fdc/cmos", test_cmos); |
| 328 | qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start); |
Pavel Hrdina | 8b9ef60 | 2012-06-13 15:43:12 +0200 | [diff] [blame] | 329 | qtest_add_func("/fdc/read_without_media", test_read_without_media); |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 330 | qtest_add_func("/fdc/media_change", test_media_change); |
Pavel Hrdina | b3ce604 | 2012-07-04 11:18:35 +0200 | [diff] [blame^] | 331 | qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt); |
Blue Swirl | 3359847 | 2012-05-17 18:55:58 +0000 | [diff] [blame] | 332 | qtest_add_func("/fdc/fuzz-registers", fuzz_registers); |
Kevin Wolf | 93e9eb6 | 2012-05-08 16:38:37 +0200 | [diff] [blame] | 333 | |
| 334 | ret = g_test_run(); |
| 335 | |
| 336 | /* Cleanup */ |
| 337 | qtest_quit(global_qtest); |
| 338 | unlink(test_image); |
| 339 | |
| 340 | return ret; |
| 341 | } |