]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/GenericBdsLib/BdsBoot.c
Add INF extension Information
[mirror_edk2.git] / MdeModulePkg / Library / GenericBdsLib / BdsBoot.c
index 30d3a50d906076feba4a713f8d773e76ac6508c6..f170daab95b96058b50331df8a5d69f6ddffcd0b 100644 (file)
@@ -1,6 +1,5 @@
 /** @file\r
 /** @file\r
-  BDS Lib functions which relate with create or process the boot\r
-  option.\r
+  BDS Lib functions which relate with create or process the boot option.\r
 \r
 Copyright (c) 2004 - 2008, Intel Corporation. <BR>\r
 All rights reserved. This program and the accompanying materials\r
 \r
 Copyright (c) 2004 - 2008, Intel Corporation. <BR>\r
 All rights reserved. This program and the accompanying materials\r
@@ -51,7 +50,7 @@ BdsLibDoLegacyBoot (
     return EFI_UNSUPPORTED;\r
   }\r
   //\r
     return EFI_UNSUPPORTED;\r
   }\r
   //\r
-  // Notes: if we seperate the int 19, then we don't need to refresh BBS\r
+  // Notes: if we separate the int 19, then we don't need to refresh BBS\r
   //\r
   BdsRefreshBbsTableForBoot (Option);\r
 \r
   //\r
   BdsRefreshBbsTableForBoot (Option);\r
 \r
@@ -73,17 +72,17 @@ BdsLibDoLegacyBoot (
 \r
 \r
 /**\r
 \r
 \r
 /**\r
-  Process the boot option follow the EFI 1.1 specification and\r
+  Process the boot option follow the UEFI specification and\r
   special treat the legacy boot option with BBS_DEVICE_PATH.\r
 \r
   @param  Option                 The boot option need to be processed\r
   @param  DevicePath             The device path which describe where to load the\r
   special treat the legacy boot option with BBS_DEVICE_PATH.\r
 \r
   @param  Option                 The boot option need to be processed\r
   @param  DevicePath             The device path which describe where to load the\r
-                                 boot image or the legcy BBS device path to boot\r
+                                 boot image or the legacy BBS device path to boot\r
                                  the legacy OS\r
                                  the legacy OS\r
-  @param  ExitDataSize           Returned directly from gBS->StartImage ()\r
-  @param  ExitData               Returned directly from gBS->StartImage ()\r
+  @param  ExitDataSize           The size of exit data.\r
+  @param  ExitData               Data returned when Boot image failed.\r
 \r
 \r
-  @retval EFI_SUCCESS            Status from gBS->StartImage ()\r
+  @retval EFI_SUCCESS            Boot from the input boot option successfully.\r
   @retval EFI_NOT_FOUND          If the Device Path is not found in the system\r
 \r
 **/\r
   @retval EFI_NOT_FOUND          If the Device Path is not found in the system\r
 \r
 **/\r
@@ -120,8 +119,8 @@ BdsLibBootViaBootOption (
 \r
   //\r
   // Notes: this code can be remove after the s3 script table\r
 \r
   //\r
   // Notes: this code can be remove after the s3 script table\r
-  // hook on the event EFI_EVENT_SIGNAL_READY_TO_BOOT or\r
-  // EFI_EVENT_SIGNAL_LEGACY_BOOT\r
+  // hook on the event EVT_SIGNAL_READY_TO_BOOT or\r
+  // EVT_SIGNAL_LEGACY_BOOT\r
   //\r
   Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL, (VOID **) &AcpiS3Save);\r
   if (!EFI_ERROR (Status)) {\r
   //\r
   Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL, (VOID **) &AcpiS3Save);\r
   if (!EFI_ERROR (Status)) {\r
@@ -142,7 +141,7 @@ BdsLibBootViaBootOption (
     }\r
   }\r
   //\r
     }\r
   }\r
   //\r
-  // Signal the EFI_EVENT_SIGNAL_READY_TO_BOOT event\r
+  // Signal the EVT_SIGNAL_READY_TO_BOOT event\r
   //\r
   EfiSignalEventReadyToBoot();\r
   \r
   //\r
   EfiSignalEventReadyToBoot();\r
   \r
