]> git.proxmox.com Git - efi-boot-shim.git/blob - shim.h
Merge branch 'fixes20240429' into 'master'
[efi-boot-shim.git] / shim.h
1 // SPDX-License-Identifier: BSD-2-Clause-Patent
2
3 #ifndef SHIM_H_
4 #define SHIM_H_
5
6 #ifdef SHIM_UNIT_TEST
7 #define _GNU_SOURCE
8 #endif
9
10 #if defined __GNUC__ && defined __GNUC_MINOR__
11 # define GNUC_PREREQ(maj, min) \
12 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
13 #else
14 # define GNUC_PREREQ(maj, min) 0
15 #endif
16 #if defined __clang_major__ && defined __clang_minor__
17 # define CLANG_PREREQ(maj, min) \
18 ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
19 #else
20 # define CLANG_PREREQ(maj, min) 0
21 #endif
22
23 #if defined(__x86_64__)
24 /* gcc 4.5.4 is the first documented release with -mabi=ms */
25 /* gcc 4.7.1 is the first one with __builtin_ms_va_list */
26 #if !GNUC_PREREQ(4, 7) && !CLANG_PREREQ(3, 4)
27 #error On x86_64 you must have a compiler new enough to support __attribute__((__ms_abi__))
28 #endif
29
30 #if CLANG_PREREQ(3, 4)
31 #pragma GCC diagnostic ignored "-Wpointer-bool-conversion"
32 #endif
33
34 #if !defined(GNU_EFI_USE_EXTERNAL_STDARG)
35 #define GNU_EFI_USE_EXTERNAL_STDARG
36 #endif
37
38 #if !defined(GNU_EFI_USE_MS_ABI)
39 #define GNU_EFI_USE_MS_ABI
40 #endif
41
42 #ifdef NO_BUILTIN_VA_FUNCS
43 #undef NO_BUILTIN_VA_FUNCS
44 #endif
45 #endif
46
47 #include <ctype.h>
48 #include <stdbool.h>
49 #include <stddef.h>
50 #include <stdint.h>
51 #include <stdarg.h>
52 #include <string.h>
53 #include <strings.h>
54
55 #ifndef SHIM_UNIT_TEST
56 #include <efi.h>
57 #include <efilib.h>
58 #undef uefi_call_wrapper
59 #include <efierr.h>
60 #include <efiip.h>
61
62 #if defined(__x86_64__) && !defined(HAVE_USE_MS_ABI)
63 #error something has gone wrong with the gnu-efi includes and defines
64 #endif
65 #endif
66
67 #ifdef SHIM_UNIT_TEST
68 #include "include/test.h"
69 #endif
70
71 #ifdef __x86_64__
72 #ifndef DEFAULT_LOADER
73 #define DEFAULT_LOADER L"\\grubx64.efi"
74 #endif
75 #ifndef DEFAULT_LOADER_CHAR
76 #define DEFAULT_LOADER_CHAR "\\grubx64.efi"
77 #endif
78 #ifndef EFI_ARCH
79 #define EFI_ARCH L"x64"
80 #endif
81 #ifndef DEBUGDIR
82 #define DEBUGDIR L"/usr/lib/debug/usr/share/shim/x64/"
83 #endif
84 #endif
85
86 #if defined(__i686__) || defined(__i386__)
87 #ifndef DEFAULT_LOADER
88 #define DEFAULT_LOADER L"\\grubia32.efi"
89 #endif
90 #ifndef DEFAULT_LOADER_CHAR
91 #define DEFAULT_LOADER_CHAR "\\grubia32.efi"
92 #endif
93 #ifndef EFI_ARCH
94 #define EFI_ARCH L"ia32"
95 #endif
96 #ifndef DEBUGDIR
97 #define DEBUGDIR L"/usr/lib/debug/usr/share/shim/ia32/"
98 #endif
99 #endif
100
101 #if defined(__aarch64__)
102 #ifndef DEFAULT_LOADER
103 #define DEFAULT_LOADER L"\\grubaa64.efi"
104 #endif
105 #ifndef DEFAULT_LOADER_CHAR
106 #define DEFAULT_LOADER_CHAR "\\grubaa64.efi"
107 #endif
108 #ifndef EFI_ARCH
109 #define EFI_ARCH L"aa64"
110 #endif
111 #ifndef DEBUGDIR
112 #define DEBUGDIR L"/usr/lib/debug/usr/share/shim/aa64/"
113 #endif
114 #endif
115
116 #if defined(__arm__)
117 #ifndef DEFAULT_LOADER
118 #define DEFAULT_LOADER L"\\grubarm.efi"
119 #endif
120 #ifndef DEFAULT_LOADER_CHAR
121 #define DEFAULT_LOADER_CHAR "\\grubarm.efi"
122 #endif
123 #ifndef EFI_ARCH
124 #define EFI_ARCH L"arm"
125 #endif
126 #ifndef DEBUGDIR
127 #define DEBUGDIR L"/usr/lib/debug/usr/share/shim/arm/"
128 #endif
129 #endif
130
131 #ifndef DEBUGSRC
132 #define DEBUGSRC L"/usr/src/debug/shim-" VERSIONSTR "." EFI_ARCH
133 #endif
134
135 #define FALLBACK L"\\fb" EFI_ARCH L".efi"
136 #define MOK_MANAGER L"\\mm" EFI_ARCH L".efi"
137
138 #if defined(VENDOR_DB_FILE)
139 # define vendor_authorized vendor_db
140 # define vendor_authorized_size vendor_db_size
141 # define vendor_authorized_category VENDOR_ADDEND_DB
142 #elif defined(VENDOR_CERT_FILE)
143 # define vendor_authorized vendor_cert
144 # define vendor_authorized_size vendor_cert_size
145 # define vendor_authorized_category VENDOR_ADDEND_X509
146 #else
147 # define vendor_authorized vendor_null
148 # define vendor_authorized_size vendor_null_size
149 # define vendor_authorized_category VENDOR_ADDEND_NONE
150 #endif
151
152 #if defined(VENDOR_DBX_FILE)
153 # define vendor_deauthorized vendor_dbx
154 # define vendor_deauthorized_size vendor_dbx_size
155 #else
156 # define vendor_deauthorized vendor_deauthorized_null
157 # define vendor_deauthorized_size vendor_deauthorized_null_size
158 #endif
159
160 #include "include/asm.h"
161 #include "include/compiler.h"
162 #include "include/list.h"
163 #include "include/configtable.h"
164 #include "include/console.h"
165 #include "include/crypt_blowfish.h"
166 #include "include/efiauthenticated.h"
167 #include "include/errors.h"
168 #include "include/execute.h"
169 #include "include/guid.h"
170 #include "include/http.h"
171 #include "include/httpboot.h"
172 #include "include/ip4config2.h"
173 #include "include/ip6config.h"
174 #include "include/load-options.h"
175 #include "include/mok.h"
176 #include "include/netboot.h"
177 #include "include/passwordcrypt.h"
178 #include "include/peimage.h"
179 #include "include/pe.h"
180 #include "include/replacements.h"
181 #include "include/sbat.h"
182 #include "include/sbat_var_defs.h"
183 #if defined(OVERRIDE_SECURITY_POLICY)
184 #include "include/security_policy.h"
185 #endif
186 #include "include/simple_file.h"
187 #include "include/str.h"
188 #include "include/tpm.h"
189 #include "include/cc.h"
190 #include "include/ucs2.h"
191 #include "include/variables.h"
192 #include "include/hexdump.h"
193
194 #include "version.h"
195
196 #ifndef SHIM_UNIT_TEST
197 #include "Cryptlib/Include/OpenSslSupport.h"
198 #endif
199
200 #define MEM_ATTR_R 4
201 #define MEM_ATTR_W 2
202 #define MEM_ATTR_X 1
203
204 INTERFACE_DECL(_SHIM_LOCK);
205
206 typedef
207 EFI_STATUS
208 (*EFI_SHIM_LOCK_VERIFY) (
209 IN VOID *buffer,
210 IN UINT32 size
211 );
212
213 typedef
214 EFI_STATUS
215 (*EFI_SHIM_LOCK_HASH) (
216 IN char *data,
217 IN int datasize,
218 PE_COFF_LOADER_IMAGE_CONTEXT *context,
219 UINT8 *sha256hash,
220 UINT8 *sha1hash
221 );
222
223 typedef
224 EFI_STATUS
225 (*EFI_SHIM_LOCK_CONTEXT) (
226 IN VOID *data,
227 IN unsigned int datasize,
228 PE_COFF_LOADER_IMAGE_CONTEXT *context
229 );
230
231 typedef struct _SHIM_LOCK {
232 EFI_SHIM_LOCK_VERIFY Verify;
233 EFI_SHIM_LOCK_HASH Hash;
234 EFI_SHIM_LOCK_CONTEXT Context;
235 } SHIM_LOCK;
236
237 extern EFI_STATUS shim_init(void);
238 extern void shim_fini(void);
239 extern EFI_STATUS EFIAPI LogError_(const char *file, int line, const char *func,
240 const CHAR16 *fmt, ...);
241 extern EFI_STATUS EFIAPI VLogError(const char *file, int line, const char *func,
242 const CHAR16 *fmt, ms_va_list args);
243 extern VOID LogHexdump_(const char *file, int line, const char *func,
244 const void *data, size_t sz);
245 extern VOID PrintErrors(VOID);
246 extern VOID ClearErrors(VOID);
247 extern VOID restore_loaded_image(VOID);
248 extern EFI_STATUS start_image(EFI_HANDLE image_handle, CHAR16 *ImagePath);
249 extern EFI_STATUS import_mok_state(EFI_HANDLE image_handle);
250
251 extern UINT32 vendor_authorized_size;
252 extern UINT8 *vendor_authorized;
253
254 extern UINT32 vendor_deauthorized_size;
255 extern UINT8 *vendor_deauthorized;
256
257 extern UINT32 user_cert_size;
258 extern UINT8 *user_cert;
259
260 #if defined(ENABLE_SHIM_CERT)
261 extern UINT32 build_cert_size;
262 extern UINT8 *build_cert;
263 #endif /* defined(ENABLE_SHIM_CERT) */
264
265 extern UINT8 user_insecure_mode;
266 extern UINT8 ignore_db;
267 extern UINT8 trust_mok_list;
268 extern UINT8 mok_policy;
269
270 extern UINT8 in_protocol;
271 extern void *load_options;
272 extern UINT32 load_options_size;
273
274 BOOLEAN secure_mode (void);
275
276 EFI_STATUS
277 verify_buffer (char *data, int datasize,
278 PE_COFF_LOADER_IMAGE_CONTEXT *context,
279 UINT8 *sha256hash, UINT8 *sha1hash);
280
281 #ifndef SHIM_UNIT_TEST
282 #define perror_(file, line, func, fmt, ...) ({ \
283 UINTN __perror_ret = 0; \
284 if (!in_protocol) \
285 __perror_ret = console_print((fmt), ##__VA_ARGS__); \
286 LogError_(file, line, func, fmt, ##__VA_ARGS__); \
287 __perror_ret; \
288 })
289 #define perror(fmt, ...) \
290 perror_(__FILE__, __LINE__ - 1, __func__, fmt, ##__VA_ARGS__)
291 #define LogError(fmt, ...) \
292 LogError_(__FILE__, __LINE__ - 1, __func__, fmt, ##__VA_ARGS__)
293 #else
294 #define perror(fmt, ...)
295 #define LogError(fmt, ...)
296 #endif
297
298 #ifdef ENABLE_SHIM_DEVEL
299 #define FALLBACK_VERBOSE_VAR_NAME L"FALLBACK_DEVEL_VERBOSE"
300 #define VERBOSE_VAR_NAME L"SHIM_DEVEL_VERBOSE"
301 #define DEBUG_VAR_NAME L"SHIM_DEVEL_DEBUG"
302 #else
303 #define FALLBACK_VERBOSE_VAR_NAME L"FALLBACK_VERBOSE"
304 #define VERBOSE_VAR_NAME L"SHIM_VERBOSE"
305 #define DEBUG_VAR_NAME L"SHIM_DEBUG"
306 #endif
307
308 char *translate_slashes(char *out, const char *str);
309
310 #endif /* SHIM_H_ */