efi_loader.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 <blk.h>
  11. #include <log.h>
  12. #include <part_efi.h>
  13. #include <efi_api.h>
  14. #include <image.h>
  15. #include <pe.h>
  16. struct blk_desc;
  17. static inline int guidcmp(const void *g1, const void *g2)
  18. {
  19. return memcmp(g1, g2, sizeof(efi_guid_t));
  20. }
  21. static inline void *guidcpy(void *dst, const void *src)
  22. {
  23. return memcpy(dst, src, sizeof(efi_guid_t));
  24. }
  25. /* No need for efi loader support in SPL */
  26. #if CONFIG_IS_ENABLED(EFI_LOADER)
  27. #include <linux/list.h>
  28. #include <linux/oid_registry.h>
  29. /* Maximum number of configuration tables */
  30. #define EFI_MAX_CONFIGURATION_TABLES 16
  31. /* GUID used by the root node */
  32. #define U_BOOT_GUID \
  33. EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
  34. 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
  35. /* GUID used as host device on sandbox */
  36. #define U_BOOT_HOST_DEV_GUID \
  37. EFI_GUID(0xbbe4e671, 0x5773, 0x4ea1, \
  38. 0x9a, 0xab, 0x3a, 0x7d, 0xbf, 0x40, 0xc4, 0x82)
  39. /* GUID used as root for virtio devices */
  40. #define U_BOOT_VIRTIO_DEV_GUID \
  41. EFI_GUID(0x63293792, 0xadf5, 0x9325, \
  42. 0xb9, 0x9f, 0x4e, 0x0e, 0x45, 0x5c, 0x1b, 0x1e)
  43. /* Use internal device tree when starting UEFI application */
  44. #define EFI_FDT_USE_INTERNAL NULL
  45. /* Root node */
  46. extern efi_handle_t efi_root;
  47. /* Set to EFI_SUCCESS when initialized */
  48. extern efi_status_t efi_obj_list_initialized;
  49. /* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
  50. extern bool efi_st_keep_devices;
  51. /* EFI system partition */
  52. extern struct efi_system_partition {
  53. enum if_type if_type;
  54. int devnum;
  55. u8 part;
  56. } efi_system_partition;
  57. int __efi_entry_check(void);
  58. int __efi_exit_check(void);
  59. const char *__efi_nesting(void);
  60. const char *__efi_nesting_inc(void);
  61. const char *__efi_nesting_dec(void);
  62. /*
  63. * Enter the u-boot world from UEFI:
  64. */
  65. #define EFI_ENTRY(format, ...) do { \
  66. assert(__efi_entry_check()); \
  67. debug("%sEFI: Entry %s(" format ")\n", __efi_nesting_inc(), \
  68. __func__, ##__VA_ARGS__); \
  69. } while(0)
  70. /*
  71. * Exit the u-boot world back to UEFI:
  72. */
  73. #define EFI_EXIT(ret) ({ \
  74. typeof(ret) _r = ret; \
  75. debug("%sEFI: Exit: %s: %u\n", __efi_nesting_dec(), \
  76. __func__, (u32)((uintptr_t) _r & ~EFI_ERROR_MASK)); \
  77. assert(__efi_exit_check()); \
  78. _r; \
  79. })
  80. /*
  81. * Call non-void UEFI function from u-boot and retrieve return value:
  82. */
  83. #define EFI_CALL(exp) ({ \
  84. debug("%sEFI: Call: %s\n", __efi_nesting_inc(), #exp); \
  85. assert(__efi_exit_check()); \
  86. typeof(exp) _r = exp; \
  87. assert(__efi_entry_check()); \
  88. debug("%sEFI: %lu returned by %s\n", __efi_nesting_dec(), \
  89. (unsigned long)((uintptr_t)_r & ~EFI_ERROR_MASK), #exp); \
  90. _r; \
  91. })
  92. /*
  93. * Call void UEFI function from u-boot:
  94. */
  95. #define EFI_CALL_VOID(exp) do { \
  96. debug("%sEFI: Call: %s\n", __efi_nesting_inc(), #exp); \
  97. assert(__efi_exit_check()); \
  98. exp; \
  99. assert(__efi_entry_check()); \
  100. debug("%sEFI: Return From: %s\n", __efi_nesting_dec(), #exp); \
  101. } while(0)
  102. /*
  103. * Write an indented message with EFI prefix
  104. */
  105. #define EFI_PRINT(format, ...) ({ \
  106. debug("%sEFI: " format, __efi_nesting(), \
  107. ##__VA_ARGS__); \
  108. })
  109. #ifdef CONFIG_SYS_CACHELINE_SIZE
  110. #define EFI_CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE
  111. #else
  112. /* Just use the greatest cache flush alignment requirement I'm aware of */
  113. #define EFI_CACHELINE_SIZE 128
  114. #endif
  115. /* Key identifying current memory map */
  116. extern efi_uintn_t efi_memory_map_key;
  117. extern struct efi_runtime_services efi_runtime_services;
  118. extern struct efi_system_table systab;
  119. extern struct efi_simple_text_output_protocol efi_con_out;
  120. extern struct efi_simple_text_input_protocol efi_con_in;
  121. extern struct efi_console_control_protocol efi_console_control;
  122. extern const struct efi_device_path_to_text_protocol efi_device_path_to_text;
  123. /* implementation of the EFI_DEVICE_PATH_UTILITIES_PROTOCOL */
  124. extern const struct efi_device_path_utilities_protocol
  125. efi_device_path_utilities;
  126. /* current version of the EFI_UNICODE_COLLATION_PROTOCOL */
  127. extern const struct efi_unicode_collation_protocol
  128. efi_unicode_collation_protocol2;
  129. extern const struct efi_hii_config_routing_protocol efi_hii_config_routing;
  130. extern const struct efi_hii_config_access_protocol efi_hii_config_access;
  131. extern const struct efi_hii_database_protocol efi_hii_database;
  132. extern const struct efi_hii_string_protocol efi_hii_string;
  133. uint16_t *efi_dp_str(struct efi_device_path *dp);
  134. /* GUID of the U-Boot root node */
  135. extern const efi_guid_t efi_u_boot_guid;
  136. #ifdef CONFIG_SANDBOX
  137. /* GUID of U-Boot host device on sandbox */
  138. extern const efi_guid_t efi_guid_host_dev;
  139. #endif
  140. /* GUID of the EFI_BLOCK_IO_PROTOCOL */
  141. extern const efi_guid_t efi_block_io_guid;
  142. extern const efi_guid_t efi_global_variable_guid;
  143. extern const efi_guid_t efi_guid_console_control;
  144. extern const efi_guid_t efi_guid_device_path;
  145. /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
  146. extern const efi_guid_t efi_guid_driver_binding_protocol;
  147. /* event group ExitBootServices() invoked */
  148. extern const efi_guid_t efi_guid_event_group_exit_boot_services;
  149. /* event group SetVirtualAddressMap() invoked */
  150. extern const efi_guid_t efi_guid_event_group_virtual_address_change;
  151. /* event group memory map changed */
  152. extern const efi_guid_t efi_guid_event_group_memory_map_change;
  153. /* event group boot manager about to boot */
  154. extern const efi_guid_t efi_guid_event_group_ready_to_boot;
  155. /* event group ResetSystem() invoked (before ExitBootServices) */
  156. extern const efi_guid_t efi_guid_event_group_reset_system;
  157. /* GUID of the device tree table */
  158. extern const efi_guid_t efi_guid_fdt;
  159. extern const efi_guid_t efi_guid_loaded_image;
  160. extern const efi_guid_t efi_guid_loaded_image_device_path;
  161. extern const efi_guid_t efi_guid_device_path_to_text_protocol;
  162. extern const efi_guid_t efi_simple_file_system_protocol_guid;
  163. extern const efi_guid_t efi_file_info_guid;
  164. /* GUID for file system information */
  165. extern const efi_guid_t efi_file_system_info_guid;
  166. extern const efi_guid_t efi_guid_device_path_utilities_protocol;
  167. /* GUID of the deprecated Unicode collation protocol */
  168. extern const efi_guid_t efi_guid_unicode_collation_protocol;
  169. /* GUIDs of the Load File and Load File2 protocol */
  170. extern const efi_guid_t efi_guid_load_file_protocol;
  171. extern const efi_guid_t efi_guid_load_file2_protocol;
  172. /* GUID of the Unicode collation protocol */
  173. extern const efi_guid_t efi_guid_unicode_collation_protocol2;
  174. extern const efi_guid_t efi_guid_hii_config_routing_protocol;
  175. extern const efi_guid_t efi_guid_hii_config_access_protocol;
  176. extern const efi_guid_t efi_guid_hii_database_protocol;
  177. extern const efi_guid_t efi_guid_hii_string_protocol;
  178. /* GUIDs for authentication */
  179. extern const efi_guid_t efi_guid_image_security_database;
  180. extern const efi_guid_t efi_guid_sha256;
  181. extern const efi_guid_t efi_guid_cert_x509;
  182. extern const efi_guid_t efi_guid_cert_x509_sha256;
  183. extern const efi_guid_t efi_guid_cert_type_pkcs7;
  184. /* GUID of RNG protocol */
  185. extern const efi_guid_t efi_guid_rng_protocol;
  186. /* GUID of capsule update result */
  187. extern const efi_guid_t efi_guid_capsule_report;
  188. /* GUID of firmware management protocol */
  189. extern const efi_guid_t efi_guid_firmware_management_protocol;
  190. /* GUID for the ESRT */
  191. extern const efi_guid_t efi_esrt_guid;
  192. extern char __efi_runtime_start[], __efi_runtime_stop[];
  193. extern char __efi_runtime_rel_start[], __efi_runtime_rel_stop[];
  194. /**
  195. * struct efi_open_protocol_info_item - open protocol info item
  196. *
  197. * When a protocol is opened a open protocol info entry is created.
  198. * These are maintained in a list.
  199. *
  200. * @link: link to the list of open protocol info entries of a protocol
  201. * @info: information about the opening of a protocol
  202. */
  203. struct efi_open_protocol_info_item {
  204. struct list_head link;
  205. struct efi_open_protocol_info_entry info;
  206. };
  207. /**
  208. * struct efi_handler - single protocol interface of a handle
  209. *
  210. * When the UEFI payload wants to open a protocol on an object to get its
  211. * interface (usually a struct with callback functions), this struct maps the
  212. * protocol GUID to the respective protocol interface
  213. *
  214. * @link: link to the list of protocols of a handle
  215. * @guid: GUID of the protocol
  216. * @protocol_interface: protocol interface
  217. * @open_infos: link to the list of open protocol info items
  218. */
  219. struct efi_handler {
  220. struct list_head link;
  221. const efi_guid_t *guid;
  222. void *protocol_interface;
  223. struct list_head open_infos;
  224. };
  225. /**
  226. * enum efi_object_type - type of EFI object
  227. *
  228. * In UnloadImage we must be able to identify if the handle relates to a
  229. * started image.
  230. */
  231. enum efi_object_type {
  232. /** @EFI_OBJECT_TYPE_UNDEFINED: undefined image type */
  233. EFI_OBJECT_TYPE_UNDEFINED = 0,
  234. /** @EFI_OBJECT_TYPE_U_BOOT_FIRMWARE: U-Boot firmware */
  235. EFI_OBJECT_TYPE_U_BOOT_FIRMWARE,
  236. /** @EFI_OBJECT_TYPE_LOADED_IMAGE: loaded image (not started) */
  237. EFI_OBJECT_TYPE_LOADED_IMAGE,
  238. /** @EFI_OBJECT_TYPE_STARTED_IMAGE: started image */
  239. EFI_OBJECT_TYPE_STARTED_IMAGE,
  240. };
  241. /**
  242. * struct efi_object - dereferenced EFI handle
  243. *
  244. * @link: pointers to put the handle into a linked list
  245. * @protocols: linked list with the protocol interfaces installed on this
  246. * handle
  247. * @type: image type if the handle relates to an image
  248. *
  249. * UEFI offers a flexible and expandable object model. The objects in the UEFI
  250. * API are devices, drivers, and loaded images. struct efi_object is our storage
  251. * structure for these objects.
  252. *
  253. * When including this structure into a larger structure always put it first so
  254. * that when deleting a handle the whole encompassing structure can be freed.
  255. *
  256. * A pointer to this structure is referred to as a handle. Typedef efi_handle_t
  257. * has been created for such pointers.
  258. */
  259. struct efi_object {
  260. /* Every UEFI object is part of a global object list */
  261. struct list_head link;
  262. /* The list of protocols */
  263. struct list_head protocols;
  264. enum efi_object_type type;
  265. };
  266. enum efi_image_auth_status {
  267. EFI_IMAGE_AUTH_FAILED = 0,
  268. EFI_IMAGE_AUTH_PASSED,
  269. };
  270. /**
  271. * struct efi_loaded_image_obj - handle of a loaded image
  272. *
  273. * @header: EFI object header
  274. * @exit_status: exit status passed to Exit()
  275. * @exit_data_size: exit data size passed to Exit()
  276. * @exit_data: exit data passed to Exit()
  277. * @exit_jmp: long jump buffer for returning from started image
  278. * @entry: entry address of the relocated image
  279. * @image_type: indicates if the image is an applicition or a driver
  280. * @auth_status: indicates if the image is authenticated
  281. */
  282. struct efi_loaded_image_obj {
  283. struct efi_object header;
  284. efi_status_t *exit_status;
  285. efi_uintn_t *exit_data_size;
  286. u16 **exit_data;
  287. struct jmp_buf_data *exit_jmp;
  288. EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
  289. struct efi_system_table *st);
  290. u16 image_type;
  291. enum efi_image_auth_status auth_status;
  292. };
  293. /**
  294. * struct efi_event
  295. *
  296. * @link: Link to list of all events
  297. * @queue_link: Link to the list of queued events
  298. * @type: Type of event, see efi_create_event
  299. * @notify_tpl: Task priority level of notifications
  300. * @notify_function: Function to call when the event is triggered
  301. * @notify_context: Data to be passed to the notify function
  302. * @group: Event group
  303. * @trigger_time: Period of the timer
  304. * @trigger_next: Next time to trigger the timer
  305. * @trigger_type: Type of timer, see efi_set_timer
  306. * @is_signaled: The event occurred. The event is in the signaled state.
  307. */
  308. struct efi_event {
  309. struct list_head link;
  310. struct list_head queue_link;
  311. uint32_t type;
  312. efi_uintn_t notify_tpl;
  313. void (EFIAPI *notify_function)(struct efi_event *event, void *context);
  314. void *notify_context;
  315. const efi_guid_t *group;
  316. u64 trigger_next;
  317. u64 trigger_time;
  318. enum efi_timer_delay trigger_type;
  319. bool is_signaled;
  320. };
  321. /* This list contains all UEFI objects we know of */
  322. extern struct list_head efi_obj_list;
  323. /* List of all events */
  324. extern struct list_head efi_events;
  325. /**
  326. * struct efi_protocol_notification - handle for notified protocol
  327. *
  328. * When a protocol interface is installed for which an event was registered with
  329. * the RegisterProtocolNotify() service this structure is used to hold the
  330. * handle on which the protocol interface was installed.
  331. *
  332. * @link: link to list of all handles notified for this event
  333. * @handle: handle on which the notified protocol interface was installed
  334. */
  335. struct efi_protocol_notification {
  336. struct list_head link;
  337. efi_handle_t handle;
  338. };
  339. /**
  340. * struct efi_register_notify_event - event registered by
  341. * RegisterProtocolNotify()
  342. *
  343. * The address of this structure serves as registration value.
  344. *
  345. * @link: link to list of all registered events
  346. * @event: registered event. The same event may registered for multiple
  347. * GUIDs.
  348. * @protocol: protocol for which the event is registered
  349. * @handles: linked list of all handles on which the notified protocol was
  350. * installed
  351. */
  352. struct efi_register_notify_event {
  353. struct list_head link;
  354. struct efi_event *event;
  355. efi_guid_t protocol;
  356. struct list_head handles;
  357. };
  358. /* List of all events registered by RegisterProtocolNotify() */
  359. extern struct list_head efi_register_notify_events;
  360. /* Initialize efi execution environment */
  361. efi_status_t efi_init_obj_list(void);
  362. /* Install device tree */
  363. efi_status_t efi_install_fdt(void *fdt);
  364. /* Run loaded UEFI image */
  365. efi_status_t efi_run_image(void *source_buffer, efi_uintn_t source_size);
  366. /* Initialize variable services */
  367. efi_status_t efi_init_variables(void);
  368. /* Notify ExitBootServices() is called */
  369. void efi_variables_boot_exit_notify(void);
  370. /* Called by bootefi to initialize root node */
  371. efi_status_t efi_root_node_register(void);
  372. /* Called by bootefi to initialize runtime */
  373. efi_status_t efi_initialize_system_table(void);
  374. /* efi_runtime_detach() - detach unimplemented runtime functions */
  375. void efi_runtime_detach(void);
  376. /* efi_convert_pointer() - convert pointer to virtual address */
  377. efi_status_t EFIAPI efi_convert_pointer(efi_uintn_t debug_disposition,
  378. void **address);
  379. /* Carve out DT reserved memory ranges */
  380. void efi_carve_out_dt_rsv(void *fdt);
  381. /* Called by bootefi to make console interface available */
  382. efi_status_t efi_console_register(void);
  383. /* Called by bootefi to make all disk storage accessible as EFI objects */
  384. efi_status_t efi_disk_register(void);
  385. /* Called by efi_init_obj_list() to install EFI_RNG_PROTOCOL */
  386. efi_status_t efi_rng_register(void);
  387. /* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
  388. efi_status_t efi_tcg2_register(void);
  389. /* Create handles and protocols for the partitions of a block device */
  390. int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
  391. const char *if_typename, int diskid,
  392. const char *pdevname);
  393. /* Check if it is EFI system partition */
  394. bool efi_disk_is_system_part(efi_handle_t handle);
  395. /* Called by bootefi to make GOP (graphical) interface available */
  396. efi_status_t efi_gop_register(void);
  397. /* Called by bootefi to make the network interface available */
  398. efi_status_t efi_net_register(void);
  399. /* Called by bootefi to make the watchdog available */
  400. efi_status_t efi_watchdog_register(void);
  401. efi_status_t efi_initrd_register(void);
  402. void efi_initrd_deregister(void);
  403. /* Called by bootefi to make SMBIOS tables available */
  404. /**
  405. * efi_acpi_register() - write out ACPI tables
  406. *
  407. * Called by bootefi to make ACPI tables available
  408. *
  409. * @return 0 if OK, -ENOMEM if no memory is available for the tables
  410. */
  411. efi_status_t efi_acpi_register(void);
  412. /**
  413. * efi_smbios_register() - write out SMBIOS tables
  414. *
  415. * Called by bootefi to make SMBIOS tables available
  416. *
  417. * @return 0 if OK, -ENOMEM if no memory is available for the tables
  418. */
  419. efi_status_t efi_smbios_register(void);
  420. struct efi_simple_file_system_protocol *
  421. efi_fs_from_path(struct efi_device_path *fp);
  422. /* Called by networking code to memorize the dhcp ack package */
  423. void efi_net_set_dhcp_ack(void *pkt, int len);
  424. /* Called by efi_set_watchdog_timer to reset the timer */
  425. efi_status_t efi_set_watchdog(unsigned long timeout);
  426. /* Called from places to check whether a timer expired */
  427. void efi_timer_check(void);
  428. /* Check if a buffer contains a PE-COFF image */
  429. efi_status_t efi_check_pe(void *buffer, size_t size, void **nt_header);
  430. /* PE loader implementation */
  431. efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
  432. void *efi, size_t efi_size,
  433. struct efi_loaded_image *loaded_image_info);
  434. /* Called once to store the pristine gd pointer */
  435. void efi_save_gd(void);
  436. /* Special case handler for error/abort that just tries to dtrt to get
  437. * back to u-boot world */
  438. void efi_restore_gd(void);
  439. /* Call this to relocate the runtime section to an address space */
  440. void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map);
  441. /* Call this to set the current device name */
  442. void efi_set_bootdev(const char *dev, const char *devnr, const char *path,
  443. void *buffer, size_t buffer_size);
  444. /* Add a new object to the object list. */
  445. void efi_add_handle(efi_handle_t obj);
  446. /* Create handle */
  447. efi_status_t efi_create_handle(efi_handle_t *handle);
  448. /* Delete handle */
  449. void efi_delete_handle(efi_handle_t obj);
  450. /* Call this to validate a handle and find the EFI object for it */
  451. struct efi_object *efi_search_obj(const efi_handle_t handle);
  452. /* Load image */
  453. efi_status_t EFIAPI efi_load_image(bool boot_policy,
  454. efi_handle_t parent_image,
  455. struct efi_device_path *file_path,
  456. void *source_buffer,
  457. efi_uintn_t source_size,
  458. efi_handle_t *image_handle);
  459. /* Start image */
  460. efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
  461. efi_uintn_t *exit_data_size,
  462. u16 **exit_data);
  463. /* Unload image */
  464. efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle);
  465. /* Find a protocol on a handle */
  466. efi_status_t efi_search_protocol(const efi_handle_t handle,
  467. const efi_guid_t *protocol_guid,
  468. struct efi_handler **handler);
  469. /* Install new protocol on a handle */
  470. efi_status_t efi_add_protocol(const efi_handle_t handle,
  471. const efi_guid_t *protocol,
  472. void *protocol_interface);
  473. /* Open protocol */
  474. efi_status_t efi_protocol_open(struct efi_handler *handler,
  475. void **protocol_interface, void *agent_handle,
  476. void *controller_handle, uint32_t attributes);
  477. /* Delete protocol from a handle */
  478. efi_status_t efi_remove_protocol(const efi_handle_t handle,
  479. const efi_guid_t *protocol,
  480. void *protocol_interface);
  481. /* Delete all protocols from a handle */
  482. efi_status_t efi_remove_all_protocols(const efi_handle_t handle);
  483. /* Install multiple protocol interfaces */
  484. efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
  485. (efi_handle_t *handle, ...);
  486. /* Get handles that support a given protocol */
  487. efi_status_t EFIAPI efi_locate_handle_buffer(
  488. enum efi_locate_search_type search_type,
  489. const efi_guid_t *protocol, void *search_key,
  490. efi_uintn_t *no_handles, efi_handle_t **buffer);
  491. /* Close an previously opened protocol interface */
  492. efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
  493. const efi_guid_t *protocol,
  494. efi_handle_t agent_handle,
  495. efi_handle_t controller_handle);
  496. /* Open a protocol interface */
  497. efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
  498. const efi_guid_t *protocol,
  499. void **protocol_interface);
  500. /* Call this to create an event */
  501. efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
  502. void (EFIAPI *notify_function) (
  503. struct efi_event *event,
  504. void *context),
  505. void *notify_context, efi_guid_t *group,
  506. struct efi_event **event);
  507. /* Call this to set a timer */
  508. efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
  509. uint64_t trigger_time);
  510. /* Call this to signal an event */
  511. void efi_signal_event(struct efi_event *event);
  512. /* open file system: */
  513. struct efi_simple_file_system_protocol *efi_simple_file_system(
  514. struct blk_desc *desc, int part, struct efi_device_path *dp);
  515. /* open file from device-path: */
  516. struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp);
  517. /* Registers a callback function for a notification event. */
  518. efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
  519. struct efi_event *event,
  520. void **registration);
  521. efi_status_t efi_file_size(struct efi_file_handle *fh, efi_uintn_t *size);
  522. /* get a device path from a Boot#### option */
  523. struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t guid);
  524. /**
  525. * efi_size_in_pages() - convert size in bytes to size in pages
  526. *
  527. * This macro returns the number of EFI memory pages required to hold 'size'
  528. * bytes.
  529. *
  530. * @size: size in bytes
  531. * Return: size in pages
  532. */
  533. #define efi_size_in_pages(size) (((size) + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT)
  534. /* Generic EFI memory allocator, call this to get memory */
  535. void *efi_alloc(uint64_t len, int memory_type);
  536. /* More specific EFI memory allocator, called by EFI payloads */
  537. efi_status_t efi_allocate_pages(int type, int memory_type, efi_uintn_t pages,
  538. uint64_t *memory);
  539. /* EFI memory free function. */
  540. efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages);
  541. /* EFI memory allocator for small allocations */
  542. efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size,
  543. void **buffer);
  544. /* EFI pool memory free function. */
  545. efi_status_t efi_free_pool(void *buffer);
  546. /* Returns the EFI memory map */
  547. efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
  548. struct efi_mem_desc *memory_map,
  549. efi_uintn_t *map_key,
  550. efi_uintn_t *descriptor_size,
  551. uint32_t *descriptor_version);
  552. /* Adds a range into the EFI memory map */
  553. efi_status_t efi_add_memory_map(u64 start, u64 size, int memory_type);
  554. /* Adds a conventional range into the EFI memory map */
  555. efi_status_t efi_add_conventional_memory_map(u64 ram_start, u64 ram_end,
  556. u64 ram_top);
  557. /* Called by board init to initialize the EFI drivers */
  558. efi_status_t efi_driver_init(void);
  559. /* Called by board init to initialize the EFI memory map */
  560. int efi_memory_init(void);
  561. /* Adds new or overrides configuration table entry to the system table */
  562. efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table);
  563. /* Sets up a loaded image */
  564. efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
  565. struct efi_device_path *file_path,
  566. struct efi_loaded_image_obj **handle_ptr,
  567. struct efi_loaded_image **info_ptr);
  568. /* Print information about all loaded images */
  569. void efi_print_image_infos(void *pc);
  570. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  571. extern void *efi_bounce_buffer;
  572. #define EFI_LOADER_BOUNCE_BUFFER_SIZE (64 * 1024 * 1024)
  573. #endif
  574. struct efi_device_path *efi_dp_next(const struct efi_device_path *dp);
  575. int efi_dp_match(const struct efi_device_path *a,
  576. const struct efi_device_path *b);
  577. struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
  578. struct efi_device_path **rem);
  579. /* get size of the first device path instance excluding end node */
  580. efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp);
  581. /* size of multi-instance device path excluding end node */
  582. efi_uintn_t efi_dp_size(const struct efi_device_path *dp);
  583. struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp);
  584. struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
  585. const struct efi_device_path *dp2);
  586. struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
  587. const struct efi_device_path *node);
  588. /* Create a device path node of given type, sub-type, length */
  589. struct efi_device_path *efi_dp_create_device_node(const u8 type,
  590. const u8 sub_type,
  591. const u16 length);
  592. /* Append device path instance */
  593. struct efi_device_path *efi_dp_append_instance(
  594. const struct efi_device_path *dp,
  595. const struct efi_device_path *dpi);
  596. /* Get next device path instance */
  597. struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
  598. efi_uintn_t *size);
  599. /* Check if a device path contains muliple instances */
  600. bool efi_dp_is_multi_instance(const struct efi_device_path *dp);
  601. struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part);
  602. /* Create a device node for a block device partition. */
  603. struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part);
  604. struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
  605. const char *path);
  606. struct efi_device_path *efi_dp_from_eth(void);
  607. struct efi_device_path *efi_dp_from_mem(uint32_t mem_type,
  608. uint64_t start_address,
  609. uint64_t end_address);
  610. /* Determine the last device path node that is not the end node. */
  611. const struct efi_device_path *efi_dp_last_node(
  612. const struct efi_device_path *dp);
  613. efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
  614. struct efi_device_path **device_path,
  615. struct efi_device_path **file_path);
  616. efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
  617. const char *path,
  618. struct efi_device_path **device,
  619. struct efi_device_path **file);
  620. ssize_t efi_dp_check_length(const struct efi_device_path *dp,
  621. const size_t maxlen);
  622. #define EFI_DP_TYPE(_dp, _type, _subtype) \
  623. (((_dp)->type == DEVICE_PATH_TYPE_##_type) && \
  624. ((_dp)->sub_type == DEVICE_PATH_SUB_TYPE_##_subtype))
  625. /**
  626. * __efi_runtime_data - declares a non-const variable for EFI runtime section
  627. *
  628. * This macro indicates that a variable is non-const and should go into the
  629. * EFI runtime section, and thus still be available when the OS is running.
  630. *
  631. * Only use on variables not declared const.
  632. *
  633. * Example:
  634. *
  635. * ::
  636. *
  637. * static __efi_runtime_data my_computed_table[256];
  638. */
  639. #define __efi_runtime_data __section(".data.efi_runtime")
  640. /**
  641. * __efi_runtime_rodata - declares a read-only variable for EFI runtime section
  642. *
  643. * This macro indicates that a variable is read-only (const) and should go into
  644. * the EFI runtime section, and thus still be available when the OS is running.
  645. *
  646. * Only use on variables also declared const.
  647. *
  648. * Example:
  649. *
  650. * ::
  651. *
  652. * static const __efi_runtime_rodata my_const_table[] = { 1, 2, 3 };
  653. */
  654. #define __efi_runtime_rodata __section(".rodata.efi_runtime")
  655. /**
  656. * __efi_runtime - declares a function for EFI runtime section
  657. *
  658. * This macro indicates that a function should go into the EFI runtime section,
  659. * and thus still be available when the OS is running.
  660. *
  661. * Example:
  662. *
  663. * ::
  664. *
  665. * static __efi_runtime compute_my_table(void);
  666. */
  667. #define __efi_runtime __section(".text.efi_runtime")
  668. /* Indicate supported runtime services */
  669. efi_status_t efi_init_runtime_supported(void);
  670. /* Update CRC32 in table header */
  671. void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table);
  672. /* Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region
  673. * to make it available at runtime */
  674. efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len);
  675. /* Boards may provide the functions below to implement RTS functionality */
  676. void __efi_runtime EFIAPI efi_reset_system(
  677. enum efi_reset_type reset_type,
  678. efi_status_t reset_status,
  679. unsigned long data_size, void *reset_data);
  680. /* Architecture specific initialization of the EFI subsystem */
  681. efi_status_t efi_reset_system_init(void);
  682. efi_status_t __efi_runtime EFIAPI efi_get_time(
  683. struct efi_time *time,
  684. struct efi_time_cap *capabilities);
  685. efi_status_t __efi_runtime EFIAPI efi_set_time(struct efi_time *time);
  686. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  687. /*
  688. * Entry point for the tests of the EFI API.
  689. * It is called by 'bootefi selftest'
  690. */
  691. efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
  692. struct efi_system_table *systab);
  693. #endif
  694. efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
  695. const efi_guid_t *vendor, u32 *attributes,
  696. efi_uintn_t *data_size, void *data);
  697. efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
  698. u16 *variable_name,
  699. efi_guid_t *vendor);
  700. efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
  701. const efi_guid_t *vendor, u32 attributes,
  702. efi_uintn_t data_size, const void *data);
  703. efi_status_t EFIAPI efi_query_variable_info(
  704. u32 attributes, u64 *maximum_variable_storage_size,
  705. u64 *remaining_variable_storage_size,
  706. u64 *maximum_variable_size);
  707. void *efi_get_var(u16 *name, const efi_guid_t *vendor, efi_uintn_t *size);
  708. /*
  709. * See section 3.1.3 in the v2.7 UEFI spec for more details on
  710. * the layout of EFI_LOAD_OPTION. In short it is:
  711. *
  712. * typedef struct _EFI_LOAD_OPTION {
  713. * UINT32 Attributes;
  714. * UINT16 FilePathListLength;
  715. * // CHAR16 Description[]; <-- variable length, NULL terminated
  716. * // EFI_DEVICE_PATH_PROTOCOL FilePathList[];
  717. * <-- FilePathListLength bytes
  718. * // UINT8 OptionalData[];
  719. * } EFI_LOAD_OPTION;
  720. */
  721. struct efi_load_option {
  722. u32 attributes;
  723. u16 file_path_length;
  724. u16 *label;
  725. struct efi_device_path *file_path;
  726. const u8 *optional_data;
  727. };
  728. struct efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
  729. efi_uintn_t *size, efi_guid_t guid);
  730. struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
  731. const struct efi_device_path *dp2);
  732. efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
  733. efi_uintn_t *size);
  734. unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data);
  735. efi_status_t efi_set_load_options(efi_handle_t handle,
  736. efi_uintn_t load_options_size,
  737. void *load_options);
  738. efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options);
  739. /**
  740. * struct efi_image_regions - A list of memory regions
  741. *
  742. * @max: Maximum number of regions
  743. * @num: Number of regions
  744. * @reg: array of regions
  745. */
  746. struct efi_image_regions {
  747. int max;
  748. int num;
  749. struct image_region reg[];
  750. };
  751. /**
  752. * struct efi_sig_data - A decoded data of struct efi_signature_data
  753. *
  754. * This structure represents an internal form of signature in
  755. * signature database. A listed list may represent a signature list.
  756. *
  757. * @next: Pointer to next entry
  758. * @owner: Signature owner
  759. * @data: Pointer to signature data
  760. * @size: Size of signature data
  761. */
  762. struct efi_sig_data {
  763. struct efi_sig_data *next;
  764. efi_guid_t owner;
  765. void *data;
  766. size_t size;
  767. };
  768. /**
  769. * struct efi_signature_store - A decoded data of signature database
  770. *
  771. * This structure represents an internal form of signature database.
  772. *
  773. * @next: Pointer to next entry
  774. * @sig_type: Signature type
  775. * @sig_data_list: Pointer to signature list
  776. */
  777. struct efi_signature_store {
  778. struct efi_signature_store *next;
  779. efi_guid_t sig_type;
  780. struct efi_sig_data *sig_data_list;
  781. };
  782. struct x509_certificate;
  783. struct pkcs7_message;
  784. bool efi_signature_lookup_digest(struct efi_image_regions *regs,
  785. struct efi_signature_store *db);
  786. bool efi_signature_verify(struct efi_image_regions *regs,
  787. struct pkcs7_message *msg,
  788. struct efi_signature_store *db,
  789. struct efi_signature_store *dbx);
  790. static inline bool efi_signature_verify_one(struct efi_image_regions *regs,
  791. struct pkcs7_message *msg,
  792. struct efi_signature_store *db)
  793. {
  794. return efi_signature_verify(regs, msg, db, NULL);
  795. }
  796. bool efi_signature_check_signers(struct pkcs7_message *msg,
  797. struct efi_signature_store *dbx);
  798. efi_status_t efi_image_region_add(struct efi_image_regions *regs,
  799. const void *start, const void *end,
  800. int nocheck);
  801. void efi_sigstore_free(struct efi_signature_store *sigstore);
  802. struct efi_signature_store *efi_build_signature_store(void *sig_list,
  803. efi_uintn_t size);
  804. struct efi_signature_store *efi_sigstore_parse_sigdb(u16 *name);
  805. bool efi_secure_boot_enabled(void);
  806. bool efi_capsule_auth_enabled(void);
  807. bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp,
  808. WIN_CERTIFICATE **auth, size_t *auth_len);
  809. struct pkcs7_message *efi_parse_pkcs7_header(const void *buf,
  810. size_t buflen,
  811. u8 **tmpbuf);
  812. /* runtime implementation of memcpy() */
  813. void efi_memcpy_runtime(void *dest, const void *src, size_t n);
  814. /* commonly used helper function */
  815. u16 *efi_create_indexed_name(u16 *buffer, size_t buffer_size, const char *name,
  816. unsigned int index);
  817. extern const struct efi_firmware_management_protocol efi_fmp_fit;
  818. extern const struct efi_firmware_management_protocol efi_fmp_raw;
  819. /* Capsule update */
  820. efi_status_t EFIAPI efi_update_capsule(
  821. struct efi_capsule_header **capsule_header_array,
  822. efi_uintn_t capsule_count,
  823. u64 scatter_gather_list);
  824. efi_status_t EFIAPI efi_query_capsule_caps(
  825. struct efi_capsule_header **capsule_header_array,
  826. efi_uintn_t capsule_count,
  827. u64 *maximum_capsule_size,
  828. u32 *reset_type);
  829. efi_status_t efi_capsule_authenticate(const void *capsule,
  830. efi_uintn_t capsule_size,
  831. void **image, efi_uintn_t *image_size);
  832. #define EFI_CAPSULE_DIR L"\\EFI\\UpdateCapsule\\"
  833. /* Hook at initialization */
  834. efi_status_t efi_launch_capsules(void);
  835. #else /* CONFIG_IS_ENABLED(EFI_LOADER) */
  836. /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */
  837. #define __efi_runtime_data
  838. #define __efi_runtime_rodata
  839. #define __efi_runtime
  840. static inline efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
  841. {
  842. return EFI_SUCCESS;
  843. }
  844. /* No loader configured, stub out EFI_ENTRY */
  845. static inline void efi_restore_gd(void) { }
  846. static inline void efi_set_bootdev(const char *dev, const char *devnr,
  847. const char *path, void *buffer,
  848. size_t buffer_size) { }
  849. static inline void efi_net_set_dhcp_ack(void *pkt, int len) { }
  850. static inline void efi_print_image_infos(void *pc) { }
  851. static inline efi_status_t efi_launch_capsules(void)
  852. {
  853. return EFI_SUCCESS;
  854. }
  855. #endif /* CONFIG_IS_ENABLED(EFI_LOADER) */
  856. /**
  857. * Install the ESRT system table.
  858. *
  859. * @return status code
  860. */
  861. efi_status_t efi_esrt_register(void);
  862. /**
  863. * efi_esrt_populate() - Populates the ESRT entries from the FMP instances
  864. * present in the system.
  865. * If an ESRT already exists, the old ESRT is replaced in the system table.
  866. * The memory of the old ESRT is deallocated.
  867. *
  868. * Return:
  869. * - EFI_SUCCESS if the ESRT is correctly created
  870. * - error code otherwise.
  871. */
  872. efi_status_t efi_esrt_populate(void);
  873. #endif /* _EFI_LOADER_H */