2012-05-08 18:38:37 +04:00
|
|
|
/*
|
|
|
|
* Floppy test cases.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Kevin Wolf <kwolf@redhat.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-02-08 21:08:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2012-05-08 18:38:37 +04:00
|
|
|
|
|
|
|
|
2019-09-03 08:50:26 +03:00
|
|
|
#include "libqtest-single.h"
|
2018-08-06 09:53:25 +03:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2012-05-08 18:38:37 +04:00
|
|
|
|
2018-08-06 09:53:25 +03:00
|
|
|
/* TODO actually test the results and get rid of this */
|
|
|
|
#define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__))
|
|
|
|
|
2021-11-24 19:15:36 +03:00
|
|
|
#define DRIVE_FLOPPY_BLANK \
|
|
|
|
"-drive if=floppy,file=null-co://,file.read-zeroes=on,format=raw,size=1440k"
|
|
|
|
|
2012-05-08 18:38:37 +04:00
|
|
|
#define TEST_IMAGE_SIZE 1440 * 1024
|
|
|
|
|
|
|
|
#define FLOPPY_BASE 0x3f0
|
|
|
|
#define FLOPPY_IRQ 6
|
|
|
|
|
|
|
|
enum {
|
|
|
|
reg_sra = 0x0,
|
|
|
|
reg_srb = 0x1,
|
|
|
|
reg_dor = 0x2,
|
|
|
|
reg_msr = 0x4,
|
|
|
|
reg_dsr = 0x4,
|
|
|
|
reg_fifo = 0x5,
|
|
|
|
reg_dir = 0x7,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2012-07-16 17:48:27 +04:00
|
|
|
CMD_SENSE_INT = 0x08,
|
2012-09-21 01:20:07 +04:00
|
|
|
CMD_READ_ID = 0x0a,
|
2012-07-16 17:48:27 +04:00
|
|
|
CMD_SEEK = 0x0f,
|
2012-09-19 01:04:56 +04:00
|
|
|
CMD_VERIFY = 0x16,
|
2012-07-16 17:48:27 +04:00
|
|
|
CMD_READ = 0xe6,
|
|
|
|
CMD_RELATIVE_SEEK_OUT = 0x8f,
|
|
|
|
CMD_RELATIVE_SEEK_IN = 0xcf,
|
2012-05-08 18:38:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2012-09-19 01:02:59 +04:00
|
|
|
BUSY = 0x10,
|
|
|
|
NONDMA = 0x20,
|
2012-05-08 18:38:37 +04:00
|
|
|
RQM = 0x80,
|
|
|
|
DIO = 0x40,
|
|
|
|
|
|
|
|
DSKCHG = 0x80,
|
|
|
|
};
|
|
|
|
|
2022-09-25 14:29:47 +03:00
|
|
|
static char *test_image;
|
2012-05-08 18:38:37 +04:00
|
|
|
|
|
|
|
#define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
|
|
|
|
#define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
|
|
|
|
|
2012-05-24 13:02:30 +04:00
|
|
|
static uint8_t base = 0x70;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
CMOS_FLOPPY = 0x10,
|
|
|
|
};
|
|
|
|
|
2012-05-08 18:38:37 +04:00
|
|
|
static void floppy_send(uint8_t byte)
|
|
|
|
{
|
|
|
|
uint8_t msr;
|
|
|
|
|
|
|
|
msr = inb(FLOPPY_BASE + reg_msr);
|
|
|
|
assert_bit_set(msr, RQM);
|
|
|
|
assert_bit_clear(msr, DIO);
|
|
|
|
|
|
|
|
outb(FLOPPY_BASE + reg_fifo, byte);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t floppy_recv(void)
|
|
|
|
{
|
|
|
|
uint8_t msr;
|
|
|
|
|
|
|
|
msr = inb(FLOPPY_BASE + reg_msr);
|
|
|
|
assert_bit_set(msr, RQM | DIO);
|
|
|
|
|
|
|
|
return inb(FLOPPY_BASE + reg_fifo);
|
|
|
|
}
|
|
|
|
|
2012-07-16 18:06:56 +04:00
|
|
|
/* pcn: Present Cylinder Number */
|
|
|
|
static void ack_irq(uint8_t *pcn)
|
2012-05-08 18:38:37 +04:00
|
|
|
{
|
2012-07-16 17:48:27 +04:00
|
|
|
uint8_t ret;
|
|
|
|
|
2012-05-08 18:38:37 +04:00
|
|
|
g_assert(get_irq(FLOPPY_IRQ));
|
|
|
|
floppy_send(CMD_SENSE_INT);
|
|
|
|
floppy_recv();
|
2012-07-16 18:06:56 +04:00
|
|
|
|
2012-07-16 17:48:27 +04:00
|
|
|
ret = floppy_recv();
|
2012-07-16 18:06:56 +04:00
|
|
|
if (pcn != NULL) {
|
|
|
|
*pcn = ret;
|
|
|
|
}
|
2012-07-16 17:48:27 +04:00
|
|
|
|
2012-07-16 18:06:56 +04:00
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
2012-05-08 18:38:37 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 01:04:56 +04:00
|
|
|
static uint8_t send_read_command(uint8_t cmd)
|
2012-06-13 17:43:12 +04:00
|
|
|
{
|
|
|
|
uint8_t drive = 0;
|
|
|
|
uint8_t head = 0;
|
|
|
|
uint8_t cyl = 0;
|
|
|
|
uint8_t sect_addr = 1;
|
|
|
|
uint8_t sect_size = 2;
|
|
|
|
uint8_t eot = 1;
|
|
|
|
uint8_t gap = 0x1b;
|
|
|
|
uint8_t gpl = 0xff;
|
|
|
|
|
|
|
|
uint8_t msr = 0;
|
|
|
|
uint8_t st0;
|
|
|
|
|
|
|
|
uint8_t ret = 0;
|
|
|
|
|
2012-09-19 01:04:56 +04:00
|
|
|
floppy_send(cmd);
|
2012-06-13 17:43:12 +04:00
|
|
|
floppy_send(head << 2 | drive);
|
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
|
|
|
floppy_send(cyl);
|
|
|
|
floppy_send(head);
|
|
|
|
floppy_send(sect_addr);
|
|
|
|
floppy_send(sect_size);
|
|
|
|
floppy_send(eot);
|
|
|
|
floppy_send(gap);
|
|
|
|
floppy_send(gpl);
|
|
|
|
|
|
|
|
uint8_t i = 0;
|
|
|
|
uint8_t n = 2;
|
|
|
|
for (; i < n; i++) {
|
|
|
|
msr = inb(FLOPPY_BASE + reg_msr);
|
|
|
|
if (msr == 0xd0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= n) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
st0 = floppy_recv();
|
2012-09-21 01:07:53 +04:00
|
|
|
if (st0 != 0x40) {
|
2012-06-13 17:43:12 +04:00
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
floppy_recv();
|
|
|
|
floppy_recv();
|
|
|
|
floppy_recv();
|
|
|
|
floppy_recv();
|
|
|
|
floppy_recv();
|
|
|
|
floppy_recv();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-19 01:02:59 +04:00
|
|
|
static uint8_t send_read_no_dma_command(int nb_sect, uint8_t expected_st0)
|
|
|
|
{
|
|
|
|
uint8_t drive = 0;
|
|
|
|
uint8_t head = 0;
|
|
|
|
uint8_t cyl = 0;
|
|
|
|
uint8_t sect_addr = 1;
|
|
|
|
uint8_t sect_size = 2;
|
|
|
|
uint8_t eot = nb_sect;
|
|
|
|
uint8_t gap = 0x1b;
|
|
|
|
uint8_t gpl = 0xff;
|
|
|
|
|
|
|
|
uint8_t msr = 0;
|
|
|
|
uint8_t st0;
|
|
|
|
|
|
|
|
uint8_t ret = 0;
|
|
|
|
|
|
|
|
floppy_send(CMD_READ);
|
|
|
|
floppy_send(head << 2 | drive);
|
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
|
|
|
floppy_send(cyl);
|
|
|
|
floppy_send(head);
|
|
|
|
floppy_send(sect_addr);
|
|
|
|
floppy_send(sect_size);
|
|
|
|
floppy_send(eot);
|
|
|
|
floppy_send(gap);
|
|
|
|
floppy_send(gpl);
|
|
|
|
|
|
|
|
uint16_t i = 0;
|
|
|
|
uint8_t n = 2;
|
|
|
|
for (; i < n; i++) {
|
|
|
|
msr = inb(FLOPPY_BASE + reg_msr);
|
|
|
|
if (msr == (BUSY | NONDMA | DIO | RQM)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= n) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Non-DMA mode */
|
|
|
|
for (i = 0; i < 512 * 2 * nb_sect; i++) {
|
|
|
|
msr = inb(FLOPPY_BASE + reg_msr);
|
|
|
|
assert_bit_set(msr, BUSY | RQM | DIO);
|
|
|
|
inb(FLOPPY_BASE + reg_fifo);
|
|
|
|
}
|
|
|
|
|
2015-05-21 16:19:38 +03:00
|
|
|
msr = inb(FLOPPY_BASE + reg_msr);
|
|
|
|
assert_bit_set(msr, BUSY | RQM | DIO);
|
|
|
|
g_assert(get_irq(FLOPPY_IRQ));
|
|
|
|
|
2012-09-19 01:02:59 +04:00
|
|
|
st0 = floppy_recv();
|
|
|
|
if (st0 != expected_st0) {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
floppy_recv();
|
|
|
|
floppy_recv();
|
|
|
|
floppy_recv();
|
|
|
|
floppy_recv();
|
|
|
|
floppy_recv();
|
2015-05-21 16:19:38 +03:00
|
|
|
g_assert(get_irq(FLOPPY_IRQ));
|
2012-09-19 01:02:59 +04:00
|
|
|
floppy_recv();
|
|
|
|
|
2015-05-21 16:19:38 +03:00
|
|
|
/* Check that we're back in command phase */
|
|
|
|
msr = inb(FLOPPY_BASE + reg_msr);
|
|
|
|
assert_bit_clear(msr, BUSY | DIO);
|
|
|
|
assert_bit_set(msr, RQM);
|
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
|
|
|
|
2012-09-19 01:02:59 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-16 18:06:56 +04:00
|
|
|
static void send_seek(int cyl)
|
2012-05-08 18:38:37 +04:00
|
|
|
{
|
|
|
|
int drive = 0;
|
|
|
|
int head = 0;
|
|
|
|
|
|
|
|
floppy_send(CMD_SEEK);
|
|
|
|
floppy_send(head << 2 | drive);
|
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
|
|
|
floppy_send(cyl);
|
2012-07-16 18:06:56 +04:00
|
|
|
ack_irq(NULL);
|
2012-05-08 18:38:37 +04:00
|
|
|
}
|
|
|
|
|
2012-05-24 13:02:30 +04:00
|
|
|
static uint8_t cmos_read(uint8_t reg)
|
2012-05-08 18:38:37 +04:00
|
|
|
{
|
2012-05-24 13:02:30 +04:00
|
|
|
outb(base + 0, reg);
|
|
|
|
return inb(base + 1);
|
|
|
|
}
|
2012-05-08 18:38:37 +04:00
|
|
|
|
2012-05-24 13:02:30 +04:00
|
|
|
static void test_cmos(void)
|
|
|
|
{
|
|
|
|
uint8_t cmos;
|
2012-05-08 18:38:37 +04:00
|
|
|
|
2012-05-24 13:02:30 +04:00
|
|
|
cmos = cmos_read(CMOS_FLOPPY);
|
2016-01-22 23:51:04 +03:00
|
|
|
g_assert(cmos == 0x40 || cmos == 0x50);
|
2012-05-24 13:02:30 +04:00
|
|
|
}
|
2012-05-08 18:38:37 +04:00
|
|
|
|
2012-05-24 13:02:30 +04:00
|
|
|
static void test_no_media_on_start(void)
|
|
|
|
{
|
|
|
|
uint8_t dir;
|
|
|
|
|
|
|
|
/* Media changed bit must be set all time after start if there is
|
|
|
|
* no media in drive. */
|
2012-05-08 18:38:37 +04:00
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
2012-07-16 18:06:56 +04:00
|
|
|
send_seek(1);
|
2012-05-08 18:38:37 +04:00
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
2012-05-24 13:02:30 +04:00
|
|
|
}
|
|
|
|
|
2012-06-13 17:43:12 +04:00
|
|
|
static void test_read_without_media(void)
|
|
|
|
{
|
|
|
|
uint8_t ret;
|
|
|
|
|
2012-09-19 01:04:56 +04:00
|
|
|
ret = send_read_command(CMD_READ);
|
2012-06-13 17:43:12 +04:00
|
|
|
g_assert(ret == 0);
|
|
|
|
}
|
|
|
|
|
2012-09-19 00:48:48 +04:00
|
|
|
static void test_media_insert(void)
|
2012-05-24 13:02:30 +04:00
|
|
|
{
|
|
|
|
uint8_t dir;
|
2012-05-08 18:38:37 +04:00
|
|
|
|
2012-05-24 13:02:30 +04:00
|
|
|
/* Insert media in drive. DSKCHK should not be reset until a step pulse
|
2012-05-08 18:38:37 +04:00
|
|
|
* is sent. */
|
2017-04-28 00:58:20 +03:00
|
|
|
qmp_discard_response("{'execute':'blockdev-change-medium', 'arguments':{"
|
|
|
|
" 'id':'floppy0', 'filename': %s, 'format': 'raw' }}",
|
2013-10-30 17:54:32 +04:00
|
|
|
test_image);
|
2012-05-08 18:38:37 +04:00
|
|
|
|
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
|
|
|
|
2012-07-16 18:06:56 +04:00
|
|
|
send_seek(0);
|
2012-07-04 18:26:04 +04:00
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
|
|
|
|
|
|
|
/* Step to next track should clear DSKCHG bit. */
|
2012-07-16 18:06:56 +04:00
|
|
|
send_seek(1);
|
2012-05-08 18:38:37 +04:00
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_clear(dir, DSKCHG);
|
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_clear(dir, DSKCHG);
|
2012-09-19 00:48:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_media_change(void)
|
|
|
|
{
|
|
|
|
uint8_t dir;
|
|
|
|
|
|
|
|
test_media_insert();
|
2012-05-24 13:02:30 +04:00
|
|
|
|
|
|
|
/* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
|
|
|
|
* reset the bit. */
|
2013-10-30 17:54:32 +04:00
|
|
|
qmp_discard_response("{'execute':'eject', 'arguments':{"
|
2017-04-28 00:58:20 +03:00
|
|
|
" 'id':'floppy0' }}");
|
2012-05-24 13:02:30 +04:00
|
|
|
|
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
|
|
|
|
2012-07-16 18:06:56 +04:00
|
|
|
send_seek(0);
|
2012-07-04 18:26:04 +04:00
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
|
|
|
|
2012-07-16 18:06:56 +04:00
|
|
|
send_seek(1);
|
2012-05-24 13:02:30 +04:00
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
|
|
|
dir = inb(FLOPPY_BASE + reg_dir);
|
|
|
|
assert_bit_set(dir, DSKCHG);
|
2012-05-08 18:38:37 +04:00
|
|
|
}
|
|
|
|
|
2012-07-04 13:18:35 +04:00
|
|
|
static void test_sense_interrupt(void)
|
|
|
|
{
|
|
|
|
int drive = 0;
|
|
|
|
int head = 0;
|
|
|
|
int cyl = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
floppy_send(CMD_SENSE_INT);
|
|
|
|
ret = floppy_recv();
|
|
|
|
g_assert(ret == 0x80);
|
|
|
|
|
|
|
|
floppy_send(CMD_SEEK);
|
|
|
|
floppy_send(head << 2 | drive);
|
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
|
|
|
floppy_send(cyl);
|
|
|
|
|
|
|
|
floppy_send(CMD_SENSE_INT);
|
|
|
|
ret = floppy_recv();
|
|
|
|
g_assert(ret == 0x20);
|
|
|
|
floppy_recv();
|
|
|
|
}
|
|
|
|
|
2012-07-16 17:48:27 +04:00
|
|
|
static void test_relative_seek(void)
|
|
|
|
{
|
|
|
|
uint8_t drive = 0;
|
|
|
|
uint8_t head = 0;
|
|
|
|
uint8_t cyl = 1;
|
2012-07-16 18:06:56 +04:00
|
|
|
uint8_t pcn;
|
2012-07-16 17:48:27 +04:00
|
|
|
|
|
|
|
/* Send seek to track 0 */
|
2012-07-16 18:06:56 +04:00
|
|
|
send_seek(0);
|
2012-07-16 17:48:27 +04:00
|
|
|
|
|
|
|
/* Send relative seek to increase track by 1 */
|
|
|
|
floppy_send(CMD_RELATIVE_SEEK_IN);
|
|
|
|
floppy_send(head << 2 | drive);
|
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
|
|
|
floppy_send(cyl);
|
|
|
|
|
2012-07-16 18:06:56 +04:00
|
|
|
ack_irq(&pcn);
|
|
|
|
g_assert(pcn == 1);
|
2012-07-16 17:48:27 +04:00
|
|
|
|
|
|
|
/* Send relative seek to decrease track by 1 */
|
|
|
|
floppy_send(CMD_RELATIVE_SEEK_OUT);
|
|
|
|
floppy_send(head << 2 | drive);
|
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
|
|
|
floppy_send(cyl);
|
|
|
|
|
2012-07-16 18:06:56 +04:00
|
|
|
ack_irq(&pcn);
|
|
|
|
g_assert(pcn == 0);
|
2012-07-16 17:48:27 +04:00
|
|
|
}
|
|
|
|
|
2012-09-21 01:20:07 +04:00
|
|
|
static void test_read_id(void)
|
|
|
|
{
|
|
|
|
uint8_t drive = 0;
|
|
|
|
uint8_t head = 0;
|
|
|
|
uint8_t cyl;
|
|
|
|
uint8_t st0;
|
2015-05-21 16:19:38 +03:00
|
|
|
uint8_t msr;
|
2012-09-21 01:20:07 +04:00
|
|
|
|
|
|
|
/* Seek to track 0 and check with READ ID */
|
|
|
|
send_seek(0);
|
|
|
|
|
|
|
|
floppy_send(CMD_READ_ID);
|
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
|
|
|
floppy_send(head << 2 | drive);
|
|
|
|
|
2015-05-21 16:19:38 +03:00
|
|
|
msr = inb(FLOPPY_BASE + reg_msr);
|
|
|
|
if (!get_irq(FLOPPY_IRQ)) {
|
|
|
|
assert_bit_set(msr, BUSY);
|
|
|
|
assert_bit_clear(msr, RQM);
|
|
|
|
}
|
|
|
|
|
2012-09-21 01:20:07 +04:00
|
|
|
while (!get_irq(FLOPPY_IRQ)) {
|
|
|
|
/* qemu involves a timer with READ ID... */
|
|
|
|
clock_step(1000000000LL / 50);
|
|
|
|
}
|
|
|
|
|
2015-05-21 16:19:38 +03:00
|
|
|
msr = inb(FLOPPY_BASE + reg_msr);
|
|
|
|
assert_bit_set(msr, BUSY | RQM | DIO);
|
|
|
|
|
2012-09-21 01:20:07 +04:00
|
|
|
st0 = floppy_recv();
|
|
|
|
floppy_recv();
|
|
|
|
floppy_recv();
|
|
|
|
cyl = floppy_recv();
|
|
|
|
head = floppy_recv();
|
|
|
|
floppy_recv();
|
2015-05-21 16:19:38 +03:00
|
|
|
g_assert(get_irq(FLOPPY_IRQ));
|
2012-09-21 01:20:07 +04:00
|
|
|
floppy_recv();
|
2015-05-21 16:19:38 +03:00
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
2012-09-21 01:20:07 +04:00
|
|
|
|
|
|
|
g_assert_cmpint(cyl, ==, 0);
|
|
|
|
g_assert_cmpint(head, ==, 0);
|
|
|
|
g_assert_cmpint(st0, ==, head << 2);
|
|
|
|
|
|
|
|
/* Seek to track 8 on head 1 and check with READ ID */
|
|
|
|
head = 1;
|
|
|
|
cyl = 8;
|
|
|
|
|
|
|
|
floppy_send(CMD_SEEK);
|
|
|
|
floppy_send(head << 2 | drive);
|
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
|
|
|
floppy_send(cyl);
|
|
|
|
g_assert(get_irq(FLOPPY_IRQ));
|
|
|
|
ack_irq(NULL);
|
|
|
|
|
|
|
|
floppy_send(CMD_READ_ID);
|
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
|
|
|
floppy_send(head << 2 | drive);
|
|
|
|
|
2015-05-21 16:19:38 +03:00
|
|
|
msr = inb(FLOPPY_BASE + reg_msr);
|
|
|
|
if (!get_irq(FLOPPY_IRQ)) {
|
|
|
|
assert_bit_set(msr, BUSY);
|
|
|
|
assert_bit_clear(msr, RQM);
|
|
|
|
}
|
|
|
|
|
2012-09-21 01:20:07 +04:00
|
|
|
while (!get_irq(FLOPPY_IRQ)) {
|
|
|
|
/* qemu involves a timer with READ ID... */
|
|
|
|
clock_step(1000000000LL / 50);
|
|
|
|
}
|
|
|
|
|
2015-05-21 16:19:38 +03:00
|
|
|
msr = inb(FLOPPY_BASE + reg_msr);
|
|
|
|
assert_bit_set(msr, BUSY | RQM | DIO);
|
|
|
|
|
2012-09-21 01:20:07 +04:00
|
|
|
st0 = floppy_recv();
|
|
|
|
floppy_recv();
|
|
|
|
floppy_recv();
|
|
|
|
cyl = floppy_recv();
|
|
|
|
head = floppy_recv();
|
|
|
|
floppy_recv();
|
2015-05-21 16:19:38 +03:00
|
|
|
g_assert(get_irq(FLOPPY_IRQ));
|
2012-09-21 01:20:07 +04:00
|
|
|
floppy_recv();
|
2015-05-21 16:19:38 +03:00
|
|
|
g_assert(!get_irq(FLOPPY_IRQ));
|
2012-09-21 01:20:07 +04:00
|
|
|
|
|
|
|
g_assert_cmpint(cyl, ==, 8);
|
|
|
|
g_assert_cmpint(head, ==, 1);
|
|
|
|
g_assert_cmpint(st0, ==, head << 2);
|
|
|
|
}
|
|
|
|
|
2012-09-19 01:02:59 +04:00
|
|
|
static void test_read_no_dma_1(void)
|
|
|
|
{
|
|
|
|
uint8_t ret;
|
|
|
|
|
|
|
|
outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
|
|
|
|
send_seek(0);
|
2012-09-21 01:07:53 +04:00
|
|
|
ret = send_read_no_dma_command(1, 0x04);
|
2012-09-19 01:02:59 +04:00
|
|
|
g_assert(ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_read_no_dma_18(void)
|
|
|
|
{
|
|
|
|
uint8_t ret;
|
|
|
|
|
|
|
|
outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
|
|
|
|
send_seek(0);
|
2012-09-21 01:07:53 +04:00
|
|
|
ret = send_read_no_dma_command(18, 0x04);
|
2012-09-19 01:02:59 +04:00
|
|
|
g_assert(ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_read_no_dma_19(void)
|
|
|
|
{
|
|
|
|
uint8_t ret;
|
|
|
|
|
|
|
|
outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
|
|
|
|
send_seek(0);
|
|
|
|
ret = send_read_no_dma_command(19, 0x20);
|
|
|
|
g_assert(ret == 0);
|
|
|
|
}
|
|
|
|
|
2012-09-19 01:04:56 +04:00
|
|
|
static void test_verify(void)
|
|
|
|
{
|
|
|
|
uint8_t ret;
|
|
|
|
|
|
|
|
ret = send_read_command(CMD_VERIFY);
|
|
|
|
g_assert(ret == 0);
|
|
|
|
}
|
|
|
|
|
2012-05-17 22:55:58 +04:00
|
|
|
/* success if no crash or abort */
|
|
|
|
static void fuzz_registers(void)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 1000; i++) {
|
|
|
|
uint8_t reg, val;
|
|
|
|
|
|
|
|
reg = (uint8_t)g_test_rand_int_range(0, 8);
|
|
|
|
val = (uint8_t)g_test_rand_int_range(0, 256);
|
|
|
|
|
|
|
|
outb(FLOPPY_BASE + reg, val);
|
|
|
|
inb(FLOPPY_BASE + reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 19:15:36 +03:00
|
|
|
static bool qtest_check_clang_sanitizer(void)
|
|
|
|
{
|
2022-04-20 16:21:45 +03:00
|
|
|
#ifdef QEMU_SANITIZE_ADDRESS
|
2021-11-24 19:15:36 +03:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
g_test_skip("QEMU not configured using --enable-sanitizers");
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static void test_cve_2021_20196(void)
|
|
|
|
{
|
|
|
|
QTestState *s;
|
|
|
|
|
|
|
|
if (!qtest_check_clang_sanitizer()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = qtest_initf("-nographic -m 32M -nodefaults " DRIVE_FLOPPY_BLANK);
|
|
|
|
|
|
|
|
qtest_outw(s, 0x3f4, 0x0500);
|
|
|
|
qtest_outb(s, 0x3f5, 0x00);
|
|
|
|
qtest_outb(s, 0x3f5, 0x00);
|
|
|
|
qtest_outw(s, 0x3f4, 0x0000);
|
|
|
|
qtest_outb(s, 0x3f5, 0x00);
|
|
|
|
qtest_outw(s, 0x3f1, 0x0400);
|
|
|
|
qtest_outw(s, 0x3f4, 0x0000);
|
|
|
|
qtest_outw(s, 0x3f4, 0x0000);
|
|
|
|
qtest_outb(s, 0x3f5, 0x00);
|
|
|
|
qtest_outb(s, 0x3f5, 0x01);
|
|
|
|
qtest_outw(s, 0x3f1, 0x0500);
|
|
|
|
qtest_outb(s, 0x3f5, 0x00);
|
|
|
|
qtest_quit(s);
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:57:33 +03:00
|
|
|
static void test_cve_2021_3507(void)
|
|
|
|
{
|
|
|
|
QTestState *s;
|
|
|
|
|
|
|
|
s = qtest_initf("-nographic -m 32M -nodefaults "
|
|
|
|
"-drive file=%s,format=raw,if=floppy,snapshot=on",
|
|
|
|
test_image);
|
|
|
|
qtest_outl(s, 0x9, 0x0a0206);
|
|
|
|
qtest_outw(s, 0x3f4, 0x1600);
|
|
|
|
qtest_outw(s, 0x3f4, 0x0000);
|
|
|
|
qtest_outw(s, 0x3f4, 0x0000);
|
|
|
|
qtest_outw(s, 0x3f4, 0x0000);
|
|
|
|
qtest_outw(s, 0x3f4, 0x0200);
|
|
|
|
qtest_outw(s, 0x3f4, 0x0200);
|
|
|
|
qtest_outw(s, 0x3f4, 0x0000);
|
|
|
|
qtest_outw(s, 0x3f4, 0x0000);
|
|
|
|
qtest_outw(s, 0x3f4, 0x0000);
|
|
|
|
qtest_quit(s);
|
|
|
|
}
|
|
|
|
|
2012-05-08 18:38:37 +04:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Create a temporary raw image */
|
2022-09-25 14:29:47 +03:00
|
|
|
fd = g_file_open_tmp("qtest.XXXXXX", &test_image, NULL);
|
2012-05-08 18:38:37 +04:00
|
|
|
g_assert(fd >= 0);
|
|
|
|
ret = ftruncate(fd, TEST_IMAGE_SIZE);
|
|
|
|
g_assert(ret == 0);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* Run the tests */
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
|
2022-02-15 19:25:31 +03:00
|
|
|
qtest_start("-machine pc -device floppy,id=floppy0");
|
2012-05-08 18:38:37 +04:00
|
|
|
qtest_irq_intercept_in(global_qtest, "ioapic");
|
2012-05-24 13:02:30 +04:00
|
|
|
qtest_add_func("/fdc/cmos", test_cmos);
|
|
|
|
qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start);
|
2012-06-13 17:43:12 +04:00
|
|
|
qtest_add_func("/fdc/read_without_media", test_read_without_media);
|
2012-05-08 18:38:37 +04:00
|
|
|
qtest_add_func("/fdc/media_change", test_media_change);
|
2012-07-04 13:18:35 +04:00
|
|
|
qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt);
|
2012-07-16 17:48:27 +04:00
|
|
|
qtest_add_func("/fdc/relative_seek", test_relative_seek);
|
2012-09-21 01:20:07 +04:00
|
|
|
qtest_add_func("/fdc/read_id", test_read_id);
|
2012-09-19 01:04:56 +04:00
|
|
|
qtest_add_func("/fdc/verify", test_verify);
|
2012-09-19 00:49:30 +04:00
|
|
|
qtest_add_func("/fdc/media_insert", test_media_insert);
|
2012-09-19 01:02:59 +04:00
|
|
|
qtest_add_func("/fdc/read_no_dma_1", test_read_no_dma_1);
|
|
|
|
qtest_add_func("/fdc/read_no_dma_18", test_read_no_dma_18);
|
|
|
|
qtest_add_func("/fdc/read_no_dma_19", test_read_no_dma_19);
|
2012-05-17 22:55:58 +04:00
|
|
|
qtest_add_func("/fdc/fuzz-registers", fuzz_registers);
|
2021-11-24 19:15:36 +03:00
|
|
|
qtest_add_func("/fdc/fuzz/cve_2021_20196", test_cve_2021_20196);
|
2021-11-18 14:57:33 +03:00
|
|
|
qtest_add_func("/fdc/fuzz/cve_2021_3507", test_cve_2021_3507);
|
2012-05-08 18:38:37 +04:00
|
|
|
|
|
|
|
ret = g_test_run();
|
|
|
|
|
|
|
|
/* Cleanup */
|
2013-06-20 10:55:29 +04:00
|
|
|
qtest_end();
|
2012-05-08 18:38:37 +04:00
|
|
|
unlink(test_image);
|
2022-09-25 14:29:47 +03:00
|
|
|
g_free(test_image);
|
2012-05-08 18:38:37 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|