X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FUniversal%2FCapsulePei%2FUefiCapsule.c;h=36b04e34064b1caec15d86b4fffb23e9bb6532af;hp=e60105b31caa69d719565fa3f0a89f3093059a83;hb=9d510e61fceee7b92955ef9a3c20343752d8ce3f;hpb=359cb1a3b91fe6a31213fc213ee7d630a40f399b diff --git a/MdeModulePkg/Universal/CapsulePei/UefiCapsule.c b/MdeModulePkg/Universal/CapsulePei/UefiCapsule.c index e60105b31c..36b04e3406 100644 --- a/MdeModulePkg/Universal/CapsulePei/UefiCapsule.c +++ b/MdeModulePkg/Universal/CapsulePei/UefiCapsule.c @@ -1,16 +1,10 @@ /** @file Capsule update PEIM for UEFI2.0 -Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2018, 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 which accompanies this distribution. The -full text of the license may be found at -http://opensource.org/licenses/bsd-license.php - -THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +SPDX-License-Identifier: BSD-2-Clause-Patent **/ @@ -41,6 +35,7 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST IA32_DESCRIPTOR mGdt = { (UINTN) mGdtEntries }; + /** The function will check if 1G page is supported. @@ -131,7 +126,7 @@ Create4GPageTables ( IN EFI_PHYSICAL_ADDRESS PageTablesAddress, IN BOOLEAN Page1GSupport ) -{ +{ UINT8 PhysicalAddressBits; EFI_PHYSICAL_ADDRESS PageAddress; UINTN IndexOfPml4Entries; @@ -145,6 +140,12 @@ Create4GPageTables ( PAGE_TABLE_ENTRY *PageDirectoryEntry; UINTN BigPageAddress; PAGE_TABLE_1G_ENTRY *PageDirectory1GEntry; + UINT64 AddressEncMask; + + // + // Make sure AddressEncMask is contained to smallest supported address field. + // + AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64; // // Create 4G page table by default, @@ -164,7 +165,7 @@ Create4GPageTables ( } // - // Pre-allocate big pages to avoid later allocations. + // Pre-allocate big pages to avoid later allocations. // BigPageAddress = (UINTN) PageTablesAddress; @@ -187,18 +188,18 @@ Create4GPageTables ( // // Make a PML4 Entry // - PageMapLevel4Entry->Uint64 = (UINT64)(UINTN)PageDirectoryPointerEntry; + PageMapLevel4Entry->Uint64 = (UINT64)(UINTN)PageDirectoryPointerEntry | AddressEncMask; PageMapLevel4Entry->Bits.ReadWrite = 1; PageMapLevel4Entry->Bits.Present = 1; if (Page1GSupport) { PageDirectory1GEntry = (VOID *) PageDirectoryPointerEntry; - + for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress += SIZE_1GB) { // // Fill in the Page Directory entries // - PageDirectory1GEntry->Uint64 = (UINT64)PageAddress; + PageDirectory1GEntry->Uint64 = (UINT64)PageAddress | AddressEncMask; PageDirectory1GEntry->Bits.ReadWrite = 1; PageDirectory1GEntry->Bits.Present = 1; PageDirectory1GEntry->Bits.MustBe1 = 1; @@ -208,14 +209,14 @@ Create4GPageTables ( // // Each Directory Pointer entries points to a page of Page Directory entires. // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop. - // + // PageDirectoryEntry = (VOID *) BigPageAddress; BigPageAddress += SIZE_4KB; // // Fill in a Page Directory Pointer Entries // - PageDirectoryPointerEntry->Uint64 = (UINT64)(UINTN)PageDirectoryEntry; + PageDirectoryPointerEntry->Uint64 = (UINT64)(UINTN)PageDirectoryEntry | AddressEncMask; PageDirectoryPointerEntry->Bits.ReadWrite = 1; PageDirectoryPointerEntry->Bits.Present = 1; @@ -223,7 +224,7 @@ Create4GPageTables ( // // Fill in the Page Directory entries // - PageDirectoryEntry->Uint64 = (UINT64)PageAddress; + PageDirectoryEntry->Uint64 = (UINT64)PageAddress | AddressEncMask; PageDirectoryEntry->Bits.ReadWrite = 1; PageDirectoryEntry->Bits.Present = 1; PageDirectoryEntry->Bits.MustBe1 = 1; @@ -321,6 +322,14 @@ Thunk32To64 ( // AsmWriteCr3 ((UINTN) PageTableAddress); + DEBUG (( + DEBUG_INFO, + "%a() Stack Base: 0x%lx, Stack Size: 0x%lx\n", + __FUNCTION__, + Context->StackBufferBase, + Context->StackBufferLength + )); + // // Disable interrupt of Debug timer, since the IDT table cannot work in long mode // @@ -344,7 +353,7 @@ Thunk32To64 ( if ((UINTN) ReturnContext->ReturnStatus != 0) { Status = ENCODE_ERROR ((UINTN) ReturnContext->ReturnStatus); } - + return Status; } @@ -385,7 +394,7 @@ ModeSwitch ( ZeroMem (&Context, sizeof (SWITCH_32_TO_64_CONTEXT)); ZeroMem (&ReturnContext, sizeof (SWITCH_64_TO_32_CONTEXT)); - + MemoryBase64 = (UINT64) (UINTN) *MemoryBase; MemorySize64 = (UINT64) (UINTN) *MemorySize; MemoryEnd64 = MemoryBase64 + MemorySize64; @@ -393,7 +402,7 @@ ModeSwitch ( Page1GSupport = IsPage1GSupport (); // - // Merge memory range reserved for stack and page table + // Merge memory range reserved for stack and page table // if (LongModeBuffer->StackBaseAddress < LongModeBuffer->PageTableAddress) { ReservedRangeBase = LongModeBuffer->StackBaseAddress; @@ -435,6 +444,7 @@ ModeSwitch ( Context.MemoryBase64Ptr = (EFI_PHYSICAL_ADDRESS)(UINTN)&MemoryBase64; Context.MemorySize64Ptr = (EFI_PHYSICAL_ADDRESS)(UINTN)&MemorySize64; Context.Page1GSupport = Page1GSupport; + Context.AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64; // // Prepare data for return back @@ -445,14 +455,14 @@ ModeSwitch ( // Will save the return status of processing capsule // ReturnContext.ReturnStatus = 0; - + // // Save original GDT // AsmReadGdtr ((IA32_DESCRIPTOR *)&ReturnContext.Gdtr); - + Status = Thunk32To64 (LongModeBuffer->PageTableAddress, &Context, &ReturnContext); - + if (!EFI_ERROR (Status)) { *MemoryBase = (VOID *) (UINTN) MemoryBase64; *MemorySize = (UINTN) MemorySize64; @@ -607,6 +617,82 @@ GetPhysicalAddressBits ( } #endif +/** + Sort memory resource entries based upon PhysicalStart, from low to high. + + @param[in, out] MemoryResource A pointer to the memory resource entry buffer. + +**/ +VOID +SortMemoryResourceDescriptor ( + IN OUT MEMORY_RESOURCE_DESCRIPTOR *MemoryResource + ) +{ + MEMORY_RESOURCE_DESCRIPTOR *MemoryResourceEntry; + MEMORY_RESOURCE_DESCRIPTOR *NextMemoryResourceEntry; + MEMORY_RESOURCE_DESCRIPTOR TempMemoryResource; + + MemoryResourceEntry = MemoryResource; + NextMemoryResourceEntry = MemoryResource + 1; + while (MemoryResourceEntry->ResourceLength != 0) { + while (NextMemoryResourceEntry->ResourceLength != 0) { + if (MemoryResourceEntry->PhysicalStart > NextMemoryResourceEntry->PhysicalStart) { + CopyMem (&TempMemoryResource, MemoryResourceEntry, sizeof (MEMORY_RESOURCE_DESCRIPTOR)); + CopyMem (MemoryResourceEntry, NextMemoryResourceEntry, sizeof (MEMORY_RESOURCE_DESCRIPTOR)); + CopyMem (NextMemoryResourceEntry, &TempMemoryResource, sizeof (MEMORY_RESOURCE_DESCRIPTOR)); + } + + NextMemoryResourceEntry = NextMemoryResourceEntry + 1; + } + + MemoryResourceEntry = MemoryResourceEntry + 1; + NextMemoryResourceEntry = MemoryResourceEntry + 1; + } +} + +/** + Merge continous memory resource entries. + + @param[in, out] MemoryResource A pointer to the memory resource entry buffer. + +**/ +VOID +MergeMemoryResourceDescriptor ( + IN OUT MEMORY_RESOURCE_DESCRIPTOR *MemoryResource + ) +{ + MEMORY_RESOURCE_DESCRIPTOR *MemoryResourceEntry; + MEMORY_RESOURCE_DESCRIPTOR *NewMemoryResourceEntry; + MEMORY_RESOURCE_DESCRIPTOR *NextMemoryResourceEntry; + MEMORY_RESOURCE_DESCRIPTOR *MemoryResourceEnd; + + MemoryResourceEntry = MemoryResource; + NewMemoryResourceEntry = MemoryResource; + while (MemoryResourceEntry->ResourceLength != 0) { + CopyMem (NewMemoryResourceEntry, MemoryResourceEntry, sizeof (MEMORY_RESOURCE_DESCRIPTOR)); + NextMemoryResourceEntry = MemoryResourceEntry + 1; + + while ((NextMemoryResourceEntry->ResourceLength != 0) && + (NextMemoryResourceEntry->PhysicalStart == (MemoryResourceEntry->PhysicalStart + MemoryResourceEntry->ResourceLength))) { + MemoryResourceEntry->ResourceLength += NextMemoryResourceEntry->ResourceLength; + if (NewMemoryResourceEntry != MemoryResourceEntry) { + NewMemoryResourceEntry->ResourceLength += NextMemoryResourceEntry->ResourceLength; + } + + NextMemoryResourceEntry = NextMemoryResourceEntry + 1; + } + + MemoryResourceEntry = NextMemoryResourceEntry; + NewMemoryResourceEntry = NewMemoryResourceEntry + 1; + } + + // + // Set NULL terminate memory resource descriptor after merging. + // + MemoryResourceEnd = NewMemoryResourceEntry; + ZeroMem (MemoryResourceEnd, sizeof (MEMORY_RESOURCE_DESCRIPTOR)); +} + /** Build memory resource descriptor from resource descriptor in HOB list. @@ -650,7 +736,7 @@ BuildMemoryResourceDescriptor ( Status = PeiServicesAllocatePool ((1 + 1) * sizeof (MEMORY_RESOURCE_DESCRIPTOR), (VOID **) &MemoryResource); ASSERT_EFI_ERROR (Status); ZeroMem (MemoryResource, (1 + 1) * sizeof (MEMORY_RESOURCE_DESCRIPTOR)); - + MemoryResource[0].PhysicalStart = 0; MemoryResource[0].ResourceLength = LShiftU64 (1, GetPhysicalAddressBits ()); DEBUG ((EFI_D_INFO, "MemoryResource[0x0] - Start(0x%0lx) Length(0x%0lx)\n", @@ -687,6 +773,20 @@ BuildMemoryResourceDescriptor ( Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw); } + SortMemoryResourceDescriptor (MemoryResource); + MergeMemoryResourceDescriptor (MemoryResource); + + DEBUG ((DEBUG_INFO, "Dump MemoryResource[] after sorted and merged\n")); + for (Index = 0; MemoryResource[Index].ResourceLength != 0; Index++) { + DEBUG (( + DEBUG_INFO, + " MemoryResource[0x%x] - Start(0x%0lx) Length(0x%0lx)\n", + Index, + MemoryResource[Index].PhysicalStart, + MemoryResource[Index].ResourceLength + )); + } + return MemoryResource; } @@ -721,7 +821,7 @@ GetCapsuleDescriptors ( CapsuleVarName[0] = 0; ValidIndex = 0; CapsuleDataPtr64 = 0; - + Status = PeiServicesLocatePpi ( &gEfiPeiReadOnlyVariable2PpiGuid, 0, @@ -746,7 +846,7 @@ GetCapsuleDescriptors ( (VOID *) &CapsuleDataPtr64 ); if (EFI_ERROR (Status)) { - DEBUG ((EFI_D_ERROR, "Capsule -- capsule variable not set\n")); + DEBUG ((DEBUG_INFO, "Capsule -- capsule variable not set\n")); return EFI_NOT_FOUND; } // @@ -760,7 +860,13 @@ GetCapsuleDescriptors ( return EFI_SUCCESS; } } else { - UnicodeValueToString (TempVarName, 0, Index, 0); + UnicodeValueToStringS ( + TempVarName, + sizeof (CapsuleVarName) - ((UINTN)TempVarName - (UINTN)CapsuleVarName), + 0, + Index, + 0 + ); Status = PPIVariableServices->GetVariable ( PPIVariableServices, CapsuleVarName, @@ -772,7 +878,7 @@ GetCapsuleDescriptors ( if (EFI_ERROR (Status)) { break; } - + // // If this BlockList has been linked before, skip this variable // @@ -788,7 +894,7 @@ GetCapsuleDescriptors ( continue; } } - + // // Cache BlockList which has been processed // @@ -796,7 +902,7 @@ GetCapsuleDescriptors ( Index ++; } } - + return EFI_SUCCESS; } @@ -836,7 +942,7 @@ CapsuleCoalesce ( UINTN VariableCount; CHAR16 CapsuleVarName[30]; CHAR16 *TempVarName; - EFI_PHYSICAL_ADDRESS CapsuleDataPtr64; + EFI_PHYSICAL_ADDRESS CapsuleDataPtr64; EFI_STATUS Status; EFI_BOOT_MODE BootMode; EFI_PEI_READ_ONLY_VARIABLE2_PPI *PPIVariableServices; @@ -860,11 +966,11 @@ CapsuleCoalesce ( // Status = PeiServicesGetBootMode (&BootMode); if (EFI_ERROR (Status) || (BootMode != BOOT_ON_FLASH_UPDATE)) { - DEBUG ((EFI_D_ERROR, "Boot mode is not correct for capsule update path.\n")); + DEBUG ((EFI_D_ERROR, "Boot mode is not correct for capsule update path.\n")); Status = EFI_NOT_FOUND; goto Done; } - + // // User may set the same ScatterGatherList with several different variables, // so cache all ScatterGatherList for check later. @@ -883,7 +989,13 @@ CapsuleCoalesce ( TempVarName = CapsuleVarName + StrLen (CapsuleVarName); while (TRUE) { if (Index > 0) { - UnicodeValueToString (TempVarName, 0, Index, 0); + UnicodeValueToStringS ( + TempVarName, + sizeof (CapsuleVarName) - ((UINTN)TempVarName - (UINTN)CapsuleVarName), + 0, + Index, + 0 + ); } Status = PPIVariableServices->GetVariable ( PPIVariableServices, @@ -903,9 +1015,9 @@ CapsuleCoalesce ( VariableCount++; Index++; } - + DEBUG ((EFI_D_INFO,"Capsule variable count = %d\n", VariableCount)); - + // // The last entry is the end flag. // @@ -918,9 +1030,9 @@ CapsuleCoalesce ( DEBUG ((EFI_D_ERROR, "AllocatePages Failed!, Status = %x\n", Status)); goto Done; } - + ZeroMem (VariableArrayAddress, (VariableCount + 1) * sizeof (EFI_PHYSICAL_ADDRESS)); - + // // Find out if we actually have a capsule. // GetCapsuleDescriptors depends on variable PPI, so it should run in 32-bit environment. @@ -949,7 +1061,7 @@ CapsuleCoalesce ( Status = EFI_NOT_FOUND; goto Done; } - + Status = FindCapsuleCoalesceImage (&CoalesceImageEntryPoint, &CoalesceImageMachineType); if ((EFI_ERROR (Status)) || (CoalesceImageMachineType != EFI_IMAGE_MACHINE_X64)) { DEBUG ((EFI_D_ERROR, "Fail to find CapsuleX64 module in FV!\n")); @@ -971,13 +1083,13 @@ CapsuleCoalesce ( // Status = CapsuleDataCoalesce (PeiServices, (EFI_PHYSICAL_ADDRESS *)(UINTN)VariableArrayAddress, MemoryResource, MemoryBase, MemorySize); #endif - + DEBUG ((EFI_D_INFO, "Capsule Coalesce Status = %r!\n", Status)); if (Status == EFI_BUFFER_TOO_SMALL) { DEBUG ((EFI_D_ERROR, "There is not enough memory to process capsule!\n")); } - + if (Status == EFI_NOT_FOUND) { DEBUG ((EFI_D_ERROR, "Fail to parse capsule descriptor in memory!\n")); REPORT_STATUS_CODE ( @@ -1010,9 +1122,9 @@ CheckCapsuleUpdate ( return Status; } /** - This function will look at a capsule and determine if it's a test pattern. + This function will look at a capsule and determine if it's a test pattern. If it is, then it will verify it and emit an error message if corruption is detected. - + @param PeiServices Standard pei services pointer @param CapsuleBase Base address of coalesced capsule, which is preceeded by private data. Very implementation specific. @@ -1101,7 +1213,7 @@ CreateState ( UINT32 Index; EFI_PHYSICAL_ADDRESS BaseAddress; UINT64 Length; - + PrivateData = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) CapsuleBase; if (PrivateData->Signature != EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE) { return EFI_VOLUME_CORRUPTED; @@ -1157,7 +1269,7 @@ CreateState ( BuildCvHob (BaseAddress, Length); } - + return EFI_SUCCESS; }