1*1e8a1faeSThomas Huth /* 2*1e8a1faeSThomas Huth * Boot order test cases. 3*1e8a1faeSThomas Huth * 4*1e8a1faeSThomas Huth * Copyright (c) 2013 Red Hat Inc. 5*1e8a1faeSThomas Huth * 6*1e8a1faeSThomas Huth * Authors: 7*1e8a1faeSThomas Huth * Markus Armbruster <armbru@redhat.com>, 8*1e8a1faeSThomas Huth * 9*1e8a1faeSThomas Huth * This work is licensed under the terms of the GNU GPL, version 2 or later. 10*1e8a1faeSThomas Huth * See the COPYING file in the top-level directory. 11*1e8a1faeSThomas Huth */ 12*1e8a1faeSThomas Huth 13*1e8a1faeSThomas Huth #include "qemu/osdep.h" 14*1e8a1faeSThomas Huth #include "libqos/fw_cfg.h" 15*1e8a1faeSThomas Huth #include "libqtest.h" 16*1e8a1faeSThomas Huth #include "qapi/qmp/qdict.h" 17*1e8a1faeSThomas Huth #include "standard-headers/linux/qemu_fw_cfg.h" 18*1e8a1faeSThomas Huth 19*1e8a1faeSThomas Huth /* TODO actually test the results and get rid of this */ 20*1e8a1faeSThomas Huth #define qmp_discard_response(qs, ...) qobject_unref(qtest_qmp(qs, __VA_ARGS__)) 21*1e8a1faeSThomas Huth 22*1e8a1faeSThomas Huth typedef struct { 23*1e8a1faeSThomas Huth const char *args; 24*1e8a1faeSThomas Huth uint64_t expected_boot; 25*1e8a1faeSThomas Huth uint64_t expected_reboot; 26*1e8a1faeSThomas Huth } boot_order_test; 27*1e8a1faeSThomas Huth 28*1e8a1faeSThomas Huth static void test_a_boot_order(const char *machine, 29*1e8a1faeSThomas Huth const char *test_args, 30*1e8a1faeSThomas Huth uint64_t (*read_boot_order)(QTestState *), 31*1e8a1faeSThomas Huth uint64_t expected_boot, 32*1e8a1faeSThomas Huth uint64_t expected_reboot) 33*1e8a1faeSThomas Huth { 34*1e8a1faeSThomas Huth uint64_t actual; 35*1e8a1faeSThomas Huth QTestState *qts; 36*1e8a1faeSThomas Huth 37*1e8a1faeSThomas Huth qts = qtest_initf("-nodefaults%s%s %s", machine ? " -M " : "", 38*1e8a1faeSThomas Huth machine ?: "", test_args); 39*1e8a1faeSThomas Huth actual = read_boot_order(qts); 40*1e8a1faeSThomas Huth g_assert_cmphex(actual, ==, expected_boot); 41*1e8a1faeSThomas Huth qmp_discard_response(qts, "{ 'execute': 'system_reset' }"); 42*1e8a1faeSThomas Huth /* 43*1e8a1faeSThomas Huth * system_reset only requests reset. We get a RESET event after 44*1e8a1faeSThomas Huth * the actual reset completes. Need to wait for that. 45*1e8a1faeSThomas Huth */ 46*1e8a1faeSThomas Huth qtest_qmp_eventwait(qts, "RESET"); 47*1e8a1faeSThomas Huth actual = read_boot_order(qts); 48*1e8a1faeSThomas Huth g_assert_cmphex(actual, ==, expected_reboot); 49*1e8a1faeSThomas Huth qtest_quit(qts); 50*1e8a1faeSThomas Huth } 51*1e8a1faeSThomas Huth 52*1e8a1faeSThomas Huth static void test_boot_orders(const char *machine, 53*1e8a1faeSThomas Huth uint64_t (*read_boot_order)(QTestState *), 54*1e8a1faeSThomas Huth const boot_order_test *tests) 55*1e8a1faeSThomas Huth { 56*1e8a1faeSThomas Huth int i; 57*1e8a1faeSThomas Huth 58*1e8a1faeSThomas Huth for (i = 0; tests[i].args; i++) { 59*1e8a1faeSThomas Huth test_a_boot_order(machine, tests[i].args, 60*1e8a1faeSThomas Huth read_boot_order, 61*1e8a1faeSThomas Huth tests[i].expected_boot, 62*1e8a1faeSThomas Huth tests[i].expected_reboot); 63*1e8a1faeSThomas Huth } 64*1e8a1faeSThomas Huth } 65*1e8a1faeSThomas Huth 66*1e8a1faeSThomas Huth static uint8_t read_mc146818(QTestState *qts, uint16_t port, uint8_t reg) 67*1e8a1faeSThomas Huth { 68*1e8a1faeSThomas Huth qtest_outb(qts, port, reg); 69*1e8a1faeSThomas Huth return qtest_inb(qts, port + 1); 70*1e8a1faeSThomas Huth } 71*1e8a1faeSThomas Huth 72*1e8a1faeSThomas Huth static uint64_t read_boot_order_pc(QTestState *qts) 73*1e8a1faeSThomas Huth { 74*1e8a1faeSThomas Huth uint8_t b1 = read_mc146818(qts, 0x70, 0x38); 75*1e8a1faeSThomas Huth uint8_t b2 = read_mc146818(qts, 0x70, 0x3d); 76*1e8a1faeSThomas Huth 77*1e8a1faeSThomas Huth return b1 | (b2 << 8); 78*1e8a1faeSThomas Huth } 79*1e8a1faeSThomas Huth 80*1e8a1faeSThomas Huth static const boot_order_test test_cases_pc[] = { 81*1e8a1faeSThomas Huth { "", 82*1e8a1faeSThomas Huth 0x1230, 0x1230 }, 83*1e8a1faeSThomas Huth { "-no-fd-bootchk", 84*1e8a1faeSThomas Huth 0x1231, 0x1231 }, 85*1e8a1faeSThomas Huth { "-boot c", 86*1e8a1faeSThomas Huth 0x0200, 0x0200 }, 87*1e8a1faeSThomas Huth { "-boot nda", 88*1e8a1faeSThomas Huth 0x3410, 0x3410 }, 89*1e8a1faeSThomas Huth { "-boot order=", 90*1e8a1faeSThomas Huth 0, 0 }, 91*1e8a1faeSThomas Huth { "-boot order= -boot order=c", 92*1e8a1faeSThomas Huth 0x0200, 0x0200 }, 93*1e8a1faeSThomas Huth { "-boot once=a", 94*1e8a1faeSThomas Huth 0x0100, 0x1230 }, 95*1e8a1faeSThomas Huth { "-boot once=a -no-fd-bootchk", 96*1e8a1faeSThomas Huth 0x0101, 0x1231 }, 97*1e8a1faeSThomas Huth { "-boot once=a,order=c", 98*1e8a1faeSThomas Huth 0x0100, 0x0200 }, 99*1e8a1faeSThomas Huth { "-boot once=d -boot order=nda", 100*1e8a1faeSThomas Huth 0x0300, 0x3410 }, 101*1e8a1faeSThomas Huth { "-boot once=a -boot once=b -boot once=c", 102*1e8a1faeSThomas Huth 0x0200, 0x1230 }, 103*1e8a1faeSThomas Huth {} 104*1e8a1faeSThomas Huth }; 105*1e8a1faeSThomas Huth 106*1e8a1faeSThomas Huth static void test_pc_boot_order(void) 107*1e8a1faeSThomas Huth { 108*1e8a1faeSThomas Huth test_boot_orders(NULL, read_boot_order_pc, test_cases_pc); 109*1e8a1faeSThomas Huth } 110*1e8a1faeSThomas Huth 111*1e8a1faeSThomas Huth static uint8_t read_m48t59(QTestState *qts, uint64_t addr, uint16_t reg) 112*1e8a1faeSThomas Huth { 113*1e8a1faeSThomas Huth qtest_writeb(qts, addr, reg & 0xff); 114*1e8a1faeSThomas Huth qtest_writeb(qts, addr + 1, reg >> 8); 115*1e8a1faeSThomas Huth return qtest_readb(qts, addr + 3); 116*1e8a1faeSThomas Huth } 117*1e8a1faeSThomas Huth 118*1e8a1faeSThomas Huth static uint64_t read_boot_order_prep(QTestState *qts) 119*1e8a1faeSThomas Huth { 120*1e8a1faeSThomas Huth return read_m48t59(qts, 0x80000000 + 0x74, 0x34); 121*1e8a1faeSThomas Huth } 122*1e8a1faeSThomas Huth 123*1e8a1faeSThomas Huth static const boot_order_test test_cases_prep[] = { 124*1e8a1faeSThomas Huth { "", 'c', 'c' }, 125*1e8a1faeSThomas Huth { "-boot c", 'c', 'c' }, 126*1e8a1faeSThomas Huth { "-boot d", 'd', 'd' }, 127*1e8a1faeSThomas Huth {} 128*1e8a1faeSThomas Huth }; 129*1e8a1faeSThomas Huth 130*1e8a1faeSThomas Huth static void test_prep_boot_order(void) 131*1e8a1faeSThomas Huth { 132*1e8a1faeSThomas Huth test_boot_orders("prep", read_boot_order_prep, test_cases_prep); 133*1e8a1faeSThomas Huth } 134*1e8a1faeSThomas Huth 135*1e8a1faeSThomas Huth static uint64_t read_boot_order_pmac(QTestState *qts) 136*1e8a1faeSThomas Huth { 137*1e8a1faeSThomas Huth QFWCFG *fw_cfg = mm_fw_cfg_init(qts, 0xf0000510); 138*1e8a1faeSThomas Huth 139*1e8a1faeSThomas Huth return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE); 140*1e8a1faeSThomas Huth } 141*1e8a1faeSThomas Huth 142*1e8a1faeSThomas Huth static const boot_order_test test_cases_fw_cfg[] = { 143*1e8a1faeSThomas Huth { "", 'c', 'c' }, 144*1e8a1faeSThomas Huth { "-boot c", 'c', 'c' }, 145*1e8a1faeSThomas Huth { "-boot d", 'd', 'd' }, 146*1e8a1faeSThomas Huth { "-boot once=d,order=c", 'd', 'c' }, 147*1e8a1faeSThomas Huth {} 148*1e8a1faeSThomas Huth }; 149*1e8a1faeSThomas Huth 150*1e8a1faeSThomas Huth static void test_pmac_oldworld_boot_order(void) 151*1e8a1faeSThomas Huth { 152*1e8a1faeSThomas Huth test_boot_orders("g3beige", read_boot_order_pmac, test_cases_fw_cfg); 153*1e8a1faeSThomas Huth } 154*1e8a1faeSThomas Huth 155*1e8a1faeSThomas Huth static void test_pmac_newworld_boot_order(void) 156*1e8a1faeSThomas Huth { 157*1e8a1faeSThomas Huth test_boot_orders("mac99", read_boot_order_pmac, test_cases_fw_cfg); 158*1e8a1faeSThomas Huth } 159*1e8a1faeSThomas Huth 160*1e8a1faeSThomas Huth static uint64_t read_boot_order_sun4m(QTestState *qts) 161*1e8a1faeSThomas Huth { 162*1e8a1faeSThomas Huth QFWCFG *fw_cfg = mm_fw_cfg_init(qts, 0xd00000510ULL); 163*1e8a1faeSThomas Huth 164*1e8a1faeSThomas Huth return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE); 165*1e8a1faeSThomas Huth } 166*1e8a1faeSThomas Huth 167*1e8a1faeSThomas Huth static void test_sun4m_boot_order(void) 168*1e8a1faeSThomas Huth { 169*1e8a1faeSThomas Huth test_boot_orders("SS-5", read_boot_order_sun4m, test_cases_fw_cfg); 170*1e8a1faeSThomas Huth } 171*1e8a1faeSThomas Huth 172*1e8a1faeSThomas Huth static uint64_t read_boot_order_sun4u(QTestState *qts) 173*1e8a1faeSThomas Huth { 174*1e8a1faeSThomas Huth QFWCFG *fw_cfg = io_fw_cfg_init(qts, 0x510); 175*1e8a1faeSThomas Huth 176*1e8a1faeSThomas Huth return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE); 177*1e8a1faeSThomas Huth } 178*1e8a1faeSThomas Huth 179*1e8a1faeSThomas Huth static void test_sun4u_boot_order(void) 180*1e8a1faeSThomas Huth { 181*1e8a1faeSThomas Huth test_boot_orders("sun4u", read_boot_order_sun4u, test_cases_fw_cfg); 182*1e8a1faeSThomas Huth } 183*1e8a1faeSThomas Huth 184*1e8a1faeSThomas Huth int main(int argc, char *argv[]) 185*1e8a1faeSThomas Huth { 186*1e8a1faeSThomas Huth const char *arch = qtest_get_arch(); 187*1e8a1faeSThomas Huth 188*1e8a1faeSThomas Huth g_test_init(&argc, &argv, NULL); 189*1e8a1faeSThomas Huth 190*1e8a1faeSThomas Huth if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { 191*1e8a1faeSThomas Huth qtest_add_func("boot-order/pc", test_pc_boot_order); 192*1e8a1faeSThomas Huth } else if (strcmp(arch, "ppc") == 0 || strcmp(arch, "ppc64") == 0) { 193*1e8a1faeSThomas Huth qtest_add_func("boot-order/prep", test_prep_boot_order); 194*1e8a1faeSThomas Huth qtest_add_func("boot-order/pmac_oldworld", 195*1e8a1faeSThomas Huth test_pmac_oldworld_boot_order); 196*1e8a1faeSThomas Huth qtest_add_func("boot-order/pmac_newworld", 197*1e8a1faeSThomas Huth test_pmac_newworld_boot_order); 198*1e8a1faeSThomas Huth } else if (strcmp(arch, "sparc") == 0) { 199*1e8a1faeSThomas Huth qtest_add_func("boot-order/sun4m", test_sun4m_boot_order); 200*1e8a1faeSThomas Huth } else if (strcmp(arch, "sparc64") == 0) { 201*1e8a1faeSThomas Huth qtest_add_func("boot-order/sun4u", test_sun4u_boot_order); 202*1e8a1faeSThomas Huth } 203*1e8a1faeSThomas Huth 204*1e8a1faeSThomas Huth return g_test_run(); 205*1e8a1faeSThomas Huth } 206