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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/*++ @file
Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
Portions copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _SEC_MAIN_H__
#define _SEC_MAIN_H__
//
// Name mangle to prevent build errors. I.e conflicts between EFI and OS
//
#define NTOHL _UNIX_EFI_NAME_MANGLE_NTOHL_
#define NTOHLL _UNIX_EFI_NAME_MANGLE_NTOHLL_
#define HTONL _UNIX_EFI_NAME_MANGLE_HTONL_
#define HTONLL _UNIX_EFI_NAME_MANGLE_HTONLL_
#define NTOHS _UNIX_EFI_NAME_MANGLE_NTOHS_
#define HTONS _UNIX_EFI_NAME_MANGLE_HTOHS_
#define B0 _UNIX_EFI_NAME_MANGLE_B0_
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/termios.h>
#include <sys/time.h>
#if __CYGWIN__
#include <sys/dirent.h>
#else
#include <sys/dir.h>
#endif
#include <sys/mman.h>
#include <dlfcn.h>
#include <unistd.h>
#include <poll.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/statvfs.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <net/if.h>
#include <ifaddrs.h>
#ifdef __APPLE__
#include <net/if_dl.h>
#include <net/bpf.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/disk.h>
#define _XOPEN_SOURCE
#ifndef _Bool
#define _Bool char // for clang debug
#endif
#else
#include <termio.h>
#include <sys/vfs.h>
#include <linux/fs.h>
#endif
#include <utime.h>
#undef NTOHL
#undef NTOHLL
#undef HTONL
#undef HTONLL
#undef NTOHS
#undef HTONS
#undef B0
#undef CR3
#include <PiPei.h>
#include <Uefi.h>
#include <Library/PeCoffLib.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/PrintLib.h>
#include <Library/PcdLib.h>
#include <Library/DebugLib.h>
#include <Library/ReportStatusCodeLib.h>
#include <Library/ThunkPpiList.h>
#include <Library/ThunkProtocolList.h>
#include <Library/PeiServicesLib.h>
#include <Library/PeCoffGetEntryPointLib.h>
#include <Library/EmuMagicPageLib.h>
#include <Ppi/EmuThunk.h>
#include <Ppi/StatusCode.h>
#include <Protocol/SimplePointer.h>
#include <Protocol/SimpleTextIn.h>
#include <Protocol/SimpleTextInEx.h>
#include <Protocol/UgaDraw.h>
#include <Protocol/SimpleFileSystem.h>
#include <Protocol/EmuThunk.h>
#include <Protocol/EmuIoThunk.h>
#include <Protocol/EmuGraphicsWindow.h>
#include <Protocol/EmuThread.h>
#include <Protocol/EmuBlockIo.h>
#include <Protocol/EmuSnp.h>
#include <Guid/FileInfo.h>
#include <Guid/FileSystemInfo.h>
#include <Guid/FileSystemVolumeLabelInfo.h>
#include "Gasket.h"
#define STACK_SIZE 0x20000
typedef struct {
EFI_PHYSICAL_ADDRESS Address;
UINT64 Size;
} EMU_FD_INFO;
typedef struct {
EFI_PHYSICAL_ADDRESS Memory;
UINT64 Size;
} EMU_SYSTEM_MEMORY;
#define MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE 0x100
typedef struct {
PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext;
VOID *ModHandle;
} IMAGE_CONTEXT_TO_MOD_HANDLE;
EFI_STATUS
EFIAPI
SecUnixPeiLoadFile (
VOID *Pe32Data,
EFI_PHYSICAL_ADDRESS *ImageAddress,
UINT64 *ImageSize,
EFI_PHYSICAL_ADDRESS *EntryPoint
);
int
main (
IN int Argc,
IN char **Argv,
IN char **Envp
);
VOID
SecLoadFromCore (
IN UINTN LargestRegion,
IN UINTN LargestRegionSize,
IN UINTN BootFirmwareVolumeBase,
IN VOID *PeiCoreFile
);
EFI_STATUS
SecLoadFile (
IN VOID *Pe32Data,
IN EFI_PHYSICAL_ADDRESS *ImageAddress,
IN UINT64 *ImageSize,
IN EFI_PHYSICAL_ADDRESS *EntryPoint
);
EFI_STATUS
SecFfsFindPeiCore (
IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,
OUT VOID **Pe32Data
);
EFI_STATUS
SecFfsFindNextFile (
IN EFI_FV_FILETYPE SearchType,
IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,
IN OUT EFI_FFS_FILE_HEADER **FileHeader
);
EFI_STATUS
SecFfsFindSectionData (
IN EFI_SECTION_TYPE SectionType,
IN EFI_FFS_FILE_HEADER *FfsFileHeader,
IN OUT VOID **SectionData
);
EFI_STATUS
EFIAPI
SecUnixPeCoffLoaderLoadAsDll (
IN CHAR8 *PdbFileName,
IN VOID **ImageEntryPoint,
OUT VOID **ModHandle
);
EFI_STATUS
EFIAPI
SecUnixPeCoffLoaderFreeLibrary (
OUT VOID *ModHandle
);
EFI_STATUS
SecUnixFdAddress (
IN UINTN Index,
IN OUT EFI_PHYSICAL_ADDRESS *FdBase,
IN OUT UINT64 *FdSize,
IN OUT EFI_PHYSICAL_ADDRESS *FixUp
)
;
EFI_STATUS
EFIAPI
GasketSecUnixFdAddress (
IN UINTN Index,
IN OUT EFI_PHYSICAL_ADDRESS *FdBase,
IN OUT UINT64 *FdSize,
IN OUT EFI_PHYSICAL_ADDRESS *FixUp
)
;
EFI_STATUS
GetImageReadFunction (
IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,
IN EFI_PHYSICAL_ADDRESS *TopOfMemory
);
EFI_STATUS
EFIAPI
SecImageRead (
IN VOID *FileHandle,
IN UINTN FileOffset,
IN OUT UINTN *ReadSize,
OUT VOID *Buffer
);
CHAR16 *
AsciiToUnicode (
IN CHAR8 *Ascii,
IN UINTN *StrLen OPTIONAL
);
UINTN
CountSeparatorsInString (
IN const CHAR16 *String,
IN CHAR16 Separator
);
EFI_STATUS
EFIAPI
SecTemporaryRamSupport (
IN CONST EFI_PEI_SERVICES **PeiServices,
IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
IN UINTN CopySize
);
EFI_STATUS
EFIAPI
GasketSecTemporaryRamSupport (
IN CONST EFI_PEI_SERVICES **PeiServices,
IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
IN UINTN CopySize
);
RETURN_STATUS
EFIAPI
SecPeCoffGetEntryPoint (
IN VOID *Pe32Data,
IN OUT VOID **EntryPoint
);
VOID
EFIAPI
SecPeCoffRelocateImageExtraAction (
IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
);
VOID
EFIAPI
SecPeCoffLoaderUnloadImageExtraAction (
IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
);
VOID
PeiSwitchStacks (
IN SWITCH_STACK_ENTRY_POINT EntryPoint,
IN VOID *Context1 OPTIONAL,
IN VOID *Context2 OPTIONAL,
IN VOID *NewStack
);
VOID
SecInitThunkProtocol (
VOID
);
EFI_PHYSICAL_ADDRESS *
MapMemory (
INTN fd,
UINT64 length,
INTN prot,
INTN flags);
EFI_STATUS
MapFile (
IN CHAR8 *FileName,
IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
OUT UINT64 *Length
);
EFI_STATUS
MapFd0 (
IN CHAR8 *FileName,
IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
OUT UINT64 *Length
);
BOOLEAN
EfiSystemMemoryRange (
IN VOID *MemoryAddress
);
VOID SecSleep (UINT64 Nanoseconds);
VOID SecEnableInterrupt (VOID);
VOID SecDisableInterrupt (VOID);
BOOLEAN SecInterruptEanbled (VOID);
extern EMU_THUNK_PROTOCOL gEmuThunkProtocol;
extern EMU_IO_THUNK_PROTOCOL gX11ThunkIo;
extern EMU_IO_THUNK_PROTOCOL gPosixFileSystemThunkIo;
extern EMU_IO_THUNK_PROTOCOL gPthreadThunkIo;
extern EMU_IO_THUNK_PROTOCOL gBlockIoThunkIo;
extern EMU_IO_THUNK_PROTOCOL gSnpThunkIo;
#endif
|