efi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2014 Oracle Co., Daniel Kiper
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/bitops.h>
  18. #include <linux/efi.h>
  19. #include <linux/init.h>
  20. #include <linux/string.h>
  21. #include <xen/xen.h>
  22. #include <xen/xen-ops.h>
  23. #include <xen/interface/platform.h>
  24. #include <asm/page.h>
  25. #include <asm/setup.h>
  26. #include <asm/xen/hypercall.h>
  27. static efi_char16_t vendor[100] __initdata;
  28. static efi_system_table_t efi_systab_xen __initdata = {
  29. .hdr = {
  30. .signature = EFI_SYSTEM_TABLE_SIGNATURE,
  31. .revision = 0, /* Initialized later. */
  32. .headersize = 0, /* Ignored by Linux Kernel. */
  33. .crc32 = 0, /* Ignored by Linux Kernel. */
  34. .reserved = 0
  35. },
  36. .fw_vendor = EFI_INVALID_TABLE_ADDR, /* Initialized later. */
  37. .fw_revision = 0, /* Initialized later. */
  38. .con_in_handle = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
  39. .con_in = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
  40. .con_out_handle = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
  41. .con_out = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
  42. .stderr_handle = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
  43. .stderr = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
  44. .runtime = (efi_runtime_services_t *)EFI_INVALID_TABLE_ADDR,
  45. /* Not used under Xen. */
  46. .boottime = (efi_boot_services_t *)EFI_INVALID_TABLE_ADDR,
  47. /* Not used under Xen. */
  48. .nr_tables = 0, /* Initialized later. */
  49. .tables = EFI_INVALID_TABLE_ADDR /* Initialized later. */
  50. };
  51. static efi_system_table_t __init *xen_efi_probe(void)
  52. {
  53. struct xen_platform_op op = {
  54. .cmd = XENPF_firmware_info,
  55. .u.firmware_info = {
  56. .type = XEN_FW_EFI_INFO,
  57. .index = XEN_FW_EFI_CONFIG_TABLE
  58. }
  59. };
  60. union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
  61. if (!xen_initial_domain() || HYPERVISOR_platform_op(&op) < 0)
  62. return NULL;
  63. /* Here we know that Xen runs on EFI platform. */
  64. efi.get_time = xen_efi_get_time;
  65. efi.set_time = xen_efi_set_time;
  66. efi.get_wakeup_time = xen_efi_get_wakeup_time;
  67. efi.set_wakeup_time = xen_efi_set_wakeup_time;
  68. efi.get_variable = xen_efi_get_variable;
  69. efi.get_next_variable = xen_efi_get_next_variable;
  70. efi.set_variable = xen_efi_set_variable;
  71. efi.set_variable_nonblocking = xen_efi_set_variable;
  72. efi.query_variable_info = xen_efi_query_variable_info;
  73. efi.query_variable_info_nonblocking = xen_efi_query_variable_info;
  74. efi.update_capsule = xen_efi_update_capsule;
  75. efi.query_capsule_caps = xen_efi_query_capsule_caps;
  76. efi.get_next_high_mono_count = xen_efi_get_next_high_mono_count;
  77. efi.reset_system = xen_efi_reset_system;
  78. efi_systab_xen.tables = info->cfg.addr;
  79. efi_systab_xen.nr_tables = info->cfg.nent;
  80. op.cmd = XENPF_firmware_info;
  81. op.u.firmware_info.type = XEN_FW_EFI_INFO;
  82. op.u.firmware_info.index = XEN_FW_EFI_VENDOR;
  83. info->vendor.bufsz = sizeof(vendor);
  84. set_xen_guest_handle(info->vendor.name, vendor);
  85. if (HYPERVISOR_platform_op(&op) == 0) {
  86. efi_systab_xen.fw_vendor = __pa_symbol(vendor);
  87. efi_systab_xen.fw_revision = info->vendor.revision;
  88. } else
  89. efi_systab_xen.fw_vendor = __pa_symbol(L"UNKNOWN");
  90. op.cmd = XENPF_firmware_info;
  91. op.u.firmware_info.type = XEN_FW_EFI_INFO;
  92. op.u.firmware_info.index = XEN_FW_EFI_VERSION;
  93. if (HYPERVISOR_platform_op(&op) == 0)
  94. efi_systab_xen.hdr.revision = info->version;
  95. op.cmd = XENPF_firmware_info;
  96. op.u.firmware_info.type = XEN_FW_EFI_INFO;
  97. op.u.firmware_info.index = XEN_FW_EFI_RT_VERSION;
  98. if (HYPERVISOR_platform_op(&op) == 0)
  99. efi.runtime_version = info->version;
  100. return &efi_systab_xen;
  101. }
  102. /*
  103. * Determine whether we're in secure boot mode.
  104. *
  105. * Please keep the logic in sync with
  106. * drivers/firmware/efi/libstub/secureboot.c:efi_get_secureboot().
  107. */
  108. static enum efi_secureboot_mode xen_efi_get_secureboot(void)
  109. {
  110. static efi_guid_t efi_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
  111. static efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
  112. efi_status_t status;
  113. u8 moksbstate, secboot, setupmode;
  114. unsigned long size;
  115. size = sizeof(secboot);
  116. status = efi.get_variable(L"SecureBoot", &efi_variable_guid,
  117. NULL, &size, &secboot);
  118. if (status == EFI_NOT_FOUND)
  119. return efi_secureboot_mode_disabled;
  120. if (status != EFI_SUCCESS)
  121. goto out_efi_err;
  122. size = sizeof(setupmode);
  123. status = efi.get_variable(L"SetupMode", &efi_variable_guid,
  124. NULL, &size, &setupmode);
  125. if (status != EFI_SUCCESS)
  126. goto out_efi_err;
  127. if (secboot == 0 || setupmode == 1)
  128. return efi_secureboot_mode_disabled;
  129. /* See if a user has put the shim into insecure mode. */
  130. size = sizeof(moksbstate);
  131. status = efi.get_variable(L"MokSBStateRT", &shim_guid,
  132. NULL, &size, &moksbstate);
  133. /* If it fails, we don't care why. Default to secure. */
  134. if (status != EFI_SUCCESS)
  135. goto secure_boot_enabled;
  136. if (moksbstate == 1)
  137. return efi_secureboot_mode_disabled;
  138. secure_boot_enabled:
  139. pr_info("UEFI Secure Boot is enabled.\n");
  140. return efi_secureboot_mode_enabled;
  141. out_efi_err:
  142. pr_err("Could not determine UEFI Secure Boot status.\n");
  143. return efi_secureboot_mode_unknown;
  144. }
  145. void __init xen_efi_init(void)
  146. {
  147. efi_system_table_t *efi_systab_xen;
  148. efi_systab_xen = xen_efi_probe();
  149. if (efi_systab_xen == NULL)
  150. return;
  151. strncpy((char *)&boot_params.efi_info.efi_loader_signature, "Xen",
  152. sizeof(boot_params.efi_info.efi_loader_signature));
  153. boot_params.efi_info.efi_systab = (__u32)__pa(efi_systab_xen);
  154. boot_params.efi_info.efi_systab_hi = (__u32)(__pa(efi_systab_xen) >> 32);
  155. boot_params.secure_boot = xen_efi_get_secureboot();
  156. set_bit(EFI_BOOT, &efi.flags);
  157. set_bit(EFI_PARAVIRT, &efi.flags);
  158. set_bit(EFI_64BIT, &efi.flags);
  159. }