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

1
.gitignore vendored
View File

@ -10,3 +10,4 @@ intlog.*
*.img *.img
.vscode .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 all: bootloader tools kernel
cp bin/bootloader.bin image.bin cp bin/bootloader.bin image.bin
qemu-img resize -f raw image.bin 64M 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 mount image.bin mount
sudo mkdir mount/mod # for kernel modules sudo mkdir mount/mod # for kernel modules
sudo mkdir mount/bin # for binary executables sudo mkdir mount/bin # for binary executables

View File

@ -325,69 +325,88 @@ part_2:
; debug ; debug
push eax push eax
mov edi, disk_module_file mov edi, initrd_file
call open_file call open_file
; debug
cmp eax, -1 cmp eax, -1
je .find_fs_driver_no_disk je .start32_no_initrc
mov [disk_module_struct.size], edx
; 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 mov esi, eax
and edx, 0xfffff000 and edx, 0xfffff000
add edx, 0x2000 add edx, 0x2000
add [kload_paddr], edx add [kload_paddr], edx
; mov eax, [kload_paddr] ; ; mov eax, [kload_paddr]
; jmp $ ; ; jmp $
; debug ; ; debug
mov edi, [kload_paddr] mov edi, [kload_paddr]
mov [disk_module_struct.ptr], edi mov [initrd.ptr], edi
push edx
call read_file call read_file
pop edx
add [kload_paddr], edx
jc load_fail jc load_fail
; mov esi, [kload_paddr] jmp .start32
; call load_elf ; ; mov esi, [kload_paddr]
; debug ; ; call load_elf
jc load_fail ; ; debug
jmp .find_fs_driver ; jc load_fail
.find_fs_driver_no_disk: ; jmp .find_fs_driver
mov ax, 0xe44 ; .find_fs_driver_no_disk:
int 0x10 ; mov ax, 0xe44
.find_fs_driver: ; int 0x10
; .find_fs_driver:
; mov edi, fs_module_file ; mov edi, fs_module_file
; call open_file ; call open_file
; ; debug
; cmp eax, -1 ; cmp eax, -1
; je .start32_no_fs ; je .start32_no_fs
; mov [fs_module_struct.size], edx ; mov [fs_module_struct.size], edx
; mov eax, esi ; mov esi, eax
; and edx, 0xfffffc00 ; and edx, 0xfffff000
; add edx, 0x1000 ; add edx, 0x2000
; add [kload_paddr], edx ; add [kload_paddr], edx
; ; mov eax, [kload_paddr]
; ; jmp $
; ; debug
; mov edi, [kload_paddr] ; mov edi, [kload_paddr]
; mov [fs_module_struct.ptr], edi ; mov [fs_module_struct.ptr], edi
; push edx
; call read_file ; call read_file
; pop edx
; add [kload_paddr], edx
; jc load_fail ; jc load_fail
; ; jmp .find_initrc
mov edi, fs_module_file ; ;
call open_file ; .start32_no_fs:
; debug ; mov ax, 0xe46
cmp eax, -1 ; int 0x10
je .start32_no_fs ; .find_initrc:
mov [fs_module_struct.size], edx ; mov edi, initrc_file
mov esi, eax ; call open_file
and edx, 0xfffff000 ; ; debug
add edx, 0x2000 ; cmp eax, -1
add [kload_paddr], edx ; je .start32_no_initrc
; mov eax, [kload_paddr] ; mov [initrc_struct.size], edx
; jmp $ ; mov esi, eax
; debug ; and edx, 0xfffff000
mov edi, [kload_paddr] ; add edx, 0x2000
mov [fs_module_struct.ptr], edi ; add [kload_paddr], edx
call read_file ; ; mov eax, [kload_paddr]
jc load_fail ; ; jmp $
jmp .start32 ; ; debug
; ; mov edi, [kload_paddr]
.start32_no_fs: ; mov [initrc_struct.ptr], edi
mov ax, 0xe46 ; call read_file
; jc load_fail
; jmp .start32
.start32_no_initrc:
mov ax, 0xe49
int 0x10 int 0x10
.start32: .start32:
; debug ; debug
@ -884,23 +903,34 @@ k_info:
.xres: dw 80 .xres: dw 80
.yres: dw 25 .yres: dw 25
.framebuffer_ptr: dd 0xb8000 .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 ;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: ; disk_module_struct:
.ptr: dd 0 ; .ptr: dd 0
.size: dd 0 ; .size: dd 0
.type: dd 1 ; .type: dd 1
.flags: db 0 ; .flags: db 0
.next_entry: dd fs_module_struct ; .next_entry: dd fs_module_struct
fs_module_struct: ; fs_module_struct:
.ptr: dd 0 ; .ptr: dd 0
.size: dd 0 ; .size: dd 0
.type: dd 2 ; .type: dd 2
.flags: db 0 ; .flags: db 0
.next_entry: dd 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" kernel_file: db "kernel", 0x0, 0x0, "elf"
disk_module_file: db "idm", 0, 0, 0, 0, 0, "elf" initrd_file: db "initrd", 0x0, 0x0, "rd"
fs_module_file: db "ifsm", 0, 0, 0, 0, "elf" ; 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 loaded_fat_block: dd 0
last_allocated_pgtb: dd 0x10000 last_allocated_pgtb: dd 0x10000
sacrifice1: dd 0 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/modules.h"
#include "system/elf.h" #include "system/elf.h"
#include "system/vfs.h" #include "system/vfs.h"
#include "drivers/ustar.h"
kernel_info_t *boot_info = 0; kernel_info_t *boot_info = 0;
@ -19,17 +20,9 @@ void pid0(){
void sysinit(){ void sysinit(){
mlog("KERNEL", "PID 1 Started\n", MLOG_PRINT); mlog("KERNEL", "PID 1 Started\n", MLOG_PRINT);
pci_init(); pci_init();
modules_init(boot_info); // modules_init(boot_info, 0);
vfile_t *file = fopen("/dev/disk"); read_initrd(boot_info->initrd);
vfile_t **dir_data = file->access.data.ptr; vfile_t *initrc = fopen("/boot/initrc.conf");
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");
// uint8_t *buffer = kmalloc(1); // uint8_t *buffer = kmalloc(1);
// int status = fread(drive_test, buffer, 0, 4096); // int status = fread(drive_test, buffer, 0, 4096);
// if(status == -1){ // if(status == -1){
@ -41,6 +34,7 @@ void sysinit(){
// } // }
//fopen shell file & execute it. //fopen shell file & execute it.
printf("Bleh\n");
for(;;); for(;;);
} }
extern void kmain(kernel_info_t *kernel_info){ 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", VFILE_DIRECTORY, kmalloc(1), 1);
fcreate("/dev/disk", VFILE_DIRECTORY, kmalloc(1), 1); fcreate("/dev/disk", VFILE_DIRECTORY, kmalloc(1), 1);
fcreate("/tmp", 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); mlog("KERNEL", "Initializing Scheduler & starting PID 1\n", MLOG_PRINT);
boot_info = kernel_info; boot_info = kernel_info;
scheduler_init(); scheduler_init();

View File

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

@ -24,3 +24,15 @@ inline uint32_t strcmp(char *str1, char *str2){
char *strtok(char *str, char delim); char *strtok(char *str, char delim);
void strcpy(char *src, char *dest); 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; return return_value;
} }
void modules_init(kernel_info_t *kernel_info){ void modules_init(){
mlog(MODULE_NAME, "Initializing Boot-Time Modules\n", MLOG_PRINT); 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; // uint32_t i = 0;
modules = init_vector(0, sizeof(module_t), 0, 0); modules = init_vector(0, sizeof(module_t), 0, 0);
while(module){ // while(module){
if(!module->ptr){ // if(!module->ptr){
continue; // continue;
} // }
void (*entry)(void* api, uint32_t version) = load_elf(module->ptr, PT_SYS); // if(module->flags & MODULE_INITRC){
mlog(MODULE_NAME, "Loaded module with ID %x\n", MLOG_PRINT, module->id); // fcreate("initrc", VFILE_POINTER, module->ptr, 1);
// module->type |= 0x8000;//set present flag in type // continue;
module->flags |= MODULE_PRESENT; // }
module = module->link; // void (*entry)(void* api, uint32_t version) = load_elf(module->ptr, PT_SYS);
(*entry)(module_api, 0); // 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 #pragma once
#include "vfs.h"
#include <stdint.h> #include <stdint.h>
#include "../shared/kstdlib.h" #include "../shared/kstdlib.h"
#define MODULE_PRESENT 0x80 #define MODULE_PRESENT 0x80
#define MODULE_INITRC 0x01
//semver is represented by hexadecimal //semver is represented by hexadecimal
//first two digits are the major number //first two digits are the major number
//second two digits are the minor number //second two digits are the minor number
@ -41,4 +43,4 @@ typedef struct module{
void (*fini)(void); void (*fini)(void);
}module_t; }module_t;
void modules_init(kernel_info_t *kernel_info); void modules_init();

View File

@ -59,3 +59,32 @@ void strpad(char *string, char padding, uint32_t length){
strcpy(string, tmp + length - strlen(string)); strcpy(string, tmp + length - strlen(string));
strcpy(tmp, 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_POINTER:
case VFILE_DIRECTORY: case VFILE_DIRECTORY:
case VFILE_MOUNT: case VFILE_MOUNT:
case VFILE_SYMLINK:
void *ptr = va_arg(args, void*); void *ptr = va_arg(args, void*);
vfile->access.data.ptr = ptr; vfile->access.data.ptr = ptr;
vfile->access.data.size_pgs = va_arg(args, uint32_t); 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 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){ switch(tmpfile->type){
case VFILE_DIRECTORY: case VFILE_DIRECTORY:
current_dir = tmpfile; 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: case VFILE_DEVICE:
return file_entry->access.funcs.write(file_entry, byte_array, offset, count); return file_entry->access.funcs.write(file_entry, byte_array, offset, count);
break; break;
case VFILE_SYMLINK:
return fwrite(file_entry->access.data.ptr, byte_array, offset, count);
} }
return INT32_MIN;//how did we get here? 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: case VFILE_DEVICE:
return file_entry->access.funcs.read(file_entry, byte_array, offset, count); return file_entry->access.funcs.read(file_entry, byte_array, offset, count);
break; 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 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){ vfile_t *fopen(char *name){
if(name[0] == '/'){ if(name[0] == '/'){
if(strlen(name) == 1){
return &root_dir;
}
name++; name++;
} }
if(name[strlen(name) - 1] == '/'){ if(name[strlen(name) - 1] == '/'){
@ -188,6 +199,9 @@ vfile_t *fopen(char *name){
// printf("not found\n"); // printf("not found\n");
return 0; return 0;
} }
if(tmpfile->type == VFILE_SYMLINK){
tmpfile = (vfile_t *)tmpfile->access.data.ptr;
}
switch(tmpfile->type){ switch(tmpfile->type){
case VFILE_DIRECTORY: case VFILE_DIRECTORY:
// printf("found directory in %s: %s\n", current_dir->name, tmpfile->name); // 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_DIRECTORY,
VFILE_PDIR,//used for directories in physical filesystems. VFILE_PDIR,//used for directories in physical filesystems.
VFILE_FILE,//physical files VFILE_FILE,//physical files
VFILE_SYMLINK,
VFILE_PIPE //yay we have pipes VFILE_PIPE //yay we have pipes
}VFILE_TYPE; }VFILE_TYPE;