3 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
4 Portions copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #define MAP_ANONYMOUS MAP_ANON
20 EMU_THUNK_PPI mSecEmuThunkPpi
= {
21 GasketSecUnixPeiAutoScan
,
22 GasketSecUnixFdAddress
,
23 GasketSecEmuThunkAddress
26 char *gGdbWorkingFileName
= NULL
;
27 unsigned int mScriptSymbolChangesCount
= 0;
31 // Default information about where the FD is located.
32 // This array gets filled in with information from EFI_FIRMWARE_VOLUMES
33 // EFI_FIRMWARE_VOLUMES is a host environment variable set by system.cmd.
34 // The number of array elements is allocated base on parsing
35 // EFI_FIRMWARE_VOLUMES and the memory is never freed.
37 UINTN gFdInfoCount
= 0;
41 // Array that supports seperate memory rantes.
42 // The memory ranges are set in system.cmd via the EFI_MEMORY_SIZE variable.
43 // The number of array elements is allocated base on parsing
44 // EFI_MEMORY_SIZE and the memory is never freed.
46 UINTN gSystemMemoryCount
= 0;
47 EMU_SYSTEM_MEMORY
*gSystemMemory
;
51 UINTN mImageContextModHandleArraySize
= 0;
52 IMAGE_CONTEXT_TO_MOD_HANDLE
*mImageContextModHandleArray
= NULL
;
54 EFI_PEI_PPI_DESCRIPTOR
*gPpiList
;
61 Breakpoint target for Xcode project. Set in the Xcode XML
63 Xcode breakpoint will 'source Host.gdb'
64 gGdbWorkingFileName is set to Host.gdb
79 Main entry point to SEC for Unix. This is a unix program
82 Argc - Number of command line arguments
83 Argv - Array of command line argument strings
84 Envp - Array of environment variable strings
99 EFI_PHYSICAL_ADDRESS InitialStackMemory
;
100 UINT64 InitialStackMemorySize
;
107 EFI_PEI_FILE_HANDLE FileHandle
;
109 CHAR16
*MemorySizeStr
;
110 CHAR16
*FirmwareVolumesStr
;
115 // Xcode does not support sourcing gdb scripts directly, so the Xcode XML
116 // has a break point script to source the GdbRun.sh script.
118 SecGdbConfigBreak ();
121 // If dlopen doesn't work, then we build a gdb script to allow the
122 // symbols to be loaded.
124 Index
= strlen (*Argv
);
125 gGdbWorkingFileName
= AllocatePool (Index
+ strlen(".gdb") + 1);
126 strcpy (gGdbWorkingFileName
, *Argv
);
127 strcat (gGdbWorkingFileName
, ".gdb");
130 // Empty out the gdb symbols script file.
132 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
133 if (GdbTempFile
!= NULL
) {
134 fclose (GdbTempFile
);
137 printf ("\nEDK II UNIX Host Emulation Environment from http://www.tianocore.org/edk2/\n");
142 MemorySizeStr
= (CHAR16
*) PcdGetPtr (PcdEmuMemorySize
);
143 FirmwareVolumesStr
= (CHAR16
*) PcdGetPtr (PcdEmuFirmwareVolume
);
146 // PPIs pased into PEI_CORE
148 AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI
, &gEmuThunkPpiGuid
, &mSecEmuThunkPpi
);
150 SecInitThunkProtocol ();
153 // Emulator Bus Driver Thunks
155 AddThunkProtocol (&gX11ThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuGop
), TRUE
);
156 AddThunkProtocol (&gPosixFileSystemThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuFileSystem
), TRUE
);
157 AddThunkProtocol (&gBlockIoThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuVirtualDisk
), TRUE
);
158 AddThunkProtocol (&gSnpThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuNetworkInterface
), TRUE
);
161 // Emulator other Thunks
163 AddThunkProtocol (&gPthreadThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuApCount
), FALSE
);
165 // EmuSecLibConstructor ();
167 gPpiList
= GetThunkPpiList ();
170 // Allocate space for gSystemMemory Array
172 gSystemMemoryCount
= CountSeparatorsInString (MemorySizeStr
, '!') + 1;
173 gSystemMemory
= AllocateZeroPool (gSystemMemoryCount
* sizeof (EMU_SYSTEM_MEMORY
));
174 if (gSystemMemory
== NULL
) {
175 printf ("ERROR : Can not allocate memory for system. Exiting.\n");
179 // Allocate space for gSystemMemory Array
181 gFdInfoCount
= CountSeparatorsInString (FirmwareVolumesStr
, '!') + 1;
182 gFdInfo
= AllocateZeroPool (gFdInfoCount
* sizeof (EMU_FD_INFO
));
183 if (gFdInfo
== NULL
) {
184 printf ("ERROR : Can not allocate memory for fd info. Exiting.\n");
188 printf (" BootMode 0x%02x\n", (unsigned int)PcdGet32 (PcdEmuBootMode
));
191 // Open up a 128K file to emulate temp memory for SEC.
192 // on a real platform this would be SRAM, or using the cache as RAM.
193 // Set InitialStackMemory to zero so UnixOpenFile will allocate a new mapping
195 InitialStackMemorySize
= STACK_SIZE
;
196 InitialStackMemory
= (UINTN
)MapMemory (
197 0, (UINT32
) InitialStackMemorySize
,
198 PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_ANONYMOUS
| MAP_PRIVATE
200 if (InitialStackMemory
== 0) {
201 printf ("ERROR : Can not open SecStack Exiting\n");
205 printf (" OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n",
206 (unsigned int)(InitialStackMemorySize
/ 1024),
207 (unsigned long)InitialStackMemory
210 for (StackPointer
= (UINTN
*) (UINTN
) InitialStackMemory
;
211 StackPointer
< (UINTN
*)(UINTN
)((UINTN
) InitialStackMemory
+ (UINT64
) InitialStackMemorySize
);
213 *StackPointer
= 0x5AA55AA5;
217 // Open All the firmware volumes and remember the info in the gFdInfo global
219 FileName
= (CHAR8
*) AllocatePool (StrLen (FirmwareVolumesStr
) + 1);
220 if (FileName
== NULL
) {
221 printf ("ERROR : Can not allocate memory for firmware volume string\n");
226 for (Done
= FALSE
, Index
= 0, PeiIndex
= 0, SecFile
= NULL
;
227 FirmwareVolumesStr
[Index2
] != 0;
229 for (Index1
= 0; (FirmwareVolumesStr
[Index2
] != '!') && (FirmwareVolumesStr
[Index2
] != 0); Index2
++) {
230 FileName
[Index1
++] = FirmwareVolumesStr
[Index2
];
232 if (FirmwareVolumesStr
[Index2
] == '!') {
235 FileName
[Index1
] = '\0';
238 // Map FV Recovery Read Only and other areas Read/Write
246 // Open the FD and remember where it got mapped into our processes address space
251 &gFdInfo
[Index
].Address
,
255 if (EFI_ERROR (Status
)) {
256 printf ("ERROR : Can not open Firmware Device File %s (%x). Exiting.\n", FileName
, (unsigned int)Status
);
260 printf (" FD loaded from %s at 0x%08lx",FileName
, (unsigned long)gFdInfo
[Index
].Address
);
262 if (SecFile
== NULL
) {
264 // Assume the beginning of the FD is an FV and look for the SEC Core.
265 // Load the first one we find.
268 Status
= PeiServicesFfsFindNextFile (
269 EFI_FV_FILETYPE_SECURITY_CORE
,
270 (EFI_PEI_FV_HANDLE
)(UINTN
)gFdInfo
[Index
].Address
,
273 if (!EFI_ERROR (Status
)) {
274 Status
= PeiServicesFfsFindSectionData (EFI_SECTION_PE32
, FileHandle
, &SecFile
);
275 if (!EFI_ERROR (Status
)) {
277 printf (" contains SEC Core");
285 if (SecFile
== NULL
) {
286 printf ("ERROR : SEC not found!\n");
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 discontinuous 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 initialized 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 separators in String
830 String - String to process
831 Separator - Item to count
834 Number of Separator in String
838 CountSeparatorsInString (
839 IN
const CHAR16
*String
,
845 for (Count
= 0; *String
!= '\0'; String
++) {
846 if (*String
== Separator
) {
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
);
1158 // This is for the lldb breakpoint only
1159 SecGdbScriptBreak (ImageContext
->PdbPointer
, strlen (ImageContext
->PdbPointer
) + 1, (long unsigned int)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
), 1);
1164 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
1165 if (GdbTempFile
!= NULL
) {
1168 "add-symbol-file %s 0x%08lx\n",
1169 ImageContext
->PdbPointer
,
1170 (long unsigned int)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
)
1172 fclose (GdbTempFile
);
1175 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
1176 // Hey what can you say scripting in gdb is not that great....
1177 // Also used for the lldb breakpoint script. The lldb breakpoint script does
1178 // not use the file, it uses the arguments.
1180 SecGdbScriptBreak (ImageContext
->PdbPointer
, strlen (ImageContext
->PdbPointer
) + 1, (long unsigned int)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
), 1);
1191 SecPeCoffRelocateImageExtraAction (
1192 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1195 if (!DlLoadImage (ImageContext
)) {
1196 GdbScriptAddImage (ImageContext
);
1202 Adds the image to a gdb script so it's symbols can be unloaded.
1203 The RemoveFirmwareSymbolFile helper macro is used.
1205 @param ImageContext The PE/COFF image context
1209 GdbScriptRemoveImage (
1210 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1216 // Need to skip .PDB files created from VC++
1218 if (IsPdbFile (ImageContext
->PdbPointer
)) {
1222 if (FeaturePcdGet (PcdEmulatorLazyLoadSymbols
)) {
1224 // Write the file we need for the gdb script
1226 GdbTempFile
= fopen (gGdbWorkingFileName
, "a");
1227 if (GdbTempFile
!= NULL
) {
1228 mScriptSymbolChangesCount
++;
1231 "RemoveFirmwareSymbolFile 0x%x %s\n",
1232 mScriptSymbolChangesCount
,
1233 ImageContext
->PdbPointer
1235 fclose (GdbTempFile
);
1236 SecGdbScriptBreak (ImageContext
->PdbPointer
, strlen (ImageContext
->PdbPointer
) + 1, 0, 0);
1241 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
1242 if (GdbTempFile
!= NULL
) {
1243 fprintf (GdbTempFile
, "remove-symbol-file %s\n", ImageContext
->PdbPointer
);
1244 fclose (GdbTempFile
);
1247 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
1248 // Hey what can you say scripting in gdb is not that great....
1250 SecGdbScriptBreak (ImageContext
->PdbPointer
, strlen (ImageContext
->PdbPointer
) + 1, 0, 0);
1260 SecPeCoffUnloadImageExtraAction (
1261 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1267 // Check to see if the image symbols were loaded with gdb script, or dlopen
1269 Handle
= RemoveHandle (ImageContext
);
1270 if (Handle
!= NULL
) {
1277 GdbScriptRemoveImage (ImageContext
);