3 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
4 Portions copyright (c) 2008 - 2009, 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 Unix emulator of SEC phase. It's really a Posix application, but this is
19 Ok since all the other modules for NT32 are NOT Posix applications.
21 This program processes host environment variables and figures out
22 what the memory layout will be, how may FD's will be loaded and also
23 what the boot mode is.
25 The SEC registers a set of services with the SEC core. gPrivateDispatchTable
26 is a list of PPI's produced by the SEC that are availble for usage in PEI.
28 This code produces 128 K of temporary memory for the PEI stack by opening a
29 host file and mapping it directly to memory addresses.
31 The system.cmd script is used to set host environment variables that drive
32 the configuration opitons of the SEC.
38 #include <Ppi/UnixPeiLoadFile.h>
39 #include <Ppi/TemporaryRamSupport.h>
43 #define MAP_ANONYMOUS MAP_ANON
44 char *gGdbWorkingFileName
= NULL
;
52 UNIX_PEI_LOAD_FILE_PPI mSecUnixLoadFilePpi
= { SecUnixPeiLoadFile
};
54 PEI_UNIX_AUTOSCAN_PPI mSecUnixAutoScanPpi
= { SecUnixPeiAutoScan
};
56 PEI_UNIX_THUNK_PPI mSecUnixThunkPpi
= { SecUnixUnixThunkAddress
};
58 EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi
= { SecPeiReportStatusCode
};
60 UNIX_FWH_PPI mSecFwhInformationPpi
= { SecUnixFdAddress
};
62 TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi
= {SecTemporaryRamSupport
};
64 EFI_PEI_PPI_DESCRIPTOR gPrivateDispatchTable
[] = {
66 EFI_PEI_PPI_DESCRIPTOR_PPI
,
67 &gUnixPeiLoadFilePpiGuid
,
71 EFI_PEI_PPI_DESCRIPTOR_PPI
,
72 &gPeiUnixAutoScanPpiGuid
,
76 EFI_PEI_PPI_DESCRIPTOR_PPI
,
77 &gPeiUnixThunkPpiGuid
,
81 EFI_PEI_PPI_DESCRIPTOR_PPI
,
82 &gEfiPeiStatusCodePpiGuid
,
86 EFI_PEI_PPI_DESCRIPTOR_PPI
,
87 &gEfiTemporaryRamSupportPpiGuid
,
88 &mSecTemporaryRamSupportPpi
91 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
93 &mSecFwhInformationPpi
99 // Default information about where the FD is located.
100 // This array gets filled in with information from EFI_FIRMWARE_VOLUMES
101 // EFI_FIRMWARE_VOLUMES is a host environment variable set by system.cmd.
102 // The number of array elements is allocated base on parsing
103 // EFI_FIRMWARE_VOLUMES and the memory is never freed.
105 UINTN gFdInfoCount
= 0;
106 UNIX_FD_INFO
*gFdInfo
;
109 // Array that supports seperate memory rantes.
110 // The memory ranges are set in system.cmd via the EFI_MEMORY_SIZE variable.
111 // The number of array elements is allocated base on parsing
112 // EFI_MEMORY_SIZE and the memory is never freed.
114 UINTN gSystemMemoryCount
= 0;
115 UNIX_SYSTEM_MEMORY
*gSystemMemory
;
119 UINTN mImageContextModHandleArraySize
= 0;
120 IMAGE_CONTEXT_TO_MOD_HANDLE
*mImageContextModHandleArray
= NULL
;
126 UINT32 TemporaryMemoryBase
,
127 UINT32 PermenentMemoryBase
130 EFI_PHYSICAL_ADDRESS
*
140 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
146 SecNt32PeCoffRelocateImage (
147 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
160 Main entry point to SEC for Unix. This is a unix program
163 Argc - Number of command line arguments
164 Argv - Array of command line argument strings
165 Envp - Array of environmemt variable strings
174 EFI_PHYSICAL_ADDRESS InitialStackMemory
;
175 UINT64 InitialStackMemorySize
;
183 CHAR16
*MemorySizeStr
;
184 CHAR16
*FirmwareVolumesStr
;
190 MemorySizeStr
= (CHAR16
*) PcdGetPtr (PcdUnixMemorySizeForSecMain
);
191 FirmwareVolumesStr
= (CHAR16
*) PcdGetPtr (PcdUnixFirmwareVolume
);
193 printf ("\nEDK SEC Main UNIX Emulation Environment from edk2.sourceforge.net\n");
197 // We can't use dlopen on OS X, so we need a scheme to get symboles into gdb
198 // We need to create a temp file that contains gdb commands so we can load
199 // symbols when we load every PE/COFF image.
201 Index
= strlen (*Argv
);
202 gGdbWorkingFileName
= malloc (Index
+ strlen(".gdb") + 1);
203 strcpy (gGdbWorkingFileName
, *Argv
);
204 strcat (gGdbWorkingFileName
, ".gdb");
209 // Allocate space for gSystemMemory Array
211 gSystemMemoryCount
= CountSeperatorsInString (MemorySizeStr
, '!') + 1;
212 gSystemMemory
= calloc (gSystemMemoryCount
, sizeof (UNIX_SYSTEM_MEMORY
));
213 if (gSystemMemory
== NULL
) {
214 printf ("ERROR : Can not allocate memory for system. Exiting.\n");
218 // Allocate space for gSystemMemory Array
220 gFdInfoCount
= CountSeperatorsInString (FirmwareVolumesStr
, '!') + 1;
221 gFdInfo
= calloc (gFdInfoCount
, sizeof (UNIX_FD_INFO
));
222 if (gFdInfo
== NULL
) {
223 printf ("ERROR : Can not allocate memory for fd info. Exiting.\n");
227 // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)
229 printf (" BootMode 0x%02x\n", (unsigned int)PcdGet32 (PcdUnixBootMode
));
232 // Open up a 128K file to emulate temp memory for PEI.
233 // on a real platform this would be SRAM, or using the cache as RAM.
234 // Set InitialStackMemory to zero so UnixOpenFile will allocate a new mapping
236 InitialStackMemorySize
= STACK_SIZE
;
237 InitialStackMemory
= (UINTN
)MapMemory(0,
238 (UINT32
) InitialStackMemorySize
,
239 PROT_READ
| PROT_WRITE
,
240 MAP_ANONYMOUS
| MAP_PRIVATE
);
241 if (InitialStackMemory
== 0) {
242 printf ("ERROR : Can not open SecStack Exiting\n");
246 printf (" SEC passing in %u KB of temp RAM at 0x%08lx to PEI\n",
247 (unsigned int)(InitialStackMemorySize
/ 1024),
248 (unsigned long)InitialStackMemory
);
250 for (StackPointer
= (UINTN
*) (UINTN
) InitialStackMemory
;
251 StackPointer
< (UINTN
*)(UINTN
)((UINTN
) InitialStackMemory
+ (UINT64
) InitialStackMemorySize
);
253 *StackPointer
= 0x5AA55AA5;
257 // Open All the firmware volumes and remember the info in the gFdInfo global
259 FileName
= (CHAR8
*)malloc (StrLen (FirmwareVolumesStr
) + 1);
260 if (FileName
== NULL
) {
261 printf ("ERROR : Can not allocate memory for firmware volume string\n");
266 for (Done
= FALSE
, Index
= 0, PeiIndex
= 0, PeiCoreFile
= NULL
;
267 FirmwareVolumesStr
[Index2
] != 0;
269 for (Index1
= 0; (FirmwareVolumesStr
[Index2
] != '!') && (FirmwareVolumesStr
[Index2
] != 0); Index2
++)
270 FileName
[Index1
++] = FirmwareVolumesStr
[Index2
];
271 if (FirmwareVolumesStr
[Index2
] == '!')
273 FileName
[Index1
] = '\0';
276 // Open the FD and remmeber where it got mapped into our processes address space
280 &gFdInfo
[Index
].Address
,
283 if (EFI_ERROR (Status
)) {
284 printf ("ERROR : Can not open Firmware Device File %s (%x). Exiting.\n", FileName
, (unsigned int)Status
);
288 printf (" FD loaded from %s at 0x%08lx",
289 FileName
, (unsigned long)gFdInfo
[Index
].Address
);
291 if (PeiCoreFile
== NULL
) {
293 // Assume the beginning of the FD is an FV and look for the PEI Core.
294 // Load the first one we find.
296 Status
= SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) gFdInfo
[Index
].Address
, &PeiCoreFile
);
297 if (!EFI_ERROR (Status
)) {
299 printf (" contains SEC Core");
306 // Calculate memory regions and store the information in the gSystemMemory
307 // global for later use. The autosizing code will use this data to
308 // map this memory into the SEC process memory space.
315 // Save the size of the memory.
317 while (MemorySizeStr
[Index1
] >= '0' && MemorySizeStr
[Index1
] <= '9') {
318 val
= val
* 10 + MemorySizeStr
[Index1
] - '0';
321 gSystemMemory
[Index
++].Size
= val
* 0x100000;
322 if (MemorySizeStr
[Index1
] == 0)
330 // Hand off to PEI Core
332 SecLoadFromCore ((UINTN
) InitialStackMemory
, (UINTN
) InitialStackMemorySize
, (UINTN
) gFdInfo
[0].Address
, PeiCoreFile
);
335 // If we get here, then the PEI Core returned. This is an error as PEI should
336 // always hand off to DXE.
338 printf ("ERROR : PEI Core returned\n");
342 EFI_PHYSICAL_ADDRESS
*
349 STATIC UINTN base
= 0x40000000;
350 CONST UINTN align
= (1 << 24);
352 BOOLEAN isAligned
= 0;
355 // Try to get an aligned block somewhere in the address space of this
358 while((!isAligned
) && (base
!= 0)) {
359 res
= mmap ((void *)base
, length
, prot
, flags
, fd
, 0);
360 if (res
== MAP_FAILED
) {
363 if ((((UINTN
)res
) & ~(align
-1)) == (UINTN
)res
) {
377 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
383 Opens and memory maps a file using Unix services. If BaseAddress is non zero
384 the process will try and allocate the memory starting at BaseAddress.
387 FileName - The name of the file to open and map
388 MapSize - The amount of the file to map in bytes
389 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for
390 memory emulation, and exiting files for firmware volume emulation
391 BaseAddress - The base address of the mapped file in the user address space.
392 If passed in as NULL the a new memory region is used.
393 If passed in as non NULL the request memory region is used for
394 the mapping of the file into the process space.
395 Length - The size of the mapped region in bytes
398 EFI_SUCCESS - The file was opened and mapped.
399 EFI_NOT_FOUND - FileName was not found in the current directory
400 EFI_DEVICE_ERROR - An error occured attempting to map the opened file
408 fd
= open (FileName
, O_RDONLY
);
410 return EFI_NOT_FOUND
;
411 FileSize
= lseek (fd
, 0, SEEK_END
);
416 /* Read entry address. */
417 lseek (fd
, FileSize
- 0x20, SEEK_SET
);
418 if (read (fd
, &EntryAddress
, 4) != 4)
421 return EFI_DEVICE_ERROR
;
426 res
= MapMemory(fd
, FileSize
, PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_PRIVATE
);
430 if (res
== MAP_FAILED
)
431 return EFI_DEVICE_ERROR
;
433 *Length
= (UINT64
) FileSize
;
434 *BaseAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) res
;
439 #define BYTES_PER_RECORD 512
443 SecPeiReportStatusCode (
444 IN CONST EFI_PEI_SERVICES
**PeiServices
,
445 IN EFI_STATUS_CODE_TYPE CodeType
,
446 IN EFI_STATUS_CODE_VALUE Value
,
448 IN CONST EFI_GUID
*CallerId
,
449 IN CONST EFI_STATUS_CODE_DATA
*Data OPTIONAL
455 This routine produces the ReportStatusCode PEI service. It's passed
456 up to the PEI Core via a PPI. T
458 This code currently uses the UNIX clib printf. This does not work the same way
459 as the EFI Print (), as %t, %g, %s as Unicode are not supported.
462 (see EFI_PEI_REPORT_STATUS_CODE)
465 EFI_SUCCESS - Always return success
468 // TODO: PeiServices - add argument and description to function comment
469 // TODO: CodeType - add argument and description to function comment
470 // TODO: Value - add argument and description to function comment
471 // TODO: Instance - add argument and description to function comment
472 // TODO: CallerId - add argument and description to function comment
473 // TODO: Data - add argument and description to function comment
477 CHAR8 PrintBuffer
[BYTES_PER_RECORD
* 2];
485 } else if (ReportStatusCodeExtractAssertInfo (CodeType
, Value
, Data
, &Filename
, &Description
, &LineNumber
)) {
487 // Processes ASSERT ()
489 printf ("ASSERT %s(%d): %s\n", Filename
, (int)LineNumber
, Description
);
491 } else if (ReportStatusCodeExtractDebugInfo (Data
, &ErrorLevel
, &Marker
, &Format
)) {
493 // Process DEBUG () macro
495 AsciiBSPrint (PrintBuffer
, BYTES_PER_RECORD
, Format
, Marker
);
496 printf ("%s", PrintBuffer
);
505 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
506 IN VOID
*Context1
, OPTIONAL
507 IN VOID
*Context2
, OPTIONAL
508 IN VOID
*Context3
, OPTIONAL
514 IN UINTN LargestRegion
,
515 IN UINTN LargestRegionSize
,
516 IN UINTN BootFirmwareVolumeBase
,
517 IN VOID
*PeiCorePe32File
522 This is the service to load the PEI Core from the Firmware Volume
525 LargestRegion - Memory to use for PEI.
526 LargestRegionSize - Size of Memory to use for PEI
527 BootFirmwareVolumeBase - Start of the Boot FV
528 PeiCorePe32File - PEI Core PE32
531 Success means control is transfered and thus we should never return
536 EFI_PHYSICAL_ADDRESS TopOfMemory
;
539 EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint
;
540 EFI_PHYSICAL_ADDRESS PeiImageAddress
;
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 (PcdUnixFirmwareFdSize
);
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 // Load the PEI Core from a Firmware Volume
586 Status
= SecUnixPeiLoadFile (
592 if (EFI_ERROR (Status
)) {
597 // Transfer control to the PEI Core
600 (SWITCH_STACK_ENTRY_POINT
) (UINTN
) PeiCoreEntryPoint
,
602 (VOID
*) (UINTN
) ((EFI_PEI_PPI_DESCRIPTOR
*) &gPrivateDispatchTable
),
607 // If we get here, then the PEI Core returned. This is an error
616 OUT EFI_PHYSICAL_ADDRESS
*MemoryBase
,
617 OUT UINT64
*MemorySize
622 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.
623 It allows discontiguous memory regions to be supported by the emulator.
624 It uses gSystemMemory[] and gSystemMemoryCount that were created by
625 parsing the host environment variable EFI_MEMORY_SIZE.
626 The size comes from the varaible and the address comes from the call to
630 Index - Which memory region to use
631 MemoryBase - Return Base address of memory region
632 MemorySize - Return size in bytes of the memory region
635 EFI_SUCCESS - If memory region was mapped
636 EFI_UNSUPPORTED - If Index is not supported
642 if (Index
>= gSystemMemoryCount
) {
643 return EFI_UNSUPPORTED
;
647 res
= MapMemory(0, gSystemMemory
[Index
].Size
,
648 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
649 MAP_PRIVATE
| MAP_ANONYMOUS
);
650 if (res
== MAP_FAILED
)
651 return EFI_DEVICE_ERROR
;
652 *MemorySize
= gSystemMemory
[Index
].Size
;
653 *MemoryBase
= (UINTN
)res
;
654 gSystemMemory
[Index
].Memory
= *MemoryBase
;
661 SecUnixUnixThunkAddress (
667 Since the SEC is the only Unix program in stack it must export
668 an interface to do POSIX calls. gUnix is initailized in UnixThunk.c.
671 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);
672 InterfaceBase - Address of the gUnix global
675 EFI_SUCCESS - Data returned
686 OUT EFI_PHYSICAL_ADDRESS
*ImageAddress
,
687 OUT UINT64
*ImageSize
,
688 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
693 Loads and relocates a PE/COFF image into memory.
696 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
697 ImageAddress - The base address of the relocated PE/COFF image
698 ImageSize - The size of the relocated PE/COFF image
699 EntryPoint - The entry point of the relocated PE/COFF image
702 EFI_SUCCESS - The file was loaded and relocated
703 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
708 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
710 ZeroMem (&ImageContext
, sizeof (ImageContext
));
711 ImageContext
.Handle
= Pe32Data
;
713 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) SecImageRead
;
715 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
716 if (EFI_ERROR (Status
)) {
720 // Allocate space in UNIX (not emulator) memory. Extra space is for alignment
722 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) malloc ((UINTN
) (ImageContext
.ImageSize
+ (ImageContext
.SectionAlignment
* 2)));
723 if (ImageContext
.ImageAddress
== 0) {
724 return EFI_OUT_OF_RESOURCES
;
727 // Align buffer on section boundry
729 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
730 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
733 Status
= PeCoffLoaderLoadImage (&ImageContext
);
734 if (EFI_ERROR (Status
)) {
738 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
739 if (EFI_ERROR (Status
)) {
744 SecPeCoffRelocateImageExtraAction (&ImageContext
);
747 // BugBug: Flush Instruction Cache Here when CPU Lib is ready
750 *ImageAddress
= ImageContext
.ImageAddress
;
751 *ImageSize
= ImageContext
.ImageSize
;
752 *EntryPoint
= ImageContext
.EntryPoint
;
760 SecPeCoffGetEntryPoint (
762 IN OUT VOID
**EntryPoint
766 EFI_PHYSICAL_ADDRESS ImageAddress
;
768 EFI_PHYSICAL_ADDRESS PhysEntryPoint
;
770 Status
= SecUnixPeiLoadFile (Pe32Data
, &ImageAddress
, &ImageSize
, &PhysEntryPoint
);
772 *EntryPoint
= (VOID
*)(UINTN
)PhysEntryPoint
;
782 IN OUT EFI_PHYSICAL_ADDRESS
*FdBase
,
783 IN OUT UINT64
*FdSize
,
784 IN OUT EFI_PHYSICAL_ADDRESS
*FixUp
789 Return the FD Size and base address. Since the FD is loaded from a
790 file into host memory only the SEC will know it's address.
793 Index - Which FD, starts at zero.
794 FdSize - Size of the FD in bytes
795 FdBase - Start address of the FD. Assume it points to an FV Header
796 FixUp - Difference between actual FD address and build address
799 EFI_SUCCESS - Return the Base address and size of the FV
800 EFI_UNSUPPORTED - Index does nto map to an FD in the system
804 if (Index
>= gFdInfoCount
) {
805 return EFI_UNSUPPORTED
;
808 *FdBase
= gFdInfo
[Index
].Address
;
809 *FdSize
= gFdInfo
[Index
].Size
;
812 if (*FdBase
== 0 && *FdSize
== 0) {
813 return EFI_UNSUPPORTED
;
818 // FD 0 has XIP code and well known PCD values
819 // If the memory buffer could not be allocated at the FD build address
820 // the Fixup is the difference.
822 *FixUp
= *FdBase
- PcdGet32 (PcdUnixFdBaseAddress
);
833 IN OUT UINTN
*ReadSize
,
839 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
842 FileHandle - The handle to the PE/COFF file
843 FileOffset - The offset, in bytes, into the file to read
844 ReadSize - The number of bytes to read from the file starting at FileOffset
845 Buffer - A pointer to the buffer to read the data into.
848 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
856 Destination8
= Buffer
;
857 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
860 *(Destination8
++) = *(Source8
++);
867 CountSeperatorsInString (
868 IN
const CHAR16
*String
,
874 Count the number of seperators in String
877 String - String to process
878 Seperator - Item to count
881 Number of Seperator in String
887 for (Count
= 0; *String
!= '\0'; String
++) {
888 if (*String
== Seperator
) {
899 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
905 Store the ModHandle in an array indexed by the Pdb File name.
906 The ModHandle is needed to unload the image.
909 ImageContext - Input data returned from PE Laoder Library. Used to find the
910 .PDB file name of the PE Image.
911 ModHandle - Returned from LoadLibraryEx() and stored for call to
915 EFI_SUCCESS - ModHandle was stored.
920 IMAGE_CONTEXT_TO_MOD_HANDLE
*Array
;
924 Array
= mImageContextModHandleArray
;
925 for (Index
= 0; Index
< mImageContextModHandleArraySize
; Index
++, Array
++) {
926 if (Array
->ImageContext
== NULL
) {
928 // Make a copy of the stirng and store the ModHandle
930 Array
->ImageContext
= ImageContext
;
931 Array
->ModHandle
= ModHandle
;
937 // No free space in mImageContextModHandleArray so grow it by
938 // IMAGE_CONTEXT_TO_MOD_HANDLE entires. realloc will
939 // copy the old values to the new locaiton. But it does
940 // not zero the new memory area.
942 PreviousSize
= mImageContextModHandleArraySize
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
);
943 mImageContextModHandleArraySize
+= MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE
;
945 mImageContextModHandleArray
= realloc (mImageContextModHandleArray
, mImageContextModHandleArraySize
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
));
946 if (mImageContextModHandleArray
== NULL
) {
948 return EFI_OUT_OF_RESOURCES
;
951 memset (mImageContextModHandleArray
+ PreviousSize
, 0, MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE
* sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE
));
953 return AddHandle (ImageContext
, ModHandle
);
959 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
964 Return the ModHandle and delete the entry in the array.
967 ImageContext - Input data returned from PE Laoder Library. Used to find the
968 .PDB file name of the PE Image.
971 ModHandle - ModHandle assoicated with ImageContext is returned
972 NULL - No ModHandle associated with ImageContext
977 IMAGE_CONTEXT_TO_MOD_HANDLE
*Array
;
979 if (ImageContext
->PdbPointer
== NULL
) {
981 // If no PDB pointer there is no ModHandle so return NULL
986 Array
= mImageContextModHandleArray
;
987 for (Index
= 0; Index
< mImageContextModHandleArraySize
; Index
++, Array
++) {
988 if ((Array
->ImageContext
== ImageContext
)) {
990 // If you find a match return it and delete the entry
992 Array
->ImageContext
= NULL
;
993 return Array
->ModHandle
;
1003 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to source a
1004 // add-symbol-file command. Hey what can you say scripting in gdb is not that great....
1006 // Put .gdbinit in the CWD where you do gdb SecMain.dll for source level debug
1009 // b SecGdbScriptBreak
1012 // source SecMain.dll.gdb
1024 SecUnixLoaderBreak (
1032 IN CHAR8
*PdbFileName
1037 if (PdbFileName
== NULL
) {
1041 Len
= strlen (PdbFileName
);
1042 if ((Len
< 5)|| (PdbFileName
[Len
- 4] != '.')) {
1046 if ((PdbFileName
[Len
- 3] == 'P' || PdbFileName
[Len
- 3] == 'p') &&
1047 (PdbFileName
[Len
- 2] == 'D' || PdbFileName
[Len
- 2] == 'd') &&
1048 (PdbFileName
[Len
- 1] == 'B' || PdbFileName
[Len
- 1] == 'b')) {
1056 #define MAX_SPRINT_BUFFER_SIZE 0x200
1060 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1064 "0x%08lx Loading %s with entry point 0x%08lx\n",
1065 (unsigned long)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
),
1066 ImageContext
->PdbPointer
,
1067 (unsigned long)ImageContext
->EntryPoint
1070 // Keep output synced up
1077 SecPeCoffRelocateImageExtraAction (
1078 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1083 PrintLoadAddress (ImageContext
);
1086 // In mach-o (OS X executable) dlopen() can only load files in the MH_DYLIB of MH_BUNDLE format.
1087 // To convert to PE/COFF we need to construct a mach-o with the MH_PRELOAD format. We create
1088 // .dSYM files for the PE/COFF images that can be used by gdb for source level debugging.
1093 // In the Mach-O to PE/COFF conversion the size of the PE/COFF headers is not accounted for.
1094 // Thus we need to skip over the PE/COFF header when giving load addresses for our symbol table.
1096 if (ImageContext
->PdbPointer
!= NULL
&& !IsPdbFile (ImageContext
->PdbPointer
)) {
1098 // Now we have a database of the images that are currently loaded
1102 // 'symbol-file' will clear out currnet symbol mappings in gdb.
1103 // you can do a 'add-symbol-file filename address' for every image we loaded to get source
1104 // level debug in gdb. Note Sec, being a true application will work differently.
1106 // We add the PE/COFF header size into the image as the mach-O does not have a header in
1107 // loaded into system memory.
1109 // This gives us a data base of gdb commands and after something is unloaded that entry will be
1110 // removed. We don't yet have the scheme of how to comunicate with gdb, but we have the
1111 // data base of info ready to roll.
1113 // We could use qXfer:libraries:read, but OS X GDB does not currently support it.
1115 // <library name="/lib/libc.so.6"> // ImageContext->PdbPointer
1116 // <segment address="0x10000000"/> // ImageContext->ImageAddress + ImageContext->SizeOfHeaders
1122 // Write the file we need for the gdb script
1124 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
1125 if (GdbTempFile
!= NULL
) {
1126 fprintf (GdbTempFile
, "add-symbol-file %s 0x%x\n", ImageContext
->PdbPointer
, (unsigned int)(ImageContext
->ImageAddress
+ ImageContext
->SizeOfHeaders
));
1127 fclose (GdbTempFile
);
1130 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
1131 // Hey what can you say scripting in gdb is not that great....
1133 SecGdbScriptBreak ();
1136 AddHandle (ImageContext
, ImageContext
->PdbPointer
);
1142 void *Handle
= NULL
;
1146 "Loading %s 0x%08lx - entry point 0x%08lx\n",
1147 ImageContext
->PdbPointer
,
1148 (unsigned long)ImageContext
->ImageAddress
,
1149 (unsigned long)ImageContext
->EntryPoint
);
1151 Handle
= dlopen (ImageContext
->PdbPointer
, RTLD_NOW
);
1154 Entry
= dlsym (Handle
, "_ModuleEntryPoint");
1156 printf("%s\n", dlerror());
1159 if (Entry
!= NULL
) {
1160 ImageContext
->EntryPoint
= (UINTN
)Entry
;
1161 printf("Change %s Entrypoint to :0x%08lx\n", ImageContext
->PdbPointer
, (unsigned long)Entry
);
1164 SecUnixLoaderBreak ();
1174 SecPeCoffLoaderUnloadImageExtraAction (
1175 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1180 Handle
= RemoveHandle (ImageContext
);
1185 if (Handle
!= NULL
) {
1187 // Need to skip .PDB files created from VC++
1189 if (!IsPdbFile (ImageContext
->PdbPointer
)) {
1191 // Write the file we need for the gdb script
1193 GdbTempFile
= fopen (gGdbWorkingFileName
, "w");
1194 if (GdbTempFile
!= NULL
) {
1195 fprintf (GdbTempFile
, "remove-symbol-file %s\n", ImageContext
->PdbPointer
);
1196 fclose (GdbTempFile
);
1199 // Target for gdb breakpoint in a script that uses gGdbWorkingFileName to set a breakpoint.
1200 // Hey what can you say scripting in gdb is not that great....
1202 SecGdbScriptBreak ();
1209 // Don't want to confuse gdb with symbols for something that got unloaded
1211 if (Handle
!= NULL
) {
1228 SecTemporaryRamSupport (
1229 IN CONST EFI_PEI_SERVICES
**PeiServices
,
1230 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase
,
1231 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase
,
1236 // Migrate the whole temporary memory to permenent memory.
1239 (VOID
*)(UINTN
)PermanentMemoryBase
,
1240 (VOID
*)(UINTN
)TemporaryMemoryBase
,
1245 // SecSwitchStack function must be invoked after the memory migration
1246 // immediatly, also we need fixup the stack change caused by new call into
1247 // permenent memory.
1250 (UINT32
) TemporaryMemoryBase
,
1251 (UINT32
) PermanentMemoryBase
1255 // We need *not* fix the return address because currently,
1256 // The PeiCore is excuted in flash.
1260 // Simulate to invalid temporary memory, terminate temporary memory
1262 //ZeroMem ((VOID*)(UINTN)TemporaryMemoryBase, CopySize);