Skip to content

Commit

Permalink
File System Labels [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
Szymekk44 committed Sep 21, 2024
1 parent 677136a commit 8427ece
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 13 deletions.
31 changes: 29 additions & 2 deletions source/Cosmos.System2/FileSystem/Disk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.ComponentModel.Design.Serialization;
using System.Net.Mime;
using System.Text;
using Cosmos.HAL;
using Cosmos.HAL.BlockDevice;
using Cosmos.System.FileSystem.FAT;
using Cosmos.System.FileSystem.ISO9660;
Expand Down Expand Up @@ -282,7 +283,7 @@ public virtual void FormatPartition(int index, string format, bool quick = true,
}
if (format.StartsWith("FAT"))
{
FatFileSystem.CreateFatFileSystem(part.Host, FilesystemLetter + ":\\", xSize, format);
FatFileSystem.CreateFatFileSystem(part.Host, FilesystemLetter, xSize, format);
Mount();
}
else
Expand All @@ -306,7 +307,33 @@ public void MountPartition(int index)
//We already mounted this partiton
return;
}
string xRootPath = string.Concat(VFSManager.GetNextFilesystemLetter(), VFSBase.VolumeSeparatorChar, VFSBase.DirectorySeparatorChar);
string Label = "";

var xBPB = part.Host.NewBlockArray(1);
part.Host.ReadBlock(0UL, 1U, ref xBPB);
ushort xSig = BitConverter.ToUInt16(xBPB, 510);
if (xSig == 0xAA55) //FAT signature
{
global::System.Console.WriteLine("Correct FAT signature");
byte[] volumeLabelBytes = new byte[11];
Array.Copy(xBPB, 0x047, volumeLabelBytes, 0, 11);
int actualLength = Array.IndexOf(volumeLabelBytes, (byte)0);
if (actualLength == -1)
{
actualLength = volumeLabelBytes.Length;
}
byte[] trimmedVolumeLabelBytes = new byte[actualLength];
Array.Copy(volumeLabelBytes, trimmedVolumeLabelBytes, actualLength);
Label = Encoding.UTF8.GetString(trimmedVolumeLabelBytes);
global::System.Console.WriteLine("Label (saved): " + Label);
}
else
{
Label = VFSManager.GetNextFilesystemLetter();
global::System.Console.WriteLine("Generated new Label " + Label);
}

string xRootPath = string.Concat(Label, VFSBase.VolumeSeparatorChar, VFSBase.DirectorySeparatorChar);
var xSize = (long)(Host.BlockCount * Host.BlockSize / 1024 / 1024);

