]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Disk/PartitionDxe/Mbr.c
MdeModulePkg/PartitionDxe: Revert changes for the special MBR
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / PartitionDxe / Mbr.c
index 194633452583754aee9dc30d7acc962ce3864c27..f0c92aa09a596c235050688705db4fc51433922e 100644 (file)
@@ -3,23 +3,18 @@
 \r
   MBR - Master Boot Record is in the first sector of a partitioned hard disk.\r
         The MBR supports four partitions per disk. The MBR also contains legacy\r
-        code that is not run on an EFI system. The legacy code reads the \r
-        first sector of the active partition into memory and \r
+        code that is not run on an EFI system. The legacy code reads the\r
+        first sector of the active partition into memory and\r
 \r
-  BPB - BIOS Parameter Block is in the first sector of a FAT file system. \r
-        The BPB contains information about the FAT file system. The BPB is \r
+  BPB - BIOS Parameter Block is in the first sector of a FAT file system.\r
+        The BPB contains information about the FAT file system. The BPB is\r
         always on the first sector of a media. The first sector also contains\r
         the legacy boot strap code.\r
 \r
+Copyright (c) 2018 Qualcomm Datacenter Technologies, Inc.\r
 Copyright (c) 2014, Hewlett-Packard Development Company, L.P.<BR>\r
-Copyright (c) 2006 - 2016, 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
@@ -30,7 +25,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
   @param  Mbr         Parent Handle.\r
   @param  LastLba     Last Lba address on the device.\r
-   \r
+\r
   @retval TRUE        Mbr is a Valid MBR.\r
   @retval FALSE       Mbr is not a Valid MBR.\r
 \r
