efi_boottime.c 98 KB

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