efi_boottime.c 75 KB

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