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