]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c
Fix memory leak issues.
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / GenericBdsLib / BdsMisc.c
index 3d2e199e52e86123f6ef8b0dc6f6f995d9957668..ada77d26ded416c8ad455a573f680bf5c29c7596 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Misc BDS library function\r
 \r
-Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2012, 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
@@ -257,6 +257,14 @@ BdsLibRegisterNewOption (
     if (OptionPtr == NULL) {\r
       continue;\r
     }\r
+\r
+    //\r
+    // Validate the variable.\r
+    //\r
+    if (!ValidateOption(OptionPtr, OptionSize)) {\r
+      continue;\r
+    }\r
+\r
     TempPtr         =   OptionPtr;\r
     TempPtr         +=  sizeof (UINT32) + sizeof (UINT16);\r
     Description     =   (CHAR16 *) TempPtr;\r
@@ -390,6 +398,189 @@ BdsLibRegisterNewOption (
   return Status;\r
 }\r
 \r
+/**\r
+  Returns the size of a device path in bytes.\r
+\r
+  This function returns the size, in bytes, of the device path data structure \r
+  specified by DevicePath including the end of device path node. If DevicePath \r
+  is NULL, then 0 is returned. If the length of the device path is bigger than\r
+  MaxSize, also return 0 to indicate this is an invalidate device path.\r
+\r
+  @param  DevicePath         A pointer to a device path data structure.\r
+  @param  MaxSize            Max valid device path size. If big than this size, \r
+                             return error.\r
+  \r
+  @retval 0                  An invalid device path.\r
+  @retval Others             The size of a device path in bytes.\r
+\r
+**/\r
+UINTN\r
+GetDevicePathSizeEx (\r
+  IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath,\r
+  IN UINTN                           MaxSize\r
+  )\r
+{\r
+  UINTN  Size;\r
+  UINTN  NodeSize;\r
+\r
+  if (DevicePath == NULL) {\r
+    return 0;\r
+  }\r
+\r
+  //\r
+  // Search for the end of the device path structure\r
+  //\r
+  Size = 0;\r
+  while (!IsDevicePathEnd (DevicePath)) {\r
+    NodeSize = DevicePathNodeLength (DevicePath);\r
+    if (NodeSize < END_DEVICE_PATH_LENGTH) {\r
+      return 0;\r
+    }\r
+    Size += NodeSize;\r
+    if (Size > MaxSize) {\r
+      return 0;\r
+    }\r
+    DevicePath = NextDevicePathNode (DevicePath);\r
+  }\r
+  Size += DevicePathNodeLength (DevicePath);\r
+  if (Size > MaxSize) {\r
+    return 0;\r
+  }\r
+\r
+  return Size;\r
+}\r
+\r
+/**\r
+  Returns the length of a Null-terminated Unicode string. If the length is \r
+  bigger than MaxStringLen, return length 0 to indicate that this is an \r
+  invalidate string.\r
+\r
+  This function returns the number of Unicode characters in the Null-terminated\r
+  Unicode string specified by String. \r
+\r
+  If String is NULL, then ASSERT().\r
+  If String is not aligned on a 16-bit boundary, then ASSERT().\r
+\r
+  @param  String           A pointer to a Null-terminated Unicode string.\r
+  @param  MaxStringLen     Max string len in this string.\r
+\r
+  @retval 0                An invalid string.\r
+  @retval Others           The length of String.\r
+\r
+**/\r
+UINTN\r
+StrSizeEx (\r
+  IN      CONST CHAR16              *String,\r
+  IN      UINTN                     MaxStringLen\r
+  )\r
+{\r
+  UINTN                             Length;\r
+\r
+  ASSERT (String != NULL && MaxStringLen != 0);\r
+  ASSERT (((UINTN) String & BIT0) == 0);\r
+\r
+  for (Length = 0; *String != L'\0' && MaxStringLen != Length; String++, Length++);\r
+\r
+  if (*String != L'\0' && MaxStringLen == Length) {\r
+    return 0;\r
+  }\r
+\r
+  return (Length + 1) * sizeof (*String);\r
+}\r
+\r
+/**\r
+  Validate the EFI Boot#### variable (VendorGuid/Name)\r
+\r
+  @param  Variable              Boot#### variable data.\r
+  @param  VariableSize          Returns the size of the EFI variable that was read\r
+\r
+  @retval TRUE                  The variable data is correct.\r
+  @retval FALSE                 The variable data is corrupted.\r
+\r
+**/\r
+BOOLEAN \r
+ValidateOption (\r
+  UINT8                     *Variable,\r
+  UINTN                     VariableSize\r
+  )\r
+{\r
+  UINT16                    FilePathSize;\r
+  UINT8                     *TempPtr;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *TempPath;\r
+  UINTN                     TempSize;\r
+\r
+  //\r
+  // Skip the option attribute\r
+  //\r
+  TempPtr    = 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
+\r
+  //\r
+  // Get the option's description string size\r
+  //\r
+  TempSize = StrSizeEx ((CHAR16 *) TempPtr, VariableSize);\r
+  TempPtr += TempSize;\r
+\r
+  //\r
+  // Get the option's device path\r
+  //\r
+  DevicePath =  (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
+  TempPtr    += FilePathSize;\r
+\r
+  //\r
+  // Validation boot option variable.\r
+  //\r
+  if ((FilePathSize == 0) || (TempSize == 0)) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (TempSize + FilePathSize + sizeof (UINT16) + sizeof (UINT16) > VariableSize) {\r
+    return FALSE;\r
+  }\r
+\r
+  TempPath = DevicePath;\r
+  while (FilePathSize > 0) {\r
+    TempSize = GetDevicePathSizeEx (TempPath, FilePathSize);\r
+    if (TempSize == 0) {\r
+      return FALSE;\r
+    }\r
+    FilePathSize = (UINT16) (FilePathSize - TempSize);\r
+    TempPath    += TempSize;\r
+  }\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Convert a single character to number.\r
+  It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'\r
+  \r
+  @param Char    The input char which need to change to a hex number.\r
+  \r
+**/\r
+UINTN\r
+CharToUint (\r
+  IN CHAR16                           Char\r
+  )\r
+{\r
+  if ((Char >= L'0') && (Char <= L'9')) {\r
+    return (UINTN) (Char - L'0');\r
+  }\r
+\r
+  if ((Char >= L'A') && (Char <= L'F')) {\r
+    return (UINTN) (Char - L'A' + 0xA);\r
+  }\r
+\r
+  ASSERT (FALSE);\r
+  return 0;\r
+}\r
 \r
 /**\r
   Build the boot#### or driver#### option from the VariableName, the\r
@@ -417,11 +608,13 @@ BdsLibVariableToOption (
   UINT8                     *TempPtr;\r
   UINTN                     VariableSize;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *TempPath;\r
   BDS_COMMON_OPTION         *Option;\r
   VOID                      *LoadOptions;\r
   UINT32                    LoadOptionsSize;\r
   CHAR16                    *Description;\r
   UINT8                     NumOff;\r
+  UINTN                     TempSize;\r
   //\r
   // Read the variable. We will never free this data.\r
   //\r
@@ -433,6 +626,14 @@ BdsLibVariableToOption (
   if (Variable == NULL) {\r
     return NULL;\r
   }\r
+\r
+  //\r
+  // Validate Boot#### variable data.\r
+  //\r
+  if (!ValidateOption(Variable, VariableSize)) {\r
+    return NULL;\r
+  }\r
+\r
   //\r
   // Notes: careful defined the variable of Boot#### or\r
   // Driver####, consider use some macro to abstract the code\r
@@ -458,7 +659,11 @@ BdsLibVariableToOption (
   //\r
   // Get the option's description string size\r
   //\r
-  TempPtr     += StrSize ((CHAR16 *) TempPtr);\r
+  TempSize = StrSizeEx ((CHAR16 *) TempPtr, VariableSize);\r
+  if (TempSize == 0) {\r
+    return NULL;\r
+  }\r
+  TempPtr += TempSize;\r
 \r
   //\r
   // Get the option's device path\r
@@ -466,7 +671,26 @@ BdsLibVariableToOption (
   DevicePath =  (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
   TempPtr    += FilePathSize;\r
 \r
+  //\r
+  // Validation device path.\r
+  //\r
+  TempPath       = DevicePath;\r
+  while (FilePathSize > 0) {\r
+    TempSize = GetDevicePathSizeEx (TempPath, FilePathSize);\r
+    if (TempSize == 0) {\r
+      return NULL;\r
+    }\r
+    FilePathSize = (UINT16) (FilePathSize - TempSize);\r
+    TempPath    += TempSize;\r
+  }\r
+\r
+  //\r
+  // Get load opion data.\r
+  //\r
   LoadOptions     = TempPtr;\r
+  if (VariableSize < (UINTN)(TempPtr - Variable)) {\r
+    return NULL;\r
+  }\r
   LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable));\r
 \r
   //\r
@@ -499,11 +723,11 @@ BdsLibVariableToOption (
   // Unicode stream to ASCII without any loss in meaning.\r
   //\r
   if (*VariableName == 'B') {\r
-    NumOff = (UINT8) (sizeof (L"Boot") / sizeof(CHAR16) - 1);\r
-    Option->BootCurrent = (UINT16) ((VariableName[NumOff]  -'0') * 0x1000);\r
-    Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+1]-'0') * 0x100));\r
-    Option->BootCurrent = (UINT16) (Option->BootCurrent +  ((VariableName[NumOff+2]-'0') * 0x10));\r
-    Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+3]-'0')));\r
+    NumOff = (UINT8) (sizeof (L"Boot") / sizeof (CHAR16) - 1);\r
+    Option->BootCurrent = (UINT16) (CharToUint (VariableName[NumOff+0]) * 0x1000) \r
+               + (UINT16) (CharToUint (VariableName[NumOff+1]) * 0x100)\r
+               + (UINT16) (CharToUint (VariableName[NumOff+2]) * 0x10)\r
+               + (UINT16) (CharToUint (VariableName[NumOff+3]) * 0x1);\r
   }\r
   //\r
   // Insert active entry to BdsDeviceList\r
@@ -515,9 +739,11 @@ BdsLibVariableToOption (
   }\r
 \r
   FreePool (Variable);\r
+  FreePool (Option->Description);\r
+  FreePool (Option->DevicePath);\r
+  FreePool (Option->LoadOptions);\r
   FreePool (Option);\r
   return NULL;\r
-\r
 }\r
 \r
 /**\r
@@ -946,7 +1172,7 @@ SetupResetReminder (
       //\r
       // If the user hits the YES Response key, reset\r
       //\r
-      if ((Key.UnicodeChar == CHAR_CARRIAGE_RETURN)) {\r
+      if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
         gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r
       }\r
       gST->ConOut->ClearScreen (gST->ConOut);\r
@@ -1096,21 +1322,14 @@ BdsLibGetImageHeader (
 }\r
 \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
-\r
-  @param Event           The event that triggered this notification function.\r
-  @param Context         Pointer to the notification functions context.\r
-\r
+  This routine adjusts the memory information for different memory type and \r
+  saves them into the variables for next boot. It conditionally resets the\r
+  system when the memory information changes. Platform can reserve memory \r
+  large enough (125% of actual requirement) to avoid the reset in the first boot.\r
 **/\r
 VOID\r
-EFIAPI\r
 BdsSetMemoryTypeInformationVariable (\r
-  EFI_EVENT  Event,\r
-  VOID       *Context\r
+  VOID\r
   )\r
 {\r
   EFI_STATUS                   Status;\r
@@ -1125,16 +1344,26 @@ BdsSetMemoryTypeInformationVariable (
   EFI_HOB_GUID_TYPE            *GuidHob;\r
   BOOLEAN                      MemoryTypeInformationModified;\r
   BOOLEAN                      MemoryTypeInformationVariableExists;\r
+  EFI_BOOT_MODE                BootMode;\r
 \r
   MemoryTypeInformationModified       = FALSE;\r
   MemoryTypeInformationVariableExists = FALSE;\r
 \r
+\r
+  BootMode = GetBootModeHob ();\r
+  //\r
+  // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.\r
   //\r
-  // Only get the the Memory Type Information variable in the boot mode \r
+  if (BootMode == BOOT_IN_RECOVERY_MODE) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // Only check the the Memory Type Information variable in the boot mode \r
   // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type\r
   // Information is not valid in this boot mode.\r
   //\r
-  if (GetBootModeHob () != BOOT_WITH_DEFAULT_SETTINGS) {\r
+  if (BootMode != BOOT_WITH_DEFAULT_SETTINGS) {\r
     VariableSize = 0;\r
     Status = gRT->GetVariable (\r
                     EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
@@ -1184,29 +1413,34 @@ BdsSetMemoryTypeInformationVariable (
 \r
   for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
 \r
-    Current = 0;\r
     for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {\r
       if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {\r
-        Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;\r
         break;\r
       }\r
     }\r
-\r
     if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {\r
       continue;\r
     }\r
 \r
+    //\r
+    // Previous is the number of pages pre-allocated\r
+    // Current is the number of pages actually needed\r
+    //\r
     Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;\r
+    Current  = CurrentMemoryTypeInformation[Index1].NumberOfPages;\r
+    Next     = Previous;\r
 \r
     //\r
     // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail\r
     //\r
-    if (!MemoryTypeInformationVariableExists && Current < Previous) {\r
-      Next = Current + (Current >> 2);\r
+    if (Current < Previous) {\r
+      if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {\r
+        Next = Current + (Current >> 2);\r
+      } else if (!MemoryTypeInformationVariableExists) {\r
+        Next = MAX (Current + (Current >> 2), Previous);\r
+      }\r
     } else if (Current > Previous) {\r
       Next = Current + (Current >> 2);\r
-    } else {\r
-      Next = Previous;\r
     }\r
     if (Next > 0 && Next < 4) {\r
       Next = 4;\r
@@ -1238,17 +1472,15 @@ BdsSetMemoryTypeInformationVariable (
     // so the new Memory Type Information setting will be used to guarantee that an S4\r
     // entry/resume cycle will not fail.\r
     //\r
-    if (MemoryTypeInformationModified) {\r
-      DEBUG ((EFI_D_ERROR, "Memory Type Information settings change. Warm Reset!!!\n"));\r
+    if (MemoryTypeInformationModified && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {\r
+      DEBUG ((EFI_D_INFO, "Memory Type Information settings change. Warm Reset!!!\n"));\r
       gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
     }\r
   }\r
 }\r
 \r
 /**\r
-  This routine register a function to adjust the different type memory page number\r
-  just before booting and save the updated info into the variable for next boot to use.\r
-\r
+  This routine is kept for backward compatibility.\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -1256,18 +1488,6 @@ BdsLibSaveMemoryTypeInformation (
   VOID\r
   )\r
 {\r
-  EFI_STATUS                   Status;\r
-  EFI_EVENT                    ReadyToBootEvent;\r
-\r
-  Status = EfiCreateEventReadyToBootEx (\r
-           TPL_CALLBACK,\r
-           BdsSetMemoryTypeInformationVariable,\r
-           NULL,\r
-           &ReadyToBootEvent\r
-           );\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((DEBUG_ERROR,"Bds Set Memory Type Informationa Variable Fails\n"));\r
-  }\r
 }\r
 \r
 \r