quirks.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #define pr_fmt(fmt) "efi: " fmt
  2. #include <linux/init.h>
  3. #include <linux/kernel.h>
  4. #include <linux/string.h>
  5. #include <linux/time.h>
  6. #include <linux/types.h>
  7. #include <linux/efi.h>
  8. #include <linux/slab.h>
  9. #include <linux/memblock.h>
  10. #include <linux/bootmem.h>
  11. #include <linux/acpi.h>
  12. #include <linux/dmi.h>
  13. #include <asm/efi.h>
  14. #include <asm/uv/uv.h>
  15. #define EFI_MIN_RESERVE 5120
  16. #define EFI_DUMMY_GUID \
  17. EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
  18. static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
  19. static bool efi_no_storage_paranoia;
  20. /*
  21. * Some firmware implementations refuse to boot if there's insufficient
  22. * space in the variable store. The implementation of garbage collection
  23. * in some FW versions causes stale (deleted) variables to take up space
  24. * longer than intended and space is only freed once the store becomes
  25. * almost completely full.
  26. *
  27. * Enabling this option disables the space checks in
  28. * efi_query_variable_store() and forces garbage collection.
  29. *
  30. * Only enable this option if deleting EFI variables does not free up
  31. * space in your variable store, e.g. if despite deleting variables
  32. * you're unable to create new ones.
  33. */
  34. static int __init setup_storage_paranoia(char *arg)
  35. {
  36. efi_no_storage_paranoia = true;
  37. return 0;
  38. }
  39. early_param("efi_no_storage_paranoia", setup_storage_paranoia);
  40. /*
  41. * Deleting the dummy variable which kicks off garbage collection
  42. */
  43. void efi_delete_dummy_variable(void)
  44. {
  45. efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  46. EFI_VARIABLE_NON_VOLATILE |
  47. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  48. EFI_VARIABLE_RUNTIME_ACCESS,
  49. 0, NULL);
  50. }
  51. /*
  52. * In the nonblocking case we do not attempt to perform garbage
  53. * collection if we do not have enough free space. Rather, we do the
  54. * bare minimum check and give up immediately if the available space
  55. * is below EFI_MIN_RESERVE.
  56. *
  57. * This function is intended to be small and simple because it is
  58. * invoked from crash handler paths.
  59. */
  60. static efi_status_t
  61. query_variable_store_nonblocking(u32 attributes, unsigned long size)
  62. {
  63. efi_status_t status;
  64. u64 storage_size, remaining_size, max_size;
  65. status = efi.query_variable_info_nonblocking(attributes, &storage_size,
  66. &remaining_size,
  67. &max_size);
  68. if (status != EFI_SUCCESS)
  69. return status;
  70. if (remaining_size - size < EFI_MIN_RESERVE)
  71. return EFI_OUT_OF_RESOURCES;
  72. return EFI_SUCCESS;
  73. }
  74. /*
  75. * Some firmware implementations refuse to boot if there's insufficient space
  76. * in the variable store. Ensure that we never use more than a safe limit.
  77. *
  78. * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
  79. * store.
  80. */
  81. efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
  82. bool nonblocking)
  83. {
  84. efi_status_t status;
  85. u64 storage_size, remaining_size, max_size;
  86. if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
  87. return 0;
  88. if (nonblocking)
  89. return query_variable_store_nonblocking(attributes, size);
  90. status = efi.query_variable_info(attributes, &storage_size,
  91. &remaining_size, &max_size);
  92. if (status != EFI_SUCCESS)
  93. return status;
  94. /*
  95. * We account for that by refusing the write if permitting it would
  96. * reduce the available space to under 5KB. This figure was provided by
  97. * Samsung, so should be safe.
  98. */
  99. if ((remaining_size - size < EFI_MIN_RESERVE) &&
  100. !efi_no_storage_paranoia) {
  101. /*
  102. * Triggering garbage collection may require that the firmware
  103. * generate a real EFI_OUT_OF_RESOURCES error. We can force
  104. * that by attempting to use more space than is available.
  105. */
  106. unsigned long dummy_size = remaining_size + 1024;
  107. void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
  108. if (!dummy)
  109. return EFI_OUT_OF_RESOURCES;
  110. status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  111. EFI_VARIABLE_NON_VOLATILE |
  112. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  113. EFI_VARIABLE_RUNTIME_ACCESS,
  114. dummy_size, dummy);
  115. if (status == EFI_SUCCESS) {
  116. /*
  117. * This should have failed, so if it didn't make sure
  118. * that we delete it...
  119. */
  120. efi_delete_dummy_variable();
  121. }
  122. kfree(dummy);
  123. /*
  124. * The runtime code may now have triggered a garbage collection
  125. * run, so check the variable info again
  126. */
  127. status = efi.query_variable_info(attributes, &storage_size,
  128. &remaining_size, &max_size);
  129. if (status != EFI_SUCCESS)
  130. return status;
  131. /*
  132. * There still isn't enough room, so return an error
  133. */
  134. if (remaining_size - size < EFI_MIN_RESERVE)
  135. return EFI_OUT_OF_RESOURCES;
  136. }
  137. return EFI_SUCCESS;
  138. }
  139. EXPORT_SYMBOL_GPL(efi_query_variable_store);
  140. /*
  141. * Helper function for efi_reserve_boot_services() to figure out if we
  142. * can free regions in efi_free_boot_services().
  143. *
  144. * Use this function to ensure we do not free regions owned by somebody
  145. * else. We must only reserve (and then free) regions:
  146. *
  147. * - Not within any part of the kernel
  148. * - Not the BIOS reserved area (E820_RESERVED, E820_NVS, etc)
  149. */
  150. static bool can_free_region(u64 start, u64 size)
  151. {
  152. if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))
  153. return false;
  154. if (!e820_all_mapped(start, start+size, E820_RAM))
  155. return false;
  156. return true;
  157. }
  158. /*
  159. * The UEFI specification makes it clear that the operating system is free to do
  160. * whatever it wants with boot services code after ExitBootServices() has been
  161. * called. Ignoring this recommendation a significant bunch of EFI implementations
  162. * continue calling into boot services code (SetVirtualAddressMap). In order to
  163. * work around such buggy implementations we reserve boot services region during
  164. * EFI init and make sure it stays executable. Then, after SetVirtualAddressMap(), it
  165. * is discarded.
  166. */
  167. void __init efi_reserve_boot_services(void)
  168. {
  169. efi_memory_desc_t *md;
  170. for_each_efi_memory_desc(md) {
  171. u64 start = md->phys_addr;
  172. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  173. bool already_reserved;
  174. if (md->type != EFI_BOOT_SERVICES_CODE &&
  175. md->type != EFI_BOOT_SERVICES_DATA)
  176. continue;
  177. already_reserved = memblock_is_region_reserved(start, size);
  178. /*
  179. * Because the following memblock_reserve() is paired
  180. * with free_bootmem_late() for this region in
  181. * efi_free_boot_services(), we must be extremely
  182. * careful not to reserve, and subsequently free,
  183. * critical regions of memory (like the kernel image) or
  184. * those regions that somebody else has already
  185. * reserved.
  186. *
  187. * A good example of a critical region that must not be
  188. * freed is page zero (first 4Kb of memory), which may
  189. * contain boot services code/data but is marked
  190. * E820_RESERVED by trim_bios_range().
  191. */
  192. if (!already_reserved) {
  193. memblock_reserve(start, size);
  194. /*
  195. * If we are the first to reserve the region, no
  196. * one else cares about it. We own it and can
  197. * free it later.
  198. */
  199. if (can_free_region(start, size))
  200. continue;
  201. }
  202. /*
  203. * We don't own the region. We must not free it.
  204. *
  205. * Setting this bit for a boot services region really
  206. * doesn't make sense as far as the firmware is
  207. * concerned, but it does provide us with a way to tag
  208. * those regions that must not be paired with
  209. * free_bootmem_late().
  210. */
  211. md->attribute |= EFI_MEMORY_RUNTIME;
  212. }
  213. }
  214. void __init efi_free_boot_services(void)
  215. {
  216. efi_memory_desc_t *md;
  217. for_each_efi_memory_desc(md) {
  218. unsigned long long start = md->phys_addr;
  219. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  220. size_t rm_size;
  221. if (md->type != EFI_BOOT_SERVICES_CODE &&
  222. md->type != EFI_BOOT_SERVICES_DATA)
  223. continue;
  224. /* Do not free, someone else owns it: */
  225. if (md->attribute & EFI_MEMORY_RUNTIME)
  226. continue;
  227. /*
  228. * Nasty quirk: if all sub-1MB memory is used for boot
  229. * services, we can get here without having allocated the
  230. * real mode trampoline. It's too late to hand boot services
  231. * memory back to the memblock allocator, so instead
  232. * try to manually allocate the trampoline if needed.
  233. *
  234. * I've seen this on a Dell XPS 13 9350 with firmware
  235. * 1.4.4 with SGX enabled booting Linux via Fedora 24's
  236. * grub2-efi on a hard disk. (And no, I don't know why
  237. * this happened, but Linux should still try to boot rather
  238. * panicing early.)
  239. */
  240. rm_size = real_mode_size_needed();
  241. if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {
  242. set_real_mode_mem(start, rm_size);
  243. start += rm_size;
  244. size -= rm_size;
  245. }
  246. free_bootmem_late(start, size);
  247. }
  248. efi_unmap_memmap();
  249. }
  250. /*
  251. * A number of config table entries get remapped to virtual addresses
  252. * after entering EFI virtual mode. However, the kexec kernel requires
  253. * their physical addresses therefore we pass them via setup_data and
  254. * correct those entries to their respective physical addresses here.
  255. *
  256. * Currently only handles smbios which is necessary for some firmware
  257. * implementation.
  258. */
  259. int __init efi_reuse_config(u64 tables, int nr_tables)
  260. {
  261. int i, sz, ret = 0;
  262. void *p, *tablep;
  263. struct efi_setup_data *data;
  264. if (!efi_setup)
  265. return 0;
  266. if (!efi_enabled(EFI_64BIT))
  267. return 0;
  268. data = early_memremap(efi_setup, sizeof(*data));
  269. if (!data) {
  270. ret = -ENOMEM;
  271. goto out;
  272. }
  273. if (!data->smbios)
  274. goto out_memremap;
  275. sz = sizeof(efi_config_table_64_t);
  276. p = tablep = early_memremap(tables, nr_tables * sz);
  277. if (!p) {
  278. pr_err("Could not map Configuration table!\n");
  279. ret = -ENOMEM;
  280. goto out_memremap;
  281. }
  282. for (i = 0; i < efi.systab->nr_tables; i++) {
  283. efi_guid_t guid;
  284. guid = ((efi_config_table_64_t *)p)->guid;
  285. if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
  286. ((efi_config_table_64_t *)p)->table = data->smbios;
  287. p += sz;
  288. }
  289. early_memunmap(tablep, nr_tables * sz);
  290. out_memremap:
  291. early_memunmap(data, sizeof(*data));
  292. out:
  293. return ret;
  294. }
  295. static const struct dmi_system_id sgi_uv1_dmi[] = {
  296. { NULL, "SGI UV1",
  297. { DMI_MATCH(DMI_PRODUCT_NAME, "Stoutland Platform"),
  298. DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"),
  299. DMI_MATCH(DMI_BIOS_VENDOR, "SGI.COM"),
  300. }
  301. },
  302. { } /* NULL entry stops DMI scanning */
  303. };
  304. void __init efi_apply_memmap_quirks(void)
  305. {
  306. /*
  307. * Once setup is done earlier, unmap the EFI memory map on mismatched
  308. * firmware/kernel architectures since there is no support for runtime
  309. * services.
  310. */
  311. if (!efi_runtime_supported()) {
  312. pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
  313. efi_unmap_memmap();
  314. }
  315. /* UV2+ BIOS has a fix for this issue. UV1 still needs the quirk. */
  316. if (dmi_check_system(sgi_uv1_dmi))
  317. set_bit(EFI_OLD_MEMMAP, &efi.flags);
  318. }
  319. /*
  320. * For most modern platforms the preferred method of powering off is via
  321. * ACPI. However, there are some that are known to require the use of
  322. * EFI runtime services and for which ACPI does not work at all.
  323. *
  324. * Using EFI is a last resort, to be used only if no other option
  325. * exists.
  326. */
  327. bool efi_reboot_required(void)
  328. {
  329. if (!acpi_gbl_reduced_hardware)
  330. return false;
  331. efi_reboot_quirk_mode = EFI_RESET_WARM;
  332. return true;
  333. }
  334. bool efi_poweroff_required(void)
  335. {
  336. return acpi_gbl_reduced_hardware || acpi_no_s5;
  337. }