hd-geo-test.c 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Hard disk geometry test cases.
 *
 * Copyright (c) 2012 Red Hat Inc.
 *
 * Authors:
 *  Markus Armbruster <armbru@redhat.com>,
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

/*
 * Covers only IDE and tests only CMOS contents.  Better than nothing.
 * Improvements welcome.
 */

P
Peter Maydell 已提交
18
#include "qemu/osdep.h"
19 20 21
#include "qemu-common.h"
#include "libqtest.h"

22 23
#define ARGV_SIZE 256

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static char *create_test_img(int secs)
{
    char *template = strdup("/tmp/qtest.XXXXXX");
    int fd, ret;

    fd = mkstemp(template);
    g_assert(fd >= 0);
    ret = ftruncate(fd, (off_t)secs * 512);
    g_assert(ret == 0);
    close(fd);
    return template;
}

typedef struct {
    int cyls, heads, secs, trans;
} CHST;

typedef enum {
    mbr_blank, mbr_lba, mbr_chs,
    mbr_last
} MBRcontents;

typedef enum {
    /* order is relevant */
    backend_small, backend_large, backend_empty,
    backend_last
} Backend;

static const int img_secs[backend_last] = {
    [backend_small] = 61440,
    [backend_large] = 8388608,
    [backend_empty] = -1,
};

static const CHST hd_chst[backend_last][mbr_last] = {
    [backend_small] = {
        [mbr_blank] = { 60, 16, 63, 0 },
        [mbr_lba]   = { 60, 16, 63, 2 },
        [mbr_chs]   = { 60, 16, 63, 0 }
    },
    [backend_large] = {
        [mbr_blank] = { 8322, 16, 63, 1 },
        [mbr_lba]   = { 8322, 16, 63, 1 },
        [mbr_chs]   = { 8322, 16, 63, 0 }
    },
};

71
static char *img_file_name[backend_last];
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

static const CHST *cur_ide[4];

static bool is_hd(const CHST *expected_chst)
{
    return expected_chst && expected_chst->cyls;
}

static void test_cmos_byte(int reg, int expected)
{
    enum { cmos_base = 0x70 };
    int actual;

    outb(cmos_base + 0, reg);
    actual = inb(cmos_base + 1);
    g_assert(actual == expected);
}

static void test_cmos_bytes(int reg0, int n, uint8_t expected[])
{
    int i;

    for (i = 0; i < 9; i++) {
        test_cmos_byte(reg0 + i, expected[i]);
    }
}

static void test_cmos_disk_data(void)
{
    test_cmos_byte(0x12,
                   (is_hd(cur_ide[0]) ? 0xf0 : 0) |
                   (is_hd(cur_ide[1]) ? 0x0f : 0));
}

static void test_cmos_drive_cyl(int reg0, const CHST *expected_chst)
{
    if (is_hd(expected_chst)) {
        int c = expected_chst->cyls;
        int h = expected_chst->heads;
        int s = expected_chst->secs;
        uint8_t expected_bytes[9] = {
            c & 0xff, c >> 8, h, 0xff, 0xff, 0xc0 | ((h > 8) << 3),
            c & 0xff, c >> 8, s
        };
        test_cmos_bytes(reg0, 9, expected_bytes);
    } else {
        int i;

        for (i = 0; i < 9; i++) {
            test_cmos_byte(reg0 + i, 0);
        }
    }
}

static void test_cmos_drive1(void)
{
    test_cmos_byte(0x19, is_hd(cur_ide[0]) ? 47 : 0);
    test_cmos_drive_cyl(0x1b, cur_ide[0]);
}

static void test_cmos_drive2(void)
{
    test_cmos_byte(0x1a, is_hd(cur_ide[1]) ? 47 : 0);
    test_cmos_drive_cyl(0x24, cur_ide[1]);
}

static void test_cmos_disktransflag(void)
{
    int val, i;

    val = 0;
    for (i = 0; i < ARRAY_SIZE(cur_ide); i++) {
        if (is_hd(cur_ide[i])) {
            val |= cur_ide[i]->trans << (2 * i);
        }
    }
    test_cmos_byte(0x39, val);
}

static void test_cmos(void)
{
    test_cmos_disk_data();
    test_cmos_drive1();
    test_cmos_drive2();
    test_cmos_disktransflag();
}

static int append_arg(int argc, char *argv[], int argv_sz, char *arg)
{
    g_assert(argc + 1 < argv_sz);
    argv[argc++] = arg;
    argv[argc] = NULL;
    return argc;
}

