efi_boottime.c 77 KB

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