efi_boottime.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. /*
  2. * EFI application boot time services
  3. *
  4. * Copyright (c) 2016 Alexander Graf
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <efi_loader.h>
  10. #include <malloc.h>
  11. #include <asm/global_data.h>
  12. #include <libfdt_env.h>
  13. #include <u-boot/crc.h>
  14. #include <bootm.h>
  15. #include <inttypes.h>
  16. #include <watchdog.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /* This list contains all the EFI objects our payload has access to */
  19. LIST_HEAD(efi_obj_list);
  20. /*
  21. * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
  22. * we need to do trickery with caches. Since we don't want to break the EFI
  23. * aware boot path, only apply hacks when loading exiting directly (breaking
  24. * direct Linux EFI booting along the way - oh well).
  25. */
  26. static bool efi_is_direct_boot = true;
  27. /*
  28. * EFI can pass arbitrary additional "tables" containing vendor specific
  29. * information to the payload. One such table is the FDT table which contains
  30. * a pointer to a flattened device tree blob.
  31. *
  32. * In most cases we want to pass an FDT to the payload, so reserve one slot of
  33. * config table space for it. The pointer gets populated by do_bootefi_exec().
  34. */
  35. static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
  36. #ifdef CONFIG_ARM
  37. /*
  38. * The "gd" pointer lives in a register on ARM and AArch64 that we declare
  39. * fixed when compiling U-Boot. However, the payload does not know about that
  40. * restriction so we need to manually swap its and our view of that register on
  41. * EFI callback entry/exit.
  42. */
  43. static volatile void *efi_gd, *app_gd;
  44. #endif
  45. /* Called from do_bootefi_exec() */
  46. void efi_save_gd(void)
  47. {
  48. #ifdef CONFIG_ARM
  49. efi_gd = gd;
  50. #endif
  51. }
  52. /* Called on every callback entry */
  53. void efi_restore_gd(void)
  54. {
  55. #ifdef CONFIG_ARM
  56. /* Only restore if we're already in EFI context */
  57. if (!efi_gd)
  58. return;
  59. if (gd != efi_gd)
  60. app_gd = gd;
  61. gd = efi_gd;
  62. #endif
  63. }
  64. /* Called on every callback exit */
  65. efi_status_t efi_exit_func(efi_status_t ret)
  66. {
  67. #ifdef CONFIG_ARM
  68. gd = app_gd;
  69. #endif
  70. return ret;
  71. }
  72. /* Low 32 bit */
  73. #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
  74. /* High 32 bit */
  75. #define EFI_HIGH32(a) (a >> 32)
  76. /*
  77. * 64bit division by 10 implemented as multiplication by 1 / 10
  78. *
  79. * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
  80. */
  81. #define EFI_TENTH 0x199999999999999A
  82. static u64 efi_div10(u64 a)
  83. {
  84. u64 prod;
  85. u64 rem;
  86. u64 ret;
  87. ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
  88. prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
  89. rem = EFI_LOW32(prod);
  90. ret += EFI_HIGH32(prod);
  91. prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
  92. rem += EFI_LOW32(prod);
  93. ret += EFI_HIGH32(prod);
  94. prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
  95. rem += EFI_HIGH32(prod);
  96. ret += EFI_HIGH32(rem);
  97. /* Round to nearest integer */
  98. if (rem >= (1 << 31))
  99. ++ret;
  100. return ret;
  101. }
  102. void efi_signal_event(struct efi_event *event)
  103. {
  104. if (event->signaled)
  105. return;
  106. event->signaled = 1;
  107. if (event->type & EVT_NOTIFY_SIGNAL) {
  108. EFI_EXIT(EFI_SUCCESS);
  109. event->notify_function(event, event->notify_context);
  110. EFI_ENTRY("returning from notification function");
  111. }
  112. }
  113. static efi_status_t efi_unsupported(const char *funcname)
  114. {
  115. debug("EFI: App called into unimplemented function %s\n", funcname);
  116. return EFI_EXIT(EFI_UNSUPPORTED);
  117. }
  118. static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
  119. {
  120. EFI_ENTRY("0x%zx", new_tpl);
  121. return EFI_EXIT(0);
  122. }
  123. static void EFIAPI efi_restore_tpl(UINTN old_tpl)
  124. {
  125. EFI_ENTRY("0x%zx", old_tpl);
  126. efi_unsupported(__func__);
  127. }
  128. static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
  129. unsigned long pages,
  130. uint64_t *memory)
  131. {
  132. efi_status_t r;
  133. EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
  134. r = efi_allocate_pages(type, memory_type, pages, memory);
  135. return EFI_EXIT(r);
  136. }
  137. static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
  138. unsigned long pages)
  139. {
  140. efi_status_t r;
  141. EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
  142. r = efi_free_pages(memory, pages);
  143. return EFI_EXIT(r);
  144. }
  145. static efi_status_t EFIAPI efi_get_memory_map_ext(
  146. unsigned long *memory_map_size,
  147. struct efi_mem_desc *memory_map,
  148. unsigned long *map_key,
  149. unsigned long *descriptor_size,
  150. uint32_t *descriptor_version)
  151. {
  152. efi_status_t r;
  153. EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
  154. map_key, descriptor_size, descriptor_version);
  155. r = efi_get_memory_map(memory_map_size, memory_map, map_key,
  156. descriptor_size, descriptor_version);
  157. return EFI_EXIT(r);
  158. }
  159. static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
  160. unsigned long size,
  161. void **buffer)
  162. {
  163. efi_status_t r;
  164. EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
  165. r = efi_allocate_pool(pool_type, size, buffer);
  166. return EFI_EXIT(r);
  167. }
  168. static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
  169. {
  170. efi_status_t r;
  171. EFI_ENTRY("%p", buffer);
  172. r = efi_free_pool(buffer);
  173. return EFI_EXIT(r);
  174. }
  175. /*
  176. * Our event capabilities are very limited. Only a small limited
  177. * number of events is allowed to coexist.
  178. */
  179. static struct efi_event efi_events[16];
  180. efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
  181. void (EFIAPI *notify_function) (
  182. struct efi_event *event,
  183. void *context),
  184. void *notify_context, struct efi_event **event)
  185. {
  186. int i;
  187. if (event == NULL)
  188. return EFI_INVALID_PARAMETER;
  189. if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
  190. return EFI_INVALID_PARAMETER;
  191. if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
  192. notify_function == NULL)
  193. return EFI_INVALID_PARAMETER;
  194. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  195. if (efi_events[i].type)
  196. continue;
  197. efi_events[i].type = type;
  198. efi_events[i].notify_tpl = notify_tpl;
  199. efi_events[i].notify_function = notify_function;
  200. efi_events[i].notify_context = notify_context;
  201. /* Disable timers on bootup */
  202. efi_events[i].trigger_next = -1ULL;
  203. efi_events[i].signaled = 0;
  204. *event = &efi_events[i];
  205. return EFI_SUCCESS;
  206. }
  207. return EFI_OUT_OF_RESOURCES;
  208. }
  209. static efi_status_t EFIAPI efi_create_event_ext(
  210. uint32_t type, UINTN notify_tpl,
  211. void (EFIAPI *notify_function) (
  212. struct efi_event *event,
  213. void *context),
  214. void *notify_context, struct efi_event **event)
  215. {
  216. EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
  217. notify_context);
  218. return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
  219. notify_context, event));
  220. }
  221. /*
  222. * Our timers have to work without interrupts, so we check whenever keyboard
  223. * input or disk accesses happen if enough time elapsed for it to fire.
  224. */
  225. void efi_timer_check(void)
  226. {
  227. int i;
  228. u64 now = timer_get_us();
  229. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  230. if (!efi_events[i].type ||
  231. !(efi_events[i].type & EVT_TIMER) ||
  232. efi_events[i].trigger_type == EFI_TIMER_STOP ||
  233. now < efi_events[i].trigger_next)
  234. continue;
  235. if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
  236. efi_events[i].trigger_next +=
  237. efi_events[i].trigger_time;
  238. efi_events[i].signaled = 0;
  239. }
  240. efi_signal_event(&efi_events[i]);
  241. }
  242. WATCHDOG_RESET();
  243. }
  244. efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
  245. uint64_t trigger_time)
  246. {
  247. int i;
  248. /*
  249. * The parameter defines a multiple of 100ns.
  250. * We use multiples of 1000ns. So divide by 10.
  251. */
  252. trigger_time = efi_div10(trigger_time);
  253. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  254. if (event != &efi_events[i])
  255. continue;
  256. if (!(event->type & EVT_TIMER))
  257. break;
  258. switch (type) {
  259. case EFI_TIMER_STOP:
  260. event->trigger_next = -1ULL;
  261. break;
  262. case EFI_TIMER_PERIODIC:
  263. case EFI_TIMER_RELATIVE:
  264. event->trigger_next =
  265. timer_get_us() + trigger_time;
  266. break;
  267. default:
  268. return EFI_INVALID_PARAMETER;
  269. }
  270. event->trigger_type = type;
  271. event->trigger_time = trigger_time;
  272. return EFI_SUCCESS;
  273. }
  274. return EFI_INVALID_PARAMETER;
  275. }
  276. static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
  277. enum efi_timer_delay type,
  278. uint64_t trigger_time)
  279. {
  280. EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
  281. return EFI_EXIT(efi_set_timer(event, type, trigger_time));
  282. }
  283. static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
  284. struct efi_event **event,
  285. unsigned long *index)
  286. {
  287. int i, j;
  288. EFI_ENTRY("%ld, %p, %p", num_events, event, index);
  289. /* Check parameters */
  290. if (!num_events || !event)
  291. return EFI_EXIT(EFI_INVALID_PARAMETER);
  292. for (i = 0; i < num_events; ++i) {
  293. for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
  294. if (event[i] == &efi_events[j])
  295. goto known_event;
  296. }
  297. return EFI_EXIT(EFI_INVALID_PARAMETER);
  298. known_event:
  299. if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
  300. return EFI_EXIT(EFI_INVALID_PARAMETER);
  301. }
  302. /* Wait for signal */
  303. for (;;) {
  304. for (i = 0; i < num_events; ++i) {
  305. if (event[i]->signaled)
  306. goto out;
  307. }
  308. /* Allow events to occur. */
  309. efi_timer_check();
  310. }
  311. out:
  312. /*
  313. * Reset the signal which is passed to the caller to allow periodic
  314. * events to occur.
  315. */
  316. event[i]->signaled = 0;
  317. if (index)
  318. *index = i;
  319. return EFI_EXIT(EFI_SUCCESS);
  320. }
  321. static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
  322. {
  323. int i;
  324. EFI_ENTRY("%p", event);
  325. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  326. if (event != &efi_events[i])
  327. continue;
  328. efi_signal_event(event);
  329. break;
  330. }
  331. return EFI_EXIT(EFI_SUCCESS);
  332. }
  333. static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
  334. {
  335. int i;
  336. EFI_ENTRY("%p", event);
  337. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  338. if (event == &efi_events[i]) {
  339. event->type = 0;
  340. event->trigger_next = -1ULL;
  341. event->signaled = 0;
  342. return EFI_EXIT(EFI_SUCCESS);
  343. }
  344. }
  345. return EFI_EXIT(EFI_INVALID_PARAMETER);
  346. }
  347. static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
  348. {
  349. int i;
  350. EFI_ENTRY("%p", event);
  351. efi_timer_check();
  352. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  353. if (event != &efi_events[i])
  354. continue;
  355. if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
  356. break;
  357. if (event->signaled)
  358. return EFI_EXIT(EFI_SUCCESS);
  359. return EFI_EXIT(EFI_NOT_READY);
  360. }
  361. return EFI_EXIT(EFI_INVALID_PARAMETER);
  362. }
  363. static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
  364. efi_guid_t *protocol, int protocol_interface_type,
  365. void *protocol_interface)
  366. {
  367. struct list_head *lhandle;
  368. int i;
  369. efi_status_t r;
  370. if (!handle || !protocol ||
  371. protocol_interface_type != EFI_NATIVE_INTERFACE) {
  372. r = EFI_INVALID_PARAMETER;
  373. goto out;
  374. }
  375. /* Create new handle if requested. */
  376. if (!*handle) {
  377. r = EFI_OUT_OF_RESOURCES;
  378. goto out;
  379. }
  380. /* Find object. */
  381. list_for_each(lhandle, &efi_obj_list) {
  382. struct efi_object *efiobj;
  383. efiobj = list_entry(lhandle, struct efi_object, link);
  384. if (efiobj->handle != *handle)
  385. continue;
  386. /* Check if protocol is already installed on the handle. */
  387. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  388. struct efi_handler *handler = &efiobj->protocols[i];
  389. if (!handler->guid)
  390. continue;
  391. if (!guidcmp(handler->guid, protocol)) {
  392. r = EFI_INVALID_PARAMETER;
  393. goto out;
  394. }
  395. }
  396. /* Install protocol in first empty slot. */
  397. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  398. struct efi_handler *handler = &efiobj->protocols[i];
  399. if (handler->guid)
  400. continue;
  401. handler->guid = protocol;
  402. handler->protocol_interface = protocol_interface;
  403. r = EFI_SUCCESS;
  404. goto out;
  405. }
  406. r = EFI_OUT_OF_RESOURCES;
  407. goto out;
  408. }
  409. r = EFI_INVALID_PARAMETER;
  410. out:
  411. return r;
  412. }
  413. static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
  414. efi_guid_t *protocol, int protocol_interface_type,
  415. void *protocol_interface)
  416. {
  417. EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
  418. protocol_interface);
  419. return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
  420. protocol_interface_type,
  421. protocol_interface));
  422. }
  423. static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
  424. efi_guid_t *protocol, void *old_interface,
  425. void *new_interface)
  426. {
  427. EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
  428. new_interface);
  429. return EFI_EXIT(EFI_ACCESS_DENIED);
  430. }
  431. static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
  432. efi_guid_t *protocol, void *protocol_interface)
  433. {
  434. struct list_head *lhandle;
  435. int i;
  436. efi_status_t r = EFI_NOT_FOUND;
  437. if (!handle || !protocol) {
  438. r = EFI_INVALID_PARAMETER;
  439. goto out;
  440. }
  441. list_for_each(lhandle, &efi_obj_list) {
  442. struct efi_object *efiobj;
  443. efiobj = list_entry(lhandle, struct efi_object, link);
  444. if (efiobj->handle != handle)
  445. continue;
  446. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  447. struct efi_handler *handler = &efiobj->protocols[i];
  448. const efi_guid_t *hprotocol = handler->guid;
  449. if (!hprotocol)
  450. continue;
  451. if (!guidcmp(hprotocol, protocol)) {
  452. if (handler->protocol_interface) {
  453. r = EFI_ACCESS_DENIED;
  454. } else {
  455. handler->guid = 0;
  456. r = EFI_SUCCESS;
  457. }
  458. goto out;
  459. }
  460. }
  461. }
  462. out:
  463. return r;
  464. }
  465. static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
  466. efi_guid_t *protocol, void *protocol_interface)
  467. {
  468. EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
  469. return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
  470. protocol_interface));
  471. }
  472. static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
  473. struct efi_event *event,
  474. void **registration)
  475. {
  476. EFI_ENTRY("%p, %p, %p", protocol, event, registration);
  477. return EFI_EXIT(EFI_OUT_OF_RESOURCES);
  478. }
  479. static int efi_search(enum efi_locate_search_type search_type,
  480. efi_guid_t *protocol, void *search_key,
  481. struct efi_object *efiobj)
  482. {
  483. int i;
  484. switch (search_type) {
  485. case all_handles:
  486. return 0;
  487. case by_register_notify:
  488. return -1;
  489. case by_protocol:
  490. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  491. const efi_guid_t *guid = efiobj->protocols[i].guid;
  492. if (guid && !guidcmp(guid, protocol))
  493. return 0;
  494. }
  495. return -1;
  496. }
  497. return -1;
  498. }
  499. static efi_status_t EFIAPI efi_locate_handle(
  500. enum efi_locate_search_type search_type,
  501. efi_guid_t *protocol, void *search_key,
  502. unsigned long *buffer_size, efi_handle_t *buffer)
  503. {
  504. struct list_head *lhandle;
  505. unsigned long size = 0;
  506. /* Count how much space we need */
  507. list_for_each(lhandle, &efi_obj_list) {
  508. struct efi_object *efiobj;
  509. efiobj = list_entry(lhandle, struct efi_object, link);
  510. if (!efi_search(search_type, protocol, search_key, efiobj)) {
  511. size += sizeof(void*);
  512. }
  513. }
  514. if (*buffer_size < size) {
  515. *buffer_size = size;
  516. return EFI_BUFFER_TOO_SMALL;
  517. }
  518. /* Then fill the array */
  519. list_for_each(lhandle, &efi_obj_list) {
  520. struct efi_object *efiobj;
  521. efiobj = list_entry(lhandle, struct efi_object, link);
  522. if (!efi_search(search_type, protocol, search_key, efiobj)) {
  523. *(buffer++) = efiobj->handle;
  524. }
  525. }
  526. *buffer_size = size;
  527. return EFI_SUCCESS;
  528. }
  529. static efi_status_t EFIAPI efi_locate_handle_ext(
  530. enum efi_locate_search_type search_type,
  531. efi_guid_t *protocol, void *search_key,
  532. unsigned long *buffer_size, efi_handle_t *buffer)
  533. {
  534. EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
  535. buffer_size, buffer);
  536. return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
  537. buffer_size, buffer));
  538. }
  539. static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
  540. struct efi_device_path **device_path,
  541. efi_handle_t *device)
  542. {
  543. EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
  544. return EFI_EXIT(EFI_NOT_FOUND);
  545. }
  546. efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
  547. {
  548. int i;
  549. /* Check for guid override */
  550. for (i = 0; i < systab.nr_tables; i++) {
  551. if (!guidcmp(guid, &efi_conf_table[i].guid)) {
  552. efi_conf_table[i].table = table;
  553. return EFI_SUCCESS;
  554. }
  555. }
  556. /* No override, check for overflow */
  557. if (i >= ARRAY_SIZE(efi_conf_table))
  558. return EFI_OUT_OF_RESOURCES;
  559. /* Add a new entry */
  560. memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
  561. efi_conf_table[i].table = table;
  562. systab.nr_tables = i + 1;
  563. return EFI_SUCCESS;
  564. }
  565. static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
  566. void *table)
  567. {
  568. EFI_ENTRY("%p, %p", guid, table);
  569. return EFI_EXIT(efi_install_configuration_table(guid, table));
  570. }
  571. static efi_status_t EFIAPI efi_load_image(bool boot_policy,
  572. efi_handle_t parent_image,
  573. struct efi_device_path *file_path,
  574. void *source_buffer,
  575. unsigned long source_size,
  576. efi_handle_t *image_handle)
  577. {
  578. static struct efi_object loaded_image_info_obj = {
  579. .protocols = {
  580. {
  581. .guid = &efi_guid_loaded_image,
  582. },
  583. },
  584. };
  585. struct efi_loaded_image *info;
  586. struct efi_object *obj;
  587. EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
  588. file_path, source_buffer, source_size, image_handle);
  589. info = malloc(sizeof(*info));
  590. loaded_image_info_obj.protocols[0].protocol_interface = info;
  591. obj = malloc(sizeof(loaded_image_info_obj));
  592. memset(info, 0, sizeof(*info));
  593. memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
  594. obj->handle = info;
  595. info->file_path = file_path;
  596. info->reserved = efi_load_pe(source_buffer, info);
  597. if (!info->reserved) {
  598. free(info);
  599. free(obj);
  600. return EFI_EXIT(EFI_UNSUPPORTED);
  601. }
  602. *image_handle = info;
  603. list_add_tail(&obj->link, &efi_obj_list);
  604. return EFI_EXIT(EFI_SUCCESS);
  605. }
  606. static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
  607. unsigned long *exit_data_size,
  608. s16 **exit_data)
  609. {
  610. ulong (*entry)(void *image_handle, struct efi_system_table *st);
  611. struct efi_loaded_image *info = image_handle;
  612. EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
  613. entry = info->reserved;
  614. efi_is_direct_boot = false;
  615. /* call the image! */
  616. if (setjmp(&info->exit_jmp)) {
  617. /* We returned from the child image */
  618. return EFI_EXIT(info->exit_status);
  619. }
  620. entry(image_handle, &systab);
  621. /* Should usually never get here */
  622. return EFI_EXIT(EFI_SUCCESS);
  623. }
  624. static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
  625. efi_status_t exit_status, unsigned long exit_data_size,
  626. int16_t *exit_data)
  627. {
  628. struct efi_loaded_image *loaded_image_info = (void*)image_handle;
  629. EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
  630. exit_data_size, exit_data);
  631. loaded_image_info->exit_status = exit_status;
  632. longjmp(&loaded_image_info->exit_jmp, 1);
  633. panic("EFI application exited");
  634. }
  635. static struct efi_object *efi_search_obj(void *handle)
  636. {
  637. struct list_head *lhandle;
  638. list_for_each(lhandle, &efi_obj_list) {
  639. struct efi_object *efiobj;
  640. efiobj = list_entry(lhandle, struct efi_object, link);
  641. if (efiobj->handle == handle)
  642. return efiobj;
  643. }
  644. return NULL;
  645. }
  646. static efi_status_t EFIAPI efi_unload_image(void *image_handle)
  647. {
  648. struct efi_object *efiobj;
  649. EFI_ENTRY("%p", image_handle);
  650. efiobj = efi_search_obj(image_handle);
  651. if (efiobj)
  652. list_del(&efiobj->link);
  653. return EFI_EXIT(EFI_SUCCESS);
  654. }
  655. static void efi_exit_caches(void)
  656. {
  657. #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
  658. /*
  659. * Grub on 32bit ARM needs to have caches disabled before jumping into
  660. * a zImage, but does not know of all cache layers. Give it a hand.
  661. */
  662. if (efi_is_direct_boot)
  663. cleanup_before_linux();
  664. #endif
  665. }
  666. static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
  667. unsigned long map_key)
  668. {
  669. EFI_ENTRY("%p, %ld", image_handle, map_key);
  670. board_quiesce_devices();
  671. /* Fix up caches for EFI payloads if necessary */
  672. efi_exit_caches();
  673. /* This stops all lingering devices */
  674. bootm_disable_interrupts();
  675. /* Give the payload some time to boot */
  676. WATCHDOG_RESET();
  677. return EFI_EXIT(EFI_SUCCESS);
  678. }
  679. static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
  680. {
  681. static uint64_t mono = 0;
  682. EFI_ENTRY("%p", count);
  683. *count = mono++;
  684. return EFI_EXIT(EFI_SUCCESS);
  685. }
  686. static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
  687. {
  688. EFI_ENTRY("%ld", microseconds);
  689. udelay(microseconds);
  690. return EFI_EXIT(EFI_SUCCESS);
  691. }
  692. static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
  693. uint64_t watchdog_code,
  694. unsigned long data_size,
  695. uint16_t *watchdog_data)
  696. {
  697. EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
  698. data_size, watchdog_data);
  699. return efi_unsupported(__func__);
  700. }
  701. static efi_status_t EFIAPI efi_connect_controller(
  702. efi_handle_t controller_handle,
  703. efi_handle_t *driver_image_handle,
  704. struct efi_device_path *remain_device_path,
  705. bool recursive)
  706. {
  707. EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
  708. remain_device_path, recursive);
  709. return EFI_EXIT(EFI_NOT_FOUND);
  710. }
  711. static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
  712. void *driver_image_handle,
  713. void *child_handle)
  714. {
  715. EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
  716. child_handle);
  717. return EFI_EXIT(EFI_INVALID_PARAMETER);
  718. }
  719. static efi_status_t EFIAPI efi_close_protocol(void *handle,
  720. efi_guid_t *protocol,
  721. void *agent_handle,
  722. void *controller_handle)
  723. {
  724. EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
  725. controller_handle);
  726. return EFI_EXIT(EFI_NOT_FOUND);
  727. }
  728. static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
  729. efi_guid_t *protocol,
  730. struct efi_open_protocol_info_entry **entry_buffer,
  731. unsigned long *entry_count)
  732. {
  733. EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
  734. entry_count);
  735. return EFI_EXIT(EFI_NOT_FOUND);
  736. }
  737. static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
  738. efi_guid_t ***protocol_buffer,
  739. unsigned long *protocol_buffer_count)
  740. {
  741. unsigned long buffer_size;
  742. struct efi_object *efiobj;
  743. unsigned long i, j;
  744. struct list_head *lhandle;
  745. efi_status_t r;
  746. EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
  747. protocol_buffer_count);
  748. if (!handle || !protocol_buffer || !protocol_buffer_count)
  749. return EFI_EXIT(EFI_INVALID_PARAMETER);
  750. *protocol_buffer = NULL;
  751. *protocol_buffer_count = 0;
  752. list_for_each(lhandle, &efi_obj_list) {
  753. efiobj = list_entry(lhandle, struct efi_object, link);
  754. if (efiobj->handle != handle)
  755. continue;
  756. /* Count protocols */
  757. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  758. if (efiobj->protocols[i].guid)
  759. ++*protocol_buffer_count;
  760. }
  761. /* Copy guids */
  762. if (*protocol_buffer_count) {
  763. buffer_size = sizeof(efi_guid_t *) *
  764. *protocol_buffer_count;
  765. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
  766. buffer_size,
  767. (void **)protocol_buffer);
  768. if (r != EFI_SUCCESS)
  769. return EFI_EXIT(r);
  770. j = 0;
  771. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
  772. if (efiobj->protocols[i].guid) {
  773. (*protocol_buffer)[j] = (void *)
  774. efiobj->protocols[i].guid;
  775. ++j;
  776. }
  777. }
  778. }
  779. break;
  780. }
  781. return EFI_EXIT(EFI_SUCCESS);
  782. }
  783. static efi_status_t EFIAPI efi_locate_handle_buffer(
  784. enum efi_locate_search_type search_type,
  785. efi_guid_t *protocol, void *search_key,
  786. unsigned long *no_handles, efi_handle_t **buffer)
  787. {
  788. efi_status_t r;
  789. unsigned long buffer_size = 0;
  790. EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
  791. no_handles, buffer);
  792. if (!no_handles || !buffer) {
  793. r = EFI_INVALID_PARAMETER;
  794. goto out;
  795. }
  796. *no_handles = 0;
  797. *buffer = NULL;
  798. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  799. *buffer);
  800. if (r != EFI_BUFFER_TOO_SMALL)
  801. goto out;
  802. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
  803. (void **)buffer);
  804. if (r != EFI_SUCCESS)
  805. goto out;
  806. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  807. *buffer);
  808. if (r == EFI_SUCCESS)
  809. *no_handles = buffer_size / sizeof(void *);
  810. out:
  811. return EFI_EXIT(r);
  812. }
  813. static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
  814. void *registration,
  815. void **protocol_interface)
  816. {
  817. struct list_head *lhandle;
  818. int i;
  819. EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
  820. if (!protocol || !protocol_interface)
  821. return EFI_EXIT(EFI_INVALID_PARAMETER);
  822. list_for_each(lhandle, &efi_obj_list) {
  823. struct efi_object *efiobj;
  824. efiobj = list_entry(lhandle, struct efi_object, link);
  825. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  826. struct efi_handler *handler = &efiobj->protocols[i];
  827. if (!handler->guid)
  828. continue;
  829. if (!guidcmp(handler->guid, protocol)) {
  830. *protocol_interface =
  831. handler->protocol_interface;
  832. return EFI_EXIT(EFI_SUCCESS);
  833. }
  834. }
  835. }
  836. *protocol_interface = NULL;
  837. return EFI_EXIT(EFI_NOT_FOUND);
  838. }
  839. static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
  840. void **handle, ...)
  841. {
  842. EFI_ENTRY("%p", handle);
  843. va_list argptr;
  844. efi_guid_t *protocol;
  845. void *protocol_interface;
  846. efi_status_t r = EFI_SUCCESS;
  847. int i = 0;
  848. if (!handle)
  849. return EFI_EXIT(EFI_INVALID_PARAMETER);
  850. va_start(argptr, handle);
  851. for (;;) {
  852. protocol = va_arg(argptr, efi_guid_t*);
  853. if (!protocol)
  854. break;
  855. protocol_interface = va_arg(argptr, void*);
  856. r = efi_install_protocol_interface(handle, protocol,
  857. EFI_NATIVE_INTERFACE,
  858. protocol_interface);
  859. if (r != EFI_SUCCESS)
  860. break;
  861. i++;
  862. }
  863. va_end(argptr);
  864. if (r == EFI_SUCCESS)
  865. return EFI_EXIT(r);
  866. /* If an error occured undo all changes. */
  867. va_start(argptr, handle);
  868. for (; i; --i) {
  869. protocol = va_arg(argptr, efi_guid_t*);
  870. protocol_interface = va_arg(argptr, void*);
  871. efi_uninstall_protocol_interface(handle, protocol,
  872. protocol_interface);
  873. }
  874. va_end(argptr);
  875. return EFI_EXIT(r);
  876. }
  877. static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
  878. void *handle, ...)
  879. {
  880. EFI_ENTRY("%p", handle);
  881. return EFI_EXIT(EFI_INVALID_PARAMETER);
  882. }
  883. static efi_status_t EFIAPI efi_calculate_crc32(void *data,
  884. unsigned long data_size,
  885. uint32_t *crc32_p)
  886. {
  887. EFI_ENTRY("%p, %ld", data, data_size);
  888. *crc32_p = crc32(0, data, data_size);
  889. return EFI_EXIT(EFI_SUCCESS);
  890. }
  891. static void EFIAPI efi_copy_mem(void *destination, void *source,
  892. unsigned long length)
  893. {
  894. EFI_ENTRY("%p, %p, %ld", destination, source, length);
  895. memcpy(destination, source, length);
  896. }
  897. static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
  898. {
  899. EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
  900. memset(buffer, value, size);
  901. }
  902. static efi_status_t EFIAPI efi_open_protocol(
  903. void *handle, efi_guid_t *protocol,
  904. void **protocol_interface, void *agent_handle,
  905. void *controller_handle, uint32_t attributes)
  906. {
  907. struct list_head *lhandle;
  908. int i;
  909. efi_status_t r = EFI_INVALID_PARAMETER;
  910. EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
  911. protocol_interface, agent_handle, controller_handle,
  912. attributes);
  913. if (!handle || !protocol ||
  914. (!protocol_interface && attributes !=
  915. EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
  916. goto out;
  917. }
  918. switch (attributes) {
  919. case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
  920. case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
  921. case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
  922. break;
  923. case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
  924. if (controller_handle == handle)
  925. goto out;
  926. case EFI_OPEN_PROTOCOL_BY_DRIVER:
  927. case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
  928. if (controller_handle == NULL)
  929. goto out;
  930. case EFI_OPEN_PROTOCOL_EXCLUSIVE:
  931. if (agent_handle == NULL)
  932. goto out;
  933. break;
  934. default:
  935. goto out;
  936. }
  937. list_for_each(lhandle, &efi_obj_list) {
  938. struct efi_object *efiobj;
  939. efiobj = list_entry(lhandle, struct efi_object, link);
  940. if (efiobj->handle != handle)
  941. continue;
  942. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  943. struct efi_handler *handler = &efiobj->protocols[i];
  944. const efi_guid_t *hprotocol = handler->guid;
  945. if (!hprotocol)
  946. continue;
  947. if (!guidcmp(hprotocol, protocol)) {
  948. if (attributes !=
  949. EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
  950. *protocol_interface =
  951. handler->protocol_interface;
  952. }
  953. r = EFI_SUCCESS;
  954. goto out;
  955. }
  956. }
  957. goto unsupported;
  958. }
  959. unsupported:
  960. r = EFI_UNSUPPORTED;
  961. out:
  962. return EFI_EXIT(r);
  963. }
  964. static efi_status_t EFIAPI efi_handle_protocol(void *handle,
  965. efi_guid_t *protocol,
  966. void **protocol_interface)
  967. {
  968. return efi_open_protocol(handle, protocol, protocol_interface, NULL,
  969. NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
  970. }
  971. static const struct efi_boot_services efi_boot_services = {
  972. .hdr = {
  973. .headersize = sizeof(struct efi_table_hdr),
  974. },
  975. .raise_tpl = efi_raise_tpl,
  976. .restore_tpl = efi_restore_tpl,
  977. .allocate_pages = efi_allocate_pages_ext,
  978. .free_pages = efi_free_pages_ext,
  979. .get_memory_map = efi_get_memory_map_ext,
  980. .allocate_pool = efi_allocate_pool_ext,
  981. .free_pool = efi_free_pool_ext,
  982. .create_event = efi_create_event_ext,
  983. .set_timer = efi_set_timer_ext,
  984. .wait_for_event = efi_wait_for_event,
  985. .signal_event = efi_signal_event_ext,
  986. .close_event = efi_close_event,
  987. .check_event = efi_check_event,
  988. .install_protocol_interface = efi_install_protocol_interface_ext,
  989. .reinstall_protocol_interface = efi_reinstall_protocol_interface,
  990. .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
  991. .handle_protocol = efi_handle_protocol,
  992. .reserved = NULL,
  993. .register_protocol_notify = efi_register_protocol_notify,
  994. .locate_handle = efi_locate_handle_ext,
  995. .locate_device_path = efi_locate_device_path,
  996. .install_configuration_table = efi_install_configuration_table_ext,
  997. .load_image = efi_load_image,
  998. .start_image = efi_start_image,
  999. .exit = efi_exit,
  1000. .unload_image = efi_unload_image,
  1001. .exit_boot_services = efi_exit_boot_services,
  1002. .get_next_monotonic_count = efi_get_next_monotonic_count,
  1003. .stall = efi_stall,
  1004. .set_watchdog_timer = efi_set_watchdog_timer,
  1005. .connect_controller = efi_connect_controller,
  1006. .disconnect_controller = efi_disconnect_controller,
  1007. .open_protocol = efi_open_protocol,
  1008. .close_protocol = efi_close_protocol,
  1009. .open_protocol_information = efi_open_protocol_information,
  1010. .protocols_per_handle = efi_protocols_per_handle,
  1011. .locate_handle_buffer = efi_locate_handle_buffer,
  1012. .locate_protocol = efi_locate_protocol,
  1013. .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
  1014. .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
  1015. .calculate_crc32 = efi_calculate_crc32,
  1016. .copy_mem = efi_copy_mem,
  1017. .set_mem = efi_set_mem,
  1018. };
  1019. static uint16_t __efi_runtime_data firmware_vendor[] =
  1020. { 'D','a','s',' ','U','-','b','o','o','t',0 };
  1021. struct efi_system_table __efi_runtime_data systab = {
  1022. .hdr = {
  1023. .signature = EFI_SYSTEM_TABLE_SIGNATURE,
  1024. .revision = 0x20005, /* 2.5 */
  1025. .headersize = sizeof(struct efi_table_hdr),
  1026. },
  1027. .fw_vendor = (long)firmware_vendor,
  1028. .con_in = (void*)&efi_con_in,
  1029. .con_out = (void*)&efi_con_out,
  1030. .std_err = (void*)&efi_con_out,
  1031. .runtime = (void*)&efi_runtime_services,
  1032. .boottime = (void*)&efi_boot_services,
  1033. .nr_tables = 0,
  1034. .tables = (void*)efi_conf_table,
  1035. };