quirks.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. * The UEFI specification makes it clear that the operating system is
  142. * free to do whatever it wants with boot services code after
  143. * ExitBootServices() has been called. Ignoring this recommendation a
  144. * significant bunch of EFI implementations continue calling into boot
  145. * services code (SetVirtualAddressMap). In order to work around such
  146. * buggy implementations we reserve boot services region during EFI
  147. * init and make sure it stays executable. Then, after
  148. * SetVirtualAddressMap(), it is discarded.
  149. *
  150. * However, some boot services regions contain data that is required
  151. * by drivers, so we need to track which memory ranges can never be
  152. * freed. This is done by tagging those regions with the
  153. * EFI_MEMORY_RUNTIME attribute.
  154. *
  155. * Any driver that wants to mark a region as reserved must use
  156. * efi_mem_reserve() which will insert a new EFI memory descriptor
  157. * into efi.memmap (splitting existing regions if necessary) and tag
  158. * it with EFI_MEMORY_RUNTIME.
  159. */
  160. void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)
  161. {
  162. phys_addr_t new_phys, new_size;
  163. struct efi_mem_range mr;
  164. efi_memory_desc_t md;
  165. int num_entries;
  166. void *new;
  167. if (efi_mem_desc_lookup(addr, &md)) {
  168. pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr);
  169. return;
  170. }
  171. if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) {
  172. pr_err("Region spans EFI memory descriptors, %pa\n", &addr);
  173. return;
  174. }
  175. size += addr % EFI_PAGE_SIZE;
  176. size = round_up(size, EFI_PAGE_SIZE);
  177. addr = round_down(addr, EFI_PAGE_SIZE);
  178. mr.range.start = addr;
  179. mr.range.end = addr + size - 1;
  180. mr.attribute = md.attribute | EFI_MEMORY_RUNTIME;
  181. num_entries = efi_memmap_split_count(&md, &mr.range);
  182. num_entries += efi.memmap.nr_map;
  183. new_size = efi.memmap.desc_size * num_entries;
  184. new_phys = efi_memmap_alloc(num_entries);
  185. if (!new_phys) {
  186. pr_err("Could not allocate boot services memmap\n");
  187. return;
  188. }
  189. new = early_memremap(new_phys, new_size);
  190. if (!new) {
  191. pr_err("Failed to map new boot services memmap\n");
  192. return;
  193. }
  194. efi_memmap_insert(&efi.memmap, new, &mr);
  195. early_memunmap(new, new_size);
  196. efi_memmap_install(new_phys, num_entries);
  197. }
  198. /*
  199. * Helper function for efi_reserve_boot_services() to figure out if we
  200. * can free regions in efi_free_boot_services().
  201. *
  202. * Use this function to ensure we do not free regions owned by somebody
  203. * else. We must only reserve (and then free) regions:
  204. *
  205. * - Not within any part of the kernel
  206. * - Not the BIOS reserved area (E820_RESERVED, E820_NVS, etc)
  207. */
  208. static bool can_free_region(u64 start, u64 size)
  209. {
  210. if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))
  211. return false;
  212. if (!e820_all_mapped(start, start+size, E820_RAM))
  213. return false;
  214. return true;
  215. }
  216. void __init efi_reserve_boot_services(void)
  217. {
  218. efi_memory_desc_t *md;
  219. for_each_efi_memory_desc(md) {
  220. u64 start = md->phys_addr;
  221. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  222. bool already_reserved;
  223. if (md->type != EFI_BOOT_SERVICES_CODE &&
  224. md->type != EFI_BOOT_SERVICES_DATA)
  225. continue;
  226. already_reserved = memblock_is_region_reserved(start, size);
  227. /*
  228. * Because the following memblock_reserve() is paired
  229. * with free_bootmem_late() for this region in
  230. * efi_free_boot_services(), we must be extremely
  231. * careful not to reserve, and subsequently free,
  232. * critical regions of memory (like the kernel image) or
  233. * those regions that somebody else has already
  234. * reserved.
  235. *
  236. * A good example of a critical region that must not be
  237. * freed is page zero (first 4Kb of memory), which may
  238. * contain boot services code/data but is marked
  239. * E820_RESERVED by trim_bios_range().
  240. */
  241. if (!already_reserved) {
  242. memblock_reserve(start, size);
  243. /*
  244. * If we are the first to reserve the region, no
  245. * one else cares about it. We own it and can
  246. * free it later.
  247. */
  248. if (can_free_region(start, size))
  249. continue;
  250. }
  251. /*
  252. * We don't own the region. We must not free it.
  253. *
  254. * Setting this bit for a boot services region really
  255. * doesn't make sense as far as the firmware is
  256. * concerned, but it does provide us with a way to tag
  257. * those regions that must not be paired with
  258. * free_bootmem_late().
  259. */
  260. md->attribute |= EFI_MEMORY_RUNTIME;
  261. }
  262. }
  263. void __init efi_free_boot_services(void)
  264. {
  265. phys_addr_t new_phys, new_size;
  266. efi_memory_desc_t *md;
  267. int num_entries = 0;
  268. void *new, *new_md;
  269. for_each_efi_memory_desc(md) {
  270. unsigned long long start = md->phys_addr;
  271. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  272. size_t rm_size;
  273. if (md->type != EFI_BOOT_SERVICES_CODE &&
  274. md->type != EFI_BOOT_SERVICES_DATA) {
  275. num_entries++;
  276. continue;
  277. }
  278. /* Do not free, someone else owns it: */
  279. if (md->attribute & EFI_MEMORY_RUNTIME) {
  280. num_entries++;
  281. continue;
  282. }
  283. /*
  284. * Nasty quirk: if all sub-1MB memory is used for boot
  285. * services, we can get here without having allocated the
  286. * real mode trampoline. It's too late to hand boot services
  287. * memory back to the memblock allocator, so instead
  288. * try to manually allocate the trampoline if needed.
  289. *
  290. * I've seen this on a Dell XPS 13 9350 with firmware
  291. * 1.4.4 with SGX enabled booting Linux via Fedora 24's
  292. * grub2-efi on a hard disk. (And no, I don't know why
  293. * this happened, but Linux should still try to boot rather
  294. * panicing early.)
  295. */
  296. rm_size = real_mode_size_needed();
  297. if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {
  298. set_real_mode_mem(start, rm_size);
  299. start += rm_size;
  300. size -= rm_size;
  301. }
  302. free_bootmem_late(start, size);
  303. }
  304. new_size = efi.memmap.desc_size * num_entries;
  305. new_phys = efi_memmap_alloc(num_entries);
  306. if (!new_phys) {
  307. pr_err("Failed to allocate new EFI memmap\n");
  308. return;
  309. }
  310. new = memremap(new_phys, new_size, MEMREMAP_WB);
  311. if (!new) {
  312. pr_err("Failed to map new EFI memmap\n");
  313. return;
  314. }
  315. /*
  316. * Build a new EFI memmap that excludes any boot services
  317. * regions that are not tagged EFI_MEMORY_RUNTIME, since those
  318. * regions have now been freed.
  319. */
  320. new_md = new;
  321. for_each_efi_memory_desc(md) {
  322. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  323. (md->type == EFI_BOOT_SERVICES_CODE ||
  324. md->type == EFI_BOOT_SERVICES_DATA))
  325. continue;
  326. memcpy(new_md, md, efi.memmap.desc_size);
  327. new_md += efi.memmap.desc_size;
  328. }
  329. memunmap(new);
  330. if (efi_memmap_install(new_phys, num_entries)) {
  331. pr_err("Could not install new EFI memmap\n");
  332. return;
  333. }
  334. }
  335. /*
  336. * A number of config table entries get remapped to virtual addresses
  337. * after entering EFI virtual mode. However, the kexec kernel requires
  338. * their physical addresses therefore we pass them via setup_data and
  339. * correct those entries to their respective physical addresses here.
  340. *
  341. * Currently only handles smbios which is necessary for some firmware
  342. * implementation.
  343. */
  344. int __init efi_reuse_config(u64 tables, int nr_tables)
  345. {
  346. int i, sz, ret = 0;
  347. void *p, *tablep;
  348. struct efi_setup_data *data;
  349. if (!efi_setup)
  350. return 0;
  351. if (!efi_enabled(EFI_64BIT))
  352. return 0;
  353. data = early_memremap(efi_setup, sizeof(*data));
  354. if (!data) {
  355. ret = -ENOMEM;
  356. goto out;
  357. }
  358. if (!data->smbios)
  359. goto out_memremap;
  360. sz = sizeof(efi_config_table_64_t);
  361. p = tablep = early_memremap(tables, nr_tables * sz);
  362. if (!p) {
  363. pr_err("Could not map Configuration table!\n");
  364. ret = -ENOMEM;
  365. goto out_memremap;
  366. }
  367. for (i = 0; i < efi.systab->nr_tables; i++) {
  368. efi_guid_t guid;
  369. guid = ((efi_config_table_64_t *)p)->guid;
  370. if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
  371. ((efi_config_table_64_t *)p)->table = data->smbios;
  372. p += sz;
  373. }
  374. early_memunmap(tablep, nr_tables * sz);
  375. out_memremap:
  376. early_memunmap(data, sizeof(*data));
  377. out:
  378. return ret;
  379. }
  380. static const struct dmi_system_id sgi_uv1_dmi[] = {
  381. { NULL, "SGI UV1",
  382. { DMI_MATCH(DMI_PRODUCT_NAME, "Stoutland Platform"),
  383. DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"),
  384. DMI_MATCH(DMI_BIOS_VENDOR, "SGI.COM"),
  385. }
  386. },
  387. { } /* NULL entry stops DMI scanning */
  388. };
  389. void __init efi_apply_memmap_quirks(void)
  390. {
  391. /*
  392. * Once setup is done earlier, unmap the EFI memory map on mismatched
  393. * firmware/kernel architectures since there is no support for runtime
  394. * services.
  395. */
  396. if (!efi_runtime_supported()) {
  397. pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
  398. efi_memmap_unmap();
  399. }
  400. /* UV2+ BIOS has a fix for this issue. UV1 still needs the quirk. */
  401. if (dmi_check_system(sgi_uv1_dmi))
  402. set_bit(EFI_OLD_MEMMAP, &efi.flags);
  403. }
  404. /*
  405. * For most modern platforms the preferred method of powering off is via
  406. * ACPI. However, there are some that are known to require the use of
  407. * EFI runtime services and for which ACPI does not work at all.
  408. *
  409. * Using EFI is a last resort, to be used only if no other option
  410. * exists.
  411. */
  412. bool efi_reboot_required(void)
  413. {
  414. if (!acpi_gbl_reduced_hardware)
  415. return false;
  416. efi_reboot_quirk_mode = EFI_RESET_WARM;
  417. return true;
  418. }
  419. bool efi_poweroff_required(void)
  420. {
  421. return acpi_gbl_reduced_hardware || acpi_no_s5;
  422. }