/** @file
Implements functions to pad firmware file.
Copyright (c) 2006 - 2011, 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 "FwVolDriver.h"
/**
Calculate the checksum for a PAD file.
@param PadFileHeader The Pad File to be caculeted the checksum.
**/
)
{
if (IS_FFS_FILE2 (PadFileHeader)) {
//
// Calculate checksum of Pad File Data
//
CalculateCheckSum8 ((UINT8 *) PadFileHeader + sizeof (EFI_FFS_FILE_HEADER2), FFS_FILE2_SIZE (PadFileHeader) - sizeof (EFI_FFS_FILE_HEADER2));
} else {
//
// Calculate checksum of Pad File Data
//
CalculateCheckSum8 ((UINT8 *) PadFileHeader + sizeof (EFI_FFS_FILE_HEADER), FFS_FILE_SIZE (PadFileHeader) - sizeof (EFI_FFS_FILE_HEADER));
}
} else {
}
return ;
}
/**
Create a PAD File in the Free Space.
@param FvDevice Firmware Volume Device.
@param FreeSpaceEntry Indicating in which Free Space(Cache) the Pad file will be inserted.
@param Size Pad file Size, not include the header.
@param PadFileEntry The Ffs File Entry that points to this Pad File.
@retval EFI_SUCCESS Successfully create a PAD file.
@retval EFI_OUT_OF_RESOURCES No enough free space to create a PAD file.
@retval EFI_INVALID_PARAMETER Size is not 8 byte alignment.
@retval EFI_DEVICE_ERROR Free space is not erased.
**/
)
{
HeaderSize = sizeof (EFI_FFS_FILE_HEADER);
if (FileSize > 0x00FFFFFF) {
HeaderSize = sizeof (EFI_FFS_FILE_HEADER2);
}
return EFI_OUT_OF_RESOURCES;
}
if ((Size & 0x07) != 0) {
return EFI_INVALID_PARAMETER;
}
//
// First double check the space
//
if (!IsBufferErased (
)) {
return EFI_DEVICE_ERROR;
}
//
// Create File Step 1
//
NumBytesWritten = sizeof (EFI_FFS_FILE_STATE);
);
return Status;
}
//
// Update Free Space Entry, since header is allocated
//
//
// Fill File Name Guid, here we assign a NULL-GUID to Pad files
//
//
// Fill File Type, checksum(0), Attributes(0), Size
//
PadFileHeader->Attributes = 0;
if ((FileSize) > 0x00FFFFFF) {
} else {
}
(UINT8 *) PadFileHeader
);
return Status;
}
//
// Step 2, then Mark header valid, since no data write,
// mark the data valid at the same time.
//
NumBytesWritten = sizeof (EFI_FFS_FILE_STATE);
);
return Status;
}
//
// Update Free Space Entry, since header is allocated
//
//
// If successfully, insert an FfsFileEntry at the end of ffs file list
//
return EFI_SUCCESS;
}
/**
Fill pad file header within firmware cache.
@param PadFileHeader The start of the Pad File Buffer.
@param PadFileLength The length of the pad file including the header.
**/
)
{
//
// Fill File Name Guid, here we assign a NULL-GUID to Pad files
//
//
// Fill File Type, checksum(0), Attributes(0), Size
//
PadFileHeader->Attributes = 0;
if (PadFileLength > 0x00FFFFFF) {
} else {
}
//
// Set File State to 0x00000111
//
return ;
}
/**
Create entire FFS file.
@param FileHeader Starting Address of a Buffer that hold the FFS File image.
@param FfsFileBuffer The source buffer that contains the File Data.
@param BufferSize The length of FfsFileBuffer.
@param ActualFileSize Size of FFS file.
@param FileName The Guid of Ffs File.
@param FileType The type of the written Ffs File.
@param FileAttributes The attributes of the written Ffs File.
@retval EFI_INVALID_PARAMETER File type is not valid.
@retval EFI_SUCCESS FFS file is successfully created.
**/
)
{
//
// File Type value 0x0E~0xE0 are reserved
//
return EFI_INVALID_PARAMETER;
}
//
// First fill all fields ready in FfsFileBuffer
//
//
// Convert the FileAttributes to FFSFileAttributes
//
if (ActualFileSize > 0x00FFFFFF) {
} else {
}
//
// Copy data from FfsFileBuffer to FileHeader(cache)
//
return EFI_SUCCESS;
}
/**
Fill some other extra space using 0xFF(Erase Value).
@param ErasePolarity Fv erase value.
@param FileHeader Point to the start of FFS File.
@param ExtraLength The pading length.
**/
)
{
if (IS_FFS_FILE2 (FileHeader)) {
} else {
}
if (ErasePolarity == 0) {
PadingByte = 0;
} else {
PadingByte = 0xFF;
}
//
// Fill the non-used space with Padding Byte
//
return ;
}
/**
Free File List entry pointed by FileListHead.
@param FileListHeader FileListEntry Header.
**/
)
{
//
// Loop the whole list entry to free resources
//
}
return ;
}
/**
Create a new file within a PAD file area.
@param FvDevice Firmware Volume Device.
@param FfsFileBuffer A buffer that holds an FFS file,(it contains a File Header which is in init state).
@param BufferSize The size of FfsFileBuffer.
@param ActualFileSize The actual file length, it may not be multiples of 8.
@param FileName The FFS File Name.
@param FileType The FFS File Type.
@param FileAttributes The Attributes of the FFS File to be created.
@retval EFI_SUCCESS Successfully create a new file within the found PAD file area.
@retval EFI_OUT_OF_RESOURCES No suitable PAD file is found.
@retval other errors New file is created failed.
**/
)
{
//
// First get the required alignment from the File Attributes
//
//
// Find a suitable PAD File
//
&PadSize,
);
return EFI_OUT_OF_RESOURCES;
}
//
// Step 1: Update Pad File Header
//
NumBytesWritten = sizeof (EFI_FFS_FILE_STATE);
);
return Status;
}
//
// Step 2: Update Pad area
//
if (IS_FFS_FILE2 (OldPadFileHeader)) {
PadFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) OldPadFileHeader + sizeof (EFI_FFS_FILE_HEADER2));
} else {
PadFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) OldPadFileHeader + sizeof (EFI_FFS_FILE_HEADER));
}
if (PadSize != 0) {
//
// Insert a PAD file before to achieve required alignment
//
}
Status = FvFillFfsFile (
);
return Status;
}
//
// we can insert another PAD file
//
} else {
//
// because left size cannot hold another PAD file header,
// adjust the writing file size (just in cache)
//
);
}
}
//
// Start writing to FV
//
if (IS_FFS_FILE2 (OldPadFileHeader)) {
} else {
}
);
return Status;
}
//
// Step 3: Mark Pad file header as EFI_FILE_HEADER_INVALID
//
NumBytesWritten = sizeof (EFI_FFS_FILE_STATE);
);
return Status;
}
//
// If all successfully, update FFS_FILE_LIST
//
//
// Delete old pad file entry
//
return EFI_SUCCESS;
}
/**
Free all FfsBuffer.
@param NumOfFiles Number of FfsBuffer.
@param FfsBuffer An array of pointer to an FFS File Buffer
**/
)
{
}
}
}
/**
Create multiple files within a PAD File area.
@param FvDevice Firmware Volume Device.
@param PadFileEntry The pad file entry to be written in.
@param NumOfFiles Total File number to be written.
@param BufferSize The array of buffer size of each FfsBuffer.
@param ActualFileSize The array of actual file size.
@param PadSize The array of leading pad file size for each FFS File
@param FfsBuffer The array of Ffs Buffer pointer.
@param FileData The array of EFI_FV_WRITE_FILE_DATA structure,
used to get name, attributes, type, etc.
@retval EFI_SUCCESS Add the input multiple files into PAD file area.
@retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
@retval other error Files can't be added into PAD file area.
**/
)
{
if (IS_FFS_FILE2 (OldPadFileHeader)) {
} else {
}
);
return Status;
}
//
// Update PAD area
//
TotalSize = 0;
if (IS_FFS_FILE2 (OldPadFileHeader)) {
PadFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) OldPadFileHeader + sizeof (EFI_FFS_FILE_HEADER2));
} else {
PadFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) OldPadFileHeader + sizeof (EFI_FFS_FILE_HEADER));
}
if (NewFileListEntry == NULL) {
return EFI_OUT_OF_RESOURCES;
}
}
Status = FvFillFfsFile (
);
return Status;
}
if (NewFileListEntry == NULL) {
return EFI_OUT_OF_RESOURCES;
}
}
//
// Maybe we need a tail pad file
//
if (PadAreaLength > TotalSize) {
//
// we can insert another PAD file
//
if (NewFileListEntry == NULL) {
return EFI_OUT_OF_RESOURCES;
}
} else {
//
// because left size cannot hold another PAD file header,
// adjust the writing file size (just in cache)
//
);
}
}
//
// Start writing to FV
//
if (IS_FFS_FILE2 (OldPadFileHeader)) {
} else {
}
);
return Status;
}
);
return Status;
}
//
// Update File List Link
//
//
// First delete old pad file entry
//
return EFI_SUCCESS;
}
/**
Create multiple files within the Free Space.
@param FvDevice Firmware Volume Device.
@param FreeSpaceEntry Indicating in which Free Space(Cache) the multiple files will be inserted.
@param NumOfFiles Total File number to be written.
@param BufferSize The array of buffer size of each FfsBuffer.
@param ActualFileSize The array of actual file size.
@param PadSize The array of leading pad file size for each FFS File
@param FfsBuffer The array of Ffs Buffer pointer.
@param FileData The array of EFI_FV_WRITE_FILE_DATA structure,
used to get name, attributes, type, etc.
@retval EFI_SUCCESS Add the input multiple files into PAD file area.
@retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
@retval other error Files can't be added into PAD file area.
**/
)
{
TotalSize = 0;
if (NewFileListEntry == NULL) {
return EFI_OUT_OF_RESOURCES;
}
}
Status = FvFillFfsFile (
);
return Status;
}
if (NewFileListEntry == NULL) {
return EFI_OUT_OF_RESOURCES;
}
}
return EFI_OUT_OF_RESOURCES;
}
//
// Start writing to FV
//
);
return Status;
}
}
return EFI_SUCCESS;
}
/**
Write multiple files into FV in reliable method.
@param FvDevice Firmware Volume Device.
@param NumOfFiles Total File number to be written.
@param FileData The array of EFI_FV_WRITE_FILE_DATA structure,
used to get name, attributes, type, etc
@param FileOperation The array of operation for each file.
@retval EFI_SUCCESS Files are added into FV.
@retval EFI_OUT_OF_RESOURCES No enough free PAD files to add the input files.
@retval EFI_INVALID_PARAMETER File number is less than or equal to 1.
@retval EFI_UNSUPPORTED File number exceeds the supported max numbers of files.
**/
)
{
//
// To use this function, we must ensure that the NumOfFiles is great
// than 1
//
if (NumOfFiles <= 1) {
return EFI_INVALID_PARAMETER;
}
if (NumOfFiles > MAX_FILES) {
return EFI_UNSUPPORTED;
}
//
// Adjust file size
//
HeaderSize = sizeof (EFI_FFS_FILE_HEADER);
HeaderSize = sizeof (EFI_FFS_FILE_HEADER2);
}
//
// clear file attributes, zero-length file does not have any attributes
//
}
BufferSize[Index1]++;
}
//
// Copy File Data into FileBuffer
//
CopyMem (
);
}
}
}
//
// If update file, mark the original file header to
// EFI_FILE_MARKED_FOR_UPDATE
//
if (!IsCreateFile) {
Key = 0;
do {
OldFileType = 0;
Fv,
&Key,
);
return Status;
}
//
// Get FfsFileEntry from the search key
//
);
return Status;
}
}
}
//
// First to search a suitable pad file that can hold so
// many files
//
);
if (Status == EFI_NOT_FOUND) {
//
// Try to find a free space that can hold these files
//
);
return EFI_OUT_OF_RESOURCES;
}
);
} else {
//
// Create multiple files inside such a pad file
// to achieve lock-step update
//
);
}
return Status;
}
//
// Delete those updated files
//
}
}
//
// Set those files' state to EFI_FILE_DELETED
//
return Status;
}
}
}
return EFI_SUCCESS;
}