3 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
4 Portions copyright (c) 2008 - 2011, 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 #define MAP_ANONYMOUS MAP_ANON
19 char *gGdbWorkingFileName
= NULL
;
27 EMU_THUNK_PPI mSecEmuThunkPpi
= {
28 GasketSecUnixPeiAutoScan
,
29 GasketSecUnixFdAddress
,
30 GasketSecEmuThunkAddress
37 // Default information about where the FD is located.
38 // This array gets filled in with information from EFI_FIRMWARE_VOLUMES
39 // EFI_FIRMWARE_VOLUMES is a host environment variable set by system.cmd.
40 // The number of array elements is allocated base on parsing
41 // EFI_FIRMWARE_VOLUMES and the memory is never freed.
43 UINTN gFdInfoCount
= 0;
47 // Array that supports seperate memory rantes.
48 // The memory ranges are set in system.cmd via the EFI_MEMORY_SIZE variable.
49 // The number of array elements is allocated base on parsing
50 // EFI_MEMORY_SIZE and the memory is never freed.
52 UINTN gSystemMemoryCount
= 0;
53 EMU_SYSTEM_MEMORY
*gSystemMemory
;
57 UINTN mImageContextModHandleArraySize
= 0;
58 IMAGE_CONTEXT_TO_MOD_HANDLE
*mImageContextModHandleArray
= NULL
;
60 EFI_PEI_PPI_DESCRIPTOR
*gPpiList
;
65 Main entry point to SEC for Unix. This is a unix program
68 Argc - Number of command line arguments
69 Argv - Array of command line argument strings
70 Envp - Array of environmemt variable strings
85 EFI_PHYSICAL_ADDRESS InitialStackMemory
;
86 UINT64 InitialStackMemorySize
;
93 EFI_PEI_FILE_HANDLE FileHandle
;
95 CHAR16
*MemorySizeStr
;
96 CHAR16
*FirmwareVolumesStr
;
102 MemorySizeStr
= (CHAR16
*) PcdGetPtr (PcdEmuMemorySize
);
103 FirmwareVolumesStr
= (CHAR16
*) PcdGetPtr (PcdEmuFirmwareVolume
);
105 printf ("\nEDK II UNIX Emulation Environment from edk2.sourceforge.net\n");
108 // PPIs pased into PEI_CORE
110 AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI
, &gEmuThunkPpiGuid
, &mSecEmuThunkPpi
);
112 SecInitThunkProtocol ();
115 // Emulator Bus Driver Thunks
117 AddThunkProtocol (&gX11ThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuGop
), TRUE
);
118 AddThunkProtocol (&gPosixFileSystemThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuFileSystem
), TRUE
);
119 AddThunkProtocol (&gBlockIoThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuVirtualDisk
), TRUE
);
120 AddThunkProtocol (&gSnpThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuNetworkInterface
), TRUE
);
123 // Emulator other Thunks
125 AddThunkProtocol (&gPthreadThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuApCount
), FALSE
);
127 // EmuSecLibConstructor ();
129 gPpiList
= GetThunkPpiList ();
134 // We can't use dlopen on OS X, so we need a scheme to get symboles into gdb
135 // We need to create a temp file that contains gdb commands so we can load
136 // symbols when we load every PE/COFF image.
138 Index
= strlen (*Argv
);
139 gGdbWorkingFileName
= AllocatePool (Index
+ strlen(".gdb") + 1);
140 strcpy (gGdbWorkingFileName
, *Argv
);
141 strcat (gGdbWorkingFileName
, ".gdb");
146 // Allocate space for gSystemMemory Array
148 gSystemMemoryCount
= CountSeperatorsInString (MemorySizeStr
, '!') + 1;
149 gSystemMemory
= AllocateZeroPool (gSystemMemoryCount
* sizeof (EMU_SYSTEM_MEMORY
));
150 if (gSystemMemory
== NULL
) {
151 printf ("ERROR : Can not allocate memory for system. Exiting.\n");
155 // Allocate space for gSystemMemory Array
157 gFdInfoCount
= CountSeperatorsInString (FirmwareVolumesStr
, '!') + 1;
158 gFdInfo
= AllocateZeroPool (gFdInfoCount
* sizeof (EMU_FD_INFO
));
159 if (gFdInfo
== NULL
) {
160 printf ("ERROR : Can not allocate memory for fd info. Exiting.\n");
164 printf (" BootMode 0x%02x\n", (unsigned int)PcdGet32 (PcdEmuBootMode
));
167 // Open up a 128K file to emulate temp memory for SEC.
168 // on a real platform this would be SRAM, or using the cache as RAM.
169 // Set InitialStackMemory to zero so UnixOpenFile will allocate a new mapping
171 InitialStackMemorySize
= STACK_SIZE
;
172 InitialStackMemory
= (UINTN
)MapMemory (
173 0, (UINT32
) InitialStackMemorySize
,
174 PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_ANONYMOUS
| MAP_PRIVATE
176 if (InitialStackMemory
== 0) {
177 printf ("ERROR : Can not open SecStack Exiting\n");
181 printf (" OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n",
182 (unsigned int)(InitialStackMemorySize
/ 1024),
183 (unsigned long)InitialStackMemory
186 for (StackPointer
= (UINTN
*) (UINTN
) InitialStackMemory
;
187 StackPointer
< (UINTN
*)(UINTN
)((UINTN
) InitialStackMemory
+ (UINT64
) InitialStackMemorySize
);
189 *StackPointer
= 0x5AA55AA5;
193 // Open All the firmware volumes and remember the info in the gFdInfo global
195 FileName
= (CHAR8
*) AllocatePool (StrLen (FirmwareVolumesStr
) + 1);
196 if (FileName
== NULL
) {
197 printf ("ERROR : Can not allocate memory for firmware volume string\n");
202 for (Done
= FALSE
, Index
= 0, PeiIndex
= 0, SecFile
= NULL
;
203 FirmwareVolumesStr
[Index2
] != 0;
205 for (Index1
= 0; (FirmwareVolumesStr
[Index2
] != '!') && (FirmwareVolumesStr
[Index2
] != 0); Index2
++) {
206 FileName
[Index1
++] = FirmwareVolumesStr
[Index2
];
208 if (FirmwareVolumesStr
[Index2
] == '!') {
211 FileName
[Index1
] = '\0';
214 // Map FV Recovery Read Only and other areas Read/Write
222 // Open the FD and remmeber where it got mapped into our processes address space
227 &gFdInfo
[Index
].Address
,
231 if (EFI_ERROR (Status
)) {
232 printf ("ERROR : Can not open Firmware Device File %s (%x). Exiting.\n", FileName
, (unsigned int)Status
);
236 printf (" FD loaded from %s at 0x%08lx",FileName
, (unsigned long)gFdInfo
[Index
].Address
);
238 if (SecFile
== NULL
) {
240 // Assume the beginning of the FD is an FV and look for the SEC Core.
241 // Load the first one we find.
244 Status
= PeiServicesFfsFindNextFile (
245 EFI_FV_FILETYPE_SECURITY_CORE
,
246 (EFI_PEI_FV_HANDLE
)(UINTN
)gFdInfo
[Index
].Address
,
249 if (!EFI_ERROR (Status
)) {
250 Status
= PeiServicesFfsFindSectionData (EFI_SECTION_PE32
, FileHandle
, &SecFile
);
251 if (!EFI_ERROR (Status
)) {
253 printf (" contains SEC Core");
261 // Calculate memory regions and store the information in the gSystemMemory
262 // global for later use. The autosizing code will use this data to
263 // map this memory into the SEC process memory space.
270 // Save the size of the memory.
272 while (MemorySizeStr
[Index1
] >= '0' && MemorySizeStr
[Index1
] <= '9') {
273 val
= val
* 10 + MemorySizeStr
[Index1
] - '0';
276 gSystemMemory
[Index
++].Size
= val
* 0x100000;
277 if (MemorySizeStr
[Index1
] == 0) {
288 SecLoadFromCore ((UINTN
) InitialStackMemory
, (UINTN
) InitialStackMemorySize
, (UINTN
) gFdInfo
[0].Address
, SecFile
);
291 // If we get here, then the SEC Core returned. This is an error as SEC should
292 // always hand off to PEI Core and then on to DXE Core.
294 printf ("ERROR : SEC returned\n");
299 EFI_PHYSICAL_ADDRESS
*
307 STATIC UINTN base
= 0x40000000;
308 CONST UINTN align
= (1 << 24);
310 BOOLEAN isAligned
= 0;
313 // Try to get an aligned block somewhere in the address space of this
316 while((!isAligned
) && (base
!= 0)) {
317 res
= mmap ((void *)base
, length
, prot
, flags
, fd
, 0);
318 if (res
== MAP_FAILED
) {
321 if ((((UINTN
)res
) & ~(align
-1)) == (UINTN
)res
) {
335 Opens and memory maps a file using Unix services. If BaseAddress is non zero
336 the process will try and allocate the memory starting at BaseAddress.
339 FileName - The name of the file to open and map
340 MapSize - The amount of the file to map in bytes
341 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for
342 memory emulation, and exiting files for firmware volume emulation
343 BaseAddress - The base address of the mapped file in the user address space.
344 If passed in as NULL the a new memory region is used.
345 If passed in as non NULL the request memory region is used for
346 the mapping of the file into the process space.
347 Length - The size of the mapped region in bytes
350 EFI_SUCCESS - The file was opened and mapped.
351 EFI_NOT_FOUND - FileName was not found in the current directory
352 EFI_DEVICE_ERROR - An error occured attempting to map the opened file
358 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
366 fd
= open (FileName
, O_RDWR
);
368 return EFI_NOT_FOUND
;
370 FileSize
= lseek (fd
, 0, SEEK_END
);
373 res
= MapMemory (fd
, FileSize
, PROT_READ
| PROT_EXEC
, MAP_PRIVATE
);
378 perror ("MapFile() Failed");
379 return EFI_DEVICE_ERROR
;
382 *Length
= (UINT64
) FileSize
;
383 *BaseAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) res
;
391 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
396 void *res
, *res2
, *res3
;
401 fd
= open (FileName
, O_RDWR
);
403 return EFI_NOT_FOUND
;
405 FileSize
= lseek (fd
, 0, SEEK_END
);
407 FvSize
= FixedPcdGet64 (PcdEmuFlashFvRecoverySize
);
409 // Assume start of FD is Recovery FV, and make it write protected
411 (void *)(UINTN
)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase
),
413 PROT_READ
| PROT_EXEC
,
418 if (res
== MAP_FAILED
) {
419 perror ("MapFd0() Failed res =");
421 return EFI_DEVICE_ERROR
;
422 } else if (res
!= (void *)(UINTN
)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase
)) {
423 // We could not load at the build address, so we need to allow writes
424 munmap (res
, FvSize
);
426 (void *)(UINTN
)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase
),
428 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
433 if (res
== MAP_FAILED
) {
434 perror ("MapFd0() Failed res =");
436 return EFI_DEVICE_ERROR
;
440 // Map the rest of the FD as read/write
442 (void *)(UINTN
)(FixedPcdGet64 (PcdEmuFlashFvRecoveryBase
) + FvSize
),
444 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
450 if (res2
== MAP_FAILED
) {
451 perror ("MapFd0() Failed res2 =");
452 return EFI_DEVICE_ERROR
;
456 // If enabled use the magic page to communicate between modules
457 // This replaces the PI PeiServicesTable pointer mechanism that
458 // deos not work in the emulator. It also allows the removal of
459 // writable globals from SEC, PEI_CORE (libraries), PEIMs
461 EmuMagicPage
= (void *)(UINTN
)FixedPcdGet64 (PcdPeiServicesTablePage
);
462 if (EmuMagicPage
!= NULL
) {
464 (void *)EmuMagicPage
,
466 PROT_READ
| PROT_WRITE
,
467 MAP_PRIVATE
| MAP_ANONYMOUS
,
471 if (res3
!= EmuMagicPage
) {
472 printf ("MapFd0(): Could not allocate PeiServicesTablePage @ %lx\n", (long unsigned int)EmuMagicPage
);
473 return EFI_DEVICE_ERROR
;
477 *Length
= (UINT64
) FileSize
;
478 *BaseAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) res
;
487 This is the service to load the SEC Core from the Firmware Volume
490 LargestRegion - Memory to use for SEC.
491 LargestRegionSize - Size of Memory to use for PEI
492 BootFirmwareVolumeBase - Start of the Boot FV
493 PeiCorePe32File - SEC PE32
496 Success means control is transfered and thus we should never return
501 IN UINTN LargestRegion
,
502 IN UINTN LargestRegionSize
,
503 IN UINTN BootFirmwareVolumeBase
,
504 IN VOID
*PeiCorePe32File
508 EFI_PHYSICAL_ADDRESS TopOfMemory
;
510 EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint
;
511 EFI_SEC_PEI_HAND_OFF
*SecCoreData
;
515 // Compute Top Of Memory for Stack and PEI Core Allocations
517 TopOfMemory
= LargestRegion
+ LargestRegionSize
;
518 PeiStackSize
= (UINTN
)RShiftU64((UINT64
)STACK_SIZE
,1);
521 // |-----------| <---- TemporaryRamBase + TemporaryRamSize
524 // |-----------| <---- StackBase / PeiTemporaryMemoryBase
527 // |-----------| <---- TemporaryRamBase
529 TopOfStack
= (VOID
*)(LargestRegion
+ PeiStackSize
);
530 TopOfMemory
= LargestRegion
+ PeiStackSize
;
533 // Reservet space for storing PeiCore's parament in stack.
535 TopOfStack
= (VOID
*)((UINTN
)TopOfStack
- sizeof (EFI_SEC_PEI_HAND_OFF
) - CPU_STACK_ALIGNMENT
);
536 TopOfStack
= ALIGN_POINTER (TopOfStack
, CPU_STACK_ALIGNMENT
);
540 // Bind this information into the SEC hand-off state
542 SecCoreData
= (EFI_SEC_PEI_HAND_OFF
*)(UINTN
) TopOfStack
;
543 SecCoreData
->DataSize
= sizeof(EFI_SEC_PEI_HAND_OFF
);
544 SecCoreData
->BootFirmwareVolumeBase
= (VOID
*)BootFirmwareVolumeBase
;
545 SecCoreData
->BootFirmwareVolumeSize
= PcdGet32 (PcdEmuFirmwareFdSize
);
546 SecCoreData
->TemporaryRamBase
= (VOID
*)(UINTN
)LargestRegion
;
547 SecCoreData
->TemporaryRamSize
= STACK_SIZE
;
548 SecCoreData
->StackBase
= SecCoreData
->TemporaryRamBase
;
549 SecCoreData
->StackSize
= PeiStackSize
;
550 SecCoreData
->PeiTemporaryRamBase
= (VOID
*) ((UINTN
) SecCoreData
->TemporaryRamBase
+ PeiStackSize
);
551 SecCoreData
->PeiTemporaryRamSize
= STACK_SIZE
- PeiStackSize
;
554 // Find the SEC Core Entry Point
556 Status
= SecPeCoffGetEntryPoint (PeiCorePe32File
, (VOID
**)&PeiCoreEntryPoint
);
557 if (EFI_ERROR (Status
)) {
562 // Transfer control to the SEC Core
565 (SWITCH_STACK_ENTRY_POINT
) (UINTN
) PeiCoreEntryPoint
,
571 // If we get here, then the SEC Core returned. This is an error
580 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.
581 It allows discontiguous memory regions to be supported by the emulator.
582 It uses gSystemMemory[] and gSystemMemoryCount that were created by
583 parsing the host environment variable EFI_MEMORY_SIZE.
584 The size comes from the varaible and the address comes from the call to
588 Index - Which memory region to use
589 MemoryBase - Return Base address of memory region
590 MemorySize - Return size in bytes of the memory region
593 EFI_SUCCESS - If memory region was mapped
594 EFI_UNSUPPORTED - If Index is not supported
600 OUT EFI_PHYSICAL_ADDRESS
*MemoryBase
,
601 OUT UINT64
*MemorySize
606 if (Index
>= gSystemMemoryCount
) {
607 return EFI_UNSUPPORTED
;
612 0, gSystemMemory
[Index
].Size
,
613 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
614 MAP_PRIVATE
| MAP_ANONYMOUS
616 if (res
== MAP_FAILED
) {
617 return EFI_DEVICE_ERROR
;
619 *MemorySize
= gSystemMemory
[Index
].Size
;
620 *MemoryBase
= (UINTN
)res
;
621 gSystemMemory
[Index
].Memory
= *MemoryBase
;
630 Check to see if an address range is in the EFI GCD memory map.
632 This is all of GCD for system memory passed to DXE Core. FV
633 mapping and other device mapped into system memory are not
634 inlcuded in the check.
637 Index - Which memory region to use
638 MemoryBase - Return Base address of memory region
639 MemorySize - Return size in bytes of the memory region
642 TRUE - Address is in the EFI GCD memory map
643 FALSE - Address is NOT in memory map
647 EfiSystemMemoryRange (
648 IN VOID
*MemoryAddress
652 EFI_PHYSICAL_ADDRESS MemoryBase
;
654 MemoryBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)MemoryAddress
;
655 for (Index
= 0; Index
< gSystemMemoryCount
; Index
++) {
656 if ((MemoryBase
>= gSystemMemory
[Index
].Memory
) &&
657 (MemoryBase
< (gSystemMemory
[Index
].Memory
+ gSystemMemory
[Index
].Size
)) ) {
669 Since the SEC is the only Unix program in stack it must export
670 an interface to do POSIX calls. gUnix is initailized in UnixThunk.c.
673 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);
674 InterfaceBase - Address of the gUnix global
677 EFI_SUCCESS - Data returned
685 return &gEmuThunkProtocol
;
692 SecPeCoffGetEntryPoint (
694 IN OUT VOID
**EntryPoint
698 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
700 ZeroMem (&ImageContext
, sizeof (ImageContext
));
701 ImageContext
.Handle
= Pe32Data
;
702 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) SecImageRead
;
704 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
705 if (EFI_ERROR (Status
)) {
709 if (ImageContext
.ImageAddress
!= (UINTN
)Pe32Data
) {
711 // Relocate image to match the address where it resides
713 ImageContext
.ImageAddress
= (UINTN
)Pe32Data
;
714 Status
= PeCoffLoaderLoadImage (&ImageContext
);
715 if (EFI_ERROR (Status
)) {
719 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
720 if (EFI_ERROR (Status
)) {
725 // Or just return image entry point
727 ImageContext
.PdbPointer
= PeCoffLoaderGetPdbPointer (Pe32Data
);
728 Status
= PeCoffLoaderGetEntryPoint (Pe32Data
, EntryPoint
);
729 if (EFI_ERROR (Status
)) {
732 ImageContext
.EntryPoint
= (UINTN
)*EntryPoint
;
735 // On Unix a dlopen is done that will change the entry point
736 SecPeCoffRelocateImageExtraAction (&ImageContext
);
737 *EntryPoint
= (VOID
*)(UINTN
)ImageContext
.EntryPoint
;
747 Return the FD Size and base address. Since the FD is loaded from a
748 file into host memory only the SEC will know it's address.
751 Index - Which FD, starts at zero.
752 FdSize - Size of the FD in bytes
753 FdBase - Start address of the FD. Assume it points to an FV Header
754 FixUp - Difference between actual FD address and build address
757 EFI_SUCCESS - Return the Base address and size of the FV
758 EFI_UNSUPPORTED - Index does nto map to an FD in the system
764 IN OUT EFI_PHYSICAL_ADDRESS
*FdBase
,
765 IN OUT UINT64
*FdSize
,
766 IN OUT EFI_PHYSICAL_ADDRESS
*FixUp
769 if (Index
>= gFdInfoCount
) {
770 return EFI_UNSUPPORTED
;
773 *FdBase
= gFdInfo
[Index
].Address
;
774 *FdSize
= gFdInfo
[Index
].Size
;
777 if (*FdBase
== 0 && *FdSize
== 0) {
778 return EFI_UNSUPPORTED
;
783 // FD 0 has XIP code and well known PCD values
784 // If the memory buffer could not be allocated at the FD build address
785 // the Fixup is the difference.
787 *FixUp
= *FdBase
- PcdGet64 (PcdEmuFdBaseAddress
);
797 Count the number of seperators in String
800 String - String to process
801 Seperator - Item to count
804 Number of Seperator in String
808 CountSeperatorsInString (
809 IN
const CHAR16
*String
,
815 for (Count
= 0; *String
!= '\0'; String
++) {
816 if (*String
== Seperator
) {
830 IN OUT UINTN
*ReadSize
,
836 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
839 FileHandle - The handle to the PE/COFF file
840 FileOffset - The offset, in bytes, into the file to read
841 ReadSize - The number of bytes to read from the file starting at FileOffset
842 Buffer - A pointer to the buffer to read the data into.
845 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
853 Destination8
= Buffer
;
854 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
857 *(Destination8
++) = *(Source8
++);
867 Store the ModHandle in an array indexed by the Pdb File name.
868 The ModHandle is needed to unload the image.
871 ImageContext - Input data returned from PE Laoder Library. Used to find the
872 .PDB file name of the PE Image.
873 ModHandle - Returned from LoadLibraryEx() and stored for call to
877 EFI_SUCCESS - ModHandle was stored.
882 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
887 IMAGE_CONTEXT_TO_MOD_HANDLE
*Array
;
891 Array
= mImageContextModHandleArray
;
892 for (Index
= 0; Index
< mImageContextModHandleArraySize
; Index
++, Array
++) {
893 if (Array
->ImageContext
== NULL
) {
895 // Make a copy of the stirng and store the ModHandle
897 Array
->ImageContext
= ImageContext
;
898 Array
->ModHandle
= ModHandle
;
904 // No free space in mImageContextModHandleArray so grow it by
905 // IMAGE_CONTEXT_TO_MOD_HANDLE entires. realloc will
906 // copy the old values to the new locaiton. But it does
907 // not zero the new memory area.
909 PreviousSize
= mImageContextModHandleArraySize
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
);
910 mImageContextModHandleArraySize
+= MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE
;
912 mImageContextModHandleArray
= ReallocatePool (
913 (mImageContextModHandleArraySize
- 1) * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
),
914 mImageContextModHandleArraySize
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
),
915 mImageContextModHandleArray
917 if (mImageContextModHandleArray
== NULL
) {
919 return EFI_OUT_OF_RESOURCES
;
922 memset (mImageContextModHandleArray
+ PreviousSize
, 0, MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
));
924 return AddHandle (ImageContext
, ModHandle
);
931 Return the ModHandle and delete the entry in the array.
934 ImageContext - Input data returned from PE Laoder Library. Used to find the
935 .PDB file name of the PE Image.
938 ModHandle - ModHandle assoicated with ImageContext is returned
939 NULL - No ModHandle associated with ImageContext
944 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
948 IMAGE_CONTEXT_TO_MOD_HANDLE
*Array
;
950 if (ImageContext
->PdbPointer
== NULL
) {
952 // If no PDB pointer there is no ModHandle so return NULL
957 Array
= mImageContextModHandleArray
;
958 for (Index
= 0; Index
< mImageContextModHandleArraySize
; Index
++, Array
++) {
959 if (Array
->ImageContext
== ImageContext
) {
961 // If you find a match return it and delete the entry
963 Array
->ImageContext
= NULL
;
964 return Array
->ModHandle
;
974 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to source a
975 // add-symbol-file command. Hey what can you say scripting in gdb is not that great....
977 // Put .gdbinit in the CWD where you do gdb SecMain.dll for source level debug
980 // b SecGdbScriptBreak
983 // source SecMain.gdb
1003 IN CHAR8
*PdbFileName
1008 if (PdbFileName
== NULL
) {
1012 Len
= strlen (PdbFileName
);
1013 if ((Len
< 5)|| (PdbFileName
[Len
- 4] != '.')) {
1017 if ((PdbFileName
[Len
- 3] == 'P' || PdbFileName
[Len
- 3] == 'p') &&
1018 (PdbFileName
[Len
- 2] == 'D' || PdbFileName
[Len
- 2] == 'd') &&
1019 (PdbFileName
[Len
- 1] == 'B' || PdbFileName
[Len
- 1] == 'b')) {
1027 #define MAX_SPRINT_BUFFER_SIZE 0x200
1031 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1034 if (ImageContext
->PdbPointer
== NULL
) {
1036 "0x%08lx Loading NO DEBUG with entry point 0x%08lx\n",
1037 (unsigned long)(ImageContext
->ImageAddress
),
1038 (unsigned long)ImageContext
->EntryPoint
1042 "0x%08lx Loading %s with entry point 0x%08lx\n",
1043 (unsigned long)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
),
1044 ImageContext
->PdbPointer
,
1045 (unsigned long)ImageContext
->EntryPoint
1048 // Keep output synced up
1055 SecPeCoffRelocateImageExtraAction (
1056 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1061 BOOLEAN EnabledOnEntry
;
1064 // Make sure writting of the file is an atomic operation
1066 if (SecInterruptEanbled ()) {
1067 SecDisableInterrupt ();
1068 EnabledOnEntry
= TRUE
;
1070 EnabledOnEntry
= FALSE
;
1073 PrintLoadAddress (ImageContext
);
1076 // In mach-o (OS X executable) dlopen() can only load files in the MH_DYLIB of MH_BUNDLE format.
1077 // To convert to PE/COFF we need to construct a mach-o with the MH_PRELOAD format. We create
1078 // .dSYM files for the PE/COFF images that can be used by gdb for source level debugging.
1083 // In the Mach-O to PE/COFF conversion the size of the PE/COFF headers is not accounted for.
1084 // Thus we need to skip over the PE/COFF header when giving load addresses for our symbol table.
1086 if (ImageContext
->PdbPointer
!= NULL
&& !IsPdbFile (ImageContext
->PdbPointer
)) {
1088 // Now we have a database of the images that are currently loaded
1092 // 'symbol-file' will clear out currnet symbol mappings in gdb.
1093 // you can do a 'add-symbol-file filename address' for every image we loaded to get source
1094 // level debug in gdb. Note Sec, being a true application will work differently.
1096 // We add the PE/COFF header size into the image as the mach-O does not have a header in
1097 // loaded into system memory.
1099 // This gives us a data base of gdb commands and after something is unloaded that entry will be
1100 // removed. We don't yet have the scheme of how to comunicate with gdb, but we have the
1101 // data base of info ready to roll.
1103 // We could use qXfer:libraries:read, but OS X GDB does not currently support it.
1105 // <library name="/lib/libc.so.6"> // ImageContext->PdbPointer
1106 // <segment address="0x10000000"/> // ImageContext->ImageAddress + ImageContext->SizeOfHeaders
1112 // Write the file we need for the gdb script
1114 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
1115 if (GdbTempFile
!= NULL
) {
1116 fprintf (GdbTempFile
, "add-symbol-file %s 0x%08lx\n", ImageContext
->PdbPointer
, (long unsigned int)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
));
1117 fclose (GdbTempFile
);
1120 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
1121 // Hey what can you say scripting in gdb is not that great....
1123 SecGdbScriptBreak ();
1128 AddHandle (ImageContext
, ImageContext
->PdbPointer
);
1130 if (EnabledOnEntry
) {
1131 SecEnableInterrupt ();
1139 void *Handle
= NULL
;
1142 if (ImageContext
->PdbPointer
== NULL
) {
1146 if (!IsPdbFile (ImageContext
->PdbPointer
)) {
1152 "Loading %s 0x%08lx - entry point 0x%08lx\n",
1153 ImageContext
->PdbPointer
,
1154 (unsigned long)ImageContext
->ImageAddress
,
1155 (unsigned long)ImageContext
->EntryPoint
1158 Handle
= dlopen (ImageContext
->PdbPointer
, RTLD_NOW
);
1161 Entry
= dlsym (Handle
, "_ModuleEntryPoint");
1163 printf("%s\n", dlerror());
1166 if (Entry
!= NULL
) {
1167 ImageContext
->EntryPoint
= (UINTN
)Entry
;
1168 printf ("Change %s Entrypoint to :0x%08lx\n", ImageContext
->PdbPointer
, (unsigned long)Entry
);
1171 SecUnixLoaderBreak ();
1181 SecPeCoffUnloadImageExtraAction (
1182 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1187 Handle
= RemoveHandle (ImageContext
);
1191 BOOLEAN EnabledOnEntry
;
1193 if (Handle
!= NULL
) {
1195 // Need to skip .PDB files created from VC++
1197 if (!IsPdbFile (ImageContext
->PdbPointer
)) {
1198 if (SecInterruptEanbled ()) {
1199 SecDisableInterrupt ();
1200 EnabledOnEntry
= TRUE
;
1202 EnabledOnEntry
= FALSE
;
1206 // Write the file we need for the gdb script
1208 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
1209 if (GdbTempFile
!= NULL
) {
1210 fprintf (GdbTempFile
, "remove-symbol-file %s\n", ImageContext
->PdbPointer
);
1211 fclose (GdbTempFile
);
1214 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
1215 // Hey what can you say scripting in gdb is not that great....
1217 SecGdbScriptBreak ();
1222 if (EnabledOnEntry
) {
1223 SecEnableInterrupt ();
1230 // Don't want to confuse gdb with symbols for something that got unloaded
1232 if (Handle
!= NULL
) {