]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
MdeModulePkg/UefiBootManagerLib: Fix small LoadOptionToVariable leak
[mirror_edk2.git] / MdeModulePkg / Library / UefiBootManagerLib / BmLoadOption.c
index f88f8e02451c71ad75f7399bfe6f661aa504c3e3..7bf96646c690e2606fe77b7c8333855fe0bc8a2c 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   Load option library functions which relate with creating and processing load options.\r
 \r
-Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>\r
-(C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
+Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2015-2018 Hewlett Packard Enterprise Development LP<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
@@ -90,7 +90,7 @@ BmGetFreeOptionNumber (
   OUT UINT16                            *FreeOptionNumber\r
   )\r
 {\r
-  \r
+\r
   UINTN         OptionNumber;\r
   UINTN         Index;\r
   UINT16        *OptionOrder;\r
@@ -98,7 +98,7 @@ BmGetFreeOptionNumber (
   UINT16        *BootNext;\r
 \r
   ASSERT (FreeOptionNumber != NULL);\r
-  ASSERT (LoadOptionType == LoadOptionTypeDriver || \r
+  ASSERT (LoadOptionType == LoadOptionTypeDriver ||\r
           LoadOptionType == LoadOptionTypeBoot ||\r
           LoadOptionType == LoadOptionTypeSysPrep);\r
 \r
@@ -110,9 +110,9 @@ BmGetFreeOptionNumber (
     GetEfiGlobalVariable2 (L"BootNext", (VOID**) &BootNext, NULL);\r
   }\r
 \r
-  for (OptionNumber = 0; \r
+  for (OptionNumber = 0;\r
        OptionNumber < OptionOrderSize / sizeof (UINT16)\r
-                    + ((BootNext != NULL) ? 1 : 0); \r
+                    + ((BootNext != NULL) ? 1 : 0);\r
        OptionNumber++\r
        ) {\r
     //\r
@@ -125,10 +125,10 @@ BmGetFreeOptionNumber (
     }\r
 \r
     //\r
-    // We didn't find it in the ****Order array and it doesn't equal to BootNext \r
+    // We didn't find it in the ****Order array and it doesn't equal to BootNext\r
     // Otherwise, OptionNumber equals to OptionOrderSize / sizeof (UINT16) + 1\r
     //\r
-    if ((Index == OptionOrderSize / sizeof (UINT16)) && \r
+    if ((Index == OptionOrderSize / sizeof (UINT16)) &&\r
         ((BootNext == NULL) || (OptionNumber != *BootNext))\r
         ) {\r
       break;\r
@@ -203,12 +203,12 @@ EfiBootManagerLoadOptionToVariable (
   EFI_DEVICE_PATH_PROTOCOL    FilePathList[];\r
   UINT8                       OptionalData[];\r
 TODO: FilePathList[] IS:\r
-A packed array of UEFI device paths.  The first element of the \r
-array is a device path that describes the device and location of the \r
-Image for this load option.  The FilePathList[0] is specific \r
-to the device type.  Other device paths may optionally exist in the \r
-FilePathList, but their usage is OSV specific. Each element \r
-in the array is variable length, and ends at the device path end \r
+A packed array of UEFI device paths.  The first element of the\r
+array is a device path that describes the device and location of the\r
+Image for this load option.  The FilePathList[0] is specific\r
+to the device type.  Other device paths may optionally exist in the\r
+FilePathList, but their usage is OSV specific. Each element\r
+in the array is variable length, and ends at the device path end\r
 structure.\r
   */\r
   VariableSize = sizeof (Option->Attributes)\r
@@ -250,13 +250,16 @@ structure.
     VariableAttributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
   }\r
 \r
-  return gRT->SetVariable (\r
-                OptionName,\r
-                &gEfiGlobalVariableGuid,\r
-                VariableAttributes,\r
-                VariableSize,\r
-                Variable\r
-                );\r
+  Status = gRT->SetVariable (\r
+                  OptionName,\r
+                  &gEfiGlobalVariableGuid,\r
+                  VariableAttributes,\r
+                  VariableSize,\r
+                  Variable\r
+                  );\r
+  FreePool (Variable);\r
+\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -359,7 +362,7 @@ EfiBootManagerAddLoadOptionVariable (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (Option->OptionType != LoadOptionTypeDriver && \r
+  if (Option->OptionType != LoadOptionTypeDriver &&\r
       Option->OptionType != LoadOptionTypeSysPrep &&\r
       Option->OptionType != LoadOptionTypeBoot\r
       ) {\r
@@ -399,7 +402,7 @@ EfiBootManagerAddLoadOptionVariable (
 }\r
 \r
 /**\r
-  Sort the load option. The DriverOrder or BootOrder will be re-created to \r
+  Sort the load option. The DriverOrder or BootOrder will be re-created to\r
   reflect the new order.\r
 \r
   @param OptionType             Load option type\r
@@ -514,7 +517,7 @@ EfiBootManagerInitializeLoadOption (
 /**\r
   Return the index of the load option in the load option array.\r
 \r
-  The function consider two load options are equal when the \r
+  The function consider two load options are equal when the\r
   OptionType, Attributes, Description, FilePath and OptionalData are equal.\r
 \r
   @param Key    Pointer to the load option to be found.\r
@@ -618,15 +621,15 @@ EfiBootManagerDeleteLoadOptionVariable (
 /**\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
+  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
+  @param  MaxSize            Max valid device path size. If big than this size,\r
                              return error.\r
-  \r
+\r
   @retval 0                  An invalid device path.\r
   @retval Others             The size of a device path in bytes.\r
 \r
@@ -668,12 +671,12 @@ BmGetDevicePathSizeEx (
 }\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
+  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
+  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
@@ -716,7 +719,7 @@ BmStrSizeEx (
   @retval FALSE                 The variable data is corrupted.\r
 \r
 **/\r
-BOOLEAN \r
+BOOLEAN\r
 BmValidateOption (\r
   UINT8                     *Variable,\r
   UINTN                     VariableSize\r
@@ -862,7 +865,7 @@ EFIAPI
 EfiBootManagerVariableToLoadOptionEx (\r
   IN CHAR16                           *VariableName,\r
   IN EFI_GUID                         *VendorGuid,\r
-  IN OUT EFI_BOOT_MANAGER_LOAD_OPTION *Option  \r
+  IN OUT EFI_BOOT_MANAGER_LOAD_OPTION *Option\r
   )\r
 {\r
   EFI_STATUS                         Status;\r
@@ -1034,7 +1037,7 @@ BmCollectLoadOptions (
 /**\r
   Returns an array of load options based on the EFI variable\r
   L"BootOrder"/L"DriverOrder" and the L"Boot####"/L"Driver####" variables impled by it.\r
-  #### is the hex value of the UINT16 in each BootOrder/DriverOrder entry. \r
+  #### is the hex value of the UINT16 in each BootOrder/DriverOrder entry.\r
 \r
   @param  LoadOptionCount   Returns number of entries in the array.\r
   @param  LoadOptionType    The type of the load option.\r
@@ -1122,8 +1125,8 @@ EfiBootManagerGetLoadOptions (
 \r
   @param  LoadOption   Pointer to boot option to Free.\r
 \r
-  @return EFI_SUCCESS   BootOption was freed \r
-  @return EFI_NOT_FOUND BootOption == NULL \r
+  @return EFI_SUCCESS   BootOption was freed\r
+  @return EFI_NOT_FOUND BootOption == NULL\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1150,14 +1153,14 @@ EfiBootManagerFreeLoadOption (
 }\r
 \r
 /**\r
-  Free an EFI_BOOT_MANGER_LOAD_OPTION array that was allocated by \r
+  Free an EFI_BOOT_MANGER_LOAD_OPTION array that was allocated by\r
   EfiBootManagerGetLoadOptions().\r
 \r
   @param  Option       Pointer to boot option array to free.\r
   @param  OptionCount  Number of array entries in BootOption\r
 \r
-  @return EFI_SUCCESS   BootOption was freed \r
-  @return EFI_NOT_FOUND BootOption == NULL \r
+  @return EFI_SUCCESS   BootOption was freed\r
+  @return EFI_NOT_FOUND BootOption == NULL\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1230,7 +1233,7 @@ BmIsLoadOptionPeHeaderValid (
       // Check PE32 or PE32+ magic, and machine type\r
       //\r
       OptionalHeader = (EFI_IMAGE_OPTIONAL_HEADER32 *) &PeHeader->Pe32.OptionalHeader;\r
-      if ((OptionalHeader->Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC || \r
+      if ((OptionalHeader->Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC ||\r
            OptionalHeader->Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) &&\r
           EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeHeader->Pe32.FileHeader.Machine)\r
           ) {\r
@@ -1346,7 +1349,7 @@ BmGetNextLoadOptionBuffer (
 \r
   @param LoadOption  Pointer to the load option.\r
 \r
-  @retval EFI_INVALID_PARAMETER  The load option type is invalid, \r
+  @retval EFI_INVALID_PARAMETER  The load option type is invalid,\r
                                  or the load option file path doesn't point to a valid file.\r
   @retval EFI_UNSUPPORTED        The load option type is of LoadOptionTypeBoot.\r
   @retval EFI_SUCCESS            The load option is inactive, or successfully loaded and executed.\r