efi_boottime.c 77 KB

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