vbos/init/main.c
Michael Shaw cfda99c7c7 main: add initramfs read test
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-10 12:57:52 -04:00

143 lines
2.9 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
/* vbos main entry point:
* VERY BAD OS: a hobby operating system
*
* Copyright 2023-2026 Michael Shaw
*/
#include <kernel/common.h>
#include <kernel/buddy.h>
#include <kernel/mem.h>
#include <kernel/device.h>
#include <kernel/requests.h>
#include <kernel/page.h>
#include <kernel/slab.h>
#include <arch/init.h>
#include <drivers/tty.h>
#include <drivers/serial.h>
#include <drivers/pci.h>
#include <drivers/acpi.h>
#include <drivers/apic.h>
#include <fs/vfs.h>
#include <fs/inode.h>
#include <fs/cpio.h>
#include <lib/stdio.h>
#include <lib/list.h>
#include <lib/rbtree.h>
#define P_STACK_SIZE 0x8000
typedef void (*initcall_t)(void);
extern initcall_t __initcall_start, __initcall_end;
struct tty *tty_printf = NULL;
static void do_initcalls()
{
for (initcall_t *fn = &__initcall_start; fn < &__initcall_end; fn++)
(*fn)();
}
static void indent(int indent)
{
for (int i = 0; i < indent; i++) {
kprintf(" ");
}
}
void printf_dir_recursive(dirent_t *dir, int ind)
{
if (!dir || !dir->inode)
return;
dirent_t *cursor;
struct dir_context ctx = {0};
int res;
while (!(res = vfs_readdir(dir, &ctx, &cursor))) {
if (!cursor || !cursor->inode)
continue;
indent(ind);
kprintf("[inode] %s\n", cursor->name);
if (cursor->inode->mode & S_IFDIR) {
printf_dir_recursive(cursor, ind + 2);
} else {
indent(ind + 1);
char *buf = kalloc(cursor->inode->file_size + 1, ALLOC_KERN);
buf[cursor->inode->file_size] = 0;
vfs_read(cursor->inode, buf, cursor->inode->file_size, 0);
kprintf("%s", buf);
kfree(buf);
}
}
}
static void kmain()
{
/* data types */
rbt_init();
list_init();
kalloc_init();
vfs_init();
arch_init();
device_layer_init();
tty_layer_init();
void *serial_port = arch_serial_init();
tty_printf = tty_create("ttyS0", serial_port);
int rows;
int cols;
tty_clear_screen(tty_printf);
tty_get_size(tty_printf, &rows, &cols);
tty_setlf(tty_printf);
tty_set_cursor_pos(tty_printf, 0, 0);
/* start the kernel */
page_init();
do_initcalls();
struct limine_file *file = module_res->modules[0];
fs_t *initramfs;
int res = cpio_read_and_init(file->address, file->size, &initramfs);
if (res < 0)
panic("failed to load initramfs: %s\n", strerror(-res));
krdy("initramfs loaded\n");
/* read entire initramfs filesystem */
printf_dir_recursive(initramfs->root_dirent, 0);
/* do nothing, for now :) */
yield();
}
void _before_stack()
{
/* these first few functions must be called in this order
*
* after calling arch_stack_init, we will lose all of our stack
* variables. only the most minimum things needed to init the stack
* here.
*/
arch_early_init();
requests_init();
mem_init();
buddy_init();
void *bsp_stack = buddy_alloc(buddy_order(P_STACK_SIZE));
arch_stack_init(bsp_stack, P_STACK_SIZE);
/* end previous note */
kmain();
}