efi_boottime.c 54 KB

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