summaryrefslogtreecommitdiffstats
path: root/boot/initrd.c
blob: f9bf47eeceb5f12fb6a224e41f935ac564fe3c3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "initrd.h"
#include "arch/x86_64/hal/obj.h"
#include "proc/init.h"
#include "jove.h"
#include "lib/printf.h"
#include "lib/tar.h"
#include "lib/string.h"
#include "hal/obj.h"
#include "hal/memory.h"

extern uintptr_t _initrd_module;

PAGEALIGN jove_objdir_t s_initrd_objdir = { 0 };

#define CHECK_IF_INITRD(obj) \
    if(obj_gettype(obj) != JOVE_OBJ_INITRD_FILE) { \
        reterr(EBADOBJ); \
    }

const char*
initrd_file_name(jove_owned_obj_t *obj)
{
    CHECK_IF_INITRD(obj);
    struct tar_header *file = obj_deref(obj);
    return tar_filename(file);
}

size_t
initrd_file_size(jove_owned_obj_t *obj)
{
    CHECK_IF_INITRD(obj);
    struct tar_header *file = obj_deref(obj);
    return tar_filesize(file);
}

void*
initrd_file_data(jove_owned_obj_t *obj)
{
    CHECK_IF_INITRD(obj);
    union tar_block *file = obj_deref(obj);
    return &(file[1]);
}

jove_owned_obj_t*
initrd_seek(jove_owned_obj_t *objdir, const char *path)
{
    if(obj_gettype(objdir) != JOVE_OBJ_OBJDIR) {
        reterr(EBADOBJ);
    }
    for(size_t i = 0; i < JOVE_OBJDIR_ENTRIES; i++) {
        jove_owned_obj_t *file_obj = objdir_getlocal(objdir, i);
        if(obj_gettype(file_obj) != JOVE_OBJ_INITRD_FILE) {
            goto loop_continue;
        }
        const char *filepath = initrd_file_name(file_obj);
        if(filepath == NULL) {
            goto loop_continue;
        }

        if(strncmp(path, filepath, strlen(filepath)+1) == 0) {
            return file_obj;
        }
loop_continue:
        obj_release(file_obj);
    }

    return NULL;
}

void initrd_setup(void)
{
    kprintf("Loading initrd from bootloader:\n");
    {
        jove_owned_obj_t *initrd_objdir_obj = objdir_getlocal(_rootdir_obj, ROOTDIR_INITRD_I);
        obj_settype(initrd_objdir_obj, JOVE_OBJ_OBJDIR);
        obj_setvalue(initrd_objdir_obj, (uintmax_t)&s_initrd_objdir);

        size_t initrd_files = 0;
        union tar_block *block = phys_tovirt(_initrd_module);
        for(;;) { 
            struct tar_header *header = &block->header;
            if(*header->filename == 0) break;

            size_t file_size = tar_filesize(header);
            kprintf("File: %s, Size: %i\n", tar_filename(header), file_size);
            {
                jove_owned_obj_t *initrd_file_obj = objdir_getlocal(initrd_objdir_obj, initrd_files++);
                obj_settype(initrd_file_obj, JOVE_OBJ_INITRD_FILE);
                obj_setvalue(initrd_file_obj, (uintmax_t)block);
                obj_release(initrd_file_obj);
            }

            block = &block[((file_size + 511) / 512) + 1];
        }
        obj_release(initrd_objdir_obj);
    }
}