Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File System changes #3085

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
53 changes: 49 additions & 4 deletions source/Cosmos.System2/FileSystem/Disk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Linq;
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 @@ -263,15 +265,26 @@ public void Clear()
}
}

public void FormatPartition(int index, string format, bool quick = true)
/// <summary>
/// Formats a partition to the specified file system type.
/// </summary>
/// <param name="index">The index of the partition to format.</param>
/// <param name="format">The file system type to format the partition to (e.g., "FAT32").</param>
/// <param name="quick">Indicates whether the formatting should be quick. Defaults to true.</param>
/// <param name="FilesystemLetter">The drive letter for the partition. If empty, one will be automatically assigned.</param>
/// <exception cref="NotImplementedException">Thrown when the specified formatting type is not supported.</exception>
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);

if (string.IsNullOrEmpty(FilesystemLetter))
{
FilesystemLetter = VFSManager.GetNextFilesystemLetter();
}
if (format.StartsWith("FAT"))
{
FatFileSystem.CreateFatFileSystem(part.Host, VFSManager.GetNextFilesystemLetter() + ":\\", xSize, format);
FatFileSystem.CreateFatFileSystem(part.Host, FilesystemLetter, xSize, format);
Mount();
}
else
Expand All @@ -295,7 +308,39 @@ 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);
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);
}

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
46 changes: 39 additions & 7 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,11 +1608,22 @@ 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);
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;
xBPB.WriteString(0x52, "FAT32 ");

//TODO: OS Boot Code
Expand Down Expand Up @@ -1691,4 +1723,4 @@ private ulong GetFatSizeSectors()
return Numerator / Denominator + 1;
}
}
}
}
6 changes: 3 additions & 3 deletions source/Cosmos.System2/FileSystem/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ protected FileSystem(Partition aDevice, string aRootPath, long aSize)
/// <summary>
/// Format drive. (delete all)
/// </summary>
/// <param name="aDriveFormat">unused.</param>
/// <param name="aQuick">unused.</param>
/// <param name="aDriveFormat">Drive format. E.g. FAT32, FAT16...</param>
/// <param name="aQuick">Quick format. If false, partition is filled with 0.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <list type = "bullet" >
/// <item>Thrown when the data length is 0 or greater then Int32.MaxValue.</item>
Expand Down 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
Loading