]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Universal/BdsDxe/BootMaint/Variable.c
IntelFrameworkModulePkg BootMaint: Use safe string functions
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / BdsDxe / BootMaint / Variable.c
index ef36ae2dc81b39122e62a6c8d55de76c46470df7..616549e64a223f505a6de112695984680f744338 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Variable operation that will be used by bootmaint\r
 \r
-Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2015, 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
@@ -165,9 +165,10 @@ Var_ChangeBootOrder (
                     BootOrderListSize * sizeof (UINT16),\r
                     BootOrderList\r
                     );\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
+    //\r
+    // Changing variable without increasing its size with current variable implementation shouldn't fail.\r
+    //\r
+    ASSERT_EFI_ERROR (Status);\r
   }\r
   return EFI_SUCCESS;\r
 }\r
@@ -303,9 +304,10 @@ Var_ChangeDriverOrder (
                     DriverOrderListSize * sizeof (UINT16),\r
                     DriverOrderList\r
                     );\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
+    //\r
+    // Changing variable without increasing its size with current variable implementation shouldn't fail.\r
+    //\r
+    ASSERT_EFI_ERROR (Status);\r
   }\r
   return EFI_SUCCESS;\r
 }\r
@@ -338,7 +340,10 @@ Var_UpdateAllConsoleOption (
                     GetDevicePathSize (OutDevicePath),\r
                     OutDevicePath\r
                     );\r
-    ASSERT (!EFI_ERROR (Status));\r
+    //\r
+    // Changing variable without increasing its size with current variable implementation shouldn't fail.\r
+    //\r
+    ASSERT_EFI_ERROR (Status);\r
   }\r
 \r
   if (InpDevicePath != NULL) {\r
@@ -350,7 +355,10 @@ Var_UpdateAllConsoleOption (
                     GetDevicePathSize (InpDevicePath),\r
                     InpDevicePath\r
                     );\r
-    ASSERT (!EFI_ERROR (Status));\r
+    //\r
+    // Changing variable without increasing its size with current variable implementation shouldn't fail.\r
+    //\r
+    ASSERT_EFI_ERROR (Status);\r
   }\r
 \r
   if (ErrDevicePath != NULL) {\r
@@ -362,7 +370,10 @@ Var_UpdateAllConsoleOption (
                     GetDevicePathSize (ErrDevicePath),\r
                     ErrDevicePath\r
                     );\r
-    ASSERT (!EFI_ERROR (Status));\r
+    //\r
+    // Changing variable without increasing its size with current variable implementation shouldn't fail.\r
+    //\r
+    ASSERT_EFI_ERROR (Status);\r
   }\r
 }\r
 \r
@@ -568,7 +579,7 @@ Var_UpdateDriverOption (
     );\r
 \r
   if (*DescriptionData == 0x0000) {\r
-    StrCpy (DescriptionData, DriverString);\r
+    StrCpyS (DescriptionData, DESCRIPTION_DATA_SIZE, DriverString);\r
   }\r
 \r
   BufferSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (DescriptionData);\r
@@ -667,38 +678,50 @@ Var_UpdateDriverOption (
                   BufferSize,\r
                   Buffer\r
                   );\r
