dragonstub.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "types.h"
  11. #include "linux/div64.h"
  12. #include "limits.h"
  13. /// @brief
  14. /// @param image
  15. /// @param cmdline_ptr
  16. /// @return
  17. EFI_STATUS efi_handle_cmdline(EFI_LOADED_IMAGE *image, char **cmdline_ptr);
  18. char *efi_convert_cmdline(EFI_LOADED_IMAGE *image, int *cmd_line_len);
  19. #define efi_table_attr(inst, attr) (inst->attr)
  20. typedef u32 efi_tcg2_event_log_format;
  21. #define INITRD_EVENT_TAG_ID 0x8F3B22ECU
  22. #define LOAD_OPTIONS_EVENT_TAG_ID 0x8F3B22EDU
  23. #define EV_EVENT_TAG 0x00000006U
  24. #define EFI_TCG2_EVENT_HEADER_VERSION 0x1
  25. struct efi_tcg2_event {
  26. u32 event_size;
  27. struct {
  28. u32 header_size;
  29. u16 header_version;
  30. u32 pcr_index;
  31. u32 event_type;
  32. } __packed event_header;
  33. /* u8[] event follows here */
  34. } __packed;
  35. struct efi_tcg2_tagged_event {
  36. u32 tagged_event_id;
  37. u32 tagged_event_data_size;
  38. /* u8 tagged event data follows here */
  39. } __packed;
  40. typedef struct efi_tcg2_event efi_tcg2_event_t;
  41. typedef struct efi_tcg2_tagged_event efi_tcg2_tagged_event_t;
  42. typedef union efi_tcg2_protocol efi_tcg2_protocol_t;
  43. union efi_tcg2_protocol {
  44. struct {
  45. void *get_capability;
  46. efi_status_t(__efiapi *get_event_log)(efi_tcg2_protocol_t *,
  47. efi_tcg2_event_log_format,
  48. efi_physical_addr_t *,
  49. efi_physical_addr_t *,
  50. efi_bool_t *);
  51. efi_status_t(__efiapi *hash_log_extend_event)(
  52. efi_tcg2_protocol_t *, u64, efi_physical_addr_t, u64,
  53. const efi_tcg2_event_t *);
  54. void *submit_command;
  55. void *get_active_pcr_banks;
  56. void *set_active_pcr_banks;
  57. void *get_result_of_set_active_pcr_banks;
  58. };
  59. struct {
  60. u32 get_capability;
  61. u32 get_event_log;
  62. u32 hash_log_extend_event;
  63. u32 submit_command;
  64. u32 get_active_pcr_banks;
  65. u32 set_active_pcr_banks;
  66. u32 get_result_of_set_active_pcr_banks;
  67. } mixed_mode;
  68. };
  69. struct riscv_efi_boot_protocol {
  70. u64 revision;
  71. efi_status_t(__efiapi *get_boot_hartid)(
  72. struct riscv_efi_boot_protocol *, unsigned long *boot_hartid);
  73. };
  74. typedef struct {
  75. u32 attributes;
  76. u16 file_path_list_length;
  77. u8 variable_data[];
  78. // efi_char16_t description[];
  79. // efi_device_path_protocol_t file_path_list[];
  80. // u8 optional_data[];
  81. } __packed efi_load_option_t;
  82. typedef struct efi_generic_dev_path efi_device_path_protocol_t;
  83. #define EFI_LOAD_OPTION_ACTIVE 0x0001U
  84. #define EFI_LOAD_OPTION_FORCE_RECONNECT 0x0002U
  85. #define EFI_LOAD_OPTION_HIDDEN 0x0008U
  86. #define EFI_LOAD_OPTION_CATEGORY 0x1f00U
  87. #define EFI_LOAD_OPTION_CATEGORY_BOOT 0x0000U
  88. #define EFI_LOAD_OPTION_CATEGORY_APP 0x0100U
  89. #define EFI_LOAD_OPTION_BOOT_MASK \
  90. (EFI_LOAD_OPTION_ACTIVE | EFI_LOAD_OPTION_HIDDEN | \
  91. EFI_LOAD_OPTION_CATEGORY)
  92. #define EFI_LOAD_OPTION_MASK \
  93. (EFI_LOAD_OPTION_FORCE_RECONNECT | EFI_LOAD_OPTION_BOOT_MASK)
  94. typedef struct {
  95. u32 attributes;
  96. u16 file_path_list_length;
  97. const efi_char16_t *description;
  98. const efi_device_path_protocol_t *file_path_list;
  99. u32 optional_data_size;
  100. const void *optional_data;
  101. } efi_load_option_unpacked_t;
  102. typedef EFI_LOADED_IMAGE efi_loaded_image_t;
  103. /* The macro below handles dispatching via the thunk if needed */
  104. #define efi_fn_call(inst, func, ...) ((inst)->func(__VA_ARGS__))
  105. #define efi_call_proto(inst, func, ...) \
  106. ({ \
  107. __typeof__(inst) __inst = (inst); \
  108. efi_fn_call(__inst, func, __inst, ##__VA_ARGS__); \
  109. })
  110. /*
  111. * This function handles the architcture specific differences between arm and
  112. * arm64 regarding where the kernel image must be loaded and any memory that
  113. * must be reserved. On failure it is required to free all
  114. * all allocations it has made.
  115. */
  116. efi_status_t
  117. handle_kernel_image(unsigned long *image_addr, unsigned long *image_size,
  118. unsigned long *reserve_addr, unsigned long *reserve_size,
  119. efi_loaded_image_t *image, efi_handle_t image_handle);
  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. char *next_arg(char *args, char **param, char **val);
  143. /**
  144. * strstarts - does @str start with @prefix?
  145. * @str: string to examine
  146. * @prefix: prefix to look for.
  147. */
  148. static inline bool strstarts(const char *str, const char *prefix)
  149. {
  150. return strncmp(str, prefix, strlen(prefix)) == 0;
  151. }
  152. efi_status_t efi_parse_options(char const *cmdline);
  153. /// @brief 要加载的内核负载信息
  154. struct payload_info {
  155. /// @brief 负载起始地址
  156. u64 payload_addr;
  157. /// @brief 负载大小
  158. u64 payload_size;
  159. };
  160. /// @brief 寻找要加载的内核负载
  161. /// @param handle efi_handle
  162. /// @param image efi_loaded_image_t
  163. /// @param ret_info 返回的负载信息
  164. /// @return
  165. efi_status_t find_payload(efi_handle_t handle, efi_loaded_image_t *loaded_image,
  166. struct payload_info *ret_info);
  167. /* shared entrypoint between the normal stub and the zboot stub */
  168. efi_status_t efi_stub_common(efi_handle_t handle,
  169. struct payload_info *payload_info,
  170. char *cmdline_ptr);
  171. efi_status_t check_platform_features(void);
  172. void *get_efi_config_table(efi_guid_t guid);
  173. typedef EFI_CONFIGURATION_TABLE efi_config_table_t;
  174. static inline int efi_guidcmp(efi_guid_t left, efi_guid_t right)
  175. {
  176. return memcmp(&left, &right, sizeof(efi_guid_t));
  177. }
  178. static inline char *efi_guid_to_str(efi_guid_t *guid, char *out)
  179. {
  180. snprintf(out, 1024, "%pUl", &guid->Data1);
  181. return out;
  182. }
  183. static inline void print_efi_guid(efi_guid_t *guid)
  184. {
  185. efi_info("GUID: data1: %p data2: %p data3: %p data4: %p\n", guid->Data1,
  186. guid->Data2, guid->Data3, guid->Data4);
  187. }