12#define MAP_ANONYMOUS MAP_ANON
20 GasketSecUnixPeiAutoScan,
21 GasketSecUnixFdAddress,
22 GasketSecEmuThunkAddress
25char *gGdbWorkingFileName =
NULL;
26unsigned int mScriptSymbolChangesCount = 0;
35UINTN gFdInfoCount = 0;
44UINTN gSystemMemoryCount = 0;
47UINTN mImageContextModHandleArraySize = 0;
92 UINT64 InitialStackMemorySize;
101 CHAR16 *MemorySizeStr;
102 CHAR16 *FirmwareVolumesStr;
111 SecGdbConfigBreak ();
117 Index = strlen (*Argv);
118 gGdbWorkingFileName =
AllocatePool (Index + strlen (
".gdb") + 1);
119 strcpy (gGdbWorkingFileName, *Argv);
120 strcat (gGdbWorkingFileName,
".gdb");
125 GdbTempFile = fopen (gGdbWorkingFileName,
"w");
126 if (GdbTempFile !=
NULL) {
127 fclose (GdbTempFile);
130 printf (
"\nEDK II UNIX Host Emulation Environment from http://www.tianocore.org/edk2/\n");
135 MemorySizeStr = (CHAR16 *)
PcdGetPtr (PcdEmuMemorySize);
136 FirmwareVolumesStr = (CHAR16 *)
PcdGetPtr (PcdEmuFirmwareVolume);
142 if (SecEmuThunkPpi ==
NULL) {
143 printf (
"ERROR : Can not allocate memory for SecEmuThunkPpi. Exiting.\n");
148 SecEmuThunkPpi->Argc = Argc;
149 SecEmuThunkPpi->Argv = Argv;
150 SecEmuThunkPpi->Envp = Envp;
151 SecEmuThunkPpi->PersistentMemorySize =
FixedPcdGet32 (PcdPersistentMemorySize);
152 AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuThunkPpiGuid, SecEmuThunkPpi);
154 SecInitThunkProtocol ();
159 AddThunkProtocol (&gX11ThunkIo, (CHAR16 *)
PcdGetPtr (PcdEmuGop),
TRUE);
160 AddThunkProtocol (&gPosixFileSystemThunkIo, (CHAR16 *)
PcdGetPtr (PcdEmuFileSystem),
TRUE);
161 AddThunkProtocol (&gBlockIoThunkIo, (CHAR16 *)
PcdGetPtr (PcdEmuVirtualDisk),
TRUE);
162 AddThunkProtocol (&gSnpThunkIo, (CHAR16 *)
PcdGetPtr (PcdEmuNetworkInterface),
TRUE);
167 AddThunkProtocol (&gPthreadThunkIo, (CHAR16 *)
PcdGetPtr (PcdEmuApCount),
FALSE);
171 gPpiList = GetThunkPpiList ();
176 gSystemMemoryCount = CountSeparatorsInString (MemorySizeStr,
'!') + 1;
178 if (gSystemMemory ==
NULL) {
179 printf (
"ERROR : Can not allocate memory for system. Exiting.\n");
186 gFdInfoCount = CountSeparatorsInString (FirmwareVolumesStr,
'!') + 1;
188 if (gFdInfo ==
NULL) {
189 printf (
"ERROR : Can not allocate memory for fd info. Exiting.\n");
193 printf (
" BootMode 0x%02x\n", (
unsigned int)
PcdGet32 (PcdEmuBootMode));
200 InitialStackMemorySize = STACK_SIZE;
201 InitialStackMemory = (
UINTN)MapMemory (
203 (UINT32)InitialStackMemorySize,
204 PROT_READ | PROT_WRITE | PROT_EXEC,
205 MAP_ANONYMOUS | MAP_PRIVATE
207 if (InitialStackMemory == 0) {
208 printf (
"ERROR : Can not open SecStack Exiting\n");
213 " OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n",
214 (
unsigned int)(InitialStackMemorySize / 1024),
215 (
unsigned long)InitialStackMemory
218 for (StackPointer = (
UINTN *)(
UINTN)InitialStackMemory;
219 StackPointer < (
UINTN *)(
UINTN)((
UINTN)InitialStackMemory + (UINT64)InitialStackMemorySize);
222 *StackPointer = 0x5AA55AA5;
229 if (FileName ==
NULL) {
230 printf (
"ERROR : Can not allocate memory for firmware volume string\n");
235 for (Done =
FALSE, Index = 0, PeiIndex = 0, SecFile =
NULL;
236 FirmwareVolumesStr[Index2] != 0;
239 for (Index1 = 0; (FirmwareVolumesStr[Index2] !=
'!') && (FirmwareVolumesStr[Index2] != 0); Index2++) {
240 FileName[Index1++] = FirmwareVolumesStr[Index2];
243 if (FirmwareVolumesStr[Index2] ==
'!') {
247 FileName[Index1] =
'\0';
263 &gFdInfo[Index].Address,
268 if (EFI_ERROR (Status)) {
269 printf (
"ERROR : Can not open Firmware Device File %s (%x). Exiting.\n", FileName, (
unsigned int)Status);
273 printf (
" FD loaded from %s at 0x%08lx", FileName, (
unsigned long)gFdInfo[Index].Address);
275 if (SecFile ==
NULL) {
282 EFI_FV_FILETYPE_SECURITY_CORE,
286 if (!EFI_ERROR (Status)) {
288 if (!EFI_ERROR (Status)) {
290 printf (
" contains SEC Core");
298 if (SecFile ==
NULL) {
299 printf (
"ERROR : SEC not found!\n");
315 while (MemorySizeStr[Index1] >=
'0' && MemorySizeStr[Index1] <=
'9') {
316 val = val * 10 + MemorySizeStr[Index1] -
'0';
320 gSystemMemory[Index++].Size = val * 0x100000;
321 if (MemorySizeStr[Index1] == 0) {
333 SecLoadFromCore ((
UINTN)InitialStackMemory, (
UINTN)InitialStackMemorySize, (
UINTN)gFdInfo[0].Address, SecFile);
339 printf (
"ERROR : SEC returned\n");
354 BOOLEAN isAligned = 0;
360 while ((!isAligned) && (base != 0)) {
361 res = mmap ((
void *)base, length, prot, flags, fd, 0);
362 if (res == MAP_FAILED) {
366 if ((((
UINTN)res) & ~(align-1)) == (
UINTN)res) {
369 munmap (res, length);
411 fd = open (FileName, O_RDWR);
413 return EFI_NOT_FOUND;
416 FileSize = lseek (fd, 0, SEEK_END);
418 res = MapMemory (fd, FileSize, PROT_READ | PROT_EXEC, MAP_PRIVATE);
423 perror (
"MapFile() Failed");
424 return EFI_DEVICE_ERROR;
427 *Length = (UINT64)FileSize;
441 void *res, *res2, *res3;
446 fd = open (FileName, O_RDWR);
448 return EFI_NOT_FOUND;
451 FileSize = lseek (fd, 0, SEEK_END);
459 PROT_READ | PROT_EXEC,
464 if (res == MAP_FAILED) {
465 perror (
"MapFd0() Failed res =");
467 return EFI_DEVICE_ERROR;
470 munmap (res, FvSize);
474 PROT_READ | PROT_WRITE | PROT_EXEC,
479 if (res == MAP_FAILED) {
480 perror (
"MapFd0() Failed res =");
482 return EFI_DEVICE_ERROR;
490 PROT_READ | PROT_WRITE | PROT_EXEC,
496 if (res2 == MAP_FAILED) {
497 perror (
"MapFd0() Failed res2 =");
498 return EFI_DEVICE_ERROR;
508 if (EmuMagicPage !=
NULL) {
510 (
void *)EmuMagicPage,
512 PROT_READ | PROT_WRITE,
513 MAP_PRIVATE | MAP_ANONYMOUS,
517 if (res3 != EmuMagicPage) {
518 printf (
"MapFd0(): Could not allocate PeiServicesTablePage @ %lx\n", (
long unsigned int)EmuMagicPage);
519 return EFI_DEVICE_ERROR;
523 *Length = (UINT64)FileSize;
548 IN UINTN BootFirmwareVolumeBase,
549 IN VOID *PeiCorePe32File
562 TopOfMemory = LargestRegion + LargestRegionSize;
574 TopOfStack = (VOID *)(LargestRegion + PeiStackSize);
575 TopOfMemory = LargestRegion + PeiStackSize;
600 Status = SecPeCoffGetEntryPoint (PeiCorePe32File, (VOID **)&PeiCoreEntryPoint);
601 if (EFI_ERROR (Status)) {
644 OUT UINT64 *MemorySize
649 if (Index >= gSystemMemoryCount) {
650 return EFI_UNSUPPORTED;
656 gSystemMemory[Index].Size,
657 PROT_READ | PROT_WRITE | PROT_EXEC,
658 MAP_PRIVATE | MAP_ANONYMOUS
660 if (res == MAP_FAILED) {
661 return EFI_DEVICE_ERROR;
664 *MemorySize = gSystemMemory[Index].Size;
665 *MemoryBase = (
UINTN)res;
666 gSystemMemory[Index].Memory = *MemoryBase;
691EfiSystemMemoryRange (
692 IN VOID *MemoryAddress
699 for (Index = 0; Index < gSystemMemoryCount; Index++) {
700 if ((MemoryBase >= gSystemMemory[Index].Memory) &&
701 (MemoryBase < (gSystemMemory[Index].Memory + gSystemMemory[Index].Size)))
729 return &gEmuThunkProtocol;
734SecPeCoffGetEntryPoint (
736 IN OUT VOID **EntryPoint
742 ZeroMem (&ImageContext,
sizeof (ImageContext));
743 ImageContext.
Handle = Pe32Data;
747 if (EFI_ERROR (Status)) {
757 if (EFI_ERROR (Status)) {
762 if (EFI_ERROR (Status)) {
769 ImageContext.
PdbPointer = PeCoffLoaderGetPdbPointer (Pe32Data);
770 Status = PeCoffLoaderGetEntryPoint (Pe32Data, EntryPoint);
771 if (EFI_ERROR (Status)) {
779 SecPeCoffRelocateImageExtraAction (&ImageContext);
806 IN OUT UINT64 *FdSize,
810 if (Index >= gFdInfoCount) {
811 return EFI_UNSUPPORTED;
814 *FdBase = gFdInfo[Index].Address;
815 *FdSize = gFdInfo[Index].Size;
818 if ((*FdBase == 0) && (*FdSize == 0)) {
819 return EFI_UNSUPPORTED;
828 *FixUp = *FdBase -
PcdGet64 (PcdEmuFdBaseAddress);
848CountSeparatorsInString (
849 IN const CHAR16 *String,
855 for (Count = 0; *String !=
'\0'; String++) {
856 if (*String == Separator) {
893 Destination8 = Buffer;
894 Source8 = (CHAR8 *)((
UINTN)FileHandle + FileOffset);
897 *(Destination8++) = *(Source8++);
929 Array = mImageContextModHandleArray;
930 for (Index = 0; Index < mImageContextModHandleArraySize; Index++, Array++) {
931 if (Array->ImageContext ==
NULL) {
935 Array->ImageContext = ImageContext;
936 Array->ModHandle = ModHandle;
948 mImageContextModHandleArraySize += MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE;
953 mImageContextModHandleArray
955 if (mImageContextModHandleArray ==
NULL) {
957 return EFI_OUT_OF_RESOURCES;
960 memset (mImageContextModHandleArray + PreviousSize, 0, MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE *
sizeof (
IMAGE_CONTEXT_TO_MOD_HANDLE));
962 return AddHandle (ImageContext, ModHandle);
987 if (ImageContext->PdbPointer ==
NULL) {
994 Array = mImageContextModHandleArray;
995 for (Index = 0; Index < mImageContextModHandleArraySize; Index++, Array++) {
996 if (Array->ImageContext == ImageContext) {
1000 Array->ImageContext =
NULL;
1001 return Array->ModHandle;
1010 IN CHAR8 *PdbFileName
1015 if (PdbFileName ==
NULL) {
1019 Len = strlen (PdbFileName);
1020 if ((Len < 5) || (PdbFileName[Len - 4] !=
'.')) {
1024 if (((PdbFileName[Len - 3] ==
'P') || (PdbFileName[Len - 3] ==
'p')) &&
1025 ((PdbFileName[Len - 2] ==
'D') || (PdbFileName[Len - 2] ==
'd')) &&
1026 ((PdbFileName[Len - 1] ==
'B') || (PdbFileName[Len - 1] ==
'b')))
1034#define MAX_SPRINT_BUFFER_SIZE 0x200
1041 if (ImageContext->PdbPointer ==
NULL) {
1044 "0x%08lx Loading NO DEBUG with entry point 0x%08lx\n",
1045 (
unsigned long)(ImageContext->ImageAddress),
1046 (
unsigned long)ImageContext->EntryPoint
1051 "0x%08lx Loading %s with entry point 0x%08lx\n",
1052 (
unsigned long)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders),
1053 ImageContext->PdbPointer,
1054 (
unsigned long)ImageContext->EntryPoint
1083 void *Handle =
NULL;
1086 if (ImageContext->PdbPointer ==
NULL) {
1090 if (!IsPdbFile (ImageContext->PdbPointer)) {
1096 "Loading %s 0x%08lx - entry point 0x%08lx\n",
1097 ImageContext->PdbPointer,
1098 (
unsigned long)ImageContext->ImageAddress,
1099 (
unsigned long)ImageContext->EntryPoint
1102 Handle = dlopen (ImageContext->PdbPointer, RTLD_NOW);
1103 if (Handle !=
NULL) {
1104 Entry = dlsym (Handle,
"_ModuleEntryPoint");
1105 AddHandle (ImageContext, Handle);
1107 printf (
"%s\n", dlerror ());
1110 if (Entry !=
NULL) {
1111 ImageContext->EntryPoint = (
UINTN)Entry;
1112 printf (
"Change %s Entrypoint to :0x%08lx\n", ImageContext->PdbPointer, (
unsigned long)Entry);
1128 long unsigned int LoadAddress,
1147 PrintLoadAddress (ImageContext);
1149 if ((ImageContext->PdbPointer !=
NULL) && !IsPdbFile (ImageContext->PdbPointer)) {
1152 GdbTempFile = fopen (gGdbWorkingFileName,
"a");
1153 if (GdbTempFile !=
NULL) {
1154 long unsigned int SymbolsAddr = (
long unsigned int)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders);
1155 mScriptSymbolChangesCount++;
1158 "AddFirmwareSymbolFile 0x%x %s 0x%08lx\n",
1159 mScriptSymbolChangesCount,
1160 ImageContext->PdbPointer,
1163 fclose (GdbTempFile);
1165 SecGdbScriptBreak (ImageContext->PdbPointer, strlen (ImageContext->PdbPointer) + 1, (
long unsigned int)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders), 1);
1170 GdbTempFile = fopen (gGdbWorkingFileName,
"w");
1171 if (GdbTempFile !=
NULL) {
1174 "add-symbol-file %s 0x%08lx\n",
1175 ImageContext->PdbPointer,
1176 (
long unsigned int)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders)
1178 fclose (GdbTempFile);
1186 SecGdbScriptBreak (ImageContext->PdbPointer, strlen (ImageContext->PdbPointer) + 1, (
long unsigned int)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders), 1);
1196SecPeCoffRelocateImageExtraAction (
1200 if (!DlLoadImage (ImageContext)) {
1201 GdbScriptAddImage (ImageContext);
1213GdbScriptRemoveImage (
1222 if (IsPdbFile (ImageContext->PdbPointer)) {
1230 GdbTempFile = fopen (gGdbWorkingFileName,
"a");
1231 if (GdbTempFile !=
NULL) {
1232 mScriptSymbolChangesCount++;
1235 "RemoveFirmwareSymbolFile 0x%x %s\n",
1236 mScriptSymbolChangesCount,
1237 ImageContext->PdbPointer
1239 fclose (GdbTempFile);
1240 SecGdbScriptBreak (ImageContext->PdbPointer, strlen (ImageContext->PdbPointer) + 1, 0, 0);
1245 GdbTempFile = fopen (gGdbWorkingFileName,
"w");
1246 if (GdbTempFile !=
NULL) {
1247 fprintf (GdbTempFile,
"remove-symbol-file %s\n", ImageContext->PdbPointer);
1248 fclose (GdbTempFile);
1254 SecGdbScriptBreak (ImageContext->PdbPointer, strlen (ImageContext->PdbPointer) + 1, 0, 0);
1263SecPeCoffUnloadImageExtraAction (
1272 Handle = RemoveHandle (ImageContext);
1273 if (Handle !=
NULL) {
1280 GdbScriptRemoveImage (ImageContext);
#define CPU_STACK_ALIGNMENT
UINT64 EFIAPI RShiftU64(IN UINT64 Operand, IN UINTN Count)
VOID(EFIAPI * SWITCH_STACK_ENTRY_POINT)(IN VOID *Context1 OPTIONAL, IN VOID *Context2 OPTIONAL)
UINTN EFIAPI StrLen(IN CONST CHAR16 *String)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
int main()
=== TEST ENGINE ================================================================================
VOID *EFIAPI ReallocatePool(IN UINTN OldSize, IN UINTN NewSize, IN VOID *OldBuffer OPTIONAL)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
EFI_STATUS EFIAPI PeiServicesFfsFindSectionData(IN EFI_SECTION_TYPE SectionType, IN EFI_PEI_FILE_HANDLE FileHandle, OUT VOID **SectionData)
EFI_STATUS EFIAPI PeiServicesFfsFindNextFile(IN EFI_FV_FILETYPE SearchType, IN EFI_PEI_FV_HANDLE VolumeHandle, IN OUT EFI_PEI_FILE_HANDLE *FileHandle)
#define ALIGN_POINTER(Pointer, Alignment)
#define PcdGet64(TokenName)
#define FixedPcdGet32(TokenName)
#define FixedPcdGet64(TokenName)
#define PcdGet32(TokenName)
#define PcdGetPtr(TokenName)
#define FeaturePcdGet(TokenName)
RETURN_STATUS EFIAPI PeCoffLoaderLoadImage(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
RETURN_STATUS EFIAPI PeCoffLoaderRelocateImage(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
RETURN_STATUS(EFIAPI * PE_COFF_LOADER_READ_FILE)(IN VOID *FileHandle, IN UINTN FileOffset, IN OUT UINTN *ReadSize, OUT VOID *Buffer)
RETURN_STATUS EFIAPI PeCoffLoaderGetImageInfo(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
VOID * EFI_PEI_FILE_HANDLE
struct _EFI_SEC_PEI_HAND_OFF EFI_SEC_PEI_HAND_OFF
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
unsigned long long UINT64 __attribute__((aligned(8)))
UINT64 EFI_PHYSICAL_ADDRESS
UINTN PeiTemporaryRamSize
VOID * BootFirmwareVolumeBase
UINTN BootFirmwareVolumeSize
VOID * PeiTemporaryRamBase
PE_COFF_LOADER_READ_FILE ImageRead
PHYSICAL_ADDRESS EntryPoint
PHYSICAL_ADDRESS ImageAddress