blob: a725bce7298f92910102fb8818436e1138340309 [file] [log] [blame]
Markus Armbrusteredbd7902013-06-26 15:52:13 +02001/*
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
Peter Maydell681c28a2016-02-08 18:08:51 +000013#include "qemu/osdep.h"
Andreas Färber530a7e42013-06-26 15:52:16 +020014#include "libqos/fw_cfg.h"
Markus Armbrusteredbd7902013-06-26 15:52:13 +020015#include "libqtest.h"
Markus Armbruster055a1ef2018-08-06 08:53:25 +020016#include "qapi/qmp/qdict.h"
Marc-André Lureau5be5df72018-08-17 17:59:10 +020017#include "standard-headers/linux/qemu_fw_cfg.h"
Markus Armbrusteredbd7902013-06-26 15:52:13 +020018
Markus Armbruster055a1ef2018-08-06 08:53:25 +020019/* TODO actually test the results and get rid of this */
Thomas Huth81736682019-01-03 12:09:51 +010020#define qmp_discard_response(qs, ...) qobject_unref(qtest_qmp(qs, __VA_ARGS__))
Markus Armbruster055a1ef2018-08-06 08:53:25 +020021
Markus Armbruster484986e2013-06-26 15:52:19 +020022typedef struct {
23 const char *args;
24 uint64_t expected_boot;
25 uint64_t expected_reboot;
26} boot_order_test;
Markus Armbrusteredbd7902013-06-26 15:52:13 +020027
Markus Armbrusteraea6a162013-06-26 15:52:18 +020028static void test_a_boot_order(const char *machine,
29 const char *test_args,
Thomas Huth81736682019-01-03 12:09:51 +010030 uint64_t (*read_boot_order)(QTestState *),
Markus Armbrusteraea6a162013-06-26 15:52:18 +020031 uint64_t expected_boot,
32 uint64_t expected_reboot)
Markus Armbrusteredbd7902013-06-26 15:52:13 +020033{
Markus Armbrusteraea6a162013-06-26 15:52:18 +020034 uint64_t actual;
Thomas Huth81736682019-01-03 12:09:51 +010035 QTestState *qts;
Markus Armbrusteredbd7902013-06-26 15:52:13 +020036
Thomas Huth81736682019-01-03 12:09:51 +010037 qts = qtest_initf("-nodefaults%s%s %s", machine ? " -M " : "",
38 machine ?: "", test_args);
39 actual = read_boot_order(qts);
Markus Armbrusteraea6a162013-06-26 15:52:18 +020040 g_assert_cmphex(actual, ==, expected_boot);
Thomas Huth81736682019-01-03 12:09:51 +010041 qmp_discard_response(qts, "{ 'execute': 'system_reset' }");
Markus Armbrusteredbd7902013-06-26 15:52:13 +020042 /*
43 * system_reset only requests reset. We get a RESET event after
44 * the actual reset completes. Need to wait for that.
45 */
Thomas Huth81736682019-01-03 12:09:51 +010046 qtest_qmp_eventwait(qts, "RESET");
47 actual = read_boot_order(qts);
Andreas Färber530a7e42013-06-26 15:52:16 +020048 g_assert_cmphex(actual, ==, expected_reboot);
Thomas Huth81736682019-01-03 12:09:51 +010049 qtest_quit(qts);
Andreas Färber530a7e42013-06-26 15:52:16 +020050}
51
Markus Armbrusteraea6a162013-06-26 15:52:18 +020052static void test_boot_orders(const char *machine,
Thomas Huth81736682019-01-03 12:09:51 +010053 uint64_t (*read_boot_order)(QTestState *),
Markus Armbrusteraea6a162013-06-26 15:52:18 +020054 const boot_order_test *tests)
Andreas Färber530a7e42013-06-26 15:52:16 +020055{
56 int i;
57
Markus Armbrusteraea6a162013-06-26 15:52:18 +020058 for (i = 0; tests[i].args; i++) {
59 test_a_boot_order(machine, tests[i].args,
60 read_boot_order,
61 tests[i].expected_boot,
62 tests[i].expected_reboot);
Andreas Färber530a7e42013-06-26 15:52:16 +020063 }
64}
65
Thomas Huth81736682019-01-03 12:09:51 +010066static uint8_t read_mc146818(QTestState *qts, uint16_t port, uint8_t reg)
Markus Armbruster484986e2013-06-26 15:52:19 +020067{
Thomas Huth81736682019-01-03 12:09:51 +010068 qtest_outb(qts, port, reg);
69 return qtest_inb(qts, port + 1);
Markus Armbruster484986e2013-06-26 15:52:19 +020070}
71
Thomas Huth81736682019-01-03 12:09:51 +010072static uint64_t read_boot_order_pc(QTestState *qts)
Markus Armbruster484986e2013-06-26 15:52:19 +020073{
Thomas Huth81736682019-01-03 12:09:51 +010074 uint8_t b1 = read_mc146818(qts, 0x70, 0x38);
75 uint8_t b2 = read_mc146818(qts, 0x70, 0x3d);
Markus Armbruster484986e2013-06-26 15:52:19 +020076
77 return b1 | (b2 << 8);
78}
79
Markus Armbrusteraea6a162013-06-26 15:52:18 +020080static const boot_order_test test_cases_pc[] = {
81 { "",
82 0x1230, 0x1230 },
83 { "-no-fd-bootchk",
84 0x1231, 0x1231 },
85 { "-boot c",
86 0x0200, 0x0200 },
87 { "-boot nda",
88 0x3410, 0x3410 },
89 { "-boot order=",
90 0, 0 },
91 { "-boot order= -boot order=c",
92 0x0200, 0x0200 },
93 { "-boot once=a",
94 0x0100, 0x1230 },
95 { "-boot once=a -no-fd-bootchk",
96 0x0101, 0x1231 },
97 { "-boot once=a,order=c",
98 0x0100, 0x0200 },
99 { "-boot once=d -boot order=nda",
100 0x0300, 0x3410 },
101 { "-boot once=a -boot once=b -boot once=c",
102 0x0200, 0x1230 },
103 {}
104};
105
106static void test_pc_boot_order(void)
107{
108 test_boot_orders(NULL, read_boot_order_pc, test_cases_pc);
109}
110
Thomas Huth81736682019-01-03 12:09:51 +0100111static uint8_t read_m48t59(QTestState *qts, uint64_t addr, uint16_t reg)
Markus Armbrustere99f87c2013-06-26 15:52:20 +0200112{
Thomas Huth81736682019-01-03 12:09:51 +0100113 qtest_writeb(qts, addr, reg & 0xff);
114 qtest_writeb(qts, addr + 1, reg >> 8);
115 return qtest_readb(qts, addr + 3);
Markus Armbrustere99f87c2013-06-26 15:52:20 +0200116}
117
Thomas Huth81736682019-01-03 12:09:51 +0100118static uint64_t read_boot_order_prep(QTestState *qts)
Markus Armbrustere99f87c2013-06-26 15:52:20 +0200119{
Thomas Huth81736682019-01-03 12:09:51 +0100120 return read_m48t59(qts, 0x80000000 + 0x74, 0x34);
Markus Armbrustere99f87c2013-06-26 15:52:20 +0200121}
122
123static const boot_order_test test_cases_prep[] = {
124 { "", 'c', 'c' },
125 { "-boot c", 'c', 'c' },
126 { "-boot d", 'd', 'd' },
127 {}
128};
129
130static void test_prep_boot_order(void)
131{
132 test_boot_orders("prep", read_boot_order_prep, test_cases_prep);
133}
134
Thomas Huth81736682019-01-03 12:09:51 +0100135static uint64_t read_boot_order_pmac(QTestState *qts)
Markus Armbrusteraea6a162013-06-26 15:52:18 +0200136{
Thomas Huth81736682019-01-03 12:09:51 +0100137 QFWCFG *fw_cfg = mm_fw_cfg_init(qts, 0xf0000510);
Markus Armbrusteraea6a162013-06-26 15:52:18 +0200138
139 return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
140}
141
142static const boot_order_test test_cases_fw_cfg[] = {
143 { "", 'c', 'c' },
144 { "-boot c", 'c', 'c' },
145 { "-boot d", 'd', 'd' },
146 { "-boot once=d,order=c", 'd', 'c' },
147 {}
148};
149
150static void test_pmac_oldworld_boot_order(void)
151{
152 test_boot_orders("g3beige", read_boot_order_pmac, test_cases_fw_cfg);
153}
154
155static void test_pmac_newworld_boot_order(void)
156{
157 test_boot_orders("mac99", read_boot_order_pmac, test_cases_fw_cfg);
158}
159
Thomas Huth81736682019-01-03 12:09:51 +0100160static uint64_t read_boot_order_sun4m(QTestState *qts)
Markus Armbrusterf88dc7d2013-06-26 15:52:21 +0200161{
Thomas Huth81736682019-01-03 12:09:51 +0100162 QFWCFG *fw_cfg = mm_fw_cfg_init(qts, 0xd00000510ULL);
Markus Armbrusterf88dc7d2013-06-26 15:52:21 +0200163
164 return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
165}
166
167static void test_sun4m_boot_order(void)
168{
169 test_boot_orders("SS-5", read_boot_order_sun4m, test_cases_fw_cfg);
170}
171
Thomas Huth81736682019-01-03 12:09:51 +0100172static uint64_t read_boot_order_sun4u(QTestState *qts)
Markus Armbruster24943972013-06-26 15:52:23 +0200173{
Thomas Huth81736682019-01-03 12:09:51 +0100174 QFWCFG *fw_cfg = io_fw_cfg_init(qts, 0x510);
Markus Armbruster24943972013-06-26 15:52:23 +0200175
176 return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
177}
178
179static void test_sun4u_boot_order(void)
180{
181 test_boot_orders("sun4u", read_boot_order_sun4u, test_cases_fw_cfg);
182}
183
Markus Armbrusteredbd7902013-06-26 15:52:13 +0200184int main(int argc, char *argv[])
185{
Andreas Färber530a7e42013-06-26 15:52:16 +0200186 const char *arch = qtest_get_arch();
187
Markus Armbrusteredbd7902013-06-26 15:52:13 +0200188 g_test_init(&argc, &argv, NULL);
189
Andreas Färber530a7e42013-06-26 15:52:16 +0200190 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
191 qtest_add_func("boot-order/pc", test_pc_boot_order);
192 } else if (strcmp(arch, "ppc") == 0 || strcmp(arch, "ppc64") == 0) {
Markus Armbrustere99f87c2013-06-26 15:52:20 +0200193 qtest_add_func("boot-order/prep", test_prep_boot_order);
Markus Armbrusteraea6a162013-06-26 15:52:18 +0200194 qtest_add_func("boot-order/pmac_oldworld",
195 test_pmac_oldworld_boot_order);
196 qtest_add_func("boot-order/pmac_newworld",
197 test_pmac_newworld_boot_order);
Markus Armbrusterf88dc7d2013-06-26 15:52:21 +0200198 } else if (strcmp(arch, "sparc") == 0) {
199 qtest_add_func("boot-order/sun4m", test_sun4m_boot_order);
Markus Armbruster24943972013-06-26 15:52:23 +0200200 } else if (strcmp(arch, "sparc64") == 0) {
201 qtest_add_func("boot-order/sun4u", test_sun4u_boot_order);
Andreas Färber530a7e42013-06-26 15:52:16 +0200202 }
Markus Armbrusteredbd7902013-06-26 15:52:13 +0200203
204 return g_test_run();
205}