efi_boottime.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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 <efi_loader.h>
  10. #include <malloc.h>
  11. #include <asm/global_data.h>
  12. #include <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. /* This list contains all the EFI objects our payload has access to */
  19. LIST_HEAD(efi_obj_list);
  20. /*
  21. * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
  22. * we need to do trickery with caches. Since we don't want to break the EFI
  23. * aware boot path, only apply hacks when loading exiting directly (breaking
  24. * direct Linux EFI booting along the way - oh well).
  25. */
  26. static bool efi_is_direct_boot = true;
  27. /*
  28. * EFI can pass arbitrary additional "tables" containing vendor specific
  29. * information to the payload. One such table is the FDT table which contains
  30. * a pointer to a flattened device tree blob.
  31. *
  32. * In most cases we want to pass an FDT to the payload, so reserve one slot of
  33. * config table space for it. The pointer gets populated by do_bootefi_exec().
  34. */
  35. static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
  36. #ifdef CONFIG_ARM
  37. /*
  38. * The "gd" pointer lives in a register on ARM and AArch64 that we declare
  39. * fixed when compiling U-Boot. However, the payload does not know about that
  40. * restriction so we need to manually swap its and our view of that register on
  41. * EFI callback entry/exit.
  42. */
  43. static volatile void *efi_gd, *app_gd;
  44. #endif
  45. static int entry_count;
  46. static int nesting_level;
  47. /* Called on every callback entry */
  48. int __efi_entry_check(void)
  49. {
  50. int ret = entry_count++ == 0;
  51. #ifdef CONFIG_ARM
  52. assert(efi_gd);
  53. app_gd = gd;
  54. gd = efi_gd;
  55. #endif
  56. return ret;
  57. }
  58. /* Called on every callback exit */
  59. int __efi_exit_check(void)
  60. {
  61. int ret = --entry_count == 0;
  62. #ifdef CONFIG_ARM
  63. gd = app_gd;
  64. #endif
  65. return ret;
  66. }
  67. /* Called from do_bootefi_exec() */
  68. void efi_save_gd(void)
  69. {
  70. #ifdef CONFIG_ARM
  71. efi_gd = gd;
  72. #endif
  73. }
  74. /*
  75. * Special case handler for error/abort that just forces things back
  76. * to u-boot world so we can dump out an abort msg, without any care
  77. * about returning back to UEFI world.
  78. */
  79. void efi_restore_gd(void)
  80. {
  81. #ifdef CONFIG_ARM
  82. /* Only restore if we're already in EFI context */
  83. if (!efi_gd)
  84. return;
  85. gd = efi_gd;
  86. #endif
  87. }
  88. /*
  89. * Two spaces per indent level, maxing out at 10.. which ought to be
  90. * enough for anyone ;-)
  91. */
  92. static const char *indent_string(int level)
  93. {
  94. const char *indent = " ";
  95. const int max = strlen(indent);
  96. level = min(max, level * 2);
  97. return &indent[max - level];
  98. }
  99. const char *__efi_nesting(void)
  100. {
  101. return indent_string(nesting_level);
  102. }
  103. const char *__efi_nesting_inc(void)
  104. {
  105. return indent_string(nesting_level++);
  106. }
  107. const char *__efi_nesting_dec(void)
  108. {
  109. return indent_string(--nesting_level);
  110. }
  111. /* Low 32 bit */
  112. #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
  113. /* High 32 bit */
  114. #define EFI_HIGH32(a) (a >> 32)
  115. /*
  116. * 64bit division by 10 implemented as multiplication by 1 / 10
  117. *
  118. * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
  119. */
  120. #define EFI_TENTH 0x199999999999999A
  121. static u64 efi_div10(u64 a)
  122. {
  123. u64 prod;
  124. u64 rem;
  125. u64 ret;
  126. ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
  127. prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
  128. rem = EFI_LOW32(prod);
  129. ret += EFI_HIGH32(prod);
  130. prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
  131. rem += EFI_LOW32(prod);
  132. ret += EFI_HIGH32(prod);
  133. prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
  134. rem += EFI_HIGH32(prod);
  135. ret += EFI_HIGH32(rem);
  136. /* Round to nearest integer */
  137. if (rem >= (1 << 31))
  138. ++ret;
  139. return ret;
  140. }
  141. void efi_signal_event(struct efi_event *event)
  142. {
  143. if (event->signaled)
  144. return;
  145. event->signaled = 1;
  146. if (event->type & EVT_NOTIFY_SIGNAL) {
  147. EFI_CALL(event->notify_function(event, event->notify_context));
  148. }
  149. }
  150. static efi_status_t efi_unsupported(const char *funcname)
  151. {
  152. debug("EFI: App called into unimplemented function %s\n", funcname);
  153. return EFI_EXIT(EFI_UNSUPPORTED);
  154. }
  155. static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
  156. {
  157. EFI_ENTRY("0x%zx", new_tpl);
  158. return EFI_EXIT(0);
  159. }
  160. static void EFIAPI efi_restore_tpl(UINTN old_tpl)
  161. {
  162. EFI_ENTRY("0x%zx", old_tpl);
  163. efi_unsupported(__func__);
  164. }
  165. static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
  166. unsigned long pages,
  167. uint64_t *memory)
  168. {
  169. efi_status_t r;
  170. EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
  171. r = efi_allocate_pages(type, memory_type, pages, memory);
  172. return EFI_EXIT(r);
  173. }
  174. static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
  175. unsigned long pages)
  176. {
  177. efi_status_t r;
  178. EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
  179. r = efi_free_pages(memory, pages);
  180. return EFI_EXIT(r);
  181. }
  182. static efi_status_t EFIAPI efi_get_memory_map_ext(
  183. unsigned long *memory_map_size,
  184. struct efi_mem_desc *memory_map,
  185. unsigned long *map_key,
  186. unsigned long *descriptor_size,
  187. uint32_t *descriptor_version)
  188. {
  189. efi_status_t r;
  190. EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
  191. map_key, descriptor_size, descriptor_version);
  192. r = efi_get_memory_map(memory_map_size, memory_map, map_key,
  193. descriptor_size, descriptor_version);
  194. return EFI_EXIT(r);
  195. }
  196. static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
  197. unsigned long size,
  198. void **buffer)
  199. {
  200. efi_status_t r;
  201. EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
  202. r = efi_allocate_pool(pool_type, size, buffer);
  203. return EFI_EXIT(r);
  204. }
  205. static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
  206. {
  207. efi_status_t r;
  208. EFI_ENTRY("%p", buffer);
  209. r = efi_free_pool(buffer);
  210. return EFI_EXIT(r);
  211. }
  212. /*
  213. * Our event capabilities are very limited. Only a small limited
  214. * number of events is allowed to coexist.
  215. */
  216. static struct efi_event efi_events[16];
  217. efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
  218. void (EFIAPI *notify_function) (
  219. struct efi_event *event,
  220. void *context),
  221. void *notify_context, struct efi_event **event)
  222. {
  223. int i;
  224. if (event == NULL)
  225. return EFI_INVALID_PARAMETER;
  226. if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
  227. return EFI_INVALID_PARAMETER;
  228. if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
  229. notify_function == NULL)
  230. return EFI_INVALID_PARAMETER;
  231. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  232. if (efi_events[i].type)
  233. continue;
  234. efi_events[i].type = type;
  235. efi_events[i].notify_tpl = notify_tpl;
  236. efi_events[i].notify_function = notify_function;
  237. efi_events[i].notify_context = notify_context;
  238. /* Disable timers on bootup */
  239. efi_events[i].trigger_next = -1ULL;
  240. efi_events[i].signaled = 0;
  241. *event = &efi_events[i];
  242. return EFI_SUCCESS;
  243. }
  244. return EFI_OUT_OF_RESOURCES;
  245. }
  246. static efi_status_t EFIAPI efi_create_event_ext(
  247. uint32_t type, UINTN notify_tpl,
  248. void (EFIAPI *notify_function) (
  249. struct efi_event *event,
  250. void *context),
  251. void *notify_context, struct efi_event **event)
  252. {
  253. EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
  254. notify_context);
  255. return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
  256. notify_context, event));
  257. }
  258. /*
  259. * Our timers have to work without interrupts, so we check whenever keyboard
  260. * input or disk accesses happen if enough time elapsed for it to fire.
  261. */
  262. void efi_timer_check(void)
  263. {
  264. int i;
  265. u64 now = timer_get_us();
  266. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  267. if (!efi_events[i].type ||
  268. !(efi_events[i].type & EVT_TIMER) ||
  269. efi_events[i].trigger_type == EFI_TIMER_STOP ||
  270. now < efi_events[i].trigger_next)
  271. continue;
  272. if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
  273. efi_events[i].trigger_next +=
  274. efi_events[i].trigger_time;
  275. efi_events[i].signaled = 0;
  276. }
  277. efi_signal_event(&efi_events[i]);
  278. }
  279. WATCHDOG_RESET();
  280. }
  281. efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
  282. uint64_t trigger_time)
  283. {
  284. int i;
  285. /*
  286. * The parameter defines a multiple of 100ns.
  287. * We use multiples of 1000ns. So divide by 10.
  288. */
  289. trigger_time = efi_div10(trigger_time);
  290. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  291. if (event != &efi_events[i])
  292. continue;
  293. if (!(event->type & EVT_TIMER))
  294. break;
  295. switch (type) {
  296. case EFI_TIMER_STOP:
  297. event->trigger_next = -1ULL;
  298. break;
  299. case EFI_TIMER_PERIODIC:
  300. case EFI_TIMER_RELATIVE:
  301. event->trigger_next =
  302. timer_get_us() + trigger_time;
  303. break;
  304. default:
  305. return EFI_INVALID_PARAMETER;
  306. }
  307. event->trigger_type = type;
  308. event->trigger_time = trigger_time;
  309. return EFI_SUCCESS;
  310. }
  311. return EFI_INVALID_PARAMETER;
  312. }
  313. static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
  314. enum efi_timer_delay type,
  315. uint64_t trigger_time)
  316. {
  317. EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
  318. return EFI_EXIT(efi_set_timer(event, type, trigger_time));
  319. }
  320. static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
  321. struct efi_event **event,
  322. unsigned long *index)
  323. {
  324. int i, j;
  325. EFI_ENTRY("%ld, %p, %p", num_events, event, index);
  326. /* Check parameters */
  327. if (!num_events || !event)
  328. return EFI_EXIT(EFI_INVALID_PARAMETER);
  329. for (i = 0; i < num_events; ++i) {
  330. for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
  331. if (event[i] == &efi_events[j])
  332. goto known_event;
  333. }
  334. return EFI_EXIT(EFI_INVALID_PARAMETER);
  335. known_event:
  336. if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
  337. return EFI_EXIT(EFI_INVALID_PARAMETER);
  338. }
  339. /* Wait for signal */
  340. for (;;) {
  341. for (i = 0; i < num_events; ++i) {
  342. if (event[i]->signaled)
  343. goto out;
  344. }
  345. /* Allow events to occur. */
  346. efi_timer_check();
  347. }
  348. out:
  349. /*
  350. * Reset the signal which is passed to the caller to allow periodic
  351. * events to occur.
  352. */
  353. event[i]->signaled = 0;
  354. if (index)
  355. *index = i;
  356. return EFI_EXIT(EFI_SUCCESS);
  357. }
  358. static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
  359. {
  360. int i;
  361. EFI_ENTRY("%p", event);
  362. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  363. if (event != &efi_events[i])
  364. continue;
  365. efi_signal_event(event);
  366. break;
  367. }
  368. return EFI_EXIT(EFI_SUCCESS);
  369. }
  370. static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
  371. {
  372. int i;
  373. EFI_ENTRY("%p", event);
  374. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  375. if (event == &efi_events[i]) {
  376. event->type = 0;
  377. event->trigger_next = -1ULL;
  378. event->signaled = 0;
  379. return EFI_EXIT(EFI_SUCCESS);
  380. }
  381. }
  382. return EFI_EXIT(EFI_INVALID_PARAMETER);
  383. }
  384. static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
  385. {
  386. int i;
  387. EFI_ENTRY("%p", event);
  388. efi_timer_check();
  389. for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
  390. if (event != &efi_events[i])
  391. continue;
  392. if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
  393. break;
  394. if (event->signaled)
  395. return EFI_EXIT(EFI_SUCCESS);
  396. return EFI_EXIT(EFI_NOT_READY);
  397. }
  398. return EFI_EXIT(EFI_INVALID_PARAMETER);
  399. }
  400. static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
  401. efi_guid_t *protocol, int protocol_interface_type,
  402. void *protocol_interface)
  403. {
  404. struct list_head *lhandle;
  405. int i;
  406. efi_status_t r;
  407. if (!handle || !protocol ||
  408. protocol_interface_type != EFI_NATIVE_INTERFACE) {
  409. r = EFI_INVALID_PARAMETER;
  410. goto out;
  411. }
  412. /* Create new handle if requested. */
  413. if (!*handle) {
  414. r = EFI_OUT_OF_RESOURCES;
  415. goto out;
  416. }
  417. /* Find object. */
  418. list_for_each(lhandle, &efi_obj_list) {
  419. struct efi_object *efiobj;
  420. efiobj = list_entry(lhandle, struct efi_object, link);
  421. if (efiobj->handle != *handle)
  422. continue;
  423. /* Check if protocol is already installed on the handle. */
  424. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  425. struct efi_handler *handler = &efiobj->protocols[i];
  426. if (!handler->guid)
  427. continue;
  428. if (!guidcmp(handler->guid, protocol)) {
  429. r = EFI_INVALID_PARAMETER;
  430. goto out;
  431. }
  432. }
  433. /* Install protocol in first empty slot. */
  434. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  435. struct efi_handler *handler = &efiobj->protocols[i];
  436. if (handler->guid)
  437. continue;
  438. handler->guid = protocol;
  439. handler->protocol_interface = protocol_interface;
  440. r = EFI_SUCCESS;
  441. goto out;
  442. }
  443. r = EFI_OUT_OF_RESOURCES;
  444. goto out;
  445. }
  446. r = EFI_INVALID_PARAMETER;
  447. out:
  448. return r;
  449. }
  450. static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
  451. efi_guid_t *protocol, int protocol_interface_type,
  452. void *protocol_interface)
  453. {
  454. EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
  455. protocol_interface);
  456. return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
  457. protocol_interface_type,
  458. protocol_interface));
  459. }
  460. static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
  461. efi_guid_t *protocol, void *old_interface,
  462. void *new_interface)
  463. {
  464. EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
  465. new_interface);
  466. return EFI_EXIT(EFI_ACCESS_DENIED);
  467. }
  468. static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
  469. efi_guid_t *protocol, void *protocol_interface)
  470. {
  471. struct list_head *lhandle;
  472. int i;
  473. efi_status_t r = EFI_NOT_FOUND;
  474. if (!handle || !protocol) {
  475. r = EFI_INVALID_PARAMETER;
  476. goto out;
  477. }
  478. list_for_each(lhandle, &efi_obj_list) {
  479. struct efi_object *efiobj;
  480. efiobj = list_entry(lhandle, struct efi_object, link);
  481. if (efiobj->handle != handle)
  482. continue;
  483. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  484. struct efi_handler *handler = &efiobj->protocols[i];
  485. const efi_guid_t *hprotocol = handler->guid;
  486. if (!hprotocol)
  487. continue;
  488. if (!guidcmp(hprotocol, protocol)) {
  489. if (handler->protocol_interface) {
  490. r = EFI_ACCESS_DENIED;
  491. } else {
  492. handler->guid = 0;
  493. r = EFI_SUCCESS;
  494. }
  495. goto out;
  496. }
  497. }
  498. }
  499. out:
  500. return r;
  501. }
  502. static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
  503. efi_guid_t *protocol, void *protocol_interface)
  504. {
  505. EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
  506. return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
  507. protocol_interface));
  508. }
  509. static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
  510. struct efi_event *event,
  511. void **registration)
  512. {
  513. EFI_ENTRY("%p, %p, %p", protocol, event, registration);
  514. return EFI_EXIT(EFI_OUT_OF_RESOURCES);
  515. }
  516. static int efi_search(enum efi_locate_search_type search_type,
  517. efi_guid_t *protocol, void *search_key,
  518. struct efi_object *efiobj)
  519. {
  520. int i;
  521. switch (search_type) {
  522. case all_handles:
  523. return 0;
  524. case by_register_notify:
  525. return -1;
  526. case by_protocol:
  527. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  528. const efi_guid_t *guid = efiobj->protocols[i].guid;
  529. if (guid && !guidcmp(guid, protocol))
  530. return 0;
  531. }
  532. return -1;
  533. }
  534. return -1;
  535. }
  536. static efi_status_t efi_locate_handle(
  537. enum efi_locate_search_type search_type,
  538. efi_guid_t *protocol, void *search_key,
  539. unsigned long *buffer_size, efi_handle_t *buffer)
  540. {
  541. struct list_head *lhandle;
  542. unsigned long size = 0;
  543. /* Count how much space we need */
  544. list_for_each(lhandle, &efi_obj_list) {
  545. struct efi_object *efiobj;
  546. efiobj = list_entry(lhandle, struct efi_object, link);
  547. if (!efi_search(search_type, protocol, search_key, efiobj)) {
  548. size += sizeof(void*);
  549. }
  550. }
  551. if (*buffer_size < size) {
  552. *buffer_size = size;
  553. return EFI_BUFFER_TOO_SMALL;
  554. }
  555. *buffer_size = size;
  556. if (size == 0)
  557. return EFI_NOT_FOUND;
  558. /* Then fill the array */
  559. list_for_each(lhandle, &efi_obj_list) {
  560. struct efi_object *efiobj;
  561. efiobj = list_entry(lhandle, struct efi_object, link);
  562. if (!efi_search(search_type, protocol, search_key, efiobj)) {
  563. *(buffer++) = efiobj->handle;
  564. }
  565. }
  566. return EFI_SUCCESS;
  567. }
  568. static efi_status_t EFIAPI efi_locate_handle_ext(
  569. enum efi_locate_search_type search_type,
  570. efi_guid_t *protocol, void *search_key,
  571. unsigned long *buffer_size, efi_handle_t *buffer)
  572. {
  573. EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
  574. buffer_size, buffer);
  575. return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
  576. buffer_size, buffer));
  577. }
  578. static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
  579. struct efi_device_path **device_path,
  580. efi_handle_t *device)
  581. {
  582. EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
  583. return EFI_EXIT(EFI_NOT_FOUND);
  584. }
  585. /* Collapses configuration table entries, removing index i */
  586. static void efi_remove_configuration_table(int i)
  587. {
  588. struct efi_configuration_table *this = &efi_conf_table[i];
  589. struct efi_configuration_table *next = &efi_conf_table[i+1];
  590. struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
  591. memmove(this, next, (ulong)end - (ulong)next);
  592. systab.nr_tables--;
  593. }
  594. efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
  595. {
  596. int i;
  597. /* Check for guid override */
  598. for (i = 0; i < systab.nr_tables; i++) {
  599. if (!guidcmp(guid, &efi_conf_table[i].guid)) {
  600. if (table)
  601. efi_conf_table[i].table = table;
  602. else
  603. efi_remove_configuration_table(i);
  604. return EFI_SUCCESS;
  605. }
  606. }
  607. if (!table)
  608. return EFI_NOT_FOUND;
  609. /* No override, check for overflow */
  610. if (i >= ARRAY_SIZE(efi_conf_table))
  611. return EFI_OUT_OF_RESOURCES;
  612. /* Add a new entry */
  613. memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
  614. efi_conf_table[i].table = table;
  615. systab.nr_tables = i + 1;
  616. return EFI_SUCCESS;
  617. }
  618. static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
  619. void *table)
  620. {
  621. EFI_ENTRY("%p, %p", guid, table);
  622. return EFI_EXIT(efi_install_configuration_table(guid, table));
  623. }
  624. static efi_status_t EFIAPI efi_load_image(bool boot_policy,
  625. efi_handle_t parent_image,
  626. struct efi_device_path *file_path,
  627. void *source_buffer,
  628. unsigned long source_size,
  629. efi_handle_t *image_handle)
  630. {
  631. static struct efi_object loaded_image_info_obj = {
  632. .protocols = {
  633. {
  634. .guid = &efi_guid_loaded_image,
  635. },
  636. },
  637. };
  638. struct efi_loaded_image *info;
  639. struct efi_object *obj;
  640. EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
  641. file_path, source_buffer, source_size, image_handle);
  642. info = malloc(sizeof(*info));
  643. loaded_image_info_obj.protocols[0].protocol_interface = info;
  644. obj = malloc(sizeof(loaded_image_info_obj));
  645. memset(info, 0, sizeof(*info));
  646. memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
  647. obj->handle = info;
  648. info->file_path = file_path;
  649. info->reserved = efi_load_pe(source_buffer, info);
  650. if (!info->reserved) {
  651. free(info);
  652. free(obj);
  653. return EFI_EXIT(EFI_UNSUPPORTED);
  654. }
  655. *image_handle = info;
  656. list_add_tail(&obj->link, &efi_obj_list);
  657. return EFI_EXIT(EFI_SUCCESS);
  658. }
  659. static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
  660. unsigned long *exit_data_size,
  661. s16 **exit_data)
  662. {
  663. ulong (*entry)(void *image_handle, struct efi_system_table *st);
  664. struct efi_loaded_image *info = image_handle;
  665. EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
  666. entry = info->reserved;
  667. efi_is_direct_boot = false;
  668. /* call the image! */
  669. if (setjmp(&info->exit_jmp)) {
  670. /* We returned from the child image */
  671. return EFI_EXIT(info->exit_status);
  672. }
  673. __efi_nesting_dec();
  674. __efi_exit_check();
  675. entry(image_handle, &systab);
  676. __efi_entry_check();
  677. __efi_nesting_inc();
  678. /* Should usually never get here */
  679. return EFI_EXIT(EFI_SUCCESS);
  680. }
  681. static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
  682. efi_status_t exit_status, unsigned long exit_data_size,
  683. int16_t *exit_data)
  684. {
  685. struct efi_loaded_image *loaded_image_info = (void*)image_handle;
  686. EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
  687. exit_data_size, exit_data);
  688. __efi_exit_check();
  689. loaded_image_info->exit_status = exit_status;
  690. longjmp(&loaded_image_info->exit_jmp, 1);
  691. panic("EFI application exited");
  692. }
  693. static struct efi_object *efi_search_obj(void *handle)
  694. {
  695. struct list_head *lhandle;
  696. list_for_each(lhandle, &efi_obj_list) {
  697. struct efi_object *efiobj;
  698. efiobj = list_entry(lhandle, struct efi_object, link);
  699. if (efiobj->handle == handle)
  700. return efiobj;
  701. }
  702. return NULL;
  703. }
  704. static efi_status_t EFIAPI efi_unload_image(void *image_handle)
  705. {
  706. struct efi_object *efiobj;
  707. EFI_ENTRY("%p", image_handle);
  708. efiobj = efi_search_obj(image_handle);
  709. if (efiobj)
  710. list_del(&efiobj->link);
  711. return EFI_EXIT(EFI_SUCCESS);
  712. }
  713. static void efi_exit_caches(void)
  714. {
  715. #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
  716. /*
  717. * Grub on 32bit ARM needs to have caches disabled before jumping into
  718. * a zImage, but does not know of all cache layers. Give it a hand.
  719. */
  720. if (efi_is_direct_boot)
  721. cleanup_before_linux();
  722. #endif
  723. }
  724. static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
  725. unsigned long map_key)
  726. {
  727. EFI_ENTRY("%p, %ld", image_handle, map_key);
  728. board_quiesce_devices();
  729. /* Fix up caches for EFI payloads if necessary */
  730. efi_exit_caches();
  731. /* This stops all lingering devices */
  732. bootm_disable_interrupts();
  733. /* Give the payload some time to boot */
  734. WATCHDOG_RESET();
  735. return EFI_EXIT(EFI_SUCCESS);
  736. }
  737. static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
  738. {
  739. static uint64_t mono = 0;
  740. EFI_ENTRY("%p", count);
  741. *count = mono++;
  742. return EFI_EXIT(EFI_SUCCESS);
  743. }
  744. static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
  745. {
  746. EFI_ENTRY("%ld", microseconds);
  747. udelay(microseconds);
  748. return EFI_EXIT(EFI_SUCCESS);
  749. }
  750. static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
  751. uint64_t watchdog_code,
  752. unsigned long data_size,
  753. uint16_t *watchdog_data)
  754. {
  755. EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
  756. data_size, watchdog_data);
  757. return efi_unsupported(__func__);
  758. }
  759. static efi_status_t EFIAPI efi_connect_controller(
  760. efi_handle_t controller_handle,
  761. efi_handle_t *driver_image_handle,
  762. struct efi_device_path *remain_device_path,
  763. bool recursive)
  764. {
  765. EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
  766. remain_device_path, recursive);
  767. return EFI_EXIT(EFI_NOT_FOUND);
  768. }
  769. static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
  770. void *driver_image_handle,
  771. void *child_handle)
  772. {
  773. EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
  774. child_handle);
  775. return EFI_EXIT(EFI_INVALID_PARAMETER);
  776. }
  777. static efi_status_t EFIAPI efi_close_protocol(void *handle,
  778. efi_guid_t *protocol,
  779. void *agent_handle,
  780. void *controller_handle)
  781. {
  782. EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
  783. controller_handle);
  784. return EFI_EXIT(EFI_NOT_FOUND);
  785. }
  786. static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
  787. efi_guid_t *protocol,
  788. struct efi_open_protocol_info_entry **entry_buffer,
  789. unsigned long *entry_count)
  790. {
  791. EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
  792. entry_count);
  793. return EFI_EXIT(EFI_NOT_FOUND);
  794. }
  795. static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
  796. efi_guid_t ***protocol_buffer,
  797. unsigned long *protocol_buffer_count)
  798. {
  799. unsigned long buffer_size;
  800. struct efi_object *efiobj;
  801. unsigned long i, j;
  802. struct list_head *lhandle;
  803. efi_status_t r;
  804. EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
  805. protocol_buffer_count);
  806. if (!handle || !protocol_buffer || !protocol_buffer_count)
  807. return EFI_EXIT(EFI_INVALID_PARAMETER);
  808. *protocol_buffer = NULL;
  809. *protocol_buffer_count = 0;
  810. list_for_each(lhandle, &efi_obj_list) {
  811. efiobj = list_entry(lhandle, struct efi_object, link);
  812. if (efiobj->handle != handle)
  813. continue;
  814. /* Count protocols */
  815. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  816. if (efiobj->protocols[i].guid)
  817. ++*protocol_buffer_count;
  818. }
  819. /* Copy guids */
  820. if (*protocol_buffer_count) {
  821. buffer_size = sizeof(efi_guid_t *) *
  822. *protocol_buffer_count;
  823. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
  824. buffer_size,
  825. (void **)protocol_buffer);
  826. if (r != EFI_SUCCESS)
  827. return EFI_EXIT(r);
  828. j = 0;
  829. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
  830. if (efiobj->protocols[i].guid) {
  831. (*protocol_buffer)[j] = (void *)
  832. efiobj->protocols[i].guid;
  833. ++j;
  834. }
  835. }
  836. }
  837. break;
  838. }
  839. return EFI_EXIT(EFI_SUCCESS);
  840. }
  841. static efi_status_t EFIAPI efi_locate_handle_buffer(
  842. enum efi_locate_search_type search_type,
  843. efi_guid_t *protocol, void *search_key,
  844. unsigned long *no_handles, efi_handle_t **buffer)
  845. {
  846. efi_status_t r;
  847. unsigned long buffer_size = 0;
  848. EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
  849. no_handles, buffer);
  850. if (!no_handles || !buffer) {
  851. r = EFI_INVALID_PARAMETER;
  852. goto out;
  853. }
  854. *no_handles = 0;
  855. *buffer = NULL;
  856. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  857. *buffer);
  858. if (r != EFI_BUFFER_TOO_SMALL)
  859. goto out;
  860. r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
  861. (void **)buffer);
  862. if (r != EFI_SUCCESS)
  863. goto out;
  864. r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
  865. *buffer);
  866. if (r == EFI_SUCCESS)
  867. *no_handles = buffer_size / sizeof(void *);
  868. out:
  869. return EFI_EXIT(r);
  870. }
  871. static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
  872. void *registration,
  873. void **protocol_interface)
  874. {
  875. struct list_head *lhandle;
  876. int i;
  877. EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
  878. if (!protocol || !protocol_interface)
  879. return EFI_EXIT(EFI_INVALID_PARAMETER);
  880. EFI_PRINT_GUID("protocol", protocol);
  881. list_for_each(lhandle, &efi_obj_list) {
  882. struct efi_object *efiobj;
  883. efiobj = list_entry(lhandle, struct efi_object, link);
  884. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  885. struct efi_handler *handler = &efiobj->protocols[i];
  886. if (!handler->guid)
  887. continue;
  888. if (!guidcmp(handler->guid, protocol)) {
  889. *protocol_interface =
  890. handler->protocol_interface;
  891. return EFI_EXIT(EFI_SUCCESS);
  892. }
  893. }
  894. }
  895. *protocol_interface = NULL;
  896. return EFI_EXIT(EFI_NOT_FOUND);
  897. }
  898. static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
  899. void **handle, ...)
  900. {
  901. EFI_ENTRY("%p", handle);
  902. va_list argptr;
  903. efi_guid_t *protocol;
  904. void *protocol_interface;
  905. efi_status_t r = EFI_SUCCESS;
  906. int i = 0;
  907. if (!handle)
  908. return EFI_EXIT(EFI_INVALID_PARAMETER);
  909. va_start(argptr, handle);
  910. for (;;) {
  911. protocol = va_arg(argptr, efi_guid_t*);
  912. if (!protocol)
  913. break;
  914. protocol_interface = va_arg(argptr, void*);
  915. r = efi_install_protocol_interface(handle, protocol,
  916. EFI_NATIVE_INTERFACE,
  917. protocol_interface);
  918. if (r != EFI_SUCCESS)
  919. break;
  920. i++;
  921. }
  922. va_end(argptr);
  923. if (r == EFI_SUCCESS)
  924. return EFI_EXIT(r);
  925. /* If an error occured undo all changes. */
  926. va_start(argptr, handle);
  927. for (; i; --i) {
  928. protocol = va_arg(argptr, efi_guid_t*);
  929. protocol_interface = va_arg(argptr, void*);
  930. efi_uninstall_protocol_interface(handle, protocol,
  931. protocol_interface);
  932. }
  933. va_end(argptr);
  934. return EFI_EXIT(r);
  935. }
  936. static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
  937. void *handle, ...)
  938. {
  939. EFI_ENTRY("%p", handle);
  940. return EFI_EXIT(EFI_INVALID_PARAMETER);
  941. }
  942. static efi_status_t EFIAPI efi_calculate_crc32(void *data,
  943. unsigned long data_size,
  944. uint32_t *crc32_p)
  945. {
  946. EFI_ENTRY("%p, %ld", data, data_size);
  947. *crc32_p = crc32(0, data, data_size);
  948. return EFI_EXIT(EFI_SUCCESS);
  949. }
  950. static void EFIAPI efi_copy_mem(void *destination, void *source,
  951. unsigned long length)
  952. {
  953. EFI_ENTRY("%p, %p, %ld", destination, source, length);
  954. memcpy(destination, source, length);
  955. }
  956. static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
  957. {
  958. EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
  959. memset(buffer, value, size);
  960. }
  961. static efi_status_t EFIAPI efi_open_protocol(
  962. void *handle, efi_guid_t *protocol,
  963. void **protocol_interface, void *agent_handle,
  964. void *controller_handle, uint32_t attributes)
  965. {
  966. struct list_head *lhandle;
  967. int i;
  968. efi_status_t r = EFI_INVALID_PARAMETER;
  969. EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
  970. protocol_interface, agent_handle, controller_handle,
  971. attributes);
  972. if (!handle || !protocol ||
  973. (!protocol_interface && attributes !=
  974. EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
  975. goto out;
  976. }
  977. EFI_PRINT_GUID("protocol", protocol);
  978. switch (attributes) {
  979. case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
  980. case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
  981. case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
  982. break;
  983. case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
  984. if (controller_handle == handle)
  985. goto out;
  986. case EFI_OPEN_PROTOCOL_BY_DRIVER:
  987. case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
  988. if (controller_handle == NULL)
  989. goto out;
  990. case EFI_OPEN_PROTOCOL_EXCLUSIVE:
  991. if (agent_handle == NULL)
  992. goto out;
  993. break;
  994. default:
  995. goto out;
  996. }
  997. list_for_each(lhandle, &efi_obj_list) {
  998. struct efi_object *efiobj;
  999. efiobj = list_entry(lhandle, struct efi_object, link);
  1000. if (efiobj->handle != handle)
  1001. continue;
  1002. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  1003. struct efi_handler *handler = &efiobj->protocols[i];
  1004. const efi_guid_t *hprotocol = handler->guid;
  1005. if (!hprotocol)
  1006. continue;
  1007. if (!guidcmp(hprotocol, protocol)) {
  1008. if (attributes !=
  1009. EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
  1010. *protocol_interface =
  1011. handler->protocol_interface;
  1012. }
  1013. r = EFI_SUCCESS;
  1014. goto out;
  1015. }
  1016. }
  1017. goto unsupported;
  1018. }
  1019. unsupported:
  1020. r = EFI_UNSUPPORTED;
  1021. out:
  1022. return EFI_EXIT(r);
  1023. }
  1024. static efi_status_t EFIAPI efi_handle_protocol(void *handle,
  1025. efi_guid_t *protocol,
  1026. void **protocol_interface)
  1027. {
  1028. return efi_open_protocol(handle, protocol, protocol_interface, NULL,
  1029. NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
  1030. }
  1031. static const struct efi_boot_services efi_boot_services = {
  1032. .hdr = {
  1033. .headersize = sizeof(struct efi_table_hdr),
  1034. },
  1035. .raise_tpl = efi_raise_tpl,
  1036. .restore_tpl = efi_restore_tpl,
  1037. .allocate_pages = efi_allocate_pages_ext,
  1038. .free_pages = efi_free_pages_ext,
  1039. .get_memory_map = efi_get_memory_map_ext,
  1040. .allocate_pool = efi_allocate_pool_ext,
  1041. .free_pool = efi_free_pool_ext,
  1042. .create_event = efi_create_event_ext,
  1043. .set_timer = efi_set_timer_ext,
  1044. .wait_for_event = efi_wait_for_event,
  1045. .signal_event = efi_signal_event_ext,
  1046. .close_event = efi_close_event,
  1047. .check_event = efi_check_event,
  1048. .install_protocol_interface = efi_install_protocol_interface_ext,
  1049. .reinstall_protocol_interface = efi_reinstall_protocol_interface,
  1050. .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
  1051. .handle_protocol = efi_handle_protocol,
  1052. .reserved = NULL,
  1053. .register_protocol_notify = efi_register_protocol_notify,
  1054. .locate_handle = efi_locate_handle_ext,
  1055. .locate_device_path = efi_locate_device_path,
  1056. .install_configuration_table = efi_install_configuration_table_ext,
  1057. .load_image = efi_load_image,
  1058. .start_image = efi_start_image,
  1059. .exit = efi_exit,
  1060. .unload_image = efi_unload_image,
  1061. .exit_boot_services = efi_exit_boot_services,
  1062. .get_next_monotonic_count = efi_get_next_monotonic_count,
  1063. .stall = efi_stall,
  1064. .set_watchdog_timer = efi_set_watchdog_timer,
  1065. .connect_controller = efi_connect_controller,
  1066. .disconnect_controller = efi_disconnect_controller,
  1067. .open_protocol = efi_open_protocol,
  1068. .close_protocol = efi_close_protocol,
  1069. .open_protocol_information = efi_open_protocol_information,
  1070. .protocols_per_handle = efi_protocols_per_handle,
  1071. .locate_handle_buffer = efi_locate_handle_buffer,
  1072. .locate_protocol = efi_locate_protocol,
  1073. .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
  1074. .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
  1075. .calculate_crc32 = efi_calculate_crc32,
  1076. .copy_mem = efi_copy_mem,
  1077. .set_mem = efi_set_mem,
  1078. };
  1079. static uint16_t __efi_runtime_data firmware_vendor[] =
  1080. { 'D','a','s',' ','U','-','b','o','o','t',0 };
  1081. struct efi_system_table __efi_runtime_data systab = {
  1082. .hdr = {
  1083. .signature = EFI_SYSTEM_TABLE_SIGNATURE,
  1084. .revision = 0x20005, /* 2.5 */
  1085. .headersize = sizeof(struct efi_table_hdr),
  1086. },
  1087. .fw_vendor = (long)firmware_vendor,
  1088. .con_in = (void*)&efi_con_in,
  1089. .con_out = (void*)&efi_con_out,
  1090. .std_err = (void*)&efi_con_out,
  1091. .runtime = (void*)&efi_runtime_services,
  1092. .boottime = (void*)&efi_boot_services,
  1093. .nr_tables = 0,
  1094. .tables = (void*)efi_conf_table,
  1095. };