]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/SetupBrowserDxe/Setup.c
MdeModulePkg: Use IsZeroGuid API for zero GUID checking
[mirror_edk2.git] / MdeModulePkg / Universal / SetupBrowserDxe / Setup.c
index 5c5fe6f215967a2bbdaf8a4f9c00f80b2c431efa..3f368b54ec7d6b67d1fe546aaa575d198fc5edde 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 Entry and initialization module for the browser.\r
 \r
-Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -55,6 +55,8 @@ LIST_ENTRY      gBrowserSaveFailFormSetList = INITIALIZE_LIST_HEAD_VARIABLE (gBr
 BOOLEAN               mSystemSubmit = FALSE;\r
 BOOLEAN               gResetRequired;\r
 BOOLEAN               gExitRequired;\r
+BOOLEAN               gFlagReconnect;\r
+BOOLEAN               gCallbackReconnect;\r
 BROWSER_SETTING_SCOPE gBrowserSettingScope = FormSetLevel;\r
 BOOLEAN               mBrowserScopeFirstSet = TRUE;\r
 EXIT_HANDLER          ExitHandlerFunction = NULL;\r
@@ -66,8 +68,6 @@ FORM_BROWSER_FORMSET  *mSystemLevelFormSet;
 CHAR16            *gEmptyString;\r
 CHAR16            *mUnknownString = L"!";\r
 \r
-EFI_GUID  gZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};\r
-\r
 extern EFI_GUID        mCurrentFormSetGuid;\r
 extern EFI_HII_HANDLE  mCurrentHiiHandle;\r
 extern UINT16          mCurrentFormId;\r
