efi_boottime.c 85 KB

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