efi_boottime.c 79 KB

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