Kernel: root partition is now passed from the commandline

This commit is contained in:
Bananymous 2023-03-30 16:37:53 +03:00
parent c1dbafc101
commit 2995a36942
4 changed files with 49 additions and 42 deletions

19
disk.sh
View File

@ -30,14 +30,9 @@ sed -e 's/\s*\([-\+[:alnum:]]*\).*/\1/' << EOF | fdisk $DISK_NAME
t # set type
2 # ... of partition 2
20 # Linux filesystem
x # expert menu
n # partition name
3 # ... of partition 3
mount-test
n # partition name
2 # ... of partition 2
banan-root
r # back to main menu
t # set type
3 # ... of partition 3
20 # Linux filesystem
w # write changes
EOF
@ -63,16 +58,16 @@ sudo grub-install --no-floppy --target=i386-pc --modules="normal ext2 multiboot"
echo -e '
menuentry "banan-os" {
multiboot /boot/banan-os.kernel
multiboot /boot/banan-os.kernel root=/dev/hda1
}
menuentry "banan-os (no serial)" {
multiboot /boot/banan-os.kernel noserial
multiboot /boot/banan-os.kernel root=/dev/hda1 noserial
}
menuentry "banan-os (no apic)" {
multiboot /boot/banan-os.kernel noapic
multiboot /boot/banan-os.kernel root=/dev/hda1 noapic
}
menuentry "banan-os (no apic, no serial)" {
multiboot /boot/banan-os.kernel noapic noserial
multiboot /boot/banan-os.kernel root=/dev/hda1 noapic noserial
}
' | sudo tee ${MOUNT_DIR}/boot/grub/grub.cfg

View File

@ -12,7 +12,7 @@ namespace Kernel
class VirtualFileSystem : public FileSystem
{
public:
static BAN::ErrorOr<void> initialize();
static BAN::ErrorOr<void> initialize(BAN::StringView);
static VirtualFileSystem& get();
virtual ~VirtualFileSystem() {};

View File

@ -12,7 +12,7 @@ namespace Kernel
static VirtualFileSystem* s_instance = nullptr;
BAN::ErrorOr<void> VirtualFileSystem::initialize()
BAN::ErrorOr<void> VirtualFileSystem::initialize(BAN::StringView root)
{
ASSERT(s_instance == nullptr);
s_instance = new VirtualFileSystem();
@ -20,8 +20,13 @@ namespace Kernel
return BAN::Error::from_errno(ENOMEM);
BAN::ScopeGuard guard([] { delete s_instance; s_instance = nullptr; } );
auto partition_inode = TRY(DeviceManager::get().read_directory_inode("hda1"));
if (root.size() < 5 || root.substring(0, 5) != "/dev/")
return BAN::Error::from_c_string("root must be in /dev/");
root = root.substring(5);
auto partition_inode = TRY(DeviceManager::get().read_directory_inode(root));
s_instance->m_root_fs = TRY(Ext2FS::create(*(Partition*)partition_inode.ptr()));
TRY(s_instance->mount(&DeviceManager::get(), "/dev"));
guard.disable();
@ -76,7 +81,7 @@ namespace Kernel
const auto path_parts = TRY(path.split('/'));
for (const auto& path_part : path_parts)
for (auto path_part : path_parts)
{
if (path_part.empty() || path_part == "."sv)
{

View File

@ -27,14 +27,13 @@ struct ParsedCommandLine
{
bool force_pic = false;
bool disable_serial = false;
BAN::StringView root;
};
ParsedCommandLine ParseCommandLine()
static bool should_disable_serial()
{
ParsedCommandLine result;
if (!(g_multiboot_info->flags & 0x02))
return result;
return false;
const char* start = g_kernel_cmdline;
const char* current = g_kernel_cmdline;
@ -42,12 +41,8 @@ ParsedCommandLine ParseCommandLine()
{
if (!*current || *current == ' ' || *current == '\t')
{
if (current - start == 6 && memcmp(start, "noapic", 6) == 0)
result.force_pic = true;
if (current - start == 8 && memcmp(start, "noserial", 8) == 0)
result.disable_serial = true;
return true;
if (!*current)
break;
start = current + 1;
@ -55,7 +50,28 @@ ParsedCommandLine ParseCommandLine()
current++;
}
return result;
return false;
}
static ParsedCommandLine cmdline;
static void parse_command_line()
{
if (!(g_multiboot_info->flags & 0x02))
return;
BAN::StringView full_command_line(g_kernel_cmdline);
auto arguments = MUST(full_command_line.split(' '));
for (auto argument : arguments)
{
if (argument == "noapic")
cmdline.force_pic = true;
else if (argument == "noserial")
cmdline.disable_serial = true;
else if (argument.size() > 5 && argument.substring(0, 5) == "root=")
cmdline.root = argument.substring(5);
}
}
struct Test
@ -87,8 +103,7 @@ extern "C" uintptr_t g_userspace_end;
extern void userspace_entry();
void init2(void*);
void device_updater(void*);
static void init2(void*);
extern "C" void kernel_main()
{
@ -96,9 +111,7 @@ extern "C" void kernel_main()
DISABLE_INTERRUPTS();
auto cmdline = ParseCommandLine();
if (!cmdline.disable_serial)
if (!should_disable_serial())
Serial::initialize();
if (g_multiboot_magic != 0x2BADB002)
{
@ -119,6 +132,9 @@ extern "C" void kernel_main()
MMU::intialize();
dprintln("MMU initialized");
parse_command_line();
dprintln("command line parsed, root='{}'", cmdline.root);
PCI::initialize();
dprintln("PCI initialized");
@ -189,16 +205,7 @@ extern "C" void kernel_main()
ASSERT(false);
}
void device_updater(void*)
{
while (true)
{
Kernel::DeviceManager::get().update();
PIT::sleep(1);
}
}
void init2(void* tty1_ptr)
static void init2(void* tty1_ptr)
{
using namespace Kernel;
using namespace Kernel::Input;
@ -207,7 +214,7 @@ void init2(void* tty1_ptr)
DeviceManager::initialize();
MUST(VirtualFileSystem::initialize());
MUST(VirtualFileSystem::initialize(cmdline.root));
if (auto res = PS2Controller::initialize(); res.is_error())
dprintln("{}", res.error());