efi_boottime.c 79 KB

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