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
26 EMU_THUNK_PPI mSecEmuThunkPpi
= {
27 GasketSecUnixPeiAutoScan
,
28 GasketSecUnixFdAddress
,
29 GasketSecEmuThunkAddress
32 char *gGdbWorkingFileName
= NULL
;
33 unsigned int mScriptSymbolChangesCount
= 0;
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
;
67 Breakpoint target for Xcode project. Set in the Xcode XML
69 Xcode breakpoint will 'source Host.gdb'
70 gGdbWorkingFileName is set to Host.gdb
85 Main entry point to SEC for Unix. This is a unix program
88 Argc - Number of command line arguments
89 Argv - Array of command line argument strings
90 Envp - Array of environmemt variable strings
105 EFI_PHYSICAL_ADDRESS InitialStackMemory
;
106 UINT64 InitialStackMemorySize
;
113 EFI_PEI_FILE_HANDLE FileHandle
;
115 CHAR16
*MemorySizeStr
;
116 CHAR16
*FirmwareVolumesStr
;
121 // Xcode does not support sourcing gdb scripts directly, so the Xcode XML
122 // has a break point script to source the GdbRun script.
124 SecGdbConfigBreak ();
127 // If dlopen doesn't work, then we build a gdb script to allow the
128 // symbols to be loaded.
130 Index
= strlen (*Argv
);
131 gGdbWorkingFileName
= AllocatePool (Index
+ strlen(".gdb") + 1);
132 strcpy (gGdbWorkingFileName
, *Argv
);
133 strcat (gGdbWorkingFileName
, ".gdb");
136 // Empty out the gdb symbols script file.
138 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
139 if (GdbTempFile
!= NULL
) {
140 fclose (GdbTempFile
);
143 printf ("\nEDK II UNIX Host Emulation Environment from edk2.sourceforge.net\n");
148 MemorySizeStr
= (CHAR16
*) PcdGetPtr (PcdEmuMemorySize
);
149 FirmwareVolumesStr
= (CHAR16
*) PcdGetPtr (PcdEmuFirmwareVolume
);
152 // PPIs pased into PEI_CORE
154 AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI
, &gEmuThunkPpiGuid
, &mSecEmuThunkPpi
);
156 SecInitThunkProtocol ();
159 // Emulator Bus Driver Thunks
161 AddThunkProtocol (&gX11ThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuGop
), TRUE
);
162 AddThunkProtocol (&gPosixFileSystemThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuFileSystem
), TRUE
);
163 AddThunkProtocol (&gBlockIoThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuVirtualDisk
), TRUE
);
164 AddThunkProtocol (&gSnpThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuNetworkInterface
), TRUE
);
167 // Emulator other Thunks
169 AddThunkProtocol (&gPthreadThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuApCount
), FALSE
);
171 // EmuSecLibConstructor ();
173 gPpiList
= GetThunkPpiList ();
176 // Allocate space for gSystemMemory Array
178 gSystemMemoryCount
= CountSeperatorsInString (MemorySizeStr
, '!') + 1;
179 gSystemMemory
= AllocateZeroPool (gSystemMemoryCount
* sizeof (EMU_SYSTEM_MEMORY
));
180 if (gSystemMemory
== NULL
) {
181 printf ("ERROR : Can not allocate memory for system. Exiting.\n");
185 // Allocate space for gSystemMemory Array
187 gFdInfoCount
= CountSeperatorsInString (FirmwareVolumesStr
, '!') + 1;
188 gFdInfo
= AllocateZeroPool (gFdInfoCount
* sizeof (EMU_FD_INFO
));
189 if (gFdInfo
== NULL
) {
190 printf ("ERROR : Can not allocate memory for fd info. Exiting.\n");
194 printf (" BootMode 0x%02x\n", (unsigned int)PcdGet32 (PcdEmuBootMode
));
197 // Open up a 128K file to emulate temp memory for SEC.
198 // on a real platform this would be SRAM, or using the cache as RAM.
199 // Set InitialStackMemory to zero so UnixOpenFile will allocate a new mapping
201 InitialStackMemorySize
= STACK_SIZE
;
202 InitialStackMemory
= (UINTN
)MapMemory (
203 0, (UINT32
) InitialStackMemorySize
,
204 PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_ANONYMOUS
| MAP_PRIVATE
206 if (InitialStackMemory
== 0) {
207 printf ("ERROR : Can not open SecStack Exiting\n");
211 printf (" OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n",
212 (unsigned int)(InitialStackMemorySize
/ 1024),
213 (unsigned long)InitialStackMemory
216 for (StackPointer
= (UINTN
*) (UINTN
) InitialStackMemory
;
217 StackPointer
< (UINTN
*)(UINTN
)((UINTN
) InitialStackMemory
+ (UINT64
) InitialStackMemorySize
);
219 *StackPointer
= 0x5AA55AA5;
223 // Open All the firmware volumes and remember the info in the gFdInfo global
225 FileName
= (CHAR8
*) AllocatePool (StrLen (FirmwareVolumesStr
) + 1);
226 if (FileName
== NULL
) {
227 printf ("ERROR : Can not allocate memory for firmware volume string\n");
232 for (Done
= FALSE
, Index
= 0, PeiIndex
= 0, SecFile
= NULL
;
233 FirmwareVolumesStr
[Index2
] != 0;
235 for (Index1
= 0; (FirmwareVolumesStr
[Index2
] != '!') && (FirmwareVolumesStr
[Index2
] != 0); Index2
++) {
236 FileName
[Index1
++] = FirmwareVolumesStr
[Index2
];
238 if (FirmwareVolumesStr
[Index2
] == '!') {
241 FileName
[Index1
] = '\0';
244 // Map FV Recovery Read Only and other areas Read/Write
252 // Open the FD and remmeber where it got mapped into our processes address space
257 &gFdInfo
[Index
].Address
,
261 if (EFI_ERROR (Status
)) {
262 printf ("ERROR : Can not open Firmware Device File %s (%x). Exiting.\n", FileName
, (unsigned int)Status
);
266 printf (" FD loaded from %s at 0x%08lx",FileName
, (unsigned long)gFdInfo
[Index
].Address
);
268 if (SecFile
== NULL
) {
270 // Assume the beginning of the FD is an FV and look for the SEC Core.
271 // Load the first one we find.
274 Status
= PeiServicesFfsFindNextFile (
275 EFI_FV_FILETYPE_SECURITY_CORE
,
276 (EFI_PEI_FV_HANDLE
)(UINTN
)gFdInfo
[Index
].Address
,
279 if (!EFI_ERROR (Status
)) {
280 Status
= PeiServicesFfsFindSectionData (EFI_SECTION_PE32
, FileHandle
, &SecFile
);
281 if (!EFI_ERROR (Status
)) {
283 printf (" contains SEC Core");
291 // Calculate memory regions and store the information in the gSystemMemory
292 // global for later use. The autosizing code will use this data to
293 // map this memory into the SEC process memory space.
300 // Save the size of the memory.
302 while (MemorySizeStr
[Index1
] >= '0' && MemorySizeStr
[Index1
] <= '9') {
303 val
= val
* 10 + MemorySizeStr
[Index1
] - '0';
306 gSystemMemory
[Index
++].Size
= val
* 0x100000;
307 if (MemorySizeStr
[Index1
] == 0) {
318 SecLoadFromCore ((UINTN
) InitialStackMemory
, (UINTN
) InitialStackMemorySize
, (UINTN
) gFdInfo
[0].Address
, SecFile
);
321 // If we get here, then the SEC Core returned. This is an error as SEC should
322 // always hand off to PEI Core and then on to DXE Core.
324 printf ("ERROR : SEC returned\n");
329 EFI_PHYSICAL_ADDRESS
*
337 STATIC UINTN base
= 0x40000000;
338 CONST UINTN align
= (1 << 24);
340 BOOLEAN isAligned
= 0;
343 // Try to get an aligned block somewhere in the address space of this
346 while((!isAligned
) && (base
!= 0)) {
347 res
= mmap ((void *)base
, length
, prot
, flags
, fd
, 0);
348 if (res
== MAP_FAILED
) {
351 if ((((UINTN
)res
) & ~(align
-1)) == (UINTN
)res
) {
365 Opens and memory maps a file using Unix services. If BaseAddress is non zero
366 the process will try and allocate the memory starting at BaseAddress.
369 FileName - The name of the file to open and map
370 MapSize - The amount of the file to map in bytes
371 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for
372 memory emulation, and exiting files for firmware volume emulation
373 BaseAddress - The base address of the mapped file in the user address space.
374 If passed in as NULL the a new memory region is used.
375 If passed in as non NULL the request memory region is used for
376 the mapping of the file into the process space.
377 Length - The size of the mapped region in bytes
380 EFI_SUCCESS - The file was opened and mapped.
381 EFI_NOT_FOUND - FileName was not found in the current directory
382 EFI_DEVICE_ERROR - An error occured attempting to map the opened file
388 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
396 fd
= open (FileName
, O_RDWR
);
398 return EFI_NOT_FOUND
;
400 FileSize
= lseek (fd
, 0, SEEK_END
);
403 res
= MapMemory (fd
, FileSize
, PROT_READ
| PROT_EXEC
, MAP_PRIVATE
);
408 perror ("MapFile() Failed");
409 return EFI_DEVICE_ERROR
;
412 *Length
= (UINT64
) FileSize
;
413 *BaseAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) res
;
421 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
426 void *res
, *res2
, *res3
;
431 fd
= open (FileName
, O_RDWR
);
433 return EFI_NOT_FOUND
;
435 FileSize
= lseek (fd
, 0, SEEK_END
);
437 FvSize
= FixedPcdGet64 (PcdEmuFlashFvRecoverySize
);
439 // Assume start of FD is Recovery FV, and make it write protected
441 (void *)(UINTN
)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase
),
443 PROT_READ
| PROT_EXEC
,
448 if (res
== MAP_FAILED
) {
449 perror ("MapFd0() Failed res =");
451 return EFI_DEVICE_ERROR
;
452 } else if (res
!= (void *)(UINTN
)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase
)) {
453 // We could not load at the build address, so we need to allow writes
454 munmap (res
, FvSize
);
456 (void *)(UINTN
)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase
),
458 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
463 if (res
== MAP_FAILED
) {
464 perror ("MapFd0() Failed res =");
466 return EFI_DEVICE_ERROR
;
470 // Map the rest of the FD as read/write
472 (void *)(UINTN
)(FixedPcdGet64 (PcdEmuFlashFvRecoveryBase
) + FvSize
),
474 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
480 if (res2
== MAP_FAILED
) {
481 perror ("MapFd0() Failed res2 =");
482 return EFI_DEVICE_ERROR
;
486 // If enabled use the magic page to communicate between modules
487 // This replaces the PI PeiServicesTable pointer mechanism that
488 // deos not work in the emulator. It also allows the removal of
489 // writable globals from SEC, PEI_CORE (libraries), PEIMs
491 EmuMagicPage
= (void *)(UINTN
)FixedPcdGet64 (PcdPeiServicesTablePage
);
492 if (EmuMagicPage
!= NULL
) {
494 (void *)EmuMagicPage
,
496 PROT_READ
| PROT_WRITE
,
497 MAP_PRIVATE
| MAP_ANONYMOUS
,
501 if (res3
!= EmuMagicPage
) {
502 printf ("MapFd0(): Could not allocate PeiServicesTablePage @ %lx\n", (long unsigned int)EmuMagicPage
);
503 return EFI_DEVICE_ERROR
;
507 *Length
= (UINT64
) FileSize
;
508 *BaseAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) res
;
517 This is the service to load the SEC Core from the Firmware Volume
520 LargestRegion - Memory to use for SEC.
521 LargestRegionSize - Size of Memory to use for PEI
522 BootFirmwareVolumeBase - Start of the Boot FV
523 PeiCorePe32File - SEC PE32
526 Success means control is transfered and thus we should never return
531 IN UINTN LargestRegion
,
532 IN UINTN LargestRegionSize
,
533 IN UINTN BootFirmwareVolumeBase
,
534 IN VOID
*PeiCorePe32File
538 EFI_PHYSICAL_ADDRESS TopOfMemory
;
540 EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint
;
541 EFI_SEC_PEI_HAND_OFF
*SecCoreData
;
545 // Compute Top Of Memory for Stack and PEI Core Allocations
547 TopOfMemory
= LargestRegion
+ LargestRegionSize
;
548 PeiStackSize
= (UINTN
)RShiftU64((UINT64
)STACK_SIZE
,1);
551 // |-----------| <---- TemporaryRamBase + TemporaryRamSize
554 // |-----------| <---- StackBase / PeiTemporaryMemoryBase
557 // |-----------| <---- TemporaryRamBase
559 TopOfStack
= (VOID
*)(LargestRegion
+ PeiStackSize
);
560 TopOfMemory
= LargestRegion
+ PeiStackSize
;
563 // Reservet space for storing PeiCore's parament in stack.
565 TopOfStack
= (VOID
*)((UINTN
)TopOfStack
- sizeof (EFI_SEC_PEI_HAND_OFF
) - CPU_STACK_ALIGNMENT
);
566 TopOfStack
= ALIGN_POINTER (TopOfStack
, CPU_STACK_ALIGNMENT
);
570 // Bind this information into the SEC hand-off state
572 SecCoreData
= (EFI_SEC_PEI_HAND_OFF
*)(UINTN
) TopOfStack
;
573 SecCoreData
->DataSize
= sizeof(EFI_SEC_PEI_HAND_OFF
);
574 SecCoreData
->BootFirmwareVolumeBase
= (VOID
*)BootFirmwareVolumeBase
;
575 SecCoreData
->BootFirmwareVolumeSize
= PcdGet32 (PcdEmuFirmwareFdSize
);
576 SecCoreData
->TemporaryRamBase
= (VOID
*)(UINTN
)LargestRegion
;
577 SecCoreData
->TemporaryRamSize
= STACK_SIZE
;
578 SecCoreData
->StackBase
= SecCoreData
->TemporaryRamBase
;
579 SecCoreData
->StackSize
= PeiStackSize
;
580 SecCoreData
->PeiTemporaryRamBase
= (VOID
*) ((UINTN
) SecCoreData
->TemporaryRamBase
+ PeiStackSize
);
581 SecCoreData
->PeiTemporaryRamSize
= STACK_SIZE
- PeiStackSize
;
584 // Find the SEC Core Entry Point
586 Status
= SecPeCoffGetEntryPoint (PeiCorePe32File
, (VOID
**)&PeiCoreEntryPoint
);
587 if (EFI_ERROR (Status
)) {
592 // Transfer control to the SEC Core
595 (SWITCH_STACK_ENTRY_POINT
) (UINTN
) PeiCoreEntryPoint
,
601 // If we get here, then the SEC Core returned. This is an error
610 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.
611 It allows discontiguous memory regions to be supported by the emulator.
612 It uses gSystemMemory[] and gSystemMemoryCount that were created by
613 parsing the host environment variable EFI_MEMORY_SIZE.
614 The size comes from the varaible and the address comes from the call to
618 Index - Which memory region to use
619 MemoryBase - Return Base address of memory region
620 MemorySize - Return size in bytes of the memory region
623 EFI_SUCCESS - If memory region was mapped
624 EFI_UNSUPPORTED - If Index is not supported
630 OUT EFI_PHYSICAL_ADDRESS
*MemoryBase
,
631 OUT UINT64
*MemorySize
636 if (Index
>= gSystemMemoryCount
) {
637 return EFI_UNSUPPORTED
;
642 0, gSystemMemory
[Index
].Size
,
643 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
644 MAP_PRIVATE
| MAP_ANONYMOUS
646 if (res
== MAP_FAILED
) {
647 return EFI_DEVICE_ERROR
;
649 *MemorySize
= gSystemMemory
[Index
].Size
;
650 *MemoryBase
= (UINTN
)res
;
651 gSystemMemory
[Index
].Memory
= *MemoryBase
;
660 Check to see if an address range is in the EFI GCD memory map.
662 This is all of GCD for system memory passed to DXE Core. FV
663 mapping and other device mapped into system memory are not
664 inlcuded in the check.
667 Index - Which memory region to use
668 MemoryBase - Return Base address of memory region
669 MemorySize - Return size in bytes of the memory region
672 TRUE - Address is in the EFI GCD memory map
673 FALSE - Address is NOT in memory map
677 EfiSystemMemoryRange (
678 IN VOID
*MemoryAddress
682 EFI_PHYSICAL_ADDRESS MemoryBase
;
684 MemoryBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)MemoryAddress
;
685 for (Index
= 0; Index
< gSystemMemoryCount
; Index
++) {
686 if ((MemoryBase
>= gSystemMemory
[Index
].Memory
) &&
687 (MemoryBase
< (gSystemMemory
[Index
].Memory
+ gSystemMemory
[Index
].Size
)) ) {
699 Since the SEC is the only Unix program in stack it must export
700 an interface to do POSIX calls. gUnix is initailized in UnixThunk.c.
703 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);
704 InterfaceBase - Address of the gUnix global
707 EFI_SUCCESS - Data returned
715 return &gEmuThunkProtocol
;
722 SecPeCoffGetEntryPoint (
724 IN OUT VOID
**EntryPoint
728 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
730 ZeroMem (&ImageContext
, sizeof (ImageContext
));
731 ImageContext
.Handle
= Pe32Data
;
732 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) SecImageRead
;
734 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
735 if (EFI_ERROR (Status
)) {
739 if (ImageContext
.ImageAddress
!= (UINTN
)Pe32Data
) {
741 // Relocate image to match the address where it resides
743 ImageContext
.ImageAddress
= (UINTN
)Pe32Data
;
744 Status
= PeCoffLoaderLoadImage (&ImageContext
);
745 if (EFI_ERROR (Status
)) {
749 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
750 if (EFI_ERROR (Status
)) {
755 // Or just return image entry point
757 ImageContext
.PdbPointer
= PeCoffLoaderGetPdbPointer (Pe32Data
);
758 Status
= PeCoffLoaderGetEntryPoint (Pe32Data
, EntryPoint
);
759 if (EFI_ERROR (Status
)) {
762 ImageContext
.EntryPoint
= (UINTN
)*EntryPoint
;
765 // On Unix a dlopen is done that will change the entry point
766 SecPeCoffRelocateImageExtraAction (&ImageContext
);
767 *EntryPoint
= (VOID
*)(UINTN
)ImageContext
.EntryPoint
;
777 Return the FD Size and base address. Since the FD is loaded from a
778 file into host memory only the SEC will know it's address.
781 Index - Which FD, starts at zero.
782 FdSize - Size of the FD in bytes
783 FdBase - Start address of the FD. Assume it points to an FV Header
784 FixUp - Difference between actual FD address and build address
787 EFI_SUCCESS - Return the Base address and size of the FV
788 EFI_UNSUPPORTED - Index does nto map to an FD in the system
794 IN OUT EFI_PHYSICAL_ADDRESS
*FdBase
,
795 IN OUT UINT64
*FdSize
,
796 IN OUT EFI_PHYSICAL_ADDRESS
*FixUp
799 if (Index
>= gFdInfoCount
) {
800 return EFI_UNSUPPORTED
;
803 *FdBase
= gFdInfo
[Index
].Address
;
804 *FdSize
= gFdInfo
[Index
].Size
;
807 if (*FdBase
== 0 && *FdSize
== 0) {
808 return EFI_UNSUPPORTED
;
813 // FD 0 has XIP code and well known PCD values
814 // If the memory buffer could not be allocated at the FD build address
815 // the Fixup is the difference.
817 *FixUp
= *FdBase
- PcdGet64 (PcdEmuFdBaseAddress
);
827 Count the number of seperators in String
830 String - String to process
831 Seperator - Item to count
834 Number of Seperator in String
838 CountSeperatorsInString (
839 IN
const CHAR16
*String
,
845 for (Count
= 0; *String
!= '\0'; String
++) {
846 if (*String
== Seperator
) {
860 IN OUT UINTN
*ReadSize
,
866 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
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.
875 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
883 Destination8
= Buffer
;
884 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
887 *(Destination8
++) = *(Source8
++);
897 Store the ModHandle in an array indexed by the Pdb File name.
898 The ModHandle is needed to unload the image.
901 ImageContext - Input data returned from PE Laoder Library. Used to find the
902 .PDB file name of the PE Image.
903 ModHandle - Returned from LoadLibraryEx() and stored for call to
907 EFI_SUCCESS - ModHandle was stored.
912 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
917 IMAGE_CONTEXT_TO_MOD_HANDLE
*Array
;
921 Array
= mImageContextModHandleArray
;
922 for (Index
= 0; Index
< mImageContextModHandleArraySize
; Index
++, Array
++) {
923 if (Array
->ImageContext
== NULL
) {
925 // Make a copy of the stirng and store the ModHandle
927 Array
->ImageContext
= ImageContext
;
928 Array
->ModHandle
= ModHandle
;
934 // No free space in mImageContextModHandleArray so grow it by
935 // IMAGE_CONTEXT_TO_MOD_HANDLE entires. realloc will
936 // copy the old values to the new locaiton. But it does
937 // not zero the new memory area.
939 PreviousSize
= mImageContextModHandleArraySize
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
);
940 mImageContextModHandleArraySize
+= MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE
;
942 mImageContextModHandleArray
= ReallocatePool (
943 (mImageContextModHandleArraySize
- 1) * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
),
944 mImageContextModHandleArraySize
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
),
945 mImageContextModHandleArray
947 if (mImageContextModHandleArray
== NULL
) {
949 return EFI_OUT_OF_RESOURCES
;
952 memset (mImageContextModHandleArray
+ PreviousSize
, 0, MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
));
954 return AddHandle (ImageContext
, ModHandle
);
961 Return the ModHandle and delete the entry in the array.
964 ImageContext - Input data returned from PE Laoder Library. Used to find the
965 .PDB file name of the PE Image.
968 ModHandle - ModHandle assoicated with ImageContext is returned
969 NULL - No ModHandle associated with ImageContext
974 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
978 IMAGE_CONTEXT_TO_MOD_HANDLE
*Array
;
980 if (ImageContext
->PdbPointer
== NULL
) {
982 // If no PDB pointer there is no ModHandle so return NULL
987 Array
= mImageContextModHandleArray
;
988 for (Index
= 0; Index
< mImageContextModHandleArraySize
; Index
++, Array
++) {
989 if (Array
->ImageContext
== ImageContext
) {
991 // If you find a match return it and delete the entry
993 Array
->ImageContext
= NULL
;
994 return Array
->ModHandle
;
1005 IN CHAR8
*PdbFileName
1010 if (PdbFileName
== NULL
) {
1014 Len
= strlen (PdbFileName
);
1015 if ((Len
< 5)|| (PdbFileName
[Len
- 4] != '.')) {
1019 if ((PdbFileName
[Len
- 3] == 'P' || PdbFileName
[Len
- 3] == 'p') &&
1020 (PdbFileName
[Len
- 2] == 'D' || PdbFileName
[Len
- 2] == 'd') &&
1021 (PdbFileName
[Len
- 1] == 'B' || PdbFileName
[Len
- 1] == 'b')) {
1029 #define MAX_SPRINT_BUFFER_SIZE 0x200
1033 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1036 if (ImageContext
->PdbPointer
== NULL
) {
1038 "0x%08lx Loading NO DEBUG with entry point 0x%08lx\n",
1039 (unsigned long)(ImageContext
->ImageAddress
),
1040 (unsigned long)ImageContext
->EntryPoint
1044 "0x%08lx Loading %s with entry point 0x%08lx\n",
1045 (unsigned long)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
),
1046 ImageContext
->PdbPointer
,
1047 (unsigned long)ImageContext
->EntryPoint
1050 // Keep output synced up
1056 Loads the image using dlopen so symbols will be automatically
1059 @param ImageContext The PE/COFF image context
1061 @retval TRUE - The image was successfully loaded
1062 @retval FALSE - The image was successfully loaded
1067 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1077 void *Handle
= NULL
;
1080 if (ImageContext
->PdbPointer
== NULL
) {
1084 if (!IsPdbFile (ImageContext
->PdbPointer
)) {
1090 "Loading %s 0x%08lx - entry point 0x%08lx\n",
1091 ImageContext
->PdbPointer
,
1092 (unsigned long)ImageContext
->ImageAddress
,
1093 (unsigned long)ImageContext
->EntryPoint
1096 Handle
= dlopen (ImageContext
->PdbPointer
, RTLD_NOW
);
1097 if (Handle
!= NULL
) {
1098 Entry
= dlsym (Handle
, "_ModuleEntryPoint");
1099 AddHandle (ImageContext
, Handle
);
1101 printf("%s\n", dlerror());
1104 if (Entry
!= NULL
) {
1105 ImageContext
->EntryPoint
= (UINTN
)Entry
;
1106 printf ("Change %s Entrypoint to :0x%08lx\n", ImageContext
->PdbPointer
, (unsigned long)Entry
);
1120 long unsigned int LoadAddress
,
1129 Adds the image to a gdb script so it's symbols can be loaded.
1130 The AddFirmwareSymbolFile helper macro is used.
1132 @param ImageContext The PE/COFF image context
1137 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1141 PrintLoadAddress (ImageContext
);
1143 if (ImageContext
->PdbPointer
!= NULL
&& !IsPdbFile (ImageContext
->PdbPointer
)) {
1145 if (FeaturePcdGet (PcdEmulatorLazyLoadSymbols
)) {
1146 GdbTempFile
= fopen (gGdbWorkingFileName
, "a");
1147 if (GdbTempFile
!= NULL
) {
1148 long unsigned int SymbolsAddr
= (long unsigned int)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
);
1149 mScriptSymbolChangesCount
++;
1152 "AddFirmwareSymbolFile 0x%x %s 0x%08lx\n",
1153 mScriptSymbolChangesCount
,
1154 ImageContext
->PdbPointer
,
1157 fclose (GdbTempFile
);
1162 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
1163 if (GdbTempFile
!= NULL
) {
1166 "add-symbol-file %s 0x%08lx\n",
1167 ImageContext
->PdbPointer
,
1168 (long unsigned int)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
)
1170 fclose (GdbTempFile
);
1173 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
1174 // Hey what can you say scripting in gdb is not that great....
1176 SecGdbScriptBreak (ImageContext
->PdbPointer
, strlen (ImageContext
->PdbPointer
), (long unsigned int)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
), 1);
1187 SecPeCoffRelocateImageExtraAction (
1188 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1191 if (!DlLoadImage (ImageContext
)) {
1192 GdbScriptAddImage (ImageContext
);
1198 Adds the image to a gdb script so it's symbols can be unloaded.
1199 The RemoveFirmwareSymbolFile helper macro is used.
1201 @param ImageContext The PE/COFF image context
1205 GdbScriptRemoveImage (
1206 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1212 // Need to skip .PDB files created from VC++
1214 if (IsPdbFile (ImageContext
->PdbPointer
)) {
1218 if (FeaturePcdGet (PcdEmulatorLazyLoadSymbols
)) {
1220 // Write the file we need for the gdb script
1222 GdbTempFile
= fopen (gGdbWorkingFileName
, "a");
1223 if (GdbTempFile
!= NULL
) {
1224 mScriptSymbolChangesCount
++;
1227 "RemoveFirmwareSymbolFile 0x%x %s\n",
1228 mScriptSymbolChangesCount
,
1229 ImageContext
->PdbPointer
1231 fclose (GdbTempFile
);
1236 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
1237 if (GdbTempFile
!= NULL
) {
1238 fprintf (GdbTempFile
, "remove-symbol-file %s\n", ImageContext
->PdbPointer
);
1239 fclose (GdbTempFile
);
1242 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
1243 // Hey what can you say scripting in gdb is not that great....
1245 SecGdbScriptBreak (ImageContext
->PdbPointer
, strlen (ImageContext
->PdbPointer
), 0, 0);
1255 SecPeCoffUnloadImageExtraAction (
1256 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1262 // Check to see if the image symbols were loaded with gdb script, or dlopen
1264 Handle
= RemoveHandle (ImageContext
);
1265 if (Handle
!= NULL
) {
1272 GdbScriptRemoveImage (ImageContext
);