]> git.proxmox.com Git - efi-boot-shim.git/blob - test.c
46cab533534079cdcab05e565b3eaebfdb5af9e0
[efi-boot-shim.git] / test.c
1 // SPDX-License-Identifier: BSD-2-Clause-Patent
2 /*
3 * test.c - stuff we need for test harnesses
4 * Copyright Peter Jones <pjones@redhat.com>
5 */
6
7 #include "shim.h"
8
9 #include <execinfo.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #define BT_BUF_SIZE (4096/sizeof(void *))
14
15 static void *frames[BT_BUF_SIZE] = { 0, };
16
17 UINT8 in_protocol = 0;
18 int debug = DEFAULT_DEBUG_PRINT_STATE;
19
20 void
21 print_traceback(int skip)
22 {
23 int nptrs;
24 char **strings;
25
26 nptrs = backtrace(frames, BT_BUF_SIZE);
27 if (nptrs < skip)
28 return;
29
30 strings = backtrace_symbols(frames, nptrs);
31 for (int i = skip; strings != NULL && i < nptrs; i++) {
32 printf("%p %s\n", (void *)frames[i], strings[i]);
33 }
34 if (strings)
35 free(strings);
36 }
37
38 #pragma GCC diagnostic ignored "-Wunused-parameter"
39 #pragma GCC diagnostic ignored "-Wunused-function"
40
41 static EFI_STATUS EFIAPI
42 mock_efi_allocate_pages(EFI_ALLOCATE_TYPE type,
43 EFI_MEMORY_TYPE memory_type,
44 UINTN nmemb,
45 EFI_PHYSICAL_ADDRESS *memory)
46 {
47 /*
48 * XXX so far this does not honor the type at all, and there's no
49 * tracking for memory_type either.
50 */
51 *memory = (EFI_PHYSICAL_ADDRESS)(uintptr_t)calloc(nmemb, 4096);
52 if ((void *)(uintptr_t)(*memory) == NULL)
53 return EFI_OUT_OF_RESOURCES;
54
55 return EFI_SUCCESS;
56 }
57
58 static EFI_STATUS EFIAPI
59 mock_efi_free_pages(EFI_PHYSICAL_ADDRESS memory,
60 UINTN nmemb)
61 {
62 free((void *)(uintptr_t)memory);
63
64 return EFI_SUCCESS;
65 }
66
67 static EFI_STATUS EFIAPI
68 mock_efi_allocate_pool(EFI_MEMORY_TYPE pool_type,
69 UINTN size,
70 VOID **buf)
71 {
72 *buf = calloc(1, size);
73 if (*buf == NULL)
74 return EFI_OUT_OF_RESOURCES;
75
76 return EFI_SUCCESS;
77 }
78
79 static EFI_STATUS EFIAPI
80 mock_efi_free_pool(void *buf)
81 {
82 free(buf);
83
84 return EFI_SUCCESS;
85 }
86
87 void EFIAPI
88 mock_efi_void()
89 {
90 ;
91 }
92
93 EFI_STATUS EFIAPI
94 mock_efi_success()
95 {
96 return EFI_SUCCESS;
97 }
98
99 EFI_STATUS EFIAPI
100 mock_efi_unsupported()
101 {
102 return EFI_UNSUPPORTED;
103 }
104
105 EFI_STATUS EFIAPI
106 mock_efi_not_found()
107 {
108 return EFI_NOT_FOUND;
109 }
110
111 EFI_BOOT_SERVICES mock_bs, mock_default_bs = {
112 .Hdr = {
113 .Signature = EFI_BOOT_SERVICES_SIGNATURE,
114 .Revision = EFI_1_10_BOOT_SERVICES_REVISION,
115 .HeaderSize = offsetof(EFI_BOOT_SERVICES, SetMem)
116 + sizeof(mock_bs.SetMem),
117 },
118
119 .RaiseTPL = mock_efi_unsupported,
120 .RestoreTPL = mock_efi_void,
121
122 .AllocatePages = mock_efi_allocate_pages,
123 .FreePages = mock_efi_free_pages,
124 .GetMemoryMap = mock_efi_unsupported,
125 .AllocatePool = mock_efi_allocate_pool,
126 .FreePool = mock_efi_free_pool,
127
128 .CreateEvent = mock_efi_unsupported,
129 .SetTimer = mock_efi_unsupported,
130 .WaitForEvent = mock_efi_unsupported,
131 .SignalEvent = mock_efi_unsupported,
132 .CloseEvent = mock_efi_unsupported,
133 .CheckEvent = mock_efi_unsupported,
134
135 .InstallProtocolInterface = mock_efi_unsupported,
136 .ReinstallProtocolInterface = mock_efi_unsupported,
137 .UninstallProtocolInterface = mock_efi_unsupported,
138 .HandleProtocol = mock_efi_unsupported,
139 #if 0
140 /*
141 * EFI 1.10 has a "Reserved" field here that's not in later
142 * revisions.
143 *
144 * I don't think it's in any actual *firmware* either.
145 */
146 .Reserved = NULL,
147 #endif
148 .RegisterProtocolNotify = mock_efi_unsupported,
149 .LocateHandle = mock_efi_not_found,
150 .LocateDevicePath = mock_efi_unsupported,
151 .InstallConfigurationTable = mock_efi_unsupported,
152
153 .LoadImage = (void *)mock_efi_unsupported,
154 .StartImage = mock_efi_unsupported,
155 .Exit = mock_efi_unsupported,
156 .UnloadImage = mock_efi_unsupported,
157 .ExitBootServices = mock_efi_unsupported,
158
159 .GetNextMonotonicCount = mock_efi_unsupported,
160 .Stall = mock_efi_unsupported,
161 .SetWatchdogTimer = mock_efi_unsupported,
162
163 .ConnectController = (void *)mock_efi_unsupported,
164 .DisconnectController = mock_efi_unsupported,
165
166 .OpenProtocol = mock_efi_unsupported,
167 .CloseProtocol = mock_efi_unsupported,
168 .OpenProtocolInformation = mock_efi_unsupported,
169
170 .ProtocolsPerHandle = mock_efi_unsupported,
171 .LocateHandleBuffer = mock_efi_unsupported,
172 .LocateProtocol = mock_efi_unsupported,
173
174 .InstallMultipleProtocolInterfaces = (void *)mock_efi_unsupported,
175 .UninstallMultipleProtocolInterfaces = (void *)mock_efi_unsupported,
176
177 .CalculateCrc32 = mock_efi_unsupported,
178
179 .CopyMem = NULL,
180 .SetMem = NULL,
181 .CreateEventEx = mock_efi_unsupported,
182 };
183
184 EFI_RUNTIME_SERVICES mock_rt, mock_default_rt = {
185 .Hdr = {
186 .Signature = EFI_RUNTIME_SERVICES_SIGNATURE,
187 .Revision = EFI_1_10_RUNTIME_SERVICES_REVISION,
188 .HeaderSize = offsetof(EFI_RUNTIME_SERVICES, ResetSystem)
189 + sizeof(mock_rt.ResetSystem),
190 },
191
192 .GetTime = mock_efi_unsupported,
193 .SetTime = mock_efi_unsupported,
194 .GetWakeupTime = mock_efi_unsupported,
195 .SetWakeupTime = (void *)mock_efi_unsupported,
196
197 .SetVirtualAddressMap = mock_efi_unsupported,
198 .ConvertPointer = mock_efi_unsupported,
199
200 .GetVariable = mock_efi_unsupported,
201 .SetVariable = mock_efi_unsupported,
202 .GetNextVariableName = mock_efi_unsupported,
203
204 .GetNextHighMonotonicCount = mock_efi_unsupported,
205 .ResetSystem = mock_efi_unsupported,
206
207 .UpdateCapsule = mock_efi_unsupported,
208 .QueryCapsuleCapabilities = mock_efi_unsupported,
209
210 .QueryVariableInfo = mock_efi_unsupported,
211 };
212
213 EFI_SYSTEM_TABLE mock_st, mock_default_st = {
214 .Hdr = {
215 .Signature = EFI_SYSTEM_TABLE_SIGNATURE,
216 .Revision = EFI_1_10_SYSTEM_TABLE_REVISION,
217 .HeaderSize = sizeof(EFI_SYSTEM_TABLE),
218 },
219 .BootServices = &mock_bs,
220 .RuntimeServices = &mock_rt,
221 };
222
223 void CONSTRUCTOR
224 init_efi_system_table(void)
225 {
226 static bool once = true;
227 if (once) {
228 once = false;
229 reset_efi_system_table();
230 }
231 }
232
233 void
234 reset_efi_system_table(void)
235 {
236 ST = &mock_st;
237 BS = &mock_bs;
238 RT = &mock_rt;
239
240 memcpy(&mock_bs, &mock_default_bs, sizeof(mock_bs));
241 memcpy(&mock_rt, &mock_default_rt, sizeof(mock_rt));
242 memcpy(&mock_st, &mock_default_st, sizeof(mock_st));
243 }
244
245 EFI_STATUS EFIAPI
246 LogError_(const char *file, int line, const char *func, const CHAR16 *fmt, ...)
247 {
248 assert(0);
249 return EFI_SUCCESS;
250 }
251
252 #ifndef HAVE_SHIM_LOCK_GUID
253 EFI_GUID SHIM_LOCK_GUID = {0x605dab50, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23 } };
254 #endif
255
256 UINTN EFIAPI
257 console_print(const CHAR16 *fmt, ...)
258 {
259 return 0;
260 }
261
262 #ifndef HAVE_START_IMAGE
263 EFI_STATUS
264 start_image(EFI_HANDLE image_handle, CHAR16 *ImagePath)
265 {
266 return EFI_UNSUPPORTED;
267 }
268 #endif
269
270 // vim:fenc=utf-8:tw=75:noet