Skip to content

Commit

Permalink
WIP - BareMetal (#1102)
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
tgiphil committed Aug 1, 2023
1 parent 119a119 commit ba149e5
Show file tree
Hide file tree
Showing 22 changed files with 431 additions and 160 deletions.
174 changes: 168 additions & 6 deletions Source/Mosa.BareMetal.HelloWorld.x86/Boot.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System;
using Mosa.DeviceDriver.ISA;
using Mosa.DeviceSystem;
using Mosa.DeviceSystem.PCI;
using Mosa.DeviceSystem.Service;
using Mosa.FileSystem.FAT;
using Mosa.Kernel.BareMetal;
using Mosa.Kernel.BareMetal.x86;
using Mosa.Runtime.Plug;

namespace Mosa.BareMetal.HelloWorld.x86;

Expand All @@ -11,13 +16,143 @@ namespace Mosa.BareMetal.HelloWorld.x86;
/// </summary>
public static class Boot
{
/// <summary>
/// Main
/// </summary>
public static void Main()
{
VGAText.SetColor(VGAColor.Brown);
VGAText.Write((byte)'+');
Debug.WriteLine("Boot::Main()");

var deviceService = Mosa.Kernel.BareMetal.Kernel.ServiceManger.GetFirstService<DeviceService>();

Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.Write("> Probing for ISA devices...");

var isaDevices = deviceService.GetChildrenOf(deviceService.GetFirstDevice<ISABus>());
Console.WriteLine("[Completed: " + isaDevices.Count + " found]");

foreach (var device in isaDevices)
{
Console.Write(" ");
Bullet(ConsoleColor.Yellow);
Console.Write(" ");
InBrackets(device.Name, ConsoleColor.White, ConsoleColor.Green);
Console.WriteLine();
}

Console.Write("> Probing for PCI devices...");
var devices = deviceService.GetDevices<PCIDevice>();
Console.WriteLine("[Completed: " + devices.Count + " found]");

foreach (var device in devices)
{
Console.Write(" ");
Bullet(ConsoleColor.Yellow);
Console.Write(" ");

var pciDevice = device.DeviceDriver as PCIDevice;
InBrackets(device.Name + ": " + pciDevice.VendorID.ToString("x") + ":" + pciDevice.DeviceID.ToString("x") + " " + pciDevice.SubSystemID.ToString("x") + ":" + pciDevice.SubSystemVendorID.ToString("x") + " (" + pciDevice.ClassCode.ToString("x") + ":" + pciDevice.SubClassCode.ToString("x") + ":" + pciDevice.ProgIF.ToString("x") + ":" + pciDevice.RevisionID.ToString("x") + ")", ConsoleColor.White, ConsoleColor.Green);

var children = deviceService.GetChildrenOf(device);

if (children.Count != 0)
{
var child = children[0];

Console.WriteLine();
Console.Write(" ");

var pciDevice2 = child.DeviceDriver as PCIDevice;
InBrackets(child.Name, ConsoleColor.White, ConsoleColor.Green);
}

Console.WriteLine();
}

Console.Write("> Probing for disk controllers...");
var diskcontrollers = deviceService.GetDevices<IDiskControllerDevice>();
Console.WriteLine("[Completed: " + diskcontrollers.Count + " found]");

foreach (var device in diskcontrollers)
{
Console.Write(" ");
Bullet(ConsoleColor.Yellow);
Console.Write(" ");
InBrackets(device.Name, ConsoleColor.White, ConsoleColor.Green);
Console.WriteLine();
}

Console.Write("> Probing for disks...");
var disks = deviceService.GetDevices<IDiskDevice>();
Console.WriteLine("[Completed: " + disks.Count + " found]");

foreach (var disk in disks)
{
Console.Write(" ");
Bullet(ConsoleColor.Yellow);
Console.Write(" ");
InBrackets(disk.Name, ConsoleColor.White, ConsoleColor.Green);
Console.Write(" " + (disk.DeviceDriver as IDiskDevice).TotalBlocks + " blocks");
Console.WriteLine();
}

var partitionService = Mosa.Kernel.BareMetal.Kernel.ServiceManger.GetFirstService<PartitionService>();

partitionService.CreatePartitionDevices();

Console.Write("> Finding partitions...");
var partitions = deviceService.GetDevices<IPartitionDevice>();
Console.WriteLine("[Completed: " + partitions.Count + " found]");

foreach (var partition in partitions)
{
Console.Write(" ");
Bullet(ConsoleColor.Yellow);
Console.Write(" ");
InBrackets(partition.Name, ConsoleColor.White, ConsoleColor.Green);
Console.Write(" " + (partition.DeviceDriver as IPartitionDevice).BlockCount.ToString() + " blocks");
Console.WriteLine();
}

//Console.Write("> Finding file systems...");

//foreach (var partition in partitions)
//{
// var fat = new FatFileSystem(partition.DeviceDriver as IPartitionDevice);

// if (fat.IsValid)
// {
// Console.WriteLine("Found a FAT file system!");

// const string filename = "TEST.TXT";

// var location = fat.FindEntry(filename);

// if (location.IsValid)
// {
// Console.Write("Found: " + filename);

// var fatFileStream = new FatFileStream(fat, location);

// uint len = (uint)fatFileStream.Length;

// Console.WriteLine(" - Length: " + len + " bytes");

// Console.Write("Reading File: ");

// for (; ; )
// {
// int i = fatFileStream.ReadByte();

// if (i < 0)
// break;

// Console.Write((char)i);
// }

// Console.WriteLine();
// }
// }
//}

while (true)
{ }
Expand All @@ -27,5 +162,32 @@ public static void Main()
public static void SetBootOptions()
{
BootOptions.EnableDebugOutput = true;
//BootOptions.EnableVirtualMemory = true;
//BootOptions.EnableMinimalBoot = true;
}

public static void InBrackets(string message, ConsoleColor outerColor, ConsoleColor innerColor)
{
var restore = Console.ForegroundColor;
Console.ForegroundColor = outerColor;
Console.Write("[");
Console.ForegroundColor = innerColor;
Console.Write(message);
Console.ForegroundColor = outerColor;
Console.Write("]");
Console.ForegroundColor = restore;
}

public static void Bullet(ConsoleColor color)
{
var restore = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write("*");
Console.ForegroundColor = restore;
}

public static void Include()
{
Mosa.Kernel.BareMetal.x86.Scheduler.SwitchToThread(null);
}
}
3 changes: 3 additions & 0 deletions Source/Mosa.Compiler.Framework/Linker/MosaLinker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ private uint ResolveSymbolLocation(SectionKind section, ulong VirtualAddress)
if (symbol.IsResolved)
continue;

if (symbol.Size == 0)
continue;

symbol.SectionOffset = position;
symbol.VirtualAddress = VirtualAddress + position;

Expand Down
11 changes: 0 additions & 11 deletions Source/Mosa.Demo.CoolWorld.x86/Plugs/EnvironmentPlug.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Source/Mosa.Demo.HelloWorld.x86/Plugs/EnvironmentPlug.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Source/Mosa.Demo.SVGAWorld.x86/Plugs/EnvironmentPlug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,4 @@ internal static void FailFast(string message)
Console.WriteLine("***FAIL FAST*** " + message);
for (; ; );
}

[Plug("System.Environment::GetProcessorCount")]
internal static int GetProcessorCount() => 1; // TODO: APIC
}
19 changes: 0 additions & 19 deletions Source/Mosa.Demo.TestWorld.x64/Plugs/EnvironmentPlug.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Source/Mosa.Demo.TestWorld.x86/Plugs/EnvironmentPlug.cs

