efi_boottime.c 79 KB

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