forms.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 1,564 行 · 第 1/4 页

C
1,564
字号
/*++

Copyright (c) 2004 - 2006, Intel Corporation                                                         
All rights reserved. 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        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:

  Forms.c

Abstract:

  This file contains the form processing code to the HII database.

--*/

#include "HiiDatabase.h"
#include "IfrLibrary.h"

CHAR16*
Ascii2Unicode (
  OUT CHAR16         *UnicodeStr,
  IN  CHAR8          *AsciiStr
  )
/*++
  
  Routine Description:

    This function converts ASCII string to Unicode string.
  
  Arguments:

    UnicodeStr     - NULL terminated Unicode output string.
    AsciieStr      - NULL terminated ASCII input string.
 
  Returns: 

    Start of the Unicode ouput string.
    
--*/

{
  CHAR16      *Str = UnicodeStr;  
  while (TRUE) {
    *(UnicodeStr++) = (CHAR16) *AsciiStr;
    if (*(AsciiStr++) == '\0') {
      return Str;
    }
  }
}

CHAR8*
Unicode2Ascii (
  OUT CHAR8          *AsciiStr,
  IN  CHAR16         *UnicodeStr
  )
/*++
  
  Routine Description:

    This function converts Unicode string to ASCII string.
  
  Arguments:

    AsciieStr      - NULL terminated ASCII output string.
    UnicodeStr     - NULL terminated Unicode input string.
 
  Returns: 

    Start of the ASCII ouput string.
    
--*/

{
  CHAR8      *Str = AsciiStr;  
  while (TRUE) {
    *(AsciiStr++) = (CHAR8) *UnicodeStr;
    if (*(UnicodeStr++) == '\0') {
      return Str;
    }
  }
}

VOID
ExtractDevicePathData (
  IN     EFI_HII_DATA_TABLE   *DataTable,
  IN     UINT8                *IfrData,
  IN OUT UINT8                **ExportBufferPtr
  )
/*++

Routine Description:
  
Arguments:

Returns: 

--*/
{
  UINT8 *ExportBuffer;

  ExportBuffer = *ExportBufferPtr;

  //
  // BUGBUG - don't have devicepath data yet, setting dummy value
  //
  DataTable++;
  ExportBuffer  = (UINT8 *) DataTable;
  ((EFI_HII_DEVICE_PATH_PACK *) ExportBuffer)->Header.Type = EFI_HII_DEVICE_PATH;
  ((EFI_HII_DEVICE_PATH_PACK *) ExportBuffer)->Header.Length = (UINT32) (sizeof (EFI_HII_DEVICE_PATH_PACK) + sizeof (EFI_DEVICE_PATH_PROTOCOL));

  //
  // BUGBUG - part of hack - skip the Device Path Pack.....place some data
  //
  ExportBuffer  = ExportBuffer + sizeof (EFI_HII_DEVICE_PATH_PACK);

  ((EFI_DEVICE_PATH_PROTOCOL *) ExportBuffer)->Type     = EFI_END_ENTIRE_DEVICE_PATH;
  ((EFI_DEVICE_PATH_PROTOCOL *) ExportBuffer)->SubType  = EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;

  //
  // BUGBUG - still part of hack....
  //
  ExportBuffer      = ExportBuffer + sizeof (EFI_DEVICE_PATH_PROTOCOL);
  *ExportBufferPtr  = ExportBuffer;
}

VOID
ExtractVariableData (
  IN OUT EFI_HII_DATA_TABLE   *DataTable,
  IN     UINT8                *IfrData,
  IN OUT UINT8                **ExportBufferPtr
  )