static int setup_common(char *argv[], int argv_sz)
{
    memset(cur_ide, 0, sizeof(cur_ide));
    return append_arg(0, argv, argv_sz,
171
                      g_strdup("-nodefaults"));
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
}

static void setup_mbr(int img_idx, MBRcontents mbr)
{
    static const uint8_t part_lba[16] = {
        /* chs 0,1,1 (lba 63) to chs 0,127,63 (8001 sectors) */
        0x80, 1, 1, 0, 6, 127, 63, 0, 63, 0, 0, 0, 0x41, 0x1F, 0, 0,
    };
    static const uint8_t part_chs[16] = {
        /* chs 0,1,1 (lba 63) to chs 7,15,63 (8001 sectors) */
        0x80, 1, 1, 0, 6,  15, 63, 7, 63, 0, 0, 0, 0x41, 0x1F, 0, 0,
    };
    uint8_t buf[512];
    int fd, ret;

    memset(buf, 0, sizeof(buf));

    if (mbr != mbr_blank) {
        buf[0x1fe] = 0x55;
        buf[0x1ff] = 0xAA;
        memcpy(buf + 0x1BE, mbr == mbr_lba ? part_lba : part_chs, 16);
    }

    fd = open(img_file_name[img_idx], O_WRONLY);
    g_assert(fd >= 0);
    ret = write(fd, buf, sizeof(buf));
    g_assert(ret == sizeof(buf));
    close(fd);
}

static int setup_ide(int argc, char *argv[], int argv_sz,
                     int ide_idx, const char *dev, int img_idx,
204
                     MBRcontents mbr)
205 206 207
{
    char *s1, *s2, *s3;

208
    s1 = g_strdup_printf("-drive id=drive%d,if=%s",
209 210 211 212 213
                         ide_idx, dev ? "none" : "ide");
    s2 = dev ? g_strdup("") : g_strdup_printf(",index=%d", ide_idx);

    if (img_secs[img_idx] >= 0) {
        setup_mbr(img_idx, mbr);
214
        s3 = g_strdup_printf(",format=raw,file=%s", img_file_name[img_idx]);
215 216 217 218
    } else {
        s3 = g_strdup(",media=cdrom");
    }
    argc = append_arg(argc, argv, argv_sz,
219
                      g_strdup_printf("%s%s%s", s1, s2, s3));
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    g_free(s1);
    g_free(s2);
    g_free(s3);

    if (dev) {
        argc = append_arg(argc, argv, argv_sz,
                          g_strdup_printf("-device %s,drive=drive%d,"
                                          "bus=ide.%d,unit=%d",
                                          dev, ide_idx,
                                          ide_idx / 2, ide_idx % 2));
    }
    return argc;
}

/*
 * Test case: no IDE devices
 */
static void test_ide_none(void)
{
239 240 241 242 243 244 245 246
    char **argv = g_new0(char *, ARGV_SIZE);
    char *args;

    setup_common(argv, ARGV_SIZE);
    args = g_strjoinv(" ", argv);
    qtest_start(args);
    g_strfreev(argv);
    g_free(args);
247
    test_cmos();
248
    qtest_end();
249 250 251 252
}

static void test_ide_mbr(bool use_device, MBRcontents mbr)
{
253 254
    char **argv = g_new0(char *, ARGV_SIZE);
    char *args;
255 256 257 258
    int argc;
    Backend i;
    const char *dev;

259
    argc = setup_common(argv, ARGV_SIZE);
260 261 262
    for (i = 0; i < backend_last; i++) {
        cur_ide[i] = &hd_chst[i][mbr];
        dev = use_device ? (is_hd(cur_ide[i]) ? "ide-hd" : "ide-cd") : NULL;
263
        argc = setup_ide(argc, argv, ARGV_SIZE, i, dev, i, mbr);
264
    }
265 266 267 268
    args = g_strjoinv(" ", argv);
    qtest_start(args);
    g_strfreev(argv);
    g_free(args);
269
    test_cmos();
270
    qtest_end();
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
}

/*
 * Test case: IDE devices (if=ide) with blank MBRs
 */
static void test_ide_drive_mbr_blank(void)
{
    test_ide_mbr(false, mbr_blank);
}

/*
 * Test case: IDE devices (if=ide) with MBRs indicating LBA is in use
 */
static void test_ide_drive_mbr_lba(void)
{
    test_ide_mbr(false, mbr_lba);
}

/*
 * Test case: IDE devices (if=ide) with MBRs indicating CHS is in use
 */
static void test_ide_drive_mbr_chs(void)
{
    test_ide_mbr(false, mbr_chs);
}

