efi_boottime.c 32 KB

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