quirks.c 22 KB

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