efi_loader.h 16 KB

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