-  ASSERT_EFI_ERROR (Status);\r
-  DriverOrderList = BdsLibGetVariableAndSize (\r
-                      L"DriverOrder",\r
-                      &gEfiGlobalVariableGuid,\r
-                      &DriverOrderListSize\r
-                      );\r
-  NewDriverOrderList = AllocateZeroPool (DriverOrderListSize + sizeof (UINT16));\r
-  ASSERT (NewDriverOrderList != NULL);\r
-  if (DriverOrderList != NULL) {\r
-    CopyMem (NewDriverOrderList, DriverOrderList, DriverOrderListSize);\r
-    EfiLibDeleteVariable (L"DriverOrder", &gEfiGlobalVariableGuid);\r
-  }\r
-  NewDriverOrderList[DriverOrderListSize / sizeof (UINT16)] = Index;\r
+  if (!EFI_ERROR (Status)) {\r
+    DriverOrderList = BdsLibGetVariableAndSize (\r
+                        L"DriverOrder",\r
+                        &gEfiGlobalVariableGuid,\r
+                        &DriverOrderListSize\r
+                        );\r
+    NewDriverOrderList = AllocateZeroPool (DriverOrderListSize + sizeof (UINT16));\r
+    ASSERT (NewDriverOrderList != NULL);\r
+    if (DriverOrderList != NULL) {\r
+      CopyMem (NewDriverOrderList, DriverOrderList, DriverOrderListSize);\r
+      EfiLibDeleteVariable (L"DriverOrder", &gEfiGlobalVariableGuid);\r
+    }\r
+    NewDriverOrderList[DriverOrderListSize / sizeof (UINT16)] = Index;\r
 \r
-  Status = gRT->SetVariable (\r
-                  L"DriverOrder",\r
-                  &gEfiGlobalVariableGuid,\r
-                  EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
-                  DriverOrderListSize + sizeof (UINT16),\r
-                  NewDriverOrderList\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-  if (DriverOrderList != NULL) {\r
-    FreePool (DriverOrderList);\r
-  }\r
-  DriverOrderList = NULL;\r
-  FreePool (NewDriverOrderList);\r
-  InsertTailList (&DriverOptionMenu.Head, &NewMenuEntry->Link);\r
-  DriverOptionMenu.MenuNumber++;\r
+    Status = gRT->SetVariable (\r
+                    L"DriverOrder",\r
+                    &gEfiGlobalVariableGuid,\r
+                    EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+                    DriverOrderListSize + sizeof (UINT16),\r
+                    NewDriverOrderList\r
+                    );\r
+    if (DriverOrderList != NULL) {\r
+      FreePool (DriverOrderList);\r
+    }\r
+    DriverOrderList = NULL;\r
+    FreePool (NewDriverOrderList);\r
+    if (!EFI_ERROR (Status)) {\r
+      InsertTailList (&DriverOptionMenu.Head, &NewMenuEntry->Link);\r
+      DriverOptionMenu.MenuNumber++;\r
+\r
+      //\r
+      // Update "change boot order" page used data, append the new add boot\r
+      // option at the end.\r
+      //\r
+      Index = 0;\r
+      while (CallbackData->BmmFakeNvData.DriverOptionOrder[Index] != 0) {\r
+        Index++;\r
+      }\r
+      CallbackData->BmmFakeNvData.DriverOptionOrder[Index] = (UINT32) (NewMenuEntry->OptionNumber + 1);\r
 \r
-  *DescriptionData  = 0x0000;\r
-  *OptionalData     = 0x0000;\r
+      *DescriptionData  = 0x0000;\r
+      *OptionalData     = 0x0000;\r
+    }\r
+  }\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -739,16 +762,20 @@ Var_UpdateBootOption (
   Index = BOpt_GetBootOptionNumber () ;\r
   UnicodeSPrint (BootString, sizeof (BootString), L"Boot%04x", Index);\r
 \r
-  if (NvRamMap->DescriptionData[0] == 0x0000) {\r
-    StrCpy (NvRamMap->DescriptionData, BootString);\r
+  if (NvRamMap->BootDescriptionData[0] == 0x0000) {\r
+    StrCpyS (\r
+      NvRamMap->BootDescriptionData,\r
+      sizeof (NvRamMap->BootDescriptionData) / sizeof (NvRamMap->BootDescriptionData[0]),\r
+      BootString\r
+      );\r
   }\r
 \r
-  BufferSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (NvRamMap->DescriptionData);\r
+  BufferSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (NvRamMap->BootDescriptionData);\r
   BufferSize += GetDevicePathSize (CallbackData->LoadContext->FilePathList);\r
 \r
-  if (NvRamMap->OptionalData[0] != 0x0000) {\r
+  if (NvRamMap->BootOptionalData[0] != 0x0000) {\r
     OptionalDataExist = TRUE;\r
-    BufferSize += StrSize (NvRamMap->OptionalData);\r
+    BufferSize += StrSize (NvRamMap->BootOptionalData);\r
   }\r
 \r
   Buffer = AllocateZeroPool (BufferSize);\r
@@ -778,21 +805,21 @@ Var_UpdateBootOption (
 \r
   CopyMem (\r
     Ptr,\r
-    NvRamMap->DescriptionData,\r
-    StrSize (NvRamMap->DescriptionData)\r
+    NvRamMap->BootDescriptionData,\r
+    StrSize (NvRamMap->BootDescriptionData)\r
     );\r
 \r
-  NewLoadContext->Description = AllocateZeroPool (StrSize (NvRamMap->DescriptionData));\r
+  NewLoadContext->Description = AllocateZeroPool (StrSize (NvRamMap->BootDescriptionData));\r
   ASSERT (NewLoadContext->Description != NULL);\r
 \r
   NewMenuEntry->DisplayString = NewLoadContext->Description;\r
   CopyMem (\r
     NewLoadContext->Description,\r
     (VOID *) Ptr,\r
-    StrSize (NvRamMap->DescriptionData)\r
+    StrSize (NvRamMap->BootDescriptionData)\r
     );\r
 \r
-  Ptr += StrSize (NvRamMap->DescriptionData);\r
+  Ptr += StrSize (NvRamMap->BootDescriptionData);\r
   CopyMem (\r
     Ptr,\r
     CallbackData->LoadContext->FilePathList,\r
@@ -825,7 +852,7 @@ Var_UpdateBootOption (
   if (OptionalDataExist) {\r
     Ptr += (UINT8) GetDevicePathSize (CallbackData->LoadContext->FilePathList);\r
 \r
-    CopyMem (Ptr, NvRamMap->OptionalData, StrSize (NvRamMap->OptionalData));\r
+    CopyMem (Ptr, NvRamMap->BootOptionalData, StrSize (NvRamMap->BootOptionalData));\r
   }\r
 \r
   Status = gRT->SetVariable (\r
@@ -835,39 +862,51 @@ Var_UpdateBootOption (
                   BufferSize,\r
                   Buffer\r
                   );\r
-  ASSERT_EFI_ERROR (Status);\r
+  if (!EFI_ERROR (Status)) {\r
 \r
-  BootOrderList = BdsLibGetVariableAndSize (\r
+    BootOrderList = BdsLibGetVariableAndSize (\r
+                      L"BootOrder",\r
+                      &gEfiGlobalVariableGuid,\r
+                      &BootOrderListSize\r
+                      );\r
+    ASSERT (BootOrderList != NULL);\r
+    NewBootOrderList = AllocateZeroPool (BootOrderListSize + sizeof (UINT16));\r
+    ASSERT (NewBootOrderList != NULL);\r
+    CopyMem (NewBootOrderList, BootOrderList, BootOrderListSize);\r
+    NewBootOrderList[BootOrderListSize / sizeof (UINT16)] = Index;\r
+\r
+    if (BootOrderList != NULL) {\r
+      FreePool (BootOrderList);\r
+    }\r
+\r
+    Status = gRT->SetVariable (\r
                     L"BootOrder",\r
                     &gEfiGlobalVariableGuid,\r
-                    &BootOrderListSize\r
+                    EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+                    BootOrderListSize + sizeof (UINT16),\r
+                    NewBootOrderList\r
                     );\r
-  ASSERT (BootOrderList != NULL);\r
-  NewBootOrderList = AllocateZeroPool (BootOrderListSize + sizeof (UINT16));\r
-  ASSERT (NewBootOrderList != NULL);\r
-  CopyMem (NewBootOrderList, BootOrderList, BootOrderListSize);\r
-  NewBootOrderList[BootOrderListSize / sizeof (UINT16)] = Index;\r
+    if (!EFI_ERROR (Status)) {\r
+\r
+      FreePool (NewBootOrderList);\r
+      NewBootOrderList = NULL;\r
+      InsertTailList (&BootOptionMenu.Head, &NewMenuEntry->Link);\r
+      BootOptionMenu.MenuNumber++;\r
+\r
+      //\r
+      // Update "change driver order" page used data, append the new add driver\r
+      // option at the end.\r
+      //\r
+      Index = 0;\r
+      while (CallbackData->BmmFakeNvData.BootOptionOrder[Index] != 0) {\r
+        Index++;\r
+      }\r
+      CallbackData->BmmFakeNvData.BootOptionOrder[Index] = (UINT32) (NewMenuEntry->OptionNumber + 1);\r
 \r
-  if (BootOrderList != NULL) {\r
-    FreePool (BootOrderList);\r
+      NvRamMap->BootDescriptionData[0]  = 0x0000;\r
+      NvRamMap->BootOptionalData[0]     = 0x0000;\r
+    }\r
   }\r
-\r
-  Status = gRT->SetVariable (\r
-                  L"BootOrder",\r
-                  &gEfiGlobalVariableGuid,\r
-                  EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
-                  BootOrderListSize + sizeof (UINT16),\r
-                  NewBootOrderList\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  FreePool (NewBootOrderList);\r
-  NewBootOrderList = NULL;\r
-  InsertTailList (&BootOptionMenu.Head, &NewMenuEntry->Link);\r
-  BootOptionMenu.MenuNumber++;\r
-\r
-  NvRamMap->DescriptionData[0]  = 0x0000;\r
-  NvRamMap->OptionalData[0]     = 0x0000;\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -968,11 +1007,11 @@ Var_UpdateBootOrder (
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  ASSERT (BootOptionMenu.MenuNumber <= (sizeof (CallbackData->BmmFakeNvData.OptionOrder) / sizeof (CallbackData->BmmFakeNvData.OptionOrder[0])));\r
+  ASSERT (BootOptionMenu.MenuNumber <= (sizeof (CallbackData->BmmFakeNvData.BootOptionOrder) / sizeof (CallbackData->BmmFakeNvData.BootOptionOrder[0])));\r
 \r
-  for (OrderIndex = 0; (OrderIndex < BootOptionMenu.MenuNumber) && (CallbackData->BmmFakeNvData.OptionOrder[OrderIndex] != 0); OrderIndex++) {\r
+  for (OrderIndex = 0; (OrderIndex < BootOptionMenu.MenuNumber) && (CallbackData->BmmFakeNvData.BootOptionOrder[OrderIndex] != 0); OrderIndex++) {\r
     for (Index = OrderIndex; Index < BootOrderListSize / sizeof (UINT16); Index++) {\r
-      if ((BootOrderList[Index] == (UINT16) (CallbackData->BmmFakeNvData.OptionOrder[OrderIndex] - 1)) && (OrderIndex != Index)) {\r
+      if ((BootOrderList[Index] == (UINT16) (CallbackData->BmmFakeNvData.BootOptionOrder[OrderIndex] - 1)) && (OrderIndex != Index)) {\r
         OptionNumber = BootOrderList[Index];\r
         CopyMem (&BootOrderList[OrderIndex + 1], &BootOrderList[OrderIndex], (Index - OrderIndex) * sizeof (UINT16));\r
         BootOrderList[OrderIndex] = OptionNumber;\r
@@ -987,6 +1026,10 @@ Var_UpdateBootOrder (
                   BootOrderListSize,\r
                   BootOrderList\r
                   );\r
+  //\r
+  // Changing the content without increasing its size with current variable implementation shouldn't fail.\r
+  //\r
+  ASSERT_EFI_ERROR (Status);\r
   FreePool (BootOrderList);\r
 \r
   GroupMultipleLegacyBootOption4SameType ();\r
@@ -1046,9 +1089,9 @@ Var_UpdateDriverOrder (
     FreePool (DriverOrderList);\r
   }\r
 \r
-  ASSERT (DriverOptionMenu.MenuNumber <= (sizeof (CallbackData->BmmFakeNvData.OptionOrder) / sizeof (CallbackData->BmmFakeNvData.OptionOrder[0])));\r
+  ASSERT (DriverOptionMenu.MenuNumber <= (sizeof (CallbackData->BmmFakeNvData.DriverOptionOrder) / sizeof (CallbackData->BmmFakeNvData.DriverOptionOrder[0])));\r
   for (Index = 0; Index < DriverOptionMenu.MenuNumber; Index++) {\r
-    NewDriverOrderList[Index] = (UINT16) (CallbackData->BmmFakeNvData.OptionOrder[Index] - 1);\r
+    NewDriverOrderList[Index] = (UINT16) (CallbackData->BmmFakeNvData.DriverOptionOrder[Index] - 1);\r
   }\r
 \r
   Status = gRT->SetVariable (\r
@@ -1058,10 +1101,11 @@ Var_UpdateDriverOrder (
                   DriverOrderListSize,\r
                   NewDriverOrderList\r
                   );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
+  //\r
+  // Changing the content without increasing its size with current variable implementation shouldn't fail.\r
+  //\r
+  ASSERT_EFI_ERROR (Status);\r
+  \r
   BOpt_FreeMenu (&DriverOptionMenu);\r
   BOpt_GetDriverOptions (CallbackData);\r
   return EFI_SUCCESS;\r
@@ -1073,6 +1117,7 @@ Var_UpdateDriverOrder (
   is also updated.\r
 \r
   @param CallbackData    The context data for BMM.\r
+  @param FormId          The form id.\r
 \r
   @return EFI_SUCCESS           The function completed successfully.\r
   @retval EFI_NOT_FOUND         If VAR_LEGACY_DEV_ORDER and gEfiLegacyDevOrderVariableGuid EFI Variable can be found.\r
@@ -1080,7 +1125,8 @@ Var_UpdateDriverOrder (
 **/\r
 EFI_STATUS\r
 Var_UpdateBBSOption (\r
-  IN BMM_CALLBACK_DATA            *CallbackData\r
+  IN BMM_CALLBACK_DATA            *CallbackData,\r
+  IN EFI_FORM_ID                  FormId\r
   )\r
 {\r
   UINTN                       Index;\r
@@ -1109,7 +1155,7 @@ Var_UpdateBBSOption (
   DisMap              = NULL;\r
   NewOrder            = NULL;\r
 \r
-  switch (CallbackData->BmmPreviousPageId) {\r
+  switch (FormId) {\r
     case FORM_SET_FD_ORDER_ID:\r
       OptionMenu            = (BM_MENU_OPTION *) &LegacyFDMenu;\r
       LegacyDev             = CallbackData->BmmFakeNvData.LegacyFD;\r
@@ -1215,7 +1261,7 @@ Var_UpdateBBSOption (
   Status = gRT->SetVariable (\r
                   VAR_LEGACY_DEV_ORDER,\r
                   &gEfiLegacyDevOrderVariableGuid,\r
-                  EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+                  EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
                   VarSize,\r
                   OriginalPtr\r
                   );\r
@@ -1258,6 +1304,10 @@ Var_UpdateBBSOption (
                       OptionSize,\r
                       BootOptionVar\r
                       );\r
+      //\r
+      // Changing the content without increasing its size with current variable implementation shouldn't fail.\r
+      //\r
+      ASSERT_EFI_ERROR (Status);\r
 \r
       FreePool (BootOptionVar);\r
     }\r
@@ -1281,6 +1331,10 @@ Var_UpdateBBSOption (
                       OptionSize,\r
                       BootOptionVar\r
                       );\r
+      //\r
+      // Changing the content without increasing its size with current variable implementation shouldn't fail.\r
+      //\r
+      ASSERT_EFI_ERROR (Status);\r
 \r
       FreePool (BootOptionVar);\r
     }\r