efi_boottime.c 84 KB

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