xref: /openbmc/qemu/tests/qtest/boot-order-test.c (revision b14df228)
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 "qapi/qmp/qdict.h"
17 #include "standard-headers/linux/qemu_fw_cfg.h"
18 
19 /* TODO actually test the results and get rid of this */
20 #define qmp_discard_response(qs, ...) qobject_unref(qtest_qmp(qs, __VA_ARGS__))
21 
22 typedef struct {
23     const char *args;
24     uint64_t expected_boot;
25     uint64_t expected_reboot;
26 } boot_order_test;
27 
28 static void test_a_boot_order(const char *machine,
29                               const char *test_args,
30                               uint64_t (*read_boot_order)(QTestState *),
31                               uint64_t expected_boot,
32                               uint64_t expected_reboot)
33 {
34     uint64_t actual;
35     QTestState *qts;
36 
37     if (machine && !qtest_has_machine(machine)) {
38         g_test_skip("Machine is not available");
39         return;
40     }
41 
42     qts = qtest_initf("-nodefaults%s%s %s", machine ? " -M " : "",
43                       machine ?: "", test_args);
44     actual = read_boot_order(qts);
45     g_assert_cmphex(actual, ==, expected_boot);
46     qmp_discard_response(qts, "{ 'execute': 'system_reset' }");
47     /*
48      * system_reset only requests reset.  We get a RESET event after
49      * the actual reset completes.  Need to wait for that.
50      */
51     qtest_qmp_eventwait(qts, "RESET");
52     actual = read_boot_order(qts);
53     g_assert_cmphex(actual, ==, expected_reboot);
54     qtest_quit(qts);
55 }
56 
57 static void test_boot_orders(const char *machine,
58                              uint64_t (*read_boot_order)(QTestState *),
59                              const boot_order_test *tests)
60 {
61     int i;
62 
63     for (i = 0; tests[i].args; i++) {
64         test_a_boot_order(machine, tests[i].args,
65                           read_boot_order,
66                           tests[i].expected_boot,
67                           tests[i].expected_reboot);
68     }
69 }
70 
71 static uint8_t read_mc146818(QTestState *qts, uint16_t port, uint8_t reg)
72 {
73     qtest_outb(qts, port, reg);
74     return qtest_inb(qts, port + 1);
75 }
76 
77 static uint64_t read_boot_order_pc(QTestState *qts)
78 {
79     uint8_t b1 = read_mc146818(qts, 0x70, 0x38);
80     uint8_t b2 = read_mc146818(qts, 0x70, 0x3d);
81 
82     return b1 | (b2 << 8);
83 }
84 
85 static const boot_order_test test_cases_pc[] = {
86     { "",
87       0x1230, 0x1230 },
88     { "-no-fd-bootchk",
89       0x1231, 0x1231 },
90     { "-boot c",
91       0x0200, 0x0200 },
92     { "-boot nda",
93       0x3410, 0x3410 },
94     { "-boot order=",
95       0, 0 },
96     { "-boot order= -boot order=c",
97       0x0200, 0x0200 },
98     { "-boot once=a",
99       0x0100, 0x1230 },
100     { "-boot once=a -no-fd-bootchk",
101       0x0101, 0x1231 },
102     { "-boot once=a,order=c",
103       0x0100, 0x0200 },
104     { "-boot once=d -boot order=nda",
105       0x0300, 0x3410 },
106     { "-boot once=a -boot once=b -boot once=c",
107       0x0200, 0x1230 },
108     {}
109 };
110 
111 static void test_pc_boot_order(void)
112 {
113     test_boot_orders(NULL, read_boot_order_pc, test_cases_pc);
114 }
115 
116 static uint64_t read_boot_order_pmac(QTestState *qts)
117 {
118     g_autoptr(QFWCFG) fw_cfg = mm_fw_cfg_init(qts, 0xf0000510);
119 
120     return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
121 }
122 
123 static const boot_order_test test_cases_fw_cfg[] = {
124     { "", 'c', 'c' },
125     { "-boot c", 'c', 'c' },
126     { "-boot d", 'd', 'd' },
127     { "-boot once=d,order=c", 'd', 'c' },
128     {}
129 };
130 
131 static void test_pmac_oldworld_boot_order(void)
132 {
133     test_boot_orders("g3beige", read_boot_order_pmac, test_cases_fw_cfg);
134 }
135 
136 static void test_pmac_newworld_boot_order(void)
137 {
138     test_boot_orders("mac99", read_boot_order_pmac, test_cases_fw_cfg);
139 }
140 
141 static uint64_t read_boot_order_sun4m(QTestState *qts)
142 {
143     g_autoptr(QFWCFG) fw_cfg = mm_fw_cfg_init(qts, 0xd00000510ULL);
144 
145     return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
146 }
147 
148 static void test_sun4m_boot_order(void)
149 {
150     test_boot_orders("SS-5", read_boot_order_sun4m, test_cases_fw_cfg);
151 }
152 
153 static uint64_t read_boot_order_sun4u(QTestState *qts)
154 {
155     g_autoptr(QFWCFG) fw_cfg = io_fw_cfg_init(qts, 0x510);
156 
157     return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
158 }
159 
160 static void test_sun4u_boot_order(void)
161 {
162     test_boot_orders("sun4u", read_boot_order_sun4u, test_cases_fw_cfg);
163 }
164 
165 int main(int argc, char *argv[])
166 {
167     const char *arch = qtest_get_arch();
168 
169     g_test_init(&argc, &argv, NULL);
170 
171     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
172         qtest_add_func("boot-order/pc", test_pc_boot_order);
173     } else if (strcmp(arch, "ppc") == 0 || strcmp(arch, "ppc64") == 0) {
174         qtest_add_func("boot-order/pmac_oldworld",
175                        test_pmac_oldworld_boot_order);
176         qtest_add_func("boot-order/pmac_newworld",
177                        test_pmac_newworld_boot_order);
178     } else if (strcmp(arch, "sparc") == 0) {
179         qtest_add_func("boot-order/sun4m", test_sun4m_boot_order);
180     } else if (strcmp(arch, "sparc64") == 0) {
181         qtest_add_func("boot-order/sun4u", test_sun4u_boot_order);
182     }
183 
184     return g_test_run();
185 }
186