efi.c 30 KB

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