/** @file
The logic to process capsule.
Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
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
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <Uefi.h>
#include <PiPei.h>
#include <Guid/CapsuleVendor.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/PrintLib.h>
typedef struct {
/**
Given a pointer to the capsule block list, info on the available system
memory, and the size of a buffer, find a free block of memory where a
buffer of the given size can be copied to safely.
@param BlockList Pointer to head of capsule block descriptors
@param MemBase Pointer to the base of memory in which we want to find free space
@param MemSize The size of the block of memory pointed to by MemBase
@param DataSize How big a free block we want to find
@return A pointer to a memory block of at least DataSize that lies somewhere
between MemBase and (MemBase + MemSize). The memory pointed to does not
contain any of the capsule block descriptors or capsule blocks pointed to
by the BlockList.
**/
UINT8 *
);
/**
Check the integrity of the capsule descriptors.
@param BlockList Pointer to the capsule descriptors
@retval NULL BlockList is not valid.
@retval LastBlockDesc Last one Block in BlockList
**/
);
/**
The capsule block descriptors may be fragmented and spread all over memory.
To simplify the coalescing of capsule blocks, first coalesce all the
capsule block descriptors low in memory.
The descriptors passed in can be fragmented throughout memory. Here
they are relocated into memory to turn them into a contiguous (null
terminated) array.
@param PeiServices pointer to PEI services table
@param BlockList pointer to the capsule block descriptors
@param MemBase base of system memory in which we can work
@param MemSize size of the system memory pointed to by MemBase
@retval NULL could not relocate the descriptors
@retval Pointer to the base of the successfully-relocated block descriptors.
**/
);
/**
Check every capsule header.
@param CapsuleHeader The pointer to EFI_CAPSULE_HEADER
@retval FALSE Capsule is OK
@retval TRUE Capsule is corrupted
**/
);
/**
Determine if two buffers overlap in memory.
@param Buff1 pointer to first buffer
@param Size1 size of Buff1
@param Buff2 pointer to second buffer
@param Size2 size of Buff2
@retval TRUE Buffers overlap in memory.
@retval FALSE Buffer doesn't overlap.
**/
);
/**
Given a pointer to a capsule block descriptor, traverse the list to figure
out how many legitimate descriptors there are, and how big the capsule it
refers to is.
@param Desc Pointer to the capsule block descriptors
NumDescriptors - optional pointer to where to return the number of descriptors
CapsuleSize - optional pointer to where to return the capsule size
@param NumDescriptors Optional pointer to where to return the number of descriptors
@param CapsuleSize Optional pointer to where to return the capsule size
@retval EFI_NOT_FOUND No descriptors containing data in the list
@retval EFI_SUCCESS Return data is valid
**/
);
/**
Given a pointer to the capsule block list, info on the available system
memory, and the size of a buffer, find a free block of memory where a
buffer of the given size can be copied to safely.
@param BlockList Pointer to head of capsule block descriptors
@param MemBase Pointer to the base of memory in which we want to find free space
@param MemSize The size of the block of memory pointed to by MemBase
@param DataSize How big a free block we want to find
@return A pointer to a memory block of at least DataSize that lies somewhere
between MemBase and (MemBase + MemSize). The memory pointed to does not
contain any of the capsule block descriptors or capsule blocks pointed to
by the BlockList.
**/
UINT8 *
)
{
//
// Need at least enough to copy the data to at the end of the buffer, so
// say the end is less the data size for easy comparisons here.
//
//
// Go through all the descriptor blocks and see if any obstruct the range
//
//
// Get the size of this block list and see if it's in the way
//
Size = sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
Size += sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
TempDesc++;
}
//
// Set our new base to the end of this block list and start all over
//
return NULL;
}
}
//
// Now go through all the blocks and make sure none are in the way
//
if (IsOverlapped (MemBase, DataSize, (UINT8 *) (UINTN) CurrDesc->Union.DataBlock, (UINTN) CurrDesc->Length)) {
//
// Set our new base to the end of this block and start all over
//
return NULL;
}
}
CurrDesc++;
}
//
// Normal continuation -- jump to next block descriptor list
//
if (!Failed) {
}
}
return MemBase;
}
/**
Check the integrity of the capsule descriptors.
@param BlockList Pointer to the capsule descriptors
@retval NULL BlockList is not valid.
@retval LastBlockDesc Last one Block in BlockList
**/
)
{
//
// Go through the list to look for inconsistencies. Check for:
// * misaligned block descriptors.
// * The first capsule header guid
// * The first capsule header flag
// * Data + Length < Data (wrap)
CapsuleSize = 0;
CapsuleCount = 0;
while ((Ptr->Length != 0) || (Ptr->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
//
// Make sure the descriptor is aligned at UINT64 in memory
//
return NULL;
}
//
// Descriptor points to another list of block descriptors somewhere
// else.
//
} else {
//
//To enhance the reliability of check-up, the first capsule's header is checked here.
//More reliabilities check-up will do later.
//
if (CapsuleSize == 0) {
//
//Move to the first capsule to check its header.
//
if (IsCapsuleCorrupted (CapsuleHeader)) {
return NULL;
}
CapsuleCount ++;
}
} else {
CapsuleSize = 0;
}
//
// Move to next BLOCK descriptor
//
Ptr++;
}
}
if ((CapsuleCount == 0) || (CapsuleSize != 0)) {
//
// No any capsule is found in BlockList or capsule data is corrupted.
//
return NULL;
}
return Ptr;
}
/**
The capsule block descriptors may be fragmented and spread all over memory.
To simplify the coalescing of capsule blocks, first coalesce all the
capsule block descriptors low in memory.
The descriptors passed in can be fragmented throughout memory. Here
they are relocated into memory to turn them into a contiguous (null
terminated) array.
@param PeiServices pointer to PEI services table
@param BlockList pointer to the capsule block descriptors
@param MemBase base of system memory in which we can work
@param MemSize size of the system memory pointed to by MemBase
@retval NULL could not relocate the descriptors
@retval Pointer to the base of the successfully-relocated block descriptors.
**/
)
{
//
// Get the info on the blocks and descriptors. Since we're going to move
// GetCapsuleInfo() returns the number of legit descriptors, so add one for
// a terminator.
//
return NULL;
}
if (MemSize < BufferSize) {
return NULL;
}
MemSize -= BufferSize;
MemBase += BufferSize;
//
// Go through all the blocks and make sure none are in the way
//
if (TempBlockDesc->Length == 0) {
//
// Next block of descriptors
//
} else {
//
// If the capsule data pointed to by this descriptor is in the way,
// move it.
//
if (IsOverlapped (
(UINT8 *) NewBlockList,
)) {
//
// Relocate the block
//
if (RelocBuffer == NULL) {
return NULL;
}
CopyMem ((VOID *) RelocBuffer, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);
DEBUG ((EFI_D_INFO, "Capsule relocate descriptors from/to/size 0x%X 0x%X 0x%X\n", (UINT32)(UINTN)TempBlockDesc->Union.DataBlock, (UINT32)(UINTN)RelocBuffer, (UINT32)(UINTN)TempBlockDesc->Length));
}
}
}
//
// Now go through all the block descriptors to make sure that they're not
// in the memory region we want to copy them to.
//
while ((CurrBlockDescHead != NULL) && (CurrBlockDescHead->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
//
// Get the size of this list then see if it overlaps our low region
//
BlockListSize = sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
while (TempBlockDesc->Length != 0) {
BlockListSize += sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);
}
if (IsOverlapped (
(UINT8 *) NewBlockList,
(UINT8 *) CurrBlockDescHead,
)) {
//
// Overlaps, so move it out of the way
//
if (RelocBuffer == NULL) {
return NULL;
}
//
// Point the previous block's next point to this copied version. If
// the tail pointer is null, then this is the first descriptor block.
//
if (PrevBlockDescTail == NULL) {
} else {
}
}
//
// Save our new tail and jump to the next block list
//
CurrBlockDescHead = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) (UINTN) TempBlockDesc->Union.ContinuationPointer;
}
//
// Cleared out low memory. Now copy the descriptors down there.
//
while ((TempBlockDesc != NULL) && (TempBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
if (TempBlockDesc->Length != 0) {
} else {
}
}
//
// Null terminate
//
CurrBlockDescHead->Length = 0;
return NewBlockList;
}
/**
Determine if two buffers overlap in memory.
@param Buff1 pointer to first buffer
@param Size1 size of Buff1
@param Buff2 pointer to second buffer
@param Size2 size of Buff2
@retval TRUE Buffers overlap in memory.
@retval FALSE Buffer doesn't overlap.
**/
)
{
//
// If buff1's end is less than the start of buff2, then it's ok.
// Also, if buff1's start is beyond buff2's end, then it's ok.
//
return FALSE;
}
return TRUE;
}
/**
Given a pointer to a capsule block descriptor, traverse the list to figure
out how many legitimate descriptors there are, and how big the capsule it
refers to is.
@param Desc Pointer to the capsule block descriptors
NumDescriptors - optional pointer to where to return the number of descriptors
CapsuleSize - optional pointer to where to return the capsule size
@param NumDescriptors Optional pointer to where to return the number of descriptors
@param CapsuleSize Optional pointer to where to return the capsule size
@retval EFI_NOT_FOUND No descriptors containing data in the list
@retval EFI_SUCCESS Return data is valid
**/
)
{
Count = 0;
Size = 0;
//
// Descriptor points to another list of block descriptors somewhere
//
} else {
Count++;
Desc++;
}
}
//
// If no descriptors, then fail
//
if (Count == 0) {
return EFI_NOT_FOUND;
}
if (NumDescriptors != NULL) {
*NumDescriptors = Count;
}
if (CapsuleSize != NULL) {
*CapsuleSize = Size;
}
return EFI_SUCCESS;
}
/**
Check every capsule header.
@param CapsuleHeader The pointer to EFI_CAPSULE_HEADER
@retval FALSE Capsule is OK
@retval TRUE Capsule is corrupted
**/
)
{
//
//A capsule to be updated across a system reset should contain CAPSULE_FLAGS_PERSIST_ACROSS_RESET.
//
return TRUE;
}
//
//Make sure the flags combination is supported by the platform.
//
if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
return TRUE;
}
if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {
return TRUE;
}
return FALSE;
}
/**
Try to verify the integrity of a capsule test pattern before the
capsule gets coalesced. This can be useful in narrowing down
where capsule data corruption occurs.
The test pattern mode fills in memory with a counting UINT32 value.
If the capsule is not divided up in a multiple of 4-byte blocks, then
things get messy doing the check. Therefore there are some cases
here where we just give up and skip the pre-coalesce check.
@param PeiServices PEI services table
@param Desc Pointer to capsule descriptors
**/
)
{
//
// Find first data descriptor
//
while ((Desc->Length == 0) && (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
}
return ;
}
//
// First one better be long enough to at least hold the test signature
//
return ;
}
//
// 0x54534554 "TEST"
//
if (*TestPtr != 0x54534554) {
return ;
}
TestCounter = 0;
//
// Skip over the signature and the size fields in the pattern data header
//
TestPtr += 2;
while (1) {
if ((TestSize & 0x03) != 0) {
return ;
}
while (TestSize > 0) {
if (*TestPtr != TestCounter) {
return ;
}
TestCounter++;
TestPtr++;
}
Desc++;
while ((Desc->Length == 0) && (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
}
return ;
}
}
}
/**
Checks for the presence of capsule descriptors.
Get capsule descriptors from variable CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
@param BlockListBuffer Pointer to the buffer of capsule descriptors variables
@param BlockDescriptorList Pointer to the capsule descriptors list
@retval EFI_SUCCESS a valid capsule is present
@retval EFI_NOT_FOUND if a valid capsule is not present
**/
)
{
Index = 0;
while (BlockListBuffer[Index] != 0) {
//
// Test integrity of descriptors.
//
TempBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index]);
//
// Return the base of the block descriptors
//
} else {
//
// Combine the different BlockList into single BlockList.
//
}
}
Index ++;
}
return EFI_SUCCESS;
}
return EFI_NOT_FOUND;
}
/**
The function to coalesce a fragmented capsule in memory.
Memory Map for coalesced capsule:
MemBase + ---->+---------------------------+<-----------+
MemSize | CapsuleOffset[49] | |
+---------------------------+ |
| ................ | |
+---------------------------+ |
| CapsuleOffset[2] | |
+---------------------------+ |
| CapsuleOffset[1] | |
+---------------------------+ |
| CapsuleOffset[0] | CapsuleSize
+---------------------------+ |
| CapsuleNumber | |
+---------------------------+ |
| | |
| | |
| Capsule Image | |
| | |
| | |
+---------------------------+ |
| PrivateData | |
DestPtr ----> +---------------------------+<-----------+
| | |
| FreeMem | FreeMemSize
| | |
FreeMemBase --->+---------------------------+<-----------+
| Terminator |
+---------------------------+
| BlockDescriptor n |
+---------------------------+
| ................. |
+---------------------------+
| BlockDescriptor 1 |
+---------------------------+
| BlockDescriptor 0 |
+---------------------------+
| PrivateDataDesc 0 |
MemBase ---->+---------------------------+<----- BlockList
@param PeiServices General purpose services available to every PEIM.
@param BlockListBuffer Point to the buffer of Capsule Descriptor Variables.
@param MemoryBase Pointer to the base of a block of memory that we can walk
all over while trying to coalesce our buffers.
On output, this variable will hold the base address of
a coalesced capsule.
@param MemorySize Size of the memory region pointed to by MemoryBase.
On output, this variable will contain the size of the
coalesced capsule.
@retval EFI_NOT_FOUND If we could not find the capsule descriptors.
@retval EFI_BUFFER_TOO_SMALL
If we could not coalesce the capsule in the memory
region provided to us.
@retval EFI_SUCCESS Processed the capsule successfully.
**/
)
{
CapsuleIndex = 0;
SizeLeft = 0;
CapsuleTimes = 0;
CapsuleImageSize = 0;
AddDataPtr = NULL;
IsCorrupted = TRUE;
CapsuleSize = 0;
NumDescriptors = 0;
//
// Build capsule descriptors list
//
return Status;
}
);
//
// Get the size of our descriptors and the capsule size. GetCapsuleInfo()
// returns the number of descriptors that actually point to data, so add
// one for a terminator. Do that below.
//
if ((CapsuleSize == 0) || (NumDescriptors == 0)) {
return EFI_NOT_FOUND;
}
//
// Initialize our local copy of private data. When we're done, we'll create a
// descriptor for it as well so that it can be put into free memory without
// trashing anything.
//
//
// In addition to PrivateDataDesc[1:0], one terminator is added
// See below RelocateBlockDescriptors()
//
NumDescriptors += 3;
//
// Don't go below some min address. If the base is below it,
// then move it up and adjust the size accordingly.
//
DEBUG ((EFI_D_INFO, "Capsule Memory range from 0x%8X to 0x%8X\n", (UINTN) *MemoryBase, (UINTN)*MemoryBase + *MemorySize));
return EFI_BUFFER_TOO_SMALL;
} else {
}
}
return EFI_BUFFER_TOO_SMALL;
}
DEBUG ((EFI_D_INFO, "Capsule Free Memory from 0x%8X to 0x%8X\n", (UINTN) FreeMemBase, (UINTN) FreeMemBase + FreeMemSize));
//
// Relocate all the block descriptors to low memory to make further
// processing easier.
//
//
// Not enough room to relocate the descriptors
//
return EFI_BUFFER_TOO_SMALL;
}
//
// Take the top of memory for the capsule. Naturally align.
//
//
// Move all the blocks to the top (high) of memory.
// Relocate all the obstructing blocks. Note that the block descriptors
// were coalesced when they were relocated, so we can just ++ the pointer.
//
while ((CurrentBlockDesc->Length != 0) || (CurrentBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {
//
// See if any of the remaining capsule blocks are in the way
//
while (TempBlockDesc->Length != 0) {
//
// Is this block in the way of where we want to copy the current descriptor to?
//
if (IsOverlapped (
)) {
//
// Relocate the block
//
return EFI_BUFFER_TOO_SMALL;
}
CopyMem ((VOID *) RelocPtr, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);
}
//
// Next descriptor
//
}
//
// Ok, we made it through. Copy the block.
// we just support greping one capsule from the lists of block descs list.
//
CapsuleTimes ++;
//
//Skip the first block descriptor that filled with EFI_CAPSULE_PEIM_PRIVATE_DATA
//
if (CapsuleTimes > 1) {
//
//For every capsule entry point, check its header to determine whether to relocate it.
//If it is invalid, skip it and move on to the next capsule. If it is valid, relocate it.
//
if (CapsuleBeginFlag) {
if (!IsCapsuleCorrupted (CapsuleHeader)) {
return EFI_BUFFER_TOO_SMALL;
}
//
// Relocate this valid capsule
//
IsCorrupted = FALSE;
//
// Cache the begin offset of this capsule
//
CapsuleOffset[CapsuleIndex++] = (UINT32) (UINTN) DestPtr - (UINT32)(UINTN)NewCapsuleBase - (UINT32)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA);
}
}
if (!IsCorrupted) {
CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) (CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);
DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,
}
} else {
//
//Here is the end of the current capsule image.
//
if (!IsCorrupted) {
DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,
}
//
// Start the next cycle
//
SizeLeft = 0;
IsCorrupted = TRUE;
}
} else {
//
//The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.
//
CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) CurrentBlockDesc->Union.DataBlock, (UINTN) CurrentBlockDesc->Length);
}
//
//Walk through the block descriptor list.
//
}
//
// We return the base of memory we want reserved, and the size.
// The memory peim should handle it appropriately from there.
//
//
//Append the offsets of mutiply capsules to the continous buffer
//
DataPtr = (VOID*)((UINTN)NewCapsuleBase + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (UINTN)CapsuleImageSize);
*AddDataPtr++ = CapsuleIndex;
return EFI_SUCCESS;
}