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