1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 #ifndef _DRIVERS_FIRMWARE_EFI_EFISTUB_H
4 #define _DRIVERS_FIRMWARE_EFI_EFISTUB_H
5
6 #include <linux/compiler.h>
7 #include <linux/cleanup.h>
8 #include <linux/efi.h>
9 #include <linux/kernel.h>
10 #include <linux/kern_levels.h>
11 #include <linux/types.h>
12 #include <asm/efi.h>
13
14 /*
15 * __init annotations should not be used in the EFI stub, since the code is
16 * either included in the decompressor (x86, ARM) where they have no effect,
17 * or the whole stub is __init annotated at the section level (arm64), by
18 * renaming the sections, in which case the __init annotation will be
19 * redundant, and will result in section names like .init.init.text, and our
20 * linker script does not expect that.
21 */
22 #undef __init
23
24 /*
25 * Allow the platform to override the allocation granularity: this allows
26 * systems that have the capability to run with a larger page size to deal
27 * with the allocations for initrd and fdt more efficiently.
28 */
29 #ifndef EFI_ALLOC_ALIGN
30 #define EFI_ALLOC_ALIGN EFI_PAGE_SIZE
31 #endif
32
33 #ifndef EFI_ALLOC_LIMIT
34 #define EFI_ALLOC_LIMIT ULONG_MAX
35 #endif
36
37 struct edid_info;
38 struct screen_info;
39 struct sysfb_display_info;
40
41 extern bool efi_no5lvl;
42 extern bool efi_nochunk;
43 extern bool efi_nokaslr;
44 extern int efi_loglevel;
45 extern int efi_mem_encrypt;
46 extern bool efi_novamap;
47 extern const efi_system_table_t *efi_system_table;
48
49 typedef union efi_dxe_services_table efi_dxe_services_table_t;
50 extern const efi_dxe_services_table_t *efi_dxe_table;
51
52 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
53 efi_system_table_t *sys_table_arg);
54
55 #ifndef ARCH_HAS_EFISTUB_WRAPPERS
56
57 #define efi_is_native() (true)
58 #define efi_table_attr(inst, attr) (inst)->attr
59 #define efi_fn_call(inst, func, ...) (inst)->func(__VA_ARGS__)
60
61 #endif
62
63 #define efi_call_proto(inst, func, ...) ({ \
64 __typeof__(inst) __inst = (inst); \
65 efi_fn_call(__inst, func, __inst, ##__VA_ARGS__); \
66 })
67 #define efi_bs_call(func, ...) \
68 efi_fn_call(efi_table_attr(efi_system_table, boottime), func, ##__VA_ARGS__)
69 #define efi_rt_call(func, ...) \
70 efi_fn_call(efi_table_attr(efi_system_table, runtime), func, ##__VA_ARGS__)
71 #define efi_dxe_call(func, ...) \
72 efi_fn_call(efi_dxe_table, func, ##__VA_ARGS__)
73
74 #define efi_info(fmt, ...) \
75 efi_printk(KERN_INFO fmt, ##__VA_ARGS__)
76 #define efi_warn(fmt, ...) \
77 efi_printk(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__)
78 #define efi_err(fmt, ...) \
79 efi_printk(KERN_ERR "ERROR: " fmt, ##__VA_ARGS__)
80 #define efi_debug(fmt, ...) \
81 efi_printk(KERN_DEBUG "DEBUG: " fmt, ##__VA_ARGS__)
82
83 #define efi_printk_once(fmt, ...) \
84 ({ \
85 static bool __print_once; \
86 bool __ret_print_once = !__print_once; \
87 \
88 if (!__print_once) { \
89 __print_once = true; \
90 efi_printk(fmt, ##__VA_ARGS__); \
91 } \
92 __ret_print_once; \
93 })
94
95 #define efi_info_once(fmt, ...) \
96 efi_printk_once(KERN_INFO fmt, ##__VA_ARGS__)
97 #define efi_warn_once(fmt, ...) \
98 efi_printk_once(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__)
99 #define efi_err_once(fmt, ...) \
100 efi_printk_once(KERN_ERR "ERROR: " fmt, ##__VA_ARGS__)
101 #define efi_debug_once(fmt, ...) \
102 efi_printk_once(KERN_DEBUG "DEBUG: " fmt, ##__VA_ARGS__)
103
104 /* Helper macros for the usual case of using simple C variables: */
105 #ifndef fdt_setprop_inplace_var
106 #define fdt_setprop_inplace_var(fdt, node_offset, name, var) \
107 fdt_setprop_inplace((fdt), (node_offset), (name), &(var), sizeof(var))
108 #endif
109
110 #ifndef fdt_setprop_var
111 #define fdt_setprop_var(fdt, node_offset, name, var) \
112 fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var))
113 #endif
114
115 #define get_efi_var(name, vendor, ...) \
116 efi_rt_call(get_variable, (efi_char16_t *)(name), \
117 (efi_guid_t *)(vendor), __VA_ARGS__)
118
119 #define set_efi_var(name, vendor, ...) \
120 efi_rt_call(set_variable, (efi_char16_t *)(name), \
121 (efi_guid_t *)(vendor), __VA_ARGS__)
122
123 #define efi_get_handle_at(array, idx) \
124 (efi_is_native() ? (array)[idx] \
125 : (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
126
127 #define efi_get_handle_num(size) \
128 ((size) / (efi_is_native() ? sizeof(efi_handle_t) : sizeof(u32)))
129
130 #define for_each_efi_handle(handle, array, num) \
131 for (int __i = 0; __i < (num) && \
132 ((handle = efi_get_handle_at((array), __i)) || true); \
133 __i++)
134
135 static inline
efi_set_u64_split(u64 data,u32 * lo,u32 * hi)136 void efi_set_u64_split(u64 data, u32 *lo, u32 *hi)
137 {
138 *lo = lower_32_bits(data);
139 *hi = upper_32_bits(data);
140 }
141
142 /*
143 * Allocation types for calls to boottime->allocate_pages.
144 */
145 #define EFI_ALLOCATE_ANY_PAGES 0
146 #define EFI_ALLOCATE_MAX_ADDRESS 1
147 #define EFI_ALLOCATE_ADDRESS 2
148 #define EFI_MAX_ALLOCATE_TYPE 3
149
150 /*
151 * The type of search to perform when calling boottime->locate_handle
152 */
153 #define EFI_LOCATE_ALL_HANDLES 0
154 #define EFI_LOCATE_BY_REGISTER_NOTIFY 1
155 #define EFI_LOCATE_BY_PROTOCOL 2
156
157 /*
158 * boottime->stall takes the time period in microseconds
159 */
160 #define EFI_USEC_PER_SEC 1000000
161
162 /*
163 * boottime->set_timer takes the time in 100ns units
164 */
165 #define EFI_100NSEC_PER_USEC ((u64)10)
166
167 /*
168 * An efi_boot_memmap is used by efi_get_memory_map() to return the
169 * EFI memory map in a dynamically allocated buffer.
170 *
171 * The buffer allocated for the EFI memory map includes extra room for
172 * a minimum of EFI_MMAP_NR_SLACK_SLOTS additional EFI memory descriptors.
173 * This facilitates the reuse of the EFI memory map buffer when a second
174 * call to ExitBootServices() is needed because of intervening changes to
175 * the EFI memory map. Other related structures, e.g. x86 e820ext, need
176 * to factor in this headroom requirement as well.
177 */
178 #define EFI_MMAP_NR_SLACK_SLOTS 32
179
180 typedef struct efi_generic_dev_path efi_device_path_protocol_t;
181
182 union efi_device_path_to_text_protocol {
183 struct {
184 efi_char16_t *(__efiapi *convert_device_node_to_text)(
185 const efi_device_path_protocol_t *,
186 bool, bool);
187 efi_char16_t *(__efiapi *convert_device_path_to_text)(
188 const efi_device_path_protocol_t *,
189 bool, bool);
190 };
191 struct {
192 u32 convert_device_node_to_text;
193 u32 convert_device_path_to_text;
194 } mixed_mode;
195 };
196
197 typedef union efi_device_path_to_text_protocol efi_device_path_to_text_protocol_t;
198
199 union efi_device_path_from_text_protocol {
200 struct {
201 efi_device_path_protocol_t *
202 (__efiapi *convert_text_to_device_node)(const efi_char16_t *);
203 efi_device_path_protocol_t *
204 (__efiapi *convert_text_to_device_path)(const efi_char16_t *);
205 };
206 struct {
207 u32 convert_text_to_device_node;
208 u32 convert_text_to_device_path;
209 } mixed_mode;
210 };
211
212 typedef union efi_device_path_from_text_protocol efi_device_path_from_text_protocol_t;
213
214 typedef void *efi_event_t;
215 /* Note that notifications won't work in mixed mode */
216 typedef void (__efiapi *efi_event_notify_t)(efi_event_t, void *);
217
218 #define EFI_EVT_TIMER 0x80000000U
219 #define EFI_EVT_RUNTIME 0x40000000U
220 #define EFI_EVT_NOTIFY_WAIT 0x00000100U
221 #define EFI_EVT_NOTIFY_SIGNAL 0x00000200U
222
223 /**
224 * efi_set_event_at() - add event to events array
225 *
226 * @events: array of UEFI events
227 * @ids: index where to put the event in the array
228 * @event: event to add to the aray
229 *
230 * boottime->wait_for_event() takes an array of events as input.
231 * Provide a helper to set it up correctly for mixed mode.
232 */
233 static inline
efi_set_event_at(efi_event_t * events,size_t idx,efi_event_t event)234 void efi_set_event_at(efi_event_t *events, size_t idx, efi_event_t event)
235 {
236 if (efi_is_native())
237 events[idx] = event;
238 else
239 ((u32 *)events)[idx] = (u32)(unsigned long)event;
240 }
241
242 #define EFI_TPL_APPLICATION 4
243 #define EFI_TPL_CALLBACK 8
244 #define EFI_TPL_NOTIFY 16
245 #define EFI_TPL_HIGH_LEVEL 31
246
247 typedef enum {
248 EfiTimerCancel,
249 EfiTimerPeriodic,
250 EfiTimerRelative
251 } EFI_TIMER_DELAY;
252
253 /*
254 * EFI Boot Services table
255 */
256 union efi_boot_services {
257 struct {
258 efi_table_hdr_t hdr;
259 void *raise_tpl;
260 void *restore_tpl;
261 efi_status_t (__efiapi *allocate_pages)(int, int, unsigned long,
262 efi_physical_addr_t *);
263 efi_status_t (__efiapi *free_pages)(efi_physical_addr_t,
264 unsigned long);
265 efi_status_t (__efiapi *get_memory_map)(unsigned long *, void *,
266 unsigned long *,
267 unsigned long *, u32 *);
268 efi_status_t (__efiapi *allocate_pool)(int, unsigned long,
269 void **);
270 efi_status_t (__efiapi *free_pool)(void *);
271 efi_status_t (__efiapi *create_event)(u32, unsigned long,
272 efi_event_notify_t, void *,
273 efi_event_t *);
274 efi_status_t (__efiapi *set_timer)(efi_event_t,
275 EFI_TIMER_DELAY, u64);
276 efi_status_t (__efiapi *wait_for_event)(unsigned long,
277 efi_event_t *,
278 unsigned long *);
279 void *signal_event;
280 efi_status_t (__efiapi *close_event)(efi_event_t);
281 void *check_event;
282 void *install_protocol_interface;
283 void *reinstall_protocol_interface;
284 void *uninstall_protocol_interface;
285 efi_status_t (__efiapi *handle_protocol)(efi_handle_t,
286 efi_guid_t *, void **);
287 void *__reserved;
288 void *register_protocol_notify;
289 efi_status_t (__efiapi *locate_handle)(int, efi_guid_t *,
290 void *, unsigned long *,
291 efi_handle_t *);
292 efi_status_t (__efiapi *locate_device_path)(efi_guid_t *,
293 efi_device_path_protocol_t **,
294 efi_handle_t *);
295 efi_status_t (__efiapi *install_configuration_table)(efi_guid_t *,
296 void *);
297 efi_status_t (__efiapi *load_image)(bool, efi_handle_t,
298 efi_device_path_protocol_t *,
299 void *, unsigned long,
300 efi_handle_t *);
301 efi_status_t (__efiapi *start_image)(efi_handle_t, unsigned long *,
302 efi_char16_t **);
303 efi_status_t __noreturn (__efiapi *exit)(efi_handle_t,
304 efi_status_t,
305 unsigned long,
306 efi_char16_t *);
307 efi_status_t (__efiapi *unload_image)(efi_handle_t);
308 efi_status_t (__efiapi *exit_boot_services)(efi_handle_t,
309 unsigned long);
310 void *get_next_monotonic_count;
311 efi_status_t (__efiapi *stall)(unsigned long);
312 void *set_watchdog_timer;
313 void *connect_controller;
314 efi_status_t (__efiapi *disconnect_controller)(efi_handle_t,
315 efi_handle_t,
316 efi_handle_t);
317 void *open_protocol;
318 void *close_protocol;
319 void *open_protocol_information;
320 void *protocols_per_handle;
321 efi_status_t (__efiapi *locate_handle_buffer)(int, efi_guid_t *,
322 void *, unsigned long *,
323 efi_handle_t **);
324 efi_status_t (__efiapi *locate_protocol)(efi_guid_t *, void *,
325 void **);
326 efi_status_t (__efiapi *install_multiple_protocol_interfaces)(efi_handle_t *, ...);
327 efi_status_t (__efiapi *uninstall_multiple_protocol_interfaces)(efi_handle_t, ...);
328 void *calculate_crc32;
329 void (__efiapi *copy_mem)(void *, const void *, unsigned long);
330 void (__efiapi *set_mem)(void *, unsigned long, unsigned char);
331 void *create_event_ex;
332 };
333 struct {
334 efi_table_hdr_t hdr;
335 u32 raise_tpl;
336 u32 restore_tpl;
337 u32 allocate_pages;
338 u32 free_pages;
339 u32 get_memory_map;
340 u32 allocate_pool;
341 u32 free_pool;
342 u32 create_event;
343 u32 set_timer;
344 u32 wait_for_event;
345 u32 signal_event;
346 u32 close_event;
347 u32 check_event;
348 u32 install_protocol_interface;
349 u32 reinstall_protocol_interface;
350 u32 uninstall_protocol_interface;
351 u32 handle_protocol;
352 u32 __reserved;
353 u32 register_protocol_notify;
354 u32 locate_handle;
355 u32 locate_device_path;
356 u32 install_configuration_table;
357 u32 load_image;
358 u32 start_image;
359 u32 exit;
360 u32 unload_image;
361 u32 exit_boot_services;
362 u32 get_next_monotonic_count;
363 u32 stall;
364 u32 set_watchdog_timer;
365 u32 connect_controller;
366 u32 disconnect_controller;
367 u32 open_protocol;
368 u32 close_protocol;
369 u32 open_protocol_information;
370 u32 protocols_per_handle;
371 u32 locate_handle_buffer;
372 u32 locate_protocol;
373 u32 install_multiple_protocol_interfaces;
374 u32 uninstall_multiple_protocol_interfaces;
375 u32 calculate_crc32;
376 u32 copy_mem;
377 u32 set_mem;
378 u32 create_event_ex;
379 } mixed_mode;
380 };
381
382 typedef enum {
383 EfiGcdMemoryTypeNonExistent,
384 EfiGcdMemoryTypeReserved,
385 EfiGcdMemoryTypeSystemMemory,
386 EfiGcdMemoryTypeMemoryMappedIo,
387 EfiGcdMemoryTypePersistent,
388 EfiGcdMemoryTypeMoreReliable,
389 EfiGcdMemoryTypeMaximum
390 } efi_gcd_memory_type_t;
391
392 typedef struct {
393 efi_physical_addr_t base_address;
394 u64 length;
395 u64 capabilities;
396 u64 attributes;
397 efi_gcd_memory_type_t gcd_memory_type;
398 void *image_handle;
399 void *device_handle;
400 } efi_gcd_memory_space_desc_t;
401
402 /*
403 * EFI DXE Services table
404 */
405 union efi_dxe_services_table {
406 struct {
407 efi_table_hdr_t hdr;
408 void *add_memory_space;
409 void *allocate_memory_space;
410 void *free_memory_space;
411 void *remove_memory_space;
412 efi_status_t (__efiapi *get_memory_space_descriptor)(efi_physical_addr_t,
413 efi_gcd_memory_space_desc_t *);
414 efi_status_t (__efiapi *set_memory_space_attributes)(efi_physical_addr_t,
415 u64, u64);
416 void *get_memory_space_map;
417 void *add_io_space;
418 void *allocate_io_space;
419 void *free_io_space;
420 void *remove_io_space;
421 void *get_io_space_descriptor;
422 void *get_io_space_map;
423 void *dispatch;
424 void *schedule;
425 void *trust;
426 void *process_firmware_volume;
427 void *set_memory_space_capabilities;
428 };
429 struct {
430 efi_table_hdr_t hdr;
431 u32 add_memory_space;
432 u32 allocate_memory_space;
433 u32 free_memory_space;
434 u32 remove_memory_space;
435 u32 get_memory_space_descriptor;
436 u32 set_memory_space_attributes;
437 u32 get_memory_space_map;
438 u32 add_io_space;
439 u32 allocate_io_space;
440 u32 free_io_space;
441 u32 remove_io_space;
442 u32 get_io_space_descriptor;
443 u32 get_io_space_map;
444 u32 dispatch;
445 u32 schedule;
446 u32 trust;
447 u32 process_firmware_volume;
448 u32 set_memory_space_capabilities;
449 } mixed_mode;
450 };
451
452 typedef union efi_memory_attribute_protocol efi_memory_attribute_protocol_t;
453
454 union efi_memory_attribute_protocol {
455 struct {
456 efi_status_t (__efiapi *get_memory_attributes)(
457 efi_memory_attribute_protocol_t *, efi_physical_addr_t, u64, u64 *);
458
459 efi_status_t (__efiapi *set_memory_attributes)(
460 efi_memory_attribute_protocol_t *, efi_physical_addr_t, u64, u64);
461
462 efi_status_t (__efiapi *clear_memory_attributes)(
463 efi_memory_attribute_protocol_t *, efi_physical_addr_t, u64, u64);
464 };
465 struct {
466 u32 get_memory_attributes;
467 u32 set_memory_attributes;
468 u32 clear_memory_attributes;
469 } mixed_mode;
470 };
471
472 typedef union efi_uga_draw_protocol efi_uga_draw_protocol_t;
473
474 union efi_uga_draw_protocol {
475 struct {
476 efi_status_t (__efiapi *get_mode)(efi_uga_draw_protocol_t *,
477 u32*, u32*, u32*, u32*);
478 void *set_mode;
479 void *blt;
480 };
481 struct {
482 u32 get_mode;
483 u32 set_mode;
484 u32 blt;
485 } mixed_mode;
486 };
487
488 typedef struct {
489 u16 scan_code;
490 efi_char16_t unicode_char;
491 } efi_input_key_t;
492
493 union efi_simple_text_input_protocol {
494 struct {
495 void *reset;
496 efi_status_t (__efiapi *read_keystroke)(efi_simple_text_input_protocol_t *,
497 efi_input_key_t *);
498 efi_event_t wait_for_key;
499 };
500 struct {
501 u32 reset;
502 u32 read_keystroke;
503 u32 wait_for_key;
504 } mixed_mode;
505 };
506
507 efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key);
508
509 union efi_simple_text_output_protocol {
510 struct {
511 void *reset;
512 efi_status_t (__efiapi *output_string)(efi_simple_text_output_protocol_t *,
513 efi_char16_t *);
514 void *test_string;
515 };
516 struct {
517 u32 reset;
518 u32 output_string;
519 u32 test_string;
520 } mixed_mode;
521 };
522
523 #define PIXEL_RGB_RESERVED_8BIT_PER_COLOR 0
524 #define PIXEL_BGR_RESERVED_8BIT_PER_COLOR 1
525 #define PIXEL_BIT_MASK 2
526 #define PIXEL_BLT_ONLY 3
527 #define PIXEL_FORMAT_MAX 4
528
529 typedef struct {
530 u32 red_mask;
531 u32 green_mask;
532 u32 blue_mask;
533 u32 reserved_mask;
534 } efi_pixel_bitmask_t;
535
536 typedef struct {
537 u32 version;
538 u32 horizontal_resolution;
539 u32 vertical_resolution;
540 int pixel_format;
541 efi_pixel_bitmask_t pixel_information;
542 u32 pixels_per_scan_line;
543 } efi_graphics_output_mode_info_t;
544
545 typedef union efi_graphics_output_protocol_mode efi_graphics_output_protocol_mode_t;
546
547 union efi_graphics_output_protocol_mode {
548 struct {
549 u32 max_mode;
550 u32 mode;
551 efi_graphics_output_mode_info_t *info;
552 unsigned long size_of_info;
553 efi_physical_addr_t frame_buffer_base;
554 unsigned long frame_buffer_size;
555 };
556 struct {
557 u32 max_mode;
558 u32 mode;
559 u32 info;
560 u32 size_of_info;
561 u64 frame_buffer_base;
562 u32 frame_buffer_size;
563 } mixed_mode;
564 };
565
566 typedef union efi_graphics_output_protocol efi_graphics_output_protocol_t;
567
568 union efi_graphics_output_protocol {
569 struct {
570 efi_status_t (__efiapi *query_mode)(efi_graphics_output_protocol_t *,
571 u32, unsigned long *,
572 efi_graphics_output_mode_info_t **);
573 efi_status_t (__efiapi *set_mode) (efi_graphics_output_protocol_t *, u32);
574 void *blt;
575 efi_graphics_output_protocol_mode_t *mode;
576 };
577 struct {
578 u32 query_mode;
579 u32 set_mode;
580 u32 blt;
581 u32 mode;
582 } mixed_mode;
583 };
584
585 typedef union efi_edid_discovered_protocol efi_edid_discovered_protocol_t;
586
587 union efi_edid_discovered_protocol {
588 struct {
589 u32 size_of_edid;
590 u8 *edid;
591 };
592 struct {
593 u32 size_of_edid;
594 u32 edid;
595 } mixed_mode;
596 };
597
598 typedef union efi_edid_active_protocol efi_edid_active_protocol_t;
599
600 union efi_edid_active_protocol {
601 struct {
602 u32 size_of_edid;
603 u8 *edid;
604 };
605 struct {
606 u32 size_of_edid;
607 u32 edid;
608 } mixed_mode;
609 };
610
611 typedef union {
612 struct {
613 u32 revision;
614 efi_handle_t parent_handle;
615 efi_system_table_t *system_table;
616 efi_handle_t device_handle;
617 void *file_path;
618 void *reserved;
619 u32 load_options_size;
620 void *load_options;
621 void *image_base;
622 __aligned_u64 image_size;
623 unsigned int image_code_type;
624 unsigned int image_data_type;
625 efi_status_t (__efiapi *unload)(efi_handle_t image_handle);
626 };
627 struct {
628 u32 revision;
629 u32 parent_handle;
630 u32 system_table;
631 u32 device_handle;
632 u32 file_path;
633 u32 reserved;
634 u32 load_options_size;
635 u32 load_options;
636 u32 image_base;
637 __aligned_u64 image_size;
638 u32 image_code_type;
639 u32 image_data_type;
640 u32 unload;
641 } mixed_mode;
642 } efi_loaded_image_t;
643
644 typedef struct {
645 u64 size;
646 u64 file_size;
647 u64 phys_size;
648 efi_time_t create_time;
649 efi_time_t last_access_time;
650 efi_time_t modification_time;
651 __aligned_u64 attribute;
652 efi_char16_t filename[];
653 } efi_file_info_t;
654
655 typedef union efi_file_protocol efi_file_protocol_t;
656
657 union efi_file_protocol {
658 struct {
659 u64 revision;
660 efi_status_t (__efiapi *open) (efi_file_protocol_t *,
661 efi_file_protocol_t **,
662 efi_char16_t *, u64,
663 u64);
664 efi_status_t (__efiapi *close) (efi_file_protocol_t *);
665 efi_status_t (__efiapi *delete) (efi_file_protocol_t *);
666 efi_status_t (__efiapi *read) (efi_file_protocol_t *,
667 unsigned long *,
668 void *);
669 efi_status_t (__efiapi *write) (efi_file_protocol_t *,
670 unsigned long, void *);
671 efi_status_t (__efiapi *get_position)(efi_file_protocol_t *,
672 u64 *);
673 efi_status_t (__efiapi *set_position)(efi_file_protocol_t *,
674 u64);
675 efi_status_t (__efiapi *get_info) (efi_file_protocol_t *,
676 efi_guid_t *,
677 unsigned long *,
678 void *);
679 efi_status_t (__efiapi *set_info) (efi_file_protocol_t *,
680 efi_guid_t *,
681 unsigned long,
682 void *);
683 efi_status_t (__efiapi *flush) (efi_file_protocol_t *);
684 };
685 struct {
686 u64 revision;
687 u32 open;
688 u32 close;
689 u32 delete;
690 u32 read;
691 u32 write;
692 u32 get_position;
693 u32 set_position;
694 u32 get_info;
695 u32 set_info;
696 u32 flush;
697 } mixed_mode;
698 };
699
700 typedef union efi_simple_file_system_protocol efi_simple_file_system_protocol_t;
701
702 union efi_simple_file_system_protocol {
703 struct {
704 u64 revision;
705 efi_status_t (__efiapi *open_volume)(efi_simple_file_system_protocol_t *,
706 efi_file_protocol_t **);
707 };
708 struct {
709 u64 revision;
710 u32 open_volume;
711 } mixed_mode;
712 };
713
714 #define EFI_FILE_MODE_READ 0x0000000000000001
715 #define EFI_FILE_MODE_WRITE 0x0000000000000002
716 #define EFI_FILE_MODE_CREATE 0x8000000000000000
717
718 typedef enum {
719 EfiPciIoWidthUint8,
720 EfiPciIoWidthUint16,
721 EfiPciIoWidthUint32,
722 EfiPciIoWidthUint64,
723 EfiPciIoWidthFifoUint8,
724 EfiPciIoWidthFifoUint16,
725 EfiPciIoWidthFifoUint32,
726 EfiPciIoWidthFifoUint64,
727 EfiPciIoWidthFillUint8,
728 EfiPciIoWidthFillUint16,
729 EfiPciIoWidthFillUint32,
730 EfiPciIoWidthFillUint64,
731 EfiPciIoWidthMaximum
732 } EFI_PCI_IO_PROTOCOL_WIDTH;
733
734 typedef enum {
735 EfiPciIoAttributeOperationGet,
736 EfiPciIoAttributeOperationSet,
737 EfiPciIoAttributeOperationEnable,
738 EfiPciIoAttributeOperationDisable,
739 EfiPciIoAttributeOperationSupported,
740 EfiPciIoAttributeOperationMaximum
741 } EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION;
742
743 typedef struct {
744 u32 read;
745 u32 write;
746 } efi_pci_io_protocol_access_32_t;
747
748 typedef union efi_pci_io_protocol efi_pci_io_protocol_t;
749
750 typedef
751 efi_status_t (__efiapi *efi_pci_io_protocol_cfg_t)(efi_pci_io_protocol_t *,
752 EFI_PCI_IO_PROTOCOL_WIDTH,
753 u32 offset,
754 unsigned long count,
755 void *buffer);
756
757 typedef struct {
758 void *read;
759 void *write;
760 } efi_pci_io_protocol_access_t;
761
762 typedef struct {
763 efi_pci_io_protocol_cfg_t read;
764 efi_pci_io_protocol_cfg_t write;
765 } efi_pci_io_protocol_config_access_t;
766
767 union efi_pci_io_protocol {
768 struct {
769 void *poll_mem;
770 void *poll_io;
771 efi_pci_io_protocol_access_t mem;
772 efi_pci_io_protocol_access_t io;
773 efi_pci_io_protocol_config_access_t pci;
774 void *copy_mem;
775 void *map;
776 void *unmap;
777 void *allocate_buffer;
778 void *free_buffer;
779 void *flush;
780 efi_status_t (__efiapi *get_location)(efi_pci_io_protocol_t *,
781 unsigned long *segment_nr,
782 unsigned long *bus_nr,
783 unsigned long *device_nr,
784 unsigned long *func_nr);
785 void *attributes;
786 void *get_bar_attributes;
787 void *set_bar_attributes;
788 uint64_t romsize;
789 void *romimage;
790 };
791 struct {
792 u32 poll_mem;
793 u32 poll_io;
794 efi_pci_io_protocol_access_32_t mem;
795 efi_pci_io_protocol_access_32_t io;
796 efi_pci_io_protocol_access_32_t pci;
797 u32 copy_mem;
798 u32 map;
799 u32 unmap;
800 u32 allocate_buffer;
801 u32 free_buffer;
802 u32 flush;
803 u32 get_location;
804 u32 attributes;
805 u32 get_bar_attributes;
806 u32 set_bar_attributes;
807 u64 romsize;
808 u32 romimage;
809 } mixed_mode;
810 };
811
812 #define EFI_PCI_IO_ATTRIBUTE_ISA_MOTHERBOARD_IO 0x0001
813 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO 0x0002
814 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO 0x0004
815 #define EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY 0x0008
816 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO 0x0010
817 #define EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO 0x0020
818 #define EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO 0x0040
819 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE 0x0080
820 #define EFI_PCI_IO_ATTRIBUTE_IO 0x0100
821 #define EFI_PCI_IO_ATTRIBUTE_MEMORY 0x0200
822 #define EFI_PCI_IO_ATTRIBUTE_BUS_MASTER 0x0400
823 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED 0x0800
824 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_DISABLE 0x1000
825 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE 0x2000
826 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM 0x4000
827 #define EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE 0x8000
828 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO_16 0x10000
829 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16 0x20000
830 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO_16 0x40000
831
832 struct efi_dev_path;
833
834 typedef union apple_properties_protocol apple_properties_protocol_t;
835
836 union apple_properties_protocol {
837 struct {
838 unsigned long version;
839 efi_status_t (__efiapi *get)(apple_properties_protocol_t *,
840 struct efi_dev_path *,
841 efi_char16_t *, void *, u32 *);
842 efi_status_t (__efiapi *set)(apple_properties_protocol_t *,
843 struct efi_dev_path *,
844 efi_char16_t *, void *, u32);
845 efi_status_t (__efiapi *del)(apple_properties_protocol_t *,
846 struct efi_dev_path *,
847 efi_char16_t *);
848 efi_status_t (__efiapi *get_all)(apple_properties_protocol_t *,
849 void *buffer, u32 *);
850 };
851 struct {
852 u32 version;
853 u32 get;
854 u32 set;
855 u32 del;
856 u32 get_all;
857 } mixed_mode;
858 };
859
860 typedef u32 efi_tcg2_event_log_format;
861
862 #define INITRD_EVENT_TAG_ID 0x8F3B22ECU
863 #define LOAD_OPTIONS_EVENT_TAG_ID 0x8F3B22EDU
864 #define EV_EVENT_TAG 0x00000006U
865 #define EFI_TCG2_EVENT_HEADER_VERSION 0x1
866
867 struct efi_tcg2_event {
868 u32 event_size;
869 struct {
870 u32 header_size;
871 u16 header_version;
872 u32 pcr_index;
873 u32 event_type;
874 } __packed event_header;
875 /* u8[] event follows here */
876 } __packed;
877
878 /* from TCG PC Client Platform Firmware Profile Specification */
879 typedef struct tdTCG_PCClientTaggedEvent {
880 u32 tagged_event_id;
881 u32 tagged_event_data_size;
882 u8 tagged_event_data[];
883 } TCG_PCClientTaggedEvent;
884
885 typedef struct efi_tcg2_event efi_tcg2_event_t;
886 typedef union efi_tcg2_protocol efi_tcg2_protocol_t;
887
888 union efi_tcg2_protocol {
889 struct {
890 void *get_capability;
891 efi_status_t (__efiapi *get_event_log)(efi_tcg2_protocol_t *,
892 efi_tcg2_event_log_format,
893 efi_physical_addr_t *,
894 efi_physical_addr_t *,
895 efi_bool_t *);
896 efi_status_t (__efiapi *hash_log_extend_event)(efi_tcg2_protocol_t *,
897 u64,
898 efi_physical_addr_t,
899 u64,
900 const efi_tcg2_event_t *);
901 void *submit_command;
902 void *get_active_pcr_banks;
903 void *set_active_pcr_banks;
904 void *get_result_of_set_active_pcr_banks;
905 };
906 struct {
907 u32 get_capability;
908 u32 get_event_log;
909 u32 hash_log_extend_event;
910 u32 submit_command;
911 u32 get_active_pcr_banks;
912 u32 set_active_pcr_banks;
913 u32 get_result_of_set_active_pcr_banks;
914 } mixed_mode;
915 };
916
917 typedef struct {
918 u8 major;
919 u8 minor;
920 } efi_cc_version_t;
921
922 typedef struct {
923 u8 type;
924 u8 sub_type;
925 } efi_cc_type_t;
926
927 /* EFI CC type/subtype defines */
928 #define EFI_CC_TYPE_NONE 0
929 #define EFI_CC_TYPE_AMD_SEV 1
930 #define EFI_CC_TYPE_INTEL_TDX 2
931
932 typedef u32 efi_cc_mr_index_t;
933
934 struct efi_cc_event {
935 u32 event_size;
936 struct {
937 u32 header_size;
938 u16 header_version;
939 u32 mr_index;
940 u32 event_type;
941 } __packed event_header;
942 /* u8[] event follows here */
943 } __packed;
944
945 typedef struct efi_cc_event efi_cc_event_t;
946
947 typedef u32 efi_cc_event_log_bitmap_t;
948 typedef u32 efi_cc_event_log_format_t;
949 typedef u32 efi_cc_event_algorithm_bitmap_t;
950
951 typedef struct {
952 u8 size;
953 efi_cc_version_t structure_version;
954 efi_cc_version_t protocol_version;
955 efi_cc_event_algorithm_bitmap_t hash_algorithm_bitmap;
956 efi_cc_event_log_bitmap_t supported_event_logs;
957 efi_cc_type_t cc_type;
958 } efi_cc_boot_service_cap_t;
959
960 #define EFI_CC_EVENT_HEADER_VERSION 1
961
962 #define EFI_CC_BOOT_HASH_ALG_SHA384 0x00000004
963
964 #define EFI_CC_EVENT_LOG_FORMAT_TCG_2 0x00000002
965
966 typedef union efi_cc_protocol efi_cc_protocol_t;
967
968 union efi_cc_protocol {
969 struct {
970 efi_status_t
971 (__efiapi *get_capability)(efi_cc_protocol_t *,
972 efi_cc_boot_service_cap_t *);
973
974 efi_status_t
975 (__efiapi *get_event_log)(efi_cc_protocol_t *,
976 efi_cc_event_log_format_t,
977 efi_physical_addr_t *,
978 efi_physical_addr_t *,
979 efi_bool_t *);
980
981 efi_status_t
982 (__efiapi *hash_log_extend_event)(efi_cc_protocol_t *, u64,
983 efi_physical_addr_t, u64,
984 const efi_cc_event_t *);
985
986 efi_status_t
987 (__efiapi *map_pcr_to_mr_index)(efi_cc_protocol_t *, u32,
988 efi_cc_mr_index_t *);
989 };
990 struct {
991 u32 get_capability;
992 u32 get_event_log;
993 u32 hash_log_extend_event;
994 u32 map_pcr_to_mr_index;
995 } mixed_mode;
996 };
997
998 struct riscv_efi_boot_protocol {
999 u64 revision;
1000
1001 efi_status_t (__efiapi *get_boot_hartid)(struct riscv_efi_boot_protocol *,
1002 unsigned long *boot_hartid);
1003 };
1004
1005 typedef union efi_load_file_protocol efi_load_file_protocol_t;
1006 typedef union efi_load_file_protocol efi_load_file2_protocol_t;
1007
1008 union efi_load_file_protocol {
1009 struct {
1010 efi_status_t (__efiapi *load_file)(efi_load_file_protocol_t *,
1011 efi_device_path_protocol_t *,
1012 bool, unsigned long *, void *);
1013 };
1014 struct {
1015 u32 load_file;
1016 } mixed_mode;
1017 };
1018
1019 typedef struct {
1020 u32 attributes;
1021 u16 file_path_list_length;
1022 u8 variable_data[];
1023 // efi_char16_t description[];
1024 // efi_device_path_protocol_t file_path_list[];
1025 // u8 optional_data[];
1026 } __packed efi_load_option_t;
1027
1028 #define EFI_LOAD_OPTION_ACTIVE 0x0001U
1029 #define EFI_LOAD_OPTION_FORCE_RECONNECT 0x0002U
1030 #define EFI_LOAD_OPTION_HIDDEN 0x0008U
1031 #define EFI_LOAD_OPTION_CATEGORY 0x1f00U
1032 #define EFI_LOAD_OPTION_CATEGORY_BOOT 0x0000U
1033 #define EFI_LOAD_OPTION_CATEGORY_APP 0x0100U
1034
1035 #define EFI_LOAD_OPTION_BOOT_MASK \
1036 (EFI_LOAD_OPTION_ACTIVE|EFI_LOAD_OPTION_HIDDEN|EFI_LOAD_OPTION_CATEGORY)
1037 #define EFI_LOAD_OPTION_MASK (EFI_LOAD_OPTION_FORCE_RECONNECT|EFI_LOAD_OPTION_BOOT_MASK)
1038
1039 typedef struct {
1040 u32 attributes;
1041 u16 file_path_list_length;
1042 const efi_char16_t *description;
1043 const efi_device_path_protocol_t *file_path_list;
1044 u32 optional_data_size;
1045 const void *optional_data;
1046 } efi_load_option_unpacked_t;
1047
1048 void efi_pci_disable_bridge_busmaster(void);
1049
1050 typedef efi_status_t (*efi_exit_boot_map_processing)(
1051 struct efi_boot_memmap *map,
1052 void *priv);
1053
1054 efi_status_t efi_exit_boot_services(void *handle, void *priv,
1055 efi_exit_boot_map_processing priv_func);
1056
1057 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
1058 unsigned long kernel_addr, char *cmdline_ptr);
1059
1060 void *get_fdt(unsigned long *fdt_size);
1061
1062 efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,
1063 unsigned long *desc_size, u32 *desc_ver);
1064 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
1065 unsigned long desc_size, efi_memory_desc_t *runtime_map,
1066 int *count);
1067
1068 efi_status_t efi_get_random_bytes(unsigned long size, u8 *out);
1069
1070 efi_status_t efi_random_alloc(unsigned long size, unsigned long align,
1071 unsigned long *addr, unsigned long random_seed,
1072 int memory_type, unsigned long alloc_min,
1073 unsigned long alloc_max);
1074
1075 efi_status_t efi_random_get_seed(void);
1076
1077 efi_status_t check_platform_features(void);
1078
1079 void *get_efi_config_table(efi_guid_t guid);
1080
1081 /* NOTE: These functions do not print a trailing newline after the string */
1082 void efi_char16_puts(efi_char16_t *);
1083 void efi_puts(const char *str);
1084
1085 __printf(1, 2) int efi_printk(char const *fmt, ...);
1086
1087 void efi_free(unsigned long size, unsigned long addr);
1088 DEFINE_FREE(efi_pool, void *, if (_T) efi_bs_call(free_pool, _T));
1089
1090 void efi_apply_loadoptions_quirk(const void **load_options, u32 *load_options_size);
1091
1092 char *efi_convert_cmdline(efi_loaded_image_t *image);
1093
1094 efi_status_t efi_get_memory_map(struct efi_boot_memmap **map,
1095 bool install_cfg_tbl);
1096
1097 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
1098 unsigned long max);
1099
1100 efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
1101 unsigned long max, unsigned long align,
1102 int memory_type);
1103
1104 efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
1105 unsigned long *addr, unsigned long min);
1106
1107 efi_status_t efi_relocate_kernel(unsigned long *image_addr,
1108 unsigned long image_size,
1109 unsigned long alloc_size,
1110 unsigned long preferred_addr,
1111 unsigned long alignment,
1112 unsigned long min_addr);
1113
1114 efi_status_t efi_parse_options(char const *cmdline);
1115
1116 void efi_parse_option_graphics(char *option);
1117
1118 efi_status_t efi_setup_graphics(struct screen_info *si, struct edid_info *edid);
1119
1120 efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
1121 const efi_char16_t *optstr,
1122 int optstr_size,
1123 unsigned long soft_limit,
1124 unsigned long hard_limit,
1125 unsigned long *load_addr,
1126 unsigned long *load_size);
1127
1128
efi_load_dtb(efi_loaded_image_t * image,unsigned long * load_addr,unsigned long * load_size)1129 static inline efi_status_t efi_load_dtb(efi_loaded_image_t *image,
1130 unsigned long *load_addr,
1131 unsigned long *load_size)
1132 {
1133 return handle_cmdline_files(image, L"dtb=", sizeof(L"dtb=") - 2,
1134 ULONG_MAX, ULONG_MAX, load_addr, load_size);
1135 }
1136
1137 efi_status_t efi_load_initrd(efi_loaded_image_t *image,
1138 unsigned long soft_limit,
1139 unsigned long hard_limit,
1140 const struct linux_efi_initrd **out);
1141 /*
1142 * This function handles the architcture specific differences between arm and
1143 * arm64 regarding where the kernel image must be loaded and any memory that
1144 * must be reserved. On failure it is required to free all
1145 * all allocations it has made.
1146 */
1147 efi_status_t handle_kernel_image(unsigned long *image_addr,
1148 unsigned long *image_size,
1149 unsigned long *reserve_addr,
1150 unsigned long *reserve_size,
1151 efi_loaded_image_t *image,
1152 efi_handle_t image_handle);
1153
1154 /* shared entrypoint between the normal stub and the zboot stub */
1155 efi_status_t efi_stub_common(efi_handle_t handle,
1156 efi_loaded_image_t *image,
1157 unsigned long image_addr,
1158 char *cmdline_ptr);
1159
1160 efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr);
1161
1162 asmlinkage void __noreturn efi_enter_kernel(unsigned long entrypoint,
1163 unsigned long fdt_addr,
1164 unsigned long fdt_size);
1165
1166 void efi_handle_post_ebs_state(void);
1167
1168 enum efi_secureboot_mode efi_get_secureboot(void);
1169
1170 #ifdef CONFIG_RESET_ATTACK_MITIGATION
1171 void efi_enable_reset_attack_mitigation(void);
1172 #else
1173 static inline void
efi_enable_reset_attack_mitigation(void)1174 efi_enable_reset_attack_mitigation(void) { }
1175 #endif
1176
1177 void efi_retrieve_eventlog(void);
1178
1179 struct sysfb_display_info *alloc_primary_display(void);
1180 struct sysfb_display_info *__alloc_primary_display(void);
1181 void free_primary_display(struct sysfb_display_info *dpy);
1182
1183 void efi_cache_sync_image(unsigned long image_base,
1184 unsigned long alloc_size);
1185
1186 struct efi_smbios_record {
1187 u8 type;
1188 u8 length;
1189 u16 handle;
1190 };
1191
1192 const struct efi_smbios_record *efi_get_smbios_record(u8 type);
1193
1194 struct efi_smbios_type1_record {
1195 struct efi_smbios_record header;
1196
1197 u8 manufacturer;
1198 u8 product_name;
1199 u8 version;
1200 u8 serial_number;
1201 efi_guid_t uuid;
1202 u8 wakeup_type;
1203 u8 sku_number;
1204 u8 family;
1205 };
1206
1207 struct efi_smbios_type4_record {
1208 struct efi_smbios_record header;
1209
1210 u8 socket;
1211 u8 processor_type;
1212 u8 processor_family;
1213 u8 processor_manufacturer;
1214 u8 processor_id[8];
1215 u8 processor_version;
1216 u8 voltage;
1217 u16 external_clock;
1218 u16 max_speed;
1219 u16 current_speed;
1220 u8 status;
1221 u8 processor_upgrade;
1222 u16 l1_cache_handle;
1223 u16 l2_cache_handle;
1224 u16 l3_cache_handle;
1225 u8 serial_number;
1226 u8 asset_tag;
1227 u8 part_number;
1228 u8 core_count;
1229 u8 enabled_core_count;
1230 u8 thread_count;
1231 u16 processor_characteristics;
1232 u16 processor_family2;
1233 u16 core_count2;
1234 u16 enabled_core_count2;
1235 u16 thread_count2;
1236 u16 thread_enabled;
1237 };
1238
1239 #define efi_get_smbios_string(__record, __field) ({ \
1240 __typeof__(__record) __rec = __record; \
1241 __efi_get_smbios_string(&__rec->header, &__rec->__field); \
1242 })
1243
1244 const u8 *__efi_get_smbios_string(const struct efi_smbios_record *record,
1245 const u8 *offset);
1246
1247 void efi_remap_image(unsigned long image_base, unsigned alloc_size,
1248 unsigned long code_size);
1249 efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
1250 unsigned long *reserve_addr,
1251 unsigned long *reserve_size,
1252 unsigned long kernel_size,
1253 unsigned long kernel_codesize,
1254 unsigned long kernel_memsize,
1255 u32 phys_seed);
1256 u32 efi_kaslr_get_phys_seed(efi_handle_t image_handle);
1257
1258 asmlinkage efi_status_t __efiapi
1259 efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab);
1260
1261 efi_status_t allocate_unaccepted_bitmap(__u32 nr_desc,
1262 struct efi_boot_memmap *map);
1263 void process_unaccepted_memory(u64 start, u64 end);
1264 void accept_memory(phys_addr_t start, unsigned long size);
1265 void arch_accept_memory(phys_addr_t start, phys_addr_t end);
1266
1267 efi_status_t efi_zboot_decompress_init(unsigned long *alloc_size);
1268 efi_status_t efi_zboot_decompress(u8 *out, unsigned long outlen);
1269
1270 #endif
1271