efi_boottime.c 29 KB

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