]> 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 5887e0f3be79a6856fcb74213195e24486598828..3f368b54ec7d6b67d1fe546aaa575d198fc5edde 100644 (file)
@@ -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
@@ -1429,40 +1429,44 @@ BufferToValue (
   *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
-    Length    = Question->StorageWidth + sizeof (CHAR16);\r
-    if (Length < ((LengthStr / 4 + 1) * 2)) {\r
-      Status = EFI_BUFFER_TOO_SMALL;\r
-    } else {\r
-      DstBuf = (CHAR16 *) Dst;\r
-      ZeroMem (TemStr, sizeof (TemStr));\r
-      for (Index = 0; Index < LengthStr; 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
+    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
-    if (Question->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
+    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
@@ -2847,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
@@ -3080,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
@@ -3123,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
@@ -3145,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
@@ -3172,7 +3297,9 @@ SubmitForForm (
   //\r
   // 6 Call callback with Submitted type to inform the driver.\r
   //\r
-  SubmitCallback (FormSet, Form);\r
+  if (!SubmitFormFail) {\r
+    SubmitCallback (FormSet, Form);\r
+  }\r
 \r
   return Status;\r
 }\r
@@ -3208,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
@@ -3269,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
@@ -3310,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
@@ -3351,7 +3495,9 @@ SubmitForFormSet (
   //\r
   // 6. Call callback with Submitted type to inform the driver.\r
   //\r
-  SubmitCallback (FormSet, NULL);\r
+  if (!SubmitFormSetFail) {\r
+    SubmitCallback (FormSet, NULL);\r
+  }\r
 \r
   return Status;\r
 }\r
@@ -3445,8 +3591,18 @@ SubmitForSystem (
         while (!IsNull (&LocalFormSet->SaveFailStorageListHead, StorageLink)) {\r
           FormSetStorage = FORMSET_STORAGE_FROM_SAVE_FAIL_LINK (StorageLink);\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
@@ -3604,6 +3760,11 @@ GetOffsetFromConfigResp (
   // Type is EFI_HII_VARSTORE_EFI_VARIABLE or EFI_HII_VARSTORE_EFI_VARIABLE_BUFFER\r
   //\r
 \r
+  //\r
+  // Convert all hex digits in ConfigResp to lower case before searching.\r
+  //\r
+  HiiToLower (ConfigResp);\r
+\r
   //\r
   // 1. Directly use Question->BlockName to find.\r
   //\r
@@ -3898,9 +4059,14 @@ GetQuestionDefault (
   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
@@ -3917,6 +4083,7 @@ 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
@@ -3948,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
@@ -4018,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
@@ -4072,8 +4241,6 @@ GetQuestionDefault (
           ((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
       return EFI_SUCCESS;\r
@@ -4081,10 +4248,30 @@ GetQuestionDefault (
   }\r
 \r
   //\r
-  // For Questions without default\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
@@ -5502,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