efi_boottime.c 31 KB

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