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 if (SecFile
== NULL
) {
292 printf ("ERROR : SEC not found!\n");
297 // Calculate memory regions and store the information in the gSystemMemory
298 // global for later use. The autosizing code will use this data to
299 // map this memory into the SEC process memory space.
306 // Save the size of the memory.
308 while (MemorySizeStr
[Index1
] >= '0' && MemorySizeStr
[Index1
] <= '9') {
309 val
= val
* 10 + MemorySizeStr
[Index1
] - '0';
312 gSystemMemory
[Index
++].Size
= val
* 0x100000;
313 if (MemorySizeStr
[Index1
] == 0) {
324 SecLoadFromCore ((UINTN
) InitialStackMemory
, (UINTN
) InitialStackMemorySize
, (UINTN
) gFdInfo
[0].Address
, SecFile
);
327 // If we get here, then the SEC Core returned. This is an error as SEC should
328 // always hand off to PEI Core and then on to DXE Core.
330 printf ("ERROR : SEC returned\n");
335 EFI_PHYSICAL_ADDRESS
*
343 STATIC UINTN base
= 0x40000000;
344 CONST UINTN align
= (1 << 24);
346 BOOLEAN isAligned
= 0;
349 // Try to get an aligned block somewhere in the address space of this
352 while((!isAligned
) && (base
!= 0)) {
353 res
= mmap ((void *)base
, length
, prot
, flags
, fd
, 0);
354 if (res
== MAP_FAILED
) {
357 if ((((UINTN
)res
) & ~(align
-1)) == (UINTN
)res
) {
371 Opens and memory maps a file using Unix services. If BaseAddress is non zero
372 the process will try and allocate the memory starting at BaseAddress.
375 FileName - The name of the file to open and map
376 MapSize - The amount of the file to map in bytes
377 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for
378 memory emulation, and exiting files for firmware volume emulation
379 BaseAddress - The base address of the mapped file in the user address space.
380 If passed in as NULL the a new memory region is used.
381 If passed in as non NULL the request memory region is used for
382 the mapping of the file into the process space.
383 Length - The size of the mapped region in bytes
386 EFI_SUCCESS - The file was opened and mapped.
387 EFI_NOT_FOUND - FileName was not found in the current directory
388 EFI_DEVICE_ERROR - An error occured attempting to map the opened file
394 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
402 fd
= open (FileName
, O_RDWR
);
404 return EFI_NOT_FOUND
;
406 FileSize
= lseek (fd
, 0, SEEK_END
);
409 res
= MapMemory (fd
, FileSize
, PROT_READ
| PROT_EXEC
, MAP_PRIVATE
);
414 perror ("MapFile() Failed");
415 return EFI_DEVICE_ERROR
;
418 *Length
= (UINT64
) FileSize
;
419 *BaseAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) res
;
427 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
432 void *res
, *res2
, *res3
;
437 fd
= open (FileName
, O_RDWR
);
439 return EFI_NOT_FOUND
;
441 FileSize
= lseek (fd
, 0, SEEK_END
);
443 FvSize
= FixedPcdGet64 (PcdEmuFlashFvRecoverySize
);
445 // Assume start of FD is Recovery FV, and make it write protected
447 (void *)(UINTN
)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase
),
449 PROT_READ
| PROT_EXEC
,
454 if (res
== MAP_FAILED
) {
455 perror ("MapFd0() Failed res =");
457 return EFI_DEVICE_ERROR
;
458 } else if (res
!= (void *)(UINTN
)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase
)) {
459 // We could not load at the build address, so we need to allow writes
460 munmap (res
, FvSize
);
462 (void *)(UINTN
)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase
),
464 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
469 if (res
== MAP_FAILED
) {
470 perror ("MapFd0() Failed res =");
472 return EFI_DEVICE_ERROR
;
476 // Map the rest of the FD as read/write
478 (void *)(UINTN
)(FixedPcdGet64 (PcdEmuFlashFvRecoveryBase
) + FvSize
),
480 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
486 if (res2
== MAP_FAILED
) {
487 perror ("MapFd0() Failed res2 =");
488 return EFI_DEVICE_ERROR
;
492 // If enabled use the magic page to communicate between modules
493 // This replaces the PI PeiServicesTable pointer mechanism that
494 // deos not work in the emulator. It also allows the removal of
495 // writable globals from SEC, PEI_CORE (libraries), PEIMs
497 EmuMagicPage
= (void *)(UINTN
)FixedPcdGet64 (PcdPeiServicesTablePage
);
498 if (EmuMagicPage
!= NULL
) {
500 (void *)EmuMagicPage
,
502 PROT_READ
| PROT_WRITE
,
503 MAP_PRIVATE
| MAP_ANONYMOUS
,
507 if (res3
!= EmuMagicPage
) {
508 printf ("MapFd0(): Could not allocate PeiServicesTablePage @ %lx\n", (long unsigned int)EmuMagicPage
);
509 return EFI_DEVICE_ERROR
;
513 *Length
= (UINT64
) FileSize
;
514 *BaseAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) res
;
523 This is the service to load the SEC Core from the Firmware Volume
526 LargestRegion - Memory to use for SEC.
527 LargestRegionSize - Size of Memory to use for PEI
528 BootFirmwareVolumeBase - Start of the Boot FV
529 PeiCorePe32File - SEC PE32
532 Success means control is transfered and thus we should never return
537 IN UINTN LargestRegion
,
538 IN UINTN LargestRegionSize
,
539 IN UINTN BootFirmwareVolumeBase
,
540 IN VOID
*PeiCorePe32File
544 EFI_PHYSICAL_ADDRESS TopOfMemory
;
546 EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint
;
547 EFI_SEC_PEI_HAND_OFF
*SecCoreData
;
551 // Compute Top Of Memory for Stack and PEI Core Allocations
553 TopOfMemory
= LargestRegion
+ LargestRegionSize
;
554 PeiStackSize
= (UINTN
)RShiftU64((UINT64
)STACK_SIZE
,1);
557 // |-----------| <---- TemporaryRamBase + TemporaryRamSize
560 // |-----------| <---- StackBase / PeiTemporaryMemoryBase
563 // |-----------| <---- TemporaryRamBase
565 TopOfStack
= (VOID
*)(LargestRegion
+ PeiStackSize
);
566 TopOfMemory
= LargestRegion
+ PeiStackSize
;
569 // Reservet space for storing PeiCore's parament in stack.
571 TopOfStack
= (VOID
*)((UINTN
)TopOfStack
- sizeof (EFI_SEC_PEI_HAND_OFF
) - CPU_STACK_ALIGNMENT
);
572 TopOfStack
= ALIGN_POINTER (TopOfStack
, CPU_STACK_ALIGNMENT
);
576 // Bind this information into the SEC hand-off state
578 SecCoreData
= (EFI_SEC_PEI_HAND_OFF
*)(UINTN
) TopOfStack
;
579 SecCoreData
->DataSize
= sizeof(EFI_SEC_PEI_HAND_OFF
);
580 SecCoreData
->BootFirmwareVolumeBase
= (VOID
*)BootFirmwareVolumeBase
;
581 SecCoreData
->BootFirmwareVolumeSize
= PcdGet32 (PcdEmuFirmwareFdSize
);
582 SecCoreData
->TemporaryRamBase
= (VOID
*)(UINTN
)LargestRegion
;
583 SecCoreData
->TemporaryRamSize
= STACK_SIZE
;
584 SecCoreData
->StackBase
= SecCoreData
->TemporaryRamBase
;
585 SecCoreData
->StackSize
= PeiStackSize
;
586 SecCoreData
->PeiTemporaryRamBase
= (VOID
*) ((UINTN
) SecCoreData
->TemporaryRamBase
+ PeiStackSize
);
587 SecCoreData
->PeiTemporaryRamSize
= STACK_SIZE
- PeiStackSize
;
590 // Find the SEC Core Entry Point
592 Status
= SecPeCoffGetEntryPoint (PeiCorePe32File
, (VOID
**)&PeiCoreEntryPoint
);
593 if (EFI_ERROR (Status
)) {
598 // Transfer control to the SEC Core
601 (SWITCH_STACK_ENTRY_POINT
) (UINTN
) PeiCoreEntryPoint
,
607 // If we get here, then the SEC Core returned. This is an error
616 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.
617 It allows discontiguous memory regions to be supported by the emulator.
618 It uses gSystemMemory[] and gSystemMemoryCount that were created by
619 parsing the host environment variable EFI_MEMORY_SIZE.
620 The size comes from the varaible and the address comes from the call to
624 Index - Which memory region to use
625 MemoryBase - Return Base address of memory region
626 MemorySize - Return size in bytes of the memory region
629 EFI_SUCCESS - If memory region was mapped
630 EFI_UNSUPPORTED - If Index is not supported
636 OUT EFI_PHYSICAL_ADDRESS
*MemoryBase
,
637 OUT UINT64
*MemorySize
642 if (Index
>= gSystemMemoryCount
) {
643 return EFI_UNSUPPORTED
;
648 0, gSystemMemory
[Index
].Size
,
649 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
650 MAP_PRIVATE
| MAP_ANONYMOUS
652 if (res
== MAP_FAILED
) {
653 return EFI_DEVICE_ERROR
;
655 *MemorySize
= gSystemMemory
[Index
].Size
;
656 *MemoryBase
= (UINTN
)res
;
657 gSystemMemory
[Index
].Memory
= *MemoryBase
;
666 Check to see if an address range is in the EFI GCD memory map.
668 This is all of GCD for system memory passed to DXE Core. FV
669 mapping and other device mapped into system memory are not
670 inlcuded in the check.
673 Index - Which memory region to use
674 MemoryBase - Return Base address of memory region
675 MemorySize - Return size in bytes of the memory region
678 TRUE - Address is in the EFI GCD memory map
679 FALSE - Address is NOT in memory map
683 EfiSystemMemoryRange (
684 IN VOID
*MemoryAddress
688 EFI_PHYSICAL_ADDRESS MemoryBase
;
690 MemoryBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)MemoryAddress
;
691 for (Index
= 0; Index
< gSystemMemoryCount
; Index
++) {
692 if ((MemoryBase
>= gSystemMemory
[Index
].Memory
) &&
693 (MemoryBase
< (gSystemMemory
[Index
].Memory
+ gSystemMemory
[Index
].Size
)) ) {
705 Since the SEC is the only Unix program in stack it must export
706 an interface to do POSIX calls. gUnix is initailized in UnixThunk.c.
709 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);
710 InterfaceBase - Address of the gUnix global
713 EFI_SUCCESS - Data returned
721 return &gEmuThunkProtocol
;
728 SecPeCoffGetEntryPoint (
730 IN OUT VOID
**EntryPoint
734 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
736 ZeroMem (&ImageContext
, sizeof (ImageContext
));
737 ImageContext
.Handle
= Pe32Data
;
738 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) SecImageRead
;
740 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
741 if (EFI_ERROR (Status
)) {
745 if (ImageContext
.ImageAddress
!= (UINTN
)Pe32Data
) {
747 // Relocate image to match the address where it resides
749 ImageContext
.ImageAddress
= (UINTN
)Pe32Data
;
750 Status
= PeCoffLoaderLoadImage (&ImageContext
);
751 if (EFI_ERROR (Status
)) {
755 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
756 if (EFI_ERROR (Status
)) {
761 // Or just return image entry point
763 ImageContext
.PdbPointer
= PeCoffLoaderGetPdbPointer (Pe32Data
);
764 Status
= PeCoffLoaderGetEntryPoint (Pe32Data
, EntryPoint
);
765 if (EFI_ERROR (Status
)) {
768 ImageContext
.EntryPoint
= (UINTN
)*EntryPoint
;
771 // On Unix a dlopen is done that will change the entry point
772 SecPeCoffRelocateImageExtraAction (&ImageContext
);
773 *EntryPoint
= (VOID
*)(UINTN
)ImageContext
.EntryPoint
;
783 Return the FD Size and base address. Since the FD is loaded from a
784 file into host memory only the SEC will know it's address.
787 Index - Which FD, starts at zero.
788 FdSize - Size of the FD in bytes
789 FdBase - Start address of the FD. Assume it points to an FV Header
790 FixUp - Difference between actual FD address and build address
793 EFI_SUCCESS - Return the Base address and size of the FV
794 EFI_UNSUPPORTED - Index does nto map to an FD in the system
800 IN OUT EFI_PHYSICAL_ADDRESS
*FdBase
,
801 IN OUT UINT64
*FdSize
,
802 IN OUT EFI_PHYSICAL_ADDRESS
*FixUp
805 if (Index
>= gFdInfoCount
) {
806 return EFI_UNSUPPORTED
;
809 *FdBase
= gFdInfo
[Index
].Address
;
810 *FdSize
= gFdInfo
[Index
].Size
;
813 if (*FdBase
== 0 && *FdSize
== 0) {
814 return EFI_UNSUPPORTED
;
819 // FD 0 has XIP code and well known PCD values
820 // If the memory buffer could not be allocated at the FD build address
821 // the Fixup is the difference.
823 *FixUp
= *FdBase
- PcdGet64 (PcdEmuFdBaseAddress
);
833 Count the number of seperators in String
836 String - String to process
837 Seperator - Item to count
840 Number of Seperator in String
844 CountSeperatorsInString (
845 IN
const CHAR16
*String
,
851 for (Count
= 0; *String
!= '\0'; String
++) {
852 if (*String
== Seperator
) {
866 IN OUT UINTN
*ReadSize
,
872 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
875 FileHandle - The handle to the PE/COFF file
876 FileOffset - The offset, in bytes, into the file to read
877 ReadSize - The number of bytes to read from the file starting at FileOffset
878 Buffer - A pointer to the buffer to read the data into.
881 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
889 Destination8
= Buffer
;
890 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
893 *(Destination8
++) = *(Source8
++);
903 Store the ModHandle in an array indexed by the Pdb File name.
904 The ModHandle is needed to unload the image.
907 ImageContext - Input data returned from PE Laoder Library. Used to find the
908 .PDB file name of the PE Image.
909 ModHandle - Returned from LoadLibraryEx() and stored for call to
913 EFI_SUCCESS - ModHandle was stored.
918 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
923 IMAGE_CONTEXT_TO_MOD_HANDLE
*Array
;
927 Array
= mImageContextModHandleArray
;
928 for (Index
= 0; Index
< mImageContextModHandleArraySize
; Index
++, Array
++) {
929 if (Array
->ImageContext
== NULL
) {
931 // Make a copy of the stirng and store the ModHandle
933 Array
->ImageContext
= ImageContext
;
934 Array
->ModHandle
= ModHandle
;
940 // No free space in mImageContextModHandleArray so grow it by
941 // IMAGE_CONTEXT_TO_MOD_HANDLE entires. realloc will
942 // copy the old values to the new locaiton. But it does
943 // not zero the new memory area.
945 PreviousSize
= mImageContextModHandleArraySize
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
);
946 mImageContextModHandleArraySize
+= MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE
;
948 mImageContextModHandleArray
= ReallocatePool (
949 (mImageContextModHandleArraySize
- 1) * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
),
950 mImageContextModHandleArraySize
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
),
951 mImageContextModHandleArray
953 if (mImageContextModHandleArray
== NULL
) {
955 return EFI_OUT_OF_RESOURCES
;
958 memset (mImageContextModHandleArray
+ PreviousSize
, 0, MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
));
960 return AddHandle (ImageContext
, ModHandle
);
967 Return the ModHandle and delete the entry in the array.
970 ImageContext - Input data returned from PE Laoder Library. Used to find the
971 .PDB file name of the PE Image.
974 ModHandle - ModHandle assoicated with ImageContext is returned
975 NULL - No ModHandle associated with ImageContext
980 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
984 IMAGE_CONTEXT_TO_MOD_HANDLE
*Array
;
986 if (ImageContext
->PdbPointer
== NULL
) {
988 // If no PDB pointer there is no ModHandle so return NULL
993 Array
= mImageContextModHandleArray
;
994 for (Index
= 0; Index
< mImageContextModHandleArraySize
; Index
++, Array
++) {
995 if (Array
->ImageContext
== ImageContext
) {
997 // If you find a match return it and delete the entry
999 Array
->ImageContext
= NULL
;
1000 return Array
->ModHandle
;
1011 IN CHAR8
*PdbFileName
1016 if (PdbFileName
== NULL
) {
1020 Len
= strlen (PdbFileName
);
1021 if ((Len
< 5)|| (PdbFileName
[Len
- 4] != '.')) {
1025 if ((PdbFileName
[Len
- 3] == 'P' || PdbFileName
[Len
- 3] == 'p') &&
1026 (PdbFileName
[Len
- 2] == 'D' || PdbFileName
[Len
- 2] == 'd') &&
1027 (PdbFileName
[Len
- 1] == 'B' || PdbFileName
[Len
- 1] == 'b')) {
1035 #define MAX_SPRINT_BUFFER_SIZE 0x200
1039 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1042 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
1050 "0x%08lx Loading %s with entry point 0x%08lx\n",
1051 (unsigned long)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
),
1052 ImageContext
->PdbPointer
,
1053 (unsigned long)ImageContext
->EntryPoint
1056 // Keep output synced up
1062 Loads the image using dlopen so symbols will be automatically
1065 @param ImageContext The PE/COFF image context
1067 @retval TRUE - The image was successfully loaded
1068 @retval FALSE - The image was successfully loaded
1073 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
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
);
1126 long unsigned int LoadAddress
,
1135 Adds the image to a gdb script so it's symbols can be loaded.
1136 The AddFirmwareSymbolFile helper macro is used.
1138 @param ImageContext The PE/COFF image context
1143 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1147 PrintLoadAddress (ImageContext
);
1149 if (ImageContext
->PdbPointer
!= NULL
&& !IsPdbFile (ImageContext
->PdbPointer
)) {
1151 if (FeaturePcdGet (PcdEmulatorLazyLoadSymbols
)) {
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
);
1168 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
1169 if (GdbTempFile
!= NULL
) {
1172 "add-symbol-file %s 0x%08lx\n",
1173 ImageContext
->PdbPointer
,
1174 (long unsigned int)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
)
1176 fclose (GdbTempFile
);
1179 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
1180 // Hey what can you say scripting in gdb is not that great....
1182 SecGdbScriptBreak (ImageContext
->PdbPointer
, strlen (ImageContext
->PdbPointer
), (long unsigned int)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
), 1);
1193 SecPeCoffRelocateImageExtraAction (
1194 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1197 if (!DlLoadImage (ImageContext
)) {
1198 GdbScriptAddImage (ImageContext
);
1204 Adds the image to a gdb script so it's symbols can be unloaded.
1205 The RemoveFirmwareSymbolFile helper macro is used.
1207 @param ImageContext The PE/COFF image context
1211 GdbScriptRemoveImage (
1212 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1218 // Need to skip .PDB files created from VC++
1220 if (IsPdbFile (ImageContext
->PdbPointer
)) {
1224 if (FeaturePcdGet (PcdEmulatorLazyLoadSymbols
)) {
1226 // Write the file we need for the gdb script
1228 GdbTempFile
= fopen (gGdbWorkingFileName
, "a");
1229 if (GdbTempFile
!= NULL
) {
1230 mScriptSymbolChangesCount
++;
1233 "RemoveFirmwareSymbolFile 0x%x %s\n",
1234 mScriptSymbolChangesCount
,
1235 ImageContext
->PdbPointer
1237 fclose (GdbTempFile
);
1242 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
1243 if (GdbTempFile
!= NULL
) {
1244 fprintf (GdbTempFile
, "remove-symbol-file %s\n", ImageContext
->PdbPointer
);
1245 fclose (GdbTempFile
);
1248 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
1249 // Hey what can you say scripting in gdb is not that great....
1251 SecGdbScriptBreak (ImageContext
->PdbPointer
, strlen (ImageContext
->PdbPointer
), 0, 0);
1261 SecPeCoffUnloadImageExtraAction (
1262 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1268 // Check to see if the image symbols were loaded with gdb script, or dlopen
1270 Handle
= RemoveHandle (ImageContext
);
1271 if (Handle
!= NULL
) {
1278 GdbScriptRemoveImage (ImageContext
);