/*
 * Test case: IDE devices (if=none) with blank MBRs
 */
static void test_ide_device_mbr_blank(void)
{
    test_ide_mbr(true, mbr_blank);
}

/*
 * Test case: IDE devices (if=none) with MBRs indicating LBA is in use
 */
static void test_ide_device_mbr_lba(void)
{
    test_ide_mbr(true, mbr_lba);
}

/*
 * Test case: IDE devices (if=none) with MBRs indicating CHS is in use
 */
static void test_ide_device_mbr_chs(void)
{
    test_ide_mbr(true, mbr_chs);
}

static void test_ide_drive_user(const char *dev, bool trans)
{
323 324
    char **argv = g_new0(char *, ARGV_SIZE);
    char *args, *opts;
325 326 327 328
    int argc;
    int secs = img_secs[backend_small];
    const CHST expected_chst = { secs / (4 * 32) , 4, 32, trans };

329
    argc = setup_common(argv, ARGV_SIZE);
330 331
    opts = g_strdup_printf("%s,%scyls=%d,heads=%d,secs=%d",
                           dev, trans ? "bios-chs-trans=lba," : "",
332
                           expected_chst.cyls, expected_chst.heads,
333
                           expected_chst.secs);
334
    cur_ide[0] = &expected_chst;
335
    argc = setup_ide(argc, argv, ARGV_SIZE, 0, opts, backend_small, mbr_chs);
336
    g_free(opts);
337 338 339 340
    args = g_strjoinv(" ", argv);
    qtest_start(args);
    g_strfreev(argv);
    g_free(args);
341
    test_cmos();
342
    qtest_end();
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
}

/*
 * Test case: IDE device (if=none) with explicit CHS
 */
static void test_ide_device_user_chs(void)
{
    test_ide_drive_user("ide-hd", false);
}

/*
 * Test case: IDE device (if=none) with explicit CHS and translation
 */
static void test_ide_device_user_chst(void)
{
    test_ide_drive_user("ide-hd", true);
}

361 362 363 364 365
/*
 * Test case: IDE devices (if=ide), but use index=0 for CD-ROM
 */
static void test_ide_drive_cd_0(void)
{
366 367
    char **argv = g_new0(char *, ARGV_SIZE);
    char *args;
368 369 370
    int argc, ide_idx;
    Backend i;

371
    argc = setup_common(argv, ARGV_SIZE);
372 373 374
    for (i = 0; i <= backend_empty; i++) {
        ide_idx = backend_empty - i;
        cur_ide[ide_idx] = &hd_chst[i][mbr_blank];
375
        argc = setup_ide(argc, argv, ARGV_SIZE, ide_idx, NULL, i, mbr_blank);
376
    }
377 378 379 380
    args = g_strjoinv(" ", argv);
    qtest_start(args);
    g_strfreev(argv);
    g_free(args);
381
    test_cmos();
382
    qtest_end();
383 384
}

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
int main(int argc, char **argv)
{
    Backend i;
    int ret;

    g_test_init(&argc, &argv, NULL);

    for (i = 0; i < backend_last; i++) {
        if (img_secs[i] >= 0) {
            img_file_name[i] = create_test_img(img_secs[i]);
        } else {
            img_file_name[i] = NULL;
        }
    }

    qtest_add_func("hd-geo/ide/none", test_ide_none);
    qtest_add_func("hd-geo/ide/drive/mbr/blank", test_ide_drive_mbr_blank);
    qtest_add_func("hd-geo/ide/drive/mbr/lba", test_ide_drive_mbr_lba);
    qtest_add_func("hd-geo/ide/drive/mbr/chs", test_ide_drive_mbr_chs);
404
    qtest_add_func("hd-geo/ide/drive/cd_0", test_ide_drive_cd_0);
405 406 407 408 409 410 411 412 413
    qtest_add_func("hd-geo/ide/device/mbr/blank", test_ide_device_mbr_blank);
    qtest_add_func("hd-geo/ide/device/mbr/lba", test_ide_device_mbr_lba);
    qtest_add_func("hd-geo/ide/device/mbr/chs", test_ide_device_mbr_chs);
    qtest_add_func("hd-geo/ide/device/user/chs", test_ide_device_user_chs);
    qtest_add_func("hd-geo/ide/device/user/chst", test_ide_device_user_chst);

    ret = g_test_run();

    for (i = 0; i < backend_last; i++) {
414 415
        if (img_file_name[i]) {
            unlink(img_file_name[i]);
416
            free(img_file_name[i]);
417
        }
418 419 420 421
    }

    return ret;
}