/** @file
Functions in this file will program the image into flash area.
Copyright (c) 2002 - 2010, 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 "UpdateDriver.h"
/**
Write a block size data into flash.
@param FvbProtocol Pointer to FVB protocol.
@param Lba Logic block index to be updated.
@param BlockSize Block size
@param Buffer Buffer data to be written.
@retval EFI_SUCCESS Write data successfully.
@retval other errors Write data failed.
**/
)
{
//
// First erase the block
//
Lba, // Lba
1, // NumOfBlocks
);
return Status;
}
//
// Write the block
//
Lba, // Lba
0, // Offset
&Size, // Size
Buffer // Buffer
);
return Status;
}
return EFI_SUCCESS;
}
/**
Write buffer data in a flash block.
@param FvbProtocol Pointer to FVB protocol.
@param Lba Logic block index to be updated.
@param Offset The offset within the block.
@param Length Size of buffer to be updated.
@param BlockSize Block size.
@param Buffer Buffer data to be updated.
@retval EFI_SUCCESS Write data successfully.
@retval other errors Write data failed.
**/
)
{
//
// If we are going to update a whole block
//
Status = UpdateOneBlock (
Lba,
);
return Status;
}
//
// If it is not a full block update, we need to coalesce data in
// the block that is not going to be updated and new data together.
//
//
// Allocate a reserved buffer to make up the final buffer for update
//
if (ReservedBuffer == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// First get the original content of the block
//
Lba,
0,
&Size,
);
return Status;
}
//
// Overwrite the reserved buffer with new content
//
Status = UpdateOneBlock (
Lba,
);
return Status;
}
/**
Get the last write log, and check the status of last write.
If not complete, restart will be taken.
@param FvbHandle Handle of FVB protocol.
@param FtwProtocol FTW protocol instance.
@param ConfigData Config data on updating driver.
@param PrivateDataSize bytes from the private data
stored for this write.
@param PrivateData A pointer to a buffer. The function will copy.
@param Lba The logical block address of the last write.
@param Offset The offset within the block of the last write.
@param Length The length of the last write.
@param Pending A Boolean value with TRUE indicating
that the write was completed.
@retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
@retval EFI_ABORTED The FTW work space is damaged.
@retval EFI_NOT_FOUND The last write is not done by this driver.
@retval EFI_SUCCESS Last write log is got.
**/
)
{
//
// Get the last write
//
&CallerId,
Lba,
);
//
// If there is no incompleted record, return success.
//
return EFI_SUCCESS;
} else if (Status == EFI_BUFFER_TOO_SMALL) {
//
// If buffer too small, reallocate buffer and call getlastwrite again
//
if (PrivateDataBuffer == NULL) {
return EFI_OUT_OF_RESOURCES;
}
&CallerId,
Lba,
);
return EFI_ABORTED;
} else {
}
} else {
return EFI_ABORTED;
}
}
//
// If the caller is not the update driver, then return.
// The update driver cannot continue to perform the update
//
return EFI_NOT_FOUND;
}
//
// Check the private data and see if it is the one I need.
//
return EFI_NOT_FOUND;
}
//
// If the caller is the update driver and complete is not true, then restart().
//
if (!Complete) {
//
// Re-start the update
//
);
//
// If restart() error, then abort().
//
//
// Now set Pending as FALSE as this record has been cleared
//
return EFI_SUCCESS;
}
}
return Status;
}
/**
Update the whole FV image in fault tolerant write method.
@param FvbHandle Handle of FVB protocol for the updated flash range.
@param FvbProtocol FVB protocol.
@param BlockMap Block array to specify flash area.
@param ConfigData Config data on updating driver.
@param ImageBuffer Image buffer to be updated.
@param ImageSize Image size.
@retval EFI_SUCCESS FV image is writed into flash.
@retval EFI_INVALID_PARAMETER Config data is not valid.
@retval EFI_NOT_FOUND FTW protocol doesn't exist.
@retval EFI_OUT_OF_RESOURCES No enough backup space.
@retval EFI_ABORTED Error happen when update FV.
**/
)
{
|| (!ConfigData->FaultTolerant)) {
return EFI_INVALID_PARAMETER;
}
//
// Get the FTW protocol
//
NULL,
(VOID **) &FtwProtocol
);
return EFI_NOT_FOUND;
}
//
// Get the maximum block size of the FV, and number of blocks
// NumOfBlocks will be the NumOfUdpates.
//
MaxBlockSize = 0;
NumOfBlocks = 0;
while (TRUE) {
break;
}
}
PtrMap++;
}
//
// Not enough backup space. return directly
//
if (FtwMaxBlockSize < MaxBlockSize) {
return EFI_OUT_OF_RESOURCES;
}
PendingLba = 0;
PendingOffset = 0;
PendingLength = 0;
//
// Fault Tolerant Write can only support actual fault tolerance if the write
// is a reclaim operation, which means the data buffer (new and old) are
// acutally both stored in flash. But for component update write, the data
// are now in memory. So we cannot actually recover the data after power
// failure.
//
sizeof (UPDATE_PRIVATE_DATA),
);
//
// Cannot continue with the write operation
//
return EFI_ABORTED;
}
return EFI_ABORTED;
}
//
// Currently we start from the pending write if there is any. But as we
// are going to update a whole FV, we can just abort last write and start
// from the very begining.
//
if (!Pending) {
//
// Now allocte the update private data in FTW. If there is pending
// write, it has already been allocated and no need to allocate here.
//
sizeof (UPDATE_PRIVATE_DATA),
);
return Status;
}
}
//
// Perform the update now. If there are pending writes, we need to
// start from the pending write instead of the very beginning.
//
Lba = 0;
Offset = 0;
CopyMem (
sizeof (EFI_GUID)
);
while (TRUE) {
break;
}
//
// Add an extra check here to see if the pending record is correct
//
//
// Error.
//
break;
}
}
Lba, // Lba
Offset, // Offset
Length, // Size
&PrivateData, // Private Data
FvbHandle, // FVB handle
UpdateBuffer // Buffer
);
}
break;
}
Lba++;
}
break;
}
PtrMap++;
}
return Status;
}
/**
Directly update the whole FV image without fault tolerant write method.
@param FvbHandle Handle of FVB protocol for the updated flash range.
@param FvbProtocol FVB protocol.
@param BlockMap Block array to specify flash area.
@param ConfigData Config data on updating driver.
@param ImageBuffer Image buffer to be updated.
@param ImageSize Image size.
@retval EFI_SUCCESS FV image is writed into flash.
@retval EFI_INVALID_PARAMETER Config data is not valid.
@retval EFI_ABORTED Error happen when update FV.
**/
)
{
|| (ConfigData->FaultTolerant)) {
return EFI_INVALID_PARAMETER;
}
UpdateLba = 0;
//
// Perform the update now
//
while (TRUE) {
break;
}
Status = UpdateOneBlock (
);
break;
}
UpdateLba++;
}
break;
}
PtrMap++;
}
return Status;
}
/**
Update the whole FV image, and reinsall FVB protocol for the updated FV image.
@param FvbHandle Handle of FVB protocol for the updated flash range.
@param FvbProtocol FVB protocol.
@param ConfigData Config data on updating driver.
@param ImageBuffer Image buffer to be updated.
@param ImageSize Image size.
@retval EFI_INVALID_PARAMETER Update type is not UpdateWholeFV.
Or Image size is not same to the size of whole FV.
@retval EFI_OUT_OF_RESOURCES No enoug memory is allocated.
@retval EFI_SUCCESS FV image is updated, and its FVB protocol is reinstalled.
**/
)
{
return EFI_INVALID_PARAMETER;
}
//
// Get the header of the firmware volume
//
FwVolHeader = NULL;
FwVolHeader = AllocatePool (((EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) (ConfigData->BaseAddress)))->HeaderLength);
if (FwVolHeader == NULL) {
return EFI_OUT_OF_RESOURCES;
}
CopyMem (
);
//
// Check if ImageSize is the same as the size of the whole FV
//
return EFI_INVALID_PARAMETER;
}
//
// Print on screen
//
}
//
// Get the block map of the firmware volume
//
//
// It is about the same if we are going to fault tolerantly update
// a certain FV in our current design. But we divide non-fault tolerant
// and fault tolerant udpate here for better maintenance as fault
// tolerance may change and may be done more wisely if we have space.
//
if (ConfigData->FaultTolerant) {
);
} else {
);
}
return Status;
}
//
// As the whole FV has been replaced, the FV driver shall re-parse the
// firmware volume. So re-install FVB protocol here
//
);
return Status;
}
/**
Update certain file in the FV.
@param FvbHandle Handle of FVB protocol for the updated flash range.
@param FvbProtocol FVB protocol.
@param ConfigData Config data on updating driver.
@param ImageBuffer Image buffer to be updated.
@param ImageSize Image size.
@param FileType FFS file type.
@param FileAttributes FFS file attribute
@retval EFI_INVALID_PARAMETER Update type is not UpdateFvFile.
Or Image size is not same to the size of whole FV.
@retval EFI_UNSUPPORTED PEIM FFS is unsupported to be updated.
@retval EFI_SUCCESS The FFS file is added into FV.
**/
)
{
return EFI_INVALID_PARAMETER;
}
//
// Print on screen
//
}
&(ConfigData->FileGuid)));
//
// Get Firmware volume protocol on this FVB protocol
//
(VOID **) &FwVolProtocol
);
return Status;
}
//
// If it is a PEIM, we need first to rebase it before committing
// the write to target
//
|| (FileType == EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER)) {
return EFI_UNSUPPORTED;
}
1, // NumberOfFiles
);
return Status;
}
/**
Update the buffer into flash area in fault tolerant write method.
@param ImageBuffer Image buffer to be updated.
@param SizeLeft Size of the image buffer.
@param UpdatedSize Size of the updated buffer.
@param ConfigData Config data on updating driver.
@param FlashAddress Flash address to be updated as start address.
@param FvbProtocol FVB protocol.
@param FvbHandle Handle of FVB protocol for the updated flash range.
@retval EFI_SUCCESS Buffer data is updated into flash.
@retval EFI_INVALID_PARAMETER Base flash address is not in FVB flash area.
@retval EFI_NOT_FOUND FTW protocol doesn't exist.
@retval EFI_OUT_OF_RESOURCES No enough backup space.
@retval EFI_ABORTED Error happen when update flash area.
**/
)
{
//
// Get the block map to update the block one by one
//
);
return Status;
}
return EFI_INVALID_PARAMETER;
}
);
if (FwVolHeader == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// For fault tolerant write, we have to know how many blocks we need to
// update. So we will calculate number of updates and max block size first
//
NumOfUpdates = 0;
MaxBlockSize = 0;
while (TotalSize > 0) {
break;
}
//
// Check if this block need to be updated
//
//
// Get the maximum block size
//
if (MaxBlockSize < BlockSize) {
}
//
// This block shall be udpated. So increment number of updates
//
NumOfUpdates++;
}
if (TotalSize <= 0) {
break;
}
}
}
PtrMap++;
}
//
// Get the FTW protocol
//
NULL,
(VOID **) &FtwProtocol
);
return EFI_NOT_FOUND;
}
//
// Not enough backup space. return directly
//
if (FtwMaxBlockSize < MaxBlockSize) {
return EFI_OUT_OF_RESOURCES;
}
PendingLba = 0;
PendingOffset = 0;
PendingLength = 0;
//
// Fault Tolerant Write can only support actual fault tolerance if the write
// is a reclaim operation, which means the data buffer (new and old) are
// acutally both stored in flash. But for component update write, the data
// are now in memory. So we cannot actually recover the data after power
// failure.
//
sizeof (UPDATE_PRIVATE_DATA),
);
//
// I'm not the owner of the pending fault tolerant write record
// Cannot continue with the write operation
//
return EFI_ABORTED;
}
return EFI_ABORTED;
}
//
// Currently we start from the pending write if there is any. But if the
// caller is exactly the same, and the new data is already a in memory, (it
// cannot be stored in flash in last write,) we can just abort last write
// and start from the very begining.
//
if (!Pending) {
//
// Now allocte the update private data in FTW. If there is pending
// write, it has already been allocated and no need to allocate here.
//
sizeof (UPDATE_PRIVATE_DATA),
);
return Status;
}
}
//
// Perform the update now. If there are pending writes, we need to
// start from the pending write instead of the very beginning.
//
Lba = 0;
Image = ImageBuffer;
CopyMem (
sizeof (EFI_GUID)
);
while (TotalSize > 0) {
break;
}
//
// So we need to update this block
//
}
//
// Add an extra check here to see if the pending record is correct
//
//
// Error.
//
break;
}
}
DEBUG ((EFI_D_UPDATE, "Update Flash area from %08LX to %08LX\n", StartAddress, (UINT64)StartAddress + Length));
Lba, // Lba
Offset, // Offset
Length, // Size
&PrivateData, // Private Data
FvbHandle, // FVB handle
Image // Buffer
);
break;
}
}
//
// Now increment StartAddress, ImageBuffer and decrease the
// left size to prepare for the next block update.
//
if (TotalSize <= 0) {
break;
}
}
Lba++;
}
break;
}
PtrMap++;
}
return EFI_SUCCESS;
}
/**
Directly update the buffer into flash area without fault tolerant write method.
@param ImageBuffer Image buffer to be updated.
@param SizeLeft Size of the image buffer.
@param UpdatedSize Size of the updated buffer.
@param FlashAddress Flash address to be updated as start address.
@param FvbProtocol FVB protocol.
@param FvbHandle Handle of FVB protocol for the updated flash range.
@retval EFI_SUCCESS Buffer data is updated into flash.
@retval EFI_INVALID_PARAMETER Base flash address is not in FVB flash area.
@retval EFI_OUT_OF_RESOURCES No enough backup space.
**/
)
{
//
// Get the block map to update the block one by one
//
);
return Status;
}
return EFI_INVALID_PARAMETER;
}
);
if (FwVolHeader == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Image = ImageBuffer;
Lba = 0;
while (TotalSize > 0) {
break;
}
//
// So we need to update this block
//
}
DEBUG ((EFI_D_UPDATE, "Update Flash area from %08LX to %08LX\n", FlashAddress, (UINT64)FlashAddress + Length));
//
// Update the block
//
Lba,
);
return Status;
}
//
// Now increment FlashAddress, ImageBuffer and decrease the
// left size to prepare for the next block update.
//
if (TotalSize <= 0) {
break;
}
}
Lba++;
}
break;
}
BlockMap++;
}
return EFI_SUCCESS;
}