]> git.proxmox.com Git - mirror_edk2.git/blob - InOsEmuPkg/Unix/Sec/SecMain.h
InOsEmuPkg: Update code to support BerkeleyPacketFilter, and add a template for Linux...
[mirror_edk2.git] / InOsEmuPkg / Unix / Sec / SecMain.h
1 /*++ @file
2
3 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
4 Portions copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #ifndef _SEC_MAIN_H__
17 #define _SEC_MAIN_H__
18
19 //
20 // Name mangle to prevent build errors. I.e conflicts between EFI and OS
21 //
22 #define NTOHL _UNIX_EFI_NAME_MANGLE_NTOHL_
23 #define HTONL _UNIX_EFI_NAME_MANGLE_HTONL_
24 #define NTOHS _UNIX_EFI_NAME_MANGLE_NTOHS_
25 #define HTONS _UNIX_EFI_NAME_MANGLE_HTOHS_
26 #define B0 _UNIX_EFI_NAME_MANGLE_B0_
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/termios.h>
34 #include <sys/time.h>
35
36 #if __CYGWIN__
37 #include <sys/dirent.h>
38 #else
39 #include <sys/dir.h>
40 #endif
41
42 #include <sys/mman.h>
43 #include <dlfcn.h>
44
45 #include <unistd.h>
46 #include <poll.h>
47 #include <fcntl.h>
48 #include <time.h>
49 #include <signal.h>
50 #include <errno.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <sys/ioctl.h>
54 #include <sys/statvfs.h>
55
56 #include <sys/socket.h>
57 #include <netdb.h>
58 #include <netinet/in.h>
59 #include <net/if.h>
60 #include <ifaddrs.h>
61
62 #ifdef __APPLE__
63 #include <net/if_dl.h>
64 #include <net/bpf.h>
65 #include <sys/param.h>
66 #include <sys/mount.h>
67 #include <sys/disk.h>
68 #define _XOPEN_SOURCE
69 #ifndef _Bool
70 #define _Bool char // for clang debug
71 #endif
72 #else
73 #include <termio.h>
74 #include <sys/vfs.h>
75 #include <linux/fs.h>
76 #endif
77
78 #include <utime.h>
79
80 #undef NTOHL
81 #undef HTONL
82 #undef NTOHS
83 #undef HTONS
84 #undef B0
85
86
87 #include <PiPei.h>
88 #include <Uefi.h>
89
90 #include <Library/PeCoffLib.h>
91 #include <Library/BaseLib.h>
92 #include <Library/BaseMemoryLib.h>
93 #include <Library/PrintLib.h>
94 #include <Library/PcdLib.h>
95 #include <Library/DebugLib.h>
96 #include <Library/ReportStatusCodeLib.h>
97
98 #include <Library/ThunkPpiList.h>
99 #include <Library/ThunkProtocolList.h>
100 #include <Library/PeiServicesLib.h>
101 #include <Library/PeCoffGetEntryPointLib.h>
102 #include <Library/EmuMagicPageLib.h>
103
104 #include <Ppi/EmuThunk.h>
105 #include <Ppi/StatusCode.h>
106
107 #include <Protocol/SimplePointer.h>
108 #include <Protocol/SimpleTextIn.h>
109 #include <Protocol/SimpleTextInEx.h>
110 #include <Protocol/UgaDraw.h>
111 #include <Protocol/SimpleFileSystem.h>
112
113 #include <Protocol/EmuThunk.h>
114 #include <Protocol/EmuIoThunk.h>
115 #include <Protocol/EmuGraphicsWindow.h>
116 #include <Protocol/EmuThread.h>
117 #include <Protocol/EmuBlockIo.h>
118 #include <Protocol/EmuSnp.h>
119
120 #include <Guid/FileInfo.h>
121 #include <Guid/FileSystemInfo.h>
122 #include <Guid/FileSystemVolumeLabelInfo.h>
123
124
125 #include "Gasket.h"
126
127
128 #define STACK_SIZE 0x20000
129
130 typedef struct {
131 EFI_PHYSICAL_ADDRESS Address;
132 UINT64 Size;
133 } EMU_FD_INFO;
134
135 typedef struct {
136 EFI_PHYSICAL_ADDRESS Memory;
137 UINT64 Size;
138 } EMU_SYSTEM_MEMORY;
139
140
141 #define MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE 0x100
142
143 typedef struct {
144 PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext;
145 VOID *ModHandle;
146 } IMAGE_CONTEXT_TO_MOD_HANDLE;
147
148
149 EFI_STATUS
150 EFIAPI
151 SecUnixPeiLoadFile (
152 VOID *Pe32Data,
153 EFI_PHYSICAL_ADDRESS *ImageAddress,
154 UINT64 *ImageSize,
155 EFI_PHYSICAL_ADDRESS *EntryPoint
156 );
157
158 int
159 main (
160 IN int Argc,
161 IN char **Argv,
162 IN char **Envp
163 );
164
165 VOID
166 SecLoadFromCore (
167 IN UINTN LargestRegion,
168 IN UINTN LargestRegionSize,
169 IN UINTN BootFirmwareVolumeBase,
170 IN VOID *PeiCoreFile
171 );
172
173 EFI_STATUS
174 SecLoadFile (
175 IN VOID *Pe32Data,
176 IN EFI_PHYSICAL_ADDRESS *ImageAddress,
177 IN UINT64 *ImageSize,
178 IN EFI_PHYSICAL_ADDRESS *EntryPoint
179 );
180
181 EFI_STATUS
182 SecFfsFindPeiCore (
183 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,
184 OUT VOID **Pe32Data
185 );
186
187 EFI_STATUS
188 SecFfsFindNextFile (
189 IN EFI_FV_FILETYPE SearchType,
190 IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,
191 IN OUT EFI_FFS_FILE_HEADER **FileHeader
192 );
193
194 EFI_STATUS
195 SecFfsFindSectionData (
196 IN EFI_SECTION_TYPE SectionType,
197 IN EFI_FFS_FILE_HEADER *FfsFileHeader,
198 IN OUT VOID **SectionData
199 );
200
201 EFI_STATUS
202 EFIAPI
203 SecUnixPeCoffLoaderLoadAsDll (
204 IN CHAR8 *PdbFileName,
205 IN VOID **ImageEntryPoint,
206 OUT VOID **ModHandle
207 );
208
209 EFI_STATUS
210 EFIAPI
211 SecUnixPeCoffLoaderFreeLibrary (
212 OUT VOID *ModHandle
213 );
214
215 EFI_STATUS
216 EFIAPI
217 SecUnixFdAddress (
218 IN UINTN Index,
219 IN OUT EFI_PHYSICAL_ADDRESS *FdBase,
220 IN OUT UINT64 *FdSize,
221 IN OUT EFI_PHYSICAL_ADDRESS *FixUp
222 )
223 ;
224
225 EFI_STATUS
226 EFIAPI
227 GasketSecUnixFdAddress (
228 IN UINTN Index,
229 IN OUT EFI_PHYSICAL_ADDRESS *FdBase,
230 IN OUT UINT64 *FdSize,
231 IN OUT EFI_PHYSICAL_ADDRESS *FixUp
232 )
233 ;
234
235
236 EFI_STATUS
237 GetImageReadFunction (
238 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,
239 IN EFI_PHYSICAL_ADDRESS *TopOfMemory
240 );
241
242 EFI_STATUS
243 EFIAPI
244 SecImageRead (
245 IN VOID *FileHandle,
246 IN UINTN FileOffset,
247 IN OUT UINTN *ReadSize,
248 OUT VOID *Buffer
249 );
250
251 CHAR16 *
252 AsciiToUnicode (
253 IN CHAR8 *Ascii,
254 IN UINTN *StrLen OPTIONAL
255 );
256
257 UINTN
258 CountSeperatorsInString (
259 IN const CHAR16 *String,
260 IN CHAR16 Seperator
261 );
262
263 EFI_STATUS
264 EFIAPI
265 SecTemporaryRamSupport (
266 IN CONST EFI_PEI_SERVICES **PeiServices,
267 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
268 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
269 IN UINTN CopySize
270 );
271
272 EFI_STATUS
273 EFIAPI
274 GasketSecTemporaryRamSupport (
275 IN CONST EFI_PEI_SERVICES **PeiServices,
276 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
277 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
278 IN UINTN CopySize
279 );
280
281
282 RETURN_STATUS
283 EFIAPI
284 SecPeCoffGetEntryPoint (
285 IN VOID *Pe32Data,
286 IN OUT VOID **EntryPoint
287 );
288
289 VOID
290 EFIAPI
291 SecPeCoffRelocateImageExtraAction (
292 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
293 );
294
295 VOID
296 EFIAPI
297 SecPeCoffLoaderUnloadImageExtraAction (
298 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
299 );
300
301
302 VOID
303 EFIAPI
304 PeiSwitchStacks (
305 IN SWITCH_STACK_ENTRY_POINT EntryPoint,
306 IN VOID *Context1, OPTIONAL
307 IN VOID *Context2, OPTIONAL
308 IN VOID *NewStack
309 );
310
311 VOID
312 SecInitThunkProtocol (
313 VOID
314 );
315
316
317 EFI_PHYSICAL_ADDRESS *
318 MapMemory (
319 INTN fd,
320 UINT64 length,
321 INTN prot,
322 INTN flags);
323
324 EFI_STATUS
325 MapFile (
326 IN CHAR8 *FileName,
327 IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
328 OUT UINT64 *Length
329 );
330
331 EFI_STATUS
332 MapFd0 (
333 IN CHAR8 *FileName,
334 IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
335 OUT UINT64 *Length
336 );
337
338
339
340 VOID SecSleep (UINT64 Milliseconds);
341 VOID SecEnableInterrupt (VOID);
342 VOID SecDisableInterrupt (VOID);
343 BOOLEAN SecInterruptEanbled (VOID);
344
345
346 extern EMU_THUNK_PROTOCOL gEmuThunkProtocol;
347 extern EMU_IO_THUNK_PROTOCOL gX11ThunkIo;
348 extern EMU_IO_THUNK_PROTOCOL gPosixFileSystemThunkIo;
349 extern EMU_IO_THUNK_PROTOCOL gPthreadThunkIo;
350 extern EMU_IO_THUNK_PROTOCOL gBlockIoThunkIo;
351 extern EMU_IO_THUNK_PROTOCOL gSnpThunkIo;
352
353 #endif