efi_boottime.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144
  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 msg, without any care about returning back
  95. * 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. * ReturnValue: 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 bootup */
  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 100ns
  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 100ns.
  683. * We use multiples of 1000ns. 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 100ns
  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. if (device_path) {
  1368. info->device_handle = efi_dp_find_obj(device_path, NULL);
  1369. /*
  1370. * When asking for the device path interface, return
  1371. * bootefi_device_path
  1372. */
  1373. ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
  1374. device_path);
  1375. if (ret != EFI_SUCCESS)
  1376. goto failure;
  1377. }
  1378. /*
  1379. * When asking for the loaded_image interface, just
  1380. * return handle which points to loaded_image_info
  1381. */
  1382. ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
  1383. if (ret != EFI_SUCCESS)
  1384. goto failure;
  1385. ret = efi_add_protocol(obj->handle,
  1386. &efi_guid_device_path_to_text_protocol,
  1387. (void *)&efi_device_path_to_text);
  1388. if (ret != EFI_SUCCESS)
  1389. goto failure;
  1390. ret = efi_add_protocol(obj->handle,
  1391. &efi_guid_device_path_utilities_protocol,
  1392. (void *)&efi_device_path_utilities);
  1393. if (ret != EFI_SUCCESS)
  1394. goto failure;
  1395. return ret;
  1396. failure:
  1397. printf("ERROR: Failure to install protocols for loaded image\n");
  1398. return ret;
  1399. }
  1400. /**
  1401. * efi_load_image_from_path() - load an image using a file path
  1402. * @file_path: the path of the image to load
  1403. * @buffer: buffer containing the loaded image
  1404. *
  1405. * Return: status code
  1406. */
  1407. efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
  1408. void **buffer)
  1409. {
  1410. struct efi_file_info *info = NULL;
  1411. struct efi_file_handle *f;
  1412. static efi_status_t ret;
  1413. efi_uintn_t bs;
  1414. f = efi_file_from_path(file_path);
  1415. if (!f)
  1416. return EFI_DEVICE_ERROR;
  1417. bs = 0;
  1418. EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
  1419. &bs, info));
  1420. if (ret == EFI_BUFFER_TOO_SMALL) {
  1421. info = malloc(bs);
  1422. EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
  1423. &bs, info));
  1424. }
  1425. if (ret != EFI_SUCCESS)
  1426. goto error;
  1427. ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
  1428. if (ret)
  1429. goto error;
  1430. bs = info->file_size;
  1431. EFI_CALL(ret = f->read(f, &bs, *buffer));
  1432. error:
  1433. free(info);
  1434. EFI_CALL(f->close(f));
  1435. if (ret != EFI_SUCCESS) {
  1436. efi_free_pool(*buffer);
  1437. *buffer = NULL;
  1438. }
  1439. return ret;
  1440. }
  1441. /**
  1442. * efi_load_image() - load an EFI image into memory
  1443. * @boot_policy: true for request originating from the boot manager
  1444. * @parent_image: the caller's image handle
  1445. * @file_path: the path of the image to load
  1446. * @source_buffer: memory location from which the image is installed
  1447. * @source_size: size of the memory area from which the image is installed
  1448. * @image_handle: handle for the newly installed image
  1449. *
  1450. * This function implements the LoadImage service.
  1451. *
  1452. * See the Unified Extensible Firmware Interface (UEFI) specification
  1453. * for details.
  1454. *
  1455. * Return: status code
  1456. */
  1457. static efi_status_t EFIAPI efi_load_image(bool boot_policy,
  1458. efi_handle_t parent_image,
  1459. struct efi_device_path *file_path,
  1460. void *source_buffer,
  1461. efi_uintn_t source_size,
  1462. efi_handle_t *image_handle)
  1463. {
  1464. struct efi_loaded_image *info;
  1465. struct efi_object *obj;
  1466. efi_status_t ret;
  1467. EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
  1468. file_path, source_buffer, source_size, image_handle);
  1469. if (!image_handle || !parent_image) {
  1470. ret = EFI_INVALID_PARAMETER;
  1471. goto error;
  1472. }
  1473. if (!source_buffer && !file_path) {
  1474. ret = EFI_NOT_FOUND;
  1475. goto error;
  1476. }
  1477. info = calloc(1, sizeof(*info));
  1478. if (!info) {
  1479. ret = EFI_OUT_OF_RESOURCES;
  1480. goto error;
  1481. }
  1482. obj = calloc(1, sizeof(*obj));
  1483. if (!obj) {
  1484. free(info);
  1485. ret = EFI_OUT_OF_RESOURCES;
  1486. goto error;
  1487. }
  1488. if (!source_buffer) {
  1489. struct efi_device_path *dp, *fp;
  1490. ret = efi_load_image_from_path(file_path, &source_buffer);
  1491. if (ret != EFI_SUCCESS)
  1492. goto failure;
  1493. /*
  1494. * split file_path which contains both the device and
  1495. * file parts:
  1496. */
  1497. efi_dp_split_file_path(file_path, &dp, &fp);
  1498. ret = efi_setup_loaded_image(info, obj, dp, fp);
  1499. if (ret != EFI_SUCCESS)
  1500. goto failure;
  1501. } else {
  1502. /* In this case, file_path is the "device" path, ie.
  1503. * something like a HARDWARE_DEVICE:MEMORY_MAPPED
  1504. */
  1505. ret = efi_setup_loaded_image(info, obj, file_path, NULL);
  1506. if (ret != EFI_SUCCESS)
  1507. goto failure;
  1508. }
  1509. info->reserved = efi_load_pe(source_buffer, info);
  1510. if (!info->reserved) {
  1511. ret = EFI_UNSUPPORTED;
  1512. goto failure;
  1513. }
  1514. info->system_table = &systab;
  1515. info->parent_handle = parent_image;
  1516. *image_handle = obj->handle;
  1517. return EFI_EXIT(EFI_SUCCESS);
  1518. failure:
  1519. free(info);
  1520. efi_delete_handle(obj);
  1521. error:
  1522. return EFI_EXIT(ret);
  1523. }
  1524. /**
  1525. * efi_start_image() - dall the entry point of an image
  1526. * @image_handle: handle of the image
  1527. * @exit_data_size: size of the buffer
  1528. * @exit_data: buffer to receive the exit data of the called image
  1529. *
  1530. * This function implements the StartImage service.
  1531. *
  1532. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1533. * details.
  1534. *
  1535. * Return: status code
  1536. */
  1537. static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
  1538. unsigned long *exit_data_size,
  1539. s16 **exit_data)
  1540. {
  1541. EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
  1542. struct efi_system_table *st);
  1543. struct efi_loaded_image *info = image_handle;
  1544. efi_status_t ret;
  1545. EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
  1546. entry = info->reserved;
  1547. efi_is_direct_boot = false;
  1548. /* call the image! */
  1549. if (setjmp(&info->exit_jmp)) {
  1550. /*
  1551. * We called the entry point of the child image with EFI_CALL
  1552. * in the lines below. The child image called the Exit() boot
  1553. * service efi_exit() which executed the long jump that brought
  1554. * us to the current line. This implies that the second half
  1555. * of the EFI_CALL macro has not been executed.
  1556. */
  1557. #ifdef CONFIG_ARM
  1558. /*
  1559. * efi_exit() called efi_restore_gd(). We have to undo this
  1560. * otherwise __efi_entry_check() will put the wrong value into
  1561. * app_gd.
  1562. */
  1563. gd = app_gd;
  1564. #endif
  1565. /*
  1566. * To get ready to call EFI_EXIT below we have to execute the
  1567. * missed out steps of EFI_CALL.
  1568. */
  1569. assert(__efi_entry_check());
  1570. debug("%sEFI: %lu returned by started image\n",
  1571. __efi_nesting_dec(),
  1572. (unsigned long)((uintptr_t)info->exit_status &
  1573. ~EFI_ERROR_MASK));
  1574. return EFI_EXIT(info->exit_status);
  1575. }
  1576. ret = EFI_CALL(entry(image_handle, &systab));
  1577. /*
  1578. * Usually UEFI applications call Exit() instead of returning.
  1579. * But because the world doesn not consist of ponies and unicorns,
  1580. * we're happy to emulate that behavior on behalf of a payload
  1581. * that forgot.
  1582. */
  1583. return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
  1584. }
  1585. /**
  1586. * efi_exit() - leave an EFI application or driver
  1587. * @image_handle: handle of the application or driver that is exiting
  1588. * @exit_status: status code
  1589. * @exit_data_size: size of the buffer in bytes
  1590. * @exit_data: buffer with data describing an error
  1591. *
  1592. * This function implements the Exit service.
  1593. *
  1594. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1595. * details.
  1596. *
  1597. * Return: status code
  1598. */
  1599. static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
  1600. efi_status_t exit_status,
  1601. unsigned long exit_data_size,
  1602. int16_t *exit_data)
  1603. {
  1604. /*
  1605. * We require that the handle points to the original loaded
  1606. * image protocol interface.
  1607. *
  1608. * For getting the longjmp address this is safer than locating
  1609. * the protocol because the protocol may have been reinstalled
  1610. * pointing to another memory location.
  1611. *
  1612. * TODO: We should call the unload procedure of the loaded
  1613. * image protocol.
  1614. */
  1615. struct efi_loaded_image *loaded_image_info = (void *)image_handle;
  1616. EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
  1617. exit_data_size, exit_data);
  1618. /* Make sure entry/exit counts for EFI world cross-overs match */
  1619. EFI_EXIT(exit_status);
  1620. /*
  1621. * But longjmp out with the U-Boot gd, not the application's, as
  1622. * the other end is a setjmp call inside EFI context.
  1623. */
  1624. efi_restore_gd();
  1625. loaded_image_info->exit_status = exit_status;
  1626. longjmp(&loaded_image_info->exit_jmp, 1);
  1627. panic("EFI application exited");
  1628. }
  1629. /**
  1630. * efi_unload_image() - unload an EFI image
  1631. * @image_handle: handle of the image to be unloaded
  1632. *
  1633. * This function implements the UnloadImage service.
  1634. *
  1635. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1636. * details.
  1637. *
  1638. * Return: status code
  1639. */
  1640. static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
  1641. {
  1642. struct efi_object *efiobj;
  1643. EFI_ENTRY("%p", image_handle);
  1644. efiobj = efi_search_obj(image_handle);
  1645. if (efiobj)
  1646. list_del(&efiobj->link);
  1647. return EFI_EXIT(EFI_SUCCESS);
  1648. }
  1649. /**
  1650. * efi_exit_caches() - fix up caches for EFI payloads if necessary
  1651. */
  1652. static void efi_exit_caches(void)
  1653. {
  1654. #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
  1655. /*
  1656. * Grub on 32bit ARM needs to have caches disabled before jumping into
  1657. * a zImage, but does not know of all cache layers. Give it a hand.
  1658. */
  1659. if (efi_is_direct_boot)
  1660. cleanup_before_linux();
  1661. #endif
  1662. }
  1663. /**
  1664. * efi_exit_boot_services() - stop all boot services
  1665. * @image_handle: handle of the loaded image
  1666. * @map_key: key of the memory map
  1667. *
  1668. * This function implements the ExitBootServices service.
  1669. *
  1670. * See the Unified Extensible Firmware Interface (UEFI) specification
  1671. * for details.
  1672. *
  1673. * All timer events are disabled. For exit boot services events the
  1674. * notification function is called. The boot services are disabled in the
  1675. * system table.
  1676. *
  1677. * Return: status code
  1678. */
  1679. static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
  1680. unsigned long map_key)
  1681. {
  1682. struct efi_event *evt;
  1683. EFI_ENTRY("%p, %ld", image_handle, map_key);
  1684. /* Check that the caller has read the current memory map */
  1685. if (map_key != efi_memory_map_key)
  1686. return EFI_INVALID_PARAMETER;
  1687. /* Make sure that notification functions are not called anymore */
  1688. efi_tpl = TPL_HIGH_LEVEL;
  1689. /* Check if ExitBootServices has already been called */
  1690. if (!systab.boottime)
  1691. return EFI_EXIT(EFI_SUCCESS);
  1692. /* Add related events to the event group */
  1693. list_for_each_entry(evt, &efi_events, link) {
  1694. if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
  1695. evt->group = &efi_guid_event_group_exit_boot_services;
  1696. }
  1697. /* Notify that ExitBootServices is invoked. */
  1698. list_for_each_entry(evt, &efi_events, link) {
  1699. if (evt->group &&
  1700. !guidcmp(evt->group,
  1701. &efi_guid_event_group_exit_boot_services)) {
  1702. efi_signal_event(evt, false);
  1703. break;
  1704. }
  1705. }
  1706. /* TODO Should persist EFI variables here */
  1707. board_quiesce_devices();
  1708. /* Fix up caches for EFI payloads if necessary */
  1709. efi_exit_caches();
  1710. /* This stops all lingering devices */
  1711. bootm_disable_interrupts();
  1712. /* Disable boottime services */
  1713. systab.con_in_handle = NULL;
  1714. systab.con_in = NULL;
  1715. systab.con_out_handle = NULL;
  1716. systab.con_out = NULL;
  1717. systab.stderr_handle = NULL;
  1718. systab.std_err = NULL;
  1719. systab.boottime = NULL;
  1720. /* Recalculate CRC32 */
  1721. efi_update_table_header_crc32(&systab.hdr);
  1722. /* Give the payload some time to boot */
  1723. efi_set_watchdog(0);
  1724. WATCHDOG_RESET();
  1725. return EFI_EXIT(EFI_SUCCESS);
  1726. }
  1727. /**
  1728. * efi_get_next_monotonic_count() - get next value of the counter
  1729. * @count: returned value of the counter
  1730. *
  1731. * This function implements the NextMonotonicCount service.
  1732. *
  1733. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1734. * details.
  1735. *
  1736. * Return: status code
  1737. */
  1738. static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
  1739. {
  1740. static uint64_t mono;
  1741. EFI_ENTRY("%p", count);
  1742. *count = mono++;
  1743. return EFI_EXIT(EFI_SUCCESS);
  1744. }
  1745. /**
  1746. * efi_stall() - sleep
  1747. * @microseconds: period to sleep in microseconds
  1748. *
  1749. * This function implements the Stall service.
  1750. *
  1751. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1752. * details.
  1753. *
  1754. * Return: status code
  1755. */
  1756. static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
  1757. {
  1758. EFI_ENTRY("%ld", microseconds);
  1759. udelay(microseconds);
  1760. return EFI_EXIT(EFI_SUCCESS);
  1761. }
  1762. /**
  1763. * efi_set_watchdog_timer() - reset the watchdog timer
  1764. * @timeout: seconds before reset by watchdog
  1765. * @watchdog_code: code to be logged when resetting
  1766. * @data_size: size of buffer in bytes
  1767. * @watchdog_data: buffer with data describing the reset reason
  1768. *
  1769. * This function implements the SetWatchdogTimer service.
  1770. *
  1771. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1772. * details.
  1773. *
  1774. * Return: status code
  1775. */
  1776. static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
  1777. uint64_t watchdog_code,
  1778. unsigned long data_size,
  1779. uint16_t *watchdog_data)
  1780. {
  1781. EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
  1782. data_size, watchdog_data);
  1783. return EFI_EXIT(efi_set_watchdog(timeout));
  1784. }
  1785. /**
  1786. * efi_close_protocol() - close a protocol
  1787. * @handle: handle on which the protocol shall be closed
  1788. * @protocol: GUID of the protocol to close
  1789. * @agent_handle: handle of the driver
  1790. * @controller_handle: handle of the controller
  1791. *
  1792. * This function implements the CloseProtocol service.
  1793. *
  1794. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1795. * details.
  1796. *
  1797. * Return: status code
  1798. */
  1799. static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
  1800. const efi_guid_t *protocol,
  1801. efi_handle_t agent_handle,
  1802. efi_handle_t controller_handle)
  1803. {
  1804. struct efi_handler *handler;
  1805. struct efi_open_protocol_info_item *item;
  1806. struct efi_open_protocol_info_item *pos;
  1807. efi_status_t r;
  1808. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
  1809. controller_handle);
  1810. if (!agent_handle) {
  1811. r = EFI_INVALID_PARAMETER;
  1812. goto out;
  1813. }
  1814. r = efi_search_protocol(handle, protocol, &handler);
  1815. if (r != EFI_SUCCESS)
  1816. goto out;
  1817. r = EFI_NOT_FOUND;
  1818. list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
  1819. if (item->info.agent_handle == agent_handle &&
  1820. item->info.controller_handle == controller_handle) {
  1821. efi_delete_open_info(item);
  1822. r = EFI_SUCCESS;
  1823. break;
  1824. }
  1825. }
  1826. out:
  1827. return EFI_EXIT(r);
  1828. }
  1829. /**
  1830. * efi_open_protocol_information() - provide information about then open status
  1831. * of a protocol on a handle
  1832. * @handle: handle for which the information shall be retrieved
  1833. * @protocol: GUID of the protocol
  1834. * @entry_buffer: buffer to receive the open protocol information
  1835. * @entry_count: number of entries available in the buffer
  1836. *
  1837. * This function implements the OpenProtocolInformation service.
  1838. *
  1839. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1840. * details.
  1841. *
  1842. * Return: status code
  1843. */
  1844. static efi_status_t EFIAPI efi_open_protocol_information(
  1845. efi_handle_t handle, const efi_guid_t *protocol,
  1846. struct efi_open_protocol_info_entry **entry_buffer,
  1847. efi_uintn_t *entry_count)
  1848. {
  1849. unsigned long buffer_size;
  1850. unsigned long count;
  1851. struct efi_handler *handler;
  1852. struct efi_open_protocol_info_item *item;
  1853. efi_status_t r;
  1854. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
  1855. entry_count);
  1856. /* Check parameters */
  1857. if (!entry_buffer) {
  1858. r = EFI_INVALID_PARAMETER;
  1859. goto out;
  1860. }
  1861. r = efi_search_protocol(handle, protocol, &handler);
  1862. if (r != EFI_SUCCESS)
  1863. goto out;
  1864. /* Count entries */
  1865. count = 0;
  1866. list_for_each_entry(item, &handler->open_infos, link) {
  1867. if (item->info.open_count)
  1868. ++count;
  1869. }
  1870. *entry_count = count;
  1871. *entry_buffer = NULL;
  1872. if (!count) {
  1873. r = EFI_SUCCESS;
  1874. goto out;
  1875. }
  1876. /* Copy entries */
  1877. buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
  1878. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
  1879. (void **)entry_buffer);
  1880. if (r != EFI_SUCCESS)
  1881. goto out;
  1882. list_for_each_entry_reverse(item, &handler->open_infos, link) {
  1883. if (item->info.open_count)
  1884. (*entry_buffer)[--count] = item->info;
  1885. }
  1886. out:
  1887. return EFI_EXIT(r);
  1888. }
  1889. /**
  1890. * efi_protocols_per_handle() - get protocols installed on a handle
  1891. * @handle: handle for which the information is retrieved
  1892. * @protocol_buffer: buffer with protocol GUIDs
  1893. * @protocol_buffer_count: number of entries in the buffer
  1894. *
  1895. * This function implements the ProtocolsPerHandleService.
  1896. *
  1897. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1898. * details.
  1899. *
  1900. * Return: status code
  1901. */
  1902. static efi_status_t EFIAPI efi_protocols_per_handle(
  1903. efi_handle_t handle, efi_guid_t ***protocol_buffer,
  1904. efi_uintn_t *protocol_buffer_count)
  1905. {
  1906. unsigned long buffer_size;
  1907. struct efi_object *efiobj;
  1908. struct list_head *protocol_handle;
  1909. efi_status_t r;
  1910. EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
  1911. protocol_buffer_count);
  1912. if (!handle || !protocol_buffer || !protocol_buffer_count)
  1913. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1914. *protocol_buffer = NULL;
  1915. *protocol_buffer_count = 0;
  1916. efiobj = efi_search_obj(handle);
  1917. if (!efiobj)
  1918. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1919. /* Count protocols */
  1920. list_for_each(protocol_handle, &efiobj->protocols) {
  1921. ++*protocol_buffer_count;
  1922. }
  1923. /* Copy guids */
  1924. if (*protocol_buffer_count) {
  1925. size_t j = 0;
  1926. buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
  1927. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
  1928. (void **)protocol_buffer);
  1929. if (r != EFI_SUCCESS)
  1930. return EFI_EXIT(r);
  1931. list_for_each(protocol_handle, &efiobj->protocols) {
  1932. struct efi_handler *protocol;
  1933. protocol = list_entry(protocol_handle,
  1934. struct efi_handler, link);
  1935. (*protocol_buffer)[j] = (void *)protocol->guid;
  1936. ++j;
  1937. }
  1938. }
  1939. return EFI_EXIT(EFI_SUCCESS);
  1940. }
  1941. /**
  1942. * efi_locate_handle_buffer() - locate handles implementing a protocol
  1943. * @search_type: selection criterion
  1944. * @protocol: GUID of the protocol
  1945. * @search_key: registration key
  1946. * @no_handles: number of returned handles
  1947. * @buffer: buffer with the returned handles
  1948. *
  1949. * This function implements the LocateHandleBuffer service.
  1950. *
  1951. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1952. * details.
  1953. *
  1954. * Return: status code
  1955. */
  1956. static efi_status_t EFIAPI efi_locate_handle_buffer(
  1957. enum efi_locate_search_type search_type,
  1958. const efi_guid_t *protocol, void *search_key,
  1959. efi_uintn_t *no_handles, efi_handle_t **buffer)
  1960. {
  1961. efi_status_t r;
  1962. efi_uintn_t buffer_size = 0;
  1963. EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
  1964. no_handles, buffer);
  1965. if (!no_handles || !buffer) {
  1966. r = EFI_INVALID_PARAMETER;
  1967. goto out;
  1968. }
  1969. *no_handles = 0;
  1970. *buffer = NULL;
  1971. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  1972. *buffer);
  1973. if (r != EFI_BUFFER_TOO_SMALL)
  1974. goto out;
  1975. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
  1976. (void **)buffer);
  1977. if (r != EFI_SUCCESS)
  1978. goto out;
  1979. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  1980. *buffer);
  1981. if (r == EFI_SUCCESS)
  1982. *no_handles = buffer_size / sizeof(efi_handle_t);
  1983. out:
  1984. return EFI_EXIT(r);
  1985. }
  1986. /**
  1987. * efi_locate_protocol() - find an interface implementing a protocol
  1988. * @protocol: GUID of the protocol
  1989. * @registration: registration key passed to the notification function
  1990. * @protocol_interface: interface implementing the protocol
  1991. *
  1992. * This function implements the LocateProtocol service.
  1993. *
  1994. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1995. * details.
  1996. *
  1997. * Return: status code
  1998. */
  1999. static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
  2000. void *registration,
  2001. void **protocol_interface)
  2002. {
  2003. struct list_head *lhandle;
  2004. efi_status_t ret;
  2005. EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
  2006. if (!protocol || !protocol_interface)
  2007. return EFI_EXIT(EFI_INVALID_PARAMETER);
  2008. list_for_each(lhandle, &efi_obj_list) {
  2009. struct efi_object *efiobj;
  2010. struct efi_handler *handler;
  2011. efiobj = list_entry(lhandle, struct efi_object, link);
  2012. ret = efi_search_protocol(efiobj->handle, protocol, &handler);
  2013. if (ret == EFI_SUCCESS) {
  2014. *protocol_interface = handler->protocol_interface;
  2015. return EFI_EXIT(EFI_SUCCESS);
  2016. }
  2017. }
  2018. *protocol_interface = NULL;
  2019. return EFI_EXIT(EFI_NOT_FOUND);
  2020. }
  2021. /**
  2022. * efi_locate_device_path() - Get the device path and handle of an device
  2023. * implementing a protocol
  2024. * @protocol: GUID of the protocol
  2025. * @device_path: device path
  2026. * @device: handle of the device
  2027. *
  2028. * This function implements the LocateDevicePath service.
  2029. *
  2030. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2031. * details.
  2032. *
  2033. * Return: status code
  2034. */
  2035. static efi_status_t EFIAPI efi_locate_device_path(
  2036. const efi_guid_t *protocol,
  2037. struct efi_device_path **device_path,
  2038. efi_handle_t *device)
  2039. {
  2040. struct efi_device_path *dp;
  2041. size_t i;
  2042. struct efi_handler *handler;
  2043. efi_handle_t *handles;
  2044. size_t len, len_dp;
  2045. size_t len_best = 0;
  2046. efi_uintn_t no_handles;
  2047. u8 *remainder;
  2048. efi_status_t ret;
  2049. EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
  2050. if (!protocol || !device_path || !*device_path || !device) {
  2051. ret = EFI_INVALID_PARAMETER;
  2052. goto out;
  2053. }
  2054. /* Find end of device path */
  2055. len = efi_dp_instance_size(*device_path);
  2056. /* Get all handles implementing the protocol */
  2057. ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
  2058. &no_handles, &handles));
  2059. if (ret != EFI_SUCCESS)
  2060. goto out;
  2061. for (i = 0; i < no_handles; ++i) {
  2062. /* Find the device path protocol */
  2063. ret = efi_search_protocol(handles[i], &efi_guid_device_path,
  2064. &handler);
  2065. if (ret != EFI_SUCCESS)
  2066. continue;
  2067. dp = (struct efi_device_path *)handler->protocol_interface;
  2068. len_dp = efi_dp_instance_size(dp);
  2069. /*
  2070. * This handle can only be a better fit
  2071. * if its device path length is longer than the best fit and
  2072. * if its device path length is shorter of equal the searched
  2073. * device path.
  2074. */
  2075. if (len_dp <= len_best || len_dp > len)
  2076. continue;
  2077. /* Check if dp is a subpath of device_path */
  2078. if (memcmp(*device_path, dp, len_dp))
  2079. continue;
  2080. *device = handles[i];
  2081. len_best = len_dp;
  2082. }
  2083. if (len_best) {
  2084. remainder = (u8 *)*device_path + len_best;
  2085. *device_path = (struct efi_device_path *)remainder;
  2086. ret = EFI_SUCCESS;
  2087. } else {
  2088. ret = EFI_NOT_FOUND;
  2089. }
  2090. out:
  2091. return EFI_EXIT(ret);
  2092. }
  2093. /**
  2094. * efi_install_multiple_protocol_interfaces() - Install multiple protocol
  2095. * interfaces
  2096. * @handle: handle on which the protocol interfaces shall be installed
  2097. * @...: NULL terminated argument list with pairs of protocol GUIDS and
  2098. * interfaces
  2099. *
  2100. * This function implements the MultipleProtocolInterfaces service.
  2101. *
  2102. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2103. * details.
  2104. *
  2105. * Return: status code
  2106. */
  2107. static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
  2108. void **handle, ...)
  2109. {
  2110. EFI_ENTRY("%p", handle);
  2111. efi_va_list argptr;
  2112. const efi_guid_t *protocol;
  2113. void *protocol_interface;
  2114. efi_status_t r = EFI_SUCCESS;
  2115. int i = 0;
  2116. if (!handle)
  2117. return EFI_EXIT(EFI_INVALID_PARAMETER);
  2118. efi_va_start(argptr, handle);
  2119. for (;;) {
  2120. protocol = efi_va_arg(argptr, efi_guid_t*);
  2121. if (!protocol)
  2122. break;
  2123. protocol_interface = efi_va_arg(argptr, void*);
  2124. r = EFI_CALL(efi_install_protocol_interface(
  2125. handle, protocol,
  2126. EFI_NATIVE_INTERFACE,
  2127. protocol_interface));
  2128. if (r != EFI_SUCCESS)
  2129. break;
  2130. i++;
  2131. }
  2132. efi_va_end(argptr);
  2133. if (r == EFI_SUCCESS)
  2134. return EFI_EXIT(r);
  2135. /* If an error occurred undo all changes. */
  2136. efi_va_start(argptr, handle);
  2137. for (; i; --i) {
  2138. protocol = efi_va_arg(argptr, efi_guid_t*);
  2139. protocol_interface = efi_va_arg(argptr, void*);
  2140. EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
  2141. protocol_interface));
  2142. }
  2143. efi_va_end(argptr);
  2144. return EFI_EXIT(r);
  2145. }
  2146. /**
  2147. * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
  2148. * interfaces
  2149. * @handle: handle from which the protocol interfaces shall be removed
  2150. * @...: NULL terminated argument list with pairs of protocol GUIDS and
  2151. * interfaces
  2152. *
  2153. * This function implements the UninstallMultipleProtocolInterfaces service.
  2154. *
  2155. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2156. * details.
  2157. *
  2158. * Return: status code
  2159. */
  2160. static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
  2161. void *handle, ...)
  2162. {
  2163. EFI_ENTRY("%p", handle);
  2164. efi_va_list argptr;
  2165. const efi_guid_t *protocol;
  2166. void *protocol_interface;
  2167. efi_status_t r = EFI_SUCCESS;
  2168. size_t i = 0;
  2169. if (!handle)
  2170. return EFI_EXIT(EFI_INVALID_PARAMETER);
  2171. efi_va_start(argptr, handle);
  2172. for (;;) {
  2173. protocol = efi_va_arg(argptr, efi_guid_t*);
  2174. if (!protocol)
  2175. break;
  2176. protocol_interface = efi_va_arg(argptr, void*);
  2177. r = EFI_CALL(efi_uninstall_protocol_interface(
  2178. handle, protocol,
  2179. protocol_interface));
  2180. if (r != EFI_SUCCESS)
  2181. break;
  2182. i++;
  2183. }
  2184. efi_va_end(argptr);
  2185. if (r == EFI_SUCCESS)
  2186. return EFI_EXIT(r);
  2187. /* If an error occurred undo all changes. */
  2188. efi_va_start(argptr, handle);
  2189. for (; i; --i) {
  2190. protocol = efi_va_arg(argptr, efi_guid_t*);
  2191. protocol_interface = efi_va_arg(argptr, void*);
  2192. EFI_CALL(efi_install_protocol_interface(&handle, protocol,
  2193. EFI_NATIVE_INTERFACE,
  2194. protocol_interface));
  2195. }
  2196. efi_va_end(argptr);
  2197. return EFI_EXIT(r);
  2198. }
  2199. /**
  2200. * efi_calculate_crc32() - calculate cyclic redundancy code
  2201. * @data: buffer with data
  2202. * @data_size: size of buffer in bytes
  2203. * @crc32_p: cyclic redundancy code
  2204. *
  2205. * This function implements the CalculateCrc32 service.
  2206. *
  2207. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2208. * details.
  2209. *
  2210. * Return: status code
  2211. */
  2212. static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
  2213. efi_uintn_t data_size,
  2214. u32 *crc32_p)
  2215. {
  2216. EFI_ENTRY("%p, %zu", data, data_size);
  2217. *crc32_p = crc32(0, data, data_size);
  2218. return EFI_EXIT(EFI_SUCCESS);
  2219. }
  2220. /**
  2221. * efi_copy_mem() - copy memory
  2222. * @destination: destination of the copy operation
  2223. * @source: source of the copy operation
  2224. * @length: number of bytes to copy
  2225. *
  2226. * This function implements the CopyMem service.
  2227. *
  2228. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2229. * details.
  2230. */
  2231. static void EFIAPI efi_copy_mem(void *destination, const void *source,
  2232. size_t length)
  2233. {
  2234. EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
  2235. memcpy(destination, source, length);
  2236. EFI_EXIT(EFI_SUCCESS);
  2237. }
  2238. /**
  2239. * efi_set_mem() - Fill memory with a byte value.
  2240. * @buffer: buffer to fill
  2241. * @size: size of buffer in bytes
  2242. * @value: byte to copy to the buffer
  2243. *
  2244. * This function implements the SetMem service.
  2245. *
  2246. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2247. * details.
  2248. */
  2249. static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
  2250. {
  2251. EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
  2252. memset(buffer, value, size);
  2253. EFI_EXIT(EFI_SUCCESS);
  2254. }
  2255. /**
  2256. * efi_protocol_open() - open protocol interface on a handle
  2257. * @handler: handler of a protocol
  2258. * @protocol_interface: interface implementing the protocol
  2259. * @agent_handle: handle of the driver
  2260. * @controller_handle: handle of the controller
  2261. * @attributes: attributes indicating how to open the protocol
  2262. *
  2263. * Return: status code
  2264. */
  2265. static efi_status_t efi_protocol_open(
  2266. struct efi_handler *handler,
  2267. void **protocol_interface, void *agent_handle,
  2268. void *controller_handle, uint32_t attributes)
  2269. {
  2270. struct efi_open_protocol_info_item *item;
  2271. struct efi_open_protocol_info_entry *match = NULL;
  2272. bool opened_by_driver = false;
  2273. bool opened_exclusive = false;
  2274. /* If there is no agent, only return the interface */
  2275. if (!agent_handle)
  2276. goto out;
  2277. /* For TEST_PROTOCOL ignore interface attribute */
  2278. if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
  2279. *protocol_interface = NULL;
  2280. /*
  2281. * Check if the protocol is already opened by a driver with the same
  2282. * attributes or opened exclusively
  2283. */
  2284. list_for_each_entry(item, &handler->open_infos, link) {
  2285. if (item->info.agent_handle == agent_handle) {
  2286. if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
  2287. (item->info.attributes == attributes))
  2288. return EFI_ALREADY_STARTED;
  2289. }
  2290. if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
  2291. opened_exclusive = true;
  2292. }
  2293. /* Only one controller can open the protocol exclusively */
  2294. if (opened_exclusive && attributes &
  2295. (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
  2296. return EFI_ACCESS_DENIED;
  2297. /* Prepare exclusive opening */
  2298. if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
  2299. /* Try to disconnect controllers */
  2300. list_for_each_entry(item, &handler->open_infos, link) {
  2301. if (item->info.attributes ==
  2302. EFI_OPEN_PROTOCOL_BY_DRIVER)
  2303. EFI_CALL(efi_disconnect_controller(
  2304. item->info.controller_handle,
  2305. item->info.agent_handle,
  2306. NULL));
  2307. }
  2308. opened_by_driver = false;
  2309. /* Check if all controllers are disconnected */
  2310. list_for_each_entry(item, &handler->open_infos, link) {
  2311. if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
  2312. opened_by_driver = true;
  2313. }
  2314. /* Only one controller can be conncected */
  2315. if (opened_by_driver)
  2316. return EFI_ACCESS_DENIED;
  2317. }
  2318. /* Find existing entry */
  2319. list_for_each_entry(item, &handler->open_infos, link) {
  2320. if (item->info.agent_handle == agent_handle &&
  2321. item->info.controller_handle == controller_handle)
  2322. match = &item->info;
  2323. }
  2324. /* None found, create one */
  2325. if (!match) {
  2326. match = efi_create_open_info(handler);
  2327. if (!match)
  2328. return EFI_OUT_OF_RESOURCES;
  2329. }
  2330. match->agent_handle = agent_handle;
  2331. match->controller_handle = controller_handle;
  2332. match->attributes = attributes;
  2333. match->open_count++;
  2334. out:
  2335. /* For TEST_PROTOCOL ignore interface attribute. */
  2336. if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
  2337. *protocol_interface = handler->protocol_interface;
  2338. return EFI_SUCCESS;
  2339. }
  2340. /**
  2341. * efi_open_protocol() - open protocol interface on a handle
  2342. * @handle: handle on which the protocol shall be opened
  2343. * @protocol: GUID of the protocol
  2344. * @protocol_interface: interface implementing the protocol
  2345. * @agent_handle: handle of the driver
  2346. * @controller_handle: handle of the controller
  2347. * @attributes: attributes indicating how to open the protocol
  2348. *
  2349. * This function implements the OpenProtocol interface.
  2350. *
  2351. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2352. * details.
  2353. *
  2354. * Return: status code
  2355. */
  2356. static efi_status_t EFIAPI efi_open_protocol(
  2357. void *handle, const efi_guid_t *protocol,
  2358. void **protocol_interface, void *agent_handle,
  2359. void *controller_handle, uint32_t attributes)
  2360. {
  2361. struct efi_handler *handler;
  2362. efi_status_t r = EFI_INVALID_PARAMETER;
  2363. EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
  2364. protocol_interface, agent_handle, controller_handle,
  2365. attributes);
  2366. if (!handle || !protocol ||
  2367. (!protocol_interface && attributes !=
  2368. EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
  2369. goto out;
  2370. }
  2371. switch (attributes) {
  2372. case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
  2373. case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
  2374. case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
  2375. break;
  2376. case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
  2377. if (controller_handle == handle)
  2378. goto out;
  2379. /* fall-through */
  2380. case EFI_OPEN_PROTOCOL_BY_DRIVER:
  2381. case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
  2382. /* Check that the controller handle is valid */
  2383. if (!efi_search_obj(controller_handle))
  2384. goto out;
  2385. /* fall-through */
  2386. case EFI_OPEN_PROTOCOL_EXCLUSIVE:
  2387. /* Check that the agent handle is valid */
  2388. if (!efi_search_obj(agent_handle))
  2389. goto out;
  2390. break;
  2391. default:
  2392. goto out;
  2393. }
  2394. r = efi_search_protocol(handle, protocol, &handler);
  2395. if (r != EFI_SUCCESS)
  2396. goto out;
  2397. r = efi_protocol_open(handler, protocol_interface, agent_handle,
  2398. controller_handle, attributes);
  2399. out:
  2400. return EFI_EXIT(r);
  2401. }
  2402. /**
  2403. * efi_handle_protocol() - get interface of a protocol on a handle
  2404. * @handle: handle on which the protocol shall be opened
  2405. * @protocol: GUID of the protocol
  2406. * @protocol_interface: interface implementing the protocol
  2407. *
  2408. * This function implements the HandleProtocol service.
  2409. *
  2410. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2411. * details.
  2412. *
  2413. * Return: status code
  2414. */
  2415. static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
  2416. const efi_guid_t *protocol,
  2417. void **protocol_interface)
  2418. {
  2419. return efi_open_protocol(handle, protocol, protocol_interface, NULL,
  2420. NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
  2421. }
  2422. /**
  2423. * efi_bind_controller() - bind a single driver to a controller
  2424. * @controller_handle: controller handle
  2425. * @driver_image_handle: driver handle
  2426. * @remain_device_path: remaining path
  2427. *
  2428. * Return: status code
  2429. */
  2430. static efi_status_t efi_bind_controller(
  2431. efi_handle_t controller_handle,
  2432. efi_handle_t driver_image_handle,
  2433. struct efi_device_path *remain_device_path)
  2434. {
  2435. struct efi_driver_binding_protocol *binding_protocol;
  2436. efi_status_t r;
  2437. r = EFI_CALL(efi_open_protocol(driver_image_handle,
  2438. &efi_guid_driver_binding_protocol,
  2439. (void **)&binding_protocol,
  2440. driver_image_handle, NULL,
  2441. EFI_OPEN_PROTOCOL_GET_PROTOCOL));
  2442. if (r != EFI_SUCCESS)
  2443. return r;
  2444. r = EFI_CALL(binding_protocol->supported(binding_protocol,
  2445. controller_handle,
  2446. remain_device_path));
  2447. if (r == EFI_SUCCESS)
  2448. r = EFI_CALL(binding_protocol->start(binding_protocol,
  2449. controller_handle,
  2450. remain_device_path));
  2451. EFI_CALL(efi_close_protocol(driver_image_handle,
  2452. &efi_guid_driver_binding_protocol,
  2453. driver_image_handle, NULL));
  2454. return r;
  2455. }
  2456. /**
  2457. * efi_connect_single_controller() - connect a single driver to a controller
  2458. * @controller_handle: controller
  2459. * @driver_image_handle: driver
  2460. * @remain_device_path: remainting path
  2461. *
  2462. * Return: status code
  2463. */
  2464. static efi_status_t efi_connect_single_controller(
  2465. efi_handle_t controller_handle,
  2466. efi_handle_t *driver_image_handle,
  2467. struct efi_device_path *remain_device_path)
  2468. {
  2469. efi_handle_t *buffer;
  2470. size_t count;
  2471. size_t i;
  2472. efi_status_t r;
  2473. size_t connected = 0;
  2474. /* Get buffer with all handles with driver binding protocol */
  2475. r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
  2476. &efi_guid_driver_binding_protocol,
  2477. NULL, &count, &buffer));
  2478. if (r != EFI_SUCCESS)
  2479. return r;
  2480. /* Context Override */
  2481. if (driver_image_handle) {
  2482. for (; *driver_image_handle; ++driver_image_handle) {
  2483. for (i = 0; i < count; ++i) {
  2484. if (buffer[i] == *driver_image_handle) {
  2485. buffer[i] = NULL;
  2486. r = efi_bind_controller(
  2487. controller_handle,
  2488. *driver_image_handle,
  2489. remain_device_path);
  2490. /*
  2491. * For drivers that do not support the
  2492. * controller or are already connected
  2493. * we receive an error code here.
  2494. */
  2495. if (r == EFI_SUCCESS)
  2496. ++connected;
  2497. }
  2498. }
  2499. }
  2500. }
  2501. /*
  2502. * TODO: Some overrides are not yet implemented:
  2503. * - Platform Driver Override
  2504. * - Driver Family Override Search
  2505. * - Bus Specific Driver Override
  2506. */
  2507. /* Driver Binding Search */
  2508. for (i = 0; i < count; ++i) {
  2509. if (buffer[i]) {
  2510. r = efi_bind_controller(controller_handle,
  2511. buffer[i],
  2512. remain_device_path);
  2513. if (r == EFI_SUCCESS)
  2514. ++connected;
  2515. }
  2516. }
  2517. efi_free_pool(buffer);
  2518. if (!connected)
  2519. return EFI_NOT_FOUND;
  2520. return EFI_SUCCESS;
  2521. }
  2522. /**
  2523. * efi_connect_controller() - connect a controller to a driver
  2524. * @controller_handle: handle of the controller
  2525. * @driver_image_handle: handle of the driver
  2526. * @remain_device_path: device path of a child controller
  2527. * @recursive: true to connect all child controllers
  2528. *
  2529. * This function implements the ConnectController service.
  2530. *
  2531. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2532. * details.
  2533. *
  2534. * First all driver binding protocol handles are tried for binding drivers.
  2535. * Afterwards all handles that have openened a protocol of the controller
  2536. * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
  2537. *
  2538. * Return: status code
  2539. */
  2540. static efi_status_t EFIAPI efi_connect_controller(
  2541. efi_handle_t controller_handle,
  2542. efi_handle_t *driver_image_handle,
  2543. struct efi_device_path *remain_device_path,
  2544. bool recursive)
  2545. {
  2546. efi_status_t r;
  2547. efi_status_t ret = EFI_NOT_FOUND;
  2548. struct efi_object *efiobj;
  2549. EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
  2550. remain_device_path, recursive);
  2551. efiobj = efi_search_obj(controller_handle);
  2552. if (!efiobj) {
  2553. ret = EFI_INVALID_PARAMETER;
  2554. goto out;
  2555. }
  2556. r = efi_connect_single_controller(controller_handle,
  2557. driver_image_handle,
  2558. remain_device_path);
  2559. if (r == EFI_SUCCESS)
  2560. ret = EFI_SUCCESS;
  2561. if (recursive) {
  2562. struct efi_handler *handler;
  2563. struct efi_open_protocol_info_item *item;
  2564. list_for_each_entry(handler, &efiobj->protocols, link) {
  2565. list_for_each_entry(item, &handler->open_infos, link) {
  2566. if (item->info.attributes &
  2567. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
  2568. r = EFI_CALL(efi_connect_controller(
  2569. item->info.controller_handle,
  2570. driver_image_handle,
  2571. remain_device_path,
  2572. recursive));
  2573. if (r == EFI_SUCCESS)
  2574. ret = EFI_SUCCESS;
  2575. }
  2576. }
  2577. }
  2578. }
  2579. /* Check for child controller specified by end node */
  2580. if (ret != EFI_SUCCESS && remain_device_path &&
  2581. remain_device_path->type == DEVICE_PATH_TYPE_END)
  2582. ret = EFI_SUCCESS;
  2583. out:
  2584. return EFI_EXIT(ret);
  2585. }
  2586. /**
  2587. * efi_reinstall_protocol_interface() - reinstall protocol interface
  2588. * @handle: handle on which the protocol shall be reinstalled
  2589. * @protocol: GUID of the protocol to be installed
  2590. * @old_interface: interface to be removed
  2591. * @new_interface: interface to be installed
  2592. *
  2593. * This function implements the ReinstallProtocolInterface service.
  2594. *
  2595. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2596. * details.
  2597. *
  2598. * The old interface is uninstalled. The new interface is installed.
  2599. * Drivers are connected.
  2600. *
  2601. * Return: status code
  2602. */
  2603. static efi_status_t EFIAPI efi_reinstall_protocol_interface(
  2604. efi_handle_t handle, const efi_guid_t *protocol,
  2605. void *old_interface, void *new_interface)
  2606. {
  2607. efi_status_t ret;
  2608. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
  2609. new_interface);
  2610. ret = EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
  2611. old_interface));
  2612. if (ret != EFI_SUCCESS)
  2613. goto out;
  2614. ret = EFI_CALL(efi_install_protocol_interface(&handle, protocol,
  2615. EFI_NATIVE_INTERFACE,
  2616. new_interface));
  2617. if (ret != EFI_SUCCESS)
  2618. goto out;
  2619. /*
  2620. * The returned status code has to be ignored.
  2621. * Do not create an error if no suitable driver for the handle exists.
  2622. */
  2623. EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
  2624. out:
  2625. return EFI_EXIT(ret);
  2626. }
  2627. /**
  2628. * efi_get_child_controllers() - get all child controllers associated to a driver
  2629. * @efiobj: handle of the controller
  2630. * @driver_handle: handle of the driver
  2631. * @number_of_children: number of child controllers
  2632. * @child_handle_buffer: handles of the the child controllers
  2633. *
  2634. * The allocated buffer has to be freed with free().
  2635. *
  2636. * Return: status code
  2637. */
  2638. static efi_status_t efi_get_child_controllers(
  2639. struct efi_object *efiobj,
  2640. efi_handle_t driver_handle,
  2641. efi_uintn_t *number_of_children,
  2642. efi_handle_t **child_handle_buffer)
  2643. {
  2644. struct efi_handler *handler;
  2645. struct efi_open_protocol_info_item *item;
  2646. efi_uintn_t count = 0, i;
  2647. bool duplicate;
  2648. /* Count all child controller associations */
  2649. list_for_each_entry(handler, &efiobj->protocols, link) {
  2650. list_for_each_entry(item, &handler->open_infos, link) {
  2651. if (item->info.agent_handle == driver_handle &&
  2652. item->info.attributes &
  2653. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
  2654. ++count;
  2655. }
  2656. }
  2657. /*
  2658. * Create buffer. In case of duplicate child controller assignments
  2659. * the buffer will be too large. But that does not harm.
  2660. */
  2661. *number_of_children = 0;
  2662. *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
  2663. if (!*child_handle_buffer)
  2664. return EFI_OUT_OF_RESOURCES;
  2665. /* Copy unique child handles */
  2666. list_for_each_entry(handler, &efiobj->protocols, link) {
  2667. list_for_each_entry(item, &handler->open_infos, link) {
  2668. if (item->info.agent_handle == driver_handle &&
  2669. item->info.attributes &
  2670. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
  2671. /* Check this is a new child controller */
  2672. duplicate = false;
  2673. for (i = 0; i < *number_of_children; ++i) {
  2674. if ((*child_handle_buffer)[i] ==
  2675. item->info.controller_handle)
  2676. duplicate = true;
  2677. }
  2678. /* Copy handle to buffer */
  2679. if (!duplicate) {
  2680. i = (*number_of_children)++;
  2681. (*child_handle_buffer)[i] =
  2682. item->info.controller_handle;
  2683. }
  2684. }
  2685. }
  2686. }
  2687. return EFI_SUCCESS;
  2688. }
  2689. /**
  2690. * efi_disconnect_controller() - disconnect a controller from a driver
  2691. * @controller_handle: handle of the controller
  2692. * @driver_image_handle: handle of the driver
  2693. * @child_handle: handle of the child to destroy
  2694. *
  2695. * This function implements the DisconnectController service.
  2696. *
  2697. * See the Unified Extensible Firmware Interface (UEFI) specification for
  2698. * details.
  2699. *
  2700. * Return: status code
  2701. */
  2702. static efi_status_t EFIAPI efi_disconnect_controller(
  2703. efi_handle_t controller_handle,
  2704. efi_handle_t driver_image_handle,
  2705. efi_handle_t child_handle)
  2706. {
  2707. struct efi_driver_binding_protocol *binding_protocol;
  2708. efi_handle_t *child_handle_buffer = NULL;
  2709. size_t number_of_children = 0;
  2710. efi_status_t r;
  2711. size_t stop_count = 0;
  2712. struct efi_object *efiobj;
  2713. EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
  2714. child_handle);
  2715. efiobj = efi_search_obj(controller_handle);
  2716. if (!efiobj) {
  2717. r = EFI_INVALID_PARAMETER;
  2718. goto out;
  2719. }
  2720. if (child_handle && !efi_search_obj(child_handle)) {
  2721. r = EFI_INVALID_PARAMETER;
  2722. goto out;
  2723. }
  2724. /* If no driver handle is supplied, disconnect all drivers */
  2725. if (!driver_image_handle) {
  2726. r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
  2727. goto out;
  2728. }
  2729. /* Create list of child handles */
  2730. if (child_handle) {
  2731. number_of_children = 1;
  2732. child_handle_buffer = &child_handle;
  2733. } else {
  2734. efi_get_child_controllers(efiobj,
  2735. driver_image_handle,
  2736. &number_of_children,
  2737. &child_handle_buffer);
  2738. }
  2739. /* Get the driver binding protocol */
  2740. r = EFI_CALL(efi_open_protocol(driver_image_handle,
  2741. &efi_guid_driver_binding_protocol,
  2742. (void **)&binding_protocol,
  2743. driver_image_handle, NULL,
  2744. EFI_OPEN_PROTOCOL_GET_PROTOCOL));
  2745. if (r != EFI_SUCCESS)
  2746. goto out;
  2747. /* Remove the children */
  2748. if (number_of_children) {
  2749. r = EFI_CALL(binding_protocol->stop(binding_protocol,
  2750. controller_handle,
  2751. number_of_children,
  2752. child_handle_buffer));
  2753. if (r == EFI_SUCCESS)
  2754. ++stop_count;
  2755. }
  2756. /* Remove the driver */
  2757. if (!child_handle)
  2758. r = EFI_CALL(binding_protocol->stop(binding_protocol,
  2759. controller_handle,
  2760. 0, NULL));
  2761. if (r == EFI_SUCCESS)
  2762. ++stop_count;
  2763. EFI_CALL(efi_close_protocol(driver_image_handle,
  2764. &efi_guid_driver_binding_protocol,
  2765. driver_image_handle, NULL));
  2766. if (stop_count)
  2767. r = EFI_SUCCESS;
  2768. else
  2769. r = EFI_NOT_FOUND;
  2770. out:
  2771. if (!child_handle)
  2772. free(child_handle_buffer);
  2773. return EFI_EXIT(r);
  2774. }
  2775. static struct efi_boot_services efi_boot_services = {
  2776. .hdr = {
  2777. .signature = EFI_BOOT_SERVICES_SIGNATURE,
  2778. .revision = EFI_SPECIFICATION_VERSION,
  2779. .headersize = sizeof(struct efi_boot_services),
  2780. },
  2781. .raise_tpl = efi_raise_tpl,
  2782. .restore_tpl = efi_restore_tpl,
  2783. .allocate_pages = efi_allocate_pages_ext,
  2784. .free_pages = efi_free_pages_ext,
  2785. .get_memory_map = efi_get_memory_map_ext,
  2786. .allocate_pool = efi_allocate_pool_ext,
  2787. .free_pool = efi_free_pool_ext,
  2788. .create_event = efi_create_event_ext,
  2789. .set_timer = efi_set_timer_ext,
  2790. .wait_for_event = efi_wait_for_event,
  2791. .signal_event = efi_signal_event_ext,
  2792. .close_event = efi_close_event,
  2793. .check_event = efi_check_event,
  2794. .install_protocol_interface = efi_install_protocol_interface,
  2795. .reinstall_protocol_interface = efi_reinstall_protocol_interface,
  2796. .uninstall_protocol_interface = efi_uninstall_protocol_interface,
  2797. .handle_protocol = efi_handle_protocol,
  2798. .reserved = NULL,
  2799. .register_protocol_notify = efi_register_protocol_notify,
  2800. .locate_handle = efi_locate_handle_ext,
  2801. .locate_device_path = efi_locate_device_path,
  2802. .install_configuration_table = efi_install_configuration_table_ext,
  2803. .load_image = efi_load_image,
  2804. .start_image = efi_start_image,
  2805. .exit = efi_exit,
  2806. .unload_image = efi_unload_image,
  2807. .exit_boot_services = efi_exit_boot_services,
  2808. .get_next_monotonic_count = efi_get_next_monotonic_count,
  2809. .stall = efi_stall,
  2810. .set_watchdog_timer = efi_set_watchdog_timer,
  2811. .connect_controller = efi_connect_controller,
  2812. .disconnect_controller = efi_disconnect_controller,
  2813. .open_protocol = efi_open_protocol,
  2814. .close_protocol = efi_close_protocol,
  2815. .open_protocol_information = efi_open_protocol_information,
  2816. .protocols_per_handle = efi_protocols_per_handle,
  2817. .locate_handle_buffer = efi_locate_handle_buffer,
  2818. .locate_protocol = efi_locate_protocol,
  2819. .install_multiple_protocol_interfaces =
  2820. efi_install_multiple_protocol_interfaces,
  2821. .uninstall_multiple_protocol_interfaces =
  2822. efi_uninstall_multiple_protocol_interfaces,
  2823. .calculate_crc32 = efi_calculate_crc32,
  2824. .copy_mem = efi_copy_mem,
  2825. .set_mem = efi_set_mem,
  2826. .create_event_ex = efi_create_event_ex,
  2827. };
  2828. static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
  2829. struct efi_system_table __efi_runtime_data systab = {
  2830. .hdr = {
  2831. .signature = EFI_SYSTEM_TABLE_SIGNATURE,
  2832. .revision = EFI_SPECIFICATION_VERSION,
  2833. .headersize = sizeof(struct efi_system_table),
  2834. },
  2835. .fw_vendor = firmware_vendor,
  2836. .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
  2837. .con_in = (void *)&efi_con_in,
  2838. .con_out = (void *)&efi_con_out,
  2839. .std_err = (void *)&efi_con_out,
  2840. .runtime = (void *)&efi_runtime_services,
  2841. .boottime = (void *)&efi_boot_services,
  2842. .nr_tables = 0,
  2843. .tables = NULL,
  2844. };
  2845. /**
  2846. * efi_initialize_system_table() - Initialize system table
  2847. *
  2848. * Return Value: status code
  2849. */
  2850. efi_status_t efi_initialize_system_table(void)
  2851. {
  2852. efi_status_t ret;
  2853. /* Allocate configuration table array */
  2854. ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
  2855. EFI_MAX_CONFIGURATION_TABLES *
  2856. sizeof(struct efi_configuration_table),
  2857. (void **)&systab.tables);
  2858. /* Set crc32 field in table headers */
  2859. efi_update_table_header_crc32(&systab.hdr);
  2860. efi_update_table_header_crc32(&efi_runtime_services.hdr);
  2861. efi_update_table_header_crc32(&efi_boot_services.hdr);
  2862. return ret;
  2863. }