From cfda99c7c745653a9bef764da7e86b3838dd718e Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 10 Jul 2026 12:48:55 -0400 Subject: [PATCH] main: add initramfs read test Signed-off-by: Michael Shaw --- init/main.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/init/main.c b/init/main.c index eeff5ca..336ea01 100644 --- a/init/main.c +++ b/init/main.c @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -42,6 +43,42 @@ static void do_initcalls() (*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 */ @@ -77,6 +114,11 @@ static void kmain() 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(); }