dragonstub.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #pragma once
  2. #include <efi.h>
  3. #include <efilib.h>
  4. #include "printk.h"
  5. #include "linux-efi.h"
  6. #include "compiler_attributes.h"
  7. #include "linux/ctype.h"
  8. #include <lib.h>
  9. #include <dragonstub/linux/hex.h>
  10. #include <dragonstub/minmax.h>
  11. #include "types.h"
  12. #include "linux/div64.h"
  13. #include "limits.h"
  14. #include "linux/sizes.h"
  15. /// @brief
  16. /// @param image
  17. /// @param cmdline_ptr
  18. /// @return
  19. EFI_STATUS efi_handle_cmdline(EFI_LOADED_IMAGE *image, char **cmdline_ptr);
  20. char *efi_convert_cmdline(EFI_LOADED_IMAGE *image, int *cmd_line_len);
  21. #define efi_table_attr(inst, attr) (inst->attr)
  22. typedef u32 efi_tcg2_event_log_format;
  23. #define INITRD_EVENT_TAG_ID 0x8F3B22ECU
  24. #define LOAD_OPTIONS_EVENT_TAG_ID 0x8F3B22EDU
  25. #define EV_EVENT_TAG 0x00000006U
  26. #define EFI_TCG2_EVENT_HEADER_VERSION 0x1
  27. struct efi_tcg2_event {
  28. u32 event_size;
  29. struct {
  30. u32 header_size;
  31. u16 header_version;
  32. u32 pcr_index;
  33. u32 event_type;
  34. } __packed event_header;
  35. /* u8[] event follows here */
  36. } __packed;
  37. struct efi_tcg2_tagged_event {
  38. u32 tagged_event_id;
  39. u32 tagged_event_data_size;
  40. /* u8 tagged event data follows here */
  41. } __packed;
  42. typedef struct efi_tcg2_event efi_tcg2_event_t;
  43. typedef struct efi_tcg2_tagged_event efi_tcg2_tagged_event_t;
  44. typedef union efi_tcg2_protocol efi_tcg2_protocol_t;
  45. union efi_tcg2_protocol {
  46. struct {
  47. void *get_capability;
  48. efi_status_t(__efiapi *get_event_log)(efi_tcg2_protocol_t *,
  49. efi_tcg2_event_log_format,
  50. efi_physical_addr_t *,
  51. efi_physical_addr_t *,
  52. efi_bool_t *);
  53. efi_status_t(__efiapi *hash_log_extend_event)(
  54. efi_tcg2_protocol_t *, u64, efi_physical_addr_t, u64,
  55. const efi_tcg2_event_t *);
  56. void *submit_command;
  57. void *get_active_pcr_banks;
  58. void *set_active_pcr_banks;
  59. void *get_result_of_set_active_pcr_banks;
  60. };
  61. struct {
  62. u32 get_capability;
  63. u32 get_event_log;
  64. u32 hash_log_extend_event;
  65. u32 submit_command;
  66. u32 get_active_pcr_banks;
  67. u32 set_active_pcr_banks;
  68. u32 get_result_of_set_active_pcr_banks;
  69. } mixed_mode;
  70. };
  71. struct riscv_efi_boot_protocol {
  72. u64 revision;
  73. efi_status_t(__efiapi *get_boot_hartid)(
  74. struct riscv_efi_boot_protocol *, unsigned long *boot_hartid);
  75. };
  76. typedef struct {
  77. u32 attributes;
  78. u16 file_path_list_length;
  79. u8 variable_data[];
  80. // efi_char16_t description[];
  81. // efi_device_path_protocol_t file_path_list[];
  82. // u8 optional_data[];
  83. } __packed efi_load_option_t;
  84. typedef struct efi_generic_dev_path efi_device_path_protocol_t;
  85. #define EFI_LOAD_OPTION_ACTIVE 0x0001U
  86. #define EFI_LOAD_OPTION_FORCE_RECONNECT 0x0002U
  87. #define EFI_LOAD_OPTION_HIDDEN 0x0008U
  88. #define EFI_LOAD_OPTION_CATEGORY 0x1f00U
  89. #define EFI_LOAD_OPTION_CATEGORY_BOOT 0x0000U
  90. #define EFI_LOAD_OPTION_CATEGORY_APP 0x0100U
  91. #define EFI_LOAD_OPTION_BOOT_MASK \
  92. (EFI_LOAD_OPTION_ACTIVE | EFI_LOAD_OPTION_HIDDEN | \
  93. EFI_LOAD_OPTION_CATEGORY)
  94. #define EFI_LOAD_OPTION_MASK \
  95. (EFI_LOAD_OPTION_FORCE_RECONNECT | EFI_LOAD_OPTION_BOOT_MASK)
  96. typedef struct {
  97. u32 attributes;
  98. u16 file_path_list_length;
  99. const efi_char16_t *description;
  100. const efi_device_path_protocol_t *file_path_list;
  101. u32 optional_data_size;
  102. const void *optional_data;
  103. } efi_load_option_unpacked_t;
  104. typedef EFI_LOADED_IMAGE efi_loaded_image_t;
  105. /* The macro below handles dispatching via the thunk if needed */
  106. #define efi_fn_call(inst, func, ...) ((inst)->func(__VA_ARGS__))
  107. #define efi_call_proto(inst, func, ...) \
  108. ({ \
  109. __typeof__(inst) __inst = (inst); \
  110. efi_fn_call(__inst, func, __inst, ##__VA_ARGS__); \
  111. })
  112. #define efi_rt_call(func, ...) \
  113. efi_fn_call(efi_table_attr(ST, RuntimeServices), func, ##__VA_ARGS__)
  114. #define get_efi_var(name, vendor, ...) \
  115. efi_rt_call(GetVariable, (efi_char16_t *)(name), \
  116. (efi_guid_t *)(vendor), __VA_ARGS__)
  117. #define set_efi_var(name, vendor, ...) \
  118. efi_rt_call(SetVariable, (efi_char16_t *)(name), \
  119. (efi_guid_t *)(vendor), __VA_ARGS__)
  120. char *skip_spaces(const char *str);
  121. long simple_strtol(const char *cp, char **endp, unsigned int base);
  122. unsigned int atou(const char *s);
  123. /**
  124. * simple_strtoull - convert a string to an unsigned long long
  125. * @cp: The start of the string
  126. * @endp: A pointer to the end of the parsed string will be placed here
  127. * @base: The number base to use
  128. */
  129. unsigned long long simple_strtoull(const char *cp, char **endp,
  130. unsigned int base);
  131. long simple_strtol(const char *cp, char **endp, unsigned int base);
  132. #define strtoul(cp, endp, base) simple_strtoull(cp, endp, base)
  133. size_t strnlen(const char *s, size_t maxlen);
  134. /**
  135. * strlen - Find the length of a string
  136. * @s: The string to be sized
  137. */
  138. size_t strlen(const char *s);
  139. int strncmp(const char *cs, const char *ct, size_t count);
  140. int strcmp(const char *str1, const char *str2);
  141. char *strchr(const char *s, int c);
  142. /**
  143. * strstr - Find the first substring in a %NUL terminated string
  144. * @s1: The string to be searched
  145. * @s2: The string to search for
  146. */
  147. char *strstr(const char *s1, const char *s2);
  148. char *next_arg(char *args, char **param, char **val);
  149. /**
  150. * strstarts - does @str start with @prefix?
  151. * @str: string to examine
  152. * @prefix: prefix to look for.
  153. */
  154. static inline bool strstarts(const char *str, const char *prefix)
  155. {
  156. return strncmp(str, prefix, strlen(prefix)) == 0;
  157. }
  158. efi_status_t efi_parse_options(char const *cmdline);
  159. /// @brief 要加载的内核负载信息
  160. struct payload_info {
  161. /// @brief 负载起始地址
  162. u64 payload_addr;
  163. /// @brief 负载大小
  164. u64 payload_size;
  165. /// @brief 被加载到的物理地址
  166. u64 loaded_paddr;
  167. /// @brief 加载了多大
  168. u64 loaded_size;
  169. /// @brief 加载的内核的入口物理地址
  170. u64 kernel_entry;
  171. };
  172. /// @brief 寻找要加载的内核负载
  173. /// @param handle efi_handle
  174. /// @param image efi_loaded_image_t
  175. /// @param ret_info 返回的负载信息
  176. /// @return
  177. efi_status_t find_payload(efi_handle_t handle, efi_loaded_image_t *loaded_image,
  178. struct payload_info *ret_info);
  179. /* shared entrypoint between the normal stub and the zboot stub */
  180. efi_status_t efi_stub_common(efi_handle_t handle, efi_loaded_image_t *image,
  181. struct payload_info *payload_info,
  182. char *cmdline_ptr);
  183. efi_status_t efi_boot_kernel(efi_handle_t handle, efi_loaded_image_t *image,
  184. struct payload_info *payload_info,
  185. char *cmdline_ptr);
  186. efi_status_t check_platform_features(void);
  187. void *get_efi_config_table(efi_guid_t guid);
  188. typedef EFI_CONFIGURATION_TABLE efi_config_table_t;
  189. static inline int efi_guidcmp(efi_guid_t left, efi_guid_t right)
  190. {
  191. return memcmp(&left, &right, sizeof(efi_guid_t));
  192. }
  193. static inline char *efi_guid_to_str(efi_guid_t *guid, char *out)
  194. {
  195. snprintf(out, 1024, "%pUl", &guid->Data1);
  196. return out;
  197. }
  198. static inline void print_efi_guid(efi_guid_t *guid)
  199. {
  200. efi_info("GUID: data1: %p data2: %p data3: %p data4: %p\n", guid->Data1,
  201. guid->Data2, guid->Data3, guid->Data4);
  202. }
  203. /*
  204. * efi_allocate_virtmap() - create a pool allocation for the virtmap
  205. *
  206. * Create an allocation that is of sufficient size to hold all the memory
  207. * descriptors that will be passed to SetVirtualAddressMap() to inform the
  208. * firmware about the virtual mapping that will be used under the OS to call
  209. * into the firmware.
  210. */
  211. efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,
  212. unsigned long *desc_size, u32 *desc_ver);
  213. /*
  214. * efi_get_virtmap() - create a virtual mapping for the EFI memory map
  215. *
  216. * This function populates the virt_addr fields of all memory region descriptors
  217. * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
  218. * are also copied to @runtime_map, and their total count is returned in @count.
  219. */
  220. void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
  221. unsigned long desc_size, efi_memory_desc_t *runtime_map,
  222. int *count);
  223. extern bool efi_nochunk;
  224. extern bool efi_nokaslr;
  225. extern bool efi_novamap;
  226. /*
  227. * Determine whether we're in secure boot mode.
  228. */
  229. enum efi_secureboot_mode efi_get_secureboot(void);
  230. void *get_fdt(unsigned long *fdt_size);
  231. /*
  232. * Allow the platform to override the allocation granularity: this allows
  233. * systems that have the capability to run with a larger page size to deal
  234. * with the allocations for initrd and fdt more efficiently.
  235. */
  236. #ifndef EFI_ALLOC_ALIGN
  237. #define EFI_ALLOC_ALIGN EFI_PAGE_SIZE
  238. #endif
  239. #ifndef EFI_ALLOC_LIMIT
  240. #define EFI_ALLOC_LIMIT ULONG_MAX
  241. #endif
  242. /*
  243. * Allocation types for calls to boottime->allocate_pages.
  244. */
  245. #define EFI_ALLOCATE_ANY_PAGES 0
  246. #define EFI_ALLOCATE_MAX_ADDRESS 1
  247. #define EFI_ALLOCATE_ADDRESS 2
  248. #define EFI_MAX_ALLOCATE_TYPE 3
  249. /**
  250. * efi_allocate_pages_aligned() - Allocate memory pages
  251. * @size: minimum number of bytes to allocate
  252. * @addr: On return the address of the first allocated page. The first
  253. * allocated page has alignment EFI_ALLOC_ALIGN which is an
  254. * architecture dependent multiple of the page size.
  255. * @max: the address that the last allocated memory page shall not
  256. * exceed
  257. * @align: minimum alignment of the base of the allocation
  258. *
  259. * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
  260. * to @align, which should be >= EFI_ALLOC_ALIGN. The last allocated page will
  261. * not exceed the address given by @max.
  262. *
  263. * Return: status code
  264. */
  265. efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
  266. unsigned long max, unsigned long align,
  267. int memory_type);
  268. /**
  269. * efi_allocate_pages() - Allocate memory pages
  270. * @size: minimum number of bytes to allocate
  271. * @addr: On return the address of the first allocated page. The first
  272. * allocated page has alignment EFI_ALLOC_ALIGN which is an
  273. * architecture dependent multiple of the page size.
  274. * @max: the address that the last allocated memory page shall not
  275. * exceed
  276. *
  277. * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
  278. * to EFI_ALLOC_ALIGN. The last allocated page will not exceed the address
  279. * given by @max.
  280. *
  281. * Return: status code
  282. */
  283. efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
  284. unsigned long max);
  285. /**
  286. * efi_allocate_pages_exact() - Allocate memory pages at a specific address
  287. * @size: minimum number of bytes to allocate
  288. * @addr: The address of the first allocated page.
  289. *
  290. * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
  291. * to EFI_ALLOC_ALIGN.
  292. *
  293. * Return: status code
  294. */
  295. efi_status_t efi_allocate_pages_exact(unsigned long size, unsigned long addr);
  296. /**
  297. * efi_free() - free memory pages
  298. * @size: size of the memory area to free in bytes
  299. * @addr: start of the memory area to free (must be EFI_PAGE_SIZE
  300. * aligned)
  301. *
  302. * @size is rounded up to a multiple of EFI_ALLOC_ALIGN which is an
  303. * architecture specific multiple of EFI_PAGE_SIZE. So this function should
  304. * only be used to return pages allocated with efi_allocate_pages() or
  305. * efi_low_alloc_above().
  306. */
  307. void efi_free(unsigned long size, unsigned long addr);
  308. /*
  309. * An efi_boot_memmap is used by efi_get_memory_map() to return the
  310. * EFI memory map in a dynamically allocated buffer.
  311. *
  312. * The buffer allocated for the EFI memory map includes extra room for
  313. * a minimum of EFI_MMAP_NR_SLACK_SLOTS additional EFI memory descriptors.
  314. * This facilitates the reuse of the EFI memory map buffer when a second
  315. * call to ExitBootServices() is needed because of intervening changes to
  316. * the EFI memory map. Other related structures, e.g. x86 e820ext, need
  317. * to factor in this headroom requirement as well.
  318. */
  319. #define EFI_MMAP_NR_SLACK_SLOTS 8
  320. /**
  321. * efi_get_memory_map() - get memory map
  322. * @map: pointer to memory map pointer to which to assign the
  323. * newly allocated memory map
  324. * @install_cfg_tbl: whether or not to install the boot memory map as a
  325. * configuration table
  326. *
  327. * Retrieve the UEFI memory map. The allocated memory leaves room for
  328. * up to EFI_MMAP_NR_SLACK_SLOTS additional memory map entries.
  329. *
  330. * Return: status code
  331. */
  332. efi_status_t efi_get_memory_map(struct efi_boot_memmap **map,
  333. bool install_cfg_tbl);
  334. #ifdef CONFIG_64BIT
  335. #define MAX_FDT_SIZE (1UL << 21)
  336. #else
  337. #error "MAX_FDT_SIZE not yet defined for 32-bit"
  338. #endif
  339. /* Helper macros for the usual case of using simple C variables: */
  340. #ifndef fdt_setprop_inplace_var
  341. #define fdt_setprop_inplace_var(fdt, node_offset, name, var) \
  342. fdt_setprop_inplace((fdt), (node_offset), (name), &(var), sizeof(var))
  343. #endif
  344. #ifndef fdt_setprop_var
  345. #define fdt_setprop_var(fdt, node_offset, name, var) \
  346. fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var))
  347. #endif
  348. #define efi_get_handle_at(array, idx) \
  349. (efi_is_native() ? (array)[idx] : \
  350. (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
  351. #define efi_get_handle_num(size) \
  352. ((size) / (efi_is_native() ? sizeof(efi_handle_t) : sizeof(u32)))
  353. /**
  354. * efi_get_random_bytes() - fill a buffer with random bytes
  355. * @size: size of the buffer
  356. * @out: caller allocated buffer to receive the random bytes
  357. *
  358. * The call will fail if either the firmware does not implement the
  359. * EFI_RNG_PROTOCOL or there are not enough random bytes available to fill
  360. * the buffer.
  361. *
  362. * Return: status code
  363. */
  364. efi_status_t efi_get_random_bytes(unsigned long size, u8 *out);
  365. typedef efi_status_t (*efi_exit_boot_map_processing)(
  366. struct efi_boot_memmap *map, void *priv);
  367. /**
  368. * efi_exit_boot_services() - Exit boot services
  369. * @handle: handle of the exiting image
  370. * @priv: argument to be passed to @priv_func
  371. * @priv_func: function to process the memory map before exiting boot services
  372. *
  373. * Handle calling ExitBootServices according to the requirements set out by the
  374. * spec. Obtains the current memory map, and returns that info after calling
  375. * ExitBootServices. The client must specify a function to perform any
  376. * processing of the memory map data prior to ExitBootServices. A client
  377. * specific structure may be passed to the function via priv. The client
  378. * function may be called multiple times.
  379. *
  380. * Return: status code
  381. */
  382. efi_status_t efi_exit_boot_services(void *handle, void *priv,
  383. efi_exit_boot_map_processing priv_func);
  384. void __noreturn efi_enter_kernel(struct payload_info *payload_info,
  385. unsigned long fdt, unsigned long fdt_size);
  386. typedef union efi_memory_attribute_protocol efi_memory_attribute_protocol_t;
  387. union efi_memory_attribute_protocol {
  388. struct {
  389. efi_status_t(__efiapi *get_memory_attributes)(
  390. efi_memory_attribute_protocol_t *, efi_physical_addr_t,
  391. u64, u64 *);
  392. efi_status_t(__efiapi *set_memory_attributes)(
  393. efi_memory_attribute_protocol_t *, efi_physical_addr_t,
  394. u64, u64);
  395. efi_status_t(__efiapi *clear_memory_attributes)(
  396. efi_memory_attribute_protocol_t *, efi_physical_addr_t,
  397. u64, u64);
  398. };
  399. struct {
  400. u32 get_memory_attributes;
  401. u32 set_memory_attributes;
  402. u32 clear_memory_attributes;
  403. } mixed_mode;
  404. };
  405. /**
  406. * 安装到efi config table的信息
  407. *
  408. * 表示dragonstub把内核加载到的地址和大小
  409. */
  410. struct dragonstub_payload_efi {
  411. u64 loaded_addr;
  412. u64 size;
  413. };
  414. #define DRAGONSTUB_EFI_PAYLOAD_EFI_GUID \
  415. MAKE_EFI_GUID(0xddf1d47c, 0x102c, 0xaaf9, 0xce, 0x34, 0xbc, 0xef, \
  416. 0x98, 0x12, 0x00, 0x31)