efi_boottime.c 75 KB

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