foreach (var item in FileSystemManager.RegisteredFileSystems)
Expand Down
51 changes: 42 additions & 9 deletions source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// #define COSMOSDEBUG

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
Expand Down Expand Up @@ -319,7 +320,8 @@ public void ClearAllFat()
/// <param name="aData">Output data byte.</param>
/// <exception cref="OverflowException">Thrown when data lenght is greater then Int32.MaxValue.</exception>
/// <exception cref="Exception">Thrown when data size invalid.</exception>
private void ReadFatSector(ulong aSector, ref byte[] aData) {
private void ReadFatSector(ulong aSector, ref byte[] aData)
{
Global.Debugger.SendInternal("-- FatFileSystem.ReadFatSector --");
ulong xSector = mFatSector + aSector;
Global.Debugger.SendInternal("xSector =" + xSector);
Expand Down Expand Up @@ -646,6 +648,11 @@ internal ulong FatEntryEofValue()
/// </summary>
public uint TotalSectorCount { get; private set; }

/// <summary>
/// File System Label used in root path.
/// </summary>
public string FileSystemLabel { get; private set; }

/// <summary>
/// FATs array.
/// </summary>
Expand Down Expand Up @@ -781,7 +788,7 @@ public static FatFileSystem CreateFatFileSystem(Partition aDevice, string aRootP
Global.Debugger.SendInternal("Creating a new " + aDriveFormat + " FileSystem.");

var fs = new FatFileSystem(aDevice, aRootPath, aSize, false);
fs.Format(aDriveFormat, true);
fs.Format(aDriveFormat, true, aRootPath);
return fs;
}

Expand Down Expand Up @@ -866,6 +873,19 @@ internal void ReadBootSector()
{
mFats[i] = new Fat(this, ReservedSectorCount + i * FatSectorCount);
}

// Read volume label (11 bytes)
byte[] volumeLabelBytes = new byte[11];
Array.Copy(xBPB, 0x047, volumeLabelBytes, 0, 11);
int actualLength = Array.IndexOf(volumeLabelBytes, (byte)0);
if (actualLength == -1)
{
actualLength = volumeLabelBytes.Length;
}
byte[] trimmedVolumeLabelBytes = new byte[actualLength];
Array.Copy(volumeLabelBytes, trimmedVolumeLabelBytes, actualLength);

FileSystemLabel = Encoding.UTF8.GetString(trimmedVolumeLabelBytes);
}

/// <summary>
Expand Down Expand Up @@ -962,7 +982,7 @@ internal void Write(long aCluster, byte[] aData, long aSize = 0, long aOffset =
{
aSize = BytesPerCluster;
}


if (mFatType == FatTypeEnum.Fat32)
{
Expand Down Expand Up @@ -997,6 +1017,7 @@ public override void DisplayFileSystemInfo()
global::System.Console.WriteLine("Root Sector Count = " + RootSectorCount);
global::System.Console.WriteLine("Sectors per Cluster = " + SectorsPerCluster);
global::System.Console.WriteLine("Total Sector Count = " + TotalSectorCount);
global::System.Console.WriteLine("File System Label = " + FileSystemLabel);

Global.Debugger.SendInternal("Bytes per Cluster =");
Global.Debugger.SendInternal(BytesPerCluster);
Expand Down Expand Up @@ -1465,7 +1486,7 @@ internal enum FatTypeEnum
/// <exception cref="ArrayTypeMismatchException">Thrown on fatal error.</exception>
/// <exception cref="InvalidCastException">Thrown when the data in aData is corrupted.</exception>
/// <exception cref="NotSupportedException">Thrown when FAT type is unknown.</exception>
public override void Format(string aDriveFormat, bool aQuick)
public override void Format(string aDriveFormat, bool aQuick, string Label)
{
/* Parmaters check */
if (Device == null)
Expand Down Expand Up @@ -1587,12 +1608,24 @@ public override void Format(string aDriveFormat, bool aQuick)
xBPB.Write8(0x42, 0x29); //signature

var SerialID = new byte[4] { 0x01, 0x02, 0x03, 0x04 };
var VolumeLabel = "COSMOSDISK";

xBPB.Copy(0x43, SerialID, 0, SerialID.Length);
xBPB.WriteString(0x47, " ");
xBPB.WriteString(0x47, VolumeLabel);
xBPB.WriteString(0x52, "FAT32 ");

byte[] labelBytes = Encoding.UTF8.GetBytes(Label);
if (labelBytes.Length < 11)
{
byte[] paddedLabelBytes = new byte[11];
Array.Copy(labelBytes, paddedLabelBytes, labelBytes.Length);
labelBytes = paddedLabelBytes;
}
else if (labelBytes.Length > 11)
{
throw new Exception("FAT32 label cannot be larger than 11 bytes.");
}

xBPB.Copy(0x047, labelBytes, 0, labelBytes.Length);
FileSystemLabel = Label;


//TODO: OS Boot Code
}
Expand Down Expand Up @@ -1691,4 +1724,4 @@ private ulong GetFatSizeSectors()
return Numerator / Denominator + 1;
}
}
}
}
2 changes: 1 addition & 1 deletion source/Cosmos.System2/FileSystem/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,6 @@ protected FileSystem(Partition aDevice, string aRootPath, long aSize)
/// <exception cref="ArrayTypeMismatchException">Thrown on fatal error.</exception>
/// <exception cref="InvalidCastException">Thrown when the data in aData is corrupted.</exception>
/// <exception cref="NotSupportedException">Thrown when FAT type is unknown.</exception>
public abstract void Format(string aDriveFormat, bool aQuick);
public abstract void Format(string aDriveFormat, bool aQuick, string Label);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public override void DeleteFile(DirectoryEntry aPath)
{
throw new NotImplementedException("Read only file system");
}
public override void Format(string aDriveFormat, bool aQuick)
public override void Format(string aDriveFormat, bool aQuick, string Label)
{
throw new NotImplementedException();
}
Expand Down

0 comments on commit 8427ece

Please sign in to comment.