efi_boottime.c 76 KB

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