Skip to content

Add Multiboot2 modules and Module file system #3099

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

Closed
wants to merge 5 commits into from
Closed
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
14 changes: 11 additions & 3 deletions source/Cosmos.Build.Tasks/CreateLimineConfig.cs
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ public override bool Execute()
var xLabelName = Path.GetFileNameWithoutExtension(xBinName);
using var xWriter = File.CreateText(Path.Combine($"{TargetDirectory}boot/", "limine.cfg"));

xWriter.WriteLineAsync(!String.IsNullOrEmpty(Timeout) ? $"TIMEOUT={Timeout}" : "TIMEOUT=0");
xWriter.WriteLineAsync(!string.IsNullOrEmpty(Timeout) ? $"TIMEOUT={Timeout}" : "TIMEOUT=0");
xWriter.WriteLineAsync("VERBOSE=yes");
xWriter.WriteLineAsync();

@@ -50,12 +50,20 @@ public override bool Execute()
? $"KERNEL_PATH=$boot:///boot/{xBinName}"
: $"KERNEL_PATH=boot:///boot/{xBinName}");

if (Modules == null) return true;
/*if (Modules == null) return true;

foreach (var module in Modules)
{
WriteIndentedLine(xWriter, $"MODULE_PATH=boot:///{module}");
}
}*/

if (!Directory.Exists($"{TargetDirectory}modules/")) return true;

foreach (var module in Directory.GetFiles($"{TargetDirectory}modules/"))
{
Log.LogMessage(MessageImportance.High, "Adding {0} module...", module);
WriteIndentedLine(xWriter, $"MODULE_PATH=boot:///modules/{module}");
}

xWriter.Flush();

222 changes: 114 additions & 108 deletions source/Cosmos.Core/Multiboot/Multiboot2.cs
Original file line number Diff line number Diff line change
@@ -1,117 +1,123 @@
/*
* PROJECT: Cosmos Development
* CONTENT: Multiboot2 class
* PROGRAMMERS: Valentin Charbonnier <valentinbreiz@gmail.com>
* RESOURCES: https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html
*/

using Cosmos.Core.Multiboot.Tags;
using IL2CPU.API.Attribs;

