efi_boottime.c 83 KB

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