]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Disk/PartitionDxe/Mbr.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / PartitionDxe / Mbr.c
index b1a99ee85b278f612734e81c14e02f81bd8c57a7..531b3b45ea0f7f6eb0eafd4dafb4ddf99030c379 100644 (file)
 \r
 Copyright (c) 2018 Qualcomm Datacenter Technologies, Inc.\r
 Copyright (c) 2014, Hewlett-Packard Development Company, L.P.<BR>\r
-Copyright (c) 2006 - 2018, 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
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -38,26 +32,27 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 **/\r
 BOOLEAN\r
 PartitionValidMbr (\r
-  IN  MASTER_BOOT_RECORD      *Mbr,\r
-  IN  EFI_LBA                 LastLba\r
+  IN  MASTER_BOOT_RECORD  *Mbr,\r
+  IN  EFI_LBA             LastLba\r
   )\r
 {\r
-  UINT32  StartingLBA;\r
-  UINT32  EndingLBA;\r
-  UINT32  NewEndingLBA;\r
-  INTN    Index1;\r
-  INTN    Index2;\r
-  BOOLEAN MbrValid;\r
+  UINT32   StartingLBA;\r
+  UINT32   EndingLBA;\r
+  UINT32   NewEndingLBA;\r
+  INTN     Index1;\r
+  INTN     Index2;\r
+  BOOLEAN  MbrValid;\r
 \r
   if (Mbr->Signature != MBR_SIGNATURE) {\r
     return FALSE;\r
   }\r
+\r
   //\r
   // The BPB also has this signature, so it can not be used alone.\r
   //\r
   MbrValid = FALSE;\r
   for (Index1 = 0; Index1 < MAX_MBR_PARTITIONS; Index1++) {\r
-    if (Mbr->Partition[Index1].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) == 0) {\r
+    if ((Mbr->Partition[Index1].OSIndicator == 0x00) || (UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) == 0)) {\r
       continue;\r
     }\r
 \r
@@ -77,18 +72,18 @@ PartitionValidMbr (
       // with INT 13h\r
       //\r
 \r
-      DEBUG((EFI_D_INFO, "PartitionValidMbr: Bad MBR partition size EndingLBA(%1x) > LastLBA(%1x)\n", EndingLBA, LastLba));\r
+      DEBUG ((DEBUG_INFO, "PartitionValidMbr: Bad MBR partition size EndingLBA(%1x) > LastLBA(%1x)\n", EndingLBA, LastLba));\r
 \r
       return FALSE;\r
     }\r
 \r
     for (Index2 = Index1 + 1; Index2 < MAX_MBR_PARTITIONS; Index2++) {\r
-      if (Mbr->Partition[Index2].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index2].SizeInLBA) == 0) {\r
+      if ((Mbr->Partition[Index2].OSIndicator == 0x00) || (UNPACK_UINT32 (Mbr->Partition[Index2].SizeInLBA) == 0)) {\r
         continue;\r
       }\r
 \r
       NewEndingLBA = UNPACK_UINT32 (Mbr->Partition[Index2].StartingLBA) + UNPACK_UINT32 (Mbr->Partition[Index2].SizeInLBA) - 1;\r
-      if (NewEndingLBA >= StartingLBA && UNPACK_UINT32 (Mbr->Partition[Index2].StartingLBA) <= EndingLBA) {\r
+      if ((NewEndingLBA >= StartingLBA) && (UNPACK_UINT32 (Mbr->Partition[Index2].StartingLBA) <= EndingLBA)) {\r
         //\r
         // This region overlaps with the Index1'th region\r
         //\r
@@ -96,13 +91,13 @@ PartitionValidMbr (
       }\r
     }\r
   }\r
+\r
   //\r
   // None of the regions overlapped so MBR is O.K.\r
   //\r
   return MbrValid;\r
 }\r
 \r