@@ -173,9 +172,10 @@ BdsLibBootViaBootOption (
   Status = BdsLibUpdateFvFileDevicePath (&DevicePath, &gEfiShellFileGuid);\r
   if (!EFI_ERROR(Status)) {\r
     if (Option->DevicePath != NULL) {\r
   Status = BdsLibUpdateFvFileDevicePath (&DevicePath, &gEfiShellFileGuid);\r
   if (!EFI_ERROR(Status)) {\r
     if (Option->DevicePath != NULL) {\r
-      SafeFreePool(Option->DevicePath);\r
+      FreePool(Option->DevicePath);\r
     }\r
     Option->DevicePath  = AllocateZeroPool (GetDevicePathSize (DevicePath));\r
     }\r
     Option->DevicePath  = AllocateZeroPool (GetDevicePathSize (DevicePath));\r
+    ASSERT(Option->DevicePath != NULL);\r
     CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));\r
     //\r
     // Update the shell boot option\r
     CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));\r
     //\r
     // Update the shell boot option\r
@@ -186,15 +186,10 @@ BdsLibBootViaBootOption (
     //\r
     // free the temporary device path created by BdsLibUpdateFvFileDevicePath()\r
     //\r
     //\r
     // free the temporary device path created by BdsLibUpdateFvFileDevicePath()\r
     //\r
-    SafeFreePool (DevicePath); \r
+    FreePool (DevicePath); \r
     DevicePath = Option->DevicePath;\r
   }\r
 \r
     DevicePath = Option->DevicePath;\r
   }\r
 \r
-  //\r
-  // Drop the TPL level from TPL_APPLICATION to TPL_APPLICATION\r
-  //\r
-  gBS->RestoreTPL (TPL_APPLICATION);\r
-\r
   DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting %S\n", Option->Description));\r
 \r
   Status = gBS->LoadImage (\r
   DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting %S\n", Option->Description));\r
 \r
   Status = gBS->LoadImage (\r
@@ -285,11 +280,6 @@ Done:
         &Option->BootCurrent\r
         );\r
 \r
         &Option->BootCurrent\r
         );\r
 \r
-  //\r
-  // Raise the TPL level back to TPL_APPLICATION\r
-  //\r
-  gBS->RaiseTPL (TPL_APPLICATION);\r
-\r
   return Status;\r
 }\r
 \r
   return Status;\r
 }\r
 \r
