efi_boottime.c 54 KB

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