efi_boottime.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  1. /*
  2. * EFI application boot time services
  3. *
  4. * Copyright (c) 2016 Alexander Graf
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <div64.h>
  10. #include <efi_loader.h>
  11. #include <environment.h>
  12. #include <malloc.h>
  13. #include <asm/global_data.h>
  14. #include <libfdt_env.h>
  15. #include <u-boot/crc.h>
  16. #include <bootm.h>
  17. #include <inttypes.h>
  18. #include <watchdog.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /* Task priority level */
  21. static efi_uintn_t efi_tpl = TPL_APPLICATION;
  22. /* This list contains all the EFI objects our payload has access to */
  23. LIST_HEAD(efi_obj_list);
  24. /*
  25. * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
  26. * we need to do trickery with caches. Since we don't want to break the EFI
  27. * aware boot path, only apply hacks when loading exiting directly (breaking
  28. * direct Linux EFI booting along the way - oh well).
  29. */
  30. static bool efi_is_direct_boot = true;
  31. /*
  32. * EFI can pass arbitrary additional "tables" containing vendor specific
  33. * information to the payload. One such table is the FDT table which contains
  34. * a pointer to a flattened device tree blob.
  35. *
  36. * In most cases we want to pass an FDT to the payload, so reserve one slot of
  37. * config table space for it. The pointer gets populated by do_bootefi_exec().
  38. */
  39. static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
  40. #ifdef CONFIG_ARM
  41. /*
  42. * The "gd" pointer lives in a register on ARM and AArch64 that we declare
  43. * fixed when compiling U-Boot. However, the payload does not know about that
  44. * restriction so we need to manually swap its and our view of that register on
  45. * EFI callback entry/exit.
  46. */
  47. static volatile void *efi_gd, *app_gd;
  48. #endif
  49. static int entry_count;
  50. static int nesting_level;
  51. /* Called on every callback entry */
  52. int __efi_entry_check(void)
  53. {
  54. int ret = entry_count++ == 0;
  55. #ifdef CONFIG_ARM
  56. assert(efi_gd);
  57. app_gd = gd;
  58. gd = efi_gd;
  59. #endif
  60. return ret;
  61. }
  62. /* Called on every callback exit */
  63. int __efi_exit_check(void)
  64. {
  65. int ret = --entry_count == 0;
  66. #ifdef CONFIG_ARM
  67. gd = app_gd;
  68. #endif
  69. return ret;
  70. }
  71. /* Called from do_bootefi_exec() */
  72. void efi_save_gd(void)
  73. {
  74. #ifdef CONFIG_ARM
  75. efi_gd = gd;
  76. #endif
  77. }
  78. /*
  79. * Special case handler for error/abort that just forces things back
  80. * to u-boot world so we can dump out an abort msg, without any care
  81. * about returning back to UEFI world.
  82. */
  83. void efi_restore_gd(void)
  84. {
  85. #ifdef CONFIG_ARM
  86. /* Only restore if we're already in EFI context */
  87. if (!efi_gd)
  88. return;
  89. gd = efi_gd;
  90. #endif
  91. }
  92. /*
  93. * Two spaces per indent level, maxing out at 10.. which ought to be
  94. * enough for anyone ;-)
  95. */
  96. static const char *indent_string(int level)
  97. {
  98. const char *indent = " ";
  99. const int max = strlen(indent);
  100. level = min(max, level * 2);
  101. return &indent[max - level];
  102. }
  103. const char *__efi_nesting(void)
  104. {
  105. return indent_string(nesting_level);
  106. }
  107. const char *__efi_nesting_inc(void)
  108. {
  109. return indent_string(nesting_level++);
  110. }
  111. const char *__efi_nesting_dec(void)
  112. {
  113. return indent_string(--nesting_level);
  114. }
  115. /*
  116. * Queue an EFI event.
  117. *
  118. * This function queues the notification function of the event for future
  119. * execution.
  120. *
  121. * The notification function is called if the task priority level of the
  122. * event is higher than the current task priority level.
  123. *
  124. * For the SignalEvent service see efi_signal_event_ext.
  125. *
  126. * @event event to signal
  127. */
  128. void efi_signal_event(struct efi_event *event)
  129. {
  130. if (event->notify_function) {
  131. event->is_queued = true;
  132. /* Check TPL */
  133. if (efi_tpl >= event->notify_tpl)
  134. return;
  135. EFI_CALL_VOID(event->notify_function(event,
  136. event->notify_context));
  137. }
  138. event->is_queued = false;
  139. }
  140. /*
  141. * Raise the task priority level.
  142. *
  143. * This function implements the RaiseTpl service.
  144. * See the Unified Extensible Firmware Interface (UEFI) specification
  145. * for details.
  146. *
  147. * @new_tpl new value of the task priority level
  148. * @return old value of the task priority level
  149. */
  150. static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
  151. {
  152. efi_uintn_t old_tpl = efi_tpl;
  153. EFI_ENTRY("0x%zx", new_tpl);
  154. if (new_tpl < efi_tpl)
  155. debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
  156. efi_tpl = new_tpl;
  157. if (efi_tpl > TPL_HIGH_LEVEL)
  158. efi_tpl = TPL_HIGH_LEVEL;
  159. EFI_EXIT(EFI_SUCCESS);
  160. return old_tpl;
  161. }
  162. /*
  163. * Lower the task priority level.
  164. *
  165. * This function implements the RestoreTpl service.
  166. * See the Unified Extensible Firmware Interface (UEFI) specification
  167. * for details.
  168. *
  169. * @old_tpl value of the task priority level to be restored
  170. */
  171. static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
  172. {
  173. EFI_ENTRY("0x%zx", old_tpl);
  174. if (old_tpl > efi_tpl)
  175. debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
  176. efi_tpl = old_tpl;
  177. if (efi_tpl > TPL_HIGH_LEVEL)
  178. efi_tpl = TPL_HIGH_LEVEL;
  179. EFI_EXIT(EFI_SUCCESS);
  180. }
  181. /*
  182. * Allocate memory pages.
  183. *
  184. * This function implements the AllocatePages service.
  185. * See the Unified Extensible Firmware Interface (UEFI) specification
  186. * for details.
  187. *
  188. * @type type of allocation to be performed
  189. * @memory_type usage type of the allocated memory
  190. * @pages number of pages to be allocated
  191. * @memory allocated memory
  192. * @return status code
  193. */
  194. static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
  195. efi_uintn_t pages,
  196. uint64_t *memory)
  197. {
  198. efi_status_t r;
  199. EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
  200. r = efi_allocate_pages(type, memory_type, pages, memory);
  201. return EFI_EXIT(r);
  202. }
  203. /*
  204. * Free memory pages.
  205. *
  206. * This function implements the FreePages service.
  207. * See the Unified Extensible Firmware Interface (UEFI) specification
  208. * for details.
  209. *
  210. * @memory start of the memory area to be freed
  211. * @pages number of pages to be freed
  212. * @return status code
  213. */
  214. static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
  215. efi_uintn_t pages)
  216. {
  217. efi_status_t r;
  218. EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
  219. r = efi_free_pages(memory, pages);
  220. return EFI_EXIT(r);
  221. }
  222. /*
  223. * Get map describing memory usage.
  224. *
  225. * This function implements the GetMemoryMap service.
  226. * See the Unified Extensible Firmware Interface (UEFI) specification
  227. * for details.
  228. *
  229. * @memory_map_size on entry the size, in bytes, of the memory map buffer,
  230. * on exit the size of the copied memory map
  231. * @memory_map buffer to which the memory map is written
  232. * @map_key key for the memory map
  233. * @descriptor_size size of an individual memory descriptor
  234. * @descriptor_version version number of the memory descriptor structure
  235. * @return status code
  236. */
  237. static efi_status_t EFIAPI efi_get_memory_map_ext(
  238. efi_uintn_t *memory_map_size,
  239. struct efi_mem_desc *memory_map,
  240. efi_uintn_t *map_key,
  241. efi_uintn_t *descriptor_size,
  242. uint32_t *descriptor_version)
  243. {
  244. efi_status_t r;
  245. EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
  246. map_key, descriptor_size, descriptor_version);
  247. r = efi_get_memory_map(memory_map_size, memory_map, map_key,
  248. descriptor_size, descriptor_version);
  249. return EFI_EXIT(r);
  250. }
  251. /*
  252. * Allocate memory from pool.
  253. *
  254. * This function implements the AllocatePool service.
  255. * See the Unified Extensible Firmware Interface (UEFI) specification
  256. * for details.
  257. *
  258. * @pool_type type of the pool from which memory is to be allocated
  259. * @size number of bytes to be allocated
  260. * @buffer allocated memory
  261. * @return status code
  262. */
  263. static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
  264. efi_uintn_t size,
  265. void **buffer)
  266. {
  267. efi_status_t r;
  268. EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
  269. r = efi_allocate_pool(pool_type, size, buffer);
  270. return EFI_EXIT(r);
  271. }
  272. /*
  273. * Free memory from pool.
  274. *
  275. * This function implements the FreePool service.
  276. * See the Unified Extensible Firmware Interface (UEFI) specification
  277. * for details.
  278. *
  279. * @buffer start of memory to be freed
  280. * @return status code
  281. */
  282. static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
  283. {
  284. efi_status_t r;
  285. EFI_ENTRY("%p", buffer);
  286. r = efi_free_pool(buffer);
  287. return EFI_EXIT(r);
  288. }
  289. /*
  290. * Create handle.
  291. *
  292. * @handle new handle
  293. * @return status code
  294. */
  295. efi_status_t efi_create_handle(void **handle)
  296. {
  297. struct efi_object *obj;
  298. efi_status_t r;
  299. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
  300. sizeof(struct efi_object),
  301. (void **)&obj);
  302. if (r != EFI_SUCCESS)
  303. return r;
  304. memset(obj, 0, sizeof(struct efi_object));
  305. obj->handle = obj;
  306. list_add_tail(&obj->link, &efi_obj_list);
  307. *handle = obj;
  308. return r;
  309. }
  310. /*
  311. * Our event capabilities are very limited. Only a small limited
  312. * number of events is allowed to coexist.
  313. */
  314. static struct efi_event efi_events[16];
  315. /*
  316. * Create an event.
  317. *
  318. * This function is used inside U-Boot code to create an event.
  319. *
  320. * For the API function implementing the CreateEvent service see
  321. * efi_create_event_ext.
  322. *
  323. * @type type of the event to create
  324. * @notify_tpl task priority level of the event
  325. * @notify_function notification function of the event
  326. * @notify_context pointer passed to the notification function
  327. * @event created event
  328. * @return status code
  329. */
  330. efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
  331. void (EFIAPI *notify_function) (
  332. struct efi_event *event,
  333. void *context),
  334. void *notify_context, struct efi_event **event)
  335. {
  336. int i;
  337. if (event == NULL)
  338. return EFI_INVALID_PARAMETER;
  339. if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
  340. return EFI_INVALID_PARAMETER;
  341. if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
  342. notify_function == NULL)
  343. return EFI_INVALID_PARAMETER;
  344. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  345. if (efi_events[i].type)
  346. continue;
  347. efi_events[i].type = type;
  348. efi_events[i].notify_tpl = notify_tpl;
  349. efi_events[i].notify_function = notify_function;
  350. efi_events[i].notify_context = notify_context;
  351. /* Disable timers on bootup */
  352. efi_events[i].trigger_next = -1ULL;
  353. efi_events[i].is_queued = false;
  354. efi_events[i].is_signaled = false;
  355. *event = &efi_events[i];
  356. return EFI_SUCCESS;
  357. }
  358. return EFI_OUT_OF_RESOURCES;
  359. }
  360. /*
  361. * Create an event.
  362. *
  363. * This function implements the CreateEvent service.
  364. * See the Unified Extensible Firmware Interface (UEFI) specification
  365. * for details.
  366. *
  367. * @type type of the event to create
  368. * @notify_tpl task priority level of the event
  369. * @notify_function notification function of the event
  370. * @notify_context pointer passed to the notification function
  371. * @event created event
  372. * @return status code
  373. */
  374. static efi_status_t EFIAPI efi_create_event_ext(
  375. uint32_t type, efi_uintn_t notify_tpl,
  376. void (EFIAPI *notify_function) (
  377. struct efi_event *event,
  378. void *context),
  379. void *notify_context, struct efi_event **event)
  380. {
  381. EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
  382. notify_context);
  383. return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
  384. notify_context, event));
  385. }
  386. /*
  387. * Check if a timer event has occurred or a queued notification function should
  388. * be called.
  389. *
  390. * Our timers have to work without interrupts, so we check whenever keyboard
  391. * input or disk accesses happen if enough time elapsed for them to fire.
  392. */
  393. void efi_timer_check(void)
  394. {
  395. int i;
  396. u64 now = timer_get_us();
  397. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  398. if (!efi_events[i].type)
  399. continue;
  400. if (efi_events[i].is_queued)
  401. efi_signal_event(&efi_events[i]);
  402. if (!(efi_events[i].type & EVT_TIMER) ||
  403. now < efi_events[i].trigger_next)
  404. continue;
  405. switch (efi_events[i].trigger_type) {
  406. case EFI_TIMER_RELATIVE:
  407. efi_events[i].trigger_type = EFI_TIMER_STOP;
  408. break;
  409. case EFI_TIMER_PERIODIC:
  410. efi_events[i].trigger_next +=
  411. efi_events[i].trigger_time;
  412. break;
  413. default:
  414. continue;
  415. }
  416. efi_events[i].is_signaled = true;
  417. efi_signal_event(&efi_events[i]);
  418. }
  419. WATCHDOG_RESET();
  420. }
  421. /*
  422. * Set the trigger time for a timer event or stop the event.
  423. *
  424. * This is the function for internal usage in U-Boot. For the API function
  425. * implementing the SetTimer service see efi_set_timer_ext.
  426. *
  427. * @event event for which the timer is set
  428. * @type type of the timer
  429. * @trigger_time trigger period in multiples of 100ns
  430. * @return status code
  431. */
  432. efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
  433. uint64_t trigger_time)
  434. {
  435. int i;
  436. /*
  437. * The parameter defines a multiple of 100ns.
  438. * We use multiples of 1000ns. So divide by 10.
  439. */
  440. do_div(trigger_time, 10);
  441. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  442. if (event != &efi_events[i])
  443. continue;
  444. if (!(event->type & EVT_TIMER))
  445. break;
  446. switch (type) {
  447. case EFI_TIMER_STOP:
  448. event->trigger_next = -1ULL;
  449. break;
  450. case EFI_TIMER_PERIODIC:
  451. case EFI_TIMER_RELATIVE:
  452. event->trigger_next =
  453. timer_get_us() + trigger_time;
  454. break;
  455. default:
  456. return EFI_INVALID_PARAMETER;
  457. }
  458. event->trigger_type = type;
  459. event->trigger_time = trigger_time;
  460. event->is_signaled = false;
  461. return EFI_SUCCESS;
  462. }
  463. return EFI_INVALID_PARAMETER;
  464. }
  465. /*
  466. * Set the trigger time for a timer event or stop the event.
  467. *
  468. * This function implements the SetTimer service.
  469. * See the Unified Extensible Firmware Interface (UEFI) specification
  470. * for details.
  471. *
  472. * @event event for which the timer is set
  473. * @type type of the timer
  474. * @trigger_time trigger period in multiples of 100ns
  475. * @return status code
  476. */
  477. static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
  478. enum efi_timer_delay type,
  479. uint64_t trigger_time)
  480. {
  481. EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
  482. return EFI_EXIT(efi_set_timer(event, type, trigger_time));
  483. }
  484. /*
  485. * Wait for events to be signaled.
  486. *
  487. * This function implements the WaitForEvent service.
  488. * See the Unified Extensible Firmware Interface (UEFI) specification
  489. * for details.
  490. *
  491. * @num_events number of events to be waited for
  492. * @events events to be waited for
  493. * @index index of the event that was signaled
  494. * @return status code
  495. */
  496. static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
  497. struct efi_event **event,
  498. efi_uintn_t *index)
  499. {
  500. int i, j;
  501. EFI_ENTRY("%zd, %p, %p", num_events, event, index);
  502. /* Check parameters */
  503. if (!num_events || !event)
  504. return EFI_EXIT(EFI_INVALID_PARAMETER);
  505. /* Check TPL */
  506. if (efi_tpl != TPL_APPLICATION)
  507. return EFI_EXIT(EFI_UNSUPPORTED);
  508. for (i = 0; i < num_events; ++i) {
  509. for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
  510. if (event[i] == &efi_events[j])
  511. goto known_event;
  512. }
  513. return EFI_EXIT(EFI_INVALID_PARAMETER);
  514. known_event:
  515. if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
  516. return EFI_EXIT(EFI_INVALID_PARAMETER);
  517. if (!event[i]->is_signaled)
  518. efi_signal_event(event[i]);
  519. }
  520. /* Wait for signal */
  521. for (;;) {
  522. for (i = 0; i < num_events; ++i) {
  523. if (event[i]->is_signaled)
  524. goto out;
  525. }
  526. /* Allow events to occur. */
  527. efi_timer_check();
  528. }
  529. out:
  530. /*
  531. * Reset the signal which is passed to the caller to allow periodic
  532. * events to occur.
  533. */
  534. event[i]->is_signaled = false;
  535. if (index)
  536. *index = i;
  537. return EFI_EXIT(EFI_SUCCESS);
  538. }
  539. /*
  540. * Signal an EFI event.
  541. *
  542. * This function implements the SignalEvent service.
  543. * See the Unified Extensible Firmware Interface (UEFI) specification
  544. * for details.
  545. *
  546. * This functions sets the signaled state of the event and queues the
  547. * notification function for execution.
  548. *
  549. * @event event to signal
  550. * @return status code
  551. */
  552. static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
  553. {
  554. int i;
  555. EFI_ENTRY("%p", event);
  556. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  557. if (event != &efi_events[i])
  558. continue;
  559. if (event->is_signaled)
  560. break;
  561. event->is_signaled = true;
  562. if (event->type & EVT_NOTIFY_SIGNAL)
  563. efi_signal_event(event);
  564. break;
  565. }
  566. return EFI_EXIT(EFI_SUCCESS);
  567. }
  568. /*
  569. * Close an EFI event.
  570. *
  571. * This function implements the CloseEvent service.
  572. * See the Unified Extensible Firmware Interface (UEFI) specification
  573. * for details.
  574. *
  575. * @event event to close
  576. * @return status code
  577. */
  578. static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
  579. {
  580. int i;
  581. EFI_ENTRY("%p", event);
  582. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  583. if (event == &efi_events[i]) {
  584. event->type = 0;
  585. event->trigger_next = -1ULL;
  586. event->is_queued = false;
  587. event->is_signaled = false;
  588. return EFI_EXIT(EFI_SUCCESS);
  589. }
  590. }
  591. return EFI_EXIT(EFI_INVALID_PARAMETER);
  592. }
  593. /*
  594. * Check if an event is signaled.
  595. *
  596. * This function implements the CheckEvent service.
  597. * See the Unified Extensible Firmware Interface (UEFI) specification
  598. * for details.
  599. *
  600. * If an event is not signaled yet the notification function is queued.
  601. *
  602. * @event event to check
  603. * @return status code
  604. */
  605. static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
  606. {
  607. int i;
  608. EFI_ENTRY("%p", event);
  609. efi_timer_check();
  610. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  611. if (event != &efi_events[i])
  612. continue;
  613. if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
  614. break;
  615. if (!event->is_signaled)
  616. efi_signal_event(event);
  617. if (event->is_signaled)
  618. return EFI_EXIT(EFI_SUCCESS);
  619. return EFI_EXIT(EFI_NOT_READY);
  620. }
  621. return EFI_EXIT(EFI_INVALID_PARAMETER);
  622. }
  623. /*
  624. * Find the internal EFI object for a handle.
  625. *
  626. * @handle handle to find
  627. * @return EFI object
  628. */
  629. struct efi_object *efi_search_obj(const void *handle)
  630. {
  631. struct efi_object *efiobj;
  632. list_for_each_entry(efiobj, &efi_obj_list, link) {
  633. if (efiobj->handle == handle)
  634. return efiobj;
  635. }
  636. return NULL;
  637. }
  638. /*
  639. * Find a protocol on a handle.
  640. *
  641. * @handle handle
  642. * @protocol_guid GUID of the protocol
  643. * @handler reference to the protocol
  644. * @return status code
  645. */
  646. efi_status_t efi_search_protocol(const void *handle,
  647. const efi_guid_t *protocol_guid,
  648. struct efi_handler **handler)
  649. {
  650. struct efi_object *efiobj;
  651. size_t i;
  652. struct efi_handler *protocol;
  653. if (!handle || !protocol_guid)
  654. return EFI_INVALID_PARAMETER;
  655. efiobj = efi_search_obj(handle);
  656. if (!efiobj)
  657. return EFI_INVALID_PARAMETER;
  658. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  659. protocol = &efiobj->protocols[i];
  660. if (!protocol->guid)
  661. continue;
  662. if (!guidcmp(protocol->guid, protocol_guid)) {
  663. if (handler)
  664. *handler = protocol;
  665. return EFI_SUCCESS;
  666. }
  667. }
  668. return EFI_NOT_FOUND;
  669. }
  670. /*
  671. * Install new protocol on a handle.
  672. *
  673. * @handle handle on which the protocol shall be installed
  674. * @protocol GUID of the protocol to be installed
  675. * @protocol_interface interface of the protocol implementation
  676. * @return status code
  677. */
  678. efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
  679. void *protocol_interface)
  680. {
  681. struct efi_object *efiobj;
  682. struct efi_handler *handler;
  683. efi_status_t ret;
  684. size_t i;
  685. efiobj = efi_search_obj(handle);
  686. if (!efiobj)
  687. return EFI_INVALID_PARAMETER;
  688. ret = efi_search_protocol(handle, protocol, NULL);
  689. if (ret != EFI_NOT_FOUND)
  690. return EFI_INVALID_PARAMETER;
  691. handler = calloc(1, sizeof(struct efi_handler));
  692. if (!handler)
  693. return EFI_OUT_OF_RESOURCES;
  694. /* Install protocol in first empty slot. */
  695. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  696. handler = &efiobj->protocols[i];
  697. if (handler->guid)
  698. continue;
  699. handler->guid = protocol;
  700. handler->protocol_interface = protocol_interface;
  701. return EFI_SUCCESS;
  702. }
  703. return EFI_OUT_OF_RESOURCES;
  704. }
  705. /*
  706. * Delete protocol from a handle.
  707. *
  708. * @handle handle from which the protocol shall be deleted
  709. * @protocol GUID of the protocol to be deleted
  710. * @protocol_interface interface of the protocol implementation
  711. * @return status code
  712. */
  713. efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
  714. void *protocol_interface)
  715. {
  716. struct efi_handler *handler;
  717. efi_status_t ret;
  718. ret = efi_search_protocol(handle, protocol, &handler);
  719. if (ret != EFI_SUCCESS)
  720. return ret;
  721. if (handler->protocol_interface != protocol_interface)
  722. return EFI_NOT_FOUND;
  723. handler->guid = NULL;
  724. handler->protocol_interface = NULL;
  725. return EFI_SUCCESS;
  726. }
  727. /*
  728. * Delete all protocols from a handle.
  729. *
  730. * @handle handle from which the protocols shall be deleted
  731. * @return status code
  732. */
  733. efi_status_t efi_remove_all_protocols(const void *handle)
  734. {
  735. struct efi_object *efiobj;
  736. struct efi_handler *handler;
  737. size_t i;
  738. efiobj = efi_search_obj(handle);
  739. if (!efiobj)
  740. return EFI_INVALID_PARAMETER;
  741. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  742. handler = &efiobj->protocols[i];
  743. handler->guid = NULL;
  744. handler->protocol_interface = NULL;
  745. }
  746. return EFI_SUCCESS;
  747. }
  748. /*
  749. * Install protocol interface.
  750. *
  751. * This function implements the InstallProtocolInterface service.
  752. * See the Unified Extensible Firmware Interface (UEFI) specification
  753. * for details.
  754. *
  755. * @handle handle on which the protocol shall be installed
  756. * @protocol GUID of the protocol to be installed
  757. * @protocol_interface_type type of the interface to be installed,
  758. * always EFI_NATIVE_INTERFACE
  759. * @protocol_interface interface of the protocol implementation
  760. * @return status code
  761. */
  762. static efi_status_t EFIAPI efi_install_protocol_interface(
  763. void **handle, const efi_guid_t *protocol,
  764. int protocol_interface_type, void *protocol_interface)
  765. {
  766. struct list_head *lhandle;
  767. int i;
  768. efi_status_t r;
  769. EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
  770. protocol_interface);
  771. if (!handle || !protocol ||
  772. protocol_interface_type != EFI_NATIVE_INTERFACE) {
  773. r = EFI_INVALID_PARAMETER;
  774. goto out;
  775. }
  776. /* Create new handle if requested. */
  777. if (!*handle) {
  778. r = efi_create_handle(handle);
  779. if (r != EFI_SUCCESS)
  780. goto out;
  781. debug("%sEFI: new handle %p\n", indent_string(nesting_level),
  782. *handle);
  783. } else {
  784. debug("%sEFI: handle %p\n", indent_string(nesting_level),
  785. *handle);
  786. }
  787. /* Find object. */
  788. list_for_each(lhandle, &efi_obj_list) {
  789. struct efi_object *efiobj;
  790. efiobj = list_entry(lhandle, struct efi_object, link);
  791. if (efiobj->handle != *handle)
  792. continue;
  793. /* Check if protocol is already installed on the handle. */
  794. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  795. struct efi_handler *handler = &efiobj->protocols[i];
  796. if (!handler->guid)
  797. continue;
  798. if (!guidcmp(handler->guid, protocol)) {
  799. r = EFI_INVALID_PARAMETER;
  800. goto out;
  801. }
  802. }
  803. /* Install protocol in first empty slot. */
  804. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  805. struct efi_handler *handler = &efiobj->protocols[i];
  806. if (handler->guid)
  807. continue;
  808. handler->guid = protocol;
  809. handler->protocol_interface = protocol_interface;
  810. r = EFI_SUCCESS;
  811. goto out;
  812. }
  813. r = EFI_OUT_OF_RESOURCES;
  814. goto out;
  815. }
  816. r = EFI_INVALID_PARAMETER;
  817. out:
  818. return EFI_EXIT(r);
  819. }
  820. /*
  821. * Reinstall protocol interface.
  822. *
  823. * This function implements the ReinstallProtocolInterface service.
  824. * See the Unified Extensible Firmware Interface (UEFI) specification
  825. * for details.
  826. *
  827. * @handle handle on which the protocol shall be
  828. * reinstalled
  829. * @protocol GUID of the protocol to be installed
  830. * @old_interface interface to be removed
  831. * @new_interface interface to be installed
  832. * @return status code
  833. */
  834. static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
  835. const efi_guid_t *protocol, void *old_interface,
  836. void *new_interface)
  837. {
  838. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
  839. new_interface);
  840. return EFI_EXIT(EFI_ACCESS_DENIED);
  841. }
  842. /*
  843. * Uninstall protocol interface.
  844. *
  845. * This function implements the UninstallProtocolInterface service.
  846. * See the Unified Extensible Firmware Interface (UEFI) specification
  847. * for details.
  848. *
  849. * @handle handle from which the protocol shall be removed
  850. * @protocol GUID of the protocol to be removed
  851. * @protocol_interface interface to be removed
  852. * @return status code
  853. */
  854. static efi_status_t EFIAPI efi_uninstall_protocol_interface(
  855. void *handle, const efi_guid_t *protocol,
  856. void *protocol_interface)
  857. {
  858. struct list_head *lhandle;
  859. int i;
  860. efi_status_t r = EFI_NOT_FOUND;
  861. EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
  862. if (!handle || !protocol) {
  863. r = EFI_INVALID_PARAMETER;
  864. goto out;
  865. }
  866. list_for_each(lhandle, &efi_obj_list) {
  867. struct efi_object *efiobj;
  868. efiobj = list_entry(lhandle, struct efi_object, link);
  869. if (efiobj->handle != handle)
  870. continue;
  871. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  872. struct efi_handler *handler = &efiobj->protocols[i];
  873. const efi_guid_t *hprotocol = handler->guid;
  874. if (!hprotocol)
  875. continue;
  876. if (!guidcmp(hprotocol, protocol)) {
  877. if (handler->protocol_interface) {
  878. r = EFI_ACCESS_DENIED;
  879. } else {
  880. handler->guid = 0;
  881. r = EFI_SUCCESS;
  882. }
  883. goto out;
  884. }
  885. }
  886. }
  887. out:
  888. return EFI_EXIT(r);
  889. }
  890. /*
  891. * Register an event for notification when a protocol is installed.
  892. *
  893. * This function implements the RegisterProtocolNotify service.
  894. * See the Unified Extensible Firmware Interface (UEFI) specification
  895. * for details.
  896. *
  897. * @protocol GUID of the protocol whose installation shall be
  898. * notified
  899. * @event event to be signaled upon installation of the protocol
  900. * @registration key for retrieving the registration information
  901. * @return status code
  902. */
  903. static efi_status_t EFIAPI efi_register_protocol_notify(
  904. const efi_guid_t *protocol,
  905. struct efi_event *event,
  906. void **registration)
  907. {
  908. EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
  909. return EFI_EXIT(EFI_OUT_OF_RESOURCES);
  910. }
  911. /*
  912. * Determine if an EFI handle implements a protocol.
  913. *
  914. * See the documentation of the LocateHandle service in the UEFI specification.
  915. *
  916. * @search_type selection criterion
  917. * @protocol GUID of the protocol
  918. * @search_key registration key
  919. * @efiobj handle
  920. * @return 0 if the handle implements the protocol
  921. */
  922. static int efi_search(enum efi_locate_search_type search_type,
  923. const efi_guid_t *protocol, void *search_key,
  924. struct efi_object *efiobj)
  925. {
  926. int i;
  927. switch (search_type) {
  928. case ALL_HANDLES:
  929. return 0;
  930. case BY_REGISTER_NOTIFY:
  931. /* RegisterProtocolNotify is not implemented yet */
  932. return -1;
  933. case BY_PROTOCOL:
  934. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  935. const efi_guid_t *guid = efiobj->protocols[i].guid;
  936. if (guid && !guidcmp(guid, protocol))
  937. return 0;
  938. }
  939. return -1;
  940. }
  941. return -1;
  942. }
  943. /*
  944. * Locate handles implementing a protocol.
  945. *
  946. * This function is meant for U-Boot internal calls. For the API implementation
  947. * of the LocateHandle service see efi_locate_handle_ext.
  948. *
  949. * @search_type selection criterion
  950. * @protocol GUID of the protocol
  951. * @search_key registration key
  952. * @buffer_size size of the buffer to receive the handles in bytes
  953. * @buffer buffer to receive the relevant handles
  954. * @return status code
  955. */
  956. static efi_status_t efi_locate_handle(
  957. enum efi_locate_search_type search_type,
  958. const efi_guid_t *protocol, void *search_key,
  959. efi_uintn_t *buffer_size, efi_handle_t *buffer)
  960. {
  961. struct efi_object *efiobj;
  962. efi_uintn_t size = 0;
  963. /* Check parameters */
  964. switch (search_type) {
  965. case ALL_HANDLES:
  966. break;
  967. case BY_REGISTER_NOTIFY:
  968. if (!search_key)
  969. return EFI_INVALID_PARAMETER;
  970. /* RegisterProtocolNotify is not implemented yet */
  971. return EFI_UNSUPPORTED;
  972. case BY_PROTOCOL:
  973. if (!protocol)
  974. return EFI_INVALID_PARAMETER;
  975. break;
  976. default:
  977. return EFI_INVALID_PARAMETER;
  978. }
  979. /*
  980. * efi_locate_handle_buffer uses this function for
  981. * the calculation of the necessary buffer size.
  982. * So do not require a buffer for buffersize == 0.
  983. */
  984. if (!buffer_size || (*buffer_size && !buffer))
  985. return EFI_INVALID_PARAMETER;
  986. /* Count how much space we need */
  987. list_for_each_entry(efiobj, &efi_obj_list, link) {
  988. if (!efi_search(search_type, protocol, search_key, efiobj))
  989. size += sizeof(void*);
  990. }
  991. if (*buffer_size < size) {
  992. *buffer_size = size;
  993. return EFI_BUFFER_TOO_SMALL;
  994. }
  995. *buffer_size = size;
  996. if (size == 0)
  997. return EFI_NOT_FOUND;
  998. /* Then fill the array */
  999. list_for_each_entry(efiobj, &efi_obj_list, link) {
  1000. if (!efi_search(search_type, protocol, search_key, efiobj))
  1001. *buffer++ = efiobj->handle;
  1002. }
  1003. return EFI_SUCCESS;
  1004. }
  1005. /*
  1006. * Locate handles implementing a protocol.
  1007. *
  1008. * This function implements the LocateHandle service.
  1009. * See the Unified Extensible Firmware Interface (UEFI) specification
  1010. * for details.
  1011. *
  1012. * @search_type selection criterion
  1013. * @protocol GUID of the protocol
  1014. * @search_key registration key
  1015. * @buffer_size size of the buffer to receive the handles in bytes
  1016. * @buffer buffer to receive the relevant handles
  1017. * @return 0 if the handle implements the protocol
  1018. */
  1019. static efi_status_t EFIAPI efi_locate_handle_ext(
  1020. enum efi_locate_search_type search_type,
  1021. const efi_guid_t *protocol, void *search_key,
  1022. efi_uintn_t *buffer_size, efi_handle_t *buffer)
  1023. {
  1024. EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
  1025. buffer_size, buffer);
  1026. return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
  1027. buffer_size, buffer));
  1028. }
  1029. /*
  1030. * Get the device path and handle of an device implementing a protocol.
  1031. *
  1032. * This function implements the LocateDevicePath service.
  1033. * See the Unified Extensible Firmware Interface (UEFI) specification
  1034. * for details.
  1035. *
  1036. * @protocol GUID of the protocol
  1037. * @device_path device path
  1038. * @device handle of the device
  1039. * @return status code
  1040. */
  1041. static efi_status_t EFIAPI efi_locate_device_path(
  1042. const efi_guid_t *protocol,
  1043. struct efi_device_path **device_path,
  1044. efi_handle_t *device)
  1045. {
  1046. struct efi_object *efiobj;
  1047. EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
  1048. efiobj = efi_dp_find_obj(*device_path, device_path);
  1049. if (!efiobj)
  1050. return EFI_EXIT(EFI_NOT_FOUND);
  1051. *device = efiobj->handle;
  1052. return EFI_EXIT(EFI_SUCCESS);
  1053. }
  1054. /* Collapses configuration table entries, removing index i */
  1055. static void efi_remove_configuration_table(int i)
  1056. {
  1057. struct efi_configuration_table *this = &efi_conf_table[i];
  1058. struct efi_configuration_table *next = &efi_conf_table[i+1];
  1059. struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
  1060. memmove(this, next, (ulong)end - (ulong)next);
  1061. systab.nr_tables--;
  1062. }
  1063. /*
  1064. * Adds, updates, or removes a configuration table.
  1065. *
  1066. * This function is used for internal calls. For the API implementation of the
  1067. * InstallConfigurationTable service see efi_install_configuration_table_ext.
  1068. *
  1069. * @guid GUID of the installed table
  1070. * @table table to be installed
  1071. * @return status code
  1072. */
  1073. efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
  1074. {
  1075. int i;
  1076. /* Check for guid override */
  1077. for (i = 0; i < systab.nr_tables; i++) {
  1078. if (!guidcmp(guid, &efi_conf_table[i].guid)) {
  1079. if (table)
  1080. efi_conf_table[i].table = table;
  1081. else
  1082. efi_remove_configuration_table(i);
  1083. return EFI_SUCCESS;
  1084. }
  1085. }
  1086. if (!table)
  1087. return EFI_NOT_FOUND;
  1088. /* No override, check for overflow */
  1089. if (i >= ARRAY_SIZE(efi_conf_table))
  1090. return EFI_OUT_OF_RESOURCES;
  1091. /* Add a new entry */
  1092. memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
  1093. efi_conf_table[i].table = table;
  1094. systab.nr_tables = i + 1;
  1095. return EFI_SUCCESS;
  1096. }
  1097. /*
  1098. * Adds, updates, or removes a configuration table.
  1099. *
  1100. * This function implements the InstallConfigurationTable service.
  1101. * See the Unified Extensible Firmware Interface (UEFI) specification
  1102. * for details.
  1103. *
  1104. * @guid GUID of the installed table
  1105. * @table table to be installed
  1106. * @return status code
  1107. */
  1108. static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
  1109. void *table)
  1110. {
  1111. EFI_ENTRY("%pUl, %p", guid, table);
  1112. return EFI_EXIT(efi_install_configuration_table(guid, table));
  1113. }
  1114. /*
  1115. * Initialize a loaded_image_info + loaded_image_info object with correct
  1116. * protocols, boot-device, etc.
  1117. *
  1118. * @info loaded image info to be passed to the entry point of the
  1119. * image
  1120. * @obj internal object associated with the loaded image
  1121. * @device_path device path of the loaded image
  1122. * @file_path file path of the loaded image
  1123. */
  1124. void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
  1125. struct efi_device_path *device_path,
  1126. struct efi_device_path *file_path)
  1127. {
  1128. obj->handle = info;
  1129. /*
  1130. * When asking for the device path interface, return
  1131. * bootefi_device_path
  1132. */
  1133. obj->protocols[0].guid = &efi_guid_device_path;
  1134. obj->protocols[0].protocol_interface = device_path;
  1135. /*
  1136. * When asking for the loaded_image interface, just
  1137. * return handle which points to loaded_image_info
  1138. */
  1139. obj->protocols[1].guid = &efi_guid_loaded_image;
  1140. obj->protocols[1].protocol_interface = info;
  1141. obj->protocols[2].guid = &efi_guid_console_control;
  1142. obj->protocols[2].protocol_interface = (void *)&efi_console_control;
  1143. obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
  1144. obj->protocols[3].protocol_interface =
  1145. (void *)&efi_device_path_to_text;
  1146. info->file_path = file_path;
  1147. if (device_path)
  1148. info->device_handle = efi_dp_find_obj(device_path, NULL);
  1149. list_add_tail(&obj->link, &efi_obj_list);
  1150. }
  1151. /*
  1152. * Load an image using a file path.
  1153. *
  1154. * @file_path the path of the image to load
  1155. * @buffer buffer containing the loaded image
  1156. * @return status code
  1157. */
  1158. efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
  1159. void **buffer)
  1160. {
  1161. struct efi_file_info *info = NULL;
  1162. struct efi_file_handle *f;
  1163. static efi_status_t ret;
  1164. uint64_t bs;
  1165. f = efi_file_from_path(file_path);
  1166. if (!f)
  1167. return EFI_DEVICE_ERROR;
  1168. bs = 0;
  1169. EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
  1170. &bs, info));
  1171. if (ret == EFI_BUFFER_TOO_SMALL) {
  1172. info = malloc(bs);
  1173. EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
  1174. &bs, info));
  1175. }
  1176. if (ret != EFI_SUCCESS)
  1177. goto error;
  1178. ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
  1179. if (ret)
  1180. goto error;
  1181. EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
  1182. error:
  1183. free(info);
  1184. EFI_CALL(f->close(f));
  1185. if (ret != EFI_SUCCESS) {
  1186. efi_free_pool(*buffer);
  1187. *buffer = NULL;
  1188. }
  1189. return ret;
  1190. }
  1191. /*
  1192. * Load an EFI image into memory.
  1193. *
  1194. * This function implements the LoadImage service.
  1195. * See the Unified Extensible Firmware Interface (UEFI) specification
  1196. * for details.
  1197. *
  1198. * @boot_policy true for request originating from the boot manager
  1199. * @parent_image the calles's image handle
  1200. * @file_path the path of the image to load
  1201. * @source_buffer memory location from which the image is installed
  1202. * @source_size size of the memory area from which the image is
  1203. * installed
  1204. * @image_handle handle for the newly installed image
  1205. * @return status code
  1206. */
  1207. static efi_status_t EFIAPI efi_load_image(bool boot_policy,
  1208. efi_handle_t parent_image,
  1209. struct efi_device_path *file_path,
  1210. void *source_buffer,
  1211. unsigned long source_size,
  1212. efi_handle_t *image_handle)
  1213. {
  1214. struct efi_loaded_image *info;
  1215. struct efi_object *obj;
  1216. EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
  1217. file_path, source_buffer, source_size, image_handle);
  1218. info = calloc(1, sizeof(*info));
  1219. obj = calloc(1, sizeof(*obj));
  1220. if (!source_buffer) {
  1221. struct efi_device_path *dp, *fp;
  1222. efi_status_t ret;
  1223. ret = efi_load_image_from_path(file_path, &source_buffer);
  1224. if (ret != EFI_SUCCESS) {
  1225. free(info);
  1226. free(obj);
  1227. return EFI_EXIT(ret);
  1228. }
  1229. /*
  1230. * split file_path which contains both the device and
  1231. * file parts:
  1232. */
  1233. efi_dp_split_file_path(file_path, &dp, &fp);
  1234. efi_setup_loaded_image(info, obj, dp, fp);
  1235. } else {
  1236. /* In this case, file_path is the "device" path, ie.
  1237. * something like a HARDWARE_DEVICE:MEMORY_MAPPED
  1238. */
  1239. efi_setup_loaded_image(info, obj, file_path, NULL);
  1240. }
  1241. info->reserved = efi_load_pe(source_buffer, info);
  1242. if (!info->reserved) {
  1243. free(info);
  1244. free(obj);
  1245. return EFI_EXIT(EFI_UNSUPPORTED);
  1246. }
  1247. info->system_table = &systab;
  1248. info->parent_handle = parent_image;
  1249. *image_handle = info;
  1250. return EFI_EXIT(EFI_SUCCESS);
  1251. }
  1252. /*
  1253. * Call the entry point of an image.
  1254. *
  1255. * This function implements the StartImage service.
  1256. * See the Unified Extensible Firmware Interface (UEFI) specification
  1257. * for details.
  1258. *
  1259. * @image_handle handle of the image
  1260. * @exit_data_size size of the buffer
  1261. * @exit_data buffer to receive the exit data of the called image
  1262. * @return status code
  1263. */
  1264. static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
  1265. unsigned long *exit_data_size,
  1266. s16 **exit_data)
  1267. {
  1268. ulong (*entry)(void *image_handle, struct efi_system_table *st);
  1269. struct efi_loaded_image *info = image_handle;
  1270. EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
  1271. entry = info->reserved;
  1272. efi_is_direct_boot = false;
  1273. /* call the image! */
  1274. if (setjmp(&info->exit_jmp)) {
  1275. /* We returned from the child image */
  1276. return EFI_EXIT(info->exit_status);
  1277. }
  1278. __efi_nesting_dec();
  1279. __efi_exit_check();
  1280. entry(image_handle, &systab);
  1281. __efi_entry_check();
  1282. __efi_nesting_inc();
  1283. /* Should usually never get here */
  1284. return EFI_EXIT(EFI_SUCCESS);
  1285. }
  1286. /*
  1287. * Leave an EFI application or driver.
  1288. *
  1289. * This function implements the Exit service.
  1290. * See the Unified Extensible Firmware Interface (UEFI) specification
  1291. * for details.
  1292. *
  1293. * @image_handle handle of the application or driver that is exiting
  1294. * @exit_status status code
  1295. * @exit_data_size size of the buffer in bytes
  1296. * @exit_data buffer with data describing an error
  1297. * @return status code
  1298. */
  1299. static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
  1300. efi_status_t exit_status, unsigned long exit_data_size,
  1301. int16_t *exit_data)
  1302. {
  1303. struct efi_loaded_image *loaded_image_info = (void*)image_handle;
  1304. EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
  1305. exit_data_size, exit_data);
  1306. /* Make sure entry/exit counts for EFI world cross-overs match */
  1307. __efi_exit_check();
  1308. /*
  1309. * But longjmp out with the U-Boot gd, not the application's, as
  1310. * the other end is a setjmp call inside EFI context.
  1311. */
  1312. efi_restore_gd();
  1313. loaded_image_info->exit_status = exit_status;
  1314. longjmp(&loaded_image_info->exit_jmp, 1);
  1315. panic("EFI application exited");
  1316. }
  1317. /*
  1318. * Unload an EFI image.
  1319. *
  1320. * This function implements the UnloadImage service.
  1321. * See the Unified Extensible Firmware Interface (UEFI) specification
  1322. * for details.
  1323. *
  1324. * @image_handle handle of the image to be unloaded
  1325. * @return status code
  1326. */
  1327. static efi_status_t EFIAPI efi_unload_image(void *image_handle)
  1328. {
  1329. struct efi_object *efiobj;
  1330. EFI_ENTRY("%p", image_handle);
  1331. efiobj = efi_search_obj(image_handle);
  1332. if (efiobj)
  1333. list_del(&efiobj->link);
  1334. return EFI_EXIT(EFI_SUCCESS);
  1335. }
  1336. /*
  1337. * Fix up caches for EFI payloads if necessary.
  1338. */
  1339. static void efi_exit_caches(void)
  1340. {
  1341. #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
  1342. /*
  1343. * Grub on 32bit ARM needs to have caches disabled before jumping into
  1344. * a zImage, but does not know of all cache layers. Give it a hand.
  1345. */
  1346. if (efi_is_direct_boot)
  1347. cleanup_before_linux();
  1348. #endif
  1349. }
  1350. /*
  1351. * Stop boot services.
  1352. *
  1353. * This function implements the ExitBootServices service.
  1354. * See the Unified Extensible Firmware Interface (UEFI) specification
  1355. * for details.
  1356. *
  1357. * @image_handle handle of the loaded image
  1358. * @map_key key of the memory map
  1359. * @return status code
  1360. */
  1361. static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
  1362. unsigned long map_key)
  1363. {
  1364. int i;
  1365. EFI_ENTRY("%p, %ld", image_handle, map_key);
  1366. /* Notify that ExitBootServices is invoked. */
  1367. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  1368. if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
  1369. continue;
  1370. efi_signal_event(&efi_events[i]);
  1371. }
  1372. /* Make sure that notification functions are not called anymore */
  1373. efi_tpl = TPL_HIGH_LEVEL;
  1374. /* XXX Should persist EFI variables here */
  1375. board_quiesce_devices();
  1376. /* Fix up caches for EFI payloads if necessary */
  1377. efi_exit_caches();
  1378. /* This stops all lingering devices */
  1379. bootm_disable_interrupts();
  1380. /* Give the payload some time to boot */
  1381. efi_set_watchdog(0);
  1382. WATCHDOG_RESET();
  1383. return EFI_EXIT(EFI_SUCCESS);
  1384. }
  1385. /*
  1386. * Get next value of the counter.
  1387. *
  1388. * This function implements the NextMonotonicCount service.
  1389. * See the Unified Extensible Firmware Interface (UEFI) specification
  1390. * for details.
  1391. *
  1392. * @count returned value of the counter
  1393. * @return status code
  1394. */
  1395. static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
  1396. {
  1397. static uint64_t mono = 0;
  1398. EFI_ENTRY("%p", count);
  1399. *count = mono++;
  1400. return EFI_EXIT(EFI_SUCCESS);
  1401. }
  1402. /*
  1403. * Sleep.
  1404. *
  1405. * This function implements the Stall sercive.
  1406. * See the Unified Extensible Firmware Interface (UEFI) specification
  1407. * for details.
  1408. *
  1409. * @microseconds period to sleep in microseconds
  1410. * @return status code
  1411. */
  1412. static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
  1413. {
  1414. EFI_ENTRY("%ld", microseconds);
  1415. udelay(microseconds);
  1416. return EFI_EXIT(EFI_SUCCESS);
  1417. }
  1418. /*
  1419. * Reset the watchdog timer.
  1420. *
  1421. * This function implements the SetWatchdogTimer service.
  1422. * See the Unified Extensible Firmware Interface (UEFI) specification
  1423. * for details.
  1424. *
  1425. * @timeout seconds before reset by watchdog
  1426. * @watchdog_code code to be logged when resetting
  1427. * @data_size size of buffer in bytes
  1428. * @watchdog_data buffer with data describing the reset reason
  1429. * @return status code
  1430. */
  1431. static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
  1432. uint64_t watchdog_code,
  1433. unsigned long data_size,
  1434. uint16_t *watchdog_data)
  1435. {
  1436. EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
  1437. data_size, watchdog_data);
  1438. return EFI_EXIT(efi_set_watchdog(timeout));
  1439. }
  1440. /*
  1441. * Connect a controller to a driver.
  1442. *
  1443. * This function implements the ConnectController service.
  1444. * See the Unified Extensible Firmware Interface (UEFI) specification
  1445. * for details.
  1446. *
  1447. * @controller_handle handle of the controller
  1448. * @driver_image_handle handle of the driver
  1449. * @remain_device_path device path of a child controller
  1450. * @recursive true to connect all child controllers
  1451. * @return status code
  1452. */
  1453. static efi_status_t EFIAPI efi_connect_controller(
  1454. efi_handle_t controller_handle,
  1455. efi_handle_t *driver_image_handle,
  1456. struct efi_device_path *remain_device_path,
  1457. bool recursive)
  1458. {
  1459. EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
  1460. remain_device_path, recursive);
  1461. return EFI_EXIT(EFI_NOT_FOUND);
  1462. }
  1463. /*
  1464. * Disconnect a controller from a driver.
  1465. *
  1466. * This function implements the DisconnectController service.
  1467. * See the Unified Extensible Firmware Interface (UEFI) specification
  1468. * for details.
  1469. *
  1470. * @controller_handle handle of the controller
  1471. * @driver_image_handle handle of the driver
  1472. * @child_handle handle of the child to destroy
  1473. * @return status code
  1474. */
  1475. static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
  1476. void *driver_image_handle,
  1477. void *child_handle)
  1478. {
  1479. EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
  1480. child_handle);
  1481. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1482. }
  1483. /*
  1484. * Close a protocol.
  1485. *
  1486. * This function implements the CloseProtocol service.
  1487. * See the Unified Extensible Firmware Interface (UEFI) specification
  1488. * for details.
  1489. *
  1490. * @handle handle on which the protocol shall be closed
  1491. * @protocol GUID of the protocol to close
  1492. * @agent_handle handle of the driver
  1493. * @controller_handle handle of the controller
  1494. * @return status code
  1495. */
  1496. static efi_status_t EFIAPI efi_close_protocol(void *handle,
  1497. const efi_guid_t *protocol,
  1498. void *agent_handle,
  1499. void *controller_handle)
  1500. {
  1501. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
  1502. controller_handle);
  1503. return EFI_EXIT(EFI_NOT_FOUND);
  1504. }
  1505. /*
  1506. * Provide information about then open status of a protocol on a handle
  1507. *
  1508. * This function implements the OpenProtocolInformation service.
  1509. * See the Unified Extensible Firmware Interface (UEFI) specification
  1510. * for details.
  1511. *
  1512. * @handle handle for which the information shall be retrieved
  1513. * @protocol GUID of the protocol
  1514. * @entry_buffer buffer to receive the open protocol information
  1515. * @entry_count number of entries available in the buffer
  1516. * @return status code
  1517. */
  1518. static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
  1519. const efi_guid_t *protocol,
  1520. struct efi_open_protocol_info_entry **entry_buffer,
  1521. efi_uintn_t *entry_count)
  1522. {
  1523. EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
  1524. entry_count);
  1525. return EFI_EXIT(EFI_NOT_FOUND);
  1526. }
  1527. /*
  1528. * Get protocols installed on a handle.
  1529. *
  1530. * This function implements the ProtocolsPerHandleService.
  1531. * See the Unified Extensible Firmware Interface (UEFI) specification
  1532. * for details.
  1533. *
  1534. * @handle handle for which the information is retrieved
  1535. * @protocol_buffer buffer with protocol GUIDs
  1536. * @protocol_buffer_count number of entries in the buffer
  1537. * @return status code
  1538. */
  1539. static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
  1540. efi_guid_t ***protocol_buffer,
  1541. efi_uintn_t *protocol_buffer_count)
  1542. {
  1543. unsigned long buffer_size;
  1544. struct efi_object *efiobj;
  1545. unsigned long i, j;
  1546. struct list_head *lhandle;
  1547. efi_status_t r;
  1548. EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
  1549. protocol_buffer_count);
  1550. if (!handle || !protocol_buffer || !protocol_buffer_count)
  1551. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1552. *protocol_buffer = NULL;
  1553. *protocol_buffer_count = 0;
  1554. list_for_each(lhandle, &efi_obj_list) {
  1555. efiobj = list_entry(lhandle, struct efi_object, link);
  1556. if (efiobj->handle != handle)
  1557. continue;
  1558. /* Count protocols */
  1559. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  1560. if (efiobj->protocols[i].guid)
  1561. ++*protocol_buffer_count;
  1562. }
  1563. /* Copy guids */
  1564. if (*protocol_buffer_count) {
  1565. buffer_size = sizeof(efi_guid_t *) *
  1566. *protocol_buffer_count;
  1567. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
  1568. buffer_size,
  1569. (void **)protocol_buffer);
  1570. if (r != EFI_SUCCESS)
  1571. return EFI_EXIT(r);
  1572. j = 0;
  1573. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
  1574. if (efiobj->protocols[i].guid) {
  1575. (*protocol_buffer)[j] = (void *)
  1576. efiobj->protocols[i].guid;
  1577. ++j;
  1578. }
  1579. }
  1580. }
  1581. break;
  1582. }
  1583. return EFI_EXIT(EFI_SUCCESS);
  1584. }
  1585. /*
  1586. * Locate handles implementing a protocol.
  1587. *
  1588. * This function implements the LocateHandleBuffer service.
  1589. * See the Unified Extensible Firmware Interface (UEFI) specification
  1590. * for details.
  1591. *
  1592. * @search_type selection criterion
  1593. * @protocol GUID of the protocol
  1594. * @search_key registration key
  1595. * @no_handles number of returned handles
  1596. * @buffer buffer with the returned handles
  1597. * @return status code
  1598. */
  1599. static efi_status_t EFIAPI efi_locate_handle_buffer(
  1600. enum efi_locate_search_type search_type,
  1601. const efi_guid_t *protocol, void *search_key,
  1602. efi_uintn_t *no_handles, efi_handle_t **buffer)
  1603. {
  1604. efi_status_t r;
  1605. efi_uintn_t buffer_size = 0;
  1606. EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
  1607. no_handles, buffer);
  1608. if (!no_handles || !buffer) {
  1609. r = EFI_INVALID_PARAMETER;
  1610. goto out;
  1611. }
  1612. *no_handles = 0;
  1613. *buffer = NULL;
  1614. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  1615. *buffer);
  1616. if (r != EFI_BUFFER_TOO_SMALL)
  1617. goto out;
  1618. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
  1619. (void **)buffer);
  1620. if (r != EFI_SUCCESS)
  1621. goto out;
  1622. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  1623. *buffer);
  1624. if (r == EFI_SUCCESS)
  1625. *no_handles = buffer_size / sizeof(void *);
  1626. out:
  1627. return EFI_EXIT(r);
  1628. }
  1629. /*
  1630. * Find an interface implementing a protocol.
  1631. *
  1632. * This function implements the LocateProtocol service.
  1633. * See the Unified Extensible Firmware Interface (UEFI) specification
  1634. * for details.
  1635. *
  1636. * @protocol GUID of the protocol
  1637. * @registration registration key passed to the notification function
  1638. * @protocol_interface interface implementing the protocol
  1639. * @return status code
  1640. */
  1641. static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
  1642. void *registration,
  1643. void **protocol_interface)
  1644. {
  1645. struct list_head *lhandle;
  1646. int i;
  1647. EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
  1648. if (!protocol || !protocol_interface)
  1649. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1650. EFI_PRINT_GUID("protocol", protocol);
  1651. list_for_each(lhandle, &efi_obj_list) {
  1652. struct efi_object *efiobj;
  1653. efiobj = list_entry(lhandle, struct efi_object, link);
  1654. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  1655. struct efi_handler *handler = &efiobj->protocols[i];
  1656. if (!handler->guid)
  1657. continue;
  1658. if (!guidcmp(handler->guid, protocol)) {
  1659. *protocol_interface =
  1660. handler->protocol_interface;
  1661. return EFI_EXIT(EFI_SUCCESS);
  1662. }
  1663. }
  1664. }
  1665. *protocol_interface = NULL;
  1666. return EFI_EXIT(EFI_NOT_FOUND);
  1667. }
  1668. /*
  1669. * Install multiple protocol interfaces.
  1670. *
  1671. * This function implements the MultipleProtocolInterfaces service.
  1672. * See the Unified Extensible Firmware Interface (UEFI) specification
  1673. * for details.
  1674. *
  1675. * @handle handle on which the protocol interfaces shall be installed
  1676. * @... NULL terminated argument list with pairs of protocol GUIDS and
  1677. * interfaces
  1678. * @return status code
  1679. */
  1680. static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
  1681. void **handle, ...)
  1682. {
  1683. EFI_ENTRY("%p", handle);
  1684. va_list argptr;
  1685. const efi_guid_t *protocol;
  1686. void *protocol_interface;
  1687. efi_status_t r = EFI_SUCCESS;
  1688. int i = 0;
  1689. if (!handle)
  1690. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1691. va_start(argptr, handle);
  1692. for (;;) {
  1693. protocol = va_arg(argptr, efi_guid_t*);
  1694. if (!protocol)
  1695. break;
  1696. protocol_interface = va_arg(argptr, void*);
  1697. r = EFI_CALL(efi_install_protocol_interface(
  1698. handle, protocol,
  1699. EFI_NATIVE_INTERFACE,
  1700. protocol_interface));
  1701. if (r != EFI_SUCCESS)
  1702. break;
  1703. i++;
  1704. }
  1705. va_end(argptr);
  1706. if (r == EFI_SUCCESS)
  1707. return EFI_EXIT(r);
  1708. /* If an error occurred undo all changes. */
  1709. va_start(argptr, handle);
  1710. for (; i; --i) {
  1711. protocol = va_arg(argptr, efi_guid_t*);
  1712. protocol_interface = va_arg(argptr, void*);
  1713. EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
  1714. protocol_interface));
  1715. }
  1716. va_end(argptr);
  1717. return EFI_EXIT(r);
  1718. }
  1719. /*
  1720. * Uninstall multiple protocol interfaces.
  1721. *
  1722. * This function implements the UninstallMultipleProtocolInterfaces service.
  1723. * See the Unified Extensible Firmware Interface (UEFI) specification
  1724. * for details.
  1725. *
  1726. * @handle handle from which the protocol interfaces shall be removed
  1727. * @... NULL terminated argument list with pairs of protocol GUIDS and
  1728. * interfaces
  1729. * @return status code
  1730. */
  1731. static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
  1732. void *handle, ...)
  1733. {
  1734. EFI_ENTRY("%p", handle);
  1735. va_list argptr;
  1736. const efi_guid_t *protocol;
  1737. void *protocol_interface;
  1738. efi_status_t r = EFI_SUCCESS;
  1739. size_t i = 0;
  1740. if (!handle)
  1741. return EFI_EXIT(EFI_INVALID_PARAMETER);
  1742. va_start(argptr, handle);
  1743. for (;;) {
  1744. protocol = va_arg(argptr, efi_guid_t*);
  1745. if (!protocol)
  1746. break;
  1747. protocol_interface = va_arg(argptr, void*);
  1748. r = EFI_CALL(efi_uninstall_protocol_interface(
  1749. handle, protocol,
  1750. protocol_interface));
  1751. if (r != EFI_SUCCESS)
  1752. break;
  1753. i++;
  1754. }
  1755. va_end(argptr);
  1756. if (r == EFI_SUCCESS)
  1757. return EFI_EXIT(r);
  1758. /* If an error occurred undo all changes. */
  1759. va_start(argptr, handle);
  1760. for (; i; --i) {
  1761. protocol = va_arg(argptr, efi_guid_t*);
  1762. protocol_interface = va_arg(argptr, void*);
  1763. EFI_CALL(efi_install_protocol_interface(&handle, protocol,
  1764. EFI_NATIVE_INTERFACE,
  1765. protocol_interface));
  1766. }
  1767. va_end(argptr);
  1768. return EFI_EXIT(r);
  1769. }
  1770. /*
  1771. * Calculate cyclic redundancy code.
  1772. *
  1773. * This function implements the CalculateCrc32 service.
  1774. * See the Unified Extensible Firmware Interface (UEFI) specification
  1775. * for details.
  1776. *
  1777. * @data buffer with data
  1778. * @data_size size of buffer in bytes
  1779. * @crc32_p cyclic redundancy code
  1780. * @return status code
  1781. */
  1782. static efi_status_t EFIAPI efi_calculate_crc32(void *data,
  1783. unsigned long data_size,
  1784. uint32_t *crc32_p)
  1785. {
  1786. EFI_ENTRY("%p, %ld", data, data_size);
  1787. *crc32_p = crc32(0, data, data_size);
  1788. return EFI_EXIT(EFI_SUCCESS);
  1789. }
  1790. /*
  1791. * Copy memory.
  1792. *
  1793. * This function implements the CopyMem service.
  1794. * See the Unified Extensible Firmware Interface (UEFI) specification
  1795. * for details.
  1796. *
  1797. * @destination destination of the copy operation
  1798. * @source source of the copy operation
  1799. * @length number of bytes to copy
  1800. */
  1801. static void EFIAPI efi_copy_mem(void *destination, const void *source,
  1802. size_t length)
  1803. {
  1804. EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
  1805. memcpy(destination, source, length);
  1806. EFI_EXIT(EFI_SUCCESS);
  1807. }
  1808. /*
  1809. * Fill memory with a byte value.
  1810. *
  1811. * This function implements the SetMem service.
  1812. * See the Unified Extensible Firmware Interface (UEFI) specification
  1813. * for details.
  1814. *
  1815. * @buffer buffer to fill
  1816. * @size size of buffer in bytes
  1817. * @value byte to copy to the buffer
  1818. */
  1819. static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
  1820. {
  1821. EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
  1822. memset(buffer, value, size);
  1823. EFI_EXIT(EFI_SUCCESS);
  1824. }
  1825. /*
  1826. * Open protocol interface on a handle.
  1827. *
  1828. * This function implements the OpenProtocol interface.
  1829. * See the Unified Extensible Firmware Interface (UEFI) specification
  1830. * for details.
  1831. *
  1832. * @handle handle on which the protocol shall be opened
  1833. * @protocol GUID of the protocol
  1834. * @protocol_interface interface implementing the protocol
  1835. * @agent_handle handle of the driver
  1836. * @controller_handle handle of the controller
  1837. * @attributes attributes indicating how to open the protocol
  1838. * @return status code
  1839. */
  1840. static efi_status_t EFIAPI efi_open_protocol(
  1841. void *handle, const efi_guid_t *protocol,
  1842. void **protocol_interface, void *agent_handle,
  1843. void *controller_handle, uint32_t attributes)
  1844. {
  1845. struct list_head *lhandle;
  1846. int i;
  1847. efi_status_t r = EFI_INVALID_PARAMETER;
  1848. EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
  1849. protocol_interface, agent_handle, controller_handle,
  1850. attributes);
  1851. if (!handle || !protocol ||
  1852. (!protocol_interface && attributes !=
  1853. EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
  1854. goto out;
  1855. }
  1856. EFI_PRINT_GUID("protocol", protocol);
  1857. switch (attributes) {
  1858. case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
  1859. case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
  1860. case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
  1861. break;
  1862. case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
  1863. if (controller_handle == handle)
  1864. goto out;
  1865. case EFI_OPEN_PROTOCOL_BY_DRIVER:
  1866. case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
  1867. if (controller_handle == NULL)
  1868. goto out;
  1869. case EFI_OPEN_PROTOCOL_EXCLUSIVE:
  1870. if (agent_handle == NULL)
  1871. goto out;
  1872. break;
  1873. default:
  1874. goto out;
  1875. }
  1876. list_for_each(lhandle, &efi_obj_list) {
  1877. struct efi_object *efiobj;
  1878. efiobj = list_entry(lhandle, struct efi_object, link);
  1879. if (efiobj->handle != handle)
  1880. continue;
  1881. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  1882. struct efi_handler *handler = &efiobj->protocols[i];
  1883. const efi_guid_t *hprotocol = handler->guid;
  1884. if (!hprotocol)
  1885. continue;
  1886. if (!guidcmp(hprotocol, protocol)) {
  1887. if (attributes !=
  1888. EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
  1889. *protocol_interface =
  1890. handler->protocol_interface;
  1891. }
  1892. r = EFI_SUCCESS;
  1893. goto out;
  1894. }
  1895. }
  1896. goto unsupported;
  1897. }
  1898. unsupported:
  1899. r = EFI_UNSUPPORTED;
  1900. out:
  1901. return EFI_EXIT(r);
  1902. }
  1903. /*
  1904. * Get interface of a protocol on a handle.
  1905. *
  1906. * This function implements the HandleProtocol service.
  1907. * See the Unified Extensible Firmware Interface (UEFI) specification
  1908. * for details.
  1909. *
  1910. * @handle handle on which the protocol shall be opened
  1911. * @protocol GUID of the protocol
  1912. * @protocol_interface interface implementing the protocol
  1913. * @return status code
  1914. */
  1915. static efi_status_t EFIAPI efi_handle_protocol(void *handle,
  1916. const efi_guid_t *protocol,
  1917. void **protocol_interface)
  1918. {
  1919. return efi_open_protocol(handle, protocol, protocol_interface, NULL,
  1920. NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
  1921. }
  1922. static const struct efi_boot_services efi_boot_services = {
  1923. .hdr = {
  1924. .headersize = sizeof(struct efi_table_hdr),
  1925. },
  1926. .raise_tpl = efi_raise_tpl,
  1927. .restore_tpl = efi_restore_tpl,
  1928. .allocate_pages = efi_allocate_pages_ext,
  1929. .free_pages = efi_free_pages_ext,
  1930. .get_memory_map = efi_get_memory_map_ext,
  1931. .allocate_pool = efi_allocate_pool_ext,
  1932. .free_pool = efi_free_pool_ext,
  1933. .create_event = efi_create_event_ext,
  1934. .set_timer = efi_set_timer_ext,
  1935. .wait_for_event = efi_wait_for_event,
  1936. .signal_event = efi_signal_event_ext,
  1937. .close_event = efi_close_event,
  1938. .check_event = efi_check_event,
  1939. .install_protocol_interface = efi_install_protocol_interface,
  1940. .reinstall_protocol_interface = efi_reinstall_protocol_interface,
  1941. .uninstall_protocol_interface = efi_uninstall_protocol_interface,
  1942. .handle_protocol = efi_handle_protocol,
  1943. .reserved = NULL,
  1944. .register_protocol_notify = efi_register_protocol_notify,
  1945. .locate_handle = efi_locate_handle_ext,
  1946. .locate_device_path = efi_locate_device_path,
  1947. .install_configuration_table = efi_install_configuration_table_ext,
  1948. .load_image = efi_load_image,
  1949. .start_image = efi_start_image,
  1950. .exit = efi_exit,
  1951. .unload_image = efi_unload_image,
  1952. .exit_boot_services = efi_exit_boot_services,
  1953. .get_next_monotonic_count = efi_get_next_monotonic_count,
  1954. .stall = efi_stall,
  1955. .set_watchdog_timer = efi_set_watchdog_timer,
  1956. .connect_controller = efi_connect_controller,
  1957. .disconnect_controller = efi_disconnect_controller,
  1958. .open_protocol = efi_open_protocol,
  1959. .close_protocol = efi_close_protocol,
  1960. .open_protocol_information = efi_open_protocol_information,
  1961. .protocols_per_handle = efi_protocols_per_handle,
  1962. .locate_handle_buffer = efi_locate_handle_buffer,
  1963. .locate_protocol = efi_locate_protocol,
  1964. .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
  1965. .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
  1966. .calculate_crc32 = efi_calculate_crc32,
  1967. .copy_mem = efi_copy_mem,
  1968. .set_mem = efi_set_mem,
  1969. };
  1970. static uint16_t __efi_runtime_data firmware_vendor[] =
  1971. { 'D','a','s',' ','U','-','b','o','o','t',0 };
  1972. struct efi_system_table __efi_runtime_data systab = {
  1973. .hdr = {
  1974. .signature = EFI_SYSTEM_TABLE_SIGNATURE,
  1975. .revision = 0x20005, /* 2.5 */
  1976. .headersize = sizeof(struct efi_table_hdr),
  1977. },
  1978. .fw_vendor = (long)firmware_vendor,
  1979. .con_in = (void*)&efi_con_in,
  1980. .con_out = (void*)&efi_con_out,
  1981. .std_err = (void*)&efi_con_out,
  1982. .runtime = (void*)&efi_runtime_services,
  1983. .boottime = (void*)&efi_boot_services,
  1984. .nr_tables = 0,
  1985. .tables = (void*)efi_conf_table,
  1986. };