]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/DriverSampleDxe/DriverSample.c
Fixed SCT test failed caused by driver sample.
[mirror_edk2.git] / MdeModulePkg / Universal / DriverSampleDxe / DriverSample.c
index 52d9b837bd425a9b3cf2e5aa8c0170274147db13..83e669b9da5da5a114129a38e192d2eb56090c12 100644 (file)
@@ -2,8 +2,8 @@
 This is an example of how a driver might export data to the HII protocol to be\r
 later utilized by the Setup Protocol\r
 \r
-Copyright (c) 2004 - 2009, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2004 - 2010, 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
 http://opensource.org/licenses/bsd-license.php\r
@@ -187,7 +187,7 @@ ValidatePassword (
   ASSERT (EncodedPassword != NULL);\r
   StrnCpy (EncodedPassword, Password, StrLen (Password));\r
   EncodePassword (EncodedPassword, StrLen (EncodedPassword) * sizeof (CHAR16));\r
-  if (CompareMem (EncodedPassword, PrivateData->Configuration.WhatIsThePassword2, StrLen (EncodedPassword) * sizeof (CHAR16)) != 0) {\r
+  if (CompareMem (EncodedPassword, PrivateData->Configuration.WhatIsThePassword2, PasswordMaxSize) != 0) {\r
     //\r
     // Old password mismatch, return EFI_NOT_READY to prompt for error message\r
     //\r
@@ -334,6 +334,280 @@ LoadNameValueNames (
   return EFI_SUCCESS;\r
 }\r
 \r
+\r
+/**\r
+  Get the value of <Number> in <BlockConfig> format, i.e. the value of OFFSET\r
+  or WIDTH or VALUE.\r
+  <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE'=<Number>\r
+\r
+  This is a internal function.\r
+\r
+  @param  StringPtr              String in <BlockConfig> format and points to the\r
+                                 first character of <Number>.\r
+  @param  Number                 The output value. Caller takes the responsibility\r
+                                 to free memory.\r
+  @param  Len                    Length of the <Number>, in characters.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES   Insufficient resources to store neccessary\r
+                                 structures.\r
+  @retval EFI_SUCCESS            Value of <Number> is outputted in Number\r
+                                 successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+GetValueOfNumber (\r
+  IN EFI_STRING                    StringPtr,\r
+  OUT UINT8                        **Number,\r
+  OUT UINTN                        *Len\r
+  )\r
+{\r
+  EFI_STRING               TmpPtr;\r
+  UINTN                    Length;\r
+  EFI_STRING               Str;\r
+  UINT8                    *Buf;\r
+  EFI_STATUS               Status;\r
+  UINT8                    DigitUint8;\r
+  UINTN                    Index;\r
+  CHAR16                   TemStr[2];\r
+\r
+  if (StringPtr == NULL || *StringPtr == L'\0' || Number == NULL || Len == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Buf = NULL;\r
+\r
+  TmpPtr = StringPtr;\r
+  while (*StringPtr != L'\0' && *StringPtr != L'&') {\r
+    StringPtr++;\r
+  }\r
+  *Len   = StringPtr - TmpPtr;\r
+  Length = *Len + 1;\r
+\r
+  Str = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));\r
+  if (Str == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Exit;\r
+  }\r
+  CopyMem (Str, TmpPtr, *Len * sizeof (CHAR16));\r
+  *(Str + *Len) = L'\0';\r
+\r
+  Length = (Length + 1) / 2;\r
+  Buf = (UINT8 *) AllocateZeroPool (Length);\r
+  if (Buf == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Exit;\r
+  }\r
+  \r
+  Length = *Len;\r
+  ZeroMem (TemStr, sizeof (TemStr));\r
+  for (Index = 0; Index < Length; Index ++) {\r
+    TemStr[0] = Str[Length - Index - 1];\r
+    DigitUint8 = (UINT8) StrHexToUint64 (TemStr);\r
+    if ((Index & 1) == 0) {\r
+      Buf [Index/2] = DigitUint8;\r
+    } else {\r
+      Buf [Index/2] = (UINT8) ((DigitUint8 << 4) + Buf [Index/2]);\r
+    }\r
+  }\r
+\r
+  *Number = Buf;\r
+  Status  = EFI_SUCCESS;\r
+\r
+Exit:\r
+  if (Str != NULL) {\r
+    FreePool (Str);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Create altcfg string. \r
+\r
+  @param  Result               The request result string.\r
+  @param  ConfigHdr            The request head info. <ConfigHdr> format.\r
+  @param  Offset               The offset of the parameter int he structure.\r
+  @param  Width                The width of the parameter.\r
+\r
+\r
+  @retval  The string with altcfg info append at the end.\r
+**/\r
+EFI_STRING \r
+CreateAltCfgString (\r
+  IN     EFI_STRING     Result,\r
+  IN     EFI_STRING     ConfigHdr,\r
+  IN     UINTN          Offset,\r
+  IN     UINTN          Width\r
+  )\r
+{\r
+  EFI_STRING StringPtr;\r
+  EFI_STRING TmpStr;\r
+  UINTN      NewLen;\r
+\r
+  //\r
+  // String Len = ConfigResp + AltConfig + AltConfig + 1("\0")\r
+  //\r
+  NewLen = (StrLen (Result) + ((1 + StrLen (ConfigHdr) + 8 + 4) + (8 + 4 + 7 + 4 + 7 + 4)) * 2 + 1) * sizeof (CHAR16);\r
+  StringPtr = AllocateZeroPool (NewLen);\r
+  if (StringPtr == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  TmpStr = StringPtr;\r
+  if (Result != NULL) {\r
+    StrCpy (StringPtr, Result);\r
+    StringPtr += StrLen (Result);  \r
+    FreePool (Result);\r
+  }\r
+  \r
+  UnicodeSPrint (\r
+  StringPtr, \r
+  (1 + StrLen (ConfigHdr) + 8 + 4 + 1) * sizeof (CHAR16), \r
+  L"&%s&ALTCFG=%04x", \r
+  ConfigHdr, \r
+  EFI_HII_DEFAULT_CLASS_STANDARD\r
+  );\r
+  StringPtr += StrLen (StringPtr);\r
+\r
+  UnicodeSPrint (\r
+    StringPtr, \r
+    (8 + 4 + 7 + 4 + 7 + 4 + 1) * sizeof (CHAR16),\r
+    L"&OFFSET=%04x&WIDTH=%04x&VALUE=%04x", \r
+    Offset, \r
+    Width,\r
+    DEFAULT_CLASS_STANDARD_VALUE\r
+    );\r
+  StringPtr += StrLen (StringPtr);  \r
+\r
+  UnicodeSPrint (\r
+  StringPtr, \r
+  (1 + StrLen (ConfigHdr) + 8 + 4 + 1) * sizeof (CHAR16), \r
+  L"&%s&ALTCFG=%04x", \r
+  ConfigHdr, \r
+  EFI_HII_DEFAULT_CLASS_MANUFACTURING\r
+  );\r
+  StringPtr += StrLen (StringPtr);\r
+\r
+  UnicodeSPrint (\r
+    StringPtr, \r
+    (8 + 4 + 7 + 4 + 7 + 4 + 1) * sizeof (CHAR16),\r
+    L"&OFFSET=%04x&WIDTH=%04x&VALUE=%04x", \r
+    Offset, \r
+    Width,\r
+    DEFAULT_CLASS_MANUFACTURING_VALUE\r
+    );\r
+  StringPtr += StrLen (StringPtr); \r
+\r
+  return TmpStr;\r
+}\r
+\r
+/**\r
+  Check whether need to add the altcfg string. if need to add, add the altcfg \r
+  string.\r
+\r
+  @param  RequestResult              The request result string.\r
+  @param  ConfigRequestHdr           The request head info. <ConfigHdr> format.\r
+\r
+**/\r
+VOID \r
+AppendAltCfgString (\r
+  IN OUT EFI_STRING                       *RequestResult,\r
+  IN     EFI_STRING                       ConfigRequestHdr\r
+  )\r
+{\r
+  EFI_STRING                          StringPtr;\r
+  EFI_STRING                          TmpPtr;\r
+  UINTN                               Length;\r
+  UINT8                               *TmpBuffer;\r
+  UINTN                               Offset;\r
+  UINTN                               Width;\r
+  UINTN                               BlockSize;\r
+  UINTN                               ValueOffset;\r
+  UINTN                               ValueWidth;\r
+  EFI_STATUS                          Status;\r
+\r
+  StringPtr = *RequestResult;\r
+  StringPtr = StrStr (StringPtr, L"OFFSET");\r
+  BlockSize = sizeof (DRIVER_SAMPLE_CONFIGURATION);\r
+  ValueOffset = OFFSET_OF (DRIVER_SAMPLE_CONFIGURATION, GetDefaultValueFromAccess);\r
+  ValueWidth  = sizeof (((DRIVER_SAMPLE_CONFIGURATION *)0)->GetDefaultValueFromAccess);\r
+\r
+  if (StringPtr == NULL) {\r
+    return;\r
+  }\r
+\r
+  while (*StringPtr != 0 && StrnCmp (StringPtr, L"OFFSET=", StrLen (L"OFFSET=")) == 0) {\r
+    //\r
+    // Back up the header of one <BlockName>\r
+    //\r
+    TmpPtr = StringPtr;\r
+\r
+    StringPtr += StrLen (L"OFFSET=");\r
+    //\r
+    // Get Offset\r
+    //\r
+    Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);\r
+    if (EFI_ERROR (Status)) {\r
+      return;\r
+    }\r
+    Offset = 0;\r
+    CopyMem (\r
+     &Offset,\r
+     TmpBuffer,\r
+     (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)\r
+     );\r
+    FreePool (TmpBuffer);\r
+\r
+    StringPtr += Length;\r
+    if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {\r
+      return;\r
+    }\r
+    StringPtr += StrLen (L"&WIDTH=");\r
+\r
+    //\r
+    // Get Width\r
+    //\r
+    Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);\r
+    if (EFI_ERROR (Status)) {\r
+      return;\r
+    }\r
+    Width = 0;\r
+    CopyMem (\r
+     &Width,\r
+     TmpBuffer,\r
+     (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)\r
+     );\r
+    FreePool (TmpBuffer);\r
+\r
+    StringPtr += Length;\r
+    if (StrnCmp (StringPtr, L"&VALUE=", StrLen (L"&VALUE=")) != 0) {\r
+      return;\r
+    }\r
+    StringPtr += StrLen (L"&VALUE=");\r
+\r
+    //\r
+    // Get Width\r
+    //\r
+    Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);\r
+    if (EFI_ERROR (Status)) {\r
+      return;\r
+    }\r
+    StringPtr += Length;\r
+\r
+    //\r
+    // Calculate Value and convert it to hex string.\r
+    //\r
+    if (Offset + Width > BlockSize) {\r
+      return;\r
+    }\r
+\r
+    if (Offset <= ValueOffset && Offset + Width >= ValueOffset + ValueWidth) {\r
+      *RequestResult = CreateAltCfgString(*RequestResult, ConfigRequestHdr, ValueOffset, ValueWidth);\r
+      return;\r
+    }\r
+  }\r
+}\r
+\r
 /**\r
   This function allows a caller to extract the current configuration for one\r
   or more named elements from the target driver.\r
@@ -355,7 +629,7 @@ LoadNameValueNames (
 \r
   @retval EFI_SUCCESS            The Results is filled with the requested values.\r
   @retval EFI_OUT_OF_RESOURCES   Not enough memory to store the results.\r
-  @retval EFI_INVALID_PARAMETER  Request is NULL, illegal syntax, or unknown name.\r
+  @retval EFI_INVALID_PARAMETER  Request is illegal syntax, or unknown name.\r
   @retval EFI_NOT_FOUND          Routing data doesn't match any storage in this\r
                                  driver.\r
 \r
@@ -380,9 +654,9 @@ ExtractConfig (
   UINTN                            ValueStrLen;\r
   CHAR16                           BackupChar;\r
   CHAR16                           *StrPointer;\r
+  BOOLEAN                          AllocatedRequest;\r
 \r
-\r
-  if (Progress == NULL || Results == NULL || Request == NULL) {\r
+  if (Progress == NULL || Results == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   //\r
@@ -392,6 +666,7 @@ ExtractConfig (
   ConfigRequest     = NULL;\r
   Size              = 0;\r
   *Progress         = Request;\r
+  AllocatedRequest  = FALSE;\r
 \r
   PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);\r
   HiiConfigRouting = PrivateData->HiiConfigRouting;\r
@@ -422,10 +697,13 @@ ExtractConfig (
     // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator\r
     //\r
     ConfigRequestHdr = HiiConstructConfigHdr (&mFormSetGuid, VariableName, PrivateData->DriverHandle[0]);\r
-    Size = (StrLen (ConfigRequest) + 32 + 1) * sizeof (CHAR16);\r
+    Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16);\r
     ConfigRequest = AllocateZeroPool (Size);\r
+    ASSERT (ConfigRequest != NULL);\r
+    AllocatedRequest = TRUE;\r
     UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize);\r
     FreePool (ConfigRequestHdr);\r
+    ConfigRequestHdr = NULL;\r
   } else {\r
     //\r
     // Check routing data in <ConfigHdr>.\r
@@ -434,110 +712,149 @@ ExtractConfig (
     if (!HiiIsConfigHdrMatch (Request, &mFormSetGuid, NULL)) {\r
       return EFI_NOT_FOUND;\r
     }\r
+    //\r
+    // Set Request to the unified request string.\r
+    //\r
     ConfigRequest = Request;\r
-\r
     //\r
-    // Check if requesting Name/Value storage\r
+    // Check whether Request includes Request Element.\r
     //\r
     if (StrStr (Request, L"OFFSET") == NULL) {\r
       //\r
-      // Update Name/Value storage Names\r
+      // Check Request Element does exist in Reques String\r
       //\r
-      Status = LoadNameValueNames (PrivateData);\r
-      if (EFI_ERROR (Status)) {\r
-        return Status;\r
+      StrPointer = StrStr (Request, L"PATH");\r
+      if (StrPointer == NULL) {\r
+        return EFI_INVALID_PARAMETER;\r
       }\r
+      if (StrStr (StrPointer, L"&") == NULL) {\r
+        Size = (StrLen (Request) + 32 + 1) * sizeof (CHAR16);\r
+        ConfigRequest    = AllocateZeroPool (Size);\r
+        ASSERT (ConfigRequest != NULL);\r
+        AllocatedRequest = TRUE;\r
+        UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", Request, (UINT64)BufferSize);\r
+      }\r
+    }\r
+  }\r
 \r
-      //\r
-      // Allocate memory for <ConfigResp>, e.g. Name0=0x11, Name1=0x1234, Name2="ABCD"\r
-      // <Request>   ::=<ConfigHdr>&Name0&Name1&Name2\r
-      // <ConfigResp>::=<ConfigHdr>&Name0=11&Name1=1234&Name2=0041004200430044\r
-      //\r
-      BufferSize = (StrLen (Request) +\r
-        1 + sizeof (PrivateData->Configuration.NameValueVar0) * 2 +\r
-        1 + sizeof (PrivateData->Configuration.NameValueVar1) * 2 +\r
-        1 + sizeof (PrivateData->Configuration.NameValueVar2) * 2 + 1) * sizeof (CHAR16);\r
-      *Results = AllocateZeroPool (BufferSize);\r
-      ASSERT (*Results != NULL);\r
-      StrCpy (*Results, Request);\r
-      Value = *Results;\r
+  //\r
+  // Check if requesting Name/Value storage\r
+  //\r
+  if (StrStr (ConfigRequest, L"OFFSET") == NULL) {\r
+    //\r
+    // Update Name/Value storage Names\r
+    //\r
+    Status = LoadNameValueNames (PrivateData);\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
 \r
-      //\r
-      // Append value of NameValueVar0, type is UINT8\r
-      //\r
-      if ((Value = StrStr (*Results, PrivateData->NameValueName[0])) != NULL) {\r
-        Value += StrLen (PrivateData->NameValueName[0]);\r
-        ValueStrLen = ((sizeof (PrivateData->Configuration.NameValueVar0) * 2) + 1);\r
-        CopyMem (Value + ValueStrLen, Value, StrSize (Value));\r
-\r
-        BackupChar = Value[ValueStrLen];\r
-        *Value++   = L'=';\r
-        Value += UnicodeValueToString (\r
-                   Value, \r
-                   PREFIX_ZERO | RADIX_HEX, \r
-                   PrivateData->Configuration.NameValueVar0, \r
-                   sizeof (PrivateData->Configuration.NameValueVar0) * 2\r
-                   );\r
-        *Value = BackupChar;\r
-      }\r
+    //\r
+    // Allocate memory for <ConfigResp>, e.g. Name0=0x11, Name1=0x1234, Name2="ABCD"\r
+    // <Request>   ::=<ConfigHdr>&Name0&Name1&Name2\r
+    // <ConfigResp>::=<ConfigHdr>&Name0=11&Name1=1234&Name2=0041004200430044\r
+    //\r
+    BufferSize = (StrLen (ConfigRequest) +\r
+      1 + sizeof (PrivateData->Configuration.NameValueVar0) * 2 +\r
+      1 + sizeof (PrivateData->Configuration.NameValueVar1) * 2 +\r
+      1 + sizeof (PrivateData->Configuration.NameValueVar2) * 2 + 1) * sizeof (CHAR16);\r
+    *Results = AllocateZeroPool (BufferSize);\r
+    ASSERT (*Results != NULL);\r
+    StrCpy (*Results, ConfigRequest);\r
+    Value = *Results;\r
 \r
-      //\r
-      // Append value of NameValueVar1, type is UINT16\r
-      //\r
-      if ((Value = StrStr (*Results, PrivateData->NameValueName[1])) != NULL) {\r
-        Value += StrLen (PrivateData->NameValueName[1]);\r
-        ValueStrLen = ((sizeof (PrivateData->Configuration.NameValueVar1) * 2) + 1);\r
-        CopyMem (Value + ValueStrLen, Value, StrSize (Value));\r
-\r
-        BackupChar = Value[ValueStrLen];\r
-        *Value++   = L'=';\r
-        Value += UnicodeValueToString (\r
-                  Value, \r
-                  PREFIX_ZERO | RADIX_HEX, \r
-                  PrivateData->Configuration.NameValueVar1\r
-                  sizeof (PrivateData->Configuration.NameValueVar1) * 2\r
-                  );\r
-        *Value = BackupChar;\r
-      }\r
+    //\r
+    // Append value of NameValueVar0, type is UINT8\r
+    //\r
+    if ((Value = StrStr (*Results, PrivateData->NameValueName[0])) != NULL) {\r
+      Value += StrLen (PrivateData->NameValueName[0]);\r
+      ValueStrLen = ((sizeof (PrivateData->Configuration.NameValueVar0) * 2) + 1);\r
+      CopyMem (Value + ValueStrLen, Value, StrSize (Value));\r
+\r
+      BackupChar = Value[ValueStrLen];\r
+      *Value++   = L'=';\r
+      Value += UnicodeValueToString (\r
+                 Value, \r
+                 PREFIX_ZERO | RADIX_HEX, \r
+                 PrivateData->Configuration.NameValueVar0\r
+                 sizeof (PrivateData->Configuration.NameValueVar0) * 2\r
+                 );\r
+      *Value = BackupChar;\r
+    }\r
 \r
+    //\r
+    // Append value of NameValueVar1, type is UINT16\r
+    //\r
+    if ((Value = StrStr (*Results, PrivateData->NameValueName[1])) != NULL) {\r
+      Value += StrLen (PrivateData->NameValueName[1]);\r
+      ValueStrLen = ((sizeof (PrivateData->Configuration.NameValueVar1) * 2) + 1);\r
+      CopyMem (Value + ValueStrLen, Value, StrSize (Value));\r
+\r
+      BackupChar = Value[ValueStrLen];\r
+      *Value++   = L'=';\r
+      Value += UnicodeValueToString (\r
+                Value, \r
+                PREFIX_ZERO | RADIX_HEX, \r
+                PrivateData->Configuration.NameValueVar1, \r
+                sizeof (PrivateData->Configuration.NameValueVar1) * 2\r
+                );\r
+      *Value = BackupChar;\r
+    }\r
+\r
+    //\r
+    // Append value of NameValueVar2, type is CHAR16 *\r
+    //\r
+    if ((Value = StrStr (*Results, PrivateData->NameValueName[2])) != NULL) {\r
+      Value += StrLen (PrivateData->NameValueName[2]);\r
+      ValueStrLen = StrLen (PrivateData->Configuration.NameValueVar2) * 4 + 1;\r
+      CopyMem (Value + ValueStrLen, Value, StrSize (Value));\r
+\r
+      *Value++ = L'=';\r
       //\r
-      // Append value of NameValueVar2, type is CHAR16 *\r
+      // Convert Unicode String to Config String, e.g. "ABCD" => "0041004200430044"\r
       //\r
-      if ((Value = StrStr (*Results, PrivateData->NameValueName[2])) != NULL) {\r
-        Value += StrLen (PrivateData->NameValueName[2]);\r
-        ValueStrLen = StrLen (PrivateData->Configuration.NameValueVar2) * 4 + 1;\r
-        CopyMem (Value + ValueStrLen, Value, StrSize (Value));\r
-\r
-        *Value++ = L'=';\r
-        //\r
-        // Convert Unicode String to Config String, e.g. "ABCD" => "0041004200430044"\r
-        //\r
-        StrPointer = (CHAR16 *) PrivateData->Configuration.NameValueVar2;\r
-        for (; *StrPointer != L'\0'; StrPointer++) {\r
-          Value += UnicodeValueToString (Value, PREFIX_ZERO | RADIX_HEX, *StrPointer, 4);\r
-        }\r
+      StrPointer = (CHAR16 *) PrivateData->Configuration.NameValueVar2;\r
+      for (; *StrPointer != L'\0'; StrPointer++) {\r
+        Value += UnicodeValueToString (Value, PREFIX_ZERO | RADIX_HEX, *StrPointer, 4);\r
       }\r
-\r
-      Progress = (EFI_STRING *) Request + StrLen (Request);\r
-      return EFI_SUCCESS;\r
+    }\r
+    \r
+    Status = EFI_SUCCESS;\r
+  } else {\r
+    //\r
+    // Convert buffer data to <ConfigResp> by helper function BlockToConfig()\r
+    //\r
+    Status = HiiConfigRouting->BlockToConfig (\r
+                                  HiiConfigRouting,\r
+                                  ConfigRequest,\r
+                                  (UINT8 *) &PrivateData->Configuration,\r
+                                  BufferSize,\r
+                                  Results,\r
+                                  Progress\r
+                                  );\r
+    if (!EFI_ERROR (Status)) {\r
+      ConfigRequestHdr = HiiConstructConfigHdr (&mFormSetGuid, VariableName, PrivateData->DriverHandle[0]);\r
+      AppendAltCfgString(Results, ConfigRequestHdr);\r
     }\r
   }\r
 \r
   //\r
-  // Convert buffer data to <ConfigResp> by helper function BlockToConfig()\r
+  // Free the allocated config request string.\r
   //\r
-  Status = HiiConfigRouting->BlockToConfig (\r
-                                HiiConfigRouting,\r
-                                ConfigRequest,\r
-                                (UINT8 *) &PrivateData->Configuration,\r
-                                BufferSize,\r
-                                Results,\r
-                                Progress\r
-                                );\r
+  if (AllocatedRequest) {\r
+    FreePool (ConfigRequest);\r
+  }\r
 \r
+  if (ConfigRequestHdr != NULL) {\r
+    FreePool (ConfigRequestHdr);\r
+  }\r
+  //\r
+  // Set Progress string to the original request string.\r
+  //\r
   if (Request == NULL) {\r
-    FreePool (ConfigRequest);\r
     *Progress = NULL;\r
+  } else if (StrStr (Request, L"OFFSET") == NULL) {\r
+    *Progress = Request + StrLen (Request);\r
   }\r
 \r
   return Status;\r
@@ -806,335 +1123,440 @@ DriverCallback (
   EFI_INPUT_KEY                   Key;\r
   DRIVER_SAMPLE_CONFIGURATION     *Configuration;\r
   UINTN                           MyVarSize;\r
+  \r
+  if (((Value == NULL) && (Action != EFI_BROWSER_ACTION_FORM_OPEN) && (Action != EFI_BROWSER_ACTION_FORM_CLOSE))||\r
+    (ActionRequest == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
 \r
-  if (Action == EFI_BROWSER_ACTION_FORM_OPEN) {\r
-    //\r
-    // On FORM_OPEN event, update the form on-the-fly\r
-    //\r
-    PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);\r
-\r
-    //\r
-    // Initialize the container for dynamic opcodes\r
-    //\r
-    StartOpCodeHandle = HiiAllocateOpCodeHandle ();\r
-    ASSERT (StartOpCodeHandle != NULL);\r
-\r
-    //\r
-    // Create Hii Extend Label OpCode as the start opcode\r
-    //\r
-    StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
-    StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
-    StartLabel->Number       = LABEL_UPDATE2;\r
 \r
-    HiiCreateActionOpCode (\r
-      StartOpCodeHandle,                // Container for dynamic created opcodes\r
-      0x1238,                           // Question ID\r
-      STRING_TOKEN(STR_SAVE_TEXT),      // Prompt text\r
-      STRING_TOKEN(STR_SAVE_TEXT),      // Help text\r
-      EFI_IFR_FLAG_CALLBACK,            // Question flag\r
-      0                                 // Action String ID\r
-    );\r
+  Status = EFI_SUCCESS;\r
+  PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);\r
 \r
-    HiiUpdateForm (\r
-      PrivateData->HiiHandle[0],  // HII handle\r
-      &mFormSetGuid,              // Formset GUID\r
-      0x3,                        // Form ID\r
-      StartOpCodeHandle,          // Label for where to insert opcodes\r
-      NULL                        // Insert data\r
-      );\r
+  switch (Action) {\r
+  case EFI_BROWSER_ACTION_FORM_OPEN:\r
+    {\r
+      if (QuestionId == 0x1234) {\r
+        //\r
+        // Sample CallBack for UEFI FORM_OPEN action:\r
+        //   Add Save action into Form 3 when Form 1 is opened.\r
+        //   This will be done only in FORM_OPEN CallBack of question with ID 0x1234 from Form 1.\r
+        //\r
+        PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);\r
 \r
-    HiiFreeOpCodeHandle (StartOpCodeHandle);\r
-    return EFI_SUCCESS;\r
-  }\r
+        //\r
+        // Initialize the container for dynamic opcodes\r
+        //\r
+        StartOpCodeHandle = HiiAllocateOpCodeHandle ();\r
+        ASSERT (StartOpCodeHandle != NULL);\r
 \r
-  if (Action == EFI_BROWSER_ACTION_FORM_CLOSE) {\r
-    //\r
-    // On FORM_CLOSE event, show up a pop-up\r
-    //\r
-    do {\r
-      CreatePopUp (\r
-        EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
-        &Key,\r
-        L"",\r
-        L"You are going to leave the Form!",\r
-        L"Press ESC or ENTER to continue ...",\r
-        L"",\r
-        NULL\r
+        //\r
+        // Create Hii Extend Label OpCode as the start opcode\r
+        //\r
+        StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
+        StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
+        StartLabel->Number       = LABEL_UPDATE2;\r
+\r
+        HiiCreateActionOpCode (\r
+          StartOpCodeHandle,                // Container for dynamic created opcodes\r
+          0x1238,                           // Question ID\r
+          STRING_TOKEN(STR_SAVE_TEXT),      // Prompt text\r
+          STRING_TOKEN(STR_SAVE_TEXT),      // Help text\r
+          EFI_IFR_FLAG_CALLBACK,            // Question flag\r
+          0                                 // Action String ID\r
         );\r
-    } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
 \r
-    return EFI_SUCCESS;\r
-  }\r
+        HiiUpdateForm (\r
+          PrivateData->HiiHandle[0],  // HII handle\r
+          &mFormSetGuid,              // Formset GUID\r
+          0x3,                        // Form ID\r
+          StartOpCodeHandle,          // Label for where to insert opcodes\r
+          NULL                        // Insert data\r
+          );\r
 \r
-  if ((Value == NULL) || (ActionRequest == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
+        HiiFreeOpCodeHandle (StartOpCodeHandle);\r
+      }\r
+    }\r
+    break;\r
 \r
-  if ((Type == EFI_IFR_TYPE_STRING) && (Value->string == 0)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
+  case EFI_BROWSER_ACTION_FORM_CLOSE:\r
+    {\r
+      if (QuestionId == 0x5678) {\r
+        //\r
+        // Sample CallBack for UEFI FORM_CLOSE action:\r
+        //   Show up a pop-up to specify Form 3 will be closed when exit Form 3.\r
+        //\r
+        do {\r
+          CreatePopUp (\r
+            EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
+            &Key,\r
+            L"",\r
+            L"You are going to leave third Form!",\r
+            L"Press ESC or ENTER to continue ...",\r
+            L"",\r
+            NULL\r
+            );\r
+        } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
+      }\r
+    }\r
+    break;\r
+    \r
+  case EFI_BROWSER_ACTION_RETRIEVE:\r
+    {\r
+      if (QuestionId == 0x1111) {\r
+        //\r
+        // EfiVarstore question takes sample action (print value as debug information) \r
+        // after read/write question.\r
+        //\r
+        MyVarSize = 1;\r
+        Status = gRT->GetVariable(\r
+                        L"MyVar",\r
+                        &mFormSetGuid,\r
+                        NULL,\r
+                        &MyVarSize,\r
+                        &MyVar\r
+                        );\r
+        ASSERT_EFI_ERROR (Status);\r
+        DEBUG ((DEBUG_INFO, "EfiVarstore question: Tall value is %d with value width %d\n", MyVar, MyVarSize));\r
+      }\r
+    }\r
+    break;\r
 \r
-  Status = EFI_SUCCESS;\r
-  PrivateData = DRIVER_SAMPLE_PRIVATE_FROM_THIS (This);\r
+  case EFI_BROWSER_ACTION_DEFAULT_STANDARD:\r
+    {\r
+      switch (QuestionId) {\r
+      case 0x1240:\r
+        Value->u8 = DEFAULT_CLASS_STANDARD_VALUE;\r
+      break;\r
 \r
-  switch (QuestionId) {\r
-  case 0x1234:\r
-    //\r
-    // Initialize the container for dynamic opcodes\r
-    //\r
-    StartOpCodeHandle = HiiAllocateOpCodeHandle ();\r
-    ASSERT (StartOpCodeHandle != NULL);\r
+      default:\r
+        Status = EFI_UNSUPPORTED;\r
+      break;\r
+      }\r
+    }\r
+    break;\r
 \r
-    EndOpCodeHandle = HiiAllocateOpCodeHandle ();\r
-    ASSERT (EndOpCodeHandle != NULL);\r
+  case EFI_BROWSER_ACTION_DEFAULT_MANUFACTURING:\r
+    {\r
+      switch (QuestionId) {\r
+      case 0x1240:\r
+        Value->u8 = DEFAULT_CLASS_MANUFACTURING_VALUE;\r
+      break;\r
 \r
-    //\r
-    // Create Hii Extend Label OpCode as the start opcode\r
-    //\r
-    StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
-    StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
-    StartLabel->Number       = LABEL_UPDATE1;\r
+      default:\r
+        Status = EFI_UNSUPPORTED;      \r
+      break;\r
+      }\r
+    }\r
+    break;\r
 \r
-    //\r
-    // Create Hii Extend Label OpCode as the end opcode\r
-    //\r
-    EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
-    EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
-    EndLabel->Number       = LABEL_END;\r
+  case EFI_BROWSER_ACTION_CHANGING:\r
+  {\r
+    switch (QuestionId) {\r
+    case 0x1234:\r
+      //\r
+      // Initialize the container for dynamic opcodes\r
+      //\r
+      StartOpCodeHandle = HiiAllocateOpCodeHandle ();\r
+      ASSERT (StartOpCodeHandle != NULL);\r
 \r
-    HiiCreateActionOpCode (\r
-      StartOpCodeHandle,                // Container for dynamic created opcodes\r
-      0x1237,                           // Question ID\r
-      STRING_TOKEN(STR_EXIT_TEXT),      // Prompt text\r
-      STRING_TOKEN(STR_EXIT_TEXT),      // Help text\r
-      EFI_IFR_FLAG_CALLBACK,            // Question flag\r
-      0                                 // Action String ID\r
-    );\r
+      EndOpCodeHandle = HiiAllocateOpCodeHandle ();\r
+      ASSERT (EndOpCodeHandle != NULL);\r
 \r
-    //\r
-    // Create Option OpCode\r
-    //\r
-    OptionsOpCodeHandle = HiiAllocateOpCodeHandle ();\r
-    ASSERT (OptionsOpCodeHandle != NULL);\r
+      //\r
+      // Create Hii Extend Label OpCode as the start opcode\r
+      //\r
+      StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
+      StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
+      StartLabel->Number       = LABEL_UPDATE1;\r
 \r
-    HiiCreateOneOfOptionOpCode (\r
-      OptionsOpCodeHandle,\r
-      STRING_TOKEN (STR_BOOT_OPTION1),\r
-      0,\r
-      EFI_IFR_NUMERIC_SIZE_1,\r
-      1\r
+      //\r
+      // Create Hii Extend Label OpCode as the end opcode\r
+      //\r
+      EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
+      EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
+      EndLabel->Number       = LABEL_END;\r
+\r
+      HiiCreateActionOpCode (\r
+        StartOpCodeHandle,                // Container for dynamic created opcodes\r
+        0x1237,                           // Question ID\r
+        STRING_TOKEN(STR_EXIT_TEXT),      // Prompt text\r
+        STRING_TOKEN(STR_EXIT_TEXT),      // Help text\r
+        EFI_IFR_FLAG_CALLBACK,            // Question flag\r
+        0                                 // Action String ID\r
       );\r
 \r
-    HiiCreateOneOfOptionOpCode (\r
-      OptionsOpCodeHandle,\r
-      STRING_TOKEN (STR_BOOT_OPTION2),\r
-      0,\r
-      EFI_IFR_NUMERIC_SIZE_1,\r
-      2\r
-      );\r
+      //\r
+      // Create Option OpCode\r
+      //\r
+      OptionsOpCodeHandle = HiiAllocateOpCodeHandle ();\r
+      ASSERT (OptionsOpCodeHandle != NULL);\r
+\r
+      HiiCreateOneOfOptionOpCode (\r
+        OptionsOpCodeHandle,\r
+        STRING_TOKEN (STR_BOOT_OPTION1),\r
+        0,\r
+        EFI_IFR_NUMERIC_SIZE_1,\r
+        1\r
+        );\r
 \r
-    //\r
-    // Prepare initial value for the dynamic created oneof Question\r
-    //\r
-    PrivateData->Configuration.DynamicOneof = 2;\r
-    Status = gRT->SetVariable(\r
-                    VariableName,\r
-                    &mFormSetGuid,\r
-                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-                    sizeof (DRIVER_SAMPLE_CONFIGURATION),\r
-                    &PrivateData->Configuration\r
-                    );\r
+      HiiCreateOneOfOptionOpCode (\r
+        OptionsOpCodeHandle,\r
+        STRING_TOKEN (STR_BOOT_OPTION2),\r
+        0,\r
+        EFI_IFR_NUMERIC_SIZE_1,\r
+        2\r
+        );\r
 \r
-    //\r
-    // Set initial vlaue of dynamic created oneof Question in Form Browser\r
-    //\r
-    Configuration = AllocateZeroPool (sizeof (DRIVER_SAMPLE_CONFIGURATION));\r
-    ASSERT (Configuration != NULL);\r
-    if (HiiGetBrowserData (&mFormSetGuid, VariableName, sizeof (DRIVER_SAMPLE_CONFIGURATION), (UINT8 *) Configuration)) {\r
-      Configuration->DynamicOneof = 2;\r
+      //\r
+      // Prepare initial value for the dynamic created oneof Question\r
+      //\r
+      PrivateData->Configuration.DynamicOneof = 2;\r
+      Status = gRT->SetVariable(\r
+                      VariableName,\r
+                      &mFormSetGuid,\r
+                      EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+                      sizeof (DRIVER_SAMPLE_CONFIGURATION),\r
+                      &PrivateData->Configuration\r
+                      );\r
 \r
       //\r
-      // Update uncommitted data of Browser\r
+      // Set initial vlaue of dynamic created oneof Question in Form Browser\r
       //\r
-      HiiSetBrowserData (\r
-        &mFormSetGuid,\r
-        VariableName,\r
-        sizeof (DRIVER_SAMPLE_CONFIGURATION),\r
-        (UINT8 *) Configuration,\r
-        NULL\r
+      Configuration = AllocateZeroPool (sizeof (DRIVER_SAMPLE_CONFIGURATION));\r
+      ASSERT (Configuration != NULL);\r
+      if (HiiGetBrowserData (&mFormSetGuid, VariableName, sizeof (DRIVER_SAMPLE_CONFIGURATION), (UINT8 *) Configuration)) {\r
+        Configuration->DynamicOneof = 2;\r
+\r
+        //\r
+        // Update uncommitted data of Browser\r
+        //\r
+        HiiSetBrowserData (\r
+          &mFormSetGuid,\r
+          VariableName,\r
+          sizeof (DRIVER_SAMPLE_CONFIGURATION),\r
+          (UINT8 *) Configuration,\r
+          NULL\r
+          );\r
+      }\r
+      FreePool (Configuration);\r
+\r
+      HiiCreateOneOfOpCode (\r
+        StartOpCodeHandle,                         // Container for dynamic created opcodes\r
+        0x8001,                                    // Question ID (or call it "key")\r
+        CONFIGURATION_VARSTORE_ID,                 // VarStore ID\r
+        (UINT16) DYNAMIC_ONE_OF_VAR_OFFSET,        // Offset in Buffer Storage\r
+        STRING_TOKEN (STR_ONE_OF_PROMPT),          // Question prompt text\r
+        STRING_TOKEN (STR_ONE_OF_HELP),            // Question help text\r
+        EFI_IFR_FLAG_CALLBACK,                     // Question flag\r
+        EFI_IFR_NUMERIC_SIZE_1,                    // Data type of Question Value\r
+        OptionsOpCodeHandle,                       // Option Opcode list\r
+        NULL                                       // Default Opcode is NULl\r
         );\r
-    }\r
-    FreePool (Configuration);\r
-\r
-    HiiCreateOneOfOpCode (\r
-      StartOpCodeHandle,                         // Container for dynamic created opcodes\r
-      0x8001,                                    // Question ID (or call it "key")\r
-      CONFIGURATION_VARSTORE_ID,                 // VarStore ID\r
-      (UINT16) DYNAMIC_ONE_OF_VAR_OFFSET,        // Offset in Buffer Storage\r
-      STRING_TOKEN (STR_ONE_OF_PROMPT),          // Question prompt text\r
-      STRING_TOKEN (STR_ONE_OF_HELP),            // Question help text\r
-      EFI_IFR_FLAG_CALLBACK,                     // Question flag\r
-      EFI_IFR_NUMERIC_SIZE_1,                    // Data type of Question Value\r
-      OptionsOpCodeHandle,                       // Option Opcode list\r
-      NULL                                       // Default Opcode is NULl\r
-      );\r
 \r
-    HiiCreateOrderedListOpCode (\r
-      StartOpCodeHandle,                         // Container for dynamic created opcodes\r
-      0x8002,                                    // Question ID\r
-      CONFIGURATION_VARSTORE_ID,                 // VarStore ID\r
-      (UINT16) DYNAMIC_ORDERED_LIST_VAR_OFFSET,  // Offset in Buffer Storage\r
-      STRING_TOKEN (STR_BOOT_OPTIONS),           // Question prompt text\r
-      STRING_TOKEN (STR_BOOT_OPTIONS),           // Question help text\r
-      EFI_IFR_FLAG_RESET_REQUIRED,               // Question flag\r
-      0,                                         // Ordered list flag, e.g. EFI_IFR_UNIQUE_SET\r
-      EFI_IFR_NUMERIC_SIZE_1,                    // Data type of Question value\r
-      5,                                         // Maximum container\r
-      OptionsOpCodeHandle,                       // Option Opcode list\r
-      NULL                                       // Default Opcode is NULl\r
-      );\r
+      HiiCreateOrderedListOpCode (\r
+        StartOpCodeHandle,                         // Container for dynamic created opcodes\r
+        0x8002,                                    // Question ID\r
+        CONFIGURATION_VARSTORE_ID,                 // VarStore ID\r
+        (UINT16) DYNAMIC_ORDERED_LIST_VAR_OFFSET,  // Offset in Buffer Storage\r
+        STRING_TOKEN (STR_BOOT_OPTIONS),           // Question prompt text\r
+        STRING_TOKEN (STR_BOOT_OPTIONS),           // Question help text\r
+        EFI_IFR_FLAG_RESET_REQUIRED,               // Question flag\r
+        0,                                         // Ordered list flag, e.g. EFI_IFR_UNIQUE_SET\r
+        EFI_IFR_NUMERIC_SIZE_1,                    // Data type of Question value\r
+        5,                                         // Maximum container\r
+        OptionsOpCodeHandle,                       // Option Opcode list\r
+        NULL                                       // Default Opcode is NULl\r
+        );\r
 \r
-    HiiCreateGotoOpCode (\r
-      StartOpCodeHandle,                // Container for dynamic created opcodes\r
-      1,                                // Target Form ID\r
-      STRING_TOKEN (STR_GOTO_FORM1),    // Prompt text\r
-      STRING_TOKEN (STR_GOTO_HELP),     // Help text\r
-      0,                                // Question flag\r
-      0x8003                            // Question ID\r
+      HiiCreateTextOpCode (\r
+        StartOpCodeHandle,\r
+        STRING_TOKEN(STR_TEXT_SAMPLE_HELP),\r
+        STRING_TOKEN(STR_TEXT_SAMPLE_HELP),\r
+        STRING_TOKEN(STR_TEXT_SAMPLE_STRING)\r
       );\r
 \r
-    HiiUpdateForm (\r
-      PrivateData->HiiHandle[0],  // HII handle\r
-      &mFormSetGuid,              // Formset GUID\r
-      0x1234,                     // Form ID\r
-      StartOpCodeHandle,          // Label for where to insert opcodes\r
-      EndOpCodeHandle             // Replace data\r
-      );\r
+      HiiCreateDateOpCode (\r
+        StartOpCodeHandle,\r
+        0x8004,\r
+        0x0,\r
+        0x0,\r
+        STRING_TOKEN(STR_DATE_SAMPLE_HELP),\r
+        STRING_TOKEN(STR_DATE_SAMPLE_HELP),\r
+        0,\r
+        QF_DATE_STORAGE_TIME,\r
+        NULL\r
+        );\r
 \r
-    HiiFreeOpCodeHandle (StartOpCodeHandle);\r
-    HiiFreeOpCodeHandle (OptionsOpCodeHandle);\r
-    HiiFreeOpCodeHandle (EndOpCodeHandle);\r
-    break;\r
+      HiiCreateTimeOpCode (\r
+        StartOpCodeHandle,\r
+        0x8005,\r
+        0x0,\r
+        0x0,\r
+        STRING_TOKEN(STR_TIME_SAMPLE_HELP),\r
+        STRING_TOKEN(STR_TIME_SAMPLE_HELP),\r
+        0,\r
+        QF_TIME_STORAGE_TIME,\r
+        NULL\r
+        );\r
 \r
-  case 0x5678:\r
-    //\r
-    // We will reach here once the Question is refreshed\r
-    //\r
+      HiiCreateGotoOpCode (\r
+        StartOpCodeHandle,                // Container for dynamic created opcodes\r
+        1,                                // Target Form ID\r
+        STRING_TOKEN (STR_GOTO_FORM1),    // Prompt text\r
+        STRING_TOKEN (STR_GOTO_HELP),     // Help text\r
+        0,                                // Question flag\r
+        0x8003                            // Question ID\r
+        );\r
 \r
-    //\r
-    // Initialize the container for dynamic opcodes\r
-    //\r
-    StartOpCodeHandle = HiiAllocateOpCodeHandle ();\r
-    ASSERT (StartOpCodeHandle != NULL);\r
+      HiiUpdateForm (\r
+        PrivateData->HiiHandle[0],  // HII handle\r
+        &mFormSetGuid,              // Formset GUID\r
+        0x1234,                     // Form ID\r
+        StartOpCodeHandle,          // Label for where to insert opcodes\r
+        EndOpCodeHandle             // Replace data\r
+        );\r
 \r
-    //\r
-    // Create Hii Extend Label OpCode as the start opcode\r
-    //\r
-    StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
-    StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
-    StartLabel->Number       = LABEL_UPDATE2;\r
+      HiiFreeOpCodeHandle (StartOpCodeHandle);\r
+      HiiFreeOpCodeHandle (OptionsOpCodeHandle);\r
+      HiiFreeOpCodeHandle (EndOpCodeHandle);\r
+      break;\r
 \r
-    HiiCreateActionOpCode (\r
-      StartOpCodeHandle,                // Container for dynamic created opcodes\r
-      0x1237,                           // Question ID\r
-      STRING_TOKEN(STR_EXIT_TEXT),      // Prompt text\r
-      STRING_TOKEN(STR_EXIT_TEXT),      // Help text\r
-      EFI_IFR_FLAG_CALLBACK,            // Question flag\r
-      0                                 // Action String ID\r
-    );\r
+    case 0x5678:\r
+      //\r
+      // We will reach here once the Question is refreshed\r
+      //\r
 \r
-    HiiUpdateForm (\r
-      PrivateData->HiiHandle[0],  // HII handle\r
-      &mFormSetGuid,              // Formset GUID\r
-      0x3,                        // Form ID\r
-      StartOpCodeHandle,          // Label for where to insert opcodes\r
-      NULL                        // Insert data\r
+      //\r
+      // Initialize the container for dynamic opcodes\r
+      //\r
+      StartOpCodeHandle = HiiAllocateOpCodeHandle ();\r
+      ASSERT (StartOpCodeHandle != NULL);\r
+\r
+      //\r
+      // Create Hii Extend Label OpCode as the start opcode\r
+      //\r
+      StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
+      StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
+      StartLabel->Number       = LABEL_UPDATE2;\r
+\r
+      HiiCreateActionOpCode (\r
+        StartOpCodeHandle,                // Container for dynamic created opcodes\r
+        0x1237,                           // Question ID\r
+        STRING_TOKEN(STR_EXIT_TEXT),      // Prompt text\r
+        STRING_TOKEN(STR_EXIT_TEXT),      // Help text\r
+        EFI_IFR_FLAG_CALLBACK,            // Question flag\r
+        0                                 // Action String ID\r
       );\r
 \r
-    HiiFreeOpCodeHandle (StartOpCodeHandle);\r
+      HiiUpdateForm (\r
+        PrivateData->HiiHandle[0],  // HII handle\r
+        &mFormSetGuid,              // Formset GUID\r
+        0x3,                        // Form ID\r
+        StartOpCodeHandle,          // Label for where to insert opcodes\r
+        NULL                        // Insert data\r
+        );\r
 \r
-    //\r
-    // Refresh the Question value\r
-    //\r
-    PrivateData->Configuration.DynamicRefresh++;\r
-    Status = gRT->SetVariable(\r
-                    VariableName,\r
-                    &mFormSetGuid,\r
-                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-                    sizeof (DRIVER_SAMPLE_CONFIGURATION),\r
-                    &PrivateData->Configuration\r
-                    );\r
+      HiiFreeOpCodeHandle (StartOpCodeHandle);\r
 \r
-    //\r
-    // Change an EFI Variable storage (MyEfiVar) asynchronous, this will cause\r
-    // the first statement in Form 3 be suppressed\r
-    //\r
-    MyVarSize = 1;\r
-    MyVar = 111;\r
-    Status = gRT->SetVariable(\r
-                    L"MyVar",\r
-                    &mFormSetGuid,\r
-                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-                    MyVarSize,\r
-                    &MyVar\r
-                    );\r
-    break;\r
+      //\r
+      // Refresh the Question value\r
+      //\r
+      PrivateData->Configuration.DynamicRefresh++;\r
+      Status = gRT->SetVariable(\r
+                      VariableName,\r
+                      &mFormSetGuid,\r
+                      EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+                      sizeof (DRIVER_SAMPLE_CONFIGURATION),\r
+                      &PrivateData->Configuration\r
+                      );\r
 \r
-  case 0x1237:\r
-    //\r
-    // User press "Exit now", request Browser to exit\r
-    //\r
-    *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;\r
-    break;\r
+      //\r
+      // Change an EFI Variable storage (MyEfiVar) asynchronous, this will cause\r
+      // the first statement in Form 3 be suppressed\r
+      //\r
+      MyVarSize = 1;\r
+      MyVar = 111;\r
+      Status = gRT->SetVariable(\r
+                      L"MyVar",\r
+                      &mFormSetGuid,\r
+                      EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+                      MyVarSize,\r
+                      &MyVar\r
+                      );\r
+      break;\r
 \r
-  case 0x1238:\r
-    //\r
-    // User press "Save now", request Browser to save the uncommitted data.\r
-    //\r
-    *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;\r
-    break;\r
+    case 0x1237:\r
+      //\r
+      // User press "Exit now", request Browser to exit\r
+      //\r
+      *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;\r
+      break;\r
 \r
-  case 0x2000:\r
-    //\r
-    // When try to set a new password, user will be chanlleged with old password.\r
-    // The Callback is responsible for validating old password input by user,\r
-    // If Callback return EFI_SUCCESS, it indicates validation pass.\r
-    //\r
-    switch (PrivateData->PasswordState) {\r
-    case BROWSER_STATE_VALIDATE_PASSWORD:\r
-      Status = ValidatePassword (PrivateData, Value->string);\r
-      if (Status == EFI_SUCCESS) {\r
-        PrivateData->PasswordState = BROWSER_STATE_SET_PASSWORD;\r
-      }\r
+    case 0x1238:\r
+      //\r
+      // User press "Save now", request Browser to save the uncommitted data.\r
+      //\r
+      *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;\r
       break;\r
 \r
-    case BROWSER_STATE_SET_PASSWORD:\r
-      Status = SetPassword (PrivateData, Value->string);\r
-      PrivateData->PasswordState = BROWSER_STATE_VALIDATE_PASSWORD;\r
+    case 0x2000:\r
+      //\r
+      // Only used to update the state.\r
+      //\r
+      if ((Type == EFI_IFR_TYPE_STRING) && (Value->string == 0) && \r
+        (PrivateData->PasswordState == BROWSER_STATE_SET_PASSWORD)) {\r
+        PrivateData->PasswordState = BROWSER_STATE_VALIDATE_PASSWORD;\r
+        return EFI_INVALID_PARAMETER;\r
+      }\r
+\r
+      //\r
+      // When try to set a new password, user will be chanlleged with old password.\r
+      // The Callback is responsible for validating old password input by user,\r
+      // If Callback return EFI_SUCCESS, it indicates validation pass.\r
+      //\r
+      switch (PrivateData->PasswordState) {\r
+      case BROWSER_STATE_VALIDATE_PASSWORD:\r
+        Status = ValidatePassword (PrivateData, Value->string);\r
+        if (Status == EFI_SUCCESS) {\r
+          PrivateData->PasswordState = BROWSER_STATE_SET_PASSWORD;\r
+        }\r
+        break;\r
+\r
+      case BROWSER_STATE_SET_PASSWORD:\r
+        Status = SetPassword (PrivateData, Value->string);\r
+        PrivateData->PasswordState = BROWSER_STATE_VALIDATE_PASSWORD;\r
+        break;\r
+\r
+      default:\r
+        break;\r
+      }\r
+\r
       break;\r
 \r
+    case 0x1111:\r
+      //\r
+      // EfiVarstore question takes sample action (print value as debug information) \r
+      // after read/write question.\r
+      //\r
+      MyVarSize = 1;\r
+      Status = gRT->GetVariable(\r
+                      L"MyVar",\r
+                      &mFormSetGuid,\r
+                      NULL,\r
+                      &MyVarSize,\r
+                      &MyVar\r
+                      );\r
+      ASSERT_EFI_ERROR (Status);\r
+      DEBUG ((DEBUG_INFO, "EfiVarstore question: Tall value is %d with value width %d\n", MyVar, MyVarSize));\r
     default:\r
       break;\r
     }\r
+  }\r
+  break;\r
 \r
-    break;\r
-\r
-  case 0x1111:\r
-    //\r
-    // EfiVarstore question takes sample action (print value as debug information) \r
-    // after read/write question.\r
-    //\r
-    MyVarSize = 1;\r
-    Status = gRT->GetVariable(\r
-                    L"MyVar",\r
-                    &mFormSetGuid,\r
-                    NULL,\r
-                    &MyVarSize,\r
-                    &MyVar\r
-                    );\r
-    ASSERT_EFI_ERROR (Status);\r
-    DEBUG ((DEBUG_INFO, "EfiVarstore question: Tall value is %d with value width %d\n", MyVar, MyVarSize));\r
   default:\r
+    Status = EFI_UNSUPPORTED;\r
     break;\r
   }\r
 \r
@@ -1399,6 +1821,9 @@ DriverSampleUnload (
   )\r
 {\r
   UINTN Index;\r
+\r
+  ASSERT (PrivateData != NULL);\r
+\r
   if (DriverHandle[0] != NULL) {\r
     gBS->UninstallMultipleProtocolInterfaces (\r
             DriverHandle[0],\r
@@ -1429,15 +1854,13 @@ DriverSampleUnload (
     HiiRemovePackages (PrivateData->HiiHandle[1]);\r
   }\r
 \r
-  if (PrivateData != NULL) {\r
-    for (Index = 0; Index < NAME_VALUE_NAME_NUMBER; Index++) {\r
-      if (PrivateData->NameValueName[Index] != NULL) {\r
-        FreePool (PrivateData->NameValueName[Index]);\r
-      }\r
+  for (Index = 0; Index < NAME_VALUE_NAME_NUMBER; Index++) {\r
+    if (PrivateData->NameValueName[Index] != NULL) {\r
+      FreePool (PrivateData->NameValueName[Index]);\r
     }\r
-    FreePool (PrivateData);\r
-    PrivateData = NULL;\r
   }\r
+  FreePool (PrivateData);\r
+  PrivateData = NULL;\r
 \r
   return EFI_SUCCESS;\r
 }\r