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