efi_boottime.c 99 KB

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