dragonstub.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /// @brief
  13. /// @param image
  14. /// @param cmdline_ptr
  15. /// @return
  16. EFI_STATUS efi_handle_cmdline(EFI_LOADED_IMAGE *image, char **cmdline_ptr);
  17. char *efi_convert_cmdline(EFI_LOADED_IMAGE *image, int *cmd_line_len);
  18. #define efi_table_attr(inst, attr) (inst->attr)
  19. typedef u32 efi_tcg2_event_log_format;
  20. #define INITRD_EVENT_TAG_ID 0x8F3B22ECU
  21. #define LOAD_OPTIONS_EVENT_TAG_ID 0x8F3B22EDU
  22. #define EV_EVENT_TAG 0x00000006U
  23. #define EFI_TCG2_EVENT_HEADER_VERSION 0x1
  24. struct efi_tcg2_event {
  25. u32 event_size;
  26. struct {
  27. u32 header_size;
  28. u16 header_version;
  29. u32 pcr_index;
  30. u32 event_type;
  31. } __packed event_header;
  32. /* u8[] event follows here */
  33. } __packed;
  34. struct efi_tcg2_tagged_event {
  35. u32 tagged_event_id;
  36. u32 tagged_event_data_size;
  37. /* u8 tagged event data follows here */
  38. } __packed;
  39. typedef struct efi_tcg2_event efi_tcg2_event_t;
  40. typedef struct efi_tcg2_tagged_event efi_tcg2_tagged_event_t;
  41. typedef union efi_tcg2_protocol efi_tcg2_protocol_t;
  42. union efi_tcg2_protocol {
  43. struct {
  44. void *get_capability;
  45. efi_status_t(__efiapi *get_event_log)(efi_tcg2_protocol_t *,
  46. efi_tcg2_event_log_format,
  47. efi_physical_addr_t *,
  48. efi_physical_addr_t *,
  49. efi_bool_t *);
  50. efi_status_t(__efiapi *hash_log_extend_event)(
  51. efi_tcg2_protocol_t *, u64, efi_physical_addr_t, u64,
  52. const efi_tcg2_event_t *);
  53. void *submit_command;
  54. void *get_active_pcr_banks;
  55. void *set_active_pcr_banks;
  56. void *get_result_of_set_active_pcr_banks;
  57. };
  58. struct {
  59. u32 get_capability;
  60. u32 get_event_log;
  61. u32 hash_log_extend_event;
  62. u32 submit_command;
  63. u32 get_active_pcr_banks;
  64. u32 set_active_pcr_banks;
  65. u32 get_result_of_set_active_pcr_banks;
  66. } mixed_mode;
  67. };
  68. struct riscv_efi_boot_protocol {
  69. u64 revision;
  70. efi_status_t(__efiapi *get_boot_hartid)(
  71. struct riscv_efi_boot_protocol *, unsigned long *boot_hartid);
  72. };
  73. typedef struct {
  74. u32 attributes;
  75. u16 file_path_list_length;
  76. u8 variable_data[];
  77. // efi_char16_t description[];
  78. // efi_device_path_protocol_t file_path_list[];
  79. // u8 optional_data[];
  80. } __packed efi_load_option_t;
  81. typedef struct efi_generic_dev_path efi_device_path_protocol_t;
  82. #define EFI_LOAD_OPTION_ACTIVE 0x0001U
  83. #define EFI_LOAD_OPTION_FORCE_RECONNECT 0x0002U
  84. #define EFI_LOAD_OPTION_HIDDEN 0x0008U
  85. #define EFI_LOAD_OPTION_CATEGORY 0x1f00U
  86. #define EFI_LOAD_OPTION_CATEGORY_BOOT 0x0000U
  87. #define EFI_LOAD_OPTION_CATEGORY_APP 0x0100U
  88. #define EFI_LOAD_OPTION_BOOT_MASK \
  89. (EFI_LOAD_OPTION_ACTIVE | EFI_LOAD_OPTION_HIDDEN | \
  90. EFI_LOAD_OPTION_CATEGORY)
  91. #define EFI_LOAD_OPTION_MASK \
  92. (EFI_LOAD_OPTION_FORCE_RECONNECT | EFI_LOAD_OPTION_BOOT_MASK)
  93. typedef struct {
  94. u32 attributes;
  95. u16 file_path_list_length;
  96. const efi_char16_t *description;
  97. const efi_device_path_protocol_t *file_path_list;
  98. u32 optional_data_size;
  99. const void *optional_data;
  100. } efi_load_option_unpacked_t;
  101. typedef EFI_LOADED_IMAGE efi_loaded_image_t;
  102. /* The macro below handles dispatching via the thunk if needed */
  103. #define efi_fn_call(inst, func, ...) ((inst)->func(__VA_ARGS__))
  104. #define efi_call_proto(inst, func, ...) \
  105. ({ \
  106. __typeof__(inst) __inst = (inst); \
  107. efi_fn_call(__inst, func, __inst, ##__VA_ARGS__); \
  108. })
  109. /*
  110. * This function handles the architcture specific differences between arm and
  111. * arm64 regarding where the kernel image must be loaded and any memory that
  112. * must be reserved. On failure it is required to free all
  113. * all allocations it has made.
  114. */
  115. efi_status_t
  116. handle_kernel_image(unsigned long *image_addr, unsigned long *image_size,
  117. unsigned long *reserve_addr, unsigned long *reserve_size,
  118. efi_loaded_image_t *image, efi_handle_t image_handle);
  119. char *skip_spaces(const char *str);
  120. long simple_strtol(const char *cp, char **endp, unsigned int base);
  121. unsigned int atou(const char *s);
  122. /**
  123. * simple_strtoull - convert a string to an unsigned long long
  124. * @cp: The start of the string
  125. * @endp: A pointer to the end of the parsed string will be placed here
  126. * @base: The number base to use
  127. */
  128. unsigned long long simple_strtoull(const char *cp, char **endp,
  129. unsigned int base);
  130. long simple_strtol(const char *cp, char **endp, unsigned int base);
  131. size_t strnlen(const char *s, size_t maxlen);
  132. /**
  133. * strlen - Find the length of a string
  134. * @s: The string to be sized
  135. */
  136. size_t strlen(const char *s);
  137. int strncmp(const char *cs, const char *ct, size_t count);
  138. int strcmp(const char *str1, const char *str2);
  139. char *next_arg(char *args, char **param, char **val);
  140. /**
  141. * strstarts - does @str start with @prefix?
  142. * @str: string to examine
  143. * @prefix: prefix to look for.
  144. */
  145. static inline bool strstarts(const char *str, const char *prefix)
  146. {
  147. return strncmp(str, prefix, strlen(prefix)) == 0;
  148. }
  149. efi_status_t efi_parse_options(char const *cmdline);
  150. /// @brief 要加载的内核负载信息
  151. struct payload_info {
  152. /// @brief 负载起始地址
  153. u64 payload_addr;
  154. /// @brief 负载大小
  155. u64 payload_size;
  156. };
  157. /// @brief 寻找要加载的内核负载
  158. /// @param handle efi_handle
  159. /// @param image efi_loaded_image_t
  160. /// @param ret_info 返回的负载信息
  161. /// @return
  162. efi_status_t find_payload(efi_handle_t handle, efi_loaded_image_t *loaded_image,
  163. struct payload_info *ret_info);