From 63e875cce4037142c805efed25101ed7d62f8efd Mon Sep 17 00:00:00 2001
From: SzymekkYT <69077038+Szymekk44@users.noreply.github.com>
Date: Sat, 21 Sep 2024 13:25:12 +0200
Subject: [PATCH 1/8] Disk formatting type as enum
---
source/Cosmos.System2/FileSystem/Disk.cs | 13 +++----------
.../Cosmos.System2/FileSystem/FAT/FatFileSystem.cs | 14 +++++---------
source/Cosmos.System2/FileSystem/FileSystem.cs | 3 ++-
.../FileSystem/ISO9660/ISO9660FileSystem.cs | 2 +-
4 files changed, 11 insertions(+), 21 deletions(-)
diff --git a/source/Cosmos.System2/FileSystem/Disk.cs b/source/Cosmos.System2/FileSystem/Disk.cs
index a69292c0f3..75c9802b01 100644
--- a/source/Cosmos.System2/FileSystem/Disk.cs
+++ b/source/Cosmos.System2/FileSystem/Disk.cs
@@ -263,21 +263,14 @@ public void Clear()
}
}
- public void FormatPartition(int index, string format, bool quick = true)
+ public void FormatPartition(int index, FileSystemType format, bool quick = true)
{
var part = Partitions[index];
var xSize = (long)(Host.BlockCount * Host.BlockSize / 1024 / 1024);
- if (format.StartsWith("FAT"))
- {
- FatFileSystem.CreateFatFileSystem(part.Host, VFSManager.GetNextFilesystemLetter() + ":\\", xSize, format);
- Mount();
- }
- else
- {
- throw new NotImplementedException(format + " formatting not supported.");
- }
+ FatFileSystem.CreateFatFileSystem(part.Host, VFSManager.GetNextFilesystemLetter() + ":\\", xSize, format);
+ Mount();
}
private readonly FileSystem[] mountedPartitions = new FileSystem[4];
diff --git a/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs b/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
index 40ba64c891..b87b53bf35 100644
--- a/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
+++ b/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
@@ -766,7 +766,7 @@ public FatFileSystem(Partition aDevice, string aRootPath, long aSize, bool fileS
///
///
/// Thrown on fatal error.
- public static FatFileSystem CreateFatFileSystem(Partition aDevice, string aRootPath, long aSize, string aDriveFormat)
+ public static FatFileSystem CreateFatFileSystem(Partition aDevice, string aRootPath, long aSize, FileSystemType aDriveFormat)
{
if (aDevice == null)
{
@@ -1465,7 +1465,7 @@ internal enum FatTypeEnum
/// Thrown on fatal error.
/// Thrown when the data in aData is corrupted.
/// Thrown when FAT type is unknown.
- public override void Format(string aDriveFormat, bool aQuick)
+ public override void Format(FileSystemType aDriveFormat, bool aQuick)
{
/* Parmaters check */
if (Device == null)
@@ -1473,22 +1473,18 @@ public override void Format(string aDriveFormat, bool aQuick)
throw new ArgumentNullException(nameof(Device));
}
- if (aDriveFormat == "FAT32")
+ if (aDriveFormat == FileSystemType.FAT32)
{
mFatType = FatTypeEnum.Fat32;
}
- else if (aDriveFormat == "FAT16")
+ else if (aDriveFormat == FileSystemType.FAT16)
{
throw new NotImplementedException("FAT16 formatting not supported yet.");
}
- else if (aDriveFormat == "FAT12")
+ else if (aDriveFormat == FileSystemType.FAT12)
{
throw new NotImplementedException("FAT12 formatting not supported yet.");
}
- else
- {
- throw new Exception("Unknown FAT type.");
- }
/* FAT Configuration */
BytesPerSector = (uint)Device.BlockSize;
diff --git a/source/Cosmos.System2/FileSystem/FileSystem.cs b/source/Cosmos.System2/FileSystem/FileSystem.cs
index 64c0db0593..69532c78bb 100644
--- a/source/Cosmos.System2/FileSystem/FileSystem.cs
+++ b/source/Cosmos.System2/FileSystem/FileSystem.cs
@@ -252,6 +252,7 @@ protected FileSystem(Partition aDevice, string aRootPath, long aSize)
/// Thrown on fatal error.
/// Thrown when the data in aData is corrupted.
/// Thrown when FAT type is unknown.
- public abstract void Format(string aDriveFormat, bool aQuick);
+ public abstract void Format(FileSystemType aDriveFormat, bool aQuick);
}
+ public enum FileSystemType { FAT12, FAT16, FAT32 }
}
diff --git a/source/Cosmos.System2/FileSystem/ISO9660/ISO9660FileSystem.cs b/source/Cosmos.System2/FileSystem/ISO9660/ISO9660FileSystem.cs
index 96ed44c18b..0da1e90f24 100644
--- a/source/Cosmos.System2/FileSystem/ISO9660/ISO9660FileSystem.cs
+++ b/source/Cosmos.System2/FileSystem/ISO9660/ISO9660FileSystem.cs
@@ -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(FileSystemType aDriveFormat, bool aQuick)
{
throw new NotImplementedException();
}
From 34e156675ac0fa837660aea00496d49d05aabe8d Mon Sep 17 00:00:00 2001
From: SzymekkYT <69077038+Szymekk44@users.noreply.github.com>
Date: Sat, 21 Sep 2024 15:07:02 +0200
Subject: [PATCH 2/8] Changed FAT Tests
---
Tests/Cosmos.System.Tests/DiskManagerTest.cs | 2 +-
Tests/Cosmos.System.Tests/VFSManagerTest.cs | 2 +-
.../System.FileSystem/DiskManagerTest.cs | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/Tests/Cosmos.System.Tests/DiskManagerTest.cs b/Tests/Cosmos.System.Tests/DiskManagerTest.cs
index 41e5c9de23..cd5e499d7d 100644
--- a/Tests/Cosmos.System.Tests/DiskManagerTest.cs
+++ b/Tests/Cosmos.System.Tests/DiskManagerTest.cs
@@ -62,7 +62,7 @@ public void Execute()
Assert.IsTrue(ourPart.RootPath == driveName, "ManagedPartition.RootPath failed drive has wrong name");
- ourDisk.FormatPartition(0, "FAT32", true);
+ ourDisk.FormatPartition(0, FileSystemType.FAT32, true);
var xDi = new DriveInfo(driveName);
diff --git a/Tests/Cosmos.System.Tests/VFSManagerTest.cs b/Tests/Cosmos.System.Tests/VFSManagerTest.cs
index 1ef7c34369..ef4b90f591 100644
--- a/Tests/Cosmos.System.Tests/VFSManagerTest.cs
+++ b/Tests/Cosmos.System.Tests/VFSManagerTest.cs
@@ -66,7 +66,7 @@ public void Test_Disk_Manager()
const string root = @"0:\";
long initialSize = VFSManager.GetTotalSize(root);
- ourDisk.FormatPartition(0, "FAT32", true);
+ ourDisk.FormatPartition(0, FileSystemType.FAT32, true);
Assert.AreEqual(initialSize, VFSManager.GetAvailableFreeSpace(root));
Assert.AreEqual(0, VFSManager.GetDirectoryListing(root).Count);
VFSManager.CreateFile(root + "test.txt");
diff --git a/Tests/Kernels/Cosmos.Kernel.Tests.DiskManager/System.FileSystem/DiskManagerTest.cs b/Tests/Kernels/Cosmos.Kernel.Tests.DiskManager/System.FileSystem/DiskManagerTest.cs
index 41c228f477..c4b6b4e1d0 100644
--- a/Tests/Kernels/Cosmos.Kernel.Tests.DiskManager/System.FileSystem/DiskManagerTest.cs
+++ b/Tests/Kernels/Cosmos.Kernel.Tests.DiskManager/System.FileSystem/DiskManagerTest.cs
@@ -55,7 +55,7 @@ public static void Execute(Debugger mDebugger)
ourDisk.Mount();
- ourDisk.FormatPartition(0, "FAT32", true);
+ ourDisk.FormatPartition(0, FileSystemType.FAT32, true);
mDebugger.Send("Format done testing HDD is really empty");
From 726f63d012964b21c895a92c5cf8a1bbcd6fa7ce Mon Sep 17 00:00:00 2001
From: SzymekkYT <69077038+Szymekk44@users.noreply.github.com>
Date: Sat, 21 Sep 2024 17:01:25 +0200
Subject: [PATCH 3/8] Reverted to string, added custom FilesystemLetter
---
source/Cosmos.System2/FileSystem/Disk.cs | 26 ++++++++++++++++---
.../FileSystem/FAT/FatFileSystem.cs | 14 ++++++----
.../Cosmos.System2/FileSystem/FileSystem.cs | 7 +++--
.../FileSystem/ISO9660/ISO9660FileSystem.cs | 2 +-
4 files changed, 35 insertions(+), 14 deletions(-)
diff --git a/source/Cosmos.System2/FileSystem/Disk.cs b/source/Cosmos.System2/FileSystem/Disk.cs
index 75c9802b01..0ff00fb0a8 100644
--- a/source/Cosmos.System2/FileSystem/Disk.cs
+++ b/source/Cosmos.System2/FileSystem/Disk.cs
@@ -263,14 +263,32 @@ public void Clear()
}
}
- public void FormatPartition(int index, FileSystemType format, bool quick = true)
+ ///
+ /// Formats a partition to the specified file system type.
+ ///
+ /// The index of the partition to format.
+ /// The file system type to format the partition to (e.g., "FAT32").
+ /// Indicates whether the formatting should be quick. Defaults to true.
+ /// The drive letter for the partition. If empty, one will be automatically assigned.
+ /// Thrown when the specified formatting type is not supported.
+ public virtual void FormatPartition(int index, string format, bool quick = true, string FilesystemLetter = "")
{
var part = Partitions[index];
var xSize = (long)(Host.BlockCount * Host.BlockSize / 1024 / 1024);
-
- FatFileSystem.CreateFatFileSystem(part.Host, VFSManager.GetNextFilesystemLetter() + ":\\", xSize, format);
- Mount();
+ if(FilesystemLetter == "" || FilesystemLetter == string.Empty)
+ {
+ FilesystemLetter = VFSManager.GetNextFilesystemLetter();
+ }
+ if (format.StartsWith("FAT"))
+ {
+ FatFileSystem.CreateFatFileSystem(part.Host, FilesystemLetter + ":\\", xSize, format);
+ Mount();
+ }
+ else
+ {
+ throw new NotImplementedException(format + " formatting not supported.");
+ }
}
private readonly FileSystem[] mountedPartitions = new FileSystem[4];
diff --git a/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs b/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
index b87b53bf35..40ba64c891 100644
--- a/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
+++ b/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
@@ -766,7 +766,7 @@ public FatFileSystem(Partition aDevice, string aRootPath, long aSize, bool fileS
///
///
/// Thrown on fatal error.
- public static FatFileSystem CreateFatFileSystem(Partition aDevice, string aRootPath, long aSize, FileSystemType aDriveFormat)
+ public static FatFileSystem CreateFatFileSystem(Partition aDevice, string aRootPath, long aSize, string aDriveFormat)
{
if (aDevice == null)
{
@@ -1465,7 +1465,7 @@ internal enum FatTypeEnum
/// Thrown on fatal error.
/// Thrown when the data in aData is corrupted.
/// Thrown when FAT type is unknown.
- public override void Format(FileSystemType aDriveFormat, bool aQuick)
+ public override void Format(string aDriveFormat, bool aQuick)
{
/* Parmaters check */
if (Device == null)
@@ -1473,18 +1473,22 @@ public override void Format(FileSystemType aDriveFormat, bool aQuick)
throw new ArgumentNullException(nameof(Device));
}
- if (aDriveFormat == FileSystemType.FAT32)
+ if (aDriveFormat == "FAT32")
{
mFatType = FatTypeEnum.Fat32;
}
- else if (aDriveFormat == FileSystemType.FAT16)
+ else if (aDriveFormat == "FAT16")
{
throw new NotImplementedException("FAT16 formatting not supported yet.");
}
- else if (aDriveFormat == FileSystemType.FAT12)
+ else if (aDriveFormat == "FAT12")
{
throw new NotImplementedException("FAT12 formatting not supported yet.");
}
+ else
+ {
+ throw new Exception("Unknown FAT type.");
+ }
/* FAT Configuration */
BytesPerSector = (uint)Device.BlockSize;
diff --git a/source/Cosmos.System2/FileSystem/FileSystem.cs b/source/Cosmos.System2/FileSystem/FileSystem.cs
index 69532c78bb..98d6179772 100644
--- a/source/Cosmos.System2/FileSystem/FileSystem.cs
+++ b/source/Cosmos.System2/FileSystem/FileSystem.cs
@@ -221,8 +221,8 @@ protected FileSystem(Partition aDevice, string aRootPath, long aSize)
///
/// Format drive. (delete all)
///
- /// unused.
- /// unused.
+ /// Drive format. E.g. FAT32, FAT16...
+ /// Quick format. If false, partition is filled with 0.
///
///
/// - Thrown when the data length is 0 or greater then Int32.MaxValue.
@@ -252,7 +252,6 @@ protected FileSystem(Partition aDevice, string aRootPath, long aSize)
/// Thrown on fatal error.
/// Thrown when the data in aData is corrupted.
/// Thrown when FAT type is unknown.
- public abstract void Format(FileSystemType aDriveFormat, bool aQuick);
+ public abstract void Format(string aDriveFormat, bool aQuick);
}
- public enum FileSystemType { FAT12, FAT16, FAT32 }
}
diff --git a/source/Cosmos.System2/FileSystem/ISO9660/ISO9660FileSystem.cs b/source/Cosmos.System2/FileSystem/ISO9660/ISO9660FileSystem.cs
index 0da1e90f24..96ed44c18b 100644
--- a/source/Cosmos.System2/FileSystem/ISO9660/ISO9660FileSystem.cs
+++ b/source/Cosmos.System2/FileSystem/ISO9660/ISO9660FileSystem.cs
@@ -243,7 +243,7 @@ public override void DeleteFile(DirectoryEntry aPath)
{
throw new NotImplementedException("Read only file system");
}
- public override void Format(FileSystemType aDriveFormat, bool aQuick)
+ public override void Format(string aDriveFormat, bool aQuick)
{
throw new NotImplementedException();
}
From 3417fe5bb60dfd11e0ab727fbdbb6b294b2c2367 Mon Sep 17 00:00:00 2001
From: SzymekkYT <69077038+Szymekk44@users.noreply.github.com>
Date: Sat, 21 Sep 2024 17:02:58 +0200
Subject: [PATCH 4/8] reverted tests
---
Tests/Cosmos.System.Tests/DiskManagerTest.cs | 2 +-
Tests/Cosmos.System.Tests/VFSManagerTest.cs | 2 +-
.../System.FileSystem/DiskManagerTest.cs | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/Tests/Cosmos.System.Tests/DiskManagerTest.cs b/Tests/Cosmos.System.Tests/DiskManagerTest.cs
index cd5e499d7d..41e5c9de23 100644
--- a/Tests/Cosmos.System.Tests/DiskManagerTest.cs
+++ b/Tests/Cosmos.System.Tests/DiskManagerTest.cs
@@ -62,7 +62,7 @@ public void Execute()
Assert.IsTrue(ourPart.RootPath == driveName, "ManagedPartition.RootPath failed drive has wrong name");
- ourDisk.FormatPartition(0, FileSystemType.FAT32, true);
+ ourDisk.FormatPartition(0, "FAT32", true);
var xDi = new DriveInfo(driveName);
diff --git a/Tests/Cosmos.System.Tests/VFSManagerTest.cs b/Tests/Cosmos.System.Tests/VFSManagerTest.cs
index ef4b90f591..1ef7c34369 100644
--- a/Tests/Cosmos.System.Tests/VFSManagerTest.cs
+++ b/Tests/Cosmos.System.Tests/VFSManagerTest.cs
@@ -66,7 +66,7 @@ public void Test_Disk_Manager()
const string root = @"0:\";
long initialSize = VFSManager.GetTotalSize(root);
- ourDisk.FormatPartition(0, FileSystemType.FAT32, true);
+ ourDisk.FormatPartition(0, "FAT32", true);
Assert.AreEqual(initialSize, VFSManager.GetAvailableFreeSpace(root));
Assert.AreEqual(0, VFSManager.GetDirectoryListing(root).Count);
VFSManager.CreateFile(root + "test.txt");
diff --git a/Tests/Kernels/Cosmos.Kernel.Tests.DiskManager/System.FileSystem/DiskManagerTest.cs b/Tests/Kernels/Cosmos.Kernel.Tests.DiskManager/System.FileSystem/DiskManagerTest.cs
index c4b6b4e1d0..41c228f477 100644
--- a/Tests/Kernels/Cosmos.Kernel.Tests.DiskManager/System.FileSystem/DiskManagerTest.cs
+++ b/Tests/Kernels/Cosmos.Kernel.Tests.DiskManager/System.FileSystem/DiskManagerTest.cs
@@ -55,7 +55,7 @@ public static void Execute(Debugger mDebugger)
ourDisk.Mount();
- ourDisk.FormatPartition(0, FileSystemType.FAT32, true);
+ ourDisk.FormatPartition(0, "FAT32", true);
mDebugger.Send("Format done testing HDD is really empty");
From 677136aeed5ab5e24428c44d68c613e371b87971 Mon Sep 17 00:00:00 2001
From: Szymekk <69077038+Szymekk44@users.noreply.github.com>
Date: Sat, 21 Sep 2024 17:31:35 +0200
Subject: [PATCH 5/8] Update Disk.cs
---
source/Cosmos.System2/FileSystem/Disk.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/source/Cosmos.System2/FileSystem/Disk.cs b/source/Cosmos.System2/FileSystem/Disk.cs
index 0ff00fb0a8..87f35303ce 100644
--- a/source/Cosmos.System2/FileSystem/Disk.cs
+++ b/source/Cosmos.System2/FileSystem/Disk.cs
@@ -276,7 +276,7 @@ public virtual void FormatPartition(int index, string format, bool quick = true,
var part = Partitions[index];
var xSize = (long)(Host.BlockCount * Host.BlockSize / 1024 / 1024);
- if(FilesystemLetter == "" || FilesystemLetter == string.Empty)
+ if (string.IsNullOrEmpty(FilesystemLetter))
{
FilesystemLetter = VFSManager.GetNextFilesystemLetter();
}
From 8427ece15708bee20644d5389bf17d6f464c3822 Mon Sep 17 00:00:00 2001
From: SzymekkYT <69077038+Szymekk44@users.noreply.github.com>
Date: Sat, 21 Sep 2024 22:03:40 +0200
Subject: [PATCH 6/8] File System Labels [WIP]
---
source/Cosmos.System2/FileSystem/Disk.cs | 31 ++++++++++-
.../FileSystem/FAT/FatFileSystem.cs | 51 +++++++++++++++----
.../Cosmos.System2/FileSystem/FileSystem.cs | 2 +-
.../FileSystem/ISO9660/ISO9660FileSystem.cs | 2 +-
4 files changed, 73 insertions(+), 13 deletions(-)
diff --git a/source/Cosmos.System2/FileSystem/Disk.cs b/source/Cosmos.System2/FileSystem/Disk.cs
index 87f35303ce..e64d6e1867 100644
--- a/source/Cosmos.System2/FileSystem/Disk.cs
+++ b/source/Cosmos.System2/FileSystem/Disk.cs
@@ -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;
@@ -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
@@ -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)
diff --git a/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs b/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
index 40ba64c891..ea0e85f896 100644
--- a/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
+++ b/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
@@ -2,6 +2,7 @@
// #define COSMOSDEBUG
using System;
+using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
@@ -319,7 +320,8 @@ public void ClearAllFat()
/// Output data byte.
/// Thrown when data lenght is greater then Int32.MaxValue.
/// Thrown when data size invalid.
- 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);
@@ -646,6 +648,11 @@ internal ulong FatEntryEofValue()
///
public uint TotalSectorCount { get; private set; }
+ ///
+ /// File System Label used in root path.
+ ///
+ public string FileSystemLabel { get; private set; }
+
///
/// FATs array.
///
@@ -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;
}
@@ -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);
}
///
@@ -962,7 +982,7 @@ internal void Write(long aCluster, byte[] aData, long aSize = 0, long aOffset =
{
aSize = BytesPerCluster;
}
-
+
if (mFatType == FatTypeEnum.Fat32)
{
@@ -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);
@@ -1465,7 +1486,7 @@ internal enum FatTypeEnum
/// Thrown on fatal error.
/// Thrown when the data in aData is corrupted.
/// Thrown when FAT type is unknown.
- public override void Format(string aDriveFormat, bool aQuick)
+ public override void Format(string aDriveFormat, bool aQuick, string Label)
{
/* Parmaters check */
if (Device == null)
@@ -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
}
@@ -1691,4 +1724,4 @@ private ulong GetFatSizeSectors()
return Numerator / Denominator + 1;
}
}
-}
+}
\ No newline at end of file
diff --git a/source/Cosmos.System2/FileSystem/FileSystem.cs b/source/Cosmos.System2/FileSystem/FileSystem.cs
index 98d6179772..5301001627 100644
--- a/source/Cosmos.System2/FileSystem/FileSystem.cs
+++ b/source/Cosmos.System2/FileSystem/FileSystem.cs
@@ -252,6 +252,6 @@ protected FileSystem(Partition aDevice, string aRootPath, long aSize)
/// Thrown on fatal error.
/// Thrown when the data in aData is corrupted.
/// Thrown when FAT type is unknown.
- public abstract void Format(string aDriveFormat, bool aQuick);
+ public abstract void Format(string aDriveFormat, bool aQuick, string Label);
}
}
diff --git a/source/Cosmos.System2/FileSystem/ISO9660/ISO9660FileSystem.cs b/source/Cosmos.System2/FileSystem/ISO9660/ISO9660FileSystem.cs
index 96ed44c18b..67b36b9fe7 100644
--- a/source/Cosmos.System2/FileSystem/ISO9660/ISO9660FileSystem.cs
+++ b/source/Cosmos.System2/FileSystem/ISO9660/ISO9660FileSystem.cs
@@ -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();
}
From 355e2be1d45f789a99bb5c02383744253df2f822 Mon Sep 17 00:00:00 2001
From: SzymekkYT <69077038+Szymekk44@users.noreply.github.com>
Date: Sat, 21 Sep 2024 22:09:41 +0200
Subject: [PATCH 7/8] Added Debug
---
source/Cosmos.System2/FileSystem/Disk.cs | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/source/Cosmos.System2/FileSystem/Disk.cs b/source/Cosmos.System2/FileSystem/Disk.cs
index e64d6e1867..cc7f238189 100644
--- a/source/Cosmos.System2/FileSystem/Disk.cs
+++ b/source/Cosmos.System2/FileSystem/Disk.cs
@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
+using System.Linq;
using System.Net.Mime;
using System.Text;
using Cosmos.HAL;
@@ -326,11 +327,17 @@ public void MountPartition(int index)
Array.Copy(volumeLabelBytes, trimmedVolumeLabelBytes, actualLength);
Label = Encoding.UTF8.GetString(trimmedVolumeLabelBytes);
global::System.Console.WriteLine("Label (saved): " + Label);
+ if(Label.Length == 0)
+ {
+ Label = VFSManager.GetNextFilesystemLetter();
+ global::System.Console.WriteLine("Label empty.");
+ global::System.Console.WriteLine("Generated new Label: " + Label);
+ }
}
else
{
Label = VFSManager.GetNextFilesystemLetter();
- global::System.Console.WriteLine("Generated new Label " + Label);
+ global::System.Console.WriteLine("Generated new Label: " + Label);
}
string xRootPath = string.Concat(Label, VFSBase.VolumeSeparatorChar, VFSBase.DirectorySeparatorChar);
From c70070f75b607d32d1baf23b90e8adfe111f0cd4 Mon Sep 17 00:00:00 2001
From: SzymekkYT <69077038+Szymekk44@users.noreply.github.com>
Date: Sat, 21 Sep 2024 23:06:13 +0200
Subject: [PATCH 8/8] Update FatFileSystem.cs
---
source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs b/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
index ea0e85f896..4e6389bd28 100644
--- a/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
+++ b/source/Cosmos.System2/FileSystem/FAT/FatFileSystem.cs
@@ -1610,7 +1610,7 @@ public override void Format(string aDriveFormat, bool aQuick, string Label)
var SerialID = new byte[4] { 0x01, 0x02, 0x03, 0x04 };
xBPB.Copy(0x43, SerialID, 0, SerialID.Length);
-
+ xBPB.WriteString(0x47, " ");
byte[] labelBytes = Encoding.UTF8.GetBytes(Label);
if (labelBytes.Length < 11)
{
@@ -1622,10 +1622,9 @@ public override void Format(string aDriveFormat, bool aQuick, string Label)
{
throw new Exception("FAT32 label cannot be larger than 11 bytes.");
}
-
xBPB.Copy(0x047, labelBytes, 0, labelBytes.Length);
FileSystemLabel = Label;
-
+ xBPB.WriteString(0x52, "FAT32 ");
//TODO: OS Boot Code
}