efi_boottime.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085
  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 <div64.h>
  10. #include <efi_loader.h>
  11. #include <environment.h>
  12. #include <malloc.h>
  13. #include <asm/global_data.h>
  14. #include <libfdt_env.h>
  15. #include <u-boot/crc.h>
  16. #include <bootm.h>
  17. #include <inttypes.h>
  18. #include <watchdog.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /* Task priority level */
  21. static efi_uintn_t efi_tpl = TPL_APPLICATION;
  22. /* This list contains all the EFI objects our payload has access to */
  23. LIST_HEAD(efi_obj_list);
  24. /*
  25. * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
  26. * we need to do trickery with caches. Since we don't want to break the EFI
  27. * aware boot path, only apply hacks when loading exiting directly (breaking
  28. * direct Linux EFI booting along the way - oh well).
  29. */
  30. static bool efi_is_direct_boot = true;
  31. /*
  32. * EFI can pass arbitrary additional "tables" containing vendor specific
  33. * information to the payload. One such table is the FDT table which contains
  34. * a pointer to a flattened device tree blob.
  35. *
  36. * In most cases we want to pass an FDT to the payload, so reserve one slot of
  37. * config table space for it. The pointer gets populated by do_bootefi_exec().
  38. */
  39. static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
  40. #ifdef CONFIG_ARM
  41. /*
  42. * The "gd" pointer lives in a register on ARM and AArch64 that we declare
  43. * fixed when compiling U-Boot. However, the payload does not know about that
  44. * restriction so we need to manually swap its and our view of that register on
  45. * EFI callback entry/exit.
  46. */
  47. static volatile void *efi_gd, *app_gd;
  48. #endif
  49. static int entry_count;
  50. static int nesting_level;
  51. /* Called on every callback entry */
  52. int __efi_entry_check(void)
  53. {
  54. int ret = entry_count++ == 0;
  55. #ifdef CONFIG_ARM
  56. assert(efi_gd);
  57. app_gd = gd;
  58. gd = efi_gd;
  59. #endif
  60. return ret;
  61. }
  62. /* Called on every callback exit */
  63. int __efi_exit_check(void)
  64. {
  65. int ret = --entry_count == 0;
  66. #ifdef CONFIG_ARM
  67. gd = app_gd;
  68. #endif
  69. return ret;
  70. }
  71. /* Called from do_bootefi_exec() */
  72. void efi_save_gd(void)
  73. {
  74. #ifdef CONFIG_ARM
  75. efi_gd = gd;
  76. #endif
  77. }
  78. /*
  79. * Special case handler for error/abort that just forces things back
  80. * to u-boot world so we can dump out an abort msg, without any care
  81. * about returning back to UEFI world.
  82. */
  83. void efi_restore_gd(void)
  84. {
  85. #ifdef CONFIG_ARM
  86. /* Only restore if we're already in EFI context */
  87. if (!efi_gd)
  88. return;
  89. gd = efi_gd;
  90. #endif
  91. }
  92. /*
  93. * Two spaces per indent level, maxing out at 10.. which ought to be
  94. * enough for anyone ;-)
  95. */
  96. static const char *indent_string(int level)
  97. {
  98. const char *indent = " ";
  99. const int max = strlen(indent);
  100. level = min(max, level * 2);
  101. return &indent[max - level];
  102. }
  103. const char *__efi_nesting(void)
  104. {
  105. return indent_string(nesting_level);
  106. }
  107. const char *__efi_nesting_inc(void)
  108. {
  109. return indent_string(nesting_level++);
  110. }
  111. const char *__efi_nesting_dec(void)
  112. {
  113. return indent_string(--nesting_level);
  114. }
  115. /*
  116. * Queue an EFI event.
  117. *
  118. * This function queues the notification function of the event for future
  119. * execution.
  120. *
  121. * The notification function is called if the task priority level of the
  122. * event is higher than the current task priority level.
  123. *
  124. * For the SignalEvent service see efi_signal_event_ext.
  125. *
  126. * @event event to signal
  127. */
  128. void efi_signal_event(struct efi_event *event)
  129. {
  130. if (event->notify_function) {
  131. event->is_queued = true;
  132. /* Check TPL */
  133. if (efi_tpl >= event->notify_tpl)
  134. return;
  135. EFI_CALL_VOID(event->notify_function(event,
  136. event->notify_context));
  137. }
  138. event->is_queued = false;
  139. }
  140. /*
  141. * Raise the task priority level.
  142. *
  143. * This function implements the RaiseTpl service.
  144. * See the Unified Extensible Firmware Interface (UEFI) specification
  145. * for details.
  146. *
  147. * @new_tpl new value of the task priority level
  148. * @return old value of the task priority level
  149. */
  150. static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
  151. {
  152. efi_uintn_t old_tpl = efi_tpl;
  153. EFI_ENTRY("0x%zx", new_tpl);
  154. if (new_tpl < efi_tpl)
  155. debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
  156. efi_tpl = new_tpl;
  157. if (efi_tpl > TPL_HIGH_LEVEL)
  158. efi_tpl = TPL_HIGH_LEVEL;
  159. EFI_EXIT(EFI_SUCCESS);
  160. return old_tpl;
  161. }
  162. /*
  163. * Lower the task priority level.
  164. *
  165. * This function implements the RestoreTpl service.
  166. * See the Unified Extensible Firmware Interface (UEFI) specification
  167. * for details.
  168. *
  169. * @old_tpl value of the task priority level to be restored
  170. */
  171. static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
  172. {
  173. EFI_ENTRY("0x%zx", old_tpl);
  174. if (old_tpl > efi_tpl)
  175. debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
  176. efi_tpl = old_tpl;
  177. if (efi_tpl > TPL_HIGH_LEVEL)
  178. efi_tpl = TPL_HIGH_LEVEL;
  179. EFI_EXIT(EFI_SUCCESS);
  180. }
  181. /*
  182. * Allocate memory pages.
  183. *
  184. * This function implements the AllocatePages service.
  185. * See the Unified Extensible Firmware Interface (UEFI) specification
  186. * for details.
  187. *
  188. * @type type of allocation to be performed
  189. * @memory_type usage type of the allocated memory
  190. * @pages number of pages to be allocated
  191. * @memory allocated memory
  192. * @return status code
  193. */
  194. static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
  195. efi_uintn_t pages,
  196. uint64_t *memory)
  197. {
  198. efi_status_t r;
  199. EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
  200. r = efi_allocate_pages(type, memory_type, pages, memory);
  201. return EFI_EXIT(r);
  202. }
  203. /*
  204. * Free memory pages.
  205. *
  206. * This function implements the FreePages service.
  207. * See the Unified Extensible Firmware Interface (UEFI) specification
  208. * for details.
  209. *
  210. * @memory start of the memory area to be freed
  211. * @pages number of pages to be freed
  212. * @return status code
  213. */
  214. static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
  215. efi_uintn_t pages)
  216. {
  217. efi_status_t r;
  218. EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
  219. r = efi_free_pages(memory, pages);
  220. return EFI_EXIT(r);
  221. }
  222. /*
  223. * Get map describing memory usage.
  224. *
  225. * This function implements the GetMemoryMap service.
  226. * See the Unified Extensible Firmware Interface (UEFI) specification
  227. * for details.
  228. *
  229. * @memory_map_size on entry the size, in bytes, of the memory map buffer,
  230. * on exit the size of the copied memory map
  231. * @memory_map buffer to which the memory map is written
  232. * @map_key key for the memory map
  233. * @descriptor_size size of an individual memory descriptor
  234. * @descriptor_version version number of the memory descriptor structure
  235. * @return status code
  236. */
  237. static efi_status_t EFIAPI efi_get_memory_map_ext(
  238. efi_uintn_t *memory_map_size,
  239. struct efi_mem_desc *memory_map,
  240. efi_uintn_t *map_key,
  241. efi_uintn_t *descriptor_size,
  242. uint32_t *descriptor_version)
  243. {
  244. efi_status_t r;
  245. EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
  246. map_key, descriptor_size, descriptor_version);
  247. r = efi_get_memory_map(memory_map_size, memory_map, map_key,
  248. descriptor_size, descriptor_version);
  249. return EFI_EXIT(r);
  250. }
  251. /*
  252. * Allocate memory from pool.
  253. *
  254. * This function implements the AllocatePool service.
  255. * See the Unified Extensible Firmware Interface (UEFI) specification
  256. * for details.
  257. *
  258. * @pool_type type of the pool from which memory is to be allocated
  259. * @size number of bytes to be allocated
  260. * @buffer allocated memory
  261. * @return status code
  262. */
  263. static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
  264. efi_uintn_t size,
  265. void **buffer)
  266. {
  267. efi_status_t r;
  268. EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
  269. r = efi_allocate_pool(pool_type, size, buffer);
  270. return EFI_EXIT(r);
  271. }
  272. /*
  273. * Free memory from pool.
  274. *
  275. * This function implements the FreePool service.
  276. * See the Unified Extensible Firmware Interface (UEFI) specification
  277. * for details.
  278. *
  279. * @buffer start of memory to be freed
  280. * @return status code
  281. */
  282. static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
  283. {
  284. efi_status_t r;
  285. EFI_ENTRY("%p", buffer);
  286. r = efi_free_pool(buffer);
  287. return EFI_EXIT(r);
  288. }
  289. static efi_status_t efi_create_handle(void **handle)
  290. {
  291. struct efi_object *obj;
  292. efi_status_t r;
  293. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
  294. sizeof(struct efi_object),
  295. (void **)&obj);
  296. if (r != EFI_SUCCESS)
  297. return r;
  298. memset(obj, 0, sizeof(struct efi_object));
  299. obj->handle = obj;
  300. list_add_tail(&obj->link, &efi_obj_list);
  301. *handle = obj;
  302. return r;
  303. }
  304. /*
  305. * Our event capabilities are very limited. Only a small limited
  306. * number of events is allowed to coexist.
  307. */
  308. static struct efi_event efi_events[16];
  309. /*
  310. * Create an event.
  311. *
  312. * This function is used inside U-Boot code to create an event.
  313. *
  314. * For the API function implementing the CreateEvent service see
  315. * efi_create_event_ext.
  316. *
  317. * @type type of the event to create
  318. * @notify_tpl task priority level of the event
  319. * @notify_function notification function of the event
  320. * @notify_context pointer passed to the notification function
  321. * @event created event
  322. * @return status code
  323. */
  324. efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
  325. void (EFIAPI *notify_function) (
  326. struct efi_event *event,
  327. void *context),
  328. void *notify_context, struct efi_event **event)
  329. {
  330. int i;
  331. if (event == NULL)
  332. return EFI_INVALID_PARAMETER;
  333. if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
  334. return EFI_INVALID_PARAMETER;
  335. if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
  336. notify_function == NULL)
  337. return EFI_INVALID_PARAMETER;
  338. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  339. if (efi_events[i].type)
  340. continue;
  341. efi_events[i].type = type;
  342. efi_events[i].notify_tpl = notify_tpl;
  343. efi_events[i].notify_function = notify_function;
  344. efi_events[i].notify_context = notify_context;
  345. /* Disable timers on bootup */
  346. efi_events[i].trigger_next = -1ULL;
  347. efi_events[i].is_queued = false;
  348. efi_events[i].is_signaled = false;
  349. *event = &efi_events[i];
  350. return EFI_SUCCESS;
  351. }
  352. return EFI_OUT_OF_RESOURCES;
  353. }
  354. /*
  355. * Create an event.
  356. *
  357. * This function implements the CreateEvent service.
  358. * See the Unified Extensible Firmware Interface (UEFI) specification
  359. * for details.
  360. *
  361. * @type type of the event to create
  362. * @notify_tpl task priority level of the event
  363. * @notify_function notification function of the event
  364. * @notify_context pointer passed to the notification function
  365. * @event created event
  366. * @return status code
  367. */
  368. static efi_status_t EFIAPI efi_create_event_ext(
  369. uint32_t type, efi_uintn_t notify_tpl,
  370. void (EFIAPI *notify_function) (
  371. struct efi_event *event,
  372. void *context),
  373. void *notify_context, struct efi_event **event)
  374. {
  375. EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
  376. notify_context);
  377. return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
  378. notify_context, event));
  379. }
  380. /*
  381. * Check if a timer event has occurred or a queued notification function should
  382. * be called.
  383. *
  384. * Our timers have to work without interrupts, so we check whenever keyboard
  385. * input or disk accesses happen if enough time elapsed for them to fire.
  386. */
  387. void efi_timer_check(void)
  388. {
  389. int i;
  390. u64 now = timer_get_us();
  391. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  392. if (!efi_events[i].type)
  393. continue;
  394. if (efi_events[i].is_queued)
  395. efi_signal_event(&efi_events[i]);
  396. if (!(efi_events[i].type & EVT_TIMER) ||
  397. now < efi_events[i].trigger_next)
  398. continue;
  399. switch (efi_events[i].trigger_type) {
  400. case EFI_TIMER_RELATIVE:
  401. efi_events[i].trigger_type = EFI_TIMER_STOP;
  402. break;
  403. case EFI_TIMER_PERIODIC:
  404. efi_events[i].trigger_next +=
  405. efi_events[i].trigger_time;
  406. break;
  407. default:
  408. continue;
  409. }
  410. efi_events[i].is_signaled = true;
  411. efi_signal_event(&efi_events[i]);
  412. }
  413. WATCHDOG_RESET();
  414. }
  415. /*
  416. * Set the trigger time for a timer event or stop the event.
  417. *
  418. * This is the function for internal usage in U-Boot. For the API function
  419. * implementing the SetTimer service see efi_set_timer_ext.
  420. *
  421. * @event event for which the timer is set
  422. * @type type of the timer
  423. * @trigger_time trigger period in multiples of 100ns
  424. * @return status code
  425. */
  426. efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
  427. uint64_t trigger_time)
  428. {
  429. int i;
  430. /*
  431. * The parameter defines a multiple of 100ns.
  432. * We use multiples of 1000ns. So divide by 10.
  433. */
  434. do_div(trigger_time, 10);
  435. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  436. if (event != &efi_events[i])
  437. continue;
  438. if (!(event->type & EVT_TIMER))
  439. break;
  440. switch (type) {
  441. case EFI_TIMER_STOP:
  442. event->trigger_next = -1ULL;
  443. break;
  444. case EFI_TIMER_PERIODIC:
  445. case EFI_TIMER_RELATIVE:
  446. event->trigger_next =
  447. timer_get_us() + trigger_time;
  448. break;
  449. default:
  450. return EFI_INVALID_PARAMETER;
  451. }
  452. event->trigger_type = type;
  453. event->trigger_time = trigger_time;
  454. event->is_signaled = false;
  455. return EFI_SUCCESS;
  456. }
  457. return EFI_INVALID_PARAMETER;
  458. }
  459. /*
  460. * Set the trigger time for a timer event or stop the event.
  461. *
  462. * This function implements the SetTimer service.
  463. * See the Unified Extensible Firmware Interface (UEFI) specification
  464. * for details.
  465. *
  466. * @event event for which the timer is set
  467. * @type type of the timer
  468. * @trigger_time trigger period in multiples of 100ns
  469. * @return status code
  470. */
  471. static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
  472. enum efi_timer_delay type,
  473. uint64_t trigger_time)
  474. {
  475. EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
  476. return EFI_EXIT(efi_set_timer(event, type, trigger_time));
  477. }
  478. /*
  479. * Wait for events to be signaled.
  480. *
  481. * This function implements the WaitForEvent service.
  482. * See the Unified Extensible Firmware Interface (UEFI) specification
  483. * for details.
  484. *
  485. * @num_events number of events to be waited for
  486. * @events events to be waited for
  487. * @index index of the event that was signaled
  488. * @return status code
  489. */
  490. static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
  491. struct efi_event **event,
  492. efi_uintn_t *index)
  493. {
  494. int i, j;
  495. EFI_ENTRY("%zd, %p, %p", num_events, event, index);
  496. /* Check parameters */
  497. if (!num_events || !event)
  498. return EFI_EXIT(EFI_INVALID_PARAMETER);
  499. /* Check TPL */
  500. if (efi_tpl != TPL_APPLICATION)
  501. return EFI_EXIT(EFI_UNSUPPORTED);
  502. for (i = 0; i < num_events; ++i) {
  503. for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
  504. if (event[i] == &efi_events[j])
  505. goto known_event;
  506. }
  507. return EFI_EXIT(EFI_INVALID_PARAMETER);
  508. known_event:
  509. if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
  510. return EFI_EXIT(EFI_INVALID_PARAMETER);
  511. if (!event[i]->is_signaled)
  512. efi_signal_event(event[i]);
  513. }
  514. /* Wait for signal */
  515. for (;;) {
  516. for (i = 0; i < num_events; ++i) {
  517. if (event[i]->is_signaled)
  518. goto out;
  519. }
  520. /* Allow events to occur. */
  521. efi_timer_check();
  522. }
  523. out:
  524. /*
  525. * Reset the signal which is passed to the caller to allow periodic
  526. * events to occur.
  527. */
  528. event[i]->is_signaled = false;
  529. if (index)
  530. *index = i;
  531. return EFI_EXIT(EFI_SUCCESS);
  532. }
  533. /*
  534. * Signal an EFI event.
  535. *
  536. * This function implements the SignalEvent service.
  537. * See the Unified Extensible Firmware Interface (UEFI) specification
  538. * for details.
  539. *
  540. * This functions sets the signaled state of the event and queues the
  541. * notification function for execution.
  542. *
  543. * @event event to signal
  544. * @return status code
  545. */
  546. static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
  547. {
  548. int i;
  549. EFI_ENTRY("%p", event);
  550. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  551. if (event != &efi_events[i])
  552. continue;
  553. if (event->is_signaled)
  554. break;
  555. event->is_signaled = true;
  556. if (event->type & EVT_NOTIFY_SIGNAL)
  557. efi_signal_event(event);
  558. break;
  559. }
  560. return EFI_EXIT(EFI_SUCCESS);
  561. }
  562. /*
  563. * Close an EFI event.
  564. *
  565. * This function implements the CloseEvent service.
  566. * See the Unified Extensible Firmware Interface (UEFI) specification
  567. * for details.
  568. *
  569. * @event event to close
  570. * @return status code
  571. */
  572. static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
  573. {
  574. int i;
  575. EFI_ENTRY("%p", event);
  576. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  577. if (event == &efi_events[i]) {
  578. event->type = 0;
  579. event->trigger_next = -1ULL;
  580. event->is_queued = false;
  581. event->is_signaled = false;
  582. return EFI_EXIT(EFI_SUCCESS);
  583. }
  584. }
  585. return EFI_EXIT(EFI_INVALID_PARAMETER);
  586. }
  587. /*
  588. * Check if an event is signaled.
  589. *
  590. * This function implements the CheckEvent service.
  591. * See the Unified Extensible Firmware Interface (UEFI) specification
  592. * for details.
  593. *
  594. * If an event is not signaled yet the notification function is queued.
  595. *
  596. * @event event to check
  597. * @return status code
  598. */
  599. static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
  600. {
  601. int i;
  602. EFI_ENTRY("%p", event);
  603. efi_timer_check();
  604. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  605. if (event != &efi_events[i])
  606. continue;
  607. if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
  608. break;
  609. if (!event->is_signaled)
  610. efi_signal_event(event);
  611. if (event->is_signaled)
  612. return EFI_EXIT(EFI_SUCCESS);
  613. return EFI_EXIT(EFI_NOT_READY);
  614. }
  615. return EFI_EXIT(EFI_INVALID_PARAMETER);
  616. }
  617. /*
  618. * Find the internal EFI object for a handle.
  619. *
  620. * @handle handle to find
  621. * @return EFI object
  622. */
  623. struct efi_object *efi_search_obj(void *handle)
  624. {
  625. struct efi_object *efiobj;
  626. list_for_each_entry(efiobj, &efi_obj_list, link) {
  627. if (efiobj->handle == handle)
  628. return efiobj;
  629. }
  630. return NULL;
  631. }
  632. /*
  633. * Install protocol interface.
  634. *
  635. * This function implements the InstallProtocolInterface service.
  636. * See the Unified Extensible Firmware Interface (UEFI) specification
  637. * for details.
  638. *
  639. * @handle handle on which the protocol shall be installed
  640. * @protocol GUID of the protocol to be installed
  641. * @protocol_interface_type type of the interface to be installed,
  642. * always EFI_NATIVE_INTERFACE
  643. * @protocol_interface interface of the protocol implementation
  644. * @return status code
  645. */
  646. static efi_status_t EFIAPI efi_install_protocol_interface(
  647. void **handle, const efi_guid_t *protocol,
  648. int protocol_interface_type, void *protocol_interface)
  649. {
  650. struct list_head *lhandle;
  651. int i;
  652. efi_status_t r;
  653. EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
  654. protocol_interface);
  655. if (!handle || !protocol ||
  656. protocol_interface_type != EFI_NATIVE_INTERFACE) {
  657. r = EFI_INVALID_PARAMETER;
  658. goto out;
  659. }
  660. /* Create new handle if requested. */
  661. if (!*handle) {
  662. r = efi_create_handle(handle);
  663. if (r != EFI_SUCCESS)
  664. goto out;
  665. debug("%sEFI: new handle %p\n", indent_string(nesting_level),
  666. *handle);
  667. } else {
  668. debug("%sEFI: handle %p\n", indent_string(nesting_level),
  669. *handle);
  670. }
  671. /* Find object. */
  672. list_for_each(lhandle, &efi_obj_list) {
  673. struct efi_object *efiobj;
  674. efiobj = list_entry(lhandle, struct efi_object, link);
  675. if (efiobj->handle != *handle)
  676. continue;
  677. /* Check if protocol is already installed on the handle. */
  678. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  679. struct efi_handler *handler = &efiobj->protocols[i];
  680. if (!handler->guid)
  681. continue;
  682. if (!guidcmp(handler->guid, protocol)) {
  683. r = EFI_INVALID_PARAMETER;
  684. goto out;
  685. }
  686. }
  687. /* Install protocol in first empty slot. */
  688. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  689. struct efi_handler *handler = &efiobj->protocols[i];
  690. if (handler->guid)
  691. continue;
  692. handler->guid = protocol;
  693. handler->protocol_interface = protocol_interface;
  694. r = EFI_SUCCESS;
  695. goto out;
  696. }
  697. r = EFI_OUT_OF_RESOURCES;
  698. goto out;
  699. }
  700. r = EFI_INVALID_PARAMETER;
  701. out:
  702. return EFI_EXIT(r);
  703. }
  704. /*
  705. * Reinstall protocol interface.
  706. *
  707. * This function implements the ReinstallProtocolInterface service.
  708. * See the Unified Extensible Firmware Interface (UEFI) specification
  709. * for details.
  710. *
  711. * @handle handle on which the protocol shall be
  712. * reinstalled
  713. * @protocol GUID of the protocol to be installed
  714. * @old_interface interface to be removed
  715. * @new_interface interface to be installed
  716. * @return status code
  717. */
  718. static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
  719. const efi_guid_t *protocol, void *old_interface,
  720. void *new_interface)
  721. {
  722. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
  723. new_interface);
  724. return EFI_EXIT(EFI_ACCESS_DENIED);
  725. }
  726. /*
  727. * Uninstall protocol interface.
  728. *
  729. * This function implements the UninstallProtocolInterface service.
  730. * See the Unified Extensible Firmware Interface (UEFI) specification
  731. * for details.
  732. *
  733. * @handle handle from which the protocol shall be removed
  734. * @protocol GUID of the protocol to be removed
  735. * @protocol_interface interface to be removed
  736. * @return status code
  737. */
  738. static efi_status_t EFIAPI efi_uninstall_protocol_interface(
  739. void *handle, const efi_guid_t *protocol,
  740. void *protocol_interface)
  741. {
  742. struct list_head *lhandle;
  743. int i;
  744. efi_status_t r = EFI_NOT_FOUND;
  745. EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
  746. if (!handle || !protocol) {
  747. r = EFI_INVALID_PARAMETER;
  748. goto out;
  749. }
  750. list_for_each(lhandle, &efi_obj_list) {
  751. struct efi_object *efiobj;
  752. efiobj = list_entry(lhandle, struct efi_object, link);
  753. if (efiobj->handle != handle)
  754. continue;
  755. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  756. struct efi_handler *handler = &efiobj->protocols[i];
  757. const efi_guid_t *hprotocol = handler->guid;
  758. if (!hprotocol)
  759. continue;
  760. if (!guidcmp(hprotocol, protocol)) {
  761. if (handler->protocol_interface) {
  762. r = EFI_ACCESS_DENIED;
  763. } else {
  764. handler->guid = 0;
  765. r = EFI_SUCCESS;
  766. }
  767. goto out;
  768. }
  769. }
  770. }
  771. out:
  772. return EFI_EXIT(r);
  773. }
  774. /*
  775. * Register an event for notification when a protocol is installed.
  776. *
  777. * This function implements the RegisterProtocolNotify service.
  778. * See the Unified Extensible Firmware Interface (UEFI) specification
  779. * for details.
  780. *
  781. * @protocol GUID of the protocol whose installation shall be
  782. * notified
  783. * @event event to be signaled upon installation of the protocol
  784. * @registration key for retrieving the registration information
  785. * @return status code
  786. */
  787. static efi_status_t EFIAPI efi_register_protocol_notify(
  788. const efi_guid_t *protocol,
  789. struct efi_event *event,
  790. void **registration)
  791. {
  792. EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
  793. return EFI_EXIT(EFI_OUT_OF_RESOURCES);
  794. }
  795. /*
  796. * Determine if an EFI handle implements a protocol.
  797. *
  798. * See the documentation of the LocateHandle service in the UEFI specification.
  799. *
  800. * @search_type selection criterion
  801. * @protocol GUID of the protocol
  802. * @search_key registration key
  803. * @efiobj handle
  804. * @return 0 if the handle implements the protocol
  805. */
  806. static int efi_search(enum efi_locate_search_type search_type,
  807. const efi_guid_t *protocol, void *search_key,
  808. struct efi_object *efiobj)
  809. {
  810. int i;
  811. switch (search_type) {
  812. case ALL_HANDLES:
  813. return 0;
  814. case BY_REGISTER_NOTIFY:
  815. /* RegisterProtocolNotify is not implemented yet */
  816. return -1;
  817. case BY_PROTOCOL:
  818. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  819. const efi_guid_t *guid = efiobj->protocols[i].guid;
  820. if (guid && !guidcmp(guid, protocol))
  821. return 0;
  822. }
  823. return -1;
  824. }
  825. return -1;
  826. }
  827. /*
  828. * Locate handles implementing a protocol.
  829. *
  830. * This function is meant for U-Boot internal calls. For the API implementation
  831. * of the LocateHandle service see efi_locate_handle_ext.
  832. *
  833. * @search_type selection criterion
  834. * @protocol GUID of the protocol
  835. * @search_key registration key
  836. * @buffer_size size of the buffer to receive the handles in bytes
  837. * @buffer buffer to receive the relevant handles
  838. * @return status code
  839. */
  840. static efi_status_t efi_locate_handle(
  841. enum efi_locate_search_type search_type,
  842. const efi_guid_t *protocol, void *search_key,
  843. efi_uintn_t *buffer_size, efi_handle_t *buffer)
  844. {
  845. struct efi_object *efiobj;
  846. efi_uintn_t size = 0;
  847. /* Check parameters */
  848. switch (search_type) {
  849. case ALL_HANDLES:
  850. break;
  851. case BY_REGISTER_NOTIFY:
  852. if (!search_key)
  853. return EFI_INVALID_PARAMETER;
  854. /* RegisterProtocolNotify is not implemented yet */
  855. return EFI_UNSUPPORTED;
  856. case BY_PROTOCOL:
  857. if (!protocol)
  858. return EFI_INVALID_PARAMETER;
  859. break;
  860. default:
  861. return EFI_INVALID_PARAMETER;
  862. }
  863. /*
  864. * efi_locate_handle_buffer uses this function for
  865. * the calculation of the necessary buffer size.
  866. * So do not require a buffer for buffersize == 0.
  867. */
  868. if (!buffer_size || (*buffer_size && !buffer))
  869. return EFI_INVALID_PARAMETER;
  870. /* Count how much space we need */
  871. list_for_each_entry(efiobj, &efi_obj_list, link) {
  872. if (!efi_search(search_type, protocol, search_key, efiobj))
  873. size += sizeof(void*);
  874. }
  875. if (*buffer_size < size) {
  876. *buffer_size = size;
  877. return EFI_BUFFER_TOO_SMALL;
  878. }
  879. *buffer_size = size;
  880. if (size == 0)
  881. return EFI_NOT_FOUND;
  882. /* Then fill the array */
  883. list_for_each_entry(efiobj, &efi_obj_list, link) {
  884. if (!efi_search(search_type, protocol, search_key, efiobj))
  885. *buffer++ = efiobj->handle;
  886. }
  887. return EFI_SUCCESS;
  888. }
  889. /*
  890. * Locate handles implementing a protocol.
  891. *
  892. * This function implements the LocateHandle service.
  893. * See the Unified Extensible Firmware Interface (UEFI) specification
  894. * for details.
  895. *
  896. * @search_type selection criterion
  897. * @protocol GUID of the protocol
  898. * @search_key registration key
  899. * @buffer_size size of the buffer to receive the handles in bytes
  900. * @buffer buffer to receive the relevant handles
  901. * @return 0 if the handle implements the protocol
  902. */
  903. static efi_status_t EFIAPI efi_locate_handle_ext(
  904. enum efi_locate_search_type search_type,
  905. const efi_guid_t *protocol, void *search_key,
  906. efi_uintn_t *buffer_size, efi_handle_t *buffer)
  907. {
  908. EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
  909. buffer_size, buffer);
  910. return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
  911. buffer_size, buffer));
  912. }
  913. /*
  914. * Get the device path and handle of an device implementing a protocol.
  915. *
  916. * This function implements the LocateDevicePath service.
  917. * See the Unified Extensible Firmware Interface (UEFI) specification
  918. * for details.
  919. *
  920. * @protocol GUID of the protocol
  921. * @device_path device path
  922. * @device handle of the device
  923. * @return status code
  924. */
  925. static efi_status_t EFIAPI efi_locate_device_path(
  926. const efi_guid_t *protocol,
  927. struct efi_device_path **device_path,
  928. efi_handle_t *device)
  929. {
  930. struct efi_object *efiobj;
  931. EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
  932. efiobj = efi_dp_find_obj(*device_path, device_path);
  933. if (!efiobj)
  934. return EFI_EXIT(EFI_NOT_FOUND);
  935. *device = efiobj->handle;
  936. return EFI_EXIT(EFI_SUCCESS);
  937. }
  938. /* Collapses configuration table entries, removing index i */
  939. static void efi_remove_configuration_table(int i)
  940. {
  941. struct efi_configuration_table *this = &efi_conf_table[i];
  942. struct efi_configuration_table *next = &efi_conf_table[i+1];
  943. struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
  944. memmove(this, next, (ulong)end - (ulong)next);
  945. systab.nr_tables--;
  946. }
  947. /*
  948. * Adds, updates, or removes a configuration table.
  949. *
  950. * This function is used for internal calls. For the API implementation of the
  951. * InstallConfigurationTable service see efi_install_configuration_table_ext.
  952. *
  953. * @guid GUID of the installed table
  954. * @table table to be installed
  955. * @return status code
  956. */
  957. efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
  958. {
  959. int i;
  960. /* Check for guid override */
  961. for (i = 0; i < systab.nr_tables; i++) {
  962. if (!guidcmp(guid, &efi_conf_table[i].guid)) {
  963. if (table)
  964. efi_conf_table[i].table = table;
  965. else
  966. efi_remove_configuration_table(i);
  967. return EFI_SUCCESS;
  968. }
  969. }
  970. if (!table)
  971. return EFI_NOT_FOUND;
  972. /* No override, check for overflow */
  973. if (i >= ARRAY_SIZE(efi_conf_table))
  974. return EFI_OUT_OF_RESOURCES;
  975. /* Add a new entry */
  976. memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
  977. efi_conf_table[i].table = table;
  978. systab.nr_tables = i + 1;
  979. return EFI_SUCCESS;
  980. }
  981. /*
  982. * Adds, updates, or removes a configuration table.
  983. *
  984. * This function implements the InstallConfigurationTable service.
  985. * See the Unified Extensible Firmware Interface (UEFI) specification
  986. * for details.
  987. *
  988. * @guid GUID of the installed table
  989. * @table table to be installed
  990. * @return status code
  991. */
  992. static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
  993. void *table)
  994. {
  995. EFI_ENTRY("%pUl, %p", guid, table);
  996. return EFI_EXIT(efi_install_configuration_table(guid, table));
  997. }
  998. /*
  999. * Initialize a loaded_image_info + loaded_image_info object with correct
  1000. * protocols, boot-device, etc.
  1001. *
  1002. * @info loaded image info to be passed to the entry point of the
  1003. * image
  1004. * @obj internal object associated with the loaded image
  1005. * @device_path device path of the loaded image
  1006. * @file_path file path of the loaded image
  1007. */
  1008. void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
  1009. struct efi_device_path *device_path,
  1010. struct efi_device_path *file_path)
  1011. {
  1012. obj->handle = info;
  1013. /*
  1014. * When asking for the device path interface, return
  1015. * bootefi_device_path
  1016. */
  1017. obj->protocols[0].guid = &efi_guid_device_path;
  1018. obj->protocols[0].protocol_interface = device_path;
  1019. /*
  1020. * When asking for the loaded_image interface, just
  1021. * return handle which points to loaded_image_info
  1022. */
  1023. obj->protocols[1].guid = &efi_guid_loaded_image;
  1024. obj->protocols[1].protocol_interface = info;
  1025. obj->protocols[2].guid = &efi_guid_console_control;
  1026. obj->protocols[2].protocol_interface = (void *)&efi_console_control;
  1027. obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
  1028. obj->protocols[3].protocol_interface =
  1029. (void *)&efi_device_path_to_text;
  1030. info->file_path = file_path;
  1031. if (device_path)
  1032. info->device_handle = efi_dp_find_obj(device_path, NULL);
  1033. list_add_tail(&obj->link, &efi_obj_list);
  1034. }
  1035. /*
  1036. * Load an image using a file path.
  1037. *
  1038. * @file_path the path of the image to load
  1039. * @buffer buffer containing the loaded image
  1040. * @return status code
  1041. */
  1042. efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
  1043. void **buffer)
  1044. {
  1045. struct efi_file_info *info = NULL;
  1046. struct efi_file_handle *f;
  1047. static efi_status_t ret;
  1048. uint64_t bs;
  1049. f = efi_file_from_path(file_path);
  1050. if (!f)
  1051. return EFI_DEVICE_ERROR;
  1052. bs = 0;
  1053. EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
  1054. &bs, info));
  1055. if (ret == EFI_BUFFER_TOO_SMALL) {
  1056. info = malloc(bs);
  1057. EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
  1058. &bs, info));
  1059. }
  1060. if (ret != EFI_SUCCESS)
  1061. goto error;
  1062. ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
  1063. if (ret)
  1064. goto error;
  1065. EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
  1066. error:
  1067. free(info);
  1068. EFI_CALL(f->close(f));
  1069. if (ret != EFI_SUCCESS) {
  1070. efi_free_pool(*buffer);
  1071. *buffer = NULL;
  1072. }
  1073. return ret;
  1074. }
  1075. /*
  1076. * Load an EFI image into memory.
  1077. *
  1078. * This function implements the LoadImage service.
  1079. * See the Unified Extensible Firmware Interface (UEFI) specification
  1080. * for details.
  1081. *
  1082. * @boot_policy true for request originating from the boot manager
  1083. * @parent_image the calles's image handle
  1084. * @file_path the path of the image to load
  1085. * @source_buffer memory location from which the image is installed
  1086. * @source_size size of the memory area from which the image is
  1087. * installed
  1088. * @image_handle handle for the newly installed image
  1089. * @return status code
  1090. */
  1091. static efi_status_t EFIAPI efi_load_image(bool boot_policy,
  1092. efi_handle_t parent_image,
  1093. struct efi_device_path *file_path,
  1094. void *source_buffer,
  1095. unsigned long source_size,
  1096. efi_handle_t *image_handle)
  1097. {
  1098. struct efi_loaded_image *info;
  1099. struct efi_object *obj;
  1100. EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
  1101. file_path, source_buffer, source_size, image_handle);
  1102. info = calloc(1, sizeof(*info));
  1103. obj = calloc(1, sizeof(*obj));
  1104. if (!source_buffer) {
  1105. struct efi_device_path *dp, *fp;
  1106. efi_status_t ret;
  1107. ret = efi_load_image_from_path(file_path, &source_buffer);
  1108. if (ret != EFI_SUCCESS) {
  1109. free(info);
  1110. free(obj);
  1111. return EFI_EXIT(ret);
  1112. }
  1113. /*
  1114. * split file_path which contains both the device and
  1115. * file parts:
  1116. */
  1117. efi_dp_split_file_path(file_path, &dp, &fp);
  1118. efi_setup_loaded_image(info, obj, dp, fp);
  1119. } else {
  1120. /* In this case, file_path is the "device" path, ie.
  1121. * something like a HARDWARE_DEVICE:MEMORY_MAPPED
  1122. */
  1123. efi_setup_loaded_image(info, obj, file_path, NULL);
  1124. }
  1125. info->reserved = efi_load_pe(source_buffer, info);
  1126. if (!info->reserved) {
  1127. free(info);
  1128. free(obj);
  1129. return EFI_EXIT(EFI_UNSUPPORTED);
  1130. }
  1131. info->system_table = &systab;
  1132. info->parent_handle = parent_image;
  1133. *image_handle = info;
  1134. return EFI_EXIT(EFI_SUCCESS);
  1135. }
  1136. /*
  1137. * Call the entry point of an image.
  1138. *
  1139. * This function implements the StartImage service.
  1140. * See the Unified Extensible Firmware Interface (UEFI) specification
  1141. * for details.
  1142. *
  1143. * @image_handle handle of the image
  1144. * @exit_data_size size of the buffer
  1145. * @exit_data buffer to receive the exit data of the called image
  1146. * @return status code
  1147. */
  1148. static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
  1149. unsigned long *exit_data_size,
  1150. s16 **exit_data)
  1151. {
  1152. ulong (*entry)(void *image_handle, struct efi_system_table *st);
  1153. struct efi_loaded_image *info = image_handle;
  1154. EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
  1155. entry = info->reserved;
  1156. efi_is_direct_boot = false;
  1157. /* call the image! */
  1158. if (setjmp(&info->exit_jmp)) {
  1159. /* We returned from the child image */
  1160. return EFI_EXIT(info->exit_status);
  1161. }
  1162. __efi_nesting_dec();
  1163. __efi_exit_check();
  1164. entry(image_handle, &systab);
  1165. __efi_entry_check();
  1166. __efi_nesting_inc();
  1167. /* Should usually never get here */
  1168. return EFI_EXIT(EFI_SUCCESS);
  1169. }
  1170. /*
  1171. * Leave an EFI application or driver.
  1172. *
  1173. * This function implements the Exit service.
  1174. * See the Unified Extensible Firmware Interface (UEFI) specification
  1175. * for details.
  1176. *
  1177. * @image_handle handle of the application or driver that is exiting
  1178. * @exit_status status code
  1179. * @exit_data_size size of the buffer in bytes
  1180. * @exit_data buffer with data describing an error
  1181. * @return status code
  1182. */
  1183. static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
  1184. efi_status_t exit_status, unsigned long exit_data_size,
  1185. int16_t *exit_data)
  1186. {
  1187. struct efi_loaded_image *loaded_image_info = (void*)image_handle;
  1188. EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
  1189. exit_data_size, exit_data);
  1190. /* Make sure entry/exit counts for EFI world cross-overs match */
  1191. __efi_exit_check();
  1192. /*
  1193. * But longjmp out with the U-Boot gd, not the application's, as
  1194. * the other end is a setjmp call inside EFI context.
  1195. */
  1196. efi_restore_gd();
  1197. loaded_image_info->exit_status = exit_status;
  1198. longjmp(&loaded_image_info->exit_jmp, 1);
  1199. panic("EFI application exited");
  1200. }
  1201. /*
  1202. * Unload an EFI image.
  1203. *
  1204. * This function implements the UnloadImage service.
  1205. * See the Unified Extensible Firmware Interface (UEFI) specification
  1206. * for details.
  1207. *
  1208. * @image_handle handle of the image to be unloaded
  1209. * @return status code
  1210. */
  1211. static efi_status_t EFIAPI efi_unload_image(void *image_handle)
  1212. {
  1213. struct efi_object *efiobj;
  1214. EFI_ENTRY("%p", image_handle);
  1215. efiobj = efi_search_obj(image_handle);
  1216. if (efiobj)
  1217. list_del(&efiobj->link);
  1218. return EFI_EXIT(EFI_SUCCESS);
  1219. }
  1220. /*
  1221. * Fix up caches for EFI payloads if necessary.
  1222. */
  1223. static void efi_exit_caches(void)
  1224. {
  1225. #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
  1226. /*
  1227. * Grub on 32bit ARM needs to have caches disabled before jumping into
  1228. * a zImage, but does not know of all cache layers. Give it a hand.
  1229. */
  1230. if (efi_is_direct_boot)
  1231. cleanup_before_linux();
  1232. #endif
  1233. }
  1234. /*
  1235. * Stop boot services.
  1236. *
  1237. * This function implements the ExitBootServices service.
  1238. * See the Unified Extensible Firmware Interface (UEFI) specification
  1239. * for details.
  1240. *
  1241. * @image_handle handle of the loaded image
  1242. * @map_key key of the memory map
  1243. * @return status code
  1244. */
  1245. static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
  1246. unsigned long map_key)
  1247. {
  1248. int i;
  1249. EFI_ENTRY("%p, %ld", image_handle, map_key);
  1250. /* Notify that ExitBootServices is invoked. */
  1251. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  1252. if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
  1253. continue;
  1254. efi_signal_event(&efi_events[i]);
  1255. }
  1256. /* Make sure that notification functions are not called anymore */
  1257. efi_tpl = TPL_HIGH_LEVEL;
  1258. /* XXX Should persist EFI variables here */
  1259. board_quiesce_devices();
  1260. /* Fix up caches for EFI payloads if necessary */
  1261. efi_exit_caches();
  1262. /* This stops all lingering devices */
  1263. bootm_disable_interrupts();
  1264. /* Give the payload some time to boot */
  1265. efi_set_watchdog(0);
  1266. WATCHDOG_RESET();
  1267. return EFI_EXIT(EFI_SUCCESS);
  1268. }
  1269. /*
  1270. * Get next value of the counter.
  1271. *
  1272. * This function implements the NextMonotonicCount service.
  1273. * See the Unified Extensible Firmware Interface (UEFI) specification
  1274. * for details.
  1275. *
  1276. * @count returned value of the counter
  1277. * @return status code
  1278. */
  1279. static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
  1280. {
  1281. static uint64_t mono = 0;
  1282. EFI_ENTRY("%p", count);
  1283. *count = mono++;
  1284. return EFI_EXIT(EFI_SUCCESS);
  1285. }
  1286. /*
  1287. * Sleep.
  1288. *
  1289. * This function implements the Stall sercive.
  1290. * See the Unified Extensible Firmware Interface (UEFI) specification
  1291. * for details.
  1292. *
  1293. * @microseconds period to sleep in microseconds
  1294. * @return status code
  1295. */
  1296. static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
  1297. {
  1298. EFI_ENTRY("%ld", microseconds);
  1299. udelay(microseconds);
  1300. return EFI_EXIT(EFI_SUCCESS);
  1301. }
  1302. /*
  1303. * Reset the watchdog timer.
  1304. *
  1305. * This function implements the SetWatchdogTimer service.
  1306. * See the Unified Extensible Firmware Interface (UEFI) specification
  1307. * for details.
  1308. *
  1309. * @timeout seconds before reset by watchdog
  1310. * @watchdog_code code to be logged when resetting
  1311. * @data_size size of buffer in bytes
  1312. * @watchdog_data buffer with data describing the reset reason
  1313. * @return status code
  1314. */
  1315. static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
  1316. uint64_t watchdog_code,
  1317. unsigned long data_size,
  1318. uint16_t *watchdog_data)
  1319. {
  1320. EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
  1321. data_size, watchdog_data);
  1322. return EFI_EXIT(efi_set_watchdog(timeout));
  1323. }
  1324. /*
  1325. * Connect a controller to a driver.
  1326. *
  1327. * This function implements the ConnectController service.
  1328. * See the Unified Extensible Firmware Interface (UEFI) specification
  1329. * for details.
  1330. *
  1331. * @controller_handle handle of the controller
  1332. * @driver_image_handle handle of the driver
  1333. * @remain_device_path device path of a child controller
  1334. * @recursive true to connect all child controllers
  1335. * @return status code
  1336. */
  1337. static efi_status_t EFIAPI efi_connect_controller(
  1338. efi_handle_t controller_handle,
  1339. efi_handle_t *driver_image_handle,
  1340. struct efi_device_path *remain_device_path,
  1341. bool recursive)
  1342. {
  1343. EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
  1344. remain_device_path, recursive);
  1345. return EFI_EXIT(EFI_NOT_FOUND);
  1346. }
  1347. /*
  1348. * Disconnect a controller from a driver.
  1349. *
  1350. * This function implements the DisconnectController service.
  1351. * See the Unified Extensible Firmware Interface (UEFI) specification
  1352. * for details.
  1353. *
  1354. * @controller_handle handle of the controller
  1355. * @driver_image_handle handle of the driver
  1356. * @child_handle handle of the child to destroy
  1357. * @return status code
  1358. */
  1359. static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
  1360. void *driver_image_handle,
  1361. void *child_handle)
  1362. {
  1363. EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
  1364. child_handle);
  1365. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1366. }
  1367. /*
  1368. * Close a protocol.
  1369. *
  1370. * This function implements the CloseProtocol service.
  1371. * See the Unified Extensible Firmware Interface (UEFI) specification
  1372. * for details.
  1373. *
  1374. * @handle handle on which the protocol shall be closed
  1375. * @protocol GUID of the protocol to close
  1376. * @agent_handle handle of the driver
  1377. * @controller_handle handle of the controller
  1378. * @return status code
  1379. */
  1380. static efi_status_t EFIAPI efi_close_protocol(void *handle,
  1381. const efi_guid_t *protocol,
  1382. void *agent_handle,
  1383. void *controller_handle)
  1384. {
  1385. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
  1386. controller_handle);
  1387. return EFI_EXIT(EFI_NOT_FOUND);
  1388. }
  1389. /*
  1390. * Provide information about then open status of a protocol on a handle
  1391. *
  1392. * This function implements the OpenProtocolInformation service.
  1393. * See the Unified Extensible Firmware Interface (UEFI) specification
  1394. * for details.
  1395. *
  1396. * @handle handle for which the information shall be retrieved
  1397. * @protocol GUID of the protocol
  1398. * @entry_buffer buffer to receive the open protocol information
  1399. * @entry_count number of entries available in the buffer
  1400. * @return status code
  1401. */
  1402. static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
  1403. const efi_guid_t *protocol,
  1404. struct efi_open_protocol_info_entry **entry_buffer,
  1405. efi_uintn_t *entry_count)
  1406. {
  1407. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
  1408. entry_count);
  1409. return EFI_EXIT(EFI_NOT_FOUND);
  1410. }
  1411. /*
  1412. * Get protocols installed on a handle.
  1413. *
  1414. * This function implements the ProtocolsPerHandleService.
  1415. * See the Unified Extensible Firmware Interface (UEFI) specification
  1416. * for details.
  1417. *
  1418. * @handle handle for which the information is retrieved
  1419. * @protocol_buffer buffer with protocol GUIDs
  1420. * @protocol_buffer_count number of entries in the buffer
  1421. * @return status code
  1422. */
  1423. static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
  1424. efi_guid_t ***protocol_buffer,
  1425. efi_uintn_t *protocol_buffer_count)
  1426. {
  1427. unsigned long buffer_size;
  1428. struct efi_object *efiobj;
  1429. unsigned long i, j;
  1430. struct list_head *lhandle;
  1431. efi_status_t r;
  1432. EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
  1433. protocol_buffer_count);
  1434. if (!handle || !protocol_buffer || !protocol_buffer_count)
  1435. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1436. *protocol_buffer = NULL;
  1437. *protocol_buffer_count = 0;
  1438. list_for_each(lhandle, &efi_obj_list) {
  1439. efiobj = list_entry(lhandle, struct efi_object, link);
  1440. if (efiobj->handle != handle)
  1441. continue;
  1442. /* Count protocols */
  1443. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  1444. if (efiobj->protocols[i].guid)
  1445. ++*protocol_buffer_count;
  1446. }
  1447. /* Copy guids */
  1448. if (*protocol_buffer_count) {
  1449. buffer_size = sizeof(efi_guid_t *) *
  1450. *protocol_buffer_count;
  1451. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
  1452. buffer_size,
  1453. (void **)protocol_buffer);
  1454. if (r != EFI_SUCCESS)
  1455. return EFI_EXIT(r);
  1456. j = 0;
  1457. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
  1458. if (efiobj->protocols[i].guid) {
  1459. (*protocol_buffer)[j] = (void *)
  1460. efiobj->protocols[i].guid;
  1461. ++j;
  1462. }
  1463. }
  1464. }
  1465. break;
  1466. }
  1467. return EFI_EXIT(EFI_SUCCESS);
  1468. }
  1469. /*
  1470. * Locate handles implementing a protocol.
  1471. *
  1472. * This function implements the LocateHandleBuffer service.
  1473. * See the Unified Extensible Firmware Interface (UEFI) specification
  1474. * for details.
  1475. *
  1476. * @search_type selection criterion
  1477. * @protocol GUID of the protocol
  1478. * @search_key registration key
  1479. * @no_handles number of returned handles
  1480. * @buffer buffer with the returned handles
  1481. * @return status code
  1482. */
  1483. static efi_status_t EFIAPI efi_locate_handle_buffer(
  1484. enum efi_locate_search_type search_type,
  1485. const efi_guid_t *protocol, void *search_key,
  1486. efi_uintn_t *no_handles, efi_handle_t **buffer)
  1487. {
  1488. efi_status_t r;
  1489. efi_uintn_t buffer_size = 0;
  1490. EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
  1491. no_handles, buffer);
  1492. if (!no_handles || !buffer) {
  1493. r = EFI_INVALID_PARAMETER;
  1494. goto out;
  1495. }
  1496. *no_handles = 0;
  1497. *buffer = NULL;
  1498. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  1499. *buffer);
  1500. if (r != EFI_BUFFER_TOO_SMALL)
  1501. goto out;
  1502. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
  1503. (void **)buffer);
  1504. if (r != EFI_SUCCESS)
  1505. goto out;
  1506. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  1507. *buffer);
  1508. if (r == EFI_SUCCESS)
  1509. *no_handles = buffer_size / sizeof(void *);
  1510. out:
  1511. return EFI_EXIT(r);
  1512. }
  1513. /*
  1514. * Find an interface implementing a protocol.
  1515. *
  1516. * This function implements the LocateProtocol service.
  1517. * See the Unified Extensible Firmware Interface (UEFI) specification
  1518. * for details.
  1519. *
  1520. * @protocol GUID of the protocol
  1521. * @registration registration key passed to the notification function
  1522. * @protocol_interface interface implementing the protocol
  1523. * @return status code
  1524. */
  1525. static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
  1526. void *registration,
  1527. void **protocol_interface)
  1528. {
  1529. struct list_head *lhandle;
  1530. int i;
  1531. EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
  1532. if (!protocol || !protocol_interface)
  1533. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1534. EFI_PRINT_GUID("protocol", protocol);
  1535. list_for_each(lhandle, &efi_obj_list) {
  1536. struct efi_object *efiobj;
  1537. efiobj = list_entry(lhandle, struct efi_object, link);
  1538. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  1539. struct efi_handler *handler = &efiobj->protocols[i];
  1540. if (!handler->guid)
  1541. continue;
  1542. if (!guidcmp(handler->guid, protocol)) {
  1543. *protocol_interface =
  1544. handler->protocol_interface;
  1545. return EFI_EXIT(EFI_SUCCESS);
  1546. }
  1547. }
  1548. }
  1549. *protocol_interface = NULL;
  1550. return EFI_EXIT(EFI_NOT_FOUND);
  1551. }
  1552. /*
  1553. * Install multiple protocol interfaces.
  1554. *
  1555. * This function implements the MultipleProtocolInterfaces service.
  1556. * See the Unified Extensible Firmware Interface (UEFI) specification
  1557. * for details.
  1558. *
  1559. * @handle handle on which the protocol interfaces shall be installed
  1560. * @... NULL terminated argument list with pairs of protocol GUIDS and
  1561. * interfaces
  1562. * @return status code
  1563. */
  1564. static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
  1565. void **handle, ...)
  1566. {
  1567. EFI_ENTRY("%p", handle);
  1568. va_list argptr;
  1569. const efi_guid_t *protocol;
  1570. void *protocol_interface;
  1571. efi_status_t r = EFI_SUCCESS;
  1572. int i = 0;
  1573. if (!handle)
  1574. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1575. va_start(argptr, handle);
  1576. for (;;) {
  1577. protocol = va_arg(argptr, efi_guid_t*);
  1578. if (!protocol)
  1579. break;
  1580. protocol_interface = va_arg(argptr, void*);
  1581. r = EFI_CALL(efi_install_protocol_interface(
  1582. handle, protocol,
  1583. EFI_NATIVE_INTERFACE,
  1584. protocol_interface));
  1585. if (r != EFI_SUCCESS)
  1586. break;
  1587. i++;
  1588. }
  1589. va_end(argptr);
  1590. if (r == EFI_SUCCESS)
  1591. return EFI_EXIT(r);
  1592. /* If an error occurred undo all changes. */
  1593. va_start(argptr, handle);
  1594. for (; i; --i) {
  1595. protocol = va_arg(argptr, efi_guid_t*);
  1596. protocol_interface = va_arg(argptr, void*);
  1597. EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
  1598. protocol_interface));
  1599. }
  1600. va_end(argptr);
  1601. return EFI_EXIT(r);
  1602. }
  1603. /*
  1604. * Uninstall multiple protocol interfaces.
  1605. *
  1606. * This function implements the UninstallMultipleProtocolInterfaces service.
  1607. * See the Unified Extensible Firmware Interface (UEFI) specification
  1608. * for details.
  1609. *
  1610. * @handle handle from which the protocol interfaces shall be removed
  1611. * @... NULL terminated argument list with pairs of protocol GUIDS and
  1612. * interfaces
  1613. * @return status code
  1614. */
  1615. static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
  1616. void *handle, ...)
  1617. {
  1618. EFI_ENTRY("%p", handle);
  1619. va_list argptr;
  1620. const efi_guid_t *protocol;
  1621. void *protocol_interface;
  1622. efi_status_t r = EFI_SUCCESS;
  1623. size_t i = 0;
  1624. if (!handle)
  1625. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1626. va_start(argptr, handle);
  1627. for (;;) {
  1628. protocol = va_arg(argptr, efi_guid_t*);
  1629. if (!protocol)
  1630. break;
  1631. protocol_interface = va_arg(argptr, void*);
  1632. r = EFI_CALL(efi_uninstall_protocol_interface(
  1633. handle, protocol,
  1634. protocol_interface));
  1635. if (r != EFI_SUCCESS)
  1636. break;
  1637. i++;
  1638. }
  1639. va_end(argptr);
  1640. if (r == EFI_SUCCESS)
  1641. return EFI_EXIT(r);
  1642. /* If an error occurred undo all changes. */
  1643. va_start(argptr, handle);
  1644. for (; i; --i) {
  1645. protocol = va_arg(argptr, efi_guid_t*);
  1646. protocol_interface = va_arg(argptr, void*);
  1647. EFI_CALL(efi_install_protocol_interface(&handle, protocol,
  1648. EFI_NATIVE_INTERFACE,
  1649. protocol_interface));
  1650. }
  1651. va_end(argptr);
  1652. return EFI_EXIT(r);
  1653. }
  1654. /*
  1655. * Calculate cyclic redundancy code.
  1656. *
  1657. * This function implements the CalculateCrc32 service.
  1658. * See the Unified Extensible Firmware Interface (UEFI) specification
  1659. * for details.
  1660. *
  1661. * @data buffer with data
  1662. * @data_size size of buffer in bytes
  1663. * @crc32_p cyclic redundancy code
  1664. * @return status code
  1665. */
  1666. static efi_status_t EFIAPI efi_calculate_crc32(void *data,
  1667. unsigned long data_size,
  1668. uint32_t *crc32_p)
  1669. {
  1670. EFI_ENTRY("%p, %ld", data, data_size);
  1671. *crc32_p = crc32(0, data, data_size);
  1672. return EFI_EXIT(EFI_SUCCESS);
  1673. }
  1674. /*
  1675. * Copy memory.
  1676. *
  1677. * This function implements the CopyMem service.
  1678. * See the Unified Extensible Firmware Interface (UEFI) specification
  1679. * for details.
  1680. *
  1681. * @destination destination of the copy operation
  1682. * @source source of the copy operation
  1683. * @length number of bytes to copy
  1684. */
  1685. static void EFIAPI efi_copy_mem(void *destination, const void *source,
  1686. size_t length)
  1687. {
  1688. EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
  1689. memcpy(destination, source, length);
  1690. EFI_EXIT(EFI_SUCCESS);
  1691. }
  1692. /*
  1693. * Fill memory with a byte value.
  1694. *
  1695. * This function implements the SetMem service.
  1696. * See the Unified Extensible Firmware Interface (UEFI) specification
  1697. * for details.
  1698. *
  1699. * @buffer buffer to fill
  1700. * @size size of buffer in bytes
  1701. * @value byte to copy to the buffer
  1702. */
  1703. static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
  1704. {
  1705. EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
  1706. memset(buffer, value, size);
  1707. EFI_EXIT(EFI_SUCCESS);
  1708. }
  1709. /*
  1710. * Open protocol interface on a handle.
  1711. *
  1712. * This function implements the OpenProtocol interface.
  1713. * See the Unified Extensible Firmware Interface (UEFI) specification
  1714. * for details.
  1715. *
  1716. * @handle handle on which the protocol shall be opened
  1717. * @protocol GUID of the protocol
  1718. * @protocol_interface interface implementing the protocol
  1719. * @agent_handle handle of the driver
  1720. * @controller_handle handle of the controller
  1721. * @attributes attributes indicating how to open the protocol
  1722. * @return status code
  1723. */
  1724. static efi_status_t EFIAPI efi_open_protocol(
  1725. void *handle, const efi_guid_t *protocol,
  1726. void **protocol_interface, void *agent_handle,
  1727. void *controller_handle, uint32_t attributes)
  1728. {
  1729. struct list_head *lhandle;
  1730. int i;
  1731. efi_status_t r = EFI_INVALID_PARAMETER;
  1732. EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
  1733. protocol_interface, agent_handle, controller_handle,
  1734. attributes);
  1735. if (!handle || !protocol ||
  1736. (!protocol_interface && attributes !=
  1737. EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
  1738. goto out;
  1739. }
  1740. EFI_PRINT_GUID("protocol", protocol);
  1741. switch (attributes) {
  1742. case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
  1743. case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
  1744. case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
  1745. break;
  1746. case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
  1747. if (controller_handle == handle)
  1748. goto out;
  1749. case EFI_OPEN_PROTOCOL_BY_DRIVER:
  1750. case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
  1751. if (controller_handle == NULL)
  1752. goto out;
  1753. case EFI_OPEN_PROTOCOL_EXCLUSIVE:
  1754. if (agent_handle == NULL)
  1755. goto out;
  1756. break;
  1757. default:
  1758. goto out;
  1759. }
  1760. list_for_each(lhandle, &efi_obj_list) {
  1761. struct efi_object *efiobj;
  1762. efiobj = list_entry(lhandle, struct efi_object, link);
  1763. if (efiobj->handle != handle)
  1764. continue;
  1765. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  1766. struct efi_handler *handler = &efiobj->protocols[i];
  1767. const efi_guid_t *hprotocol = handler->guid;
  1768. if (!hprotocol)
  1769. continue;
  1770. if (!guidcmp(hprotocol, protocol)) {
  1771. if (attributes !=
  1772. EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
  1773. *protocol_interface =
  1774. handler->protocol_interface;
  1775. }
  1776. r = EFI_SUCCESS;
  1777. goto out;
  1778. }
  1779. }
  1780. goto unsupported;
  1781. }
  1782. unsupported:
  1783. r = EFI_UNSUPPORTED;
  1784. out:
  1785. return EFI_EXIT(r);
  1786. }
  1787. /*
  1788. * Get interface of a protocol on a handle.
  1789. *
  1790. * This function implements the HandleProtocol service.
  1791. * See the Unified Extensible Firmware Interface (UEFI) specification
  1792. * for details.
  1793. *
  1794. * @handle handle on which the protocol shall be opened
  1795. * @protocol GUID of the protocol
  1796. * @protocol_interface interface implementing the protocol
  1797. * @return status code
  1798. */
  1799. static efi_status_t EFIAPI efi_handle_protocol(void *handle,
  1800. const efi_guid_t *protocol,
  1801. void **protocol_interface)
  1802. {
  1803. return efi_open_protocol(handle, protocol, protocol_interface, NULL,
  1804. NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
  1805. }
  1806. static const struct efi_boot_services efi_boot_services = {
  1807. .hdr = {
  1808. .headersize = sizeof(struct efi_table_hdr),
  1809. },
  1810. .raise_tpl = efi_raise_tpl,
  1811. .restore_tpl = efi_restore_tpl,
  1812. .allocate_pages = efi_allocate_pages_ext,
  1813. .free_pages = efi_free_pages_ext,
  1814. .get_memory_map = efi_get_memory_map_ext,
  1815. .allocate_pool = efi_allocate_pool_ext,
  1816. .free_pool = efi_free_pool_ext,
  1817. .create_event = efi_create_event_ext,
  1818. .set_timer = efi_set_timer_ext,
  1819. .wait_for_event = efi_wait_for_event,
  1820. .signal_event = efi_signal_event_ext,
  1821. .close_event = efi_close_event,
  1822. .check_event = efi_check_event,
  1823. .install_protocol_interface = efi_install_protocol_interface,
  1824. .reinstall_protocol_interface = efi_reinstall_protocol_interface,
  1825. .uninstall_protocol_interface = efi_uninstall_protocol_interface,
  1826. .handle_protocol = efi_handle_protocol,
  1827. .reserved = NULL,
  1828. .register_protocol_notify = efi_register_protocol_notify,
  1829. .locate_handle = efi_locate_handle_ext,
  1830. .locate_device_path = efi_locate_device_path,
  1831. .install_configuration_table = efi_install_configuration_table_ext,
  1832. .load_image = efi_load_image,
  1833. .start_image = efi_start_image,
  1834. .exit = efi_exit,
  1835. .unload_image = efi_unload_image,
  1836. .exit_boot_services = efi_exit_boot_services,
  1837. .get_next_monotonic_count = efi_get_next_monotonic_count,
  1838. .stall = efi_stall,
  1839. .set_watchdog_timer = efi_set_watchdog_timer,
  1840. .connect_controller = efi_connect_controller,
  1841. .disconnect_controller = efi_disconnect_controller,
  1842. .open_protocol = efi_open_protocol,
  1843. .close_protocol = efi_close_protocol,
  1844. .open_protocol_information = efi_open_protocol_information,
  1845. .protocols_per_handle = efi_protocols_per_handle,
  1846. .locate_handle_buffer = efi_locate_handle_buffer,
  1847. .locate_protocol = efi_locate_protocol,
  1848. .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
  1849. .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
  1850. .calculate_crc32 = efi_calculate_crc32,
  1851. .copy_mem = efi_copy_mem,
  1852. .set_mem = efi_set_mem,
  1853. };
  1854. static uint16_t __efi_runtime_data firmware_vendor[] =
  1855. { 'D','a','s',' ','U','-','b','o','o','t',0 };
  1856. struct efi_system_table __efi_runtime_data systab = {
  1857. .hdr = {
  1858. .signature = EFI_SYSTEM_TABLE_SIGNATURE,
  1859. .revision = 0x20005, /* 2.5 */
  1860. .headersize = sizeof(struct efi_table_hdr),
  1861. },
  1862. .fw_vendor = (long)firmware_vendor,
  1863. .con_in = (void*)&efi_con_in,
  1864. .con_out = (void*)&efi_con_out,
  1865. .std_err = (void*)&efi_con_out,
  1866. .runtime = (void*)&efi_runtime_services,
  1867. .boottime = (void*)&efi_boot_services,
  1868. .nr_tables = 0,
  1869. .tables = (void*)efi_conf_table,
  1870. };