efi_boottime.c 85 KB

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