@@ -75,7 +70,7 @@ PartitionValidMbr (
       // return FALSE since no block devices on a system are implemented\r
       // with INT 13h\r
       //\r
-    \r
+\r
       DEBUG((EFI_D_INFO, "PartitionValidMbr: Bad MBR partition size EndingLBA(%1x) > LastLBA(%1x)\n", EndingLBA, LastLba));\r
 \r
       return FALSE;\r
@@ -112,7 +107,7 @@ PartitionValidMbr (
   @param[in]  BlockIo           Parent BlockIo interface.\r
   @param[in]  BlockIo2          Parent BlockIo2 interface.\r
   @param[in]  DevicePath        Parent Device Path.\r
-   \r
+\r
   @retval EFI_SUCCESS       A child handle was added.\r
   @retval EFI_MEDIA_CHANGED Media change was detected.\r
   @retval Others            MBR partition was not found.\r
@@ -129,24 +124,35 @@ PartitionInstallMbrChildHandles (
   IN  EFI_DEVICE_PATH_PROTOCOL     *DevicePath\r
   )\r
 {\r
-  EFI_STATUS                Status;\r
-  MASTER_BOOT_RECORD        *Mbr;\r
-  UINT32                    ExtMbrStartingLba;\r
-  UINTN                     Index;\r
-  HARDDRIVE_DEVICE_PATH     HdDev;\r
-  HARDDRIVE_DEVICE_PATH     ParentHdDev;\r
-  EFI_STATUS                Found;\r
-  EFI_DEVICE_PATH_PROTOCOL  *DevicePathNode;\r
-  EFI_DEVICE_PATH_PROTOCOL  *LastDevicePathNode;\r
-  UINT32                    BlockSize;\r
-  UINT32                    MediaId;\r
-  EFI_LBA                   LastBlock;\r
+  EFI_STATUS                   Status;\r
+  MASTER_BOOT_RECORD           *Mbr;\r
+  UINT32                       ExtMbrStartingLba;\r
+  UINT32                       Index;\r
+  HARDDRIVE_DEVICE_PATH        HdDev;\r
+  HARDDRIVE_DEVICE_PATH        ParentHdDev;\r
+  EFI_STATUS                   Found;\r
+  EFI_DEVICE_PATH_PROTOCOL     *DevicePathNode;\r
+  EFI_DEVICE_PATH_PROTOCOL     *LastDevicePathNode;\r
+  UINT32                       BlockSize;\r
+  UINT32                       MediaId;\r
+  EFI_LBA                      LastSector;\r
+  EFI_PARTITION_INFO_PROTOCOL  PartitionInfo;\r
 \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
@@ -164,7 +170,7 @@ PartitionInstallMbrChildHandles (
     Found = Status;\r
     goto Done;\r
   }\r
-  if (!PartitionValidMbr (Mbr, LastBlock)) {\r
+  if (!PartitionValidMbr (Mbr, LastSector)) {\r
     goto Done;\r
   }\r
   //\r
@@ -213,9 +219,9 @@ PartitionInstallMbrChildHandles (
       if (Mbr->Partition[Index].OSIndicator == PMBR_GPT_PARTITION) {\r
         //\r
         // This is the guard MBR for the GPT. If you ever see a GPT disk with zero partitions you can get here.\r
-        //  We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating \r
+        //  We can not produce an MBR BlockIo for this device as the MBR spans the GPT headers. So formating\r
         //  this BlockIo would corrupt the GPT structures and require a recovery that would corrupt the format\r
-        //  that corrupted the GPT partition. \r
+        //  that corrupted the GPT partition.\r
         //\r
         continue;\r
       }\r
@@ -225,6 +231,14 @@ PartitionInstallMbrChildHandles (
       HdDev.PartitionSize   = UNPACK_UINT32 (Mbr->Partition[Index].SizeInLBA);\r
       CopyMem (HdDev.Signature, &(Mbr->UniqueMbrSignature[0]), sizeof (Mbr->UniqueMbrSignature));\r
 \r
+      ZeroMem (&PartitionInfo, sizeof (EFI_PARTITION_INFO_PROTOCOL));\r
+      PartitionInfo.Revision = EFI_PARTITION_INFO_PROTOCOL_REVISION;\r
+      PartitionInfo.Type     = PARTITION_TYPE_MBR;\r
+      if (Mbr->Partition[Index].OSIndicator == EFI_PARTITION) {\r
+        PartitionInfo.System = 1;\r
+      }\r
+      CopyMem (&PartitionInfo.Info.Mbr, &Mbr->Partition[Index], sizeof (MBR_PARTITION_RECORD));\r
+\r
       Status = PartitionInstallChildHandle (\r
                 This,\r
                 Handle,\r
@@ -234,10 +248,11 @@ PartitionInstallMbrChildHandles (
                 BlockIo2,\r
                 DevicePath,\r
                 (EFI_DEVICE_PATH_PROTOCOL *) &HdDev,\r
+                &PartitionInfo,\r
                 HdDev.PartitionStart,\r
                 HdDev.PartitionStart + HdDev.PartitionSize - 1,\r
                 MBR_SIZE,\r
-                (BOOLEAN) (Mbr->Partition[Index].OSIndicator == EFI_PARTITION)\r
+                ((Mbr->Partition[Index].OSIndicator == EFI_PARTITION) ? &gEfiPartTypeSystemPartGuid: NULL)\r
                 );\r
 \r
       if (!EFI_ERROR (Status)) {\r
@@ -288,6 +303,14 @@ PartitionInstallMbrChildHandles (
       //\r
       *((UINT32 *) &HdDev.Signature[0]) = 0;\r
 \r
+      ZeroMem (&PartitionInfo, sizeof (EFI_PARTITION_INFO_PROTOCOL));\r
+      PartitionInfo.Revision = EFI_PARTITION_INFO_PROTOCOL_REVISION;\r
+      PartitionInfo.Type     = PARTITION_TYPE_MBR;\r
+      if (Mbr->Partition[0].OSIndicator == EFI_PARTITION) {\r
+        PartitionInfo.System = 1;\r
+      }\r
+      CopyMem (&PartitionInfo.Info.Mbr, &Mbr->Partition[0], sizeof (MBR_PARTITION_RECORD));\r
+\r
       Status = PartitionInstallChildHandle (\r
                  This,\r
                  Handle,\r
@@ -297,10 +320,11 @@ PartitionInstallMbrChildHandles (
                  BlockIo2,\r
                  DevicePath,\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
-                 (BOOLEAN) (Mbr->Partition[0].OSIndicator == EFI_PARTITION)\r
+                 ((Mbr->Partition[0].OSIndicator == EFI_PARTITION) ? &gEfiPartTypeSystemPartGuid: NULL)\r
                  );\r
       if (!EFI_ERROR (Status)) {\r
         Found = EFI_SUCCESS;\r