]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/DriverSampleDxe/DriverSample.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / DriverSampleDxe / DriverSample.c
index 1b58a8d7186438be3b2b6dc9a0b6349698b3c8c4..36ed734e382e0beef64945017c21c19cfd62ff09 100644 (file)
@@ -2,7 +2,7 @@
 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 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2018, 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
@@ -20,6 +20,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 CHAR16     VariableName[] = L"MyIfrNVData";\r
 CHAR16     MyEfiVar[] = L"MyEfiVar";\r
+CHAR16     MyEfiBitVar[] = L"MyEfiBitVar";\r
+CHAR16     MyEfiUnionVar[] = L"MyEfiUnionVar";\r
+\r
 EFI_HANDLE                      DriverHandle[2] = {NULL, NULL};\r
 DRIVER_SAMPLE_PRIVATE_DATA      *mPrivateData = NULL;\r
 EFI_EVENT                       mEvent;\r
@@ -110,22 +113,6 @@ SetArrayData (
   }\r
 }\r
 \r
-/**\r
-  Add empty function for event process function.\r
-\r
-  @param Event    The Event need to be process\r
-  @param Context  The context of the event.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-DriverSampleInternalEmptyFunction (\r
-  IN  EFI_EVENT Event,\r
-  IN  VOID      *Context\r
-  )\r
-{\r
-}\r
-\r
 /**\r
   Notification function for keystrokes.\r
 \r
@@ -140,12 +127,12 @@ NotificationFunction(
   )\r
 {\r
   gBS->SignalEvent (mEvent);\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  Function to start monitoring for CTRL-C using SimpleTextInputEx. \r
+  Function to start monitoring for CTRL-C using SimpleTextInputEx.\r
 \r
   @retval EFI_SUCCESS           The feature is enabled.\r
   @retval EFI_OUT_OF_RESOURCES  There is not enough mnemory available.\r
@@ -188,7 +175,7 @@ InternalStartMonitor(
     if (EFI_ERROR (Status)) {\r
       break;\r
     }\r
-    \r
+\r
     KeyData.KeyState.KeyShiftState  = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;\r
     Status = SimpleEx->RegisterKeyNotify(\r
       SimpleEx,\r
@@ -204,7 +191,7 @@ InternalStartMonitor(
 }\r
 \r
 /**\r
-  Function to stop monitoring for CTRL-C using SimpleTextInputEx.  \r
+  Function to stop monitoring for CTRL-C using SimpleTextInputEx.\r
 \r
   @retval EFI_SUCCESS           The feature is enabled.\r
   @retval EFI_OUT_OF_RESOURCES  There is not enough mnemory available.\r
@@ -218,7 +205,7 @@ InternalStopMonitor(
   EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx;\r
   EFI_STATUS                        Status;\r
   EFI_HANDLE                        *Handles;\r
-  EFI_KEY_DATA                      KeyData;  \r
+  EFI_KEY_DATA                      KeyData;\r
   UINTN                             HandleCount;\r
   UINTN                             HandleIndex;\r
   EFI_HANDLE                        NotifyHandle;\r
@@ -357,7 +344,7 @@ GetValueOfNumber (
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Exit;\r
   }\r
-  \r
+\r
   Length = *Len;\r
   ZeroMem (TemStr, sizeof (TemStr));\r
   for (Index = 0; Index < Length; Index ++) {\r
@@ -382,7 +369,7 @@ Exit:
 }\r
 \r
 /**\r
-  Create altcfg string. \r
+  Create altcfg string.\r
 \r
   @param  Result               The request result string.\r
   @param  ConfigHdr            The request head info. <ConfigHdr> format.\r
@@ -392,7 +379,7 @@ Exit:
 \r
   @retval  The string with altcfg info append at the end.\r
 **/\r
