efi_boottime.c 76 KB

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