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