efi_boottime.c 98 KB

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