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