efi_boottime.c 32 KB

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