efi_boottime.c 98 KB

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