efi_boottime.c 56 KB

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