efi_boottime.c 83 KB

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