efi_boottime.c 109 KB

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