efi_boottime.c 77 KB

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