efi.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Helpers for early access to EFI configuration table.
  4. *
  5. * Originally derived from arch/x86/boot/compressed/acpi.c
  6. */
  7. #include "misc.h"
  8. /**
  9. * efi_get_type - Given a pointer to boot_params, determine the type of EFI environment.
  10. *
  11. * @bp: pointer to boot_params
  12. *
  13. * Return: EFI_TYPE_{32,64} for valid EFI environments, EFI_TYPE_NONE otherwise.
  14. */
  15. enum efi_type efi_get_type(struct boot_params *bp)
  16. {
  17. struct efi_info *ei;
  18. enum efi_type et;
  19. const char *sig;
  20. ei = &bp->efi_info;
  21. sig = (char *)&ei->efi_loader_signature;
  22. if (!strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) {
  23. et = EFI_TYPE_64;
  24. } else if (!strncmp(sig, EFI32_LOADER_SIGNATURE, 4)) {
  25. et = EFI_TYPE_32;
  26. } else {
  27. debug_putstr("No EFI environment detected.\n");
  28. et = EFI_TYPE_NONE;
  29. }
  30. #ifndef CONFIG_X86_64
  31. /*
  32. * Existing callers like acpi.c treat this case as an indicator to
  33. * fall-through to non-EFI, rather than an error, so maintain that
  34. * functionality here as well.
  35. */
  36. if (ei->efi_systab_hi || ei->efi_memmap_hi) {
  37. debug_putstr("EFI system table is located above 4GB and cannot be accessed.\n");
  38. et = EFI_TYPE_NONE;
  39. }
  40. #endif
  41. return et;
  42. }
  43. /**
  44. * efi_get_system_table - Given a pointer to boot_params, retrieve the physical address
  45. * of the EFI system table.
  46. *
  47. * @bp: pointer to boot_params
  48. *
  49. * Return: EFI system table address on success. On error, return 0.
  50. */
  51. unsigned long efi_get_system_table(struct boot_params *bp)
  52. {
  53. unsigned long sys_tbl_pa;
  54. struct efi_info *ei;
  55. enum efi_type et;
  56. /* Get systab from boot params. */
  57. ei = &bp->efi_info;
  58. #ifdef CONFIG_X86_64
  59. sys_tbl_pa = ei->efi_systab | ((__u64)ei->efi_systab_hi << 32);
  60. #else
  61. sys_tbl_pa = ei->efi_systab;
  62. #endif
  63. if (!sys_tbl_pa) {
  64. debug_putstr("EFI system table not found.");
  65. return 0;
  66. }
  67. return sys_tbl_pa;
  68. }
  69. /*
  70. * EFI config table address changes to virtual address after boot, which may
  71. * not be accessible for the kexec'd kernel. To address this, kexec provides
  72. * the initial physical address via a struct setup_data entry, which is
  73. * checked for here, along with some sanity checks.
  74. */
  75. static struct efi_setup_data *get_kexec_setup_data(struct boot_params *bp,
  76. enum efi_type et)
  77. {
  78. #ifdef CONFIG_X86_64
  79. struct efi_setup_data *esd = NULL;
  80. struct setup_data *data;
  81. u64 pa_data;
  82. pa_data = bp->hdr.setup_data;
  83. while (pa_data) {
  84. data = (struct setup_data *)pa_data;
  85. if (data->type == SETUP_EFI) {
  86. esd = (struct efi_setup_data *)(pa_data + sizeof(struct setup_data));
  87. break;
  88. }
  89. pa_data = data->next;
  90. }
  91. /*
  92. * Original ACPI code falls back to attempting normal EFI boot in these
  93. * cases, so maintain existing behavior by indicating non-kexec
  94. * environment to the caller, but print them for debugging.
  95. */
  96. if (esd && !esd->tables) {
  97. debug_putstr("kexec EFI environment missing valid configuration table.\n");
  98. return NULL;
  99. }
  100. return esd;
  101. #endif
  102. return NULL;
  103. }
  104. /**
  105. * efi_get_conf_table - Given a pointer to boot_params, locate and return the physical
  106. * address of EFI configuration table.
  107. *
  108. * @bp: pointer to boot_params
  109. * @cfg_tbl_pa: location to store physical address of config table
  110. * @cfg_tbl_len: location to store number of config table entries
  111. *
  112. * Return: 0 on success. On error, return params are left unchanged.
  113. */
  114. int efi_get_conf_table(struct boot_params *bp, unsigned long *cfg_tbl_pa,
  115. unsigned int *cfg_tbl_len)
  116. {
  117. unsigned long sys_tbl_pa;
  118. enum efi_type et;
  119. int ret;
  120. if (!cfg_tbl_pa || !cfg_tbl_len)
  121. return -EINVAL;
  122. sys_tbl_pa = efi_get_system_table(bp);
  123. if (!sys_tbl_pa)
  124. return -EINVAL;
  125. /* Handle EFI bitness properly */
  126. et = efi_get_type(bp);
  127. if (et == EFI_TYPE_64) {
  128. efi_system_table_64_t *stbl = (efi_system_table_64_t *)sys_tbl_pa;
  129. struct efi_setup_data *esd;
  130. /* kexec provides an alternative EFI conf table, check for it. */
  131. esd = get_kexec_setup_data(bp, et);
  132. *cfg_tbl_pa = esd ? esd->tables : stbl->tables;
  133. *cfg_tbl_len = stbl->nr_tables;
  134. } else if (et == EFI_TYPE_32) {
  135. efi_system_table_32_t *stbl = (efi_system_table_32_t *)sys_tbl_pa;
  136. *cfg_tbl_pa = stbl->tables;
  137. *cfg_tbl_len = stbl->nr_tables;
  138. } else {
  139. return -EINVAL;
  140. }
  141. return 0;
  142. }
  143. /* Get vendor table address/guid from EFI config table at the given index */
  144. static int get_vendor_table(void *cfg_tbl, unsigned int idx,
  145. unsigned long *vendor_tbl_pa,
  146. efi_guid_t *vendor_tbl_guid,
  147. enum efi_type et)
  148. {
  149. if (et == EFI_TYPE_64) {
  150. efi_config_table_64_t *tbl_entry = (efi_config_table_64_t *)cfg_tbl + idx;
  151. if (!IS_ENABLED(CONFIG_X86_64) && tbl_entry->table >> 32) {
  152. debug_putstr("Error: EFI config table entry located above 4GB.\n");
  153. return -EINVAL;
  154. }
  155. *vendor_tbl_pa = tbl_entry->table;
  156. *vendor_tbl_guid = tbl_entry->guid;
  157. } else if (et == EFI_TYPE_32) {
  158. efi_config_table_32_t *tbl_entry = (efi_config_table_32_t *)cfg_tbl + idx;
  159. *vendor_tbl_pa = tbl_entry->table;
  160. *vendor_tbl_guid = tbl_entry->guid;
  161. } else {
  162. return -EINVAL;
  163. }
  164. return 0;
  165. }
  166. /**
  167. * efi_find_vendor_table - Given EFI config table, search it for the physical
  168. * address of the vendor table associated with GUID.
  169. *
  170. * @bp: pointer to boot_params
  171. * @cfg_tbl_pa: pointer to EFI configuration table
  172. * @cfg_tbl_len: number of entries in EFI configuration table
  173. * @guid: GUID of vendor table
  174. *
  175. * Return: vendor table address on success. On error, return 0.
  176. */
  177. unsigned long efi_find_vendor_table(struct boot_params *bp,
  178. unsigned long cfg_tbl_pa,
  179. unsigned int cfg_tbl_len,
  180. efi_guid_t guid)
  181. {
  182. enum efi_type et;
  183. unsigned int i;
  184. et = efi_get_type(bp);
  185. if (et == EFI_TYPE_NONE)
  186. return 0;
  187. for (i = 0; i < cfg_tbl_len; i++) {
  188. unsigned long vendor_tbl_pa;
  189. efi_guid_t vendor_tbl_guid;
  190. int ret;
  191. ret = get_vendor_table((void *)cfg_tbl_pa, i,
  192. &vendor_tbl_pa,
  193. &vendor_tbl_guid, et);
  194. if (ret)
  195. return 0;
  196. if (!efi_guidcmp(guid, vendor_tbl_guid))
  197. return vendor_tbl_pa;
  198. }
  199. return 0;
  200. }