]>
Commit | Line | Data |
---|---|---|
ad6423a7 MT |
1 | /* |
2 | * Boot order test cases. | |
3 | * | |
4 | * Copyright (c) 2013 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Michael S. Tsirkin <mst@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 | ||
681c28a3 | 13 | #include "qemu/osdep.h" |
ad6423a7 | 14 | #include <glib.h> |
4500bc98 | 15 | #include <glib/gstdio.h> |
53333801 | 16 | #include "qemu-common.h" |
ad6423a7 | 17 | #include "libqtest.h" |
395e5fb4 | 18 | #include "hw/acpi/acpi-defs.h" |
60d8f328 | 19 | #include "hw/smbios/smbios.h" |
eb386aac | 20 | #include "qemu/bitmap.h" |
4e082566 | 21 | #include "boot-sector.h" |
ad6423a7 | 22 | |
9e8458c0 MA |
23 | #define MACHINE_PC "pc" |
24 | #define MACHINE_Q35 "q35" | |
25 | ||
4500bc98 MA |
26 | #define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML" |
27 | ||
53333801 | 28 | /* DSDT and SSDTs format */ |
ad6423a7 | 29 | typedef struct { |
53333801 | 30 | AcpiTableHeader header; |
9e8458c0 MA |
31 | gchar *aml; /* aml bytecode from guest */ |
32 | gsize aml_len; | |
33 | gchar *aml_file; | |
34 | gchar *asl; /* asl code generated from aml */ | |
35 | gsize asl_len; | |
36 | gchar *asl_file; | |
262f6f51 | 37 | bool tmp_files_retain; /* do not delete the temp asl/aml */ |
9e8458c0 | 38 | } QEMU_PACKED AcpiSdtTable; |
53333801 MA |
39 | |
40 | typedef struct { | |
9e8458c0 | 41 | const char *machine; |
3a9c86df | 42 | const char *variant; |
53333801 MA |
43 | uint32_t rsdp_addr; |
44 | AcpiRsdpDescriptor rsdp_table; | |
45 | AcpiRsdtDescriptorRev1 rsdt_table; | |
46 | AcpiFadtDescriptorRev1 fadt_table; | |
15650602 | 47 | AcpiFacsDescriptorRev1 facs_table; |
53333801 MA |
48 | uint32_t *rsdt_tables_addr; |
49 | int rsdt_tables_nr; | |
a3a74ab9 | 50 | GArray *tables; |
eb386aac | 51 | uint32_t smbios_ep_addr; |
86299120 | 52 | struct smbios_21_entry_point smbios_ep_table; |
53333801 | 53 | } test_data; |
ad6423a7 | 54 | |
53333801 MA |
55 | #define ACPI_READ_FIELD(field, addr) \ |
56 | do { \ | |
57 | switch (sizeof(field)) { \ | |
58 | case 1: \ | |
59 | field = readb(addr); \ | |
60 | break; \ | |
61 | case 2: \ | |
084137dd | 62 | field = readw(addr); \ |
53333801 MA |
63 | break; \ |
64 | case 4: \ | |
084137dd | 65 | field = readl(addr); \ |
53333801 MA |
66 | break; \ |
67 | case 8: \ | |
084137dd | 68 | field = readq(addr); \ |
53333801 MA |
69 | break; \ |
70 | default: \ | |
71 | g_assert(false); \ | |
72 | } \ | |
73 | addr += sizeof(field); \ | |
74 | } while (0); | |
75 | ||
76 | #define ACPI_READ_ARRAY_PTR(arr, length, addr) \ | |
77 | do { \ | |
78 | int idx; \ | |
79 | for (idx = 0; idx < length; ++idx) { \ | |
80 | ACPI_READ_FIELD(arr[idx], addr); \ | |
81 | } \ | |
82 | } while (0); | |
83 | ||
84 | #define ACPI_READ_ARRAY(arr, addr) \ | |
85 | ACPI_READ_ARRAY_PTR(arr, sizeof(arr)/sizeof(arr[0]), addr) | |
86 | ||
87 | #define ACPI_READ_TABLE_HEADER(table, addr) \ | |
88 | do { \ | |
89 | ACPI_READ_FIELD((table)->signature, addr); \ | |
90 | ACPI_READ_FIELD((table)->length, addr); \ | |
91 | ACPI_READ_FIELD((table)->revision, addr); \ | |
92 | ACPI_READ_FIELD((table)->checksum, addr); \ | |
93 | ACPI_READ_ARRAY((table)->oem_id, addr); \ | |
94 | ACPI_READ_ARRAY((table)->oem_table_id, addr); \ | |
95 | ACPI_READ_FIELD((table)->oem_revision, addr); \ | |
96 | ACPI_READ_ARRAY((table)->asl_compiler_id, addr); \ | |
97 | ACPI_READ_FIELD((table)->asl_compiler_revision, addr); \ | |
98 | } while (0); | |
99 | ||
c225aa3c MT |
100 | #define ACPI_ASSERT_CMP(actual, expected) do { \ |
101 | uint32_t ACPI_ASSERT_CMP_le = cpu_to_le32(actual); \ | |
102 | char ACPI_ASSERT_CMP_str[5] = {}; \ | |
103 | memcpy(ACPI_ASSERT_CMP_str, &ACPI_ASSERT_CMP_le, 4); \ | |
104 | g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \ | |
105 | } while (0) | |
106 | ||
107 | #define ACPI_ASSERT_CMP64(actual, expected) do { \ | |
108 | uint64_t ACPI_ASSERT_CMP_le = cpu_to_le64(actual); \ | |
109 | char ACPI_ASSERT_CMP_str[9] = {}; \ | |
110 | memcpy(ACPI_ASSERT_CMP_str, &ACPI_ASSERT_CMP_le, 8); \ | |
111 | g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \ | |
112 | } while (0) | |
113 | ||
ad6423a7 | 114 | static const char *disk = "tests/acpi-test-disk.raw"; |
9e8458c0 | 115 | static const char *data_dir = "tests/acpi-test-data"; |
cc8fa0e8 MA |
116 | #ifdef CONFIG_IASL |
117 | static const char *iasl = stringify(CONFIG_IASL); | |
118 | #else | |
119 | static const char *iasl; | |
120 | #endif | |
ad6423a7 | 121 | |
8ac2adf7 MA |
122 | static void free_test_data(test_data *data) |
123 | { | |
9e8458c0 | 124 | AcpiSdtTable *temp; |
8ac2adf7 MA |
125 | int i; |
126 | ||
ef1e1e07 | 127 | g_free(data->rsdt_tables_addr); |
9e8458c0 | 128 | |
a3a74ab9 MA |
129 | for (i = 0; i < data->tables->len; ++i) { |
130 | temp = &g_array_index(data->tables, AcpiSdtTable, i); | |
ef1e1e07 DB |
131 | g_free(temp->aml); |
132 | if (temp->aml_file && | |
133 | !temp->tmp_files_retain && | |
134 | g_strstr_len(temp->aml_file, -1, "aml-")) { | |
135 | unlink(temp->aml_file); | |
9e8458c0 | 136 | } |
ef1e1e07 DB |
137 | g_free(temp->aml_file); |
138 | g_free(temp->asl); | |
139 | if (temp->asl_file && | |
140 | !temp->tmp_files_retain) { | |
141 | unlink(temp->asl_file); | |
9e8458c0 | 142 | } |
ef1e1e07 | 143 | g_free(temp->asl_file); |
8ac2adf7 | 144 | } |
9e8458c0 | 145 | |
a3a74ab9 | 146 | g_array_free(data->tables, false); |
8ac2adf7 MA |
147 | } |
148 | ||
53333801 MA |
149 | static uint8_t acpi_checksum(const uint8_t *data, int len) |
150 | { | |
151 | int i; | |
152 | uint8_t sum = 0; | |
153 | ||
154 | for (i = 0; i < len; i++) { | |
155 | sum += data[i]; | |
156 | } | |
157 | ||
158 | return sum; | |
159 | } | |
160 | ||
161 | static void test_acpi_rsdp_address(test_data *data) | |
162 | { | |
163 | uint32_t off; | |
164 | ||
165 | /* OK, now find RSDP */ | |
166 | for (off = 0xf0000; off < 0x100000; off += 0x10) { | |
167 | uint8_t sig[] = "RSD PTR "; | |
168 | int i; | |
169 | ||
170 | for (i = 0; i < sizeof sig - 1; ++i) { | |
171 | sig[i] = readb(off + i); | |
172 | } | |
173 | ||
174 | if (!memcmp(sig, "RSD PTR ", sizeof sig)) { | |
175 | break; | |
176 | } | |
177 | } | |
178 | ||
179 | g_assert_cmphex(off, <, 0x100000); | |
180 | data->rsdp_addr = off; | |
181 | } | |
182 | ||
183 | static void test_acpi_rsdp_table(test_data *data) | |
184 | { | |
185 | AcpiRsdpDescriptor *rsdp_table = &data->rsdp_table; | |
186 | uint32_t addr = data->rsdp_addr; | |
187 | ||
188 | ACPI_READ_FIELD(rsdp_table->signature, addr); | |
c225aa3c | 189 | ACPI_ASSERT_CMP64(rsdp_table->signature, "RSD PTR "); |
53333801 MA |
190 | |
191 | ACPI_READ_FIELD(rsdp_table->checksum, addr); | |
192 | ACPI_READ_ARRAY(rsdp_table->oem_id, addr); | |
193 | ACPI_READ_FIELD(rsdp_table->revision, addr); | |
194 | ACPI_READ_FIELD(rsdp_table->rsdt_physical_address, addr); | |
195 | ACPI_READ_FIELD(rsdp_table->length, addr); | |
196 | ||
197 | /* rsdp checksum is not for the whole table, but for the first 20 bytes */ | |
198 | g_assert(!acpi_checksum((uint8_t *)rsdp_table, 20)); | |
199 | } | |
200 | ||
201 | static void test_acpi_rsdt_table(test_data *data) | |
202 | { | |
203 | AcpiRsdtDescriptorRev1 *rsdt_table = &data->rsdt_table; | |
204 | uint32_t addr = data->rsdp_table.rsdt_physical_address; | |
205 | uint32_t *tables; | |
206 | int tables_nr; | |
207 | uint8_t checksum; | |
208 | ||
209 | /* read the header */ | |
210 | ACPI_READ_TABLE_HEADER(rsdt_table, addr); | |
c225aa3c | 211 | ACPI_ASSERT_CMP(rsdt_table->signature, "RSDT"); |
53333801 MA |
212 | |
213 | /* compute the table entries in rsdt */ | |
214 | tables_nr = (rsdt_table->length - sizeof(AcpiRsdtDescriptorRev1)) / | |
215 | sizeof(uint32_t); | |
216 | g_assert_cmpint(tables_nr, >, 0); | |
217 | ||
218 | /* get the addresses of the tables pointed by rsdt */ | |
219 | tables = g_new0(uint32_t, tables_nr); | |
220 | ACPI_READ_ARRAY_PTR(tables, tables_nr, addr); | |
221 | ||
222 | checksum = acpi_checksum((uint8_t *)rsdt_table, rsdt_table->length) + | |
223 | acpi_checksum((uint8_t *)tables, tables_nr * sizeof(uint32_t)); | |
224 | g_assert(!checksum); | |
225 | ||
226 | /* SSDT tables after FADT */ | |
227 | data->rsdt_tables_addr = tables; | |
228 | data->rsdt_tables_nr = tables_nr; | |
229 | } | |
230 | ||
231 | static void test_acpi_fadt_table(test_data *data) | |
232 | { | |
233 | AcpiFadtDescriptorRev1 *fadt_table = &data->fadt_table; | |
234 | uint32_t addr; | |
235 | ||
236 | /* FADT table comes first */ | |
237 | addr = data->rsdt_tables_addr[0]; | |
238 | ACPI_READ_TABLE_HEADER(fadt_table, addr); | |
239 | ||
240 | ACPI_READ_FIELD(fadt_table->firmware_ctrl, addr); | |
241 | ACPI_READ_FIELD(fadt_table->dsdt, addr); | |
242 | ACPI_READ_FIELD(fadt_table->model, addr); | |
243 | ACPI_READ_FIELD(fadt_table->reserved1, addr); | |
244 | ACPI_READ_FIELD(fadt_table->sci_int, addr); | |
245 | ACPI_READ_FIELD(fadt_table->smi_cmd, addr); | |
246 | ACPI_READ_FIELD(fadt_table->acpi_enable, addr); | |
247 | ACPI_READ_FIELD(fadt_table->acpi_disable, addr); | |
248 | ACPI_READ_FIELD(fadt_table->S4bios_req, addr); | |
249 | ACPI_READ_FIELD(fadt_table->reserved2, addr); | |
250 | ACPI_READ_FIELD(fadt_table->pm1a_evt_blk, addr); | |
251 | ACPI_READ_FIELD(fadt_table->pm1b_evt_blk, addr); | |
252 | ACPI_READ_FIELD(fadt_table->pm1a_cnt_blk, addr); | |
253 | ACPI_READ_FIELD(fadt_table->pm1b_cnt_blk, addr); | |
254 | ACPI_READ_FIELD(fadt_table->pm2_cnt_blk, addr); | |
255 | ACPI_READ_FIELD(fadt_table->pm_tmr_blk, addr); | |
256 | ACPI_READ_FIELD(fadt_table->gpe0_blk, addr); | |
257 | ACPI_READ_FIELD(fadt_table->gpe1_blk, addr); | |
258 | ACPI_READ_FIELD(fadt_table->pm1_evt_len, addr); | |
259 | ACPI_READ_FIELD(fadt_table->pm1_cnt_len, addr); | |
260 | ACPI_READ_FIELD(fadt_table->pm2_cnt_len, addr); | |
261 | ACPI_READ_FIELD(fadt_table->pm_tmr_len, addr); | |
262 | ACPI_READ_FIELD(fadt_table->gpe0_blk_len, addr); | |
263 | ACPI_READ_FIELD(fadt_table->gpe1_blk_len, addr); | |
264 | ACPI_READ_FIELD(fadt_table->gpe1_base, addr); | |
265 | ACPI_READ_FIELD(fadt_table->reserved3, addr); | |
266 | ACPI_READ_FIELD(fadt_table->plvl2_lat, addr); | |
267 | ACPI_READ_FIELD(fadt_table->plvl3_lat, addr); | |
268 | ACPI_READ_FIELD(fadt_table->flush_size, addr); | |
269 | ACPI_READ_FIELD(fadt_table->flush_stride, addr); | |
270 | ACPI_READ_FIELD(fadt_table->duty_offset, addr); | |
271 | ACPI_READ_FIELD(fadt_table->duty_width, addr); | |
272 | ACPI_READ_FIELD(fadt_table->day_alrm, addr); | |
273 | ACPI_READ_FIELD(fadt_table->mon_alrm, addr); | |
274 | ACPI_READ_FIELD(fadt_table->century, addr); | |
275 | ACPI_READ_FIELD(fadt_table->reserved4, addr); | |
276 | ACPI_READ_FIELD(fadt_table->reserved4a, addr); | |
277 | ACPI_READ_FIELD(fadt_table->reserved4b, addr); | |
278 | ACPI_READ_FIELD(fadt_table->flags, addr); | |
279 | ||
c225aa3c | 280 | ACPI_ASSERT_CMP(fadt_table->signature, "FACP"); |
53333801 MA |
281 | g_assert(!acpi_checksum((uint8_t *)fadt_table, fadt_table->length)); |
282 | } | |
283 | ||
15650602 MA |
284 | static void test_acpi_facs_table(test_data *data) |
285 | { | |
286 | AcpiFacsDescriptorRev1 *facs_table = &data->facs_table; | |
287 | uint32_t addr = data->fadt_table.firmware_ctrl; | |
288 | ||
289 | ACPI_READ_FIELD(facs_table->signature, addr); | |
290 | ACPI_READ_FIELD(facs_table->length, addr); | |
291 | ACPI_READ_FIELD(facs_table->hardware_signature, addr); | |
292 | ACPI_READ_FIELD(facs_table->firmware_waking_vector, addr); | |
293 | ACPI_READ_FIELD(facs_table->global_lock, addr); | |
294 | ACPI_READ_FIELD(facs_table->flags, addr); | |
295 | ACPI_READ_ARRAY(facs_table->resverved3, addr); | |
296 | ||
c225aa3c | 297 | ACPI_ASSERT_CMP(facs_table->signature, "FACS"); |
15650602 MA |
298 | } |
299 | ||
53333801 MA |
300 | static void test_dst_table(AcpiSdtTable *sdt_table, uint32_t addr) |
301 | { | |
302 | uint8_t checksum; | |
303 | ||
304 | ACPI_READ_TABLE_HEADER(&sdt_table->header, addr); | |
305 | ||
306 | sdt_table->aml_len = sdt_table->header.length - sizeof(AcpiTableHeader); | |
307 | sdt_table->aml = g_malloc0(sdt_table->aml_len); | |
308 | ACPI_READ_ARRAY_PTR(sdt_table->aml, sdt_table->aml_len, addr); | |
309 | ||
310 | checksum = acpi_checksum((uint8_t *)sdt_table, sizeof(AcpiTableHeader)) + | |
9e8458c0 | 311 | acpi_checksum((uint8_t *)sdt_table->aml, sdt_table->aml_len); |
53333801 MA |
312 | g_assert(!checksum); |
313 | } | |
314 | ||
315 | static void test_acpi_dsdt_table(test_data *data) | |
316 | { | |
9e8458c0 | 317 | AcpiSdtTable dsdt_table; |
53333801 MA |
318 | uint32_t addr = data->fadt_table.dsdt; |
319 | ||
9e8458c0 | 320 | memset(&dsdt_table, 0, sizeof(dsdt_table)); |
a3a74ab9 | 321 | data->tables = g_array_new(false, true, sizeof(AcpiSdtTable)); |
9e8458c0 MA |
322 | |
323 | test_dst_table(&dsdt_table, addr); | |
c225aa3c | 324 | ACPI_ASSERT_CMP(dsdt_table.header.signature, "DSDT"); |
9e8458c0 MA |
325 | |
326 | /* Place DSDT first */ | |
a3a74ab9 | 327 | g_array_append_val(data->tables, dsdt_table); |
53333801 MA |
328 | } |
329 | ||
a3a74ab9 | 330 | static void test_acpi_tables(test_data *data) |
53333801 | 331 | { |
a3a74ab9 | 332 | int tables_nr = data->rsdt_tables_nr - 1; /* fadt is first */ |
53333801 MA |
333 | int i; |
334 | ||
a3a74ab9 | 335 | for (i = 0; i < tables_nr; i++) { |
8ac2adf7 | 336 | AcpiSdtTable ssdt_table; |
9e8458c0 MA |
337 | |
338 | memset(&ssdt_table, 0 , sizeof(ssdt_table)); | |
53333801 | 339 | uint32_t addr = data->rsdt_tables_addr[i + 1]; /* fadt is first */ |
8ac2adf7 | 340 | test_dst_table(&ssdt_table, addr); |
a3a74ab9 | 341 | g_array_append_val(data->tables, ssdt_table); |
53333801 | 342 | } |
9e8458c0 MA |
343 | } |
344 | ||
4500bc98 | 345 | static void dump_aml_files(test_data *data, bool rebuild) |
9e8458c0 MA |
346 | { |
347 | AcpiSdtTable *sdt; | |
348 | GError *error = NULL; | |
4500bc98 | 349 | gchar *aml_file = NULL; |
9e8458c0 MA |
350 | gint fd; |
351 | ssize_t ret; | |
352 | int i; | |
353 | ||
a3a74ab9 | 354 | for (i = 0; i < data->tables->len; ++i) { |
3a9c86df | 355 | const char *ext = data->variant ? data->variant : ""; |
a3a74ab9 | 356 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
9e8458c0 MA |
357 | g_assert(sdt->aml); |
358 | ||
4500bc98 | 359 | if (rebuild) { |
c225aa3c | 360 | uint32_t signature = cpu_to_le32(sdt->header.signature); |
3a9c86df IM |
361 | aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine, |
362 | (gchar *)&signature, ext); | |
4500bc98 MA |
363 | fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT, |
364 | S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH); | |
365 | } else { | |
366 | fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error); | |
367 | g_assert_no_error(error); | |
368 | } | |
369 | g_assert(fd >= 0); | |
9e8458c0 MA |
370 | |
371 | ret = qemu_write_full(fd, sdt, sizeof(AcpiTableHeader)); | |
372 | g_assert(ret == sizeof(AcpiTableHeader)); | |
373 | ret = qemu_write_full(fd, sdt->aml, sdt->aml_len); | |
374 | g_assert(ret == sdt->aml_len); | |
375 | ||
376 | close(fd); | |
4500bc98 | 377 | |
ef1e1e07 | 378 | g_free(aml_file); |
9e8458c0 MA |
379 | } |
380 | } | |
381 | ||
c225aa3c | 382 | static bool compare_signature(AcpiSdtTable *sdt, const char *signature) |
69d09245 | 383 | { |
c225aa3c | 384 | return !memcmp(&sdt->header.signature, signature, 4); |
69d09245 MA |
385 | } |
386 | ||
15d914b1 | 387 | static bool load_asl(GArray *sdts, AcpiSdtTable *sdt) |
9e8458c0 MA |
388 | { |
389 | AcpiSdtTable *temp; | |
390 | GError *error = NULL; | |
cc8fa0e8 | 391 | GString *command_line = g_string_new(iasl); |
9e8458c0 MA |
392 | gint fd; |
393 | gchar *out, *out_err; | |
394 | gboolean ret; | |
395 | int i; | |
396 | ||
397 | fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error); | |
398 | g_assert_no_error(error); | |
399 | close(fd); | |
400 | ||
401 | /* build command line */ | |
cc8fa0e8 | 402 | g_string_append_printf(command_line, " -p %s ", sdt->asl_file); |
c225aa3c MT |
403 | if (compare_signature(sdt, "DSDT") || |
404 | compare_signature(sdt, "SSDT")) { | |
69d09245 MA |
405 | for (i = 0; i < sdts->len; ++i) { |
406 | temp = &g_array_index(sdts, AcpiSdtTable, i); | |
c225aa3c MT |
407 | if (compare_signature(temp, "DSDT") || |
408 | compare_signature(temp, "SSDT")) { | |
69d09245 MA |
409 | g_string_append_printf(command_line, "-e %s ", temp->aml_file); |
410 | } | |
411 | } | |
9e8458c0 MA |
412 | } |
413 | g_string_append_printf(command_line, "-d %s", sdt->aml_file); | |
414 | ||
415 | /* pass 'out' and 'out_err' in order to be redirected */ | |
15d914b1 | 416 | ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error); |
9e8458c0 | 417 | g_assert_no_error(error); |
15d914b1 MA |
418 | if (ret) { |
419 | ret = g_file_get_contents(sdt->asl_file, (gchar **)&sdt->asl, | |
420 | &sdt->asl_len, &error); | |
421 | g_assert(ret); | |
422 | g_assert_no_error(error); | |
dac23a6c | 423 | ret = (sdt->asl_len > 0); |
15d914b1 | 424 | } |
9e8458c0 MA |
425 | |
426 | g_free(out); | |
427 | g_free(out_err); | |
428 | g_string_free(command_line, true); | |
15d914b1 MA |
429 | |
430 | return !ret; | |
9e8458c0 MA |
431 | } |
432 | ||
433 | #define COMMENT_END "*/" | |
434 | #define DEF_BLOCK "DefinitionBlock (" | |
435 | #define BLOCK_NAME_END ".aml" | |
436 | ||
437 | static GString *normalize_asl(gchar *asl_code) | |
438 | { | |
439 | GString *asl = g_string_new(asl_code); | |
440 | gchar *comment, *block_name; | |
441 | ||
442 | /* strip comments (different generation days) */ | |
443 | comment = g_strstr_len(asl->str, asl->len, COMMENT_END); | |
444 | if (comment) { | |
cb348985 PB |
445 | comment += strlen(COMMENT_END); |
446 | while (*comment == '\n') { | |
447 | comment++; | |
448 | } | |
449 | asl = g_string_erase(asl, 0, comment - asl->str); | |
9e8458c0 MA |
450 | } |
451 | ||
452 | /* strip def block name (it has file path in it) */ | |
453 | if (g_str_has_prefix(asl->str, DEF_BLOCK)) { | |
454 | block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END); | |
455 | g_assert(block_name); | |
456 | asl = g_string_erase(asl, 0, | |
457 | block_name + sizeof(BLOCK_NAME_END) - asl->str); | |
458 | } | |
459 | ||
460 | return asl; | |
461 | } | |
462 | ||
463 | static GArray *load_expected_aml(test_data *data) | |
464 | { | |
465 | int i; | |
466 | AcpiSdtTable *sdt; | |
3a9c86df | 467 | gchar *aml_file = NULL; |
9e8458c0 MA |
468 | GError *error = NULL; |
469 | gboolean ret; | |
470 | ||
a3a74ab9 MA |
471 | GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable)); |
472 | for (i = 0; i < data->tables->len; ++i) { | |
9e8458c0 | 473 | AcpiSdtTable exp_sdt; |
c225aa3c | 474 | uint32_t signature; |
3a9c86df | 475 | const char *ext = data->variant ? data->variant : ""; |
c225aa3c | 476 | |
a3a74ab9 | 477 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
9e8458c0 MA |
478 | |
479 | memset(&exp_sdt, 0, sizeof(exp_sdt)); | |
480 | exp_sdt.header.signature = sdt->header.signature; | |
481 | ||
c225aa3c | 482 | signature = cpu_to_le32(sdt->header.signature); |
3a9c86df IM |
483 | |
484 | try_again: | |
485 | aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine, | |
486 | (gchar *)&signature, ext); | |
487 | if (data->variant && !g_file_test(aml_file, G_FILE_TEST_EXISTS)) { | |
488 | g_free(aml_file); | |
489 | ext = ""; | |
490 | goto try_again; | |
491 | } | |
9e8458c0 MA |
492 | exp_sdt.aml_file = aml_file; |
493 | g_assert(g_file_test(aml_file, G_FILE_TEST_EXISTS)); | |
494 | ret = g_file_get_contents(aml_file, &exp_sdt.aml, | |
495 | &exp_sdt.aml_len, &error); | |
496 | g_assert(ret); | |
497 | g_assert_no_error(error); | |
498 | g_assert(exp_sdt.aml); | |
499 | g_assert(exp_sdt.aml_len); | |
500 | ||
a3a74ab9 | 501 | g_array_append_val(exp_tables, exp_sdt); |
9e8458c0 MA |
502 | } |
503 | ||
a3a74ab9 | 504 | return exp_tables; |
9e8458c0 MA |
505 | } |
506 | ||
507 | static void test_acpi_asl(test_data *data) | |
508 | { | |
509 | int i; | |
510 | AcpiSdtTable *sdt, *exp_sdt; | |
511 | test_data exp_data; | |
15d914b1 | 512 | gboolean exp_err, err; |
9e8458c0 MA |
513 | |
514 | memset(&exp_data, 0, sizeof(exp_data)); | |
a3a74ab9 | 515 | exp_data.tables = load_expected_aml(data); |
4500bc98 | 516 | dump_aml_files(data, false); |
a3a74ab9 | 517 | for (i = 0; i < data->tables->len; ++i) { |
9e8458c0 MA |
518 | GString *asl, *exp_asl; |
519 | ||
a3a74ab9 MA |
520 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
521 | exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i); | |
9e8458c0 | 522 | |
15d914b1 | 523 | err = load_asl(data->tables, sdt); |
9e8458c0 MA |
524 | asl = normalize_asl(sdt->asl); |
525 | ||
15d914b1 | 526 | exp_err = load_asl(exp_data.tables, exp_sdt); |
9e8458c0 MA |
527 | exp_asl = normalize_asl(exp_sdt->asl); |
528 | ||
15d914b1 MA |
529 | /* TODO: check for warnings */ |
530 | g_assert(!err || exp_err); | |
531 | ||
0651596c | 532 | if (g_strcmp0(asl->str, exp_asl->str)) { |
dac23a6c MA |
533 | if (exp_err) { |
534 | fprintf(stderr, | |
535 | "Warning! iasl couldn't parse the expected aml\n"); | |
536 | } else { | |
537 | uint32_t signature = cpu_to_le32(exp_sdt->header.signature); | |
538 | sdt->tmp_files_retain = true; | |
539 | exp_sdt->tmp_files_retain = true; | |
540 | fprintf(stderr, | |
541 | "acpi-test: Warning! %.4s mismatch. " | |
542 | "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n", | |
543 | (gchar *)&signature, | |
544 | sdt->asl_file, sdt->aml_file, | |
545 | exp_sdt->asl_file, exp_sdt->aml_file); | |
7cb08cb2 IM |
546 | if (getenv("V")) { |
547 | const char *diff_cmd = getenv("DIFF"); | |
548 | if (diff_cmd) { | |
549 | int ret G_GNUC_UNUSED; | |
550 | char *diff = g_strdup_printf("%s %s %s", diff_cmd, | |
551 | exp_sdt->asl_file, sdt->asl_file); | |
552 | ret = system(diff) ; | |
553 | g_free(diff); | |
554 | } else { | |
555 | fprintf(stderr, "acpi-test: Warning. not showing " | |
556 | "difference since no diff utility is specified. " | |
557 | "Set 'DIFF' environment variable to a preferred " | |
558 | "diff utility and run 'make V=1 check' again to " | |
559 | "see ASL difference."); | |
560 | } | |
561 | } | |
dac23a6c | 562 | } |
0651596c | 563 | } |
9e8458c0 MA |
564 | g_string_free(asl, true); |
565 | g_string_free(exp_asl, true); | |
566 | } | |
567 | ||
568 | free_test_data(&exp_data); | |
53333801 MA |
569 | } |
570 | ||
5efed5a1 | 571 | static bool smbios_ep_table_ok(test_data *data) |
eb386aac | 572 | { |
86299120 | 573 | struct smbios_21_entry_point *ep_table = &data->smbios_ep_table; |
eb386aac GS |
574 | uint32_t addr = data->smbios_ep_addr; |
575 | ||
576 | ACPI_READ_ARRAY(ep_table->anchor_string, addr); | |
5efed5a1 GS |
577 | if (memcmp(ep_table->anchor_string, "_SM_", 4)) { |
578 | return false; | |
579 | } | |
eb386aac GS |
580 | ACPI_READ_FIELD(ep_table->checksum, addr); |
581 | ACPI_READ_FIELD(ep_table->length, addr); | |
582 | ACPI_READ_FIELD(ep_table->smbios_major_version, addr); | |
583 | ACPI_READ_FIELD(ep_table->smbios_minor_version, addr); | |
584 | ACPI_READ_FIELD(ep_table->max_structure_size, addr); | |
585 | ACPI_READ_FIELD(ep_table->entry_point_revision, addr); | |
586 | ACPI_READ_ARRAY(ep_table->formatted_area, addr); | |
587 | ACPI_READ_ARRAY(ep_table->intermediate_anchor_string, addr); | |
5efed5a1 GS |
588 | if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) { |
589 | return false; | |
590 | } | |
eb386aac GS |
591 | ACPI_READ_FIELD(ep_table->intermediate_checksum, addr); |
592 | ACPI_READ_FIELD(ep_table->structure_table_length, addr); | |
5efed5a1 GS |
593 | if (ep_table->structure_table_length == 0) { |
594 | return false; | |
595 | } | |
eb386aac GS |
596 | ACPI_READ_FIELD(ep_table->structure_table_address, addr); |
597 | ACPI_READ_FIELD(ep_table->number_of_structures, addr); | |
5efed5a1 GS |
598 | if (ep_table->number_of_structures == 0) { |
599 | return false; | |
600 | } | |
eb386aac | 601 | ACPI_READ_FIELD(ep_table->smbios_bcd_revision, addr); |
5efed5a1 GS |
602 | if (acpi_checksum((uint8_t *)ep_table, sizeof *ep_table) || |
603 | acpi_checksum((uint8_t *)ep_table + 0x10, sizeof *ep_table - 0x10)) { | |
604 | return false; | |
605 | } | |
606 | return true; | |
607 | } | |
608 | ||
609 | static void test_smbios_entry_point(test_data *data) | |
610 | { | |
611 | uint32_t off; | |
612 | ||
613 | /* find smbios entry point structure */ | |
614 | for (off = 0xf0000; off < 0x100000; off += 0x10) { | |
615 | uint8_t sig[] = "_SM_"; | |
616 | int i; | |
617 | ||
618 | for (i = 0; i < sizeof sig - 1; ++i) { | |
619 | sig[i] = readb(off + i); | |
620 | } | |
621 | ||
622 | if (!memcmp(sig, "_SM_", sizeof sig)) { | |
623 | /* signature match, but is this a valid entry point? */ | |
624 | data->smbios_ep_addr = off; | |
625 | if (smbios_ep_table_ok(data)) { | |
626 | break; | |
627 | } | |
628 | } | |
629 | } | |
630 | ||
631 | g_assert_cmphex(off, <, 0x100000); | |
eb386aac GS |
632 | } |
633 | ||
634 | static inline bool smbios_single_instance(uint8_t type) | |
635 | { | |
636 | switch (type) { | |
637 | case 0: | |
638 | case 1: | |
639 | case 2: | |
640 | case 3: | |
641 | case 16: | |
642 | case 32: | |
643 | case 127: | |
644 | return true; | |
645 | default: | |
646 | return false; | |
647 | } | |
648 | } | |
649 | ||
650 | static void test_smbios_structs(test_data *data) | |
651 | { | |
652 | DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 }; | |
86299120 | 653 | struct smbios_21_entry_point *ep_table = &data->smbios_ep_table; |
eb386aac GS |
654 | uint32_t addr = ep_table->structure_table_address; |
655 | int i, len, max_len = 0; | |
656 | uint8_t type, prv, crt; | |
657 | uint8_t required_struct_types[] = {0, 1, 3, 4, 16, 17, 19, 32, 127}; | |
658 | ||
659 | /* walk the smbios tables */ | |
660 | for (i = 0; i < ep_table->number_of_structures; i++) { | |
661 | ||
662 | /* grab type and formatted area length from struct header */ | |
663 | type = readb(addr); | |
664 | g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE); | |
665 | len = readb(addr + 1); | |
666 | ||
667 | /* single-instance structs must not have been encountered before */ | |
668 | if (smbios_single_instance(type)) { | |
669 | g_assert(!test_bit(type, struct_bitmap)); | |
670 | } | |
671 | set_bit(type, struct_bitmap); | |
672 | ||
673 | /* seek to end of unformatted string area of this struct ("\0\0") */ | |
674 | prv = crt = 1; | |
675 | while (prv || crt) { | |
676 | prv = crt; | |
677 | crt = readb(addr + len); | |
678 | len++; | |
679 | } | |
680 | ||
681 | /* keep track of max. struct size */ | |
682 | if (max_len < len) { | |
683 | max_len = len; | |
684 | g_assert_cmpuint(max_len, <=, ep_table->max_structure_size); | |
685 | } | |
686 | ||
687 | /* start of next structure */ | |
688 | addr += len; | |
689 | } | |
690 | ||
691 | /* total table length and max struct size must match entry point values */ | |
692 | g_assert_cmpuint(ep_table->structure_table_length, ==, | |
693 | addr - ep_table->structure_table_address); | |
694 | g_assert_cmpuint(ep_table->max_structure_size, ==, max_len); | |
695 | ||
696 | /* required struct types must all be present */ | |
697 | for (i = 0; i < ARRAY_SIZE(required_struct_types); i++) { | |
698 | g_assert(test_bit(required_struct_types[i], struct_bitmap)); | |
699 | } | |
700 | } | |
701 | ||
8ac2adf7 | 702 | static void test_acpi_one(const char *params, test_data *data) |
ad6423a7 MT |
703 | { |
704 | char *args; | |
ad6423a7 | 705 | |
6b9e03a4 | 706 | args = g_strdup_printf("-net none -display none %s " |
b8e665e4 | 707 | "-drive id=hd0,if=none,file=%s,format=raw " |
6b9e03a4 JS |
708 | "-device ide-hd,drive=hd0 ", |
709 | params ? params : "", disk); | |
9e8458c0 | 710 | |
ad6423a7 MT |
711 | qtest_start(args); |
712 | ||
4e082566 | 713 | boot_sector_test(); |
ad6423a7 | 714 | |
8ac2adf7 MA |
715 | test_acpi_rsdp_address(data); |
716 | test_acpi_rsdp_table(data); | |
717 | test_acpi_rsdt_table(data); | |
718 | test_acpi_fadt_table(data); | |
15650602 | 719 | test_acpi_facs_table(data); |
8ac2adf7 | 720 | test_acpi_dsdt_table(data); |
a3a74ab9 | 721 | test_acpi_tables(data); |
ad6423a7 | 722 | |
cc8fa0e8 | 723 | if (iasl) { |
4500bc98 MA |
724 | if (getenv(ACPI_REBUILD_EXPECTED_AML)) { |
725 | dump_aml_files(data, true); | |
726 | } else { | |
727 | test_acpi_asl(data); | |
728 | } | |
9e8458c0 MA |
729 | } |
730 | ||
5efed5a1 | 731 | test_smbios_entry_point(data); |
eb386aac GS |
732 | test_smbios_structs(data); |
733 | ||
ad6423a7 MT |
734 | qtest_quit(global_qtest); |
735 | g_free(args); | |
736 | } | |
737 | ||
71f4be25 | 738 | static void test_acpi_piix4_tcg(void) |
ad6423a7 | 739 | { |
8ac2adf7 MA |
740 | test_data data; |
741 | ||
ad6423a7 MT |
742 | /* Supplying -machine accel argument overrides the default (qtest). |
743 | * This is to make guest actually run. | |
744 | */ | |
9e8458c0 MA |
745 | memset(&data, 0, sizeof(data)); |
746 | data.machine = MACHINE_PC; | |
8ac2adf7 | 747 | test_acpi_one("-machine accel=tcg", &data); |
9e8458c0 | 748 | free_test_data(&data); |
71f4be25 PB |
749 | } |
750 | ||
3a9c86df IM |
751 | static void test_acpi_piix4_tcg_bridge(void) |
752 | { | |
753 | test_data data; | |
754 | ||
755 | memset(&data, 0, sizeof(data)); | |
756 | data.machine = MACHINE_PC; | |
757 | data.variant = ".bridge"; | |
758 | test_acpi_one("-machine accel=tcg -device pci-bridge,chassis_nr=1", &data); | |
759 | free_test_data(&data); | |
760 | } | |
761 | ||
71f4be25 PB |
762 | static void test_acpi_q35_tcg(void) |
763 | { | |
764 | test_data data; | |
8ac2adf7 | 765 | |
9e8458c0 MA |
766 | memset(&data, 0, sizeof(data)); |
767 | data.machine = MACHINE_Q35; | |
768 | test_acpi_one("-machine q35,accel=tcg", &data); | |
8ac2adf7 | 769 | free_test_data(&data); |
ad6423a7 MT |
770 | } |
771 | ||
3a9c86df IM |
772 | static void test_acpi_q35_tcg_bridge(void) |
773 | { | |
774 | test_data data; | |
775 | ||
776 | memset(&data, 0, sizeof(data)); | |
777 | data.machine = MACHINE_Q35; | |
778 | data.variant = ".bridge"; | |
779 | test_acpi_one("-machine q35,accel=tcg -device pci-bridge,chassis_nr=1", | |
780 | &data); | |
781 | free_test_data(&data); | |
782 | } | |
783 | ||
ad6423a7 MT |
784 | int main(int argc, char *argv[]) |
785 | { | |
786 | const char *arch = qtest_get_arch(); | |
5862ad0f | 787 | int ret; |
c39a28a4 | 788 | |
4e082566 VK |
789 | ret = boot_sector_init(disk); |
790 | if(ret) | |
791 | return ret; | |
ad6423a7 MT |
792 | |
793 | g_test_init(&argc, &argv, NULL); | |
794 | ||
795 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { | |
71f4be25 | 796 | qtest_add_func("acpi/piix4/tcg", test_acpi_piix4_tcg); |
3a9c86df | 797 | qtest_add_func("acpi/piix4/tcg/bridge", test_acpi_piix4_tcg_bridge); |
71f4be25 | 798 | qtest_add_func("acpi/q35/tcg", test_acpi_q35_tcg); |
3a9c86df | 799 | qtest_add_func("acpi/q35/tcg/bridge", test_acpi_q35_tcg_bridge); |
ad6423a7 | 800 | } |
5862ad0f | 801 | ret = g_test_run(); |
4e082566 | 802 | boot_sector_cleanup(disk); |
5862ad0f | 803 | return ret; |
ad6423a7 | 804 | } |