efi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * Common EFI (Extensible Firmware Interface) support functions
  3. * Based on Extensible Firmware Interface Specification version 1.0
  4. *
  5. * Copyright (C) 1999 VA Linux Systems
  6. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  7. * Copyright (C) 1999-2002 Hewlett-Packard Co.
  8. * David Mosberger-Tang <davidm@hpl.hp.com>
  9. * Stephane Eranian <eranian@hpl.hp.com>
  10. * Copyright (C) 2005-2008 Intel Co.
  11. * Fenghua Yu <fenghua.yu@intel.com>
  12. * Bibo Mao <bibo.mao@intel.com>
  13. * Chandramouli Narayanan <mouli@linux.intel.com>
  14. * Huang Ying <ying.huang@intel.com>
  15. *
  16. * Copied from efi_32.c to eliminate the duplicated code between EFI
  17. * 32/64 support code. --ying 2007-10-26
  18. *
  19. * All EFI Runtime Services are not implemented yet as EFI only
  20. * supports physical mode addressing on SoftSDV. This is to be fixed
  21. * in a future version. --drummond 1999-07-20
  22. *
  23. * Implemented EFI runtime services and virtual mode calls. --davidm
  24. *
  25. * Goutham Rao: <goutham.rao@intel.com>
  26. * Skip non-WB memory and ignore empty memory ranges.
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/kernel.h>
  30. #include <linux/init.h>
  31. #include <linux/efi.h>
  32. #include <linux/efi-bgrt.h>
  33. #include <linux/export.h>
  34. #include <linux/bootmem.h>
  35. #include <linux/memblock.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/time.h>
  39. #include <linux/io.h>
  40. #include <linux/reboot.h>
  41. #include <linux/bcd.h>
  42. #include <linux/ucs2_string.h>
  43. #include <asm/setup.h>
  44. #include <asm/efi.h>
  45. #include <asm/time.h>
  46. #include <asm/cacheflush.h>
  47. #include <asm/tlbflush.h>
  48. #include <asm/x86_init.h>
  49. #define EFI_DEBUG 1
  50. /*
  51. * There's some additional metadata associated with each
  52. * variable. Intel's reference implementation is 60 bytes - bump that
  53. * to account for potential alignment constraints
  54. */
  55. #define VAR_METADATA_SIZE 64
  56. struct efi __read_mostly efi = {
  57. .mps = EFI_INVALID_TABLE_ADDR,
  58. .acpi = EFI_INVALID_TABLE_ADDR,
  59. .acpi20 = EFI_INVALID_TABLE_ADDR,
  60. .smbios = EFI_INVALID_TABLE_ADDR,
  61. .sal_systab = EFI_INVALID_TABLE_ADDR,
  62. .boot_info = EFI_INVALID_TABLE_ADDR,
  63. .hcdp = EFI_INVALID_TABLE_ADDR,
  64. .uga = EFI_INVALID_TABLE_ADDR,
  65. .uv_systab = EFI_INVALID_TABLE_ADDR,
  66. };
  67. EXPORT_SYMBOL(efi);
  68. struct efi_memory_map memmap;
  69. static struct efi efi_phys __initdata;
  70. static efi_system_table_t efi_systab __initdata;
  71. static u64 efi_var_store_size;
  72. static u64 efi_var_remaining_size;
  73. static u64 efi_var_max_var_size;
  74. static u64 boot_used_size;
  75. static u64 boot_var_size;
  76. static u64 active_size;
  77. unsigned long x86_efi_facility;
  78. /*
  79. * Returns 1 if 'facility' is enabled, 0 otherwise.
  80. */
  81. int efi_enabled(int facility)
  82. {
  83. return test_bit(facility, &x86_efi_facility) != 0;
  84. }
  85. EXPORT_SYMBOL(efi_enabled);
  86. static bool __initdata disable_runtime = false;
  87. static int __init setup_noefi(char *arg)
  88. {
  89. disable_runtime = true;
  90. return 0;
  91. }
  92. early_param("noefi", setup_noefi);
  93. int add_efi_memmap;
  94. EXPORT_SYMBOL(add_efi_memmap);
  95. static int __init setup_add_efi_memmap(char *arg)
  96. {
  97. add_efi_memmap = 1;
  98. return 0;
  99. }
  100. early_param("add_efi_memmap", setup_add_efi_memmap);
  101. static bool efi_no_storage_paranoia;
  102. static int __init setup_storage_paranoia(char *arg)
  103. {
  104. efi_no_storage_paranoia = true;
  105. return 0;
  106. }
  107. early_param("efi_no_storage_paranoia", setup_storage_paranoia);
  108. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  109. {
  110. unsigned long flags;
  111. efi_status_t status;
  112. spin_lock_irqsave(&rtc_lock, flags);
  113. status = efi_call_virt2(get_time, tm, tc);
  114. spin_unlock_irqrestore(&rtc_lock, flags);
  115. return status;
  116. }
  117. static efi_status_t virt_efi_set_time(efi_time_t *tm)
  118. {
  119. unsigned long flags;
  120. efi_status_t status;
  121. spin_lock_irqsave(&rtc_lock, flags);
  122. status = efi_call_virt1(set_time, tm);
  123. spin_unlock_irqrestore(&rtc_lock, flags);
  124. return status;
  125. }
  126. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  127. efi_bool_t *pending,
  128. efi_time_t *tm)
  129. {
  130. unsigned long flags;
  131. efi_status_t status;
  132. spin_lock_irqsave(&rtc_lock, flags);
  133. status = efi_call_virt3(get_wakeup_time,
  134. enabled, pending, tm);
  135. spin_unlock_irqrestore(&rtc_lock, flags);
  136. return status;
  137. }
  138. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  139. {
  140. unsigned long flags;
  141. efi_status_t status;
  142. spin_lock_irqsave(&rtc_lock, flags);
  143. status = efi_call_virt2(set_wakeup_time,
  144. enabled, tm);
  145. spin_unlock_irqrestore(&rtc_lock, flags);
  146. return status;
  147. }
  148. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  149. efi_guid_t *vendor,
  150. u32 *attr,
  151. unsigned long *data_size,
  152. void *data)
  153. {
  154. return efi_call_virt5(get_variable,
  155. name, vendor, attr,
  156. data_size, data);
  157. }
  158. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  159. efi_char16_t *name,
  160. efi_guid_t *vendor)
  161. {
  162. efi_status_t status;
  163. static bool finished = false;
  164. static u64 var_size;
  165. status = efi_call_virt3(get_next_variable,
  166. name_size, name, vendor);
  167. if (status == EFI_NOT_FOUND) {
  168. finished = true;
  169. if (var_size < boot_used_size) {
  170. boot_var_size = boot_used_size - var_size;
  171. active_size += boot_var_size;
  172. } else {
  173. printk(KERN_WARNING FW_BUG "efi: Inconsistent initial sizes\n");
  174. }
  175. }
  176. if (boot_used_size && !finished) {
  177. unsigned long size;
  178. u32 attr;
  179. efi_status_t s;
  180. void *tmp;
  181. s = virt_efi_get_variable(name, vendor, &attr, &size, NULL);
  182. if (s != EFI_BUFFER_TOO_SMALL || !size)
  183. return status;
  184. tmp = kmalloc(size, GFP_ATOMIC);
  185. if (!tmp)
  186. return status;
  187. s = virt_efi_get_variable(name, vendor, &attr, &size, tmp);
  188. if (s == EFI_SUCCESS && (attr & EFI_VARIABLE_NON_VOLATILE)) {
  189. var_size += size;
  190. var_size += ucs2_strsize(name, 1024);
  191. active_size += size;
  192. active_size += VAR_METADATA_SIZE;
  193. active_size += ucs2_strsize(name, 1024);
  194. }
  195. kfree(tmp);
  196. }
  197. return status;
  198. }
  199. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  200. efi_guid_t *vendor,
  201. u32 attr,
  202. unsigned long data_size,
  203. void *data)
  204. {
  205. efi_status_t status;
  206. u32 orig_attr = 0;
  207. unsigned long orig_size = 0;
  208. status = virt_efi_get_variable(name, vendor, &orig_attr, &orig_size,
  209. NULL);
  210. if (status != EFI_BUFFER_TOO_SMALL)
  211. orig_size = 0;
  212. status = efi_call_virt5(set_variable,
  213. name, vendor, attr,
  214. data_size, data);
  215. if (status == EFI_SUCCESS) {
  216. if (orig_size) {
  217. active_size -= orig_size;
  218. active_size -= ucs2_strsize(name, 1024);
  219. active_size -= VAR_METADATA_SIZE;
  220. }
  221. if (data_size) {
  222. active_size += data_size;
  223. active_size += ucs2_strsize(name, 1024);
  224. active_size += VAR_METADATA_SIZE;
  225. }
  226. }
  227. return status;
  228. }
  229. static efi_status_t virt_efi_query_variable_info(u32 attr,
  230. u64 *storage_space,
  231. u64 *remaining_space,
  232. u64 *max_variable_size)
  233. {
  234. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  235. return EFI_UNSUPPORTED;
  236. return efi_call_virt4(query_variable_info, attr, storage_space,
  237. remaining_space, max_variable_size);
  238. }
  239. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  240. {
  241. return efi_call_virt1(get_next_high_mono_count, count);
  242. }
  243. static void virt_efi_reset_system(int reset_type,
  244. efi_status_t status,
  245. unsigned long data_size,
  246. efi_char16_t *data)
  247. {
  248. efi_call_virt4(reset_system, reset_type, status,
  249. data_size, data);
  250. }
  251. static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
  252. unsigned long count,
  253. unsigned long sg_list)
  254. {
  255. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  256. return EFI_UNSUPPORTED;
  257. return efi_call_virt3(update_capsule, capsules, count, sg_list);
  258. }
  259. static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
  260. unsigned long count,
  261. u64 *max_size,
  262. int *reset_type)
  263. {
  264. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  265. return EFI_UNSUPPORTED;
  266. return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
  267. reset_type);
  268. }
  269. static efi_status_t __init phys_efi_set_virtual_address_map(
  270. unsigned long memory_map_size,
  271. unsigned long descriptor_size,
  272. u32 descriptor_version,
  273. efi_memory_desc_t *virtual_map)
  274. {
  275. efi_status_t status;
  276. efi_call_phys_prelog();
  277. status = efi_call_phys4(efi_phys.set_virtual_address_map,
  278. memory_map_size, descriptor_size,
  279. descriptor_version, virtual_map);
  280. efi_call_phys_epilog();
  281. return status;
  282. }
  283. static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
  284. efi_time_cap_t *tc)
  285. {
  286. unsigned long flags;
  287. efi_status_t status;
  288. spin_lock_irqsave(&rtc_lock, flags);
  289. efi_call_phys_prelog();
  290. status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
  291. virt_to_phys(tc));
  292. efi_call_phys_epilog();
  293. spin_unlock_irqrestore(&rtc_lock, flags);
  294. return status;
  295. }
  296. int efi_set_rtc_mmss(unsigned long nowtime)
  297. {
  298. int real_seconds, real_minutes;
  299. efi_status_t status;
  300. efi_time_t eft;
  301. efi_time_cap_t cap;
  302. status = efi.get_time(&eft, &cap);
  303. if (status != EFI_SUCCESS) {
  304. pr_err("Oops: efitime: can't read time!\n");
  305. return -1;
  306. }
  307. real_seconds = nowtime % 60;
  308. real_minutes = nowtime / 60;
  309. if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
  310. real_minutes += 30;
  311. real_minutes %= 60;
  312. eft.minute = real_minutes;
  313. eft.second = real_seconds;
  314. status = efi.set_time(&eft);
  315. if (status != EFI_SUCCESS) {
  316. pr_err("Oops: efitime: can't write time!\n");
  317. return -1;
  318. }
  319. return 0;
  320. }
  321. unsigned long efi_get_time(void)
  322. {
  323. efi_status_t status;
  324. efi_time_t eft;
  325. efi_time_cap_t cap;
  326. status = efi.get_time(&eft, &cap);
  327. if (status != EFI_SUCCESS)
  328. pr_err("Oops: efitime: can't read time!\n");
  329. return mktime(eft.year, eft.month, eft.day, eft.hour,
  330. eft.minute, eft.second);
  331. }
  332. /*
  333. * Tell the kernel about the EFI memory map. This might include
  334. * more than the max 128 entries that can fit in the e820 legacy
  335. * (zeropage) memory map.
  336. */
  337. static void __init do_add_efi_memmap(void)
  338. {
  339. void *p;
  340. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  341. efi_memory_desc_t *md = p;
  342. unsigned long long start = md->phys_addr;
  343. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  344. int e820_type;
  345. switch (md->type) {
  346. case EFI_LOADER_CODE:
  347. case EFI_LOADER_DATA:
  348. case EFI_BOOT_SERVICES_CODE:
  349. case EFI_BOOT_SERVICES_DATA:
  350. case EFI_CONVENTIONAL_MEMORY:
  351. if (md->attribute & EFI_MEMORY_WB)
  352. e820_type = E820_RAM;
  353. else
  354. e820_type = E820_RESERVED;
  355. break;
  356. case EFI_ACPI_RECLAIM_MEMORY:
  357. e820_type = E820_ACPI;
  358. break;
  359. case EFI_ACPI_MEMORY_NVS:
  360. e820_type = E820_NVS;
  361. break;
  362. case EFI_UNUSABLE_MEMORY:
  363. e820_type = E820_UNUSABLE;
  364. break;
  365. default:
  366. /*
  367. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  368. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  369. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  370. */
  371. e820_type = E820_RESERVED;
  372. break;
  373. }
  374. e820_add_region(start, size, e820_type);
  375. }
  376. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  377. }
  378. int __init efi_memblock_x86_reserve_range(void)
  379. {
  380. unsigned long pmap;
  381. #ifdef CONFIG_X86_32
  382. /* Can't handle data above 4GB at this time */
  383. if (boot_params.efi_info.efi_memmap_hi) {
  384. pr_err("Memory map is above 4GB, disabling EFI.\n");
  385. return -EINVAL;
  386. }
  387. pmap = boot_params.efi_info.efi_memmap;
  388. #else
  389. pmap = (boot_params.efi_info.efi_memmap |
  390. ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
  391. #endif
  392. memmap.phys_map = (void *)pmap;
  393. memmap.nr_map = boot_params.efi_info.efi_memmap_size /
  394. boot_params.efi_info.efi_memdesc_size;
  395. memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
  396. memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
  397. memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
  398. return 0;
  399. }
  400. #if EFI_DEBUG
  401. static void __init print_efi_memmap(void)
  402. {
  403. efi_memory_desc_t *md;
  404. void *p;
  405. int i;
  406. for (p = memmap.map, i = 0;
  407. p < memmap.map_end;
  408. p += memmap.desc_size, i++) {
  409. md = p;
  410. pr_info("mem%02u: type=%u, attr=0x%llx, "
  411. "range=[0x%016llx-0x%016llx) (%lluMB)\n",
  412. i, md->type, md->attribute, md->phys_addr,
  413. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  414. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  415. }
  416. }
  417. #endif /* EFI_DEBUG */
  418. void __init efi_reserve_boot_services(void)
  419. {
  420. void *p;
  421. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  422. efi_memory_desc_t *md = p;
  423. u64 start = md->phys_addr;
  424. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  425. if (md->type != EFI_BOOT_SERVICES_CODE &&
  426. md->type != EFI_BOOT_SERVICES_DATA)
  427. continue;
  428. /* Only reserve where possible:
  429. * - Not within any already allocated areas
  430. * - Not over any memory area (really needed, if above?)
  431. * - Not within any part of the kernel
  432. * - Not the bios reserved area
  433. */
  434. if ((start+size >= __pa_symbol(_text)
  435. && start <= __pa_symbol(_end)) ||
  436. !e820_all_mapped(start, start+size, E820_RAM) ||
  437. memblock_is_region_reserved(start, size)) {
  438. /* Could not reserve, skip it */
  439. md->num_pages = 0;
  440. memblock_dbg("Could not reserve boot range "
  441. "[0x%010llx-0x%010llx]\n",
  442. start, start+size-1);
  443. } else
  444. memblock_reserve(start, size);
  445. }
  446. }
  447. void __init efi_unmap_memmap(void)
  448. {
  449. clear_bit(EFI_MEMMAP, &x86_efi_facility);
  450. if (memmap.map) {
  451. early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
  452. memmap.map = NULL;
  453. }
  454. }
  455. void __init efi_free_boot_services(void)
  456. {
  457. void *p;
  458. if (!efi_is_native())
  459. return;
  460. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  461. efi_memory_desc_t *md = p;
  462. unsigned long long start = md->phys_addr;
  463. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  464. if (md->type != EFI_BOOT_SERVICES_CODE &&
  465. md->type != EFI_BOOT_SERVICES_DATA)
  466. continue;
  467. /* Could not reserve boot area */
  468. if (!size)
  469. continue;
  470. free_bootmem_late(start, size);
  471. }
  472. efi_unmap_memmap();
  473. }
  474. static int __init efi_systab_init(void *phys)
  475. {
  476. if (efi_enabled(EFI_64BIT)) {
  477. efi_system_table_64_t *systab64;
  478. u64 tmp = 0;
  479. systab64 = early_ioremap((unsigned long)phys,
  480. sizeof(*systab64));
  481. if (systab64 == NULL) {
  482. pr_err("Couldn't map the system table!\n");
  483. return -ENOMEM;
  484. }
  485. efi_systab.hdr = systab64->hdr;
  486. efi_systab.fw_vendor = systab64->fw_vendor;
  487. tmp |= systab64->fw_vendor;
  488. efi_systab.fw_revision = systab64->fw_revision;
  489. efi_systab.con_in_handle = systab64->con_in_handle;
  490. tmp |= systab64->con_in_handle;
  491. efi_systab.con_in = systab64->con_in;
  492. tmp |= systab64->con_in;
  493. efi_systab.con_out_handle = systab64->con_out_handle;
  494. tmp |= systab64->con_out_handle;
  495. efi_systab.con_out = systab64->con_out;
  496. tmp |= systab64->con_out;
  497. efi_systab.stderr_handle = systab64->stderr_handle;
  498. tmp |= systab64->stderr_handle;
  499. efi_systab.stderr = systab64->stderr;
  500. tmp |= systab64->stderr;
  501. efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
  502. tmp |= systab64->runtime;
  503. efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
  504. tmp |= systab64->boottime;
  505. efi_systab.nr_tables = systab64->nr_tables;
  506. efi_systab.tables = systab64->tables;
  507. tmp |= systab64->tables;
  508. early_iounmap(systab64, sizeof(*systab64));
  509. #ifdef CONFIG_X86_32
  510. if (tmp >> 32) {
  511. pr_err("EFI data located above 4GB, disabling EFI.\n");
  512. return -EINVAL;
  513. }
  514. #endif
  515. } else {
  516. efi_system_table_32_t *systab32;
  517. systab32 = early_ioremap((unsigned long)phys,
  518. sizeof(*systab32));
  519. if (systab32 == NULL) {
  520. pr_err("Couldn't map the system table!\n");
  521. return -ENOMEM;
  522. }
  523. efi_systab.hdr = systab32->hdr;
  524. efi_systab.fw_vendor = systab32->fw_vendor;
  525. efi_systab.fw_revision = systab32->fw_revision;
  526. efi_systab.con_in_handle = systab32->con_in_handle;
  527. efi_systab.con_in = systab32->con_in;
  528. efi_systab.con_out_handle = systab32->con_out_handle;
  529. efi_systab.con_out = systab32->con_out;
  530. efi_systab.stderr_handle = systab32->stderr_handle;
  531. efi_systab.stderr = systab32->stderr;
  532. efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
  533. efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
  534. efi_systab.nr_tables = systab32->nr_tables;
  535. efi_systab.tables = systab32->tables;
  536. early_iounmap(systab32, sizeof(*systab32));
  537. }
  538. efi.systab = &efi_systab;
  539. /*
  540. * Verify the EFI Table
  541. */
  542. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  543. pr_err("System table signature incorrect!\n");
  544. return -EINVAL;
  545. }
  546. if ((efi.systab->hdr.revision >> 16) == 0)
  547. pr_err("Warning: System table version "
  548. "%d.%02d, expected 1.00 or greater!\n",
  549. efi.systab->hdr.revision >> 16,
  550. efi.systab->hdr.revision & 0xffff);
  551. return 0;
  552. }
  553. static int __init efi_config_init(u64 tables, int nr_tables)
  554. {
  555. void *config_tables, *tablep;
  556. int i, sz;
  557. if (efi_enabled(EFI_64BIT))
  558. sz = sizeof(efi_config_table_64_t);
  559. else
  560. sz = sizeof(efi_config_table_32_t);
  561. /*
  562. * Let's see what config tables the firmware passed to us.
  563. */
  564. config_tables = early_ioremap(tables, nr_tables * sz);
  565. if (config_tables == NULL) {
  566. pr_err("Could not map Configuration table!\n");
  567. return -ENOMEM;
  568. }
  569. tablep = config_tables;
  570. pr_info("");
  571. for (i = 0; i < efi.systab->nr_tables; i++) {
  572. efi_guid_t guid;
  573. unsigned long table;
  574. if (efi_enabled(EFI_64BIT)) {
  575. u64 table64;
  576. guid = ((efi_config_table_64_t *)tablep)->guid;
  577. table64 = ((efi_config_table_64_t *)tablep)->table;
  578. table = table64;
  579. #ifdef CONFIG_X86_32
  580. if (table64 >> 32) {
  581. pr_cont("\n");
  582. pr_err("Table located above 4GB, disabling EFI.\n");
  583. early_iounmap(config_tables,
  584. efi.systab->nr_tables * sz);
  585. return -EINVAL;
  586. }
  587. #endif
  588. } else {
  589. guid = ((efi_config_table_32_t *)tablep)->guid;
  590. table = ((efi_config_table_32_t *)tablep)->table;
  591. }
  592. if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
  593. efi.mps = table;
  594. pr_cont(" MPS=0x%lx ", table);
  595. } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
  596. efi.acpi20 = table;
  597. pr_cont(" ACPI 2.0=0x%lx ", table);
  598. } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
  599. efi.acpi = table;
  600. pr_cont(" ACPI=0x%lx ", table);
  601. } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
  602. efi.smbios = table;
  603. pr_cont(" SMBIOS=0x%lx ", table);
  604. #ifdef CONFIG_X86_UV
  605. } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
  606. efi.uv_systab = table;
  607. pr_cont(" UVsystab=0x%lx ", table);
  608. #endif
  609. } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
  610. efi.hcdp = table;
  611. pr_cont(" HCDP=0x%lx ", table);
  612. } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
  613. efi.uga = table;
  614. pr_cont(" UGA=0x%lx ", table);
  615. }
  616. tablep += sz;
  617. }
  618. pr_cont("\n");
  619. early_iounmap(config_tables, efi.systab->nr_tables * sz);
  620. return 0;
  621. }
  622. static int __init efi_runtime_init(void)
  623. {
  624. efi_runtime_services_t *runtime;
  625. /*
  626. * Check out the runtime services table. We need to map
  627. * the runtime services table so that we can grab the physical
  628. * address of several of the EFI runtime functions, needed to
  629. * set the firmware into virtual mode.
  630. */
  631. runtime = early_ioremap((unsigned long)efi.systab->runtime,
  632. sizeof(efi_runtime_services_t));
  633. if (!runtime) {
  634. pr_err("Could not map the runtime service table!\n");
  635. return -ENOMEM;
  636. }
  637. /*
  638. * We will only need *early* access to the following
  639. * two EFI runtime services before set_virtual_address_map
  640. * is invoked.
  641. */
  642. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  643. efi_phys.set_virtual_address_map =
  644. (efi_set_virtual_address_map_t *)
  645. runtime->set_virtual_address_map;
  646. /*
  647. * Make efi_get_time can be called before entering
  648. * virtual mode.
  649. */
  650. efi.get_time = phys_efi_get_time;
  651. early_iounmap(runtime, sizeof(efi_runtime_services_t));
  652. return 0;
  653. }
  654. static int __init efi_memmap_init(void)
  655. {
  656. /* Map the EFI memory map */
  657. memmap.map = early_ioremap((unsigned long)memmap.phys_map,
  658. memmap.nr_map * memmap.desc_size);
  659. if (memmap.map == NULL) {
  660. pr_err("Could not map the memory map!\n");
  661. return -ENOMEM;
  662. }
  663. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  664. if (add_efi_memmap)
  665. do_add_efi_memmap();
  666. return 0;
  667. }
  668. void __init efi_init(void)
  669. {
  670. efi_char16_t *c16;
  671. char vendor[100] = "unknown";
  672. int i = 0;
  673. void *tmp;
  674. struct setup_data *data;
  675. struct efi_var_bootdata *efi_var_data;
  676. u64 pa_data;
  677. #ifdef CONFIG_X86_32
  678. if (boot_params.efi_info.efi_systab_hi ||
  679. boot_params.efi_info.efi_memmap_hi) {
  680. pr_info("Table located above 4GB, disabling EFI.\n");
  681. return;
  682. }
  683. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  684. #else
  685. efi_phys.systab = (efi_system_table_t *)
  686. (boot_params.efi_info.efi_systab |
  687. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  688. #endif
  689. if (efi_systab_init(efi_phys.systab))
  690. return;
  691. pa_data = boot_params.hdr.setup_data;
  692. while (pa_data) {
  693. data = early_ioremap(pa_data, sizeof(*efi_var_data));
  694. if (data->type == SETUP_EFI_VARS) {
  695. efi_var_data = (struct efi_var_bootdata *)data;
  696. efi_var_store_size = efi_var_data->store_size;
  697. efi_var_remaining_size = efi_var_data->remaining_size;
  698. efi_var_max_var_size = efi_var_data->max_var_size;
  699. }
  700. pa_data = data->next;
  701. early_iounmap(data, sizeof(*efi_var_data));
  702. }
  703. boot_used_size = efi_var_store_size - efi_var_remaining_size;
  704. set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
  705. /*
  706. * Show what we know for posterity
  707. */
  708. c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
  709. if (c16) {
  710. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  711. vendor[i] = *c16++;
  712. vendor[i] = '\0';
  713. } else
  714. pr_err("Could not map the firmware vendor!\n");
  715. early_iounmap(tmp, 2);
  716. pr_info("EFI v%u.%.02u by %s\n",
  717. efi.systab->hdr.revision >> 16,
  718. efi.systab->hdr.revision & 0xffff, vendor);
  719. if (efi_config_init(efi.systab->tables, efi.systab->nr_tables))
  720. return;
  721. set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
  722. /*
  723. * Note: We currently don't support runtime services on an EFI
  724. * that doesn't match the kernel 32/64-bit mode.
  725. */
  726. if (!efi_is_native())
  727. pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  728. else {
  729. if (disable_runtime || efi_runtime_init())
  730. return;
  731. set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
  732. }
  733. if (efi_memmap_init())
  734. return;
  735. set_bit(EFI_MEMMAP, &x86_efi_facility);
  736. #ifdef CONFIG_X86_32
  737. if (efi_is_native()) {
  738. x86_platform.get_wallclock = efi_get_time;
  739. x86_platform.set_wallclock = efi_set_rtc_mmss;
  740. }
  741. #endif
  742. #if EFI_DEBUG
  743. print_efi_memmap();
  744. #endif
  745. }
  746. void __init efi_late_init(void)
  747. {
  748. efi_bgrt_init();
  749. }
  750. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  751. {
  752. u64 addr, npages;
  753. addr = md->virt_addr;
  754. npages = md->num_pages;
  755. memrange_efi_to_native(&addr, &npages);
  756. if (executable)
  757. set_memory_x(addr, npages);
  758. else
  759. set_memory_nx(addr, npages);
  760. }
  761. static void __init runtime_code_page_mkexec(void)
  762. {
  763. efi_memory_desc_t *md;
  764. void *p;
  765. /* Make EFI runtime service code area executable */
  766. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  767. md = p;
  768. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  769. continue;
  770. efi_set_executable(md, true);
  771. }
  772. }
  773. /*
  774. * We can't ioremap data in EFI boot services RAM, because we've already mapped
  775. * it as RAM. So, look it up in the existing EFI memory map instead. Only
  776. * callable after efi_enter_virtual_mode and before efi_free_boot_services.
  777. */
  778. void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
  779. {
  780. void *p;
  781. if (WARN_ON(!memmap.map))
  782. return NULL;
  783. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  784. efi_memory_desc_t *md = p;
  785. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  786. u64 end = md->phys_addr + size;
  787. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  788. md->type != EFI_BOOT_SERVICES_CODE &&
  789. md->type != EFI_BOOT_SERVICES_DATA)
  790. continue;
  791. if (!md->virt_addr)
  792. continue;
  793. if (phys_addr >= md->phys_addr && phys_addr < end) {
  794. phys_addr += md->virt_addr - md->phys_addr;
  795. return (__force void __iomem *)(unsigned long)phys_addr;
  796. }
  797. }
  798. return NULL;
  799. }
  800. void efi_memory_uc(u64 addr, unsigned long size)
  801. {
  802. unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
  803. u64 npages;
  804. npages = round_up(size, page_shift) / page_shift;
  805. memrange_efi_to_native(&addr, &npages);
  806. set_memory_uc(addr, npages);
  807. }
  808. /*
  809. * This function will switch the EFI runtime services to virtual mode.
  810. * Essentially, look through the EFI memmap and map every region that
  811. * has the runtime attribute bit set in its memory descriptor and update
  812. * that memory descriptor with the virtual address obtained from ioremap().
  813. * This enables the runtime services to be called without having to
  814. * thunk back into physical mode for every invocation.
  815. */
  816. void __init efi_enter_virtual_mode(void)
  817. {
  818. efi_memory_desc_t *md, *prev_md = NULL;
  819. efi_status_t status;
  820. unsigned long size;
  821. u64 end, systab, start_pfn, end_pfn;
  822. void *p, *va, *new_memmap = NULL;
  823. int count = 0;
  824. efi.systab = NULL;
  825. /*
  826. * We don't do virtual mode, since we don't do runtime services, on
  827. * non-native EFI
  828. */
  829. if (!efi_is_native()) {
  830. efi_unmap_memmap();
  831. return;
  832. }
  833. /* Merge contiguous regions of the same type and attribute */
  834. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  835. u64 prev_size;
  836. md = p;
  837. if (!prev_md) {
  838. prev_md = md;
  839. continue;
  840. }
  841. if (prev_md->type != md->type ||
  842. prev_md->attribute != md->attribute) {
  843. prev_md = md;
  844. continue;
  845. }
  846. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  847. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  848. prev_md->num_pages += md->num_pages;
  849. md->type = EFI_RESERVED_TYPE;
  850. md->attribute = 0;
  851. continue;
  852. }
  853. prev_md = md;
  854. }
  855. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  856. md = p;
  857. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  858. md->type != EFI_BOOT_SERVICES_CODE &&
  859. md->type != EFI_BOOT_SERVICES_DATA)
  860. continue;
  861. size = md->num_pages << EFI_PAGE_SHIFT;
  862. end = md->phys_addr + size;
  863. start_pfn = PFN_DOWN(md->phys_addr);
  864. end_pfn = PFN_UP(end);
  865. if (pfn_range_is_mapped(start_pfn, end_pfn)) {
  866. va = __va(md->phys_addr);
  867. if (!(md->attribute & EFI_MEMORY_WB))
  868. efi_memory_uc((u64)(unsigned long)va, size);
  869. } else
  870. va = efi_ioremap(md->phys_addr, size,
  871. md->type, md->attribute);
  872. md->virt_addr = (u64) (unsigned long) va;
  873. if (!va) {
  874. pr_err("ioremap of 0x%llX failed!\n",
  875. (unsigned long long)md->phys_addr);
  876. continue;
  877. }
  878. systab = (u64) (unsigned long) efi_phys.systab;
  879. if (md->phys_addr <= systab && systab < end) {
  880. systab += md->virt_addr - md->phys_addr;
  881. efi.systab = (efi_system_table_t *) (unsigned long) systab;
  882. }
  883. new_memmap = krealloc(new_memmap,
  884. (count + 1) * memmap.desc_size,
  885. GFP_KERNEL);
  886. memcpy(new_memmap + (count * memmap.desc_size), md,
  887. memmap.desc_size);
  888. count++;
  889. }
  890. BUG_ON(!efi.systab);
  891. status = phys_efi_set_virtual_address_map(
  892. memmap.desc_size * count,
  893. memmap.desc_size,
  894. memmap.desc_version,
  895. (efi_memory_desc_t *)__pa(new_memmap));
  896. if (status != EFI_SUCCESS) {
  897. pr_alert("Unable to switch EFI into virtual mode "
  898. "(status=%lx)!\n", status);
  899. panic("EFI call to SetVirtualAddressMap() failed!");
  900. }
  901. /*
  902. * Now that EFI is in virtual mode, update the function
  903. * pointers in the runtime service table to the new virtual addresses.
  904. *
  905. * Call EFI services through wrapper functions.
  906. */
  907. efi.runtime_version = efi_systab.hdr.revision;
  908. efi.get_time = virt_efi_get_time;
  909. efi.set_time = virt_efi_set_time;
  910. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  911. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  912. efi.get_variable = virt_efi_get_variable;
  913. efi.get_next_variable = virt_efi_get_next_variable;
  914. efi.set_variable = virt_efi_set_variable;
  915. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  916. efi.reset_system = virt_efi_reset_system;
  917. efi.set_virtual_address_map = NULL;
  918. efi.query_variable_info = virt_efi_query_variable_info;
  919. efi.update_capsule = virt_efi_update_capsule;
  920. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  921. if (__supported_pte_mask & _PAGE_NX)
  922. runtime_code_page_mkexec();
  923. kfree(new_memmap);
  924. }
  925. /*
  926. * Convenience functions to obtain memory types and attributes
  927. */
  928. u32 efi_mem_type(unsigned long phys_addr)
  929. {
  930. efi_memory_desc_t *md;
  931. void *p;
  932. if (!efi_enabled(EFI_MEMMAP))
  933. return 0;
  934. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  935. md = p;
  936. if ((md->phys_addr <= phys_addr) &&
  937. (phys_addr < (md->phys_addr +
  938. (md->num_pages << EFI_PAGE_SHIFT))))
  939. return md->type;
  940. }
  941. return 0;
  942. }
  943. u64 efi_mem_attributes(unsigned long phys_addr)
  944. {
  945. efi_memory_desc_t *md;
  946. void *p;
  947. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  948. md = p;
  949. if ((md->phys_addr <= phys_addr) &&
  950. (phys_addr < (md->phys_addr +
  951. (md->num_pages << EFI_PAGE_SHIFT))))
  952. return md->attribute;
  953. }
  954. return 0;
  955. }
  956. /*
  957. * Some firmware has serious problems when using more than 50% of the EFI
  958. * variable store, i.e. it triggers bugs that can brick machines. Ensure that
  959. * we never use more than this safe limit.
  960. *
  961. * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
  962. * store.
  963. */
  964. efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
  965. {
  966. efi_status_t status;
  967. u64 storage_size, remaining_size, max_size;
  968. status = efi.query_variable_info(attributes, &storage_size,
  969. &remaining_size, &max_size);
  970. if (status != EFI_SUCCESS)
  971. return status;
  972. if (!max_size && remaining_size > size)
  973. printk_once(KERN_ERR FW_BUG "Broken EFI implementation"
  974. " is returning MaxVariableSize=0\n");
  975. /*
  976. * Some firmware implementations refuse to boot if there's insufficient
  977. * space in the variable store. We account for that by refusing the
  978. * write if permitting it would reduce the available space to under
  979. * 50%. However, some firmware won't reclaim variable space until
  980. * after the used (not merely the actively used) space drops below
  981. * a threshold. We can approximate that case with the value calculated
  982. * above. If both the firmware and our calculations indicate that the
  983. * available space would drop below 50%, refuse the write.
  984. */
  985. if (!storage_size || size > remaining_size ||
  986. (max_size && size > max_size))
  987. return EFI_OUT_OF_RESOURCES;
  988. if (!efi_no_storage_paranoia &&
  989. ((active_size + size + VAR_METADATA_SIZE > storage_size / 2) &&
  990. (remaining_size - size < storage_size / 2)))
  991. return EFI_OUT_OF_RESOURCES;
  992. return EFI_SUCCESS;
  993. }
  994. EXPORT_SYMBOL_GPL(efi_query_variable_store);