/** Find the Pcd database. @param FileHandle Handle of the file the external PCD database binary located. @retval The base address of external PCD database binary. @retval NULL Return NULL if not find. **/ VOID * LocateExPcdBinary ( IN EFI_PEI_FILE_HANDLE FileHandle ) { EFI_STATUS Status; VOID *PcdDb; PcdDb = NULL; ASSERT (FileHandle != NULL); Status = PeiServicesFfsFindSectionData (EFI_SECTION_RAW, FileHandle, &PcdDb); ASSERT_EFI_ERROR (Status); // // Check the first bytes (Header Signature Guid) and build version. // if (!CompareGuid (PcdDb, &gPcdDataBaseSignatureGuid) || (((PEI_PCD_DATABASE *) PcdDb)->BuildVersion != PCD_SERVICE_PEIM_VERSION)) { ASSERT (FALSE); } return PcdDb; }
/** Loads a PEIM into memory for subsequent execution. If there are compressed images or images that need to be relocated into memory for performance reasons, this service performs that transformation. @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation @param FileHandle Pointer to the FFS file header of the image. @param ImageAddressArg Pointer to PE/TE image. @param ImageSizeArg Size of PE/TE image. @param EntryPoint Pointer to entry point of specified image file for output. @param AuthenticationState - Pointer to attestation authentication state of image. @retval EFI_SUCCESS Image is successfully loaded. @retval EFI_NOT_FOUND Fail to locate necessary PPI. @retval EFI_UNSUPPORTED Image Machine Type is not supported. **/ EFI_STATUS PeiLoadImageLoadImage ( IN CONST EFI_PEI_SERVICES **PeiServices, IN EFI_PEI_FILE_HANDLE FileHandle, OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL OUT UINT64 *ImageSizeArg, OPTIONAL OUT EFI_PHYSICAL_ADDRESS *EntryPoint, OUT UINT32 *AuthenticationState ) { EFI_STATUS Status; VOID *Pe32Data; EFI_PHYSICAL_ADDRESS ImageAddress; UINT64 ImageSize; EFI_PHYSICAL_ADDRESS ImageEntryPoint; UINT16 Machine; EFI_SECTION_TYPE SearchType1; EFI_SECTION_TYPE SearchType2; *EntryPoint = 0; ImageSize = 0; *AuthenticationState = 0; if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst)) { SearchType1 = EFI_SECTION_TE; SearchType2 = EFI_SECTION_PE32; } else { SearchType1 = EFI_SECTION_PE32; SearchType2 = EFI_SECTION_TE; } // // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst // is true, TE will be searched first). // Status = PeiServicesFfsFindSectionData ( SearchType1, FileHandle, &Pe32Data ); // // If we didn't find a first exe section, try to find the second exe section. // if (EFI_ERROR (Status)) { Status = PeiServicesFfsFindSectionData ( SearchType2, FileHandle, &Pe32Data ); if (EFI_ERROR (Status)) { // // PEI core only carry the loader function fro TE and PE32 executables // If this two section does not exist, just return. // return Status; } } // // If memory is installed, perform the shadow operations // Status = LoadAndRelocatePeCoffImage ( Pe32Data, &ImageAddress, &ImageSize, &ImageEntryPoint ); ASSERT_EFI_ERROR (Status); if (EFI_ERROR (Status)) { return Status; } // // Got the entry point from the loaded Pe32Data // Pe32Data = (VOID *) ((UINTN) ImageAddress); *EntryPoint = ImageEntryPoint; Machine = PeCoffLoaderGetMachineType (Pe32Data); if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) { if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) { return EFI_UNSUPPORTED; } } if (ImageAddressArg != NULL) { *ImageAddressArg = ImageAddress; } if (ImageSizeArg != NULL) { *ImageSizeArg = ImageSize; } DEBUG_CODE_BEGIN (); CHAR8 *AsciiString; CHAR8 AsciiBuffer[512]; INT32 Index; INT32 Index1; // // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi // if (Machine != EFI_IMAGE_MACHINE_IA64) { DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint)); } else { // // For IPF Image, the real entry point should be print. // DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)(*(UINT64 *)(UINTN)*EntryPoint))); } // // Print Module Name by PeImage PDB file name. // AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data); if (AsciiString != NULL) { for (Index = (INT32) AsciiStrLen (AsciiString) - 1; Index >= 0; Index --) { if (AsciiString[Index] == '\\') { break; } } if (Index != 0) { for (Index1 = 0; AsciiString[Index + 1 + Index1] != '.'; Index1 ++) { AsciiBuffer [Index1] = AsciiString[Index + 1 + Index1]; } AsciiBuffer [Index1] = '\0'; DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer)); } } DEBUG_CODE_END (); DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n")); return EFI_SUCCESS; }
/*++ Routine Description: Main entry point to SEC for Unix. This is a unix program Arguments: Argc - Number of command line arguments Argv - Array of command line argument strings Envp - Array of environment variable strings Returns: 0 - Normal exit 1 - Abnormal exit **/ int main ( IN int Argc, IN char **Argv, IN char **Envp ) { EFI_STATUS Status; EFI_PHYSICAL_ADDRESS InitialStackMemory; UINT64 InitialStackMemorySize; UINTN Index; UINTN Index1; UINTN Index2; UINTN PeiIndex; CHAR8 *FileName; BOOLEAN Done; EFI_PEI_FILE_HANDLE FileHandle; VOID *SecFile; CHAR16 *MemorySizeStr; CHAR16 *FirmwareVolumesStr; UINTN *StackPointer; FILE *GdbTempFile; // // Xcode does not support sourcing gdb scripts directly, so the Xcode XML // has a break point script to source the GdbRun script. // SecGdbConfigBreak (); // // If dlopen doesn't work, then we build a gdb script to allow the // symbols to be loaded. // Index = strlen (*Argv); gGdbWorkingFileName = AllocatePool (Index + strlen(".gdb") + 1); strcpy (gGdbWorkingFileName, *Argv); strcat (gGdbWorkingFileName, ".gdb"); // // Empty out the gdb symbols script file. // GdbTempFile = fopen (gGdbWorkingFileName, "w"); if (GdbTempFile != NULL) { fclose (GdbTempFile); } printf ("\nEDK II UNIX Host Emulation Environment from http://www.tianocore.org/edk2/\n"); setbuf (stdout, 0); setbuf (stderr, 0); MemorySizeStr = (CHAR16 *) PcdGetPtr (PcdEmuMemorySize); FirmwareVolumesStr = (CHAR16 *) PcdGetPtr (PcdEmuFirmwareVolume); // // PPIs pased into PEI_CORE // AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuThunkPpiGuid, &mSecEmuThunkPpi); SecInitThunkProtocol (); // // Emulator Bus Driver Thunks // AddThunkProtocol (&gX11ThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuGop), TRUE); AddThunkProtocol (&gPosixFileSystemThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuFileSystem), TRUE); AddThunkProtocol (&gBlockIoThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuVirtualDisk), TRUE); AddThunkProtocol (&gSnpThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuNetworkInterface), TRUE); // // Emulator other Thunks // AddThunkProtocol (&gPthreadThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuApCount), FALSE); // EmuSecLibConstructor (); gPpiList = GetThunkPpiList (); // // Allocate space for gSystemMemory Array // gSystemMemoryCount = CountSeparatorsInString (MemorySizeStr, '!') + 1; gSystemMemory = AllocateZeroPool (gSystemMemoryCount * sizeof (EMU_SYSTEM_MEMORY)); if (gSystemMemory == NULL) { printf ("ERROR : Can not allocate memory for system. Exiting.\n"); exit (1); } // // Allocate space for gSystemMemory Array // gFdInfoCount = CountSeparatorsInString (FirmwareVolumesStr, '!') + 1; gFdInfo = AllocateZeroPool (gFdInfoCount * sizeof (EMU_FD_INFO)); if (gFdInfo == NULL) { printf ("ERROR : Can not allocate memory for fd info. Exiting.\n"); exit (1); } printf (" BootMode 0x%02x\n", (unsigned int)PcdGet32 (PcdEmuBootMode)); // // Open up a 128K file to emulate temp memory for SEC. // on a real platform this would be SRAM, or using the cache as RAM. // Set InitialStackMemory to zero so UnixOpenFile will allocate a new mapping // InitialStackMemorySize = STACK_SIZE; InitialStackMemory = (UINTN)MapMemory ( 0, (UINT32) InitialStackMemorySize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE ); if (InitialStackMemory == 0) { printf ("ERROR : Can not open SecStack Exiting\n"); exit (1); } printf (" OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n", (unsigned int)(InitialStackMemorySize / 1024), (unsigned long)InitialStackMemory ); for (StackPointer = (UINTN*) (UINTN) InitialStackMemory; StackPointer < (UINTN*)(UINTN)((UINTN) InitialStackMemory + (UINT64) InitialStackMemorySize); StackPointer ++) { *StackPointer = 0x5AA55AA5; } // // Open All the firmware volumes and remember the info in the gFdInfo global // FileName = (CHAR8 *) AllocatePool (StrLen (FirmwareVolumesStr) + 1); if (FileName == NULL) { printf ("ERROR : Can not allocate memory for firmware volume string\n"); exit (1); } Index2 = 0; for (Done = FALSE, Index = 0, PeiIndex = 0, SecFile = NULL; FirmwareVolumesStr[Index2] != 0; Index++) { for (Index1 = 0; (FirmwareVolumesStr[Index2] != '!') && (FirmwareVolumesStr[Index2] != 0); Index2++) { FileName[Index1++] = FirmwareVolumesStr[Index2]; } if (FirmwareVolumesStr[Index2] == '!') { Index2++; } FileName[Index1] = '\0'; if (Index == 0) { // Map FV Recovery Read Only and other areas Read/Write Status = MapFd0 ( FileName, &gFdInfo[0].Address, &gFdInfo[0].Size ); } else { // // Open the FD and remember where it got mapped into our processes address space // Maps Read Only // Status = MapFile ( FileName, &gFdInfo[Index].Address, &gFdInfo[Index].Size ); } if (EFI_ERROR (Status)) { printf ("ERROR : Can not open Firmware Device File %s (%x). Exiting.\n", FileName, (unsigned int)Status); exit (1); } printf (" FD loaded from %s at 0x%08lx",FileName, (unsigned long)gFdInfo[Index].Address); if (SecFile == NULL) { // // Assume the beginning of the FD is an FV and look for the SEC Core. // Load the first one we find. // FileHandle = NULL; Status = PeiServicesFfsFindNextFile ( EFI_FV_FILETYPE_SECURITY_CORE, (EFI_PEI_FV_HANDLE)(UINTN)gFdInfo[Index].Address, &FileHandle ); if (!EFI_ERROR (Status)) { Status = PeiServicesFfsFindSectionData (EFI_SECTION_PE32, FileHandle, &SecFile); if (!EFI_ERROR (Status)) { PeiIndex = Index; printf (" contains SEC Core"); } } } printf ("\n"); } if (SecFile == NULL) { printf ("ERROR : SEC not found!\n"); exit (1); } // // Calculate memory regions and store the information in the gSystemMemory // global for later use. The autosizing code will use this data to // map this memory into the SEC process memory space. // Index1 = 0; Index = 0; while (1) { UINTN val = 0; // // Save the size of the memory. // while (MemorySizeStr[Index1] >= '0' && MemorySizeStr[Index1] <= '9') { val = val * 10 + MemorySizeStr[Index1] - '0'; Index1++; } gSystemMemory[Index++].Size = val * 0x100000; if (MemorySizeStr[Index1] == 0) { break; } Index1++; } printf ("\n"); // // Hand off to SEC // SecLoadFromCore ((UINTN) InitialStackMemory, (UINTN) InitialStackMemorySize, (UINTN) gFdInfo[0].Address, SecFile); // // If we get here, then the SEC Core returned. This is an error as SEC should // always hand off to PEI Core and then on to DXE Core. // printf ("ERROR : SEC returned\n"); exit (1); }
INTN EFIAPI main ( IN INTN Argc, IN CHAR8 **Argv, IN CHAR8 **Envp ) /*++ Routine Description: Main entry point to SEC for WinNt. This is a Windows program Arguments: Argc - Number of command line arguments Argv - Array of command line argument strings Envp - Array of environment variable strings Returns: 0 - Normal exit 1 - Abnormal exit --*/ { EFI_STATUS Status; HANDLE Token; TOKEN_PRIVILEGES TokenPrivileges; VOID *TemporaryRam; UINT32 TemporaryRamSize; VOID *EmuMagicPage; UINTN Index; UINTN Index1; CHAR16 *FileName; CHAR16 *FileNamePtr; BOOLEAN Done; EFI_PEI_FILE_HANDLE FileHandle; VOID *SecFile; CHAR16 *MemorySizeStr; CHAR16 *FirmwareVolumesStr; UINT32 ProcessAffinityMask; UINT32 SystemAffinityMask; INT32 LowBit; // // Enable the privilege so that RTC driver can successfully run SetTime() // OpenProcessToken (GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &Token); if (LookupPrivilegeValue(NULL, SE_TIME_ZONE_NAME, &TokenPrivileges.Privileges[0].Luid)) { TokenPrivileges.PrivilegeCount = 1; TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(Token, FALSE, &TokenPrivileges, 0, (PTOKEN_PRIVILEGES) NULL, 0); } MemorySizeStr = (CHAR16 *) PcdGetPtr (PcdEmuMemorySize); FirmwareVolumesStr = (CHAR16 *) PcdGetPtr (PcdEmuFirmwareVolume); SecPrint ("\nEDK II WIN Host Emulation Environment from http://www.tianocore.org/edk2/\n"); // // Determine the first thread available to this process. // if (GetProcessAffinityMask (GetCurrentProcess (), &ProcessAffinityMask, &SystemAffinityMask)) { LowBit = (INT32)LowBitSet32 (ProcessAffinityMask); if (LowBit != -1) { // // Force the system to bind the process to a single thread to work // around odd semaphore type crashes. // SetProcessAffinityMask (GetCurrentProcess (), (INTN)(BIT0 << LowBit)); } } // // Make some Windows calls to Set the process to the highest priority in the // idle class. We need this to have good performance. // SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS); SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST); SecInitializeThunk (); // // PPIs pased into PEI_CORE // AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuThunkPpiGuid, &mSecEmuThunkPpi); // // Emulator Bus Driver Thunks // AddThunkProtocol (&mWinNtWndThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuGop), TRUE); AddThunkProtocol (&mWinNtFileSystemThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuFileSystem), TRUE); AddThunkProtocol (&mWinNtBlockIoThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuVirtualDisk), TRUE); // // Allocate space for gSystemMemory Array // gSystemMemoryCount = CountSeparatorsInString (MemorySizeStr, '!') + 1; gSystemMemory = calloc (gSystemMemoryCount, sizeof (NT_SYSTEM_MEMORY)); if (gSystemMemory == NULL) { SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n", MemorySizeStr); exit (1); } // // Allocate space for gSystemMemory Array // gFdInfoCount = CountSeparatorsInString (FirmwareVolumesStr, '!') + 1; gFdInfo = calloc (gFdInfoCount, sizeof (NT_FD_INFO)); if (gFdInfo == NULL) { SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n", FirmwareVolumesStr); exit (1); } // // Setup Boot Mode. // SecPrint (" BootMode 0x%02x\n", PcdGet32 (PcdEmuBootMode)); // // Allocate 128K memory to emulate temp memory for PEI. // on a real platform this would be SRAM, or using the cache as RAM. // Set TemporaryRam to zero so WinNtOpenFile will allocate a new mapping // TemporaryRamSize = TEMPORARY_RAM_SIZE; TemporaryRam = VirtualAlloc (NULL, (SIZE_T) (TemporaryRamSize), MEM_COMMIT, PAGE_EXECUTE_READWRITE); if (TemporaryRam == NULL) { SecPrint ("ERROR : Can not allocate enough space for SecStack\n"); exit (1); } SetMem32 (TemporaryRam, TemporaryRamSize, PcdGet32 (PcdInitValueInTempStack)); SecPrint (" OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n", TemporaryRamSize / SIZE_1KB, TemporaryRam ); // // If enabled use the magic page to communicate between modules // This replaces the PI PeiServicesTable pointer mechanism that // deos not work in the emulator. It also allows the removal of // writable globals from SEC, PEI_CORE (libraries), PEIMs // EmuMagicPage = (VOID *)(UINTN)(FixedPcdGet64 (PcdPeiServicesTablePage) & MAX_UINTN); if (EmuMagicPage != NULL) { UINT64 Size; Status = WinNtOpenFile ( NULL, SIZE_4KB, 0, &EmuMagicPage, &Size ); if (EFI_ERROR (Status)) { SecPrint ("ERROR : Could not allocate PeiServicesTablePage @ %p\n", EmuMagicPage); return EFI_DEVICE_ERROR; } } // // Open All the firmware volumes and remember the info in the gFdInfo global // Meanwhile, find the SEC Core. // FileNamePtr = AllocateCopyPool (StrSize (FirmwareVolumesStr), FirmwareVolumesStr); if (FileNamePtr == NULL) { SecPrint ("ERROR : Can not allocate memory for firmware volume string\n"); exit (1); } for (Done = FALSE, Index = 0, SecFile = NULL; !Done; Index++) { FileName = FileNamePtr; for (Index1 = 0; (FileNamePtr[Index1] != '!') && (FileNamePtr[Index1] != 0); Index1++) ; if (FileNamePtr[Index1] == 0) { Done = TRUE; } else { FileNamePtr[Index1] = '\0'; FileNamePtr = &FileNamePtr[Index1 + 1]; } // // Open the FD and remember where it got mapped into our processes address space // Status = WinNtOpenFile ( FileName, 0, OPEN_EXISTING, &gFdInfo[Index].Address, &gFdInfo[Index].Size ); if (EFI_ERROR (Status)) { SecPrint ("ERROR : Can not open Firmware Device File %S (0x%X). Exiting.\n", FileName, Status); exit (1); } SecPrint (" FD loaded from %S\n", FileName); if (SecFile == NULL) { // // Assume the beginning of the FD is an FV and look for the SEC Core. // Load the first one we find. // FileHandle = NULL; Status = PeiServicesFfsFindNextFile ( EFI_FV_FILETYPE_SECURITY_CORE, (EFI_PEI_FV_HANDLE)gFdInfo[Index].Address, &FileHandle ); if (!EFI_ERROR (Status)) { Status = PeiServicesFfsFindSectionData (EFI_SECTION_PE32, FileHandle, &SecFile); if (!EFI_ERROR (Status)) { SecPrint (" contains SEC Core"); } } } SecPrint ("\n"); } // // Calculate memory regions and store the information in the gSystemMemory // global for later use. The autosizing code will use this data to // map this memory into the SEC process memory space. // for (Index = 0, Done = FALSE; !Done; Index++) { // // Save the size of the memory and make a Unicode filename SystemMemory00, ... // gSystemMemory[Index].Size = _wtoi (MemorySizeStr) * SIZE_1MB; // // Find the next region // for (Index1 = 0; MemorySizeStr[Index1] != '!' && MemorySizeStr[Index1] != 0; Index1++) ; if (MemorySizeStr[Index1] == 0) { Done = TRUE; } MemorySizeStr = MemorySizeStr + Index1 + 1; } SecPrint ("\n"); // // Hand off to SEC Core // SecLoadSecCore ((UINTN)TemporaryRam, TemporaryRamSize, gFdInfo[0].Address, gFdInfo[0].Size, SecFile); // // If we get here, then the SEC Core returned. This is an error as SEC should // always hand off to PEI Core and then on to DXE Core. // SecPrint ("ERROR : SEC returned\n"); exit (1); }