efi_loader.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * EFI application loader
  4. *
  5. * Copyright (c) 2016 Alexander Graf
  6. */
  7. #ifndef _EFI_LOADER_H
  8. #define _EFI_LOADER_H 1
  9. #include <common.h>
  10. #include <part_efi.h>
  11. #include <efi_api.h>
  12. /* No need for efi loader support in SPL */
  13. #if CONFIG_IS_ENABLED(EFI_LOADER)
  14. #include <linux/list.h>
  15. /* Maximum number of configuration tables */
  16. #define EFI_MAX_CONFIGURATION_TABLES 16
  17. /* GUID used by the root node */
  18. #define U_BOOT_GUID \
  19. EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
  20. 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
  21. int __efi_entry_check(void);
  22. int __efi_exit_check(void);
  23. const char *__efi_nesting(void);
  24. const char *__efi_nesting_inc(void);
  25. const char *__efi_nesting_dec(void);
  26. /*
  27. * Enter the u-boot world from UEFI:
  28. */
  29. #define EFI_ENTRY(format, ...) do { \
  30. assert(__efi_entry_check()); \
  31. debug("%sEFI: Entry %s(" format ")\n", __efi_nesting_inc(), \
  32. __func__, ##__VA_ARGS__); \
  33. } while(0)
  34. /*
  35. * Exit the u-boot world back to UEFI:
  36. */
  37. #define EFI_EXIT(ret) ({ \
  38. typeof(ret) _r = ret; \
  39. debug("%sEFI: Exit: %s: %u\n", __efi_nesting_dec(), \
  40. __func__, (u32)((uintptr_t) _r & ~EFI_ERROR_MASK)); \
  41. assert(__efi_exit_check()); \
  42. _r; \
  43. })
  44. /*
  45. * Call non-void UEFI function from u-boot and retrieve return value:
  46. */
  47. #define EFI_CALL(exp) ({ \
  48. debug("%sEFI: Call: %s\n", __efi_nesting_inc(), #exp); \
  49. assert(__efi_exit_check()); \
  50. typeof(exp) _r = exp; \
  51. assert(__efi_entry_check()); \
  52. debug("%sEFI: %lu returned by %s\n", __efi_nesting_dec(), \
  53. (unsigned long)((uintptr_t)_r & ~EFI_ERROR_MASK), #exp); \
  54. _r; \
  55. })
  56. /*
  57. * Call void UEFI function from u-boot:
  58. */
  59. #define EFI_CALL_VOID(exp) do { \
  60. debug("%sEFI: Call: %s\n", __efi_nesting_inc(), #exp); \
  61. assert(__efi_exit_check()); \
  62. exp; \
  63. assert(__efi_entry_check()); \
  64. debug("%sEFI: Return From: %s\n", __efi_nesting_dec(), #exp); \
  65. } while(0)
  66. /*
  67. * Write an indented message with EFI prefix
  68. */
  69. #define EFI_PRINT(format, ...) ({ \
  70. debug("%sEFI: " format, __efi_nesting(), \
  71. ##__VA_ARGS__); \
  72. })
  73. #ifdef CONFIG_SYS_CACHELINE_SIZE
  74. #define EFI_CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE
  75. #else
  76. /* Just use the greatest cache flush alignment requirement I'm aware of */
  77. #define EFI_CACHELINE_SIZE 128
  78. #endif
  79. /* Key identifying current memory map */
  80. extern efi_uintn_t efi_memory_map_key;
  81. extern struct efi_runtime_services efi_runtime_services;
  82. extern struct efi_system_table systab;
  83. extern struct efi_simple_text_output_protocol efi_con_out;
  84. extern struct efi_simple_text_input_protocol efi_con_in;
  85. extern struct efi_console_control_protocol efi_console_control;
  86. extern const struct efi_device_path_to_text_protocol efi_device_path_to_text;
  87. /* implementation of the EFI_DEVICE_PATH_UTILITIES_PROTOCOL */
  88. extern const struct efi_device_path_utilities_protocol
  89. efi_device_path_utilities;
  90. /* Implementation of the EFI_UNICODE_COLLATION_PROTOCOL */
  91. extern const struct efi_unicode_collation_protocol
  92. efi_unicode_collation_protocol;
  93. uint16_t *efi_dp_str(struct efi_device_path *dp);
  94. /* GUID of the U-Boot root node */
  95. extern const efi_guid_t efi_u_boot_guid;
  96. /* GUID of the EFI_BLOCK_IO_PROTOCOL */
  97. extern const efi_guid_t efi_block_io_guid;
  98. extern const efi_guid_t efi_global_variable_guid;
  99. extern const efi_guid_t efi_guid_console_control;
  100. extern const efi_guid_t efi_guid_device_path;
  101. /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
  102. extern const efi_guid_t efi_guid_driver_binding_protocol;
  103. /* event group ExitBootServices() invoked */
  104. extern const efi_guid_t efi_guid_event_group_exit_boot_services;
  105. /* event group SetVirtualAddressMap() invoked */
  106. extern const efi_guid_t efi_guid_event_group_virtual_address_change;
  107. /* event group memory map changed */
  108. extern const efi_guid_t efi_guid_event_group_memory_map_change;
  109. /* event group boot manager about to boot */
  110. extern const efi_guid_t efi_guid_event_group_ready_to_boot;
  111. /* event group ResetSystem() invoked (before ExitBootServices) */
  112. extern const efi_guid_t efi_guid_event_group_reset_system;
  113. /* GUID of the device tree table */
  114. extern const efi_guid_t efi_guid_fdt;
  115. extern const efi_guid_t efi_guid_loaded_image;
  116. extern const efi_guid_t efi_guid_device_path_to_text_protocol;
  117. extern const efi_guid_t efi_simple_file_system_protocol_guid;
  118. extern const efi_guid_t efi_file_info_guid;
  119. /* GUID for file system information */
  120. extern const efi_guid_t efi_file_system_info_guid;
  121. extern const efi_guid_t efi_guid_device_path_utilities_protocol;
  122. /* GUID of the Unicode collation protocol */
  123. extern const efi_guid_t efi_guid_unicode_collation_protocol;
  124. extern unsigned int __efi_runtime_start, __efi_runtime_stop;
  125. extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop;
  126. /*
  127. * When a protocol is opened a open protocol info entry is created.
  128. * These are maintained in a list.
  129. */
  130. struct efi_open_protocol_info_item {
  131. /* Link to the list of open protocol info entries of a protocol */
  132. struct list_head link;
  133. struct efi_open_protocol_info_entry info;
  134. };
  135. /*
  136. * When the UEFI payload wants to open a protocol on an object to get its
  137. * interface (usually a struct with callback functions), this struct maps the
  138. * protocol GUID to the respective protocol interface
  139. */
  140. struct efi_handler {
  141. /* Link to the list of protocols of a handle */
  142. struct list_head link;
  143. const efi_guid_t *guid;
  144. void *protocol_interface;
  145. /* Link to the list of open protocol info items */
  146. struct list_head open_infos;
  147. };
  148. /*
  149. * UEFI has a poor man's OO model where one "object" can be polymorphic and have
  150. * multiple different protocols (classes) attached to it.
  151. *
  152. * This struct is the parent struct for all of our actual implementation objects
  153. * that can include it to make themselves an EFI object
  154. */
  155. struct efi_object {
  156. /* Every UEFI object is part of a global object list */
  157. struct list_head link;
  158. /* The list of protocols */
  159. struct list_head protocols;
  160. /* The object spawner can either use this for data or as identifier */
  161. void *handle;
  162. };
  163. /**
  164. * struct efi_event
  165. *
  166. * @link: Link to list of all events
  167. * @type: Type of event, see efi_create_event
  168. * @notify_tpl: Task priority level of notifications
  169. * @nofify_function: Function to call when the event is triggered
  170. * @notify_context: Data to be passed to the notify function
  171. * @group: Event group
  172. * @trigger_time: Period of the timer
  173. * @trigger_next: Next time to trigger the timer
  174. * @trigger_type: Type of timer, see efi_set_timer
  175. * @is_queued: The notification function is queued
  176. * @is_signaled: The event occurred. The event is in the signaled state.
  177. */
  178. struct efi_event {
  179. struct list_head link;
  180. uint32_t type;
  181. efi_uintn_t notify_tpl;
  182. void (EFIAPI *notify_function)(struct efi_event *event, void *context);
  183. void *notify_context;
  184. const efi_guid_t *group;
  185. u64 trigger_next;
  186. u64 trigger_time;
  187. enum efi_timer_delay trigger_type;
  188. bool is_queued;
  189. bool is_signaled;
  190. };
  191. /* This list contains all UEFI objects we know of */
  192. extern struct list_head efi_obj_list;
  193. /* List of all events */
  194. extern struct list_head efi_events;
  195. /* Called by bootefi to initialize root node */
  196. efi_status_t efi_root_node_register(void);
  197. /* Called by bootefi to initialize runtime */
  198. efi_status_t efi_initialize_system_table(void);
  199. /* Called by bootefi to make console interface available */
  200. int efi_console_register(void);
  201. /* Called by bootefi to make all disk storage accessible as EFI objects */
  202. efi_status_t efi_disk_register(void);
  203. /* Create handles and protocols for the partitions of a block device */
  204. int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
  205. const char *if_typename, int diskid,
  206. const char *pdevname);
  207. /* Called by bootefi to make GOP (graphical) interface available */
  208. efi_status_t efi_gop_register(void);
  209. /* Called by bootefi to make the network interface available */
  210. efi_status_t efi_net_register(void);
  211. /* Called by bootefi to make the watchdog available */
  212. efi_status_t efi_watchdog_register(void);
  213. /* Called by bootefi to make SMBIOS tables available */
  214. /**
  215. * efi_acpi_register() - write out ACPI tables
  216. *
  217. * Called by bootefi to make ACPI tables available
  218. *
  219. * @return 0 if OK, -ENOMEM if no memory is available for the tables
  220. */
  221. efi_status_t efi_acpi_register(void);
  222. /**
  223. * efi_smbios_register() - write out SMBIOS tables
  224. *
  225. * Called by bootefi to make SMBIOS tables available
  226. *
  227. * @return 0 if OK, -ENOMEM if no memory is available for the tables
  228. */
  229. efi_status_t efi_smbios_register(void);
  230. struct efi_simple_file_system_protocol *
  231. efi_fs_from_path(struct efi_device_path *fp);
  232. /* Called by networking code to memorize the dhcp ack package */
  233. void efi_net_set_dhcp_ack(void *pkt, int len);
  234. /* Called by efi_set_watchdog_timer to reset the timer */
  235. efi_status_t efi_set_watchdog(unsigned long timeout);
  236. /* Called from places to check whether a timer expired */
  237. void efi_timer_check(void);
  238. /* PE loader implementation */
  239. void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info);
  240. /* Called once to store the pristine gd pointer */
  241. void efi_save_gd(void);
  242. /* Special case handler for error/abort that just tries to dtrt to get
  243. * back to u-boot world */
  244. void efi_restore_gd(void);
  245. /* Call this to relocate the runtime section to an address space */
  246. void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map);
  247. /* Call this to set the current device name */
  248. void efi_set_bootdev(const char *dev, const char *devnr, const char *path);
  249. /* Add a new object to the object list. */
  250. void efi_add_handle(struct efi_object *obj);
  251. /* Create handle */
  252. efi_status_t efi_create_handle(efi_handle_t *handle);
  253. /* Delete handle */
  254. void efi_delete_handle(struct efi_object *obj);
  255. /* Call this to validate a handle and find the EFI object for it */
  256. struct efi_object *efi_search_obj(const efi_handle_t handle);
  257. /* Find a protocol on a handle */
  258. efi_status_t efi_search_protocol(const efi_handle_t handle,
  259. const efi_guid_t *protocol_guid,
  260. struct efi_handler **handler);
  261. /* Install new protocol on a handle */
  262. efi_status_t efi_add_protocol(const efi_handle_t handle,
  263. const efi_guid_t *protocol,
  264. void *protocol_interface);
  265. /* Delete protocol from a handle */
  266. efi_status_t efi_remove_protocol(const efi_handle_t handle,
  267. const efi_guid_t *protocol,
  268. void *protocol_interface);
  269. /* Delete all protocols from a handle */
  270. efi_status_t efi_remove_all_protocols(const efi_handle_t handle);
  271. /* Call this to create an event */
  272. efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
  273. void (EFIAPI *notify_function) (
  274. struct efi_event *event,
  275. void *context),
  276. void *notify_context, efi_guid_t *group,
  277. struct efi_event **event);
  278. /* Call this to set a timer */
  279. efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
  280. uint64_t trigger_time);
  281. /* Call this to signal an event */
  282. void efi_signal_event(struct efi_event *event, bool check_tpl);
  283. /* open file system: */
  284. struct efi_simple_file_system_protocol *efi_simple_file_system(
  285. struct blk_desc *desc, int part, struct efi_device_path *dp);
  286. /* open file from device-path: */
  287. struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp);
  288. /* Generic EFI memory allocator, call this to get memory */
  289. void *efi_alloc(uint64_t len, int memory_type);
  290. /* More specific EFI memory allocator, called by EFI payloads */
  291. efi_status_t efi_allocate_pages(int type, int memory_type, efi_uintn_t pages,
  292. uint64_t *memory);
  293. /* EFI memory free function. */
  294. efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages);
  295. /* EFI memory allocator for small allocations */
  296. efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size,
  297. void **buffer);
  298. /* EFI pool memory free function. */
  299. efi_status_t efi_free_pool(void *buffer);
  300. /* Returns the EFI memory map */
  301. efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
  302. struct efi_mem_desc *memory_map,
  303. efi_uintn_t *map_key,
  304. efi_uintn_t *descriptor_size,
  305. uint32_t *descriptor_version);
  306. /* Adds a range into the EFI memory map */
  307. uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
  308. bool overlap_only_ram);
  309. /* Called by board init to initialize the EFI drivers */
  310. efi_status_t efi_driver_init(void);
  311. /* Called by board init to initialize the EFI memory map */
  312. int efi_memory_init(void);
  313. /* Adds new or overrides configuration table entry to the system table */
  314. efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table);
  315. /* Sets up a loaded image */
  316. efi_status_t efi_setup_loaded_image(
  317. struct efi_loaded_image *info, struct efi_object *obj,
  318. struct efi_device_path *device_path,
  319. struct efi_device_path *file_path);
  320. efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
  321. void **buffer);
  322. /* Print information about a loaded image */
  323. efi_status_t efi_print_image_info(struct efi_loaded_image *image, void *pc);
  324. /* Print information about all loaded images */
  325. void efi_print_image_infos(void *pc);
  326. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  327. extern void *efi_bounce_buffer;
  328. #define EFI_LOADER_BOUNCE_BUFFER_SIZE (64 * 1024 * 1024)
  329. #endif
  330. struct efi_device_path *efi_dp_next(const struct efi_device_path *dp);
  331. int efi_dp_match(const struct efi_device_path *a,
  332. const struct efi_device_path *b);
  333. struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
  334. struct efi_device_path **rem);
  335. /* get size of the first device path instance excluding end node */
  336. efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp);
  337. /* size of multi-instance device path excluding end node */
  338. efi_uintn_t efi_dp_size(const struct efi_device_path *dp);
  339. struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp);
  340. struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
  341. const struct efi_device_path *dp2);
  342. struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
  343. const struct efi_device_path *node);
  344. /* Create a device path node of given type, sub-type, length */
  345. struct efi_device_path *efi_dp_create_device_node(const u8 type,
  346. const u8 sub_type,
  347. const u16 length);
  348. /* Append device path instance */
  349. struct efi_device_path *efi_dp_append_instance(
  350. const struct efi_device_path *dp,
  351. const struct efi_device_path *dpi);
  352. /* Get next device path instance */
  353. struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
  354. efi_uintn_t *size);
  355. /* Check if a device path contains muliple instances */
  356. bool efi_dp_is_multi_instance(const struct efi_device_path *dp);
  357. struct efi_device_path *efi_dp_from_dev(struct udevice *dev);
  358. struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part);
  359. /* Create a device node for a block device partition. */
  360. struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part);
  361. struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
  362. const char *path);
  363. struct efi_device_path *efi_dp_from_eth(void);
  364. struct efi_device_path *efi_dp_from_mem(uint32_t mem_type,
  365. uint64_t start_address,
  366. uint64_t end_address);
  367. /* Determine the last device path node that is not the end node. */
  368. const struct efi_device_path *efi_dp_last_node(
  369. const struct efi_device_path *dp);
  370. efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
  371. struct efi_device_path **device_path,
  372. struct efi_device_path **file_path);
  373. #define EFI_DP_TYPE(_dp, _type, _subtype) \
  374. (((_dp)->type == DEVICE_PATH_TYPE_##_type) && \
  375. ((_dp)->sub_type == DEVICE_PATH_SUB_TYPE_##_subtype))
  376. /**
  377. * ascii2unicode() - convert ASCII string to UTF-16 string
  378. *
  379. * A zero terminated ASCII string is converted to a zero terminated UTF-16
  380. * string. The output buffer must be preassigned.
  381. *
  382. * @unicode: preassigned output buffer for UTF-16 string
  383. * @ascii: ASCII string to be converted
  384. */
  385. static inline void ascii2unicode(u16 *unicode, const char *ascii)
  386. {
  387. while (*ascii)
  388. *(unicode++) = *(ascii++);
  389. *unicode = 0;
  390. }
  391. static inline int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
  392. {
  393. return memcmp(g1, g2, sizeof(efi_guid_t));
  394. }
  395. /*
  396. * Use these to indicate that your code / data should go into the EFI runtime
  397. * section and thus still be available when the OS is running
  398. */
  399. #define __efi_runtime_data __attribute__ ((section (".data.efi_runtime")))
  400. #define __efi_runtime __attribute__ ((section (".text.efi_runtime")))
  401. /* Update CRC32 in table header */
  402. void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table);
  403. /* Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region
  404. * to make it available at runtime */
  405. efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len);
  406. /* Boards may provide the functions below to implement RTS functionality */
  407. void __efi_runtime EFIAPI efi_reset_system(
  408. enum efi_reset_type reset_type,
  409. efi_status_t reset_status,
  410. unsigned long data_size, void *reset_data);
  411. /* Architecture specific initialization of the EFI subsystem */
  412. efi_status_t efi_reset_system_init(void);
  413. efi_status_t __efi_runtime EFIAPI efi_get_time(
  414. struct efi_time *time,
  415. struct efi_time_cap *capabilities);
  416. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  417. /*
  418. * Entry point for the tests of the EFI API.
  419. * It is called by 'bootefi selftest'
  420. */
  421. efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
  422. struct efi_system_table *systab);
  423. #endif
  424. efi_status_t EFIAPI efi_get_variable(u16 *variable_name, efi_guid_t *vendor,
  425. u32 *attributes, efi_uintn_t *data_size,
  426. void *data);
  427. efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
  428. u16 *variable_name,
  429. efi_guid_t *vendor);
  430. efi_status_t EFIAPI efi_set_variable(u16 *variable_name, efi_guid_t *vendor,
  431. u32 attributes, efi_uintn_t data_size,
  432. void *data);
  433. void *efi_bootmgr_load(struct efi_device_path **device_path,
  434. struct efi_device_path **file_path);
  435. #else /* CONFIG_IS_ENABLED(EFI_LOADER) */
  436. /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */
  437. #define __efi_runtime_data
  438. #define __efi_runtime
  439. static inline efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
  440. {
  441. return EFI_SUCCESS;
  442. }
  443. /* No loader configured, stub out EFI_ENTRY */
  444. static inline void efi_restore_gd(void) { }
  445. static inline void efi_set_bootdev(const char *dev, const char *devnr,
  446. const char *path) { }
  447. static inline void efi_net_set_dhcp_ack(void *pkt, int len) { }
  448. static inline void efi_print_image_infos(void *pc) { }
  449. #endif /* CONFIG_IS_ENABLED(EFI_LOADER) */
  450. #endif /* _EFI_LOADER_H */