This file was deleted.

2 changes: 2 additions & 0 deletions Source/Mosa.DeviceSystem/HAL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ public static class HAL
public static void Assert(bool condition, string message)
{
if (!condition)
{
hardwareAbstraction.Abort(message);
}
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions Source/Mosa.Kernel.BareMetal.x86/VGAConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private static void Evaluate()
case 34: VGAText.SetColor(VGAColor.Blue); break;
case 35: VGAText.SetColor(VGAColor.Magenta); break;
case 36: VGAText.SetColor(VGAColor.Cyan); break;
case 37: VGAText.SetColor(VGAColor.LightGray); break;
case 37: VGAText.SetColor(VGAColor.White); break;

case 90: VGAText.SetColor(VGAColor.DarkGray); break;
case 91: VGAText.SetColor(VGAColor.LightRed); break;
Expand All @@ -153,7 +153,7 @@ private static void Evaluate()
case 94: VGAText.SetColor(VGAColor.LightBlue); break;
case 95: VGAText.SetColor(VGAColor.LightMagenta); break;
case 96: VGAText.SetColor(VGAColor.LightCyan); break;
case 97: VGAText.SetColor(VGAColor.White); break;
case 97: VGAText.SetColor(VGAColor.LightGray); break;

case 40: VGAText.SetBackground(VGAColor.Black); break;
case 41: VGAText.SetBackground(VGAColor.Red); break;
Expand All @@ -171,7 +171,7 @@ private static void Evaluate()
case 104: VGAText.SetBackground(VGAColor.LightBlue); break;
case 105: VGAText.SetBackground(VGAColor.LightMagenta); break;
case 106: VGAText.SetBackground(VGAColor.LightCyan); break;
case 107: VGAText.SetBackground(VGAColor.White); break;
case 107: VGAText.SetBackground(VGAColor.LightGray); break;

// FUTURE:
//0 Reset all attributes
Expand Down

0 comments on commit ba149e5

Please sign in to comment.