efi_boottime.c 26 KB

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