X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FUniversal%2FAcpi%2FBootScriptExecutorDxe%2FScriptExecute.c;h=22d434914befa9c96700791af381fda4276ee1d2;hp=c5eec24e0e3df8317ee27ddbe810b8cd7950ac54;hb=ab1a5a58c95937998f3ecf384a8940130511e234;hpb=bad73446684b9528c7e14ca1a372ac89c7af15bb diff --git a/MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/ScriptExecute.c b/MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/ScriptExecute.c index c5eec24e0e..22d434914b 100644 --- a/MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/ScriptExecute.c +++ b/MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/ScriptExecute.c @@ -1,10 +1,11 @@ /** @file This is the code for Boot Script Executer module. - This driver is dispatched by Dxe core and the driver will reload itself to ACPI NVS memory + This driver is dispatched by Dxe core and the driver will reload itself to ACPI reserved memory in the entry point. The functionality is to interpret and restore the S3 boot script -Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.
+Copyright (c) 2017, AMD Incorporated. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License @@ -22,6 +23,9 @@ EFI_GUID mBootScriptExecutorImageGuid = { 0x9a8d3433, 0x9fe8, 0x42b6, { 0x87, 0xb, 0x1e, 0x31, 0xc8, 0x4e, 0xbe, 0x3b } }; +BOOLEAN mPage1GSupport = FALSE; +UINT64 mAddressEncMask = 0; + /** Entry function of Boot script exector. This function will be executed in S3 boot path. @@ -68,6 +72,16 @@ S3BootScriptExecutorEntryFunction ( // for that parameter. // Status = S3BootScriptExecute (); + + // + // If invalid script table or opcode in S3 boot script table. + // + ASSERT_EFI_ERROR (Status); + + if (EFI_ERROR (Status)) { + CpuDeadLoop (); + return Status; + } AsmWbinvd (); @@ -195,6 +209,174 @@ S3BootScriptExecutorEntryFunction ( CpuDeadLoop(); return EFI_UNSUPPORTED; } + +/** + Register image to memory profile. + + @param FileName File name of the image. + @param ImageBase Image base address. + @param ImageSize Image size. + @param FileType File type of the image. + +**/ +VOID +RegisterMemoryProfileImage ( + IN EFI_GUID *FileName, + IN PHYSICAL_ADDRESS ImageBase, + IN UINT64 ImageSize, + IN EFI_FV_FILETYPE FileType + ) +{ + EFI_STATUS Status; + EDKII_MEMORY_PROFILE_PROTOCOL *ProfileProtocol; + MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FilePath; + UINT8 TempBuffer[sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH) + sizeof (EFI_DEVICE_PATH_PROTOCOL)]; + + if ((PcdGet8 (PcdMemoryProfilePropertyMask) & BIT0) != 0) { + + FilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)TempBuffer; + Status = gBS->LocateProtocol (&gEdkiiMemoryProfileGuid, NULL, (VOID **) &ProfileProtocol); + if (!EFI_ERROR (Status)) { + EfiInitializeFwVolDevicepathNode (FilePath, FileName); + SetDevicePathEndNode (FilePath + 1); + + Status = ProfileProtocol->RegisterImage ( + ProfileProtocol, + (EFI_DEVICE_PATH_PROTOCOL *) FilePath, + ImageBase, + ImageSize, + FileType + ); + } + } +} + +/** + This is the Event notification function to reload BootScriptExecutor image + to RESERVED mem and save it to LockBox. + + @param Event Pointer to this event + @param Context Event handler private data + **/ +VOID +EFIAPI +ReadyToLockEventNotify ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + VOID *Interface; + UINT8 *Buffer; + UINTN BufferSize; + EFI_HANDLE NewImageHandle; + UINTN Pages; + EFI_PHYSICAL_ADDRESS FfsBuffer; + PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; + + Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface); + if (EFI_ERROR (Status)) { + return; + } + + // + // A workaround: Here we install a dummy handle + // + NewImageHandle = NULL; + Status = gBS->InstallProtocolInterface ( + &NewImageHandle, + &gEfiCallerIdGuid, + EFI_NATIVE_INTERFACE, + NULL + ); + ASSERT_EFI_ERROR (Status); + + // + // Reload BootScriptExecutor image itself to RESERVED mem + // + Status = GetSectionFromAnyFv ( + &gEfiCallerIdGuid, + EFI_SECTION_PE32, + 0, + (VOID **) &Buffer, + &BufferSize + ); + ASSERT_EFI_ERROR (Status); + ImageContext.Handle = Buffer; + ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory; + // + // Get information about the image being loaded + // + Status = PeCoffLoaderGetImageInfo (&ImageContext); + ASSERT_EFI_ERROR (Status); + if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) { + Pages = EFI_SIZE_TO_PAGES ((UINTN) (ImageContext.ImageSize + ImageContext.SectionAlignment)); + } else { + Pages = EFI_SIZE_TO_PAGES ((UINTN) ImageContext.ImageSize); + } + FfsBuffer = 0xFFFFFFFF; + Status = gBS->AllocatePages ( + AllocateMaxAddress, + EfiReservedMemoryType, + Pages, + &FfsBuffer + ); + ASSERT_EFI_ERROR (Status); + ImageContext.ImageAddress = (PHYSICAL_ADDRESS)(UINTN)FfsBuffer; + // + // Align buffer on section boundary + // + ImageContext.ImageAddress += ImageContext.SectionAlignment - 1; + ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1)); + // + // Load the image to our new buffer + // + Status = PeCoffLoaderLoadImage (&ImageContext); + ASSERT_EFI_ERROR (Status); + + // + // Relocate the image in our new buffer + // + Status = PeCoffLoaderRelocateImage (&ImageContext); + ASSERT_EFI_ERROR (Status); + + // + // Free the buffer allocated by ReadSection since the image has been relocated in the new buffer + // + gBS->FreePool (Buffer); + + // + // Flush the instruction cache so the image data is written before we execute it + // + InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize); + + RegisterMemoryProfileImage ( + &gEfiCallerIdGuid, + ImageContext.ImageAddress, + ImageContext.ImageSize, + EFI_FV_FILETYPE_DRIVER + ); + + Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)(ImageContext.EntryPoint)) (NewImageHandle, gST); + ASSERT_EFI_ERROR (Status); + + // + // Additional step for BootScript integrity + // Save BootScriptExecutor image + // + Status = SaveLockBox ( + &mBootScriptExecutorImageGuid, + (VOID *)(UINTN)ImageContext.ImageAddress, + (UINTN)ImageContext.ImageSize + ); + ASSERT_EFI_ERROR (Status); + + Status = SetLockBoxAttributes (&mBootScriptExecutorImageGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE); + ASSERT_EFI_ERROR (Status); + + gBS->CloseEvent (Event); +} + /** Entrypoint of Boot script exector driver, this function will be executed in normal boot phase and invoked by DXE dispatch. @@ -212,16 +394,25 @@ BootScriptExecutorEntryPoint ( IN EFI_SYSTEM_TABLE *SystemTable ) { - UINT8 *Buffer; UINTN BufferSize; UINTN Pages; - EFI_PHYSICAL_ADDRESS FfsBuffer; - PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; BOOT_SCRIPT_EXECUTOR_VARIABLE *EfiBootScriptExecutorVariable; EFI_PHYSICAL_ADDRESS BootScriptExecutorBuffer; EFI_STATUS Status; VOID *DevicePath; - EFI_HANDLE NewImageHandle; + EFI_EVENT ReadyToLockEvent; + VOID *Registration; + UINT32 RegEax; + UINT32 RegEdx; + + if (!PcdGetBool (PcdAcpiS3Enable)) { + return EFI_UNSUPPORTED; + } + + // + // Make sure AddressEncMask is contained to smallest supported address field. + // + mAddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64; // // Test if the gEfiCallerIdGuid of this image is already installed. if not, the entry @@ -230,94 +421,32 @@ BootScriptExecutorEntryPoint ( // Status = gBS->LocateProtocol (&gEfiCallerIdGuid, NULL, &DevicePath); if (EFI_ERROR (Status)) { - - // - // This is the first-time loaded by DXE core. reload itself to RESERVED mem - // - // - // A workaround: Here we install a dummy handle - // - NewImageHandle = NULL; - Status = gBS->InstallProtocolInterface ( - &NewImageHandle, - &gEfiCallerIdGuid, - EFI_NATIVE_INTERFACE, - NULL - ); - ASSERT_EFI_ERROR (Status); - - Status = GetSectionFromAnyFv ( - &gEfiCallerIdGuid, - EFI_SECTION_PE32, - 0, - (VOID **) &Buffer, - &BufferSize - ); - ASSERT_EFI_ERROR (Status); - ImageContext.Handle = Buffer; - ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory; - // - // Get information about the image being loaded - // - Status = PeCoffLoaderGetImageInfo (&ImageContext); - ASSERT_EFI_ERROR (Status); - Pages = EFI_SIZE_TO_PAGES(BufferSize + ImageContext.SectionAlignment); - FfsBuffer = 0xFFFFFFFF; - Status = gBS->AllocatePages ( - AllocateMaxAddress, - EfiReservedMemoryType, - Pages, - &FfsBuffer - ); - ASSERT_EFI_ERROR (Status); - ImageContext.ImageAddress = (PHYSICAL_ADDRESS)(UINTN)FfsBuffer; - // - // Align buffer on section boundry - // - ImageContext.ImageAddress += ImageContext.SectionAlignment - 1; - ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1); - // - // Load the image to our new buffer - // - Status = PeCoffLoaderLoadImage (&ImageContext); - ASSERT_EFI_ERROR (Status); - - // - // Relocate the image in our new buffer - // - Status = PeCoffLoaderRelocateImage (&ImageContext); - ASSERT_EFI_ERROR (Status); - - // - // Free the buffer allocated by ReadSection since the image has been relocated in the new buffer - // - gBS->FreePool (Buffer); - // - // Flush the instruction cache so the image data is written before we execute it + // Create ReadyToLock event to reload BootScriptExecutor image + // to RESERVED mem and save it to LockBox. // - InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize); - Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)(ImageContext.EntryPoint)) (NewImageHandle, SystemTable); - ASSERT_EFI_ERROR (Status); - - // - // Additional step for BootScript integrity - // Save BootScriptExecutor image - // - Status = SaveLockBox ( - &mBootScriptExecutorImageGuid, - (VOID *)(UINTN)ImageContext.ImageAddress, - (UINTN)ImageContext.ImageSize - ); - ASSERT_EFI_ERROR (Status); - - Status = SetLockBoxAttributes (&mBootScriptExecutorImageGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE); - ASSERT_EFI_ERROR (Status); - + ReadyToLockEvent = EfiCreateProtocolNotifyEvent ( + &gEfiDxeSmmReadyToLockProtocolGuid, + TPL_NOTIFY, + ReadyToLockEventNotify, + NULL, + &Registration + ); + ASSERT (ReadyToLockEvent != NULL); } else { // // the entry point is invoked after reloading. following code only run in RESERVED mem // + if (PcdGetBool(PcdUse1GPageTable)) { + AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL); + if (RegEax >= 0x80000001) { + AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx); + if ((RegEdx & BIT26) != 0) { + mPage1GSupport = TRUE; + } + } + } + BufferSize = sizeof (BOOT_SCRIPT_EXECUTOR_VARIABLE); BootScriptExecutorBuffer = 0xFFFFFFFF; @@ -353,11 +482,8 @@ BootScriptExecutorEntryPoint ( Status = SetLockBoxAttributes (&gEfiBootScriptExecutorContextGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE); ASSERT_EFI_ERROR (Status); - } return EFI_SUCCESS; } - -