efi.h 13 KB

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