efi_boottime.c 57 KB

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