quirks.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include <linux/init.h>
  2. #include <linux/kernel.h>
  3. #include <linux/string.h>
  4. #include <linux/time.h>
  5. #include <linux/types.h>
  6. #include <linux/efi.h>
  7. #include <linux/slab.h>
  8. #include <linux/memblock.h>
  9. #include <linux/bootmem.h>
  10. #include <linux/acpi.h>
  11. #include <asm/efi.h>
  12. #include <asm/uv/uv.h>
  13. #define EFI_MIN_RESERVE 5120
  14. #define EFI_DUMMY_GUID \
  15. EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
  16. static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
  17. static bool efi_no_storage_paranoia;
  18. /*
  19. * Some firmware implementations refuse to boot if there's insufficient
  20. * space in the variable store. The implementation of garbage collection
  21. * in some FW versions causes stale (deleted) variables to take up space
  22. * longer than intended and space is only freed once the store becomes
  23. * almost completely full.
  24. *
  25. * Enabling this option disables the space checks in
  26. * efi_query_variable_store() and forces garbage collection.
  27. *
  28. * Only enable this option if deleting EFI variables does not free up
  29. * space in your variable store, e.g. if despite deleting variables
  30. * you're unable to create new ones.
  31. */
  32. static int __init setup_storage_paranoia(char *arg)
  33. {
  34. efi_no_storage_paranoia = true;
  35. return 0;
  36. }
  37. early_param("efi_no_storage_paranoia", setup_storage_paranoia);
  38. /*
  39. * Deleting the dummy variable which kicks off garbage collection
  40. */
  41. void efi_delete_dummy_variable(void)
  42. {
  43. efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  44. EFI_VARIABLE_NON_VOLATILE |
  45. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  46. EFI_VARIABLE_RUNTIME_ACCESS,
  47. 0, NULL);
  48. }
  49. /*
  50. * Some firmware implementations refuse to boot if there's insufficient space
  51. * in the variable store. Ensure that we never use more than a safe limit.
  52. *
  53. * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
  54. * store.
  55. */
  56. efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
  57. {
  58. efi_status_t status;
  59. u64 storage_size, remaining_size, max_size;
  60. if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
  61. return 0;
  62. status = efi.query_variable_info(attributes, &storage_size,
  63. &remaining_size, &max_size);
  64. if (status != EFI_SUCCESS)
  65. return status;
  66. /*
  67. * We account for that by refusing the write if permitting it would
  68. * reduce the available space to under 5KB. This figure was provided by
  69. * Samsung, so should be safe.
  70. */
  71. if ((remaining_size - size < EFI_MIN_RESERVE) &&
  72. !efi_no_storage_paranoia) {
  73. /*
  74. * Triggering garbage collection may require that the firmware
  75. * generate a real EFI_OUT_OF_RESOURCES error. We can force
  76. * that by attempting to use more space than is available.
  77. */
  78. unsigned long dummy_size = remaining_size + 1024;
  79. void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
  80. if (!dummy)
  81. return EFI_OUT_OF_RESOURCES;
  82. status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  83. EFI_VARIABLE_NON_VOLATILE |
  84. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  85. EFI_VARIABLE_RUNTIME_ACCESS,
  86. dummy_size, dummy);
  87. if (status == EFI_SUCCESS) {
  88. /*
  89. * This should have failed, so if it didn't make sure
  90. * that we delete it...
  91. */
  92. efi_delete_dummy_variable();
  93. }
  94. kfree(dummy);
  95. /*
  96. * The runtime code may now have triggered a garbage collection
  97. * run, so check the variable info again
  98. */
  99. status = efi.query_variable_info(attributes, &storage_size,
  100. &remaining_size, &max_size);
  101. if (status != EFI_SUCCESS)
  102. return status;
  103. /*
  104. * There still isn't enough room, so return an error
  105. */
  106. if (remaining_size - size < EFI_MIN_RESERVE)
  107. return EFI_OUT_OF_RESOURCES;
  108. }
  109. return EFI_SUCCESS;
  110. }
  111. EXPORT_SYMBOL_GPL(efi_query_variable_store);
  112. /*
  113. * The UEFI specification makes it clear that the operating system is free to do
  114. * whatever it wants with boot services code after ExitBootServices() has been
  115. * called. Ignoring this recommendation a significant bunch of EFI implementations
  116. * continue calling into boot services code (SetVirtualAddressMap). In order to
  117. * work around such buggy implementations we reserve boot services region during
  118. * EFI init and make sure it stays executable. Then, after SetVirtualAddressMap(), it
  119. * is discarded.
  120. */
  121. void __init efi_reserve_boot_services(void)
  122. {
  123. void *p;
  124. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  125. efi_memory_desc_t *md = p;
  126. u64 start = md->phys_addr;
  127. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  128. if (md->type != EFI_BOOT_SERVICES_CODE &&
  129. md->type != EFI_BOOT_SERVICES_DATA)
  130. continue;
  131. /* Only reserve where possible:
  132. * - Not within any already allocated areas
  133. * - Not over any memory area (really needed, if above?)
  134. * - Not within any part of the kernel
  135. * - Not the bios reserved area
  136. */
  137. if ((start + size > __pa_symbol(_text)
  138. && start <= __pa_symbol(_end)) ||
  139. !e820_all_mapped(start, start+size, E820_RAM) ||
  140. memblock_is_region_reserved(start, size)) {
  141. /* Could not reserve, skip it */
  142. md->num_pages = 0;
  143. memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
  144. start, start+size-1);
  145. } else
  146. memblock_reserve(start, size);
  147. }
  148. }
  149. void __init efi_free_boot_services(void)
  150. {
  151. void *p;
  152. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  153. efi_memory_desc_t *md = p;
  154. unsigned long long start = md->phys_addr;
  155. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  156. if (md->type != EFI_BOOT_SERVICES_CODE &&
  157. md->type != EFI_BOOT_SERVICES_DATA)
  158. continue;
  159. /* Could not reserve boot area */
  160. if (!size)
  161. continue;
  162. free_bootmem_late(start, size);
  163. }
  164. efi_unmap_memmap();
  165. }
  166. /*
  167. * A number of config table entries get remapped to virtual addresses
  168. * after entering EFI virtual mode. However, the kexec kernel requires
  169. * their physical addresses therefore we pass them via setup_data and
  170. * correct those entries to their respective physical addresses here.
  171. *
  172. * Currently only handles smbios which is necessary for some firmware
  173. * implementation.
  174. */
  175. int __init efi_reuse_config(u64 tables, int nr_tables)
  176. {
  177. int i, sz, ret = 0;
  178. void *p, *tablep;
  179. struct efi_setup_data *data;
  180. if (!efi_setup)
  181. return 0;
  182. if (!efi_enabled(EFI_64BIT))
  183. return 0;
  184. data = early_memremap(efi_setup, sizeof(*data));
  185. if (!data) {
  186. ret = -ENOMEM;
  187. goto out;
  188. }
  189. if (!data->smbios)
  190. goto out_memremap;
  191. sz = sizeof(efi_config_table_64_t);
  192. p = tablep = early_memremap(tables, nr_tables * sz);
  193. if (!p) {
  194. pr_err("Could not map Configuration table!\n");
  195. ret = -ENOMEM;
  196. goto out_memremap;
  197. }
  198. for (i = 0; i < efi.systab->nr_tables; i++) {
  199. efi_guid_t guid;
  200. guid = ((efi_config_table_64_t *)p)->guid;
  201. if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
  202. ((efi_config_table_64_t *)p)->table = data->smbios;
  203. p += sz;
  204. }
  205. early_memunmap(tablep, nr_tables * sz);
  206. out_memremap:
  207. early_memunmap(data, sizeof(*data));
  208. out:
  209. return ret;
  210. }
  211. void __init efi_apply_memmap_quirks(void)
  212. {
  213. /*
  214. * Once setup is done earlier, unmap the EFI memory map on mismatched
  215. * firmware/kernel architectures since there is no support for runtime
  216. * services.
  217. */
  218. if (!efi_runtime_supported()) {
  219. pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
  220. efi_unmap_memmap();
  221. }
  222. /*
  223. * UV doesn't support the new EFI pagetable mapping yet.
  224. */
  225. if (is_uv_system())
  226. set_bit(EFI_OLD_MEMMAP, &efi.flags);
  227. }
  228. /*
  229. * For most modern platforms the preferred method of powering off is via
  230. * ACPI. However, there are some that are known to require the use of
  231. * EFI runtime services and for which ACPI does not work at all.
  232. *
  233. * Using EFI is a last resort, to be used only if no other option
  234. * exists.
  235. */
  236. bool efi_reboot_required(void)
  237. {
  238. if (!acpi_gbl_reduced_hardware)
  239. return false;
  240. efi_reboot_quirk_mode = EFI_RESET_WARM;
  241. return true;
  242. }
  243. bool efi_poweroff_required(void)
  244. {
  245. return !!acpi_gbl_reduced_hardware;
  246. }