1 /* 2 * Extensible Firmware Interface 3 * Based on 'Extensible Firmware Interface Specification' version 0.9, 4 * April 30, 1999 5 * 6 * Copyright (C) 1999 VA Linux Systems 7 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com> 8 * Copyright (C) 1999, 2002-2003 Hewlett-Packard Co. 9 * David Mosberger-Tang <davidm@hpl.hp.com> 10 * Stephane Eranian <eranian@hpl.hp.com> 11 * 12 * From include/linux/efi.h in kernel 4.1 with some additions/subtractions 13 */ 14 15 #ifndef _EFI_H 16 #define _EFI_H 17 18 #include <linux/linkage.h> 19 #include <linux/string.h> 20 #include <linux/types.h> 21 22 #ifdef CONFIG_EFI_STUB_64BIT 23 /* EFI uses the Microsoft ABI which is not the default for GCC */ 24 #define EFIAPI __attribute__((ms_abi)) 25 #else 26 #define EFIAPI asmlinkage 27 #endif 28 29 struct efi_device_path; 30 31 #define EFI_BITS_PER_LONG BITS_PER_LONG 32 33 /* With 64-bit EFI stub, EFI_BITS_PER_LONG has to be 64 */ 34 #ifdef CONFIG_EFI_STUB_64BIT 35 #undef EFI_BITS_PER_LONG 36 #define EFI_BITS_PER_LONG 64 37 #endif 38 39 #define EFI_SUCCESS 0 40 #define EFI_LOAD_ERROR (1 | (1UL << (EFI_BITS_PER_LONG - 1))) 41 #define EFI_INVALID_PARAMETER (2 | (1UL << (EFI_BITS_PER_LONG - 1))) 42 #define EFI_UNSUPPORTED (3 | (1UL << (EFI_BITS_PER_LONG - 1))) 43 #define EFI_BAD_BUFFER_SIZE (4 | (1UL << (EFI_BITS_PER_LONG - 1))) 44 #define EFI_BUFFER_TOO_SMALL (5 | (1UL << (EFI_BITS_PER_LONG - 1))) 45 #define EFI_NOT_READY (6 | (1UL << (EFI_BITS_PER_LONG - 1))) 46 #define EFI_DEVICE_ERROR (7 | (1UL << (EFI_BITS_PER_LONG - 1))) 47 #define EFI_WRITE_PROTECTED (8 | (1UL << (EFI_BITS_PER_LONG - 1))) 48 #define EFI_OUT_OF_RESOURCES (9 | (1UL << (EFI_BITS_PER_LONG - 1))) 49 #define EFI_NOT_FOUND (14 | (1UL << (EFI_BITS_PER_LONG - 1))) 50 #define EFI_ACCESS_DENIED (15 | (1UL << (EFI_BITS_PER_LONG - 1))) 51 #define EFI_SECURITY_VIOLATION (26 | (1UL << (EFI_BITS_PER_LONG - 1))) 52 53 typedef unsigned long efi_status_t; 54 typedef u64 efi_physical_addr_t; 55 typedef u64 efi_virtual_addr_t; 56 typedef void *efi_handle_t; 57 58 #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ 59 ((efi_guid_t) \ 60 {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, \ 61 ((a) >> 24) & 0xff, \ 62 (b) & 0xff, ((b) >> 8) & 0xff, \ 63 (c) & 0xff, ((c) >> 8) & 0xff, \ 64 (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) } }) 65 66 /* Generic EFI table header */ 67 struct efi_table_hdr { 68 u64 signature; 69 u32 revision; 70 u32 headersize; 71 u32 crc32; 72 u32 reserved; 73 }; 74 75 /* Enumeration of memory types introduced in UEFI */ 76 enum efi_mem_type { 77 EFI_RESERVED_MEMORY_TYPE, 78 /* 79 * The code portions of a loaded application. 80 * (Note that UEFI OS loaders are UEFI applications.) 81 */ 82 EFI_LOADER_CODE, 83 /* 84 * The data portions of a loaded application and 85 * the default data allocation type used by an application 86 * to allocate pool memory. 87 */ 88 EFI_LOADER_DATA, 89 /* The code portions of a loaded Boot Services Driver */ 90 EFI_BOOT_SERVICES_CODE, 91 /* 92 * The data portions of a loaded Boot Serves Driver and 93 * the default data allocation type used by a Boot Services 94 * Driver to allocate pool memory. 95 */ 96 EFI_BOOT_SERVICES_DATA, 97 /* The code portions of a loaded Runtime Services Driver */ 98 EFI_RUNTIME_SERVICES_CODE, 99 /* 100 * The data portions of a loaded Runtime Services Driver and 101 * the default data allocation type used by a Runtime Services 102 * Driver to allocate pool memory. 103 */ 104 EFI_RUNTIME_SERVICES_DATA, 105 /* Free (unallocated) memory */ 106 EFI_CONVENTIONAL_MEMORY, 107 /* Memory in which errors have been detected */ 108 EFI_UNUSABLE_MEMORY, 109 /* Memory that holds the ACPI tables */ 110 EFI_ACPI_RECLAIM_MEMORY, 111 /* Address space reserved for use by the firmware */ 112 EFI_ACPI_MEMORY_NVS, 113 /* 114 * Used by system firmware to request that a memory-mapped IO region 115 * be mapped by the OS to a virtual address so it can be accessed by 116 * EFI runtime services. 117 */ 118 EFI_MMAP_IO, 119 /* 120 * System memory-mapped IO region that is used to translate 121 * memory cycles to IO cycles by the processor. 122 */ 123 EFI_MMAP_IO_PORT, 124 /* 125 * Address space reserved by the firmware for code that is 126 * part of the processor. 127 */ 128 EFI_PAL_CODE, 129 130 EFI_MAX_MEMORY_TYPE, 131 EFI_TABLE_END, /* For efi_build_mem_table() */ 132 }; 133 134 /* Attribute values */ 135 enum { 136 EFI_MEMORY_UC_SHIFT = 0, /* uncached */ 137 EFI_MEMORY_WC_SHIFT = 1, /* write-coalescing */ 138 EFI_MEMORY_WT_SHIFT = 2, /* write-through */ 139 EFI_MEMORY_WB_SHIFT = 3, /* write-back */ 140 EFI_MEMORY_UCE_SHIFT = 4, /* uncached, exported */ 141 EFI_MEMORY_WP_SHIFT = 12, /* write-protect */ 142 EFI_MEMORY_RP_SHIFT = 13, /* read-protect */ 143 EFI_MEMORY_XP_SHIFT = 14, /* execute-protect */ 144 EFI_MEMORY_RUNTIME_SHIFT = 63, /* range requires runtime mapping */ 145 146 EFI_MEMORY_RUNTIME = 1ULL << EFI_MEMORY_RUNTIME_SHIFT, 147 EFI_MEM_DESC_VERSION = 1, 148 }; 149 150 #define EFI_PAGE_SHIFT 12 151 #define EFI_PAGE_SIZE (1UL << EFI_PAGE_SHIFT) 152 #define EFI_PAGE_MASK (EFI_PAGE_SIZE - 1) 153 154 struct efi_mem_desc { 155 u32 type; 156 u32 reserved; 157 efi_physical_addr_t physical_start; 158 efi_virtual_addr_t virtual_start; 159 u64 num_pages; 160 u64 attribute; 161 }; 162 163 #define EFI_MEMORY_DESCRIPTOR_VERSION 1 164 165 /* Allocation types for calls to boottime->allocate_pages*/ 166 #define EFI_ALLOCATE_ANY_PAGES 0 167 #define EFI_ALLOCATE_MAX_ADDRESS 1 168 #define EFI_ALLOCATE_ADDRESS 2 169 #define EFI_MAX_ALLOCATE_TYPE 3 170 171 /* Types and defines for Time Services */ 172 #define EFI_TIME_ADJUST_DAYLIGHT 0x1 173 #define EFI_TIME_IN_DAYLIGHT 0x2 174 #define EFI_UNSPECIFIED_TIMEZONE 0x07ff 175 176 struct efi_time { 177 u16 year; 178 u8 month; 179 u8 day; 180 u8 hour; 181 u8 minute; 182 u8 second; 183 u8 pad1; 184 u32 nanosecond; 185 s16 timezone; 186 u8 daylight; 187 u8 pad2; 188 }; 189 190 struct efi_time_cap { 191 u32 resolution; 192 u32 accuracy; 193 u8 sets_to_zero; 194 }; 195 196 enum efi_locate_search_type { 197 all_handles, 198 by_register_notify, 199 by_protocol 200 }; 201 202 struct efi_open_protocol_info_entry { 203 efi_handle_t agent_handle; 204 efi_handle_t controller_handle; 205 u32 attributes; 206 u32 open_count; 207 }; 208 209 enum efi_entry_t { 210 EFIET_END, /* Signals this is the last (empty) entry */ 211 EFIET_MEMORY_MAP, 212 213 /* Number of entries */ 214 EFIET_MEMORY_COUNT, 215 }; 216 217 #define EFI_TABLE_VERSION 1 218 219 /** 220 * struct efi_info_hdr - Header for the EFI info table 221 * 222 * @version: EFI_TABLE_VERSION 223 * @hdr_size: Size of this struct in bytes 224 * @total_size: Total size of this header plus following data 225 * @spare: Spare space for expansion 226 */ 227 struct efi_info_hdr { 228 u32 version; 229 u32 hdr_size; 230 u32 total_size; 231 u32 spare[5]; 232 }; 233 234 /** 235 * struct efi_entry_hdr - Header for a table entry 236 * 237 * @type: enum eft_entry_t 238 * @size size of entry bytes excluding header and padding 239 * @addr: address of this entry (0 if it follows the header ) 240 * @link: size of entry including header and padding 241 * @spare1: Spare space for expansion 242 * @spare2: Spare space for expansion 243 */ 244 struct efi_entry_hdr { 245 u32 type; 246 u32 size; 247 u64 addr; 248 u32 link; 249 u32 spare1; 250 u64 spare2; 251 }; 252 253 /** 254 * struct efi_entry_memmap - a memory map table passed to U-Boot 255 * 256 * @version: EFI's memory map table version 257 * @desc_size: EFI's size of each memory descriptor 258 * @spare: Spare space for expansion 259 * @desc: An array of descriptors, each @desc_size bytes apart 260 */ 261 struct efi_entry_memmap { 262 u32 version; 263 u32 desc_size; 264 u64 spare; 265 struct efi_mem_desc desc[]; 266 }; 267 268 static inline struct efi_mem_desc *efi_get_next_mem_desc( 269 struct efi_entry_memmap *map, struct efi_mem_desc *desc) 270 { 271 return (struct efi_mem_desc *)((ulong)desc + map->desc_size); 272 } 273 274 struct efi_priv { 275 efi_handle_t parent_image; 276 struct efi_device_path *device_path; 277 struct efi_system_table *sys_table; 278 struct efi_boot_services *boot; 279 struct efi_runtime_services *run; 280 bool use_pool_for_malloc; 281 unsigned long ram_base; 282 unsigned int image_data_type; 283 struct efi_info_hdr *info; 284 unsigned int info_size; 285 void *next_hdr; 286 }; 287 288 /* Base address of the EFI image */ 289 extern char image_base[]; 290 291 /* Start and end of U-Boot image (for payload) */ 292 extern char _binary_u_boot_bin_start[], _binary_u_boot_bin_end[]; 293 294 /** 295 * efi_get_sys_table() - Get access to the main EFI system table 296 * 297 * @return pointer to EFI system table 298 */ 299 300 struct efi_system_table *efi_get_sys_table(void); 301 302 /** 303 * efi_get_ram_base() - Find the base of RAM 304 * 305 * This is used when U-Boot is built as an EFI application. 306 * 307 * @return the base of RAM as known to U-Boot 308 */ 309 unsigned long efi_get_ram_base(void); 310 311 /** 312 * efi_init() - Set up ready for use of EFI boot services 313 * 314 * @priv: Pointer to our private EFI structure to fill in 315 * @banner: Banner to display when starting 316 * @image: The image handle passed to efi_main() 317 * @sys_table: The EFI system table pointer passed to efi_main() 318 */ 319 int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image, 320 struct efi_system_table *sys_table); 321 322 /** 323 * efi_malloc() - Allocate some memory from EFI 324 * 325 * @priv: Pointer to private EFI structure 326 * @size: Number of bytes to allocate 327 * @retp: Return EFI status result 328 * @return pointer to memory allocated, or NULL on error 329 */ 330 void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp); 331 332 /** 333 * efi_free() - Free memory allocated from EFI 334 * 335 * @priv: Pointer to private EFI structure 336 * @ptr: Pointer to memory to free 337 */ 338 void efi_free(struct efi_priv *priv, void *ptr); 339 340 /** 341 * efi_puts() - Write out a string to the EFI console 342 * 343 * @priv: Pointer to private EFI structure 344 * @str: String to write (note this is a ASCII, not unicode) 345 */ 346 void efi_puts(struct efi_priv *priv, const char *str); 347 348 /** 349 * efi_putc() - Write out a character to the EFI console 350 * 351 * @priv: Pointer to private EFI structure 352 * @ch: Character to write (note this is not unicode) 353 */ 354 void efi_putc(struct efi_priv *priv, const char ch); 355 356 /** 357 * efi_info_get() - get an entry from an EFI table 358 * 359 * @type: Entry type to search for 360 * @datap: Returns pointer to entry data 361 * @sizep: Returns pointer to entry size 362 * @return 0 if OK, -ENODATA if there is no table, -ENOENT if there is no entry 363 * of the requested type, -EPROTONOSUPPORT if the table has the wrong version 364 */ 365 int efi_info_get(enum efi_entry_t type, void **datap, int *sizep); 366 367 /** 368 * efi_build_mem_table() - make a sorted copy of the memory table 369 * 370 * @map: Pointer to EFI memory map table 371 * @size: Size of table in bytes 372 * @skip_bs: True to skip boot-time memory and merge it with conventional 373 * memory. This will significantly reduce the number of table 374 * entries. 375 * @return pointer to the new table. It should be freed with free() by the 376 * caller 377 */ 378 void *efi_build_mem_table(struct efi_entry_memmap *map, int size, bool skip_bs); 379 380 #endif /* _LINUX_EFI_H */ 381