efi_boottime.c 78 KB

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