Skip to content

Commit

Permalink
Merge pull request #3011 from SzymekkYT/master
Browse files Browse the repository at this point in the history
Fixed maximum size of disks and partitions (issue #2954)
  • Loading branch information
zarlo authored May 5, 2024
2 parents a55e9ca + 032db03 commit 7bd691b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion source/Cosmos.HAL2/BlockDevice/BlockDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class BlockDevice : Device
/// </summary>
/// <param name="aBlockCount">Number of blocks to alloc.</param>
/// <returns>byte array.</returns>
public byte[] NewBlockArray(uint aBlockCount)
public byte[] NewBlockArray(ulong aBlockCount)
{
return new byte[aBlockCount * mBlockSize];
}
Expand Down
12 changes: 6 additions & 6 deletions source/Cosmos.HAL2/BlockDevice/EBR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class EBR
public class PartInfo
{
public readonly byte SystemID;
public readonly uint StartSector;
public readonly uint SectorCount;
public readonly ulong StartSector;
public readonly ulong SectorCount;

public PartInfo(byte aSystemID, uint aStartSector, uint aSectorCount)
public PartInfo(byte aSystemID, ulong aStartSector, ulong aSectorCount)
{
SystemID = aSystemID;
StartSector = aStartSector;
Expand All @@ -30,7 +30,7 @@ public EBR(byte[] aEBR)
ParsePartition(aEBR, 462);
}

protected void ParsePartition(byte[] aEBR, uint aLoc)
protected void ParsePartition(byte[] aEBR, int aLoc) //This should be int. ToUInt64(byte[] value, int)!!
{
byte xSystemID = aEBR[aLoc + 4];
// SystemID = 0 means no partition
Expand All @@ -41,8 +41,8 @@ protected void ParsePartition(byte[] aEBR, uint aLoc)
}
else if (xSystemID != 0)
{
uint xStartSector = BitConverter.ToUInt32(aEBR, (int)aLoc + 8);
uint xSectorCount = BitConverter.ToUInt32(aEBR, (int)aLoc + 12);
ulong xStartSector = BitConverter.ToUInt32(aEBR, aLoc + 8);
ulong xSectorCount = BitConverter.ToUInt32(aEBR, aLoc + 12);

var xPartInfo = new PartInfo(xSystemID, xStartSector, xSectorCount);
Partitions.Add(xPartInfo);
Expand Down
12 changes: 6 additions & 6 deletions source/Cosmos.HAL2/BlockDevice/GPT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,25 @@ public GPT(BlockDevice aBlockDevice)

// Start of parition entries
ulong partEntryStart = BitConverter.ToUInt64(GPTHeader, 72);
uint numParitions = BitConverter.ToUInt32(GPTHeader, 80);
uint partSize = BitConverter.ToUInt32(GPTHeader, 84);
ulong numParitions = BitConverter.ToUInt32(GPTHeader, 80);
ulong partSize = BitConverter.ToUInt32(GPTHeader, 84);

uint paritionsPerSector = 512 / partSize;
ulong paritionsPerSector = 512 / partSize;

for (ulong i = 0; i < numParitions/paritionsPerSector; i++)
for (ulong i = 0; i < numParitions / paritionsPerSector; i++)
{

byte[] partData = new byte[512];
aBlockDevice.ReadBlock(partEntryStart + i, 1, ref partData);

for (uint j = 0; j < paritionsPerSector; j++)
{
ParseParition(partData, j * partSize);
ParseParition(partData, j * (long)partSize);
}
}
}

private void ParseParition(byte[] partData, uint off)
private void ParseParition(byte[] partData, long off)
{
byte[] guidArray = new byte[16];

Expand Down
2 changes: 1 addition & 1 deletion source/Cosmos.HAL2/BlockDevice/IDE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ internal static void ScanAndInitPartitions(BlockDevice device)
}
}
}
}
}
18 changes: 9 additions & 9 deletions source/Cosmos.HAL2/BlockDevice/MBR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class MBR
public class PartInfo
{
public readonly byte SystemID;
public readonly uint StartSector;
public readonly uint SectorCount;
public readonly ulong StartSector;
public readonly ulong SectorCount;

public PartInfo(byte aSystemID, uint aStartSector, uint aSectorCount)
public PartInfo(byte aSystemID, ulong aStartSector, ulong aSectorCount)
{
SystemID = aSystemID;
StartSector = aStartSector;
Expand All @@ -41,7 +41,7 @@ public MBR(BlockDevice device)
ParsePartition(aMBR, 494);
}

protected void ParsePartition(byte[] aMBR, uint aLoc)
protected void ParsePartition(byte[] aMBR, ulong aLoc)
{
byte xSystemID = aMBR[aLoc + 4];
// SystemID = 0 means no partition
Expand All @@ -56,8 +56,8 @@ protected void ParsePartition(byte[] aMBR, uint aLoc)
}
else if (xSystemID != 0)
{
uint xStartSector = BitConverter.ToUInt32(aMBR, (int)aLoc + 8);
uint xSectorCount = BitConverter.ToUInt32(aMBR, (int)aLoc + 12);
ulong xStartSector = BitConverter.ToUInt32(aMBR, (int)aLoc + 8);
ulong xSectorCount = BitConverter.ToUInt32(aMBR, (int)aLoc + 12);

var xPartInfo = new PartInfo(xSystemID, xStartSector, xSectorCount);
Partitions.Add(xPartInfo);
Expand Down Expand Up @@ -128,11 +128,11 @@ public void WritePartitionInformation(Partition partition, byte PartitionNo)
partition.Host.ReadBlock(0, 1, ref mb.memory);
//TO DO: Implement the CHS starting / ending sector adresses and partition type
mb.Write8((uint)(446 + (PartitionNo * 16) + 4), 0x0B);
mb.Write32((uint)(446 + (PartitionNo * 16) + 8), (uint) partition.StartingSector);
mb.Write32((uint)(446 + (PartitionNo * 16) + 12), (uint) partition.BlockCount);
mb.Write32((uint)(446 + (PartitionNo * 16) + 8), (uint)partition.StartingSector);
mb.Write32((uint)(446 + (PartitionNo * 16) + 12), (uint)partition.BlockCount);
partition.Host.WriteBlock(0, 1, ref mb.memory);
ParsePartition(mb.memory, 446 + (uint)(PartitionNo * 16));

}
}
}
26 changes: 14 additions & 12 deletions source/Cosmos.System2/FileSystem/Disk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Disk
/// <summary>
/// The size of the disk in bytes.
/// </summary>
public int Size { get; }
public long Size { get; }
/// <summary>
/// List of partitions
/// </summary>
Expand All @@ -30,10 +30,12 @@ public List<ManagedPartition> Partitions
{
List<ManagedPartition> converted = new();

if (Host.Type == BlockDeviceType.RemovableCD) {
if (Host.Type == BlockDeviceType.RemovableCD)
{
ManagedPartition part = new(new Partition(Host, 0, 1000000000), nameof(ISO9660FileSystemFactory)); // For some reason, BlockCount is always 0, so just put a large value here.

if (mountedPartitions[0] != null) {
if (mountedPartitions[0] != null)
{
var data = mountedPartitions[0];
part.RootPath = data.RootPath;
part.MountedFS = data;
Expand Down Expand Up @@ -97,7 +99,7 @@ public Disk(BlockDevice mainBlockDevice)
parts.Add(new ManagedPartition(part));
}
}
Size = (int)(mainBlockDevice.BlockCount * mainBlockDevice.BlockSize);
Size = (long)(mainBlockDevice.BlockCount * mainBlockDevice.BlockSize);
}
/// <summary>
/// Mounts all of the partitions in the disk
Expand Down Expand Up @@ -156,10 +158,9 @@ public void CreatePartition(int size)
throw new Exception("Creating partitions with GPT style not yet supported!");
}

int location;
int startingSector = 63;
uint amountOfSectors = (uint)(size * 1024 * 1024 / 512);
//TODO: Check if partition is too big
ulong location;
ulong startingSector = 63;
ulong amountOfSectors = ((ulong)size * 1024 * 1024 / 512);

if (Partitions.Count == 0)
{
Expand All @@ -169,7 +170,7 @@ public void CreatePartition(int size)
else if (Partitions.Count == 1)
{
location = 462;
startingSector = (int)(Partitions[0].Host.BlockSize + Partitions[0].Host.BlockCount);
startingSector = (ulong)(Partitions[0].Host.BlockSize + Partitions[0].Host.BlockCount);
}
else if (Partitions.Count == 2)
{
Expand Down Expand Up @@ -215,7 +216,7 @@ public void CreatePartition(int size)
mbrData[510] = boot[0];
mbrData[511] = boot[1];

Partition partion = new(Host, (ulong)startingSector, amountOfSectors);
Partition partion = new(Host, startingSector, amountOfSectors);

Partition.Partitions.Add(partion);
parts.Add(new ManagedPartition(partion));
Expand Down Expand Up @@ -299,11 +300,12 @@ public void MountPartition(int index)

foreach (var item in FileSystemManager.RegisteredFileSystems)
{
if(part.LimitFS != null && item.GetType().Name != part.LimitFS) {
if (part.LimitFS != null && item.GetType().Name != part.LimitFS)
{
Kernel.PrintDebug("Did not mount partition " + index + " as " + item.GetType().Name + " because the partition has been limited to being a " + part.LimitFS);
continue;
}

if (item.IsType(part.Host))
{
Kernel.PrintDebug("Mounted partition.");
Expand Down
3 changes: 2 additions & 1 deletion source/Cosmos.System2/FileSystem/ManagedPartition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class ManagedPartition

public string LimitFS = null;

public ManagedPartition(Partition host, string limitFS = null) {
public ManagedPartition(Partition host, string limitFS = null)
{
Host = host;
LimitFS = limitFS;
}
Expand Down

0 comments on commit 7bd691b

Please sign in to comment.