efi_boottime.c 83 KB

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