]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/GenericBdsLib/BdsBoot.c
Code scrub for GenericBdsLib
[mirror_edk2.git] / MdeModulePkg / Library / GenericBdsLib / BdsBoot.c
index 14df23af353dc024306440e5a3df80512fd70ba1..18738be8a85bb452f7f03af6d434df8f2e75b8de 100644 (file)
@@ -17,10 +17,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 BOOLEAN mEnumBootDevice = FALSE;\r
 \r
-//\r
-// This GUID is used for an EFI Variable that stores the front device pathes\r
-// for a partial device path that starts with the HD node.\r
-//\r
+///\r
+/// This GUID is used for an EFI Variable that stores the front device pathes\r
+/// for a partial device path that starts with the HD node.\r
+///\r
 EFI_GUID  mHdBootVariablePrivateGuid = { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };\r
 \r
 \r
@@ -182,10 +182,11 @@ BdsLibBootViaBootOption (
     //\r
     InitializeListHead (&TempBootLists);\r
     BdsLibRegisterNewOption (&TempBootLists, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
+    \r
     //\r
     // free the temporary device path created by BdsLibUpdateFvFileDevicePath()\r
     //\r
-    gBS->FreePool (DevicePath); \r
+    SafeFreePool (DevicePath); \r
     DevicePath = Option->DevicePath;\r
   }\r
 \r
@@ -222,7 +223,7 @@ BdsLibBootViaBootOption (
     // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
     //  machinename is ia32, ia64, x64, ...\r
     //\r
-    FilePath = FileDevicePath (Handle, DEFAULT_REMOVABLE_FILE_NAME);\r
+    FilePath = FileDevicePath (Handle, (CONST CHAR16*)PcdGetPtr(PcdDefaultBootFileName));\r
     if (FilePath != NULL) {\r
       Status = gBS->LoadImage (\r
                       TRUE,\r
@@ -340,6 +341,7 @@ BdsExpandPartitionPartialDevicePathToFull (
                       &mHdBootVariablePrivateGuid,\r
                       &CachedDevicePathSize\r
                       );\r
+                      \r
   if (CachedDevicePath != NULL) {\r
     TempNewDevicePath = CachedDevicePath;\r
     DeviceExist = FALSE;\r
@@ -374,7 +376,7 @@ BdsExpandPartitionPartialDevicePathToFull (
       // Find the matched device path.\r
       // Append the file path infomration from the boot option and return the fully expanded device path.\r
       //\r
-      DevicePath    = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
+      DevicePath     = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
       FullDevicePath = AppendDevicePath (Instance, DevicePath);\r
 \r
       //\r
@@ -385,13 +387,14 @@ BdsExpandPartitionPartialDevicePathToFull (
         // First delete the matched instance.\r
         //\r
         TempNewDevicePath = CachedDevicePath;\r
-        CachedDevicePath = BdsLibDelPartMatchInstance ( CachedDevicePath, Instance );\r
+        CachedDevicePath  = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );\r
         SafeFreePool (TempNewDevicePath);\r
+        \r
         //\r
         // Second, append the remaining parth after the matched instance\r
         //\r
         TempNewDevicePath = CachedDevicePath;\r
-        CachedDevicePath = AppendDevicePathInstance ( Instance, CachedDevicePath );\r
+        CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );\r
         SafeFreePool (TempNewDevicePath);\r
         //\r
         // Save the matching Device Path so we don't need to do a connect all next time\r
@@ -404,8 +407,9 @@ BdsExpandPartitionPartialDevicePathToFull (
                         CachedDevicePath\r
                         );\r
       }\r
-      SafeFreePool(Instance);\r
-      gBS->FreePool (CachedDevicePath);\r
+      \r
+      SafeFreePool (Instance);\r
+      SafeFreePool (CachedDevicePath);\r
       return FullDevicePath;\r
     }\r
   }\r
@@ -501,12 +505,12 @@ BdsExpandPartitionPartialDevicePathToFull (
       break;\r
     }\r
   }\r
-  gBS->FreePool (CachedDevicePath);\r
-  gBS->FreePool (BlockIoBuffer);\r
+  \r
+  SafeFreePool (CachedDevicePath);\r
+  SafeFreePool (BlockIoBuffer);\r
   return FullDevicePath;\r
 }\r
 \r