@@ -181,7 +181,7 @@ UiFindMenuList (
     // Find the same FromSet.\r
     //\r
     if (MenuList->HiiHandle == HiiHandle) {\r
-      if (CompareGuid (&MenuList->FormSetGuid, &gZeroGuid)) {\r
+      if (IsZeroGuid (&MenuList->FormSetGuid)) {\r
         //\r
         // FormSetGuid is not specified.\r
         //\r
@@ -206,19 +206,56 @@ UiFindMenuList (
   Find parent menu for current menu.\r
 \r
   @param  CurrentMenu    Current Menu\r
+  @param  SettingLevel   Whether find parent menu in Form Level or Formset level.\r
+                         In form level, just find the parent menu; \r
+                         In formset level, find the parent menu which has different\r
+                         formset guid value.\r
 \r
   @retval   The parent menu for current menu.\r
 **/\r
 FORM_ENTRY_INFO *\r
 UiFindParentMenu (\r
-  IN FORM_ENTRY_INFO  *CurrentMenu\r
+  IN FORM_ENTRY_INFO          *CurrentMenu,\r
+  IN BROWSER_SETTING_SCOPE    SettingLevel\r
   )\r
 {\r
   FORM_ENTRY_INFO    *ParentMenu;\r
+  LIST_ENTRY         *Link;\r
 \r
+  ASSERT (SettingLevel == FormLevel || SettingLevel == FormSetLevel);\r
+\r
+  if (CurrentMenu == NULL) {\r
+    return NULL;\r
+  }\r
+  \r
   ParentMenu = NULL;\r
-  if (CurrentMenu->Link.BackLink != &mPrivateData.FormBrowserEx2.FormViewHistoryHead) {\r
-    ParentMenu = FORM_ENTRY_INFO_FROM_LINK (CurrentMenu->Link.BackLink);\r
+  Link       = &CurrentMenu->Link;\r
+\r
+  while (Link->BackLink != &mPrivateData.FormBrowserEx2.FormViewHistoryHead) {\r
+    ParentMenu = FORM_ENTRY_INFO_FROM_LINK (Link->BackLink);\r
+\r
+    if (SettingLevel == FormLevel) {\r
+      //\r
+      // For FormLevel, just find the parent menu, return.\r
+      //\r
+      break;\r
+    }\r
+\r
+    if (!CompareGuid (&CurrentMenu->FormSetGuid, &ParentMenu->FormSetGuid)) {\r
+      //\r
+      // For SystemLevel, must find the menu which has different formset.\r
+      //\r
+      break;\r
+    }\r
+\r
+    Link = Link->BackLink;\r
+  }\r
+\r
+  //\r
+  // Not find the parent menu, just return NULL.\r
+  //\r
+  if (Link->BackLink == &mPrivateData.FormBrowserEx2.FormViewHistoryHead) {\r
+    return NULL;\r
   }\r
 \r
   return ParentMenu;\r
@@ -446,11 +483,13 @@ SendForm (
   UINTN                         Index;\r
   FORM_BROWSER_FORMSET          *FormSet;\r
   FORM_ENTRY_INFO               *MenuList;\r
+  BOOLEAN                       RetVal;\r
 \r
   //\r
   // If EDKII_FORM_DISPLAY_ENGINE_PROTOCOL not found, return EFI_UNSUPPORTED.\r
   //\r
   if (mFormDisplay == NULL) {\r
+    DEBUG ((DEBUG_ERROR, "Fatal Error! EDKII_FORM_DISPLAY_ENGINE_PROTOCOL not found!"));\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
@@ -459,8 +498,10 @@ SendForm (
   //\r
   SaveBrowserContext ();\r
 \r
+  gFlagReconnect = FALSE;\r
   gResetRequired = FALSE;\r
   gExitRequired  = FALSE;\r
+  gCallbackReconnect = FALSE;\r
   Status         = EFI_SUCCESS;\r
   gEmptyString   = L"";\r
   gDisplayFormData.ScreenDimensions = (EFI_SCREEN_DESCRIPTOR *) ScreenDimensions;\r
@@ -481,6 +522,14 @@ SendForm (
       FormSet = AllocateZeroPool (sizeof (FORM_BROWSER_FORMSET));\r
       ASSERT (FormSet != NULL);\r
 \r
+      //\r
+      // Validate the HiiHandle\r
+      // if validate failed, find the first validate parent HiiHandle.\r
+      //\r
+      if (!ValidateHiiHandle(Selection->Handle)) {\r
+        FindNextMenu (Selection, FormSetLevel);\r
+      }\r
+\r
       //\r
       // Initialize internal data structures of FormSet\r
       //\r
@@ -502,6 +551,15 @@ SendForm (
       gCurrentSelection = NULL;\r
       mSystemLevelFormSet = NULL;\r
 \r
+      if (gFlagReconnect || gCallbackReconnect) {\r
+        RetVal = ReconnectController (FormSet->DriverHandle);\r
+        if (!RetVal) {\r
+          PopupErrorMessage(BROWSER_RECONNECT_FAIL, NULL, NULL, NULL);\r
+        }\r
+        gFlagReconnect = FALSE;\r
+        gCallbackReconnect = FALSE;\r
+      }\r
+\r
       //\r
       // If no data is changed, don't need to save current FormSet into the maintain list.\r
       //\r
@@ -576,6 +634,8 @@ ProcessStorage (
   CHAR16                *StrPtr;\r
   UINTN                 BufferSize;\r
   UINTN                 TmpSize;\r
+  UINTN                 MaxLen;\r
+  FORMSET_STORAGE       *BrowserStorage;\r
 \r
   if (RetrieveData) {\r
     //\r
@@ -590,15 +650,17 @@ ProcessStorage (
     // Skip <ConfigHdr> and '&' to point to <ConfigBody> when first copy the configbody.\r
     // Also need to consider add "\0" at first time.\r
     //\r
-    StrPtr     = ConfigResp + StrLen (Storage->ConfigHdr) + 1;\r
+    StrPtr = StrStr (ConfigResp, L"PATH");\r
+    ASSERT (StrPtr != NULL);\r
+    StrPtr = StrStr (StrPtr, L"&");\r
+    StrPtr += 1;\r
     BufferSize = StrSize (StrPtr);\r
 \r
-\r
     //\r
     // Copy the data if the input buffer is bigger enough.\r
     //\r
     if (*ResultsDataSize >= BufferSize) {\r
-      StrCpy (*ResultsData, StrPtr);\r
+      StrCpyS (*ResultsData, *ResultsDataSize / sizeof (CHAR16), StrPtr);\r
     }\r
 \r
     *ResultsDataSize = BufferSize;\r
@@ -607,14 +669,17 @@ ProcessStorage (
     //\r
     // Prepare <ConfigResp>\r
     //\r
+    BrowserStorage = GetFstStgFromBrsStg (Storage);\r
+    ASSERT (BrowserStorage != NULL);\r
     TmpSize = StrLen (*ResultsData);\r
-    BufferSize = (TmpSize + StrLen (Storage->ConfigHdr) + 2) * sizeof (CHAR16);\r
+    BufferSize = (TmpSize + StrLen (BrowserStorage->ConfigHdr) + 2) * sizeof (CHAR16);\r
+    MaxLen = BufferSize / sizeof (CHAR16);\r
     ConfigResp = AllocateZeroPool (BufferSize);\r
     ASSERT (ConfigResp != NULL);\r
 \r
-    StrCpy (ConfigResp, Storage->ConfigHdr);\r
-    StrCat (ConfigResp, L"&");\r
-    StrCat (ConfigResp, *ResultsData);\r
+    StrCpyS (ConfigResp, MaxLen, BrowserStorage->ConfigHdr);\r
+    StrCatS (ConfigResp, MaxLen, L"&");\r
+    StrCatS (ConfigResp, MaxLen, *ResultsData);\r
 \r
     //\r
     // Update Browser uncommited data\r
@@ -791,13 +856,11 @@ FormDisplayCallback (
   IN VOID         *Context\r
   )\r
 {\r
-  EFI_STATUS                  Status;\r
-\r
   if (mFormDisplay != NULL) {\r
     return;\r
   }\r
 \r
-  Status = gBS->LocateProtocol (\r
+  gBS->LocateProtocol (\r
                   &gEdkiiFormDisplayEngineProtocolGuid,\r
                   NULL,\r
                   (VOID **) &mFormDisplay\r
@@ -1017,19 +1080,19 @@ NewStringCat (
   )\r
 {\r
   CHAR16  *NewString;\r
-  UINTN   TmpSize;\r
+  UINTN   MaxLen;\r
 \r
   if (*Dest == NULL) {\r
     NewStringCpy (Dest, Src);\r
     return;\r
   }\r
 \r
-  TmpSize = StrSize (*Dest);\r
-  NewString = AllocateZeroPool (TmpSize + StrSize (Src) - 1);\r
+  MaxLen = ( StrSize (*Dest) + StrSize (Src) - 1) / sizeof (CHAR16);\r
+  NewString = AllocateZeroPool (MaxLen * sizeof (CHAR16));\r
   ASSERT (NewString != NULL);\r
 \r
-  StrCpy (NewString, *Dest);\r
-  StrCat (NewString, Src);\r
+  StrCpyS (NewString, MaxLen, *Dest);\r
+  StrCatS (NewString, MaxLen, Src);\r
 \r
   FreePool (*Dest);\r
   *Dest = NewString;\r
@@ -1174,6 +1237,7 @@ StorageToConfigResp (
   LIST_ENTRY              *Link;\r
   NAME_VALUE_NODE         *Node;\r
   UINT8                   *SourceBuf;\r
+  FORMSET_STORAGE         *FormsetStorage;\r
 \r
   Status = EFI_SUCCESS;\r
 \r
@@ -1193,7 +1257,9 @@ StorageToConfigResp (
 \r
   case EFI_HII_VARSTORE_NAME_VALUE:\r
     *ConfigResp = NULL;\r
-    NewStringCat (ConfigResp, Storage->ConfigHdr);\r
+    FormsetStorage = GetFstStgFromBrsStg(Storage);\r
+    ASSERT (FormsetStorage != NULL);\r
+    NewStringCat (ConfigResp, FormsetStorage->ConfigHdr);\r
 \r
     Link = GetFirstNode (&Storage->NameValueListHead);\r
     while (!IsNull (&Storage->NameValueListHead, Link)) {\r
@@ -1301,6 +1367,114 @@ ConfigRespToStorage (
   return Status;\r
 }\r
 \r
+/**\r
+  Convert the buffer value to HiiValue.\r
+\r
+  @param  Question               The question.\r
+  @param  Value                  Unicode buffer save the question value.\r
+\r
+  @retval  Status whether convert the value success.\r
+\r
+**/\r
+EFI_STATUS\r
+BufferToValue (\r
+  IN OUT FORM_BROWSER_STATEMENT           *Question,\r
+  IN     CHAR16                           *Value\r
+  )\r
+{\r
+  CHAR16                       *StringPtr;\r
+  BOOLEAN                      IsBufferStorage;\r
+  CHAR16                       *DstBuf;\r
+  CHAR16                       TempChar;\r
+  UINTN                        LengthStr;\r
+  UINT8                        *Dst;\r
+  CHAR16                       TemStr[5];\r
+  UINTN                        Index;\r
+  UINT8                        DigitUint8;\r
+  BOOLEAN                      IsString;\r
+  UINTN                        Length;\r
+  EFI_STATUS                   Status;\r
+\r
+  IsString = (BOOLEAN) ((Question->HiiValue.Type == EFI_IFR_TYPE_STRING) ?  TRUE : FALSE);\r
+  if (Question->Storage->Type == EFI_HII_VARSTORE_BUFFER || \r
+      Question->Storage->Type == EFI_HII_VARSTORE_EFI_VARIABLE_BUFFER) {\r
+    IsBufferStorage = TRUE;\r
+  } else {\r
+    IsBufferStorage = FALSE;\r
+  }\r
+\r
+  //\r
+  // Question Value is provided by Buffer Storage or NameValue Storage\r
+  //\r
+  if (Question->BufferValue != NULL) {\r
+    //\r
+    // This Question is password or orderedlist\r
+    //\r
+    Dst = Question->BufferValue;\r
+  } else {\r
+    //\r
+    // Other type of Questions\r
+    //\r
+    Dst = (UINT8 *) &Question->HiiValue.Value;\r
+  }\r
+\r
+  //\r
+  // Temp cut at the end of this section, end with '\0' or '&'.\r
+  //\r
+  StringPtr = Value;\r
+  while (*StringPtr != L'\0' && *StringPtr != L'&') {\r
+    StringPtr++;\r
+  }\r
+  TempChar = *StringPtr;\r
+  *StringPtr = L'\0';\r
+\r
+  LengthStr = StrLen (Value);\r
+\r
+  //\r
+  // Value points to a Unicode hexadecimal string, we need to convert the string to the value with CHAR16/UINT8...type.\r
+  // When generating the Value string, we follow this rule: 1 byte -> 2 Unicode characters (for string: 2 byte(CHAR16) ->4 Unicode characters).\r
+  // So the maximum value string length of a question is : Question->StorageWidth * 2.\r
+  // If the value string length > Question->StorageWidth * 2, only set the string length as Question->StorageWidth * 2, then convert.\r
+  //\r
+  if (LengthStr > (UINTN) Question->StorageWidth * 2) {\r
+    Length = (UINTN) Question->StorageWidth * 2;\r
+  } else {\r
+    Length = LengthStr;\r
+  }\r
+\r
+  Status    = EFI_SUCCESS;\r
+  if (!IsBufferStorage && IsString) {\r
+    //\r
+    // Convert Config String to Unicode String, e.g "0041004200430044" => "ABCD"\r
+    // Add string tail char L'\0' into Length\r
+    //\r
+    DstBuf = (CHAR16 *) Dst;\r
+    ZeroMem (TemStr, sizeof (TemStr));\r
+    for (Index = 0; Index < Length; Index += 4) {\r
+      StrnCpyS (TemStr, sizeof (TemStr) / sizeof (CHAR16), Value + Index, 4);\r
+      DstBuf[Index/4] = (CHAR16) StrHexToUint64 (TemStr);\r
+    }\r
+    //\r
+    // Add tailing L'\0' character\r
+    //\r
+    DstBuf[Index/4] = L'\0';\r
+  } else {\r
+    ZeroMem (TemStr, sizeof (TemStr));\r
+    for (Index = 0; Index < Length; Index ++) {\r
+      TemStr[0] = Value[LengthStr - Index - 1];\r
+      DigitUint8 = (UINT8) StrHexToUint64 (TemStr);\r
+      if ((Index & 1) == 0) {\r
+        Dst [Index/2] = DigitUint8;\r
+      } else {\r
+        Dst [Index/2] = (UINT8) ((DigitUint8 << 4) + Dst [Index/2]);\r
+      }\r
+    }\r
+  }\r
+\r
+  *StringPtr = TempChar;\r
+\r
+  return Status;\r
+}\r
 \r
 /**\r
   Get Question's current Value.\r
@@ -1328,19 +1502,15 @@ GetQuestionValue (
   UINTN               StorageWidth;\r
   EFI_TIME            EfiTime;\r
   BROWSER_STORAGE     *Storage;\r
+  FORMSET_STORAGE     *FormsetStorage;\r
   EFI_IFR_TYPE_VALUE  *QuestionValue;\r
   CHAR16              *ConfigRequest;\r
   CHAR16              *Progress;\r
   CHAR16              *Result;\r
   CHAR16              *Value;\r
-  CHAR16              *StringPtr;\r
   UINTN               Length;\r
-  UINTN               Index;\r
-  UINTN               LengthStr;\r
   BOOLEAN             IsBufferStorage;\r
-  BOOLEAN             IsString;\r
-  CHAR16              TemStr[5];\r
-  UINT8               DigitUint8;\r
+  UINTN               MaxLen;\r
 \r
   Status = EFI_SUCCESS;\r
   Value  = NULL;\r
@@ -1431,7 +1601,16 @@ GetQuestionValue (
       }\r
 \r
       if (EFI_ERROR (Status)) {\r
-        return Status;\r
+        if (Question->Operand == EFI_IFR_DATE_OP){\r
+          QuestionValue->date.Year  = 0xff;\r
+          QuestionValue->date.Month = 0xff;\r
+          QuestionValue->date.Day   = 0xff;\r
+        } else {\r
+          QuestionValue->time.Hour   = 0xff;\r
+          QuestionValue->time.Minute = 0xff;\r
+          QuestionValue->time.Second = 0xff;\r
+        }\r
+        return EFI_SUCCESS;\r
       }\r
 \r
       if (Question->Operand == EFI_IFR_DATE_OP) {\r
@@ -1493,7 +1672,6 @@ GetQuestionValue (
   } else {\r
     IsBufferStorage = FALSE;\r
   }\r
-  IsString = (BOOLEAN) ((Question->HiiValue.Type == EFI_IFR_TYPE_STRING) ?  TRUE : FALSE);\r
   if (GetValueFrom == GetSetValueWithEditBuffer || GetValueFrom == GetSetValueWithBuffer ) {\r
     if (IsBufferStorage) {\r
       if (GetValueFrom == GetSetValueWithEditBuffer) {\r
@@ -1515,68 +1693,34 @@ GetQuestionValue (
       }\r
 \r
       ASSERT (Value != NULL);\r
-      LengthStr = StrLen (Value);\r
-      Status    = EFI_SUCCESS;\r
-      if (IsString) {\r
-        //\r
-        // Convert Config String to Unicode String, e.g "0041004200430044" => "ABCD"\r
-        // Add string tail char L'\0' into Length\r
-        //\r
-        Length    = StorageWidth + sizeof (CHAR16);\r
-        if (Length < ((LengthStr / 4 + 1) * 2)) {\r
-          Status = EFI_BUFFER_TOO_SMALL;\r
-        } else {\r
-          StringPtr = (CHAR16 *) Dst;\r
-          ZeroMem (TemStr, sizeof (TemStr));\r
-          for (Index = 0; Index < LengthStr; Index += 4) {\r
-            StrnCpy (TemStr, Value + Index, 4);\r
-            StringPtr[Index/4] = (CHAR16) StrHexToUint64 (TemStr);\r
-          }\r
-          //\r
-          // Add tailing L'\0' character\r
-          //\r
-          StringPtr[Index/4] = L'\0';\r
-        }\r
-      } else {\r
-        if (StorageWidth < ((LengthStr + 1) / 2)) {\r
-          Status = EFI_BUFFER_TOO_SMALL;\r
-        } else {\r
-          ZeroMem (TemStr, sizeof (TemStr));\r
-          for (Index = 0; Index < LengthStr; Index ++) {\r
-            TemStr[0] = Value[LengthStr - Index - 1];\r
-            DigitUint8 = (UINT8) StrHexToUint64 (TemStr);\r
-            if ((Index & 1) == 0) {\r
-              Dst [Index/2] = DigitUint8;\r
-            } else {\r
-              Dst [Index/2] = (UINT8) ((DigitUint8 << 4) + Dst [Index/2]);\r
-            }\r
-          }\r
-        }\r
-      }\r
-\r
+      Status = BufferToValue (Question, Value);\r
       FreePool (Value);\r
     }\r
   } else {\r
+    FormsetStorage = GetFstStgFromVarId(FormSet, Question->VarStoreId);\r
+    ASSERT (FormsetStorage != NULL);\r
     //\r
     // <ConfigRequest> ::= <ConfigHdr> + <BlockName> ||\r
     //                   <ConfigHdr> + "&" + <VariableName>\r
     //\r
     if (IsBufferStorage) {\r
-      Length = StrLen (Storage->ConfigHdr);\r
+      Length = StrLen (FormsetStorage->ConfigHdr);\r
       Length += StrLen (Question->BlockName);\r
     } else {\r
-      Length = StrLen (Storage->ConfigHdr);\r
+      Length = StrLen (FormsetStorage->ConfigHdr);\r
       Length += StrLen (Question->VariableName) + 1;\r
     }\r
-    ConfigRequest = AllocateZeroPool ((Length + 1) * sizeof (CHAR16));\r
+    // Allocate buffer include '\0'\r
+    MaxLen = Length + 1;\r
+    ConfigRequest = AllocateZeroPool (MaxLen * sizeof (CHAR16));\r
     ASSERT (ConfigRequest != NULL);\r
 \r
-    StrCpy (ConfigRequest, Storage->ConfigHdr);\r
+    StrCpyS (ConfigRequest, MaxLen, FormsetStorage->ConfigHdr);\r
     if (IsBufferStorage) {\r
-      StrCat (ConfigRequest, Question->BlockName);\r
+      StrCatS (ConfigRequest, MaxLen, Question->BlockName);\r
     } else {\r
-      StrCat (ConfigRequest, L"&");\r
-      StrCat (ConfigRequest, Question->VariableName);\r
+      StrCatS (ConfigRequest, MaxLen, L"&");\r
+      StrCatS (ConfigRequest, MaxLen, Question->VariableName);\r
     }\r
 \r
     //\r
@@ -1618,54 +1762,7 @@ GetQuestionValue (
     //\r
     Value = Value + 1;\r
 \r
-    //\r
-    // Suppress <AltResp> if any\r
-    //\r
-    StringPtr = Value;\r
-    while (*StringPtr != L'\0' && *StringPtr != L'&') {\r
-      StringPtr++;\r
-    }\r
-    *StringPtr = L'\0';\r
-\r
-    LengthStr = StrLen (Value);\r
-    Status    = EFI_SUCCESS;\r
-    if (!IsBufferStorage && IsString) {\r
-      //\r
-      // Convert Config String to Unicode String, e.g "0041004200430044" => "ABCD"\r
-      // Add string tail char L'\0' into Length\r
-      //\r
-      Length    = StorageWidth + sizeof (CHAR16);\r
-      if (Length < ((LengthStr / 4 + 1) * 2)) {\r
-        Status = EFI_BUFFER_TOO_SMALL;\r
-      } else {\r
-        StringPtr = (CHAR16 *) Dst;\r
-        ZeroMem (TemStr, sizeof (TemStr));\r
-        for (Index = 0; Index < LengthStr; Index += 4) {\r
-          StrnCpy (TemStr, Value + Index, 4);\r
-          StringPtr[Index/4] = (CHAR16) StrHexToUint64 (TemStr);\r
-        }\r
-        //\r
-        // Add tailing L'\0' character\r
-        //\r
-        StringPtr[Index/4] = L'\0';\r
-      }\r
-    } else {\r
-      if (StorageWidth < ((LengthStr + 1) / 2)) {\r
-        Status = EFI_BUFFER_TOO_SMALL;\r
-      } else {\r
-        ZeroMem (TemStr, sizeof (TemStr));\r
-        for (Index = 0; Index < LengthStr; Index ++) {\r
-          TemStr[0] = Value[LengthStr - Index - 1];\r
-          DigitUint8 = (UINT8) StrHexToUint64 (TemStr);\r
-          if ((Index & 1) == 0) {\r
-            Dst [Index/2] = DigitUint8;\r
-          } else {\r
-            Dst [Index/2] = (UINT8) ((DigitUint8 << 4) + Dst [Index/2]);\r
-          }\r
-        }\r
-      }\r
-    }\r
-\r
+    Status = BufferToValue (Question, Value);\r
     if (EFI_ERROR (Status)) {\r
       FreePool (Result);\r
       return Status;\r
@@ -1716,6 +1813,7 @@ SetQuestionValue (
   UINTN               BufferLen;\r
   UINTN               StorageWidth;\r
   BROWSER_STORAGE     *Storage;\r
+  FORMSET_STORAGE     *FormsetStorage;\r
   EFI_IFR_TYPE_VALUE  *QuestionValue;\r
   CHAR16              *ConfigResp;\r
   CHAR16              *Progress;\r
@@ -1728,6 +1826,7 @@ SetQuestionValue (
   CHAR16              *TemString;\r
   UINTN               Index;\r
   NAME_VALUE_NODE     *Node;\r
+  UINTN               MaxLen;\r
 \r
   Status = EFI_SUCCESS;\r
   Node   = NULL;\r
@@ -1910,17 +2009,20 @@ SetQuestionValue (
     } else {\r
       Length += (StorageWidth * 2);\r
     }\r
-    ConfigResp = AllocateZeroPool ((StrLen (Storage->ConfigHdr) + Length + 1) * sizeof (CHAR16));\r
+    FormsetStorage = GetFstStgFromVarId(FormSet, Question->VarStoreId);\r
+    ASSERT (FormsetStorage != NULL);\r
+    MaxLen = StrLen (FormsetStorage->ConfigHdr) + Length + 1;\r
+    ConfigResp = AllocateZeroPool (MaxLen * sizeof (CHAR16));\r
     ASSERT (ConfigResp != NULL);\r
 \r
-    StrCpy (ConfigResp, Storage->ConfigHdr);\r
+    StrCpyS (ConfigResp, MaxLen, FormsetStorage->ConfigHdr);\r
     if (IsBufferStorage) {\r
-      StrCat (ConfigResp, Question->BlockName);\r
-      StrCat (ConfigResp, L"&VALUE=");\r
+      StrCatS (ConfigResp, MaxLen, Question->BlockName);\r
+      StrCatS (ConfigResp, MaxLen, L"&VALUE=");\r
     } else {\r
-      StrCat (ConfigResp, L"&");\r
-      StrCat (ConfigResp, Question->VariableName);\r
-      StrCat (ConfigResp, L"=");\r
+      StrCatS (ConfigResp, MaxLen, L"&");\r
+      StrCatS (ConfigResp, MaxLen, Question->VariableName);\r
+      StrCatS (ConfigResp, MaxLen, L"=");\r
     }\r
 \r
     Value = ConfigResp + StrLen (ConfigResp);\r
@@ -2328,6 +2430,10 @@ SendDiscardInfoToDriver (
     //\r
     GetQuestionValue (FormSet, Form, Question, GetSetValueWithEditBuffer);\r
 \r
+    if (Question->Operand == EFI_IFR_STRING_OP){\r
+      HiiSetString (FormSet->HiiHandle, Question->HiiValue.Value.string, (CHAR16*)Question->BufferValue, NULL);\r
+    }\r
+\r
     if (Question->HiiValue.Type == EFI_IFR_TYPE_BUFFER) {\r
       TypeValue = (EFI_IFR_TYPE_VALUE *) Question->BufferValue;\r
     } else {\r
@@ -2347,119 +2453,81 @@ SendDiscardInfoToDriver (
 }\r
 \r
 /**\r
-  Validate the FormSet. If the formset is not validate, remove it from the list.\r
-\r
-  @param  FormSet                The input FormSet which need to validate.\r
-\r
-  @retval TRUE                   The handle is validate.\r
-  @retval FALSE                  The handle is invalidate.\r
-\r
-**/\r
-BOOLEAN\r
-ValidateFormSet (\r
-  FORM_BROWSER_FORMSET    *FormSet\r
-  )\r
-{\r
-  EFI_HII_HANDLE          *HiiHandles;\r
-  UINTN                   Index;\r
-  BOOLEAN                 Find;\r
-\r
-  ASSERT (FormSet != NULL);\r
-  Find = FALSE;\r
-  //\r
-  // Get all the Hii handles\r
-  //\r
-  HiiHandles = HiiGetHiiHandles (NULL);\r
-  ASSERT (HiiHandles != NULL);\r
-\r
-  //\r
-  // Search for formset of each class type\r
-  //\r
-  for (Index = 0; HiiHandles[Index] != NULL; Index++) {\r
-    if (HiiHandles[Index] == FormSet->HiiHandle) {\r
-      Find = TRUE;\r
-      break;\r
-    }\r
-  }\r
-\r
-  if (!Find) {\r
-    CleanBrowserStorage(FormSet);\r
-    RemoveEntryList (&FormSet->Link);\r
-    DestroyFormSet (FormSet);\r
-  }\r
-\r
-  FreePool (HiiHandles);\r
-\r
-  return Find;\r
-}\r
-/**\r
-  Check whether need to enable the reset flag in form level.\r
-  Also clean all ValueChanged flag in question.\r
+  When submit the question value, call the callback function with Submitted type\r
+  to inform the hii driver.\r
 \r
-  @param  SetFlag                Whether need to set the Reset Flag.\r
   @param  FormSet                FormSet data structure.\r
   @param  Form                   Form data structure.\r
 \r
 **/\r
 VOID\r
-UpdateFlagForForm (\r
-  IN BOOLEAN                          SetFlag,\r
+SubmitCallbackForForm (\r
   IN FORM_BROWSER_FORMSET             *FormSet,\r
   IN FORM_BROWSER_FORM                *Form\r
   )\r
 {\r
-  LIST_ENTRY              *Link;\r
-  FORM_BROWSER_STATEMENT  *Question;\r
-  BOOLEAN                 OldValue;\r
+  LIST_ENTRY                  *Link;\r
+  FORM_BROWSER_STATEMENT      *Question;\r
+  EFI_IFR_TYPE_VALUE          *TypeValue;\r
+  EFI_BROWSER_ACTION_REQUEST  ActionRequest;\r
+\r
+  if (FormSet->ConfigAccess == NULL) {\r
+    return;\r
+  }\r
 \r
   Link = GetFirstNode (&Form->StatementListHead);\r
   while (!IsNull (&Form->StatementListHead, Link)) {\r
     Question = FORM_BROWSER_STATEMENT_FROM_LINK (Link);\r
     Link = GetNextNode (&Form->StatementListHead, Link);\r
 \r
-    if (!Question->ValueChanged) {\r
+    if (Question->Storage == NULL || Question->Storage->Type == EFI_HII_VARSTORE_EFI_VARIABLE) {\r
       continue;\r
     }\r
 \r
-    OldValue = Question->ValueChanged;\r
+    if ((Question->QuestionFlags & EFI_IFR_FLAG_CALLBACK) != EFI_IFR_FLAG_CALLBACK) {\r
+       continue;\r
+    }\r
 \r
-    //\r
-    // Compare the buffer and editbuffer data to see whether the data has been saved.\r
-    //\r
-    Question->ValueChanged = IsQuestionValueChanged(FormSet, Form, Question, GetSetValueWithBothBuffer);\r
+    if (Question->Operand == EFI_IFR_PASSWORD_OP) {\r
+       continue;\r
+    }\r
 \r
-    //\r
-    // Only the changed data has been saved, then need to set the reset flag.\r
-    //\r
-    if (SetFlag && OldValue && !Question->ValueChanged && ((Question->QuestionFlags & EFI_IFR_FLAG_RESET_REQUIRED) != 0)) {\r
-      gResetRequired = TRUE;\r
-    } \r
+    if (Question->HiiValue.Type == EFI_IFR_TYPE_BUFFER) {\r
+      TypeValue = (EFI_IFR_TYPE_VALUE *) Question->BufferValue;\r
+    } else {\r
+      TypeValue = &Question->HiiValue.Value;\r
+    }\r
+\r
+    ActionRequest = EFI_BROWSER_ACTION_REQUEST_NONE;\r
+    FormSet->ConfigAccess->Callback (\r
+                             FormSet->ConfigAccess,\r
+                             EFI_BROWSER_ACTION_SUBMITTED,\r
+                             Question->QuestionId,\r
+                             Question->HiiValue.Type,\r
+                             TypeValue,\r
+                             &ActionRequest\r
+                             );\r
   }\r
 }\r
 \r
 /**\r
-  Check whether need to enable the reset flag.\r
-  Also clean ValueChanged flag for all statements.\r
+  When value set Success, call the submit callback function.\r
 \r
-  Form level or formset level, only one.\r
-  \r
-  @param  SetFlag                Whether need to set the Reset Flag.\r
   @param  FormSet                FormSet data structure.\r
   @param  Form                   Form data structure.\r
 \r
 **/\r
 VOID\r
-ValueChangeResetFlagUpdate (\r
-  IN BOOLEAN                          SetFlag,\r
+SubmitCallback (\r
   IN FORM_BROWSER_FORMSET             *FormSet,\r
-  IN FORM_BROWSER_FORM                *Form  \r
+  IN FORM_BROWSER_FORM                *Form\r
   )\r
 {\r
   FORM_BROWSER_FORM       *CurrentForm;\r
   LIST_ENTRY              *Link;\r
 \r
   if (Form != NULL) {\r
-    UpdateFlagForForm(SetFlag, FormSet, Form);\r
+    SubmitCallbackForForm(FormSet, Form);\r
     return;\r
   }\r
 \r
@@ -2468,13 +2536,168 @@ ValueChangeResetFlagUpdate (
     CurrentForm = FORM_BROWSER_FORM_FROM_LINK (Link);\r
     Link = GetNextNode (&FormSet->FormListHead, Link);\r
 \r
-    UpdateFlagForForm(SetFlag, FormSet, CurrentForm);\r
+    SubmitCallbackForForm(FormSet, CurrentForm);\r
   }\r
 }\r
 \r
 /**\r
-  Base on the return Progress string to find the form. \r
-  \r
+  Validate the HiiHandle.\r
+\r
+  @param  HiiHandle              The input HiiHandle which need to validate.\r
+\r
+  @retval TRUE                   The handle is validate.\r
+  @retval FALSE                  The handle is invalidate.\r
+\r
+**/\r
+BOOLEAN\r
+ValidateHiiHandle (\r
+  EFI_HII_HANDLE          HiiHandle\r
+  )\r
+{\r
+  EFI_HII_HANDLE          *HiiHandles;\r
+  UINTN                   Index;\r
+  BOOLEAN                 Find;\r
+\r
+  if (HiiHandle == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  Find = FALSE;\r
+\r
+  HiiHandles = HiiGetHiiHandles (NULL);\r
+  ASSERT (HiiHandles != NULL);\r
+\r
+  for (Index = 0; HiiHandles[Index] != NULL; Index++) {\r
+    if (HiiHandles[Index] == HiiHandle) {\r
+      Find = TRUE;\r
+      break;\r
+    }\r
+  }\r
+\r
+  FreePool (HiiHandles);\r
+\r
+  return Find;\r
+}\r
+\r
+/**\r
+  Validate the FormSet. If the formset is not validate, remove it from the list.\r
+\r
+  @param  FormSet                The input FormSet which need to validate.\r
+\r
+  @retval TRUE                   The handle is validate.\r
+  @retval FALSE                  The handle is invalidate.\r
+\r
+**/\r
+BOOLEAN\r
+ValidateFormSet (\r
+  FORM_BROWSER_FORMSET    *FormSet\r
+  )\r
+{\r
+  BOOLEAN  Find;\r
+\r
+  ASSERT (FormSet != NULL);\r
+\r
+  Find = ValidateHiiHandle(FormSet->HiiHandle);\r
+  //\r
+  // Should not remove the formset which is being used.\r
+  //\r
+  if (!Find && (FormSet != gCurrentSelection->FormSet)) {\r
+    CleanBrowserStorage(FormSet);\r
+    RemoveEntryList (&FormSet->Link);\r
+    DestroyFormSet (FormSet);\r
+  }\r
+\r
+  return Find;\r
+}\r
+/**\r
+  Check whether need to enable the reset flag in form level.\r
+  Also clean all ValueChanged flag in question.\r
+\r
+  @param  SetFlag                Whether need to set the Reset Flag.\r
+  @param  FormSet                FormSet data structure.\r
+  @param  Form                   Form data structure.\r
+\r
+**/\r
+VOID\r
+UpdateFlagForForm (\r
+  IN BOOLEAN                          SetFlag,\r
+  IN FORM_BROWSER_FORMSET             *FormSet,\r
+  IN FORM_BROWSER_FORM                *Form\r
+  )\r
+{\r
+  LIST_ENTRY              *Link;\r
+  FORM_BROWSER_STATEMENT  *Question;\r
+  BOOLEAN                 OldValue;\r
+\r
+  Link = GetFirstNode (&Form->StatementListHead);\r
+  while (!IsNull (&Form->StatementListHead, Link)) {\r
+    Question = FORM_BROWSER_STATEMENT_FROM_LINK (Link);\r
+    Link = GetNextNode (&Form->StatementListHead, Link);\r
+\r
+    if (!Question->ValueChanged) {\r
+      continue;\r
+    }\r
+\r
+    OldValue = Question->ValueChanged;\r
+\r
+    //\r
+    // Compare the buffer and editbuffer data to see whether the data has been saved.\r
+    //\r
+    Question->ValueChanged = IsQuestionValueChanged(FormSet, Form, Question, GetSetValueWithBothBuffer);\r
+\r
+    //\r
+    // Only the changed data has been saved, then need to set the reset flag.\r
+    //\r
+    if (SetFlag && OldValue && !Question->ValueChanged) {\r
+      if ((Question->QuestionFlags & EFI_IFR_FLAG_RESET_REQUIRED) != 0) {\r
+        gResetRequired = TRUE;\r
+      }\r
+\r
+      if ((Question->QuestionFlags & EFI_IFR_FLAG_RECONNECT_REQUIRED) != 0) {\r
+        gFlagReconnect = TRUE;\r
+      }\r
+    } \r
+  }\r
+}\r
+\r
+/**\r
+  Check whether need to enable the reset flag.\r
+  Also clean ValueChanged flag for all statements.\r
+\r
+  Form level or formset level, only one.\r
+  \r
+  @param  SetFlag                Whether need to set the Reset Flag.\r
+  @param  FormSet                FormSet data structure.\r
+  @param  Form                   Form data structure.\r
+\r
+**/\r
+VOID\r
+ValueChangeResetFlagUpdate (\r
+  IN BOOLEAN                          SetFlag,\r
+  IN FORM_BROWSER_FORMSET             *FormSet,\r
+  IN FORM_BROWSER_FORM                *Form  \r
+  )\r
+{\r
+  FORM_BROWSER_FORM       *CurrentForm;\r
+  LIST_ENTRY              *Link;\r
+\r
+  if (Form != NULL) {\r
+    UpdateFlagForForm(SetFlag, FormSet, Form);\r
+    return;\r
+  }\r
+\r
+  Link = GetFirstNode (&FormSet->FormListHead);\r
+  while (!IsNull (&FormSet->FormListHead, Link)) {\r
+    CurrentForm = FORM_BROWSER_FORM_FROM_LINK (Link);\r
+    Link = GetNextNode (&FormSet->FormListHead, Link);\r
+\r
+    UpdateFlagForForm(SetFlag, FormSet, CurrentForm);\r
+  }\r
+}\r
+\r
+/**\r
+  Base on the return Progress string to find the form. \r
+  \r
   Base on the first return Offset/Width (Name) string to find the form\r
   which keep this string.\r
 \r
@@ -2525,6 +2748,7 @@ FindQuestionFromProgress (
       // For Name/Value type, Skip the ConfigHdr part.\r
       //\r
       EndStr = StrStr (Progress, L"PATH=");\r
+      ASSERT (EndStr != NULL);\r
       while (*EndStr != '&') {\r
         EndStr++;\r
       }\r
@@ -2535,6 +2759,7 @@ FindQuestionFromProgress (
       // For Buffer type, Skip the ConfigHdr part.\r
       //\r
       EndStr = StrStr (Progress, L"&OFFSET=");\r
+      ASSERT (EndStr != NULL);\r
       *EndStr = '\0';\r
     }\r
 \r
@@ -2550,6 +2775,7 @@ FindQuestionFromProgress (
     // here, just keep the "Fred" string.\r
     //\r
     EndStr = StrStr (Progress, L"=");\r
+    ASSERT (EndStr != NULL);\r
     *EndStr = '\0';\r
   } else {\r
     //\r
@@ -2557,6 +2783,7 @@ FindQuestionFromProgress (
     // here, just keep the "OFFSET=0x####&WIDTH=0x####" string.\r
     //\r
     EndStr = StrStr (Progress, L"&VALUE=");\r
+    ASSERT (EndStr != NULL);\r
     *EndStr = '\0';\r
   }\r
 \r
@@ -2599,6 +2826,11 @@ FindQuestionFromProgress (
           *RetQuestion = Statement;\r
           break;\r
         }\r
+\r
+        if (Statement->VariableName != NULL && StrStr (Statement->VariableName, Progress) != NULL) {\r
+          *RetQuestion = Statement;\r
+          break;\r
+        }\r
       }\r
     }\r
 \r
@@ -2619,6 +2851,108 @@ FindQuestionFromProgress (
   return (BOOLEAN) (*RetForm != NULL);\r
 }\r
 \r
+/**\r
+  Base on the return Progress string to get the SyncConfigRequest and RestoreConfigRequest\r
+  for form and formset.\r
+\r
+  @param  Storage                 Storage which has this Progress string.\r
+  @param  ConfigRequest           The ConfigRequest string.\r
+  @param  Progress                The Progress string which has the first fail string.\r
+  @param  RestoreConfigRequest    Return the RestoreConfigRequest string.\r
+  @param  SyncConfigRequest       Return the SyncConfigRequest string.\r
+\r
+**/\r
+VOID\r
+GetSyncRestoreConfigRequest(\r
+  IN  BROWSER_STORAGE   *Storage,\r
+  IN  EFI_STRING        ConfigRequest,\r
+  IN  EFI_STRING        Progress,\r
+  OUT EFI_STRING        *RestoreConfigRequest,\r
+  OUT EFI_STRING        *SyncConfigRequest\r
+  )\r
+{\r
+  EFI_STRING    EndStr;\r
+  EFI_STRING    ConfigHdrEndStr;\r
+  EFI_STRING    ElementStr;\r
+  UINTN         TotalSize;\r
+  UINTN         RestoreEleSize;\r
+  UINTN         SyncSize;\r
+\r
+  ASSERT ((*Progress == L'&') || (*Progress == L'G'));\r
+  //\r
+  // If the Progress starts with ConfigHdr, means the failure is in the first name / value pair.\r
+  // Need to restore all the fields in the ConfigRequest.\r
+  //\r
+  if (*Progress == L'G') {\r
+    *RestoreConfigRequest = AllocateCopyPool (StrSize (ConfigRequest), ConfigRequest);\r
+    ASSERT (*RestoreConfigRequest != NULL);\r
+    return;\r
+  }\r
+\r
+  //\r
+  // Find the first fail "NAME" or "OFFSET=0x####&WIDTH=0x####" string.\r
+  //\r
+  if (Storage->Type == EFI_HII_VARSTORE_NAME_VALUE) {\r
+    //\r
+    // For Name/Value type, the data is "&Fred=16&George=16&Ron=12" formset,\r
+    // here, just keep the "Fred" string.\r
+    //\r
+    EndStr = StrStr (Progress, L"=");\r
+    ASSERT (EndStr != NULL);\r
+    *EndStr = L'\0';\r
+    //\r
+    // Find the ConfigHdr in ConfigRequest.\r
+    //\r
+    ConfigHdrEndStr = StrStr (ConfigRequest, L"PATH=");\r
+    ASSERT (ConfigHdrEndStr != NULL);\r
+    while (*ConfigHdrEndStr != L'&') {\r
+      ConfigHdrEndStr++;\r
+    }\r
+  } else {\r
+    //\r
+    // For Buffer type, the data is "OFFSET=0x####&WIDTH=0x####&VALUE=0x####",\r
+    // here, just keep the "OFFSET=0x####&WIDTH=0x####" string.\r
+    //\r
+    EndStr = StrStr (Progress, L"&VALUE=");\r
+    ASSERT (EndStr != NULL);\r
+    *EndStr = L'\0';\r
+    //\r
+    // Find the ConfigHdr in ConfigRequest.\r
+    //\r
+    ConfigHdrEndStr = StrStr (ConfigRequest, L"&OFFSET=");\r
+  }\r
+  //\r
+  // Find the first fail pair in the ConfigRequest.\r
+  //\r
+  ElementStr = StrStr (ConfigRequest, Progress);\r
+  ASSERT (ElementStr != NULL);\r
+  //\r
+  // To get the RestoreConfigRequest.\r
+  //\r
+  RestoreEleSize = StrSize (ElementStr);\r
+  TotalSize = (ConfigHdrEndStr - ConfigRequest) * sizeof (CHAR16) + RestoreEleSize + sizeof (CHAR16);\r
+  *RestoreConfigRequest = AllocateZeroPool (TotalSize);\r
+  ASSERT (*RestoreConfigRequest != NULL);\r
+  StrnCpyS (*RestoreConfigRequest, TotalSize / sizeof (CHAR16), ConfigRequest, ConfigHdrEndStr - ConfigRequest);\r
+  StrCatS (*RestoreConfigRequest, TotalSize / sizeof (CHAR16), ElementStr);\r
+  //\r
+  // To get the SyncConfigRequest.\r
+  //\r
+  SyncSize = StrSize (ConfigRequest) - RestoreEleSize + sizeof (CHAR16);\r
+  *SyncConfigRequest = AllocateZeroPool (SyncSize);\r
+  ASSERT (*SyncConfigRequest != NULL);\r
+  StrnCpyS (*SyncConfigRequest, SyncSize / sizeof (CHAR16), ConfigRequest, SyncSize / sizeof (CHAR16) - 1);\r
+\r
+  //\r
+  // restore the Progress string to the original format.\r
+  //\r
+  if (Storage->Type == EFI_HII_VARSTORE_NAME_VALUE) {\r
+    *EndStr = L'=';\r
+  } else {\r
+    *EndStr = L'&';\r
+  }\r
+}\r
+\r
 /**\r
   Popup an save error info and get user input.\r
 \r
@@ -2852,6 +3186,9 @@ SubmitForForm (
   EFI_STRING              Progress;\r
   BROWSER_STORAGE         *Storage;\r
   FORM_BROWSER_CONFIG_REQUEST  *ConfigInfo;\r
+  BOOLEAN                 SubmitFormFail;\r
+\r
+  SubmitFormFail = FALSE;\r
 \r
   if (!IsNvUpdateRequiredForForm (Form)) {\r
     return EFI_SUCCESS;\r
@@ -2895,13 +3232,19 @@ SubmitForForm (
                                       ConfigResp,\r
                                       &Progress\r
                                       );\r
-    FreePool (ConfigResp);\r
 \r
     if (EFI_ERROR (Status)) {\r
+      //\r
+      // Submit fail, to get the RestoreConfigRequest and SyncConfigRequest.\r
+      //\r
+      SubmitFormFail = TRUE;\r
+      GetSyncRestoreConfigRequest (ConfigInfo->Storage, ConfigInfo->ConfigRequest, Progress, &ConfigInfo->RestoreConfigRequest, &ConfigInfo->SyncConfigRequest);\r
       InsertTailList (&gBrowserSaveFailFormSetList, &ConfigInfo->SaveFailLink);\r
+      FreePool (ConfigResp);\r
       continue;\r
     }\r
 \r
+    FreePool (ConfigResp);\r
     //\r
     // 3. Config success, update storage shadow Buffer, only update the data belong to this form.\r
     //\r
@@ -2917,8 +3260,18 @@ SubmitForForm (
       while (!IsNull (&gBrowserSaveFailFormSetList, Link)) {\r
         ConfigInfo = FORM_BROWSER_CONFIG_REQUEST_FROM_SAVE_FAIL_LINK (Link);\r
         Link = GetNextNode (&gBrowserSaveFailFormSetList, Link);\r
-\r
-        SynchronizeStorage(ConfigInfo->Storage, ConfigInfo->ConfigRequest, FALSE);\r
+        //\r
+        // Process the submit fail question, base on the RestoreConfigRequest to restore the EditBuffer\r
+        // base on the SyncConfigRequest to Sync the buffer.\r
+        //\r
+        SynchronizeStorage (ConfigInfo->Storage, ConfigInfo->RestoreConfigRequest, FALSE);\r
+        FreePool (ConfigInfo->RestoreConfigRequest);\r
+        ConfigInfo->RestoreConfigRequest = NULL;\r
+        if (ConfigInfo->SyncConfigRequest != NULL) {\r
+          SynchronizeStorage(ConfigInfo->Storage, ConfigInfo->SyncConfigRequest, TRUE);\r
+          FreePool (ConfigInfo->SyncConfigRequest);\r
+          ConfigInfo->SyncConfigRequest = NULL;\r
+        }\r
 \r
         Status = EFI_SUCCESS;\r
       }\r
@@ -2941,6 +3294,13 @@ SubmitForForm (
   //\r
   ValueChangeResetFlagUpdate(TRUE, FormSet, Form);\r
 \r
+  //\r
+  // 6 Call callback with Submitted type to inform the driver.\r
+  //\r
+  if (!SubmitFormFail) {\r
+    SubmitCallback (FormSet, Form);\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
@@ -2975,8 +3335,10 @@ SubmitForFormSet (
   FORM_BROWSER_FORM       *Form;\r
   BOOLEAN                 HasInserted;\r
   FORM_BROWSER_STATEMENT  *Question;\r
+  BOOLEAN                 SubmitFormSetFail;\r
 \r
   HasInserted = FALSE;\r
+  SubmitFormSetFail = FALSE;\r
 \r
   if (!IsNvUpdateRequiredForFormSet (FormSet)) {\r
     return EFI_SUCCESS;\r
@@ -3036,6 +3398,11 @@ SubmitForFormSet (
                                       &Progress\r
                                       );\r
     if (EFI_ERROR (Status)) {\r
+      //\r
+      // Submit fail, to get the RestoreConfigRequest and SyncConfigRequest.\r
+      //\r
+      SubmitFormSetFail = TRUE;\r
+      GetSyncRestoreConfigRequest (FormSetStorage->BrowserStorage, FormSetStorage->ConfigRequest, Progress, &FormSetStorage->RestoreConfigRequest, &FormSetStorage->SyncConfigRequest);\r
       InsertTailList (&FormSet->SaveFailStorageListHead, &FormSetStorage->SaveFailLink);\r
       if (!HasInserted) {\r
         //\r
@@ -3077,8 +3444,18 @@ SubmitForFormSet (
           FormSetStorage = FORMSET_STORAGE_FROM_SAVE_FAIL_LINK (Link);\r
           Storage        = FormSetStorage->BrowserStorage;\r
           Link = GetNextNode (&FormSet->SaveFailStorageListHead, Link);\r
-\r
-          SynchronizeStorage(FormSetStorage->BrowserStorage, FormSetStorage->ConfigRequest, FALSE);\r
+          //\r
+          // Process the submit fail question, base on the RestoreConfigRequest to restore the EditBuffer\r
+          // base on the SyncConfigRequest to Sync the buffer.\r
+          //\r
+          SynchronizeStorage (FormSetStorage->BrowserStorage, FormSetStorage->RestoreConfigRequest, FALSE);\r
+          FreePool (FormSetStorage->RestoreConfigRequest);\r
+          FormSetStorage->RestoreConfigRequest = NULL;\r
+          if (FormSetStorage->SyncConfigRequest != NULL) {\r
+            SynchronizeStorage(FormSetStorage->BrowserStorage, FormSetStorage->SyncConfigRequest, TRUE);\r
+            FreePool (FormSetStorage->SyncConfigRequest);\r
+            FormSetStorage->SyncConfigRequest = NULL;\r
+          }\r
 \r
           Status = EFI_SUCCESS;\r
         }\r
@@ -3115,6 +3492,13 @@ SubmitForFormSet (
   // \r
   ValueChangeResetFlagUpdate(TRUE, FormSet, NULL);\r
 \r
+  //\r
+  // 6. Call callback with Submitted type to inform the driver.\r
+  //\r
+  if (!SubmitFormSetFail) {\r
+    SubmitCallback (FormSet, NULL);\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
@@ -3133,7 +3517,6 @@ SubmitForSystem (
   EFI_STATUS              Status;\r
   LIST_ENTRY              *Link;\r
   LIST_ENTRY              *StorageLink;\r
-  BROWSER_STORAGE         *Storage;\r
   FORMSET_STORAGE         *FormSetStorage;\r
   FORM_BROWSER_FORM       *Form;\r
   FORM_BROWSER_FORMSET    *LocalFormSet;\r
@@ -3199,7 +3582,6 @@ SubmitForSystem (
         StorageLink = GetFirstNode (&LocalFormSet->StorageListHead);\r
         while (!IsNull (&LocalFormSet->StorageListHead, StorageLink)) {\r
           FormSetStorage = FORMSET_STORAGE_FROM_LINK (StorageLink);\r
-          Storage        = FormSetStorage->BrowserStorage;\r
           StorageLink = GetNextNode (&LocalFormSet->StorageListHead, StorageLink);\r
 \r
           SynchronizeStorage(FormSetStorage->BrowserStorage, FormSetStorage->ConfigRequest, FALSE);\r
@@ -3208,10 +3590,19 @@ SubmitForSystem (
         StorageLink = GetFirstNode (&LocalFormSet->SaveFailStorageListHead);\r
         while (!IsNull (&LocalFormSet->SaveFailStorageListHead, StorageLink)) {\r
           FormSetStorage = FORMSET_STORAGE_FROM_SAVE_FAIL_LINK (StorageLink);\r
-          Storage        = FormSetStorage->BrowserStorage;\r
           StorageLink = GetNextNode (&LocalFormSet->SaveFailStorageListHead, StorageLink);\r
-\r
-          SynchronizeStorage(FormSetStorage->BrowserStorage, FormSetStorage->ConfigRequest, FALSE);\r
+          //\r
+          // Process the submit fail question, base on the RestoreConfigRequest to restore the EditBuffer\r
+          // base on the SyncConfigRequest to Sync the buffer.\r
+          //\r
+          SynchronizeStorage (FormSetStorage->BrowserStorage, FormSetStorage->RestoreConfigRequest, FALSE);\r
+          FreePool (FormSetStorage->RestoreConfigRequest);\r
+          FormSetStorage->RestoreConfigRequest = NULL;\r
+          if ( FormSetStorage->SyncConfigRequest != NULL) {\r
+            SynchronizeStorage (FormSetStorage->BrowserStorage, FormSetStorage->SyncConfigRequest, TRUE);\r
+            FreePool (FormSetStorage->SyncConfigRequest);\r
+            FormSetStorage->SyncConfigRequest = NULL;\r
+          }\r
         }\r
       }\r
 \r
@@ -3301,205 +3692,178 @@ SubmitForm (
 }\r
 \r
 /**\r
-  Get Question default value from AltCfg string.\r
+  Converts the unicode character of the string from uppercase to lowercase.\r
+  This is a internal function.\r
 \r
-  @param  FormSet                The form set.\r
-  @param  Question               The question.\r
-  @param  DefaultId              The default Id.\r
-\r
-  @retval EFI_SUCCESS            Question is reset to default value.\r
+  @param ConfigString  String to be converted\r
 \r
 **/\r
-EFI_STATUS\r
-GetDefaultValueFromAltCfg (\r
-  IN     FORM_BROWSER_FORMSET             *FormSet,\r
-  IN OUT FORM_BROWSER_STATEMENT           *Question,\r
-  IN     UINT16                           DefaultId\r
+VOID\r
+EFIAPI\r
+HiiToLower (\r
+  IN EFI_STRING  ConfigString\r
   )\r
 {\r
-  BOOLEAN             IsBufferStorage;\r
-  BOOLEAN             IsString;  \r
-  UINTN               Length;\r
-  BROWSER_STORAGE     *Storage;\r
-  CHAR16              *ConfigRequest;\r
-  CHAR16              *Progress;\r
-  CHAR16              *Result;\r
-  CHAR16              *ConfigResp;\r
-  CHAR16              *Value;\r
-  CHAR16              *StringPtr;\r
-  UINTN               LengthStr;\r
-  UINT8               *Dst;\r
-  CHAR16              TemStr[5];\r
-  UINTN               Index;\r
-  UINT8               DigitUint8;\r
-  EFI_STATUS          Status;\r
-\r
-  Status        = EFI_NOT_FOUND;\r
-  Length        = 0;\r
-  Dst           = NULL;\r
-  ConfigRequest = NULL;\r
-  Result        = NULL;\r
-  ConfigResp    = NULL;\r
-  Value         = NULL;\r
-  Storage       = Question->Storage;\r
+  EFI_STRING  String;\r
+  BOOLEAN     Lower;\r
 \r
-  if ((Storage == NULL) || (Storage->Type == EFI_HII_VARSTORE_EFI_VARIABLE)) {\r
-    return Status;\r
-  }\r
+  ASSERT (ConfigString != NULL);\r
 \r
   //\r
-  // Question Value is provided by Buffer Storage or NameValue Storage\r
+  // Convert all hex digits in range [A-F] in the configuration header to [a-f]\r
   //\r
-  if (Question->BufferValue != NULL) {\r
-    //\r
-    // This Question is password or orderedlist\r
-    //\r
-    Dst = Question->BufferValue;\r
-  } else {\r
-    //\r
-    // Other type of Questions\r
-    //\r
-    Dst = (UINT8 *) &Question->HiiValue.Value;\r
+  for (String = ConfigString, Lower = FALSE; *String != L'\0'; String++) {\r
+    if (*String == L'=') {\r
+      Lower = TRUE;\r
+    } else if (*String == L'&') {\r
+      Lower = FALSE;\r
+    } else if (Lower && *String >= L'A' && *String <= L'F') {\r
+      *String = (CHAR16) (*String - L'A' + L'a');\r
+    }\r
   }\r
+}\r
 \r
-  if (Storage->Type == EFI_HII_VARSTORE_BUFFER || Storage->Type == EFI_HII_VARSTORE_EFI_VARIABLE_BUFFER) {\r
-    IsBufferStorage = TRUE;\r
-  } else {\r
-    IsBufferStorage = FALSE;\r
-  }\r
-  IsString = (BOOLEAN) ((Question->HiiValue.Type == EFI_IFR_TYPE_STRING) ?  TRUE : FALSE);\r
+/**\r
+  Find the point in the ConfigResp string for this question.\r
+\r
+  @param  Question               The question.\r
+  @param  ConfigResp             Get ConfigResp string.\r
+\r
+  @retval  point to the offset where is for this question.\r
+\r
+**/\r
+CHAR16 *\r
+GetOffsetFromConfigResp (\r
+  IN FORM_BROWSER_STATEMENT           *Question,\r
+  IN CHAR16                           *ConfigResp\r
+  )\r
+{\r
+  CHAR16                       *RequestElement;\r
+  CHAR16                       *BlockData;\r
 \r
   //\r
-  // <ConfigRequest> ::= <ConfigHdr> + <BlockName> ||\r
-  //                   <ConfigHdr> + "&" + <VariableName>\r
+  // Type is EFI_HII_VARSTORE_NAME_VALUE.\r
   //\r
-  if (IsBufferStorage) {\r
-    Length  = StrLen (Storage->ConfigHdr);\r
-    Length += StrLen (Question->BlockName);\r
-  } else {\r
-    Length  = StrLen (Storage->ConfigHdr);\r
-    Length += StrLen (Question->VariableName) + 1;\r
-  }\r
-  ConfigRequest = AllocateZeroPool ((Length + 1) * sizeof (CHAR16));\r
-  ASSERT (ConfigRequest != NULL);\r
+  if (Question->Storage->Type == EFI_HII_VARSTORE_NAME_VALUE) {\r
+    RequestElement = StrStr (ConfigResp, Question->VariableName);\r
+    if (RequestElement != NULL) {\r
+      //\r
+      // Skip the "VariableName=" field.\r
+      //\r
+      RequestElement += StrLen (Question->VariableName) + 1;\r
+    }\r
 \r
-  StrCpy (ConfigRequest, Storage->ConfigHdr);\r
-  if (IsBufferStorage) {\r
-    StrCat (ConfigRequest, Question->BlockName);\r
-  } else {\r
-    StrCat (ConfigRequest, L"&");\r
-    StrCat (ConfigRequest, Question->VariableName);\r
-  }\r
-\r
-  Status = mHiiConfigRouting->ExtractConfig (\r
-                                    mHiiConfigRouting,\r
-                                    ConfigRequest,\r
-                                    &Progress,\r
-                                    &Result\r
-                                    );\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
+    return RequestElement;\r
   }\r
 \r
   //\r
-  // Call ConfigRouting GetAltCfg(ConfigRoute, <ConfigResponse>, Guid, Name, DevicePath, AltCfgId, AltCfgResp)\r
-  //    Get the default configuration string according to the default ID.\r
+  // Type is EFI_HII_VARSTORE_EFI_VARIABLE or EFI_HII_VARSTORE_EFI_VARIABLE_BUFFER\r
   //\r
-  Status = mHiiConfigRouting->GetAltConfig (\r
-                                mHiiConfigRouting,\r
-                                Result,\r
-                                &Storage->Guid,\r
-                                Storage->Name,\r
-                                NULL,\r
-                                &DefaultId,  // it can be NULL to get the current setting.\r
-                                &ConfigResp\r
-                              );\r
-  \r
+\r
   //\r
-  // The required setting can't be found. So, it is not required to be validated and set.\r
+  // Convert all hex digits in ConfigResp to lower case before searching.\r
   //\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-\r
-  if (ConfigResp == NULL) {\r
-    Status = EFI_NOT_FOUND;\r
-    goto Done;\r
-  }\r
+  HiiToLower (ConfigResp);\r
 \r
   //\r
-  // Skip <ConfigRequest>\r
+  // 1. Directly use Question->BlockName to find.\r
   //\r
-  if (IsBufferStorage) {\r
-    Value = StrStr (ConfigResp, L"&VALUE");\r
-    ASSERT (Value != NULL);\r
+  RequestElement = StrStr (ConfigResp, Question->BlockName);\r
+  if (RequestElement != NULL) {\r
     //\r
-    // Skip "&VALUE"\r
+    // Skip the "Question->BlockName&VALUE=" field.\r
     //\r
-    Value = Value + 6;\r
-  } else {\r
-    Value = StrStr (ConfigResp, Question->VariableName);\r
-    ASSERT (Value != NULL);\r
-\r
-    Value = Value + StrLen (Question->VariableName);\r
-  }\r
-  if (*Value != '=') {\r
-    Status = EFI_NOT_FOUND;\r
-    goto Done;\r
+    RequestElement += StrLen (Question->BlockName) + StrLen (L"&VALUE=");\r
+    return RequestElement;\r
   }\r
+  \r
   //\r
-  // Skip '=', point to value\r
+  // 2. Change all hex digits in Question->BlockName to lower and compare again.\r
   //\r
-  Value = Value + 1;\r
+  BlockData = AllocateCopyPool (StrSize(Question->BlockName), Question->BlockName);\r
+  ASSERT (BlockData != NULL);\r
+  HiiToLower (BlockData);\r
+  RequestElement = StrStr (ConfigResp, BlockData);\r
+  FreePool (BlockData);\r
+\r
+  if (RequestElement != NULL) {\r
+    //\r
+    // Skip the "Question->BlockName&VALUE=" field.\r
+    //\r
+    RequestElement += StrLen (Question->BlockName) + StrLen (L"&VALUE=");\r
+  }\r
+\r
+  return RequestElement;\r
+}\r
+\r
+/**\r
+  Get Question default value from AltCfg string.\r
+\r
+  @param  FormSet                The form set.\r
+  @param  Form                   The form\r
+  @param  Question               The question.\r
+\r
+  @retval EFI_SUCCESS            Question is reset to default value.\r
+\r
+**/\r
+EFI_STATUS\r
+GetDefaultValueFromAltCfg (\r
+  IN     FORM_BROWSER_FORMSET             *FormSet,\r
+  IN     FORM_BROWSER_FORM                *Form,\r
+  IN OUT FORM_BROWSER_STATEMENT           *Question\r
+  )\r
+{ \r
+  BROWSER_STORAGE              *Storage;\r
+  FORMSET_STORAGE              *FormSetStorage;\r
+  CHAR16                       *ConfigResp;\r
+  CHAR16                       *Value;\r
+  LIST_ENTRY                   *Link;\r
+  FORM_BROWSER_CONFIG_REQUEST  *ConfigInfo;\r
+\r
+  Storage = Question->Storage;\r
+  if ((Storage == NULL) || (Storage->Type == EFI_HII_VARSTORE_EFI_VARIABLE)) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
 \r
   //\r
-  // Suppress <AltResp> if any\r
+  // Try to get AltCfg string from form. If not found it, then\r
+  // try to get it from formset.\r
   //\r
-  StringPtr = Value;\r
-  while (*StringPtr != L'\0' && *StringPtr != L'&') {\r
-    StringPtr++;\r
-  }\r
-  *StringPtr = L'\0';\r
+  ConfigResp    = NULL;\r
+  Link = GetFirstNode (&Form->ConfigRequestHead);\r
+  while (!IsNull (&Form->ConfigRequestHead, Link)) {\r
+    ConfigInfo = FORM_BROWSER_CONFIG_REQUEST_FROM_LINK (Link);\r
+    Link = GetNextNode (&Form->ConfigRequestHead, Link);\r
 \r
-  LengthStr = StrLen (Value);\r
-  if (!IsBufferStorage && IsString) {\r
-    StringPtr = (CHAR16 *) Dst;\r
-    ZeroMem (TemStr, sizeof (TemStr));\r
-    for (Index = 0; Index < LengthStr; Index += 4) {\r
-      StrnCpy (TemStr, Value + Index, 4);\r
-      StringPtr[Index/4] = (CHAR16) StrHexToUint64 (TemStr);\r
+    if (Storage == ConfigInfo->Storage) {\r
+      ConfigResp = ConfigInfo->ConfigAltResp;\r
+      break;\r
     }\r
-    //\r
-    // Add tailing L'\0' character\r
-    //\r
-    StringPtr[Index/4] = L'\0';\r
-  } else {\r
-    ZeroMem (TemStr, sizeof (TemStr));\r
-    for (Index = 0; Index < LengthStr; Index ++) {\r
-      TemStr[0] = Value[LengthStr - Index - 1];\r
-      DigitUint8 = (UINT8) StrHexToUint64 (TemStr);\r
-      if ((Index & 1) == 0) {\r
-        Dst [Index/2] = DigitUint8;\r
-      } else {\r
-        Dst [Index/2] = (UINT8) ((DigitUint8 << 4) + Dst [Index/2]);\r
+  }\r
+\r
+  if (ConfigResp == NULL) {\r
+    Link = GetFirstNode (&FormSet->StorageListHead);\r
+    while (!IsNull (&FormSet->StorageListHead, Link)) {\r
+      FormSetStorage = FORMSET_STORAGE_FROM_LINK (Link);\r
+      Link = GetNextNode (&FormSet->StorageListHead, Link);\r
+\r
+      if (Storage == FormSetStorage->BrowserStorage) {\r
+        ConfigResp = FormSetStorage->ConfigAltResp;\r
+        break;\r
       }\r
     }\r
   }\r
 \r
-Done:\r
-  if (ConfigRequest != NULL){\r
-    FreePool (ConfigRequest);\r
+  if (ConfigResp == NULL) {\r
+    return EFI_NOT_FOUND;\r
   }\r
 \r
-  if (ConfigResp != NULL) {\r
-    FreePool (ConfigResp);\r
-  }\r
-  \r
-  if (Result != NULL) {\r
-    FreePool (Result);\r
+  Value = GetOffsetFromConfigResp (Question, ConfigResp);\r
+  if (Value == NULL) {\r
+    return EFI_NOT_FOUND;\r
   }\r
 \r
-  return Status;\r
+  return BufferToValue (Question, Value);\r
 }\r
 \r
 /**\r
@@ -3694,9 +4058,15 @@ GetQuestionDefault (
   EFI_BROWSER_ACTION_REQUEST      ActionRequest;\r
   INTN                            Action;\r
   CHAR16                          *NewString;\r
+  EFI_IFR_TYPE_VALUE              *TypeValue;\r
+  UINT16                          OriginalDefaultId;\r
+  FORMSET_DEFAULTSTORE            *DefaultStore;\r
+  LIST_ENTRY                      *DefaultLink;\r
 \r
   Status   = EFI_NOT_FOUND;\r
   StrValue = NULL;\r
+  OriginalDefaultId  = DefaultId;\r
+  DefaultLink        = GetFirstNode (&FormSet->DefaultStoreListHead);\r
 \r
   //\r
   // Statement don't have storage, skip them\r
@@ -3713,7 +4083,15 @@ GetQuestionDefault (
   //  4, set flags of EFI_ONE_OF_OPTION (provide Standard and Manufacturing default)\r
   //  5, set flags of EFI_IFR_CHECKBOX (provide Standard and Manufacturing default) (lowest priority)\r
   //\r
+ReGetDefault:\r
   HiiValue = &Question->HiiValue;\r
+  TypeValue = &HiiValue->Value;\r
+  if (HiiValue->Type == EFI_IFR_TYPE_BUFFER) {\r
+    //\r
+    // For orderedlist, need to pass the BufferValue to Callback function.\r
+    //\r
+    TypeValue = (EFI_IFR_TYPE_VALUE *) Question->BufferValue;\r
+  }\r
 \r
   //\r
   // Get Question defaut value from call back function.\r
@@ -3727,7 +4105,7 @@ GetQuestionDefault (
                              Action,\r
                              Question->QuestionId,\r
                              HiiValue->Type,\r
-                             &HiiValue->Value,\r
+                             TypeValue,\r
                              &ActionRequest\r
                              );\r
     if (!EFI_ERROR (Status)) {\r
@@ -3737,6 +4115,7 @@ GetQuestionDefault (
 \r
         ASSERT (StrLen (NewString) * sizeof (CHAR16) <= Question->StorageWidth);\r
         if (StrLen (NewString) * sizeof (CHAR16) <= Question->StorageWidth) {\r
+          ZeroMem (Question->BufferValue, Question->StorageWidth);\r
           CopyMem (Question->BufferValue, NewString, StrSize (NewString));\r
         } else {\r
           CopyMem (Question->BufferValue, NewString, Question->StorageWidth);\r
@@ -3752,7 +4131,7 @@ GetQuestionDefault (
   // Get default value from altcfg string.\r
   //\r
   if (ConfigAccess != NULL) {  \r
-    Status = GetDefaultValueFromAltCfg(FormSet, Question, DefaultId);\r
+    Status = GetDefaultValueFromAltCfg(FormSet, Form, Question);\r
     if (!EFI_ERROR (Status)) {\r
         return Status;\r
     }\r
@@ -3793,7 +4172,12 @@ GetQuestionDefault (
           //\r
           // Default value is embedded in EFI_IFR_DEFAULT\r
           //\r
-          CopyMem (HiiValue, &Default->Value, sizeof (EFI_HII_VALUE));\r
+          if (Default->Value.Type == EFI_IFR_TYPE_BUFFER) {\r
+            ASSERT (HiiValue->Buffer != NULL);\r
+            CopyMem (HiiValue->Buffer, Default->Value.Buffer, Default->Value.BufferLen);\r
+          } else {\r
+            CopyMem (HiiValue, &Default->Value, sizeof (EFI_HII_VALUE));\r
+          }\r
         }\r
 \r
         if (HiiValue->Type == EFI_IFR_TYPE_STRING) {\r
@@ -3802,6 +4186,7 @@ GetQuestionDefault (
             return EFI_NOT_FOUND;\r
           }\r
           if (Question->StorageWidth > StrSize (StrValue)) {\r
+            ZeroMem (Question->BufferValue, Question->StorageWidth);\r
             CopyMem (Question->BufferValue, StrValue, StrSize (StrValue));\r
           } else {\r
             CopyMem (Question->BufferValue, StrValue, Question->StorageWidth);\r
@@ -3838,101 +4223,394 @@ GetQuestionDefault (
            ) {\r
           CopyMem (HiiValue, &Option->Value, sizeof (EFI_HII_VALUE));\r
 \r
-          return EFI_SUCCESS;\r
-        }\r
-      }\r
+          return EFI_SUCCESS;\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // EFI_IFR_CHECKBOX - lowest priority\r
+  //\r
+  if (Question->Operand == EFI_IFR_CHECKBOX_OP) {\r
+    if (DefaultId <= EFI_HII_DEFAULT_CLASS_MANUFACTURING)  {\r
+      //\r
+      // Checkbox could only provide Standard and Manufacturing default\r
+      //\r
+      if (((DefaultId == EFI_HII_DEFAULT_CLASS_STANDARD) && ((Question->Flags & EFI_IFR_CHECKBOX_DEFAULT) != 0)) ||\r
+          ((DefaultId == EFI_HII_DEFAULT_CLASS_MANUFACTURING) && ((Question->Flags & EFI_IFR_CHECKBOX_DEFAULT_MFG) != 0))\r
+         ) {\r
+        HiiValue->Value.b = TRUE;\r
+      }\r
+\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+\r
+  //\r
+  // For question without default value for current default Id, we try to re-get the default value form other default id in the DefaultStoreList.\r
+  // If get, will exit the function, if not, will choose next default id in the DefaultStoreList.\r
+  // The default id in DefaultStoreList are in ascending order to make sure choose the smallest default id every time.\r
+  //\r
+  while (!IsNull(&FormSet->DefaultStoreListHead, DefaultLink)) {\r
+    DefaultStore = FORMSET_DEFAULTSTORE_FROM_LINK(DefaultLink);\r
+    DefaultLink = GetNextNode (&FormSet->DefaultStoreListHead,DefaultLink);\r
+    DefaultId = DefaultStore->DefaultId;\r
+    if (DefaultId == OriginalDefaultId) {\r
+      continue;\r
+    }\r
+    goto ReGetDefault;\r
+  }\r
+\r
+  //\r
+  // For Questions without default value for all the default id in the DefaultStoreList.\r
+  //\r
+  Status = EFI_NOT_FOUND;\r
+  switch (Question->Operand) {\r
+  case EFI_IFR_CHECKBOX_OP:\r
+    HiiValue->Value.b = FALSE;\r
+    Status = EFI_SUCCESS;\r
+    break;\r
+\r
+  case EFI_IFR_NUMERIC_OP:\r
+    //\r
+    // Take minimum value as numeric default value\r
+    //\r
+    if ((Question->Flags & EFI_IFR_DISPLAY) == 0) {\r
+      //\r
+      // In EFI_IFR_DISPLAY_INT_DEC type, should check value with int* type.\r
+      //\r
+      switch (Question->Flags & EFI_IFR_NUMERIC_SIZE) {\r
+      case EFI_IFR_NUMERIC_SIZE_1:\r
+        if (((INT8) HiiValue->Value.u8 < (INT8) Question->Minimum) || ((INT8) HiiValue->Value.u8 > (INT8) Question->Maximum)) {\r
+          HiiValue->Value.u8 = (UINT8) Question->Minimum;\r
+          Status = EFI_SUCCESS;\r
+        }\r
+        break;\r
+      case EFI_IFR_NUMERIC_SIZE_2:\r
+        if (((INT16) HiiValue->Value.u16 < (INT16) Question->Minimum) || ((INT16) HiiValue->Value.u16 > (INT16) Question->Maximum)) {\r
+          HiiValue->Value.u16 = (UINT16) Question->Minimum;\r
+          Status = EFI_SUCCESS;\r
+        }\r
+        break;\r
+      case EFI_IFR_NUMERIC_SIZE_4:\r
+        if (((INT32) HiiValue->Value.u32 < (INT32) Question->Minimum) || ((INT32) HiiValue->Value.u32 > (INT32) Question->Maximum)) {\r
+          HiiValue->Value.u32 = (UINT32) Question->Minimum;\r
+          Status = EFI_SUCCESS;\r
+        }\r
+        break;\r
+      case EFI_IFR_NUMERIC_SIZE_8:\r
+        if (((INT64) HiiValue->Value.u64 < (INT64) Question->Minimum) || ((INT64) HiiValue->Value.u64 > (INT64) Question->Maximum)) {\r
+          HiiValue->Value.u64 = Question->Minimum;\r
+          Status = EFI_SUCCESS;\r
+        }\r
+        break;\r
+      default:\r
+        break;     \r
+      }\r
+    } else {\r
+      if ((HiiValue->Value.u64 < Question->Minimum) || (HiiValue->Value.u64 > Question->Maximum)) {\r
+        HiiValue->Value.u64 = Question->Minimum;\r
+        Status = EFI_SUCCESS;\r
+      }\r
+    }\r
+    break;\r
+\r
+  case EFI_IFR_ONE_OF_OP:\r
+    //\r
+    // Take first oneof option as oneof's default value\r
+    //\r
+    if (ValueToOption (Question, HiiValue) == NULL) {\r
+      Link = GetFirstNode (&Question->OptionListHead);\r
+      while (!IsNull (&Question->OptionListHead, Link)) {\r
+        Option = QUESTION_OPTION_FROM_LINK (Link);\r
+        Link = GetNextNode (&Question->OptionListHead, Link);\r
+\r
+        if ((Option->SuppressExpression != NULL) &&\r
+            EvaluateExpressionList(Option->SuppressExpression, FALSE, NULL, NULL) != ExpressFalse) {\r
+          continue;\r
+        }\r
+\r
+        CopyMem (HiiValue, &Option->Value, sizeof (EFI_HII_VALUE));\r
+        Status = EFI_SUCCESS;\r
+        break;\r
+      }\r
+    }\r
+    break;\r
+\r
+  case EFI_IFR_ORDERED_LIST_OP:\r
+    //\r
+    // Take option sequence in IFR as ordered list's default value\r
+    //\r
+    Index = 0;\r
+    Link = GetFirstNode (&Question->OptionListHead);\r
+    while (!IsNull (&Question->OptionListHead, Link)) {\r
+      Status = EFI_SUCCESS;\r
+      Option = QUESTION_OPTION_FROM_LINK (Link);\r
+      Link = GetNextNode (&Question->OptionListHead, Link);\r
+\r
+      if ((Option->SuppressExpression != NULL) &&\r
+          EvaluateExpressionList(Option->SuppressExpression, FALSE, NULL, NULL) != ExpressFalse) {\r
+        continue;\r
+      }\r
+\r
+      SetArrayData (Question->BufferValue, Question->ValueType, Index, Option->Value.Value.u64);\r
+\r
+      Index++;\r
+      if (Index >= Question->MaxContainers) {\r
+        break;\r
+      }\r
+    }\r
+    break;\r
+\r
+  default:\r
+    break;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Get AltCfg string for current form.\r
+\r
+  @param  FormSet                Form data structure.\r
+  @param  Form                   Form data structure.\r
+  @param  DefaultId              The Class of the default.\r
+  @param  BrowserStorage         The input request storage for the questions.\r
+\r
+**/\r
+VOID\r
+ExtractAltCfgForForm (\r
+  IN FORM_BROWSER_FORMSET   *FormSet,\r
+  IN FORM_BROWSER_FORM      *Form,\r
+  IN UINT16                 DefaultId,\r
+  IN BROWSER_STORAGE        *BrowserStorage\r
+  )\r
+{\r
+  EFI_STATUS                   Status;\r
+  LIST_ENTRY                   *Link;\r
+  CHAR16                       *ConfigResp;\r
+  CHAR16                       *Progress;\r
+  CHAR16                       *Result;\r
+  BROWSER_STORAGE              *Storage;\r
+  FORM_BROWSER_CONFIG_REQUEST  *ConfigInfo;\r
+  FORMSET_STORAGE              *FormSetStorage;\r
+\r
+  //\r
+  // Check whether has get AltCfg string for this formset.\r
+  // If yes, no need to get AltCfg for form.\r
+  //\r
+  Link = GetFirstNode (&FormSet->StorageListHead);\r
+  while (!IsNull (&FormSet->StorageListHead, Link)) {\r
+    FormSetStorage = FORMSET_STORAGE_FROM_LINK (Link);\r
+    Storage        = FormSetStorage->BrowserStorage;\r
+    Link = GetNextNode (&FormSet->StorageListHead, Link);\r
+    if (BrowserStorage != NULL && BrowserStorage != Storage) {\r
+      continue;\r
+    }\r
+\r
+    if (Storage->Type != EFI_HII_VARSTORE_EFI_VARIABLE &&\r
+        FormSetStorage->ElementCount != 0 &&\r
+        FormSetStorage->HasCallAltCfg) {\r
+      return;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Get AltCfg string for each form.\r
+  //\r
+  Link = GetFirstNode (&Form->ConfigRequestHead);\r
+  while (!IsNull (&Form->ConfigRequestHead, Link)) {\r
+    ConfigInfo = FORM_BROWSER_CONFIG_REQUEST_FROM_LINK (Link);\r
+    Link = GetNextNode (&Form->ConfigRequestHead, Link);\r
+\r
+    Storage = ConfigInfo->Storage;\r
+    if (BrowserStorage != NULL && BrowserStorage != Storage) {\r
+      continue;\r
+    }\r
+\r
+    if (Storage->Type == EFI_HII_VARSTORE_EFI_VARIABLE) {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // 1. Skip if there is no RequestElement\r
+    //\r
+    if (ConfigInfo->ElementCount == 0) {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // 2. Get value through hii config routine protocol.\r
+    //\r
+    Status = mHiiConfigRouting->ExtractConfig (\r
+                                      mHiiConfigRouting,\r
+                                      ConfigInfo->ConfigRequest,\r
+                                      &Progress,\r
+                                      &Result\r
+                                      );\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // 3. Call ConfigRouting GetAltCfg(ConfigRoute, <ConfigResponse>, Guid, Name, DevicePath, AltCfgId, AltCfgResp)\r
+    //    Get the default configuration string according to the default ID.\r
+    //\r
+    Status = mHiiConfigRouting->GetAltConfig (\r
+                                  mHiiConfigRouting,\r
+                                  Result,\r
+                                  &Storage->Guid,\r
+                                  Storage->Name,\r
+                                  NULL,\r
+                                  &DefaultId,  // it can be NULL to get the current setting.\r
+                                  &ConfigResp\r
+                                );\r
+    FreePool (Result);\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+\r
+    ConfigInfo->ConfigAltResp = ConfigResp;\r
+  }\r
+}\r
+\r
+/**\r
+  Clean AltCfg string for current form.\r
+\r
+  @param  Form                   Form data structure.\r
+\r
+**/\r
+VOID\r
+CleanAltCfgForForm (\r
+  IN FORM_BROWSER_FORM   *Form\r
+  )\r
+{\r
+  LIST_ENTRY              *Link;\r
+  FORM_BROWSER_CONFIG_REQUEST  *ConfigInfo;\r
+\r
+  Link = GetFirstNode (&Form->ConfigRequestHead);\r
+  while (!IsNull (&Form->ConfigRequestHead, Link)) {\r
+    ConfigInfo = FORM_BROWSER_CONFIG_REQUEST_FROM_LINK (Link);\r
+    Link = GetNextNode (&Form->ConfigRequestHead, Link);\r
+\r
+    if (ConfigInfo->ConfigAltResp != NULL) {\r
+      FreePool (ConfigInfo->ConfigAltResp);\r
+      ConfigInfo->ConfigAltResp = NULL;\r
     }\r
   }\r
+}\r
 \r
-  //\r
-  // EFI_IFR_CHECKBOX - lowest priority\r
-  //\r
-  if (Question->Operand == EFI_IFR_CHECKBOX_OP) {\r
-    if (DefaultId <= EFI_HII_DEFAULT_CLASS_MANUFACTURING)  {\r
-      //\r
-      // Checkbox could only provide Standard and Manufacturing default\r
-      //\r
-      if (((DefaultId == EFI_HII_DEFAULT_CLASS_STANDARD) && ((Question->Flags & EFI_IFR_CHECKBOX_DEFAULT) != 0)) ||\r
-          ((DefaultId == EFI_HII_DEFAULT_CLASS_MANUFACTURING) && ((Question->Flags & EFI_IFR_CHECKBOX_DEFAULT_MFG) != 0))\r
-         ) {\r
-        HiiValue->Value.b = TRUE;\r
-      } else {\r
-        HiiValue->Value.b = FALSE;\r
-      }\r
+/**\r
+  Get AltCfg string for current formset.\r
 \r
-      return EFI_SUCCESS;\r
+  @param  FormSet                Form data structure.\r
+  @param  DefaultId              The Class of the default.\r
+  @param  BrowserStorage         The input request storage for the questions.\r
+\r
+**/\r
+VOID\r
+ExtractAltCfgForFormSet (\r
+  IN FORM_BROWSER_FORMSET   *FormSet,\r
+  IN UINT16                 DefaultId,\r
+  IN BROWSER_STORAGE        *BrowserStorage\r
+  )\r
+{\r
+  EFI_STATUS              Status;\r
+  LIST_ENTRY              *Link;\r
+  CHAR16                  *ConfigResp;\r
+  CHAR16                  *Progress;\r
+  CHAR16                  *Result;\r
+  BROWSER_STORAGE         *Storage;\r
+  FORMSET_STORAGE         *FormSetStorage;\r
+\r
+  Link = GetFirstNode (&FormSet->StorageListHead);\r
+  while (!IsNull (&FormSet->StorageListHead, Link)) {\r
+    FormSetStorage = FORMSET_STORAGE_FROM_LINK (Link);\r
+    Storage        = FormSetStorage->BrowserStorage;\r
+    Link = GetNextNode (&FormSet->StorageListHead, Link);\r
+\r
+    if (BrowserStorage != NULL && BrowserStorage != Storage) {\r
+      continue;\r
     }\r
-  }\r
 \r
-  //\r
-  // For Questions without default\r
-  //\r
-  Status = EFI_NOT_FOUND;\r
-  switch (Question->Operand) {\r
-  case EFI_IFR_NUMERIC_OP:\r
-    //\r
-    // Take minimum value as numeric default value\r
-    //\r
-    if ((HiiValue->Value.u64 < Question->Minimum) || (HiiValue->Value.u64 > Question->Maximum)) {\r
-      HiiValue->Value.u64 = Question->Minimum;\r
-      Status = EFI_SUCCESS;\r
+    if (Storage->Type == EFI_HII_VARSTORE_EFI_VARIABLE) {\r
+      continue;\r
     }\r
-    break;\r
 \r
-  case EFI_IFR_ONE_OF_OP:\r
     //\r
-    // Take first oneof option as oneof's default value\r
+    // 1. Skip if there is no RequestElement\r
     //\r
-    if (ValueToOption (Question, HiiValue) == NULL) {\r
-      Link = GetFirstNode (&Question->OptionListHead);\r
-      while (!IsNull (&Question->OptionListHead, Link)) {\r
-        Option = QUESTION_OPTION_FROM_LINK (Link);\r
-        Link = GetNextNode (&Question->OptionListHead, Link);\r
+    if (FormSetStorage->ElementCount == 0) {\r
+      continue;\r
+    }\r
 \r
-        if ((Option->SuppressExpression != NULL) &&\r
-            EvaluateExpressionList(Option->SuppressExpression, FALSE, NULL, NULL) != ExpressFalse) {\r
-          continue;\r
-        }\r
+    FormSetStorage->HasCallAltCfg = TRUE;\r
 \r
-        CopyMem (HiiValue, &Option->Value, sizeof (EFI_HII_VALUE));\r
-        Status = EFI_SUCCESS;\r
-        break;\r
-      }\r
+    //\r
+    // 2. Get value through hii config routine protocol.\r
+    //\r
+    Status = mHiiConfigRouting->ExtractConfig (\r
+                                      mHiiConfigRouting,\r
+                                      FormSetStorage->ConfigRequest,\r
+                                      &Progress,\r
+                                      &Result\r
+                                      );\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
     }\r
-    break;\r
 \r
-  case EFI_IFR_ORDERED_LIST_OP:\r
     //\r
-    // Take option sequence in IFR as ordered list's default value\r
+    // 3. Call ConfigRouting GetAltCfg(ConfigRoute, <ConfigResponse>, Guid, Name, DevicePath, AltCfgId, AltCfgResp)\r
+    //    Get the default configuration string according to the default ID.\r
     //\r
-    Index = 0;\r
-    Link = GetFirstNode (&Question->OptionListHead);\r
-    while (!IsNull (&Question->OptionListHead, Link)) {\r
-      Status = EFI_SUCCESS;\r
-      Option = QUESTION_OPTION_FROM_LINK (Link);\r
-      Link = GetNextNode (&Question->OptionListHead, Link);\r
-\r
-      if ((Option->SuppressExpression != NULL) &&\r
-          EvaluateExpressionList(Option->SuppressExpression, FALSE, NULL, NULL) != ExpressFalse) {\r
-        continue;\r
-      }\r
-\r
-      SetArrayData (Question->BufferValue, Question->ValueType, Index, Option->Value.Value.u64);\r
+    Status = mHiiConfigRouting->GetAltConfig (\r
+                                  mHiiConfigRouting,\r
+                                  Result,\r
+                                  &Storage->Guid,\r
+                                  Storage->Name,\r
+                                  NULL,\r
+                                  &DefaultId,  // it can be NULL to get the current setting.\r
+                                  &ConfigResp\r
+                                );\r
 \r
-      Index++;\r
-      if (Index >= Question->MaxContainers) {\r
-        break;\r
-      }\r
+    FreePool (Result);\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
     }\r
-    break;\r
 \r
-  default:\r
-    break;\r
+    FormSetStorage->ConfigAltResp = ConfigResp;\r
   }\r
 \r
-  return Status;\r
 }\r
 \r
+/**\r
+  Clean AltCfg string for current formset.\r
+\r
+  @param  FormSet                Form data structure.\r
+\r
+**/\r
+VOID\r
+CleanAltCfgForFormSet (\r
+  IN FORM_BROWSER_FORMSET   *FormSet\r
+  )\r
+{\r
+  LIST_ENTRY              *Link;\r
+  FORMSET_STORAGE         *FormSetStorage;\r
+\r
+  Link = GetFirstNode (&FormSet->StorageListHead);\r
+  while (!IsNull (&FormSet->StorageListHead, Link)) {\r
+    FormSetStorage = FORMSET_STORAGE_FROM_LINK (Link);\r
+    Link = GetNextNode (&FormSet->StorageListHead, Link);\r
+\r
+    if (FormSetStorage->ConfigAltResp != NULL) {\r
+      FreePool (FormSetStorage->ConfigAltResp);\r
+      FormSetStorage->ConfigAltResp = NULL;\r
+    }\r
+\r
+    FormSetStorage->HasCallAltCfg = FALSE;\r
+  }\r
+}\r
 \r
 /**\r
   Reset Questions to their initial value or default value in a Form, Formset or System.\r
@@ -3949,6 +4627,7 @@ GetQuestionDefault (
   @param  RetrieveValueFirst     Whether call the retrieve call back to\r
                                  get the initial value before get default\r
                                  value.\r
+  @param  SkipGetAltCfg          Whether skip the get altcfg string process.\r
 \r
   @retval EFI_SUCCESS            The function completed successfully.\r
   @retval EFI_UNSUPPORTED        Unsupport SettingScope.\r
@@ -3962,7 +4641,8 @@ ExtractDefault (
   IN BROWSER_SETTING_SCOPE            SettingScope,\r
   IN BROWSER_GET_DEFAULT_VALUE        GetDefaultValueScope,\r
   IN BROWSER_STORAGE                  *Storage OPTIONAL,\r
-  IN BOOLEAN                          RetrieveValueFirst\r
+  IN BOOLEAN                          RetrieveValueFirst,\r
+  IN BOOLEAN                          SkipGetAltCfg\r
   )\r
 {\r
   EFI_STATUS              Status;\r
@@ -3984,8 +4664,15 @@ ExtractDefault (
   if (GetDefaultValueScope == GetDefaultForStorage && Storage == NULL) {\r
     return EFI_UNSUPPORTED;\r
   }\r
-  \r
+\r
   if (SettingScope == FormLevel) {\r
+    //\r
+    // Prepare the AltCfg String for form.\r
+    //\r
+    if (!SkipGetAltCfg && (GetDefaultValueScope != GetDefaultForNoStorage)) {\r
+      ExtractAltCfgForForm (FormSet, Form, DefaultId, Storage);\r
+    }\r
+\r
     //\r
     // Extract Form default\r
     //\r
@@ -4042,13 +4729,34 @@ ExtractDefault (
         SetQuestionValue (FormSet, Form, Question, GetSetValueWithEditBuffer);\r
       }\r
     }\r
+\r
+    //\r
+    // Clean the AltCfg String.\r
+    //\r
+    if (!SkipGetAltCfg && (GetDefaultValueScope != GetDefaultForNoStorage)) {\r
+      CleanAltCfgForForm(Form);\r
+    }\r
   } else if (SettingScope == FormSetLevel) {\r
+    //\r
+    // Prepare the AltCfg String for formset.\r
+    //\r
+    if (!SkipGetAltCfg && (GetDefaultValueScope != GetDefaultForNoStorage)) {\r
+      ExtractAltCfgForFormSet (FormSet, DefaultId, Storage);\r
+    }\r
+\r
     FormLink = GetFirstNode (&FormSet->FormListHead);\r
     while (!IsNull (&FormSet->FormListHead, FormLink)) {\r
       Form = FORM_BROWSER_FORM_FROM_LINK (FormLink);\r
-      ExtractDefault (FormSet, Form, DefaultId, FormLevel, GetDefaultValueScope, Storage, RetrieveValueFirst);\r
+      ExtractDefault (FormSet, Form, DefaultId, FormLevel, GetDefaultValueScope, Storage, RetrieveValueFirst, SkipGetAltCfg);\r
       FormLink = GetNextNode (&FormSet->FormListHead, FormLink);\r
     }\r
+\r
+    //\r
+    // Clean the AltCfg String.\r
+    //\r
+    if (!SkipGetAltCfg && (GetDefaultValueScope != GetDefaultForNoStorage)) {\r
+      CleanAltCfgForFormSet (FormSet);\r
+    }\r
   } else if (SettingScope == SystemLevel) {\r
     //\r
     // Preload all Hii formset.\r
@@ -4070,7 +4778,7 @@ ExtractDefault (
 \r
       mSystemLevelFormSet = LocalFormSet;\r
 \r
-      ExtractDefault (LocalFormSet, NULL, DefaultId, FormSetLevel, GetDefaultValueScope, Storage, RetrieveValueFirst);\r
+      ExtractDefault (LocalFormSet, NULL, DefaultId, FormSetLevel, GetDefaultValueScope, Storage, RetrieveValueFirst, SkipGetAltCfg);\r
     }\r
 \r
     mSystemLevelFormSet = OldFormSet;\r
@@ -4181,9 +4889,8 @@ IsQuestionValueChanged (
   }\r
 \r
   CopyMem (&Question->HiiValue, &BackUpValue, sizeof (EFI_HII_VALUE));\r
-  CopyMem (Question->BufferValue, BackUpBuffer, BufferWidth);\r
-\r
   if (BackUpBuffer != NULL) {\r
+    CopyMem (Question->BufferValue, BackUpBuffer, BufferWidth);\r
     FreePool (BackUpBuffer);\r
   }\r
 \r
@@ -4328,13 +5035,13 @@ RemoveElement (
 /**\r
   Adjust config request in storage, remove the request elements existed in the input ConfigRequest.\r
 \r
-  @param  Storage                Pointer to the browser storage.\r
+  @param  Storage                Pointer to the formset storage.\r
   @param  ConfigRequest          The pointer to the Request element.\r
 \r
 **/\r
 VOID\r
 RemoveConfigRequest (\r
-  BROWSER_STORAGE   *Storage,\r
+  FORMSET_STORAGE   *Storage,\r
   CHAR16            *ConfigRequest\r
   )\r
 {\r
@@ -4349,7 +5056,7 @@ RemoveConfigRequest (
     return;\r
   }\r
 \r
-  if (Storage->Type == EFI_HII_VARSTORE_NAME_VALUE) {\r
+  if (Storage->BrowserStorage->Type == EFI_HII_VARSTORE_NAME_VALUE) {\r
     //\r
     // "&Name1&Name2" section for EFI_HII_VARSTORE_NAME_VALUE storage\r
     //\r
@@ -4364,7 +5071,7 @@ RemoveConfigRequest (
   //\r
   // Find SearchKey storage\r
   //\r
-  if (Storage->Type == EFI_HII_VARSTORE_NAME_VALUE) {\r
+  if (Storage->BrowserStorage->Type == EFI_HII_VARSTORE_NAME_VALUE) {\r
     RequestElement = StrStr (ConfigRequest, L"PATH");\r
     ASSERT (RequestElement != NULL);\r
     RequestElement = StrStr (RequestElement, SearchKey);    \r
@@ -4388,7 +5095,7 @@ RemoveConfigRequest (
       *NextRequestElement = L'\0';\r
     }\r
 \r
-    RemoveElement (Storage, RequestElement);\r
+    RemoveElement (Storage->BrowserStorage, RequestElement);\r
 \r
     if (NextRequestElement != NULL) {\r
       //\r
@@ -4403,10 +5110,10 @@ RemoveConfigRequest (
   //\r
   // If no request element remain, just remove the ConfigRequest string.\r
   //\r
-  if (StrCmp (Storage->ConfigRequest, Storage->ConfigHdr) == 0) {\r
-    FreePool (Storage->ConfigRequest);\r
-    Storage->ConfigRequest = NULL;\r
-    Storage->SpareStrLen   = 0;\r
+  if (StrCmp (Storage->BrowserStorage->ConfigRequest, Storage->ConfigHdr) == 0) {\r
+    FreePool (Storage->BrowserStorage->ConfigRequest);\r
+    Storage->BrowserStorage->ConfigRequest = NULL;\r
+    Storage->BrowserStorage->SpareStrLen   = 0;\r
   }\r
 }\r
 \r
@@ -4434,7 +5141,7 @@ CleanBrowserStorage (
         continue;\r
       }\r
 \r
-      RemoveConfigRequest (Storage->BrowserStorage, Storage->ConfigRequest);\r
+      RemoveConfigRequest (Storage, Storage->ConfigRequest);\r
     } else if (Storage->BrowserStorage->Type == EFI_HII_VARSTORE_BUFFER ||\r
                Storage->BrowserStorage->Type == EFI_HII_VARSTORE_NAME_VALUE) {\r
       if (Storage->BrowserStorage->ConfigRequest != NULL) { \r
@@ -4483,8 +5190,11 @@ AppendConfigRequest (
   CHAR16   *NewStr;\r
   UINTN    StringSize;\r
   UINTN    StrLength;\r
+  UINTN    MaxLen;\r
 \r
   StrLength = StrLen (RequestElement);\r
+  StringSize = (*ConfigRequest != NULL) ? StrSize (*ConfigRequest) : sizeof (CHAR16);\r
+  MaxLen = StringSize / sizeof (CHAR16) + *SpareStrLen;\r
 \r
   //\r
   // Append <RequestElement> to <ConfigRequest>\r
@@ -4493,8 +5203,8 @@ AppendConfigRequest (
     //\r
     // Old String buffer is not sufficient for RequestElement, allocate a new one\r
     //\r
-    StringSize = (*ConfigRequest != NULL) ? StrSize (*ConfigRequest) : sizeof (CHAR16);\r
-    NewStr = AllocateZeroPool (StringSize + CONFIG_REQUEST_STRING_INCREMENTAL * sizeof (CHAR16));\r
+    MaxLen = StringSize / sizeof (CHAR16) + CONFIG_REQUEST_STRING_INCREMENTAL;\r
+    NewStr = AllocateZeroPool (MaxLen * sizeof (CHAR16));\r
     ASSERT (NewStr != NULL);\r
 \r
     if (*ConfigRequest != NULL) {\r
@@ -4505,7 +5215,7 @@ AppendConfigRequest (
     *SpareStrLen   = CONFIG_REQUEST_STRING_INCREMENTAL;\r
   }\r
 \r
-  StrCat (*ConfigRequest, RequestElement);\r
+  StrCatS (*ConfigRequest, MaxLen, RequestElement);\r
   *SpareStrLen -= StrLength;\r
 }\r
 \r
@@ -4530,13 +5240,11 @@ ConfigRequestAdjust (
   CHAR16       *RequestElement;\r
   CHAR16       *NextRequestElement;\r
   CHAR16       *NextElementBakup;\r
-  UINTN        SpareBufLen;\r
   CHAR16       *SearchKey;\r
   CHAR16       *ValueKey;\r
   BOOLEAN      RetVal;\r
   CHAR16       *ConfigRequest;\r
 \r
-  SpareBufLen    = 0;\r
   RetVal         = FALSE;\r
   NextElementBakup = NULL;\r
   ValueKey         = NULL;\r
@@ -4633,158 +5341,6 @@ ConfigRequestAdjust (
   return RetVal;\r
 }\r
 \r
-/**\r
-\r
-  Base on ConfigRequest info to get default value for current formset. \r
-\r
-  ConfigRequest info include the info about which questions in current formset need to \r
-  get default value. This function only get these questions default value.\r
-  \r
-  @param  FormSet                FormSet data structure.\r
-  @param  Storage                Storage need to update value.\r
-  @param  ConfigRequest          The config request string.\r
-\r
-**/\r
-VOID\r
-GetDefaultForFormset (\r
-  IN FORM_BROWSER_FORMSET    *FormSet,\r
-  IN BROWSER_STORAGE         *Storage,\r
-  IN CHAR16                  *ConfigRequest\r
-  )\r
-{\r
-  UINT8             *BackUpBuf;\r
-  UINTN             BufferSize;\r
-  LIST_ENTRY        BackUpList;\r
-  NAME_VALUE_NODE   *Node;\r
-  LIST_ENTRY        *Link;\r
-  LIST_ENTRY        *NodeLink;\r
-  NAME_VALUE_NODE   *TmpNode;\r
-  EFI_STATUS        Status;\r
-  EFI_STRING        Progress;\r
-  EFI_STRING        Result;\r
-\r
-  BackUpBuf = NULL;\r
-  InitializeListHead(&BackUpList);\r
-\r
-  //\r
-  // Back update the edit buffer.\r
-  // \r
-  if (Storage->Type == EFI_HII_VARSTORE_BUFFER || \r
-      (Storage->Type == EFI_HII_VARSTORE_EFI_VARIABLE_BUFFER)) {\r
-    BackUpBuf = AllocateCopyPool (Storage->Size, Storage->EditBuffer);\r
-    ASSERT (BackUpBuf != NULL);\r
-  } else if (Storage->Type == EFI_HII_VARSTORE_NAME_VALUE) {\r
-    Link = GetFirstNode (&Storage->NameValueListHead);\r
-    while (!IsNull (&Storage->NameValueListHead, Link)) {\r
-      Node = NAME_VALUE_NODE_FROM_LINK (Link);\r
-      Link = GetNextNode (&Storage->NameValueListHead, Link);\r
-\r
-      //\r
-      // Only back Node belong to this formset.\r
-      //\r
-      if (StrStr (Storage->ConfigRequest, Node->Name) == NULL) {\r
-        continue;\r
-      }\r
-\r
-      TmpNode = AllocateCopyPool (sizeof (NAME_VALUE_NODE), Node);\r
-      ASSERT (TmpNode != NULL);\r
-      TmpNode->Name = AllocateCopyPool (StrSize(Node->Name) * sizeof (CHAR16), Node->Name);\r
-      ASSERT (TmpNode->Name != NULL);\r
-      TmpNode->EditValue = AllocateCopyPool (StrSize(Node->EditValue) * sizeof (CHAR16), Node->EditValue);\r
-      ASSERT (TmpNode->EditValue != NULL);\r
-\r
-      InsertTailList(&BackUpList, &TmpNode->Link);\r
-    }\r
-  }\r
-\r
-  //\r
-  // Get default value.\r
-  //\r
-  ExtractDefault (FormSet, NULL, EFI_HII_DEFAULT_CLASS_STANDARD, FormSetLevel, GetDefaultForStorage, Storage, TRUE);\r
-\r
-  //\r
-  // Update the question value based on the input ConfigRequest.\r
-  //\r
-  if (Storage->Type == EFI_HII_VARSTORE_BUFFER || \r
-      (Storage->Type == EFI_HII_VARSTORE_EFI_VARIABLE_BUFFER)) {\r
-    ASSERT (BackUpBuf != NULL);\r
-    BufferSize = Storage->Size;\r
-    Status = mHiiConfigRouting->BlockToConfig(\r
-                                  mHiiConfigRouting,\r
-                                  ConfigRequest,\r
-                                  Storage->EditBuffer,\r
-                                  BufferSize,\r
-                                  &Result,\r
-                                  &Progress\r
-                                  );\r
-    ASSERT_EFI_ERROR (Status);\r
-    \r
-    Status = mHiiConfigRouting->ConfigToBlock (\r
-                                  mHiiConfigRouting,\r
-                                  Result,\r
-                                  BackUpBuf,\r
-                                  &BufferSize,\r
-                                  &Progress\r
-                                  );\r
-    ASSERT_EFI_ERROR (Status);\r
-\r
-    if (Result != NULL) {\r
-      FreePool (Result);\r
-    }\r
-    \r
-    CopyMem (Storage->EditBuffer, BackUpBuf, Storage->Size);\r
-    FreePool (BackUpBuf);\r
-  } else if (Storage->Type == EFI_HII_VARSTORE_NAME_VALUE) {\r
-    //\r
-    // Update question value, only element in ConfigReqeust will be update.\r
-    //\r
-    Link = GetFirstNode (&BackUpList);\r
-    while (!IsNull (&BackUpList, Link)) {\r
-      Node = NAME_VALUE_NODE_FROM_LINK (Link);\r
-      Link = GetNextNode (&BackUpList, Link);\r
-\r
-      if (StrStr (ConfigRequest, Node->Name) != NULL) {\r
-        continue;\r
-      }\r
-\r
-      NodeLink = GetFirstNode (&Storage->NameValueListHead);\r
-      while (!IsNull (&Storage->NameValueListHead, NodeLink)) {\r
-        TmpNode  = NAME_VALUE_NODE_FROM_LINK (NodeLink);\r
-        NodeLink = GetNextNode (&Storage->NameValueListHead, NodeLink);\r
-      \r
-        if (StrCmp (Node->Name, TmpNode->Name) != 0) {\r
-          continue;\r
-        }\r
-\r
-        FreePool (TmpNode->EditValue);\r
-        TmpNode->EditValue = AllocateCopyPool (StrSize(Node->EditValue) * sizeof (CHAR16), Node->EditValue);\r
-\r
-        RemoveEntryList (&Node->Link);\r
-        FreePool (Node->EditValue);\r
-        FreePool (Node->Name);\r
-        FreePool (Node);\r
-      }\r
-    }\r
-\r
-    //\r
-    // Restore the Name/Value node.\r
-    //  \r
-    Link = GetFirstNode (&BackUpList);\r
-    while (!IsNull (&BackUpList, Link)) {\r
-      Node = NAME_VALUE_NODE_FROM_LINK (Link);\r
-      Link = GetNextNode (&BackUpList, Link);\r
\r
-      //\r
-      // Free this node.\r
-      //\r
-      RemoveEntryList (&Node->Link);\r
-      FreePool (Node->EditValue);\r
-      FreePool (Node->Name);\r
-      FreePool (Node);\r
-    }\r
-  }\r
-}\r
-\r
 /**\r
   Fill storage's edit copy with settings requested from Configuration Driver.\r
 \r
@@ -4848,14 +5404,14 @@ LoadStorage (
     // Allocate and fill a buffer large enough to hold the <ConfigHdr> template\r
     // followed by "&OFFSET=0&WIDTH=WWWW"followed by a Null-terminator\r
     //\r
-    StrLen = StrSize (Storage->BrowserStorage->ConfigHdr) + 20 * sizeof (CHAR16);\r
+    StrLen = StrSize (Storage->ConfigHdr) + 20 * sizeof (CHAR16);\r
     ConfigRequest = AllocateZeroPool (StrLen);\r
     ASSERT (ConfigRequest != NULL);\r
     UnicodeSPrint (\r
                ConfigRequest, \r
                StrLen, \r
                L"%s&OFFSET=0&WIDTH=%04x", \r
-               Storage->BrowserStorage->ConfigHdr,\r
+               Storage->ConfigHdr,\r
                Storage->BrowserStorage->Size);\r
   } else {\r
     ConfigRequest = Storage->ConfigRequest;\r
@@ -4875,7 +5431,7 @@ LoadStorage (
   // If get value fail, extract default from IFR binary\r
   //\r
   if (EFI_ERROR (Status)) {\r
-    ExtractDefault (FormSet, NULL, EFI_HII_DEFAULT_CLASS_STANDARD, FormSetLevel, GetDefaultForStorage, Storage->BrowserStorage, TRUE);\r
+    ExtractDefault (FormSet, NULL, EFI_HII_DEFAULT_CLASS_STANDARD, FormSetLevel, GetDefaultForStorage, Storage->BrowserStorage, TRUE, TRUE);\r
   } else {\r
     //\r
     // Convert Result from <ConfigAltResp> to <ConfigResp>\r
@@ -5021,7 +5577,7 @@ InitializeCurrentSetting (
   //\r
   // Extract default from IFR binary for no storage questions.\r
   //  \r
-  ExtractDefault (FormSet, NULL, EFI_HII_DEFAULT_CLASS_STANDARD, FormSetLevel, GetDefaultForNoStorage, NULL, TRUE);\r
+  ExtractDefault (FormSet, NULL, EFI_HII_DEFAULT_CLASS_STANDARD, FormSetLevel, GetDefaultForNoStorage, NULL, TRUE, FALSE);\r
 \r
   //\r
   // Request current settings from Configuration Driver\r
@@ -5133,7 +5689,7 @@ GetIfrBinaryData (
           //\r
           // Try to compare against formset GUID\r
           //\r
-          if (CompareGuid (FormSetGuid, &gZeroGuid) || \r
+          if (IsZeroGuid (FormSetGuid) ||\r
               CompareGuid (ComparingGuid, (EFI_GUID *)(OpCodeData + sizeof (EFI_IFR_OP_HEADER)))) {\r
             break;\r
           }\r
@@ -5282,8 +5838,9 @@ SaveBrowserContext (
   VOID\r
   )\r
 {\r
-  BROWSER_CONTEXT  *Context;\r
-  FORM_ENTRY_INFO     *MenuList;\r
+  BROWSER_CONTEXT      *Context;\r
+  FORM_ENTRY_INFO      *MenuList;\r
+  FORM_BROWSER_FORMSET *FormSet;\r
 \r
   gBrowserContextCount++;\r
   if (gBrowserContextCount == 1) {\r
@@ -5303,10 +5860,16 @@ SaveBrowserContext (
   //\r
   Context->Selection            = gCurrentSelection;\r
   Context->ResetRequired        = gResetRequired;\r
+  Context->FlagReconnect        = gFlagReconnect;\r
+  Context->CallbackReconnect    = gCallbackReconnect;\r
   Context->ExitRequired         = gExitRequired;\r
   Context->HiiHandle            = mCurrentHiiHandle;\r
   Context->FormId               = mCurrentFormId;\r
   CopyGuid (&Context->FormSetGuid, &mCurrentFormSetGuid);\r
+  Context->SystemLevelFormSet   = mSystemLevelFormSet;\r
+  Context->CurFakeQestId        = mCurFakeQestId;\r
+  Context->HiiPackageListUpdated = mHiiPackageListUpdated;\r
+  Context->FinishRetrieveCall   = mFinishRetrieveCall;\r
 \r
   //\r
   // Save the menu history data.\r
@@ -5319,6 +5882,17 @@ SaveBrowserContext (
     InsertTailList(&Context->FormHistoryList, &MenuList->Link);\r
   }\r
 \r
+  //\r
+  // Save formset list.\r
+  //\r
+  InitializeListHead(&Context->FormSetList);\r
+  while (!IsListEmpty (&gBrowserFormSetList)) {\r
+    FormSet = FORM_BROWSER_FORMSET_FROM_LINK (gBrowserFormSetList.ForwardLink);\r
+    RemoveEntryList (&FormSet->Link);\r
+\r
+    InsertTailList(&Context->FormSetList, &FormSet->Link);\r
+  }\r
+\r
   //\r
   // Insert to FormBrowser context list\r
   //\r
@@ -5337,7 +5911,8 @@ RestoreBrowserContext (
 {\r
   LIST_ENTRY       *Link;\r
   BROWSER_CONTEXT  *Context;\r
-  FORM_ENTRY_INFO     *MenuList;\r
+  FORM_ENTRY_INFO      *MenuList;\r
+  FORM_BROWSER_FORMSET *FormSet;\r
 \r
   ASSERT (gBrowserContextCount != 0);\r
   gBrowserContextCount--;\r
@@ -5358,10 +5933,16 @@ RestoreBrowserContext (
   //\r
   gCurrentSelection     = Context->Selection;\r
   gResetRequired        = Context->ResetRequired;\r
+  gFlagReconnect        = Context->FlagReconnect;\r
+  gCallbackReconnect    = Context->CallbackReconnect;\r
   gExitRequired         = Context->ExitRequired;\r
   mCurrentHiiHandle     = Context->HiiHandle;\r
   mCurrentFormId        = Context->FormId;\r
   CopyGuid (&mCurrentFormSetGuid, &Context->FormSetGuid);\r
+  mSystemLevelFormSet   = Context->SystemLevelFormSet;\r
+  mCurFakeQestId        = Context->CurFakeQestId;\r
+  mHiiPackageListUpdated = Context->HiiPackageListUpdated;\r
+  mFinishRetrieveCall   = Context->FinishRetrieveCall;\r
 \r
   //\r
   // Restore the menu history data.\r
@@ -5373,6 +5954,16 @@ RestoreBrowserContext (
     InsertTailList(&mPrivateData.FormBrowserEx2.FormViewHistoryHead, &MenuList->Link);\r
   }\r
 \r
+  //\r
+  // Restore the Formset data.\r
+  //\r
+  while (!IsListEmpty (&Context->FormSetList)) {\r
+    FormSet = FORM_BROWSER_FORMSET_FROM_LINK (Context->FormSetList.ForwardLink);\r
+    RemoveEntryList (&FormSet->Link);\r
+\r
+    InsertTailList(&gBrowserFormSetList, &FormSet->Link);\r
+  }\r
+\r
   //\r
   // Remove from FormBrowser context list\r
   //\r
@@ -5627,6 +6218,7 @@ SetScope (
   @retval EFI_INVALID_PARAMETER  KeyData is NULL or HelpString is NULL on register.\r
   @retval EFI_NOT_FOUND          KeyData is not found to be unregistered.\r
   @retval EFI_UNSUPPORTED        Key represents a printable character. It is conflicted with Browser.\r
+  @retval EFI_ALREADY_STARTED    Key already been registered for one hot key.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -5672,20 +6264,19 @@ RegisterHotKey (
       return EFI_NOT_FOUND;\r
     }\r
   }\r
-  \r
+\r
+  if (HotKey != NULL) {\r
+    return EFI_ALREADY_STARTED;\r
+  }\r
+\r
   //\r
-  // Register HotKey into List.\r
+  // Create new Key, and add it into List.\r
   //\r
-  if (HotKey == NULL) {\r
-    //\r
-    // Create new Key, and add it into List.\r
-    //\r
-    HotKey = AllocateZeroPool (sizeof (BROWSER_HOT_KEY));\r
-    ASSERT (HotKey != NULL);\r
-    HotKey->Signature = BROWSER_HOT_KEY_SIGNATURE;\r
-    HotKey->KeyData   = AllocateCopyPool (sizeof (EFI_INPUT_KEY), KeyData);\r
-    InsertTailList (&gBrowserHotKeyList, &HotKey->Link);\r
-  }\r
+  HotKey = AllocateZeroPool (sizeof (BROWSER_HOT_KEY));\r
+  ASSERT (HotKey != NULL);\r
+  HotKey->Signature = BROWSER_HOT_KEY_SIGNATURE;\r
+  HotKey->KeyData   = AllocateCopyPool (sizeof (EFI_INPUT_KEY), KeyData);\r
+  InsertTailList (&gBrowserHotKeyList, &HotKey->Link);\r
 \r
   //\r
   // Fill HotKey information.\r
@@ -5814,7 +6405,7 @@ ExecuteAction (
   // Executet the difault action.\r
   //\r
   if ((Action & BROWSER_ACTION_DEFAULT) != 0) {\r
-    Status = ExtractDefault (FormSet, Form, DefaultId, gBrowserSettingScope, GetDefaultForAll, NULL, FALSE);\r
+    Status = ExtractDefault (FormSet, Form, DefaultId, gBrowserSettingScope, GetDefaultForAll, NULL, FALSE, FALSE);\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r