efi/libstub: Move file I/O support code into separate file
[linux-2.6-microblaze.git] / drivers / firmware / efi / libstub / efistub.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 #ifndef _DRIVERS_FIRMWARE_EFI_EFISTUB_H
4 #define _DRIVERS_FIRMWARE_EFI_EFISTUB_H
5
6 /* error code which can't be mistaken for valid address */
7 #define EFI_ERROR       (~0UL)
8
9 /*
10  * __init annotations should not be used in the EFI stub, since the code is
11  * either included in the decompressor (x86, ARM) where they have no effect,
12  * or the whole stub is __init annotated at the section level (arm64), by
13  * renaming the sections, in which case the __init annotation will be
14  * redundant, and will result in section names like .init.init.text, and our
15  * linker script does not expect that.
16  */
17 #undef __init
18
19 /*
20  * Allow the platform to override the allocation granularity: this allows
21  * systems that have the capability to run with a larger page size to deal
22  * with the allocations for initrd and fdt more efficiently.
23  */
24 #ifndef EFI_ALLOC_ALIGN
25 #define EFI_ALLOC_ALIGN         EFI_PAGE_SIZE
26 #endif
27
28 #ifdef CONFIG_ARM
29 #define __efistub_global        __section(.data)
30 #else
31 #define __efistub_global
32 #endif
33
34 extern bool __pure nochunk(void);
35 extern bool __pure nokaslr(void);
36 extern bool __pure is_quiet(void);
37 extern bool __pure novamap(void);
38
39 extern __pure efi_system_table_t  *efi_system_table(void);
40
41 #define pr_efi(msg)             do {                    \
42         if (!is_quiet()) efi_printk("EFI stub: "msg);   \
43 } while (0)
44
45 #define pr_efi_err(msg) efi_printk("EFI stub: ERROR: "msg)
46
47 /* Helper macros for the usual case of using simple C variables: */
48 #ifndef fdt_setprop_inplace_var
49 #define fdt_setprop_inplace_var(fdt, node_offset, name, var) \
50         fdt_setprop_inplace((fdt), (node_offset), (name), &(var), sizeof(var))
51 #endif
52
53 #ifndef fdt_setprop_var
54 #define fdt_setprop_var(fdt, node_offset, name, var) \
55         fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var))
56 #endif
57
58 #define get_efi_var(name, vendor, ...)                          \
59         efi_rt_call(get_variable, (efi_char16_t *)(name),       \
60                     (efi_guid_t *)(vendor), __VA_ARGS__)
61
62 #define set_efi_var(name, vendor, ...)                          \
63         efi_rt_call(set_variable, (efi_char16_t *)(name),       \
64                     (efi_guid_t *)(vendor), __VA_ARGS__)
65
66 #define efi_get_handle_at(array, idx)                                   \
67         (efi_is_native() ? (array)[idx]                                 \
68                 : (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
69
70 #define efi_get_handle_num(size)                                        \
71         ((size) / (efi_is_native() ? sizeof(efi_handle_t) : sizeof(u32)))
72
73 #define for_each_efi_handle(handle, array, size, i)                     \
74         for (i = 0;                                                     \
75              i < efi_get_handle_num(size) &&                            \
76                 ((handle = efi_get_handle_at((array), i)) || true);     \
77              i++)
78
79 /*
80  * Allocation types for calls to boottime->allocate_pages.
81  */
82 #define EFI_ALLOCATE_ANY_PAGES          0
83 #define EFI_ALLOCATE_MAX_ADDRESS        1
84 #define EFI_ALLOCATE_ADDRESS            2
85 #define EFI_MAX_ALLOCATE_TYPE           3
86
87 /*
88  * The type of search to perform when calling boottime->locate_handle
89  */
90 #define EFI_LOCATE_ALL_HANDLES                  0
91 #define EFI_LOCATE_BY_REGISTER_NOTIFY           1
92 #define EFI_LOCATE_BY_PROTOCOL                  2
93
94 struct efi_boot_memmap {
95         efi_memory_desc_t       **map;
96         unsigned long           *map_size;
97         unsigned long           *desc_size;
98         u32                     *desc_ver;
99         unsigned long           *key_ptr;
100         unsigned long           *buff_size;
101 };
102
103 /*
104  * EFI Boot Services table
105  */
106 union efi_boot_services {
107         struct {
108                 efi_table_hdr_t hdr;
109                 void *raise_tpl;
110                 void *restore_tpl;
111                 efi_status_t (__efiapi *allocate_pages)(int, int, unsigned long,
112                                                         efi_physical_addr_t *);
113                 efi_status_t (__efiapi *free_pages)(efi_physical_addr_t,
114                                                     unsigned long);
115                 efi_status_t (__efiapi *get_memory_map)(unsigned long *, void *,
116                                                         unsigned long *,
117                                                         unsigned long *, u32 *);
118                 efi_status_t (__efiapi *allocate_pool)(int, unsigned long,
119                                                        void **);
120                 efi_status_t (__efiapi *free_pool)(void *);
121                 void *create_event;
122                 void *set_timer;
123                 void *wait_for_event;
124                 void *signal_event;
125                 void *close_event;
126                 void *check_event;
127                 void *install_protocol_interface;
128                 void *reinstall_protocol_interface;
129                 void *uninstall_protocol_interface;
130                 efi_status_t (__efiapi *handle_protocol)(efi_handle_t,
131                                                          efi_guid_t *, void **);
132                 void *__reserved;
133                 void *register_protocol_notify;
134                 efi_status_t (__efiapi *locate_handle)(int, efi_guid_t *,
135                                                        void *, unsigned long *,
136                                                        efi_handle_t *);
137                 void *locate_device_path;
138                 efi_status_t (__efiapi *install_configuration_table)(efi_guid_t *,
139                                                                      void *);
140                 void *load_image;
141                 void *start_image;
142                 void *exit;
143                 void *unload_image;
144                 efi_status_t (__efiapi *exit_boot_services)(efi_handle_t,
145                                                             unsigned long);
146                 void *get_next_monotonic_count;
147                 void *stall;
148                 void *set_watchdog_timer;
149                 void *connect_controller;
150                 efi_status_t (__efiapi *disconnect_controller)(efi_handle_t,
151                                                                efi_handle_t,
152                                                                efi_handle_t);
153                 void *open_protocol;
154                 void *close_protocol;
155                 void *open_protocol_information;
156                 void *protocols_per_handle;
157                 void *locate_handle_buffer;
158                 efi_status_t (__efiapi *locate_protocol)(efi_guid_t *, void *,
159                                                          void **);
160                 void *install_multiple_protocol_interfaces;
161                 void *uninstall_multiple_protocol_interfaces;
162                 void *calculate_crc32;
163                 void *copy_mem;
164                 void *set_mem;
165                 void *create_event_ex;
166         };
167         struct {
168                 efi_table_hdr_t hdr;
169                 u32 raise_tpl;
170                 u32 restore_tpl;
171                 u32 allocate_pages;
172                 u32 free_pages;
173                 u32 get_memory_map;
174                 u32 allocate_pool;
175                 u32 free_pool;
176                 u32 create_event;
177                 u32 set_timer;
178                 u32 wait_for_event;
179                 u32 signal_event;
180                 u32 close_event;
181                 u32 check_event;
182                 u32 install_protocol_interface;
183                 u32 reinstall_protocol_interface;
184                 u32 uninstall_protocol_interface;
185                 u32 handle_protocol;
186                 u32 __reserved;
187                 u32 register_protocol_notify;
188                 u32 locate_handle;
189                 u32 locate_device_path;
190                 u32 install_configuration_table;
191                 u32 load_image;
192                 u32 start_image;
193                 u32 exit;
194                 u32 unload_image;
195                 u32 exit_boot_services;
196                 u32 get_next_monotonic_count;
197                 u32 stall;
198                 u32 set_watchdog_timer;
199                 u32 connect_controller;
200                 u32 disconnect_controller;
201                 u32 open_protocol;
202                 u32 close_protocol;
203                 u32 open_protocol_information;
204                 u32 protocols_per_handle;
205                 u32 locate_handle_buffer;
206                 u32 locate_protocol;
207                 u32 install_multiple_protocol_interfaces;
208                 u32 uninstall_multiple_protocol_interfaces;
209                 u32 calculate_crc32;
210                 u32 copy_mem;
211                 u32 set_mem;
212                 u32 create_event_ex;
213         } mixed_mode;
214 };
215
216 typedef union efi_uga_draw_protocol efi_uga_draw_protocol_t;
217
218 union efi_uga_draw_protocol {
219         struct {
220                 efi_status_t (__efiapi *get_mode)(efi_uga_draw_protocol_t *,
221                                                   u32*, u32*, u32*, u32*);
222                 void *set_mode;
223                 void *blt;
224         };
225         struct {
226                 u32 get_mode;
227                 u32 set_mode;
228                 u32 blt;
229         } mixed_mode;
230 };
231
232 union efi_simple_text_output_protocol {
233         struct {
234                 void *reset;
235                 efi_status_t (__efiapi *output_string)(efi_simple_text_output_protocol_t *,
236                                                        efi_char16_t *);
237                 void *test_string;
238         };
239         struct {
240                 u32 reset;
241                 u32 output_string;
242                 u32 test_string;
243         } mixed_mode;
244 };
245
246 #define PIXEL_RGB_RESERVED_8BIT_PER_COLOR               0
247 #define PIXEL_BGR_RESERVED_8BIT_PER_COLOR               1
248 #define PIXEL_BIT_MASK                                  2
249 #define PIXEL_BLT_ONLY                                  3
250 #define PIXEL_FORMAT_MAX                                4
251
252 typedef struct {
253         u32 red_mask;
254         u32 green_mask;
255         u32 blue_mask;
256         u32 reserved_mask;
257 } efi_pixel_bitmask_t;
258
259 typedef struct {
260         u32 version;
261         u32 horizontal_resolution;
262         u32 vertical_resolution;
263         int pixel_format;
264         efi_pixel_bitmask_t pixel_information;
265         u32 pixels_per_scan_line;
266 } efi_graphics_output_mode_info_t;
267
268 typedef union efi_graphics_output_protocol_mode efi_graphics_output_protocol_mode_t;
269
270 union efi_graphics_output_protocol_mode {
271         struct {
272                 u32 max_mode;
273                 u32 mode;
274                 efi_graphics_output_mode_info_t *info;
275                 unsigned long size_of_info;
276                 efi_physical_addr_t frame_buffer_base;
277                 unsigned long frame_buffer_size;
278         };
279         struct {
280                 u32 max_mode;
281                 u32 mode;
282                 u32 info;
283                 u32 size_of_info;
284                 u64 frame_buffer_base;
285                 u32 frame_buffer_size;
286         } mixed_mode;
287 };
288
289 typedef union efi_graphics_output_protocol efi_graphics_output_protocol_t;
290
291 union efi_graphics_output_protocol {
292         struct {
293                 void *query_mode;
294                 void *set_mode;
295                 void *blt;
296                 efi_graphics_output_protocol_mode_t *mode;
297         };
298         struct {
299                 u32 query_mode;
300                 u32 set_mode;
301                 u32 blt;
302                 u32 mode;
303         } mixed_mode;
304 };
305
306 typedef struct {
307         u32                     revision;
308         efi_handle_t            parent_handle;
309         efi_system_table_t      *system_table;
310         efi_handle_t            device_handle;
311         void                    *file_path;
312         void                    *reserved;
313         u32                     load_options_size;
314         void                    *load_options;
315         void                    *image_base;
316         __aligned_u64           image_size;
317         unsigned int            image_code_type;
318         unsigned int            image_data_type;
319         efi_status_t            (__efiapi *unload)(efi_handle_t image_handle);
320 } efi_loaded_image_t;
321
322 typedef struct {
323         u64                     size;
324         u64                     file_size;
325         u64                     phys_size;
326         efi_time_t              create_time;
327         efi_time_t              last_access_time;
328         efi_time_t              modification_time;
329         __aligned_u64           attribute;
330         efi_char16_t            filename[1];
331 } efi_file_info_t;
332
333 typedef struct efi_file_protocol efi_file_protocol_t;
334
335 struct efi_file_protocol {
336         u64             revision;
337         efi_status_t    (__efiapi *open)        (efi_file_protocol_t *,
338                                                  efi_file_protocol_t **,
339                                                  efi_char16_t *, u64, u64);
340         efi_status_t    (__efiapi *close)       (efi_file_protocol_t *);
341         efi_status_t    (__efiapi *delete)      (efi_file_protocol_t *);
342         efi_status_t    (__efiapi *read)        (efi_file_protocol_t *,
343                                                  unsigned long *, void *);
344         efi_status_t    (__efiapi *write)       (efi_file_protocol_t *,
345                                                  unsigned long, void *);
346         efi_status_t    (__efiapi *get_position)(efi_file_protocol_t *, u64 *);
347         efi_status_t    (__efiapi *set_position)(efi_file_protocol_t *, u64);
348         efi_status_t    (__efiapi *get_info)    (efi_file_protocol_t *,
349                                                  efi_guid_t *, unsigned long *,
350                                                  void *);
351         efi_status_t    (__efiapi *set_info)    (efi_file_protocol_t *,
352                                                  efi_guid_t *, unsigned long,
353                                                  void *);
354         efi_status_t    (__efiapi *flush)       (efi_file_protocol_t *);
355 };
356
357 typedef struct efi_simple_file_system_protocol efi_simple_file_system_protocol_t;
358
359 struct efi_simple_file_system_protocol {
360         u64     revision;
361         int     (__efiapi *open_volume)(efi_simple_file_system_protocol_t *,
362                                         efi_file_protocol_t **);
363 };
364
365 #define EFI_FILE_MODE_READ      0x0000000000000001
366 #define EFI_FILE_MODE_WRITE     0x0000000000000002
367 #define EFI_FILE_MODE_CREATE    0x8000000000000000
368
369 typedef enum {
370         EfiPciIoWidthUint8,
371         EfiPciIoWidthUint16,
372         EfiPciIoWidthUint32,
373         EfiPciIoWidthUint64,
374         EfiPciIoWidthFifoUint8,
375         EfiPciIoWidthFifoUint16,
376         EfiPciIoWidthFifoUint32,
377         EfiPciIoWidthFifoUint64,
378         EfiPciIoWidthFillUint8,
379         EfiPciIoWidthFillUint16,
380         EfiPciIoWidthFillUint32,
381         EfiPciIoWidthFillUint64,
382         EfiPciIoWidthMaximum
383 } EFI_PCI_IO_PROTOCOL_WIDTH;
384
385 typedef enum {
386         EfiPciIoAttributeOperationGet,
387         EfiPciIoAttributeOperationSet,
388         EfiPciIoAttributeOperationEnable,
389         EfiPciIoAttributeOperationDisable,
390         EfiPciIoAttributeOperationSupported,
391     EfiPciIoAttributeOperationMaximum
392 } EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION;
393
394 typedef struct {
395         u32 read;
396         u32 write;
397 } efi_pci_io_protocol_access_32_t;
398
399 typedef union efi_pci_io_protocol efi_pci_io_protocol_t;
400
401 typedef
402 efi_status_t (__efiapi *efi_pci_io_protocol_cfg_t)(efi_pci_io_protocol_t *,
403                                                    EFI_PCI_IO_PROTOCOL_WIDTH,
404                                                    u32 offset,
405                                                    unsigned long count,
406                                                    void *buffer);
407
408 typedef struct {
409         void *read;
410         void *write;
411 } efi_pci_io_protocol_access_t;
412
413 typedef struct {
414         efi_pci_io_protocol_cfg_t read;
415         efi_pci_io_protocol_cfg_t write;
416 } efi_pci_io_protocol_config_access_t;
417
418 union efi_pci_io_protocol {
419         struct {
420                 void *poll_mem;
421                 void *poll_io;
422                 efi_pci_io_protocol_access_t mem;
423                 efi_pci_io_protocol_access_t io;
424                 efi_pci_io_protocol_config_access_t pci;
425                 void *copy_mem;
426                 void *map;
427                 void *unmap;
428                 void *allocate_buffer;
429                 void *free_buffer;
430                 void *flush;
431                 efi_status_t (__efiapi *get_location)(efi_pci_io_protocol_t *,
432                                                       unsigned long *segment_nr,
433                                                       unsigned long *bus_nr,
434                                                       unsigned long *device_nr,
435                                                       unsigned long *func_nr);
436                 void *attributes;
437                 void *get_bar_attributes;
438                 void *set_bar_attributes;
439                 uint64_t romsize;
440                 void *romimage;
441         };
442         struct {
443                 u32 poll_mem;
444                 u32 poll_io;
445                 efi_pci_io_protocol_access_32_t mem;
446                 efi_pci_io_protocol_access_32_t io;
447                 efi_pci_io_protocol_access_32_t pci;
448                 u32 copy_mem;
449                 u32 map;
450                 u32 unmap;
451                 u32 allocate_buffer;
452                 u32 free_buffer;
453                 u32 flush;
454                 u32 get_location;
455                 u32 attributes;
456                 u32 get_bar_attributes;
457                 u32 set_bar_attributes;
458                 u64 romsize;
459                 u32 romimage;
460         } mixed_mode;
461 };
462
463 #define EFI_PCI_IO_ATTRIBUTE_ISA_MOTHERBOARD_IO 0x0001
464 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO 0x0002
465 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO 0x0004
466 #define EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY 0x0008
467 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO 0x0010
468 #define EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO 0x0020
469 #define EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO 0x0040
470 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE 0x0080
471 #define EFI_PCI_IO_ATTRIBUTE_IO 0x0100
472 #define EFI_PCI_IO_ATTRIBUTE_MEMORY 0x0200
473 #define EFI_PCI_IO_ATTRIBUTE_BUS_MASTER 0x0400
474 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED 0x0800
475 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_DISABLE 0x1000
476 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE 0x2000
477 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM 0x4000
478 #define EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE 0x8000
479 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO_16 0x10000
480 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16 0x20000
481 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO_16 0x40000
482
483 struct efi_dev_path;
484
485 typedef union apple_properties_protocol apple_properties_protocol_t;
486
487 union apple_properties_protocol {
488         struct {
489                 unsigned long version;
490                 efi_status_t (__efiapi *get)(apple_properties_protocol_t *,
491                                              struct efi_dev_path *,
492                                              efi_char16_t *, void *, u32 *);
493                 efi_status_t (__efiapi *set)(apple_properties_protocol_t *,
494                                              struct efi_dev_path *,
495                                              efi_char16_t *, void *, u32);
496                 efi_status_t (__efiapi *del)(apple_properties_protocol_t *,
497                                              struct efi_dev_path *,
498                                              efi_char16_t *);
499                 efi_status_t (__efiapi *get_all)(apple_properties_protocol_t *,
500                                                  void *buffer, u32 *);
501         };
502         struct {
503                 u32 version;
504                 u32 get;
505                 u32 set;
506                 u32 del;
507                 u32 get_all;
508         } mixed_mode;
509 };
510
511 typedef u32 efi_tcg2_event_log_format;
512
513 typedef union efi_tcg2_protocol efi_tcg2_protocol_t;
514
515 union efi_tcg2_protocol {
516         struct {
517                 void *get_capability;
518                 efi_status_t (__efiapi *get_event_log)(efi_handle_t,
519                                                        efi_tcg2_event_log_format,
520                                                        efi_physical_addr_t *,
521                                                        efi_physical_addr_t *,
522                                                        efi_bool_t *);
523                 void *hash_log_extend_event;
524                 void *submit_command;
525                 void *get_active_pcr_banks;
526                 void *set_active_pcr_banks;
527                 void *get_result_of_set_active_pcr_banks;
528         };
529         struct {
530                 u32 get_capability;
531                 u32 get_event_log;
532                 u32 hash_log_extend_event;
533                 u32 submit_command;
534                 u32 get_active_pcr_banks;
535                 u32 set_active_pcr_banks;
536                 u32 get_result_of_set_active_pcr_banks;
537         } mixed_mode;
538 };
539
540 void efi_pci_disable_bridge_busmaster(void);
541
542 typedef efi_status_t (*efi_exit_boot_map_processing)(
543         struct efi_boot_memmap *map,
544         void *priv);
545
546 efi_status_t efi_exit_boot_services(void *handle,
547                                     struct efi_boot_memmap *map,
548                                     void *priv,
549                                     efi_exit_boot_map_processing priv_func);
550
551 void efi_char16_printk(efi_char16_t *);
552
553 efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
554                                             unsigned long *new_fdt_addr,
555                                             unsigned long max_addr,
556                                             u64 initrd_addr, u64 initrd_size,
557                                             char *cmdline_ptr,
558                                             unsigned long fdt_addr,
559                                             unsigned long fdt_size);
560
561 void *get_fdt(unsigned long *fdt_size);
562
563 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
564                      unsigned long desc_size, efi_memory_desc_t *runtime_map,
565                      int *count);
566
567 efi_status_t efi_get_random_bytes(unsigned long size, u8 *out);
568
569 efi_status_t efi_random_alloc(unsigned long size, unsigned long align,
570                               unsigned long *addr, unsigned long random_seed);
571
572 efi_status_t check_platform_features(void);
573
574 void *get_efi_config_table(efi_guid_t guid);
575
576 void efi_printk(char *str);
577
578 void efi_free(unsigned long size, unsigned long addr);
579
580 char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len,
581                           unsigned long max_addr);
582
583 efi_status_t efi_get_memory_map(struct efi_boot_memmap *map);
584
585 efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
586                                  unsigned long *addr, unsigned long min);
587
588 static inline
589 efi_status_t efi_low_alloc(unsigned long size, unsigned long align,
590                            unsigned long *addr)
591 {
592         /*
593          * Don't allocate at 0x0. It will confuse code that
594          * checks pointers against NULL. Skip the first 8
595          * bytes so we start at a nice even number.
596          */
597         return efi_low_alloc_above(size, align, addr, 0x8);
598 }
599
600 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
601                                 unsigned long max);
602
603 efi_status_t efi_relocate_kernel(unsigned long *image_addr,
604                                  unsigned long image_size,
605                                  unsigned long alloc_size,
606                                  unsigned long preferred_addr,
607                                  unsigned long alignment,
608                                  unsigned long min_addr);
609
610 efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
611                                   char *cmd_line, char *option_string,
612                                   unsigned long max_addr,
613                                   unsigned long *load_addr,
614                                   unsigned long *load_size);
615
616 efi_status_t efi_parse_options(char const *cmdline);
617
618 efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto,
619                            unsigned long size);
620
621 #endif