IntelSiliconPkg MicrocodeUpdateDxe: Honor FIT table

It is the second step for
https://bugzilla.tianocore.org/show_bug.cgi?id=540.

V2: Use error handling instead of ASSERT for FIT table checking result.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
This commit is contained in:
Star Zeng
2018-03-28 16:52:12 +08:00
parent 0edb7ec5ce
commit e91797885a
4 changed files with 601 additions and 29 deletions

View File

@ -368,7 +368,7 @@ GetMatchedProcessor (
On output, the index of target CPU which matches the Microcode.
@retval EFI_SUCCESS The Microcode image passes verification.
@retval EFI_VOLUME_CORRUPTED The Microcode image is corrupt.
@retval EFI_VOLUME_CORRUPTED The Microcode image is corrupted.
@retval EFI_INCOMPATIBLE_VERSION The Microcode image version is incorrect.
@retval EFI_UNSUPPORTED The Microcode ProcessorSignature or ProcessorFlags is incorrect.
@retval EFI_SECURITY_VIOLATION The Microcode image fails to load.
@ -550,7 +550,7 @@ VerifyMicrocode (
}
*LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INCORRECT_VERSION;
if (AbortReason != NULL) {
*AbortReason = AllocateCopyPool(sizeof(L"UnsupportedProcessSignature/ProcessorFlags"), L"UnsupportedProcessSignature/ProcessorFlags");
*AbortReason = AllocateCopyPool(sizeof(L"UnsupportedProcessorSignature/ProcessorFlags"), L"UnsupportedProcessorSignature/ProcessorFlags");
}
return EFI_UNSUPPORTED;
}
@ -622,6 +622,124 @@ GetNextMicrocode (
return NULL;
}
/**
Get next FIT Microcode entrypoint.
@param[in] MicrocodeFmpPrivate The Microcode driver private data
@param[in] MicrocodeEntryPoint Current Microcode entrypoint
@return next FIT Microcode entrypoint.
**/
CPU_MICROCODE_HEADER *
GetNextFitMicrocode (
IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate,
IN CPU_MICROCODE_HEADER *MicrocodeEntryPoint
)
{
UINTN Index;
for (Index = 0; Index < MicrocodeFmpPrivate->FitMicrocodeEntryCount; Index++) {
if (MicrocodeEntryPoint == MicrocodeFmpPrivate->FitMicrocodeInfo[Index].MicrocodeEntryPoint) {
if (Index == (UINTN) MicrocodeFmpPrivate->FitMicrocodeEntryCount - 1) {
// it is last one
return NULL;
} else {
// return next one
return MicrocodeFmpPrivate->FitMicrocodeInfo[Index + 1].MicrocodeEntryPoint;
}
}
}
ASSERT(FALSE);
return NULL;
}
/**
Find empty FIT Microcode entrypoint.
@param[in] MicrocodeFmpPrivate The Microcode driver private data
@param[in] ImageSize The size of Microcode image buffer in bytes.
@param[out] AvailableSize Available size of the empty FIT Microcode entrypoint.
@return Empty FIT Microcode entrypoint.
**/
CPU_MICROCODE_HEADER *
FindEmptyFitMicrocode (
IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate,
IN UINTN ImageSize,
OUT UINTN *AvailableSize
)
{
UINTN Index;
CPU_MICROCODE_HEADER *MicrocodeEntryPoint;
CPU_MICROCODE_HEADER *NextMicrocodeEntryPoint;
VOID *MicrocodePatchAddress;
UINTN MicrocodePatchRegionSize;
MicrocodePatchAddress = MicrocodeFmpPrivate->MicrocodePatchAddress;
MicrocodePatchRegionSize = MicrocodeFmpPrivate->MicrocodePatchRegionSize;
for (Index = 0; Index < MicrocodeFmpPrivate->FitMicrocodeEntryCount; Index++) {
if (MicrocodeFmpPrivate->FitMicrocodeInfo[Index].Empty) {
MicrocodeEntryPoint = MicrocodeFmpPrivate->FitMicrocodeInfo[Index].MicrocodeEntryPoint;
NextMicrocodeEntryPoint = GetNextFitMicrocode (MicrocodeFmpPrivate, MicrocodeEntryPoint);
if (NextMicrocodeEntryPoint != NULL) {
*AvailableSize = (UINTN) NextMicrocodeEntryPoint - (UINTN) MicrocodeEntryPoint;
} else {
*AvailableSize = (UINTN) MicrocodePatchAddress + MicrocodePatchRegionSize - (UINTN) MicrocodeEntryPoint;
}
if (*AvailableSize >= ImageSize) {
return MicrocodeEntryPoint;
}
}
}
return NULL;
}
/**
Find unused FIT Microcode entrypoint.
@param[in] MicrocodeFmpPrivate The Microcode driver private data
@param[in] ImageSize The size of Microcode image buffer in bytes.
@param[out] AvailableSize Available size of the unused FIT Microcode entrypoint.
@return Unused FIT Microcode entrypoint.
**/
CPU_MICROCODE_HEADER *
FindUnusedFitMicrocode (
IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate,
IN UINTN ImageSize,
OUT UINTN *AvailableSize
)
{
UINTN Index;
CPU_MICROCODE_HEADER *MicrocodeEntryPoint;
CPU_MICROCODE_HEADER *NextMicrocodeEntryPoint;
VOID *MicrocodePatchAddress;
UINTN MicrocodePatchRegionSize;
MicrocodePatchAddress = MicrocodeFmpPrivate->MicrocodePatchAddress;
MicrocodePatchRegionSize = MicrocodeFmpPrivate->MicrocodePatchRegionSize;
for (Index = 0; Index < MicrocodeFmpPrivate->FitMicrocodeEntryCount; Index++) {
if (!MicrocodeFmpPrivate->FitMicrocodeInfo[Index].InUse) {
MicrocodeEntryPoint = MicrocodeFmpPrivate->FitMicrocodeInfo[Index].MicrocodeEntryPoint;
NextMicrocodeEntryPoint = GetNextFitMicrocode (MicrocodeFmpPrivate, MicrocodeEntryPoint);
if (NextMicrocodeEntryPoint != NULL) {
*AvailableSize = (UINTN) NextMicrocodeEntryPoint - (UINTN) MicrocodeEntryPoint;
} else {
*AvailableSize = (UINTN) MicrocodePatchAddress + MicrocodePatchRegionSize - (UINTN) MicrocodeEntryPoint;
}
if (*AvailableSize >= ImageSize) {
return MicrocodeEntryPoint;
}
}
}
return NULL;
}
/**
Get current Microcode used region size.
@ -666,7 +784,7 @@ UpdateMicrocode (
DEBUG((DEBUG_INFO, "PlatformUpdate:"));
DEBUG((DEBUG_INFO, " Address - 0x%lx,", Address));
DEBUG((DEBUG_INFO, " Legnth - 0x%x\n", ImageSize));
DEBUG((DEBUG_INFO, " Length - 0x%x\n", ImageSize));
Status = MicrocodeFlashWrite (
Address,
@ -681,6 +799,201 @@ UpdateMicrocode (
return Status;
}
/**
Update Microcode flash region with FIT.
@param[in] MicrocodeFmpPrivate The Microcode driver private data
@param[in] TargetMicrocodeEntryPoint Target Microcode entrypoint to be updated
@param[in] Image The Microcode image buffer.
@param[in] ImageSize The size of Microcode image buffer in bytes.
@param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
@retval EFI_SUCCESS The Microcode image is written.
@retval EFI_WRITE_PROTECTED The flash device is read only.
**/
EFI_STATUS
UpdateMicrocodeFlashRegionWithFit (
IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate,
IN CPU_MICROCODE_HEADER *TargetMicrocodeEntryPoint,
IN VOID *Image,
IN UINTN ImageSize,
OUT UINT32 *LastAttemptStatus
)
{
VOID *MicrocodePatchAddress;
UINTN MicrocodePatchRegionSize;
UINTN TargetTotalSize;
EFI_STATUS Status;
VOID *MicrocodePatchScratchBuffer;
UINT8 *ScratchBufferPtr;
UINTN ScratchBufferSize;
UINTN RestSize;
UINTN AvailableSize;
VOID *NextMicrocodeEntryPoint;
VOID *EmptyFitMicrocodeEntry;
VOID *UnusedFitMicrocodeEntry;
DEBUG((DEBUG_INFO, "UpdateMicrocodeFlashRegionWithFit: Image - 0x%x, size - 0x%x\n", Image, ImageSize));
MicrocodePatchAddress = MicrocodeFmpPrivate->MicrocodePatchAddress;
MicrocodePatchRegionSize = MicrocodeFmpPrivate->MicrocodePatchRegionSize;
MicrocodePatchScratchBuffer = AllocateZeroPool (MicrocodePatchRegionSize);
if (MicrocodePatchScratchBuffer == NULL) {
DEBUG((DEBUG_ERROR, "Fail to allocate Microcode Scratch buffer\n"));
*LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES;
return EFI_OUT_OF_RESOURCES;
}
ScratchBufferPtr = MicrocodePatchScratchBuffer;
ScratchBufferSize = 0;
//
// Target data collection
//
TargetTotalSize = 0;
AvailableSize = 0;
if (TargetMicrocodeEntryPoint != NULL) {
if (TargetMicrocodeEntryPoint->DataSize == 0) {
TargetTotalSize = 2048;
} else {
TargetTotalSize = TargetMicrocodeEntryPoint->TotalSize;
}
DEBUG((DEBUG_INFO, " TargetTotalSize - 0x%x\n", TargetTotalSize));
NextMicrocodeEntryPoint = GetNextFitMicrocode (MicrocodeFmpPrivate, TargetMicrocodeEntryPoint);
DEBUG((DEBUG_INFO, " NextMicrocodeEntryPoint - 0x%x\n", NextMicrocodeEntryPoint));
if (NextMicrocodeEntryPoint != NULL) {
ASSERT ((UINTN) NextMicrocodeEntryPoint >= ((UINTN) TargetMicrocodeEntryPoint + TargetTotalSize));
AvailableSize = (UINTN) NextMicrocodeEntryPoint - (UINTN) TargetMicrocodeEntryPoint;
} else {
AvailableSize = (UINTN) MicrocodePatchAddress + MicrocodePatchRegionSize - (UINTN) TargetMicrocodeEntryPoint;
}
DEBUG((DEBUG_INFO, " AvailableSize - 0x%x\n", AvailableSize));
ASSERT (AvailableSize >= TargetTotalSize);
}
//
// Total Size means the Microcode size.
// Available Size means the Microcode size plus the pad till (1) next Microcode or (2) the end.
//
// (1)
// +------+-----------+-----+------+===================+
// | MCU1 | Microcode | PAD | MCU2 | Empty |
// +------+-----------+-----+------+===================+
// | TotalSize |
// |<-AvailableSize->|
//
// (2)
// +------+-----------+===================+
// | MCU | Microcode | Empty |
// +------+-----------+===================+
// | TotalSize |
// |<- AvailableSize ->|
//
//
// Update based on policy
//
//
// 1. If there is enough space to update old one in situ, replace old microcode in situ.
//
if (AvailableSize >= ImageSize) {
DEBUG((DEBUG_INFO, "Replace old microcode in situ\n"));
//
// +------+------------+------+===================+
// |Other | Old Image | ... | Empty |
// +------+------------+------+===================+
//
// +------+---------+--+------+===================+
// |Other |New Image|FF| ... | Empty |
// +------+---------+--+------+===================+
//
// 1.1. Copy new image
CopyMem (ScratchBufferPtr, Image, ImageSize);
ScratchBufferSize += ImageSize;
ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;
// 1.2. Pad 0xFF
RestSize = AvailableSize - ImageSize;
if (RestSize > 0) {
SetMem (ScratchBufferPtr, RestSize, 0xFF);
ScratchBufferSize += RestSize;
ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;
}
Status = UpdateMicrocode((UINTN)TargetMicrocodeEntryPoint, MicrocodePatchScratchBuffer, ScratchBufferSize, LastAttemptStatus);
return Status;
}
//
// 2. If there is empty FIT microcode entry with enough space, use it.
//
EmptyFitMicrocodeEntry = FindEmptyFitMicrocode (MicrocodeFmpPrivate, ImageSize, &AvailableSize);
if (EmptyFitMicrocodeEntry != NULL) {
DEBUG((DEBUG_INFO, "Use empty FIT microcode entry\n"));
// 2.1. Copy new image
CopyMem (ScratchBufferPtr, Image, ImageSize);
ScratchBufferSize += ImageSize;
ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;
// 2.2. Pad 0xFF
RestSize = AvailableSize - ImageSize;
if (RestSize > 0) {
SetMem (ScratchBufferPtr, RestSize, 0xFF);
ScratchBufferSize += RestSize;
ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;
}
Status = UpdateMicrocode ((UINTN) EmptyFitMicrocodeEntry, MicrocodePatchScratchBuffer, ScratchBufferSize, LastAttemptStatus);
if (!EFI_ERROR (Status) && (TargetMicrocodeEntryPoint != NULL)) {
//
// Empty old microcode.
//
ScratchBufferPtr = MicrocodePatchScratchBuffer;
SetMem (ScratchBufferPtr, TargetTotalSize, 0xFF);
ScratchBufferSize = TargetTotalSize;
ScratchBufferPtr = (UINT8 *) MicrocodePatchScratchBuffer + ScratchBufferSize;
UpdateMicrocode ((UINTN) TargetMicrocodeEntryPoint, MicrocodePatchScratchBuffer, ScratchBufferSize, LastAttemptStatus);
}
return Status;
}
//
// 3. If there is unused microcode entry with enough space, use it.
//
UnusedFitMicrocodeEntry = FindUnusedFitMicrocode (MicrocodeFmpPrivate, ImageSize, &AvailableSize);
if (UnusedFitMicrocodeEntry != NULL) {
DEBUG((DEBUG_INFO, "Use unused FIT microcode entry\n"));
// 3.1. Copy new image
CopyMem (ScratchBufferPtr, Image, ImageSize);
ScratchBufferSize += ImageSize;
ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;
// 3.2. Pad 0xFF
RestSize = AvailableSize - ImageSize;
if (RestSize > 0) {
SetMem (ScratchBufferPtr, RestSize, 0xFF);
ScratchBufferSize += RestSize;
ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;
}
Status = UpdateMicrocode ((UINTN) UnusedFitMicrocodeEntry, MicrocodePatchScratchBuffer, ScratchBufferSize, LastAttemptStatus);
if (!EFI_ERROR (Status) && (TargetMicrocodeEntryPoint != NULL)) {
//
// Empty old microcode.
//
ScratchBufferPtr = MicrocodePatchScratchBuffer;
SetMem (ScratchBufferPtr, TargetTotalSize, 0xFF);
ScratchBufferSize = TargetTotalSize;
ScratchBufferPtr = (UINT8 *) MicrocodePatchScratchBuffer + ScratchBufferSize;
UpdateMicrocode ((UINTN) TargetMicrocodeEntryPoint, MicrocodePatchScratchBuffer, ScratchBufferSize, LastAttemptStatus);
}
return Status;
}
//
// 4. No usable FIT microcode entry.
//
DEBUG((DEBUG_ERROR, "No usable FIT microcode entry\n"));
*LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES;
Status = EFI_OUT_OF_RESOURCES;
return Status;
}
/**
Update Microcode flash region.
@ -753,8 +1066,8 @@ UpdateMicrocodeFlashRegion (
AvailableSize = (UINTN)MicrocodePatchAddress + MicrocodePatchRegionSize - (UINTN)TargetMicrocodeEntryPoint;
}
DEBUG((DEBUG_INFO, " AvailableSize - 0x%x\n", AvailableSize));
ASSERT (AvailableSize >= TargetTotalSize);
}
ASSERT (AvailableSize >= TargetTotalSize);
UsedRegionSize = GetCurrentMicrocodeUsedRegionSize(MicrocodeFmpPrivate);
DEBUG((DEBUG_INFO, " UsedRegionSize - 0x%x\n", UsedRegionSize));
ASSERT (UsedRegionSize >= TargetTotalSize);
@ -762,8 +1075,8 @@ UpdateMicrocodeFlashRegion (
ASSERT ((UINTN)MicrocodePatchAddress + UsedRegionSize >= ((UINTN)TargetMicrocodeEntryPoint + TargetTotalSize));
}
//
// Total Size means the Microcode data size.
// Available Size means the Microcode data size plus the pad till (1) next Microcode or (2) the end.
// Total Size means the Microcode size.
// Available Size means the Microcode size plus the pad till (1) next Microcode or (2) the end.
//
// (1)
// +------+-----------+-----+------+===================+
@ -793,11 +1106,11 @@ UpdateMicrocodeFlashRegion (
DEBUG((DEBUG_INFO, "Replace old microcode in situ\n"));
//
// +------+------------+------+===================+
// |Other1| Old Image |Other2| Empty |
// |Other | Old Image | ... | Empty |
// +------+------------+------+===================+
//
// +------+---------+--+------+===================+
// |Other1|New Image|FF|Other2| Empty |
// |Other |New Image|FF| ... | Empty |
// +------+---------+--+------+===================+
//
// 1.1. Copy new image
@ -835,11 +1148,11 @@ UpdateMicrocodeFlashRegion (
DEBUG((DEBUG_INFO, "Reorg and replace old microcode\n"));
//
// +------+------------+------+===================+
// |Other1| Old Image |Other2| Empty |
// |Other | Old Image | ... | Empty |
// +------+------------+------+===================+
//
// +------+---------------+------+================+
// |Other1| New Image |Other2| Empty |
// |Other | New Image | ... | Empty |
// +------+---------------+------+================+
//
// 2.1. Copy new image
@ -849,7 +1162,7 @@ UpdateMicrocodeFlashRegion (
// 2.2. Copy rest images after the old image.
if (NextMicrocodeEntryPoint != 0) {
RestSize = (UINTN)MicrocodePatchAddress + UsedRegionSize - ((UINTN)NextMicrocodeEntryPoint);
CopyMem (ScratchBufferPtr, (UINT8 *)TargetMicrocodeEntryPoint + TargetTotalSize, RestSize);
CopyMem (ScratchBufferPtr, NextMicrocodeEntryPoint, RestSize);
ScratchBufferSize += RestSize;
ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;
}
@ -932,7 +1245,7 @@ UpdateMicrocodeFlashRegion (
call to FreePool().
@retval EFI_SUCCESS The Microcode image is written.
@retval EFI_VOLUME_CORRUPTED The Microcode image is corrupt.
@retval EFI_VOLUME_CORRUPTED The Microcode image is corrupted.
@retval EFI_INCOMPATIBLE_VERSION The Microcode image version is incorrect.
@retval EFI_SECURITY_VIOLATION The Microcode image fails to load.
@retval EFI_WRITE_PROTECTED The flash device is read only.
@ -963,7 +1276,6 @@ MicrocodeWrite (
return EFI_OUT_OF_RESOURCES;
}
*LastAttemptVersion = ((CPU_MICROCODE_HEADER *)Image)->UpdateRevision;
TargetCpuIndex = (UINTN)-1;
Status = VerifyMicrocode(MicrocodeFmpPrivate, AlignedImage, ImageSize, TRUE, LastAttemptStatus, AbortReason, &TargetCpuIndex);
if (EFI_ERROR(Status)) {
@ -972,6 +1284,7 @@ MicrocodeWrite (
return Status;
}
DEBUG((DEBUG_INFO, "Pass VerifyMicrocode\n"));
*LastAttemptVersion = ((CPU_MICROCODE_HEADER *)Image)->UpdateRevision;
DEBUG((DEBUG_INFO, " TargetCpuIndex - 0x%x\n", TargetCpuIndex));
ASSERT (TargetCpuIndex < MicrocodeFmpPrivate->ProcessorCount);
@ -985,13 +1298,23 @@ MicrocodeWrite (
}
DEBUG((DEBUG_INFO, " TargetMicrocodeEntryPoint - 0x%x\n", TargetMicrocodeEntryPoint));
Status = UpdateMicrocodeFlashRegion(
MicrocodeFmpPrivate,
TargetMicrocodeEntryPoint,
AlignedImage,
ImageSize,
LastAttemptStatus
);
if (MicrocodeFmpPrivate->FitMicrocodeInfo != NULL) {
Status = UpdateMicrocodeFlashRegionWithFit (
MicrocodeFmpPrivate,
TargetMicrocodeEntryPoint,
AlignedImage,
ImageSize,
LastAttemptStatus
);
} else {
Status = UpdateMicrocodeFlashRegion (
MicrocodeFmpPrivate,
TargetMicrocodeEntryPoint,
AlignedImage,
ImageSize,
LastAttemptStatus
);
}
FreePool(AlignedImage);