namespace Cosmos.Core.Multiboot
{
/// <summary>
/// Multiboot2 class. Used for multiboot parsing.
/// </summary>
public unsafe class Multiboot2
{
#region Properties

public static BasicMemoryInformation* BasicMemoryInformation { get; set; }
public static bool IsVBEAvailable => Framebuffer->Address != 753664; // Some kinda default number.
public static Framebuffer* Framebuffer { get; set; }
public static MemoryMap* MemoryMap { get; set; }
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo the formatting changes to make reviewing easier.

* PROJECT: Cosmos Development
* CONTENT: Multiboot2 class
* PROGRAMMERS: Valentin Charbonnier <valentinbreiz@gmail.com>
* RESOURCES: https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html
*/

using System.Collections.Generic;
using Cosmos.Core.Multiboot.Tags;
using IL2CPU.API.Attribs;

namespace Cosmos.Core.Multiboot
{
/// <summary>
/// Multiboot2 class. Used for multiboot parsing.
/// </summary>
public unsafe class Multiboot2
{
#region Properties

public static BasicMemoryInformation* BasicMemoryInformation { get; set; }
public static bool IsVBEAvailable => Framebuffer->Address != 753664; // Some kinda default number.
public static Framebuffer* Framebuffer { get; set; }
public static MemoryMap* MemoryMap { get; set; }
public static EFI64* EFI64 { get; set; }
public static AcpiOld* AcpiOld { get; set; }
public static AcpiNew* AcpiNew { get; set; }

#endregion

#region Methods

/// /// <summary>
/// Parse multiboot2 structure
/// </summary>
public static void Init()
{
if (!isInitialized)
{
isInitialized = true;

uint MbAddress = GetMBIAddress();

MB2Tag* Tag;

for (Tag = (MB2Tag*)(MbAddress + 8); Tag->Type != 0; Tag = (MB2Tag*)((byte*)Tag + (Tag->Size + 7 & ~7)))
{
switch (Tag->Type)
{
case 4:
BasicMemoryInformation = (BasicMemoryInformation*)Tag;
break;
case 6:
MemoryMap = (MemoryMap*)Tag;
break;
case 7:
// Ignore because we use Framebuffer tags now :)
//VBEInfo = (VBEInfo*)Tag;
break;
case 8:
Framebuffer = (Framebuffer*)Tag;
break;
case 12:
EFI64 = (EFI64*)Tag;
public static AcpiNew* AcpiNew { get; set; }
public static PointerList<MB2Module> Modules { get; set; }

#endregion

#region Methods

/// <summary>
/// Parse multiboot2 structure
/// </summary>
public static void Init()
{
if (!isInitialized)
{
isInitialized = true;

Modules = new PointerList<MB2Module>();

uint MbAddress = GetMBIAddress();
MB2Tag* Tag;

for (Tag = (MB2Tag*)(MbAddress + 8); Tag->Type != 0; Tag = (MB2Tag*)((byte*)Tag + (Tag->Size + 7 & ~7)))
{
switch (Tag->Type)
{
case 3:
Modules.Add((MB2Module*)Tag);
break;
case 4:
BasicMemoryInformation = (BasicMemoryInformation*)Tag;
break;
case 6:
MemoryMap = (MemoryMap*)Tag;
break;
case 7:
// Ignore because we use Framebuffer tags now :)
//VBEInfo = (VBEInfo*)Tag;
break;
case 8:
Framebuffer = (Framebuffer*)Tag;
break;
case 12:
EFI64 = (EFI64*)Tag;
break;
case 14:
AcpiOld = (AcpiOld*)Tag;
break;
case 15:
AcpiNew = (AcpiNew*)Tag;
break;
default:
break;
}
}
}
}

/// <summary>
/// Get MemLower
/// </summary>
/// <returns>MemLower</returns>
public static uint GetMemLower()
{
return BasicMemoryInformation->MemLower;
}

/// <summary>
/// Get MemUpper
/// </summary>
/// <returns>MemUpper</returns>
public static uint GetMemUpper()
{
return BasicMemoryInformation->MemUpper;
}

/// <summary>
/// Checks if Multiboot returned a memory map
/// </summary>
/// <returns>True if is available, false if not</returns>
public static bool MemoryMapExists() => MemoryMap != null;

/// /// <summary>
/// Get Multiboot address. Plugged.
/// </summary>
/// <returns>The Multiboot Address</returns>
[PlugMethod(PlugRequired = true)]
public static uint GetMBIAddress() => throw null;

#endregion

#region Fields

private static bool isInitialized = false;

#endregion
}
break;
default:
break;
}
}
}
}
/// <summary>
/// Get MemLower
/// </summary>
/// <returns>MemLower</returns>
public static uint GetMemLower()
{
return BasicMemoryInformation->MemLower;
}
/// <summary>
/// Get MemUpper
/// </summary>
/// <returns>MemUpper</returns>
public static uint GetMemUpper()
{
return BasicMemoryInformation->MemUpper;
}
/// <summary>
/// Checks if Multiboot returned a memory map
/// </summary>
/// <returns>True if is available, false if not</returns>
public static bool MemoryMapExists() => MemoryMap != null;
/// /// <summary>
/// Get Multiboot address. Plugged.
/// </summary>
/// <returns>The Multiboot Address</returns>
[PlugMethod(PlugRequired = true)]
public static uint GetMBIAddress() => throw null;
#endregion
#region Fields
private static bool isInitialized = false;
#endregion
}
}
40 changes: 40 additions & 0 deletions source/Cosmos.Core/Multiboot/Tags/MB2Module.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Cosmos.Core.Multiboot.Tags
{
/// <summary>
/// Multiboot2 Module tag
/// </summary>
[StructLayout(LayoutKind.Explicit, Size = 44)]
public unsafe readonly struct MB2Module
{
/// <summary>
/// Information about the tag.
/// </summary>
[FieldOffset(0)]
public readonly MB2Tag Info;

/// <summary>
/// Starting address of the module.
/// </summary>
[FieldOffset(8)]
public readonly uint ModuleStartAddress;

/// <summary>
/// Ending address of the module.
/// </summary>
[FieldOffset(16)]
public readonly uint ModuleEndAddress;

/// <summary>
/// Zero-end string of the command line.
/// </summary>
[FieldOffset(24)]
public readonly char* CommandLine;
}
}
109 changes: 109 additions & 0 deletions source/Cosmos.Core/PointerList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;

namespace Cosmos.Core
{
/// <summary>
/// PointerList class. Used for making list of pointers.
/// </summary>
/// <typeparam name="T">Non-pointer type of pointer.</typeparam>
public unsafe class PointerList<T> : IEnumerable where T : unmanaged
{
private T*[] values;
private int index = 0;
private int maxLength;
private int enumerator_index = -1;

/// <summary>
/// Count of pointers.
/// </summary>
public int Count
{
get
{
return index;
}
}

private void expand()
{
T*[] newArray = new T*[maxLength + 1];

if (maxLength > 0)
{
for (int i = 0; i < Count; i++)
{
newArray[i] = values[i];
}
}

values = newArray;
}

public PointerList(int maxLength = 0)
{
this.maxLength = maxLength;

if (maxLength > 0)
{
values = new T*[maxLength];
}
}

public void Add(T* value)
{
if (index + 1 >= maxLength)
{
expand();
}

values[index++] = value;
}

public T* this[int key]
{
get
{
return values[key];
}
}


public IEnumerator GetEnumerator()
{
return new PointerListEnum<T>(this);
}
}

/// <summary>
/// Enumerator for PointerList class.
/// </summary>
public unsafe class PointerListEnum<T> : IEnumerator where T : unmanaged
{
private int index = -1;
private PointerList<T> list;

public object Current => *list[index];

public PointerListEnum(PointerList<T> list)
{
this.list = list;
}

bool IEnumerator.MoveNext()
{
index++;
return index < list.Count;
}

void IEnumerator.Reset()
{
index = -1;
}
}
}
106 changes: 58 additions & 48 deletions source/Cosmos.Debug.GDB/packages.lock.json
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
{
"version": 1,
"dependencies": {
".NETFramework,Version=v4.7.2": {
"Microsoft.NETFramework.ReferenceAssemblies": {
"type": "Direct",
"requested": "[1.0.3, )",
"resolved": "1.0.3",
"contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==",
"dependencies": {
"Microsoft.NETFramework.ReferenceAssemblies.net472": "1.0.3"
}
},
"Microsoft.NETFramework.ReferenceAssemblies.net472": {
"type": "Transitive",
"resolved": "1.0.3",
"contentHash": "0E7evZXHXaDYYiLRfpyXvCh+yzM2rNTyuZDI+ZO7UUqSc6GfjePiXTdqJGtgIKUwdI81tzQKmaWprnUiPj9hAw=="
},
"Microsoft.Win32.Registry": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "dDoKi0PnDz31yAyETfRntsLArTlVAVzUzCIvvEDsDsucrl33Dl8pIJG06ePTJTI3tGpeyHS9Cq7Foc/s4EeKcg==",
"dependencies": {
"System.Security.AccessControl": "5.0.0",
"System.Security.Principal.Windows": "5.0.0"
}
},
"System.Security.AccessControl": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "dagJ1mHZO3Ani8GH0PHpPEe/oYO+rVdbQjvjJkBRNQkX4t0r1iaeGn8+/ybkSLEan3/slM0t59SVdHzuHf2jmw==",
"dependencies": {
"System.Security.Principal.Windows": "5.0.0"
}
},
"System.Security.Principal.Windows": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "t0MGLukB5WAVU9bO3MGzvlGnyJPgUlcwerXn1kzBRjwLKixT96XV0Uza41W49gVd8zEMFu9vQEFlv0IOrytICA=="
},
"cosmos.build.common": {
"type": "Project",
"dependencies": {
"Microsoft.Win32.Registry": "[5.0.0, )"
}
}
}
}
{
"version": 1,
"dependencies": {
".NETFramework,Version=v4.7.2": {
"Microsoft.Win32.Registry": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "dDoKi0PnDz31yAyETfRntsLArTlVAVzUzCIvvEDsDsucrl33Dl8pIJG06ePTJTI3tGpeyHS9Cq7Foc/s4EeKcg==",
"dependencies": {
"System.Security.AccessControl": "5.0.0",
"System.Security.Principal.Windows": "5.0.0"
}
},
"System.Security.AccessControl": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "dagJ1mHZO3Ani8GH0PHpPEe/oYO+rVdbQjvjJkBRNQkX4t0r1iaeGn8+/ybkSLEan3/slM0t59SVdHzuHf2jmw==",
"dependencies": {
"System.Security.Principal.Windows": "5.0.0"
}
},
"System.Security.Principal.Windows": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "t0MGLukB5WAVU9bO3MGzvlGnyJPgUlcwerXn1kzBRjwLKixT96XV0Uza41W49gVd8zEMFu9vQEFlv0IOrytICA=="
},
"cosmos.build.common": {
"type": "Project",
"dependencies": {
"Microsoft.Win32.Registry": "[5.0.0, )"
}
}
},
".NETFramework,Version=v4.7.2/win7-x86": {
"Microsoft.Win32.Registry": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "dDoKi0PnDz31yAyETfRntsLArTlVAVzUzCIvvEDsDsucrl33Dl8pIJG06ePTJTI3tGpeyHS9Cq7Foc/s4EeKcg==",
"dependencies": {
"System.Security.AccessControl": "5.0.0",
"System.Security.Principal.Windows": "5.0.0"
}
},
"System.Security.AccessControl": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "dagJ1mHZO3Ani8GH0PHpPEe/oYO+rVdbQjvjJkBRNQkX4t0r1iaeGn8+/ybkSLEan3/slM0t59SVdHzuHf2jmw==",
"dependencies": {
"System.Security.Principal.Windows": "5.0.0"
}
},
"System.Security.Principal.Windows": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "t0MGLukB5WAVU9bO3MGzvlGnyJPgUlcwerXn1kzBRjwLKixT96XV0Uza41W49gVd8zEMFu9vQEFlv0IOrytICA=="
}
}
}
}
33 changes: 33 additions & 0 deletions source/Cosmos.System2/FileSystem/ModuleFS/MFSDirectoryEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cosmos.System.FileSystem.Listing;