-\r
 /**\r
   Install child handles if the Handle supports MBR format.\r
 \r
@@ -141,14 +136,24 @@ PartitionInstallMbrChildHandles (
   EFI_DEVICE_PATH_PROTOCOL     *LastDevicePathNode;\r
   UINT32                       BlockSize;\r
   UINT32                       MediaId;\r
-  EFI_LBA                      LastBlock;\r
+  EFI_LBA                      LastSector;\r
   EFI_PARTITION_INFO_PROTOCOL  PartitionInfo;\r
 \r
-  Found           = EFI_NOT_FOUND;\r
+  Found = EFI_NOT_FOUND;\r
 \r
-  BlockSize = BlockIo->Media->BlockSize;\r
-  MediaId   = BlockIo->Media->MediaId;\r
-  LastBlock = BlockIo->Media->LastBlock;\r
+  BlockSize  = BlockIo->Media->BlockSize;\r
+  MediaId    = BlockIo->Media->MediaId;\r
+  LastSector = DivU64x32 (\r
+                 MultU64x32 (BlockIo->Media->LastBlock + 1, BlockSize),\r
+                 MBR_SIZE\r
+                 ) - 1;\r
+\r
+  //\r
+  // Ensure the block size can hold the MBR\r
+  //\r
+  if (BlockSize < sizeof (MASTER_BOOT_RECORD)) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
 \r
   Mbr = AllocatePool (BlockSize);\r
   if (Mbr == NULL) {\r
@@ -166,9 +171,11 @@ PartitionInstallMbrChildHandles (
     Found = Status;\r
     goto Done;\r
   }\r
-  if (!PartitionValidMbr (Mbr, LastBlock)) {\r
+\r
+  if (!PartitionValidMbr (Mbr, LastSector)) {\r
     goto Done;\r
   }\r
+\r
   //\r
   // We have a valid mbr - add each partition\r
   //\r
@@ -179,14 +186,15 @@ PartitionInstallMbrChildHandles (
   ZeroMem (&ParentHdDev, sizeof (ParentHdDev));\r
   DevicePathNode = DevicePath;\r
   while (!IsDevicePathEnd (DevicePathNode)) {\r
-    LastDevicePathNode  = DevicePathNode;\r
-    DevicePathNode      = NextDevicePathNode (DevicePathNode);\r
+    LastDevicePathNode = DevicePathNode;\r
+    DevicePathNode     = NextDevicePathNode (DevicePathNode);\r
   }\r
 \r
   if (LastDevicePathNode != NULL) {\r
-    if (DevicePathType (LastDevicePathNode) == MEDIA_DEVICE_PATH &&\r
-        DevicePathSubType (LastDevicePathNode) == MEDIA_HARDDRIVE_DP\r
-        ) {\r
+    if ((DevicePathType (LastDevicePathNode) == MEDIA_DEVICE_PATH) &&\r
+        (DevicePathSubType (LastDevicePathNode) == MEDIA_HARDDRIVE_DP)\r
+        )\r
+    {\r
       CopyMem (&ParentHdDev, LastDevicePathNode, sizeof (ParentHdDev));\r
     } else {\r
       LastDevicePathNode = NULL;\r
@@ -194,18 +202,18 @@ PartitionInstallMbrChildHandles (
   }\r
 \r
   ZeroMem (&HdDev, sizeof (HdDev));\r
-  HdDev.Header.Type     = MEDIA_DEVICE_PATH;\r
-  HdDev.Header.SubType  = MEDIA_HARDDRIVE_DP;\r
+  HdDev.Header.Type    = MEDIA_DEVICE_PATH;\r
+  HdDev.Header.SubType = MEDIA_HARDDRIVE_DP;\r
   SetDevicePathNodeLength (&HdDev.Header, sizeof (HdDev));\r
-  HdDev.MBRType         = MBR_TYPE_PCAT;\r
-  HdDev.SignatureType   = SIGNATURE_TYPE_MBR;\r
+  HdDev.MBRType       = MBR_TYPE_PCAT;\r
+  HdDev.SignatureType = SIGNATURE_TYPE_MBR;\r
 \r
   if (LastDevicePathNode == NULL) {\r
     //\r
     // This is a MBR, add each partition\r
     //\r
     for (Index = 0; Index < MAX_MBR_PARTITIONS; Index++) {\r
-      if (Mbr->Partition[Index].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA) == 0) {\r
+      if ((Mbr->Partition[Index].OSIndicator == 0x00) || (UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA) == 0)) {\r
         //\r
         // Don't use null MBR entries\r
         //\r
@@ -233,23 +241,24 @@ PartitionInstallMbrChildHandles (
       if (Mbr->Partition[Index].OSIndicator == EFI_PARTITION) {\r
         PartitionInfo.System = 1;\r
       }\r
+\r
       CopyMem (&PartitionInfo.Info.Mbr, &Mbr->Partition[Index], sizeof (MBR_PARTITION_RECORD));\r
 \r
       Status = PartitionInstallChildHandle (\r
-                This,\r
-                Handle,\r
-                DiskIo,\r
-                DiskIo2,\r
-                BlockIo,\r
-                BlockIo2,\r
-                DevicePath,\r
-                (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,\r
-                &PartitionInfo,\r
-                HdDev.PartitionStart,\r
-                HdDev.PartitionStart + HdDev.PartitionSize - 1,\r
-                MBR_SIZE,\r
-                ((Mbr->Partition[Index].OSIndicator == EFI_PARTITION) ? &gEfiPartTypeSystemPartGuid: NULL)\r
-                );\r
+                 This,\r
+                 Handle,\r
+                 DiskIo,\r
+                 DiskIo2,\r
+                 BlockIo,\r
+                 BlockIo2,\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&HdDev,\r
+                 &PartitionInfo,\r
+                 HdDev.PartitionStart,\r
+                 HdDev.PartitionStart + HdDev.PartitionSize - 1,\r
+                 MBR_SIZE,\r
+                 ((Mbr->Partition[Index].OSIndicator == EFI_PARTITION) ? &gEfiPartTypeSystemPartGuid : NULL)\r
+                 );\r
 \r
       if (!EFI_ERROR (Status)) {\r
         Found = EFI_SUCCESS;\r
@@ -264,7 +273,6 @@ PartitionInstallMbrChildHandles (
     ExtMbrStartingLba = 0;\r
 \r
     do {\r
-\r
       Status = DiskIo->ReadDisk (\r
                          DiskIo,\r
                          MediaId,\r
@@ -282,22 +290,32 @@ PartitionInstallMbrChildHandles (
       }\r
 \r
       if ((Mbr->Partition[0].OSIndicator == EXTENDED_DOS_PARTITION) ||\r
-          (Mbr->Partition[0].OSIndicator == EXTENDED_WINDOWS_PARTITION)) {\r
+          (Mbr->Partition[0].OSIndicator == EXTENDED_WINDOWS_PARTITION))\r
+      {\r
         ExtMbrStartingLba = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA);\r
+        //\r
+        // A value of 0 is invalid for StartingLBA\r
+        //\r
+        if (ExtMbrStartingLba == 0) {\r
+          break;\r
+        }\r
+\r
         continue;\r
       }\r
+\r
       HdDev.PartitionNumber = ++Index;\r
       HdDev.PartitionStart  = UNPACK_UINT32 (Mbr->Partition[0].StartingLBA) + ExtMbrStartingLba + ParentHdDev.PartitionStart;\r
       HdDev.PartitionSize   = UNPACK_UINT32 (Mbr->Partition[0].SizeInLBA);\r
       if ((HdDev.PartitionStart + HdDev.PartitionSize - 1 >= ParentHdDev.PartitionStart + ParentHdDev.PartitionSize) ||\r
-          (HdDev.PartitionStart <= ParentHdDev.PartitionStart)) {\r
+          (HdDev.PartitionStart <= ParentHdDev.PartitionStart))\r
+      {\r
         break;\r
       }\r
 \r
       //\r
       // The signature in EBR(Extended Boot Record) should always be 0.\r
       //\r
-      *((UINT32 *) &HdDev.Signature[0]) = 0;\r
+      *((UINT32 *)&HdDev.Signature[0]) = 0;\r
 \r
       ZeroMem (&PartitionInfo, sizeof (EFI_PARTITION_INFO_PROTOCOL));\r
       PartitionInfo.Revision = EFI_PARTITION_INFO_PROTOCOL_REVISION;\r
@@ -305,6 +323,7 @@ PartitionInstallMbrChildHandles (
       if (Mbr->Partition[0].OSIndicator == EFI_PARTITION) {\r
         PartitionInfo.System = 1;\r
       }\r
+\r
       CopyMem (&PartitionInfo.Info.Mbr, &Mbr->Partition[0], sizeof (MBR_PARTITION_RECORD));\r
 \r
       Status = PartitionInstallChildHandle (\r
@@ -315,12 +334,12 @@ PartitionInstallMbrChildHandles (
                  BlockIo,\r
                  BlockIo2,\r
                  DevicePath,\r
-                 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&HdDev,\r
                  &PartitionInfo,\r
                  HdDev.PartitionStart - ParentHdDev.PartitionStart,\r
                  HdDev.PartitionStart - ParentHdDev.PartitionStart + HdDev.PartitionSize - 1,\r
                  MBR_SIZE,\r
-                 ((Mbr->Partition[0].OSIndicator == EFI_PARTITION) ? &gEfiPartTypeSystemPartGuid: NULL)\r
+                 ((Mbr->Partition[0].OSIndicator == EFI_PARTITION) ? &gEfiPartTypeSystemPartGuid : NULL)\r
                  );\r
       if (!EFI_ERROR (Status)) {\r
         Found = EFI_SUCCESS;\r
@@ -328,7 +347,8 @@ PartitionInstallMbrChildHandles (
 \r
       if ((Mbr->Partition[1].OSIndicator != EXTENDED_DOS_PARTITION) &&\r
           (Mbr->Partition[1].OSIndicator != EXTENDED_WINDOWS_PARTITION)\r
-          ) {\r
+          )\r
+      {\r
         break;\r
       }\r
 \r