Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Library/DiscUtils.Core/Partitions/GuidPartitionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,17 @@ public static GuidPartitionTable Initialize(Stream disk, Geometry diskGeometry)
{
// Create the protective MBR partition record.
var pt = BiosPartitionTable.Initialize(disk, diskGeometry);
pt.CreatePrimaryByCylinder(0, diskGeometry.Cylinders - 1, BiosPartitionTypes.GptProtective, false);

// The UEFI spec ("Protective MBR") requires the 0xEE record to have
// StartingLBA exactly 1 (the LBA of the GPT header) and SizeInLBA equal
// to the size of the disk minus one, capped at 0xFFFFFFFF. A cylinder
// aligned record starts at LBA 63 instead, which the Linux kernel
// (block/partitions/efi.c pmbr_part_valid, requires starting_lba == 1)
// and EDK2 firmware (MdeModulePkg PartitionDxe, UNPACK_UINT32(StartingLBA) == 1)
// reject, causing the whole GPT to be ignored. Use a sector-based record
// starting at LBA 1 covering the rest of the disk instead.
var sectorCount = disk.Length / diskGeometry.BytesPerSector;
pt.CreatePrimaryBySector(1, Math.Min(sectorCount - 1, uint.MaxValue), BiosPartitionTypes.GptProtective, false);

// Create the GPT headers, and blank-out the entry areas
const int EntryCount = 128;
Expand Down
52 changes: 52 additions & 0 deletions Tests/LibraryTests/Partitions/GuidPartitionTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using DiscUtils;
using DiscUtils.Partitions;
using DiscUtils.Streams;
using DiscUtils.Vdi;
Expand Down Expand Up @@ -181,4 +183,54 @@ public void Delete()
Assert.Equal(sectorCount[2], table[1].SectorCount);
}

[Fact]
public void ProtectiveMbrIsSpecCompliant()
{
var ms = new MemoryStream();
ms.SetLength(3 * 1024 * 1024);
GuidPartitionTable.Initialize(ms, Geometry.FromCapacity(ms.Length));

ms.Position = 0;
var sector = new byte[512];
ms.ReadExactly(sector, 0, sector.Length);

// Boot signature.
Assert.Equal(0x55, sector[510]);
Assert.Equal(0xAA, sector[511]);

// Exactly one of the four partition records must be the 0xEE protective
// record; the other three must be empty (type 0x00).
var protectiveIndex = -1;
var protectiveCount = 0;
var emptyCount = 0;
for (var i = 0; i < 4; ++i)
{
var type = sector[0x1BE + 16 * i + 4];
if (type == 0xEE)
{
protectiveIndex = i;
++protectiveCount;
}
else if (type == 0x00)
{
++emptyCount;
}
}

Assert.Equal(1, protectiveCount);
Assert.Equal(3, emptyCount);

var record = sector.AsSpan(0x1BE + 16 * protectiveIndex, 16);

// Status byte must be non-bootable.
Assert.Equal(0, record[0]);

// UEFI spec: StartingLBA == 1 and SizeInLBA == disk size in sectors - 1.
var startingLba = EndianUtilities.ToUInt32LittleEndian(record.Slice(8));
var sizeInLba = EndianUtilities.ToUInt32LittleEndian(record.Slice(12));

Assert.Equal(1U, startingLba);
Assert.Equal((uint)(ms.Length / 512 - 1), sizeInLba);
}

}
Loading