efi_boottime.c 84 KB

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