efi_boottime.c 85 KB

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