Skip to content

Commit

Permalink
Enable dynamically scaling up the number of max part entry if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Feb 27, 2024
1 parent 9b6e03d commit 65a8098
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion FirmwareGen/GPT/GPTUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ internal static byte[] MakeGPT(ulong DiskSize, ulong SectorSize, GPTPartition[]
ulong LastLBA = (DiskSize / SectorSize) - 1;

ulong PartitionArrayLBACount = 4;

if ((ulong)DefaultPartitionTable.Length * 128 > PartitionArrayLBACount * SectorSize)
{
throw new Exception("Unsupported Configuration, too many partitions to fit. File an issue");
}

ulong TotalGPTLBACount = 1 /* GPT Header */ + PartitionArrayLBACount /* Partition Table */;
ulong LastUsableLBA = LastLBA - TotalGPTLBACount;

Expand Down Expand Up @@ -164,6 +170,17 @@ private static byte[] MakeGPT(ulong FirstLBA, ulong LastLBA, ulong SectorSize, G
ulong FirstUsableLBA = FirstLBA + TotalGPTLBACount;
ulong LastUsableLBA = LastLBA - TotalGPTLBACount;

uint PartitionEntryCount = 32;
if ((uint)Partitions.Length + 2 > PartitionEntryCount)
{
if ((uint)Partitions.Length + 2 > PartitionEntryCount * 4)
{
throw new Exception("Unsupported Configuration, too many partitions than supported, please file an issue.");
}

PartitionEntryCount = 128;
}

GPTHeader Header = new()
{
Signature = "EFI PART",
Expand All @@ -177,7 +194,7 @@ private static byte[] MakeGPT(ulong FirstLBA, ulong LastLBA, ulong SectorSize, G
LastUsableLBA = LastUsableLBA,
DiskGUID = new Guid("efa6243a-085f-e745-f2ce-54d39ef34351"),
PartitionArrayLBA = IsBackupGPT ? LastLBA - TotalGPTLBACount + 1 : FirstLBA + 1,
PartitionEntryCount = 32,
PartitionEntryCount = PartitionEntryCount,
PartitionEntrySize = 128,
PartitionArrayCRC32 = 0
};
Expand Down

0 comments on commit 65a8098

Please sign in to comment.