@@ -297,7 +287,7 @@ Done:
 /**\r
   Expand a device path that starts with a hard drive media device path node to be a\r
   full device path that includes the full hardware path to the device. We need\r
 /**\r
   Expand a device path that starts with a hard drive media device path node to be a\r
   full device path that includes the full hardware path to the device. We need\r
-  to do this so it can be booted. As an optimizaiton the front match (the part point\r
+  to do this so it can be booted. As an optimization the front match (the part point\r
   to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable\r
   so a connect all is not required on every boot. All successful history device path\r
   which point to partition node (the front part) will be saved.\r
   to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable\r
   so a connect all is not required on every boot. All successful history device path\r
   which point to partition node (the front part) will be saved.\r
@@ -349,8 +339,8 @@ BdsExpandPartitionPartialDevicePathToFull (
     do {\r
       //\r
       // Check every instance of the variable\r
     do {\r
       //\r
       // Check every instance of the variable\r
-      // First, check wheather the instance contain the partition node, which is needed for distinguishing  multi\r
-      // partial partition boot option. Second, check wheather the instance could be connected.\r
+      // First, check whether the instance contain the partition node, which is needed for distinguishing  multi\r
+      // partial partition boot option. Second, check whether the instance could be connected.\r
       //\r
       Instance  = GetNextDevicePathInstance (&TempNewDevicePath, &Size);\r
       if (MatchPartitionDevicePathNode (Instance, HardDriveDevicePath)) {\r
       //\r
       Instance  = GetNextDevicePathInstance (&TempNewDevicePath, &Size);\r
       if (MatchPartitionDevicePathNode (Instance, HardDriveDevicePath)) {\r
@@ -368,19 +358,19 @@ BdsExpandPartitionPartialDevicePathToFull (
       // Come here means the first instance is not matched\r
       //\r
       NeedAdjust = TRUE;\r
       // Come here means the first instance is not matched\r
       //\r
       NeedAdjust = TRUE;\r
-      SafeFreePool(Instance);\r
+      FreePool(Instance);\r
     } while (TempNewDevicePath != NULL);\r
 \r
     if (DeviceExist) {\r
       //\r
       // Find the matched device path.\r
     } while (TempNewDevicePath != NULL);\r
 \r
     if (DeviceExist) {\r
       //\r
       // Find the matched device path.\r
-      // Append the file path infomration from the boot option and return the fully expanded device path.\r
+      // Append the file path information from the boot option and return the fully expanded device path.\r
       //\r
       DevicePath     = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
       FullDevicePath = AppendDevicePath (Instance, DevicePath);\r
 \r
       //\r
       //\r
       DevicePath     = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
       FullDevicePath = AppendDevicePath (Instance, DevicePath);\r
 \r
       //\r
-      // Adjust the 'HDDP' instances sequense if the matched one is not first one.\r
+      // Adjust the 'HDDP' instances sequence if the matched one is not first one.\r
       //\r
       if (NeedAdjust) {\r
         //\r
       //\r
       if (NeedAdjust) {\r
         //\r
@@ -388,14 +378,14 @@ BdsExpandPartitionPartialDevicePathToFull (
         //\r
         TempNewDevicePath = CachedDevicePath;\r
         CachedDevicePath  = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );\r
         //\r
         TempNewDevicePath = CachedDevicePath;\r
         CachedDevicePath  = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );\r
-        SafeFreePool (TempNewDevicePath);\r
+        FreePool (TempNewDevicePath);\r
         \r
         //\r
         \r
         //\r
-        // Second, append the remaining parth after the matched instance\r
+        // Second, append the remaining path after the matched instance\r
         //\r
         TempNewDevicePath = CachedDevicePath;\r
         CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );\r
         //\r
         TempNewDevicePath = CachedDevicePath;\r
         CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );\r
-        SafeFreePool (TempNewDevicePath);\r
+        FreePool (TempNewDevicePath);\r
         //\r
         // Save the matching Device Path so we don't need to do a connect all next time\r
         //\r
         //\r
         // Save the matching Device Path so we don't need to do a connect all next time\r
         //\r
@@ -408,15 +398,15 @@ BdsExpandPartitionPartialDevicePathToFull (
                         );\r
       }\r
       \r
                         );\r
       }\r
       \r
-      SafeFreePool (Instance);\r
-      SafeFreePool (CachedDevicePath);\r
+      FreePool (Instance);\r
+      FreePool (CachedDevicePath);\r
       return FullDevicePath;\r
     }\r
   }\r
 \r
   //\r
   // If we get here we fail to find or 'HDDP' not exist, and now we need\r
       return FullDevicePath;\r
     }\r
   }\r
 \r
   //\r
   // If we get here we fail to find or 'HDDP' not exist, and now we need\r
-  // to seach all devices in the system for a matched partition\r
+  // to search all devices in the system for a matched partition\r
   //\r
   BdsLibConnectAllDriversToAllControllers ();\r
   Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);\r
   //\r
   BdsLibConnectAllDriversToAllControllers ();\r
   Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);\r
@@ -445,28 +435,28 @@ BdsExpandPartitionPartialDevicePathToFull (
       FullDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);\r
 \r
       //\r
       FullDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);\r
 \r
       //\r
-      // Save the matched patition device path in 'HDDP' variable\r
+      // Save the matched partition device path in 'HDDP' variable\r
       //\r
       if (CachedDevicePath != NULL) {\r
         //\r
       //\r
       if (CachedDevicePath != NULL) {\r
         //\r
-        // Save the matched patition device path as first instance of 'HDDP' variable\r
+        // Save the matched partition device path as first instance of 'HDDP' variable\r
         //\r
         if (BdsLibMatchDevicePaths (CachedDevicePath, BlockIoDevicePath)) {\r
           TempNewDevicePath = CachedDevicePath;\r
           CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);\r
         //\r
         if (BdsLibMatchDevicePaths (CachedDevicePath, BlockIoDevicePath)) {\r
           TempNewDevicePath = CachedDevicePath;\r
           CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);\r
-          SafeFreePool(TempNewDevicePath);\r
+          FreePool(TempNewDevicePath);\r
 \r
           TempNewDevicePath = CachedDevicePath;\r
           CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
 \r
           TempNewDevicePath = CachedDevicePath;\r
           CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
-          SafeFreePool(TempNewDevicePath);\r
+          FreePool(TempNewDevicePath);\r
         } else {\r
           TempNewDevicePath = CachedDevicePath;\r
           CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
         } else {\r
           TempNewDevicePath = CachedDevicePath;\r
           CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
-          SafeFreePool(TempNewDevicePath);\r
+          FreePool(TempNewDevicePath);\r
         }\r
         //\r
         // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller\r
         }\r
         //\r
         // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller\r
-        // If the user try to boot many OS in different HDs or partitions, in theary, the 'HDDP' variable maybe become larger and larger.\r
+        // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.\r
         //\r
         InstanceNum = 0;\r
         TempNewDevicePath = CachedDevicePath;\r
         //\r
         InstanceNum = 0;\r
         TempNewDevicePath = CachedDevicePath;\r
@@ -506,8 +496,10 @@ BdsExpandPartitionPartialDevicePathToFull (
     }\r
   }\r
   \r
     }\r
   }\r
   \r
-  SafeFreePool (CachedDevicePath);\r
-  SafeFreePool (BlockIoBuffer);\r
+  FreePool (CachedDevicePath);\r
+  if (BlockIoBuffer != NULL) {\r
+    FreePool (BlockIoBuffer);\r
+  }\r
   return FullDevicePath;\r
 }\r
 \r
   return FullDevicePath;\r
 }\r
 \r
@@ -519,8 +511,8 @@ BdsExpandPartitionPartialDevicePathToFull (
   @param  HardDriveDevicePath    A device path which starts with a hard drive media\r
                                  device path.\r
 \r
   @param  HardDriveDevicePath    A device path which starts with a hard drive media\r
                                  device path.\r
 \r
-  @retval TRUE                   There is a matched device path instance FALSE\r
-  @retval FALSE                  There is no matched device path instance\r
+  @retval TRUE                   There is a matched device path instance.\r
+  @retval FALSE                  There is no matched device path instance.\r
 \r
 **/\r
 BOOLEAN\r
 \r
 **/\r
 BOOLEAN\r
