efi_boottime.c 54 KB

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