]> git.proxmox.com Git - efi-boot-shim.git/blame - test.c
Clean up better after build. Closes: #1046268
[efi-boot-shim.git] / test.c
CommitLineData
031e5cce
SM
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
031e5cce
SM
7#include "shim.h"
8
8529e0f7
SM
9#include <execinfo.h>
10#include <stdio.h>
11#include <string.h>
12
13#define BT_BUF_SIZE (4096/sizeof(void *))
14
15static void *frames[BT_BUF_SIZE] = { 0, };
16
031e5cce
SM
17UINT8 in_protocol = 0;
18int debug = DEFAULT_DEBUG_PRINT_STATE;
19
8529e0f7
SM
20void
21print_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
031e5cce
SM
38#pragma GCC diagnostic ignored "-Wunused-parameter"
39#pragma GCC diagnostic ignored "-Wunused-function"
40
8529e0f7
SM
41static EFI_STATUS EFIAPI
42mock_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
58static EFI_STATUS EFIAPI
59mock_efi_free_pages(EFI_PHYSICAL_ADDRESS memory,
60 UINTN nmemb)
61{
62 free((void *)(uintptr_t)memory);
63
64 return EFI_SUCCESS;
65}
66
67static EFI_STATUS EFIAPI
68mock_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
79static EFI_STATUS EFIAPI
80mock_efi_free_pool(void *buf)
81{
82 free(buf);
83
84 return EFI_SUCCESS;
85}
86
87void EFIAPI
88mock_efi_void()
89{
90 ;
91}
92
031e5cce 93EFI_STATUS EFIAPI
8529e0f7 94mock_efi_success()
031e5cce 95{
031e5cce
SM
96 return EFI_SUCCESS;
97}
98
8529e0f7
SM
99EFI_STATUS EFIAPI
100mock_efi_unsupported()
101{
102 return EFI_UNSUPPORTED;
103}
031e5cce 104
8529e0f7
SM
105EFI_STATUS EFIAPI
106mock_efi_not_found()
107{
108 return EFI_NOT_FOUND;
031e5cce
SM
109}
110
8529e0f7
SM
111EFI_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
184EFI_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};
031e5cce 212
8529e0f7
SM
213EFI_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};
031e5cce 222
8529e0f7
SM
223void CONSTRUCTOR
224init_efi_system_table(void)
225{
226 static bool once = true;
227 if (once) {
228 once = false;
229 reset_efi_system_table();
031e5cce 230 }
031e5cce
SM
231}
232
8529e0f7
SM
233void
234reset_efi_system_table(void)
031e5cce 235{
8529e0f7
SM
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));
031e5cce
SM
243}
244
8529e0f7
SM
245EFI_STATUS EFIAPI
246LogError_(const char *file, int line, const char *func, const CHAR16 *fmt, ...)
031e5cce 247{
8529e0f7
SM
248 assert(0);
249 return EFI_SUCCESS;
031e5cce
SM
250}
251
8529e0f7 252#ifndef HAVE_SHIM_LOCK_GUID
031e5cce 253EFI_GUID SHIM_LOCK_GUID = {0x605dab50, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23 } };
8529e0f7
SM
254#endif
255
256UINTN EFIAPI
257console_print(const CHAR16 *fmt, ...)
258{
259 return 0;
260}
261
e6ace38a
SM
262void
263console_error(CHAR16 *err, EFI_STATUS efi_status)
264{
265 return;
266}
267
8529e0f7
SM
268#ifndef HAVE_START_IMAGE
269EFI_STATUS
270start_image(EFI_HANDLE image_handle, CHAR16 *ImagePath)
271{
272 return EFI_UNSUPPORTED;
273}
274#endif
031e5cce
SM
275
276// vim:fenc=utf-8:tw=75:noet