namespace Cosmos.System.FileSystem.ModuleFS
{
public class MFSDirectoryEntry : DirectoryEntry
{
private ModuleFileSystem mfs;
private string path;
private string name;

public MFSDirectoryEntry(ModuleFileSystem fs, string fullPath, string fileName, uint size, DirectoryEntryTypeEnum entryType) : base(fs, null, fullPath, fileName, size, entryType)
{
mfs = fs;
path = fullPath;
name = fileName;
}

public override Stream GetFileStream()
{
return new MemoryStream(mfs.MFSReadFile(path.Replace(mfs.rootPath, "")));
}

public override long GetUsedSpace() => 0;
public override void SetName(string aName) => throw new NotImplementedException("Read-only File system");
public override void SetSize(long aSize) => throw new NotImplementedException("Read-only File system");
}
}
88 changes: 88 additions & 0 deletions source/Cosmos.System2/FileSystem/ModuleFS/ModuleFileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cosmos.Core.Multiboot;
using Cosmos.Core.Multiboot.Tags;
using Cosmos.HAL.BlockDevice;
using Cosmos.System.FileSystem.Listing;

namespace Cosmos.System.FileSystem.ModuleFS
{
public class ModuleFileSystem : FileSystem
{
private string _label = "MFSDisk";
public override long AvailableFreeSpace => 0;
public override long TotalFreeSpace => 0;
public override string Type => "ModuleFS";
public override string Label { get => _label; set => _label = value; }

internal string rootPath;

public ModuleFileSystem(long Size, string rootPath) : base(null, rootPath, Size)
{
this.rootPath = rootPath;
}

private unsafe string GetName(MB2Module module)
{
string str = "";

for (int i = 0; module.CommandLine[i] != 0; i++)
{
str = str + module.CommandLine[i];
}
return str;
}

internal unsafe byte[] MFSReadFile(string path)
{
byte[] data = null;

foreach (MB2Module module in Multiboot2.Modules)
{
if (path == GetName(module))
{
byte* file_pointer = (byte*)module.ModuleStartAddress;
data = new byte[module.ModuleEndAddress - module.ModuleStartAddress];

for (int i = 0; i < module.ModuleEndAddress - module.ModuleStartAddress; i++)
{
data[i] = file_pointer[i];
}
}
}

return data;
}

public override DirectoryEntry CreateDirectory(DirectoryEntry aParentDirectory, string aNewDirectory) => throw new NotImplementedException();
public override DirectoryEntry CreateFile(DirectoryEntry aParentDirectory, string aNewFile) => throw new NotImplementedException();
public override void DeleteDirectory(DirectoryEntry aPath) => throw new NotImplementedException();
public override void DeleteFile(DirectoryEntry aPath) => throw new NotImplementedException();

public override void DisplayFileSystemInfo()
{
global::System.Console.WriteLine("Volume label is " + _label);
}

public override void Format(string aDriveFormat, bool aQuick) => throw new NotImplementedException("Read-only File system");
public override List<DirectoryEntry> GetDirectoryListing(DirectoryEntry baseDirectory)
{
List<DirectoryEntry > list = new List<DirectoryEntry>();

foreach (MB2Module module in Multiboot2.Modules)
{
list.Add(new MFSDirectoryEntry(this, rootPath + GetName(module), GetName(module), module.ModuleEndAddress - module.ModuleStartAddress, DirectoryEntryTypeEnum.File));
}
return list;
}

public override DirectoryEntry GetRootDirectory()
{
return new MFSDirectoryEntry(this, rootPath, "RootDir", 0, DirectoryEntryTypeEnum.Directory);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cosmos.HAL.BlockDevice;

namespace Cosmos.System.FileSystem.ModuleFS
{
public class ModuleFileSystemFactory : FileSystemFactory
{
private static ModuleFileSystem mfs_instance = null;
public override string Name => "ModuleFS";

public override FileSystem Create(Partition aDevice, string aRootPath, long aSize) => new ModuleFileSystem(aSize, aRootPath);
public override bool IsType(Partition aDevice)
{
return false;
}
}
}
2,532 changes: 1,259 additions & 1,273 deletions source/Cosmos.VS.Windows/packages.lock.json

Large diffs are not rendered by default.