]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellInstall1CommandsLib/Bcfg.c
Refine code to make it more safely.
[mirror_edk2.git] / ShellPkg / Library / UefiShellInstall1CommandsLib / Bcfg.c
index 5dcbfb6950e0f8cc70a5267cc5a09610fd61c064..a7893f5eeede79f85e938ca467b8611e49eaa816 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Main file for bcfg shell Install1 function.\r
 \r
-  Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2010 - 2014, 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
@@ -47,16 +47,6 @@ typedef struct {
   CONST CHAR16          *OptData;\r
 } BGFG_OPERATION;\r
 \r
-/**\r
-  Get the actual number of entries in EFI_KEY_OPTION.Keys, from 0-3.\r
-\r
-  @param   KeyOption  Pointer to the EFI_KEY_OPTION structure. \r
-\r
-  @return  Actual number of entries in EFI_KEY_OPTION.Keys.\r
-**/\r
-#define KEY_OPTION_INPUT_KEY_COUNT(KeyOption) \\r
-  (((KeyOption)->KeyData & EFI_KEY_OPTION_INPUT_KEY_COUNT_MASK) >> LowBitSet32 (EFI_KEY_OPTION_INPUT_KEY_COUNT_MASK))\r
-\r
 /**\r
   Update the optional data for a boot or driver option.\r
 \r
@@ -523,6 +513,7 @@ BcfgAddInstall1(
 \r
       CopyMem (TempByteBuffer, Desc, DescSize);\r
       TempByteBuffer += DescSize;\r
+      ASSERT (FilePath != NULL);\r
       CopyMem (TempByteBuffer, FilePath, FilePathSize);\r
 \r
       UnicodeSPrint (OptionStr, sizeof(OptionStr), L"%s%04x", Target == BcfgTargetBootOrder?L"Boot":L"Driver", TargetLocation);\r
@@ -840,7 +831,7 @@ BcfgAddOptInstall1(
           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellInstall1HiiHandle, Walker);\r
           ShellStatus = SHELL_INVALID_PARAMETER;\r
         }\r
-        NewKeyOption.KeyData = (UINT32)Intermediate;\r
+        NewKeyOption.KeyData.PackedValue = (UINT32)Intermediate;\r
         Temp = StrStr(Walker, L" ");\r
         if (Temp != NULL) {\r
           Walker = Temp;\r
@@ -855,13 +846,13 @@ BcfgAddOptInstall1(
         // Now we know how many EFI_INPUT_KEY structs we need to attach to the end of the EFI_KEY_OPTION struct.  \r
         // Re-allocate with the added information.\r
         //\r
-        KeyOptionBuffer = AllocateCopyPool(sizeof(EFI_KEY_OPTION) + (sizeof(EFI_INPUT_KEY) * KEY_OPTION_INPUT_KEY_COUNT (&NewKeyOption)), &NewKeyOption);\r
+        KeyOptionBuffer = AllocateCopyPool(sizeof(EFI_KEY_OPTION) + (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount), &NewKeyOption);\r
         if (KeyOptionBuffer == NULL) {\r
           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellInstall1HiiHandle);\r
           ShellStatus = SHELL_OUT_OF_RESOURCES;\r
         }\r
       }\r
-      for (LoopCounter = 0 ; ShellStatus == SHELL_SUCCESS && LoopCounter < KEY_OPTION_INPUT_KEY_COUNT (&NewKeyOption); LoopCounter++) {\r
+      for (LoopCounter = 0 ; ShellStatus == SHELL_SUCCESS && LoopCounter < NewKeyOption.KeyData.Options.InputKeyCount; LoopCounter++) {\r
         //\r
         // ScanCode\r
         //\r
@@ -927,7 +918,7 @@ BcfgAddOptInstall1(
           VariableName,\r
           (EFI_GUID*)&gEfiGlobalVariableGuid,\r
           EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS,\r
-          sizeof(EFI_KEY_OPTION) + (sizeof(EFI_INPUT_KEY) * KEY_OPTION_INPUT_KEY_COUNT (&NewKeyOption)),\r
+          sizeof(EFI_KEY_OPTION) + (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount),\r
           KeyOptionBuffer);\r
         if (EFI_ERROR(Status)) {\r
           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_BCFG_SET_VAR_FAIL), gShellInstall1HiiHandle, VariableName, Status);\r
@@ -1049,8 +1040,12 @@ BcfgDisplayDumpInstall1(
 \r
     if ((*(UINT16*)(Buffer+4)) != 0) {\r
       DevPath = AllocateZeroPool(*(UINT16*)(Buffer+4));\r
-      CopyMem(DevPath, Buffer+6+StrSize((CHAR16*)(Buffer+6)), *(UINT16*)(Buffer+4));\r
-      DevPathString = gDevPathToText->ConvertDevicePathToText(DevPath, TRUE, FALSE);\r
+      if (DevPath == NULL) {\r
+        DevPathString = NULL;\r
+      } else {\r
+        CopyMem(DevPath, Buffer+6+StrSize((CHAR16*)(Buffer+6)), *(UINT16*)(Buffer+4));\r
+        DevPathString = ConvertDevicePathToText(DevPath, TRUE, FALSE);\r
+      }\r
     } else {\r
       DevPath       = NULL;\r
       DevPathString = NULL;\r
@@ -1220,12 +1215,16 @@ ShellCommandRunBcfgInstall (
         CurrentOperation.Order);\r
       if (Status == EFI_BUFFER_TOO_SMALL) {\r
         CurrentOperation.Order = AllocateZeroPool(Length+(4*sizeof(CurrentOperation.Order[0])));\r
-        Status = gRT->GetVariable(\r
-          CurrentOperation.Target == BcfgTargetBootOrder?(CHAR16*)L"BootOrder":(CHAR16*)L"DriverOrder",\r
-          (EFI_GUID*)&gEfiGlobalVariableGuid,\r
-          NULL,\r
-          &Length,\r
-          CurrentOperation.Order);\r
+        if (CurrentOperation.Order == NULL) {\r
+          ShellStatus = SHELL_OUT_OF_RESOURCES;\r
+        } else {\r
+          Status = gRT->GetVariable(\r
+            CurrentOperation.Target == BcfgTargetBootOrder?(CHAR16*)L"BootOrder":(CHAR16*)L"DriverOrder",\r
+            (EFI_GUID*)&gEfiGlobalVariableGuid,\r
+            NULL,\r
+            &Length,\r
+            CurrentOperation.Order);\r
+        }\r
       }\r
     }\r
 \r