-EFI_STRING \r
+EFI_STRING\r
 CreateAltCfgString (\r
   IN     EFI_STRING     Result,\r
   IN     EFI_STRING     ConfigHdr,\r
@@ -417,60 +404,60 @@ CreateAltCfgString (
   TmpStr = StringPtr;\r
   if (Result != NULL) {\r
     StrCpyS (StringPtr, NewLen / sizeof (CHAR16), Result);\r
-    StringPtr += StrLen (Result);  \r
+    StringPtr += StrLen (Result);\r
     FreePool (Result);\r
   }\r
-  \r
+\r
   UnicodeSPrint (\r
-  StringPtr, \r
-  (1 + StrLen (ConfigHdr) + 8 + 4 + 1) * sizeof (CHAR16), \r
-  L"&%s&ALTCFG=%04x", \r
-  ConfigHdr, \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
+    StringPtr,\r
     (8 + 4 + 7 + 4 + 7 + 4 + 1) * sizeof (CHAR16),\r
-    L"&OFFSET=%04x&WIDTH=%04x&VALUE=%04x", \r
-    Offset, \r
+    L"&OFFSET=%04x&WIDTH=%04x&VALUE=%04x",\r
+    Offset,\r
     Width,\r
     DEFAULT_CLASS_STANDARD_VALUE\r
     );\r
-  StringPtr += StrLen (StringPtr);  \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
+  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
+    StringPtr,\r
     (8 + 4 + 7 + 4 + 7 + 4 + 1) * sizeof (CHAR16),\r
-    L"&OFFSET=%04x&WIDTH=%04x&VALUE=%04x", \r
-    Offset, \r
+    L"&OFFSET=%04x&WIDTH=%04x&VALUE=%04x",\r
+    Offset,\r
     Width,\r
     DEFAULT_CLASS_MANUFACTURING_VALUE\r
     );\r
-  StringPtr += StrLen (StringPtr); \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
+  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
+VOID\r
 AppendAltCfgString (\r
   IN OUT EFI_STRING                       *RequestResult,\r
   IN     EFI_STRING                       ConfigRequestHdr\r
@@ -680,6 +667,13 @@ ExtractConfig (
     if (HiiIsConfigHdrMatch(Request, &gDriverSampleFormSetGuid, MyEfiVar)) {\r
       return EFI_UNSUPPORTED;\r
     }\r
+    if (HiiIsConfigHdrMatch(Request, &gDriverSampleFormSetGuid, MyEfiBitVar)) {\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+    if (HiiIsConfigHdrMatch(Request, &gDriverSampleFormSetGuid, MyEfiUnionVar)) {\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
     //\r
     // Set Request to the unified request string.\r
     //\r
@@ -741,12 +735,14 @@ ExtractConfig (
 \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
+      UnicodeValueToStringS (\r
+        Value,\r
+        BufferSize - ((UINTN)Value - (UINTN)*Results),\r
+        PREFIX_ZERO | RADIX_HEX,\r
+        PrivateData->Configuration.NameValueVar0,\r
+        sizeof (PrivateData->Configuration.NameValueVar0) * 2\r
+        );\r
+      Value += StrnLenS (Value, (BufferSize - ((UINTN)Value - (UINTN)*Results)) / sizeof (CHAR16));\r
       *Value = BackupChar;\r
     }\r
 \r
@@ -760,12 +756,14 @@ ExtractConfig (
 \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
+      UnicodeValueToStringS (\r
+        Value,\r
+        BufferSize - ((UINTN)Value - (UINTN)*Results),\r
+        PREFIX_ZERO | RADIX_HEX,\r
+        PrivateData->Configuration.NameValueVar1,\r
+        sizeof (PrivateData->Configuration.NameValueVar1) * 2\r
+        );\r
+      Value += StrnLenS (Value, (BufferSize - ((UINTN)Value - (UINTN)*Results)) / sizeof (CHAR16));\r
       *Value = BackupChar;\r
     }\r
 \r
@@ -783,10 +781,17 @@ ExtractConfig (
       //\r
       StrPointer = (CHAR16 *) PrivateData->Configuration.NameValueVar2;\r
       for (; *StrPointer != L'\0'; StrPointer++) {\r
-        Value += UnicodeValueToString (Value, PREFIX_ZERO | RADIX_HEX, *StrPointer, 4);\r
+        UnicodeValueToStringS (\r
+          Value,\r
+          BufferSize - ((UINTN)Value - (UINTN)*Results),\r
+          PREFIX_ZERO | RADIX_HEX,\r
+          *StrPointer,\r
+          4\r
+          );\r
+        Value += StrnLenS (Value, (BufferSize - ((UINTN)Value - (UINTN)*Results)) / sizeof (CHAR16));\r
       }\r
     }\r
-    \r
+\r
     Status = EFI_SUCCESS;\r
   } else {\r
     //\r
@@ -890,6 +895,12 @@ RouteConfig (
   if (HiiIsConfigHdrMatch(Configuration, &gDriverSampleFormSetGuid, MyEfiVar)) {\r
     return EFI_UNSUPPORTED;\r
   }\r
+  if (HiiIsConfigHdrMatch(Configuration, &gDriverSampleFormSetGuid, MyEfiBitVar)) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+  if (HiiIsConfigHdrMatch(Configuration, &gDriverSampleFormSetGuid, MyEfiUnionVar)) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
 \r
   //\r
   // Get Buffer Storage data from EFI variable\r
@@ -1105,6 +1116,9 @@ DriverCallback (
   CHAR16                          *TmpStr;\r
   UINTN                           Index;\r
   UINT64                          BufferValue;\r
+  EFI_HII_POPUP_SELECTION         UserSelection;\r
+\r
+  UserSelection = 0xFF;\r
 \r
   if (((Value == NULL) && (Action != EFI_BROWSER_ACTION_FORM_OPEN) && (Action != EFI_BROWSER_ACTION_FORM_CLOSE))||\r
     (ActionRequest == NULL)) {\r
@@ -1195,7 +1209,7 @@ DriverCallback (
       }\r
     }\r
     break;\r
-    \r
+\r
   case EFI_BROWSER_ACTION_RETRIEVE:\r
     {\r
       switch (QuestionId ) {\r
@@ -1241,7 +1255,7 @@ DriverCallback (
           EFI_IFR_FLAG_CALLBACK,            // Question flag\r
           0                                 // Action String ID\r
         );\r
-      \r
+\r
         HiiUpdateForm (\r
           PrivateData->HiiHandle[0],        // HII handle\r
           &gDriverSampleFormSetGuid,        // Formset GUID\r
@@ -1299,6 +1313,10 @@ DriverCallback (
         }\r
       break;\r
 \r
+      case 0x6666:\r
+        Value->u8 = 12;\r
+        break;\r
+\r
       default:\r
         Status = EFI_UNSUPPORTED;\r
       break;\r
@@ -1313,8 +1331,12 @@ DriverCallback (
         Value->u8 = DEFAULT_CLASS_MANUFACTURING_VALUE;\r
       break;\r
 \r
+     case 0x6666:\r
+        Value->u8 = 13;\r
+        break;\r
+\r
       default:\r
-        Status = EFI_UNSUPPORTED;      \r
+        Status = EFI_UNSUPPORTED;\r
       break;\r
       }\r
     }\r
@@ -1515,14 +1537,14 @@ DriverCallback (
         //\r
         *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;\r
         break;\r
-      \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
+\r
       case 0x1241:\r
       case 0x1246:\r
         //\r
@@ -1530,21 +1552,21 @@ DriverCallback (
         //\r
         *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT;\r
         break;\r
-      \r
+\r
       case 0x1242:\r
         //\r
         // User press "Discard current form now", request Browser to discard the uncommitted data.\r
         //\r
         *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD;\r
         break;\r
-      \r
+\r
       case 0x1243:\r
         //\r
         // User press "Submit current form now", request Browser to save the uncommitted data.\r
         //\r
         *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_APPLY;\r
         break;\r
-      \r
+\r
       case 0x1244:\r
       case 0x1245:\r
         //\r
@@ -1584,7 +1606,7 @@ DriverCallback (
         //\r
         // 2. If system support this keyword, just try to change value.\r
         //\r
-        \r
+\r
         //\r
         // Change value from '0' to '1' or from '1' to '0'\r
         //\r
@@ -1624,6 +1646,22 @@ DriverCallback (
         }\r
         break;\r
 \r
+      case 0x1330:\r
+        Status = mPrivateData->HiiPopup->CreatePopup (\r
+          mPrivateData->HiiPopup,\r
+          EfiHiiPopupStyleInfo,\r
+          EfiHiiPopupTypeYesNo,\r
+          mPrivateData->HiiHandle[0],\r
+          STRING_TOKEN (STR_POPUP_STRING),\r
+          &UserSelection\r
+          );\r
+        if (!EFI_ERROR (Status)) {\r
+          if (UserSelection == EfiHiiPopupSelectionYes) {\r
+            *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;\r
+          }\r
+        }\r
+        break;\r
+\r
       default:\r
       break;\r
     }\r
@@ -1683,6 +1721,7 @@ DriverSampleInit (
   EFI_FORM_BROWSER2_PROTOCOL      *FormBrowser2;\r
   EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;\r
   EFI_CONFIG_KEYWORD_HANDLER_PROTOCOL *HiiKeywordHandler;\r
+  EFI_HII_POPUP_PROTOCOL              *PopupHandler;\r
   CHAR16                          *NewString;\r
   UINTN                           BufferSize;\r
   DRIVER_SAMPLE_CONFIGURATION     *Configuration;\r
@@ -1690,6 +1729,8 @@ DriverSampleInit (
   EFI_STRING                      ConfigRequestHdr;\r
   EFI_STRING                      NameRequestHdr;\r
   MY_EFI_VARSTORE_DATA            *VarStoreConfig;\r
+  MY_EFI_BITS_VARSTORE_DATA       *BitsVarStoreConfig;\r
+  MY_EFI_UNION_DATA               *UnionConfig;\r
   EFI_INPUT_KEY                   HotKey;\r
   EDKII_FORM_BROWSER_EXTENSION_PROTOCOL *FormBrowserEx;\r
 \r
@@ -1768,6 +1809,15 @@ DriverSampleInit (
   }\r
   mPrivateData->HiiKeywordHandler = HiiKeywordHandler;\r
 \r
+  //\r
+  // Locate HiiPopup protocol\r
+  //\r
+  Status = gBS->LocateProtocol (&gEfiHiiPopupProtocolGuid, NULL, (VOID **) &PopupHandler);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  mPrivateData->HiiPopup = PopupHandler;\r
+\r
   Status = gBS->InstallMultipleProtocolInterfaces (\r
                   &DriverHandle[0],\r
                   &gEfiDevicePathProtocolGuid,\r
@@ -1969,10 +2019,104 @@ DriverSampleInit (
   }\r
   FreePool (ConfigRequestHdr);\r
 \r
+  //\r
+  // Initialize Bits efi varstore configuration data\r
+  //\r
+  BitsVarStoreConfig = &mPrivateData->BitsVarStoreConfig;\r
+  ZeroMem (BitsVarStoreConfig, sizeof (MY_EFI_BITS_VARSTORE_DATA));\r
+\r
+  ConfigRequestHdr = HiiConstructConfigHdr (&gDriverSampleFormSetGuid, MyEfiBitVar, DriverHandle[0]);\r
+  ASSERT (ConfigRequestHdr != NULL);\r
+\r
+  BufferSize = sizeof (MY_EFI_BITS_VARSTORE_DATA);\r
+  Status = gRT->GetVariable (MyEfiBitVar, &gDriverSampleFormSetGuid, NULL, &BufferSize, BitsVarStoreConfig);\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // Store zero data to EFI variable Storage.\r
+    //\r
+    Status = gRT->SetVariable(\r
+                    MyEfiBitVar,\r
+                    &gDriverSampleFormSetGuid,\r
+                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+                    sizeof (MY_EFI_BITS_VARSTORE_DATA),\r
+                    BitsVarStoreConfig\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      DriverSampleUnload (ImageHandle);\r
+      return Status;\r
+    }\r
+    //\r
+    // EFI variable for NV config doesn't exit, we should build this variable\r
+    // based on default values stored in IFR\r
+    //\r
+    ActionFlag = HiiSetToDefaults (ConfigRequestHdr, EFI_HII_DEFAULT_CLASS_STANDARD);\r
+    if (!ActionFlag) {\r
+      DriverSampleUnload (ImageHandle);\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+  } else {\r
+    //\r
+    // EFI variable does exist and Validate Current Setting\r
+    //\r
+    ActionFlag = HiiValidateSettings (ConfigRequestHdr);\r
+    if (!ActionFlag) {\r
+      DriverSampleUnload (ImageHandle);\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+  }\r
+  FreePool (ConfigRequestHdr);\r
+\r
+   //\r
+  // Initialize Union efi varstore configuration data\r
+  //\r
+  UnionConfig = &mPrivateData->UnionConfig;\r
+  ZeroMem (UnionConfig, sizeof (MY_EFI_UNION_DATA));\r
+\r
+  ConfigRequestHdr = HiiConstructConfigHdr (&gDriverSampleFormSetGuid, MyEfiUnionVar, DriverHandle[0]);\r
+  ASSERT (ConfigRequestHdr != NULL);\r
+\r
+  BufferSize = sizeof (MY_EFI_UNION_DATA);\r
+  Status = gRT->GetVariable (MyEfiUnionVar, &gDriverSampleFormSetGuid, NULL, &BufferSize, UnionConfig);\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // Store zero data to EFI variable Storage.\r
+    //\r
+    Status = gRT->SetVariable(\r
+                    MyEfiUnionVar,\r
+                    &gDriverSampleFormSetGuid,\r
+                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+                    sizeof (MY_EFI_UNION_DATA),\r
+                    UnionConfig\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      DriverSampleUnload (ImageHandle);\r
+      return Status;\r
+    }\r
+    //\r
+    // EFI variable for NV config doesn't exit, we should build this variable\r
+    // based on default values stored in IFR\r
+    //\r
+    ActionFlag = HiiSetToDefaults (ConfigRequestHdr, EFI_HII_DEFAULT_CLASS_STANDARD);\r
+    if (!ActionFlag) {\r
+      DriverSampleUnload (ImageHandle);\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+  } else {\r
+    //\r
+    // EFI variable does exist and Validate Current Setting\r
+    //\r
+    ActionFlag = HiiValidateSettings (ConfigRequestHdr);\r
+    if (!ActionFlag) {\r
+      DriverSampleUnload (ImageHandle);\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+  }\r
+  FreePool (ConfigRequestHdr);\r
+\r
   Status = gBS->CreateEventEx (\r
-        EVT_NOTIFY_SIGNAL, \r
+        EVT_NOTIFY_SIGNAL,\r
         TPL_NOTIFY,\r
-        DriverSampleInternalEmptyFunction,\r
+        EfiEventEmptyFunction,\r
         NULL,\r
         &gEfiIfrRefreshIdOpGuid,\r
         &mEvent\r
@@ -1981,7 +2125,7 @@ DriverSampleInit (
 \r
   //\r
   // Example of how to use BrowserEx protocol to register HotKey.\r
-  // \r
+  //\r
   Status = gBS->LocateProtocol (&gEdkiiFormBrowserExProtocolGuid, NULL, (VOID **) &FormBrowserEx);\r
   if (!EFI_ERROR (Status)) {\r
     //\r
@@ -1992,7 +2136,7 @@ DriverSampleInit (
     FormBrowserEx->RegisterHotKey (&HotKey, 0, 0, NULL);\r
     HotKey.ScanCode    = SCAN_F10;\r
     FormBrowserEx->RegisterHotKey (&HotKey, 0, 0, NULL);\r
-    \r
+\r
     //\r
     // Register the default HotKey F9 and F10 again.\r
     //\r