efi.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * efi.c - EFI subsystem
  4. *
  5. * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
  6. * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
  7. * Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
  8. *
  9. * This code registers /sys/firmware/efi{,/efivars} when EFI is supported,
  10. * allowing the efivarfs to be mounted or the efivars module to be loaded.
  11. * The existance of /sys/firmware/efi may also be used by userspace to
  12. * determine that the system supports EFI.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/kobject.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/device.h>
  20. #include <linux/efi.h>
  21. #include <linux/of.h>
  22. #include <linux/initrd.h>
  23. #include <linux/io.h>
  24. #include <linux/kexec.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/random.h>
  27. #include <linux/reboot.h>
  28. #include <linux/slab.h>
  29. #include <linux/acpi.h>
  30. #include <linux/ucs2_string.h>
  31. #include <linux/memblock.h>
  32. #include <linux/security.h>
  33. #include <linux/notifier.h>
  34. #include <asm/early_ioremap.h>
  35. struct efi __read_mostly efi = {
  36. .runtime_supported_mask = EFI_RT_SUPPORTED_ALL,
  37. .acpi = EFI_INVALID_TABLE_ADDR,
  38. .acpi20 = EFI_INVALID_TABLE_ADDR,
  39. .smbios = EFI_INVALID_TABLE_ADDR,
  40. .smbios3 = EFI_INVALID_TABLE_ADDR,
  41. .esrt = EFI_INVALID_TABLE_ADDR,
  42. .tpm_log = EFI_INVALID_TABLE_ADDR,
  43. .tpm_final_log = EFI_INVALID_TABLE_ADDR,
  44. .ovmf_debug_log = EFI_INVALID_TABLE_ADDR,
  45. #ifdef CONFIG_LOAD_UEFI_KEYS
  46. .mokvar_table = EFI_INVALID_TABLE_ADDR,
  47. #endif
  48. #ifdef CONFIG_EFI_COCO_SECRET
  49. .coco_secret = EFI_INVALID_TABLE_ADDR,
  50. #endif
  51. #ifdef CONFIG_UNACCEPTED_MEMORY
  52. .unaccepted = EFI_INVALID_TABLE_ADDR,
  53. #endif
  54. };
  55. EXPORT_SYMBOL(efi);
  56. unsigned long __ro_after_init efi_rng_seed = EFI_INVALID_TABLE_ADDR;
  57. static unsigned long __initdata mem_reserve = EFI_INVALID_TABLE_ADDR;
  58. static unsigned long __initdata rt_prop = EFI_INVALID_TABLE_ADDR;
  59. static unsigned long __initdata initrd = EFI_INVALID_TABLE_ADDR;
  60. extern unsigned long primary_display_table;
  61. struct mm_struct efi_mm = {
  62. .mm_mt = MTREE_INIT_EXT(mm_mt, MM_MT_FLAGS, efi_mm.mmap_lock),
  63. .mm_users = ATOMIC_INIT(2),
  64. .mm_count = ATOMIC_INIT(1),
  65. .write_protect_seq = SEQCNT_ZERO(efi_mm.write_protect_seq),
  66. MMAP_LOCK_INITIALIZER(efi_mm)
  67. .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
  68. .mmlist = LIST_HEAD_INIT(efi_mm.mmlist),
  69. .user_ns = &init_user_ns,
  70. #ifdef CONFIG_SCHED_MM_CID
  71. .mm_cid.lock = __RAW_SPIN_LOCK_UNLOCKED(efi_mm.mm_cid.lock),
  72. #endif
  73. .flexible_array = MM_STRUCT_FLEXIBLE_ARRAY_INIT,
  74. };
  75. struct workqueue_struct *efi_rts_wq;
  76. static bool disable_runtime = IS_ENABLED(CONFIG_EFI_DISABLE_RUNTIME);
  77. static int __init setup_noefi(char *arg)
  78. {
  79. disable_runtime = true;
  80. return 0;
  81. }
  82. early_param("noefi", setup_noefi);
  83. bool efi_runtime_disabled(void)
  84. {
  85. return disable_runtime;
  86. }
  87. bool __pure __efi_soft_reserve_enabled(void)
  88. {
  89. return !efi_enabled(EFI_MEM_NO_SOFT_RESERVE);
  90. }
  91. static int __init parse_efi_cmdline(char *str)
  92. {
  93. if (!str) {
  94. pr_warn("need at least one option\n");
  95. return -EINVAL;
  96. }
  97. if (parse_option_str(str, "debug"))
  98. set_bit(EFI_DBG, &efi.flags);
  99. if (parse_option_str(str, "noruntime"))
  100. disable_runtime = true;
  101. if (parse_option_str(str, "runtime"))
  102. disable_runtime = false;
  103. if (parse_option_str(str, "nosoftreserve"))
  104. set_bit(EFI_MEM_NO_SOFT_RESERVE, &efi.flags);
  105. return 0;
  106. }
  107. early_param("efi", parse_efi_cmdline);
  108. struct kobject *efi_kobj;
  109. /*
  110. * Let's not leave out systab information that snuck into
  111. * the efivars driver
  112. * Note, do not add more fields in systab sysfs file as it breaks sysfs
  113. * one value per file rule!
  114. */
  115. static ssize_t systab_show(struct kobject *kobj,
  116. struct kobj_attribute *attr, char *buf)
  117. {
  118. char *str = buf;
  119. if (!kobj || !buf)
  120. return -EINVAL;
  121. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  122. str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
  123. if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  124. str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
  125. /*
  126. * If both SMBIOS and SMBIOS3 entry points are implemented, the
  127. * SMBIOS3 entry point shall be preferred, so we list it first to
  128. * let applications stop parsing after the first match.
  129. */
  130. if (efi.smbios3 != EFI_INVALID_TABLE_ADDR)
  131. str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
  132. if (efi.smbios != EFI_INVALID_TABLE_ADDR)
  133. str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
  134. return str - buf;
  135. }
  136. static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400);
  137. static ssize_t fw_platform_size_show(struct kobject *kobj,
  138. struct kobj_attribute *attr, char *buf)
  139. {
  140. return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32);
  141. }
  142. extern __weak struct kobj_attribute efi_attr_fw_vendor;
  143. extern __weak struct kobj_attribute efi_attr_runtime;
  144. extern __weak struct kobj_attribute efi_attr_config_table;
  145. static struct kobj_attribute efi_attr_fw_platform_size =
  146. __ATTR_RO(fw_platform_size);
  147. static struct attribute *efi_subsys_attrs[] = {
  148. &efi_attr_systab.attr,
  149. &efi_attr_fw_platform_size.attr,
  150. &efi_attr_fw_vendor.attr,
  151. &efi_attr_runtime.attr,
  152. &efi_attr_config_table.attr,
  153. NULL,
  154. };
  155. umode_t __weak efi_attr_is_visible(struct kobject *kobj, struct attribute *attr,
  156. int n)
  157. {
  158. return attr->mode;
  159. }
  160. static const struct attribute_group efi_subsys_attr_group = {
  161. .attrs = efi_subsys_attrs,
  162. .is_visible = efi_attr_is_visible,
  163. };
  164. struct blocking_notifier_head efivar_ops_nh;
  165. EXPORT_SYMBOL_GPL(efivar_ops_nh);
  166. static struct efivars generic_efivars;
  167. static struct efivar_operations generic_ops;
  168. static bool generic_ops_supported(void)
  169. {
  170. unsigned long name_size;
  171. efi_status_t status;
  172. efi_char16_t name;
  173. efi_guid_t guid;
  174. name_size = sizeof(name);
  175. if (!efi.get_next_variable)
  176. return false;
  177. status = efi.get_next_variable(&name_size, &name, &guid);
  178. if (status == EFI_UNSUPPORTED)
  179. return false;
  180. return true;
  181. }
  182. static int generic_ops_register(void)
  183. {
  184. if (!generic_ops_supported())
  185. return 0;
  186. generic_ops.get_variable = efi.get_variable;
  187. generic_ops.get_next_variable = efi.get_next_variable;
  188. generic_ops.query_variable_store = efi_query_variable_store;
  189. generic_ops.query_variable_info = efi.query_variable_info;
  190. if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE)) {
  191. generic_ops.set_variable = efi.set_variable;
  192. generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
  193. }
  194. return efivars_register(&generic_efivars, &generic_ops);
  195. }
  196. static void generic_ops_unregister(void)
  197. {
  198. if (!generic_ops.get_variable)
  199. return;
  200. efivars_unregister(&generic_efivars);
  201. }
  202. void efivars_generic_ops_register(void)
  203. {
  204. generic_ops_register();
  205. }
  206. EXPORT_SYMBOL_GPL(efivars_generic_ops_register);
  207. void efivars_generic_ops_unregister(void)
  208. {
  209. generic_ops_unregister();
  210. }
  211. EXPORT_SYMBOL_GPL(efivars_generic_ops_unregister);
  212. #ifdef CONFIG_EFI_CUSTOM_SSDT_OVERLAYS
  213. #define EFIVAR_SSDT_NAME_MAX 16UL
  214. static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
  215. static int __init efivar_ssdt_setup(char *str)
  216. {
  217. int ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
  218. if (ret)
  219. return ret;
  220. if (strlen(str) < sizeof(efivar_ssdt))
  221. memcpy(efivar_ssdt, str, strlen(str));
  222. else
  223. pr_warn("efivar_ssdt: name too long: %s\n", str);
  224. return 1;
  225. }
  226. __setup("efivar_ssdt=", efivar_ssdt_setup);
  227. static __init int efivar_ssdt_load(void)
  228. {
  229. unsigned long name_size = 256;
  230. efi_char16_t *name = NULL;
  231. efi_status_t status;
  232. efi_guid_t guid;
  233. int ret = 0;
  234. if (!efivar_ssdt[0])
  235. return 0;
  236. name = kzalloc(name_size, GFP_KERNEL);
  237. if (!name)
  238. return -ENOMEM;
  239. for (;;) {
  240. char utf8_name[EFIVAR_SSDT_NAME_MAX];
  241. unsigned long data_size = 0;
  242. void *data;
  243. int limit;
  244. status = efi.get_next_variable(&name_size, name, &guid);
  245. if (status == EFI_NOT_FOUND) {
  246. break;
  247. } else if (status == EFI_BUFFER_TOO_SMALL) {
  248. efi_char16_t *name_tmp =
  249. krealloc(name, name_size, GFP_KERNEL);
  250. if (!name_tmp) {
  251. ret = -ENOMEM;
  252. goto out;
  253. }
  254. name = name_tmp;
  255. continue;
  256. }
  257. limit = min(EFIVAR_SSDT_NAME_MAX, name_size);
  258. ucs2_as_utf8(utf8_name, name, limit - 1);
  259. if (strncmp(utf8_name, efivar_ssdt, limit) != 0)
  260. continue;
  261. pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt, &guid);
  262. status = efi.get_variable(name, &guid, NULL, &data_size, NULL);
  263. if (status != EFI_BUFFER_TOO_SMALL || !data_size) {
  264. ret = -EIO;
  265. goto out;
  266. }
  267. data = kmalloc(data_size, GFP_KERNEL);
  268. if (!data) {
  269. ret = -ENOMEM;
  270. goto out;
  271. }
  272. status = efi.get_variable(name, &guid, NULL, &data_size, data);
  273. if (status == EFI_SUCCESS) {
  274. acpi_status acpi_ret = acpi_load_table(data, NULL);
  275. if (ACPI_FAILURE(acpi_ret)) {
  276. pr_err("efivar_ssdt: failed to load table: %u\n",
  277. acpi_ret);
  278. } else {
  279. /*
  280. * The @data will be in use by ACPI engine,
  281. * do not free it!
  282. */
  283. continue;
  284. }
  285. } else {
  286. pr_err("efivar_ssdt: failed to get var data: 0x%lx\n", status);
  287. }
  288. kfree(data);
  289. }
  290. out:
  291. kfree(name);
  292. return ret;
  293. }
  294. #else
  295. static inline int efivar_ssdt_load(void) { return 0; }
  296. #endif
  297. #ifdef CONFIG_DEBUG_FS
  298. #define EFI_DEBUGFS_MAX_BLOBS 32
  299. static struct debugfs_blob_wrapper debugfs_blob[EFI_DEBUGFS_MAX_BLOBS];
  300. static void __init efi_debugfs_init(void)
  301. {
  302. struct dentry *efi_debugfs;
  303. efi_memory_desc_t *md;
  304. char name[32];
  305. int type_count[EFI_BOOT_SERVICES_DATA + 1] = {};
  306. int i = 0;
  307. efi_debugfs = debugfs_create_dir("efi", NULL);
  308. if (IS_ERR(efi_debugfs))
  309. return;
  310. for_each_efi_memory_desc(md) {
  311. switch (md->type) {
  312. case EFI_BOOT_SERVICES_CODE:
  313. snprintf(name, sizeof(name), "boot_services_code%d",
  314. type_count[md->type]++);
  315. break;
  316. case EFI_BOOT_SERVICES_DATA:
  317. snprintf(name, sizeof(name), "boot_services_data%d",
  318. type_count[md->type]++);
  319. break;
  320. default:
  321. continue;
  322. }
  323. if (i >= EFI_DEBUGFS_MAX_BLOBS) {
  324. pr_warn("More then %d EFI boot service segments, only showing first %d in debugfs\n",
  325. EFI_DEBUGFS_MAX_BLOBS, EFI_DEBUGFS_MAX_BLOBS);
  326. break;
  327. }
  328. debugfs_blob[i].size = md->num_pages << EFI_PAGE_SHIFT;
  329. debugfs_blob[i].data = memremap(md->phys_addr,
  330. debugfs_blob[i].size,
  331. MEMREMAP_WB);
  332. if (!debugfs_blob[i].data)
  333. continue;
  334. debugfs_create_blob(name, 0400, efi_debugfs, &debugfs_blob[i]);
  335. i++;
  336. }
  337. }
  338. #else
  339. static inline void efi_debugfs_init(void) {}
  340. #endif
  341. /*
  342. * We register the efi subsystem with the firmware subsystem and the
  343. * efivars subsystem with the efi subsystem, if the system was booted with
  344. * EFI.
  345. */
  346. static int __init efisubsys_init(void)
  347. {
  348. int error;
  349. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  350. efi.runtime_supported_mask = 0;
  351. if (!efi_enabled(EFI_BOOT))
  352. return 0;
  353. if (efi.runtime_supported_mask) {
  354. /*
  355. * Since we process only one efi_runtime_service() at a time, an
  356. * ordered workqueue (which creates only one execution context)
  357. * should suffice for all our needs.
  358. */
  359. efi_rts_wq = alloc_ordered_workqueue("efi_rts_wq", 0);
  360. if (!efi_rts_wq) {
  361. pr_err("Creating efi_rts_wq failed, EFI runtime services disabled.\n");
  362. clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
  363. efi.runtime_supported_mask = 0;
  364. return 0;
  365. }
  366. }
  367. if (efi_rt_services_supported(EFI_RT_SUPPORTED_TIME_SERVICES))
  368. platform_device_register_simple("rtc-efi", 0, NULL, 0);
  369. /* We register the efi directory at /sys/firmware/efi */
  370. efi_kobj = kobject_create_and_add("efi", firmware_kobj);
  371. if (!efi_kobj) {
  372. pr_err("efi: Firmware registration failed.\n");
  373. error = -ENOMEM;
  374. goto err_destroy_wq;
  375. }
  376. if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE |
  377. EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME)) {
  378. error = generic_ops_register();
  379. if (error)
  380. goto err_put;
  381. error = efivar_ssdt_load();
  382. if (error)
  383. pr_err("efi: failed to load SSDT, error %d.\n", error);
  384. platform_device_register_simple("efivars", 0, NULL, 0);
  385. }
  386. BLOCKING_INIT_NOTIFIER_HEAD(&efivar_ops_nh);
  387. error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
  388. if (error) {
  389. pr_err("efi: Sysfs attribute export failed with error %d.\n",
  390. error);
  391. goto err_unregister;
  392. }
  393. /* and the standard mountpoint for efivarfs */
  394. error = sysfs_create_mount_point(efi_kobj, "efivars");
  395. if (error) {
  396. pr_err("efivars: Subsystem registration failed.\n");
  397. goto err_remove_group;
  398. }
  399. if (efi_enabled(EFI_DBG) && efi_enabled(EFI_PRESERVE_BS_REGIONS))
  400. efi_debugfs_init();
  401. #ifdef CONFIG_EFI_COCO_SECRET
  402. if (efi.coco_secret != EFI_INVALID_TABLE_ADDR)
  403. platform_device_register_simple("efi_secret", 0, NULL, 0);
  404. #endif
  405. if (IS_ENABLED(CONFIG_OVMF_DEBUG_LOG) &&
  406. efi.ovmf_debug_log != EFI_INVALID_TABLE_ADDR)
  407. ovmf_log_probe(efi.ovmf_debug_log);
  408. return 0;
  409. err_remove_group:
  410. sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
  411. err_unregister:
  412. if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE |
  413. EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME))
  414. generic_ops_unregister();
  415. err_put:
  416. kobject_put(efi_kobj);
  417. efi_kobj = NULL;
  418. err_destroy_wq:
  419. if (efi_rts_wq)
  420. destroy_workqueue(efi_rts_wq);
  421. return error;
  422. }
  423. subsys_initcall(efisubsys_init);
  424. void __init efi_find_mirror(void)
  425. {
  426. efi_memory_desc_t *md;
  427. u64 mirror_size = 0, total_size = 0;
  428. if (!efi_enabled(EFI_MEMMAP))
  429. return;
  430. for_each_efi_memory_desc(md) {
  431. unsigned long long start = md->phys_addr;
  432. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  433. total_size += size;
  434. if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
  435. memblock_mark_mirror(start, size);
  436. mirror_size += size;
  437. }
  438. }
  439. if (mirror_size)
  440. pr_info("Memory: %lldM/%lldM mirrored memory\n",
  441. mirror_size>>20, total_size>>20);
  442. }
  443. /*
  444. * Find the efi memory descriptor for a given physical address. Given a
  445. * physical address, determine if it exists within an EFI Memory Map entry,
  446. * and if so, populate the supplied memory descriptor with the appropriate
  447. * data.
  448. */
  449. int __efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
  450. {
  451. efi_memory_desc_t *md;
  452. if (!efi_enabled(EFI_MEMMAP)) {
  453. pr_err_once("EFI_MEMMAP is not enabled.\n");
  454. return -EINVAL;
  455. }
  456. if (!out_md) {
  457. pr_err_once("out_md is null.\n");
  458. return -EINVAL;
  459. }
  460. for_each_efi_memory_desc(md) {
  461. u64 size;
  462. u64 end;
  463. /* skip bogus entries (including empty ones) */
  464. if ((md->phys_addr & (EFI_PAGE_SIZE - 1)) ||
  465. (md->num_pages <= 0) ||
  466. (md->num_pages > (U64_MAX - md->phys_addr) >> EFI_PAGE_SHIFT))
  467. continue;
  468. size = md->num_pages << EFI_PAGE_SHIFT;
  469. end = md->phys_addr + size;
  470. if (phys_addr >= md->phys_addr && phys_addr < end) {
  471. memcpy(out_md, md, sizeof(*out_md));
  472. return 0;
  473. }
  474. }
  475. return -ENOENT;
  476. }
  477. extern int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
  478. __weak __alias(__efi_mem_desc_lookup);
  479. EXPORT_SYMBOL_GPL(efi_mem_desc_lookup);
  480. /*
  481. * Calculate the highest address of an efi memory descriptor.
  482. */
  483. u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
  484. {
  485. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  486. u64 end = md->phys_addr + size;
  487. return end;
  488. }
  489. void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {}
  490. /**
  491. * efi_mem_reserve - Reserve an EFI memory region
  492. * @addr: Physical address to reserve
  493. * @size: Size of reservation
  494. *
  495. * Mark a region as reserved from general kernel allocation and
  496. * prevent it being released by efi_free_boot_services().
  497. *
  498. * This function should be called drivers once they've parsed EFI
  499. * configuration tables to figure out where their data lives, e.g.
  500. * efi_esrt_init().
  501. */
  502. void __init efi_mem_reserve(phys_addr_t addr, u64 size)
  503. {
  504. /* efi_mem_reserve() does not work under Xen */
  505. if (WARN_ON_ONCE(efi_enabled(EFI_PARAVIRT)))
  506. return;
  507. if (!memblock_is_region_reserved(addr, size))
  508. memblock_reserve(addr, size);
  509. /*
  510. * Some architectures (x86) reserve all boot services ranges
  511. * until efi_free_boot_services() because of buggy firmware
  512. * implementations. This means the above memblock_reserve() is
  513. * superfluous on x86 and instead what it needs to do is
  514. * ensure the @start, @size is not freed.
  515. */
  516. efi_arch_mem_reserve(addr, size);
  517. }
  518. static const efi_config_table_type_t common_tables[] __initconst = {
  519. {ACPI_20_TABLE_GUID, &efi.acpi20, "ACPI 2.0" },
  520. {ACPI_TABLE_GUID, &efi.acpi, "ACPI" },
  521. {SMBIOS_TABLE_GUID, &efi.smbios, "SMBIOS" },
  522. {SMBIOS3_TABLE_GUID, &efi.smbios3, "SMBIOS 3.0" },
  523. {EFI_SYSTEM_RESOURCE_TABLE_GUID, &efi.esrt, "ESRT" },
  524. {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, &efi_mem_attr_table, "MEMATTR" },
  525. {LINUX_EFI_RANDOM_SEED_TABLE_GUID, &efi_rng_seed, "RNG" },
  526. {LINUX_EFI_TPM_EVENT_LOG_GUID, &efi.tpm_log, "TPMEventLog" },
  527. {EFI_TCG2_FINAL_EVENTS_TABLE_GUID, &efi.tpm_final_log, "TPMFinalLog" },
  528. {EFI_CC_FINAL_EVENTS_TABLE_GUID, &efi.tpm_final_log, "CCFinalLog" },
  529. {LINUX_EFI_MEMRESERVE_TABLE_GUID, &mem_reserve, "MEMRESERVE" },
  530. {LINUX_EFI_INITRD_MEDIA_GUID, &initrd, "INITRD" },
  531. {EFI_RT_PROPERTIES_TABLE_GUID, &rt_prop, "RTPROP" },
  532. #ifdef CONFIG_OVMF_DEBUG_LOG
  533. {OVMF_MEMORY_LOG_TABLE_GUID, &efi.ovmf_debug_log, "OvmfDebugLog" },
  534. #endif
  535. #ifdef CONFIG_EFI_RCI2_TABLE
  536. {DELLEMC_EFI_RCI2_TABLE_GUID, &rci2_table_phys },
  537. #endif
  538. #ifdef CONFIG_LOAD_UEFI_KEYS
  539. {LINUX_EFI_MOK_VARIABLE_TABLE_GUID, &efi.mokvar_table, "MOKvar" },
  540. #endif
  541. #ifdef CONFIG_EFI_COCO_SECRET
  542. {LINUX_EFI_COCO_SECRET_AREA_GUID, &efi.coco_secret, "CocoSecret" },
  543. #endif
  544. #ifdef CONFIG_UNACCEPTED_MEMORY
  545. {LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID, &efi.unaccepted, "Unaccepted" },
  546. #endif
  547. #ifdef CONFIG_EFI_GENERIC_STUB
  548. {LINUX_EFI_PRIMARY_DISPLAY_TABLE_GUID, &primary_display_table },
  549. #endif
  550. {},
  551. };
  552. static __init int match_config_table(const efi_guid_t *guid,
  553. unsigned long table,
  554. const efi_config_table_type_t *table_types)
  555. {
  556. int i;
  557. for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
  558. if (efi_guidcmp(*guid, table_types[i].guid))
  559. continue;
  560. if (!efi_config_table_is_usable(guid, table)) {
  561. if (table_types[i].name[0])
  562. pr_cont("(%s=0x%lx unusable) ",
  563. table_types[i].name, table);
  564. return 1;
  565. }
  566. *(table_types[i].ptr) = table;
  567. if (table_types[i].name[0])
  568. pr_cont("%s=0x%lx ", table_types[i].name, table);
  569. return 1;
  570. }
  571. return 0;
  572. }
  573. /**
  574. * reserve_unaccepted - Map and reserve unaccepted configuration table
  575. * @unaccepted: Pointer to unaccepted memory table
  576. *
  577. * memblock_add() makes sure that the table is mapped in direct mapping. During
  578. * normal boot it happens automatically because the table is allocated from
  579. * usable memory. But during crashkernel boot only memory specifically reserved
  580. * for crash scenario is mapped. memblock_add() forces the table to be mapped
  581. * in crashkernel case.
  582. *
  583. * Align the range to the nearest page borders. Ranges smaller than page size
  584. * are not going to be mapped.
  585. *
  586. * memblock_reserve() makes sure that future allocations will not touch the
  587. * table.
  588. */
  589. static __init void reserve_unaccepted(struct efi_unaccepted_memory *unaccepted)
  590. {
  591. phys_addr_t start, end;
  592. start = PAGE_ALIGN_DOWN(efi.unaccepted);
  593. end = PAGE_ALIGN(efi.unaccepted + sizeof(*unaccepted) + unaccepted->size);
  594. memblock_add(start, end - start);
  595. memblock_reserve(start, end - start);
  596. }
  597. int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
  598. int count,
  599. const efi_config_table_type_t *arch_tables)
  600. {
  601. const efi_config_table_64_t *tbl64 = (void *)config_tables;
  602. const efi_config_table_32_t *tbl32 = (void *)config_tables;
  603. const efi_guid_t *guid;
  604. unsigned long table;
  605. int i;
  606. pr_info("");
  607. for (i = 0; i < count; i++) {
  608. if (!IS_ENABLED(CONFIG_X86)) {
  609. guid = &config_tables[i].guid;
  610. table = (unsigned long)config_tables[i].table;
  611. } else if (efi_enabled(EFI_64BIT)) {
  612. guid = &tbl64[i].guid;
  613. table = tbl64[i].table;
  614. if (IS_ENABLED(CONFIG_X86_32) &&
  615. tbl64[i].table > U32_MAX) {
  616. pr_cont("\n");
  617. pr_err("Table located above 4GB, disabling EFI.\n");
  618. return -EINVAL;
  619. }
  620. } else {
  621. guid = &tbl32[i].guid;
  622. table = tbl32[i].table;
  623. }
  624. if (!match_config_table(guid, table, common_tables) && arch_tables)
  625. match_config_table(guid, table, arch_tables);
  626. }
  627. pr_cont("\n");
  628. set_bit(EFI_CONFIG_TABLES, &efi.flags);
  629. if (efi_rng_seed != EFI_INVALID_TABLE_ADDR) {
  630. struct linux_efi_random_seed *seed;
  631. u32 size = 0;
  632. seed = early_memremap(efi_rng_seed, sizeof(*seed));
  633. if (seed != NULL) {
  634. size = min_t(u32, seed->size, SZ_1K); // sanity check
  635. early_memunmap(seed, sizeof(*seed));
  636. } else {
  637. pr_err("Could not map UEFI random seed!\n");
  638. }
  639. if (size > 0) {
  640. seed = early_memremap(efi_rng_seed,
  641. sizeof(*seed) + size);
  642. if (seed != NULL) {
  643. add_bootloader_randomness(seed->bits, size);
  644. memzero_explicit(seed->bits, size);
  645. early_memunmap(seed, sizeof(*seed) + size);
  646. } else {
  647. pr_err("Could not map UEFI random seed!\n");
  648. }
  649. }
  650. }
  651. if (!IS_ENABLED(CONFIG_X86_32) && efi_enabled(EFI_MEMMAP))
  652. efi_memattr_init();
  653. efi_tpm_eventlog_init();
  654. if (mem_reserve != EFI_INVALID_TABLE_ADDR) {
  655. unsigned long prsv = mem_reserve;
  656. while (prsv) {
  657. struct linux_efi_memreserve *rsv;
  658. u8 *p;
  659. /*
  660. * Just map a full page: that is what we will get
  661. * anyway, and it permits us to map the entire entry
  662. * before knowing its size.
  663. */
  664. p = early_memremap(ALIGN_DOWN(prsv, PAGE_SIZE),
  665. PAGE_SIZE);
  666. if (p == NULL) {
  667. pr_err("Could not map UEFI memreserve entry!\n");
  668. return -ENOMEM;
  669. }
  670. rsv = (void *)(p + prsv % PAGE_SIZE);
  671. /* reserve the entry itself */
  672. memblock_reserve(prsv,
  673. struct_size(rsv, entry, rsv->size));
  674. for (i = 0; i < atomic_read(&rsv->count); i++) {
  675. memblock_reserve(rsv->entry[i].base,
  676. rsv->entry[i].size);
  677. }
  678. prsv = rsv->next;
  679. early_memunmap(p, PAGE_SIZE);
  680. }
  681. }
  682. if (rt_prop != EFI_INVALID_TABLE_ADDR) {
  683. efi_rt_properties_table_t *tbl;
  684. tbl = early_memremap(rt_prop, sizeof(*tbl));
  685. if (tbl) {
  686. efi.runtime_supported_mask &= tbl->runtime_services_supported;
  687. early_memunmap(tbl, sizeof(*tbl));
  688. }
  689. }
  690. if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) &&
  691. initrd != EFI_INVALID_TABLE_ADDR && phys_initrd_size == 0) {
  692. struct linux_efi_initrd *tbl;
  693. tbl = early_memremap(initrd, sizeof(*tbl));
  694. if (tbl) {
  695. phys_initrd_start = tbl->base;
  696. phys_initrd_size = tbl->size;
  697. tbl->base = tbl->size = 0;
  698. early_memunmap(tbl, sizeof(*tbl));
  699. }
  700. }
  701. if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) &&
  702. efi.unaccepted != EFI_INVALID_TABLE_ADDR) {
  703. struct efi_unaccepted_memory *unaccepted;
  704. unaccepted = early_memremap(efi.unaccepted, sizeof(*unaccepted));
  705. if (unaccepted) {
  706. if (unaccepted->version == 1) {
  707. reserve_unaccepted(unaccepted);
  708. } else {
  709. efi.unaccepted = EFI_INVALID_TABLE_ADDR;
  710. }
  711. early_memunmap(unaccepted, sizeof(*unaccepted));
  712. }
  713. }
  714. return 0;
  715. }
  716. int __init efi_systab_check_header(const efi_table_hdr_t *systab_hdr)
  717. {
  718. if (systab_hdr->signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  719. pr_err("System table signature incorrect!\n");
  720. return -EINVAL;
  721. }
  722. return 0;
  723. }
  724. static const efi_char16_t *__init map_fw_vendor(unsigned long fw_vendor,
  725. size_t size)
  726. {
  727. const efi_char16_t *ret;
  728. ret = early_memremap_ro(fw_vendor, size);
  729. if (!ret)
  730. pr_err("Could not map the firmware vendor!\n");
  731. return ret;
  732. }
  733. static void __init unmap_fw_vendor(const void *fw_vendor, size_t size)
  734. {
  735. early_memunmap((void *)fw_vendor, size);
  736. }
  737. void __init efi_systab_report_header(const efi_table_hdr_t *systab_hdr,
  738. unsigned long fw_vendor)
  739. {
  740. char vendor[100] = "unknown";
  741. const efi_char16_t *c16;
  742. size_t i;
  743. u16 rev;
  744. c16 = map_fw_vendor(fw_vendor, sizeof(vendor) * sizeof(efi_char16_t));
  745. if (c16) {
  746. for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i)
  747. vendor[i] = c16[i];
  748. vendor[i] = '\0';
  749. unmap_fw_vendor(c16, sizeof(vendor) * sizeof(efi_char16_t));
  750. }
  751. rev = (u16)systab_hdr->revision;
  752. pr_info("EFI v%u.%u", systab_hdr->revision >> 16, rev / 10);
  753. rev %= 10;
  754. if (rev)
  755. pr_cont(".%u", rev);
  756. pr_cont(" by %s\n", vendor);
  757. if (IS_ENABLED(CONFIG_X86_64) &&
  758. systab_hdr->revision > EFI_1_10_SYSTEM_TABLE_REVISION &&
  759. !strcmp(vendor, "Apple")) {
  760. pr_info("Apple Mac detected, using EFI v1.10 runtime services only\n");
  761. efi.runtime_version = EFI_1_10_SYSTEM_TABLE_REVISION;
  762. }
  763. }
  764. static __initdata char memory_type_name[][13] = {
  765. "Reserved",
  766. "Loader Code",
  767. "Loader Data",
  768. "Boot Code",
  769. "Boot Data",
  770. "Runtime Code",
  771. "Runtime Data",
  772. "Conventional",
  773. "Unusable",
  774. "ACPI Reclaim",
  775. "ACPI Mem NVS",
  776. "MMIO",
  777. "MMIO Port",
  778. "PAL Code",
  779. "Persistent",
  780. "Unaccepted",
  781. };
  782. char * __init efi_md_typeattr_format(char *buf, size_t size,
  783. const efi_memory_desc_t *md)
  784. {
  785. char *pos;
  786. int type_len;
  787. u64 attr;
  788. pos = buf;
  789. if (md->type >= ARRAY_SIZE(memory_type_name))
  790. type_len = snprintf(pos, size, "[type=%u", md->type);
  791. else
  792. type_len = snprintf(pos, size, "[%-*s",
  793. (int)(sizeof(memory_type_name[0]) - 1),
  794. memory_type_name[md->type]);
  795. if (type_len >= size)
  796. return buf;
  797. pos += type_len;
  798. size -= type_len;
  799. attr = md->attribute;
  800. if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
  801. EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
  802. EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
  803. EFI_MEMORY_NV | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO |
  804. EFI_MEMORY_MORE_RELIABLE | EFI_MEMORY_HOT_PLUGGABLE |
  805. EFI_MEMORY_RUNTIME))
  806. snprintf(pos, size, "|attr=0x%016llx]",
  807. (unsigned long long)attr);
  808. else
  809. snprintf(pos, size,
  810. "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
  811. attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
  812. attr & EFI_MEMORY_HOT_PLUGGABLE ? "HP" : "",
  813. attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
  814. attr & EFI_MEMORY_CPU_CRYPTO ? "CC" : "",
  815. attr & EFI_MEMORY_SP ? "SP" : "",
  816. attr & EFI_MEMORY_NV ? "NV" : "",
  817. attr & EFI_MEMORY_XP ? "XP" : "",
  818. attr & EFI_MEMORY_RP ? "RP" : "",
  819. attr & EFI_MEMORY_WP ? "WP" : "",
  820. attr & EFI_MEMORY_RO ? "RO" : "",
  821. attr & EFI_MEMORY_UCE ? "UCE" : "",
  822. attr & EFI_MEMORY_WB ? "WB" : "",
  823. attr & EFI_MEMORY_WT ? "WT" : "",
  824. attr & EFI_MEMORY_WC ? "WC" : "",
  825. attr & EFI_MEMORY_UC ? "UC" : "");
  826. return buf;
  827. }
  828. /*
  829. * efi_mem_attributes - lookup memmap attributes for physical address
  830. * @phys_addr: the physical address to lookup
  831. *
  832. * Search in the EFI memory map for the region covering
  833. * @phys_addr. Returns the EFI memory attributes if the region
  834. * was found in the memory map, 0 otherwise.
  835. */
  836. u64 efi_mem_attributes(unsigned long phys_addr)
  837. {
  838. efi_memory_desc_t *md;
  839. if (!efi_enabled(EFI_MEMMAP))
  840. return 0;
  841. for_each_efi_memory_desc(md) {
  842. if ((md->phys_addr <= phys_addr) &&
  843. (phys_addr < (md->phys_addr +
  844. (md->num_pages << EFI_PAGE_SHIFT))))
  845. return md->attribute;
  846. }
  847. return 0;
  848. }
  849. /*
  850. * efi_mem_type - lookup memmap type for physical address
  851. * @phys_addr: the physical address to lookup
  852. *
  853. * Search in the EFI memory map for the region covering @phys_addr.
  854. * Returns the EFI memory type if the region was found in the memory
  855. * map, -EINVAL otherwise.
  856. */
  857. int efi_mem_type(unsigned long phys_addr)
  858. {
  859. const efi_memory_desc_t *md;
  860. if (!efi_enabled(EFI_MEMMAP))
  861. return -ENOTSUPP;
  862. for_each_efi_memory_desc(md) {
  863. if ((md->phys_addr <= phys_addr) &&
  864. (phys_addr < (md->phys_addr +
  865. (md->num_pages << EFI_PAGE_SHIFT))))
  866. return md->type;
  867. }
  868. return -EINVAL;
  869. }
  870. int efi_status_to_err(efi_status_t status)
  871. {
  872. int err;
  873. switch (status) {
  874. case EFI_SUCCESS:
  875. err = 0;
  876. break;
  877. case EFI_INVALID_PARAMETER:
  878. err = -EINVAL;
  879. break;
  880. case EFI_OUT_OF_RESOURCES:
  881. err = -ENOSPC;
  882. break;
  883. case EFI_DEVICE_ERROR:
  884. err = -EIO;
  885. break;
  886. case EFI_WRITE_PROTECTED:
  887. err = -EROFS;
  888. break;
  889. case EFI_SECURITY_VIOLATION:
  890. err = -EACCES;
  891. break;
  892. case EFI_NOT_FOUND:
  893. err = -ENOENT;
  894. break;
  895. case EFI_ABORTED:
  896. err = -EINTR;
  897. break;
  898. default:
  899. err = -EINVAL;
  900. }
  901. return err;
  902. }
  903. EXPORT_SYMBOL_GPL(efi_status_to_err);
  904. static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock);
  905. static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
  906. static int __init efi_memreserve_map_root(void)
  907. {
  908. if (mem_reserve == EFI_INVALID_TABLE_ADDR)
  909. return -ENODEV;
  910. efi_memreserve_root = memremap(mem_reserve,
  911. sizeof(*efi_memreserve_root),
  912. MEMREMAP_WB);
  913. if (WARN_ON_ONCE(!efi_memreserve_root))
  914. return -ENOMEM;
  915. return 0;
  916. }
  917. static int efi_mem_reserve_iomem(phys_addr_t addr, u64 size)
  918. {
  919. struct resource *res, *parent;
  920. int ret;
  921. res = kzalloc_obj(struct resource, GFP_ATOMIC);
  922. if (!res)
  923. return -ENOMEM;
  924. res->name = "reserved";
  925. res->flags = IORESOURCE_MEM;
  926. res->start = addr;
  927. res->end = addr + size - 1;
  928. /* we expect a conflict with a 'System RAM' region */
  929. parent = request_resource_conflict(&iomem_resource, res);
  930. ret = parent ? request_resource(parent, res) : 0;
  931. /*
  932. * Given that efi_mem_reserve_iomem() can be called at any
  933. * time, only call memblock_reserve() if the architecture
  934. * keeps the infrastructure around.
  935. */
  936. if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) && !ret)
  937. memblock_reserve(addr, size);
  938. return ret;
  939. }
  940. int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size)
  941. {
  942. struct linux_efi_memreserve *rsv;
  943. unsigned long prsv;
  944. int rc, index;
  945. if (efi_memreserve_root == (void *)ULONG_MAX)
  946. return -ENODEV;
  947. if (!efi_memreserve_root) {
  948. rc = efi_memreserve_map_root();
  949. if (rc)
  950. return rc;
  951. }
  952. /* first try to find a slot in an existing linked list entry */
  953. for (prsv = efi_memreserve_root->next; prsv; ) {
  954. rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
  955. if (!rsv)
  956. return -ENOMEM;
  957. index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size);
  958. if (index < rsv->size) {
  959. rsv->entry[index].base = addr;
  960. rsv->entry[index].size = size;
  961. memunmap(rsv);
  962. return efi_mem_reserve_iomem(addr, size);
  963. }
  964. prsv = rsv->next;
  965. memunmap(rsv);
  966. }
  967. /* no slot found - allocate a new linked list entry */
  968. rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_ATOMIC);
  969. if (!rsv)
  970. return -ENOMEM;
  971. rc = efi_mem_reserve_iomem(__pa(rsv), SZ_4K);
  972. if (rc) {
  973. free_page((unsigned long)rsv);
  974. return rc;
  975. }
  976. /*
  977. * The memremap() call above assumes that a linux_efi_memreserve entry
  978. * never crosses a page boundary, so let's ensure that this remains true
  979. * even when kexec'ing a 4k pages kernel from a >4k pages kernel, by
  980. * using SZ_4K explicitly in the size calculation below.
  981. */
  982. rsv->size = EFI_MEMRESERVE_COUNT(SZ_4K);
  983. atomic_set(&rsv->count, 1);
  984. rsv->entry[0].base = addr;
  985. rsv->entry[0].size = size;
  986. spin_lock(&efi_mem_reserve_persistent_lock);
  987. rsv->next = efi_memreserve_root->next;
  988. efi_memreserve_root->next = __pa(rsv);
  989. spin_unlock(&efi_mem_reserve_persistent_lock);
  990. return efi_mem_reserve_iomem(addr, size);
  991. }
  992. static int __init efi_memreserve_root_init(void)
  993. {
  994. if (efi_memreserve_root)
  995. return 0;
  996. if (efi_memreserve_map_root())
  997. efi_memreserve_root = (void *)ULONG_MAX;
  998. return 0;
  999. }
  1000. early_initcall(efi_memreserve_root_init);
  1001. #ifdef CONFIG_KEXEC
  1002. static int update_efi_random_seed(struct notifier_block *nb,
  1003. unsigned long code, void *unused)
  1004. {
  1005. struct linux_efi_random_seed *seed;
  1006. u32 size = 0;
  1007. if (!kexec_in_progress)
  1008. return NOTIFY_DONE;
  1009. seed = memremap(efi_rng_seed, sizeof(*seed), MEMREMAP_WB);
  1010. if (seed != NULL) {
  1011. size = min(seed->size, EFI_RANDOM_SEED_SIZE);
  1012. memunmap(seed);
  1013. } else {
  1014. pr_err("Could not map UEFI random seed!\n");
  1015. }
  1016. if (size > 0) {
  1017. seed = memremap(efi_rng_seed, sizeof(*seed) + size,
  1018. MEMREMAP_WB);
  1019. if (seed != NULL) {
  1020. seed->size = size;
  1021. get_random_bytes(seed->bits, seed->size);
  1022. memunmap(seed);
  1023. } else {
  1024. pr_err("Could not map UEFI random seed!\n");
  1025. }
  1026. }
  1027. return NOTIFY_DONE;
  1028. }
  1029. static struct notifier_block efi_random_seed_nb = {
  1030. .notifier_call = update_efi_random_seed,
  1031. };
  1032. static int __init register_update_efi_random_seed(void)
  1033. {
  1034. if (efi_rng_seed == EFI_INVALID_TABLE_ADDR)
  1035. return 0;
  1036. return register_reboot_notifier(&efi_random_seed_nb);
  1037. }
  1038. late_initcall(register_update_efi_random_seed);
  1039. #endif