/** @file
Serialize Variables Library implementation
Copyright (c) 2004 - 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 "SerializeVariablesLib.h"
/**
Serialization format:
The SerializeVariablesLib interface does not specify a format
for the serialization of the variable data. This library uses
a packed array of a non-uniformly sized data structure elements.
Each variable is stored (packed) as:
UINT32 VendorNameSize; // Name size in bytes
CHAR16 VendorName[?]; // The variable unicode name including the
// null terminating character.
EFI_GUID VendorGuid; // The variable GUID
UINT32 DataSize; // The size of variable data in bytes
UINT8 Data[?]; // The variable data
**/
/**
Unpacks the next variable from the buffer
@param[in] Buffer - Buffer pointing to the next variable instance
On subsequent calls, the pointer should be incremented
by the returned SizeUsed value.
@param[in] MaxSize - Max allowable size for the variable data
On subsequent calls, this should be decremented
by the returned SizeUsed value.
@param[out] Name - Variable name string (address in Buffer)
@param[out] NameSize - Size of Name in bytes
@param[out] Guid - GUID of variable (address in Buffer)
@param[out] Attributes - Attributes of variable
@param[out] Data - Buffer containing Data for variable (address in Buffer)
@param[out] DataSize - Size of Data in bytes
@param[out] SizeUsed - Total size used for this variable instance in Buffer
@return EFI_STATUS based on the success or failure of the operation
**/
)
{
Offset = 0;
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
return EFI_SUCCESS;
}
/**
Iterates through the variables in the buffer, and calls a callback
function for each variable found.
@param[in] CallbackFunction - Function called for each variable instance
@param[in] Context - Passed to each call of CallbackFunction
@param[in] Buffer - Buffer containing serialized variables
@param[in] MaxSize - Size of Buffer in bytes
@return EFI_STATUS based on the success or failure of the operation
**/
)
{
SizeUsed = 0;
AlignedName = NULL;
AlignedNameMaxSize = 0;
Attributes = 0;
DataSize = 0;
for (
) {
(MaxSize - TotalSizeUsed),
&Name,
&NameSize,
&Guid,
&DataSize,
&Data,
);
return Status;
}
//
// We copy the name to a separately allocated buffer,
// to be sure it is 16-bit aligned.
//
if (NameSize > AlignedNameMaxSize) {
if (AlignedName != NULL) {
}
}
if (AlignedName == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Run the callback function
//
Status = (*CallbackFunction) (
Guid,
);
}
if (AlignedName != NULL) {
}
//
// Make sure the entire buffer was used, or else return an error
//
if (TotalSizeUsed != MaxSize) {
DEBUG ((
"Deserialize variables error: TotalSizeUsed(%d) != MaxSize(%d)\n",
));
return EFI_INVALID_PARAMETER;
}
return EFI_SUCCESS;
}
)
{
return RETURN_SUCCESS;
}
)
{
return SerializeVariablesAddVariable (
);
}
)
{
return gRT->SetVariable (
);
}
)
{
return RETURN_SUCCESS;
}
//
// Double the required size to lessen the need to re-allocate in the future
//
return RETURN_OUT_OF_RESOURCES;
}
}
return RETURN_SUCCESS;
}
)
{
CopyMem (
Data,
);
}
/**
Creates a new variable serialization instance
@param[out] Handle - Handle for a variable serialization instance
@retval RETURN_SUCCESS - The variable serialization instance was
successfully created.
@retval RETURN_OUT_OF_RESOURCES - There we not enough resources to
create the variable serialization instance.
**/
)
{
return RETURN_OUT_OF_RESOURCES;
}
return RETURN_SUCCESS;
}
/**
Free memory associated with a variable serialization instance
@param[in] Handle - Handle for a variable serialization instance
@retval RETURN_SUCCESS - The variable serialization instance was
successfully freed.
@retval RETURN_INVALID_PARAMETER - Handle was not a valid
variable serialization instance.
**/
)
{
return RETURN_INVALID_PARAMETER;
}
}
return RETURN_SUCCESS;
}
/**
Creates a new variable serialization instance using the given
binary representation of the variables to fill the new instance
@param[out] Handle - Handle for a variable serialization instance
@param[in] Buffer - A buffer with the serialized representation
of the variables. Must be the same format as produced
by SerializeVariablesToBuffer.
@param[in] Size - This is the size of the binary representation
of the variables.
@retval RETURN_SUCCESS - The binary representation was successfully
imported into a new variable serialization instance
@retval RETURN_OUT_OF_RESOURCES - There we not enough resources to
create the new variable serialization instance
**/
)
{
if (RETURN_ERROR (Status)) {
return Status;
}
NULL,
);
if (RETURN_ERROR (Status)) {
return Status;
}
);
if (RETURN_ERROR (Status)) {
return Status;
}
return Status;
}
/**
Iterates all variables found with RuntimeServices GetNextVariableName
@param[in] CallbackFunction - Function called for each variable instance
@param[in] Context - Passed to each call of CallbackFunction
@retval RETURN_SUCCESS - All variables were iterated without the
CallbackFunction returning an error
@retval RETURN_OUT_OF_RESOURCES - There we not enough resources to
iterate through the variables
@return Any of RETURN_ERROR indicates an error reading the variable
or an error was returned from CallbackFunction
**/
)
{
//
// Initialize the variable name and data buffer variables.
//
VariableNameBufferSize = sizeof (CHAR16);
VariableData = NULL;
for (;;) {
//
// Get the next variable name and guid
//
);
if (Status == EFI_BUFFER_TOO_SMALL) {
//
// The currently allocated VariableName buffer is too small,
// so we allocate a larger buffer, and copy the old buffer
// to it.
//
break;
}
if (VariableName != NULL) {
}
//
// Try to get the next variable name again with the larger buffer.
//
);
}
if (Status == EFI_NOT_FOUND) {
}
break;
}
//
// Get the variable data and attributes
//
);
if (Status == EFI_BUFFER_TOO_SMALL) {
//
// The currently allocated VariableData buffer is too small,
// so we allocate a larger buffer.
//
if (VariableDataBufferSize != 0) {
VariableData = NULL;
}
if (VariableData == NULL) {
break;
}
//
// Try to read the variable again with the larger buffer.
//
);
}
break;
}
//
// Run the callback function
//
Status = (*CallbackFunction) (
);
break;
}
}
if (VariableName != NULL) {
}
if (VariableData != NULL) {
}
return Status;
}
/**
Iterates all variables found in the variable serialization instance
@param[in] Handle - Handle for a variable serialization instance
@param[in] CallbackFunction - Function called for each variable instance
@param[in] Context - Passed to each call of CallbackFunction
@retval RETURN_SUCCESS - All variables were iterated without the
CallbackFunction returning an error
@retval RETURN_OUT_OF_RESOURCES - There we not enough resources to
iterate through the variables
@return Any of RETURN_ERROR indicates an error reading the variable
or an error was returned from CallbackFunction
**/
)
{
return IterateVariablesInBuffer (
);
} else {
return RETURN_SUCCESS;
}
}
/**
Sets all variables found in the variable serialization instance
@param[in] Handle - Handle for a variable serialization instance
@retval RETURN_SUCCESS - All variables were set successfully
@retval RETURN_OUT_OF_RESOURCES - There we not enough resources to
set all the variables
@return Any of RETURN_ERROR indicates an error reading the variables
or in attempting to set a variable
**/
)
{
);
}
/**
Adds a variable to the variable serialization instance
@param[in] Handle - Handle for a variable serialization instance
@param[in] VariableName - Refer to RuntimeServices GetVariable
@param[in] VendorGuid - Refer to RuntimeServices GetVariable
@param[in] Attributes - Refer to RuntimeServices GetVariable
@param[in] DataSize - Refer to RuntimeServices GetVariable
@param[in] Data - Refer to RuntimeServices GetVariable
@retval RETURN_SUCCESS - All variables were set successfully
@retval RETURN_OUT_OF_RESOURCES - There we not enough resources to
add the variable
@retval RETURN_INVALID_PARAMETER - Handle was not a valid
variable serialization instance or
VariableName, VariableGuid or Data are NULL.
**/
)
{
}
sizeof (SerializedNameSize) +
sizeof (*VendorGuid) +
sizeof (Attributes) +
sizeof (SerializedDataSize) +
);
if (RETURN_ERROR (Status)) {
return Status;
}
//
// Add name size (UINT32)
//
//
// Add variable unicode name string
//
//
// Add variable GUID
//
//
// Add variable attributes
//
//
// Add variable data size (UINT32)
//
//
// Add variable data
//
return RETURN_SUCCESS;
}
/**
Serializes the variables known to this instance into the
provided buffer.
@param[in] Handle - Handle for a variable serialization instance
@param[out] Buffer - A buffer to store the binary representation
of the variables.
@param[in,out] Size - On input this is the size of the buffer.
On output this is the size of the binary representation
of the variables.
@retval RETURN_SUCCESS - The binary representation was successfully
completed and returned in the buffer.
@retval RETURN_OUT_OF_RESOURCES - There we not enough resources to
save the variables to the buffer.
@retval RETURN_INVALID_PARAMETER - Handle was not a valid
variable serialization instance or
Size or Buffer were NULL.
@retval RETURN_BUFFER_TOO_SMALL - The Buffer size as indicated by
the Size parameter was too small for the serialized
variable data. Size is returned with the required size.
**/
)
{
return RETURN_INVALID_PARAMETER;
}
return RETURN_BUFFER_TOO_SMALL;
}
return RETURN_INVALID_PARAMETER;
}
return RETURN_SUCCESS;
}