/*++

Routine Description:

  This function extract the EFI_HII_VARIABLE_PACK portion from the 
  each of the EFI_HII_PACKAGE_INSTANCE in HII handle database.
  
Arguments:

  DataTable       – On input, this parameter point to the EFI_HII_DATA_TABLE structure
                    of the final data buffer for the EFI_HII_EXPORT interface. This function
                    update the NumberOfVariableData attribute.
  IfrData         - It points to a staring address of a EFI_HII_IFR_PACK structure.
  ExportBufferPtr – On input, it points the starting address of the data buffer to 
                    host the variable pack. On output, it is the starting address
                    of data buffer for the next extraction operation.
Returns: 

  VOID
  
--*/
{
  EFI_HII_VARIABLE_PACK       *VariableContents;
  UINT8                       *ExportBuffer;
  UINTN                       Index;
  UINTN                       Index2;
  UINTN                       TempValue;
  UINTN                       TempValue2;
  EFI_FORM_CALLBACK_PROTOCOL  *FormCallback;
  EFI_PHYSICAL_ADDRESS        CallbackHandle;
  EFI_STATUS                  Status;
  CHAR16                      *String;

  FormCallback    = NULL;
  CallbackHandle  = 0;
  ExportBuffer    = *ExportBufferPtr;

  for (Index = 0; IfrData[Index] != EFI_IFR_END_FORM_SET_OP;) {
    VariableContents = (EFI_HII_VARIABLE_PACK *) ExportBuffer;

    switch (IfrData[Index]) {
    case EFI_IFR_FORM_SET_OP:
      TempValue = EFI_HII_VARIABLE;
      EfiCopyMem (&VariableContents->Header.Type, &TempValue, sizeof (UINT16));
      EfiCopyMem (&TempValue, &((EFI_IFR_FORM_SET *) &IfrData[Index])->NvDataSize, sizeof (UINT16));

      //
      // If the variable has 0 size, do not process it
      //
      if (TempValue == 0) {
        break;
      }
      //
      // Add the size of the variable pack overhead.  Later, will also add the size of the
      // name of the variable.
      //
      TempValue = TempValue + sizeof (EFI_HII_VARIABLE_PACK);

      EfiCopyMem (&VariableContents->Header.Length, &TempValue, sizeof (UINT32));
      EfiCopyMem (
        &CallbackHandle,
        &((EFI_IFR_FORM_SET *) &IfrData[Index])->CallbackHandle,
        sizeof (EFI_PHYSICAL_ADDRESS)
        );
      if (CallbackHandle != 0) {
        Status = gBS->HandleProtocol (
                        (EFI_HANDLE) (UINTN) CallbackHandle,
                        &gEfiFormCallbackProtocolGuid,
                        &FormCallback
                        );
      }
      //
      // Since we have a "Setup" variable that wasn't specified by a variable op-code
      // it will have a VariableId of 0.  All other variable op-codes will have a designation
      // of VariableId 1+
      //
      TempValue = 0;
      EfiCopyMem (&VariableContents->VariableId, &TempValue, sizeof (UINT16));
      EfiCopyMem (&VariableContents->VariableGuid, &((EFI_IFR_FORM_SET *) &IfrData[Index])->Guid, sizeof (EFI_GUID));
      TempValue = sizeof (SETUP_MAP_NAME);
      EfiCopyMem (&VariableContents->VariableNameLength, &TempValue, sizeof (UINT32));

      //
      // Add the size of the name to the Header Length
      //
      TempValue2 = 0;
      EfiCopyMem (&TempValue2, &VariableContents->Header.Length, sizeof (UINT32));
      TempValue2 = TempValue + TempValue2;
      EfiCopyMem (&VariableContents->Header.Length, &TempValue2, sizeof (UINT32));

      ExportBuffer = ExportBuffer + sizeof (EFI_HII_VARIABLE_PACK);
      EfiCopyMem (ExportBuffer, SETUP_MAP_NAME, sizeof (SETUP_MAP_NAME));
      ExportBuffer = ExportBuffer + sizeof (SETUP_MAP_NAME);

      EfiCopyMem (&TempValue, &((EFI_IFR_FORM_SET *) &IfrData[Index])->NvDataSize, sizeof (UINT16));

      if ((FormCallback != NULL) && (FormCallback->NvRead != NULL)) {
        Status = FormCallback->NvRead (
                                 FormCallback,
                                 SETUP_MAP_NAME,
                                 &VariableContents->VariableGuid,
                                 NULL,
                                 &TempValue,
                                 ExportBuffer
                                 );
      } else {
        Status = gRT->GetVariable (
                        SETUP_MAP_NAME,
                        &VariableContents->VariableGuid,
                        NULL,
                        &TempValue,
                        ExportBuffer
                        );
      }

      ExportBuffer = (UINT8 *) (UINTN) (((UINTN) ExportBuffer) + TempValue);
      DataTable->NumberOfVariableData++;
      break;

    case EFI_IFR_VARSTORE_OP:
      TempValue = EFI_HII_VARIABLE;
      EfiCopyMem (&VariableContents->Header.Type, &TempValue, sizeof (UINT16));
      EfiCopyMem (&TempValue, &((EFI_IFR_VARSTORE *) &IfrData[Index])->Size, sizeof (UINT16));

      //
      // If the variable has 0 size, do not process it
      //
      if (TempValue == 0) {
        break;
      }
      //
      // Add the size of the variable pack overhead.  Later, will also add the size of the
      // name of the variable.
      //
      TempValue = TempValue + sizeof (EFI_HII_VARIABLE_PACK);

      EfiCopyMem (&VariableContents->Header.Length, &TempValue, sizeof (UINT32));
      EfiCopyMem (&VariableContents->VariableId, &((EFI_IFR_VARSTORE *) &IfrData[Index])->VarId, sizeof (UINT16));
      EfiCopyMem (&VariableContents->VariableGuid, &((EFI_IFR_VARSTORE *) &IfrData[Index])->Guid, sizeof (EFI_GUID));
      TempValue = (UINTN) ((EFI_IFR_VARSTORE *) &IfrData[Index])->Header.Length - sizeof (EFI_IFR_VARSTORE);
      TempValue = TempValue * 2;
      EfiCopyMem (&VariableContents->VariableNameLength, &TempValue, sizeof (UINT32));

      //
      // Add the size of the name to the Header Length
      //
      TempValue2 = 0;
      EfiCopyMem (&TempValue2, &VariableContents->Header.Length, sizeof (UINT32));
      TempValue2 = TempValue + TempValue2;
      EfiCopyMem (&VariableContents->Header.Length, &TempValue2, sizeof (UINT32));

      ExportBuffer  = ExportBuffer + sizeof (EFI_HII_VARIABLE_PACK);
      String        = (CHAR16 *) ExportBuffer;
      for (Index2 = 0; Index2 < TempValue / 2; Index2++) {
        ExportBuffer[Index2 * 2]      = IfrData[Index + sizeof (EFI_IFR_VARSTORE) + Index2];
        ExportBuffer[Index2 * 2 + 1]  = 0;
      }

      ExportBuffer = ExportBuffer + TempValue;

      EfiCopyMem (&TempValue, &((EFI_IFR_VARSTORE *) &IfrData[Index])->Size, sizeof (UINT16));

      if ((FormCallback != NULL) && (FormCallback->NvRead != NULL)) {
        Status = FormCallback->NvRead (
                                 FormCallback,
                                 String,
                                 &VariableContents->VariableGuid,
                                 NULL,
                                 &TempValue,
                                 ExportBuffer
                                 );
      } else {
        Status = gRT->GetVariable (
                        String,
                        &VariableContents->VariableGuid,
                        NULL,
                        &TempValue,
                        ExportBuffer
                        );
      }

      ExportBuffer = (UINT8 *) (UINTN) (((UINTN) ExportBuffer) + TempValue);
      DataTable->NumberOfVariableData++;
      break;
    }

    Index = IfrData[Index + 1] + Index;
  }
  //
  // If we have added a variable pack, add a dummy empty one to signify the end
  //
  if (ExportBuffer != *ExportBufferPtr) {
    VariableContents  = (EFI_HII_VARIABLE_PACK *) ExportBuffer;
    TempValue         = EFI_HII_VARIABLE;
    EfiCopyMem (&VariableContents->Header.Type, &TempValue, sizeof (UINT16));
    TempValue = sizeof (EFI_HII_VARIABLE_PACK);
    EfiCopyMem (&VariableContents->Header.Length, &TempValue, sizeof (UINT32));
    ExportBuffer = ExportBuffer + sizeof (EFI_HII_VARIABLE_PACK);
  }

  *ExportBufferPtr = ExportBuffer;
}

