1 // SPDX-License-Identifier: BSD-2-Clause-Patent
3 * test.c - stuff we need for test harnesses
4 * Copyright Peter Jones <pjones@redhat.com>
13 #define BT_BUF_SIZE (4096/sizeof(void *))
15 static void *frames
[BT_BUF_SIZE
] = { 0, };
17 UINT8 in_protocol
= 0;
18 int debug
= DEFAULT_DEBUG_PRINT_STATE
;
21 print_traceback(int skip
)
26 nptrs
= backtrace(frames
, BT_BUF_SIZE
);
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
]);
38 #pragma GCC diagnostic ignored "-Wunused-parameter"
39 #pragma GCC diagnostic ignored "-Wunused-function"
41 static EFI_STATUS EFIAPI
42 mock_efi_allocate_pages(EFI_ALLOCATE_TYPE type
,
43 EFI_MEMORY_TYPE memory_type
,
45 EFI_PHYSICAL_ADDRESS
*memory
)
48 * XXX so far this does not honor the type at all, and there's no
49 * tracking for memory_type either.
51 *memory
= (EFI_PHYSICAL_ADDRESS
)(uintptr_t)calloc(nmemb
, 4096);
52 if ((void *)(uintptr_t)(*memory
) == NULL
)
53 return EFI_OUT_OF_RESOURCES
;
58 static EFI_STATUS EFIAPI
59 mock_efi_free_pages(EFI_PHYSICAL_ADDRESS memory
,
62 free((void *)(uintptr_t)memory
);
67 static EFI_STATUS EFIAPI
68 mock_efi_allocate_pool(EFI_MEMORY_TYPE pool_type
,
72 *buf
= calloc(1, size
);
74 return EFI_OUT_OF_RESOURCES
;
79 static EFI_STATUS EFIAPI
80 mock_efi_free_pool(void *buf
)
100 mock_efi_unsupported()
102 return EFI_UNSUPPORTED
;
108 return EFI_NOT_FOUND
;
111 EFI_BOOT_SERVICES mock_bs
, mock_default_bs
= {
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
),
119 .RaiseTPL
= mock_efi_unsupported
,
120 .RestoreTPL
= mock_efi_void
,
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
,
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
,
135 .InstallProtocolInterface
= mock_efi_unsupported
,
136 .ReinstallProtocolInterface
= mock_efi_unsupported
,
137 .UninstallProtocolInterface
= mock_efi_unsupported
,
138 .HandleProtocol
= mock_efi_unsupported
,
141 * EFI 1.10 has a "Reserved" field here that's not in later
144 * I don't think it's in any actual *firmware* either.
148 .RegisterProtocolNotify
= mock_efi_unsupported
,
149 .LocateHandle
= mock_efi_not_found
,
150 .LocateDevicePath
= mock_efi_unsupported
,
151 .InstallConfigurationTable
= mock_efi_unsupported
,
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
,
159 .GetNextMonotonicCount
= mock_efi_unsupported
,
160 .Stall
= mock_efi_unsupported
,
161 .SetWatchdogTimer
= mock_efi_unsupported
,
163 .ConnectController
= (void *)mock_efi_unsupported
,
164 .DisconnectController
= mock_efi_unsupported
,
166 .OpenProtocol
= mock_efi_unsupported
,
167 .CloseProtocol
= mock_efi_unsupported
,
168 .OpenProtocolInformation
= mock_efi_unsupported
,
170 .ProtocolsPerHandle
= mock_efi_unsupported
,
171 .LocateHandleBuffer
= mock_efi_unsupported
,
172 .LocateProtocol
= mock_efi_unsupported
,
174 .InstallMultipleProtocolInterfaces
= (void *)mock_efi_unsupported
,
175 .UninstallMultipleProtocolInterfaces
= (void *)mock_efi_unsupported
,
177 .CalculateCrc32
= mock_efi_unsupported
,
181 .CreateEventEx
= mock_efi_unsupported
,
184 EFI_RUNTIME_SERVICES mock_rt
, mock_default_rt
= {
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
),
192 .GetTime
= mock_efi_unsupported
,
193 .SetTime
= mock_efi_unsupported
,
194 .GetWakeupTime
= mock_efi_unsupported
,
195 .SetWakeupTime
= (void *)mock_efi_unsupported
,
197 .SetVirtualAddressMap
= mock_efi_unsupported
,
198 .ConvertPointer
= mock_efi_unsupported
,
200 .GetVariable
= mock_efi_unsupported
,
201 .SetVariable
= mock_efi_unsupported
,
202 .GetNextVariableName
= mock_efi_unsupported
,
204 .GetNextHighMonotonicCount
= mock_efi_unsupported
,
205 .ResetSystem
= mock_efi_unsupported
,
207 .UpdateCapsule
= mock_efi_unsupported
,
208 .QueryCapsuleCapabilities
= mock_efi_unsupported
,
210 .QueryVariableInfo
= mock_efi_unsupported
,
213 EFI_SYSTEM_TABLE mock_st
, mock_default_st
= {
215 .Signature
= EFI_SYSTEM_TABLE_SIGNATURE
,
216 .Revision
= EFI_1_10_SYSTEM_TABLE_REVISION
,
217 .HeaderSize
= sizeof(EFI_SYSTEM_TABLE
),
219 .BootServices
= &mock_bs
,
220 .RuntimeServices
= &mock_rt
,
224 init_efi_system_table(void)
226 static bool once
= true;
229 reset_efi_system_table();
234 reset_efi_system_table(void)
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
));
246 LogError_(const char *file
, int line
, const char *func
, const CHAR16
*fmt
, ...)
252 #ifndef HAVE_SHIM_LOCK_GUID
253 EFI_GUID SHIM_LOCK_GUID
= {0x605dab50, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23 } };
257 console_print(const CHAR16
*fmt
, ...)
263 console_error(CHAR16
*err
, EFI_STATUS efi_status
)
268 #ifndef HAVE_START_IMAGE
270 start_image(EFI_HANDLE image_handle
, CHAR16
*ImagePath
)
272 return EFI_UNSUPPORTED
;
276 // vim:fenc=utf-8:tw=75:noet