efi_boottime.c 74 KB

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