]> git.proxmox.com Git - mirror_qemu.git/blame - tests/boot-order-test.c
boot-order-test: Better separate target-specific and generic parts
[mirror_qemu.git] / tests / boot-order-test.c
CommitLineData
edbd790d
MA
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
530a7e48 13#include <string.h>
edbd790d 14#include <glib.h>
530a7e48 15#include "libqos/fw_cfg.h"
edbd790d
MA
16#include "libqtest.h"
17
aea6a169 18static uint8_t read_mc146818(uint16_t port, uint8_t reg)
edbd790d 19{
aea6a169
MA
20 outb(port, reg);
21 return inb(port + 1);
edbd790d
MA
22}
23
aea6a169 24static uint64_t read_boot_order_pc(void)
edbd790d 25{
aea6a169
MA
26 uint8_t b1 = read_mc146818(0x70, 0x38);
27 uint8_t b2 = read_mc146818(0x70, 0x3d);
28
29 return b1 | (b2 << 8);
edbd790d
MA
30}
31
aea6a169
MA
32static void test_a_boot_order(const char *machine,
33 const char *test_args,
34 uint64_t (*read_boot_order)(void),
35 uint64_t expected_boot,
36 uint64_t expected_reboot)
edbd790d 37{
aea6a169
MA
38 char *args;
39 uint64_t actual;
edbd790d 40
aea6a169
MA
41 args = g_strdup_printf("-nodefaults -display none%s%s %s",
42 machine ? " -M " : "",
43 machine ?: "",
44 test_args);
edbd790d 45 qtest_start(args);
aea6a169
MA
46 actual = read_boot_order();
47 g_assert_cmphex(actual, ==, expected_boot);
edbd790d
MA
48 qmp("{ 'execute': 'system_reset' }");
49 /*
50 * system_reset only requests reset. We get a RESET event after
51 * the actual reset completes. Need to wait for that.
52 */
53 qmp(""); /* HACK: wait for event */
aea6a169
MA
54 actual = read_boot_order();
55 g_assert_cmphex(actual, ==, expected_reboot);
edbd790d
MA
56 qtest_quit(global_qtest);
57 g_free(args);
58}
59
aea6a169
MA
60typedef struct {
61 const char *args;
62 uint64_t expected_boot;
63 uint64_t expected_reboot;
64} boot_order_test;
65
66static void test_boot_orders(const char *machine,
67 uint64_t (*read_boot_order)(void),
68 const boot_order_test *tests)
edbd790d 69{
aea6a169
MA
70 int i;
71
72 for (i = 0; tests[i].args; i++) {
73 test_a_boot_order(machine, tests[i].args,
74 read_boot_order,
75 tests[i].expected_boot,
76 tests[i].expected_reboot);
77 }
edbd790d
MA
78}
79
aea6a169
MA
80static 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}
530a7e48
AF
110
111#define NO_QEMU_PROTOS
112#include "hw/nvram/fw_cfg.h"
113#undef NO_QEMU_PROTOS
114
aea6a169 115static uint64_t read_boot_order_pmac(void)
530a7e48 116{
aea6a169 117 QFWCFG *fw_cfg = mm_fw_cfg_init(0xf0000510);
530a7e48 118
aea6a169 119 return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
530a7e48
AF
120}
121
aea6a169
MA
122static const boot_order_test test_cases_fw_cfg[] = {
123 { "", 'c', 'c' },
124 { "-boot c", 'c', 'c' },
125 { "-boot d", 'd', 'd' },
126 { "-boot once=d,order=c", 'd', 'c' },
127 {}
128};
530a7e48 129
aea6a169
MA
130static void test_pmac_oldworld_boot_order(void)
131{
132 test_boot_orders("g3beige", read_boot_order_pmac, test_cases_fw_cfg);
133}
530a7e48 134
aea6a169
MA
135static void test_pmac_newworld_boot_order(void)
136{
137 test_boot_orders("mac99", read_boot_order_pmac, test_cases_fw_cfg);
530a7e48
AF
138}
139
edbd790d
MA
140int main(int argc, char *argv[])
141{
530a7e48
AF
142 const char *arch = qtest_get_arch();
143
edbd790d
MA
144 g_test_init(&argc, &argv, NULL);
145
530a7e48
AF
146 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
147 qtest_add_func("boot-order/pc", test_pc_boot_order);
148 } else if (strcmp(arch, "ppc") == 0 || strcmp(arch, "ppc64") == 0) {
aea6a169
MA
149 qtest_add_func("boot-order/pmac_oldworld",
150 test_pmac_oldworld_boot_order);
151 qtest_add_func("boot-order/pmac_newworld",
152 test_pmac_newworld_boot_order);
530a7e48 153 }
edbd790d
MA
154
155 return g_test_run();
156}