quirks.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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/acpi.h>
  11. #include <linux/dmi.h>
  12. #include <asm/e820/api.h>
  13. #include <asm/efi.h>
  14. #include <asm/uv/uv.h>
  15. #include <asm/cpu_device_id.h>
  16. #include <asm/reboot.h>
  17. #define EFI_MIN_RESERVE 5120
  18. #define EFI_DUMMY_GUID \
  19. EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
  20. #define QUARK_CSH_SIGNATURE 0x5f435348 /* _CSH */
  21. #define QUARK_SECURITY_HEADER_SIZE 0x400
  22. /*
  23. * Header prepended to the standard EFI capsule on Quark systems the are based
  24. * on Intel firmware BSP.
  25. * @csh_signature: Unique identifier to sanity check signed module
  26. * presence ("_CSH").
  27. * @version: Current version of CSH used. Should be one for Quark A0.
  28. * @modulesize: Size of the entire module including the module header
  29. * and payload.
  30. * @security_version_number_index: Index of SVN to use for validation of signed
  31. * module.
  32. * @security_version_number: Used to prevent against roll back of modules.
  33. * @rsvd_module_id: Currently unused for Clanton (Quark).
  34. * @rsvd_module_vendor: Vendor Identifier. For Intel products value is
  35. * 0x00008086.
  36. * @rsvd_date: BCD representation of build date as yyyymmdd, where
  37. * yyyy=4 digit year, mm=1-12, dd=1-31.
  38. * @headersize: Total length of the header including including any
  39. * padding optionally added by the signing tool.
  40. * @hash_algo: What Hash is used in the module signing.
  41. * @cryp_algo: What Crypto is used in the module signing.
  42. * @keysize: Total length of the key data including including any
  43. * padding optionally added by the signing tool.
  44. * @signaturesize: Total length of the signature including including any
  45. * padding optionally added by the signing tool.
  46. * @rsvd_next_header: 32-bit pointer to the next Secure Boot Module in the
  47. * chain, if there is a next header.
  48. * @rsvd: Reserved, padding structure to required size.
  49. *
  50. * See also QuartSecurityHeader_t in
  51. * Quark_EDKII_v1.2.1.1/QuarkPlatformPkg/Include/QuarkBootRom.h
  52. * from https://downloadcenter.intel.com/download/23197/Intel-Quark-SoC-X1000-Board-Support-Package-BSP
  53. */
  54. struct quark_security_header {
  55. u32 csh_signature;
  56. u32 version;
  57. u32 modulesize;
  58. u32 security_version_number_index;
  59. u32 security_version_number;
  60. u32 rsvd_module_id;
  61. u32 rsvd_module_vendor;
  62. u32 rsvd_date;
  63. u32 headersize;
  64. u32 hash_algo;
  65. u32 cryp_algo;
  66. u32 keysize;
  67. u32 signaturesize;
  68. u32 rsvd_next_header;
  69. u32 rsvd[2];
  70. };
  71. static const efi_char16_t efi_dummy_name[] = L"DUMMY";
  72. static bool efi_no_storage_paranoia;
  73. /*
  74. * Some firmware implementations refuse to boot if there's insufficient
  75. * space in the variable store. The implementation of garbage collection
  76. * in some FW versions causes stale (deleted) variables to take up space
  77. * longer than intended and space is only freed once the store becomes
  78. * almost completely full.
  79. *
  80. * Enabling this option disables the space checks in
  81. * efi_query_variable_store() and forces garbage collection.
  82. *
  83. * Only enable this option if deleting EFI variables does not free up
  84. * space in your variable store, e.g. if despite deleting variables
  85. * you're unable to create new ones.
  86. */
  87. static int __init setup_storage_paranoia(char *arg)
  88. {
  89. efi_no_storage_paranoia = true;
  90. return 0;
  91. }
  92. early_param("efi_no_storage_paranoia", setup_storage_paranoia);
  93. /*
  94. * Deleting the dummy variable which kicks off garbage collection
  95. */
  96. void efi_delete_dummy_variable(void)
  97. {
  98. efi.set_variable_nonblocking((efi_char16_t *)efi_dummy_name,
  99. &EFI_DUMMY_GUID,
  100. EFI_VARIABLE_NON_VOLATILE |
  101. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  102. EFI_VARIABLE_RUNTIME_ACCESS, 0, NULL);
  103. }
  104. /*
  105. * In the nonblocking case we do not attempt to perform garbage
  106. * collection if we do not have enough free space. Rather, we do the
  107. * bare minimum check and give up immediately if the available space
  108. * is below EFI_MIN_RESERVE.
  109. *
  110. * This function is intended to be small and simple because it is
  111. * invoked from crash handler paths.
  112. */
  113. static efi_status_t
  114. query_variable_store_nonblocking(u32 attributes, unsigned long size)
  115. {
  116. efi_status_t status;
  117. u64 storage_size, remaining_size, max_size;
  118. status = efi.query_variable_info_nonblocking(attributes, &storage_size,
  119. &remaining_size,
  120. &max_size);
  121. if (status != EFI_SUCCESS)
  122. return status;
  123. if (remaining_size - size < EFI_MIN_RESERVE)
  124. return EFI_OUT_OF_RESOURCES;
  125. return EFI_SUCCESS;
  126. }
  127. /*
  128. * Some firmware implementations refuse to boot if there's insufficient space
  129. * in the variable store. Ensure that we never use more than a safe limit.
  130. *
  131. * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
  132. * store.
  133. */
  134. efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
  135. bool nonblocking)
  136. {
  137. efi_status_t status;
  138. u64 storage_size, remaining_size, max_size;
  139. if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
  140. return 0;
  141. if (nonblocking)
  142. return query_variable_store_nonblocking(attributes, size);
  143. status = efi.query_variable_info(attributes, &storage_size,
  144. &remaining_size, &max_size);
  145. if (status != EFI_SUCCESS)
  146. return status;
  147. /*
  148. * We account for that by refusing the write if permitting it would
  149. * reduce the available space to under 5KB. This figure was provided by
  150. * Samsung, so should be safe.
  151. */
  152. if ((remaining_size - size < EFI_MIN_RESERVE) &&
  153. !efi_no_storage_paranoia) {
  154. /*
  155. * Triggering garbage collection may require that the firmware
  156. * generate a real EFI_OUT_OF_RESOURCES error. We can force
  157. * that by attempting to use more space than is available.
  158. */
  159. unsigned long dummy_size = remaining_size + 1024;
  160. void *dummy = kzalloc(dummy_size, GFP_KERNEL);
  161. if (!dummy)
  162. return EFI_OUT_OF_RESOURCES;
  163. status = efi.set_variable((efi_char16_t *)efi_dummy_name,
  164. &EFI_DUMMY_GUID,
  165. EFI_VARIABLE_NON_VOLATILE |
  166. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  167. EFI_VARIABLE_RUNTIME_ACCESS,
  168. dummy_size, dummy);
  169. if (status == EFI_SUCCESS) {
  170. /*
  171. * This should have failed, so if it didn't make sure
  172. * that we delete it...
  173. */
  174. efi_delete_dummy_variable();
  175. }
  176. kfree(dummy);
  177. /*
  178. * The runtime code may now have triggered a garbage collection
  179. * run, so check the variable info again
  180. */
  181. status = efi.query_variable_info(attributes, &storage_size,
  182. &remaining_size, &max_size);
  183. if (status != EFI_SUCCESS)
  184. return status;
  185. /*
  186. * There still isn't enough room, so return an error
  187. */
  188. if (remaining_size - size < EFI_MIN_RESERVE)
  189. return EFI_OUT_OF_RESOURCES;
  190. }
  191. return EFI_SUCCESS;
  192. }
  193. EXPORT_SYMBOL_GPL(efi_query_variable_store);
  194. /*
  195. * The UEFI specification makes it clear that the operating system is
  196. * free to do whatever it wants with boot services code after
  197. * ExitBootServices() has been called. Ignoring this recommendation a
  198. * significant bunch of EFI implementations continue calling into boot
  199. * services code (SetVirtualAddressMap). In order to work around such
  200. * buggy implementations we reserve boot services region during EFI
  201. * init and make sure it stays executable. Then, after
  202. * SetVirtualAddressMap(), it is discarded.
  203. *
  204. * However, some boot services regions contain data that is required
  205. * by drivers, so we need to track which memory ranges can never be
  206. * freed. This is done by tagging those regions with the
  207. * EFI_MEMORY_RUNTIME attribute.
  208. *
  209. * Any driver that wants to mark a region as reserved must use
  210. * efi_mem_reserve() which will insert a new EFI memory descriptor
  211. * into efi.memmap (splitting existing regions if necessary) and tag
  212. * it with EFI_MEMORY_RUNTIME.
  213. */
  214. void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)
  215. {
  216. phys_addr_t new_phys, new_size;
  217. struct efi_mem_range mr;
  218. efi_memory_desc_t md;
  219. int num_entries;
  220. void *new;
  221. if (efi_mem_desc_lookup(addr, &md) ||
  222. md.type != EFI_BOOT_SERVICES_DATA) {
  223. pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr);
  224. return;
  225. }
  226. if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) {
  227. pr_err("Region spans EFI memory descriptors, %pa\n", &addr);
  228. return;
  229. }
  230. /* No need to reserve regions that will never be freed. */
  231. if (md.attribute & EFI_MEMORY_RUNTIME)
  232. return;
  233. size += addr % EFI_PAGE_SIZE;
  234. size = round_up(size, EFI_PAGE_SIZE);
  235. addr = round_down(addr, EFI_PAGE_SIZE);
  236. mr.range.start = addr;
  237. mr.range.end = addr + size - 1;
  238. mr.attribute = md.attribute | EFI_MEMORY_RUNTIME;
  239. num_entries = efi_memmap_split_count(&md, &mr.range);
  240. num_entries += efi.memmap.nr_map;
  241. new_size = efi.memmap.desc_size * num_entries;
  242. new_phys = efi_memmap_alloc(num_entries);
  243. if (!new_phys) {
  244. pr_err("Could not allocate boot services memmap\n");
  245. return;
  246. }
  247. new = early_memremap(new_phys, new_size);
  248. if (!new) {
  249. pr_err("Failed to map new boot services memmap\n");
  250. return;
  251. }
  252. efi_memmap_insert(&efi.memmap, new, &mr);
  253. early_memunmap(new, new_size);
  254. efi_memmap_install(new_phys, num_entries);
  255. }
  256. /*
  257. * Helper function for efi_reserve_boot_services() to figure out if we
  258. * can free regions in efi_free_boot_services().
  259. *
  260. * Use this function to ensure we do not free regions owned by somebody
  261. * else. We must only reserve (and then free) regions:
  262. *
  263. * - Not within any part of the kernel
  264. * - Not the BIOS reserved area (E820_TYPE_RESERVED, E820_TYPE_NVS, etc)
  265. */
  266. static bool can_free_region(u64 start, u64 size)
  267. {
  268. if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))
  269. return false;
  270. if (!e820__mapped_all(start, start+size, E820_TYPE_RAM))
  271. return false;
  272. return true;
  273. }
  274. void __init efi_reserve_boot_services(void)
  275. {
  276. efi_memory_desc_t *md;
  277. for_each_efi_memory_desc(md) {
  278. u64 start = md->phys_addr;
  279. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  280. bool already_reserved;
  281. if (md->type != EFI_BOOT_SERVICES_CODE &&
  282. md->type != EFI_BOOT_SERVICES_DATA)
  283. continue;
  284. already_reserved = memblock_is_region_reserved(start, size);
  285. /*
  286. * Because the following memblock_reserve() is paired
  287. * with memblock_free_late() for this region in
  288. * efi_free_boot_services(), we must be extremely
  289. * careful not to reserve, and subsequently free,
  290. * critical regions of memory (like the kernel image) or
  291. * those regions that somebody else has already
  292. * reserved.
  293. *
  294. * A good example of a critical region that must not be
  295. * freed is page zero (first 4Kb of memory), which may
  296. * contain boot services code/data but is marked
  297. * E820_TYPE_RESERVED by trim_bios_range().
  298. */
  299. if (!already_reserved) {
  300. memblock_reserve(start, size);
  301. /*
  302. * If we are the first to reserve the region, no
  303. * one else cares about it. We own it and can
  304. * free it later.
  305. */
  306. if (can_free_region(start, size))
  307. continue;
  308. }
  309. /*
  310. * We don't own the region. We must not free it.
  311. *
  312. * Setting this bit for a boot services region really
  313. * doesn't make sense as far as the firmware is
  314. * concerned, but it does provide us with a way to tag
  315. * those regions that must not be paired with
  316. * memblock_free_late().
  317. */
  318. md->attribute |= EFI_MEMORY_RUNTIME;
  319. }
  320. }
  321. void __init efi_free_boot_services(void)
  322. {
  323. phys_addr_t new_phys, new_size;
  324. efi_memory_desc_t *md;
  325. int num_entries = 0;
  326. void *new, *new_md;
  327. for_each_efi_memory_desc(md) {
  328. unsigned long long start = md->phys_addr;
  329. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  330. size_t rm_size;
  331. if (md->type != EFI_BOOT_SERVICES_CODE &&
  332. md->type != EFI_BOOT_SERVICES_DATA) {
  333. num_entries++;
  334. continue;
  335. }
  336. /* Do not free, someone else owns it: */
  337. if (md->attribute & EFI_MEMORY_RUNTIME) {
  338. num_entries++;
  339. continue;
  340. }
  341. /*
  342. * Nasty quirk: if all sub-1MB memory is used for boot
  343. * services, we can get here without having allocated the
  344. * real mode trampoline. It's too late to hand boot services
  345. * memory back to the memblock allocator, so instead
  346. * try to manually allocate the trampoline if needed.
  347. *
  348. * I've seen this on a Dell XPS 13 9350 with firmware
  349. * 1.4.4 with SGX enabled booting Linux via Fedora 24's
  350. * grub2-efi on a hard disk. (And no, I don't know why
  351. * this happened, but Linux should still try to boot rather
  352. * panicing early.)
  353. */
  354. rm_size = real_mode_size_needed();
  355. if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {
  356. set_real_mode_mem(start, rm_size);
  357. start += rm_size;
  358. size -= rm_size;
  359. }
  360. memblock_free_late(start, size);
  361. }
  362. if (!num_entries)
  363. return;
  364. new_size = efi.memmap.desc_size * num_entries;
  365. new_phys = efi_memmap_alloc(num_entries);
  366. if (!new_phys) {
  367. pr_err("Failed to allocate new EFI memmap\n");
  368. return;
  369. }
  370. new = memremap(new_phys, new_size, MEMREMAP_WB);
  371. if (!new) {
  372. pr_err("Failed to map new EFI memmap\n");
  373. return;
  374. }
  375. /*
  376. * Build a new EFI memmap that excludes any boot services
  377. * regions that are not tagged EFI_MEMORY_RUNTIME, since those
  378. * regions have now been freed.
  379. */
  380. new_md = new;
  381. for_each_efi_memory_desc(md) {
  382. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  383. (md->type == EFI_BOOT_SERVICES_CODE ||
  384. md->type == EFI_BOOT_SERVICES_DATA))
  385. continue;
  386. memcpy(new_md, md, efi.memmap.desc_size);
  387. new_md += efi.memmap.desc_size;
  388. }
  389. memunmap(new);
  390. if (efi_memmap_install(new_phys, num_entries)) {
  391. pr_err("Could not install new EFI memmap\n");
  392. return;
  393. }
  394. }
  395. /*
  396. * A number of config table entries get remapped to virtual addresses
  397. * after entering EFI virtual mode. However, the kexec kernel requires
  398. * their physical addresses therefore we pass them via setup_data and
  399. * correct those entries to their respective physical addresses here.
  400. *
  401. * Currently only handles smbios which is necessary for some firmware
  402. * implementation.
  403. */
  404. int __init efi_reuse_config(u64 tables, int nr_tables)
  405. {
  406. int i, sz, ret = 0;
  407. void *p, *tablep;
  408. struct efi_setup_data *data;
  409. if (!efi_setup)
  410. return 0;
  411. if (!efi_enabled(EFI_64BIT))
  412. return 0;
  413. data = early_memremap(efi_setup, sizeof(*data));
  414. if (!data) {
  415. ret = -ENOMEM;
  416. goto out;
  417. }
  418. if (!data->smbios)
  419. goto out_memremap;
  420. sz = sizeof(efi_config_table_64_t);
  421. p = tablep = early_memremap(tables, nr_tables * sz);
  422. if (!p) {
  423. pr_err("Could not map Configuration table!\n");
  424. ret = -ENOMEM;
  425. goto out_memremap;
  426. }
  427. for (i = 0; i < efi.systab->nr_tables; i++) {
  428. efi_guid_t guid;
  429. guid = ((efi_config_table_64_t *)p)->guid;
  430. if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
  431. ((efi_config_table_64_t *)p)->table = data->smbios;
  432. p += sz;
  433. }
  434. early_memunmap(tablep, nr_tables * sz);
  435. out_memremap:
  436. early_memunmap(data, sizeof(*data));
  437. out:
  438. return ret;
  439. }
  440. static const struct dmi_system_id sgi_uv1_dmi[] = {
  441. { NULL, "SGI UV1",
  442. { DMI_MATCH(DMI_PRODUCT_NAME, "Stoutland Platform"),
  443. DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"),
  444. DMI_MATCH(DMI_BIOS_VENDOR, "SGI.COM"),
  445. }
  446. },
  447. { } /* NULL entry stops DMI scanning */
  448. };
  449. void __init efi_apply_memmap_quirks(void)
  450. {
  451. /*
  452. * Once setup is done earlier, unmap the EFI memory map on mismatched
  453. * firmware/kernel architectures since there is no support for runtime
  454. * services.
  455. */
  456. if (!efi_runtime_supported()) {
  457. pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
  458. efi_memmap_unmap();
  459. }
  460. /* UV2+ BIOS has a fix for this issue. UV1 still needs the quirk. */
  461. if (dmi_check_system(sgi_uv1_dmi))
  462. set_bit(EFI_OLD_MEMMAP, &efi.flags);
  463. }
  464. /*
  465. * For most modern platforms the preferred method of powering off is via
  466. * ACPI. However, there are some that are known to require the use of
  467. * EFI runtime services and for which ACPI does not work at all.
  468. *
  469. * Using EFI is a last resort, to be used only if no other option
  470. * exists.
  471. */
  472. bool efi_reboot_required(void)
  473. {
  474. if (!acpi_gbl_reduced_hardware)
  475. return false;
  476. efi_reboot_quirk_mode = EFI_RESET_WARM;
  477. return true;
  478. }
  479. bool efi_poweroff_required(void)
  480. {
  481. return acpi_gbl_reduced_hardware || acpi_no_s5;
  482. }
  483. #ifdef CONFIG_EFI_CAPSULE_QUIRK_QUARK_CSH
  484. static int qrk_capsule_setup_info(struct capsule_info *cap_info, void **pkbuff,
  485. size_t hdr_bytes)
  486. {
  487. struct quark_security_header *csh = *pkbuff;
  488. /* Only process data block that is larger than the security header */
  489. if (hdr_bytes < sizeof(struct quark_security_header))
  490. return 0;
  491. if (csh->csh_signature != QUARK_CSH_SIGNATURE ||
  492. csh->headersize != QUARK_SECURITY_HEADER_SIZE)
  493. return 1;
  494. /* Only process data block if EFI header is included */
  495. if (hdr_bytes < QUARK_SECURITY_HEADER_SIZE +
  496. sizeof(efi_capsule_header_t))
  497. return 0;
  498. pr_debug("Quark security header detected\n");
  499. if (csh->rsvd_next_header != 0) {
  500. pr_err("multiple Quark security headers not supported\n");
  501. return -EINVAL;
  502. }
  503. *pkbuff += csh->headersize;
  504. cap_info->total_size = csh->headersize;
  505. /*
  506. * Update the first page pointer to skip over the CSH header.
  507. */
  508. cap_info->phys[0] += csh->headersize;
  509. /*
  510. * cap_info->capsule should point at a virtual mapping of the entire
  511. * capsule, starting at the capsule header. Our image has the Quark
  512. * security header prepended, so we cannot rely on the default vmap()
  513. * mapping created by the generic capsule code.
  514. * Given that the Quark firmware does not appear to care about the
  515. * virtual mapping, let's just point cap_info->capsule at our copy
  516. * of the capsule header.
  517. */
  518. cap_info->capsule = &cap_info->header;
  519. return 1;
  520. }
  521. #define ICPU(family, model, quirk_handler) \
  522. { X86_VENDOR_INTEL, family, model, X86_FEATURE_ANY, \
  523. (unsigned long)&quirk_handler }
  524. static const struct x86_cpu_id efi_capsule_quirk_ids[] = {
  525. ICPU(5, 9, qrk_capsule_setup_info), /* Intel Quark X1000 */
  526. { }
  527. };
  528. int efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
  529. size_t hdr_bytes)
  530. {
  531. int (*quirk_handler)(struct capsule_info *, void **, size_t);
  532. const struct x86_cpu_id *id;
  533. int ret;
  534. if (hdr_bytes < sizeof(efi_capsule_header_t))
  535. return 0;
  536. cap_info->total_size = 0;
  537. id = x86_match_cpu(efi_capsule_quirk_ids);
  538. if (id) {
  539. /*
  540. * The quirk handler is supposed to return
  541. * - a value > 0 if the setup should continue, after advancing
  542. * kbuff as needed
  543. * - 0 if not enough hdr_bytes are available yet
  544. * - a negative error code otherwise
  545. */
  546. quirk_handler = (typeof(quirk_handler))id->driver_data;
  547. ret = quirk_handler(cap_info, &kbuff, hdr_bytes);
  548. if (ret <= 0)
  549. return ret;
  550. }
  551. memcpy(&cap_info->header, kbuff, sizeof(cap_info->header));
  552. cap_info->total_size += cap_info->header.imagesize;
  553. return __efi_capsule_setup_info(cap_info);
  554. }
  555. #endif
  556. /*
  557. * If any access by any efi runtime service causes a page fault, then,
  558. * 1. If it's efi_reset_system(), reboot through BIOS.
  559. * 2. If any other efi runtime service, then
  560. * a. Return error status to the efi caller process.
  561. * b. Disable EFI Runtime Services forever and
  562. * c. Freeze efi_rts_wq and schedule new process.
  563. *
  564. * @return: Returns, if the page fault is not handled. This function
  565. * will never return if the page fault is handled successfully.
  566. */
  567. void efi_recover_from_page_fault(unsigned long phys_addr)
  568. {
  569. if (!IS_ENABLED(CONFIG_X86_64))
  570. return;
  571. /*
  572. * Make sure that an efi runtime service caused the page fault.
  573. * "efi_mm" cannot be used to check if the page fault had occurred
  574. * in the firmware context because efi=old_map doesn't use efi_pgd.
  575. */
  576. if (efi_rts_work.efi_rts_id == NONE)
  577. return;
  578. /*
  579. * Address range 0x0000 - 0x0fff is always mapped in the efi_pgd, so
  580. * page faulting on these addresses isn't expected.
  581. */
  582. if (phys_addr >= 0x0000 && phys_addr <= 0x0fff)
  583. return;
  584. /*
  585. * Print stack trace as it might be useful to know which EFI Runtime
  586. * Service is buggy.
  587. */
  588. WARN(1, FW_BUG "Page fault caused by firmware at PA: 0x%lx\n",
  589. phys_addr);
  590. /*
  591. * Buggy efi_reset_system() is handled differently from other EFI
  592. * Runtime Services as it doesn't use efi_rts_wq. Although,
  593. * native_machine_emergency_restart() says that machine_real_restart()
  594. * could fail, it's better not to compilcate this fault handler
  595. * because this case occurs *very* rarely and hence could be improved
  596. * on a need by basis.
  597. */
  598. if (efi_rts_work.efi_rts_id == RESET_SYSTEM) {
  599. pr_info("efi_reset_system() buggy! Reboot through BIOS\n");
  600. machine_real_restart(MRR_BIOS);
  601. return;
  602. }
  603. /*
  604. * Before calling EFI Runtime Service, the kernel has switched the
  605. * calling process to efi_mm. Hence, switch back to task_mm.
  606. */
  607. arch_efi_call_virt_teardown();
  608. /* Signal error status to the efi caller process */
  609. efi_rts_work.status = EFI_ABORTED;
  610. complete(&efi_rts_work.efi_rts_comp);
  611. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  612. pr_info("Froze efi_rts_wq and disabled EFI Runtime Services\n");
  613. /*
  614. * Call schedule() in an infinite loop, so that any spurious wake ups
  615. * will never run efi_rts_wq again.
  616. */
  617. for (;;) {
  618. set_current_state(TASK_IDLE);
  619. schedule();
  620. }
  621. return;
  622. }