^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2014 Oracle Co., Daniel Kiper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <xen/xen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <xen/xen-ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <xen/interface/platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/xen/hypercall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static efi_char16_t vendor[100] __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static efi_system_table_t efi_systab_xen __initdata = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) .hdr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) .signature = EFI_SYSTEM_TABLE_SIGNATURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) .revision = 0, /* Initialized later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) .headersize = 0, /* Ignored by Linux Kernel. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) .crc32 = 0, /* Ignored by Linux Kernel. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) .reserved = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) .fw_vendor = EFI_INVALID_TABLE_ADDR, /* Initialized later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) .fw_revision = 0, /* Initialized later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) .con_in_handle = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) .con_in = NULL, /* Not used under Xen. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) .con_out_handle = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) .con_out = NULL, /* Not used under Xen. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) .stderr_handle = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) .stderr = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .runtime = (efi_runtime_services_t *)EFI_INVALID_TABLE_ADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* Not used under Xen. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .boottime = (efi_boot_services_t *)EFI_INVALID_TABLE_ADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* Not used under Xen. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .nr_tables = 0, /* Initialized later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) .tables = EFI_INVALID_TABLE_ADDR /* Initialized later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static efi_system_table_t __init *xen_efi_probe(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct xen_platform_op op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .cmd = XENPF_firmware_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .u.firmware_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .type = XEN_FW_EFI_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .index = XEN_FW_EFI_CONFIG_TABLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (!xen_initial_domain() || HYPERVISOR_platform_op(&op) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Here we know that Xen runs on EFI platform. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) xen_efi_runtime_setup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) efi_systab_xen.tables = info->cfg.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) efi_systab_xen.nr_tables = info->cfg.nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) op.cmd = XENPF_firmware_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) op.u.firmware_info.type = XEN_FW_EFI_INFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) op.u.firmware_info.index = XEN_FW_EFI_VENDOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) info->vendor.bufsz = sizeof(vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) set_xen_guest_handle(info->vendor.name, vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (HYPERVISOR_platform_op(&op) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) efi_systab_xen.fw_vendor = __pa_symbol(vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) efi_systab_xen.fw_revision = info->vendor.revision;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) efi_systab_xen.fw_vendor = __pa_symbol(L"UNKNOWN");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) op.cmd = XENPF_firmware_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) op.u.firmware_info.type = XEN_FW_EFI_INFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) op.u.firmware_info.index = XEN_FW_EFI_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (HYPERVISOR_platform_op(&op) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) efi_systab_xen.hdr.revision = info->version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) op.cmd = XENPF_firmware_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) op.u.firmware_info.type = XEN_FW_EFI_INFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) op.u.firmware_info.index = XEN_FW_EFI_RT_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (HYPERVISOR_platform_op(&op) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) efi.runtime_version = info->version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return &efi_systab_xen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * Determine whether we're in secure boot mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * Please keep the logic in sync with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * drivers/firmware/efi/libstub/secureboot.c:efi_get_secureboot().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static enum efi_secureboot_mode xen_efi_get_secureboot(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static efi_guid_t efi_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) u8 moksbstate, secboot, setupmode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) size = sizeof(secboot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) status = efi.get_variable(L"SecureBoot", &efi_variable_guid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) NULL, &size, &secboot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (status == EFI_NOT_FOUND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return efi_secureboot_mode_disabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) goto out_efi_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) size = sizeof(setupmode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) status = efi.get_variable(L"SetupMode", &efi_variable_guid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) NULL, &size, &setupmode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto out_efi_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (secboot == 0 || setupmode == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return efi_secureboot_mode_disabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* See if a user has put the shim into insecure mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) size = sizeof(moksbstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) status = efi.get_variable(L"MokSBStateRT", &shim_guid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) NULL, &size, &moksbstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /* If it fails, we don't care why. Default to secure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) goto secure_boot_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (moksbstate == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return efi_secureboot_mode_disabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) secure_boot_enabled:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) pr_info("UEFI Secure Boot is enabled.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return efi_secureboot_mode_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) out_efi_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) pr_err("Could not determine UEFI Secure Boot status.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return efi_secureboot_mode_unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) void __init xen_efi_init(struct boot_params *boot_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) efi_system_table_t *efi_systab_xen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) efi_systab_xen = xen_efi_probe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (efi_systab_xen == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) strncpy((char *)&boot_params->efi_info.efi_loader_signature, "Xen",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) sizeof(boot_params->efi_info.efi_loader_signature));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) boot_params->efi_info.efi_systab = (__u32)__pa(efi_systab_xen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) boot_params->efi_info.efi_systab_hi = (__u32)(__pa(efi_systab_xen) >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) boot_params->secure_boot = xen_efi_get_secureboot();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) set_bit(EFI_BOOT, &efi.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) set_bit(EFI_PARAVIRT, &efi.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) set_bit(EFI_64BIT, &efi.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }