Loading initrd instead of modules separately

This commit is contained in:
notsomeidiot123 2025-09-27 14:38:32 -04:00
parent 9cb5502bbf
commit 95f55f1623
15 changed files with 266 additions and 135 deletions

3
.gitignore vendored
View File

@ -9,4 +9,5 @@ diskwrite
intlog.*
*.img
.vscode
bochsrc
bochsrc
initrd.rd

3
initrc.conf Normal file
View File

@ -0,0 +1,3 @@
echo "This is a test"
panic

View File

@ -10,7 +10,9 @@ OBJS := $(patsubst src/kernel/%.c, bin/kernel/%.o, $(SRCS))
all: bootloader tools kernel
cp bin/bootloader.bin image.bin
qemu-img resize -f raw image.bin 64M
./diskwrite idm.elf ifsm.elf kernel.elf -o image.bin
tar --format=ustar -cf initrd.rd idm.elf ifsm.elf initrc.conf
# ./diskwrite idm.elf ifsm.elf kernel.elf initrc.conf -o image.bin
./diskwrite initrd.rd kernel.elf -o image.bin
sudo mount image.bin mount
sudo mkdir mount/mod # for kernel modules
sudo mkdir mount/bin # for binary executables

View File

@ -325,69 +325,88 @@ part_2:
; debug
push eax
mov edi, disk_module_file
mov edi, initrd_file
call open_file
; debug
cmp eax, -1
je .find_fs_driver_no_disk
mov [disk_module_struct.size], edx
je .start32_no_initrc
; mov edi, disk_module_file
; call open_file
; ; debug
; cmp eax, -1
; je .find_fs_driver_no_disk
mov [initrd.size], edx
mov esi, eax
and edx, 0xfffff000
add edx, 0x2000
add [kload_paddr], edx
; mov eax, [kload_paddr]
; jmp $
; debug
; ; mov eax, [kload_paddr]
; ; jmp $
; ; debug
mov edi, [kload_paddr]
mov [disk_module_struct.ptr], edi
mov [initrd.ptr], edi
push edx
call read_file
pop edx
add [kload_paddr], edx
jc load_fail
; mov esi, [kload_paddr]
; call load_elf
; debug
jc load_fail
jmp .find_fs_driver
.find_fs_driver_no_disk:
mov ax, 0xe44
int 0x10
.find_fs_driver:
; mov edi, fs_module_file
; call open_file
; cmp eax, -1
; je .start32_no_fs
; mov [fs_module_struct.size], edx
; mov eax, esi
; and edx, 0xfffffc00
; add edx, 0x1000
; add [kload_paddr], edx
; mov edi, [kload_paddr]
; mov [fs_module_struct.ptr], edi
; call read_file
; jc load_fail
;
mov edi, fs_module_file
call open_file
; debug
cmp eax, -1
je .start32_no_fs
mov [fs_module_struct.size], edx
mov esi, eax
and edx, 0xfffff000
add edx, 0x2000
add [kload_paddr], edx
; mov eax, [kload_paddr]
; jmp $
; debug
mov edi, [kload_paddr]
mov [fs_module_struct.ptr], edi
call read_file
jc load_fail
jmp .start32
;
.start32_no_fs:
mov ax, 0xe46
jmp .start32
; ; mov esi, [kload_paddr]
; ; call load_elf
; ; debug
; jc load_fail
; jmp .find_fs_driver
; .find_fs_driver_no_disk:
; mov ax, 0xe44
; int 0x10
; .find_fs_driver:
; mov edi, fs_module_file
; call open_file
; ; debug
; cmp eax, -1
; je .start32_no_fs
; mov [fs_module_struct.size], edx
; mov esi, eax
; and edx, 0xfffff000
; add edx, 0x2000
; add [kload_paddr], edx
; ; mov eax, [kload_paddr]
; ; jmp $
; ; debug
; mov edi, [kload_paddr]
; mov [fs_module_struct.ptr], edi
; push edx
; call read_file
; pop edx
; add [kload_paddr], edx
; jc load_fail
; jmp .find_initrc
; ;
; .start32_no_fs:
; mov ax, 0xe46
; int 0x10
; .find_initrc:
; mov edi, initrc_file
; call open_file
; ; debug
; cmp eax, -1
; je .start32_no_initrc
; mov [initrc_struct.size], edx
; mov esi, eax
; and edx, 0xfffff000
; add edx, 0x2000
; add [kload_paddr], edx
; ; mov eax, [kload_paddr]
; ; jmp $
; ; debug
; mov edi, [kload_paddr]
; mov [initrc_struct.ptr], edi
; call read_file
; jc load_fail
; jmp .start32
.start32_no_initrc:
mov ax, 0xe49
int 0x10
.start32:
; debug
@ -884,23 +903,34 @@ k_info:
.xres: dw 80
.yres: dw 25
.framebuffer_ptr: dd 0xb8000
.loaded_modules: dd disk_module_struct
.loaded_modules: dd initrd
;loaded_modules points to a struct array containing a ptr to the entry point of each pre-loaded module to be executed during startup
disk_module_struct:
.ptr: dd 0
.size: dd 0
.type: dd 1
.flags: db 0
.next_entry: dd fs_module_struct
fs_module_struct:
.ptr: dd 0
.size: dd 0
.type: dd 2
.flags: db 0
.next_entry: dd 0
; disk_module_struct:
; .ptr: dd 0
; .size: dd 0
; .type: dd 1
; .flags: db 0
; .next_entry: dd fs_module_struct
; fs_module_struct:
; .ptr: dd 0
; .size: dd 0
; .type: dd 2
; .flags: db 0
; .next_entry: dd initrc_struct
; initrc_struct:
; .ptr: dd 0
; .size: dd 0
; .type: dd 2
; .flags: db 1
; .next_entry: dd 0
initrd:
.ptr dd 0
.size dd 0
kernel_file: db "kernel", 0x0, 0x0, "elf"
disk_module_file: db "idm", 0, 0, 0, 0, 0, "elf"
fs_module_file: db "ifsm", 0, 0, 0, 0, "elf"
initrd_file: db "initrd", 0x0, 0x0, "rd"
; disk_module_file: db "idm", 0, 0, 0, 0, 0, "elf"
; fs_module_file: db "ifsm", 0, 0, 0, 0, "elf"
; initrc_file: db "initrc", 0, 0, "conf"
loaded_fat_block: dd 0
last_allocated_pgtb: dd 0x10000
sacrifice1: dd 0

View File

@ -0,0 +1,42 @@
#include "ustar.h"
#include "../system/vfs.h"
#include "../shared/kstdlib.h"
#include "../shared/string.h"
#include "../shared/memory.h"
uint32_t ustar_getsize(const char *in){
uint32_t size = 0;
uint32_t j;
uint32_t count = 1;
for (j = 11; j > 0; j--, count *= 8){
size += ((in[j - 1] - '0') * count);
}
return size;
}
int read_initrd(initrd_t *initrd){
// printf("Name: %s", ((USTAR_file_t * )initrd->ptr)->filename);
char *archive = initrd->ptr;
uint32_t offset = 0;
while (!memcmp(((USTAR_file_t *)(archive + offset))->ustar, "ustar", 5)) {
USTAR_file_t *file = (USTAR_file_t *)(archive + offset);
int filesize = ustar_getsize(file->fsize);
// if (!memcmp(file->filename, filename, strlen(filename) + 1)) {
// *out = ptr + 512;
// return filesize;
// }
uint32_t fsize_pgs = (((filesize + 4095)/4096) + 1);
// uint32_t *ptr = kmalloc(fsize_pgs);
// for(uint32_t i = 0; i < (filesize/4) + 1; i++){
// ptr[i] = ((uint32_t *)(archive + offset))[i];
// }
char final_fname[80] = "/boot/";
strcat(file->filename, final_fname);
fcreate(final_fname, VFILE_POINTER, archive + offset + sizeof(USTAR_file_t), fsize_pgs);
printf("%s, %d, %d\n", final_fname, filesize, fsize_pgs);
offset += (((filesize + 511) / 512) + 1) * 512;
}
}

View File

@ -0,0 +1,31 @@
#pragma once
#include "stdint.h"
#include "../shared/kstdlib.h"
typedef struct{
char filename[100];
uint64_t mode;
uint64_t owner_uid;
uint64_t group_uid;
char fsize[12];
char last_modified[12];
uint64_t checksum;
uint8_t type;
char linked_file[100];
char ustar[6];
char version [2];
char owner_name[32];
char group_name[32];
char dev_major[8];
char dev_minor[8];
char filename_prefix[155];
}__attribute__((packed)) USTAR_file_t;
enum USTAR_TYPES{
FILE,
SYMLINK,
CHARDEV,
BLOCKDEV,
DIR,
PIPE
};
int read_initrd(initrd_t *ptr);

View File

@ -10,6 +10,7 @@
#include "system/modules.h"
#include "system/elf.h"
#include "system/vfs.h"
#include "drivers/ustar.h"
kernel_info_t *boot_info = 0;
@ -19,17 +20,9 @@ void pid0(){
void sysinit(){
mlog("KERNEL", "PID 1 Started\n", MLOG_PRINT);
pci_init();
modules_init(boot_info);
vfile_t *file = fopen("/dev/disk");
vfile_t **dir_data = file->access.data.ptr;
printf("/dev/disk/: %x\n", file);
// fcreate("/dev/disk/test", VFILE_DEVICE, 0, 0);
int i = 0;
while(dir_data[i]){
printf("%d: %s\n", i, dir_data[i]->name);
i++;
}
// vfile_t *drive_test = fopen("/dev/disk/ide0");
// modules_init(boot_info, 0);
read_initrd(boot_info->initrd);
vfile_t *initrc = fopen("/boot/initrc.conf");
// uint8_t *buffer = kmalloc(1);
// int status = fread(drive_test, buffer, 0, 4096);
// if(status == -1){
@ -41,6 +34,7 @@ void sysinit(){
// }
//fopen shell file & execute it.
printf("Bleh\n");
for(;;);
}
extern void kmain(kernel_info_t *kernel_info){
@ -57,6 +51,7 @@ extern void kmain(kernel_info_t *kernel_info){
fcreate("/dev", VFILE_DIRECTORY, kmalloc(1), 1);
fcreate("/dev/disk", VFILE_DIRECTORY, kmalloc(1), 1);
fcreate("/tmp", VFILE_DIRECTORY, kmalloc(1), 1);
fcreate("/boot", VFILE_DIRECTORY, kmalloc(1), 1);
mlog("KERNEL", "Initializing Scheduler & starting PID 1\n", MLOG_PRINT);
boot_info = kernel_info;
scheduler_init();

View File

@ -9,13 +9,10 @@
#define MLOG_WARN 3
#define MLOG_ERR 4
typedef struct module_linked_list{
typedef struct initrd{
void *ptr;
uint32_t size;
uint32_t id;
uint8_t flags;
struct module_linked_list *link;
}__attribute__((packed)) kernel_module_t;
}__attribute__((packed)) initrd_t;
typedef struct vector{
void *ptr;
@ -37,7 +34,7 @@ typedef struct kernel_info{
uint16_t monitor_x_resolution;
uint16_t monitor_y_resolution;
void *framebuffer;
kernel_module_t *loaded_modules;
initrd_t *initrd;
// uint32_t loaded_module_count;
}__attribute__((packed)) kernel_info_t;

View File

@ -1,32 +0,0 @@
#include "string.h"
#pragma warning
char res[256];
char* strtok(char* string, char d)
{
static char* input = 0;
if (string){
input = string;
}
if (!input){
return 0;
}
int i = 0;
for (; input[i] != 0; i++) {
if (input[i] != d){
res[i] = input[i];
}
else {
res[i] = '\0';
input = input + i+1;
return res;
}
}
res[i] = '\0';
input = 0;
return res;
}

View File

@ -23,4 +23,16 @@ inline uint32_t strcmp(char *str1, char *str2){
}
char *strtok(char *str, char delim);
void strcpy(char *src, char *dest);
void memcpy(char *src, char *dest, uint32_t c);
void memcpy(char *src, char *dest, uint32_t c);
inline void strcat(char *src, char *dest){
strcpy(src, dest + strlen(dest));
}
inline int memcmp(char *ptr1, char *ptr2, uint32_t size){
for(uint32_t i = 0; i < size; i++){
if(ptr1[i] == ptr2[i]){
continue;
}
return ptr1[i] - ptr2[i];
}
return 0;
}

View File

@ -129,20 +129,24 @@ uint32_t module_api(uint32_t func, ...){
return return_value;
}
void modules_init(kernel_info_t *kernel_info){
void modules_init(){
mlog(MODULE_NAME, "Initializing Boot-Time Modules\n", MLOG_PRINT);
kernel_module_t *module = kernel_info->loaded_modules;
// kernel_module_t *module = kernel_info->loaded_modules;
// uint32_t i = 0;
modules = init_vector(0, sizeof(module_t), 0, 0);
while(module){
if(!module->ptr){
continue;
}
void (*entry)(void* api, uint32_t version) = load_elf(module->ptr, PT_SYS);
mlog(MODULE_NAME, "Loaded module with ID %x\n", MLOG_PRINT, module->id);
// module->type |= 0x8000;//set present flag in type
module->flags |= MODULE_PRESENT;
module = module->link;
(*entry)(module_api, 0);
}
// while(module){
// if(!module->ptr){
// continue;
// }
// if(module->flags & MODULE_INITRC){
// fcreate("initrc", VFILE_POINTER, module->ptr, 1);
// continue;
// }
// void (*entry)(void* api, uint32_t version) = load_elf(module->ptr, PT_SYS);
// mlog(MODULE_NAME, "Loaded module with ID %x\n", MLOG_PRINT, module->id);
// // module->type |= 0x8000;//set present flag in type
// module->flags |= MODULE_PRESENT;
// module = module->link;
// (*entry)(module_api, 0);
// }
}

View File

@ -1,8 +1,10 @@
#pragma once
#include "vfs.h"
#include <stdint.h>
#include "../shared/kstdlib.h"
#define MODULE_PRESENT 0x80
#define MODULE_INITRC 0x01
//semver is represented by hexadecimal
//first two digits are the major number
//second two digits are the minor number
@ -41,4 +43,4 @@ typedef struct module{
void (*fini)(void);
}module_t;
void modules_init(kernel_info_t *kernel_info);
void modules_init();

View File

@ -58,4 +58,33 @@ void strpad(char *string, char padding, uint32_t length){
}
strcpy(string, tmp + length - strlen(string));
strcpy(tmp, string);
}
char res[256];
char* strtok(char* string, char d)
{
static char* input = 0;
if (string){
input = string;
}
if (!input){
return 0;
}
int i = 0;
for (; input[i] != 0; i++) {
if (input[i] != d){
res[i] = input[i];
}
else {
res[i] = '\0';
input = input + i+1;
return res;
}
}
res[i] = '\0';
input = 0;
return res;
}

View File

@ -75,6 +75,7 @@ vfile_t *fcreate(char *name, VFILE_TYPE type, ...){
case VFILE_POINTER:
case VFILE_DIRECTORY:
case VFILE_MOUNT:
case VFILE_SYMLINK:
void *ptr = va_arg(args, void*);
vfile->access.data.ptr = ptr;
vfile->access.data.size_pgs = va_arg(args, uint32_t);
@ -93,6 +94,9 @@ vfile_t *fcreate(char *name, VFILE_TYPE type, ...){
}
return vfile; //allow drivers to make last minute changes before it's sent to the user
}
if(tmpfile->type == VFILE_SYMLINK){
tmpfile = (vfile_t *)tmpfile->access.data.ptr;
}
switch(tmpfile->type){
case VFILE_DIRECTORY:
current_dir = tmpfile;
@ -132,6 +136,8 @@ int fwrite(vfile_t *file_entry, void *byte_array, uint32_t offset, uint32_t coun
case VFILE_DEVICE:
return file_entry->access.funcs.write(file_entry, byte_array, offset, count);
break;
case VFILE_SYMLINK:
return fwrite(file_entry->access.data.ptr, byte_array, offset, count);
}
return INT32_MIN;//how did we get here?
}
@ -151,6 +157,8 @@ int fread(vfile_t *file_entry, void *byte_array, uint32_t offset, uint32_t count
case VFILE_DEVICE:
return file_entry->access.funcs.read(file_entry, byte_array, offset, count);
break;
case VFILE_SYMLINK:
return fread(file_entry->access.data.ptr, byte_array, offset, count);
}
return INT32_MIN; //Okay, alright, funny joke guys but we really shouldn't be able to get here
}
@ -169,6 +177,9 @@ vfile_t *search_dir(char *name, vfile_t dir){
vfile_t *fopen(char *name){
if(name[0] == '/'){
if(strlen(name) == 1){
return &root_dir;
}
name++;
}
if(name[strlen(name) - 1] == '/'){
@ -188,6 +199,9 @@ vfile_t *fopen(char *name){
// printf("not found\n");
return 0;
}
if(tmpfile->type == VFILE_SYMLINK){
tmpfile = (vfile_t *)tmpfile->access.data.ptr;
}
switch(tmpfile->type){
case VFILE_DIRECTORY:
// printf("found directory in %s: %s\n", current_dir->name, tmpfile->name);

View File

@ -8,6 +8,7 @@ typedef enum vfile_type{
VFILE_DIRECTORY,
VFILE_PDIR,//used for directories in physical filesystems.
VFILE_FILE,//physical files
VFILE_SYMLINK,
VFILE_PIPE //yay we have pipes
}VFILE_TYPE;