efi_boottime.c 77 KB

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