]>
Commit | Line | Data |
---|---|---|
804405e7 | 1 | /*++ |
2 | ||
d0a3ead2 | 3 | Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR> |
2ff79f2e | 4 | Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR> |
f9b8ab56 | 5 | This program and the accompanying materials |
804405e7 | 6 | are licensed and made available under the terms and conditions of the BSD License |
7 | which accompanies this distribution. The full text of the license may be found at | |
8 | http://opensource.org/licenses/bsd-license.php | |
9 | ||
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. | |
12 | ||
13 | Module Name: | |
14 | ||
15 | SecMain.c | |
16 | ||
17 | Abstract: | |
ccd55824 | 18 | Unix emulator of SEC phase. It's really a Posix application, but this is |
804405e7 | 19 | Ok since all the other modules for NT32 are NOT Posix applications. |
20 | ||
21 | This program processes host environment variables and figures out | |
22 | what the memory layout will be, how may FD's will be loaded and also | |
23 | what the boot mode is. | |
24 | ||
25 | The SEC registers a set of services with the SEC core. gPrivateDispatchTable | |
26 | is a list of PPI's produced by the SEC that are availble for usage in PEI. | |
27 | ||
28 | This code produces 128 K of temporary memory for the PEI stack by opening a | |
29 | host file and mapping it directly to memory addresses. | |
30 | ||
31 | The system.cmd script is used to set host environment variables that drive | |
32 | the configuration opitons of the SEC. | |
33 | ||
34 | --*/ | |
35 | ||
36 | #include "SecMain.h" | |
37 | #include <sys/mman.h> | |
38 | #include <Ppi/UnixPeiLoadFile.h> | |
804405e7 | 39 | #include <Ppi/TemporaryRamSupport.h> |
40 | #include <dlfcn.h> | |
ccd55824 | 41 | |
42 | #ifdef __APPLE__ | |
43 | #define MAP_ANONYMOUS MAP_ANON | |
44 | char *gGdbWorkingFileName = NULL; | |
45 | #endif | |
46 | ||
47 | ||
804405e7 | 48 | // |
49 | // Globals | |
50 | // | |
3ff2e324 | 51 | #if defined(__APPLE__) || defined(MDE_CPU_X64) |
bb111c23 | 52 | UNIX_PEI_LOAD_FILE_PPI mSecUnixLoadFilePpi = { GasketSecUnixPeiLoadFile }; |
53 | PEI_UNIX_AUTOSCAN_PPI mSecUnixAutoScanPpi = { GasketSecUnixPeiAutoScan }; | |
54 | PEI_UNIX_THUNK_PPI mSecUnixThunkPpi = { GasketSecUnixUnixThunkAddress }; | |
55 | EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi = { GasketSecPeiReportStatusCode }; | |
56 | UNIX_FWH_PPI mSecFwhInformationPpi = { GasketSecUnixFdAddress }; | |
d0a3ead2 | 57 | EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi = { GasketSecTemporaryRamSupport }; |
bb111c23 | 58 | #else |
59 | UNIX_PEI_LOAD_FILE_PPI mSecUnixLoadFilePpi = { SecUnixPeiLoadFile }; | |
60 | PEI_UNIX_AUTOSCAN_PPI mSecUnixAutoScanPpi = { SecUnixPeiAutoScan }; | |
61 | PEI_UNIX_THUNK_PPI mSecUnixThunkPpi = { SecUnixUnixThunkAddress }; | |
73aa7f04 | 62 | EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi = { SecPeiReportStatusCode }; |
ccd55824 | 63 | UNIX_FWH_PPI mSecFwhInformationPpi = { SecUnixFdAddress }; |
d0a3ead2 | 64 | EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi = { SecTemporaryRamSupport }; |
bb111c23 | 65 | #endif |
804405e7 | 66 | |
67 | EFI_PEI_PPI_DESCRIPTOR gPrivateDispatchTable[] = { | |
804405e7 | 68 | { |
69 | EFI_PEI_PPI_DESCRIPTOR_PPI, | |
70 | &gUnixPeiLoadFilePpiGuid, | |
ccd55824 | 71 | &mSecUnixLoadFilePpi |
804405e7 | 72 | }, |
73 | { | |
74 | EFI_PEI_PPI_DESCRIPTOR_PPI, | |
75 | &gPeiUnixAutoScanPpiGuid, | |
ccd55824 | 76 | &mSecUnixAutoScanPpi |
804405e7 | 77 | }, |
78 | { | |
79 | EFI_PEI_PPI_DESCRIPTOR_PPI, | |
80 | &gPeiUnixThunkPpiGuid, | |
ccd55824 | 81 | &mSecUnixThunkPpi |
804405e7 | 82 | }, |
83 | { | |
84 | EFI_PEI_PPI_DESCRIPTOR_PPI, | |
85 | &gEfiPeiStatusCodePpiGuid, | |
86 | &mSecStatusCodePpi | |
87 | }, | |
88 | { | |
89 | EFI_PEI_PPI_DESCRIPTOR_PPI, | |
90 | &gEfiTemporaryRamSupportPpiGuid, | |
91 | &mSecTemporaryRamSupportPpi | |
92 | }, | |
93 | { | |
804405e7 | 94 | EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, |
95 | &gUnixFwhPpiGuid, | |
96 | &mSecFwhInformationPpi | |
97 | } | |
98 | }; | |
99 | ||
100 | ||
101 | // | |
102 | // Default information about where the FD is located. | |
103 | // This array gets filled in with information from EFI_FIRMWARE_VOLUMES | |
104 | // EFI_FIRMWARE_VOLUMES is a host environment variable set by system.cmd. | |
105 | // The number of array elements is allocated base on parsing | |
106 | // EFI_FIRMWARE_VOLUMES and the memory is never freed. | |
107 | // | |
108 | UINTN gFdInfoCount = 0; | |
109 | UNIX_FD_INFO *gFdInfo; | |
110 | ||
111 | // | |
112 | // Array that supports seperate memory rantes. | |
113 | // The memory ranges are set in system.cmd via the EFI_MEMORY_SIZE variable. | |
114 | // The number of array elements is allocated base on parsing | |
115 | // EFI_MEMORY_SIZE and the memory is never freed. | |
116 | // | |
117 | UINTN gSystemMemoryCount = 0; | |
118 | UNIX_SYSTEM_MEMORY *gSystemMemory; | |
119 | ||
ccd55824 | 120 | |
121 | ||
122 | UINTN mImageContextModHandleArraySize = 0; | |
123 | IMAGE_CONTEXT_TO_MOD_HANDLE *mImageContextModHandleArray = NULL; | |
124 | ||
125 | ||
804405e7 | 126 | VOID |
127 | EFIAPI | |
128 | SecSwitchStack ( | |
129 | UINT32 TemporaryMemoryBase, | |
130 | UINT32 PermenentMemoryBase | |
131 | ); | |
132 | ||
804405e7 | 133 | EFI_PHYSICAL_ADDRESS * |
134 | MapMemory ( | |
135 | INTN fd, | |
136 | UINT64 length, | |
137 | INTN prot, | |
138 | INTN flags); | |
139 | ||
804405e7 | 140 | EFI_STATUS |
141 | MapFile ( | |
142 | IN CHAR8 *FileName, | |
143 | IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress, | |
144 | OUT UINT64 *Length | |
145 | ); | |
3ff2e324 | 146 | |
398b646f | 147 | EFI_STATUS |
148 | EFIAPI | |
149 | SecNt32PeCoffRelocateImage ( | |
150 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext | |
151 | ); | |
804405e7 | 152 | |
153 | ||
ccd55824 | 154 | int |
804405e7 | 155 | main ( |
ccd55824 | 156 | IN int Argc, |
157 | IN char **Argv, | |
158 | IN char **Envp | |
804405e7 | 159 | ) |
160 | /*++ | |
161 | ||
162 | Routine Description: | |
ccd55824 | 163 | Main entry point to SEC for Unix. This is a unix program |
804405e7 | 164 | |
165 | Arguments: | |
166 | Argc - Number of command line arguments | |
167 | Argv - Array of command line argument strings | |
168 | Envp - Array of environmemt variable strings | |
169 | ||
170 | Returns: | |
171 | 0 - Normal exit | |
172 | 1 - Abnormal exit | |
173 | ||
174 | --*/ | |
175 | { | |
176 | EFI_STATUS Status; | |
177 | EFI_PHYSICAL_ADDRESS InitialStackMemory; | |
178 | UINT64 InitialStackMemorySize; | |
179 | UINTN Index; | |
180 | UINTN Index1; | |
181 | UINTN Index2; | |
182 | UINTN PeiIndex; | |
183 | CHAR8 *FileName; | |
184 | BOOLEAN Done; | |
185 | VOID *PeiCoreFile; | |
186 | CHAR16 *MemorySizeStr; | |
187 | CHAR16 *FirmwareVolumesStr; | |
188 | UINTN *StackPointer; | |
189 | ||
190 | setbuf(stdout, 0); | |
191 | setbuf(stderr, 0); | |
192 | ||
cdccd99e | 193 | MemorySizeStr = (CHAR16 *) PcdGetPtr (PcdUnixMemorySizeForSecMain); |
194 | FirmwareVolumesStr = (CHAR16 *) PcdGetPtr (PcdUnixFirmwareVolume); | |
804405e7 | 195 | |
d5cdd257 | 196 | printf ("\nEDK SEC Main UNIX Emulation Environment from edk2.sourceforge.net\n"); |
804405e7 | 197 | |
ccd55824 | 198 | #ifdef __APPLE__ |
199 | // | |
200 | // We can't use dlopen on OS X, so we need a scheme to get symboles into gdb | |
201 | // We need to create a temp file that contains gdb commands so we can load | |
202 | // symbols when we load every PE/COFF image. | |
203 | // | |
204 | Index = strlen (*Argv); | |
7ee3b613 | 205 | gGdbWorkingFileName = malloc (Index + strlen(".gdb") + 1); |
ccd55824 | 206 | strcpy (gGdbWorkingFileName, *Argv); |
207 | strcat (gGdbWorkingFileName, ".gdb"); | |
208 | #endif | |
209 | ||
210 | ||
804405e7 | 211 | // |
212 | // Allocate space for gSystemMemory Array | |
213 | // | |
214 | gSystemMemoryCount = CountSeperatorsInString (MemorySizeStr, '!') + 1; | |
215 | gSystemMemory = calloc (gSystemMemoryCount, sizeof (UNIX_SYSTEM_MEMORY)); | |
216 | if (gSystemMemory == NULL) { | |
217 | printf ("ERROR : Can not allocate memory for system. Exiting.\n"); | |
218 | exit (1); | |
219 | } | |
220 | // | |
221 | // Allocate space for gSystemMemory Array | |
222 | // | |
223 | gFdInfoCount = CountSeperatorsInString (FirmwareVolumesStr, '!') + 1; | |
224 | gFdInfo = calloc (gFdInfoCount, sizeof (UNIX_FD_INFO)); | |
225 | if (gFdInfo == NULL) { | |
226 | printf ("ERROR : Can not allocate memory for fd info. Exiting.\n"); | |
227 | exit (1); | |
228 | } | |
229 | // | |
230 | // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION) | |
231 | // | |
cdccd99e | 232 | printf (" BootMode 0x%02x\n", (unsigned int)PcdGet32 (PcdUnixBootMode)); |
804405e7 | 233 | |
234 | // | |
235 | // Open up a 128K file to emulate temp memory for PEI. | |
236 | // on a real platform this would be SRAM, or using the cache as RAM. | |
ccd55824 | 237 | // Set InitialStackMemory to zero so UnixOpenFile will allocate a new mapping |
804405e7 | 238 | // |
239 | InitialStackMemorySize = STACK_SIZE; | |
3ff2e324 | 240 | InitialStackMemory = (UINTN)MapMemory(0, |
804405e7 | 241 | (UINT32) InitialStackMemorySize, |
d34689b4 | 242 | PROT_READ | PROT_WRITE | PROT_EXEC, |
804405e7 | 243 | MAP_ANONYMOUS | MAP_PRIVATE); |
244 | if (InitialStackMemory == 0) { | |
245 | printf ("ERROR : Can not open SecStack Exiting\n"); | |
246 | exit (1); | |
247 | } | |
248 | ||
249 | printf (" SEC passing in %u KB of temp RAM at 0x%08lx to PEI\n", | |
ccd55824 | 250 | (unsigned int)(InitialStackMemorySize / 1024), |
804405e7 | 251 | (unsigned long)InitialStackMemory); |
3ff2e324 | 252 | |
804405e7 | 253 | for (StackPointer = (UINTN*) (UINTN) InitialStackMemory; |
ccd55824 | 254 | StackPointer < (UINTN*)(UINTN)((UINTN) InitialStackMemory + (UINT64) InitialStackMemorySize); |
804405e7 | 255 | StackPointer ++) { |
256 | *StackPointer = 0x5AA55AA5; | |
257 | } | |
258 | ||
259 | // | |
260 | // Open All the firmware volumes and remember the info in the gFdInfo global | |
261 | // | |
262 | FileName = (CHAR8 *)malloc (StrLen (FirmwareVolumesStr) + 1); | |
263 | if (FileName == NULL) { | |
264 | printf ("ERROR : Can not allocate memory for firmware volume string\n"); | |
265 | exit (1); | |
266 | } | |
267 | ||
268 | Index2 = 0; | |
269 | for (Done = FALSE, Index = 0, PeiIndex = 0, PeiCoreFile = NULL; | |
270 | FirmwareVolumesStr[Index2] != 0; | |
271 | Index++) { | |
272 | for (Index1 = 0; (FirmwareVolumesStr[Index2] != '!') && (FirmwareVolumesStr[Index2] != 0); Index2++) | |
273 | FileName[Index1++] = FirmwareVolumesStr[Index2]; | |
274 | if (FirmwareVolumesStr[Index2] == '!') | |
275 | Index2++; | |
276 | FileName[Index1] = '\0'; | |
277 | ||
278 | // | |
279 | // Open the FD and remmeber where it got mapped into our processes address space | |
280 | // | |
281 | Status = MapFile ( | |
2ac288f9 | 282 | FileName, |
283 | &gFdInfo[Index].Address, | |
284 | &gFdInfo[Index].Size | |
285 | ); | |
804405e7 | 286 | if (EFI_ERROR (Status)) { |
ccd55824 | 287 | printf ("ERROR : Can not open Firmware Device File %s (%x). Exiting.\n", FileName, (unsigned int)Status); |
804405e7 | 288 | exit (1); |
289 | } | |
290 | ||
291 | printf (" FD loaded from %s at 0x%08lx", | |
2ac288f9 | 292 | FileName, (unsigned long)gFdInfo[Index].Address); |
804405e7 | 293 | |
294 | if (PeiCoreFile == NULL) { | |
295 | // | |
296 | // Assume the beginning of the FD is an FV and look for the PEI Core. | |
297 | // Load the first one we find. | |
298 | // | |
299 | Status = SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) gFdInfo[Index].Address, &PeiCoreFile); | |
300 | if (!EFI_ERROR (Status)) { | |
301 | PeiIndex = Index; | |
302 | printf (" contains SEC Core"); | |
303 | } | |
304 | } | |
305 | ||
306 | printf ("\n"); | |
307 | } | |
308 | // | |
309 | // Calculate memory regions and store the information in the gSystemMemory | |
310 | // global for later use. The autosizing code will use this data to | |
311 | // map this memory into the SEC process memory space. | |
312 | // | |
313 | Index1 = 0; | |
314 | Index = 0; | |
315 | while (1) { | |
316 | UINTN val = 0; | |
317 | // | |
318 | // Save the size of the memory. | |
319 | // | |
320 | while (MemorySizeStr[Index1] >= '0' && MemorySizeStr[Index1] <= '9') { | |
321 | val = val * 10 + MemorySizeStr[Index1] - '0'; | |
322 | Index1++; | |
323 | } | |
324 | gSystemMemory[Index++].Size = val * 0x100000; | |
325 | if (MemorySizeStr[Index1] == 0) | |
326 | break; | |
327 | Index1++; | |
328 | } | |
329 | ||
330 | printf ("\n"); | |
331 | ||
332 | // | |
333 | // Hand off to PEI Core | |
334 | // | |
335 | SecLoadFromCore ((UINTN) InitialStackMemory, (UINTN) InitialStackMemorySize, (UINTN) gFdInfo[0].Address, PeiCoreFile); | |
336 | ||
337 | // | |
338 | // If we get here, then the PEI Core returned. This is an error as PEI should | |
339 | // always hand off to DXE. | |
340 | // | |
341 | printf ("ERROR : PEI Core returned\n"); | |
342 | exit (1); | |
343 | } | |
344 | ||
345 | EFI_PHYSICAL_ADDRESS * | |
346 | MapMemory ( | |
347 | INTN fd, | |
348 | UINT64 length, | |
349 | INTN prot, | |
350 | INTN flags) | |
351 | { | |
352 | STATIC UINTN base = 0x40000000; | |
353 | CONST UINTN align = (1 << 24); | |
354 | VOID *res = NULL; | |
355 | BOOLEAN isAligned = 0; | |
356 | ||
357 | // | |
358 | // Try to get an aligned block somewhere in the address space of this | |
359 | // process. | |
360 | // | |
361 | while((!isAligned) && (base != 0)) { | |
362 | res = mmap ((void *)base, length, prot, flags, fd, 0); | |
363 | if (res == MAP_FAILED) { | |
364 | return NULL; | |
365 | } | |
366 | if ((((UINTN)res) & ~(align-1)) == (UINTN)res) { | |
367 | isAligned=1; | |
368 | } | |
369 | else { | |
370 | munmap(res, length); | |
371 | base += align; | |
372 | } | |
373 | } | |
374 | return res; | |
375 | } | |
376 | ||
377 | EFI_STATUS | |
378 | MapFile ( | |
379 | IN CHAR8 *FileName, | |
380 | IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress, | |
381 | OUT UINT64 *Length | |
382 | ) | |
383 | /*++ | |
384 | ||
385 | Routine Description: | |
ccd55824 | 386 | Opens and memory maps a file using Unix services. If BaseAddress is non zero |
804405e7 | 387 | the process will try and allocate the memory starting at BaseAddress. |
388 | ||
389 | Arguments: | |
390 | FileName - The name of the file to open and map | |
391 | MapSize - The amount of the file to map in bytes | |
392 | CreationDisposition - The flags to pass to CreateFile(). Use to create new files for | |
393 | memory emulation, and exiting files for firmware volume emulation | |
394 | BaseAddress - The base address of the mapped file in the user address space. | |
395 | If passed in as NULL the a new memory region is used. | |
396 | If passed in as non NULL the request memory region is used for | |
397 | the mapping of the file into the process space. | |
398 | Length - The size of the mapped region in bytes | |
399 | ||
400 | Returns: | |
401 | EFI_SUCCESS - The file was opened and mapped. | |
402 | EFI_NOT_FOUND - FileName was not found in the current directory | |
403 | EFI_DEVICE_ERROR - An error occured attempting to map the opened file | |
404 | ||
405 | --*/ | |
406 | { | |
407 | int fd; | |
408 | VOID *res; | |
409 | UINTN FileSize; | |
410 | ||
411 | fd = open (FileName, O_RDONLY); | |
412 | if (fd < 0) | |
413 | return EFI_NOT_FOUND; | |
414 | FileSize = lseek (fd, 0, SEEK_END); | |
415 | ||
416 | #if 0 | |
417 | if (IsMain) | |
418 | { | |
419 | /* Read entry address. */ | |
420 | lseek (fd, FileSize - 0x20, SEEK_SET); | |
421 | if (read (fd, &EntryAddress, 4) != 4) | |
2ac288f9 | 422 | { |
423 | close (fd); | |
424 | return EFI_DEVICE_ERROR; | |
425 | } | |
804405e7 | 426 | } |
427 | #endif | |
428 | ||
429 | res = MapMemory(fd, FileSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE); | |
3ff2e324 | 430 | |
804405e7 | 431 | close (fd); |
432 | ||
433 | if (res == MAP_FAILED) | |
434 | return EFI_DEVICE_ERROR; | |
435 | ||
436 | *Length = (UINT64) FileSize; | |
437 | *BaseAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) res; | |
438 | ||
439 | return EFI_SUCCESS; | |
440 | } | |
441 | ||
442 | #define BYTES_PER_RECORD 512 | |
443 | ||
444 | EFI_STATUS | |
445 | EFIAPI | |
446 | SecPeiReportStatusCode ( | |
ccd55824 | 447 | IN CONST EFI_PEI_SERVICES **PeiServices, |
804405e7 | 448 | IN EFI_STATUS_CODE_TYPE CodeType, |
449 | IN EFI_STATUS_CODE_VALUE Value, | |
450 | IN UINT32 Instance, | |
ccd55824 | 451 | IN CONST EFI_GUID *CallerId, |
452 | IN CONST EFI_STATUS_CODE_DATA *Data OPTIONAL | |
804405e7 | 453 | ) |
454 | /*++ | |
455 | ||
456 | Routine Description: | |
457 | ||
458 | This routine produces the ReportStatusCode PEI service. It's passed | |
459 | up to the PEI Core via a PPI. T | |
460 | ||
461 | This code currently uses the UNIX clib printf. This does not work the same way | |
462 | as the EFI Print (), as %t, %g, %s as Unicode are not supported. | |
463 | ||
464 | Arguments: | |
465 | (see EFI_PEI_REPORT_STATUS_CODE) | |
466 | ||
467 | Returns: | |
468 | EFI_SUCCESS - Always return success | |
469 | ||
470 | --*/ | |
471 | // TODO: PeiServices - add argument and description to function comment | |
472 | // TODO: CodeType - add argument and description to function comment | |
473 | // TODO: Value - add argument and description to function comment | |
474 | // TODO: Instance - add argument and description to function comment | |
475 | // TODO: CallerId - add argument and description to function comment | |
476 | // TODO: Data - add argument and description to function comment | |
477 | { | |
478 | CHAR8 *Format; | |
9c98c8e1 | 479 | BASE_LIST Marker; |
804405e7 | 480 | CHAR8 PrintBuffer[BYTES_PER_RECORD * 2]; |
481 | CHAR8 *Filename; | |
482 | CHAR8 *Description; | |
483 | UINT32 LineNumber; | |
484 | UINT32 ErrorLevel; | |
485 | ||
486 | ||
487 | if (Data == NULL) { | |
488 | } else if (ReportStatusCodeExtractAssertInfo (CodeType, Value, Data, &Filename, &Description, &LineNumber)) { | |
489 | // | |
490 | // Processes ASSERT () | |
491 | // | |
ccd55824 | 492 | printf ("ASSERT %s(%d): %s\n", Filename, (int)LineNumber, Description); |
804405e7 | 493 | |
494 | } else if (ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) { | |
495 | // | |
3ff2e324 | 496 | // Process DEBUG () macro |
804405e7 | 497 | // |
9c98c8e1 | 498 | AsciiBSPrint (PrintBuffer, BYTES_PER_RECORD, Format, Marker); |
b7f76514 | 499 | printf ("%s", PrintBuffer); |
804405e7 | 500 | } |
501 | ||
502 | return EFI_SUCCESS; | |
503 | } | |
504 | ||
804405e7 | 505 | VOID |
506 | EFIAPI | |
507 | PeiSwitchStacks ( | |
508 | IN SWITCH_STACK_ENTRY_POINT EntryPoint, | |
509 | IN VOID *Context1, OPTIONAL | |
510 | IN VOID *Context2, OPTIONAL | |
511 | IN VOID *Context3, OPTIONAL | |
512 | IN VOID *NewStack | |
67f86803 | 513 | ); |
804405e7 | 514 | |
515 | VOID | |
516 | SecLoadFromCore ( | |
517 | IN UINTN LargestRegion, | |
518 | IN UINTN LargestRegionSize, | |
519 | IN UINTN BootFirmwareVolumeBase, | |
520 | IN VOID *PeiCorePe32File | |
521 | ) | |
522 | /*++ | |
523 | ||
524 | Routine Description: | |
525 | This is the service to load the PEI Core from the Firmware Volume | |
526 | ||
527 | Arguments: | |
528 | LargestRegion - Memory to use for PEI. | |
529 | LargestRegionSize - Size of Memory to use for PEI | |
530 | BootFirmwareVolumeBase - Start of the Boot FV | |
531 | PeiCorePe32File - PEI Core PE32 | |
532 | ||
533 | Returns: | |
534 | Success means control is transfered and thus we should never return | |
535 | ||
536 | --*/ | |
537 | { | |
538 | EFI_STATUS Status; | |
539 | EFI_PHYSICAL_ADDRESS TopOfMemory; | |
540 | VOID *TopOfStack; | |
541 | UINT64 PeiCoreSize; | |
542 | EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint; | |
543 | EFI_PHYSICAL_ADDRESS PeiImageAddress; | |
544 | EFI_SEC_PEI_HAND_OFF *SecCoreData; | |
545 | UINTN PeiStackSize; | |
249383cf | 546 | EFI_PEI_PPI_DESCRIPTOR *DispatchTable; |
69cf40e0 | 547 | UINTN DispatchTableSize; |
804405e7 | 548 | |
549 | // | |
550 | // Compute Top Of Memory for Stack and PEI Core Allocations | |
551 | // | |
552 | TopOfMemory = LargestRegion + LargestRegionSize; | |
553 | PeiStackSize = (UINTN)RShiftU64((UINT64)STACK_SIZE,1); | |
554 | ||
555 | // | |
556 | // |-----------| <---- TemporaryRamBase + TemporaryRamSize | |
557 | // | Heap | | |
558 | // | | | |
559 | // |-----------| <---- StackBase / PeiTemporaryMemoryBase | |
560 | // | | | |
561 | // | Stack | | |
562 | // |-----------| <---- TemporaryRamBase | |
3ff2e324 | 563 | // |
804405e7 | 564 | TopOfStack = (VOID *)(LargestRegion + PeiStackSize); |
565 | TopOfMemory = LargestRegion + PeiStackSize; | |
566 | ||
567 | // | |
568 | // Reservet space for storing PeiCore's parament in stack. | |
3ff2e324 | 569 | // |
804405e7 | 570 | TopOfStack = (VOID *)((UINTN)TopOfStack - sizeof (EFI_SEC_PEI_HAND_OFF) - CPU_STACK_ALIGNMENT); |
571 | TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT); | |
572 | ||
3ff2e324 | 573 | |
804405e7 | 574 | // |
575 | // Bind this information into the SEC hand-off state | |
576 | // | |
577 | SecCoreData = (EFI_SEC_PEI_HAND_OFF*)(UINTN) TopOfStack; | |
578 | SecCoreData->DataSize = sizeof(EFI_SEC_PEI_HAND_OFF); | |
579 | SecCoreData->BootFirmwareVolumeBase = (VOID*)BootFirmwareVolumeBase; | |
cdccd99e | 580 | SecCoreData->BootFirmwareVolumeSize = PcdGet32 (PcdUnixFirmwareFdSize); |
3ff2e324 | 581 | SecCoreData->TemporaryRamBase = (VOID*)(UINTN)LargestRegion; |
804405e7 | 582 | SecCoreData->TemporaryRamSize = STACK_SIZE; |
583 | SecCoreData->StackBase = SecCoreData->TemporaryRamBase; | |
584 | SecCoreData->StackSize = PeiStackSize; | |
585 | SecCoreData->PeiTemporaryRamBase = (VOID*) ((UINTN) SecCoreData->TemporaryRamBase + PeiStackSize); | |
586 | SecCoreData->PeiTemporaryRamSize = STACK_SIZE - PeiStackSize; | |
587 | ||
588 | // | |
589 | // Load the PEI Core from a Firmware Volume | |
590 | // | |
ccd55824 | 591 | Status = SecUnixPeiLoadFile ( |
804405e7 | 592 | PeiCorePe32File, |
593 | &PeiImageAddress, | |
594 | &PeiCoreSize, | |
595 | &PeiCoreEntryPoint | |
596 | ); | |
597 | if (EFI_ERROR (Status)) { | |
598 | return ; | |
599 | } | |
3ff2e324 | 600 | |
69cf40e0 | 601 | DispatchTableSize = sizeof (gPrivateDispatchTable); |
602 | DispatchTableSize += OverrideDispatchTableExtraSize (); | |
3ff2e324 | 603 | |
69cf40e0 | 604 | DispatchTable = malloc (DispatchTableSize); |
605 | if (DispatchTable == NULL) { | |
606 | return; | |
607 | } | |
3ff2e324 | 608 | |
249383cf | 609 | // |
610 | // Allow an override for extra PPIs to be passed up to PEI | |
611 | // This is an easy way to enable OS specific customizations | |
612 | // | |
69cf40e0 | 613 | OverrideDispatchTable (&gPrivateDispatchTable[0], sizeof (gPrivateDispatchTable), DispatchTable, DispatchTableSize); |
3ff2e324 | 614 | |
804405e7 | 615 | // |
616 | // Transfer control to the PEI Core | |
617 | // | |
618 | PeiSwitchStacks ( | |
619 | (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint, | |
620 | SecCoreData, | |
249383cf | 621 | (VOID *)DispatchTable, |
804405e7 | 622 | NULL, |
623 | TopOfStack | |
624 | ); | |
625 | // | |
626 | // If we get here, then the PEI Core returned. This is an error | |
627 | // | |
628 | return ; | |
629 | } | |
630 | ||
631 | EFI_STATUS | |
632 | EFIAPI | |
ccd55824 | 633 | SecUnixPeiAutoScan ( |
804405e7 | 634 | IN UINTN Index, |
635 | OUT EFI_PHYSICAL_ADDRESS *MemoryBase, | |
636 | OUT UINT64 *MemorySize | |
637 | ) | |
638 | /*++ | |
639 | ||
640 | Routine Description: | |
641 | This service is called from Index == 0 until it returns EFI_UNSUPPORTED. | |
642 | It allows discontiguous memory regions to be supported by the emulator. | |
643 | It uses gSystemMemory[] and gSystemMemoryCount that were created by | |
644 | parsing the host environment variable EFI_MEMORY_SIZE. | |
645 | The size comes from the varaible and the address comes from the call to | |
ccd55824 | 646 | UnixOpenFile. |
804405e7 | 647 | |
648 | Arguments: | |
649 | Index - Which memory region to use | |
650 | MemoryBase - Return Base address of memory region | |
651 | MemorySize - Return size in bytes of the memory region | |
652 | ||
653 | Returns: | |
654 | EFI_SUCCESS - If memory region was mapped | |
655 | EFI_UNSUPPORTED - If Index is not supported | |
656 | ||
657 | --*/ | |
658 | { | |
659 | void *res; | |
660 | ||
661 | if (Index >= gSystemMemoryCount) { | |
662 | return EFI_UNSUPPORTED; | |
663 | } | |
664 | ||
665 | *MemoryBase = 0; | |
666 | res = MapMemory(0, gSystemMemory[Index].Size, | |
2ac288f9 | 667 | PROT_READ | PROT_WRITE | PROT_EXEC, |
668 | MAP_PRIVATE | MAP_ANONYMOUS); | |
804405e7 | 669 | if (res == MAP_FAILED) |
670 | return EFI_DEVICE_ERROR; | |
671 | *MemorySize = gSystemMemory[Index].Size; | |
672 | *MemoryBase = (UINTN)res; | |
673 | gSystemMemory[Index].Memory = *MemoryBase; | |
674 | ||
675 | return EFI_SUCCESS; | |
676 | } | |
677 | ||
678 | VOID * | |
679 | EFIAPI | |
ccd55824 | 680 | SecUnixUnixThunkAddress ( |
804405e7 | 681 | VOID |
682 | ) | |
683 | /*++ | |
684 | ||
685 | Routine Description: | |
686 | Since the SEC is the only Unix program in stack it must export | |
ccd55824 | 687 | an interface to do POSIX calls. gUnix is initailized in UnixThunk.c. |
804405e7 | 688 | |
689 | Arguments: | |
690 | InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL); | |
ccd55824 | 691 | InterfaceBase - Address of the gUnix global |
804405e7 | 692 | |
693 | Returns: | |
694 | EFI_SUCCESS - Data returned | |
695 | ||
696 | --*/ | |
697 | { | |
698 | return gUnix; | |
699 | } | |
700 | ||
701 | ||
702 | EFI_STATUS | |
ccd55824 | 703 | SecUnixPeiLoadFile ( |
804405e7 | 704 | IN VOID *Pe32Data, |
ccd55824 | 705 | OUT EFI_PHYSICAL_ADDRESS *ImageAddress, |
706 | OUT UINT64 *ImageSize, | |
707 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint | |
804405e7 | 708 | ) |
709 | /*++ | |
710 | ||
711 | Routine Description: | |
712 | Loads and relocates a PE/COFF image into memory. | |
713 | ||
714 | Arguments: | |
715 | Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated | |
716 | ImageAddress - The base address of the relocated PE/COFF image | |
717 | ImageSize - The size of the relocated PE/COFF image | |
718 | EntryPoint - The entry point of the relocated PE/COFF image | |
719 | ||
720 | Returns: | |
721 | EFI_SUCCESS - The file was loaded and relocated | |
722 | EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file | |
723 | ||
724 | --*/ | |
725 | { | |
726 | EFI_STATUS Status; | |
727 | PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; | |
728 | ||
729 | ZeroMem (&ImageContext, sizeof (ImageContext)); | |
730 | ImageContext.Handle = Pe32Data; | |
731 | ||
732 | ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) SecImageRead; | |
733 | ||
398b646f | 734 | Status = PeCoffLoaderGetImageInfo (&ImageContext); |
804405e7 | 735 | if (EFI_ERROR (Status)) { |
736 | return Status; | |
737 | } | |
3ff2e324 | 738 | |
739 | ||
804405e7 | 740 | // |
741 | // Allocate space in UNIX (not emulator) memory. Extra space is for alignment | |
742 | // | |
d34689b4 | 743 | ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) MapMemory ( |
3ff2e324 | 744 | 0, |
d34689b4 | 745 | (UINT32) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)), |
746 | PROT_READ | PROT_WRITE | PROT_EXEC, | |
747 | MAP_ANONYMOUS | MAP_PRIVATE | |
748 | ); | |
804405e7 | 749 | if (ImageContext.ImageAddress == 0) { |
750 | return EFI_OUT_OF_RESOURCES; | |
751 | } | |
3ff2e324 | 752 | |
804405e7 | 753 | // |
754 | // Align buffer on section boundry | |
755 | // | |
f7bef78c | 756 | ImageContext.ImageAddress += ImageContext.SectionAlignment - 1; |
d34689b4 | 757 | ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1)); |
804405e7 | 758 | |
759 | ||
398b646f | 760 | Status = PeCoffLoaderLoadImage (&ImageContext); |
804405e7 | 761 | if (EFI_ERROR (Status)) { |
762 | return Status; | |
763 | } | |
3ff2e324 | 764 | |
7ee3b613 A |
765 | Status = PeCoffLoaderRelocateImage (&ImageContext); |
766 | if (EFI_ERROR (Status)) { | |
767 | return Status; | |
768 | } | |
3ff2e324 | 769 | |
804405e7 | 770 | |
ccd55824 | 771 | SecPeCoffRelocateImageExtraAction (&ImageContext); |
804405e7 | 772 | |
773 | // | |
774 | // BugBug: Flush Instruction Cache Here when CPU Lib is ready | |
775 | // | |
776 | ||
777 | *ImageAddress = ImageContext.ImageAddress; | |
778 | *ImageSize = ImageContext.ImageSize; | |
779 | *EntryPoint = ImageContext.EntryPoint; | |
780 | ||
781 | return EFI_SUCCESS; | |
782 | } | |
783 | ||
ccd55824 | 784 | |
785 | RETURN_STATUS | |
786 | EFIAPI | |
787 | SecPeCoffGetEntryPoint ( | |
788 | IN VOID *Pe32Data, | |
789 | IN OUT VOID **EntryPoint | |
790 | ) | |
791 | { | |
792 | EFI_STATUS Status; | |
793 | EFI_PHYSICAL_ADDRESS ImageAddress; | |
794 | UINT64 ImageSize; | |
795 | EFI_PHYSICAL_ADDRESS PhysEntryPoint; | |
796 | ||
797 | Status = SecUnixPeiLoadFile (Pe32Data, &ImageAddress, &ImageSize, &PhysEntryPoint); | |
798 | ||
799 | *EntryPoint = (VOID *)(UINTN)PhysEntryPoint; | |
800 | return Status; | |
801 | } | |
802 | ||
803 | ||
804 | ||
804405e7 | 805 | EFI_STATUS |
806 | EFIAPI | |
ccd55824 | 807 | SecUnixFdAddress ( |
804405e7 | 808 | IN UINTN Index, |
809 | IN OUT EFI_PHYSICAL_ADDRESS *FdBase, | |
7ee3b613 A |
810 | IN OUT UINT64 *FdSize, |
811 | IN OUT EFI_PHYSICAL_ADDRESS *FixUp | |
804405e7 | 812 | ) |
813 | /*++ | |
814 | ||
815 | Routine Description: | |
816 | Return the FD Size and base address. Since the FD is loaded from a | |
817 | file into host memory only the SEC will know it's address. | |
818 | ||
819 | Arguments: | |
820 | Index - Which FD, starts at zero. | |
821 | FdSize - Size of the FD in bytes | |
822 | FdBase - Start address of the FD. Assume it points to an FV Header | |
7ee3b613 | 823 | FixUp - Difference between actual FD address and build address |
804405e7 | 824 | |
825 | Returns: | |
826 | EFI_SUCCESS - Return the Base address and size of the FV | |
827 | EFI_UNSUPPORTED - Index does nto map to an FD in the system | |
828 | ||
829 | --*/ | |
830 | { | |
831 | if (Index >= gFdInfoCount) { | |
832 | return EFI_UNSUPPORTED; | |
833 | } | |
834 | ||
835 | *FdBase = gFdInfo[Index].Address; | |
836 | *FdSize = gFdInfo[Index].Size; | |
7ee3b613 | 837 | *FixUp = 0; |
804405e7 | 838 | |
839 | if (*FdBase == 0 && *FdSize == 0) { | |
840 | return EFI_UNSUPPORTED; | |
841 | } | |
842 | ||
7ee3b613 A |
843 | if (Index == 0) { |
844 | // | |
3ff2e324 | 845 | // FD 0 has XIP code and well known PCD values |
7ee3b613 A |
846 | // If the memory buffer could not be allocated at the FD build address |
847 | // the Fixup is the difference. | |
848 | // | |
b9c8e50e | 849 | *FixUp = *FdBase - PcdGet64 (PcdUnixFdBaseAddress); |
7ee3b613 A |
850 | } |
851 | ||
804405e7 | 852 | return EFI_SUCCESS; |
853 | } | |
854 | ||
855 | EFI_STATUS | |
856 | EFIAPI | |
857 | SecImageRead ( | |
858 | IN VOID *FileHandle, | |
859 | IN UINTN FileOffset, | |
860 | IN OUT UINTN *ReadSize, | |
861 | OUT VOID *Buffer | |
862 | ) | |
863 | /*++ | |
864 | ||
865 | Routine Description: | |
866 | Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file | |
867 | ||
868 | Arguments: | |
869 | FileHandle - The handle to the PE/COFF file | |
870 | FileOffset - The offset, in bytes, into the file to read | |
871 | ReadSize - The number of bytes to read from the file starting at FileOffset | |
872 | Buffer - A pointer to the buffer to read the data into. | |
873 | ||
874 | Returns: | |
875 | EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset | |
876 | ||
877 | --*/ | |
878 | { | |
879 | CHAR8 *Destination8; | |
880 | CHAR8 *Source8; | |
881 | UINTN Length; | |
882 | ||
883 | Destination8 = Buffer; | |
884 | Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset); | |
885 | Length = *ReadSize; | |
886 | while (Length--) { | |
887 | *(Destination8++) = *(Source8++); | |
888 | } | |
889 | ||
890 | return EFI_SUCCESS; | |
891 | } | |
892 | ||
893 | UINTN | |
894 | CountSeperatorsInString ( | |
895 | IN const CHAR16 *String, | |
896 | IN CHAR16 Seperator | |
897 | ) | |
898 | /*++ | |
899 | ||
900 | Routine Description: | |
901 | Count the number of seperators in String | |
902 | ||
903 | Arguments: | |
904 | String - String to process | |
905 | Seperator - Item to count | |
906 | ||
907 | Returns: | |
908 | Number of Seperator in String | |
909 | ||
910 | --*/ | |
911 | { | |
912 | UINTN Count; | |
913 | ||
914 | for (Count = 0; *String != '\0'; String++) { | |
915 | if (*String == Seperator) { | |
916 | Count++; | |
917 | } | |
918 | } | |
919 | ||
920 | return Count; | |
921 | } | |
922 | ||
923 | ||
ccd55824 | 924 | EFI_STATUS |
925 | AddHandle ( | |
926 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext, | |
927 | IN VOID *ModHandle | |
928 | ) | |
929 | /*++ | |
930 | ||
931 | Routine Description: | |
932 | Store the ModHandle in an array indexed by the Pdb File name. | |
3ff2e324 | 933 | The ModHandle is needed to unload the image. |
ccd55824 | 934 | |
935 | Arguments: | |
3ff2e324 | 936 | ImageContext - Input data returned from PE Laoder Library. Used to find the |
ccd55824 | 937 | .PDB file name of the PE Image. |
3ff2e324 | 938 | ModHandle - Returned from LoadLibraryEx() and stored for call to |
ccd55824 | 939 | FreeLibrary(). |
940 | ||
941 | Returns: | |
3ff2e324 | 942 | EFI_SUCCESS - ModHandle was stored. |
ccd55824 | 943 | |
944 | --*/ | |
945 | { | |
946 | UINTN Index; | |
947 | IMAGE_CONTEXT_TO_MOD_HANDLE *Array; | |
948 | UINTN PreviousSize; | |
949 | ||
950 | ||
951 | Array = mImageContextModHandleArray; | |
952 | for (Index = 0; Index < mImageContextModHandleArraySize; Index++, Array++) { | |
953 | if (Array->ImageContext == NULL) { | |
954 | // | |
955 | // Make a copy of the stirng and store the ModHandle | |
956 | // | |
957 | Array->ImageContext = ImageContext; | |
958 | Array->ModHandle = ModHandle; | |
959 | return EFI_SUCCESS; | |
960 | } | |
961 | } | |
3ff2e324 | 962 | |
ccd55824 | 963 | // |
3ff2e324 | 964 | // No free space in mImageContextModHandleArray so grow it by |
ccd55824 | 965 | // IMAGE_CONTEXT_TO_MOD_HANDLE entires. realloc will |
966 | // copy the old values to the new locaiton. But it does | |
967 | // not zero the new memory area. | |
968 | // | |
969 | PreviousSize = mImageContextModHandleArraySize * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE); | |
970 | mImageContextModHandleArraySize += MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE; | |
971 | ||
972 | mImageContextModHandleArray = realloc (mImageContextModHandleArray, mImageContextModHandleArraySize * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE)); | |
973 | if (mImageContextModHandleArray == NULL) { | |
974 | ASSERT (FALSE); | |
975 | return EFI_OUT_OF_RESOURCES; | |
976 | } | |
3ff2e324 | 977 | |
ccd55824 | 978 | memset (mImageContextModHandleArray + PreviousSize, 0, MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE)); |
3ff2e324 | 979 | |
ccd55824 | 980 | return AddHandle (ImageContext, ModHandle); |
981 | } | |
982 | ||
983 | ||
984 | VOID * | |
985 | RemoveHandle ( | |
986 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext | |
987 | ) | |
988 | /*++ | |
989 | ||
990 | Routine Description: | |
991 | Return the ModHandle and delete the entry in the array. | |
992 | ||
993 | Arguments: | |
3ff2e324 | 994 | ImageContext - Input data returned from PE Laoder Library. Used to find the |
ccd55824 | 995 | .PDB file name of the PE Image. |
996 | ||
997 | Returns: | |
998 | ModHandle - ModHandle assoicated with ImageContext is returned | |
999 | NULL - No ModHandle associated with ImageContext | |
1000 | ||
1001 | --*/ | |
1002 | { | |
1003 | UINTN Index; | |
1004 | IMAGE_CONTEXT_TO_MOD_HANDLE *Array; | |
1005 | ||
1006 | if (ImageContext->PdbPointer == NULL) { | |
1007 | // | |
1008 | // If no PDB pointer there is no ModHandle so return NULL | |
1009 | // | |
1010 | return NULL; | |
1011 | } | |
1012 | ||
1013 | Array = mImageContextModHandleArray; | |
1014 | for (Index = 0; Index < mImageContextModHandleArraySize; Index++, Array++) { | |
2d7df642 | 1015 | if (Array->ImageContext == ImageContext) { |
ccd55824 | 1016 | // |
1017 | // If you find a match return it and delete the entry | |
1018 | // | |
1019 | Array->ImageContext = NULL; | |
1020 | return Array->ModHandle; | |
1021 | } | |
1022 | } | |
1023 | ||
1024 | return NULL; | |
1025 | } | |
1026 | ||
1027 | ||
1028 | ||
1029 | // | |
3ff2e324 | 1030 | // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to source a |
ccd55824 | 1031 | // add-symbol-file command. Hey what can you say scripting in gdb is not that great.... |
1032 | // | |
1033 | // Put .gdbinit in the CWD where you do gdb SecMain.dll for source level debug | |
1034 | // | |
1035 | // cat .gdbinit | |
1036 | // b SecGdbScriptBreak | |
1037 | // command | |
1038 | // silent | |
1039 | // source SecMain.dll.gdb | |
1040 | // c | |
1041 | // end | |
1042 | // | |
1043 | VOID | |
1044 | SecGdbScriptBreak ( | |
1045 | VOID | |
1046 | ) | |
1047 | { | |
1048 | } | |
804405e7 | 1049 | |
804405e7 | 1050 | VOID |
1051 | SecUnixLoaderBreak ( | |
1052 | VOID | |
1053 | ) | |
1054 | { | |
1055 | } | |
1056 | ||
ccd55824 | 1057 | BOOLEAN |
1058 | IsPdbFile ( | |
1059 | IN CHAR8 *PdbFileName | |
1060 | ) | |
1061 | { | |
1062 | UINTN Len; | |
1063 | ||
1064 | if (PdbFileName == NULL) { | |
1065 | return FALSE; | |
1066 | } | |
1067 | ||
1068 | Len = strlen (PdbFileName); | |
1069 | if ((Len < 5)|| (PdbFileName[Len - 4] != '.')) { | |
1070 | return FALSE; | |
1071 | } | |
3ff2e324 | 1072 | |
ad33170d | 1073 | if ((PdbFileName[Len - 3] == 'D' || PdbFileName[Len - 3] == 'd') && |
1074 | (PdbFileName[Len - 2] == 'L' || PdbFileName[Len - 2] == 'l') && | |
1075 | (PdbFileName[Len - 1] == 'L' || PdbFileName[Len - 1] == 'l')) { | |
ccd55824 | 1076 | return TRUE; |
1077 | } | |
3ff2e324 | 1078 | |
ccd55824 | 1079 | return FALSE; |
1080 | } | |
1081 | ||
1082 | ||
1083 | #define MAX_SPRINT_BUFFER_SIZE 0x200 | |
1084 | ||
1085 | void | |
1086 | PrintLoadAddress ( | |
1087 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext | |
1088 | ) | |
1089 | { | |
bb111c23 | 1090 | if (ImageContext->PdbPointer == NULL) { |
1091 | fprintf (stderr, | |
1092 | "0x%08lx Loading NO DEBUG with entry point 0x%08lx\n", | |
3ff2e324 | 1093 | (unsigned long)(ImageContext->ImageAddress), |
bb111c23 | 1094 | (unsigned long)ImageContext->EntryPoint |
1095 | ); | |
1096 | } else { | |
1097 | fprintf (stderr, | |
1098 | "0x%08lx Loading %s with entry point 0x%08lx\n", | |
1099 | (unsigned long)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders), | |
3ff2e324 | 1100 | ImageContext->PdbPointer, |
bb111c23 | 1101 | (unsigned long)ImageContext->EntryPoint |
1102 | ); | |
1103 | } | |
ccd55824 | 1104 | // Keep output synced up |
1105 | fflush (stderr); | |
1106 | } | |
1107 | ||
1108 | ||
1109 | VOID | |
804405e7 | 1110 | EFIAPI |
ccd55824 | 1111 | SecPeCoffRelocateImageExtraAction ( |
804405e7 | 1112 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext |
1113 | ) | |
1114 | { | |
3ff2e324 | 1115 | |
ccd55824 | 1116 | #ifdef __APPLE__ |
2ff79f2e | 1117 | BOOLEAN EnabledOnEntry; |
1118 | ||
1119 | // | |
1120 | // Make sure writting of the file is an atomic operation | |
1121 | // | |
1122 | if (UnixInterruptEanbled ()) { | |
1123 | UnixDisableInterrupt (); | |
1124 | EnabledOnEntry = TRUE; | |
1125 | } else { | |
1126 | EnabledOnEntry = FALSE; | |
1127 | } | |
1128 | ||
ccd55824 | 1129 | PrintLoadAddress (ImageContext); |
1130 | ||
1131 | // | |
1132 | // In mach-o (OS X executable) dlopen() can only load files in the MH_DYLIB of MH_BUNDLE format. | |
1133 | // To convert to PE/COFF we need to construct a mach-o with the MH_PRELOAD format. We create | |
1134 | // .dSYM files for the PE/COFF images that can be used by gdb for source level debugging. | |
1135 | // | |
1136 | FILE *GdbTempFile; | |
3ff2e324 | 1137 | |
ccd55824 | 1138 | // |
1139 | // In the Mach-O to PE/COFF conversion the size of the PE/COFF headers is not accounted for. | |
1140 | // Thus we need to skip over the PE/COFF header when giving load addresses for our symbol table. | |
1141 | // | |
1142 | if (ImageContext->PdbPointer != NULL && !IsPdbFile (ImageContext->PdbPointer)) { | |
1143 | // | |
1144 | // Now we have a database of the images that are currently loaded | |
1145 | // | |
3ff2e324 | 1146 | |
ccd55824 | 1147 | // |
3ff2e324 | 1148 | // 'symbol-file' will clear out currnet symbol mappings in gdb. |
1149 | // you can do a 'add-symbol-file filename address' for every image we loaded to get source | |
1150 | // level debug in gdb. Note Sec, being a true application will work differently. | |
1151 | // | |
1152 | // We add the PE/COFF header size into the image as the mach-O does not have a header in | |
1153 | // loaded into system memory. | |
ccd55824 | 1154 | // |
ccd55824 | 1155 | // This gives us a data base of gdb commands and after something is unloaded that entry will be |
3ff2e324 | 1156 | // removed. We don't yet have the scheme of how to comunicate with gdb, but we have the |
ccd55824 | 1157 | // data base of info ready to roll. |
1158 | // | |
3ff2e324 | 1159 | // We could use qXfer:libraries:read, but OS X GDB does not currently support it. |
1160 | // <library-list> | |
ccd55824 | 1161 | // <library name="/lib/libc.so.6"> // ImageContext->PdbPointer |
1162 | // <segment address="0x10000000"/> // ImageContext->ImageAddress + ImageContext->SizeOfHeaders | |
3ff2e324 | 1163 | // </library> |
1164 | // </library-list> | |
ccd55824 | 1165 | // |
3ff2e324 | 1166 | |
ccd55824 | 1167 | // |
1168 | // Write the file we need for the gdb script | |
1169 | // | |
1170 | GdbTempFile = fopen (gGdbWorkingFileName, "w"); | |
1171 | if (GdbTempFile != NULL) { | |
d34689b4 | 1172 | fprintf (GdbTempFile, "add-symbol-file %s 0x%08lx\n", ImageContext->PdbPointer, (long unsigned int)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)); |
ccd55824 | 1173 | fclose (GdbTempFile); |
3ff2e324 | 1174 | |
ccd55824 | 1175 | // |
3ff2e324 | 1176 | // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint. |
ccd55824 | 1177 | // Hey what can you say scripting in gdb is not that great.... |
1178 | // | |
1179 | SecGdbScriptBreak (); | |
2ff79f2e | 1180 | } else { |
1181 | ASSERT (FALSE); | |
ccd55824 | 1182 | } |
1183 | ||
1184 | AddHandle (ImageContext, ImageContext->PdbPointer); | |
3ff2e324 | 1185 | |
2ff79f2e | 1186 | if (EnabledOnEntry) { |
1187 | UnixEnableInterrupt (); | |
1188 | } | |
1189 | ||
1190 | ||
ccd55824 | 1191 | } |
3ff2e324 | 1192 | |
ccd55824 | 1193 | #else |
3ff2e324 | 1194 | |
ccd55824 | 1195 | void *Handle = NULL; |
1196 | void *Entry = NULL; | |
3ff2e324 | 1197 | |
1198 | if (ImageContext->PdbPointer == NULL) { | |
1199 | return; | |
1200 | } | |
1201 | ||
1202 | if (!IsPdbFile (ImageContext->PdbPointer)) { | |
1203 | return; | |
1204 | } | |
1205 | ||
1206 | fprintf (stderr, | |
804405e7 | 1207 | "Loading %s 0x%08lx - entry point 0x%08lx\n", |
1208 | ImageContext->PdbPointer, | |
1209 | (unsigned long)ImageContext->ImageAddress, | |
1210 | (unsigned long)ImageContext->EntryPoint); | |
1211 | ||
7ee3b613 | 1212 | Handle = dlopen (ImageContext->PdbPointer, RTLD_NOW); |
3ff2e324 | 1213 | |
865c7e17 | 1214 | if (Handle) { |
7ee3b613 | 1215 | Entry = dlsym (Handle, "_ModuleEntryPoint"); |
865c7e17 | 1216 | } else { |
3ff2e324 | 1217 | printf("%s\n", dlerror()); |
865c7e17 | 1218 | } |
3ff2e324 | 1219 | |
865c7e17 | 1220 | if (Entry != NULL) { |
ccd55824 | 1221 | ImageContext->EntryPoint = (UINTN)Entry; |
1222 | printf("Change %s Entrypoint to :0x%08lx\n", ImageContext->PdbPointer, (unsigned long)Entry); | |
865c7e17 | 1223 | } |
1224 | ||
804405e7 | 1225 | SecUnixLoaderBreak (); |
1226 | ||
ccd55824 | 1227 | #endif |
1228 | ||
1229 | return; | |
804405e7 | 1230 | } |
1231 | ||
1232 | ||
ccd55824 | 1233 | VOID |
804405e7 | 1234 | EFIAPI |
ccd55824 | 1235 | SecPeCoffLoaderUnloadImageExtraAction ( |
804405e7 | 1236 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext |
1237 | ) | |
1238 | { | |
ccd55824 | 1239 | VOID *Handle; |
1240 | ||
1241 | Handle = RemoveHandle (ImageContext); | |
1242 | ||
1243 | #ifdef __APPLE__ | |
1244 | FILE *GdbTempFile; | |
2ff79f2e | 1245 | BOOLEAN EnabledOnEntry; |
3ff2e324 | 1246 | |
ccd55824 | 1247 | if (Handle != NULL) { |
1248 | // | |
1249 | // Need to skip .PDB files created from VC++ | |
1250 | // | |
3ff2e324 | 1251 | if (!IsPdbFile (ImageContext->PdbPointer)) { |
2ff79f2e | 1252 | if (UnixInterruptEanbled ()) { |
1253 | UnixDisableInterrupt (); | |
1254 | EnabledOnEntry = TRUE; | |
1255 | } else { | |
1256 | EnabledOnEntry = FALSE; | |
1257 | } | |
1258 | ||
ccd55824 | 1259 | // |
1260 | // Write the file we need for the gdb script | |
1261 | // | |
1262 | GdbTempFile = fopen (gGdbWorkingFileName, "w"); | |
1263 | if (GdbTempFile != NULL) { | |
1264 | fprintf (GdbTempFile, "remove-symbol-file %s\n", ImageContext->PdbPointer); | |
1265 | fclose (GdbTempFile); | |
3ff2e324 | 1266 | |
ccd55824 | 1267 | // |
3ff2e324 | 1268 | // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint. |
ccd55824 | 1269 | // Hey what can you say scripting in gdb is not that great.... |
1270 | // | |
1271 | SecGdbScriptBreak (); | |
2ff79f2e | 1272 | } else { |
1273 | ASSERT (FALSE); | |
1274 | } | |
1275 | ||
1276 | if (EnabledOnEntry) { | |
1277 | UnixEnableInterrupt (); | |
ccd55824 | 1278 | } |
1279 | } | |
1280 | } | |
3ff2e324 | 1281 | |
ccd55824 | 1282 | #else |
1283 | // | |
1284 | // Don't want to confuse gdb with symbols for something that got unloaded | |
1285 | // | |
1286 | if (Handle != NULL) { | |
1287 | dlclose (Handle); | |
1288 | } | |
1289 | ||
1290 | #endif | |
1291 | return; | |
804405e7 | 1292 | } |
1293 | ||
1294 | VOID | |
1295 | ModuleEntryPoint ( | |
1296 | VOID | |
1297 | ) | |
1298 | { | |
1299 | } | |
1300 | ||
1301 | EFI_STATUS | |
1302 | EFIAPI | |
1303 | SecTemporaryRamSupport ( | |
1304 | IN CONST EFI_PEI_SERVICES **PeiServices, | |
1305 | IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase, | |
1306 | IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase, | |
1307 | IN UINTN CopySize | |
1308 | ) | |
1309 | { | |
1310 | // | |
1311 | // Migrate the whole temporary memory to permenent memory. | |
3ff2e324 | 1312 | // |
804405e7 | 1313 | CopyMem ( |
3ff2e324 | 1314 | (VOID*)(UINTN)PermanentMemoryBase, |
1315 | (VOID*)(UINTN)TemporaryMemoryBase, | |
804405e7 | 1316 | CopySize |
1317 | ); | |
1318 | ||
1319 | // | |
1320 | // SecSwitchStack function must be invoked after the memory migration | |
3ff2e324 | 1321 | // immediatly, also we need fixup the stack change caused by new call into |
804405e7 | 1322 | // permenent memory. |
3ff2e324 | 1323 | // |
804405e7 | 1324 | SecSwitchStack ( |
1325 | (UINT32) TemporaryMemoryBase, | |
1326 | (UINT32) PermanentMemoryBase | |
1327 | ); | |
1328 | ||
1329 | // | |
3ff2e324 | 1330 | // We need *not* fix the return address because currently, |
804405e7 | 1331 | // The PeiCore is excuted in flash. |
1332 | // | |
1333 | ||
1334 | // | |
1fd8c31a | 1335 | // Simulate to invalid temporary memory, terminate temporary memory |
3ff2e324 | 1336 | // |
804405e7 | 1337 | //ZeroMem ((VOID*)(UINTN)TemporaryMemoryBase, CopySize); |
3ff2e324 | 1338 | |
804405e7 | 1339 | return EFI_SUCCESS; |
1340 | } |