efi_boottime.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI application boot time services
  4. *
  5. * Copyright (c) 2016 Alexander Graf
  6. */
  7. #include <common.h>
  8. #include <div64.h>
  9. #include <efi_loader.h>
  10. #include <environment.h>
  11. #include <malloc.h>
  12. #include <linux/libfdt_env.h>
  13. #include <u-boot/crc.h>
  14. #include <bootm.h>
  15. #include <inttypes.h>
  16. #include <watchdog.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /* Task priority level */
  19. static efi_uintn_t efi_tpl = TPL_APPLICATION;
  20. /* This list contains all the EFI objects our payload has access to */
  21. LIST_HEAD(efi_obj_list);
  22. /* List of all events */
  23. LIST_HEAD(efi_events);
  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. #ifdef CONFIG_ARM
  32. /*
  33. * The "gd" pointer lives in a register on ARM and AArch64 that we declare
  34. * fixed when compiling U-Boot. However, the payload does not know about that
  35. * restriction so we need to manually swap its and our view of that register on
  36. * EFI callback entry/exit.
  37. */
  38. static volatile void *efi_gd, *app_gd;
  39. #endif
  40. static int entry_count;
  41. static int nesting_level;
  42. /* GUID of the device tree table */
  43. const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
  44. /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
  45. const efi_guid_t efi_guid_driver_binding_protocol =
  46. EFI_DRIVER_BINDING_PROTOCOL_GUID;
  47. /* event group ExitBootServices() invoked */
  48. const efi_guid_t efi_guid_event_group_exit_boot_services =
  49. EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
  50. /* event group SetVirtualAddressMap() invoked */
  51. const efi_guid_t efi_guid_event_group_virtual_address_change =
  52. EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
  53. /* event group memory map changed */
  54. const efi_guid_t efi_guid_event_group_memory_map_change =
  55. EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
  56. /* event group boot manager about to boot */
  57. const efi_guid_t efi_guid_event_group_ready_to_boot =
  58. EFI_EVENT_GROUP_READY_TO_BOOT;
  59. /* event group ResetSystem() invoked (before ExitBootServices) */
  60. const efi_guid_t efi_guid_event_group_reset_system =
  61. EFI_EVENT_GROUP_RESET_SYSTEM;
  62. static efi_status_t EFIAPI efi_disconnect_controller(
  63. efi_handle_t controller_handle,
  64. efi_handle_t driver_image_handle,
  65. efi_handle_t child_handle);
  66. /* Called on every callback entry */
  67. int __efi_entry_check(void)
  68. {
  69. int ret = entry_count++ == 0;
  70. #ifdef CONFIG_ARM
  71. assert(efi_gd);
  72. app_gd = gd;
  73. gd = efi_gd;
  74. #endif
  75. return ret;
  76. }
  77. /* Called on every callback exit */
  78. int __efi_exit_check(void)
  79. {
  80. int ret = --entry_count == 0;
  81. #ifdef CONFIG_ARM
  82. gd = app_gd;
  83. #endif
  84. return ret;
  85. }
  86. /* Called from do_bootefi_exec() */
  87. void efi_save_gd(void)
  88. {
  89. #ifdef CONFIG_ARM
  90. efi_gd = gd;
  91. #endif
  92. }
  93. /*
  94. * Special case handler for error/abort that just forces things back to u-boot
  95. * world so we can dump out an abort msg, without any care about returning back
  96. * to UEFI world.
  97. */
  98. void efi_restore_gd(void)
  99. {
  100. #ifdef CONFIG_ARM
  101. /* Only restore if we're already in EFI context */
  102. if (!efi_gd)
  103. return;
  104. gd = efi_gd;
  105. #endif
  106. }
  107. /**
  108. * indent_string() - returns a string for indenting with two spaces per level
  109. * @level: indent level
  110. *
  111. * A maximum of ten indent levels is supported. Higher indent levels will be
  112. * truncated.
  113. *
  114. * Return: A string for indenting with two spaces per level is
  115. * returned.
  116. */
  117. static const char *indent_string(int level)
  118. {
  119. const char *indent = " ";
  120. const int max = strlen(indent);
  121. level = min(max, level * 2);
  122. return &indent[max - level];
  123. }
  124. const char *__efi_nesting(void)
  125. {
  126. return indent_string(nesting_level);
  127. }
  128. const char *__efi_nesting_inc(void)
  129. {
  130. return indent_string(nesting_level++);
  131. }
  132. const char *__efi_nesting_dec(void)
  133. {
  134. return indent_string(--nesting_level);
  135. }
  136. /**
  137. * efi_queue_event() - queue an EFI event
  138. * @event: event to signal
  139. * @check_tpl: check the TPL level
  140. *
  141. * This function queues the notification function of the event for future
  142. * execution.
  143. *
  144. * The notification function is called if the task priority level of the event
  145. * is higher than the current task priority level.
  146. *
  147. * For the SignalEvent service see efi_signal_event_ext.
  148. *
  149. */
  150. static void efi_queue_event(struct efi_event *event, bool check_tpl)
  151. {
  152. if (event->notify_function) {
  153. event->is_queued = true;
  154. /* Check TPL */
  155. if (check_tpl && efi_tpl >= event->notify_tpl)
  156. return;
  157. EFI_CALL_VOID(event->notify_function(event,
  158. event->notify_context));
  159. }
  160. event->is_queued = false;
  161. }
  162. /**
  163. * is_valid_tpl() - check if the task priority level is valid
  164. *
  165. * @tpl: TPL level to check
  166. * ReturnValue: status code
  167. */
  168. efi_status_t is_valid_tpl(efi_uintn_t tpl)
  169. {
  170. switch (tpl) {
  171. case TPL_APPLICATION:
  172. case TPL_CALLBACK:
  173. case TPL_NOTIFY:
  174. case TPL_HIGH_LEVEL:
  175. return EFI_SUCCESS;
  176. default:
  177. return EFI_INVALID_PARAMETER;
  178. }
  179. }
  180. /**
  181. * efi_signal_event() - signal an EFI event
  182. * @event: event to signal
  183. * @check_tpl: check the TPL level
  184. *
  185. * This function signals an event. If the event belongs to an event group all
  186. * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
  187. * their notification function is queued.
  188. *
  189. * For the SignalEvent service see efi_signal_event_ext.
  190. */
  191. void efi_signal_event(struct efi_event *event, bool check_tpl)
  192. {
  193. if (event->group) {
  194. struct efi_event *evt;
  195. /*
  196. * The signaled state has to set before executing any
  197. * notification function
  198. */
  199. list_for_each_entry(evt, &efi_events, link) {
  200. if (!evt->group || guidcmp(evt->group, event->group))
  201. continue;
  202. if (evt->is_signaled)
  203. continue;
  204. evt->is_signaled = true;
  205. if (evt->type & EVT_NOTIFY_SIGNAL &&
  206. evt->notify_function)
  207. evt->is_queued = true;
  208. }
  209. list_for_each_entry(evt, &efi_events, link) {
  210. if (!evt->group || guidcmp(evt->group, event->group))
  211. continue;
  212. if (evt->is_queued)
  213. efi_queue_event(evt, check_tpl);
  214. }
  215. } else if (!event->is_signaled) {
  216. event->is_signaled = true;
  217. if (event->type & EVT_NOTIFY_SIGNAL)
  218. efi_queue_event(event, check_tpl);
  219. }
  220. }
  221. /**
  222. * efi_raise_tpl() - raise the task priority level
  223. * @new_tpl: new value of the task priority level
  224. *
  225. * This function implements the RaiseTpl service.
  226. *
  227. * See the Unified Extensible Firmware Interface (UEFI) specification for
  228. * details.
  229. *
  230. * Return: old value of the task priority level
  231. */
  232. static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
  233. {
  234. efi_uintn_t old_tpl = efi_tpl;
  235. EFI_ENTRY("0x%zx", new_tpl);
  236. if (new_tpl < efi_tpl)
  237. debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
  238. efi_tpl = new_tpl;
  239. if (efi_tpl > TPL_HIGH_LEVEL)
  240. efi_tpl = TPL_HIGH_LEVEL;
  241. EFI_EXIT(EFI_SUCCESS);
  242. return old_tpl;
  243. }
  244. /**
  245. * efi_restore_tpl() - lower the task priority level
  246. * @old_tpl: value of the task priority level to be restored
  247. *
  248. * This function implements the RestoreTpl service.
  249. *
  250. * See the Unified Extensible Firmware Interface (UEFI) specification for
  251. * details.
  252. */
  253. static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
  254. {
  255. EFI_ENTRY("0x%zx", old_tpl);
  256. if (old_tpl > efi_tpl)
  257. debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
  258. efi_tpl = old_tpl;
  259. if (efi_tpl > TPL_HIGH_LEVEL)
  260. efi_tpl = TPL_HIGH_LEVEL;
  261. /*
  262. * Lowering the TPL may have made queued events eligible for execution.
  263. */
  264. efi_timer_check();
  265. EFI_EXIT(EFI_SUCCESS);
  266. }
  267. /**
  268. * efi_allocate_pages_ext() - allocate memory pages
  269. * @type: type of allocation to be performed
  270. * @memory_type: usage type of the allocated memory
  271. * @pages: number of pages to be allocated
  272. * @memory: allocated memory
  273. *
  274. * This function implements the AllocatePages service.
  275. *
  276. * See the Unified Extensible Firmware Interface (UEFI) specification for
  277. * details.
  278. *
  279. * Return: status code
  280. */
  281. static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
  282. efi_uintn_t pages,
  283. uint64_t *memory)
  284. {
  285. efi_status_t r;
  286. EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
  287. r = efi_allocate_pages(type, memory_type, pages, memory);
  288. return EFI_EXIT(r);
  289. }
  290. /**
  291. * efi_free_pages_ext() - Free memory pages.
  292. * @memory: start of the memory area to be freed
  293. * @pages: number of pages to be freed
  294. *
  295. * This function implements the FreePages service.
  296. *
  297. * See the Unified Extensible Firmware Interface (UEFI) specification for
  298. * details.
  299. *
  300. * Return: status code
  301. */
  302. static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
  303. efi_uintn_t pages)
  304. {
  305. efi_status_t r;
  306. EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
  307. r = efi_free_pages(memory, pages);
  308. return EFI_EXIT(r);
  309. }
  310. /**
  311. * efi_get_memory_map_ext() - get map describing memory usage
  312. * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
  313. * on exit the size of the copied memory map
  314. * @memory_map: buffer to which the memory map is written
  315. * @map_key: key for the memory map
  316. * @descriptor_size: size of an individual memory descriptor
  317. * @descriptor_version: version number of the memory descriptor structure
  318. *
  319. * This function implements the GetMemoryMap service.
  320. *
  321. * See the Unified Extensible Firmware Interface (UEFI) specification for
  322. * details.
  323. *
  324. * Return: status code
  325. */
  326. static efi_status_t EFIAPI efi_get_memory_map_ext(
  327. efi_uintn_t *memory_map_size,
  328. struct efi_mem_desc *memory_map,
  329. efi_uintn_t *map_key,
  330. efi_uintn_t *descriptor_size,
  331. uint32_t *descriptor_version)
  332. {
  333. efi_status_t r;
  334. EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
  335. map_key, descriptor_size, descriptor_version);
  336. r = efi_get_memory_map(memory_map_size, memory_map, map_key,
  337. descriptor_size, descriptor_version);
  338. return EFI_EXIT(r);
  339. }
  340. /**
  341. * efi_allocate_pool_ext() - allocate memory from pool
  342. * @pool_type: type of the pool from which memory is to be allocated
  343. * @size: number of bytes to be allocated
  344. * @buffer: allocated memory
  345. *
  346. * This function implements the AllocatePool service.
  347. *
  348. * See the Unified Extensible Firmware Interface (UEFI) specification for
  349. * details.
  350. *
  351. * Return: status code
  352. */
  353. static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
  354. efi_uintn_t size,
  355. void **buffer)
  356. {
  357. efi_status_t r;
  358. EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
  359. r = efi_allocate_pool(pool_type, size, buffer);
  360. return EFI_EXIT(r);
  361. }
  362. /**
  363. * efi_free_pool_ext() - free memory from pool
  364. * @buffer: start of memory to be freed
  365. *
  366. * This function implements the FreePool service.
  367. *
  368. * See the Unified Extensible Firmware Interface (UEFI) specification for
  369. * details.
  370. *
  371. * Return: status code
  372. */
  373. static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
  374. {
  375. efi_status_t r;
  376. EFI_ENTRY("%p", buffer);
  377. r = efi_free_pool(buffer);
  378. return EFI_EXIT(r);
  379. }
  380. /**
  381. * efi_add_handle() - add a new object to the object list
  382. * @obj: object to be added
  383. *
  384. * The protocols list is initialized. The object handle is set.
  385. */
  386. void efi_add_handle(struct efi_object *obj)
  387. {
  388. if (!obj)
  389. return;
  390. INIT_LIST_HEAD(&obj->protocols);
  391. obj->handle = obj;
  392. list_add_tail(&obj->link, &efi_obj_list);
  393. }
  394. /**
  395. * efi_create_handle() - create handle
  396. * @handle: new handle
  397. *
  398. * Return: status code
  399. */
  400. efi_status_t efi_create_handle(efi_handle_t *handle)
  401. {
  402. struct efi_object *obj;
  403. obj = calloc(1, sizeof(struct efi_object));
  404. if (!obj)
  405. return EFI_OUT_OF_RESOURCES;
  406. efi_add_handle(obj);
  407. *handle = obj->handle;
  408. return EFI_SUCCESS;
  409. }
  410. /**
  411. * efi_search_protocol() - find a protocol on a handle.
  412. * @handle: handle
  413. * @protocol_guid: GUID of the protocol
  414. * @handler: reference to the protocol
  415. *
  416. * Return: status code
  417. */
  418. efi_status_t efi_search_protocol(const efi_handle_t handle,
  419. const efi_guid_t *protocol_guid,
  420. struct efi_handler **handler)
  421. {
  422. struct efi_object *efiobj;
  423. struct list_head *lhandle;
  424. if (!handle || !protocol_guid)
  425. return EFI_INVALID_PARAMETER;
  426. efiobj = efi_search_obj(handle);
  427. if (!efiobj)
  428. return EFI_INVALID_PARAMETER;
  429. list_for_each(lhandle, &efiobj->protocols) {
  430. struct efi_handler *protocol;
  431. protocol = list_entry(lhandle, struct efi_handler, link);
  432. if (!guidcmp(protocol->guid, protocol_guid)) {
  433. if (handler)
  434. *handler = protocol;
  435. return EFI_SUCCESS;
  436. }
  437. }
  438. return EFI_NOT_FOUND;
  439. }
  440. /**
  441. * efi_remove_protocol() - delete protocol from a handle
  442. * @handle: handle from which the protocol shall be deleted
  443. * @protocol: GUID of the protocol to be deleted
  444. * @protocol_interface: interface of the protocol implementation
  445. *
  446. * Return: status code
  447. */
  448. efi_status_t efi_remove_protocol(const efi_handle_t handle,
  449. const efi_guid_t *protocol,
  450. void *protocol_interface)
  451. {
  452. struct efi_handler *handler;
  453. efi_status_t ret;
  454. ret = efi_search_protocol(handle, protocol, &handler);
  455. if (ret != EFI_SUCCESS)
  456. return ret;
  457. if (guidcmp(handler->guid, protocol))
  458. return EFI_INVALID_PARAMETER;
  459. if (handler->protocol_interface != protocol_interface)
  460. return EFI_INVALID_PARAMETER;
  461. list_del(&handler->link);
  462. free(handler);
  463. return EFI_SUCCESS;
  464. }
  465. /**
  466. * efi_remove_all_protocols() - delete all protocols from a handle
  467. * @handle: handle from which the protocols shall be deleted
  468. *
  469. * Return: status code
  470. */
  471. efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
  472. {
  473. struct efi_object *efiobj;
  474. struct efi_handler *protocol;
  475. struct efi_handler *pos;
  476. efiobj = efi_search_obj(handle);
  477. if (!efiobj)
  478. return EFI_INVALID_PARAMETER;
  479. list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
  480. efi_status_t ret;
  481. ret = efi_remove_protocol(handle, protocol->guid,
  482. protocol->protocol_interface);
  483. if (ret != EFI_SUCCESS)
  484. return ret;
  485. }
  486. return EFI_SUCCESS;
  487. }
  488. /**
  489. * efi_delete_handle() - delete handle
  490. *
  491. * @obj: handle to delete
  492. */
  493. void efi_delete_handle(struct efi_object *obj)
  494. {
  495. if (!obj)
  496. return;
  497. efi_remove_all_protocols(obj->handle);
  498. list_del(&obj->link);
  499. free(obj);
  500. }
  501. /**
  502. * efi_is_event() - check if a pointer is a valid event
  503. * @event: pointer to check
  504. *
  505. * Return: status code
  506. */
  507. static efi_status_t efi_is_event(const struct efi_event *event)
  508. {
  509. const struct efi_event *evt;
  510. if (!event)
  511. return EFI_INVALID_PARAMETER;
  512. list_for_each_entry(evt, &efi_events, link) {
  513. if (evt == event)
  514. return EFI_SUCCESS;
  515. }
  516. return EFI_INVALID_PARAMETER;
  517. }
  518. /**
  519. * efi_create_event() - create an event
  520. * @type: type of the event to create
  521. * @notify_tpl: task priority level of the event
  522. * @notify_function: notification function of the event
  523. * @notify_context: pointer passed to the notification function
  524. * @group: event group
  525. * @event: created event
  526. *
  527. * This function is used inside U-Boot code to create an event.
  528. *
  529. * For the API function implementing the CreateEvent service see
  530. * efi_create_event_ext.
  531. *
  532. * Return: status code
  533. */
  534. efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
  535. void (EFIAPI *notify_function) (
  536. struct efi_event *event,
  537. void *context),
  538. void *notify_context, efi_guid_t *group,
  539. struct efi_event **event)
  540. {
  541. struct efi_event *evt;
  542. if (event == NULL)
  543. return EFI_INVALID_PARAMETER;
  544. switch (type) {
  545. case 0:
  546. case EVT_TIMER:
  547. case EVT_NOTIFY_SIGNAL:
  548. case EVT_TIMER | EVT_NOTIFY_SIGNAL:
  549. case EVT_NOTIFY_WAIT:
  550. case EVT_TIMER | EVT_NOTIFY_WAIT:
  551. case EVT_SIGNAL_EXIT_BOOT_SERVICES:
  552. case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
  553. break;
  554. default:
  555. return EFI_INVALID_PARAMETER;
  556. }
  557. if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
  558. (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
  559. return EFI_INVALID_PARAMETER;
  560. evt = calloc(1, sizeof(struct efi_event));
  561. if (!evt)
  562. return EFI_OUT_OF_RESOURCES;
  563. evt->type = type;
  564. evt->notify_tpl = notify_tpl;
  565. evt->notify_function = notify_function;
  566. evt->notify_context = notify_context;
  567. evt->group = group;
  568. /* Disable timers on bootup */
  569. evt->trigger_next = -1ULL;
  570. evt->is_queued = false;
  571. evt->is_signaled = false;
  572. list_add_tail(&evt->link, &efi_events);
  573. *event = evt;
  574. return EFI_SUCCESS;
  575. }
  576. /*
  577. * efi_create_event_ex() - create an event in a group
  578. * @type: type of the event to create
  579. * @notify_tpl: task priority level of the event
  580. * @notify_function: notification function of the event
  581. * @notify_context: pointer passed to the notification function
  582. * @event: created event
  583. * @event_group: event group
  584. *
  585. * This function implements the CreateEventEx service.
  586. *
  587. * See the Unified Extensible Firmware Interface (UEFI) specification for
  588. * details.
  589. *
  590. * Return: status code
  591. */
  592. efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
  593. void (EFIAPI *notify_function) (
  594. struct efi_event *event,
  595. void *context),
  596. void *notify_context,
  597. efi_guid_t *event_group,
  598. struct efi_event **event)
  599. {
  600. EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
  601. notify_context, event_group);
  602. return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
  603. notify_context, event_group, event));
  604. }
  605. /**
  606. * efi_create_event_ext() - create an event
  607. * @type: type of the event to create
  608. * @notify_tpl: task priority level of the event
  609. * @notify_function: notification function of the event
  610. * @notify_context: pointer passed to the notification function
  611. * @event: created event
  612. *
  613. * This function implements the CreateEvent service.
  614. *
  615. * See the Unified Extensible Firmware Interface (UEFI) specification for
  616. * details.
  617. *
  618. * Return: status code
  619. */
  620. static efi_status_t EFIAPI efi_create_event_ext(
  621. uint32_t type, efi_uintn_t notify_tpl,
  622. void (EFIAPI *notify_function) (
  623. struct efi_event *event,
  624. void *context),
  625. void *notify_context, struct efi_event **event)
  626. {
  627. EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
  628. notify_context);
  629. return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
  630. notify_context, NULL, event));
  631. }
  632. /**
  633. * efi_timer_check() - check if a timer event has occurred
  634. *
  635. * Check if a timer event has occurred or a queued notification function should
  636. * be called.
  637. *
  638. * Our timers have to work without interrupts, so we check whenever keyboard
  639. * input or disk accesses happen if enough time elapsed for them to fire.
  640. */
  641. void efi_timer_check(void)
  642. {
  643. struct efi_event *evt;
  644. u64 now = timer_get_us();
  645. list_for_each_entry(evt, &efi_events, link) {
  646. if (evt->is_queued)
  647. efi_queue_event(evt, true);
  648. if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
  649. continue;
  650. switch (evt->trigger_type) {
  651. case EFI_TIMER_RELATIVE:
  652. evt->trigger_type = EFI_TIMER_STOP;
  653. break;
  654. case EFI_TIMER_PERIODIC:
  655. evt->trigger_next += evt->trigger_time;
  656. break;
  657. default:
  658. continue;
  659. }
  660. evt->is_signaled = false;
  661. efi_signal_event(evt, true);
  662. }
  663. WATCHDOG_RESET();
  664. }
  665. /**
  666. * efi_set_timer() - set the trigger time for a timer event or stop the event
  667. * @event: event for which the timer is set
  668. * @type: type of the timer
  669. * @trigger_time: trigger period in multiples of 100ns
  670. *
  671. * This is the function for internal usage in U-Boot. For the API function
  672. * implementing the SetTimer service see efi_set_timer_ext.
  673. *
  674. * Return: status code
  675. */
  676. efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
  677. uint64_t trigger_time)
  678. {
  679. /* Check that the event is valid */
  680. if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
  681. return EFI_INVALID_PARAMETER;
  682. /*
  683. * The parameter defines a multiple of 100ns.
  684. * We use multiples of 1000ns. So divide by 10.
  685. */
  686. do_div(trigger_time, 10);
  687. switch (type) {
  688. case EFI_TIMER_STOP:
  689. event->trigger_next = -1ULL;
  690. break;
  691. case EFI_TIMER_PERIODIC:
  692. case EFI_TIMER_RELATIVE:
  693. event->trigger_next = timer_get_us() + trigger_time;
  694. break;
  695. default:
  696. return EFI_INVALID_PARAMETER;
  697. }
  698. event->trigger_type = type;
  699. event->trigger_time = trigger_time;
  700. event->is_signaled = false;
  701. return EFI_SUCCESS;
  702. }
  703. /**
  704. * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
  705. * event
  706. * @event: event for which the timer is set
  707. * @type: type of the timer
  708. * @trigger_time: trigger period in multiples of 100ns
  709. *
  710. * This function implements the SetTimer service.
  711. *
  712. * See the Unified Extensible Firmware Interface (UEFI) specification for
  713. * details.
  714. *
  715. *
  716. * Return: status code
  717. */
  718. static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
  719. enum efi_timer_delay type,
  720. uint64_t trigger_time)
  721. {
  722. EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
  723. return EFI_EXIT(efi_set_timer(event, type, trigger_time));
  724. }
  725. /**
  726. * efi_wait_for_event() - wait for events to be signaled
  727. * @num_events: number of events to be waited for
  728. * @event: events to be waited for
  729. * @index: index of the event that was signaled
  730. *
  731. * This function implements the WaitForEvent service.
  732. *
  733. * See the Unified Extensible Firmware Interface (UEFI) specification for
  734. * details.
  735. *
  736. * Return: status code
  737. */
  738. static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
  739. struct efi_event **event,
  740. efi_uintn_t *index)
  741. {
  742. int i;
  743. EFI_ENTRY("%zd, %p, %p", num_events, event, index);
  744. /* Check parameters */
  745. if (!num_events || !event)
  746. return EFI_EXIT(EFI_INVALID_PARAMETER);
  747. /* Check TPL */
  748. if (efi_tpl != TPL_APPLICATION)
  749. return EFI_EXIT(EFI_UNSUPPORTED);
  750. for (i = 0; i < num_events; ++i) {
  751. if (efi_is_event(event[i]) != EFI_SUCCESS)
  752. return EFI_EXIT(EFI_INVALID_PARAMETER);
  753. if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
  754. return EFI_EXIT(EFI_INVALID_PARAMETER);
  755. if (!event[i]->is_signaled)
  756. efi_queue_event(event[i], true);
  757. }
  758. /* Wait for signal */
  759. for (;;) {
  760. for (i = 0; i < num_events; ++i) {
  761. if (event[i]->is_signaled)
  762. goto out;
  763. }
  764. /* Allow events to occur. */
  765. efi_timer_check();
  766. }
  767. out:
  768. /*
  769. * Reset the signal which is passed to the caller to allow periodic
  770. * events to occur.
  771. */
  772. event[i]->is_signaled = false;
  773. if (index)
  774. *index = i;
  775. return EFI_EXIT(EFI_SUCCESS);
  776. }
  777. /**
  778. * efi_signal_event_ext() - signal an EFI event
  779. * @event: event to signal
  780. *
  781. * This function implements the SignalEvent service.
  782. *
  783. * See the Unified Extensible Firmware Interface (UEFI) specification for
  784. * details.
  785. *
  786. * This functions sets the signaled state of the event and queues the
  787. * notification function for execution.
  788. *
  789. * Return: status code
  790. */
  791. static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
  792. {
  793. EFI_ENTRY("%p", event);
  794. if (efi_is_event(event) != EFI_SUCCESS)
  795. return EFI_EXIT(EFI_INVALID_PARAMETER);
  796. efi_signal_event(event, true);
  797. return EFI_EXIT(EFI_SUCCESS);
  798. }
  799. /**
  800. * efi_close_event() - close an EFI event
  801. * @event: event to close
  802. *
  803. * This function implements the CloseEvent service.
  804. *
  805. * See the Unified Extensible Firmware Interface (UEFI) specification for
  806. * details.
  807. *
  808. * Return: status code
  809. */
  810. static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
  811. {
  812. EFI_ENTRY("%p", event);
  813. if (efi_is_event(event) != EFI_SUCCESS)
  814. return EFI_EXIT(EFI_INVALID_PARAMETER);
  815. list_del(&event->link);
  816. free(event);
  817. return EFI_EXIT(EFI_SUCCESS);
  818. }
  819. /**
  820. * efi_check_event() - check if an event is signaled
  821. * @event: event to check
  822. *
  823. * This function implements the CheckEvent service.
  824. *
  825. * See the Unified Extensible Firmware Interface (UEFI) specification for
  826. * details.
  827. *
  828. * If an event is not signaled yet, the notification function is queued. The
  829. * signaled state is cleared.
  830. *
  831. * Return: status code
  832. */
  833. static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
  834. {
  835. EFI_ENTRY("%p", event);
  836. efi_timer_check();
  837. if (efi_is_event(event) != EFI_SUCCESS ||
  838. event->type & EVT_NOTIFY_SIGNAL)
  839. return EFI_EXIT(EFI_INVALID_PARAMETER);
  840. if (!event->is_signaled)
  841. efi_queue_event(event, true);
  842. if (event->is_signaled) {
  843. event->is_signaled = false;
  844. return EFI_EXIT(EFI_SUCCESS);
  845. }
  846. return EFI_EXIT(EFI_NOT_READY);
  847. }
  848. /**
  849. * efi_search_obj() - find the internal EFI object for a handle
  850. * @handle: handle to find
  851. *
  852. * Return: EFI object
  853. */
  854. struct efi_object *efi_search_obj(const efi_handle_t handle)
  855. {
  856. struct efi_object *efiobj;
  857. list_for_each_entry(efiobj, &efi_obj_list, link) {
  858. if (efiobj->handle == handle)
  859. return efiobj;
  860. }
  861. return NULL;
  862. }
  863. /**
  864. * efi_open_protocol_info_entry() - create open protocol info entry and add it
  865. * to a protocol
  866. * @handler: handler of a protocol
  867. *
  868. * Return: open protocol info entry
  869. */
  870. static struct efi_open_protocol_info_entry *efi_create_open_info(
  871. struct efi_handler *handler)
  872. {
  873. struct efi_open_protocol_info_item *item;
  874. item = calloc(1, sizeof(struct efi_open_protocol_info_item));
  875. if (!item)
  876. return NULL;
  877. /* Append the item to the open protocol info list. */
  878. list_add_tail(&item->link, &handler->open_infos);
  879. return &item->info;
  880. }
  881. /**
  882. * efi_delete_open_info() - remove an open protocol info entry from a protocol
  883. * @item: open protocol info entry to delete
  884. *
  885. * Return: status code
  886. */
  887. static efi_status_t efi_delete_open_info(
  888. struct efi_open_protocol_info_item *item)
  889. {
  890. list_del(&item->link);
  891. free(item);
  892. return EFI_SUCCESS;
  893. }
  894. /**
  895. * efi_add_protocol() - install new protocol on a handle
  896. * @handle: handle on which the protocol shall be installed
  897. * @protocol: GUID of the protocol to be installed
  898. * @protocol_interface: interface of the protocol implementation
  899. *
  900. * Return: status code
  901. */
  902. efi_status_t efi_add_protocol(const efi_handle_t handle,
  903. const efi_guid_t *protocol,
  904. void *protocol_interface)
  905. {
  906. struct efi_object *efiobj;
  907. struct efi_handler *handler;
  908. efi_status_t ret;
  909. efiobj = efi_search_obj(handle);
  910. if (!efiobj)
  911. return EFI_INVALID_PARAMETER;
  912. ret = efi_search_protocol(handle, protocol, NULL);
  913. if (ret != EFI_NOT_FOUND)
  914. return EFI_INVALID_PARAMETER;
  915. handler = calloc(1, sizeof(struct efi_handler));
  916. if (!handler)
  917. return EFI_OUT_OF_RESOURCES;
  918. handler->guid = protocol;
  919. handler->protocol_interface = protocol_interface;
  920. INIT_LIST_HEAD(&handler->open_infos);
  921. list_add_tail(&handler->link, &efiobj->protocols);
  922. if (!guidcmp(&efi_guid_device_path, protocol))
  923. EFI_PRINT("installed device path '%pD'\n", protocol_interface);
  924. return EFI_SUCCESS;
  925. }
  926. /**
  927. * efi_install_protocol_interface() - install protocol interface
  928. * @handle: handle on which the protocol shall be installed
  929. * @protocol: GUID of the protocol to be installed
  930. * @protocol_interface_type: type of the interface to be installed,
  931. * always EFI_NATIVE_INTERFACE
  932. * @protocol_interface: interface of the protocol implementation
  933. *
  934. * This function implements the InstallProtocolInterface service.
  935. *
  936. * See the Unified Extensible Firmware Interface (UEFI) specification for
  937. * details.
  938. *
  939. * Return: status code
  940. */
  941. static efi_status_t EFIAPI efi_install_protocol_interface(
  942. void **handle, const efi_guid_t *protocol,
  943. int protocol_interface_type, void *protocol_interface)
  944. {
  945. efi_status_t r;
  946. EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
  947. protocol_interface);
  948. if (!handle || !protocol ||
  949. protocol_interface_type != EFI_NATIVE_INTERFACE) {
  950. r = EFI_INVALID_PARAMETER;
  951. goto out;
  952. }
  953. /* Create new handle if requested. */
  954. if (!*handle) {
  955. r = efi_create_handle(handle);
  956. if (r != EFI_SUCCESS)
  957. goto out;
  958. debug("%sEFI: new handle %p\n", indent_string(nesting_level),
  959. *handle);
  960. } else {
  961. debug("%sEFI: handle %p\n", indent_string(nesting_level),
  962. *handle);
  963. }
  964. /* Add new protocol */
  965. r = efi_add_protocol(*handle, protocol, protocol_interface);
  966. out:
  967. return EFI_EXIT(r);
  968. }
  969. /**
  970. * efi_get_drivers() - get all drivers associated to a controller
  971. * @efiobj: handle of the controller
  972. * @protocol: protocol guid (optional)
  973. * @number_of_drivers: number of child controllers
  974. * @driver_handle_buffer: handles of the the drivers
  975. *
  976. * The allocated buffer has to be freed with free().
  977. *
  978. * Return: status code
  979. */
  980. static efi_status_t efi_get_drivers(struct efi_object *efiobj,
  981. const efi_guid_t *protocol,
  982. efi_uintn_t *number_of_drivers,
  983. efi_handle_t **driver_handle_buffer)
  984. {
  985. struct efi_handler *handler;
  986. struct efi_open_protocol_info_item *item;
  987. efi_uintn_t count = 0, i;
  988. bool duplicate;
  989. /* Count all driver associations */
  990. list_for_each_entry(handler, &efiobj->protocols, link) {
  991. if (protocol && guidcmp(handler->guid, protocol))
  992. continue;
  993. list_for_each_entry(item, &handler->open_infos, link) {
  994. if (item->info.attributes &
  995. EFI_OPEN_PROTOCOL_BY_DRIVER)
  996. ++count;
  997. }
  998. }
  999. /*
  1000. * Create buffer. In case of duplicate driver assignments the buffer
  1001. * will be too large. But that does not harm.
  1002. */
  1003. *number_of_drivers = 0;
  1004. *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
  1005. if (!*driver_handle_buffer)
  1006. return EFI_OUT_OF_RESOURCES;
  1007. /* Collect unique driver handles */
  1008. list_for_each_entry(handler, &efiobj->protocols, link) {
  1009. if (protocol && guidcmp(handler->guid, protocol))
  1010. continue;
  1011. list_for_each_entry(item, &handler->open_infos, link) {
  1012. if (item->info.attributes &
  1013. EFI_OPEN_PROTOCOL_BY_DRIVER) {
  1014. /* Check this is a new driver */
  1015. duplicate = false;
  1016. for (i = 0; i < *number_of_drivers; ++i) {
  1017. if ((*driver_handle_buffer)[i] ==
  1018. item->info.agent_handle)
  1019. duplicate = true;
  1020. }
  1021. /* Copy handle to buffer */
  1022. if (!duplicate) {
  1023. i = (*number_of_drivers)++;
  1024. (*driver_handle_buffer)[i] =
  1025. item->info.agent_handle;
  1026. }
  1027. }
  1028. }
  1029. }
  1030. return EFI_SUCCESS;
  1031. }
  1032. /**
  1033. * efi_disconnect_all_drivers() - disconnect all drivers from a controller
  1034. * @efiobj: handle of the controller
  1035. * @protocol: protocol guid (optional)
  1036. * @child_handle: handle of the child to destroy
  1037. *
  1038. * This function implements the DisconnectController service.
  1039. *
  1040. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1041. * details.
  1042. *
  1043. * Return: status code
  1044. */
  1045. static efi_status_t efi_disconnect_all_drivers(
  1046. struct efi_object *efiobj,
  1047. const efi_guid_t *protocol,
  1048. efi_handle_t child_handle)
  1049. {
  1050. efi_uintn_t number_of_drivers;
  1051. efi_handle_t *driver_handle_buffer;
  1052. efi_status_t r, ret;
  1053. ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
  1054. &driver_handle_buffer);
  1055. if (ret != EFI_SUCCESS)
  1056. return ret;
  1057. ret = EFI_NOT_FOUND;
  1058. while (number_of_drivers) {
  1059. r = EFI_CALL(efi_disconnect_controller(
  1060. efiobj->handle,
  1061. driver_handle_buffer[--number_of_drivers],
  1062. child_handle));
  1063. if (r == EFI_SUCCESS)
  1064. ret = r;
  1065. }
  1066. free(driver_handle_buffer);
  1067. return ret;
  1068. }
  1069. /**
  1070. * efi_uninstall_protocol_interface() - uninstall protocol interface
  1071. * @handle: handle from which the protocol shall be removed
  1072. * @protocol: GUID of the protocol to be removed
  1073. * @protocol_interface: interface to be removed
  1074. *
  1075. * This function implements the UninstallProtocolInterface service.
  1076. *
  1077. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1078. * details.
  1079. *
  1080. * Return: status code
  1081. */
  1082. static efi_status_t EFIAPI efi_uninstall_protocol_interface(
  1083. efi_handle_t handle, const efi_guid_t *protocol,
  1084. void *protocol_interface)
  1085. {
  1086. struct efi_object *efiobj;
  1087. struct efi_handler *handler;
  1088. struct efi_open_protocol_info_item *item;
  1089. struct efi_open_protocol_info_item *pos;
  1090. efi_status_t r;
  1091. EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
  1092. /* Check handle */
  1093. efiobj = efi_search_obj(handle);
  1094. if (!efiobj) {
  1095. r = EFI_INVALID_PARAMETER;
  1096. goto out;
  1097. }
  1098. /* Find the protocol on the handle */
  1099. r = efi_search_protocol(handle, protocol, &handler);
  1100. if (r != EFI_SUCCESS)
  1101. goto out;
  1102. /* Disconnect controllers */
  1103. efi_disconnect_all_drivers(efiobj, protocol, NULL);
  1104. if (!list_empty(&handler->open_infos)) {
  1105. r = EFI_ACCESS_DENIED;
  1106. goto out;
  1107. }
  1108. /* Close protocol */
  1109. list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
  1110. if (item->info.attributes ==
  1111. EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
  1112. item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
  1113. item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
  1114. list_del(&item->link);
  1115. }
  1116. if (!list_empty(&handler->open_infos)) {
  1117. r = EFI_ACCESS_DENIED;
  1118. goto out;
  1119. }
  1120. r = efi_remove_protocol(handle, protocol, protocol_interface);
  1121. out:
  1122. return EFI_EXIT(r);
  1123. }
  1124. /**
  1125. * efi_register_protocol_notify() - register an event for notification when a
  1126. * protocol is installed.
  1127. * @protocol: GUID of the protocol whose installation shall be notified
  1128. * @event: event to be signaled upon installation of the protocol
  1129. * @registration: key for retrieving the registration information
  1130. *
  1131. * This function implements the RegisterProtocolNotify service.
  1132. * See the Unified Extensible Firmware Interface (UEFI) specification
  1133. * for details.
  1134. *
  1135. * Return: status code
  1136. */
  1137. static efi_status_t EFIAPI efi_register_protocol_notify(
  1138. const efi_guid_t *protocol,
  1139. struct efi_event *event,
  1140. void **registration)
  1141. {
  1142. EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
  1143. return EFI_EXIT(EFI_OUT_OF_RESOURCES);
  1144. }
  1145. /**
  1146. * efi_search() - determine if an EFI handle implements a protocol
  1147. * @search_type: selection criterion
  1148. * @protocol: GUID of the protocol
  1149. * @search_key: registration key
  1150. * @efiobj: handle
  1151. *
  1152. * See the documentation of the LocateHandle service in the UEFI specification.
  1153. *
  1154. * Return: 0 if the handle implements the protocol
  1155. */
  1156. static int efi_search(enum efi_locate_search_type search_type,
  1157. const efi_guid_t *protocol, void *search_key,
  1158. struct efi_object *efiobj)
  1159. {
  1160. efi_status_t ret;
  1161. switch (search_type) {
  1162. case ALL_HANDLES:
  1163. return 0;
  1164. case BY_REGISTER_NOTIFY:
  1165. /* TODO: RegisterProtocolNotify is not implemented yet */
  1166. return -1;
  1167. case BY_PROTOCOL:
  1168. ret = efi_search_protocol(efiobj->handle, protocol, NULL);
  1169. return (ret != EFI_SUCCESS);
  1170. default:
  1171. /* Invalid search type */
  1172. return -1;
  1173. }
  1174. }
  1175. /**
  1176. * efi_locate_handle() - locate handles implementing a protocol
  1177. * @search_type: selection criterion
  1178. * @protocol: GUID of the protocol
  1179. * @search_key: registration key
  1180. * @buffer_size: size of the buffer to receive the handles in bytes
  1181. * @buffer: buffer to receive the relevant handles
  1182. *
  1183. * This function is meant for U-Boot internal calls. For the API implementation
  1184. * of the LocateHandle service see efi_locate_handle_ext.
  1185. *
  1186. * Return: status code
  1187. */
  1188. static efi_status_t efi_locate_handle(
  1189. enum efi_locate_search_type search_type,
  1190. const efi_guid_t *protocol, void *search_key,
  1191. efi_uintn_t *buffer_size, efi_handle_t *buffer)
  1192. {
  1193. struct efi_object *efiobj;
  1194. efi_uintn_t size = 0;
  1195. /* Check parameters */
  1196. switch (search_type) {
  1197. case ALL_HANDLES:
  1198. break;
  1199. case BY_REGISTER_NOTIFY:
  1200. if (!search_key)
  1201. return EFI_INVALID_PARAMETER;
  1202. /* RegisterProtocolNotify is not implemented yet */
  1203. return EFI_UNSUPPORTED;
  1204. case BY_PROTOCOL:
  1205. if (!protocol)
  1206. return EFI_INVALID_PARAMETER;
  1207. break;
  1208. default:
  1209. return EFI_INVALID_PARAMETER;
  1210. }
  1211. /*
  1212. * efi_locate_handle_buffer uses this function for
  1213. * the calculation of the necessary buffer size.
  1214. * So do not require a buffer for buffersize == 0.
  1215. */
  1216. if (!buffer_size || (*buffer_size && !buffer))
  1217. return EFI_INVALID_PARAMETER;
  1218. /* Count how much space we need */
  1219. list_for_each_entry(efiobj, &efi_obj_list, link) {
  1220. if (!efi_search(search_type, protocol, search_key, efiobj))
  1221. size += sizeof(void *);
  1222. }
  1223. if (*buffer_size < size) {
  1224. *buffer_size = size;
  1225. return EFI_BUFFER_TOO_SMALL;
  1226. }
  1227. *buffer_size = size;
  1228. if (size == 0)
  1229. return EFI_NOT_FOUND;
  1230. /* Then fill the array */
  1231. list_for_each_entry(efiobj, &efi_obj_list, link) {
  1232. if (!efi_search(search_type, protocol, search_key, efiobj))
  1233. *buffer++ = efiobj->handle;
  1234. }
  1235. return EFI_SUCCESS;
  1236. }
  1237. /**
  1238. * efi_locate_handle_ext() - locate handles implementing a protocol.
  1239. * @search_type: selection criterion
  1240. * @protocol: GUID of the protocol
  1241. * @search_key: registration key
  1242. * @buffer_size: size of the buffer to receive the handles in bytes
  1243. * @buffer: buffer to receive the relevant handles
  1244. *
  1245. * This function implements the LocateHandle service.
  1246. *
  1247. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1248. * details.
  1249. *
  1250. * Return: 0 if the handle implements the protocol
  1251. */
  1252. static efi_status_t EFIAPI efi_locate_handle_ext(
  1253. enum efi_locate_search_type search_type,
  1254. const efi_guid_t *protocol, void *search_key,
  1255. efi_uintn_t *buffer_size, efi_handle_t *buffer)
  1256. {
  1257. EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
  1258. buffer_size, buffer);
  1259. return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
  1260. buffer_size, buffer));
  1261. }
  1262. /**
  1263. * efi_remove_configuration_table() - collapses configuration table entries,
  1264. * removing index i
  1265. *
  1266. * @i: index of the table entry to be removed
  1267. */
  1268. static void efi_remove_configuration_table(int i)
  1269. {
  1270. struct efi_configuration_table *this = &systab.tables[i];
  1271. struct efi_configuration_table *next = &systab.tables[i + 1];
  1272. struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
  1273. memmove(this, next, (ulong)end - (ulong)next);
  1274. systab.nr_tables--;
  1275. }
  1276. /**
  1277. * efi_install_configuration_table() - adds, updates, or removes a
  1278. * configuration table
  1279. * @guid: GUID of the installed table
  1280. * @table: table to be installed
  1281. *
  1282. * This function is used for internal calls. For the API implementation of the
  1283. * InstallConfigurationTable service see efi_install_configuration_table_ext.
  1284. *
  1285. * Return: status code
  1286. */
  1287. efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
  1288. void *table)
  1289. {
  1290. struct efi_event *evt;
  1291. int i;
  1292. if (!guid)
  1293. return EFI_INVALID_PARAMETER;
  1294. /* Check for guid override */
  1295. for (i = 0; i < systab.nr_tables; i++) {
  1296. if (!guidcmp(guid, &systab.tables[i].guid)) {
  1297. if (table)
  1298. systab.tables[i].table = table;
  1299. else
  1300. efi_remove_configuration_table(i);
  1301. goto out;
  1302. }
  1303. }
  1304. if (!table)
  1305. return EFI_NOT_FOUND;
  1306. /* No override, check for overflow */
  1307. if (i >= EFI_MAX_CONFIGURATION_TABLES)
  1308. return EFI_OUT_OF_RESOURCES;
  1309. /* Add a new entry */
  1310. memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
  1311. systab.tables[i].table = table;
  1312. systab.nr_tables = i + 1;
  1313. out:
  1314. /* systab.nr_tables may have changed. So we need to update the crc32 */
  1315. efi_update_table_header_crc32(&systab.hdr);
  1316. /* Notify that the configuration table was changed */
  1317. list_for_each_entry(evt, &efi_events, link) {
  1318. if (evt->group && !guidcmp(evt->group, guid)) {
  1319. efi_signal_event(evt, false);
  1320. break;
  1321. }
  1322. }
  1323. return EFI_SUCCESS;
  1324. }
  1325. /**
  1326. * efi_install_configuration_table_ex() - Adds, updates, or removes a
  1327. * configuration table.
  1328. * @guid: GUID of the installed table
  1329. * @table: table to be installed
  1330. *
  1331. * This function implements the InstallConfigurationTable service.
  1332. *
  1333. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1334. * details.
  1335. *
  1336. * Return: status code
  1337. */
  1338. static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
  1339. void *table)
  1340. {
  1341. EFI_ENTRY("%pUl, %p", guid, table);
  1342. return EFI_EXIT(efi_install_configuration_table(guid, table));
  1343. }
  1344. /**
  1345. * efi_setup_loaded_image() - initialize a loaded image
  1346. * @info: loaded image info to be passed to the entry point of the image
  1347. * @obj: internal object associated with the loaded image
  1348. * @device_path: device path of the loaded image
  1349. * @file_path: file path of the loaded image
  1350. *
  1351. * Initialize a loaded_image_info and loaded_image_info object with correct
  1352. * protocols, boot-device, etc.
  1353. *
  1354. * Return: status code
  1355. */
  1356. efi_status_t efi_setup_loaded_image(
  1357. struct efi_loaded_image *info, struct efi_object *obj,
  1358. struct efi_device_path *device_path,
  1359. struct efi_device_path *file_path)
  1360. {
  1361. efi_status_t ret;
  1362. /* Add internal object to object list */
  1363. efi_add_handle(obj);
  1364. /* efi_exit() assumes that the handle points to the info */
  1365. obj->handle = info;
  1366. info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
  1367. info->file_path = file_path;
  1368. if (device_path) {
  1369. info->device_handle = efi_dp_find_obj(device_path, NULL);
  1370. /*
  1371. * When asking for the device path interface, return
  1372. * bootefi_device_path
  1373. */
  1374. ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
  1375. device_path);
  1376. if (ret != EFI_SUCCESS)
  1377. goto failure;
  1378. }
  1379. /*
  1380. * When asking for the loaded_image interface, just
  1381. * return handle which points to loaded_image_info
  1382. */
  1383. ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
  1384. if (ret != EFI_SUCCESS)
  1385. goto failure;
  1386. ret = efi_add_protocol(obj->handle,
  1387. &efi_guid_device_path_to_text_protocol,
  1388. (void *)&efi_device_path_to_text);
  1389. if (ret != EFI_SUCCESS)
  1390. goto failure;
  1391. ret = efi_add_protocol(obj->handle,
  1392. &efi_guid_device_path_utilities_protocol,
  1393. (void *)&efi_device_path_utilities);
  1394. if (ret != EFI_SUCCESS)
  1395. goto failure;
  1396. return ret;
  1397. failure:
  1398. printf("ERROR: Failure to install protocols for loaded image\n");
  1399. return ret;
  1400. }
  1401. /**
  1402. * efi_load_image_from_path() - load an image using a file path
  1403. * @file_path: the path of the image to load
  1404. * @buffer: buffer containing the loaded image
  1405. *
  1406. * Return: status code
  1407. */
  1408. efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
  1409. void **buffer)
  1410. {
  1411. struct efi_file_info *info = NULL;
  1412. struct efi_file_handle *f;
  1413. static efi_status_t ret;
  1414. efi_uintn_t bs;
  1415. f = efi_file_from_path(file_path);
  1416. if (!f)
  1417. return EFI_DEVICE_ERROR;
  1418. bs = 0;
  1419. EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
  1420. &bs, info));
  1421. if (ret == EFI_BUFFER_TOO_SMALL) {
  1422. info = malloc(bs);
  1423. EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
  1424. &bs, info));
  1425. }
  1426. if (ret != EFI_SUCCESS)
  1427. goto error;
  1428. ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
  1429. if (ret)
  1430. goto error;
  1431. bs = info->file_size;
  1432. EFI_CALL(ret = f->read(f, &bs, *buffer));
  1433. error:
  1434. free(info);
  1435. EFI_CALL(f->close(f));
  1436. if (ret != EFI_SUCCESS) {
  1437. efi_free_pool(*buffer);
  1438. *buffer = NULL;
  1439. }
  1440. return ret;
  1441. }
  1442. /**
  1443. * efi_load_image() - load an EFI image into memory
  1444. * @boot_policy: true for request originating from the boot manager
  1445. * @parent_image: the caller's image handle
  1446. * @file_path: the path of the image to load
  1447. * @source_buffer: memory location from which the image is installed
  1448. * @source_size: size of the memory area from which the image is installed
  1449. * @image_handle: handle for the newly installed image
  1450. *
  1451. * This function implements the LoadImage service.
  1452. *
  1453. * See the Unified Extensible Firmware Interface (UEFI) specification
  1454. * for details.
  1455. *
  1456. * Return: status code
  1457. */
  1458. static efi_status_t EFIAPI efi_load_image(bool boot_policy,
  1459. efi_handle_t parent_image,
  1460. struct efi_device_path *file_path,
  1461. void *source_buffer,
  1462. efi_uintn_t source_size,
  1463. efi_handle_t *image_handle)
  1464. {
  1465. struct efi_loaded_image *info;
  1466. struct efi_object *obj;
  1467. efi_status_t ret;
  1468. EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
  1469. file_path, source_buffer, source_size, image_handle);
  1470. if (!image_handle || !parent_image) {
  1471. ret = EFI_INVALID_PARAMETER;
  1472. goto error;
  1473. }
  1474. if (!source_buffer && !file_path) {
  1475. ret = EFI_NOT_FOUND;
  1476. goto error;
  1477. }
  1478. info = calloc(1, sizeof(*info));
  1479. if (!info) {
  1480. ret = EFI_OUT_OF_RESOURCES;
  1481. goto error;
  1482. }
  1483. obj = calloc(1, sizeof(*obj));
  1484. if (!obj) {
  1485. free(info);
  1486. ret = EFI_OUT_OF_RESOURCES;
  1487. goto error;
  1488. }
  1489. if (!source_buffer) {
  1490. struct efi_device_path *dp, *fp;
  1491. ret = efi_load_image_from_path(file_path, &source_buffer);
  1492. if (ret != EFI_SUCCESS)
  1493. goto failure;
  1494. /*
  1495. * split file_path which contains both the device and
  1496. * file parts:
  1497. */
  1498. efi_dp_split_file_path(file_path, &dp, &fp);
  1499. ret = efi_setup_loaded_image(info, obj, dp, fp);
  1500. if (ret != EFI_SUCCESS)
  1501. goto failure;
  1502. } else {
  1503. /* In this case, file_path is the "device" path, ie.
  1504. * something like a HARDWARE_DEVICE:MEMORY_MAPPED
  1505. */
  1506. ret = efi_setup_loaded_image(info, obj, file_path, NULL);
  1507. if (ret != EFI_SUCCESS)
  1508. goto failure;
  1509. }
  1510. info->reserved = efi_load_pe(source_buffer, info);
  1511. if (!info->reserved) {
  1512. ret = EFI_UNSUPPORTED;
  1513. goto failure;
  1514. }
  1515. info->system_table = &systab;
  1516. info->parent_handle = parent_image;
  1517. *image_handle = obj->handle;
  1518. return EFI_EXIT(EFI_SUCCESS);
  1519. failure:
  1520. free(info);
  1521. efi_delete_handle(obj);
  1522. error:
  1523. return EFI_EXIT(ret);
  1524. }
  1525. /**
  1526. * efi_start_image() - dall the entry point of an image
  1527. * @image_handle: handle of the image
  1528. * @exit_data_size: size of the buffer
  1529. * @exit_data: buffer to receive the exit data of the called image
  1530. *
  1531. * This function implements the StartImage service.
  1532. *
  1533. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1534. * details.
  1535. *
  1536. * Return: status code
  1537. */
  1538. static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
  1539. unsigned long *exit_data_size,
  1540. s16 **exit_data)
  1541. {
  1542. EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
  1543. struct efi_system_table *st);
  1544. struct efi_loaded_image *info = image_handle;
  1545. efi_status_t ret;
  1546. EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
  1547. entry = info->reserved;
  1548. efi_is_direct_boot = false;
  1549. /* call the image! */
  1550. if (setjmp(&info->exit_jmp)) {
  1551. /*
  1552. * We called the entry point of the child image with EFI_CALL
  1553. * in the lines below. The child image called the Exit() boot
  1554. * service efi_exit() which executed the long jump that brought
  1555. * us to the current line. This implies that the second half
  1556. * of the EFI_CALL macro has not been executed.
  1557. */
  1558. #ifdef CONFIG_ARM
  1559. /*
  1560. * efi_exit() called efi_restore_gd(). We have to undo this
  1561. * otherwise __efi_entry_check() will put the wrong value into
  1562. * app_gd.
  1563. */
  1564. gd = app_gd;
  1565. #endif
  1566. /*
  1567. * To get ready to call EFI_EXIT below we have to execute the
  1568. * missed out steps of EFI_CALL.
  1569. */
  1570. assert(__efi_entry_check());
  1571. debug("%sEFI: %lu returned by started image\n",
  1572. __efi_nesting_dec(),
  1573. (unsigned long)((uintptr_t)info->exit_status &
  1574. ~EFI_ERROR_MASK));
  1575. return EFI_EXIT(info->exit_status);
  1576. }
  1577. ret = EFI_CALL(entry(image_handle, &systab));
  1578. /*
  1579. * Usually UEFI applications call Exit() instead of returning.
  1580. * But because the world doesn not consist of ponies and unicorns,
  1581. * we're happy to emulate that behavior on behalf of a payload
  1582. * that forgot.
  1583. */
  1584. return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
  1585. }
  1586. /**
  1587. * efi_exit() - leave an EFI application or driver
  1588. * @image_handle: handle of the application or driver that is exiting
  1589. * @exit_status: status code
  1590. * @exit_data_size: size of the buffer in bytes
  1591. * @exit_data: buffer with data describing an error
  1592. *
  1593. * This function implements the Exit service.
  1594. *
  1595. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1596. * details.
  1597. *
  1598. * Return: status code
  1599. */
  1600. static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
  1601. efi_status_t exit_status,
  1602. unsigned long exit_data_size,
  1603. int16_t *exit_data)
  1604. {
  1605. /*
  1606. * We require that the handle points to the original loaded
  1607. * image protocol interface.
  1608. *
  1609. * For getting the longjmp address this is safer than locating
  1610. * the protocol because the protocol may have been reinstalled
  1611. * pointing to another memory location.
  1612. *
  1613. * TODO: We should call the unload procedure of the loaded
  1614. * image protocol.
  1615. */
  1616. struct efi_loaded_image *loaded_image_info = (void *)image_handle;
  1617. EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
  1618. exit_data_size, exit_data);
  1619. /* Make sure entry/exit counts for EFI world cross-overs match */
  1620. EFI_EXIT(exit_status);
  1621. /*
  1622. * But longjmp out with the U-Boot gd, not the application's, as
  1623. * the other end is a setjmp call inside EFI context.
  1624. */
  1625. efi_restore_gd();
  1626. loaded_image_info->exit_status = exit_status;
  1627. longjmp(&loaded_image_info->exit_jmp, 1);
  1628. panic("EFI application exited");
  1629. }
  1630. /**
  1631. * efi_unload_image() - unload an EFI image
  1632. * @image_handle: handle of the image to be unloaded
  1633. *
  1634. * This function implements the UnloadImage service.
  1635. *
  1636. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1637. * details.
  1638. *
  1639. * Return: status code
  1640. */
  1641. static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
  1642. {
  1643. struct efi_object *efiobj;
  1644. EFI_ENTRY("%p", image_handle);
  1645. efiobj = efi_search_obj(image_handle);
  1646. if (efiobj)
  1647. list_del(&efiobj->link);
  1648. return EFI_EXIT(EFI_SUCCESS);
  1649. }
  1650. /**
  1651. * efi_exit_caches() - fix up caches for EFI payloads if necessary
  1652. */
  1653. static void efi_exit_caches(void)
  1654. {
  1655. #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
  1656. /*
  1657. * Grub on 32bit ARM needs to have caches disabled before jumping into
  1658. * a zImage, but does not know of all cache layers. Give it a hand.
  1659. */
  1660. if (efi_is_direct_boot)
  1661. cleanup_before_linux();
  1662. #endif
  1663. }
  1664. /**
  1665. * efi_exit_boot_services() - stop all boot services
  1666. * @image_handle: handle of the loaded image
  1667. * @map_key: key of the memory map
  1668. *
  1669. * This function implements the ExitBootServices service.
  1670. *
  1671. * See the Unified Extensible Firmware Interface (UEFI) specification
  1672. * for details.
  1673. *
  1674. * All timer events are disabled. For exit boot services events the
  1675. * notification function is called. The boot services are disabled in the
  1676. * system table.
  1677. *
  1678. * Return: status code
  1679. */
  1680. static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
  1681. unsigned long map_key)
  1682. {
  1683. struct efi_event *evt;
  1684. EFI_ENTRY("%p, %ld", image_handle, map_key);
  1685. /* Check that the caller has read the current memory map */
  1686. if (map_key != efi_memory_map_key)
  1687. return EFI_INVALID_PARAMETER;
  1688. /* Make sure that notification functions are not called anymore */
  1689. efi_tpl = TPL_HIGH_LEVEL;
  1690. /* Check if ExitBootServices has already been called */
  1691. if (!systab.boottime)
  1692. return EFI_EXIT(EFI_SUCCESS);
  1693. /* Add related events to the event group */
  1694. list_for_each_entry(evt, &efi_events, link) {
  1695. if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
  1696. evt->group = &efi_guid_event_group_exit_boot_services;
  1697. }
  1698. /* Notify that ExitBootServices is invoked. */
  1699. list_for_each_entry(evt, &efi_events, link) {
  1700. if (evt->group &&
  1701. !guidcmp(evt->group,
  1702. &efi_guid_event_group_exit_boot_services)) {
  1703. efi_signal_event(evt, false);
  1704. break;
  1705. }
  1706. }
  1707. /* TODO Should persist EFI variables here */
  1708. board_quiesce_devices();
  1709. /* Fix up caches for EFI payloads if necessary */
  1710. efi_exit_caches();
  1711. /* This stops all lingering devices */
  1712. bootm_disable_interrupts();
  1713. /* Disable boottime services */
  1714. systab.con_in_handle = NULL;
  1715. systab.con_in = NULL;
  1716. systab.con_out_handle = NULL;
  1717. systab.con_out = NULL;
  1718. systab.stderr_handle = NULL;
  1719. systab.std_err = NULL;
  1720. systab.boottime = NULL;
  1721. /* Recalculate CRC32 */
  1722. efi_update_table_header_crc32(&systab.hdr);
  1723. /* Give the payload some time to boot */
  1724. efi_set_watchdog(0);
  1725. WATCHDOG_RESET();
  1726. return EFI_EXIT(EFI_SUCCESS);
  1727. }
  1728. /**
  1729. * efi_get_next_monotonic_count() - get next value of the counter
  1730. * @count: returned value of the counter
  1731. *
  1732. * This function implements the NextMonotonicCount service.
  1733. *
  1734. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1735. * details.
  1736. *
  1737. * Return: status code
  1738. */
  1739. static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
  1740. {
  1741. static uint64_t mono;
  1742. EFI_ENTRY("%p", count);
  1743. *count = mono++;
  1744. return EFI_EXIT(EFI_SUCCESS);
  1745. }
  1746. /**
  1747. * efi_stall() - sleep
  1748. * @microseconds: period to sleep in microseconds
  1749. *
  1750. * This function implements the Stall service.
  1751. *
  1752. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1753. * details.
  1754. *
  1755. * Return: status code
  1756. */
  1757. static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
  1758. {
  1759. EFI_ENTRY("%ld", microseconds);
  1760. udelay(microseconds);
  1761. return EFI_EXIT(EFI_SUCCESS);
  1762. }
  1763. /**
  1764. * efi_set_watchdog_timer() - reset the watchdog timer
  1765. * @timeout: seconds before reset by watchdog
  1766. * @watchdog_code: code to be logged when resetting
  1767. * @data_size: size of buffer in bytes
  1768. * @watchdog_data: buffer with data describing the reset reason
  1769. *
  1770. * This function implements the SetWatchdogTimer service.
  1771. *
  1772. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1773. * details.
  1774. *
  1775. * Return: status code
  1776. */
  1777. static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
  1778. uint64_t watchdog_code,
  1779. unsigned long data_size,
  1780. uint16_t *watchdog_data)
  1781. {
  1782. EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
  1783. data_size, watchdog_data);
  1784. return EFI_EXIT(efi_set_watchdog(timeout));
  1785. }
  1786. /**
  1787. * efi_close_protocol() - close a protocol
  1788. * @handle: handle on which the protocol shall be closed
  1789. * @protocol: GUID of the protocol to close
  1790. * @agent_handle: handle of the driver
  1791. * @controller_handle: handle of the controller
  1792. *
  1793. * This function implements the CloseProtocol service.
  1794. *
  1795. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1796. * details.
  1797. *
  1798. * Return: status code
  1799. */
  1800. static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
  1801. const efi_guid_t *protocol,
  1802. efi_handle_t agent_handle,
  1803. efi_handle_t controller_handle)
  1804. {
  1805. struct efi_handler *handler;
  1806. struct efi_open_protocol_info_item *item;
  1807. struct efi_open_protocol_info_item *pos;
  1808. efi_status_t r;
  1809. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
  1810. controller_handle);
  1811. if (!agent_handle) {
  1812. r = EFI_INVALID_PARAMETER;
  1813. goto out;
  1814. }
  1815. r = efi_search_protocol(handle, protocol, &handler);
  1816. if (r != EFI_SUCCESS)
  1817. goto out;
  1818. r = EFI_NOT_FOUND;
  1819. list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
  1820. if (item->info.agent_handle == agent_handle &&
  1821. item->info.controller_handle == controller_handle) {
  1822. efi_delete_open_info(item);
  1823. r = EFI_SUCCESS;
  1824. break;
  1825. }
  1826. }
  1827. out:
  1828. return EFI_EXIT(r);
  1829. }
  1830. /**
  1831. * efi_open_protocol_information() - provide information about then open status
  1832. * of a protocol on a handle
  1833. * @handle: handle for which the information shall be retrieved
  1834. * @protocol: GUID of the protocol
  1835. * @entry_buffer: buffer to receive the open protocol information
  1836. * @entry_count: number of entries available in the buffer
  1837. *
  1838. * This function implements the OpenProtocolInformation service.
  1839. *
  1840. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1841. * details.
  1842. *
  1843. * Return: status code
  1844. */
  1845. static efi_status_t EFIAPI efi_open_protocol_information(
  1846. efi_handle_t handle, const efi_guid_t *protocol,
  1847. struct efi_open_protocol_info_entry **entry_buffer,
  1848. efi_uintn_t *entry_count)
  1849. {
  1850. unsigned long buffer_size;
  1851. unsigned long count;
  1852. struct efi_handler *handler;
  1853. struct efi_open_protocol_info_item *item;
  1854. efi_status_t r;
  1855. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
  1856. entry_count);
  1857. /* Check parameters */
  1858. if (!entry_buffer) {
  1859. r = EFI_INVALID_PARAMETER;
  1860. goto out;
  1861. }
  1862. r = efi_search_protocol(handle, protocol, &handler);
  1863. if (r != EFI_SUCCESS)
  1864. goto out;
  1865. /* Count entries */
  1866. count = 0;
  1867. list_for_each_entry(item, &handler->open_infos, link) {
  1868. if (item->info.open_count)
  1869. ++count;
  1870. }
  1871. *entry_count = count;
  1872. *entry_buffer = NULL;
  1873. if (!count) {
  1874. r = EFI_SUCCESS;
  1875. goto out;
  1876. }
  1877. /* Copy entries */
  1878. buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
  1879. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
  1880. (void **)entry_buffer);
  1881. if (r != EFI_SUCCESS)
  1882. goto out;
  1883. list_for_each_entry_reverse(item, &handler->open_infos, link) {
  1884. if (item->info.open_count)
  1885. (*entry_buffer)[--count] = item->info;
  1886. }
  1887. out:
  1888. return EFI_EXIT(r);
  1889. }
  1890. /**
  1891. * efi_protocols_per_handle() - get protocols installed on a handle
  1892. * @handle: handle for which the information is retrieved
  1893. * @protocol_buffer: buffer with protocol GUIDs
  1894. * @protocol_buffer_count: number of entries in the buffer
  1895. *
  1896. * This function implements the ProtocolsPerHandleService.
  1897. *
  1898. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1899. * details.
  1900. *
  1901. * Return: status code
  1902. */
  1903. static efi_status_t EFIAPI efi_protocols_per_handle(
  1904. efi_handle_t handle, efi_guid_t ***protocol_buffer,
  1905. efi_uintn_t *protocol_buffer_count)
  1906. {
  1907. unsigned long buffer_size;
  1908. struct efi_object *efiobj;
  1909. struct list_head *protocol_handle;
  1910. efi_status_t r;
  1911. EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
  1912. protocol_buffer_count);
  1913. if (!handle || !protocol_buffer || !protocol_buffer_count)
  1914. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1915. *protocol_buffer = NULL;
  1916. *protocol_buffer_count = 0;
  1917. efiobj = efi_search_obj(handle);
  1918. if (!efiobj)
  1919. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1920. /* Count protocols */
  1921. list_for_each(protocol_handle, &efiobj->protocols) {
  1922. ++*protocol_buffer_count;
  1923. }
  1924. /* Copy guids */
  1925. if (*protocol_buffer_count) {
  1926. size_t j = 0;
  1927. buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
  1928. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
  1929. (void **)protocol_buffer);
  1930. if (r != EFI_SUCCESS)
  1931. return EFI_EXIT(r);
  1932. list_for_each(protocol_handle, &efiobj->protocols) {
  1933. struct efi_handler *protocol;
  1934. protocol = list_entry(protocol_handle,
  1935. struct efi_handler, link);
  1936. (*protocol_buffer)[j] = (void *)protocol->guid;
  1937. ++j;
  1938. }
  1939. }
  1940. return EFI_EXIT(EFI_SUCCESS);
  1941. }
  1942. /**
  1943. * efi_locate_handle_buffer() - locate handles implementing a protocol
  1944. * @search_type: selection criterion
  1945. * @protocol: GUID of the protocol
  1946. * @search_key: registration key
  1947. * @no_handles: number of returned handles
  1948. * @buffer: buffer with the returned handles
  1949. *
  1950. * This function implements the LocateHandleBuffer service.
  1951. *
  1952. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1953. * details.
  1954. *
  1955. * Return: status code
  1956. */
  1957. static efi_status_t EFIAPI efi_locate_handle_buffer(
  1958. enum efi_locate_search_type search_type,
  1959. const efi_guid_t *protocol, void *search_key,
  1960. efi_uintn_t *no_handles, efi_handle_t **buffer)
  1961. {
  1962. efi_status_t r;
  1963. efi_uintn_t buffer_size = 0;
  1964. EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
  1965. no_handles, buffer);
  1966. if (!no_handles || !buffer) {
  1967. r = EFI_INVALID_PARAMETER;
  1968. goto out;
  1969. }
  1970. *no_handles = 0;
  1971. *buffer = NULL;
  1972. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  1973. *buffer);
  1974. if (r != EFI_BUFFER_TOO_SMALL)
  1975. goto out;
  1976. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
  1977. (void **)buffer);
  1978. if (r != EFI_SUCCESS)
  1979. goto out;
  1980. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  1981. *buffer);
  1982. if (r == EFI_SUCCESS)
  1983. *no_handles = buffer_size / sizeof(efi_handle_t);
  1984. out:
  1985. return EFI_EXIT(r);
  1986. }
  1987. /**
  1988. * efi_locate_protocol() - find an interface implementing a protocol
  1989. * @protocol: GUID of the protocol
  1990. * @registration: registration key passed to the notification function
  1991. * @protocol_interface: interface implementing the protocol
  1992. *
  1993. * This function implements the LocateProtocol service.
  1994. *
  1995. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1996. * details.
  1997. *
  1998. * Return: status code
  1999. */
  2000. static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
  2001. void *registration,
  2002. void **protocol_interface)
  2003. {
  2004. struct list_head *lhandle;
  2005. efi_status_t ret;
  2006. EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
  2007. if (!protocol || !protocol_interface)
  2008. return EFI_EXIT(EFI_INVALID_PARAMETER);
  2009. list_for_each(lhandle, &efi_obj_list) {
  2010. struct efi_object *efiobj;
  2011. struct efi_handler *handler;
  2012. efiobj = list_entry(lhandle, struct efi_object, link);
  2013. ret = efi_search_protocol(efiobj->handle, protocol, &handler);
  2014. if (ret == EFI_SUCCESS) {
  2015. *protocol_interface = handler->protocol_interface;
  2016. return EFI_EXIT(EFI_SUCCESS);
  2017. }
  2018. }
  2019. *protocol_interface = NULL;
  2020. return EFI_EXIT(EFI_NOT_FOUND);
  2021. }
  2022. /**
  2023. * efi_locate_device_path() - Get the device path and handle of an device
  2024. * implementing a protocol
  2025. * @protocol: GUID of the protocol
  2026. * @device_path: device path
  2027. * @device: handle of the device
  2028. *
  2029. * This function implements the LocateDevicePath service.
  2030. *
  2031. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2032. * details.
  2033. *
  2034. * Return: status code
  2035. */
  2036. static efi_status_t EFIAPI efi_locate_device_path(
  2037. const efi_guid_t *protocol,
  2038. struct efi_device_path **device_path,
  2039. efi_handle_t *device)
  2040. {
  2041. struct efi_device_path *dp;
  2042. size_t i;
  2043. struct efi_handler *handler;
  2044. efi_handle_t *handles;
  2045. size_t len, len_dp;
  2046. size_t len_best = 0;
  2047. efi_uintn_t no_handles;
  2048. u8 *remainder;
  2049. efi_status_t ret;
  2050. EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
  2051. if (!protocol || !device_path || !*device_path || !device) {
  2052. ret = EFI_INVALID_PARAMETER;
  2053. goto out;
  2054. }
  2055. /* Find end of device path */
  2056. len = efi_dp_instance_size(*device_path);
  2057. /* Get all handles implementing the protocol */
  2058. ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
  2059. &no_handles, &handles));
  2060. if (ret != EFI_SUCCESS)
  2061. goto out;
  2062. for (i = 0; i < no_handles; ++i) {
  2063. /* Find the device path protocol */
  2064. ret = efi_search_protocol(handles[i], &efi_guid_device_path,
  2065. &handler);
  2066. if (ret != EFI_SUCCESS)
  2067. continue;
  2068. dp = (struct efi_device_path *)handler->protocol_interface;
  2069. len_dp = efi_dp_instance_size(dp);
  2070. /*
  2071. * This handle can only be a better fit
  2072. * if its device path length is longer than the best fit and
  2073. * if its device path length is shorter of equal the searched
  2074. * device path.
  2075. */
  2076. if (len_dp <= len_best || len_dp > len)
  2077. continue;
  2078. /* Check if dp is a subpath of device_path */
  2079. if (memcmp(*device_path, dp, len_dp))
  2080. continue;
  2081. *device = handles[i];
  2082. len_best = len_dp;
  2083. }
  2084. if (len_best) {
  2085. remainder = (u8 *)*device_path + len_best;
  2086. *device_path = (struct efi_device_path *)remainder;
  2087. ret = EFI_SUCCESS;
  2088. } else {
  2089. ret = EFI_NOT_FOUND;
  2090. }
  2091. out:
  2092. return EFI_EXIT(ret);
  2093. }
  2094. /**
  2095. * efi_install_multiple_protocol_interfaces() - Install multiple protocol
  2096. * interfaces
  2097. * @handle: handle on which the protocol interfaces shall be installed
  2098. * @...: NULL terminated argument list with pairs of protocol GUIDS and
  2099. * interfaces
  2100. *
  2101. * This function implements the MultipleProtocolInterfaces service.
  2102. *
  2103. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2104. * details.
  2105. *
  2106. * Return: status code
  2107. */
  2108. static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
  2109. void **handle, ...)
  2110. {
  2111. EFI_ENTRY("%p", handle);
  2112. efi_va_list argptr;
  2113. const efi_guid_t *protocol;
  2114. void *protocol_interface;
  2115. efi_status_t r = EFI_SUCCESS;
  2116. int i = 0;
  2117. if (!handle)
  2118. return EFI_EXIT(EFI_INVALID_PARAMETER);
  2119. efi_va_start(argptr, handle);
  2120. for (;;) {
  2121. protocol = efi_va_arg(argptr, efi_guid_t*);
  2122. if (!protocol)
  2123. break;
  2124. protocol_interface = efi_va_arg(argptr, void*);
  2125. r = EFI_CALL(efi_install_protocol_interface(
  2126. handle, protocol,
  2127. EFI_NATIVE_INTERFACE,
  2128. protocol_interface));
  2129. if (r != EFI_SUCCESS)
  2130. break;
  2131. i++;
  2132. }
  2133. efi_va_end(argptr);
  2134. if (r == EFI_SUCCESS)
  2135. return EFI_EXIT(r);
  2136. /* If an error occurred undo all changes. */
  2137. efi_va_start(argptr, handle);
  2138. for (; i; --i) {
  2139. protocol = efi_va_arg(argptr, efi_guid_t*);
  2140. protocol_interface = efi_va_arg(argptr, void*);
  2141. EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
  2142. protocol_interface));
  2143. }
  2144. efi_va_end(argptr);
  2145. return EFI_EXIT(r);
  2146. }
  2147. /**
  2148. * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
  2149. * interfaces
  2150. * @handle: handle from which the protocol interfaces shall be removed
  2151. * @...: NULL terminated argument list with pairs of protocol GUIDS and
  2152. * interfaces
  2153. *
  2154. * This function implements the UninstallMultipleProtocolInterfaces service.
  2155. *
  2156. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2157. * details.
  2158. *
  2159. * Return: status code
  2160. */
  2161. static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
  2162. void *handle, ...)
  2163. {
  2164. EFI_ENTRY("%p", handle);
  2165. efi_va_list argptr;
  2166. const efi_guid_t *protocol;
  2167. void *protocol_interface;
  2168. efi_status_t r = EFI_SUCCESS;
  2169. size_t i = 0;
  2170. if (!handle)
  2171. return EFI_EXIT(EFI_INVALID_PARAMETER);
  2172. efi_va_start(argptr, handle);
  2173. for (;;) {
  2174. protocol = efi_va_arg(argptr, efi_guid_t*);
  2175. if (!protocol)
  2176. break;
  2177. protocol_interface = efi_va_arg(argptr, void*);
  2178. r = EFI_CALL(efi_uninstall_protocol_interface(
  2179. handle, protocol,
  2180. protocol_interface));
  2181. if (r != EFI_SUCCESS)
  2182. break;
  2183. i++;
  2184. }
  2185. efi_va_end(argptr);
  2186. if (r == EFI_SUCCESS)
  2187. return EFI_EXIT(r);
  2188. /* If an error occurred undo all changes. */
  2189. efi_va_start(argptr, handle);
  2190. for (; i; --i) {
  2191. protocol = efi_va_arg(argptr, efi_guid_t*);
  2192. protocol_interface = efi_va_arg(argptr, void*);
  2193. EFI_CALL(efi_install_protocol_interface(&handle, protocol,
  2194. EFI_NATIVE_INTERFACE,
  2195. protocol_interface));
  2196. }
  2197. efi_va_end(argptr);
  2198. return EFI_EXIT(r);
  2199. }
  2200. /**
  2201. * efi_calculate_crc32() - calculate cyclic redundancy code
  2202. * @data: buffer with data
  2203. * @data_size: size of buffer in bytes
  2204. * @crc32_p: cyclic redundancy code
  2205. *
  2206. * This function implements the CalculateCrc32 service.
  2207. *
  2208. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2209. * details.
  2210. *
  2211. * Return: status code
  2212. */
  2213. static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
  2214. efi_uintn_t data_size,
  2215. u32 *crc32_p)
  2216. {
  2217. EFI_ENTRY("%p, %zu", data, data_size);
  2218. *crc32_p = crc32(0, data, data_size);
  2219. return EFI_EXIT(EFI_SUCCESS);
  2220. }
  2221. /**
  2222. * efi_copy_mem() - copy memory
  2223. * @destination: destination of the copy operation
  2224. * @source: source of the copy operation
  2225. * @length: number of bytes to copy
  2226. *
  2227. * This function implements the CopyMem service.
  2228. *
  2229. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2230. * details.
  2231. */
  2232. static void EFIAPI efi_copy_mem(void *destination, const void *source,
  2233. size_t length)
  2234. {
  2235. EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
  2236. memcpy(destination, source, length);
  2237. EFI_EXIT(EFI_SUCCESS);
  2238. }
  2239. /**
  2240. * efi_set_mem() - Fill memory with a byte value.
  2241. * @buffer: buffer to fill
  2242. * @size: size of buffer in bytes
  2243. * @value: byte to copy to the buffer
  2244. *
  2245. * This function implements the SetMem service.
  2246. *
  2247. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2248. * details.
  2249. */
  2250. static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
  2251. {
  2252. EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
  2253. memset(buffer, value, size);
  2254. EFI_EXIT(EFI_SUCCESS);
  2255. }
  2256. /**
  2257. * efi_protocol_open() - open protocol interface on a handle
  2258. * @handler: handler of a protocol
  2259. * @protocol_interface: interface implementing the protocol
  2260. * @agent_handle: handle of the driver
  2261. * @controller_handle: handle of the controller
  2262. * @attributes: attributes indicating how to open the protocol
  2263. *
  2264. * Return: status code
  2265. */
  2266. static efi_status_t efi_protocol_open(
  2267. struct efi_handler *handler,
  2268. void **protocol_interface, void *agent_handle,
  2269. void *controller_handle, uint32_t attributes)
  2270. {
  2271. struct efi_open_protocol_info_item *item;
  2272. struct efi_open_protocol_info_entry *match = NULL;
  2273. bool opened_by_driver = false;
  2274. bool opened_exclusive = false;
  2275. /* If there is no agent, only return the interface */
  2276. if (!agent_handle)
  2277. goto out;
  2278. /* For TEST_PROTOCOL ignore interface attribute */
  2279. if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
  2280. *protocol_interface = NULL;
  2281. /*
  2282. * Check if the protocol is already opened by a driver with the same
  2283. * attributes or opened exclusively
  2284. */
  2285. list_for_each_entry(item, &handler->open_infos, link) {
  2286. if (item->info.agent_handle == agent_handle) {
  2287. if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
  2288. (item->info.attributes == attributes))
  2289. return EFI_ALREADY_STARTED;
  2290. }
  2291. if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
  2292. opened_exclusive = true;
  2293. }
  2294. /* Only one controller can open the protocol exclusively */
  2295. if (opened_exclusive && attributes &
  2296. (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
  2297. return EFI_ACCESS_DENIED;
  2298. /* Prepare exclusive opening */
  2299. if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
  2300. /* Try to disconnect controllers */
  2301. list_for_each_entry(item, &handler->open_infos, link) {
  2302. if (item->info.attributes ==
  2303. EFI_OPEN_PROTOCOL_BY_DRIVER)
  2304. EFI_CALL(efi_disconnect_controller(
  2305. item->info.controller_handle,
  2306. item->info.agent_handle,
  2307. NULL));
  2308. }
  2309. opened_by_driver = false;
  2310. /* Check if all controllers are disconnected */
  2311. list_for_each_entry(item, &handler->open_infos, link) {
  2312. if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
  2313. opened_by_driver = true;
  2314. }
  2315. /* Only one controller can be conncected */
  2316. if (opened_by_driver)
  2317. return EFI_ACCESS_DENIED;
  2318. }
  2319. /* Find existing entry */
  2320. list_for_each_entry(item, &handler->open_infos, link) {
  2321. if (item->info.agent_handle == agent_handle &&
  2322. item->info.controller_handle == controller_handle)
  2323. match = &item->info;
  2324. }
  2325. /* None found, create one */
  2326. if (!match) {
  2327. match = efi_create_open_info(handler);
  2328. if (!match)
  2329. return EFI_OUT_OF_RESOURCES;
  2330. }
  2331. match->agent_handle = agent_handle;
  2332. match->controller_handle = controller_handle;
  2333. match->attributes = attributes;
  2334. match->open_count++;
  2335. out:
  2336. /* For TEST_PROTOCOL ignore interface attribute. */
  2337. if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
  2338. *protocol_interface = handler->protocol_interface;
  2339. return EFI_SUCCESS;
  2340. }
  2341. /**
  2342. * efi_open_protocol() - open protocol interface on a handle
  2343. * @handle: handle on which the protocol shall be opened
  2344. * @protocol: GUID of the protocol
  2345. * @protocol_interface: interface implementing the protocol
  2346. * @agent_handle: handle of the driver
  2347. * @controller_handle: handle of the controller
  2348. * @attributes: attributes indicating how to open the protocol
  2349. *
  2350. * This function implements the OpenProtocol interface.
  2351. *
  2352. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2353. * details.
  2354. *
  2355. * Return: status code
  2356. */
  2357. static efi_status_t EFIAPI efi_open_protocol(
  2358. void *handle, const efi_guid_t *protocol,
  2359. void **protocol_interface, void *agent_handle,
  2360. void *controller_handle, uint32_t attributes)
  2361. {
  2362. struct efi_handler *handler;
  2363. efi_status_t r = EFI_INVALID_PARAMETER;
  2364. EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
  2365. protocol_interface, agent_handle, controller_handle,
  2366. attributes);
  2367. if (!handle || !protocol ||
  2368. (!protocol_interface && attributes !=
  2369. EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
  2370. goto out;
  2371. }
  2372. switch (attributes) {
  2373. case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
  2374. case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
  2375. case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
  2376. break;
  2377. case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
  2378. if (controller_handle == handle)
  2379. goto out;
  2380. /* fall-through */
  2381. case EFI_OPEN_PROTOCOL_BY_DRIVER:
  2382. case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
  2383. /* Check that the controller handle is valid */
  2384. if (!efi_search_obj(controller_handle))
  2385. goto out;
  2386. /* fall-through */
  2387. case EFI_OPEN_PROTOCOL_EXCLUSIVE:
  2388. /* Check that the agent handle is valid */
  2389. if (!efi_search_obj(agent_handle))
  2390. goto out;
  2391. break;
  2392. default:
  2393. goto out;
  2394. }
  2395. r = efi_search_protocol(handle, protocol, &handler);
  2396. if (r != EFI_SUCCESS)
  2397. goto out;
  2398. r = efi_protocol_open(handler, protocol_interface, agent_handle,
  2399. controller_handle, attributes);
  2400. out:
  2401. return EFI_EXIT(r);
  2402. }
  2403. /**
  2404. * efi_handle_protocol() - get interface of a protocol on a handle
  2405. * @handle: handle on which the protocol shall be opened
  2406. * @protocol: GUID of the protocol
  2407. * @protocol_interface: interface implementing the protocol
  2408. *
  2409. * This function implements the HandleProtocol service.
  2410. *
  2411. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2412. * details.
  2413. *
  2414. * Return: status code
  2415. */
  2416. static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
  2417. const efi_guid_t *protocol,
  2418. void **protocol_interface)
  2419. {
  2420. return efi_open_protocol(handle, protocol, protocol_interface, NULL,
  2421. NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
  2422. }
  2423. /**
  2424. * efi_bind_controller() - bind a single driver to a controller
  2425. * @controller_handle: controller handle
  2426. * @driver_image_handle: driver handle
  2427. * @remain_device_path: remaining path
  2428. *
  2429. * Return: status code
  2430. */
  2431. static efi_status_t efi_bind_controller(
  2432. efi_handle_t controller_handle,
  2433. efi_handle_t driver_image_handle,
  2434. struct efi_device_path *remain_device_path)
  2435. {
  2436. struct efi_driver_binding_protocol *binding_protocol;
  2437. efi_status_t r;
  2438. r = EFI_CALL(efi_open_protocol(driver_image_handle,
  2439. &efi_guid_driver_binding_protocol,
  2440. (void **)&binding_protocol,
  2441. driver_image_handle, NULL,
  2442. EFI_OPEN_PROTOCOL_GET_PROTOCOL));
  2443. if (r != EFI_SUCCESS)
  2444. return r;
  2445. r = EFI_CALL(binding_protocol->supported(binding_protocol,
  2446. controller_handle,
  2447. remain_device_path));
  2448. if (r == EFI_SUCCESS)
  2449. r = EFI_CALL(binding_protocol->start(binding_protocol,
  2450. controller_handle,
  2451. remain_device_path));
  2452. EFI_CALL(efi_close_protocol(driver_image_handle,
  2453. &efi_guid_driver_binding_protocol,
  2454. driver_image_handle, NULL));
  2455. return r;
  2456. }
  2457. /**
  2458. * efi_connect_single_controller() - connect a single driver to a controller
  2459. * @controller_handle: controller
  2460. * @driver_image_handle: driver
  2461. * @remain_device_path: remainting path
  2462. *
  2463. * Return: status code
  2464. */
  2465. static efi_status_t efi_connect_single_controller(
  2466. efi_handle_t controller_handle,
  2467. efi_handle_t *driver_image_handle,
  2468. struct efi_device_path *remain_device_path)
  2469. {
  2470. efi_handle_t *buffer;
  2471. size_t count;
  2472. size_t i;
  2473. efi_status_t r;
  2474. size_t connected = 0;
  2475. /* Get buffer with all handles with driver binding protocol */
  2476. r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
  2477. &efi_guid_driver_binding_protocol,
  2478. NULL, &count, &buffer));
  2479. if (r != EFI_SUCCESS)
  2480. return r;
  2481. /* Context Override */
  2482. if (driver_image_handle) {
  2483. for (; *driver_image_handle; ++driver_image_handle) {
  2484. for (i = 0; i < count; ++i) {
  2485. if (buffer[i] == *driver_image_handle) {
  2486. buffer[i] = NULL;
  2487. r = efi_bind_controller(
  2488. controller_handle,
  2489. *driver_image_handle,
  2490. remain_device_path);
  2491. /*
  2492. * For drivers that do not support the
  2493. * controller or are already connected
  2494. * we receive an error code here.
  2495. */
  2496. if (r == EFI_SUCCESS)
  2497. ++connected;
  2498. }
  2499. }
  2500. }
  2501. }
  2502. /*
  2503. * TODO: Some overrides are not yet implemented:
  2504. * - Platform Driver Override
  2505. * - Driver Family Override Search
  2506. * - Bus Specific Driver Override
  2507. */
  2508. /* Driver Binding Search */
  2509. for (i = 0; i < count; ++i) {
  2510. if (buffer[i]) {
  2511. r = efi_bind_controller(controller_handle,
  2512. buffer[i],
  2513. remain_device_path);
  2514. if (r == EFI_SUCCESS)
  2515. ++connected;
  2516. }
  2517. }
  2518. efi_free_pool(buffer);
  2519. if (!connected)
  2520. return EFI_NOT_FOUND;
  2521. return EFI_SUCCESS;
  2522. }
  2523. /**
  2524. * efi_connect_controller() - connect a controller to a driver
  2525. * @controller_handle: handle of the controller
  2526. * @driver_image_handle: handle of the driver
  2527. * @remain_device_path: device path of a child controller
  2528. * @recursive: true to connect all child controllers
  2529. *
  2530. * This function implements the ConnectController service.
  2531. *
  2532. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2533. * details.
  2534. *
  2535. * First all driver binding protocol handles are tried for binding drivers.
  2536. * Afterwards all handles that have openened a protocol of the controller
  2537. * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
  2538. *
  2539. * Return: status code
  2540. */
  2541. static efi_status_t EFIAPI efi_connect_controller(
  2542. efi_handle_t controller_handle,
  2543. efi_handle_t *driver_image_handle,
  2544. struct efi_device_path *remain_device_path,
  2545. bool recursive)
  2546. {
  2547. efi_status_t r;
  2548. efi_status_t ret = EFI_NOT_FOUND;
  2549. struct efi_object *efiobj;
  2550. EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
  2551. remain_device_path, recursive);
  2552. efiobj = efi_search_obj(controller_handle);
  2553. if (!efiobj) {
  2554. ret = EFI_INVALID_PARAMETER;
  2555. goto out;
  2556. }
  2557. r = efi_connect_single_controller(controller_handle,
  2558. driver_image_handle,
  2559. remain_device_path);
  2560. if (r == EFI_SUCCESS)
  2561. ret = EFI_SUCCESS;
  2562. if (recursive) {
  2563. struct efi_handler *handler;
  2564. struct efi_open_protocol_info_item *item;
  2565. list_for_each_entry(handler, &efiobj->protocols, link) {
  2566. list_for_each_entry(item, &handler->open_infos, link) {
  2567. if (item->info.attributes &
  2568. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
  2569. r = EFI_CALL(efi_connect_controller(
  2570. item->info.controller_handle,
  2571. driver_image_handle,
  2572. remain_device_path,
  2573. recursive));
  2574. if (r == EFI_SUCCESS)
  2575. ret = EFI_SUCCESS;
  2576. }
  2577. }
  2578. }
  2579. }
  2580. /* Check for child controller specified by end node */
  2581. if (ret != EFI_SUCCESS && remain_device_path &&
  2582. remain_device_path->type == DEVICE_PATH_TYPE_END)
  2583. ret = EFI_SUCCESS;
  2584. out:
  2585. return EFI_EXIT(ret);
  2586. }
  2587. /**
  2588. * efi_reinstall_protocol_interface() - reinstall protocol interface
  2589. * @handle: handle on which the protocol shall be reinstalled
  2590. * @protocol: GUID of the protocol to be installed
  2591. * @old_interface: interface to be removed
  2592. * @new_interface: interface to be installed
  2593. *
  2594. * This function implements the ReinstallProtocolInterface service.
  2595. *
  2596. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2597. * details.
  2598. *
  2599. * The old interface is uninstalled. The new interface is installed.
  2600. * Drivers are connected.
  2601. *
  2602. * Return: status code
  2603. */
  2604. static efi_status_t EFIAPI efi_reinstall_protocol_interface(
  2605. efi_handle_t handle, const efi_guid_t *protocol,
  2606. void *old_interface, void *new_interface)
  2607. {
  2608. efi_status_t ret;
  2609. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
  2610. new_interface);
  2611. ret = EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
  2612. old_interface));
  2613. if (ret != EFI_SUCCESS)
  2614. goto out;
  2615. ret = EFI_CALL(efi_install_protocol_interface(&handle, protocol,
  2616. EFI_NATIVE_INTERFACE,
  2617. new_interface));
  2618. if (ret != EFI_SUCCESS)
  2619. goto out;
  2620. /*
  2621. * The returned status code has to be ignored.
  2622. * Do not create an error if no suitable driver for the handle exists.
  2623. */
  2624. EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
  2625. out:
  2626. return EFI_EXIT(ret);
  2627. }
  2628. /**
  2629. * efi_get_child_controllers() - get all child controllers associated to a driver
  2630. * @efiobj: handle of the controller
  2631. * @driver_handle: handle of the driver
  2632. * @number_of_children: number of child controllers
  2633. * @child_handle_buffer: handles of the the child controllers
  2634. *
  2635. * The allocated buffer has to be freed with free().
  2636. *
  2637. * Return: status code
  2638. */
  2639. static efi_status_t efi_get_child_controllers(
  2640. struct efi_object *efiobj,
  2641. efi_handle_t driver_handle,
  2642. efi_uintn_t *number_of_children,
  2643. efi_handle_t **child_handle_buffer)
  2644. {
  2645. struct efi_handler *handler;
  2646. struct efi_open_protocol_info_item *item;
  2647. efi_uintn_t count = 0, i;
  2648. bool duplicate;
  2649. /* Count all child controller associations */
  2650. list_for_each_entry(handler, &efiobj->protocols, link) {
  2651. list_for_each_entry(item, &handler->open_infos, link) {
  2652. if (item->info.agent_handle == driver_handle &&
  2653. item->info.attributes &
  2654. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
  2655. ++count;
  2656. }
  2657. }
  2658. /*
  2659. * Create buffer. In case of duplicate child controller assignments
  2660. * the buffer will be too large. But that does not harm.
  2661. */
  2662. *number_of_children = 0;
  2663. *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
  2664. if (!*child_handle_buffer)
  2665. return EFI_OUT_OF_RESOURCES;
  2666. /* Copy unique child handles */
  2667. list_for_each_entry(handler, &efiobj->protocols, link) {
  2668. list_for_each_entry(item, &handler->open_infos, link) {
  2669. if (item->info.agent_handle == driver_handle &&
  2670. item->info.attributes &
  2671. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
  2672. /* Check this is a new child controller */
  2673. duplicate = false;
  2674. for (i = 0; i < *number_of_children; ++i) {
  2675. if ((*child_handle_buffer)[i] ==
  2676. item->info.controller_handle)
  2677. duplicate = true;
  2678. }
  2679. /* Copy handle to buffer */
  2680. if (!duplicate) {
  2681. i = (*number_of_children)++;
  2682. (*child_handle_buffer)[i] =
  2683. item->info.controller_handle;
  2684. }
  2685. }
  2686. }
  2687. }
  2688. return EFI_SUCCESS;
  2689. }
  2690. /**
  2691. * efi_disconnect_controller() - disconnect a controller from a driver
  2692. * @controller_handle: handle of the controller
  2693. * @driver_image_handle: handle of the driver
  2694. * @child_handle: handle of the child to destroy
  2695. *
  2696. * This function implements the DisconnectController service.
  2697. *
  2698. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2699. * details.
  2700. *
  2701. * Return: status code
  2702. */
  2703. static efi_status_t EFIAPI efi_disconnect_controller(
  2704. efi_handle_t controller_handle,
  2705. efi_handle_t driver_image_handle,
  2706. efi_handle_t child_handle)
  2707. {
  2708. struct efi_driver_binding_protocol *binding_protocol;
  2709. efi_handle_t *child_handle_buffer = NULL;
  2710. size_t number_of_children = 0;
  2711. efi_status_t r;
  2712. size_t stop_count = 0;
  2713. struct efi_object *efiobj;
  2714. EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
  2715. child_handle);
  2716. efiobj = efi_search_obj(controller_handle);
  2717. if (!efiobj) {
  2718. r = EFI_INVALID_PARAMETER;
  2719. goto out;
  2720. }
  2721. if (child_handle && !efi_search_obj(child_handle)) {
  2722. r = EFI_INVALID_PARAMETER;
  2723. goto out;
  2724. }
  2725. /* If no driver handle is supplied, disconnect all drivers */
  2726. if (!driver_image_handle) {
  2727. r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
  2728. goto out;
  2729. }
  2730. /* Create list of child handles */
  2731. if (child_handle) {
  2732. number_of_children = 1;
  2733. child_handle_buffer = &child_handle;
  2734. } else {
  2735. efi_get_child_controllers(efiobj,
  2736. driver_image_handle,
  2737. &number_of_children,
  2738. &child_handle_buffer);
  2739. }
  2740. /* Get the driver binding protocol */
  2741. r = EFI_CALL(efi_open_protocol(driver_image_handle,
  2742. &efi_guid_driver_binding_protocol,
  2743. (void **)&binding_protocol,
  2744. driver_image_handle, NULL,
  2745. EFI_OPEN_PROTOCOL_GET_PROTOCOL));
  2746. if (r != EFI_SUCCESS)
  2747. goto out;
  2748. /* Remove the children */
  2749. if (number_of_children) {
  2750. r = EFI_CALL(binding_protocol->stop(binding_protocol,
  2751. controller_handle,
  2752. number_of_children,
  2753. child_handle_buffer));
  2754. if (r == EFI_SUCCESS)
  2755. ++stop_count;
  2756. }
  2757. /* Remove the driver */
  2758. if (!child_handle)
  2759. r = EFI_CALL(binding_protocol->stop(binding_protocol,
  2760. controller_handle,
  2761. 0, NULL));
  2762. if (r == EFI_SUCCESS)
  2763. ++stop_count;
  2764. EFI_CALL(efi_close_protocol(driver_image_handle,
  2765. &efi_guid_driver_binding_protocol,
  2766. driver_image_handle, NULL));
  2767. if (stop_count)
  2768. r = EFI_SUCCESS;
  2769. else
  2770. r = EFI_NOT_FOUND;
  2771. out:
  2772. if (!child_handle)
  2773. free(child_handle_buffer);
  2774. return EFI_EXIT(r);
  2775. }
  2776. static struct efi_boot_services efi_boot_services = {
  2777. .hdr = {
  2778. .signature = EFI_BOOT_SERVICES_SIGNATURE,
  2779. .revision = EFI_SPECIFICATION_VERSION,
  2780. .headersize = sizeof(struct efi_boot_services),
  2781. },
  2782. .raise_tpl = efi_raise_tpl,
  2783. .restore_tpl = efi_restore_tpl,
  2784. .allocate_pages = efi_allocate_pages_ext,
  2785. .free_pages = efi_free_pages_ext,
  2786. .get_memory_map = efi_get_memory_map_ext,
  2787. .allocate_pool = efi_allocate_pool_ext,
  2788. .free_pool = efi_free_pool_ext,
  2789. .create_event = efi_create_event_ext,
  2790. .set_timer = efi_set_timer_ext,
  2791. .wait_for_event = efi_wait_for_event,
  2792. .signal_event = efi_signal_event_ext,
  2793. .close_event = efi_close_event,
  2794. .check_event = efi_check_event,
  2795. .install_protocol_interface = efi_install_protocol_interface,
  2796. .reinstall_protocol_interface = efi_reinstall_protocol_interface,
  2797. .uninstall_protocol_interface = efi_uninstall_protocol_interface,
  2798. .handle_protocol = efi_handle_protocol,
  2799. .reserved = NULL,
  2800. .register_protocol_notify = efi_register_protocol_notify,
  2801. .locate_handle = efi_locate_handle_ext,
  2802. .locate_device_path = efi_locate_device_path,
  2803. .install_configuration_table = efi_install_configuration_table_ext,
  2804. .load_image = efi_load_image,
  2805. .start_image = efi_start_image,
  2806. .exit = efi_exit,
  2807. .unload_image = efi_unload_image,
  2808. .exit_boot_services = efi_exit_boot_services,
  2809. .get_next_monotonic_count = efi_get_next_monotonic_count,
  2810. .stall = efi_stall,
  2811. .set_watchdog_timer = efi_set_watchdog_timer,
  2812. .connect_controller = efi_connect_controller,
  2813. .disconnect_controller = efi_disconnect_controller,
  2814. .open_protocol = efi_open_protocol,
  2815. .close_protocol = efi_close_protocol,
  2816. .open_protocol_information = efi_open_protocol_information,
  2817. .protocols_per_handle = efi_protocols_per_handle,
  2818. .locate_handle_buffer = efi_locate_handle_buffer,
  2819. .locate_protocol = efi_locate_protocol,
  2820. .install_multiple_protocol_interfaces =
  2821. efi_install_multiple_protocol_interfaces,
  2822. .uninstall_multiple_protocol_interfaces =
  2823. efi_uninstall_multiple_protocol_interfaces,
  2824. .calculate_crc32 = efi_calculate_crc32,
  2825. .copy_mem = efi_copy_mem,
  2826. .set_mem = efi_set_mem,
  2827. .create_event_ex = efi_create_event_ex,
  2828. };
  2829. static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
  2830. struct efi_system_table __efi_runtime_data systab = {
  2831. .hdr = {
  2832. .signature = EFI_SYSTEM_TABLE_SIGNATURE,
  2833. .revision = EFI_SPECIFICATION_VERSION,
  2834. .headersize = sizeof(struct efi_system_table),
  2835. },
  2836. .fw_vendor = firmware_vendor,
  2837. .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
  2838. .con_in = (void *)&efi_con_in,
  2839. .con_out = (void *)&efi_con_out,
  2840. .std_err = (void *)&efi_con_out,
  2841. .runtime = (void *)&efi_runtime_services,
  2842. .boottime = (void *)&efi_boot_services,
  2843. .nr_tables = 0,
  2844. .tables = NULL,
  2845. };
  2846. /**
  2847. * efi_initialize_system_table() - Initialize system table
  2848. *
  2849. * Return Value: status code
  2850. */
  2851. efi_status_t efi_initialize_system_table(void)
  2852. {
  2853. efi_status_t ret;
  2854. /* Allocate configuration table array */
  2855. ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
  2856. EFI_MAX_CONFIGURATION_TABLES *
  2857. sizeof(struct efi_configuration_table),
  2858. (void **)&systab.tables);
  2859. /* Set crc32 field in table headers */
  2860. efi_update_table_header_crc32(&systab.hdr);
  2861. efi_update_table_header_crc32(&efi_runtime_services.hdr);
  2862. efi_update_table_header_crc32(&efi_boot_services.hdr);
  2863. return ret;
  2864. }