efi_boottime.c 98 KB

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