The point of this API is to fetch all FW descriptors, parse them and return list of supported interfaces and SMM feature for given combination of machine type and guest architecture. Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- src/qemu/qemu_firmware.c | 71 ++++++++++++++++++++++++++++++++++++++- src/qemu/qemu_firmware.h | 10 ++++++ tests/qemufirmwaretest.c | 72 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) diff --git a/src/qemu/qemu_firmware.c b/src/qemu/qemu_firmware.c index 8e9a225982..84c14321f6 100644 --- a/src/qemu/qemu_firmware.c +++ b/src/qemu/qemu_firmware.c @@ -1349,7 +1349,8 @@ qemuFirmwareFetchParsedConfigs(bool privileged, } VIR_STEAL_PTR(*firmwaresRet, firmwares); - VIR_STEAL_PTR(*pathsRet, paths); + if (pathsRet) + VIR_STEAL_PTR(*pathsRet, paths); return npaths; error: @@ -1415,3 +1416,71 @@ qemuFirmwareFillDomain(virQEMUDriverPtr driver, VIR_FREE(firmwares); return ret; } + + +int +qemuFirmwareGetSupported(const char *machine, + virArch arch, + bool privileged, + uint64_t *supported, + bool *secure) +{ + qemuFirmwarePtr *firmwares = NULL; + ssize_t nfirmwares = 0; + size_t i; + + *supported = VIR_DOMAIN_OS_DEF_FIRMWARE_NONE; + *secure = false; + + if ((nfirmwares = qemuFirmwareFetchParsedConfigs(privileged, + &firmwares, NULL)) < 0) + return -1; + + for (i = 0; i < nfirmwares; i++) { + qemuFirmwarePtr fw = firmwares[i]; + size_t j; + + if (!qemuFirmwareMatchesMachineArch(fw, machine, arch)) + continue; + + for (j = 0; j < fw->ninterfaces; j++) { + switch (fw->interfaces[j]) { + case QEMU_FIRMWARE_OS_INTERFACE_UEFI: + *supported |= 1ULL << VIR_DOMAIN_OS_DEF_FIRMWARE_EFI; + break; + case QEMU_FIRMWARE_OS_INTERFACE_BIOS: + *supported |= 1ULL << VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS; + break; + case QEMU_FIRMWARE_OS_INTERFACE_NONE: + case QEMU_FIRMWARE_OS_INTERFACE_OPENFIRMWARE: + case QEMU_FIRMWARE_OS_INTERFACE_UBOOT: + case QEMU_FIRMWARE_OS_INTERFACE_LAST: + default: + break; + } + } + + for (j = 0; j < fw->nfeatures; j++) { + switch (fw->features[j]) { + case QEMU_FIRMWARE_FEATURE_REQUIRES_SMM: + *secure = true; + break; + case QEMU_FIRMWARE_FEATURE_NONE: + case QEMU_FIRMWARE_FEATURE_ACPI_S3: + case QEMU_FIRMWARE_FEATURE_ACPI_S4: + case QEMU_FIRMWARE_FEATURE_AMD_SEV: + case QEMU_FIRMWARE_FEATURE_ENROLLED_KEYS: + case QEMU_FIRMWARE_FEATURE_SECURE_BOOT: + case QEMU_FIRMWARE_FEATURE_VERBOSE_DYNAMIC: + case QEMU_FIRMWARE_FEATURE_VERBOSE_STATIC: + case QEMU_FIRMWARE_FEATURE_LAST: + break; + } + } + } + + for (i = 0; i < nfirmwares; i++) + qemuFirmwareFree(firmwares[i]); + VIR_FREE(firmwares); + return 0; +} diff --git a/src/qemu/qemu_firmware.h b/src/qemu/qemu_firmware.h index 7f8a0e4a15..5b3822b329 100644 --- a/src/qemu/qemu_firmware.h +++ b/src/qemu/qemu_firmware.h @@ -24,6 +24,7 @@ # include "domain_conf.h" # include "viralloc.h" # include "qemu_conf.h" +# include "virarch.h" typedef struct _qemuFirmware qemuFirmware; typedef qemuFirmware *qemuFirmwarePtr; @@ -48,4 +49,13 @@ qemuFirmwareFillDomain(virQEMUDriverPtr driver, virDomainObjPtr vm, unsigned int flags); +int +qemuFirmwareGetSupported(const char *machine, + virArch arch, + bool privileged, + uint64_t *supported, + bool *secure); + +verify(VIR_DOMAIN_OS_DEF_FIRMWARE_LAST <= 64); + #endif /* LIBVIRT_QEMU_FIRMWARE_H */ diff --git a/tests/qemufirmwaretest.c b/tests/qemufirmwaretest.c index 2b5cbf649b..5fbd19f7c5 100644 --- a/tests/qemufirmwaretest.c +++ b/tests/qemufirmwaretest.c @@ -1,5 +1,7 @@ #include <config.h> +#include <inttypes.h> + #include "testutils.h" #include "virfilewrapper.h" #include "qemu/qemu_firmware.h" @@ -99,6 +101,53 @@ testFWPrecedence(const void *opaque ATTRIBUTE_UNUSED) } +struct supportedData { + const char *machine; + virArch arch; + bool secure; + 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; + size_t i; + + for (i = 0; i < data->ninterfaces; i++) + expectedInterfaces |= 1 << data->interfaces[i]; + + if (qemuFirmwareGetSupported(data->machine, data->arch, false, + &actualInterfaces, &actualSecure) < 0) { + fprintf(stderr, "Unable to get list of supported interfaces\n"); + return -1; + } + + if (actualInterfaces != expectedInterfaces) { + fprintf(stderr, + "Mismatch in supported interfaces. " + "Expected 0x%" PRIx64 " got 0x%" PRIx64 "\n", + expectedInterfaces, actualInterfaces); + return -1; + } + + if (actualSecure != data->secure) { + fprintf(stderr, + "Mismatch in supported secure boot. " + "Expected %d got %d\n", + data->secure, actualSecure); + return -1; + } + + return 0; +} + + static int mymain(void) { @@ -127,6 +176,29 @@ mymain(void) if (virTestRun("QEMU FW precedence test", testFWPrecedence, NULL) < 0) ret = -1; +#define DO_SUPPORTED_TEST(machine, arch, secure, ...) \ + do { \ + unsigned int interfaces[] = {__VA_ARGS__}; \ + struct supportedData data = {machine, arch, secure, \ + interfaces, ARRAY_CARDINALITY(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, + VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS, + VIR_DOMAIN_OS_DEF_FIRMWARE_EFI); + DO_SUPPORTED_TEST("pc-i440fx-3.1", VIR_ARCH_I686, false, + VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS); + DO_SUPPORTED_TEST("pc-q35-3.1", VIR_ARCH_X86_64, true, + VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS, + VIR_DOMAIN_OS_DEF_FIRMWARE_EFI); + DO_SUPPORTED_TEST("pc-q35-3.1", VIR_ARCH_I686, false, + VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS); + DO_SUPPORTED_TEST("virt-3.1", VIR_ARCH_AARCH64, false, + VIR_DOMAIN_OS_DEF_FIRMWARE_EFI); + virFileWrapperClearPrefixes(); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; -- 2.21.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list