3 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
4 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 This program and the accompanying materials
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
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.
18 Unix emulator of SEC phase. It's really a Posix application, but this is
19 Ok since all the other modules for NT32 are NOT Posix applications.
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.
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.
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.
31 The system.cmd script is used to set host environment variables that drive
32 the configuration opitons of the SEC.
38 #include <Ppi/UnixPeiLoadFile.h>
39 #include <Ppi/TemporaryRamSupport.h>
43 #define MAP_ANONYMOUS MAP_ANON
44 char *gGdbWorkingFileName
= NULL
;
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
};
57 TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi
= { GasketSecTemporaryRamSupport
};
59 UNIX_PEI_LOAD_FILE_PPI mSecUnixLoadFilePpi
= { SecUnixPeiLoadFile
};
60 PEI_UNIX_AUTOSCAN_PPI mSecUnixAutoScanPpi
= { SecUnixPeiAutoScan
};
61 PEI_UNIX_THUNK_PPI mSecUnixThunkPpi
= { SecUnixUnixThunkAddress
};
62 EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi
= { SecPeiReportStatusCode
};
63 UNIX_FWH_PPI mSecFwhInformationPpi
= { SecUnixFdAddress
};
64 TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi
= { SecTemporaryRamSupport
};
67 EFI_PEI_PPI_DESCRIPTOR gPrivateDispatchTable
[] = {
69 EFI_PEI_PPI_DESCRIPTOR_PPI
,
70 &gUnixPeiLoadFilePpiGuid
,
74 EFI_PEI_PPI_DESCRIPTOR_PPI
,
75 &gPeiUnixAutoScanPpiGuid
,
79 EFI_PEI_PPI_DESCRIPTOR_PPI
,
80 &gPeiUnixThunkPpiGuid
,
84 EFI_PEI_PPI_DESCRIPTOR_PPI
,
85 &gEfiPeiStatusCodePpiGuid
,
89 EFI_PEI_PPI_DESCRIPTOR_PPI
,
90 &gEfiTemporaryRamSupportPpiGuid
,
91 &mSecTemporaryRamSupportPpi
94 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
96 &mSecFwhInformationPpi
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.
108 UINTN gFdInfoCount
= 0;
109 UNIX_FD_INFO
*gFdInfo
;
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.
117 UINTN gSystemMemoryCount
= 0;
118 UNIX_SYSTEM_MEMORY
*gSystemMemory
;
122 UINTN mImageContextModHandleArraySize
= 0;
123 IMAGE_CONTEXT_TO_MOD_HANDLE
*mImageContextModHandleArray
= NULL
;
129 UINT32 TemporaryMemoryBase
,
130 UINT32 PermenentMemoryBase
133 EFI_PHYSICAL_ADDRESS
*
143 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
149 SecNt32PeCoffRelocateImage (
150 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
163 Main entry point to SEC for Unix. This is a unix program
166 Argc - Number of command line arguments
167 Argv - Array of command line argument strings
168 Envp - Array of environmemt variable strings
177 EFI_PHYSICAL_ADDRESS InitialStackMemory
;
178 UINT64 InitialStackMemorySize
;
186 CHAR16
*MemorySizeStr
;
187 CHAR16
*FirmwareVolumesStr
;
193 MemorySizeStr
= (CHAR16
*) PcdGetPtr (PcdUnixMemorySizeForSecMain
);
194 FirmwareVolumesStr
= (CHAR16
*) PcdGetPtr (PcdUnixFirmwareVolume
);
196 printf ("\nEDK SEC Main UNIX Emulation Environment from edk2.sourceforge.net\n");
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.
204 Index
= strlen (*Argv
);
205 gGdbWorkingFileName
= malloc (Index
+ strlen(".gdb") + 1);
206 strcpy (gGdbWorkingFileName
, *Argv
);
207 strcat (gGdbWorkingFileName
, ".gdb");
212 // Allocate space for gSystemMemory Array
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");
221 // Allocate space for gSystemMemory Array
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");
230 // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)
232 printf (" BootMode 0x%02x\n", (unsigned int)PcdGet32 (PcdUnixBootMode
));
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.
237 // Set InitialStackMemory to zero so UnixOpenFile will allocate a new mapping
239 InitialStackMemorySize
= STACK_SIZE
;
240 InitialStackMemory
= (UINTN
)MapMemory(0,
241 (UINT32
) InitialStackMemorySize
,
242 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
243 MAP_ANONYMOUS
| MAP_PRIVATE
);
244 if (InitialStackMemory
== 0) {
245 printf ("ERROR : Can not open SecStack Exiting\n");
249 printf (" SEC passing in %u KB of temp RAM at 0x%08lx to PEI\n",
250 (unsigned int)(InitialStackMemorySize
/ 1024),
251 (unsigned long)InitialStackMemory
);
253 for (StackPointer
= (UINTN
*) (UINTN
) InitialStackMemory
;
254 StackPointer
< (UINTN
*)(UINTN
)((UINTN
) InitialStackMemory
+ (UINT64
) InitialStackMemorySize
);
256 *StackPointer
= 0x5AA55AA5;
260 // Open All the firmware volumes and remember the info in the gFdInfo global
262 FileName
= (CHAR8
*)malloc (StrLen (FirmwareVolumesStr
) + 1);
263 if (FileName
== NULL
) {
264 printf ("ERROR : Can not allocate memory for firmware volume string\n");
269 for (Done
= FALSE
, Index
= 0, PeiIndex
= 0, PeiCoreFile
= NULL
;
270 FirmwareVolumesStr
[Index2
] != 0;
272 for (Index1
= 0; (FirmwareVolumesStr
[Index2
] != '!') && (FirmwareVolumesStr
[Index2
] != 0); Index2
++)
273 FileName
[Index1
++] = FirmwareVolumesStr
[Index2
];
274 if (FirmwareVolumesStr
[Index2
] == '!')
276 FileName
[Index1
] = '\0';
279 // Open the FD and remmeber where it got mapped into our processes address space
283 &gFdInfo
[Index
].Address
,
286 if (EFI_ERROR (Status
)) {
287 printf ("ERROR : Can not open Firmware Device File %s (%x). Exiting.\n", FileName
, (unsigned int)Status
);
291 printf (" FD loaded from %s at 0x%08lx",
292 FileName
, (unsigned long)gFdInfo
[Index
].Address
);
294 if (PeiCoreFile
== NULL
) {
296 // Assume the beginning of the FD is an FV and look for the PEI Core.
297 // Load the first one we find.
299 Status
= SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) gFdInfo
[Index
].Address
, &PeiCoreFile
);
300 if (!EFI_ERROR (Status
)) {
302 printf (" contains SEC Core");
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.
318 // Save the size of the memory.
320 while (MemorySizeStr
[Index1
] >= '0' && MemorySizeStr
[Index1
] <= '9') {
321 val
= val
* 10 + MemorySizeStr
[Index1
] - '0';
324 gSystemMemory
[Index
++].Size
= val
* 0x100000;
325 if (MemorySizeStr
[Index1
] == 0)
333 // Hand off to PEI Core
335 SecLoadFromCore ((UINTN
) InitialStackMemory
, (UINTN
) InitialStackMemorySize
, (UINTN
) gFdInfo
[0].Address
, PeiCoreFile
);
338 // If we get here, then the PEI Core returned. This is an error as PEI should
339 // always hand off to DXE.
341 printf ("ERROR : PEI Core returned\n");
345 EFI_PHYSICAL_ADDRESS
*
352 STATIC UINTN base
= 0x40000000;
353 CONST UINTN align
= (1 << 24);
355 BOOLEAN isAligned
= 0;
358 // Try to get an aligned block somewhere in the address space of this
361 while((!isAligned
) && (base
!= 0)) {
362 res
= mmap ((void *)base
, length
, prot
, flags
, fd
, 0);
363 if (res
== MAP_FAILED
) {
366 if ((((UINTN
)res
) & ~(align
-1)) == (UINTN
)res
) {
380 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
386 Opens and memory maps a file using Unix services. If BaseAddress is non zero
387 the process will try and allocate the memory starting at BaseAddress.
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
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
411 fd
= open (FileName
, O_RDONLY
);
413 return EFI_NOT_FOUND
;
414 FileSize
= lseek (fd
, 0, SEEK_END
);
419 /* Read entry address. */
420 lseek (fd
, FileSize
- 0x20, SEEK_SET
);
421 if (read (fd
, &EntryAddress
, 4) != 4)
424 return EFI_DEVICE_ERROR
;
429 res
= MapMemory(fd
, FileSize
, PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_PRIVATE
);
433 if (res
== MAP_FAILED
)
434 return EFI_DEVICE_ERROR
;
436 *Length
= (UINT64
) FileSize
;
437 *BaseAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) res
;
442 #define BYTES_PER_RECORD 512
446 SecPeiReportStatusCode (
447 IN CONST EFI_PEI_SERVICES
**PeiServices
,
448 IN EFI_STATUS_CODE_TYPE CodeType
,
449 IN EFI_STATUS_CODE_VALUE Value
,
451 IN CONST EFI_GUID
*CallerId
,
452 IN CONST EFI_STATUS_CODE_DATA
*Data OPTIONAL
458 This routine produces the ReportStatusCode PEI service. It's passed
459 up to the PEI Core via a PPI. T
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.
465 (see EFI_PEI_REPORT_STATUS_CODE)
468 EFI_SUCCESS - Always return success
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
480 CHAR8 PrintBuffer
[BYTES_PER_RECORD
* 2];
488 } else if (ReportStatusCodeExtractAssertInfo (CodeType
, Value
, Data
, &Filename
, &Description
, &LineNumber
)) {
490 // Processes ASSERT ()
492 printf ("ASSERT %s(%d): %s\n", Filename
, (int)LineNumber
, Description
);
494 } else if (ReportStatusCodeExtractDebugInfo (Data
, &ErrorLevel
, &Marker
, &Format
)) {
496 // Process DEBUG () macro
498 AsciiBSPrint (PrintBuffer
, BYTES_PER_RECORD
, Format
, Marker
);
499 printf ("%s", PrintBuffer
);
508 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
509 IN VOID
*Context1
, OPTIONAL
510 IN VOID
*Context2
, OPTIONAL
511 IN VOID
*Context3
, OPTIONAL
517 IN UINTN LargestRegion
,
518 IN UINTN LargestRegionSize
,
519 IN UINTN BootFirmwareVolumeBase
,
520 IN VOID
*PeiCorePe32File
525 This is the service to load the PEI Core from the Firmware Volume
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
534 Success means control is transfered and thus we should never return
539 EFI_PHYSICAL_ADDRESS TopOfMemory
;
542 EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint
;
543 EFI_PHYSICAL_ADDRESS PeiImageAddress
;
544 EFI_SEC_PEI_HAND_OFF
*SecCoreData
;
548 // Compute Top Of Memory for Stack and PEI Core Allocations
550 TopOfMemory
= LargestRegion
+ LargestRegionSize
;
551 PeiStackSize
= (UINTN
)RShiftU64((UINT64
)STACK_SIZE
,1);
554 // |-----------| <---- TemporaryRamBase + TemporaryRamSize
557 // |-----------| <---- StackBase / PeiTemporaryMemoryBase
560 // |-----------| <---- TemporaryRamBase
562 TopOfStack
= (VOID
*)(LargestRegion
+ PeiStackSize
);
563 TopOfMemory
= LargestRegion
+ PeiStackSize
;
566 // Reservet space for storing PeiCore's parament in stack.
568 TopOfStack
= (VOID
*)((UINTN
)TopOfStack
- sizeof (EFI_SEC_PEI_HAND_OFF
) - CPU_STACK_ALIGNMENT
);
569 TopOfStack
= ALIGN_POINTER (TopOfStack
, CPU_STACK_ALIGNMENT
);
573 // Bind this information into the SEC hand-off state
575 SecCoreData
= (EFI_SEC_PEI_HAND_OFF
*)(UINTN
) TopOfStack
;
576 SecCoreData
->DataSize
= sizeof(EFI_SEC_PEI_HAND_OFF
);
577 SecCoreData
->BootFirmwareVolumeBase
= (VOID
*)BootFirmwareVolumeBase
;
578 SecCoreData
->BootFirmwareVolumeSize
= PcdGet32 (PcdUnixFirmwareFdSize
);
579 SecCoreData
->TemporaryRamBase
= (VOID
*)(UINTN
)LargestRegion
;
580 SecCoreData
->TemporaryRamSize
= STACK_SIZE
;
581 SecCoreData
->StackBase
= SecCoreData
->TemporaryRamBase
;
582 SecCoreData
->StackSize
= PeiStackSize
;
583 SecCoreData
->PeiTemporaryRamBase
= (VOID
*) ((UINTN
) SecCoreData
->TemporaryRamBase
+ PeiStackSize
);
584 SecCoreData
->PeiTemporaryRamSize
= STACK_SIZE
- PeiStackSize
;
587 // Load the PEI Core from a Firmware Volume
589 Status
= SecUnixPeiLoadFile (
595 if (EFI_ERROR (Status
)) {
600 // Transfer control to the PEI Core
603 (SWITCH_STACK_ENTRY_POINT
) (UINTN
) PeiCoreEntryPoint
,
605 (VOID
*) (UINTN
) ((EFI_PEI_PPI_DESCRIPTOR
*) &gPrivateDispatchTable
),
610 // If we get here, then the PEI Core returned. This is an error
619 OUT EFI_PHYSICAL_ADDRESS
*MemoryBase
,
620 OUT UINT64
*MemorySize
625 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.
626 It allows discontiguous memory regions to be supported by the emulator.
627 It uses gSystemMemory[] and gSystemMemoryCount that were created by
628 parsing the host environment variable EFI_MEMORY_SIZE.
629 The size comes from the varaible and the address comes from the call to
633 Index - Which memory region to use
634 MemoryBase - Return Base address of memory region
635 MemorySize - Return size in bytes of the memory region
638 EFI_SUCCESS - If memory region was mapped
639 EFI_UNSUPPORTED - If Index is not supported
645 if (Index
>= gSystemMemoryCount
) {
646 return EFI_UNSUPPORTED
;
650 res
= MapMemory(0, gSystemMemory
[Index
].Size
,
651 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
652 MAP_PRIVATE
| MAP_ANONYMOUS
);
653 if (res
== MAP_FAILED
)
654 return EFI_DEVICE_ERROR
;
655 *MemorySize
= gSystemMemory
[Index
].Size
;
656 *MemoryBase
= (UINTN
)res
;
657 gSystemMemory
[Index
].Memory
= *MemoryBase
;
664 SecUnixUnixThunkAddress (
670 Since the SEC is the only Unix program in stack it must export
671 an interface to do POSIX calls. gUnix is initailized in UnixThunk.c.
674 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);
675 InterfaceBase - Address of the gUnix global
678 EFI_SUCCESS - Data returned
689 OUT EFI_PHYSICAL_ADDRESS
*ImageAddress
,
690 OUT UINT64
*ImageSize
,
691 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
696 Loads and relocates a PE/COFF image into memory.
699 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
700 ImageAddress - The base address of the relocated PE/COFF image
701 ImageSize - The size of the relocated PE/COFF image
702 EntryPoint - The entry point of the relocated PE/COFF image
705 EFI_SUCCESS - The file was loaded and relocated
706 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
711 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
713 ZeroMem (&ImageContext
, sizeof (ImageContext
));
714 ImageContext
.Handle
= Pe32Data
;
716 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) SecImageRead
;
718 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
719 if (EFI_ERROR (Status
)) {
725 // Allocate space in UNIX (not emulator) memory. Extra space is for alignment
727 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) MapMemory (
729 (UINT32
) (ImageContext
.ImageSize
+ (ImageContext
.SectionAlignment
* 2)),
730 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
731 MAP_ANONYMOUS
| MAP_PRIVATE
733 if (ImageContext
.ImageAddress
== 0) {
734 return EFI_OUT_OF_RESOURCES
;
738 // Align buffer on section boundry
740 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
741 ImageContext
.ImageAddress
&= ~((EFI_PHYSICAL_ADDRESS
)(ImageContext
.SectionAlignment
- 1));
744 Status
= PeCoffLoaderLoadImage (&ImageContext
);
745 if (EFI_ERROR (Status
)) {
749 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
750 if (EFI_ERROR (Status
)) {
755 SecPeCoffRelocateImageExtraAction (&ImageContext
);
758 // BugBug: Flush Instruction Cache Here when CPU Lib is ready
761 *ImageAddress
= ImageContext
.ImageAddress
;
762 *ImageSize
= ImageContext
.ImageSize
;
763 *EntryPoint
= ImageContext
.EntryPoint
;
771 SecPeCoffGetEntryPoint (
773 IN OUT VOID
**EntryPoint
777 EFI_PHYSICAL_ADDRESS ImageAddress
;
779 EFI_PHYSICAL_ADDRESS PhysEntryPoint
;
781 Status
= SecUnixPeiLoadFile (Pe32Data
, &ImageAddress
, &ImageSize
, &PhysEntryPoint
);
783 *EntryPoint
= (VOID
*)(UINTN
)PhysEntryPoint
;
793 IN OUT EFI_PHYSICAL_ADDRESS
*FdBase
,
794 IN OUT UINT64
*FdSize
,
795 IN OUT EFI_PHYSICAL_ADDRESS
*FixUp
800 Return the FD Size and base address. Since the FD is loaded from a
801 file into host memory only the SEC will know it's address.
804 Index - Which FD, starts at zero.
805 FdSize - Size of the FD in bytes
806 FdBase - Start address of the FD. Assume it points to an FV Header
807 FixUp - Difference between actual FD address and build address
810 EFI_SUCCESS - Return the Base address and size of the FV
811 EFI_UNSUPPORTED - Index does nto map to an FD in the system
815 if (Index
>= gFdInfoCount
) {
816 return EFI_UNSUPPORTED
;
819 *FdBase
= gFdInfo
[Index
].Address
;
820 *FdSize
= gFdInfo
[Index
].Size
;
823 if (*FdBase
== 0 && *FdSize
== 0) {
824 return EFI_UNSUPPORTED
;
829 // FD 0 has XIP code and well known PCD values
830 // If the memory buffer could not be allocated at the FD build address
831 // the Fixup is the difference.
833 *FixUp
= *FdBase
- PcdGet64 (PcdUnixFdBaseAddress
);
844 IN OUT UINTN
*ReadSize
,
850 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
853 FileHandle - The handle to the PE/COFF file
854 FileOffset - The offset, in bytes, into the file to read
855 ReadSize - The number of bytes to read from the file starting at FileOffset
856 Buffer - A pointer to the buffer to read the data into.
859 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
867 Destination8
= Buffer
;
868 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
871 *(Destination8
++) = *(Source8
++);
878 CountSeperatorsInString (
879 IN
const CHAR16
*String
,
885 Count the number of seperators in String
888 String - String to process
889 Seperator - Item to count
892 Number of Seperator in String
898 for (Count
= 0; *String
!= '\0'; String
++) {
899 if (*String
== Seperator
) {
910 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
916 Store the ModHandle in an array indexed by the Pdb File name.
917 The ModHandle is needed to unload the image.
920 ImageContext - Input data returned from PE Laoder Library. Used to find the
921 .PDB file name of the PE Image.
922 ModHandle - Returned from LoadLibraryEx() and stored for call to
926 EFI_SUCCESS - ModHandle was stored.
931 IMAGE_CONTEXT_TO_MOD_HANDLE
*Array
;
935 Array
= mImageContextModHandleArray
;
936 for (Index
= 0; Index
< mImageContextModHandleArraySize
; Index
++, Array
++) {
937 if (Array
->ImageContext
== NULL
) {
939 // Make a copy of the stirng and store the ModHandle
941 Array
->ImageContext
= ImageContext
;
942 Array
->ModHandle
= ModHandle
;
948 // No free space in mImageContextModHandleArray so grow it by
949 // IMAGE_CONTEXT_TO_MOD_HANDLE entires. realloc will
950 // copy the old values to the new locaiton. But it does
951 // not zero the new memory area.
953 PreviousSize
= mImageContextModHandleArraySize
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
);
954 mImageContextModHandleArraySize
+= MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE
;
956 mImageContextModHandleArray
= realloc (mImageContextModHandleArray
, mImageContextModHandleArraySize
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
));
957 if (mImageContextModHandleArray
== NULL
) {
959 return EFI_OUT_OF_RESOURCES
;
962 memset (mImageContextModHandleArray
+ PreviousSize
, 0, MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
));
964 return AddHandle (ImageContext
, ModHandle
);
970 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
975 Return the ModHandle and delete the entry in the array.
978 ImageContext - Input data returned from PE Laoder Library. Used to find the
979 .PDB file name of the PE Image.
982 ModHandle - ModHandle assoicated with ImageContext is returned
983 NULL - No ModHandle associated with ImageContext
988 IMAGE_CONTEXT_TO_MOD_HANDLE
*Array
;
990 if (ImageContext
->PdbPointer
== NULL
) {
992 // If no PDB pointer there is no ModHandle so return NULL
997 Array
= mImageContextModHandleArray
;
998 for (Index
= 0; Index
< mImageContextModHandleArraySize
; Index
++, Array
++) {
999 if ((Array
->ImageContext
== ImageContext
)) {
1001 // If you find a match return it and delete the entry
1003 Array
->ImageContext
= NULL
;
1004 return Array
->ModHandle
;
1014 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to source a
1015 // add-symbol-file command. Hey what can you say scripting in gdb is not that great....
1017 // Put .gdbinit in the CWD where you do gdb SecMain.dll for source level debug
1020 // b SecGdbScriptBreak
1023 // source SecMain.dll.gdb
1035 SecUnixLoaderBreak (
1043 IN CHAR8
*PdbFileName
1048 if (PdbFileName
== NULL
) {
1052 Len
= strlen (PdbFileName
);
1053 if ((Len
< 5)|| (PdbFileName
[Len
- 4] != '.')) {
1057 if ((PdbFileName
[Len
- 3] == 'P' || PdbFileName
[Len
- 3] == 'p') &&
1058 (PdbFileName
[Len
- 2] == 'D' || PdbFileName
[Len
- 2] == 'd') &&
1059 (PdbFileName
[Len
- 1] == 'B' || PdbFileName
[Len
- 1] == 'b')) {
1067 #define MAX_SPRINT_BUFFER_SIZE 0x200
1071 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1074 if (ImageContext
->PdbPointer
== NULL
) {
1076 "0x%08lx Loading NO DEBUG with entry point 0x%08lx\n",
1077 (unsigned long)(ImageContext
->ImageAddress
),
1078 (unsigned long)ImageContext
->EntryPoint
1082 "0x%08lx Loading %s with entry point 0x%08lx\n",
1083 (unsigned long)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
),
1084 ImageContext
->PdbPointer
,
1085 (unsigned long)ImageContext
->EntryPoint
1088 // Keep output synced up
1095 SecPeCoffRelocateImageExtraAction (
1096 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1101 PrintLoadAddress (ImageContext
);
1104 // In mach-o (OS X executable) dlopen() can only load files in the MH_DYLIB of MH_BUNDLE format.
1105 // To convert to PE/COFF we need to construct a mach-o with the MH_PRELOAD format. We create
1106 // .dSYM files for the PE/COFF images that can be used by gdb for source level debugging.
1111 // In the Mach-O to PE/COFF conversion the size of the PE/COFF headers is not accounted for.
1112 // Thus we need to skip over the PE/COFF header when giving load addresses for our symbol table.
1114 if (ImageContext
->PdbPointer
!= NULL
&& !IsPdbFile (ImageContext
->PdbPointer
)) {
1116 // Now we have a database of the images that are currently loaded
1120 // 'symbol-file' will clear out currnet symbol mappings in gdb.
1121 // you can do a 'add-symbol-file filename address' for every image we loaded to get source
1122 // level debug in gdb. Note Sec, being a true application will work differently.
1124 // We add the PE/COFF header size into the image as the mach-O does not have a header in
1125 // loaded into system memory.
1127 // This gives us a data base of gdb commands and after something is unloaded that entry will be
1128 // removed. We don't yet have the scheme of how to comunicate with gdb, but we have the
1129 // data base of info ready to roll.
1131 // We could use qXfer:libraries:read, but OS X GDB does not currently support it.
1133 // <library name="/lib/libc.so.6"> // ImageContext->PdbPointer
1134 // <segment address="0x10000000"/> // ImageContext->ImageAddress + ImageContext->SizeOfHeaders
1140 // Write the file we need for the gdb script
1142 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
1143 if (GdbTempFile
!= NULL
) {
1144 fprintf (GdbTempFile
, "add-symbol-file %s 0x%08lx\n", ImageContext
->PdbPointer
, (long unsigned int)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
));
1145 fclose (GdbTempFile
);
1148 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
1149 // Hey what can you say scripting in gdb is not that great....
1151 SecGdbScriptBreak ();
1154 AddHandle (ImageContext
, ImageContext
->PdbPointer
);
1160 void *Handle
= NULL
;
1164 "Loading %s 0x%08lx - entry point 0x%08lx\n",
1165 ImageContext
->PdbPointer
,
1166 (unsigned long)ImageContext
->ImageAddress
,
1167 (unsigned long)ImageContext
->EntryPoint
);
1169 Handle
= dlopen (ImageContext
->PdbPointer
, RTLD_NOW
);
1172 Entry
= dlsym (Handle
, "_ModuleEntryPoint");
1174 printf("%s\n", dlerror());
1177 if (Entry
!= NULL
) {
1178 ImageContext
->EntryPoint
= (UINTN
)Entry
;
1179 printf("Change %s Entrypoint to :0x%08lx\n", ImageContext
->PdbPointer
, (unsigned long)Entry
);
1182 SecUnixLoaderBreak ();
1192 SecPeCoffLoaderUnloadImageExtraAction (
1193 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1198 Handle
= RemoveHandle (ImageContext
);
1203 if (Handle
!= NULL
) {
1205 // Need to skip .PDB files created from VC++
1207 if (!IsPdbFile (ImageContext
->PdbPointer
)) {
1209 // Write the file we need for the gdb script
1211 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
1212 if (GdbTempFile
!= NULL
) {
1213 fprintf (GdbTempFile
, "remove-symbol-file %s\n", ImageContext
->PdbPointer
);
1214 fclose (GdbTempFile
);
1217 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
1218 // Hey what can you say scripting in gdb is not that great....
1220 SecGdbScriptBreak ();
1227 // Don't want to confuse gdb with symbols for something that got unloaded
1229 if (Handle
!= NULL
) {
1246 SecTemporaryRamSupport (
1247 IN CONST EFI_PEI_SERVICES
**PeiServices
,
1248 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase
,
1249 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase
,
1254 // Migrate the whole temporary memory to permenent memory.
1257 (VOID
*)(UINTN
)PermanentMemoryBase
,
1258 (VOID
*)(UINTN
)TemporaryMemoryBase
,
1263 // SecSwitchStack function must be invoked after the memory migration
1264 // immediatly, also we need fixup the stack change caused by new call into
1265 // permenent memory.
1268 (UINT32
) TemporaryMemoryBase
,
1269 (UINT32
) PermanentMemoryBase
1273 // We need *not* fix the return address because currently,
1274 // The PeiCore is excuted in flash.
1278 // Simulate to invalid temporary memory, terminate temporary memory
1280 //ZeroMem ((VOID*)(UINTN)TemporaryMemoryBase, CopySize);