efi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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(unsigned long nowtime)
  299. {
  300. efi_status_t status;
  301. efi_time_t eft;
  302. efi_time_cap_t cap;
  303. struct rtc_time tm;
  304. status = efi.get_time(&eft, &cap);
  305. if (status != EFI_SUCCESS) {
  306. pr_err("Oops: efitime: can't read time!\n");
  307. return -1;
  308. }
  309. rtc_time_to_tm(nowtime, &tm);
  310. if (!rtc_valid_tm(&tm)) {
  311. eft.year = tm.tm_year + 1900;
  312. eft.month = tm.tm_mon + 1;
  313. eft.day = tm.tm_mday;
  314. eft.minute = tm.tm_min;
  315. eft.second = tm.tm_sec;
  316. eft.nanosecond = 0;
  317. } else {
  318. printk(KERN_ERR
  319. "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
  320. __FUNCTION__, nowtime);
  321. return -1;
  322. }
  323. status = efi.set_time(&eft);
  324. if (status != EFI_SUCCESS) {
  325. pr_err("Oops: efitime: can't write time!\n");
  326. return -1;
  327. }
  328. return 0;
  329. }
  330. unsigned long efi_get_time(void)
  331. {
  332. efi_status_t status;
  333. efi_time_t eft;
  334. efi_time_cap_t cap;
  335. status = efi.get_time(&eft, &cap);
  336. if (status != EFI_SUCCESS)
  337. pr_err("Oops: efitime: can't read time!\n");
  338. return mktime(eft.year, eft.month, eft.day, eft.hour,
  339. eft.minute, eft.second);
  340. }
  341. /*
  342. * Tell the kernel about the EFI memory map. This might include
  343. * more than the max 128 entries that can fit in the e820 legacy
  344. * (zeropage) memory map.
  345. */
  346. static void __init do_add_efi_memmap(void)
  347. {
  348. void *p;
  349. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  350. efi_memory_desc_t *md = p;
  351. unsigned long long start = md->phys_addr;
  352. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  353. int e820_type;
  354. switch (md->type) {
  355. case EFI_LOADER_CODE:
  356. case EFI_LOADER_DATA:
  357. case EFI_BOOT_SERVICES_CODE:
  358. case EFI_BOOT_SERVICES_DATA:
  359. case EFI_CONVENTIONAL_MEMORY:
  360. if (md->attribute & EFI_MEMORY_WB)
  361. e820_type = E820_RAM;
  362. else
  363. e820_type = E820_RESERVED;
  364. break;
  365. case EFI_ACPI_RECLAIM_MEMORY:
  366. e820_type = E820_ACPI;
  367. break;
  368. case EFI_ACPI_MEMORY_NVS:
  369. e820_type = E820_NVS;
  370. break;
  371. case EFI_UNUSABLE_MEMORY:
  372. e820_type = E820_UNUSABLE;
  373. break;
  374. default:
  375. /*
  376. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  377. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  378. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  379. */
  380. e820_type = E820_RESERVED;
  381. break;
  382. }
  383. e820_add_region(start, size, e820_type);
  384. }
  385. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  386. }
  387. int __init efi_memblock_x86_reserve_range(void)
  388. {
  389. struct efi_info *e = &boot_params.efi_info;
  390. unsigned long pmap;
  391. #ifdef CONFIG_X86_32
  392. /* Can't handle data above 4GB at this time */
  393. if (e->efi_memmap_hi) {
  394. pr_err("Memory map is above 4GB, disabling EFI.\n");
  395. return -EINVAL;
  396. }
  397. pmap = e->efi_memmap;
  398. #else
  399. pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
  400. #endif
  401. memmap.phys_map = (void *)pmap;
  402. memmap.nr_map = e->efi_memmap_size /
  403. e->efi_memdesc_size;
  404. memmap.desc_size = e->efi_memdesc_size;
  405. memmap.desc_version = e->efi_memdesc_version;
  406. memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
  407. return 0;
  408. }
  409. #if EFI_DEBUG
  410. static void __init print_efi_memmap(void)
  411. {
  412. efi_memory_desc_t *md;
  413. void *p;
  414. int i;
  415. for (p = memmap.map, i = 0;
  416. p < memmap.map_end;
  417. p += memmap.desc_size, i++) {
  418. md = p;
  419. pr_info("mem%02u: type=%u, attr=0x%llx, "
  420. "range=[0x%016llx-0x%016llx) (%lluMB)\n",
  421. i, md->type, md->attribute, md->phys_addr,
  422. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  423. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  424. }
  425. }
  426. #endif /* EFI_DEBUG */
  427. void __init efi_reserve_boot_services(void)
  428. {
  429. void *p;
  430. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  431. efi_memory_desc_t *md = p;
  432. u64 start = md->phys_addr;
  433. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  434. if (md->type != EFI_BOOT_SERVICES_CODE &&
  435. md->type != EFI_BOOT_SERVICES_DATA)
  436. continue;
  437. /* Only reserve where possible:
  438. * - Not within any already allocated areas
  439. * - Not over any memory area (really needed, if above?)
  440. * - Not within any part of the kernel
  441. * - Not the bios reserved area
  442. */
  443. if ((start+size >= __pa_symbol(_text)
  444. && start <= __pa_symbol(_end)) ||
  445. !e820_all_mapped(start, start+size, E820_RAM) ||
  446. memblock_is_region_reserved(start, size)) {
  447. /* Could not reserve, skip it */
  448. md->num_pages = 0;
  449. memblock_dbg("Could not reserve boot range "
  450. "[0x%010llx-0x%010llx]\n",
  451. start, start+size-1);
  452. } else
  453. memblock_reserve(start, size);
  454. }
  455. }
  456. void __init efi_unmap_memmap(void)
  457. {
  458. clear_bit(EFI_MEMMAP, &x86_efi_facility);
  459. if (memmap.map) {
  460. early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
  461. memmap.map = NULL;
  462. }
  463. }
  464. void __init efi_free_boot_services(void)
  465. {
  466. void *p;
  467. if (!efi_is_native())
  468. return;
  469. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  470. efi_memory_desc_t *md = p;
  471. unsigned long long start = md->phys_addr;
  472. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  473. if (md->type != EFI_BOOT_SERVICES_CODE &&
  474. md->type != EFI_BOOT_SERVICES_DATA)
  475. continue;
  476. /* Could not reserve boot area */
  477. if (!size)
  478. continue;
  479. free_bootmem_late(start, size);
  480. }
  481. efi_unmap_memmap();
  482. }
  483. static int __init efi_systab_init(void *phys)
  484. {
  485. if (efi_enabled(EFI_64BIT)) {
  486. efi_system_table_64_t *systab64;
  487. u64 tmp = 0;
  488. systab64 = early_ioremap((unsigned long)phys,
  489. sizeof(*systab64));
  490. if (systab64 == NULL) {
  491. pr_err("Couldn't map the system table!\n");
  492. return -ENOMEM;
  493. }
  494. efi_systab.hdr = systab64->hdr;
  495. efi_systab.fw_vendor = systab64->fw_vendor;
  496. tmp |= systab64->fw_vendor;
  497. efi_systab.fw_revision = systab64->fw_revision;
  498. efi_systab.con_in_handle = systab64->con_in_handle;
  499. tmp |= systab64->con_in_handle;
  500. efi_systab.con_in = systab64->con_in;
  501. tmp |= systab64->con_in;
  502. efi_systab.con_out_handle = systab64->con_out_handle;
  503. tmp |= systab64->con_out_handle;
  504. efi_systab.con_out = systab64->con_out;
  505. tmp |= systab64->con_out;
  506. efi_systab.stderr_handle = systab64->stderr_handle;
  507. tmp |= systab64->stderr_handle;
  508. efi_systab.stderr = systab64->stderr;
  509. tmp |= systab64->stderr;
  510. efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
  511. tmp |= systab64->runtime;
  512. efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
  513. tmp |= systab64->boottime;
  514. efi_systab.nr_tables = systab64->nr_tables;
  515. efi_systab.tables = systab64->tables;
  516. tmp |= systab64->tables;
  517. early_iounmap(systab64, sizeof(*systab64));
  518. #ifdef CONFIG_X86_32
  519. if (tmp >> 32) {
  520. pr_err("EFI data located above 4GB, disabling EFI.\n");
  521. return -EINVAL;
  522. }
  523. #endif
  524. } else {
  525. efi_system_table_32_t *systab32;
  526. systab32 = early_ioremap((unsigned long)phys,
  527. sizeof(*systab32));
  528. if (systab32 == NULL) {
  529. pr_err("Couldn't map the system table!\n");
  530. return -ENOMEM;
  531. }
  532. efi_systab.hdr = systab32->hdr;
  533. efi_systab.fw_vendor = systab32->fw_vendor;
  534. efi_systab.fw_revision = systab32->fw_revision;
  535. efi_systab.con_in_handle = systab32->con_in_handle;
  536. efi_systab.con_in = systab32->con_in;
  537. efi_systab.con_out_handle = systab32->con_out_handle;
  538. efi_systab.con_out = systab32->con_out;
  539. efi_systab.stderr_handle = systab32->stderr_handle;
  540. efi_systab.stderr = systab32->stderr;
  541. efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
  542. efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
  543. efi_systab.nr_tables = systab32->nr_tables;
  544. efi_systab.tables = systab32->tables;
  545. early_iounmap(systab32, sizeof(*systab32));
  546. }
  547. efi.systab = &efi_systab;
  548. /*
  549. * Verify the EFI Table
  550. */
  551. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  552. pr_err("System table signature incorrect!\n");
  553. return -EINVAL;
  554. }
  555. if ((efi.systab->hdr.revision >> 16) == 0)
  556. pr_err("Warning: System table version "
  557. "%d.%02d, expected 1.00 or greater!\n",
  558. efi.systab->hdr.revision >> 16,
  559. efi.systab->hdr.revision & 0xffff);
  560. return 0;
  561. }
  562. static int __init efi_config_init(u64 tables, int nr_tables)
  563. {
  564. void *config_tables, *tablep;
  565. int i, sz;
  566. if (efi_enabled(EFI_64BIT))
  567. sz = sizeof(efi_config_table_64_t);
  568. else
  569. sz = sizeof(efi_config_table_32_t);
  570. /*
  571. * Let's see what config tables the firmware passed to us.
  572. */
  573. config_tables = early_ioremap(tables, nr_tables * sz);
  574. if (config_tables == NULL) {
  575. pr_err("Could not map Configuration table!\n");
  576. return -ENOMEM;
  577. }
  578. tablep = config_tables;
  579. pr_info("");
  580. for (i = 0; i < efi.systab->nr_tables; i++) {
  581. efi_guid_t guid;
  582. unsigned long table;
  583. if (efi_enabled(EFI_64BIT)) {
  584. u64 table64;
  585. guid = ((efi_config_table_64_t *)tablep)->guid;
  586. table64 = ((efi_config_table_64_t *)tablep)->table;
  587. table = table64;
  588. #ifdef CONFIG_X86_32
  589. if (table64 >> 32) {
  590. pr_cont("\n");
  591. pr_err("Table located above 4GB, disabling EFI.\n");
  592. early_iounmap(config_tables,
  593. efi.systab->nr_tables * sz);
  594. return -EINVAL;
  595. }
  596. #endif
  597. } else {
  598. guid = ((efi_config_table_32_t *)tablep)->guid;
  599. table = ((efi_config_table_32_t *)tablep)->table;
  600. }
  601. if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
  602. efi.mps = table;
  603. pr_cont(" MPS=0x%lx ", table);
  604. } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
  605. efi.acpi20 = table;
  606. pr_cont(" ACPI 2.0=0x%lx ", table);
  607. } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
  608. efi.acpi = table;
  609. pr_cont(" ACPI=0x%lx ", table);
  610. } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
  611. efi.smbios = table;
  612. pr_cont(" SMBIOS=0x%lx ", table);
  613. #ifdef CONFIG_X86_UV
  614. } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
  615. efi.uv_systab = table;
  616. pr_cont(" UVsystab=0x%lx ", table);
  617. #endif
  618. } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
  619. efi.hcdp = table;
  620. pr_cont(" HCDP=0x%lx ", table);
  621. } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
  622. efi.uga = table;
  623. pr_cont(" UGA=0x%lx ", table);
  624. }
  625. tablep += sz;
  626. }
  627. pr_cont("\n");
  628. early_iounmap(config_tables, efi.systab->nr_tables * sz);
  629. return 0;
  630. }
  631. static int __init efi_runtime_init(void)
  632. {
  633. efi_runtime_services_t *runtime;
  634. /*
  635. * Check out the runtime services table. We need to map
  636. * the runtime services table so that we can grab the physical
  637. * address of several of the EFI runtime functions, needed to
  638. * set the firmware into virtual mode.
  639. */
  640. runtime = early_ioremap((unsigned long)efi.systab->runtime,
  641. sizeof(efi_runtime_services_t));
  642. if (!runtime) {
  643. pr_err("Could not map the runtime service table!\n");
  644. return -ENOMEM;
  645. }
  646. /*
  647. * We will only need *early* access to the following
  648. * two EFI runtime services before set_virtual_address_map
  649. * is invoked.
  650. */
  651. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  652. efi_phys.set_virtual_address_map =
  653. (efi_set_virtual_address_map_t *)
  654. runtime->set_virtual_address_map;
  655. /*
  656. * Make efi_get_time can be called before entering
  657. * virtual mode.
  658. */
  659. efi.get_time = phys_efi_get_time;
  660. early_iounmap(runtime, sizeof(efi_runtime_services_t));
  661. return 0;
  662. }
  663. static int __init efi_memmap_init(void)
  664. {
  665. /* Map the EFI memory map */
  666. memmap.map = early_ioremap((unsigned long)memmap.phys_map,
  667. memmap.nr_map * memmap.desc_size);
  668. if (memmap.map == NULL) {
  669. pr_err("Could not map the memory map!\n");
  670. return -ENOMEM;
  671. }
  672. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  673. if (add_efi_memmap)
  674. do_add_efi_memmap();
  675. return 0;
  676. }
  677. void __init efi_init(void)
  678. {
  679. efi_char16_t *c16;
  680. char vendor[100] = "unknown";
  681. int i = 0;
  682. void *tmp;
  683. struct setup_data *data;
  684. struct efi_var_bootdata *efi_var_data;
  685. u64 pa_data;
  686. #ifdef CONFIG_X86_32
  687. if (boot_params.efi_info.efi_systab_hi ||
  688. boot_params.efi_info.efi_memmap_hi) {
  689. pr_info("Table located above 4GB, disabling EFI.\n");
  690. return;
  691. }
  692. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  693. #else
  694. efi_phys.systab = (efi_system_table_t *)
  695. (boot_params.efi_info.efi_systab |
  696. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  697. #endif
  698. if (efi_systab_init(efi_phys.systab))
  699. return;
  700. pa_data = boot_params.hdr.setup_data;
  701. while (pa_data) {
  702. data = early_ioremap(pa_data, sizeof(*efi_var_data));
  703. if (data->type == SETUP_EFI_VARS) {
  704. efi_var_data = (struct efi_var_bootdata *)data;
  705. efi_var_store_size = efi_var_data->store_size;
  706. efi_var_remaining_size = efi_var_data->remaining_size;
  707. efi_var_max_var_size = efi_var_data->max_var_size;
  708. }
  709. pa_data = data->next;
  710. early_iounmap(data, sizeof(*efi_var_data));
  711. }
  712. boot_used_size = efi_var_store_size - efi_var_remaining_size;
  713. set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
  714. /*
  715. * Show what we know for posterity
  716. */
  717. c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
  718. if (c16) {
  719. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  720. vendor[i] = *c16++;
  721. vendor[i] = '\0';
  722. } else
  723. pr_err("Could not map the firmware vendor!\n");
  724. early_iounmap(tmp, 2);
  725. pr_info("EFI v%u.%.02u by %s\n",
  726. efi.systab->hdr.revision >> 16,
  727. efi.systab->hdr.revision & 0xffff, vendor);
  728. if (efi_config_init(efi.systab->tables, efi.systab->nr_tables))
  729. return;
  730. set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
  731. /*
  732. * Note: We currently don't support runtime services on an EFI
  733. * that doesn't match the kernel 32/64-bit mode.
  734. */
  735. if (!efi_is_native())
  736. pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  737. else {
  738. if (disable_runtime || efi_runtime_init())
  739. return;
  740. set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
  741. }
  742. if (efi_memmap_init())
  743. return;
  744. set_bit(EFI_MEMMAP, &x86_efi_facility);
  745. #ifdef CONFIG_X86_32
  746. if (efi_is_native()) {
  747. x86_platform.get_wallclock = efi_get_time;
  748. x86_platform.set_wallclock = efi_set_rtc_mmss;
  749. }
  750. #endif
  751. #if EFI_DEBUG
  752. print_efi_memmap();
  753. #endif
  754. }
  755. void __init efi_late_init(void)
  756. {
  757. efi_bgrt_init();
  758. }
  759. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  760. {
  761. u64 addr, npages;
  762. addr = md->virt_addr;
  763. npages = md->num_pages;
  764. memrange_efi_to_native(&addr, &npages);
  765. if (executable)
  766. set_memory_x(addr, npages);
  767. else
  768. set_memory_nx(addr, npages);
  769. }
  770. static void __init runtime_code_page_mkexec(void)
  771. {
  772. efi_memory_desc_t *md;
  773. void *p;
  774. /* Make EFI runtime service code area executable */
  775. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  776. md = p;
  777. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  778. continue;
  779. efi_set_executable(md, true);
  780. }
  781. }
  782. /*
  783. * We can't ioremap data in EFI boot services RAM, because we've already mapped
  784. * it as RAM. So, look it up in the existing EFI memory map instead. Only
  785. * callable after efi_enter_virtual_mode and before efi_free_boot_services.
  786. */
  787. void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
  788. {
  789. void *p;
  790. if (WARN_ON(!memmap.map))
  791. return NULL;
  792. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  793. efi_memory_desc_t *md = p;
  794. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  795. u64 end = md->phys_addr + size;
  796. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  797. md->type != EFI_BOOT_SERVICES_CODE &&
  798. md->type != EFI_BOOT_SERVICES_DATA)
  799. continue;
  800. if (!md->virt_addr)
  801. continue;
  802. if (phys_addr >= md->phys_addr && phys_addr < end) {
  803. phys_addr += md->virt_addr - md->phys_addr;
  804. return (__force void __iomem *)(unsigned long)phys_addr;
  805. }
  806. }
  807. return NULL;
  808. }
  809. void efi_memory_uc(u64 addr, unsigned long size)
  810. {
  811. unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
  812. u64 npages;
  813. npages = round_up(size, page_shift) / page_shift;
  814. memrange_efi_to_native(&addr, &npages);
  815. set_memory_uc(addr, npages);
  816. }
  817. /*
  818. * This function will switch the EFI runtime services to virtual mode.
  819. * Essentially, look through the EFI memmap and map every region that
  820. * has the runtime attribute bit set in its memory descriptor and update
  821. * that memory descriptor with the virtual address obtained from ioremap().
  822. * This enables the runtime services to be called without having to
  823. * thunk back into physical mode for every invocation.
  824. */
  825. void __init efi_enter_virtual_mode(void)
  826. {
  827. efi_memory_desc_t *md, *prev_md = NULL;
  828. efi_status_t status;
  829. unsigned long size;
  830. u64 end, systab, start_pfn, end_pfn;
  831. void *p, *va, *new_memmap = NULL;
  832. int count = 0;
  833. efi.systab = NULL;
  834. /*
  835. * We don't do virtual mode, since we don't do runtime services, on
  836. * non-native EFI
  837. */
  838. if (!efi_is_native()) {
  839. efi_unmap_memmap();
  840. return;
  841. }
  842. /* Merge contiguous regions of the same type and attribute */
  843. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  844. u64 prev_size;
  845. md = p;
  846. if (!prev_md) {
  847. prev_md = md;
  848. continue;
  849. }
  850. if (prev_md->type != md->type ||
  851. prev_md->attribute != md->attribute) {
  852. prev_md = md;
  853. continue;
  854. }
  855. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  856. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  857. prev_md->num_pages += md->num_pages;
  858. md->type = EFI_RESERVED_TYPE;
  859. md->attribute = 0;
  860. continue;
  861. }
  862. prev_md = md;
  863. }
  864. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  865. md = p;
  866. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  867. md->type != EFI_BOOT_SERVICES_CODE &&
  868. md->type != EFI_BOOT_SERVICES_DATA)
  869. continue;
  870. size = md->num_pages << EFI_PAGE_SHIFT;
  871. end = md->phys_addr + size;
  872. start_pfn = PFN_DOWN(md->phys_addr);
  873. end_pfn = PFN_UP(end);
  874. if (pfn_range_is_mapped(start_pfn, end_pfn)) {
  875. va = __va(md->phys_addr);
  876. if (!(md->attribute & EFI_MEMORY_WB))
  877. efi_memory_uc((u64)(unsigned long)va, size);
  878. } else
  879. va = efi_ioremap(md->phys_addr, size,
  880. md->type, md->attribute);
  881. md->virt_addr = (u64) (unsigned long) va;
  882. if (!va) {
  883. pr_err("ioremap of 0x%llX failed!\n",
  884. (unsigned long long)md->phys_addr);
  885. continue;
  886. }
  887. systab = (u64) (unsigned long) efi_phys.systab;
  888. if (md->phys_addr <= systab && systab < end) {
  889. systab += md->virt_addr - md->phys_addr;
  890. efi.systab = (efi_system_table_t *) (unsigned long) systab;
  891. }
  892. new_memmap = krealloc(new_memmap,
  893. (count + 1) * memmap.desc_size,
  894. GFP_KERNEL);
  895. memcpy(new_memmap + (count * memmap.desc_size), md,
  896. memmap.desc_size);
  897. count++;
  898. }
  899. BUG_ON(!efi.systab);
  900. status = phys_efi_set_virtual_address_map(
  901. memmap.desc_size * count,
  902. memmap.desc_size,
  903. memmap.desc_version,
  904. (efi_memory_desc_t *)__pa(new_memmap));
  905. if (status != EFI_SUCCESS) {
  906. pr_alert("Unable to switch EFI into virtual mode "
  907. "(status=%lx)!\n", status);
  908. panic("EFI call to SetVirtualAddressMap() failed!");
  909. }
  910. /*
  911. * Now that EFI is in virtual mode, update the function
  912. * pointers in the runtime service table to the new virtual addresses.
  913. *
  914. * Call EFI services through wrapper functions.
  915. */
  916. efi.runtime_version = efi_systab.hdr.revision;
  917. efi.get_time = virt_efi_get_time;
  918. efi.set_time = virt_efi_set_time;
  919. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  920. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  921. efi.get_variable = virt_efi_get_variable;
  922. efi.get_next_variable = virt_efi_get_next_variable;
  923. efi.set_variable = virt_efi_set_variable;
  924. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  925. efi.reset_system = virt_efi_reset_system;
  926. efi.set_virtual_address_map = NULL;
  927. efi.query_variable_info = virt_efi_query_variable_info;
  928. efi.update_capsule = virt_efi_update_capsule;
  929. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  930. if (__supported_pte_mask & _PAGE_NX)
  931. runtime_code_page_mkexec();
  932. kfree(new_memmap);
  933. }
  934. /*
  935. * Convenience functions to obtain memory types and attributes
  936. */
  937. u32 efi_mem_type(unsigned long phys_addr)
  938. {
  939. efi_memory_desc_t *md;
  940. void *p;
  941. if (!efi_enabled(EFI_MEMMAP))
  942. return 0;
  943. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  944. md = p;
  945. if ((md->phys_addr <= phys_addr) &&
  946. (phys_addr < (md->phys_addr +
  947. (md->num_pages << EFI_PAGE_SHIFT))))
  948. return md->type;
  949. }
  950. return 0;
  951. }
  952. u64 efi_mem_attributes(unsigned long phys_addr)
  953. {
  954. efi_memory_desc_t *md;
  955. void *p;
  956. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  957. md = p;
  958. if ((md->phys_addr <= phys_addr) &&
  959. (phys_addr < (md->phys_addr +
  960. (md->num_pages << EFI_PAGE_SHIFT))))
  961. return md->attribute;
  962. }
  963. return 0;
  964. }
  965. /*
  966. * Some firmware has serious problems when using more than 50% of the EFI
  967. * variable store, i.e. it triggers bugs that can brick machines. Ensure that
  968. * we never use more than this safe limit.
  969. *
  970. * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
  971. * store.
  972. */
  973. efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
  974. {
  975. efi_status_t status;
  976. u64 storage_size, remaining_size, max_size;
  977. status = efi.query_variable_info(attributes, &storage_size,
  978. &remaining_size, &max_size);
  979. if (status != EFI_SUCCESS)
  980. return status;
  981. if (!max_size && remaining_size > size)
  982. printk_once(KERN_ERR FW_BUG "Broken EFI implementation"
  983. " is returning MaxVariableSize=0\n");
  984. /*
  985. * Some firmware implementations refuse to boot if there's insufficient
  986. * space in the variable store. We account for that by refusing the
  987. * write if permitting it would reduce the available space to under
  988. * 50%. However, some firmware won't reclaim variable space until
  989. * after the used (not merely the actively used) space drops below
  990. * a threshold. We can approximate that case with the value calculated
  991. * above. If both the firmware and our calculations indicate that the
  992. * available space would drop below 50%, refuse the write.
  993. */
  994. if (!storage_size || size > remaining_size ||
  995. (max_size && size > max_size))
  996. return EFI_OUT_OF_RESOURCES;
  997. if (!efi_no_storage_paranoia &&
  998. ((active_size + size + VAR_METADATA_SIZE > storage_size / 2) &&
  999. (remaining_size - size < storage_size / 2)))
  1000. return EFI_OUT_OF_RESOURCES;
  1001. return EFI_SUCCESS;
  1002. }
  1003. EXPORT_SYMBOL_GPL(efi_query_variable_store);