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