efi_loader.h 16 KB

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