efi.c 30 KB

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