UpdateDefaultSettingInFormPackage (
  HII_IFR_PACKAGE_INSTANCE *FormPackage
  )
{
  UINTN                    IfrOffset;
  UINTN                    PackageLength;
  EFI_IFR_VARSTORE_EFI     *IfrEfiVarStore;
  EFI_IFR_OP_HEADER        *IfrOpHdr;
  EFI_IFR_ONE_OF_OPTION    *IfrOneOfOption;
  UINT8                    IfrQuestionType;
  UINT8                    IfrScope;
  EFI_IFR_QUESTION_HEADER  *IfrQuestionHdr;
  EFI_IFR_VARSTORE_EFI     **EfiVarStoreList;
  UINTN                    EfiVarStoreMaxNum;
  UINTN                    EfiVarStoreNumber;
  UINT16                   *DefaultIdList;
  UINTN                    DefaultIdNumber;
  UINTN                    DefaultIdMaxNum;
  UINTN                    Index;
  UINTN                    EfiVarStoreIndex;
  EFI_IFR_TYPE_VALUE       IfrValue;
  EFI_IFR_TYPE_VALUE       IfrManufactValue;
  BOOLEAN                  StandardDefaultIsSet;
  BOOLEAN                  ManufactDefaultIsSet;
  EFI_IFR_CHECKBOX         *IfrCheckBox;
  EFI_STATUS               Status;
  EFI_IFR_DEFAULT          *IfrDefault;
  UINTN                    Width;
  EFI_IFR_QUESTION_HEADER  VarStoreQuestionHeader;
  BOOLEAN                  QuestionReferBitField;

  //
  // If no default setting, do nothing
  //
  if (gNvDefaultStoreSize == 0) {
    gNvDefaultStoreSize = PcdGetSize (PcdNvStoreDefaultValueBuffer);
  }
  if (gNvDefaultStoreSize < sizeof (PCD_NV_STORE_DEFAULT_BUFFER_HEADER)) {
    return;
  }

  ZeroMem (&VarStoreQuestionHeader, sizeof (VarStoreQuestionHeader));
  PackageLength = FormPackage->FormPkgHdr.Length - sizeof (EFI_HII_PACKAGE_HEADER);
  Width         = 0;
  IfrOffset     = 0;
  IfrScope      = 0;
  IfrOpHdr      = (EFI_IFR_OP_HEADER *) FormPackage->IfrData;
  IfrQuestionHdr    = NULL;
  IfrQuestionType   = 0;
  EfiVarStoreMaxNum = 0;
  EfiVarStoreNumber = 0;
  DefaultIdMaxNum   = 0;
  DefaultIdNumber   = 0;
  EfiVarStoreList   = NULL;
  DefaultIdList     = NULL;
  StandardDefaultIsSet = FALSE;
  ManufactDefaultIsSet = FALSE;
  QuestionReferBitField = FALSE;

  while (IfrOffset < PackageLength) {
    switch (IfrOpHdr->OpCode) {
    case EFI_IFR_VARSTORE_EFI_OP:
      if (EfiVarStoreNumber >= EfiVarStoreMaxNum) {
        //
        // Reallocate EFI VarStore Buffer
        //
        EfiVarStoreList   = ReallocatePool (EfiVarStoreMaxNum * sizeof (UINTN), (EfiVarStoreMaxNum + BASE_NUMBER) * sizeof (UINTN), EfiVarStoreList);
        if (EfiVarStoreList == NULL) {
          goto Done;
        }
        EfiVarStoreMaxNum = EfiVarStoreMaxNum + BASE_NUMBER;
      }
      IfrEfiVarStore = (EFI_IFR_VARSTORE_EFI *) IfrOpHdr;
      //
      // Convert VarStore Name from ASCII string to Unicode string.
      //
      EfiVarStoreList [EfiVarStoreNumber] = AllocatePool (IfrEfiVarStore->Header.Length + AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name));
      if (EfiVarStoreList [EfiVarStoreNumber] == NULL) {
        break;
      }
      CopyMem (EfiVarStoreList [EfiVarStoreNumber], IfrEfiVarStore, IfrEfiVarStore->Header.Length);
      AsciiStrToUnicodeStrS ((CHAR8 *)IfrEfiVarStore->Name, (CHAR16 *) &(EfiVarStoreList [EfiVarStoreNumber]->Name[0]), AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name) * sizeof (CHAR16));
      Status = FindQuestionDefaultSetting (EFI_HII_DEFAULT_CLASS_STANDARD, EfiVarStoreList[EfiVarStoreNumber], &VarStoreQuestionHeader, NULL, IfrEfiVarStore->Size, FALSE);
      if (!EFI_ERROR (Status)) {
        EfiVarStoreNumber ++;
      } else {
        FreePool (EfiVarStoreList [EfiVarStoreNumber]);
        EfiVarStoreList [EfiVarStoreNumber] = NULL;
      }
      break;
    case EFI_IFR_DEFAULTSTORE_OP:
      if (DefaultIdNumber >= DefaultIdMaxNum) {
        //
        // Reallocate DefaultIdNumber
        //
        DefaultIdList   = ReallocatePool (DefaultIdMaxNum * sizeof (UINT16), (DefaultIdMaxNum + BASE_NUMBER) * sizeof (UINT16), DefaultIdList);
        if (DefaultIdList == NULL) {
          goto Done;
        }
        DefaultIdMaxNum = DefaultIdMaxNum + BASE_NUMBER;
      }
      DefaultIdList[DefaultIdNumber ++] = ((EFI_IFR_DEFAULTSTORE *) IfrOpHdr)->DefaultId;
      break;
    case EFI_IFR_FORM_OP:
    case EFI_IFR_FORM_MAP_OP:
      //
      // No EFI varstore is found and directly return.
      //
      if (EfiVarStoreNumber == 0 || DefaultIdNumber == 0) {
        goto Done;
      }
      break;
    case EFI_IFR_CHECKBOX_OP:
      IfrScope         = IfrOpHdr->Scope;
      IfrQuestionType  = IfrOpHdr->OpCode;
      IfrQuestionHdr   = (EFI_IFR_QUESTION_HEADER *) (IfrOpHdr + 1);
      IfrCheckBox      = (EFI_IFR_CHECKBOX *) IfrOpHdr;
      EfiVarStoreIndex = IsEfiVarStoreQuestion (IfrQuestionHdr, EfiVarStoreList, EfiVarStoreNumber);
      Width            = sizeof (BOOLEAN);
      if (EfiVarStoreIndex < EfiVarStoreNumber) {
        for (Index = 0; Index < DefaultIdNumber; Index ++) {
          if (DefaultIdList[Index] == EFI_HII_DEFAULT_CLASS_STANDARD) {
            Status = FindQuestionDefaultSetting (DefaultIdList[Index], EfiVarStoreList[EfiVarStoreIndex], IfrQuestionHdr, &IfrValue, sizeof (BOOLEAN), QuestionReferBitField);
            if (!EFI_ERROR (Status)) {
              if (IfrValue.b) {
                IfrCheckBox->Flags = IfrCheckBox->Flags | EFI_IFR_CHECKBOX_DEFAULT;
              } else {
                IfrCheckBox->Flags = IfrCheckBox->Flags & (~EFI_IFR_CHECKBOX_DEFAULT);
              }
            }
          } else if (DefaultIdList[Index] == EFI_HII_DEFAULT_CLASS_MANUFACTURING) {
            Status = FindQuestionDefaultSetting (DefaultIdList[Index], EfiVarStoreList[EfiVarStoreIndex], IfrQuestionHdr, &IfrValue, sizeof (BOOLEAN), QuestionReferBitField);
            if (!EFI_ERROR (Status)) {
              if (IfrValue.b) {
                IfrCheckBox->Flags = IfrCheckBox->Flags | EFI_IFR_CHECKBOX_DEFAULT_MFG;
              } else {
                IfrCheckBox->Flags = IfrCheckBox->Flags & (~EFI_IFR_CHECKBOX_DEFAULT_MFG);
              }
            }
          }
        }
      }
      break;
    case EFI_IFR_NUMERIC_OP:
      IfrScope         = IfrOpHdr->Scope;
      IfrQuestionType  = IfrOpHdr->OpCode;
      IfrQuestionHdr   = (EFI_IFR_QUESTION_HEADER *) (IfrOpHdr + 1);
      if (QuestionReferBitField) {
        Width          = (UINTN) (((EFI_IFR_ONE_OF *) IfrOpHdr)->Flags & EDKII_IFR_NUMERIC_SIZE_BIT);
      } else {
        Width          = (UINTN) ((UINT32) 1 << (((EFI_IFR_ONE_OF *) IfrOpHdr)->Flags & EFI_IFR_NUMERIC_SIZE));
      }
      break;
    case EFI_IFR_ONE_OF_OP:
      IfrScope         = IfrOpHdr->Scope;
      IfrQuestionType  = IfrOpHdr->OpCode;
      IfrQuestionHdr   = (EFI_IFR_QUESTION_HEADER *) (IfrOpHdr + 1);
      if (QuestionReferBitField) {
        Width          = (UINTN) (((EFI_IFR_ONE_OF *) IfrOpHdr)->Flags & EDKII_IFR_NUMERIC_SIZE_BIT);
      } else {
        Width          = (UINTN) ((UINT32) 1 << (((EFI_IFR_ONE_OF *) IfrOpHdr)->Flags & EFI_IFR_NUMERIC_SIZE));
      }
      EfiVarStoreIndex = IsEfiVarStoreQuestion (IfrQuestionHdr, EfiVarStoreList, EfiVarStoreNumber);
      StandardDefaultIsSet = FALSE;
      ManufactDefaultIsSet = FALSE;
      //
      // Find Default and Manufacturing default for OneOf question
      //
      if (EfiVarStoreIndex < EfiVarStoreNumber) {
        for (Index = 0; Index < DefaultIdNumber; Index ++) {
          if (DefaultIdList[Index] == EFI_HII_DEFAULT_CLASS_STANDARD) {
            Status = FindQuestionDefaultSetting (EFI_HII_DEFAULT_CLASS_STANDARD, EfiVarStoreList[EfiVarStoreIndex], IfrQuestionHdr, &IfrValue, Width, QuestionReferBitField);
            if (!EFI_ERROR (Status)) {
              StandardDefaultIsSet = TRUE;
            }
          } else if (DefaultIdList[Index] == EFI_HII_DEFAULT_CLASS_MANUFACTURING) {
            Status = FindQuestionDefaultSetting (EFI_HII_DEFAULT_CLASS_MANUFACTURING, EfiVarStoreList[EfiVarStoreIndex], IfrQuestionHdr, &IfrManufactValue, Width, QuestionReferBitField);
            if (!EFI_ERROR (Status)) {
              ManufactDefaultIsSet = TRUE;
            }
          }
        }
      }
      break;
    case EFI_IFR_ORDERED_LIST_OP:
      IfrScope         = IfrOpHdr->Scope;
      IfrQuestionType  = IfrOpHdr->OpCode;
      IfrQuestionHdr   = (EFI_IFR_QUESTION_HEADER *) (IfrOpHdr + 1);
      break;
    case EFI_IFR_ONE_OF_OPTION_OP:
      if (IfrQuestionHdr != NULL && IfrScope > 0) {
        IfrOneOfOption = (EFI_IFR_ONE_OF_OPTION *) IfrOpHdr;
        if (IfrQuestionType == EFI_IFR_ONE_OF_OP) {
          Width = (UINTN) ((UINT32) 1 << (IfrOneOfOption->Flags & EFI_IFR_NUMERIC_SIZE));
          if (StandardDefaultIsSet) {
            if (CompareMem (&IfrOneOfOption->Value, &IfrValue, Width) == 0) {
              IfrOneOfOption->Flags |= EFI_IFR_OPTION_DEFAULT;
            } else {
              IfrOneOfOption->Flags &= ~EFI_IFR_OPTION_DEFAULT;
            }
          }
          if (ManufactDefaultIsSet) {
            if (CompareMem (&IfrOneOfOption->Value, &IfrManufactValue, Width) == 0) {
              IfrOneOfOption->Flags |= EFI_IFR_OPTION_DEFAULT_MFG;
            } else {
              IfrOneOfOption->Flags &= ~EFI_IFR_OPTION_DEFAULT_MFG;
            }
          }
        }
      }
      break;
    case EFI_IFR_DEFAULT_OP:
      if (IfrQuestionHdr != NULL && IfrScope > 0) {
        IfrDefault = (EFI_IFR_DEFAULT *) IfrOpHdr;
        //
        // Collect default value width
        //
        if (!QuestionReferBitField) {
          Width = 0;
          if (IfrDefault->Type == EFI_IFR_TYPE_NUM_SIZE_8 || IfrDefault->Type == EFI_IFR_TYPE_BOOLEAN) {
            Width = 1;
          } else if (IfrDefault->Type == EFI_IFR_TYPE_NUM_SIZE_16) {
            Width = 2;
          } else if (IfrDefault->Type == EFI_IFR_TYPE_NUM_SIZE_32) {
            Width = 4;
          } else if (IfrDefault->Type == EFI_IFR_TYPE_NUM_SIZE_64) {
            Width = 8;
          } else if (IfrDefault->Type == EFI_IFR_TYPE_BUFFER) {
            Width = IfrDefault->Header.Length - OFFSET_OF (EFI_IFR_DEFAULT, Value);
          }
        }
        //
        // Update the default value
        //
        if (Width > 0) {
          EfiVarStoreIndex = IsEfiVarStoreQuestion (IfrQuestionHdr, EfiVarStoreList, EfiVarStoreNumber);
          if (EfiVarStoreIndex < EfiVarStoreNumber) {
            Status = FindQuestionDefaultSetting (IfrDefault->DefaultId, EfiVarStoreList[EfiVarStoreIndex], IfrQuestionHdr, &IfrDefault->Value, Width, QuestionReferBitField);
          }
        }
      }
      break;
    case EFI_IFR_END_OP:
      if (IfrQuestionHdr != NULL) {
        if (IfrScope > 0) {
          IfrScope --;
        }
        if (IfrScope == 0) {
          IfrQuestionHdr = NULL;
          QuestionReferBitField = FALSE;
        }
      }
      break;
    case EFI_IFR_GUID_OP:
      if (CompareGuid ((EFI_GUID *)((UINT8 *)IfrOpHdr + sizeof (EFI_IFR_OP_HEADER)), &gEdkiiIfrBitVarstoreGuid)) {
        QuestionReferBitField = TRUE;
      }
      break;
    default:
      break;
    }
    IfrOffset = IfrOffset + IfrOpHdr->Length;
    IfrOpHdr  = (EFI_IFR_OP_HEADER *) ((UINT8 *) IfrOpHdr + IfrOpHdr->Length);
    if (IfrScope > 0) {
      IfrScope += IfrOpHdr->Scope;
    }
  }

Done:
  if (EfiVarStoreList != NULL) {
    for (Index = 0; Index < EfiVarStoreNumber; Index ++) {
      FreePool (EfiVarStoreList [Index]);
    }
  }
  return;
}