efi.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Extensible Firmware Interface
  4. * Based on 'Extensible Firmware Interface Specification' version 0.9,
  5. * April 30, 1999
  6. *
  7. * Copyright (C) 1999 VA Linux Systems
  8. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  9. * Copyright (C) 1999, 2002-2003 Hewlett-Packard Co.
  10. * David Mosberger-Tang <davidm@hpl.hp.com>
  11. * Stephane Eranian <eranian@hpl.hp.com>
  12. *
  13. * From include/linux/efi.h in kernel 4.1 with some additions/subtractions
  14. */
  15. #ifndef _EFI_H
  16. #define _EFI_H
  17. #include <linux/linkage.h>
  18. #include <linux/string.h>
  19. #include <linux/types.h>
  20. /* Type INTN in UEFI specification */
  21. #define efi_intn_t ssize_t
  22. /* Type UINTN in UEFI specification*/
  23. #define efi_uintn_t size_t
  24. /*
  25. * EFI on x86_64 uses the Microsoft ABI which is not the default for GCC.
  26. *
  27. * There are two scenarios for EFI on x86_64: building a 64-bit EFI stub
  28. * codes (CONFIG_EFI_STUB_64BIT) and building a 64-bit U-Boot (CONFIG_X86_64).
  29. * Either needs to be properly built with the '-m64' compiler flag, and hence
  30. * it is enough to only check the compiler provided define __x86_64__ here.
  31. */
  32. #ifdef __x86_64__
  33. #define EFIAPI __attribute__((ms_abi))
  34. #define efi_va_list __builtin_ms_va_list
  35. #define efi_va_start __builtin_ms_va_start
  36. #define efi_va_arg __builtin_va_arg
  37. #define efi_va_end __builtin_ms_va_end
  38. #else
  39. #define EFIAPI asmlinkage
  40. #define efi_va_list va_list
  41. #define efi_va_start va_start
  42. #define efi_va_arg va_arg
  43. #define efi_va_end va_end
  44. #endif /* __x86_64__ */
  45. #define EFI32_LOADER_SIGNATURE "EL32"
  46. #define EFI64_LOADER_SIGNATURE "EL64"
  47. struct efi_device_path;
  48. typedef struct {
  49. u8 b[16];
  50. } efi_guid_t __attribute__((aligned(8)));
  51. #define EFI_BITS_PER_LONG (sizeof(long) * 8)
  52. /* Bit mask for EFI status code with error */
  53. #define EFI_ERROR_MASK (1UL << (EFI_BITS_PER_LONG - 1))
  54. /* Status codes returned by EFI protocols */
  55. #define EFI_SUCCESS 0
  56. #define EFI_LOAD_ERROR (EFI_ERROR_MASK | 1)
  57. #define EFI_INVALID_PARAMETER (EFI_ERROR_MASK | 2)
  58. #define EFI_UNSUPPORTED (EFI_ERROR_MASK | 3)
  59. #define EFI_BAD_BUFFER_SIZE (EFI_ERROR_MASK | 4)
  60. #define EFI_BUFFER_TOO_SMALL (EFI_ERROR_MASK | 5)
  61. #define EFI_NOT_READY (EFI_ERROR_MASK | 6)
  62. #define EFI_DEVICE_ERROR (EFI_ERROR_MASK | 7)
  63. #define EFI_WRITE_PROTECTED (EFI_ERROR_MASK | 8)
  64. #define EFI_OUT_OF_RESOURCES (EFI_ERROR_MASK | 9)
  65. #define EFI_VOLUME_CORRUPTED (EFI_ERROR_MASK | 10)
  66. #define EFI_VOLUME_FULL (EFI_ERROR_MASK | 11)
  67. #define EFI_NO_MEDIA (EFI_ERROR_MASK | 12)
  68. #define EFI_MEDIA_CHANGED (EFI_ERROR_MASK | 13)
  69. #define EFI_NOT_FOUND (EFI_ERROR_MASK | 14)
  70. #define EFI_ACCESS_DENIED (EFI_ERROR_MASK | 15)
  71. #define EFI_NO_RESPONSE (EFI_ERROR_MASK | 16)
  72. #define EFI_NO_MAPPING (EFI_ERROR_MASK | 17)
  73. #define EFI_TIMEOUT (EFI_ERROR_MASK | 18)
  74. #define EFI_NOT_STARTED (EFI_ERROR_MASK | 19)
  75. #define EFI_ALREADY_STARTED (EFI_ERROR_MASK | 20)
  76. #define EFI_ABORTED (EFI_ERROR_MASK | 21)
  77. #define EFI_ICMP_ERROR (EFI_ERROR_MASK | 22)
  78. #define EFI_TFTP_ERROR (EFI_ERROR_MASK | 23)
  79. #define EFI_PROTOCOL_ERROR (EFI_ERROR_MASK | 24)
  80. #define EFI_INCOMPATIBLE_VERSION (EFI_ERROR_MASK | 25)
  81. #define EFI_SECURITY_VIOLATION (EFI_ERROR_MASK | 26)
  82. #define EFI_CRC_ERROR (EFI_ERROR_MASK | 27)
  83. #define EFI_END_OF_MEDIA (EFI_ERROR_MASK | 28)
  84. #define EFI_END_OF_FILE (EFI_ERROR_MASK | 31)
  85. #define EFI_INVALID_LANGUAGE (EFI_ERROR_MASK | 32)
  86. #define EFI_COMPROMISED_DATA (EFI_ERROR_MASK | 33)
  87. #define EFI_IP_ADDRESS_CONFLICT (EFI_ERROR_MASK | 34)
  88. #define EFI_HTTP_ERROR (EFI_ERROR_MASK | 35)
  89. #define EFI_WARN_UNKNOWN_GLYPH 1
  90. #define EFI_WARN_DELETE_FAILURE 2
  91. #define EFI_WARN_WRITE_FAILURE 3
  92. #define EFI_WARN_BUFFER_TOO_SMALL 4
  93. #define EFI_WARN_STALE_DATA 5
  94. #define EFI_WARN_FILE_SYSTEM 6
  95. #define EFI_WARN_RESET_REQUIRED 7
  96. typedef unsigned long efi_status_t;
  97. typedef u64 efi_physical_addr_t;
  98. typedef u64 efi_virtual_addr_t;
  99. typedef struct efi_object *efi_handle_t;
  100. #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
  101. {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, \
  102. ((a) >> 24) & 0xff, \
  103. (b) & 0xff, ((b) >> 8) & 0xff, \
  104. (c) & 0xff, ((c) >> 8) & 0xff, \
  105. (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) } }
  106. /* Generic EFI table header */
  107. struct efi_table_hdr {
  108. u64 signature;
  109. u32 revision;
  110. u32 headersize;
  111. u32 crc32;
  112. u32 reserved;
  113. };
  114. /* Allocation types for calls to boottime->allocate_pages*/
  115. /**
  116. * enum efi_allocate_type - address restriction for memory allocation
  117. */
  118. enum efi_allocate_type {
  119. /**
  120. * @EFI_ALLOCATE_ANY_PAGES:
  121. * Allocate any block of sufficient size. Ignore memory address.
  122. */
  123. EFI_ALLOCATE_ANY_PAGES,
  124. /**
  125. * @EFI_ALLOCATE_MAX_ADDRESS:
  126. * Allocate a memory block with an uppermost address less or equal
  127. * to the indicated address.
  128. */
  129. EFI_ALLOCATE_MAX_ADDRESS,
  130. /**
  131. * @EFI_ALLOCATE_ADDRESS:
  132. * Allocate a memory block starting at the indicatged adress.
  133. */
  134. EFI_ALLOCATE_ADDRESS,
  135. /**
  136. * @EFI_MAX_ALLOCATE_TYPE:
  137. * Value use for range checking.
  138. */
  139. EFI_MAX_ALLOCATE_TYPE,
  140. };
  141. /* Enumeration of memory types introduced in UEFI */
  142. enum efi_memory_type {
  143. EFI_RESERVED_MEMORY_TYPE,
  144. /*
  145. * The code portions of a loaded application.
  146. * (Note that UEFI OS loaders are UEFI applications.)
  147. */
  148. EFI_LOADER_CODE,
  149. /*
  150. * The data portions of a loaded application and
  151. * the default data allocation type used by an application
  152. * to allocate pool memory.
  153. */
  154. EFI_LOADER_DATA,
  155. /* The code portions of a loaded Boot Services Driver */
  156. EFI_BOOT_SERVICES_CODE,
  157. /*
  158. * The data portions of a loaded Boot Services Driver and
  159. * the default data allocation type used by a Boot Services
  160. * Driver to allocate pool memory.
  161. */
  162. EFI_BOOT_SERVICES_DATA,
  163. /* The code portions of a loaded Runtime Services Driver */
  164. EFI_RUNTIME_SERVICES_CODE,
  165. /*
  166. * The data portions of a loaded Runtime Services Driver and
  167. * the default data allocation type used by a Runtime Services
  168. * Driver to allocate pool memory.
  169. */
  170. EFI_RUNTIME_SERVICES_DATA,
  171. /* Free (unallocated) memory */
  172. EFI_CONVENTIONAL_MEMORY,
  173. /* Memory in which errors have been detected */
  174. EFI_UNUSABLE_MEMORY,
  175. /* Memory that holds the ACPI tables */
  176. EFI_ACPI_RECLAIM_MEMORY,
  177. /* Address space reserved for use by the firmware */
  178. EFI_ACPI_MEMORY_NVS,
  179. /*
  180. * Used by system firmware to request that a memory-mapped IO region
  181. * be mapped by the OS to a virtual address so it can be accessed by
  182. * EFI runtime services.
  183. */
  184. EFI_MMAP_IO,
  185. /*
  186. * System memory-mapped IO region that is used to translate
  187. * memory cycles to IO cycles by the processor.
  188. */
  189. EFI_MMAP_IO_PORT,
  190. /*
  191. * Address space reserved by the firmware for code that is
  192. * part of the processor.
  193. */
  194. EFI_PAL_CODE,
  195. /*
  196. * Byte addressable non-volatile memory.
  197. */
  198. EFI_PERSISTENT_MEMORY_TYPE,
  199. /*
  200. * Unaccepted memory must be accepted by boot target before usage.
  201. */
  202. EFI_UNACCEPTED_MEMORY_TYPE,
  203. EFI_MAX_MEMORY_TYPE,
  204. };
  205. /* Attribute values */
  206. #define EFI_MEMORY_UC ((u64)0x0000000000000001ULL) /* uncached */
  207. #define EFI_MEMORY_WC ((u64)0x0000000000000002ULL) /* write-coalescing */
  208. #define EFI_MEMORY_WT ((u64)0x0000000000000004ULL) /* write-through */
  209. #define EFI_MEMORY_WB ((u64)0x0000000000000008ULL) /* write-back */
  210. #define EFI_MEMORY_UCE ((u64)0x0000000000000010ULL) /* uncached, exported */
  211. #define EFI_MEMORY_WP ((u64)0x0000000000001000ULL) /* write-protect */
  212. #define EFI_MEMORY_RP ((u64)0x0000000000002000ULL) /* read-protect */
  213. #define EFI_MEMORY_XP ((u64)0x0000000000004000ULL) /* execute-protect */
  214. #define EFI_MEMORY_NV ((u64)0x0000000000008000ULL) /* non-volatile */
  215. #define EFI_MEMORY_MORE_RELIABLE \
  216. ((u64)0x0000000000010000ULL) /* higher reliability */
  217. #define EFI_MEMORY_RO ((u64)0x0000000000020000ULL) /* read-only */
  218. #define EFI_MEMORY_SP ((u64)0x0000000000040000ULL) /* specific-purpose memory (SPM) */
  219. #define EFI_MEMORY_CPU_CRYPTO ((u64)0x0000000000080000ULL) /* cryptographically protectable */
  220. #define EFI_MEMORY_RUNTIME ((u64)0x8000000000000000ULL) /* range requires runtime mapping */
  221. #define EFI_MEM_DESC_VERSION 1
  222. #define EFI_PAGE_SHIFT 12
  223. #define EFI_PAGE_SIZE (1ULL << EFI_PAGE_SHIFT)
  224. #define EFI_PAGE_MASK (EFI_PAGE_SIZE - 1)
  225. struct efi_mem_desc {
  226. u32 type;
  227. u32 reserved;
  228. efi_physical_addr_t physical_start;
  229. efi_virtual_addr_t virtual_start;
  230. u64 num_pages;
  231. u64 attribute;
  232. };
  233. #define EFI_MEMORY_DESCRIPTOR_VERSION 1
  234. /* Types and defines for Time Services */
  235. #define EFI_TIME_ADJUST_DAYLIGHT 0x1
  236. #define EFI_TIME_IN_DAYLIGHT 0x2
  237. #define EFI_UNSPECIFIED_TIMEZONE 0x07ff
  238. struct efi_time {
  239. u16 year;
  240. u8 month;
  241. u8 day;
  242. u8 hour;
  243. u8 minute;
  244. u8 second;
  245. u8 pad1;
  246. u32 nanosecond;
  247. s16 timezone;
  248. u8 daylight;
  249. u8 pad2;
  250. };
  251. struct efi_time_cap {
  252. u32 resolution;
  253. u32 accuracy;
  254. u8 sets_to_zero;
  255. };
  256. enum efi_locate_search_type {
  257. ALL_HANDLES,
  258. BY_REGISTER_NOTIFY,
  259. BY_PROTOCOL
  260. };
  261. struct efi_open_protocol_info_entry {
  262. efi_handle_t agent_handle;
  263. efi_handle_t controller_handle;
  264. u32 attributes;
  265. u32 open_count;
  266. };
  267. enum efi_entry_t {
  268. EFIET_END, /* Signals this is the last (empty) entry */
  269. EFIET_MEMORY_MAP,
  270. EFIET_GOP_MODE,
  271. EFIET_SYS_TABLE,
  272. /* Number of entries */
  273. EFIET_MEMORY_COUNT,
  274. };
  275. #define EFI_TABLE_VERSION 1
  276. /**
  277. * struct efi_info_hdr - Header for the EFI info table
  278. *
  279. * @version: EFI_TABLE_VERSION
  280. * @hdr_size: Size of this struct in bytes
  281. * @total_size: Total size of this header plus following data
  282. * @spare: Spare space for expansion
  283. */
  284. struct efi_info_hdr {
  285. u32 version;
  286. u32 hdr_size;
  287. u32 total_size;
  288. u32 spare[5];
  289. };
  290. /**
  291. * struct efi_entry_hdr - Header for a table entry
  292. *
  293. * @type: enum eft_entry_t
  294. * @size size of entry bytes excluding header and padding
  295. * @addr: address of this entry (0 if it follows the header )
  296. * @link: size of entry including header and padding
  297. * @spare1: Spare space for expansion
  298. * @spare2: Spare space for expansion
  299. */
  300. struct efi_entry_hdr {
  301. u32 type;
  302. u32 size;
  303. u64 addr;
  304. u32 link;
  305. u32 spare1;
  306. u64 spare2;
  307. };
  308. /**
  309. * struct efi_entry_memmap - a memory map table passed to U-Boot
  310. *
  311. * @version: EFI's memory map table version
  312. * @desc_size: EFI's size of each memory descriptor
  313. * @spare: Spare space for expansion
  314. * @desc: An array of descriptors, each @desc_size bytes apart
  315. */
  316. struct efi_entry_memmap {
  317. u32 version;
  318. u32 desc_size;
  319. u64 spare;
  320. struct efi_mem_desc desc[];
  321. };
  322. /**
  323. * struct efi_entry_gopmode - a GOP mode table passed to U-Boot
  324. *
  325. * @fb_base: EFI's framebuffer base address
  326. * @fb_size: EFI's framebuffer size
  327. * @info_size: GOP mode info structure size
  328. * @info: Start address of the GOP mode info structure
  329. */
  330. struct efi_entry_gopmode {
  331. efi_physical_addr_t fb_base;
  332. /*
  333. * Not like the ones in 'struct efi_gop_mode' which are 'unsigned
  334. * long', @fb_size and @info_size have to be 'u64' here. As the EFI
  335. * stub codes may have different bit size from the U-Boot payload,
  336. * using 'long' will cause mismatch between the producer (stub) and
  337. * the consumer (payload).
  338. */
  339. u64 fb_size;
  340. u64 info_size;
  341. /*
  342. * We cannot directly use 'struct efi_gop_mode_info info[]' here as
  343. * it causes compiler to complain: array type has incomplete element
  344. * type 'struct efi_gop_mode_info'.
  345. */
  346. struct /* efi_gop_mode_info */ {
  347. u32 version;
  348. u32 width;
  349. u32 height;
  350. u32 pixel_format;
  351. u32 pixel_bitmask[4];
  352. u32 pixels_per_scanline;
  353. } info[];
  354. };
  355. /**
  356. * struct efi_entry_systable - system table passed to U-Boot
  357. *
  358. * @sys_table: EFI system table address
  359. */
  360. struct efi_entry_systable {
  361. efi_physical_addr_t sys_table;
  362. };
  363. static inline struct efi_mem_desc *efi_get_next_mem_desc(
  364. struct efi_entry_memmap *map, struct efi_mem_desc *desc)
  365. {
  366. return (struct efi_mem_desc *)((ulong)desc + map->desc_size);
  367. }
  368. struct efi_priv {
  369. efi_handle_t parent_image;
  370. struct efi_device_path *device_path;
  371. struct efi_system_table *sys_table;
  372. struct efi_boot_services *boot;
  373. struct efi_runtime_services *run;
  374. bool use_pool_for_malloc;
  375. unsigned long ram_base;
  376. unsigned int image_data_type;
  377. struct efi_info_hdr *info;
  378. unsigned int info_size;
  379. void *next_hdr;
  380. };
  381. /* Base address of the EFI image */
  382. extern char image_base[];
  383. /* Start and end of U-Boot image (for payload) */
  384. extern char _binary_u_boot_bin_start[], _binary_u_boot_bin_end[];
  385. /*
  386. * Variable Attributes
  387. */
  388. #define EFI_VARIABLE_NON_VOLATILE 0x0000000000000001
  389. #define EFI_VARIABLE_BOOTSERVICE_ACCESS 0x0000000000000002
  390. #define EFI_VARIABLE_RUNTIME_ACCESS 0x0000000000000004
  391. #define EFI_VARIABLE_HARDWARE_ERROR_RECORD 0x0000000000000008
  392. #define EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS 0x0000000000000010
  393. #define EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS 0x0000000000000020
  394. #define EFI_VARIABLE_APPEND_WRITE 0x0000000000000040
  395. #define EFI_VARIABLE_MASK (EFI_VARIABLE_NON_VOLATILE | \
  396. EFI_VARIABLE_BOOTSERVICE_ACCESS | \
  397. EFI_VARIABLE_RUNTIME_ACCESS | \
  398. EFI_VARIABLE_HARDWARE_ERROR_RECORD | \
  399. EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | \
  400. EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | \
  401. EFI_VARIABLE_APPEND_WRITE)
  402. /**
  403. * efi_get_sys_table() - Get access to the main EFI system table
  404. *
  405. * @return pointer to EFI system table
  406. */
  407. struct efi_system_table *efi_get_sys_table(void);
  408. /**
  409. * efi_get_ram_base() - Find the base of RAM
  410. *
  411. * This is used when U-Boot is built as an EFI application.
  412. *
  413. * @return the base of RAM as known to U-Boot
  414. */
  415. unsigned long efi_get_ram_base(void);
  416. /**
  417. * efi_init() - Set up ready for use of EFI boot services
  418. *
  419. * @priv: Pointer to our private EFI structure to fill in
  420. * @banner: Banner to display when starting
  421. * @image: The image handle passed to efi_main()
  422. * @sys_table: The EFI system table pointer passed to efi_main()
  423. */
  424. int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
  425. struct efi_system_table *sys_table);
  426. /**
  427. * efi_malloc() - Allocate some memory from EFI
  428. *
  429. * @priv: Pointer to private EFI structure
  430. * @size: Number of bytes to allocate
  431. * @retp: Return EFI status result
  432. * @return pointer to memory allocated, or NULL on error
  433. */
  434. void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp);
  435. /**
  436. * efi_free() - Free memory allocated from EFI
  437. *
  438. * @priv: Pointer to private EFI structure
  439. * @ptr: Pointer to memory to free
  440. */
  441. void efi_free(struct efi_priv *priv, void *ptr);
  442. /**
  443. * efi_puts() - Write out a string to the EFI console
  444. *
  445. * @priv: Pointer to private EFI structure
  446. * @str: String to write (note this is a ASCII, not unicode)
  447. */
  448. void efi_puts(struct efi_priv *priv, const char *str);
  449. /**
  450. * efi_putc() - Write out a character to the EFI console
  451. *
  452. * @priv: Pointer to private EFI structure
  453. * @ch: Character to write (note this is not unicode)
  454. */
  455. void efi_putc(struct efi_priv *priv, const char ch);
  456. /**
  457. * efi_info_get() - get an entry from an EFI table
  458. *
  459. * @type: Entry type to search for
  460. * @datap: Returns pointer to entry data
  461. * @sizep: Returns pointer to entry size
  462. * @return 0 if OK, -ENODATA if there is no table, -ENOENT if there is no entry
  463. * of the requested type, -EPROTONOSUPPORT if the table has the wrong version
  464. */
  465. int efi_info_get(enum efi_entry_t type, void **datap, int *sizep);
  466. #endif /* _LINUX_EFI_H */