]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg: Use BmForEachVariable to collect all key options
authorRuiyu Ni <ruiyu.ni@intel.com>
Tue, 17 Nov 2015 10:10:16 +0000 (10:10 +0000)
committerniruiyu <niruiyu@Edk2>
Tue, 17 Nov 2015 10:10:16 +0000 (10:10 +0000)
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Sunny Wang <sunnywang@hpe.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@18857 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Library/UefiBootManagerLib/BmHotkey.c

index f9023572557321a190b6b691f570013d453cfe6a..af303e8469ab8e9fd542581c2e25a370cf7f28ef 100644 (file)
@@ -31,18 +31,37 @@ EFI_BOOT_MANAGER_LOAD_OPTION mBmHotkeyBootOption      = { LoadOptionNumberUnassi
 EFI_BOOT_MANAGER_KEY_OPTION  *mBmContinueKeyOption    = NULL;\r
 VOID                         *mBmTxtInExRegistration  = NULL;\r
 \r
+\r
+/**\r
+  Return the buffer size of the EFI_BOOT_MANAGER_KEY_OPTION data.\r
+\r
+  @param   KeyOption            The input key option info.\r
+\r
+  @retval  The buffer size of the key option data.\r
+**/\r
+UINTN\r
+BmSizeOfKeyOption (\r
+  EFI_BOOT_MANAGER_KEY_OPTION  *KeyOption\r
+  )\r
+{\r
+  return OFFSET_OF (EFI_BOOT_MANAGER_KEY_OPTION, Keys)\r
+    + KeyOption->KeyData.Options.InputKeyCount * sizeof (EFI_INPUT_KEY);\r
+}\r
+\r
 /**\r
 \r
   Check whether the input key option is valid.\r
 \r
-  @param   KeyOption               Input key option info.\r
+  @param   KeyOption          Key option.\r
+  @param   KeyOptionSize      Size of the key option.\r
 \r
   @retval  TRUE               Input key option is valid.\r
   @retval  FALSE              Input key option is not valid.\r
 **/\r
 BOOLEAN\r
 BmIsKeyOptionValid (\r
-  IN EFI_BOOT_MANAGER_KEY_OPTION     *KeyOption\r
+  IN EFI_BOOT_MANAGER_KEY_OPTION     *KeyOption,\r
+  IN UINTN                           KeyOptionSize\r
 )\r
 {\r
   UINT16   OptionName[BM_OPTION_NAME_LEN];\r
@@ -50,10 +69,17 @@ BmIsKeyOptionValid (
   UINTN    BootOptionSize;\r
   UINT32   Crc;\r
 \r
+  if (BmSizeOfKeyOption (KeyOption) != KeyOptionSize) {\r
+    return FALSE;\r
+  }\r
+\r
   //\r
   // Check whether corresponding Boot Option exist\r
   //\r
-  UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", KeyOption->BootOption);\r
+  UnicodeSPrint (\r
+    OptionName, sizeof (OptionName), L"%s%04x",\r
+    mBmLoadOptionName[LoadOptionTypeBoot], KeyOption->BootOption\r
+    );\r
   GetEfiGlobalVariable2 (OptionName, (VOID **) &BootOption, &BootOptionSize);\r
 \r
   if (BootOption == NULL) {\r
@@ -110,20 +136,58 @@ BmIsKeyOptionVariable (
   return TRUE;\r
 }\r
 \r
-/**\r
-  Return the buffer size of the EFI_BOOT_MANAGER_KEY_OPTION data.\r
+typedef struct {\r
+  EFI_BOOT_MANAGER_KEY_OPTION *KeyOptions;\r
+  UINTN                       KeyOptionCount;\r
+} BM_COLLECT_KEY_OPTIONS_PARAM;\r
 \r
-  @param   KeyOption            The input key option info.\r
+/**\r
+  Visitor function to collect the key options from NV storage.\r
 \r
-  @retval  The buffer size of the key option data.\r
+  @param Name    Variable name.\r
+  @param Guid    Variable GUID.\r
+  @param Context The same context passed to BmForEachVariable.\r
 **/\r
-UINTN\r
-BmSizeOfKeyOption (\r
-  EFI_BOOT_MANAGER_KEY_OPTION  *KeyOption\r
+VOID\r
+BmCollectKeyOptions (\r
+  CHAR16               *Name,\r
+  EFI_GUID             *Guid,\r
+  VOID                 *Context\r
   )\r
 {\r
-  return OFFSET_OF (EFI_BOOT_MANAGER_KEY_OPTION, Keys)\r
-    + KeyOption->KeyData.Options.InputKeyCount * sizeof (EFI_INPUT_KEY);\r
+  UINTN                        Index;\r
+  BM_COLLECT_KEY_OPTIONS_PARAM *Param;\r
+  EFI_BOOT_MANAGER_KEY_OPTION  *KeyOption;\r
+  UINT16                       OptionNumber;\r
+  UINTN                        KeyOptionSize;\r
+\r
+  Param = (BM_COLLECT_KEY_OPTIONS_PARAM *) Context;\r
+\r
+  if (BmIsKeyOptionVariable (Name, Guid, &OptionNumber)) {\r
+    GetEfiGlobalVariable2 (Name, (VOID**) &KeyOption, &KeyOptionSize);\r
+    ASSERT (KeyOption != NULL);\r
+    KeyOption->OptionNumber = OptionNumber;\r
+    if (BmIsKeyOptionValid (KeyOption, KeyOptionSize)) {\r
+      Param->KeyOptions = ReallocatePool (\r
+                            Param->KeyOptionCount * sizeof (EFI_BOOT_MANAGER_KEY_OPTION),\r
+                            (Param->KeyOptionCount + 1) * sizeof (EFI_BOOT_MANAGER_KEY_OPTION),\r
+                            Param->KeyOptions\r
+                            );\r
+      ASSERT (Param->KeyOptions != NULL);\r
+      //\r
+      // Insert the key option in order\r
+      //\r
+      for (Index = 0; Index < Param->KeyOptionCount; Index++) {\r
+        if (KeyOption->OptionNumber < Param->KeyOptions[Index].OptionNumber) {\r
+          break;\r
+        }\r
+      }\r
+      CopyMem (&Param->KeyOptions[Index + 1], &Param->KeyOptions[Index], (Param->KeyOptionCount - Index) * sizeof (EFI_BOOT_MANAGER_KEY_OPTION));\r
+      CopyMem (&Param->KeyOptions[Index], KeyOption, BmSizeOfKeyOption (KeyOption));\r
+      Param->KeyOptionCount++;\r
+    }\r
+    FreePool (KeyOption);\r
+  }\r
 }\r
 \r
 /**\r
@@ -139,71 +203,20 @@ BmGetKeyOptions (
   OUT UINTN     *Count\r
   )\r
 {\r
-  EFI_STATUS                  Status;\r
-  UINTN                       Index;\r
-  CHAR16                      *Name;\r
-  EFI_GUID                    Guid;\r
-  UINTN                       NameSize;\r
-  UINTN                       NewNameSize;\r
-  EFI_BOOT_MANAGER_KEY_OPTION *KeyOptions;\r
-  EFI_BOOT_MANAGER_KEY_OPTION *KeyOption;\r
-  UINT16                      OptionNumber;\r
+  BM_COLLECT_KEY_OPTIONS_PARAM Param;\r
 \r
   if (Count == NULL) {\r
     return NULL;\r
   }\r
 \r
-  *Count     = 0;\r
-  KeyOptions = NULL;\r
-\r
-  NameSize = sizeof (CHAR16);\r
-  Name     = AllocateZeroPool (NameSize);\r
-  ASSERT (Name != NULL);\r
-  while (TRUE) {\r
-    NewNameSize = NameSize;\r
-    Status = gRT->GetNextVariableName (&NewNameSize, Name, &Guid);\r
-    if (Status == EFI_BUFFER_TOO_SMALL) {\r
-      Name = ReallocatePool (NameSize, NewNameSize, Name);\r
-      ASSERT (Name != NULL);\r
-      Status = gRT->GetNextVariableName (&NewNameSize, Name, &Guid);\r
-      NameSize = NewNameSize;\r
-    }\r
-\r
-    if (Status == EFI_NOT_FOUND) {\r
-      break;\r
-    }\r
-    ASSERT_EFI_ERROR (Status);\r
+  Param.KeyOptions = NULL;\r
+  Param.KeyOptionCount = 0;\r
 \r
-    if (BmIsKeyOptionVariable (Name ,&Guid, &OptionNumber)) {\r
-      GetEfiGlobalVariable2 (Name, (VOID**) &KeyOption, NULL);\r
-      ASSERT (KeyOption != NULL);\r
-      if (BmIsKeyOptionValid (KeyOption)) {\r
-        KeyOptions = ReallocatePool (\r
-                       *Count * sizeof (EFI_BOOT_MANAGER_KEY_OPTION),\r
-                       (*Count + 1) * sizeof (EFI_BOOT_MANAGER_KEY_OPTION),\r
-                       KeyOptions\r
-                       );\r
-        ASSERT (KeyOptions != NULL);\r
-        //\r
-        // Insert the key option in order\r
-        //\r
-        for (Index = 0; Index < *Count; Index++) {\r
-          if (OptionNumber < KeyOptions[Index].OptionNumber) {\r
-            break;\r
-          }\r
-        }\r
-        CopyMem (&KeyOptions[Index + 1], &KeyOptions[Index], (*Count - Index) * sizeof (EFI_BOOT_MANAGER_KEY_OPTION));\r
-        CopyMem (&KeyOptions[Index], KeyOption, BmSizeOfKeyOption (KeyOption));\r
-        KeyOptions[Index].OptionNumber = OptionNumber;\r
-        (*Count)++;\r
-      }\r
-      FreePool (KeyOption);\r
-    }\r
-  }\r
+  BmForEachVariable (BmCollectKeyOptions, (VOID *) &Param);\r
 \r
-  FreePool (Name);\r
+  *Count = Param.KeyOptionCount;\r
 \r
-  return KeyOptions;\r
+  return Param.KeyOptions;\r
 }\r
 \r
 /**\r
@@ -401,7 +414,10 @@ BmHotkeyCallback (
           //\r
           // Launch its BootOption\r
           //\r
-          UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", Hotkey->BootOption);\r
+          UnicodeSPrint (\r
+            OptionName, sizeof (OptionName), L"%s%04x",\r
+            mBmLoadOptionName[LoadOptionTypeBoot], Hotkey->BootOption\r
+            );\r
           Status = EfiBootManagerVariableToLoadOption (OptionName, &mBmHotkeyBootOption);\r
           DEBUG ((EFI_D_INFO, "[Bds]Hotkey for %s pressed - %r\n", OptionName, Status));\r
           if (EFI_ERROR (Status)) {\r
@@ -913,7 +929,10 @@ EfiBootManagerAddKeyOptionVariable (
   UINTN                          KeyOptionNumber;\r
   CHAR16                         KeyOptionName[sizeof ("Key####")];\r
 \r
-  UnicodeSPrint (BootOptionName, sizeof (BootOptionName), L"Boot%04x", BootOptionNumber);\r
+  UnicodeSPrint (\r
+    BootOptionName, sizeof (BootOptionName), L"%s%04x",\r
+    mBmLoadOptionName[LoadOptionTypeBoot], BootOptionNumber\r
+    );\r
   GetEfiGlobalVariable2 (BootOptionName, &BootOption, &BootOptionSize);\r
 \r
   if (BootOption == NULL) {\r