efi_boottime.c 77 KB

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