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