-\r
 /**\r
   Check whether there is a instance in BlockIoDevicePath, which contain multi device path\r
   instances, has the same partition node with HardDriveDevicePath device path\r
@@ -516,7 +520,7 @@ BdsExpandPartitionPartialDevicePathToFull (
                                  device path.\r
 \r
   @retval TRUE                   There is a matched device path instance FALSE\r
-                                 -There is no matched device path instance\r
+  @retval FALSE                  There is no matched device path instance\r
 \r
 **/\r
 BOOLEAN\r
@@ -535,10 +539,11 @@ MatchPartitionDevicePathNode (
   if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {\r
     return FALSE;\r
   }\r
+  \r
   //\r
   // Make PreviousDevicePath == the device path node before the end node\r
   //\r
-  DevicePath          = BlockIoDevicePath;\r
+  DevicePath              = BlockIoDevicePath;\r
   BlockIoHdDevicePathNode = NULL;\r
 \r
   //\r
@@ -564,6 +569,7 @@ MatchPartitionDevicePathNode (
   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
@@ -585,7 +591,6 @@ MatchPartitionDevicePathNode (
   return Match;\r
 }\r
 \r
-\r
 /**\r
   Delete the boot option associated with the handle passed in.\r
 \r
@@ -620,21 +625,30 @@ BdsLibDeleteOptionFromHandle (
   BootOrder     = NULL;\r
   BootOrderSize = 0;\r
 \r
+  //\r
+  // Check "BootOrder" variable, if no, means there is no any boot order.\r
+  //\r
   BootOrder = BdsLibGetVariableAndSize (\r
                 L"BootOrder",\r
                 &gEfiGlobalVariableGuid,\r
                 &BootOrderSize\r
                 );\r
-  if (NULL == BootOrder) {\r
+  if (BootOrder == NULL) {\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
+  //\r
+  // Convert device handle to device path protocol instance\r
+  //\r
   DevicePath = DevicePathFromHandle (Handle);\r
   if (DevicePath == NULL) {\r
     return EFI_NOT_FOUND;\r
   }\r
   DevicePathSize = GetDevicePathSize (DevicePath);\r
 \r
+  //\r
+  // Loop all boot order variable and find the matching device path\r
+  //\r
   Index = 0;\r
   while (Index < BootOrderSize / sizeof (UINT16)) {\r
     UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
@@ -643,8 +657,9 @@ BdsLibDeleteOptionFromHandle (
                       &gEfiGlobalVariableGuid,\r
                       &BootOptionSize\r
                       );\r
-    if (NULL == BootOptionVar) {\r
-      gBS->FreePool (BootOrder);\r
+                      \r
+    if (BootOptionVar == NULL) {\r
+      SafeFreePool (BootOrder);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
@@ -660,14 +675,17 @@ BdsLibDeleteOptionFromHandle (
     if ((OptionDevicePathSize == DevicePathSize) &&\r
         (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {\r
       BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
-      gBS->FreePool (BootOptionVar);\r
+      SafeFreePool (BootOptionVar);\r
       break;\r
     }\r
 \r
-    gBS->FreePool (BootOptionVar);\r
+    SafeFreePool (BootOptionVar);\r
     Index++;\r
   }\r
 \r
+  //\r
+  // Adjust number of boot option for "BootOrder" variable.\r
+  //\r
   Status = gRT->SetVariable (\r
                   L"BootOrder",\r
                   &gEfiGlobalVariableGuid,\r
@@ -676,7 +694,7 @@ BdsLibDeleteOptionFromHandle (
                   BootOrder\r
                   );\r
 \r
-  gBS->FreePool (BootOrder);\r
+  SafeFreePool (BootOrder);\r
 \r
   return Status;\r
 }\r
@@ -686,8 +704,6 @@ BdsLibDeleteOptionFromHandle (
   Delete all invalid EFI boot options. The probable invalid boot option could\r
   be Removable media or Network boot device.\r
 \r
-  VOID\r
-\r
   @retval EFI_SUCCESS            Delete all invalid boot option success\r
   @retval EFI_NOT_FOUND          Variable "BootOrder" is not found\r
   @retval EFI_OUT_OF_RESOURCES   Lack of memory resource\r
@@ -714,6 +730,9 @@ BdsDeleteAllInvalidEfiBootOption (
   BootOrder     = NULL;\r
   BootOrderSize = 0;\r
 \r
+  //\r
+  // Check "BootOrder" variable firstly, this variable hold the number of boot options\r
+  //\r
   BootOrder = BdsLibGetVariableAndSize (\r
                 L"BootOrder",\r
                 &gEfiGlobalVariableGuid,\r
@@ -732,7 +751,7 @@ BdsDeleteAllInvalidEfiBootOption (
                       &BootOptionSize\r
                       );\r
     if (NULL == BootOptionVar) {\r
-      gBS->FreePool (BootOrder);\r
+      SafeFreePool (BootOrder);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
@@ -746,7 +765,7 @@ BdsDeleteAllInvalidEfiBootOption (
     //\r
     if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&\r
         (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {\r
-      gBS->FreePool (BootOptionVar);\r
+      SafeFreePool (BootOptionVar);\r
       Index++;\r
       continue;\r
     }\r
@@ -768,7 +787,7 @@ BdsDeleteAllInvalidEfiBootOption (
       BootOrder[Index] = 0xffff;\r
     }\r
 \r
-    gBS->FreePool (BootOptionVar);\r
+    SafeFreePool (BootOptionVar);\r
     Index++;\r
   }\r
 \r
@@ -790,7 +809,7 @@ BdsDeleteAllInvalidEfiBootOption (
                   BootOrder\r
                   );\r
 \r
-  gBS->FreePool (BootOrder);\r
+  SafeFreePool (BootOrder);\r
 \r
   return Status;\r
 }\r
@@ -842,11 +861,12 @@ BdsLibEnumerateAllBootOption (
   EFI_IMAGE_OPTIONAL_HEADER_UNION       HdrData;\r
   EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;\r
 \r
-  FloppyNumber = 0;\r
-  CdromNumber = 0;\r
-  UsbNumber = 0;\r
-  MiscNumber = 0;\r
+  FloppyNumber  = 0;\r
+  CdromNumber   = 0;\r
+  UsbNumber     = 0;\r
+  MiscNumber    = 0;\r
   ZeroMem (Buffer, sizeof (Buffer));\r
+  \r
   //\r
   // If the boot device enumerate happened, just get the boot\r
   // device from the boot order variable\r
@@ -855,6 +875,7 @@ BdsLibEnumerateAllBootOption (
     BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
     return EFI_SUCCESS;\r
   }\r
+  \r
   //\r
   // Notes: this dirty code is to get the legacy boot option from the\r
   // BBS table and create to variable as the EFI boot option, it should\r
@@ -866,6 +887,7 @@ BdsLibEnumerateAllBootOption (
   // Delete invalid boot option\r
   //\r
   BdsDeleteAllInvalidEfiBootOption ();\r
+  \r
   //\r
   // Parse removable media\r
   //\r
@@ -876,6 +898,7 @@ BdsLibEnumerateAllBootOption (
         &NumberBlockIoHandles,\r
         &BlockIoHandles\r
         );\r
+        \r
   for (Index = 0; Index < NumberBlockIoHandles; Index++) {\r
     Status = gBS->HandleProtocol (\r
                     BlockIoHandles[Index],\r
@@ -950,7 +973,7 @@ BdsLibEnumerateAllBootOption (
   }\r
 \r
   if (NumberBlockIoHandles != 0) {\r
-    gBS->FreePool (BlockIoHandles);\r
+    SafeFreePool (BlockIoHandles);\r
   }\r
 \r
   //\r
@@ -985,7 +1008,7 @@ BdsLibEnumerateAllBootOption (
     NeedDelete = TRUE;\r
     Status     = BdsLibGetImageHeader (\r
                    FileSystemHandles[Index],\r
-                   DEFAULT_REMOVABLE_FILE_NAME,\r
+                   (CHAR16*)PcdGetPtr (PcdDefaultBootFileName),\r
                    &DosHeader,\r
                    Hdr\r
                    );\r
@@ -1012,7 +1035,7 @@ BdsLibEnumerateAllBootOption (
   }\r
 \r
   if (NumberFileSystemHandles != 0) {\r
-    gBS->FreePool (FileSystemHandles);\r
+    SafeFreePool (FileSystemHandles);\r
   }\r
 \r
   //\r
@@ -1035,7 +1058,7 @@ BdsLibEnumerateAllBootOption (
   }\r
 \r
   if (NumberSimpleNetworkHandles != 0) {\r
-    gBS->FreePool (SimpleNetworkHandles);\r
+    SafeFreePool (SimpleNetworkHandles);\r
   }\r
 \r
   //\r
@@ -1089,7 +1112,7 @@ BdsLibEnumerateAllBootOption (
   }\r
 \r
   if (FvHandleCount != 0) {\r
-    gBS->FreePool (FvHandleBuffer);\r
+    SafeFreePool (FvHandleBuffer);\r
   }\r
   //\r
   // Make sure every boot only have one time\r
@@ -1101,7 +1124,6 @@ BdsLibEnumerateAllBootOption (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Build the boot option with the handle parsed in.\r
 \r
@@ -1156,12 +1178,7 @@ BdsLibBuildOptionFromShell (
   // Build the shell device path\r
   //\r
   EfiInitializeFwVolDevicepathNode (&ShellNode, &gEfiShellFileGuid);\r
-  //\r
-  //ShellNode.Header.Type     = MEDIA_DEVICE_PATH;\r
-  //ShellNode.Header.SubType  = MEDIA_FV_FILEPATH_DP;\r
-  //SetDevicePathNodeLength (&ShellNode.Header, sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH));\r
-  //CopyMem (&ShellNode.NameGuid, &gEfiShellFileGuid, sizeof (EFI_GUID));\r
-  //\r
+\r
   DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);\r
 \r
   //\r
@@ -1171,7 +1188,6 @@ BdsLibBuildOptionFromShell (
 \r
 }\r
 \r
-\r
 /**\r
   Boot from the EFI1.1 spec defined "BootNext" variable\r
 \r
@@ -1225,8 +1241,6 @@ BdsLibBootNext (
 \r
 }\r
 \r
-\r
-\r
 /**\r
   Return the bootable media handle.\r
   First, check the device is connected\r
@@ -1264,6 +1278,7 @@ BdsLibGetBootableHandle (
   EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION   Hdr;\r
 \r
   UpdatedDevicePath = DevicePath;\r
+  \r
   //\r
   // Check whether the device is connected\r
   //\r
@@ -1278,7 +1293,7 @@ BdsLibGetBootableHandle (
       // Fail to find the proper BlockIo and simple file protocol, maybe because device not present,  we need to connect it firstly\r
       //\r
       UpdatedDevicePath = DevicePath;\r
-      Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
+      Status            = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
       gBS->ConnectController (Handle, NULL, NULL, TRUE);\r
     }\r
   } else {\r
@@ -1302,7 +1317,7 @@ BdsLibGetBootableHandle (
                BlockIo->Media->BlockSize,\r
                Buffer\r
                );\r
-      gBS->FreePool (Buffer);\r
+      SafeFreePool(Buffer);\r
     }\r
   }\r
 \r
@@ -1361,7 +1376,7 @@ BdsLibGetBootableHandle (
       Hdr.Union = &HdrData;\r
       Status = BdsLibGetImageHeader (\r
                  SimpleFileSystemHandles[Index],\r
-                 DEFAULT_REMOVABLE_FILE_NAME,\r
+                 (CHAR16*)PcdGetPtr(PcdDefaultBootFileName),\r
                  &DosHeader,\r
                  Hdr\r
                  );\r
@@ -1374,19 +1389,13 @@ BdsLibGetBootableHandle (
     }\r
   }\r
 \r
-  if (DupDevicePath != NULL) {\r
-    SafeFreePool(DupDevicePath);\r
-  }\r
-  if (SimpleFileSystemHandles !=NULL ) {\r
-    gBS->FreePool (SimpleFileSystemHandles);\r
-  }\r
+  SafeFreePool(DupDevicePath);\r
+\r
+  SafeFreePool(SimpleFileSystemHandles);\r
 \r
   return ReturnHandle;\r
 }\r
 \r
-\r
-\r
-\r
 /**\r
   Check to see if the network cable is plugged in. If the DevicePath is not\r
   connected it will be connected.\r
@@ -1458,8 +1467,6 @@ BdsLibNetworkBootWithMediaPresent (
   return MediaPresent;\r
 }\r
 \r
-\r
-\r
 /**\r
   For a bootable Device path, return its boot type.\r
 \r
@@ -1554,16 +1561,15 @@ BdsGetBootTypeFromDevicePath (
   return BDS_EFI_UNSUPPORT;\r
 }\r
 \r
-\r
 /**\r
   Check whether the Device path in a boot option point to a valide bootable device,\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
-  @return TRUE      -- the Device path  is valide\r
-  @return FALSE   -- the Device path  is invalide .\r
+  @retval TRUE          the Device path  is valide\r
+  @retval FALSE         the Device path  is invalide .\r
 \r
 **/\r
 BOOLEAN\r
@@ -1581,6 +1587,7 @@ BdsLibIsValidEFIBootOptDevicePath (
 \r
   TempDevicePath = DevPath;\r
   LastDeviceNode = DevPath;\r
+  \r
   //\r
   // Check if it's a valid boot option for network boot device\r
   // Only check if there is SimpleNetworkProtocol installed. If yes, that means\r
@@ -1603,6 +1610,7 @@ BdsLibIsValidEFIBootOptDevicePath (
                     &Handle\r
                     );\r
   }\r
+  \r
   if (!EFI_ERROR (Status)) {\r
     if (CheckMedia) {\r
       //\r
@@ -1642,7 +1650,7 @@ BdsLibIsValidEFIBootOptDevicePath (
       return TRUE;\r
     } else {\r
       if (Status == EFI_SUCCESS) {\r
-        gBS->FreePool (TempDevicePath); \r
+        SafeFreePool (TempDevicePath); \r
       }\r
       return FALSE;\r
     }\r
@@ -1666,6 +1674,7 @@ BdsLibIsValidEFIBootOptDevicePath (
       Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
     }\r
   }\r
+  \r
   if (!EFI_ERROR (Status)) {\r
     Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
     if (!EFI_ERROR (Status)) {\r
@@ -1727,7 +1736,7 @@ EFI_STATUS
 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
@@ -1754,6 +1763,7 @@ BdsLibUpdateFvFileDevicePath (
   if (FileGuid == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
+  \r
   //\r
   // Check whether the device path point to the default the input Fv file\r
   //\r
@@ -1885,9 +1895,8 @@ BdsLibUpdateFvFileDevicePath (
       FoundFvHandle = FvHandleBuffer[Index];\r
       break;\r
     }\r
-    if (FvHandleBuffer !=NULL ) {\r
-      FreePool (FvHandleBuffer);  \r
-    }\r
+    \r
+    SafeFreePool (FvHandleBuffer);  \r
   }\r
 \r
   if (FindFvFile) {\r