EFI_STATUS
EFIAPI
HiiExportDatabase (
  IN     EFI_HII_PROTOCOL *This,
  IN     EFI_HII_HANDLE   Handle,
  IN OUT UINTN            *BufferSize,
  OUT    VOID             *Buffer
  )
/*++

Routine Description:
  
  This function allows a program to extract a form or form package that has 
  previously been registered with the EFI HII database.

Arguments:

Returns: 

--*/
{
  EFI_HII_PACKAGE_INSTANCE  *PackageInstance;
  EFI_HII_DATA              *HiiData;
  EFI_HII_HANDLE_DATABASE   *HandleDatabase;
  EFI_HII_IFR_PACK          *FormPack;
  UINT8                     *RawData;
  UINT8                     *ExportBuffer;
  EFI_HII_EXPORT_TABLE      *ExportTable;
  EFI_HII_DATA_TABLE        *DataTable;
  BOOLEAN                   InsufficientSize;
  BOOLEAN                   VariableExist;
  UINT16                    NumberOfHiiDataTables;
  UINTN                     SizeNeeded;
  UINTN                     Index;
  UINTN                     VariableSize;
  UINTN                     TempValue;

  if (This == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  HiiData               = EFI_HII_DATA_FROM_THIS (This);

  HandleDatabase        = HiiData->DatabaseHead;

  FormPack              = NULL;
  RawData               = NULL;
  PackageInstance       = NULL;
  InsufficientSize      = FALSE;
  NumberOfHiiDataTables = 0;
  VariableSize          = 0;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?