quirks.c 8.0 KB

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