forked from libvirt/libvirt
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathqemufirmwaretest.c
269 lines (218 loc) · 8.57 KB
/
qemufirmwaretest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <config.h>
#include <inttypes.h>
#include "testutils.h"
#include "virfilewrapper.h"
#include "qemu/qemu_firmware.h"
#include "configmake.h"
#define VIR_FROM_THIS VIR_FROM_QEMU
/* A very basic test. Parse given JSON firmware description into
* an internal structure, format it back and compare with the
* contents of the file (minus some keys that are not parsed).
*/
static int
testParseFormatFW(const void *opaque)
{
const char *filename = opaque;
g_autofree char *path = NULL;
g_autoptr(qemuFirmware) fw = NULL;
g_autofree char *buf = NULL;
g_autoptr(virJSONValue) json = NULL;
g_autofree char *expected = NULL;
g_autofree char *actual = NULL;
path = g_strdup_printf("%s/qemufirmwaredata/%s", abs_srcdir, filename);
if (!(fw = qemuFirmwareParse(path)))
return -1;
if (virFileReadAll(path,
1024 * 1024, /* 1MiB */
&buf) < 0)
return -1;
if (!(json = virJSONValueFromString(buf)))
return -1;
/* Description and tags are not parsed. */
if (virJSONValueObjectRemoveKey(json, "description", NULL) < 0 ||
virJSONValueObjectRemoveKey(json, "tags", NULL) < 0)
return -1;
if (!(expected = virJSONValueToString(json, true)))
return -1;
if (!(actual = qemuFirmwareFormat(fw)))
return -1;
return virTestCompareToString(expected, actual);
}
static int
testFWPrecedence(const void *opaque G_GNUC_UNUSED)
{
g_autofree char *fakehome = NULL;
VIR_AUTOSTRINGLIST fwList = NULL;
size_t nfwList;
size_t i;
const char *expected[] = {
PREFIX "/share/qemu/firmware/40-bios.json",
SYSCONFDIR "/qemu/firmware/40-ovmf-sb-keys.json",
PREFIX "/share/qemu/firmware/50-ovmf-sb-keys.json",
PREFIX "/share/qemu/firmware/61-ovmf.json",
PREFIX "/share/qemu/firmware/70-aavmf.json",
};
const size_t nexpected = G_N_ELEMENTS(expected);
fakehome = g_strdup(abs_srcdir "/qemufirmwaredata/home/user/.config");
g_setenv("XDG_CONFIG_HOME", fakehome, TRUE);
if (qemuFirmwareFetchConfigs(&fwList, false) < 0)
return -1;
if (!fwList) {
fprintf(stderr, "Expected a non-NULL result, but got a NULL result\n");
return -1;
}
nfwList = virStringListLength((const char **)fwList);
for (i = 0; i < MAX(nfwList, nexpected); i++) {
const char *e = i < nexpected ? expected[i] : NULL;
const char *f = i < nfwList ? fwList[i] : NULL;
if (STRNEQ_NULLABLE(e, f)) {
fprintf(stderr,
"Unexpected path (i=%zu). Expected %s got %s \n",
i, NULLSTR(e), NULLSTR(f));
return -1;
}
}
return 0;
}
struct supportedData {
const char *machine;
virArch arch;
bool secure;
const char *fwlist;
unsigned int *interfaces;
size_t ninterfaces;
};
static int
testSupportedFW(const void *opaque)
{
const struct supportedData *data = opaque;
uint64_t actualInterfaces;
uint64_t expectedInterfaces = 0;
bool actualSecure;
virFirmwarePtr *expFWs = NULL;
size_t nexpFWs = 0;
virFirmwarePtr *actFWs = NULL;
size_t nactFWs = 0;
size_t i;
int ret = -1;
for (i = 0; i < data->ninterfaces; i++)
expectedInterfaces |= 1ULL << data->interfaces[i];
if (virFirmwareParseList(data->fwlist, &expFWs, &nexpFWs) < 0) {
fprintf(stderr, "Unable to parse list of expected FW paths\n");
return -1;
}
/* virFirmwareParseList() expects to see pairs of paths: ${FW}:${NVRAM}.
* Well, some images don't have a NVRAM store. In that case NULL was passed:
* ${FW}:NULL. Now iterate over expected firmwares and fix this. */
for (i = 0; i < nexpFWs; i++) {
virFirmwarePtr tmp = expFWs[i];
if (STREQ(tmp->nvram, "NULL"))
VIR_FREE(tmp->nvram);
}
if (qemuFirmwareGetSupported(data->machine, data->arch, false,
&actualInterfaces, &actualSecure, &actFWs, &nactFWs) < 0) {
fprintf(stderr, "Unable to get list of supported interfaces\n");
goto cleanup;
}
if (actualInterfaces != expectedInterfaces) {
fprintf(stderr,
"Mismatch in supported interfaces. "
"Expected 0x%" PRIx64 " got 0x%" PRIx64 "\n",
expectedInterfaces, actualInterfaces);
goto cleanup;
}
if (actualSecure != data->secure) {
fprintf(stderr,
"Mismatch in SMM requirement/support. "
"Expected %d got %d\n",
data->secure, actualSecure);
goto cleanup;
}
for (i = 0; i < nactFWs; i++) {
virFirmwarePtr actFW = actFWs[i];
virFirmwarePtr expFW = NULL;
if (i >= nexpFWs) {
fprintf(stderr, "Unexpected FW image: %s NVRAM: %s\n",
actFW->name, NULLSTR(actFW->nvram));
goto cleanup;
}
expFW = expFWs[i];
if (STRNEQ(actFW->name, expFW->name) ||
STRNEQ_NULLABLE(actFW->nvram, expFW->nvram)) {
fprintf(stderr, "Unexpected FW image: %s NVRAM: %s\n"
"Expected: %s NVRAM: %s\n",
actFW->name, NULLSTR(actFW->nvram),
expFW->name, NULLSTR(expFW->nvram));
goto cleanup;
}
}
if (i < nexpFWs) {
fprintf(stderr, "Expected FW image: %s NVRAM: %s got nothing\n",
expFWs[i]->name, NULLSTR(expFWs[i]->nvram));
goto cleanup;
}
ret = 0;
cleanup:
virFirmwareFreeList(actFWs, nactFWs);
virFirmwareFreeList(expFWs, nexpFWs);
return ret;
}
static int
mymain(void)
{
int ret = 0;
virFileWrapperAddPrefix(SYSCONFDIR "/qemu/firmware",
abs_srcdir "/qemufirmwaredata/etc/qemu/firmware");
virFileWrapperAddPrefix(PREFIX "/share/qemu/firmware",
abs_srcdir "/qemufirmwaredata/usr/share/qemu/firmware");
virFileWrapperAddPrefix("/home/user/.config/qemu/firmware",
abs_srcdir "/qemufirmwaredata/home/user/.config/qemu/firmware");
#define DO_PARSE_TEST(filename) \
do { \
if (virTestRun("QEMU FW " filename, \
testParseFormatFW, filename) < 0) \
ret = -1; \
} while (0)
DO_PARSE_TEST("usr/share/qemu/firmware/40-bios.json");
DO_PARSE_TEST("usr/share/qemu/firmware/50-ovmf-sb-keys.json");
DO_PARSE_TEST("usr/share/qemu/firmware/60-ovmf-sb.json");
DO_PARSE_TEST("usr/share/qemu/firmware/61-ovmf.json");
DO_PARSE_TEST("usr/share/qemu/firmware/70-aavmf.json");
if (virTestRun("QEMU FW precedence test", testFWPrecedence, NULL) < 0)
ret = -1;
/* The @fwlist contains pairs of ${FW}:${NVRAM}. If there's
* no NVRAM expected pass literal "NULL" and test fixes that
* later. */
#define DO_SUPPORTED_TEST(machine, arch, secure, fwlist, ...) \
do { \
unsigned int interfaces[] = {__VA_ARGS__}; \
struct supportedData data = {machine, arch, secure, fwlist, \
interfaces, G_N_ELEMENTS(interfaces)}; \
if (virTestRun("QEMU FW SUPPORTED " machine " " #arch, \
testSupportedFW, &data) < 0) \
ret = -1; \
} while (0)
DO_SUPPORTED_TEST("pc-i440fx-3.1", VIR_ARCH_X86_64, false,
"/usr/share/seabios/bios-256k.bin:NULL:"
"/usr/share/OVMF/OVMF_CODE.fd:/usr/share/OVMF/OVMF_VARS.fd",
VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS,
VIR_DOMAIN_OS_DEF_FIRMWARE_EFI);
DO_SUPPORTED_TEST("pc-i440fx-3.1", VIR_ARCH_I686, false,
"/usr/share/seabios/bios-256k.bin:NULL",
VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS);
DO_SUPPORTED_TEST("pc-q35-3.1", VIR_ARCH_X86_64, true,
"/usr/share/seabios/bios-256k.bin:NULL:"
"/usr/share/OVMF/OVMF_CODE.secboot.fd:/usr/share/OVMF/OVMF_VARS.secboot.fd:"
"/usr/share/OVMF/OVMF_CODE.fd:/usr/share/OVMF/OVMF_VARS.fd",
VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS,
VIR_DOMAIN_OS_DEF_FIRMWARE_EFI);
DO_SUPPORTED_TEST("pc-q35-3.1", VIR_ARCH_I686, false,
"/usr/share/seabios/bios-256k.bin:NULL",
VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS);
DO_SUPPORTED_TEST("virt-3.1", VIR_ARCH_AARCH64, false,
"/usr/share/AAVMF/AAVMF_CODE.fd:/usr/share/AAVMF/AAVMF_VARS.fd",
VIR_DOMAIN_OS_DEF_FIRMWARE_EFI);
virFileWrapperClearPrefixes();
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
VIR_TEST_MAIN(mymain)