efi_loader.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * EFI application loader
  3. *
  4. * Copyright (c) 2016 Alexander Graf
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <part_efi.h>
  10. #include <efi_api.h>
  11. /* No need for efi loader support in SPL */
  12. #if defined(CONFIG_EFI_LOADER) && !defined(CONFIG_SPL_BUILD)
  13. #include <linux/list.h>
  14. int __efi_entry_check(void);
  15. int __efi_exit_check(void);
  16. const char *__efi_nesting(void);
  17. const char *__efi_nesting_inc(void);
  18. const char *__efi_nesting_dec(void);
  19. /*
  20. * Enter the u-boot world from UEFI:
  21. */
  22. #define EFI_ENTRY(format, ...) do { \
  23. assert(__efi_entry_check()); \
  24. debug("%sEFI: Entry %s(" format ")\n", __efi_nesting_inc(), \
  25. __func__, ##__VA_ARGS__); \
  26. } while(0)
  27. /*
  28. * Exit the u-boot world back to UEFI:
  29. */
  30. #define EFI_EXIT(ret) ({ \
  31. typeof(ret) _r = ret; \
  32. debug("%sEFI: Exit: %s: %u\n", __efi_nesting_dec(), \
  33. __func__, (u32)((uintptr_t) _r & ~EFI_ERROR_MASK)); \
  34. assert(__efi_exit_check()); \
  35. _r; \
  36. })
  37. /*
  38. * Call non-void UEFI function from u-boot and retrieve return value:
  39. */
  40. #define EFI_CALL(exp) ({ \
  41. debug("%sEFI: Call: %s\n", __efi_nesting_inc(), #exp); \
  42. assert(__efi_exit_check()); \
  43. typeof(exp) _r = exp; \
  44. assert(__efi_entry_check()); \
  45. debug("%sEFI: %lu returned by %s\n", __efi_nesting_dec(), \
  46. (unsigned long)((uintptr_t)_r & ~EFI_ERROR_MASK), #exp); \
  47. _r; \
  48. })
  49. /*
  50. * Call void UEFI function from u-boot:
  51. */
  52. #define EFI_CALL_VOID(exp) do { \
  53. debug("%sEFI: Call: %s\n", __efi_nesting_inc(), #exp); \
  54. assert(__efi_exit_check()); \
  55. exp; \
  56. assert(__efi_entry_check()); \
  57. debug("%sEFI: Return From: %s\n", __efi_nesting_dec(), #exp); \
  58. } while(0)
  59. /*
  60. * Write GUID
  61. */
  62. #define EFI_PRINT_GUID(txt, guid) ({ \
  63. debug("%sEFI: %s %pUl\n", __efi_nesting(), txt, guid); \
  64. })
  65. extern struct efi_runtime_services efi_runtime_services;
  66. extern struct efi_system_table systab;
  67. extern const struct efi_simple_text_output_protocol efi_con_out;
  68. extern struct efi_simple_input_interface efi_con_in;
  69. extern const struct efi_console_control_protocol efi_console_control;
  70. extern const struct efi_device_path_to_text_protocol efi_device_path_to_text;
  71. uint16_t *efi_dp_str(struct efi_device_path *dp);
  72. extern const efi_guid_t efi_guid_console_control;
  73. extern const efi_guid_t efi_guid_device_path;
  74. extern const efi_guid_t efi_guid_loaded_image;
  75. extern const efi_guid_t efi_guid_device_path_to_text_protocol;
  76. extern unsigned int __efi_runtime_start, __efi_runtime_stop;
  77. extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop;
  78. /*
  79. * When the UEFI payload wants to open a protocol on an object to get its
  80. * interface (usually a struct with callback functions), this struct maps the
  81. * protocol GUID to the respective protocol interface */
  82. struct efi_handler {
  83. const efi_guid_t *guid;
  84. void *protocol_interface;
  85. };
  86. /*
  87. * UEFI has a poor man's OO model where one "object" can be polymorphic and have
  88. * multiple different protocols (classes) attached to it.
  89. *
  90. * This struct is the parent struct for all of our actual implementation objects
  91. * that can include it to make themselves an EFI object
  92. */
  93. struct efi_object {
  94. /* Every UEFI object is part of a global object list */
  95. struct list_head link;
  96. /* We support up to 8 "protocols" an object can be accessed through */
  97. struct efi_handler protocols[8];
  98. /* The object spawner can either use this for data or as identifier */
  99. void *handle;
  100. };
  101. #define EFI_PROTOCOL_OBJECT(_guid, _protocol) (struct efi_object){ \
  102. .protocols = {{ \
  103. .guid = &(_guid), \
  104. .protocol_interface = (void *)(_protocol), \
  105. }}, \
  106. .handle = (void *)(_protocol), \
  107. }
  108. /**
  109. * struct efi_event
  110. *
  111. * @type: Type of event, see efi_create_event
  112. * @notify_tpl: Task priority level of notifications
  113. * @trigger_time: Period of the timer
  114. * @trigger_next: Next time to trigger the timer
  115. * @nofify_function: Function to call when the event is triggered
  116. * @notify_context: Data to be passed to the notify function
  117. * @trigger_type: Type of timer, see efi_set_timer
  118. * @queued: The notification functionis queued
  119. * @signaled: The event occured
  120. */
  121. struct efi_event {
  122. uint32_t type;
  123. UINTN notify_tpl;
  124. void (EFIAPI *notify_function)(struct efi_event *event, void *context);
  125. void *notify_context;
  126. u64 trigger_next;
  127. u64 trigger_time;
  128. enum efi_timer_delay trigger_type;
  129. int queued;
  130. int signaled;
  131. };
  132. /* This list contains all UEFI objects we know of */
  133. extern struct list_head efi_obj_list;
  134. /* Called by bootefi to make console interface available */
  135. int efi_console_register(void);
  136. /* Called by bootefi to make all disk storage accessible as EFI objects */
  137. int efi_disk_register(void);
  138. /* Called by bootefi to make GOP (graphical) interface available */
  139. int efi_gop_register(void);
  140. /* Called by bootefi to make the network interface available */
  141. int efi_net_register(void);
  142. /* Called by bootefi to make SMBIOS tables available */
  143. void efi_smbios_register(void);
  144. /* Called by networking code to memorize the dhcp ack package */
  145. void efi_net_set_dhcp_ack(void *pkt, int len);
  146. /* Called from places to check whether a timer expired */
  147. void efi_timer_check(void);
  148. /* PE loader implementation */
  149. void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info);
  150. /* Called once to store the pristine gd pointer */
  151. void efi_save_gd(void);
  152. /* Special case handler for error/abort that just tries to dtrt to get
  153. * back to u-boot world */
  154. void efi_restore_gd(void);
  155. /* Call this to relocate the runtime section to an address space */
  156. void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map);
  157. /* Call this to set the current device name */
  158. void efi_set_bootdev(const char *dev, const char *devnr, const char *path);
  159. /* Call this to create an event */
  160. efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
  161. void (EFIAPI *notify_function) (
  162. struct efi_event *event,
  163. void *context),
  164. void *notify_context, struct efi_event **event);
  165. /* Call this to set a timer */
  166. efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
  167. uint64_t trigger_time);
  168. /* Call this to signal an event */
  169. void efi_signal_event(struct efi_event *event);
  170. /* Generic EFI memory allocator, call this to get memory */
  171. void *efi_alloc(uint64_t len, int memory_type);
  172. /* More specific EFI memory allocator, called by EFI payloads */
  173. efi_status_t efi_allocate_pages(int type, int memory_type, unsigned long pages,
  174. uint64_t *memory);
  175. /* EFI memory free function. */
  176. efi_status_t efi_free_pages(uint64_t memory, unsigned long pages);
  177. /* EFI memory allocator for small allocations */
  178. efi_status_t efi_allocate_pool(int pool_type, unsigned long size,
  179. void **buffer);
  180. /* EFI pool memory free function. */
  181. efi_status_t efi_free_pool(void *buffer);
  182. /* Returns the EFI memory map */
  183. efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
  184. struct efi_mem_desc *memory_map,
  185. unsigned long *map_key,
  186. unsigned long *descriptor_size,
  187. uint32_t *descriptor_version);
  188. /* Adds a range into the EFI memory map */
  189. uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
  190. bool overlap_only_ram);
  191. /* Called by board init to initialize the EFI memory map */
  192. int efi_memory_init(void);
  193. /* Adds new or overrides configuration table entry to the system table */
  194. efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table);
  195. void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
  196. struct efi_device_path *device_path,
  197. struct efi_device_path *file_path);
  198. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  199. extern void *efi_bounce_buffer;
  200. #define EFI_LOADER_BOUNCE_BUFFER_SIZE (64 * 1024 * 1024)
  201. #endif
  202. struct efi_device_path *efi_dp_next(const struct efi_device_path *dp);
  203. int efi_dp_match(struct efi_device_path *a, struct efi_device_path *b);
  204. struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
  205. struct efi_device_path **rem);
  206. unsigned efi_dp_size(const struct efi_device_path *dp);
  207. struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp);
  208. struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
  209. const struct efi_device_path *dp2);
  210. struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
  211. const struct efi_device_path *node);
  212. struct efi_device_path *efi_dp_from_dev(struct udevice *dev);
  213. struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part);
  214. struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
  215. const char *path);
  216. struct efi_device_path *efi_dp_from_eth(void);
  217. void efi_dp_split_file_path(struct efi_device_path *full_path,
  218. struct efi_device_path **device_path,
  219. struct efi_device_path **file_path);
  220. #define EFI_DP_TYPE(_dp, _type, _subtype) \
  221. (((_dp)->type == DEVICE_PATH_TYPE_##_type) && \
  222. ((_dp)->sub_type == DEVICE_PATH_SUB_TYPE_##_subtype))
  223. /* Convert strings from normal C strings to uEFI strings */
  224. static inline void ascii2unicode(u16 *unicode, const char *ascii)
  225. {
  226. while (*ascii)
  227. *(unicode++) = *(ascii++);
  228. }
  229. static inline int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
  230. {
  231. return memcmp(g1, g2, sizeof(efi_guid_t));
  232. }
  233. /*
  234. * Use these to indicate that your code / data should go into the EFI runtime
  235. * section and thus still be available when the OS is running
  236. */
  237. #define __efi_runtime_data __attribute__ ((section ("efi_runtime_data")))
  238. #define __efi_runtime __attribute__ ((section ("efi_runtime_text")))
  239. /* Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region
  240. * to make it available at runtime */
  241. void efi_add_runtime_mmio(void *mmio_ptr, u64 len);
  242. /* Boards may provide the functions below to implement RTS functionality */
  243. void __efi_runtime EFIAPI efi_reset_system(
  244. enum efi_reset_type reset_type,
  245. efi_status_t reset_status,
  246. unsigned long data_size, void *reset_data);
  247. void efi_reset_system_init(void);
  248. efi_status_t __efi_runtime EFIAPI efi_get_time(
  249. struct efi_time *time,
  250. struct efi_time_cap *capabilities);
  251. void efi_get_time_init(void);
  252. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  253. /*
  254. * Entry point for the tests of the EFI API.
  255. * It is called by 'bootefi selftest'
  256. */
  257. efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
  258. struct efi_system_table *systab);
  259. #endif
  260. #else /* defined(EFI_LOADER) && !defined(CONFIG_SPL_BUILD) */
  261. /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */
  262. #define __efi_runtime_data
  263. #define __efi_runtime
  264. static inline void efi_add_runtime_mmio(void *mmio_ptr, u64 len) { }
  265. /* No loader configured, stub out EFI_ENTRY */
  266. static inline void efi_restore_gd(void) { }
  267. static inline void efi_set_bootdev(const char *dev, const char *devnr,
  268. const char *path) { }
  269. static inline void efi_net_set_dhcp_ack(void *pkt, int len) { }
  270. #endif