efi_boottime.c 57 KB

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