efi_boottime.c 79 KB

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