efi_boottime.c 103 KB

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