]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/GenericBdsLib/BdsMisc.c
Parameter checking in BdsLibMatchDevicePaths() is wrong. This bug was found because...
[mirror_edk2.git] / MdeModulePkg / Library / GenericBdsLib / BdsMisc.c
index 084fa0de4ba15771d894781d9a62477af822a101..8b3dcd783b8d60b63124719d7c859a69dea8c93d 100644 (file)
@@ -17,8 +17,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #define MAX_STRING_LEN        200\r
 \r
-static BOOLEAN   mFeaturerSwitch = TRUE;\r
-static BOOLEAN   mResetRequired = FALSE;\r
+BOOLEAN   mFeaturerSwitch = TRUE;\r
+BOOLEAN   mResetRequired  = FALSE;\r
 \r
 extern UINT16 gPlatformBootTimeOutDefault;\r
 \r
@@ -45,31 +45,19 @@ BdsLibGetTimeout (
   //\r
   Size    = sizeof (UINT16);\r
   Status  = gRT->GetVariable (L"Timeout", &gEfiGlobalVariableGuid, NULL, &Size, &Timeout);\r
-  if (!EFI_ERROR (Status)) {\r
-    return Timeout;\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // According to UEFI 2.0 spec, it should treat the Timeout value as 0xffff\r
+    // (default value PcdPlatformBootTimeOutDefault) when L"Timeout" variable is not present.\r
+    // To make the current EFI Automatic-Test activity possible, platform can choose other value\r
+    // for automatic boot when the variable is not present.\r
+    //\r
+    Timeout = PcdGet16 (PcdPlatformBootTimeOutDefault);\r
   }\r
-  //\r
-  // To make the current EFI Automatic-Test activity possible, just add\r
-  // following code to make AutoBoot enabled when this variable is not\r
-  // present.\r
-  // This code should be removed later.\r
-  //\r
-  Timeout = PcdGet16 (PcdPlatformBootTimeOutDefault);\r
 \r
-  //\r
-  // Notes: Platform should set default variable if non exists on all error cases!!!\r
-  //\r
-  Status = gRT->SetVariable (\r
-                  L"Timeout",\r
-                  &gEfiGlobalVariableGuid,\r
-                  EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
-                  sizeof (UINT16),\r
-                  &Timeout\r
-                  );\r
   return Timeout;\r
 }\r
 \r