@@ -531,7 +523,6 @@ MatchPartitionDevicePathNode (
   )\r
 {\r
   HARDDRIVE_DEVICE_PATH     *TmpHdPath;\r
   )\r
 {\r
   HARDDRIVE_DEVICE_PATH     *TmpHdPath;\r
-  HARDDRIVE_DEVICE_PATH     *TempPath;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
   BOOLEAN                   Match;\r
   EFI_DEVICE_PATH_PROTOCOL  *BlockIoHdDevicePathNode;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
   BOOLEAN                   Match;\r
   EFI_DEVICE_PATH_PROTOCOL  *BlockIoHdDevicePathNode;\r
@@ -567,20 +558,19 @@ MatchPartitionDevicePathNode (
   // See if the harddrive device path in blockio matches the orig Hard Drive Node\r
   //\r
   TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePathNode;\r
   // See if the harddrive device path in blockio matches the orig Hard Drive Node\r
   //\r
   TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePathNode;\r
-  TempPath  = (HARDDRIVE_DEVICE_PATH *) BdsLibUnpackDevicePath ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
   Match = FALSE;\r
   \r
   //\r
   // Check for the match\r
   //\r
   Match = FALSE;\r
   \r
   //\r
   // Check for the match\r
   //\r
-  if ((TmpHdPath->MBRType == TempPath->MBRType) &&\r
-      (TmpHdPath->SignatureType == TempPath->SignatureType)) {\r
+  if ((TmpHdPath->MBRType == HardDriveDevicePath->MBRType) &&\r
+      (TmpHdPath->SignatureType == HardDriveDevicePath->SignatureType)) {\r
     switch (TmpHdPath->SignatureType) {\r
     case SIGNATURE_TYPE_GUID:\r
     switch (TmpHdPath->SignatureType) {\r
     case SIGNATURE_TYPE_GUID:\r
-      Match = CompareGuid ((EFI_GUID *)TmpHdPath->Signature, (EFI_GUID *)TempPath->Signature);\r
+      Match = CompareGuid ((EFI_GUID *)TmpHdPath->Signature, (EFI_GUID *)HardDriveDevicePath->Signature);\r
       break;\r
     case SIGNATURE_TYPE_MBR:\r
       break;\r
     case SIGNATURE_TYPE_MBR:\r
-      Match = (BOOLEAN)(*((UINT32 *)(&(TmpHdPath->Signature[0]))) == *(UINT32 *)(&(TempPath->Signature[0])));\r
+      Match = (BOOLEAN)(*((UINT32 *)(&(TmpHdPath->Signature[0]))) == ReadUnaligned32((UINT32 *)(&(HardDriveDevicePath->Signature[0]))));\r
       break;\r
     default:\r
       Match = FALSE;\r
       break;\r
     default:\r
       Match = FALSE;\r
@@ -659,7 +649,7 @@ BdsLibDeleteOptionFromHandle (
                       );\r
                       \r
     if (BootOptionVar == NULL) {\r
                       );\r
                       \r
     if (BootOptionVar == NULL) {\r
-      SafeFreePool (BootOrder);\r
+      FreePool (BootOrder);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
@@ -675,11 +665,11 @@ BdsLibDeleteOptionFromHandle (
     if ((OptionDevicePathSize == DevicePathSize) &&\r
         (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {\r
       BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
     if ((OptionDevicePathSize == DevicePathSize) &&\r
         (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {\r
       BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
-      SafeFreePool (BootOptionVar);\r
+      FreePool (BootOptionVar);\r
       break;\r
     }\r
 \r
       break;\r
     }\r
 \r
-    SafeFreePool (BootOptionVar);\r
+    FreePool (BootOptionVar);\r
     Index++;\r
   }\r
 \r
     Index++;\r
   }\r
 \r
@@ -694,7 +684,7 @@ BdsLibDeleteOptionFromHandle (
                   BootOrder\r
                   );\r
 \r
                   BootOrder\r
                   );\r
 \r
-  SafeFreePool (BootOrder);\r
+  FreePool (BootOrder);\r
 \r
   return Status;\r
 }\r
 \r
   return Status;\r
 }\r
@@ -751,7 +741,7 @@ BdsDeleteAllInvalidEfiBootOption (
                       &BootOptionSize\r
                       );\r
     if (NULL == BootOptionVar) {\r
                       &BootOptionSize\r
                       );\r
     if (NULL == BootOptionVar) {\r
-      SafeFreePool (BootOrder);\r
+      FreePool (BootOrder);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
@@ -765,7 +755,7 @@ BdsDeleteAllInvalidEfiBootOption (
     //\r
     if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&\r
         (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {\r
     //\r
     if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&\r
         (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {\r
-      SafeFreePool (BootOptionVar);\r
+      FreePool (BootOptionVar);\r
       Index++;\r
       continue;\r
     }\r
       Index++;\r
       continue;\r
     }\r
@@ -787,7 +777,7 @@ BdsDeleteAllInvalidEfiBootOption (
       BootOrder[Index] = 0xffff;\r
     }\r
 \r
       BootOrder[Index] = 0xffff;\r
     }\r
 \r
-    SafeFreePool (BootOptionVar);\r
+    FreePool (BootOptionVar);\r
     Index++;\r
   }\r
 \r
     Index++;\r
   }\r
 \r
@@ -809,7 +799,7 @@ BdsDeleteAllInvalidEfiBootOption (
                   BootOrder\r
                   );\r
 \r
                   BootOrder\r
                   );\r
 \r
-  SafeFreePool (BootOrder);\r
+  FreePool (BootOrder);\r
 \r
   return Status;\r
 }\r
 \r
   return Status;\r
 }\r
@@ -817,7 +807,7 @@ BdsDeleteAllInvalidEfiBootOption (
 \r
 /**\r
   This function will enumerate all possible boot device in the system,\r
 \r
 /**\r
   This function will enumerate all possible boot device in the system,\r
-  it will only excute once of every boot.\r
+  it will only execute once of every boot.\r
 \r
   @param  BdsBootOptionList      The header of the link list which indexed all\r
                                  current boot options\r
 \r
   @param  BdsBootOptionList      The header of the link list which indexed all\r
                                  current boot options\r
@@ -973,7 +963,7 @@ BdsLibEnumerateAllBootOption (
   }\r
 \r
   if (NumberBlockIoHandles != 0) {\r
   }\r
 \r
   if (NumberBlockIoHandles != 0) {\r
-    SafeFreePool (BlockIoHandles);\r
+    FreePool (BlockIoHandles);\r
   }\r
 \r
   //\r
   }\r
 \r
   //\r
@@ -1035,7 +1025,7 @@ BdsLibEnumerateAllBootOption (
   }\r
 \r
   if (NumberFileSystemHandles != 0) {\r
   }\r
 \r
   if (NumberFileSystemHandles != 0) {\r
-    SafeFreePool (FileSystemHandles);\r
+    FreePool (FileSystemHandles);\r
   }\r
 \r
   //\r
   }\r
 \r
   //\r
@@ -1058,7 +1048,7 @@ BdsLibEnumerateAllBootOption (
   }\r
 \r
   if (NumberSimpleNetworkHandles != 0) {\r
   }\r
 \r
   if (NumberSimpleNetworkHandles != 0) {\r
-    SafeFreePool (SimpleNetworkHandles);\r
+    FreePool (SimpleNetworkHandles);\r
   }\r
 \r
   //\r
   }\r
 \r
   //\r
@@ -1112,7 +1102,7 @@ BdsLibEnumerateAllBootOption (
   }\r
 \r
   if (FvHandleCount != 0) {\r
   }\r
 \r
   if (FvHandleCount != 0) {\r
-    SafeFreePool (FvHandleBuffer);\r
+    FreePool (FvHandleBuffer);\r
   }\r
   //\r
   // Make sure every boot only have one time\r
   }\r
   //\r
   // Make sure every boot only have one time\r
@@ -1125,13 +1115,13 @@ BdsLibEnumerateAllBootOption (
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
-  Build the boot option with the handle parsed in.\r
+  Build the boot option with the handle parsed in\r
 \r
   @param  Handle                 The handle which present the device path to create\r
                                  boot option\r
   @param  BdsBootOptionList      The header of the link list which indexed all\r
                                  current boot options\r
 \r
   @param  Handle                 The handle which present the device path to create\r
                                  boot option\r
   @param  BdsBootOptionList      The header of the link list which indexed all\r
                                  current boot options\r
-  @param  String                 Boot option name.\r
+  @param  String                 The description of the boot option.\r
 \r
 **/\r
 VOID\r
 \r
 **/\r
 VOID\r
@@ -1189,7 +1179,7 @@ BdsLibBuildOptionFromShell (
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
-  Boot from the EFI1.1 spec defined "BootNext" variable\r
+  Boot from the UEFI spec defined "BootNext" variable.\r
 \r
 **/\r
 VOID\r
 \r
 **/\r
 VOID\r
@@ -1249,7 +1239,6 @@ BdsLibBootNext (
 \r
   @param  DevicePath             Device Path to a  bootable device\r
 \r
 \r
   @param  DevicePath             Device Path to a  bootable device\r
 \r
-  @retval NULL                   The device path points to an EFI bootable Media\r
   @retval NULL                   The media on the DevicePath is not bootable\r
 \r
 **/\r
   @retval NULL                   The media on the DevicePath is not bootable\r
 \r
 **/\r
@@ -1298,7 +1287,7 @@ BdsLibGetBootableHandle (
     }\r
   } else {\r
     //\r
     }\r
   } else {\r
     //\r
-    // Get BlockIo protocal and check removable attribute\r
+    // Get BlockIo protocol and check removable attribute\r
     //\r
     Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
     //\r
     //\r
     Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
     //\r
@@ -1317,7 +1306,7 @@ BdsLibGetBootableHandle (
                BlockIo->Media->BlockSize,\r
                Buffer\r
                );\r
                BlockIo->Media->BlockSize,\r
                Buffer\r
                );\r
-      SafeFreePool(Buffer);\r
+      FreePool(Buffer);\r
     }\r
   }\r
 \r
     }\r
   }\r
 \r
@@ -1327,9 +1316,11 @@ BdsLibGetBootableHandle (
 \r
   //\r
   // If fail to get bootable handle specified by a USB boot option, the BDS should try to find other bootable device in the same USB bus\r
 \r
   //\r
   // If fail to get bootable handle specified by a USB boot option, the BDS should try to find other bootable device in the same USB bus\r
-  // Try to locate the USB node device path first, if fail then use its previour PCI node to search\r
+  // Try to locate the USB node device path first, if fail then use its previous PCI node to search\r
   //\r
   DupDevicePath = DuplicateDevicePath (DevicePath);\r
   //\r
   DupDevicePath = DuplicateDevicePath (DevicePath);\r
+  ASSERT (DupDevicePath != NULL);\r
+  \r
   UpdatedDevicePath = DupDevicePath;\r
   Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
   //\r
   UpdatedDevicePath = DupDevicePath;\r
   Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
   //\r
@@ -1389,9 +1380,11 @@ BdsLibGetBootableHandle (
     }\r
   }\r
 \r
     }\r
   }\r
 \r
