efi_loader.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #define EFI_ENTRY(format, ...) do { \
  15. efi_restore_gd(); \
  16. debug("EFI: Entry %s(" format ")\n", __func__, ##__VA_ARGS__); \
  17. } while(0)
  18. #define EFI_EXIT(ret) efi_exit_func(ret);
  19. extern struct efi_runtime_services efi_runtime_services;
  20. extern struct efi_system_table systab;
  21. extern const struct efi_simple_text_output_protocol efi_con_out;
  22. extern struct efi_simple_input_interface efi_con_in;
  23. extern const struct efi_console_control_protocol efi_console_control;
  24. extern const struct efi_device_path_to_text_protocol efi_device_path_to_text;
  25. extern const efi_guid_t efi_guid_console_control;
  26. extern const efi_guid_t efi_guid_device_path;
  27. extern const efi_guid_t efi_guid_loaded_image;
  28. extern const efi_guid_t efi_guid_device_path_to_text_protocol;
  29. extern unsigned int __efi_runtime_start, __efi_runtime_stop;
  30. extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop;
  31. /*
  32. * When the UEFI payload wants to open a protocol on an object to get its
  33. * interface (usually a struct with callback functions), this struct maps the
  34. * protocol GUID to the respective protocol interface */
  35. struct efi_handler {
  36. const efi_guid_t *guid;
  37. void *protocol_interface;
  38. };
  39. /*
  40. * UEFI has a poor man's OO model where one "object" can be polymorphic and have
  41. * multiple different protocols (classes) attached to it.
  42. *
  43. * This struct is the parent struct for all of our actual implementation objects
  44. * that can include it to make themselves an EFI object
  45. */
  46. struct efi_object {
  47. /* Every UEFI object is part of a global object list */
  48. struct list_head link;
  49. /* We support up to 8 "protocols" an object can be accessed through */
  50. struct efi_handler protocols[8];
  51. /* The object spawner can either use this for data or as identifier */
  52. void *handle;
  53. };
  54. /**
  55. * struct efi_event
  56. *
  57. * @type: Type of event, see efi_create_event
  58. * @notify_tpl: Task priority level of notifications
  59. * @trigger_time: Period of the timer
  60. * @trigger_next: Next time to trigger the timer
  61. * @nofify_function: Function to call when the event is triggered
  62. * @notify_context: Data to be passed to the notify function
  63. * @trigger_type: Type of timer, see efi_set_timer
  64. * @signaled: The notify function was already called
  65. */
  66. struct efi_event {
  67. uint32_t type;
  68. UINTN notify_tpl;
  69. void (EFIAPI *notify_function)(struct efi_event *event, void *context);
  70. void *notify_context;
  71. u64 trigger_next;
  72. u64 trigger_time;
  73. enum efi_timer_delay trigger_type;
  74. int signaled;
  75. };
  76. /* This list contains all UEFI objects we know of */
  77. extern struct list_head efi_obj_list;
  78. /* Called by bootefi to make console interface available */
  79. int efi_console_register(void);
  80. /* Called by bootefi to make all disk storage accessible as EFI objects */
  81. int efi_disk_register(void);
  82. /* Called by bootefi to make GOP (graphical) interface available */
  83. int efi_gop_register(void);
  84. /* Called by bootefi to make the network interface available */
  85. int efi_net_register(void **handle);
  86. /* Called by bootefi to make SMBIOS tables available */
  87. void efi_smbios_register(void);
  88. /* Called by networking code to memorize the dhcp ack package */
  89. void efi_net_set_dhcp_ack(void *pkt, int len);
  90. /* Called from places to check whether a timer expired */
  91. void efi_timer_check(void);
  92. /* PE loader implementation */
  93. void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info);
  94. /* Called once to store the pristine gd pointer */
  95. void efi_save_gd(void);
  96. /* Called from EFI_ENTRY on callback entry to put gd into the gd register */
  97. void efi_restore_gd(void);
  98. /* Called from EFI_EXIT on callback exit to restore the gd register */
  99. efi_status_t efi_exit_func(efi_status_t ret);
  100. /* Call this to relocate the runtime section to an address space */
  101. void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map);
  102. /* Call this to set the current device name */
  103. void efi_set_bootdev(const char *dev, const char *devnr, const char *path);
  104. /* Call this to create an event */
  105. efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
  106. void (EFIAPI *notify_function) (
  107. struct efi_event *event,
  108. void *context),
  109. void *notify_context, struct efi_event **event);
  110. /* Call this to set a timer */
  111. efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
  112. uint64_t trigger_time);
  113. /* Call this to signal an event */
  114. void efi_signal_event(struct efi_event *event);
  115. /* Generic EFI memory allocator, call this to get memory */
  116. void *efi_alloc(uint64_t len, int memory_type);
  117. /* More specific EFI memory allocator, called by EFI payloads */
  118. efi_status_t efi_allocate_pages(int type, int memory_type, unsigned long pages,
  119. uint64_t *memory);
  120. /* EFI memory free function. */
  121. efi_status_t efi_free_pages(uint64_t memory, unsigned long pages);
  122. /* EFI memory allocator for small allocations */
  123. efi_status_t efi_allocate_pool(int pool_type, unsigned long size,
  124. void **buffer);
  125. /* EFI pool memory free function. */
  126. efi_status_t efi_free_pool(void *buffer);
  127. /* Returns the EFI memory map */
  128. efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
  129. struct efi_mem_desc *memory_map,
  130. unsigned long *map_key,
  131. unsigned long *descriptor_size,
  132. uint32_t *descriptor_version);
  133. /* Adds a range into the EFI memory map */
  134. uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
  135. bool overlap_only_ram);
  136. /* Called by board init to initialize the EFI memory map */
  137. int efi_memory_init(void);
  138. /* Adds new or overrides configuration table entry to the system table */
  139. efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table);
  140. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  141. extern void *efi_bounce_buffer;
  142. #define EFI_LOADER_BOUNCE_BUFFER_SIZE (64 * 1024 * 1024)
  143. #endif
  144. /* Convert strings from normal C strings to uEFI strings */
  145. static inline void ascii2unicode(u16 *unicode, const char *ascii)
  146. {
  147. while (*ascii)
  148. *(unicode++) = *(ascii++);
  149. }
  150. static inline int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
  151. {
  152. return memcmp(g1, g2, sizeof(efi_guid_t));
  153. }
  154. /*
  155. * Use these to indicate that your code / data should go into the EFI runtime
  156. * section and thus still be available when the OS is running
  157. */
  158. #define __efi_runtime_data __attribute__ ((section ("efi_runtime_data")))
  159. #define __efi_runtime __attribute__ ((section ("efi_runtime_text")))
  160. /* Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region
  161. * to make it available at runtime */
  162. void efi_add_runtime_mmio(void *mmio_ptr, u64 len);
  163. /* Boards may provide the functions below to implement RTS functionality */
  164. void __efi_runtime EFIAPI efi_reset_system(
  165. enum efi_reset_type reset_type,
  166. efi_status_t reset_status,
  167. unsigned long data_size, void *reset_data);
  168. void efi_reset_system_init(void);
  169. efi_status_t __efi_runtime EFIAPI efi_get_time(
  170. struct efi_time *time,
  171. struct efi_time_cap *capabilities);
  172. void efi_get_time_init(void);
  173. #else /* defined(EFI_LOADER) && !defined(CONFIG_SPL_BUILD) */
  174. /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */
  175. #define __efi_runtime_data
  176. #define __efi_runtime
  177. static inline void efi_add_runtime_mmio(void *mmio_ptr, u64 len) { }
  178. /* No loader configured, stub out EFI_ENTRY */
  179. static inline void efi_restore_gd(void) { }
  180. static inline void efi_set_bootdev(const char *dev, const char *devnr,
  181. const char *path) { }
  182. static inline void efi_net_set_dhcp_ack(void *pkt, int len) { }
  183. #endif