-\r
 /**\r
   The function will go through the driver optoin link list, load and start\r
   every driver the driver optoin device path point to.\r
@@ -99,6 +87,7 @@ BdsLibLoadDrivers (
   //\r
   for (Link = BdsDriverLists->ForwardLink; Link != BdsDriverLists; Link = Link->ForwardLink) {\r
     Option = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);\r
+    \r
     //\r
     // If a load option is not marked as LOAD_OPTION_ACTIVE,\r
     // the boot manager will not automatically load the option.\r
@@ -106,6 +95,7 @@ BdsLibLoadDrivers (
     if (!IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_ACTIVE)) {\r
       continue;\r
     }\r
+    \r
     //\r
     // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,\r
     // then all of the EFI drivers in the system will be disconnected and\r
@@ -114,6 +104,7 @@ BdsLibLoadDrivers (
     if (IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_FORCE_RECONNECT)) {\r
       ReconnectAll = TRUE;\r
     }\r
+    \r
     //\r
     // Make sure the driver path is connected.\r
     //\r
@@ -162,6 +153,7 @@ BdsLibLoadDrivers (
       gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
     }\r
   }\r
+  \r
   //\r
   // Process the LOAD_OPTION_FORCE_RECONNECT driver option\r
   //\r
@@ -172,10 +164,9 @@ BdsLibLoadDrivers (
 \r
 }\r
 \r
-\r
 /**\r
-  Get the Option Number that does not used\r
-  Try to locate the specific option variable one by one untile find a free number\r
+  Get the Option Number that does not used.\r
+  Try to locate the specific option variable one by one untile find a free number.\r
 \r
   @param  VariableName          Indicate if the boot#### or driver#### option\r
 \r
@@ -187,7 +178,6 @@ BdsLibGetFreeOptionNumber (
   IN  CHAR16    *VariableName\r
   )\r
 {\r
-  UINT16        Number;\r
   UINTN         Index;\r
   CHAR16        StrTemp[10];\r
   UINT16        *OptionBuffer;\r
@@ -207,18 +197,17 @@ BdsLibGetFreeOptionNumber (
     // try if the option number is used\r
     //\r
     OptionBuffer = BdsLibGetVariableAndSize (\r
-              StrTemp,\r
-              &gEfiGlobalVariableGuid,\r
-              &OptionSize\r
-              );\r
+                     StrTemp,\r
+                     &gEfiGlobalVariableGuid,\r
+                     &OptionSize\r
+                     );\r
     if (OptionBuffer == NULL) {\r
       break;\r
     }\r
-    Index++;\r
-  } while (1);\r
+    Index ++;\r
+  } while (TRUE);\r
 \r
-  Number = (UINT16) Index;\r
-  return Number;\r
+  return ((UINT16) Index);\r
 }\r
 \r
 \r
@@ -272,6 +261,7 @@ BdsLibRegisterNewOption (
   Description           = NULL;\r
   OptionOrderPtr        = NULL;\r
   UpdateDescription     = FALSE;\r
+  Status                = EFI_SUCCESS;\r
   ZeroMem (OptionName, sizeof (OptionName));\r
 \r
   TempOptionSize = 0;\r
@@ -300,11 +290,11 @@ BdsLibRegisterNewOption (
     if (OptionPtr == NULL) {\r
       continue;\r
     }\r
-    TempPtr = OptionPtr;\r
-    TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
-    Description = (CHAR16 *) TempPtr;\r
-    TempPtr += StrSize ((CHAR16 *) TempPtr);\r
-    OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
+    TempPtr         =   OptionPtr;\r
+    TempPtr         +=  sizeof (UINT32) + sizeof (UINT16);\r
+    Description     =   (CHAR16 *) TempPtr;\r
+    TempPtr         +=  StrSize ((CHAR16 *) TempPtr);\r
+    OptionDevicePath =  (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
 \r
     //\r
     // Notes: the description may will change base on the GetStringToken\r
@@ -314,32 +304,34 @@ BdsLibRegisterNewOption (
         //\r
         // Got the option, so just return\r
         //\r
-        gBS->FreePool (OptionPtr);\r
-        gBS->FreePool (TempOptionPtr);\r
+        FreePool (OptionPtr);\r
+        FreePool (TempOptionPtr);\r
         return EFI_SUCCESS;\r
       } else {\r
         //\r
         // Option description changed, need update.\r
         //\r
         UpdateDescription = TRUE;\r
-        gBS->FreePool (OptionPtr);\r
+        FreePool (OptionPtr);\r
         break;\r
       }\r
     }\r
 \r
-    gBS->FreePool (OptionPtr);\r
+    FreePool (OptionPtr);\r
   }\r
 \r
   OptionSize          = sizeof (UINT32) + sizeof (UINT16) + StrSize (String);\r
-  OptionSize += GetDevicePathSize (DevicePath);\r
+  OptionSize          += GetDevicePathSize (DevicePath);\r
   OptionPtr           = AllocateZeroPool (OptionSize);\r
+  ASSERT (OptionPtr != NULL);\r
+  \r
   TempPtr             = OptionPtr;\r
   *(UINT32 *) TempPtr = LOAD_OPTION_ACTIVE;\r
-  TempPtr += sizeof (UINT32);\r
+  TempPtr             += sizeof (UINT32);\r
   *(UINT16 *) TempPtr = (UINT16) GetDevicePathSize (DevicePath);\r
-  TempPtr += sizeof (UINT16);\r
+  TempPtr             += sizeof (UINT16);\r
   CopyMem (TempPtr, String, StrSize (String));\r
-  TempPtr += StrSize (String);\r
+  TempPtr             += StrSize (String);\r
   CopyMem (TempPtr, DevicePath, GetDevicePathSize (DevicePath));\r
 \r
   if (UpdateDescription) {\r
@@ -371,12 +363,14 @@ BdsLibRegisterNewOption (
   // Return if only need to update a changed description or fail to set option.\r
   //\r
   if (EFI_ERROR (Status) || UpdateDescription) {\r
-    gBS->FreePool (OptionPtr);\r
-    gBS->FreePool (TempOptionPtr);\r
+    FreePool (OptionPtr);\r
+    if (TempOptionPtr != NULL) {\r
+      FreePool (TempOptionPtr);\r
+    }\r
     return Status;\r
   }\r
 \r
-  gBS->FreePool (OptionPtr);\r
+  FreePool (OptionPtr);\r
 \r
   //\r
   // Update the option order variable\r
@@ -394,11 +388,10 @@ BdsLibRegisterNewOption (
                     sizeof (UINT16),\r
                     &BootOrderEntry\r
                     );\r
-    if (EFI_ERROR (Status)) {\r
-      gBS->FreePool (TempOptionPtr);\r
-      return Status;\r
+    if (TempOptionPtr != NULL) {\r
+      FreePool (TempOptionPtr);\r
     }\r
-    return EFI_SUCCESS;\r
+    return Status;\r
   }\r
 \r
   //\r
@@ -406,6 +399,8 @@ BdsLibRegisterNewOption (
   //\r
   OrderItemNum = (TempOptionSize / sizeof (UINT16)) + 1 ;\r
   OptionOrderPtr = AllocateZeroPool ( OrderItemNum * sizeof (UINT16));\r
+  ASSERT (OptionOrderPtr!= NULL);\r
+  \r
   CopyMem (OptionOrderPtr, TempOptionPtr, (OrderItemNum - 1) * sizeof (UINT16));\r
 \r
   OptionOrderPtr[Index] = RegisterOptionNumber;\r
@@ -417,22 +412,16 @@ BdsLibRegisterNewOption (
                   OrderItemNum * sizeof (UINT16),\r
                   OptionOrderPtr\r
                   );\r
-  if (EFI_ERROR (Status)) {\r
-    gBS->FreePool (TempOptionPtr);\r
-    gBS->FreePool (OptionOrderPtr);\r
-    return Status;\r
-  }\r
+  FreePool (TempOptionPtr);\r
+  FreePool (OptionOrderPtr);\r
 \r
-  gBS->FreePool (TempOptionPtr);\r
-  gBS->FreePool (OptionOrderPtr);\r
-\r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 \r
 /**\r
   Build the boot#### or driver#### option from the VariableName, the\r
-  build boot#### or driver#### will also be linked to BdsCommonOptionList\r
+  build boot#### or driver#### will also be linked to BdsCommonOptionList.\r
 \r
   @param  BdsCommonOptionList   The header of the boot#### or driver#### option\r
                                 link list\r
@@ -479,15 +468,15 @@ BdsLibVariableToOption (
   //\r
   // Get the option attribute\r
   //\r
-  TempPtr   = Variable;\r
-  Attribute = *(UINT32 *) Variable;\r
-  TempPtr += sizeof (UINT32);\r
+  TempPtr   =  Variable;\r
+  Attribute =  *(UINT32 *) Variable;\r
+  TempPtr   += sizeof (UINT32);\r
 \r
   //\r
   // Get the option's device path size\r
   //\r
-  FilePathSize = *(UINT16 *) TempPtr;\r
-  TempPtr += sizeof (UINT16);\r
+  FilePathSize =  *(UINT16 *) TempPtr;\r
+  TempPtr      += sizeof (UINT16);\r
 \r
   //\r
   // Get the option's description string\r
@@ -497,13 +486,13 @@ BdsLibVariableToOption (
   //\r
   // Get the option's description string size\r
   //\r
-  TempPtr += StrSize ((CHAR16 *) TempPtr);\r
+  TempPtr     += StrSize ((CHAR16 *) TempPtr);\r
 \r
   //\r
   // Get the option's device path\r
   //\r
-  DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
-  TempPtr += FilePathSize;\r
+  DevicePath =  (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
+  TempPtr    += FilePathSize;\r
 \r
   LoadOptions     = TempPtr;\r
   LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable));\r
@@ -544,17 +533,16 @@ BdsLibVariableToOption (
   //\r
   if ((Option->Attribute & LOAD_OPTION_ACTIVE) == LOAD_OPTION_ACTIVE) {\r
     InsertTailList (BdsCommonOptionList, &Option->Link);\r
-    gBS->FreePool (Variable);\r
+    FreePool (Variable);\r
     return Option;\r
   }\r
 \r
-  gBS->FreePool (Variable);\r
-  gBS->FreePool (Option);\r
+  FreePool (Variable);\r
+  FreePool (Option);\r
   return NULL;\r
 \r
 }\r
 \r
-\r
 /**\r
   Process BootOrder, or DriverOrder variables, by calling\r
   BdsLibVariableToOption () for each UINT16 in the variables.\r
@@ -611,12 +599,11 @@ BdsLibBuildOptionFromVar (
 \r
   }\r
 \r
-  gBS->FreePool (OptionOrder);\r
+  FreePool (OptionOrder);\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Get boot mode by looking up configuration table and parsing HOB list\r
 \r
@@ -636,7 +623,6 @@ BdsLibGetBootMode (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Read the EFI variable (VendorGuid/Name) and return a dynamically allocated\r
   buffer, and the size of the buffer. If failure return NULL.\r
@@ -690,7 +676,6 @@ BdsLibGetVariableAndSize (
   return Buffer;\r
 }\r
 \r
-\r
 /**\r
   Delete the instance in Multi which matches partly with Single instance\r
 \r
@@ -739,9 +724,11 @@ BdsLibDelPartMatchInstance (
       //\r
       TempNewDevicePath = NewDevicePath;\r
       NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance);\r
-      SafeFreePool(TempNewDevicePath);\r
+      if (TempNewDevicePath != NULL) {\r
+        FreePool(TempNewDevicePath);\r
+      }\r
     }\r
-    SafeFreePool(Instance);\r
+    FreePool(Instance);\r
     Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
     InstanceSize  -= END_DEVICE_PATH_LENGTH;\r
   }\r
@@ -749,7 +736,6 @@ BdsLibDelPartMatchInstance (
   return NewDevicePath;\r
 }\r
 \r
-\r
 /**\r
   Function compares a device path data structure to that of all the nodes of a\r
   second device path instance.\r
@@ -774,7 +760,7 @@ BdsLibMatchDevicePaths (
   EFI_DEVICE_PATH_PROTOCOL  *DevicePathInst;\r
   UINTN                     Size;\r
 \r
-  if (!Multi || !Single) {\r
+  if (Multi == NULL || Single  == NULL) {\r
     return FALSE;\r
   }\r
 \r
@@ -790,18 +776,17 @@ BdsLibMatchDevicePaths (
     // return success\r
     //\r
     if (CompareMem (Single, DevicePathInst, Size) == 0) {\r
-      gBS->FreePool (DevicePathInst);\r
+      FreePool (DevicePathInst);\r
       return TRUE;\r
     }\r
 \r
-    gBS->FreePool (DevicePathInst);\r
+    FreePool (DevicePathInst);\r
     DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
   }\r
 \r
   return FALSE;\r
 }\r
 \r
-\r
 /**\r
   This function prints a series of strings.\r
 \r
@@ -820,19 +805,19 @@ BdsLibOutputStrings (
   ...\r
   )\r
 {\r
-  VA_LIST     args;\r
+  VA_LIST     Args;\r
   EFI_STATUS  Status;\r
   CHAR16      *String;\r
 \r
   Status = EFI_SUCCESS;\r
-  VA_START (args, ConOut);\r
+  VA_START (Args, ConOut);\r
 \r
   while (!EFI_ERROR (Status)) {\r
     //\r
     // If String is NULL, then it's the end of the list\r
     //\r
-    String = VA_ARG (args, CHAR16 *);\r
-    if (!String) {\r
+    String = VA_ARG (Args, CHAR16 *);\r
+    if (String != NULL) {\r
       break;\r
     }\r
 \r
@@ -977,8 +962,8 @@ SetupResetReminder (
         IfrLibCreatePopUp (2, &Key, StringBuffer1, StringBuffer2);\r
       } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
 \r
-      gBS->FreePool (StringBuffer1);\r
-      gBS->FreePool (StringBuffer2);\r
+      FreePool (StringBuffer1);\r
+      FreePool (StringBuffer2);\r
       //\r
       // If the user hits the YES Response key, reset\r
       //\r
@@ -990,15 +975,13 @@ SetupResetReminder (
   }\r
 }\r
 \r
-\r
 /**\r
-  Get the headers (dos, image, optional header) from an image\r
+  Get the headers (dos, image, optional header) from an image.\r
 \r
   @param  Device                SimpleFileSystem device handle\r
   @param  FileName              File name for the image\r
   @param  DosHeader             Pointer to dos header\r
-  @param  ImageHeader           Pointer to image header\r
-  @param  OptionalHeader        Pointer to optional header\r
+  @param  Hdr                   Pointer to optional header\r
 \r
   @retval EFI_SUCCESS           Successfully get the machine type.\r
   @retval EFI_NOT_FOUND         The file is not found.\r
@@ -1041,6 +1024,7 @@ BdsLibGetImageHeader (
                      &Root\r
                      );\r
   if (EFI_ERROR (Status)) {\r
+    Root = NULL;\r
     goto Done;\r
   }\r
 \r
@@ -1069,13 +1053,14 @@ BdsLibGetImageHeader (
       break;\r
     }\r
     if (Status != EFI_BUFFER_TOO_SMALL) {\r
+      FreePool (Info);\r
       goto Done;\r
     }\r
-    gBS->FreePool (Info);\r
+    FreePool (Info);\r
   } while (TRUE);\r
 \r
   FileSize = Info->FileSize;\r
-  gBS->FreePool (Info);\r
+  FreePool (Info);\r
 \r
   //\r
   // Read dos header\r
@@ -1130,30 +1115,23 @@ BdsLibGetImageHeader (
   return Status;\r
 }\r
 \r
-VOID\r
-EFIAPI\r
-BdsSetMemoryTypeInformationVariable (\r
-  EFI_EVENT  Event,\r
-  VOID       *Context\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   This routine is a notification function for legayc boot or exit boot\r
   service event. It will adjust the memory information for different\r
-  memory type and save them into the variables for next boot\r
-\r
-Arguments:\r
+  memory type and save them into the variables for next boot.\r
 \r
-  Event    - The event that triggered this notification function\r
-  Context  - Pointer to the notification functions context\r
 \r
-Returns:\r
+  @param Event           The event that triggered this notification function.\r
+  @param Context         Pointer to the notification functions context.\r
 \r
-  None.\r
-\r
---*/\r
+**/\r
+VOID\r
+EFIAPI\r
+BdsSetMemoryTypeInformationVariable (\r
+  EFI_EVENT  Event,\r
+  VOID       *Context\r
+  )\r
 {\r
   EFI_STATUS                   Status;\r
   EFI_MEMORY_TYPE_INFORMATION  *PreviousMemoryTypeInformation;\r
@@ -1250,10 +1228,9 @@ Returns:
   return;\r
 }\r
 \r
-\r
 /**\r
   This routine register a function to adjust the different type memory page number just before booting\r
-  and save the updated info into the variable for next boot to use\r
+  and save the updated info into the variable for next boot to use.\r
 \r
 **/\r
 VOID\r