-  SafeFreePool(DupDevicePath);\r
+  FreePool(DupDevicePath);\r
 \r
 \r
-  SafeFreePool(SimpleFileSystemHandles);\r
+  if (SimpleFileSystemHandles != NULL) {\r
+    FreePool(SimpleFileSystemHandles);\r
+  }\r
 \r
   return ReturnHandle;\r
 }\r
 \r
   return ReturnHandle;\r
 }\r
@@ -1472,23 +1465,23 @@ BdsLibNetworkBootWithMediaPresent (
 \r
   @param  DevicePath                      The bootable device Path to check\r
 \r
 \r
   @param  DevicePath                      The bootable device Path to check\r
 \r
-  @retval BDS_EFI_MEDIA_HD_BOOT           If the device path contains any media deviec path node, it is media boot type\r
+  @retval BDS_EFI_MEDIA_HD_BOOT           If the device path contains any media device path node, it is media boot type\r
                                           For the floppy node, handle it as media node\r
                                           For the floppy node, handle it as media node\r
-  @retval BDS_EFI_MEDIA_CDROM_BOOT        If the device path contains any media deviec path node, it is media boot type\r
+  @retval BDS_EFI_MEDIA_CDROM_BOOT        If the device path contains any media device path node, it is media boot type\r
                                           For the floppy node, handle it as media node\r
                                           For the floppy node, handle it as media node\r
-  @retval BDS_EFI_ACPI_FLOPPY_BOOT        If the device path contains any media deviec path node, it is media boot type\r
+  @retval BDS_EFI_ACPI_FLOPPY_BOOT        If the device path contains any media device path node, it is media boot type\r
                                           For the floppy node, handle it as media node\r
                                           For the floppy node, handle it as media node\r
-  @retval BDS_EFI_MESSAGE_ATAPI_BOOT      If the device path not contains any media deviec path node,  and\r
+  @retval BDS_EFI_MESSAGE_ATAPI_BOOT      If the device path not contains any media device path node,  and\r
                                           its last device path node point to a message device path node, it is\r
   \r
                                           its last device path node point to a message device path node, it is\r
   \r
-  @retval BDS_EFI_MESSAGE_SCSI_BOOT       If the device path not contains any media deviec path node,  and\r
+  @retval BDS_EFI_MESSAGE_SCSI_BOOT       If the device path not contains any media device path node,  and\r
                                           its last device path node point to a message device path node, it is\r
                                           its last device path node point to a message device path node, it is\r
-  @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If the device path not contains any media deviec path node,  and\r
+  @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If the device path not contains any media device path node,  and\r
                                           its last device path node point to a message device path node, it is\r
                                           its last device path node point to a message device path node, it is\r
-  @retval BDS_EFI_MESSAGE_MISC_BOOT       If the device path not contains any media deviec path node,  and\r
+  @retval BDS_EFI_MESSAGE_MISC_BOOT       If the device path not contains any media device path node,  and\r
                                           its last device path node point to a message device path node, it is\r
   @retval BDS_LEGACY_BBS_BOOT             Legacy boot type\r
                                           its last device path node point to a message device path node, it is\r
   @retval BDS_LEGACY_BBS_BOOT             Legacy boot type\r
-  @retval BDS_EFI_UNSUPPORT               An EFI Removable BlockIO device path not point to a media and message devie,   \r
+  @retval BDS_EFI_UNSUPPORT               An EFI Removable BlockIO device path not point to a media and message device,   \r
 \r
 **/\r
 UINT32\r
 \r
 **/\r
 UINT32\r
@@ -1562,14 +1555,14 @@ BdsGetBootTypeFromDevicePath (
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
-  Check whether the Device path in a boot option point to a valide bootable device,\r
+  Check whether the Device path in a boot option point to a valid bootable device,\r
   And if CheckMedia is true, check the device is ready to boot now.\r
 \r
   And if CheckMedia is true, check the device is ready to boot now.\r
 \r
-  @param DevPath        the Device path in a boot option\r
-  @param CheckMedia     if true, check the device is ready to boot now.\r
+  @param  DevPath     the Device path in a boot option\r
+  @param  CheckMedia  if true, check the device is ready to boot now.\r
 \r
 \r
-  @retval TRUE          the Device path  is valide\r
-  @retval FALSE         the Device path  is invalide .\r
+  @retval TRUE        the Device path  is valid\r
+  @retval FALSE       the Device path  is invalid .\r
 \r
 **/\r
 BOOLEAN\r
 \r
 **/\r
 BOOLEAN\r
@@ -1628,9 +1621,9 @@ BdsLibIsValidEFIBootOptDevicePath (
   // If the boot option point to a file, it is a valid EFI boot option,\r
   // and assume it is ready to boot now\r
   //\r
   // If the boot option point to a file, it is a valid EFI boot option,\r
   // and assume it is ready to boot now\r
   //\r
-  while (!EfiIsDevicePathEnd (TempDevicePath)) {\r
+  while (!IsDevicePathEnd (TempDevicePath)) {\r
      LastDeviceNode = TempDevicePath;\r
      LastDeviceNode = TempDevicePath;\r
-     TempDevicePath = EfiNextDevicePathNode (TempDevicePath);\r
+     TempDevicePath = NextDevicePathNode (TempDevicePath);\r
   }\r
   if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r
     (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {\r
   }\r
   if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r
     (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {\r
@@ -1650,7 +1643,7 @@ BdsLibIsValidEFIBootOptDevicePath (
       return TRUE;\r
     } else {\r
       if (Status == EFI_SUCCESS) {\r
       return TRUE;\r
     } else {\r
       if (Status == EFI_SUCCESS) {\r
-        SafeFreePool (TempDevicePath); \r
+        FreePool (TempDevicePath); \r
       }\r
       return FALSE;\r
     }\r
       }\r
       return FALSE;\r
     }\r
@@ -1715,7 +1708,7 @@ BdsLibIsValidEFIBootOptDevicePath (
 /**\r
   According to a file guild, check a Fv file device path is valid. If it is invalid,\r
   try to return the valid device path.\r
 /**\r
   According to a file guild, check a Fv file device path is valid. If it is invalid,\r
   try to return the valid device path.\r
-  FV address maybe changes for memory layout adjust from time to time, use this funciton\r
+  FV address maybe changes for memory layout adjust from time to time, use this function\r
   could promise the Fv file device path is right.\r
 \r
   @param  DevicePath             on input, the Fv file device path need to check on\r
   could promise the Fv file device path is right.\r
 \r
   @param  DevicePath             on input, the Fv file device path need to check on\r
@@ -1736,7 +1729,7 @@ EFI_STATUS
 EFIAPI\r
 BdsLibUpdateFvFileDevicePath (\r
   IN  OUT EFI_DEVICE_PATH_PROTOCOL      ** DevicePath,\r
 EFIAPI\r
 BdsLibUpdateFvFileDevicePath (\r
   IN  OUT EFI_DEVICE_PATH_PROTOCOL      ** DevicePath,\r
-  IN      EFI_GUID                      *FileGuid\r
+  IN  EFI_GUID                          *FileGuid\r
   )\r
 {\r
   EFI_DEVICE_PATH_PROTOCOL      *TempDevicePath;\r
   )\r
 {\r
   EFI_DEVICE_PATH_PROTOCOL      *TempDevicePath;\r
@@ -1769,9 +1762,9 @@ BdsLibUpdateFvFileDevicePath (
   //\r
   TempDevicePath = *DevicePath;\r
   LastDeviceNode = TempDevicePath;\r
   //\r
   TempDevicePath = *DevicePath;\r
   LastDeviceNode = TempDevicePath;\r
-  while (!EfiIsDevicePathEnd (TempDevicePath)) {\r
+  while (!IsDevicePathEnd (TempDevicePath)) {\r
      LastDeviceNode = TempDevicePath;\r
      LastDeviceNode = TempDevicePath;\r
-     TempDevicePath = EfiNextDevicePathNode (TempDevicePath);\r
+     TempDevicePath = NextDevicePathNode (TempDevicePath);\r
   }\r
   GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (\r
                 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode\r
   }\r
   GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (\r
                 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode\r
@@ -1895,8 +1888,10 @@ BdsLibUpdateFvFileDevicePath (
       FoundFvHandle = FvHandleBuffer[Index];\r
       break;\r
     }\r
       FoundFvHandle = FvHandleBuffer[Index];\r
       break;\r
     }\r
-    \r
-    SafeFreePool (FvHandleBuffer);  \r
+\r
+    if (FvHandleBuffer != NULL) {\r
+      FreePool (FvHandleBuffer);  \r
+    }\r
   }\r
 \r
   if (FindFvFile) {\r
   }\r
 \r
   if (FindFvFile) {\r