efi_boottime.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI application boot time services
  4. *
  5. * Copyright (c) 2016 Alexander Graf
  6. */
  7. #include <common.h>
  8. #include <div64.h>
  9. #include <efi_loader.h>
  10. #include <environment.h>
  11. #include <malloc.h>
  12. #include <asm/global_data.h>
  13. #include <linux/libfdt_env.h>
  14. #include <u-boot/crc.h>
  15. #include <bootm.h>
  16. #include <inttypes.h>
  17. #include <watchdog.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. /* Task priority level */
  20. static efi_uintn_t efi_tpl = TPL_APPLICATION;
  21. /* This list contains all the EFI objects our payload has access to */
  22. LIST_HEAD(efi_obj_list);
  23. /* List of all events */
  24. LIST_HEAD(efi_events);
  25. /*
  26. * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
  27. * we need to do trickery with caches. Since we don't want to break the EFI
  28. * aware boot path, only apply hacks when loading exiting directly (breaking
  29. * direct Linux EFI booting along the way - oh well).
  30. */
  31. static bool efi_is_direct_boot = true;
  32. /*
  33. * EFI can pass arbitrary additional "tables" containing vendor specific
  34. * information to the payload. One such table is the FDT table which contains
  35. * a pointer to a flattened device tree blob.
  36. *
  37. * In most cases we want to pass an FDT to the payload, so reserve one slot of
  38. * config table space for it. The pointer gets populated by do_bootefi_exec().
  39. */
  40. static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
  41. #ifdef CONFIG_ARM
  42. /*
  43. * The "gd" pointer lives in a register on ARM and AArch64 that we declare
  44. * fixed when compiling U-Boot. However, the payload does not know about that
  45. * restriction so we need to manually swap its and our view of that register on
  46. * EFI callback entry/exit.
  47. */
  48. static volatile void *efi_gd, *app_gd;
  49. #endif
  50. static int entry_count;
  51. static int nesting_level;
  52. /* GUID of the device tree table */
  53. const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
  54. /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
  55. const efi_guid_t efi_guid_driver_binding_protocol =
  56. EFI_DRIVER_BINDING_PROTOCOL_GUID;
  57. /* event group ExitBootServices() invoked */
  58. const efi_guid_t efi_guid_event_group_exit_boot_services =
  59. EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
  60. /* event group SetVirtualAddressMap() invoked */
  61. const efi_guid_t efi_guid_event_group_virtual_address_change =
  62. EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
  63. /* event group memory map changed */
  64. const efi_guid_t efi_guid_event_group_memory_map_change =
  65. EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
  66. /* event group boot manager about to boot */
  67. const efi_guid_t efi_guid_event_group_ready_to_boot =
  68. EFI_EVENT_GROUP_READY_TO_BOOT;
  69. /* event group ResetSystem() invoked (before ExitBootServices) */
  70. const efi_guid_t efi_guid_event_group_reset_system =
  71. EFI_EVENT_GROUP_RESET_SYSTEM;
  72. static efi_status_t EFIAPI efi_disconnect_controller(
  73. efi_handle_t controller_handle,
  74. efi_handle_t driver_image_handle,
  75. efi_handle_t child_handle);
  76. /* Called on every callback entry */
  77. int __efi_entry_check(void)
  78. {
  79. int ret = entry_count++ == 0;
  80. #ifdef CONFIG_ARM
  81. assert(efi_gd);
  82. app_gd = gd;
  83. gd = efi_gd;
  84. #endif
  85. return ret;
  86. }
  87. /* Called on every callback exit */
  88. int __efi_exit_check(void)
  89. {
  90. int ret = --entry_count == 0;
  91. #ifdef CONFIG_ARM
  92. gd = app_gd;
  93. #endif
  94. return ret;
  95. }
  96. /* Called from do_bootefi_exec() */
  97. void efi_save_gd(void)
  98. {
  99. #ifdef CONFIG_ARM
  100. efi_gd = gd;
  101. #endif
  102. }
  103. /*
  104. * Special case handler for error/abort that just forces things back
  105. * to u-boot world so we can dump out an abort msg, without any care
  106. * about returning back to UEFI world.
  107. */
  108. void efi_restore_gd(void)
  109. {
  110. #ifdef CONFIG_ARM
  111. /* Only restore if we're already in EFI context */
  112. if (!efi_gd)
  113. return;
  114. gd = efi_gd;
  115. #endif
  116. }
  117. /*
  118. * Return a string for indenting with two spaces per level. A maximum of ten
  119. * indent levels is supported. Higher indent levels will be truncated.
  120. *
  121. * @level indent level
  122. * @return indent string
  123. */
  124. static const char *indent_string(int level)
  125. {
  126. const char *indent = " ";
  127. const int max = strlen(indent);
  128. level = min(max, level * 2);
  129. return &indent[max - level];
  130. }
  131. const char *__efi_nesting(void)
  132. {
  133. return indent_string(nesting_level);
  134. }
  135. const char *__efi_nesting_inc(void)
  136. {
  137. return indent_string(nesting_level++);
  138. }
  139. const char *__efi_nesting_dec(void)
  140. {
  141. return indent_string(--nesting_level);
  142. }
  143. /*
  144. * Queue an EFI event.
  145. *
  146. * This function queues the notification function of the event for future
  147. * execution.
  148. *
  149. * The notification function is called if the task priority level of the
  150. * event is higher than the current task priority level.
  151. *
  152. * For the SignalEvent service see efi_signal_event_ext.
  153. *
  154. * @event event to signal
  155. * @check_tpl check the TPL level
  156. */
  157. static void efi_queue_event(struct efi_event *event, bool check_tpl)
  158. {
  159. if (event->notify_function) {
  160. event->is_queued = true;
  161. /* Check TPL */
  162. if (check_tpl && efi_tpl >= event->notify_tpl)
  163. return;
  164. EFI_CALL_VOID(event->notify_function(event,
  165. event->notify_context));
  166. }
  167. event->is_queued = false;
  168. }
  169. /*
  170. * Signal an EFI event.
  171. *
  172. * This function signals an event. If the event belongs to an event group
  173. * all events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
  174. * their notification function is queued.
  175. *
  176. * For the SignalEvent service see efi_signal_event_ext.
  177. *
  178. * @event event to signal
  179. * @check_tpl check the TPL level
  180. */
  181. void efi_signal_event(struct efi_event *event, bool check_tpl)
  182. {
  183. if (event->group) {
  184. struct efi_event *evt;
  185. /*
  186. * The signaled state has to set before executing any
  187. * notification function
  188. */
  189. list_for_each_entry(evt, &efi_events, link) {
  190. if (!evt->group || guidcmp(evt->group, event->group))
  191. continue;
  192. if (evt->is_signaled)
  193. continue;
  194. evt->is_signaled = true;
  195. if (evt->type & EVT_NOTIFY_SIGNAL &&
  196. evt->notify_function)
  197. evt->is_queued = true;
  198. }
  199. list_for_each_entry(evt, &efi_events, link) {
  200. if (!evt->group || guidcmp(evt->group, event->group))
  201. continue;
  202. if (evt->is_queued)
  203. efi_queue_event(evt, check_tpl);
  204. }
  205. } else if (!event->is_signaled) {
  206. event->is_signaled = true;
  207. if (event->type & EVT_NOTIFY_SIGNAL)
  208. efi_queue_event(event, check_tpl);
  209. }
  210. }
  211. /*
  212. * Raise the task priority level.
  213. *
  214. * This function implements the RaiseTpl service.
  215. * See the Unified Extensible Firmware Interface (UEFI) specification
  216. * for details.
  217. *
  218. * @new_tpl new value of the task priority level
  219. * @return old value of the task priority level
  220. */
  221. static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
  222. {
  223. efi_uintn_t old_tpl = efi_tpl;
  224. EFI_ENTRY("0x%zx", new_tpl);
  225. if (new_tpl < efi_tpl)
  226. debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
  227. efi_tpl = new_tpl;
  228. if (efi_tpl > TPL_HIGH_LEVEL)
  229. efi_tpl = TPL_HIGH_LEVEL;
  230. EFI_EXIT(EFI_SUCCESS);
  231. return old_tpl;
  232. }
  233. /*
  234. * Lower the task priority level.
  235. *
  236. * This function implements the RestoreTpl service.
  237. * See the Unified Extensible Firmware Interface (UEFI) specification
  238. * for details.
  239. *
  240. * @old_tpl value of the task priority level to be restored
  241. */
  242. static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
  243. {
  244. EFI_ENTRY("0x%zx", old_tpl);
  245. if (old_tpl > efi_tpl)
  246. debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
  247. efi_tpl = old_tpl;
  248. if (efi_tpl > TPL_HIGH_LEVEL)
  249. efi_tpl = TPL_HIGH_LEVEL;
  250. /*
  251. * Lowering the TPL may have made queued events eligible for execution.
  252. */
  253. efi_timer_check();
  254. EFI_EXIT(EFI_SUCCESS);
  255. }
  256. /*
  257. * Allocate memory pages.
  258. *
  259. * This function implements the AllocatePages service.
  260. * See the Unified Extensible Firmware Interface (UEFI) specification
  261. * for details.
  262. *
  263. * @type type of allocation to be performed
  264. * @memory_type usage type of the allocated memory
  265. * @pages number of pages to be allocated
  266. * @memory allocated memory
  267. * @return status code
  268. */
  269. static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
  270. efi_uintn_t pages,
  271. uint64_t *memory)
  272. {
  273. efi_status_t r;
  274. EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
  275. r = efi_allocate_pages(type, memory_type, pages, memory);
  276. return EFI_EXIT(r);
  277. }
  278. /*
  279. * Free memory pages.
  280. *
  281. * This function implements the FreePages service.
  282. * See the Unified Extensible Firmware Interface (UEFI) specification
  283. * for details.
  284. *
  285. * @memory start of the memory area to be freed
  286. * @pages number of pages to be freed
  287. * @return status code
  288. */
  289. static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
  290. efi_uintn_t pages)
  291. {
  292. efi_status_t r;
  293. EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
  294. r = efi_free_pages(memory, pages);
  295. return EFI_EXIT(r);
  296. }
  297. /*
  298. * Get map describing memory usage.
  299. *
  300. * This function implements the GetMemoryMap service.
  301. * See the Unified Extensible Firmware Interface (UEFI) specification
  302. * for details.
  303. *
  304. * @memory_map_size on entry the size, in bytes, of the memory map buffer,
  305. * on exit the size of the copied memory map
  306. * @memory_map buffer to which the memory map is written
  307. * @map_key key for the memory map
  308. * @descriptor_size size of an individual memory descriptor
  309. * @descriptor_version version number of the memory descriptor structure
  310. * @return status code
  311. */
  312. static efi_status_t EFIAPI efi_get_memory_map_ext(
  313. efi_uintn_t *memory_map_size,
  314. struct efi_mem_desc *memory_map,
  315. efi_uintn_t *map_key,
  316. efi_uintn_t *descriptor_size,
  317. uint32_t *descriptor_version)
  318. {
  319. efi_status_t r;
  320. EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
  321. map_key, descriptor_size, descriptor_version);
  322. r = efi_get_memory_map(memory_map_size, memory_map, map_key,
  323. descriptor_size, descriptor_version);
  324. return EFI_EXIT(r);
  325. }
  326. /*
  327. * Allocate memory from pool.
  328. *
  329. * This function implements the AllocatePool service.
  330. * See the Unified Extensible Firmware Interface (UEFI) specification
  331. * for details.
  332. *
  333. * @pool_type type of the pool from which memory is to be allocated
  334. * @size number of bytes to be allocated
  335. * @buffer allocated memory
  336. * @return status code
  337. */
  338. static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
  339. efi_uintn_t size,
  340. void **buffer)
  341. {
  342. efi_status_t r;
  343. EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
  344. r = efi_allocate_pool(pool_type, size, buffer);
  345. return EFI_EXIT(r);
  346. }
  347. /*
  348. * Free memory from pool.
  349. *
  350. * This function implements the FreePool service.
  351. * See the Unified Extensible Firmware Interface (UEFI) specification
  352. * for details.
  353. *
  354. * @buffer start of memory to be freed
  355. * @return status code
  356. */
  357. static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
  358. {
  359. efi_status_t r;
  360. EFI_ENTRY("%p", buffer);
  361. r = efi_free_pool(buffer);
  362. return EFI_EXIT(r);
  363. }
  364. /*
  365. * Add a new object to the object list.
  366. *
  367. * The protocols list is initialized.
  368. * The object handle is set.
  369. *
  370. * @obj object to be added
  371. */
  372. void efi_add_handle(struct efi_object *obj)
  373. {
  374. if (!obj)
  375. return;
  376. INIT_LIST_HEAD(&obj->protocols);
  377. obj->handle = obj;
  378. list_add_tail(&obj->link, &efi_obj_list);
  379. }
  380. /*
  381. * Create handle.
  382. *
  383. * @handle new handle
  384. * @return status code
  385. */
  386. efi_status_t efi_create_handle(efi_handle_t *handle)
  387. {
  388. struct efi_object *obj;
  389. efi_status_t r;
  390. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
  391. sizeof(struct efi_object),
  392. (void **)&obj);
  393. if (r != EFI_SUCCESS)
  394. return r;
  395. efi_add_handle(obj);
  396. *handle = obj->handle;
  397. return r;
  398. }
  399. /*
  400. * Find a protocol on a handle.
  401. *
  402. * @handle handle
  403. * @protocol_guid GUID of the protocol
  404. * @handler reference to the protocol
  405. * @return status code
  406. */
  407. efi_status_t efi_search_protocol(const efi_handle_t handle,
  408. const efi_guid_t *protocol_guid,
  409. struct efi_handler **handler)
  410. {
  411. struct efi_object *efiobj;
  412. struct list_head *lhandle;
  413. if (!handle || !protocol_guid)
  414. return EFI_INVALID_PARAMETER;
  415. efiobj = efi_search_obj(handle);
  416. if (!efiobj)
  417. return EFI_INVALID_PARAMETER;
  418. list_for_each(lhandle, &efiobj->protocols) {
  419. struct efi_handler *protocol;
  420. protocol = list_entry(lhandle, struct efi_handler, link);
  421. if (!guidcmp(protocol->guid, protocol_guid)) {
  422. if (handler)
  423. *handler = protocol;
  424. return EFI_SUCCESS;
  425. }
  426. }
  427. return EFI_NOT_FOUND;
  428. }
  429. /*
  430. * Delete protocol from a handle.
  431. *
  432. * @handle handle from which the protocol shall be deleted
  433. * @protocol GUID of the protocol to be deleted
  434. * @protocol_interface interface of the protocol implementation
  435. * @return status code
  436. */
  437. efi_status_t efi_remove_protocol(const efi_handle_t handle,
  438. const efi_guid_t *protocol,
  439. void *protocol_interface)
  440. {
  441. struct efi_handler *handler;
  442. efi_status_t ret;
  443. ret = efi_search_protocol(handle, protocol, &handler);
  444. if (ret != EFI_SUCCESS)
  445. return ret;
  446. if (guidcmp(handler->guid, protocol))
  447. return EFI_INVALID_PARAMETER;
  448. list_del(&handler->link);
  449. free(handler);
  450. return EFI_SUCCESS;
  451. }
  452. /*
  453. * Delete all protocols from a handle.
  454. *
  455. * @handle handle from which the protocols shall be deleted
  456. * @return status code
  457. */
  458. efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
  459. {
  460. struct efi_object *efiobj;
  461. struct efi_handler *protocol;
  462. struct efi_handler *pos;
  463. efiobj = efi_search_obj(handle);
  464. if (!efiobj)
  465. return EFI_INVALID_PARAMETER;
  466. list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
  467. efi_status_t ret;
  468. ret = efi_remove_protocol(handle, protocol->guid,
  469. protocol->protocol_interface);
  470. if (ret != EFI_SUCCESS)
  471. return ret;
  472. }
  473. return EFI_SUCCESS;
  474. }
  475. /*
  476. * Delete handle.
  477. *
  478. * @handle handle to delete
  479. */
  480. void efi_delete_handle(struct efi_object *obj)
  481. {
  482. if (!obj)
  483. return;
  484. efi_remove_all_protocols(obj->handle);
  485. list_del(&obj->link);
  486. free(obj);
  487. }
  488. /*
  489. * Check if a pointer is a valid event.
  490. *
  491. * @event pointer to check
  492. * @return status code
  493. */
  494. static efi_status_t efi_is_event(const struct efi_event *event)
  495. {
  496. const struct efi_event *evt;
  497. if (!event)
  498. return EFI_INVALID_PARAMETER;
  499. list_for_each_entry(evt, &efi_events, link) {
  500. if (evt == event)
  501. return EFI_SUCCESS;
  502. }
  503. return EFI_INVALID_PARAMETER;
  504. }
  505. /*
  506. * Create an event.
  507. *
  508. * This function is used inside U-Boot code to create an event.
  509. *
  510. * For the API function implementing the CreateEvent service see
  511. * efi_create_event_ext.
  512. *
  513. * @type type of the event to create
  514. * @notify_tpl task priority level of the event
  515. * @notify_function notification function of the event
  516. * @notify_context pointer passed to the notification function
  517. * @event created event
  518. * @return status code
  519. */
  520. efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
  521. void (EFIAPI *notify_function) (
  522. struct efi_event *event,
  523. void *context),
  524. void *notify_context, efi_guid_t *group,
  525. struct efi_event **event)
  526. {
  527. struct efi_event *evt;
  528. if (event == NULL)
  529. return EFI_INVALID_PARAMETER;
  530. if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
  531. return EFI_INVALID_PARAMETER;
  532. if ((type & (EVT_NOTIFY_SIGNAL | EVT_NOTIFY_WAIT)) &&
  533. notify_function == NULL)
  534. return EFI_INVALID_PARAMETER;
  535. evt = calloc(1, sizeof(struct efi_event));
  536. if (!evt)
  537. return EFI_OUT_OF_RESOURCES;
  538. evt->type = type;
  539. evt->notify_tpl = notify_tpl;
  540. evt->notify_function = notify_function;
  541. evt->notify_context = notify_context;
  542. evt->group = group;
  543. /* Disable timers on bootup */
  544. evt->trigger_next = -1ULL;
  545. evt->is_queued = false;
  546. evt->is_signaled = false;
  547. list_add_tail(&evt->link, &efi_events);
  548. *event = evt;
  549. return EFI_SUCCESS;
  550. }
  551. /*
  552. * Create an event in a group.
  553. *
  554. * This function implements the CreateEventEx service.
  555. * See the Unified Extensible Firmware Interface (UEFI) specification
  556. * for details.
  557. * TODO: Support event groups
  558. *
  559. * @type type of the event to create
  560. * @notify_tpl task priority level of the event
  561. * @notify_function notification function of the event
  562. * @notify_context pointer passed to the notification function
  563. * @event created event
  564. * @event_group event group
  565. * @return status code
  566. */
  567. efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
  568. void (EFIAPI *notify_function) (
  569. struct efi_event *event,
  570. void *context),
  571. void *notify_context,
  572. efi_guid_t *event_group,
  573. struct efi_event **event)
  574. {
  575. EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
  576. notify_context, event_group);
  577. return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
  578. notify_context, event_group, event));
  579. }
  580. /*
  581. * Create an event.
  582. *
  583. * This function implements the CreateEvent service.
  584. * See the Unified Extensible Firmware Interface (UEFI) specification
  585. * for details.
  586. *
  587. * @type type of the event to create
  588. * @notify_tpl task priority level of the event
  589. * @notify_function notification function of the event
  590. * @notify_context pointer passed to the notification function
  591. * @event created event
  592. * @return status code
  593. */
  594. static efi_status_t EFIAPI efi_create_event_ext(
  595. uint32_t type, efi_uintn_t notify_tpl,
  596. void (EFIAPI *notify_function) (
  597. struct efi_event *event,
  598. void *context),
  599. void *notify_context, struct efi_event **event)
  600. {
  601. EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
  602. notify_context);
  603. return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
  604. notify_context, NULL, event));
  605. }
  606. /*
  607. * Check if a timer event has occurred or a queued notification function should
  608. * be called.
  609. *
  610. * Our timers have to work without interrupts, so we check whenever keyboard
  611. * input or disk accesses happen if enough time elapsed for them to fire.
  612. */
  613. void efi_timer_check(void)
  614. {
  615. struct efi_event *evt;
  616. u64 now = timer_get_us();
  617. list_for_each_entry(evt, &efi_events, link) {
  618. if (evt->is_queued)
  619. efi_queue_event(evt, true);
  620. if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
  621. continue;
  622. switch (evt->trigger_type) {
  623. case EFI_TIMER_RELATIVE:
  624. evt->trigger_type = EFI_TIMER_STOP;
  625. break;
  626. case EFI_TIMER_PERIODIC:
  627. evt->trigger_next += evt->trigger_time;
  628. break;
  629. default:
  630. continue;
  631. }
  632. evt->is_signaled = false;
  633. efi_signal_event(evt, true);
  634. }
  635. WATCHDOG_RESET();
  636. }
  637. /*
  638. * Set the trigger time for a timer event or stop the event.
  639. *
  640. * This is the function for internal usage in U-Boot. For the API function
  641. * implementing the SetTimer service see efi_set_timer_ext.
  642. *
  643. * @event event for which the timer is set
  644. * @type type of the timer
  645. * @trigger_time trigger period in multiples of 100ns
  646. * @return status code
  647. */
  648. efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
  649. uint64_t trigger_time)
  650. {
  651. /* Check that the event is valid */
  652. if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
  653. return EFI_INVALID_PARAMETER;
  654. /*
  655. * The parameter defines a multiple of 100ns.
  656. * We use multiples of 1000ns. So divide by 10.
  657. */
  658. do_div(trigger_time, 10);
  659. switch (type) {
  660. case EFI_TIMER_STOP:
  661. event->trigger_next = -1ULL;
  662. break;
  663. case EFI_TIMER_PERIODIC:
  664. case EFI_TIMER_RELATIVE:
  665. event->trigger_next = timer_get_us() + trigger_time;
  666. break;
  667. default:
  668. return EFI_INVALID_PARAMETER;
  669. }
  670. event->trigger_type = type;
  671. event->trigger_time = trigger_time;
  672. event->is_signaled = false;
  673. return EFI_SUCCESS;
  674. }
  675. /*
  676. * Set the trigger time for a timer event or stop the event.
  677. *
  678. * This function implements the SetTimer service.
  679. * See the Unified Extensible Firmware Interface (UEFI) specification
  680. * for details.
  681. *
  682. * @event event for which the timer is set
  683. * @type type of the timer
  684. * @trigger_time trigger period in multiples of 100ns
  685. * @return status code
  686. */
  687. static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
  688. enum efi_timer_delay type,
  689. uint64_t trigger_time)
  690. {
  691. EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
  692. return EFI_EXIT(efi_set_timer(event, type, trigger_time));
  693. }
  694. /*
  695. * Wait for events to be signaled.
  696. *
  697. * This function implements the WaitForEvent service.
  698. * See the Unified Extensible Firmware Interface (UEFI) specification
  699. * for details.
  700. *
  701. * @num_events number of events to be waited for
  702. * @events events to be waited for
  703. * @index index of the event that was signaled
  704. * @return status code
  705. */
  706. static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
  707. struct efi_event **event,
  708. efi_uintn_t *index)
  709. {
  710. int i;
  711. EFI_ENTRY("%zd, %p, %p", num_events, event, index);
  712. /* Check parameters */
  713. if (!num_events || !event)
  714. return EFI_EXIT(EFI_INVALID_PARAMETER);
  715. /* Check TPL */
  716. if (efi_tpl != TPL_APPLICATION)
  717. return EFI_EXIT(EFI_UNSUPPORTED);
  718. for (i = 0; i < num_events; ++i) {
  719. if (efi_is_event(event[i]) != EFI_SUCCESS)
  720. return EFI_EXIT(EFI_INVALID_PARAMETER);
  721. if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
  722. return EFI_EXIT(EFI_INVALID_PARAMETER);
  723. if (!event[i]->is_signaled)
  724. efi_queue_event(event[i], true);
  725. }
  726. /* Wait for signal */
  727. for (;;) {
  728. for (i = 0; i < num_events; ++i) {
  729. if (event[i]->is_signaled)
  730. goto out;
  731. }
  732. /* Allow events to occur. */
  733. efi_timer_check();
  734. }
  735. out:
  736. /*
  737. * Reset the signal which is passed to the caller to allow periodic
  738. * events to occur.
  739. */
  740. event[i]->is_signaled = false;
  741. if (index)
  742. *index = i;
  743. return EFI_EXIT(EFI_SUCCESS);
  744. }
  745. /*
  746. * Signal an EFI event.
  747. *
  748. * This function implements the SignalEvent service.
  749. * See the Unified Extensible Firmware Interface (UEFI) specification
  750. * for details.
  751. *
  752. * This functions sets the signaled state of the event and queues the
  753. * notification function for execution.
  754. *
  755. * @event event to signal
  756. * @return status code
  757. */
  758. static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
  759. {
  760. EFI_ENTRY("%p", event);
  761. if (efi_is_event(event) != EFI_SUCCESS)
  762. return EFI_EXIT(EFI_INVALID_PARAMETER);
  763. efi_signal_event(event, true);
  764. return EFI_EXIT(EFI_SUCCESS);
  765. }
  766. /*
  767. * Close an EFI event.
  768. *
  769. * This function implements the CloseEvent service.
  770. * See the Unified Extensible Firmware Interface (UEFI) specification
  771. * for details.
  772. *
  773. * @event event to close
  774. * @return status code
  775. */
  776. static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
  777. {
  778. EFI_ENTRY("%p", event);
  779. if (efi_is_event(event) != EFI_SUCCESS)
  780. return EFI_EXIT(EFI_INVALID_PARAMETER);
  781. list_del(&event->link);
  782. free(event);
  783. return EFI_EXIT(EFI_SUCCESS);
  784. }
  785. /*
  786. * Check if an event is signaled.
  787. *
  788. * This function implements the CheckEvent service.
  789. * See the Unified Extensible Firmware Interface (UEFI) specification
  790. * for details.
  791. *
  792. * If an event is not signaled yet, the notification function is queued.
  793. * The signaled state is cleared.
  794. *
  795. * @event event to check
  796. * @return status code
  797. */
  798. static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
  799. {
  800. EFI_ENTRY("%p", event);
  801. efi_timer_check();
  802. if (efi_is_event(event) != EFI_SUCCESS ||
  803. event->type & EVT_NOTIFY_SIGNAL)
  804. return EFI_EXIT(EFI_INVALID_PARAMETER);
  805. if (!event->is_signaled)
  806. efi_queue_event(event, true);
  807. if (event->is_signaled) {
  808. event->is_signaled = false;
  809. return EFI_EXIT(EFI_SUCCESS);
  810. }
  811. return EFI_EXIT(EFI_NOT_READY);
  812. }
  813. /*
  814. * Find the internal EFI object for a handle.
  815. *
  816. * @handle handle to find
  817. * @return EFI object
  818. */
  819. struct efi_object *efi_search_obj(const efi_handle_t handle)
  820. {
  821. struct efi_object *efiobj;
  822. list_for_each_entry(efiobj, &efi_obj_list, link) {
  823. if (efiobj->handle == handle)
  824. return efiobj;
  825. }
  826. return NULL;
  827. }
  828. /*
  829. * Create open protocol info entry and add it to a protocol.
  830. *
  831. * @handler handler of a protocol
  832. * @return open protocol info entry
  833. */
  834. static struct efi_open_protocol_info_entry *efi_create_open_info(
  835. struct efi_handler *handler)
  836. {
  837. struct efi_open_protocol_info_item *item;
  838. item = calloc(1, sizeof(struct efi_open_protocol_info_item));
  839. if (!item)
  840. return NULL;
  841. /* Append the item to the open protocol info list. */
  842. list_add_tail(&item->link, &handler->open_infos);
  843. return &item->info;
  844. }
  845. /*
  846. * Remove an open protocol info entry from a protocol.
  847. *
  848. * @handler handler of a protocol
  849. * @return status code
  850. */
  851. static efi_status_t efi_delete_open_info(
  852. struct efi_open_protocol_info_item *item)
  853. {
  854. list_del(&item->link);
  855. free(item);
  856. return EFI_SUCCESS;
  857. }
  858. /*
  859. * Install new protocol on a handle.
  860. *
  861. * @handle handle on which the protocol shall be installed
  862. * @protocol GUID of the protocol to be installed
  863. * @protocol_interface interface of the protocol implementation
  864. * @return status code
  865. */
  866. efi_status_t efi_add_protocol(const efi_handle_t handle,
  867. const efi_guid_t *protocol,
  868. void *protocol_interface)
  869. {
  870. struct efi_object *efiobj;
  871. struct efi_handler *handler;
  872. efi_status_t ret;
  873. efiobj = efi_search_obj(handle);
  874. if (!efiobj)
  875. return EFI_INVALID_PARAMETER;
  876. ret = efi_search_protocol(handle, protocol, NULL);
  877. if (ret != EFI_NOT_FOUND)
  878. return EFI_INVALID_PARAMETER;
  879. handler = calloc(1, sizeof(struct efi_handler));
  880. if (!handler)
  881. return EFI_OUT_OF_RESOURCES;
  882. handler->guid = protocol;
  883. handler->protocol_interface = protocol_interface;
  884. INIT_LIST_HEAD(&handler->open_infos);
  885. list_add_tail(&handler->link, &efiobj->protocols);
  886. if (!guidcmp(&efi_guid_device_path, protocol))
  887. EFI_PRINT("installed device path '%pD'\n", protocol_interface);
  888. return EFI_SUCCESS;
  889. }
  890. /*
  891. * Install protocol interface.
  892. *
  893. * This function implements the InstallProtocolInterface service.
  894. * See the Unified Extensible Firmware Interface (UEFI) specification
  895. * for details.
  896. *
  897. * @handle handle on which the protocol shall be installed
  898. * @protocol GUID of the protocol to be installed
  899. * @protocol_interface_type type of the interface to be installed,
  900. * always EFI_NATIVE_INTERFACE
  901. * @protocol_interface interface of the protocol implementation
  902. * @return status code
  903. */
  904. static efi_status_t EFIAPI efi_install_protocol_interface(
  905. void **handle, const efi_guid_t *protocol,
  906. int protocol_interface_type, void *protocol_interface)
  907. {
  908. efi_status_t r;
  909. EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
  910. protocol_interface);
  911. if (!handle || !protocol ||
  912. protocol_interface_type != EFI_NATIVE_INTERFACE) {
  913. r = EFI_INVALID_PARAMETER;
  914. goto out;
  915. }
  916. /* Create new handle if requested. */
  917. if (!*handle) {
  918. r = efi_create_handle(handle);
  919. if (r != EFI_SUCCESS)
  920. goto out;
  921. debug("%sEFI: new handle %p\n", indent_string(nesting_level),
  922. *handle);
  923. } else {
  924. debug("%sEFI: handle %p\n", indent_string(nesting_level),
  925. *handle);
  926. }
  927. /* Add new protocol */
  928. r = efi_add_protocol(*handle, protocol, protocol_interface);
  929. out:
  930. return EFI_EXIT(r);
  931. }
  932. /*
  933. * Reinstall protocol interface.
  934. *
  935. * This function implements the ReinstallProtocolInterface service.
  936. * See the Unified Extensible Firmware Interface (UEFI) specification
  937. * for details.
  938. *
  939. * @handle handle on which the protocol shall be
  940. * reinstalled
  941. * @protocol GUID of the protocol to be installed
  942. * @old_interface interface to be removed
  943. * @new_interface interface to be installed
  944. * @return status code
  945. */
  946. static efi_status_t EFIAPI efi_reinstall_protocol_interface(
  947. efi_handle_t handle, const efi_guid_t *protocol,
  948. void *old_interface, void *new_interface)
  949. {
  950. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
  951. new_interface);
  952. return EFI_EXIT(EFI_ACCESS_DENIED);
  953. }
  954. /*
  955. * Get all drivers associated to a controller.
  956. * The allocated buffer has to be freed with free().
  957. *
  958. * @efiobj handle of the controller
  959. * @protocol protocol guid (optional)
  960. * @number_of_drivers number of child controllers
  961. * @driver_handle_buffer handles of the the drivers
  962. * @return status code
  963. */
  964. static efi_status_t efi_get_drivers(struct efi_object *efiobj,
  965. const efi_guid_t *protocol,
  966. efi_uintn_t *number_of_drivers,
  967. efi_handle_t **driver_handle_buffer)
  968. {
  969. struct efi_handler *handler;
  970. struct efi_open_protocol_info_item *item;
  971. efi_uintn_t count = 0, i;
  972. bool duplicate;
  973. /* Count all driver associations */
  974. list_for_each_entry(handler, &efiobj->protocols, link) {
  975. if (protocol && guidcmp(handler->guid, protocol))
  976. continue;
  977. list_for_each_entry(item, &handler->open_infos, link) {
  978. if (item->info.attributes &
  979. EFI_OPEN_PROTOCOL_BY_DRIVER)
  980. ++count;
  981. }
  982. }
  983. /*
  984. * Create buffer. In case of duplicate driver assignments the buffer
  985. * will be too large. But that does not harm.
  986. */
  987. *number_of_drivers = 0;
  988. *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
  989. if (!*driver_handle_buffer)
  990. return EFI_OUT_OF_RESOURCES;
  991. /* Collect unique driver handles */
  992. list_for_each_entry(handler, &efiobj->protocols, link) {
  993. if (protocol && guidcmp(handler->guid, protocol))
  994. continue;
  995. list_for_each_entry(item, &handler->open_infos, link) {
  996. if (item->info.attributes &
  997. EFI_OPEN_PROTOCOL_BY_DRIVER) {
  998. /* Check this is a new driver */
  999. duplicate = false;
  1000. for (i = 0; i < *number_of_drivers; ++i) {
  1001. if ((*driver_handle_buffer)[i] ==
  1002. item->info.agent_handle)
  1003. duplicate = true;
  1004. }
  1005. /* Copy handle to buffer */
  1006. if (!duplicate) {
  1007. i = (*number_of_drivers)++;
  1008. (*driver_handle_buffer)[i] =
  1009. item->info.agent_handle;
  1010. }
  1011. }
  1012. }
  1013. }
  1014. return EFI_SUCCESS;
  1015. }
  1016. /*
  1017. * Disconnect all drivers from a controller.
  1018. *
  1019. * This function implements the DisconnectController service.
  1020. * See the Unified Extensible Firmware Interface (UEFI) specification
  1021. * for details.
  1022. *
  1023. * @efiobj handle of the controller
  1024. * @protocol protocol guid (optional)
  1025. * @child_handle handle of the child to destroy
  1026. * @return status code
  1027. */
  1028. static efi_status_t efi_disconnect_all_drivers(
  1029. struct efi_object *efiobj,
  1030. const efi_guid_t *protocol,
  1031. efi_handle_t child_handle)
  1032. {
  1033. efi_uintn_t number_of_drivers;
  1034. efi_handle_t *driver_handle_buffer;
  1035. efi_status_t r, ret;
  1036. ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
  1037. &driver_handle_buffer);
  1038. if (ret != EFI_SUCCESS)
  1039. return ret;
  1040. ret = EFI_NOT_FOUND;
  1041. while (number_of_drivers) {
  1042. r = EFI_CALL(efi_disconnect_controller(
  1043. efiobj->handle,
  1044. driver_handle_buffer[--number_of_drivers],
  1045. child_handle));
  1046. if (r == EFI_SUCCESS)
  1047. ret = r;
  1048. }
  1049. free(driver_handle_buffer);
  1050. return ret;
  1051. }
  1052. /*
  1053. * Uninstall protocol interface.
  1054. *
  1055. * This function implements the UninstallProtocolInterface service.
  1056. * See the Unified Extensible Firmware Interface (UEFI) specification
  1057. * for details.
  1058. *
  1059. * @handle handle from which the protocol shall be removed
  1060. * @protocol GUID of the protocol to be removed
  1061. * @protocol_interface interface to be removed
  1062. * @return status code
  1063. */
  1064. static efi_status_t EFIAPI efi_uninstall_protocol_interface(
  1065. efi_handle_t handle, const efi_guid_t *protocol,
  1066. void *protocol_interface)
  1067. {
  1068. struct efi_object *efiobj;
  1069. struct efi_handler *handler;
  1070. struct efi_open_protocol_info_item *item;
  1071. struct efi_open_protocol_info_item *pos;
  1072. efi_status_t r;
  1073. EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
  1074. /* Check handle */
  1075. efiobj = efi_search_obj(handle);
  1076. if (!efiobj) {
  1077. r = EFI_INVALID_PARAMETER;
  1078. goto out;
  1079. }
  1080. /* Find the protocol on the handle */
  1081. r = efi_search_protocol(handle, protocol, &handler);
  1082. if (r != EFI_SUCCESS)
  1083. goto out;
  1084. /* Disconnect controllers */
  1085. efi_disconnect_all_drivers(efiobj, protocol, NULL);
  1086. if (!list_empty(&handler->open_infos)) {
  1087. r = EFI_ACCESS_DENIED;
  1088. goto out;
  1089. }
  1090. /* Close protocol */
  1091. list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
  1092. if (item->info.attributes ==
  1093. EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
  1094. item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
  1095. item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
  1096. list_del(&item->link);
  1097. }
  1098. if (!list_empty(&handler->open_infos)) {
  1099. r = EFI_ACCESS_DENIED;
  1100. goto out;
  1101. }
  1102. r = efi_remove_protocol(handle, protocol, protocol_interface);
  1103. out:
  1104. return EFI_EXIT(r);
  1105. }
  1106. /*
  1107. * Register an event for notification when a protocol is installed.
  1108. *
  1109. * This function implements the RegisterProtocolNotify service.
  1110. * See the Unified Extensible Firmware Interface (UEFI) specification
  1111. * for details.
  1112. *
  1113. * @protocol GUID of the protocol whose installation shall be
  1114. * notified
  1115. * @event event to be signaled upon installation of the protocol
  1116. * @registration key for retrieving the registration information
  1117. * @return status code
  1118. */
  1119. static efi_status_t EFIAPI efi_register_protocol_notify(
  1120. const efi_guid_t *protocol,
  1121. struct efi_event *event,
  1122. void **registration)
  1123. {
  1124. EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
  1125. return EFI_EXIT(EFI_OUT_OF_RESOURCES);
  1126. }
  1127. /*
  1128. * Determine if an EFI handle implements a protocol.
  1129. *
  1130. * See the documentation of the LocateHandle service in the UEFI specification.
  1131. *
  1132. * @search_type selection criterion
  1133. * @protocol GUID of the protocol
  1134. * @search_key registration key
  1135. * @efiobj handle
  1136. * @return 0 if the handle implements the protocol
  1137. */
  1138. static int efi_search(enum efi_locate_search_type search_type,
  1139. const efi_guid_t *protocol, void *search_key,
  1140. struct efi_object *efiobj)
  1141. {
  1142. efi_status_t ret;
  1143. switch (search_type) {
  1144. case ALL_HANDLES:
  1145. return 0;
  1146. case BY_REGISTER_NOTIFY:
  1147. /* TODO: RegisterProtocolNotify is not implemented yet */
  1148. return -1;
  1149. case BY_PROTOCOL:
  1150. ret = efi_search_protocol(efiobj->handle, protocol, NULL);
  1151. return (ret != EFI_SUCCESS);
  1152. default:
  1153. /* Invalid search type */
  1154. return -1;
  1155. }
  1156. }
  1157. /*
  1158. * Locate handles implementing a protocol.
  1159. *
  1160. * This function is meant for U-Boot internal calls. For the API implementation
  1161. * of the LocateHandle service see efi_locate_handle_ext.
  1162. *
  1163. * @search_type selection criterion
  1164. * @protocol GUID of the protocol
  1165. * @search_key registration key
  1166. * @buffer_size size of the buffer to receive the handles in bytes
  1167. * @buffer buffer to receive the relevant handles
  1168. * @return status code
  1169. */
  1170. static efi_status_t efi_locate_handle(
  1171. enum efi_locate_search_type search_type,
  1172. const efi_guid_t *protocol, void *search_key,
  1173. efi_uintn_t *buffer_size, efi_handle_t *buffer)
  1174. {
  1175. struct efi_object *efiobj;
  1176. efi_uintn_t size = 0;
  1177. /* Check parameters */
  1178. switch (search_type) {
  1179. case ALL_HANDLES:
  1180. break;
  1181. case BY_REGISTER_NOTIFY:
  1182. if (!search_key)
  1183. return EFI_INVALID_PARAMETER;
  1184. /* RegisterProtocolNotify is not implemented yet */
  1185. return EFI_UNSUPPORTED;
  1186. case BY_PROTOCOL:
  1187. if (!protocol)
  1188. return EFI_INVALID_PARAMETER;
  1189. break;
  1190. default:
  1191. return EFI_INVALID_PARAMETER;
  1192. }
  1193. /*
  1194. * efi_locate_handle_buffer uses this function for
  1195. * the calculation of the necessary buffer size.
  1196. * So do not require a buffer for buffersize == 0.
  1197. */
  1198. if (!buffer_size || (*buffer_size && !buffer))
  1199. return EFI_INVALID_PARAMETER;
  1200. /* Count how much space we need */
  1201. list_for_each_entry(efiobj, &efi_obj_list, link) {
  1202. if (!efi_search(search_type, protocol, search_key, efiobj))
  1203. size += sizeof(void *);
  1204. }
  1205. if (*buffer_size < size) {
  1206. *buffer_size = size;
  1207. return EFI_BUFFER_TOO_SMALL;
  1208. }
  1209. *buffer_size = size;
  1210. if (size == 0)
  1211. return EFI_NOT_FOUND;
  1212. /* Then fill the array */
  1213. list_for_each_entry(efiobj, &efi_obj_list, link) {
  1214. if (!efi_search(search_type, protocol, search_key, efiobj))
  1215. *buffer++ = efiobj->handle;
  1216. }
  1217. return EFI_SUCCESS;
  1218. }
  1219. /*
  1220. * Locate handles implementing a protocol.
  1221. *
  1222. * This function implements the LocateHandle service.
  1223. * See the Unified Extensible Firmware Interface (UEFI) specification
  1224. * for details.
  1225. *
  1226. * @search_type selection criterion
  1227. * @protocol GUID of the protocol
  1228. * @search_key registration key
  1229. * @buffer_size size of the buffer to receive the handles in bytes
  1230. * @buffer buffer to receive the relevant handles
  1231. * @return 0 if the handle implements the protocol
  1232. */
  1233. static efi_status_t EFIAPI efi_locate_handle_ext(
  1234. enum efi_locate_search_type search_type,
  1235. const efi_guid_t *protocol, void *search_key,
  1236. efi_uintn_t *buffer_size, efi_handle_t *buffer)
  1237. {
  1238. EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
  1239. buffer_size, buffer);
  1240. return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
  1241. buffer_size, buffer));
  1242. }
  1243. /* Collapses configuration table entries, removing index i */
  1244. static void efi_remove_configuration_table(int i)
  1245. {
  1246. struct efi_configuration_table *this = &efi_conf_table[i];
  1247. struct efi_configuration_table *next = &efi_conf_table[i + 1];
  1248. struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
  1249. memmove(this, next, (ulong)end - (ulong)next);
  1250. systab.nr_tables--;
  1251. }
  1252. /*
  1253. * Adds, updates, or removes a configuration table.
  1254. *
  1255. * This function is used for internal calls. For the API implementation of the
  1256. * InstallConfigurationTable service see efi_install_configuration_table_ext.
  1257. *
  1258. * @guid GUID of the installed table
  1259. * @table table to be installed
  1260. * @return status code
  1261. */
  1262. efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
  1263. void *table)
  1264. {
  1265. struct efi_event *evt;
  1266. int i;
  1267. if (!guid)
  1268. return EFI_INVALID_PARAMETER;
  1269. /* Check for guid override */
  1270. for (i = 0; i < systab.nr_tables; i++) {
  1271. if (!guidcmp(guid, &efi_conf_table[i].guid)) {
  1272. if (table)
  1273. efi_conf_table[i].table = table;
  1274. else
  1275. efi_remove_configuration_table(i);
  1276. goto out;
  1277. }
  1278. }
  1279. if (!table)
  1280. return EFI_NOT_FOUND;
  1281. /* No override, check for overflow */
  1282. if (i >= ARRAY_SIZE(efi_conf_table))
  1283. return EFI_OUT_OF_RESOURCES;
  1284. /* Add a new entry */
  1285. memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
  1286. efi_conf_table[i].table = table;
  1287. systab.nr_tables = i + 1;
  1288. out:
  1289. /* Notify that the configuration table was changed */
  1290. list_for_each_entry(evt, &efi_events, link) {
  1291. if (evt->group && !guidcmp(evt->group, guid)) {
  1292. efi_signal_event(evt, false);
  1293. break;
  1294. }
  1295. }
  1296. return EFI_SUCCESS;
  1297. }
  1298. /*
  1299. * Adds, updates, or removes a configuration table.
  1300. *
  1301. * This function implements the InstallConfigurationTable service.
  1302. * See the Unified Extensible Firmware Interface (UEFI) specification
  1303. * for details.
  1304. *
  1305. * @guid GUID of the installed table
  1306. * @table table to be installed
  1307. * @return status code
  1308. */
  1309. static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
  1310. void *table)
  1311. {
  1312. EFI_ENTRY("%pUl, %p", guid, table);
  1313. return EFI_EXIT(efi_install_configuration_table(guid, table));
  1314. }
  1315. /*
  1316. * Initialize a loaded_image_info + loaded_image_info object with correct
  1317. * protocols, boot-device, etc.
  1318. *
  1319. * @info loaded image info to be passed to the entry point of the
  1320. * image
  1321. * @obj internal object associated with the loaded image
  1322. * @device_path device path of the loaded image
  1323. * @file_path file path of the loaded image
  1324. * @return status code
  1325. */
  1326. efi_status_t efi_setup_loaded_image(
  1327. struct efi_loaded_image *info, struct efi_object *obj,
  1328. struct efi_device_path *device_path,
  1329. struct efi_device_path *file_path)
  1330. {
  1331. efi_status_t ret;
  1332. /* Add internal object to object list */
  1333. efi_add_handle(obj);
  1334. /* efi_exit() assumes that the handle points to the info */
  1335. obj->handle = info;
  1336. info->file_path = file_path;
  1337. if (device_path) {
  1338. info->device_handle = efi_dp_find_obj(device_path, NULL);
  1339. /*
  1340. * When asking for the device path interface, return
  1341. * bootefi_device_path
  1342. */
  1343. ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
  1344. device_path);
  1345. if (ret != EFI_SUCCESS)
  1346. goto failure;
  1347. }
  1348. /*
  1349. * When asking for the loaded_image interface, just
  1350. * return handle which points to loaded_image_info
  1351. */
  1352. ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
  1353. if (ret != EFI_SUCCESS)
  1354. goto failure;
  1355. ret = efi_add_protocol(obj->handle,
  1356. &efi_guid_device_path_to_text_protocol,
  1357. (void *)&efi_device_path_to_text);
  1358. if (ret != EFI_SUCCESS)
  1359. goto failure;
  1360. ret = efi_add_protocol(obj->handle,
  1361. &efi_guid_device_path_utilities_protocol,
  1362. (void *)&efi_device_path_utilities);
  1363. if (ret != EFI_SUCCESS)
  1364. goto failure;
  1365. return ret;
  1366. failure:
  1367. printf("ERROR: Failure to install protocols for loaded image\n");
  1368. return ret;
  1369. }
  1370. /*
  1371. * Load an image using a file path.
  1372. *
  1373. * @file_path the path of the image to load
  1374. * @buffer buffer containing the loaded image
  1375. * @return status code
  1376. */
  1377. efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
  1378. void **buffer)
  1379. {
  1380. struct efi_file_info *info = NULL;
  1381. struct efi_file_handle *f;
  1382. static efi_status_t ret;
  1383. efi_uintn_t bs;
  1384. f = efi_file_from_path(file_path);
  1385. if (!f)
  1386. return EFI_DEVICE_ERROR;
  1387. bs = 0;
  1388. EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
  1389. &bs, info));
  1390. if (ret == EFI_BUFFER_TOO_SMALL) {
  1391. info = malloc(bs);
  1392. EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
  1393. &bs, info));
  1394. }
  1395. if (ret != EFI_SUCCESS)
  1396. goto error;
  1397. ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
  1398. if (ret)
  1399. goto error;
  1400. bs = info->file_size;
  1401. EFI_CALL(ret = f->read(f, &bs, *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. efi_uintn_t 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, %zd, %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_instance_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_instance_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. };