我是C#的新手,目前正在使用COSMOS为我的OS类制作一个简单的FileSystem。目前我正在尝试实现一个“重新格式化”功能,当在控制台中键入“重新格式化”一词时,操作系统(通过QEMU模拟)会对磁盘进行分区。目前这是我的代码:
public static void console()
{
while (true)
{
Console.WriteLine("Console: ");
String input = Console.ReadLine();
if (input == "exit")
{
Cosmos.Sys.Deboot.ShutDown();
}
else if (input == "cpumem")
{
Console.WriteLine(Cosmos.Kernel.CPU.AmountOfMemory.ToString());
}
else if (input == "restart")
{
Cosmos.Sys.Deboot.Reboot();
}
else if (input == "devices")
{
var devices = Cosmos.Sys.FileSystem.Disk.Devices.ToArray();
}
else if (input == "reformat")
{
try
{
Partition part = null;
for (int j = 0; j < Cosmos.Hardware.BlockDevice.Devices.Count; j++)
{
if (Cosmos.Hardware.BlockDevice.Devices[j] is Partition)
{
part = (Partition)Cosmos.Hardware.BlockDevice.Devices[j];
}
}
var fs = new Cosmos.Sys.FileSystem.FAT32.FAT32(part);
uint cluster = 100;
fs.Format("newCluster", cluster);
}
catch
{
//Do Something warn user.
}
}
}
}
最重要的是这一点:
else if (input == "reformat")
{
try
{
Partition part = null;
for (int j = 0; j < Cosmos.Hardware.BlockDevice.Devices.Count; j++)
{
if (Cosmos.Hardware.BlockDevice.Devices[j] is Partition)
{
part = (Partition)Cosmos.Hardware.BlockDevice.Devices[j];
}
}
var fs = new Cosmos.Sys.FileSystem.FAT32.FAT32(part);
uint cluster = 100;
fs.Format("newCluster", cluster);
}
catch
{
//Do Something warn user.
}
}
这与位于此处的内容类似:http://cosmos-tutorials.webs.com/atafat.html
然而,当我运行它时,我收到此错误:
我相信这是因为我缺乏这条线:
Cosmos.System.Filesystem.FileSystem.AddMapping("C", FATFS);
FATFileList = FATFS.GetRoot();
位于上面的链接中。有没有其他方式来映射?还是我完全错过了什么? COSMOS文档并没有真正说明,源代码对于像我这样的初学者来说实在令人困惑,因为它对函数的工作原理或它们的作用没有任何评论。我使用的是旧版本的COSMOS(Milestone 4),因为它是唯一适用于Visual Studio C#2008的版本。较新版本仅在Visual Studio C#2010中运行。
答案 0 :(得分:0)
啊,我认识到这一点......我必须调试我自己正在研究的Cosmos项目中的类似情况(我正在使用与VS2010兼容的Cosmos,但同样的情况可能也适用于旧版本.. 。)
如果尝试在null对象上调用方法,则会发生这种情况。类型0x ........,方法0x ........特别提到了编译代码中调用失败的位置。 “未找到!”意味着无法找到它正在寻找的方法,大概是因为你在空引用上调用它。
我正在使用VirtualBox进行测试,发现如果您使用的是全新的空白硬盘映像,则不会有分区。因此,条件永远不会满足,您的分区将永远不会被设置,然后Cosmos将尝试在null分区上执行方法!
仔细查看如何设置分区(它已初始化为null)。对于初学者,我会在每次“if(块设备是分区)”条件满足时打印一条简单的消息......我愿意打赌它永远不会打印。
希望这有帮助...我自己仍在学习Cosmos和自定义内核,但在我的案例中修复null引用解决了我的问题。如果这就是问题所在,那么下一步当然是弄清楚为什么你不会首先获得任何分区......
其余的代码看起来很好,但我不确定你是如何实现其余类的。内核调试可能是一场噩梦,祝你好运!