efi_boottime.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176
  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. int i;
  897. switch (search_type) {
  898. case ALL_HANDLES:
  899. return 0;
  900. case BY_REGISTER_NOTIFY:
  901. /* RegisterProtocolNotify is not implemented yet */
  902. return -1;
  903. case BY_PROTOCOL:
  904. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  905. const efi_guid_t *guid = efiobj->protocols[i].guid;
  906. if (guid && !guidcmp(guid, protocol))
  907. return 0;
  908. }
  909. return -1;
  910. }
  911. return -1;
  912. }
  913. /*
  914. * Locate handles implementing a protocol.
  915. *
  916. * This function is meant for U-Boot internal calls. For the API implementation
  917. * of the LocateHandle service see efi_locate_handle_ext.
  918. *
  919. * @search_type selection criterion
  920. * @protocol GUID of the protocol
  921. * @search_key registration key
  922. * @buffer_size size of the buffer to receive the handles in bytes
  923. * @buffer buffer to receive the relevant handles
  924. * @return status code
  925. */
  926. static efi_status_t efi_locate_handle(
  927. enum efi_locate_search_type search_type,
  928. const efi_guid_t *protocol, void *search_key,
  929. efi_uintn_t *buffer_size, efi_handle_t *buffer)
  930. {
  931. struct efi_object *efiobj;
  932. efi_uintn_t size = 0;
  933. /* Check parameters */
  934. switch (search_type) {
  935. case ALL_HANDLES:
  936. break;
  937. case BY_REGISTER_NOTIFY:
  938. if (!search_key)
  939. return EFI_INVALID_PARAMETER;
  940. /* RegisterProtocolNotify is not implemented yet */
  941. return EFI_UNSUPPORTED;
  942. case BY_PROTOCOL:
  943. if (!protocol)
  944. return EFI_INVALID_PARAMETER;
  945. break;
  946. default:
  947. return EFI_INVALID_PARAMETER;
  948. }
  949. /*
  950. * efi_locate_handle_buffer uses this function for
  951. * the calculation of the necessary buffer size.
  952. * So do not require a buffer for buffersize == 0.
  953. */
  954. if (!buffer_size || (*buffer_size && !buffer))
  955. return EFI_INVALID_PARAMETER;
  956. /* Count how much space we need */
  957. list_for_each_entry(efiobj, &efi_obj_list, link) {
  958. if (!efi_search(search_type, protocol, search_key, efiobj))
  959. size += sizeof(void*);
  960. }
  961. if (*buffer_size < size) {
  962. *buffer_size = size;
  963. return EFI_BUFFER_TOO_SMALL;
  964. }
  965. *buffer_size = size;
  966. if (size == 0)
  967. return EFI_NOT_FOUND;
  968. /* Then fill the array */
  969. list_for_each_entry(efiobj, &efi_obj_list, link) {
  970. if (!efi_search(search_type, protocol, search_key, efiobj))
  971. *buffer++ = efiobj->handle;
  972. }
  973. return EFI_SUCCESS;
  974. }
  975. /*
  976. * Locate handles implementing a protocol.
  977. *
  978. * This function implements the LocateHandle service.
  979. * See the Unified Extensible Firmware Interface (UEFI) specification
  980. * for details.
  981. *
  982. * @search_type selection criterion
  983. * @protocol GUID of the protocol
  984. * @search_key registration key
  985. * @buffer_size size of the buffer to receive the handles in bytes
  986. * @buffer buffer to receive the relevant handles
  987. * @return 0 if the handle implements the protocol
  988. */
  989. static efi_status_t EFIAPI efi_locate_handle_ext(
  990. enum efi_locate_search_type search_type,
  991. const efi_guid_t *protocol, void *search_key,
  992. efi_uintn_t *buffer_size, efi_handle_t *buffer)
  993. {
  994. EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
  995. buffer_size, buffer);
  996. return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
  997. buffer_size, buffer));
  998. }
  999. /*
  1000. * Get the device path and handle of an device implementing a protocol.
  1001. *
  1002. * This function implements the LocateDevicePath service.
  1003. * See the Unified Extensible Firmware Interface (UEFI) specification
  1004. * for details.
  1005. *
  1006. * @protocol GUID of the protocol
  1007. * @device_path device path
  1008. * @device handle of the device
  1009. * @return status code
  1010. */
  1011. static efi_status_t EFIAPI efi_locate_device_path(
  1012. const efi_guid_t *protocol,
  1013. struct efi_device_path **device_path,
  1014. efi_handle_t *device)
  1015. {
  1016. struct efi_object *efiobj;
  1017. EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
  1018. efiobj = efi_dp_find_obj(*device_path, device_path);
  1019. if (!efiobj)
  1020. return EFI_EXIT(EFI_NOT_FOUND);
  1021. *device = efiobj->handle;
  1022. return EFI_EXIT(EFI_SUCCESS);
  1023. }
  1024. /* Collapses configuration table entries, removing index i */
  1025. static void efi_remove_configuration_table(int i)
  1026. {
  1027. struct efi_configuration_table *this = &efi_conf_table[i];
  1028. struct efi_configuration_table *next = &efi_conf_table[i+1];
  1029. struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
  1030. memmove(this, next, (ulong)end - (ulong)next);
  1031. systab.nr_tables--;
  1032. }
  1033. /*
  1034. * Adds, updates, or removes a configuration table.
  1035. *
  1036. * This function is used for internal calls. For the API implementation of the
  1037. * InstallConfigurationTable service see efi_install_configuration_table_ext.
  1038. *
  1039. * @guid GUID of the installed table
  1040. * @table table to be installed
  1041. * @return status code
  1042. */
  1043. efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
  1044. {
  1045. int i;
  1046. /* Check for guid override */
  1047. for (i = 0; i < systab.nr_tables; i++) {
  1048. if (!guidcmp(guid, &efi_conf_table[i].guid)) {
  1049. if (table)
  1050. efi_conf_table[i].table = table;
  1051. else
  1052. efi_remove_configuration_table(i);
  1053. return EFI_SUCCESS;
  1054. }
  1055. }
  1056. if (!table)
  1057. return EFI_NOT_FOUND;
  1058. /* No override, check for overflow */
  1059. if (i >= ARRAY_SIZE(efi_conf_table))
  1060. return EFI_OUT_OF_RESOURCES;
  1061. /* Add a new entry */
  1062. memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
  1063. efi_conf_table[i].table = table;
  1064. systab.nr_tables = i + 1;
  1065. return EFI_SUCCESS;
  1066. }
  1067. /*
  1068. * Adds, updates, or removes a configuration table.
  1069. *
  1070. * This function implements the InstallConfigurationTable service.
  1071. * See the Unified Extensible Firmware Interface (UEFI) specification
  1072. * for details.
  1073. *
  1074. * @guid GUID of the installed table
  1075. * @table table to be installed
  1076. * @return status code
  1077. */
  1078. static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
  1079. void *table)
  1080. {
  1081. EFI_ENTRY("%pUl, %p", guid, table);
  1082. return EFI_EXIT(efi_install_configuration_table(guid, table));
  1083. }
  1084. /*
  1085. * Initialize a loaded_image_info + loaded_image_info object with correct
  1086. * protocols, boot-device, etc.
  1087. *
  1088. * @info loaded image info to be passed to the entry point of the
  1089. * image
  1090. * @obj internal object associated with the loaded image
  1091. * @device_path device path of the loaded image
  1092. * @file_path file path of the loaded image
  1093. */
  1094. void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
  1095. struct efi_device_path *device_path,
  1096. struct efi_device_path *file_path)
  1097. {
  1098. obj->handle = info;
  1099. /*
  1100. * When asking for the device path interface, return
  1101. * bootefi_device_path
  1102. */
  1103. obj->protocols[0].guid = &efi_guid_device_path;
  1104. obj->protocols[0].protocol_interface = device_path;
  1105. /*
  1106. * When asking for the loaded_image interface, just
  1107. * return handle which points to loaded_image_info
  1108. */
  1109. obj->protocols[1].guid = &efi_guid_loaded_image;
  1110. obj->protocols[1].protocol_interface = info;
  1111. obj->protocols[2].guid = &efi_guid_console_control;
  1112. obj->protocols[2].protocol_interface = (void *)&efi_console_control;
  1113. obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
  1114. obj->protocols[3].protocol_interface =
  1115. (void *)&efi_device_path_to_text;
  1116. info->file_path = file_path;
  1117. if (device_path)
  1118. info->device_handle = efi_dp_find_obj(device_path, NULL);
  1119. list_add_tail(&obj->link, &efi_obj_list);
  1120. }
  1121. /*
  1122. * Load an image using a file path.
  1123. *
  1124. * @file_path the path of the image to load
  1125. * @buffer buffer containing the loaded image
  1126. * @return status code
  1127. */
  1128. efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
  1129. void **buffer)
  1130. {
  1131. struct efi_file_info *info = NULL;
  1132. struct efi_file_handle *f;
  1133. static efi_status_t ret;
  1134. uint64_t bs;
  1135. f = efi_file_from_path(file_path);
  1136. if (!f)
  1137. return EFI_DEVICE_ERROR;
  1138. bs = 0;
  1139. EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
  1140. &bs, info));
  1141. if (ret == EFI_BUFFER_TOO_SMALL) {
  1142. info = malloc(bs);
  1143. EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
  1144. &bs, info));
  1145. }
  1146. if (ret != EFI_SUCCESS)
  1147. goto error;
  1148. ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
  1149. if (ret)
  1150. goto error;
  1151. EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
  1152. error:
  1153. free(info);
  1154. EFI_CALL(f->close(f));
  1155. if (ret != EFI_SUCCESS) {
  1156. efi_free_pool(*buffer);
  1157. *buffer = NULL;
  1158. }
  1159. return ret;
  1160. }
  1161. /*
  1162. * Load an EFI image into memory.
  1163. *
  1164. * This function implements the LoadImage service.
  1165. * See the Unified Extensible Firmware Interface (UEFI) specification
  1166. * for details.
  1167. *
  1168. * @boot_policy true for request originating from the boot manager
  1169. * @parent_image the calles's image handle
  1170. * @file_path the path of the image to load
  1171. * @source_buffer memory location from which the image is installed
  1172. * @source_size size of the memory area from which the image is
  1173. * installed
  1174. * @image_handle handle for the newly installed image
  1175. * @return status code
  1176. */
  1177. static efi_status_t EFIAPI efi_load_image(bool boot_policy,
  1178. efi_handle_t parent_image,
  1179. struct efi_device_path *file_path,
  1180. void *source_buffer,
  1181. unsigned long source_size,
  1182. efi_handle_t *image_handle)
  1183. {
  1184. struct efi_loaded_image *info;
  1185. struct efi_object *obj;
  1186. EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
  1187. file_path, source_buffer, source_size, image_handle);
  1188. info = calloc(1, sizeof(*info));
  1189. obj = calloc(1, sizeof(*obj));
  1190. if (!source_buffer) {
  1191. struct efi_device_path *dp, *fp;
  1192. efi_status_t ret;
  1193. ret = efi_load_image_from_path(file_path, &source_buffer);
  1194. if (ret != EFI_SUCCESS) {
  1195. free(info);
  1196. free(obj);
  1197. return EFI_EXIT(ret);
  1198. }
  1199. /*
  1200. * split file_path which contains both the device and
  1201. * file parts:
  1202. */
  1203. efi_dp_split_file_path(file_path, &dp, &fp);
  1204. efi_setup_loaded_image(info, obj, dp, fp);
  1205. } else {
  1206. /* In this case, file_path is the "device" path, ie.
  1207. * something like a HARDWARE_DEVICE:MEMORY_MAPPED
  1208. */
  1209. efi_setup_loaded_image(info, obj, file_path, NULL);
  1210. }
  1211. info->reserved = efi_load_pe(source_buffer, info);
  1212. if (!info->reserved) {
  1213. free(info);
  1214. free(obj);
  1215. return EFI_EXIT(EFI_UNSUPPORTED);
  1216. }
  1217. info->system_table = &systab;
  1218. info->parent_handle = parent_image;
  1219. *image_handle = info;
  1220. return EFI_EXIT(EFI_SUCCESS);
  1221. }
  1222. /*
  1223. * Call the entry point of an image.
  1224. *
  1225. * This function implements the StartImage service.
  1226. * See the Unified Extensible Firmware Interface (UEFI) specification
  1227. * for details.
  1228. *
  1229. * @image_handle handle of the image
  1230. * @exit_data_size size of the buffer
  1231. * @exit_data buffer to receive the exit data of the called image
  1232. * @return status code
  1233. */
  1234. static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
  1235. unsigned long *exit_data_size,
  1236. s16 **exit_data)
  1237. {
  1238. ulong (*entry)(void *image_handle, struct efi_system_table *st);
  1239. struct efi_loaded_image *info = image_handle;
  1240. EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
  1241. entry = info->reserved;
  1242. efi_is_direct_boot = false;
  1243. /* call the image! */
  1244. if (setjmp(&info->exit_jmp)) {
  1245. /* We returned from the child image */
  1246. return EFI_EXIT(info->exit_status);
  1247. }
  1248. __efi_nesting_dec();
  1249. __efi_exit_check();
  1250. entry(image_handle, &systab);
  1251. __efi_entry_check();
  1252. __efi_nesting_inc();
  1253. /* Should usually never get here */
  1254. return EFI_EXIT(EFI_SUCCESS);
  1255. }
  1256. /*
  1257. * Leave an EFI application or driver.
  1258. *
  1259. * This function implements the Exit service.
  1260. * See the Unified Extensible Firmware Interface (UEFI) specification
  1261. * for details.
  1262. *
  1263. * @image_handle handle of the application or driver that is exiting
  1264. * @exit_status status code
  1265. * @exit_data_size size of the buffer in bytes
  1266. * @exit_data buffer with data describing an error
  1267. * @return status code
  1268. */
  1269. static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
  1270. efi_status_t exit_status, unsigned long exit_data_size,
  1271. int16_t *exit_data)
  1272. {
  1273. struct efi_loaded_image *loaded_image_info = (void*)image_handle;
  1274. EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
  1275. exit_data_size, exit_data);
  1276. /* Make sure entry/exit counts for EFI world cross-overs match */
  1277. __efi_exit_check();
  1278. /*
  1279. * But longjmp out with the U-Boot gd, not the application's, as
  1280. * the other end is a setjmp call inside EFI context.
  1281. */
  1282. efi_restore_gd();
  1283. loaded_image_info->exit_status = exit_status;
  1284. longjmp(&loaded_image_info->exit_jmp, 1);
  1285. panic("EFI application exited");
  1286. }
  1287. /*
  1288. * Unload an EFI image.
  1289. *
  1290. * This function implements the UnloadImage service.
  1291. * See the Unified Extensible Firmware Interface (UEFI) specification
  1292. * for details.
  1293. *
  1294. * @image_handle handle of the image to be unloaded
  1295. * @return status code
  1296. */
  1297. static efi_status_t EFIAPI efi_unload_image(void *image_handle)
  1298. {
  1299. struct efi_object *efiobj;
  1300. EFI_ENTRY("%p", image_handle);
  1301. efiobj = efi_search_obj(image_handle);
  1302. if (efiobj)
  1303. list_del(&efiobj->link);
  1304. return EFI_EXIT(EFI_SUCCESS);
  1305. }
  1306. /*
  1307. * Fix up caches for EFI payloads if necessary.
  1308. */
  1309. static void efi_exit_caches(void)
  1310. {
  1311. #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
  1312. /*
  1313. * Grub on 32bit ARM needs to have caches disabled before jumping into
  1314. * a zImage, but does not know of all cache layers. Give it a hand.
  1315. */
  1316. if (efi_is_direct_boot)
  1317. cleanup_before_linux();
  1318. #endif
  1319. }
  1320. /*
  1321. * Stop boot services.
  1322. *
  1323. * This function implements the ExitBootServices service.
  1324. * See the Unified Extensible Firmware Interface (UEFI) specification
  1325. * for details.
  1326. *
  1327. * @image_handle handle of the loaded image
  1328. * @map_key key of the memory map
  1329. * @return status code
  1330. */
  1331. static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
  1332. unsigned long map_key)
  1333. {
  1334. int i;
  1335. EFI_ENTRY("%p, %ld", image_handle, map_key);
  1336. /* Notify that ExitBootServices is invoked. */
  1337. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  1338. if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
  1339. continue;
  1340. efi_signal_event(&efi_events[i]);
  1341. }
  1342. /* Make sure that notification functions are not called anymore */
  1343. efi_tpl = TPL_HIGH_LEVEL;
  1344. /* XXX Should persist EFI variables here */
  1345. board_quiesce_devices();
  1346. /* Fix up caches for EFI payloads if necessary */
  1347. efi_exit_caches();
  1348. /* This stops all lingering devices */
  1349. bootm_disable_interrupts();
  1350. /* Give the payload some time to boot */
  1351. efi_set_watchdog(0);
  1352. WATCHDOG_RESET();
  1353. return EFI_EXIT(EFI_SUCCESS);
  1354. }
  1355. /*
  1356. * Get next value of the counter.
  1357. *
  1358. * This function implements the NextMonotonicCount service.
  1359. * See the Unified Extensible Firmware Interface (UEFI) specification
  1360. * for details.
  1361. *
  1362. * @count returned value of the counter
  1363. * @return status code
  1364. */
  1365. static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
  1366. {
  1367. static uint64_t mono = 0;
  1368. EFI_ENTRY("%p", count);
  1369. *count = mono++;
  1370. return EFI_EXIT(EFI_SUCCESS);
  1371. }
  1372. /*
  1373. * Sleep.
  1374. *
  1375. * This function implements the Stall sercive.
  1376. * See the Unified Extensible Firmware Interface (UEFI) specification
  1377. * for details.
  1378. *
  1379. * @microseconds period to sleep in microseconds
  1380. * @return status code
  1381. */
  1382. static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
  1383. {
  1384. EFI_ENTRY("%ld", microseconds);
  1385. udelay(microseconds);
  1386. return EFI_EXIT(EFI_SUCCESS);
  1387. }
  1388. /*
  1389. * Reset the watchdog timer.
  1390. *
  1391. * This function implements the SetWatchdogTimer service.
  1392. * See the Unified Extensible Firmware Interface (UEFI) specification
  1393. * for details.
  1394. *
  1395. * @timeout seconds before reset by watchdog
  1396. * @watchdog_code code to be logged when resetting
  1397. * @data_size size of buffer in bytes
  1398. * @watchdog_data buffer with data describing the reset reason
  1399. * @return status code
  1400. */
  1401. static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
  1402. uint64_t watchdog_code,
  1403. unsigned long data_size,
  1404. uint16_t *watchdog_data)
  1405. {
  1406. EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
  1407. data_size, watchdog_data);
  1408. return EFI_EXIT(efi_set_watchdog(timeout));
  1409. }
  1410. /*
  1411. * Connect a controller to a driver.
  1412. *
  1413. * This function implements the ConnectController service.
  1414. * See the Unified Extensible Firmware Interface (UEFI) specification
  1415. * for details.
  1416. *
  1417. * @controller_handle handle of the controller
  1418. * @driver_image_handle handle of the driver
  1419. * @remain_device_path device path of a child controller
  1420. * @recursive true to connect all child controllers
  1421. * @return status code
  1422. */
  1423. static efi_status_t EFIAPI efi_connect_controller(
  1424. efi_handle_t controller_handle,
  1425. efi_handle_t *driver_image_handle,
  1426. struct efi_device_path *remain_device_path,
  1427. bool recursive)
  1428. {
  1429. EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
  1430. remain_device_path, recursive);
  1431. return EFI_EXIT(EFI_NOT_FOUND);
  1432. }
  1433. /*
  1434. * Disconnect a controller from a driver.
  1435. *
  1436. * This function implements the DisconnectController service.
  1437. * See the Unified Extensible Firmware Interface (UEFI) specification
  1438. * for details.
  1439. *
  1440. * @controller_handle handle of the controller
  1441. * @driver_image_handle handle of the driver
  1442. * @child_handle handle of the child to destroy
  1443. * @return status code
  1444. */
  1445. static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
  1446. void *driver_image_handle,
  1447. void *child_handle)
  1448. {
  1449. EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
  1450. child_handle);
  1451. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1452. }
  1453. /*
  1454. * Close a protocol.
  1455. *
  1456. * This function implements the CloseProtocol service.
  1457. * See the Unified Extensible Firmware Interface (UEFI) specification
  1458. * for details.
  1459. *
  1460. * @handle handle on which the protocol shall be closed
  1461. * @protocol GUID of the protocol to close
  1462. * @agent_handle handle of the driver
  1463. * @controller_handle handle of the controller
  1464. * @return status code
  1465. */
  1466. static efi_status_t EFIAPI efi_close_protocol(void *handle,
  1467. const efi_guid_t *protocol,
  1468. void *agent_handle,
  1469. void *controller_handle)
  1470. {
  1471. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
  1472. controller_handle);
  1473. return EFI_EXIT(EFI_NOT_FOUND);
  1474. }
  1475. /*
  1476. * Provide information about then open status of a protocol on a handle
  1477. *
  1478. * This function implements the OpenProtocolInformation service.
  1479. * See the Unified Extensible Firmware Interface (UEFI) specification
  1480. * for details.
  1481. *
  1482. * @handle handle for which the information shall be retrieved
  1483. * @protocol GUID of the protocol
  1484. * @entry_buffer buffer to receive the open protocol information
  1485. * @entry_count number of entries available in the buffer
  1486. * @return status code
  1487. */
  1488. static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
  1489. const efi_guid_t *protocol,
  1490. struct efi_open_protocol_info_entry **entry_buffer,
  1491. efi_uintn_t *entry_count)
  1492. {
  1493. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
  1494. entry_count);
  1495. return EFI_EXIT(EFI_NOT_FOUND);
  1496. }
  1497. /*
  1498. * Get protocols installed on a handle.
  1499. *
  1500. * This function implements the ProtocolsPerHandleService.
  1501. * See the Unified Extensible Firmware Interface (UEFI) specification
  1502. * for details.
  1503. *
  1504. * @handle handle for which the information is retrieved
  1505. * @protocol_buffer buffer with protocol GUIDs
  1506. * @protocol_buffer_count number of entries in the buffer
  1507. * @return status code
  1508. */
  1509. static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
  1510. efi_guid_t ***protocol_buffer,
  1511. efi_uintn_t *protocol_buffer_count)
  1512. {
  1513. unsigned long buffer_size;
  1514. struct efi_object *efiobj;
  1515. unsigned long i, j;
  1516. struct list_head *lhandle;
  1517. efi_status_t r;
  1518. EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
  1519. protocol_buffer_count);
  1520. if (!handle || !protocol_buffer || !protocol_buffer_count)
  1521. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1522. *protocol_buffer = NULL;
  1523. *protocol_buffer_count = 0;
  1524. list_for_each(lhandle, &efi_obj_list) {
  1525. efiobj = list_entry(lhandle, struct efi_object, link);
  1526. if (efiobj->handle != handle)
  1527. continue;
  1528. /* Count protocols */
  1529. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  1530. if (efiobj->protocols[i].guid)
  1531. ++*protocol_buffer_count;
  1532. }
  1533. /* Copy guids */
  1534. if (*protocol_buffer_count) {
  1535. buffer_size = sizeof(efi_guid_t *) *
  1536. *protocol_buffer_count;
  1537. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
  1538. buffer_size,
  1539. (void **)protocol_buffer);
  1540. if (r != EFI_SUCCESS)
  1541. return EFI_EXIT(r);
  1542. j = 0;
  1543. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
  1544. if (efiobj->protocols[i].guid) {
  1545. (*protocol_buffer)[j] = (void *)
  1546. efiobj->protocols[i].guid;
  1547. ++j;
  1548. }
  1549. }
  1550. }
  1551. break;
  1552. }
  1553. return EFI_EXIT(EFI_SUCCESS);
  1554. }
  1555. /*
  1556. * Locate handles implementing a protocol.
  1557. *
  1558. * This function implements the LocateHandleBuffer service.
  1559. * See the Unified Extensible Firmware Interface (UEFI) specification
  1560. * for details.
  1561. *
  1562. * @search_type selection criterion
  1563. * @protocol GUID of the protocol
  1564. * @search_key registration key
  1565. * @no_handles number of returned handles
  1566. * @buffer buffer with the returned handles
  1567. * @return status code
  1568. */
  1569. static efi_status_t EFIAPI efi_locate_handle_buffer(
  1570. enum efi_locate_search_type search_type,
  1571. const efi_guid_t *protocol, void *search_key,
  1572. efi_uintn_t *no_handles, efi_handle_t **buffer)
  1573. {
  1574. efi_status_t r;
  1575. efi_uintn_t buffer_size = 0;
  1576. EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
  1577. no_handles, buffer);
  1578. if (!no_handles || !buffer) {
  1579. r = EFI_INVALID_PARAMETER;
  1580. goto out;
  1581. }
  1582. *no_handles = 0;
  1583. *buffer = NULL;
  1584. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  1585. *buffer);
  1586. if (r != EFI_BUFFER_TOO_SMALL)
  1587. goto out;
  1588. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
  1589. (void **)buffer);
  1590. if (r != EFI_SUCCESS)
  1591. goto out;
  1592. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  1593. *buffer);
  1594. if (r == EFI_SUCCESS)
  1595. *no_handles = buffer_size / sizeof(void *);
  1596. out:
  1597. return EFI_EXIT(r);
  1598. }
  1599. /*
  1600. * Find an interface implementing a protocol.
  1601. *
  1602. * This function implements the LocateProtocol service.
  1603. * See the Unified Extensible Firmware Interface (UEFI) specification
  1604. * for details.
  1605. *
  1606. * @protocol GUID of the protocol
  1607. * @registration registration key passed to the notification function
  1608. * @protocol_interface interface implementing the protocol
  1609. * @return status code
  1610. */
  1611. static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
  1612. void *registration,
  1613. void **protocol_interface)
  1614. {
  1615. struct list_head *lhandle;
  1616. int i;
  1617. EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
  1618. if (!protocol || !protocol_interface)
  1619. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1620. EFI_PRINT_GUID("protocol", protocol);
  1621. list_for_each(lhandle, &efi_obj_list) {
  1622. struct efi_object *efiobj;
  1623. efiobj = list_entry(lhandle, struct efi_object, link);
  1624. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  1625. struct efi_handler *handler = &efiobj->protocols[i];
  1626. if (!handler->guid)
  1627. continue;
  1628. if (!guidcmp(handler->guid, protocol)) {
  1629. *protocol_interface =
  1630. handler->protocol_interface;
  1631. return EFI_EXIT(EFI_SUCCESS);
  1632. }
  1633. }
  1634. }
  1635. *protocol_interface = NULL;
  1636. return EFI_EXIT(EFI_NOT_FOUND);
  1637. }
  1638. /*
  1639. * Install multiple protocol interfaces.
  1640. *
  1641. * This function implements the MultipleProtocolInterfaces service.
  1642. * See the Unified Extensible Firmware Interface (UEFI) specification
  1643. * for details.
  1644. *
  1645. * @handle handle on which the protocol interfaces shall be installed
  1646. * @... NULL terminated argument list with pairs of protocol GUIDS and
  1647. * interfaces
  1648. * @return status code
  1649. */
  1650. static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
  1651. void **handle, ...)
  1652. {
  1653. EFI_ENTRY("%p", handle);
  1654. va_list argptr;
  1655. const efi_guid_t *protocol;
  1656. void *protocol_interface;
  1657. efi_status_t r = EFI_SUCCESS;
  1658. int i = 0;
  1659. if (!handle)
  1660. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1661. va_start(argptr, handle);
  1662. for (;;) {
  1663. protocol = va_arg(argptr, efi_guid_t*);
  1664. if (!protocol)
  1665. break;
  1666. protocol_interface = va_arg(argptr, void*);
  1667. r = EFI_CALL(efi_install_protocol_interface(
  1668. handle, protocol,
  1669. EFI_NATIVE_INTERFACE,
  1670. protocol_interface));
  1671. if (r != EFI_SUCCESS)
  1672. break;
  1673. i++;
  1674. }
  1675. va_end(argptr);
  1676. if (r == EFI_SUCCESS)
  1677. return EFI_EXIT(r);
  1678. /* If an error occurred undo all changes. */
  1679. va_start(argptr, handle);
  1680. for (; i; --i) {
  1681. protocol = va_arg(argptr, efi_guid_t*);
  1682. protocol_interface = va_arg(argptr, void*);
  1683. EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
  1684. protocol_interface));
  1685. }
  1686. va_end(argptr);
  1687. return EFI_EXIT(r);
  1688. }
  1689. /*
  1690. * Uninstall multiple protocol interfaces.
  1691. *
  1692. * This function implements the UninstallMultipleProtocolInterfaces service.
  1693. * See the Unified Extensible Firmware Interface (UEFI) specification
  1694. * for details.
  1695. *
  1696. * @handle handle from which the protocol interfaces shall be removed
  1697. * @... NULL terminated argument list with pairs of protocol GUIDS and
  1698. * interfaces
  1699. * @return status code
  1700. */
  1701. static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
  1702. void *handle, ...)
  1703. {
  1704. EFI_ENTRY("%p", handle);
  1705. va_list argptr;
  1706. const efi_guid_t *protocol;
  1707. void *protocol_interface;
  1708. efi_status_t r = EFI_SUCCESS;
  1709. size_t i = 0;
  1710. if (!handle)
  1711. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1712. va_start(argptr, handle);
  1713. for (;;) {
  1714. protocol = va_arg(argptr, efi_guid_t*);
  1715. if (!protocol)
  1716. break;
  1717. protocol_interface = va_arg(argptr, void*);
  1718. r = EFI_CALL(efi_uninstall_protocol_interface(
  1719. handle, protocol,
  1720. protocol_interface));
  1721. if (r != EFI_SUCCESS)
  1722. break;
  1723. i++;
  1724. }
  1725. va_end(argptr);
  1726. if (r == EFI_SUCCESS)
  1727. return EFI_EXIT(r);
  1728. /* If an error occurred undo all changes. */
  1729. va_start(argptr, handle);
  1730. for (; i; --i) {
  1731. protocol = va_arg(argptr, efi_guid_t*);
  1732. protocol_interface = va_arg(argptr, void*);
  1733. EFI_CALL(efi_install_protocol_interface(&handle, protocol,
  1734. EFI_NATIVE_INTERFACE,
  1735. protocol_interface));
  1736. }
  1737. va_end(argptr);
  1738. return EFI_EXIT(r);
  1739. }
  1740. /*
  1741. * Calculate cyclic redundancy code.
  1742. *
  1743. * This function implements the CalculateCrc32 service.
  1744. * See the Unified Extensible Firmware Interface (UEFI) specification
  1745. * for details.
  1746. *
  1747. * @data buffer with data
  1748. * @data_size size of buffer in bytes
  1749. * @crc32_p cyclic redundancy code
  1750. * @return status code
  1751. */
  1752. static efi_status_t EFIAPI efi_calculate_crc32(void *data,
  1753. unsigned long data_size,
  1754. uint32_t *crc32_p)
  1755. {
  1756. EFI_ENTRY("%p, %ld", data, data_size);
  1757. *crc32_p = crc32(0, data, data_size);
  1758. return EFI_EXIT(EFI_SUCCESS);
  1759. }
  1760. /*
  1761. * Copy memory.
  1762. *
  1763. * This function implements the CopyMem service.
  1764. * See the Unified Extensible Firmware Interface (UEFI) specification
  1765. * for details.
  1766. *
  1767. * @destination destination of the copy operation
  1768. * @source source of the copy operation
  1769. * @length number of bytes to copy
  1770. */
  1771. static void EFIAPI efi_copy_mem(void *destination, const void *source,
  1772. size_t length)
  1773. {
  1774. EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
  1775. memcpy(destination, source, length);
  1776. EFI_EXIT(EFI_SUCCESS);
  1777. }
  1778. /*
  1779. * Fill memory with a byte value.
  1780. *
  1781. * This function implements the SetMem service.
  1782. * See the Unified Extensible Firmware Interface (UEFI) specification
  1783. * for details.
  1784. *
  1785. * @buffer buffer to fill
  1786. * @size size of buffer in bytes
  1787. * @value byte to copy to the buffer
  1788. */
  1789. static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
  1790. {
  1791. EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
  1792. memset(buffer, value, size);
  1793. EFI_EXIT(EFI_SUCCESS);
  1794. }
  1795. /*
  1796. * Open protocol interface on a handle.
  1797. *
  1798. * This function implements the OpenProtocol interface.
  1799. * See the Unified Extensible Firmware Interface (UEFI) specification
  1800. * for details.
  1801. *
  1802. * @handle handle on which the protocol shall be opened
  1803. * @protocol GUID of the protocol
  1804. * @protocol_interface interface implementing the protocol
  1805. * @agent_handle handle of the driver
  1806. * @controller_handle handle of the controller
  1807. * @attributes attributes indicating how to open the protocol
  1808. * @return status code
  1809. */
  1810. static efi_status_t EFIAPI efi_open_protocol(
  1811. void *handle, const efi_guid_t *protocol,
  1812. void **protocol_interface, void *agent_handle,
  1813. void *controller_handle, uint32_t attributes)
  1814. {
  1815. struct list_head *lhandle;
  1816. int i;
  1817. efi_status_t r = EFI_INVALID_PARAMETER;
  1818. EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
  1819. protocol_interface, agent_handle, controller_handle,
  1820. attributes);
  1821. if (!handle || !protocol ||
  1822. (!protocol_interface && attributes !=
  1823. EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
  1824. goto out;
  1825. }
  1826. EFI_PRINT_GUID("protocol", protocol);
  1827. switch (attributes) {
  1828. case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
  1829. case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
  1830. case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
  1831. break;
  1832. case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
  1833. if (controller_handle == handle)
  1834. goto out;
  1835. case EFI_OPEN_PROTOCOL_BY_DRIVER:
  1836. case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
  1837. if (controller_handle == NULL)
  1838. goto out;
  1839. case EFI_OPEN_PROTOCOL_EXCLUSIVE:
  1840. if (agent_handle == NULL)
  1841. goto out;
  1842. break;
  1843. default:
  1844. goto out;
  1845. }
  1846. list_for_each(lhandle, &efi_obj_list) {
  1847. struct efi_object *efiobj;
  1848. efiobj = list_entry(lhandle, struct efi_object, link);
  1849. if (efiobj->handle != handle)
  1850. continue;
  1851. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  1852. struct efi_handler *handler = &efiobj->protocols[i];
  1853. const efi_guid_t *hprotocol = handler->guid;
  1854. if (!hprotocol)
  1855. continue;
  1856. if (!guidcmp(hprotocol, protocol)) {
  1857. if (attributes !=
  1858. EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
  1859. *protocol_interface =
  1860. handler->protocol_interface;
  1861. }
  1862. r = EFI_SUCCESS;
  1863. goto out;
  1864. }
  1865. }
  1866. goto unsupported;
  1867. }
  1868. unsupported:
  1869. r = EFI_UNSUPPORTED;
  1870. out:
  1871. return EFI_EXIT(r);
  1872. }
  1873. /*
  1874. * Get interface of a protocol on a handle.
  1875. *
  1876. * This function implements the HandleProtocol service.
  1877. * See the Unified Extensible Firmware Interface (UEFI) specification
  1878. * for details.
  1879. *
  1880. * @handle handle on which the protocol shall be opened
  1881. * @protocol GUID of the protocol
  1882. * @protocol_interface interface implementing the protocol
  1883. * @return status code
  1884. */
  1885. static efi_status_t EFIAPI efi_handle_protocol(void *handle,
  1886. const efi_guid_t *protocol,
  1887. void **protocol_interface)
  1888. {
  1889. return efi_open_protocol(handle, protocol, protocol_interface, NULL,
  1890. NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
  1891. }
  1892. static const struct efi_boot_services efi_boot_services = {
  1893. .hdr = {
  1894. .headersize = sizeof(struct efi_table_hdr),
  1895. },
  1896. .raise_tpl = efi_raise_tpl,
  1897. .restore_tpl = efi_restore_tpl,
  1898. .allocate_pages = efi_allocate_pages_ext,
  1899. .free_pages = efi_free_pages_ext,
  1900. .get_memory_map = efi_get_memory_map_ext,
  1901. .allocate_pool = efi_allocate_pool_ext,
  1902. .free_pool = efi_free_pool_ext,
  1903. .create_event = efi_create_event_ext,
  1904. .set_timer = efi_set_timer_ext,
  1905. .wait_for_event = efi_wait_for_event,
  1906. .signal_event = efi_signal_event_ext,
  1907. .close_event = efi_close_event,
  1908. .check_event = efi_check_event,
  1909. .install_protocol_interface = efi_install_protocol_interface,
  1910. .reinstall_protocol_interface = efi_reinstall_protocol_interface,
  1911. .uninstall_protocol_interface = efi_uninstall_protocol_interface,
  1912. .handle_protocol = efi_handle_protocol,
  1913. .reserved = NULL,
  1914. .register_protocol_notify = efi_register_protocol_notify,
  1915. .locate_handle = efi_locate_handle_ext,
  1916. .locate_device_path = efi_locate_device_path,
  1917. .install_configuration_table = efi_install_configuration_table_ext,
  1918. .load_image = efi_load_image,
  1919. .start_image = efi_start_image,
  1920. .exit = efi_exit,
  1921. .unload_image = efi_unload_image,
  1922. .exit_boot_services = efi_exit_boot_services,
  1923. .get_next_monotonic_count = efi_get_next_monotonic_count,
  1924. .stall = efi_stall,
  1925. .set_watchdog_timer = efi_set_watchdog_timer,
  1926. .connect_controller = efi_connect_controller,
  1927. .disconnect_controller = efi_disconnect_controller,
  1928. .open_protocol = efi_open_protocol,
  1929. .close_protocol = efi_close_protocol,
  1930. .open_protocol_information = efi_open_protocol_information,
  1931. .protocols_per_handle = efi_protocols_per_handle,
  1932. .locate_handle_buffer = efi_locate_handle_buffer,
  1933. .locate_protocol = efi_locate_protocol,
  1934. .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
  1935. .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
  1936. .calculate_crc32 = efi_calculate_crc32,
  1937. .copy_mem = efi_copy_mem,
  1938. .set_mem = efi_set_mem,
  1939. };
  1940. static uint16_t __efi_runtime_data firmware_vendor[] =
  1941. { 'D','a','s',' ','U','-','b','o','o','t',0 };
  1942. struct efi_system_table __efi_runtime_data systab = {
  1943. .hdr = {
  1944. .signature = EFI_SYSTEM_TABLE_SIGNATURE,
  1945. .revision = 0x20005, /* 2.5 */
  1946. .headersize = sizeof(struct efi_table_hdr),
  1947. },
  1948. .fw_vendor = (long)firmware_vendor,
  1949. .con_in = (void*)&efi_con_in,
  1950. .con_out = (void*)&efi_con_out,
  1951. .std_err = (void*)&efi_con_out,
  1952. .runtime = (void*)&efi_runtime_services,
  1953. .boottime = (void*)&efi_boot_services,
  1954. .nr_tables = 0,
  1955. .tables = (void*)efi_conf_table,
  1956. };