efi.c 4.3 KB

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