efi_loader.h 34 KB

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