efi_boottime.c 85 KB

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