quirks.c 13 KB

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