blob: 0ba7b205ccb1369938f1a3c05ddba6c048697249 [file]
/** @file
Dynamic Hii Form Serialization API functions
Copyright (c) 2026, Arm Limited. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
@par Reference(s):
- UEFI specification 2.11, section 33.3.8 Forms Package
**/
#include <Base.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Uefi/UefiInternalFormRepresentation.h>
#include "InternalHiiFormsLib.h"
#define MAX_FORM_PACKAGE_SIZE (SIZE_16MB + sizeof (UINT32))
/** The initial buffer size allocated for the
IFR byte-stream.
*/
#define IFR_INITIAL_BUFFER_SIZE 1024
/** The max length of an HII opcode.
*/
#define MAX_HII_OPCODE_LENGTH 127
/** Re-size the IFR byte-stream buffer.
Re-size the IFR byte-stream buffer to accommodate the TotalSize passed to
the function.
@param [in] BufInfo IFR buffer data structure pointer.
@param [in] TotalSize Minimum number of bytes that the function should
allocate.
@retval EFI_SUCCESS The varstore was created successfully.
@retval EFI_BAD_BUFFER_SIZE Requested size overflows.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiResizeIfrByteBuffer (
IN DYN_HII_IFR_BUFFER *BufInfo,
IN UINT32 TotalSize
)
{
UINT8 *Buf;
UINT64 NewSize;
/// Try to double the size of the buffer, but
/// do not exceed max allowed form package size
NewSize = MIN ((UINT64)BufInfo->Size * 2, MAX_FORM_PACKAGE_SIZE);
/// Now take the max of the requested size and
/// the size computed above.
NewSize = MAX (TotalSize, NewSize);
Buf = ReallocatePool (BufInfo->Size, NewSize, BufInfo->Data);
if (Buf == NULL) {
return EFI_OUT_OF_RESOURCES;
}
BufInfo->Data = Buf;
BufInfo->Size = (UINT32)NewSize;
return EFI_SUCCESS;
}
/** Check if the IFR byte buffer is large enough.
Check if the IFR byte buffer is large enough to hold Size bytes. If not,
call the function to re-allocate the sufficient number of bytes.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@param [in] Size The number of bytes that the buffer should
be able to hold.
@retval EFI_SUCCESS The IFR buffer is large enough, or the re-size
operation was successfull.
@retval EFI_BAD_BUFFER_SIZE Requested size overflows.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiIfrBufferSizeCheck (
IN DYN_HII_IFR_BUFFER *BufInfo,
IN UINT32 Size
)
{
UINT64 NewSize;
if (BufInfo->Size - BufInfo->Position >= Size) {
return EFI_SUCCESS;
}
NewSize = BufInfo->Size + Size;
if (NewSize >= MAX_FORM_PACKAGE_SIZE) {
return EFI_BAD_BUFFER_SIZE;
}
return DynHiiResizeIfrByteBuffer (BufInfo, (UINT32)NewSize);
}
/** Initialize the IFR byte-stream buffer.
Initialize the IFR byte-stream buffer with IFR_INITIAL_BUFFER_SIZE bytes.
Also allocate memory for DYN_HII_IFR_BUFFER structure which holds the IFR
buffer.
@param [out] Buf Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The varstore was created successfully.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiInitIfrByteBuffer (
OUT DYN_HII_IFR_BUFFER *Buf
)
{
Buf->Data = AllocateZeroPool (IFR_INITIAL_BUFFER_SIZE);
if (Buf->Data == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Buf->Size = IFR_INITIAL_BUFFER_SIZE;
Buf->Position = sizeof (UINT32) + sizeof (EFI_HII_PACKAGE_HEADER);
return EFI_SUCCESS;
}
/** Invoke the function to serialize the given node.
Based on the parameters passed to the function, invoke the serializing
function that has been registered.
@param [in] Table Table of serializing functions.
@param [in] TableCount Total count of functions in the table.
@param [in] Type Index in the table array that is to be invoked.
@param [in] Hdr Pointer to the node header to be serialized.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The serialization function was called successfully.
@retval EFI_INVALID_PARAMETER One of the input parameter is invalid.
@retval EFI_UNSUPPORTED The node type to be serialized does not currently
have a serialization function added.
**/
static
EFI_STATUS
DynHiiDispatchSerializeHandler (
IN CONST DYN_HII_SERIALIZE_FN *Table,
IN UINTN TableCount,
IN UINTN Type,
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
DYN_HII_SERIALIZE_FN Handler;
if ((Table == NULL) || (Hdr == NULL) || (BufInfo == NULL) ||
(Type == 0) || (Type >= TableCount))
{
return EFI_INVALID_PARAMETER;
}
Handler = Table[Type];
if (Handler == NULL) {
return EFI_UNSUPPORTED;
}
return Handler (Hdr, BufInfo);
}
/** Insert the EFI_IFR_END opcode into the IFR buffer.
Insert the EFI_IFR_END opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiInsertEndOp (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 EndSize;
EFI_STATUS Status;
EFI_IFR_END End;
if (Hdr->Scope == 0) {
return EFI_SUCCESS;
}
EndSize = sizeof (EFI_IFR_END);
if (EndSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, EndSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
End.Header.OpCode = EFI_IFR_END_OP;
End.Header.Length = (UINT8)EndSize;
End.Header.Scope = 0;
CopyMem (Buf, &End, sizeof (EFI_IFR_END));
BufInfo->Position += sizeof (EFI_IFR_END);
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_VARSTORE opcode into the IFR buffer.
Insert the EFI_IFR_VARSTORE opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeVarstoreBuffer (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 StrSize;
UINT32 VarstoreSize;
EFI_STATUS Status;
EFI_IFR_VARSTORE Varstore;
DYN_HII_VARSTORE *VarstoreNode;
DYN_HII_VARSTORE_BUFFER_DATA *VarstoreBuf;
VarstoreNode = (DYN_HII_VARSTORE *)Hdr;
VarstoreBuf = &VarstoreNode->Data.Buffer;
StrSize = (UINT32)AsciiStrSize (VarstoreBuf->Name);
VarstoreSize = OFFSET_OF (EFI_IFR_VARSTORE, Name) + StrSize;
if (VarstoreSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, VarstoreSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Varstore.Header.OpCode = EFI_IFR_VARSTORE_OP;
Varstore.Header.Length = (UINT8)VarstoreSize;
Varstore.Header.Scope = 0;
CopyMem (&Varstore.Guid, &VarstoreBuf->Guid, sizeof (EFI_GUID));
Varstore.VarStoreId = VarstoreBuf->VarstoreId;
Varstore.Size = VarstoreBuf->Size;
CopyMem (Buf, &Varstore, OFFSET_OF (EFI_IFR_VARSTORE, Name));
BufInfo->Position += OFFSET_OF (EFI_IFR_VARSTORE, Name);
Buf += OFFSET_OF (EFI_IFR_VARSTORE, Name);
Status = AsciiStrCpyS ((CHAR8 *)Buf, StrSize, VarstoreBuf->Name);
if (EFI_ERROR (Status)) {
return Status;
}
BufInfo->Position += StrSize;
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_VARSTORE_EFI opcode into the IFR buffer.
Insert the EFI_IFR_VARSTORE_EFI opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeVarstoreEfi (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 StrSize;
UINT32 VarstoreSize;
EFI_STATUS Status;
EFI_IFR_VARSTORE_EFI Varstore;
DYN_HII_VARSTORE *VarstoreNode;
DYN_HII_VARSTORE_EFI_DATA *VarstoreEfi;
VarstoreNode = (DYN_HII_VARSTORE *)Hdr;
VarstoreEfi = &VarstoreNode->Data.Efi;
StrSize = (UINT32)AsciiStrSize (VarstoreEfi->Name);
VarstoreSize = OFFSET_OF (EFI_IFR_VARSTORE_EFI, Name) + StrSize;
if (VarstoreSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, VarstoreSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Varstore.Header.OpCode = EFI_IFR_VARSTORE_EFI_OP;
Varstore.Header.Length = (UINT8)VarstoreSize;
Varstore.Header.Scope = 0;
CopyMem (&Varstore.Guid, &VarstoreEfi->Guid, sizeof (EFI_GUID));
Varstore.VarStoreId = VarstoreEfi->VarstoreId;
Varstore.Attributes = VarstoreEfi->Attributes;
Varstore.Size = VarstoreEfi->Size;
CopyMem (Buf, &Varstore, OFFSET_OF (EFI_IFR_VARSTORE_EFI, Name));
BufInfo->Position += OFFSET_OF (EFI_IFR_VARSTORE_EFI, Name);
Buf += OFFSET_OF (EFI_IFR_VARSTORE_EFI, Name);
Status = AsciiStrCpyS ((CHAR8 *)Buf, StrSize, VarstoreEfi->Name);
if (EFI_ERROR (Status)) {
return Status;
}
BufInfo->Position += StrSize;
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_VARSTORE_NAME_VALUE opcode into the IFR buffer.
Insert the EFI_IFR_VARSTORE_NAME_VALUE opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeVarstoreNameValue (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 VarstoreSize;
EFI_STATUS Status;
EFI_IFR_VARSTORE_NAME_VALUE Varstore;
DYN_HII_VARSTORE *VarstoreNode;
DYN_HII_VARSTORE_NAME_VALUE_DATA *VarstoreNameValue;
VarstoreNode = (DYN_HII_VARSTORE *)Hdr;
VarstoreNameValue = &VarstoreNode->Data.NameValue;
VarstoreSize = sizeof (EFI_IFR_VARSTORE_NAME_VALUE);
if (VarstoreSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, VarstoreSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Varstore.Header.OpCode = EFI_IFR_VARSTORE_NAME_VALUE_OP;
Varstore.Header.Length = (UINT8)VarstoreSize;
Varstore.Header.Scope = 0;
CopyMem (&Varstore.Guid, &VarstoreNameValue->Guid, sizeof (EFI_GUID));
Varstore.VarStoreId = VarstoreNameValue->VarstoreId;
CopyMem (Buf, &Varstore, sizeof (EFI_IFR_VARSTORE_NAME_VALUE));
BufInfo->Position += sizeof (EFI_IFR_VARSTORE_NAME_VALUE);
return EFI_SUCCESS;
}
/** Serialize the varstore data into the IFR buffer.
Check for the type of varstore and call the appropriate function for
serializing the varstore data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_INVALID_PARAMETER Invalid varstore type.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeVarstore (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
CONST DYN_HII_VARSTORE *VarstoreNode;
static CONST DYN_HII_SERIALIZE_FN VarstoreSerializers[DynHiiVarstoreMax] = {
NULL, // 0 is invalid
DynHiiSerializeVarstoreBuffer, // DynHiiVarstoreBuffer
DynHiiSerializeVarstoreEfi, // DynHiiVarstoreEfi
DynHiiSerializeVarstoreNameValue // DynHiiVarstoreNameValue
};
if ((Hdr == NULL) || (Hdr->Type != DynHiiNodeVarstore)) {
return EFI_INVALID_PARAMETER;
}
VarstoreNode = (CONST DYN_HII_VARSTORE *)Hdr;
if (VarstoreNode->VarstoreType == 0) {
return EFI_INVALID_PARAMETER;
}
return DynHiiDispatchSerializeHandler (
VarstoreSerializers,
ARRAY_SIZE (VarstoreSerializers),
VarstoreNode->VarstoreType,
Hdr,
BufInfo
);
}
/** Insert the EFI_IFR_FORM opcode into the IFR buffer.
Insert the EFI_IFR_FORM opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeForm (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 FormSize;
EFI_STATUS Status;
EFI_IFR_FORM Form;
DYN_HII_FORM *FormNode;
ASSERT (Hdr->Type == DynHiiNodeForm);
FormNode = (DYN_HII_FORM *)Hdr;
FormSize = sizeof (EFI_IFR_FORM);
if (FormSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, FormSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Form.Header.OpCode = EFI_IFR_FORM_OP;
Form.Header.Length = (UINT8)FormSize;
Form.Header.Scope = 1;
Form.FormId = FormNode->FormId;
Form.FormTitle = FormNode->Title;
CopyMem (Buf, &Form, sizeof (EFI_IFR_FORM));
BufInfo->Position += sizeof (EFI_IFR_FORM);
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_FORM_SET opcode into the IFR buffer.
Insert the EFI_IFR_FORM_SET opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeFormset (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT8 Idx;
UINT32 FormsetSize;
EFI_STATUS Status;
EFI_IFR_FORM_SET Formset;
DYN_HII_FORMSET *FormsetNode;
ASSERT (Hdr->Type == DynHiiNodeFormset);
FormsetNode = (DYN_HII_FORMSET *)Hdr;
ASSERT (FormsetNode->ClassGuidCount <= 3);
FormsetSize = sizeof (EFI_IFR_FORM_SET) + FormsetNode->ClassGuidCount * sizeof (EFI_GUID);
if (FormsetSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, FormsetSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Formset.Header.OpCode = EFI_IFR_FORM_SET_OP;
Formset.Header.Length = (UINT8)FormsetSize;
Formset.Header.Scope = 1;
CopyMem (&Formset.Guid, &FormsetNode->FormsetGuid, sizeof (EFI_GUID));
Formset.FormSetTitle = FormsetNode->Title;
Formset.Help = FormsetNode->Help;
Formset.Flags = FormsetNode->ClassGuidCount & 0x3;
CopyMem (Buf, &Formset, sizeof (EFI_IFR_FORM_SET));
BufInfo->Position += sizeof (EFI_IFR_FORM_SET);
Buf = BufInfo->Data + BufInfo->Position;
for (Idx = 0; Idx < Formset.Flags; Idx++) {
CopyMem ((EFI_GUID *)Buf, &FormsetNode->ClassGuid[Idx], sizeof (EFI_GUID));
Buf += sizeof (EFI_GUID);
BufInfo->Position += sizeof (EFI_GUID);
}
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_CHECKBOX opcode into the IFR buffer.
Insert the EFI_IFR_CHECKBOX opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeCheckbox (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 CheckboxSize;
EFI_STATUS Status;
EFI_IFR_CHECKBOX Checkbox;
DYN_HII_STATEMENT_DATA *Data;
DYN_HII_STATEMENT *StatementNode;
StatementNode = (DYN_HII_STATEMENT *)Hdr;
Data = &StatementNode->Data;
CheckboxSize = sizeof (EFI_IFR_CHECKBOX);
if (CheckboxSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, CheckboxSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Checkbox.Header.OpCode = EFI_IFR_CHECKBOX_OP;
Checkbox.Header.Length = (UINT8)CheckboxSize;
Checkbox.Header.Scope = 1;
Checkbox.Question = Data->QuestionHdr;
Checkbox.Flags = Data->Statement.Checkbox.Flags;
CopyMem (Buf, &Checkbox, sizeof (EFI_IFR_CHECKBOX));
BufInfo->Position += sizeof (EFI_IFR_CHECKBOX);
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_ONE_OF opcode into the IFR buffer.
Insert the EFI_IFR_ONE_OF opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_INVALID_PARAMETER Invalid data size.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeOneOf (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 OneOfSize;
EFI_STATUS Status;
EFI_IFR_ONE_OF OneOf;
DYN_HII_ONE_OF_DATA *OneOfData;
DYN_HII_STATEMENT_DATA *Data;
DYN_HII_STATEMENT *StatementNode;
StatementNode = (DYN_HII_STATEMENT *)Hdr;
Data = &StatementNode->Data;
OneOfData = &Data->Statement.OneOf;
OneOfSize = OFFSET_OF (EFI_IFR_ONE_OF, data);
switch (OneOfData->Flags & EFI_IFR_NUMERIC_SIZE) {
case EFI_IFR_NUMERIC_SIZE_1:
OneOfSize += sizeof (OneOf.data.u8);
break;
case EFI_IFR_NUMERIC_SIZE_2:
OneOfSize += sizeof (OneOf.data.u16);
break;
case EFI_IFR_NUMERIC_SIZE_4:
OneOfSize += sizeof (OneOf.data.u32);
break;
case EFI_IFR_NUMERIC_SIZE_8:
OneOfSize += sizeof (OneOf.data.u64);
break;
default:
return EFI_INVALID_PARAMETER;
}
if (OneOfSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, OneOfSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
OneOf.Header.OpCode = EFI_IFR_ONE_OF_OP;
OneOf.Header.Length = (UINT8)OneOfSize;
OneOf.Header.Scope = 1;
OneOf.Question = Data->QuestionHdr;
OneOf.Flags = OneOfData->Flags;
switch (OneOf.Flags & EFI_IFR_NUMERIC_SIZE) {
case EFI_IFR_NUMERIC_SIZE_1:
OneOf.data.u8.MinValue = (UINT8)OneOfData->MinValue;
OneOf.data.u8.MaxValue = (UINT8)OneOfData->MaxValue;
OneOf.data.u8.Step = (UINT8)OneOfData->Step;
break;
case EFI_IFR_NUMERIC_SIZE_2:
OneOf.data.u16.MinValue = (UINT16)OneOfData->MinValue;
OneOf.data.u16.MaxValue = (UINT16)OneOfData->MaxValue;
OneOf.data.u16.Step = (UINT16)OneOfData->Step;
break;
case EFI_IFR_NUMERIC_SIZE_4:
OneOf.data.u32.MinValue = (UINT32)OneOfData->MinValue;
OneOf.data.u32.MaxValue = (UINT32)OneOfData->MaxValue;
OneOf.data.u32.Step = (UINT32)OneOfData->Step;
break;
case EFI_IFR_NUMERIC_SIZE_8:
OneOf.data.u64.MinValue = (UINT64)OneOfData->MinValue;
OneOf.data.u64.MaxValue = (UINT64)OneOfData->MaxValue;
OneOf.data.u64.Step = (UINT64)OneOfData->Step;
break;
default:
return EFI_INVALID_PARAMETER;
}
CopyMem (Buf, &OneOf, OneOfSize);
BufInfo->Position += OneOfSize;
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_NUMERIC opcode into the IFR buffer.
Insert the EFI_IFR_NUMERIC opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_INVALID_PARAMETER Invalid data size.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeNumeric (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 NumericSize;
EFI_STATUS Status;
EFI_IFR_NUMERIC Numeric;
DYN_HII_NUMERIC_DATA *NumericData;
DYN_HII_STATEMENT_DATA *Data;
DYN_HII_STATEMENT *StatementNode;
StatementNode = (DYN_HII_STATEMENT *)Hdr;
Data = &StatementNode->Data;
NumericData = &Data->Statement.Numeric;
NumericSize = OFFSET_OF (EFI_IFR_NUMERIC, data);
switch (NumericData->Flags & EFI_IFR_NUMERIC_SIZE) {
case EFI_IFR_NUMERIC_SIZE_1:
NumericSize += sizeof (Numeric.data.u8);
break;
case EFI_IFR_NUMERIC_SIZE_2:
NumericSize += sizeof (Numeric.data.u16);
break;
case EFI_IFR_NUMERIC_SIZE_4:
NumericSize += sizeof (Numeric.data.u32);
break;
case EFI_IFR_NUMERIC_SIZE_8:
NumericSize += sizeof (Numeric.data.u64);
break;
default:
return EFI_INVALID_PARAMETER;
}
if (NumericSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, NumericSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Numeric.Header.OpCode = EFI_IFR_NUMERIC_OP;
Numeric.Header.Length = (UINT8)NumericSize;
Numeric.Header.Scope = 1;
Numeric.Question = Data->QuestionHdr;
Numeric.Flags = NumericData->Flags;
switch (Numeric.Flags & EFI_IFR_NUMERIC_SIZE) {
case EFI_IFR_NUMERIC_SIZE_1:
Numeric.data.u8.MinValue = (UINT8)NumericData->MinValue;
Numeric.data.u8.MaxValue = (UINT8)NumericData->MaxValue;
Numeric.data.u8.Step = (UINT8)NumericData->Step;
break;
case EFI_IFR_NUMERIC_SIZE_2:
Numeric.data.u16.MinValue = (UINT16)NumericData->MinValue;
Numeric.data.u16.MaxValue = (UINT16)NumericData->MaxValue;
Numeric.data.u16.Step = (UINT16)NumericData->Step;
break;
case EFI_IFR_NUMERIC_SIZE_4:
Numeric.data.u32.MinValue = (UINT32)NumericData->MinValue;
Numeric.data.u32.MaxValue = (UINT32)NumericData->MaxValue;
Numeric.data.u32.Step = (UINT32)NumericData->Step;
break;
case EFI_IFR_NUMERIC_SIZE_8:
Numeric.data.u64.MinValue = (UINT64)NumericData->MinValue;
Numeric.data.u64.MaxValue = (UINT64)NumericData->MaxValue;
Numeric.data.u64.Step = (UINT64)NumericData->Step;
break;
default:
return EFI_INVALID_PARAMETER;
}
CopyMem (Buf, &Numeric, NumericSize);
BufInfo->Position += NumericSize;
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_REF opcode into the IFR buffer.
Insert the EFI_IFR_REF opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeRef1 (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 RefSize;
EFI_STATUS Status;
EFI_IFR_REF Ref;
DYN_HII_REF_DATA *RefData;
DYN_HII_STATEMENT *StatementNode;
StatementNode = (DYN_HII_STATEMENT *)Hdr;
RefData = &StatementNode->Data.Statement.Ref;
RefSize = sizeof (EFI_IFR_REF);
if (RefSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, RefSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Ref.Header.OpCode = EFI_IFR_REF_OP;
Ref.Header.Length = (UINT8)RefSize;
Ref.Header.Scope = 0;
Ref.Question = StatementNode->Data.QuestionHdr;
Ref.FormId = RefData->FormId;
CopyMem (Buf, &Ref, sizeof (EFI_IFR_REF));
BufInfo->Position += sizeof (EFI_IFR_REF);
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_REF2 opcode into the IFR buffer.
Insert the EFI_IFR_REF2 opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeRef2 (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 RefSize;
EFI_STATUS Status;
EFI_IFR_REF2 Ref;
DYN_HII_REF_DATA *RefData;
DYN_HII_STATEMENT *StatementNode;
StatementNode = (DYN_HII_STATEMENT *)Hdr;
RefData = &StatementNode->Data.Statement.Ref;
RefSize = sizeof (EFI_IFR_REF2);
if (RefSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, RefSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Ref.Header.OpCode = EFI_IFR_REF_OP;
Ref.Header.Length = (UINT8)RefSize;
Ref.Header.Scope = 0;
Ref.Question = StatementNode->Data.QuestionHdr;
Ref.FormId = RefData->FormId;
Ref.QuestionId = RefData->QuestionId;
CopyMem (Buf, &Ref, sizeof (EFI_IFR_REF2));
BufInfo->Position += sizeof (EFI_IFR_REF2);
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_REF3 opcode into the IFR buffer.
Insert the EFI_IFR_REF3 opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeRef3 (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 RefSize;
EFI_STATUS Status;
EFI_IFR_REF3 Ref;
DYN_HII_REF_DATA *RefData;
DYN_HII_STATEMENT *StatementNode;
StatementNode = (DYN_HII_STATEMENT *)Hdr;
RefData = &StatementNode->Data.Statement.Ref;
RefSize = sizeof (EFI_IFR_REF3);
if (RefSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, RefSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Ref.Header.OpCode = EFI_IFR_REF_OP;
Ref.Header.Length = (UINT8)RefSize;
Ref.Header.Scope = 0;
Ref.Question = StatementNode->Data.QuestionHdr;
Ref.FormId = RefData->FormId;
Ref.QuestionId = RefData->QuestionId;
CopyMem (&Ref.FormSetId, &RefData->FormSetGuid, sizeof (EFI_GUID));
CopyMem (Buf, &Ref, sizeof (EFI_IFR_REF3));
BufInfo->Position += sizeof (EFI_IFR_REF3);
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_REF4 opcode into the IFR buffer.
Insert the EFI_IFR_REF4 opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeRef4 (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 RefSize;
EFI_STATUS Status;
EFI_IFR_REF4 Ref;
DYN_HII_REF_DATA *RefData;
DYN_HII_STATEMENT *StatementNode;
StatementNode = (DYN_HII_STATEMENT *)Hdr;
RefData = &StatementNode->Data.Statement.Ref;
RefSize = sizeof (EFI_IFR_REF4);
if (RefSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, RefSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Ref.Header.OpCode = EFI_IFR_REF_OP;
Ref.Header.Length = (UINT8)RefSize;
Ref.Header.Scope = 0;
Ref.Question = StatementNode->Data.QuestionHdr;
Ref.FormId = RefData->FormId;
Ref.QuestionId = RefData->QuestionId;
Ref.DevicePath = RefData->DevicePath;
CopyMem (&Ref.FormSetId, &RefData->FormSetGuid, sizeof (EFI_GUID));
CopyMem (Buf, &Ref, sizeof (EFI_IFR_REF4));
BufInfo->Position += sizeof (EFI_IFR_REF4);
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_SUBTITLE opcode into the IFR buffer.
Insert the EFI_IFR_SUBTITLE opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeSubtitle (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 SubtitleSize;
EFI_STATUS Status;
EFI_IFR_SUBTITLE Subtitle;
DYN_HII_SUBTITLE_DATA *SubtitleData;
DYN_HII_STATEMENT *StatementNode;
StatementNode = (DYN_HII_STATEMENT *)Hdr;
SubtitleData = &StatementNode->Data.Statement.Subtitle;
SubtitleSize = sizeof (EFI_IFR_SUBTITLE);
if (SubtitleSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, SubtitleSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Subtitle.Header.OpCode = EFI_IFR_SUBTITLE_OP;
Subtitle.Header.Length = (UINT8)SubtitleSize;
Subtitle.Header.Scope = 0;
Subtitle.Statement.Prompt = SubtitleData->Prompt;
Subtitle.Statement.Help = SubtitleData->Help;
Subtitle.Flags = SubtitleData->Flags;
CopyMem (Buf, &Subtitle, sizeof (EFI_IFR_SUBTITLE));
BufInfo->Position += sizeof (EFI_IFR_SUBTITLE);
return EFI_SUCCESS;
}
/** Serialize the cross-reference statement data into the IFR buffer.
Check for the type of cross-reference statement and call the
appropriate function for serializing the ref data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_UNSUPPORTED Currently unsupported Ref type.
@retval EFI_INVALID_PARAMETER One of the input parameters was invalid.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeRef (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
CONST DYN_HII_STATEMENT *StatementNode;
CONST DYN_HII_REF_DATA *RefData;
static CONST DYN_HII_SERIALIZE_FN RefSerializers[DynHiiRefMax] = {
NULL, // 0 is invalid
DynHiiSerializeRef1, // DynHiiRef1
DynHiiSerializeRef2, // DynHiiRef2
DynHiiSerializeRef3, // DynHiiRef3
DynHiiSerializeRef4, // DynHiiRef4
NULL // DynHiiRef5, unsupported
};
if ((Hdr == NULL) || (Hdr->Type != DynHiiNodeStatement)) {
return EFI_INVALID_PARAMETER;
}
StatementNode = (CONST DYN_HII_STATEMENT *)Hdr;
RefData = &StatementNode->Data.Statement.Ref;
return DynHiiDispatchSerializeHandler (
RefSerializers,
ARRAY_SIZE (RefSerializers),
RefData->RefType,
Hdr,
BufInfo
);
}
/** Serialize the statement/question data into the IFR buffer.
Check for the type of statement/question and call the appropriate
function for serializing the data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_UNSUPPORTED Serialization function currently not supported
for the statement type.
@retval EFI_INVALID_PARAMETER One of the input parameters was invalid.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeStatement (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
CONST DYN_HII_STATEMENT *StatementNode;
static CONST DYN_HII_SERIALIZE_FN StatementSerializers[DynHiiStMax] = {
NULL, // 0 is invalid
DynHiiSerializeCheckbox, // DynHiiStCheckbox
DynHiiSerializeNumeric, // DynHiiStNumeric
DynHiiSerializeOneOf, // DynHiiStOneOf
DynHiiSerializeRef, // DynHiiStRef
DynHiiSerializeSubtitle, // DynHiiStSubtitle
NULL, // DynHiiStOrderedList, unsupported
NULL, // DynHiiStAction, unsupported
NULL, // DynHiiStString, unsupported
NULL, // DynHiiStText, unsupported
NULL, // DynHiiStPassword, unsupported
};
if ((Hdr == NULL) || (BufInfo == NULL) ||
(Hdr->Type != DynHiiNodeStatement))
{
return EFI_INVALID_PARAMETER;
}
StatementNode = (CONST DYN_HII_STATEMENT *)Hdr;
return DynHiiDispatchSerializeHandler (
StatementSerializers,
ARRAY_SIZE (StatementSerializers),
StatementNode->StatementType,
Hdr,
BufInfo
);
}
/** Insert the EFI_IFR_ONE_OF_OPTION opcode into the IFR buffer.
Insert the EFI_IFR_ONE_OF_OPTION opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeOption (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 OptionSize;
EFI_STATUS Status;
EFI_IFR_ONE_OF_OPTION Option;
DYN_HII_ONE_OF_OPTION *OptionNode;
OptionNode = (DYN_HII_ONE_OF_OPTION *)Hdr;
OptionSize = OFFSET_OF (EFI_IFR_ONE_OF_OPTION, Value);
switch (OptionNode->Type) {
case EFI_IFR_TYPE_NUM_SIZE_8:
OptionSize += sizeof (UINT8);
break;
case EFI_IFR_TYPE_NUM_SIZE_16:
OptionSize += sizeof (UINT16);
break;
case EFI_IFR_TYPE_NUM_SIZE_32:
OptionSize += sizeof (UINT32);
break;
case EFI_IFR_TYPE_NUM_SIZE_64:
OptionSize += sizeof (UINT64);
break;
default:
return EFI_INVALID_PARAMETER;
}
if (OptionSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, OptionSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Option.Header.OpCode = EFI_IFR_ONE_OF_OPTION_OP;
Option.Header.Length = (UINT8)OptionSize;
Option.Header.Scope = 0;
Option.Option = OptionNode->Text;
Option.Flags = OptionNode->Flags;
Option.Type = OptionNode->Type;
Option.Value = OptionNode->Value;
CopyMem (Buf, &Option, OptionSize);
BufInfo->Position += OptionSize;
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_DEFAULT opcode into the IFR buffer.
Insert the EFI_IFR_DEFAULT opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeDefault (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 DefaultSize;
EFI_STATUS Status;
EFI_IFR_DEFAULT Default;
DYN_HII_DEFAULT *DefaultNode;
DefaultNode = (DYN_HII_DEFAULT *)Hdr;
DefaultSize = OFFSET_OF (EFI_IFR_DEFAULT, Value);
switch (DefaultNode->Type) {
case EFI_IFR_TYPE_NUM_SIZE_8:
DefaultSize += sizeof (UINT8);
break;
case EFI_IFR_TYPE_NUM_SIZE_16:
DefaultSize += sizeof (UINT16);
break;
case EFI_IFR_TYPE_NUM_SIZE_32:
DefaultSize += sizeof (UINT32);
break;
case EFI_IFR_TYPE_NUM_SIZE_64:
DefaultSize += sizeof (UINT64);
break;
case EFI_IFR_TYPE_BOOLEAN:
DefaultSize += sizeof (BOOLEAN);
break;
default:
return EFI_INVALID_PARAMETER;
}
if (DefaultSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, DefaultSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Default.Header.OpCode = EFI_IFR_DEFAULT_OP;
Default.Header.Length = (UINT8)DefaultSize;
Default.Header.Scope = 0;
Default.DefaultId = DefaultNode->DefaultId;
Default.Type = DefaultNode->Type;
Default.Value = DefaultNode->Value;
CopyMem (Buf, &Default, DefaultSize);
BufInfo->Position += DefaultSize;
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_DEFAULTSTORE opcode into the IFR buffer.
In absence of an explicit addition of a defaultstore, insert the implicit
EFI_IFR_DEFAULTSTORE opcode data into the IFR buffer.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiAddImplicitDefaultstores (
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 Idx;
UINT8 *Buf;
UINT32 DefaultstoreSize;
EFI_STATUS Status;
EFI_IFR_DEFAULTSTORE Defaultstore;
DefaultstoreSize = 2 * sizeof (EFI_IFR_DEFAULTSTORE);
Status = DynHiiIfrBufferSizeCheck (BufInfo, DefaultstoreSize);
if (EFI_ERROR (Status)) {
return Status;
}
DefaultstoreSize = sizeof (EFI_IFR_DEFAULTSTORE);
if (DefaultstoreSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Buf = BufInfo->Data + BufInfo->Position;
Defaultstore.Header.OpCode = EFI_IFR_DEFAULTSTORE_OP;
Defaultstore.Header.Length = (UINT8)DefaultstoreSize;
Defaultstore.Header.Scope = 0;
Defaultstore.DefaultName = 0x0;
for (Idx = 0; Idx < 2; Idx++) {
Defaultstore.DefaultId = Idx;
CopyMem (Buf, &Defaultstore, sizeof (EFI_IFR_DEFAULTSTORE));
Buf += sizeof (EFI_IFR_DEFAULTSTORE);
BufInfo->Position += sizeof (EFI_IFR_DEFAULTSTORE);
}
return EFI_SUCCESS;
}
/** Insert the EFI_IFR_DEFAULTSTORE opcode into the IFR buffer.
Insert the EFI_IFR_DEFAULTSTORE opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_BAD_BUFFER_SIZE Opcode data size greater than 127 bytes.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
DynHiiSerializeDefaultstore (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
UINT8 *Buf;
UINT32 DefaultstoreSize;
EFI_STATUS Status;
EFI_IFR_DEFAULTSTORE Defaultstore;
DYN_HII_DEFAULTSTORE *DefaultstoreNode;
DefaultstoreNode = (DYN_HII_DEFAULTSTORE *)Hdr;
DefaultstoreSize = sizeof (EFI_IFR_DEFAULTSTORE);
if (DefaultstoreSize > MAX_HII_OPCODE_LENGTH) {
return EFI_BAD_BUFFER_SIZE;
}
Status = DynHiiIfrBufferSizeCheck (BufInfo, DefaultstoreSize);
if (EFI_ERROR (Status)) {
return Status;
}
Buf = BufInfo->Data + BufInfo->Position;
Defaultstore.Header.OpCode = EFI_IFR_DEFAULTSTORE_OP;
Defaultstore.Header.Length = (UINT8)DefaultstoreSize;
Defaultstore.Header.Scope = 0;
Defaultstore.DefaultName = DefaultstoreNode->DefaultName;
Defaultstore.DefaultId = DefaultstoreNode->DefaultId;
CopyMem (Buf, &Defaultstore, sizeof (EFI_IFR_DEFAULTSTORE));
BufInfo->Position += sizeof (EFI_IFR_DEFAULTSTORE);
return EFI_SUCCESS;
}
/** Serialize the given HII node.
Check for the type of the node, and call the appropriate function for
inserting the opcode data into the IFR buffer.
@param [in] Hdr Pointer to the node header.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS The opcode was added successfully to the IFR
buffer.
@retval EFI_UNSUPPORTED The node is currently unsupported.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
static
EFI_STATUS
EFIAPI
DynHiiSerializeNode (
IN CONST DYN_HII_NODE_HDR *Hdr,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
static CONST DYN_HII_SERIALIZE_FN NodeSerializers[DynHiiNodeMax] = {
NULL, // 0 is invalid
DynHiiSerializeFormset, // DynHiiNodeFormset
DynHiiSerializeForm, // DynHiiNodeForm
DynHiiSerializeDefaultstore, // DynHiiNodeDefaultStore
DynHiiSerializeVarstore, // DynHiiNodeVarstore
DynHiiSerializeStatement, // DynHiiNodeStatement
DynHiiSerializeOption, // DynHiiNodeOption
DynHiiSerializeDefault, // DynHiiNodeDefault
NULL, // DynHiiNodeString, unsupported
NULL // DynHiiNodeExpression, unsupported
};
if ((Hdr == NULL) || (BufInfo == NULL)) {
return EFI_INVALID_PARAMETER;
}
return DynHiiDispatchSerializeHandler (
NodeSerializers,
ARRAY_SIZE (NodeSerializers),
Hdr->Type,
Hdr,
BufInfo
);
}
/** Process the statement child based on it's type
Iterate through the list of a statement children, and serialize a child
node if it matches the NodeType argument passed to this function. This
ensures that child nodes of a certain type are clubbed together in the
IFR byte-stream.
@param [in] ChildList List of child nodes of a Statement/Question.
@param [in] NodeType Type of child node to be serialized.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS If the processing was successfull.
EFI_INVALID_PARAMETER Any invalid child node type found.
**/
static
EFI_STATUS
DynHiiSerializeStatementChild (
IN CONST LIST_ENTRY *ChildList,
IN DYN_HII_NODE_TYPE NodeType,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
LIST_ENTRY *Link;
EFI_STATUS Status;
DYN_HII_NODE_HDR *Hdr;
Link = GetFirstNode (ChildList);
while (!IsNull (ChildList, Link)) {
Hdr = BASE_CR (Link, DYN_HII_NODE_HDR, Link);
if (Hdr->Type == NodeType) {
Status = DynHiiSerializeNode (Hdr, BufInfo);
if (EFI_ERROR (Status)) {
return Status;
}
}
if ((Hdr->Type != DynHiiNodeDefault) && (Hdr->Type != DynHiiNodeOption)) {
return EFI_INVALID_PARAMETER;
}
Link = GetNextNode (ChildList, Link);
}
return EFI_SUCCESS;
}
/** Process the statement/question node.
Serialize the statement/question node data by calling the corresponding
function. Check for any question defaults, and process them if present.
Finally, add the end opcode for the statement if the statement is scoped.
@param [in] Statement Statement/Question to be processed.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS If the processing was successfull or any error code
returned by the serialization functions.
**/
static
EFI_STATUS
DynHiiProcessStatement (
IN CONST DYN_HII_STATEMENT *Statement,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
EFI_STATUS Status;
Status = DynHiiSerializeNode (&Statement->Hdr, BufInfo);
if (EFI_ERROR (Status)) {
return Status;
}
Status = DynHiiSerializeStatementChild (
&Statement->ChildList,
DynHiiNodeOption,
BufInfo
);
if (EFI_ERROR (Status)) {
return Status;
}
Status = DynHiiSerializeStatementChild (
&Statement->ChildList,
DynHiiNodeDefault,
BufInfo
);
if (EFI_ERROR (Status)) {
return Status;
}
return DynHiiInsertEndOp (&Statement->Hdr, BufInfo);
}
/** Iterate the statement list.
Iterate through the statement list, and for each node found, call the
function to serialize the process the node data.
@param [in] StatementList List of statement nodes.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS If the processing was successfull or any error code
returned by the called functions.
**/
static
EFI_STATUS
DynHiiIterateStatementList (
IN CONST LIST_ENTRY *StatementList,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
EFI_STATUS Status;
LIST_ENTRY *Link;
DYN_HII_STATEMENT *Statement;
Link = GetFirstNode (StatementList);
while (!IsNull (StatementList, Link)) {
Statement = BASE_CR (Link, DYN_HII_STATEMENT, Hdr.Link);
Status = DynHiiProcessStatement (Statement, BufInfo);
if (EFI_ERROR (Status)) {
return Status;
}
Link = GetNextNode (StatementList, Link);
}
return EFI_SUCCESS;
}
/** Process the form node.
Serialize the form node data by calling the corresponding function. Process
the Statements under the form. Finally, add the end opcode for the form.
@param [in] Form Pointer to the Form node.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS If the processing was successfull or any error code
returned by the called functions.
**/
static
EFI_STATUS
DynHiiProcessForm (
IN CONST DYN_HII_FORM *Form,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
EFI_STATUS Status;
Status = DynHiiSerializeNode (&Form->Hdr, BufInfo);
if (EFI_ERROR (Status)) {
return Status;
}
Status = DynHiiIterateStatementList (&Form->ChildList, BufInfo);
if (EFI_ERROR (Status)) {
return Status;
}
return DynHiiInsertEndOp (&Form->Hdr, BufInfo);
}
/** Iterate the varstore list.
Iterate through the varstore list, and for each node found, call the function
to serialize the node data into the IFR buffer.
@param [in] VarstoreList List of default nodes.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS If the serialization was successfull or any error code
returned by the serialization function.
**/
static
EFI_STATUS
DynHiiIterateVarstoreList (
IN CONST LIST_ENTRY *VarstoreList,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
EFI_STATUS Status;
LIST_ENTRY *Link;
DYN_HII_VARSTORE *Varstore;
Link = GetFirstNode (VarstoreList);
while (!IsNull (VarstoreList, Link)) {
Varstore = BASE_CR (Link, DYN_HII_VARSTORE, Hdr.Link);
Status = DynHiiSerializeNode (&Varstore->Hdr, BufInfo);
if (EFI_ERROR (Status)) {
return Status;
}
Link = GetNextNode (VarstoreList, Link);
}
return EFI_SUCCESS;
}
/** Iterate the form list.
Iterate through the form list, and for each node found, call the function
to process the form.
@param [in] FormList List of form nodes.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS If the serialization was successfull or any error code
returned by the called functions.
**/
static
EFI_STATUS
DynHiiIterateFormList (
IN CONST LIST_ENTRY *FormList,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
EFI_STATUS Status;
LIST_ENTRY *Link;
DYN_HII_FORM *Form;
Link = GetFirstNode (FormList);
while (!IsNull (FormList, Link)) {
Form = BASE_CR (Link, DYN_HII_FORM, Hdr.Link);
Status = DynHiiProcessForm (Form, BufInfo);
if (EFI_ERROR (Status)) {
return Status;
}
Link = GetNextNode (FormList, Link);
}
return EFI_SUCCESS;
}
/** Iterate the default store list.
Iterate through the default store list, and for each node found, call the
function to serialize the node into the IFR buffer.
@param [in] DefaultstoreList List of default store nodes.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS If the serialization was successfull or any error code
returned by the called functions.
**/
static
EFI_STATUS
DynHiiIterateDefaultstoreList (
IN CONST LIST_ENTRY *DefaultstoreList,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
EFI_STATUS Status;
LIST_ENTRY *Link;
DYN_HII_DEFAULTSTORE *Defaultstore;
Link = GetFirstNode (DefaultstoreList);
while (!IsNull (DefaultstoreList, Link)) {
Defaultstore = BASE_CR (Link, DYN_HII_DEFAULTSTORE, Hdr.Link);
Status = DynHiiSerializeNode (&Defaultstore->Hdr, BufInfo);
if (EFI_ERROR (Status)) {
return Status;
}
Link = GetNextNode (DefaultstoreList, Link);
}
return EFI_SUCCESS;
}
/** Process the formset node.
Serialize the formset node data by calling the corresponding function. Process
the child nodes of the formset, like varstores, forms. Finally, insert the
end opcode into the IFR buffer.
@param [in] Formset Pointer to the formset node.
@param [in] BufInfo Pointer to the DYN_HII_IFR_BUFFER structure.
@retval EFI_SUCCESS If the processing was successfull or any error code
returned by the called functions.
**/
static
EFI_STATUS
DynHiiProcessFormset (
IN CONST DYN_HII_FORMSET *Formset,
IN DYN_HII_IFR_BUFFER *BufInfo
)
{
EFI_STATUS Status;
Status = DynHiiSerializeNode (&Formset->Hdr, BufInfo);
if (EFI_ERROR (Status)) {
return Status;
}
if (!IsListEmpty (&Formset->DefaultstoreList)) {
Status = DynHiiIterateDefaultstoreList (
&Formset->DefaultstoreList,
BufInfo
);
if (EFI_ERROR (Status)) {
return Status;
}
} else {
Status = DynHiiAddImplicitDefaultstores (BufInfo);
if (EFI_ERROR (Status)) {
return Status;
}
}
Status = DynHiiIterateVarstoreList (&Formset->VarstoreList, BufInfo);
if (EFI_ERROR (Status)) {
return Status;
}
Status = DynHiiIterateFormList (&Formset->ChildList, BufInfo);
if (EFI_ERROR (Status)) {
return Status;
}
return DynHiiInsertEndOp (&Formset->Hdr, BufInfo);
}
/** Initialize the Form Package header.
Initialize the EFI_HII_PACKAGE_HEADER for the forms package.
@param [in] IfrBuf Pointer to the IFR byte buffer.
@param [in] Length Length of the Forms package, including the
preceding four bytes for array length.
**/
static
VOID
DynHiiInitFormPkgHdr (
IN UINT8 *IfrBuf,
IN UINT32 Length
)
{
UINT8 *PkgHdr;
UINT32 *Buf;
EFI_HII_PACKAGE_HEADER Hdr;
Buf = (UINT32 *)IfrBuf;
*Buf = Length;
PkgHdr = IfrBuf + sizeof (UINT32);
Hdr.Length = Length - sizeof (UINT32);
Hdr.Type = EFI_HII_PACKAGE_FORMS;
CopyMem (PkgHdr, &Hdr, sizeof (EFI_HII_PACKAGE_HEADER));
}
/** Dump the contents of the IFR buffer.
Useful for debugging.
@param [in] IfrBuf Pointer to the IFR byte buffer.
@param [in] Size Size in bytes of the IFR byte buffer.
**/
static
VOID
DumpIfrBuffer (
IN CONST UINT8 *IfrBuf,
IN CONST UINT32 Size
)
{
UINT32 Offset;
CONST UINT8 *Buf;
if (Size <= 8) {
return;
}
DEBUG ((
DEBUG_VERBOSE,
"INFO: %a: IfrBuf => %p, "
"IfrBuf Size => 0x%x\n",
__func__,
IfrBuf,
Size
));
DEBUG ((
DEBUG_VERBOSE,
"***** Generated IFR Byte Stream *****\n"
));
DEBUG ((
DEBUG_VERBOSE,
"IfrByteStream[] = {\n"
));
Buf = IfrBuf;
Offset = 0;
DUMP_HEX (DEBUG_VERBOSE, Offset, Buf, sizeof (UINT32), "");
Buf += sizeof (UINT32);
Offset += sizeof (UINT32);
DEBUG ((DEBUG_VERBOSE, "\n\n"));
DUMP_HEX (DEBUG_VERBOSE, Offset, Buf, sizeof (UINT32), "");
Buf += sizeof (UINT32);
Offset += sizeof (UINT32);
DEBUG ((DEBUG_VERBOSE, "\n\n"));
DUMP_HEX (DEBUG_VERBOSE, Offset, Buf, Size - 8, "");
DEBUG ((
DEBUG_VERBOSE,
"};\n\n"
));
}
/** Generate the IFR byte-stream for the Formset.
Serialize the Formset and it's child nodes to generate an IFR byte-stream
based Form package. This can then be added to the HII database through the
HiiAddPackages() call.
The serialized IFR buffer is passed back to the caller in IfrBuf. It is
the caller's responsibility to free up the IFR buffer through a call to
FreePool().
@param [in] FormsetHandle The formset hierarchy that needs to be
serialized.
@param [out] IfrBuf Pointer to the IFR data buffer.
@retval EFI_SUCCESS The Default was created successfully.
@retval EFI_INVALID_PARAMETER One of the input parameter is invalid.
@retval EFI_OUT_OF_RESOURCES Unable to allocate memory.
**/
EFI_STATUS
EFIAPI
DynHiiGenerateFormPackage (
IN CONST DYN_HII_HANDLE FormsetHandle,
OUT UINT8 **IfrBuf
)
{
UINT8 *Buf;
EFI_STATUS Status;
DYN_HII_FORMSET *Formset;
DYN_HII_IFR_BUFFER BufInfo;
Formset = FormsetHandle;
if ((Formset == NULL) || (Formset->Hdr.Type != DynHiiNodeFormset) ||
(IfrBuf == NULL))
{
return EFI_INVALID_PARAMETER;
}
*IfrBuf = NULL;
ZeroMem (&BufInfo, sizeof (DYN_HII_IFR_BUFFER));
Status = DynHiiInitIfrByteBuffer (&BufInfo);
if (Status == EFI_OUT_OF_RESOURCES) {
return Status;
}
Status = DynHiiProcessFormset (Formset, &BufInfo);
if (EFI_ERROR (Status)) {
goto err_handle;
}
Buf = ReallocatePool (
BufInfo.Size,
BufInfo.Position,
BufInfo.Data
);
if (Buf == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto err_handle;
}
DynHiiInitFormPkgHdr (Buf, BufInfo.Position);
*IfrBuf = Buf;
DumpIfrBuffer (Buf, BufInfo.Position);
return EFI_SUCCESS;
err_handle:
if (BufInfo.Data != NULL) {
FreePool (BufInfo.Data);
}
return Status;
}