usb: Add basic code to emulate Chipidea USB IP
[qemu/ar7.git] / tests / vmgenid-test.c
blob68ff954578d3e6fea7b936e574cac2a2917e3c4c
1 /*
2 * QTest testcase for VM Generation ID
4 * Copyright (c) 2016 Red Hat, Inc.
5 * Copyright (c) 2017 Skyport Systems
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include "qemu/bitmap.h"
13 #include "qemu/uuid.h"
14 #include "hw/acpi/acpi-defs.h"
15 #include "boot-sector.h"
16 #include "acpi-utils.h"
17 #include "libqtest.h"
19 #define VGID_GUID "324e6eaf-d1d1-4bf6-bf41-b9bb6c91fb87"
20 #define VMGENID_GUID_OFFSET 40 /* allow space for
21 * OVMF SDT Header Probe Supressor
23 #define RSDP_ADDR_INVALID 0x100000 /* RSDP must be below this address */
25 typedef struct {
26 AcpiTableHeader header;
27 gchar name_op;
28 gchar vgia[4];
29 gchar val_op;
30 uint32_t vgia_val;
31 } QEMU_PACKED VgidTable;
33 static uint32_t acpi_find_vgia(void)
35 uint32_t rsdp_offset;
36 uint32_t guid_offset = 0;
37 AcpiRsdpDescriptor rsdp_table;
38 uint32_t rsdt, rsdt_table_length;
39 AcpiRsdtDescriptorRev1 rsdt_table;
40 size_t tables_nr;
41 uint32_t *tables;
42 AcpiTableHeader ssdt_table;
43 VgidTable vgid_table;
44 int i;
46 /* Wait for guest firmware to finish and start the payload. */
47 boot_sector_test();
49 /* Tables should be initialized now. */
50 rsdp_offset = acpi_find_rsdp_address();
52 g_assert_cmphex(rsdp_offset, <, RSDP_ADDR_INVALID);
54 acpi_parse_rsdp_table(rsdp_offset, &rsdp_table);
56 rsdt = le32_to_cpu(rsdp_table.rsdt_physical_address);
57 /* read the header */
58 ACPI_READ_TABLE_HEADER(&rsdt_table, rsdt);
59 ACPI_ASSERT_CMP(rsdt_table.signature, "RSDT");
60 rsdt_table_length = le32_to_cpu(rsdt_table.length);
62 /* compute the table entries in rsdt */
63 g_assert_cmpint(rsdt_table_length, >, sizeof(AcpiRsdtDescriptorRev1));
64 tables_nr = (rsdt_table_length - sizeof(AcpiRsdtDescriptorRev1)) /
65 sizeof(uint32_t);
67 /* get the addresses of the tables pointed by rsdt */
68 tables = g_new0(uint32_t, tables_nr);
69 ACPI_READ_ARRAY_PTR(tables, tables_nr, rsdt);
71 for (i = 0; i < tables_nr; i++) {
72 uint32_t addr = le32_to_cpu(tables[i]);
73 ACPI_READ_TABLE_HEADER(&ssdt_table, addr);
74 if (!strncmp((char *)ssdt_table.oem_table_id, "VMGENID", 7)) {
75 /* the first entry in the table should be VGIA
76 * That's all we need
78 ACPI_READ_FIELD(vgid_table.name_op, addr);
79 g_assert(vgid_table.name_op == 0x08); /* name */
80 ACPI_READ_ARRAY(vgid_table.vgia, addr);
81 g_assert(memcmp(vgid_table.vgia, "VGIA", 4) == 0);
82 ACPI_READ_FIELD(vgid_table.val_op, addr);
83 g_assert(vgid_table.val_op == 0x0C); /* dword */
84 ACPI_READ_FIELD(vgid_table.vgia_val, addr);
85 /* The GUID is written at a fixed offset into the fw_cfg file
86 * in order to implement the "OVMF SDT Header probe suppressor"
87 * see docs/specs/vmgenid.txt for more details
89 guid_offset = le32_to_cpu(vgid_table.vgia_val) + VMGENID_GUID_OFFSET;
90 break;
93 g_free(tables);
94 return guid_offset;
97 static void read_guid_from_memory(QemuUUID *guid)
99 uint32_t vmgenid_addr;
100 int i;
102 vmgenid_addr = acpi_find_vgia();
103 g_assert(vmgenid_addr);
105 /* Read the GUID directly from guest memory */
106 for (i = 0; i < 16; i++) {
107 guid->data[i] = readb(vmgenid_addr + i);
109 /* The GUID is in little-endian format in the guest, while QEMU
110 * uses big-endian. Swap after reading.
112 qemu_uuid_bswap(guid);
115 static void read_guid_from_monitor(QemuUUID *guid)
117 QDict *rsp, *rsp_ret;
118 const char *guid_str;
120 rsp = qmp("{ 'execute': 'query-vm-generation-id' }");
121 if (qdict_haskey(rsp, "return")) {
122 rsp_ret = qdict_get_qdict(rsp, "return");
123 g_assert(qdict_haskey(rsp_ret, "guid"));
124 guid_str = qdict_get_str(rsp_ret, "guid");
125 g_assert(qemu_uuid_parse(guid_str, guid) == 0);
127 QDECREF(rsp);
130 static char disk[] = "tests/vmgenid-test-disk-XXXXXX";
132 #define GUID_CMD(guid) \
133 "-machine accel=kvm:tcg " \
134 "-device vmgenid,id=testvgid,guid=%s " \
135 "-drive id=hd0,if=none,file=%s,format=raw " \
136 "-device ide-hd,drive=hd0 ", guid, disk
138 static void vmgenid_set_guid_test(void)
140 QemuUUID expected, measured;
142 g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
144 global_qtest = qtest_startf(GUID_CMD(VGID_GUID));
146 /* Read the GUID from accessing guest memory */
147 read_guid_from_memory(&measured);
148 g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0);
150 qtest_quit(global_qtest);
153 static void vmgenid_set_guid_auto_test(void)
155 QemuUUID measured;
157 global_qtest = qtest_startf(GUID_CMD("auto"));
159 read_guid_from_memory(&measured);
161 /* Just check that the GUID is non-null */
162 g_assert(!qemu_uuid_is_null(&measured));
164 qtest_quit(global_qtest);
167 static void vmgenid_query_monitor_test(void)
169 QemuUUID expected, measured;
171 g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
173 global_qtest = qtest_startf(GUID_CMD(VGID_GUID));
175 /* Read the GUID via the monitor */
176 read_guid_from_monitor(&measured);
177 g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0);
179 qtest_quit(global_qtest);
182 int main(int argc, char **argv)
184 int ret;
186 ret = boot_sector_init(disk);
187 if (ret) {
188 return ret;
191 g_test_init(&argc, &argv, NULL);
193 qtest_add_func("/vmgenid/vmgenid/set-guid",
194 vmgenid_set_guid_test);
195 qtest_add_func("/vmgenid/vmgenid/set-guid-auto",
196 vmgenid_set_guid_auto_test);
197 qtest_add_func("/vmgenid/vmgenid/query-monitor",
198 vmgenid_query_monitor_test);
199 ret = g_test_run();
200 boot_sector_cleanup(disk);
202 return ret;