efi_loader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 GUID
  63. */
  64. #define EFI_PRINT_GUID(txt, guid) ({ \
  65. debug("%sEFI: %s %pUl\n", __efi_nesting(), txt, guid); \
  66. })
  67. extern struct efi_runtime_services efi_runtime_services;
  68. extern struct efi_system_table systab;
  69. extern const struct efi_simple_text_output_protocol efi_con_out;
  70. extern struct efi_simple_input_interface efi_con_in;
  71. extern const struct efi_console_control_protocol efi_console_control;
  72. extern const struct efi_device_path_to_text_protocol efi_device_path_to_text;
  73. uint16_t *efi_dp_str(struct efi_device_path *dp);
  74. extern const efi_guid_t efi_global_variable_guid;
  75. extern const efi_guid_t efi_guid_console_control;
  76. extern const efi_guid_t efi_guid_device_path;
  77. extern const efi_guid_t efi_guid_loaded_image;
  78. extern const efi_guid_t efi_guid_device_path_to_text_protocol;
  79. extern const efi_guid_t efi_simple_file_system_protocol_guid;
  80. extern const efi_guid_t efi_file_info_guid;
  81. extern unsigned int __efi_runtime_start, __efi_runtime_stop;
  82. extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop;
  83. /*
  84. * When the UEFI payload wants to open a protocol on an object to get its
  85. * interface (usually a struct with callback functions), this struct maps the
  86. * protocol GUID to the respective protocol interface */
  87. struct efi_handler {
  88. const efi_guid_t *guid;
  89. void *protocol_interface;
  90. };
  91. /*
  92. * UEFI has a poor man's OO model where one "object" can be polymorphic and have
  93. * multiple different protocols (classes) attached to it.
  94. *
  95. * This struct is the parent struct for all of our actual implementation objects
  96. * that can include it to make themselves an EFI object
  97. */
  98. struct efi_object {
  99. /* Every UEFI object is part of a global object list */
  100. struct list_head link;
  101. /* We support up to 16 "protocols" an object can be accessed through */
  102. struct efi_handler protocols[16];
  103. /* The object spawner can either use this for data or as identifier */
  104. void *handle;
  105. };
  106. #define EFI_PROTOCOL_OBJECT(_guid, _protocol) (struct efi_object){ \
  107. .protocols = {{ \
  108. .guid = &(_guid), \
  109. .protocol_interface = (void *)(_protocol), \
  110. }}, \
  111. .handle = (void *)(_protocol), \
  112. }
  113. /**
  114. * struct efi_event
  115. *
  116. * @type: Type of event, see efi_create_event
  117. * @notify_tpl: Task priority level of notifications
  118. * @trigger_time: Period of the timer
  119. * @trigger_next: Next time to trigger the timer
  120. * @nofify_function: Function to call when the event is triggered
  121. * @notify_context: Data to be passed to the notify function
  122. * @trigger_type: Type of timer, see efi_set_timer
  123. * @queued: The notification function is queued
  124. * @signaled: The event occurred. The event is in the signaled state.
  125. */
  126. struct efi_event {
  127. uint32_t type;
  128. efi_uintn_t notify_tpl;
  129. void (EFIAPI *notify_function)(struct efi_event *event, void *context);
  130. void *notify_context;
  131. u64 trigger_next;
  132. u64 trigger_time;
  133. enum efi_timer_delay trigger_type;
  134. bool is_queued;
  135. bool is_signaled;
  136. };
  137. /* This list contains all UEFI objects we know of */
  138. extern struct list_head efi_obj_list;
  139. /* Called by bootefi to make console interface available */
  140. int efi_console_register(void);
  141. /* Called by bootefi to make all disk storage accessible as EFI objects */
  142. int efi_disk_register(void);
  143. /* Called by bootefi to make GOP (graphical) interface available */
  144. int efi_gop_register(void);
  145. /* Called by bootefi to make the network interface available */
  146. int efi_net_register(void);
  147. /* Called by bootefi to make the watchdog available */
  148. int efi_watchdog_register(void);
  149. /* Called by bootefi to make SMBIOS tables available */
  150. void efi_smbios_register(void);
  151. struct efi_simple_file_system_protocol *
  152. efi_fs_from_path(struct efi_device_path *fp);
  153. /* Called by networking code to memorize the dhcp ack package */
  154. void efi_net_set_dhcp_ack(void *pkt, int len);
  155. /* Called by efi_set_watchdog_timer to reset the timer */
  156. efi_status_t efi_set_watchdog(unsigned long timeout);
  157. /* Called from places to check whether a timer expired */
  158. void efi_timer_check(void);
  159. /* PE loader implementation */
  160. void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info);
  161. /* Called once to store the pristine gd pointer */
  162. void efi_save_gd(void);
  163. /* Special case handler for error/abort that just tries to dtrt to get
  164. * back to u-boot world */
  165. void efi_restore_gd(void);
  166. /* Call this to relocate the runtime section to an address space */
  167. void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map);
  168. /* Call this to set the current device name */
  169. void efi_set_bootdev(const char *dev, const char *devnr, const char *path);
  170. /* Create handle */
  171. efi_status_t efi_create_handle(void **handle);
  172. /* Call this to validate a handle and find the EFI object for it */
  173. struct efi_object *efi_search_obj(void *handle);
  174. /* Call this to create an event */
  175. efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
  176. void (EFIAPI *notify_function) (
  177. struct efi_event *event,
  178. void *context),
  179. void *notify_context, struct efi_event **event);
  180. /* Call this to set a timer */
  181. efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
  182. uint64_t trigger_time);
  183. /* Call this to signal an event */
  184. void efi_signal_event(struct efi_event *event);
  185. /* open file system: */
  186. struct efi_simple_file_system_protocol *efi_simple_file_system(
  187. struct blk_desc *desc, int part, struct efi_device_path *dp);
  188. /* open file from device-path: */
  189. struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp);
  190. /* Generic EFI memory allocator, call this to get memory */
  191. void *efi_alloc(uint64_t len, int memory_type);
  192. /* More specific EFI memory allocator, called by EFI payloads */
  193. efi_status_t efi_allocate_pages(int type, int memory_type, efi_uintn_t pages,
  194. uint64_t *memory);
  195. /* EFI memory free function. */
  196. efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages);
  197. /* EFI memory allocator for small allocations */
  198. efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size,
  199. void **buffer);
  200. /* EFI pool memory free function. */
  201. efi_status_t efi_free_pool(void *buffer);
  202. /* Returns the EFI memory map */
  203. efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
  204. struct efi_mem_desc *memory_map,
  205. efi_uintn_t *map_key,
  206. efi_uintn_t *descriptor_size,
  207. uint32_t *descriptor_version);
  208. /* Adds a range into the EFI memory map */
  209. uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
  210. bool overlap_only_ram);
  211. /* Called by board init to initialize the EFI memory map */
  212. int efi_memory_init(void);
  213. /* Adds new or overrides configuration table entry to the system table */
  214. efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table);
  215. void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
  216. struct efi_device_path *device_path,
  217. struct efi_device_path *file_path);
  218. efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
  219. void **buffer);
  220. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  221. extern void *efi_bounce_buffer;
  222. #define EFI_LOADER_BOUNCE_BUFFER_SIZE (64 * 1024 * 1024)
  223. #endif
  224. struct efi_device_path *efi_dp_next(const struct efi_device_path *dp);
  225. int efi_dp_match(const struct efi_device_path *a,
  226. const struct efi_device_path *b);
  227. struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
  228. struct efi_device_path **rem);
  229. unsigned efi_dp_size(const struct efi_device_path *dp);
  230. struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp);
  231. struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
  232. const struct efi_device_path *dp2);
  233. struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
  234. const struct efi_device_path *node);
  235. struct efi_device_path *efi_dp_from_dev(struct udevice *dev);
  236. struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part);
  237. struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
  238. const char *path);
  239. struct efi_device_path *efi_dp_from_eth(void);
  240. struct efi_device_path *efi_dp_from_mem(uint32_t mem_type,
  241. uint64_t start_address,
  242. uint64_t end_address);
  243. void efi_dp_split_file_path(struct efi_device_path *full_path,
  244. struct efi_device_path **device_path,
  245. struct efi_device_path **file_path);
  246. #define EFI_DP_TYPE(_dp, _type, _subtype) \
  247. (((_dp)->type == DEVICE_PATH_TYPE_##_type) && \
  248. ((_dp)->sub_type == DEVICE_PATH_SUB_TYPE_##_subtype))
  249. /* Convert strings from normal C strings to uEFI strings */
  250. static inline void ascii2unicode(u16 *unicode, const char *ascii)
  251. {
  252. while (*ascii)
  253. *(unicode++) = *(ascii++);
  254. }
  255. static inline int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
  256. {
  257. return memcmp(g1, g2, sizeof(efi_guid_t));
  258. }
  259. /*
  260. * Use these to indicate that your code / data should go into the EFI runtime
  261. * section and thus still be available when the OS is running
  262. */
  263. #define __efi_runtime_data __attribute__ ((section ("efi_runtime_data")))
  264. #define __efi_runtime __attribute__ ((section ("efi_runtime_text")))
  265. /* Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region
  266. * to make it available at runtime */
  267. void efi_add_runtime_mmio(void *mmio_ptr, u64 len);
  268. /* Boards may provide the functions below to implement RTS functionality */
  269. void __efi_runtime EFIAPI efi_reset_system(
  270. enum efi_reset_type reset_type,
  271. efi_status_t reset_status,
  272. unsigned long data_size, void *reset_data);
  273. void efi_reset_system_init(void);
  274. efi_status_t __efi_runtime EFIAPI efi_get_time(
  275. struct efi_time *time,
  276. struct efi_time_cap *capabilities);
  277. void efi_get_time_init(void);
  278. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  279. /*
  280. * Entry point for the tests of the EFI API.
  281. * It is called by 'bootefi selftest'
  282. */
  283. efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
  284. struct efi_system_table *systab);
  285. #endif
  286. efi_status_t EFIAPI efi_get_variable(s16 *variable_name,
  287. efi_guid_t *vendor, u32 *attributes,
  288. unsigned long *data_size, void *data);
  289. efi_status_t EFIAPI efi_get_next_variable(
  290. unsigned long *variable_name_size,
  291. s16 *variable_name, efi_guid_t *vendor);
  292. efi_status_t EFIAPI efi_set_variable(s16 *variable_name,
  293. efi_guid_t *vendor, u32 attributes,
  294. unsigned long data_size, void *data);
  295. void *efi_bootmgr_load(struct efi_device_path **device_path,
  296. struct efi_device_path **file_path);
  297. #else /* defined(EFI_LOADER) && !defined(CONFIG_SPL_BUILD) */
  298. /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */
  299. #define __efi_runtime_data
  300. #define __efi_runtime
  301. static inline void efi_add_runtime_mmio(void *mmio_ptr, u64 len) { }
  302. /* No loader configured, stub out EFI_ENTRY */
  303. static inline void efi_restore_gd(void) { }
  304. static inline void efi_set_bootdev(const char *dev, const char *devnr,
  305. const char *path) { }
  306. static inline void efi_net_set_dhcp_ack(void *pkt, int len) { }
  307. #endif /* CONFIG_EFI_LOADER && !CONFIG_SPL_BUILD */
  308. #endif /* _EFI_LOADER_H */