Starting with writing docs ended with me fixing a critical bug

This commit is contained in:
notsomeidiot123 2025-08-24 18:19:32 -04:00
parent 4266211e57
commit 2e6f8a289b
9 changed files with 5337 additions and 18 deletions

14
docs/modules.md Normal file
View File

@ -0,0 +1,14 @@
#define MODULE_API_REGISTER 0
#define MODULE_API_ADDFUNC 1
#define MODULE_API_DELFUNC 2
#define MODULE_API_ADDINT 3
#define MODULE_API_DELINT 4
#define MODULE_API_PRINT 5
#define MODULE_API_READ 6 //read from virtual file
#define MODULE_API_WRITE 7 //write to virtual file
#define MODULE_API_CREAT 8 //create a virtual file and assigns it to the the proper module (requires having a read and write function passed)
#define MODULE_API_DELET 9
# Kimi's OS Kernel Module API
On boot time, the bootloader will search the filesystem for the files `idm.elf` and `ifsm.elf` in the root directory. These are the only two modules loaded at boot time. Afterwards, the kernel will search the folder

5265
dump.hd Normal file

File diff suppressed because it is too large Load Diff

View File

@ -11,7 +11,7 @@ all: bootloader tools kernel
cp bin/bootloader.bin image.bin
qemu-img resize -f raw image.bin 512M
./diskwrite -v -l idm.elf ifsm.elf kernel.elf -o image.bin
# ./diskwrite -v idm.elf -o image.bin
# ./diskwrite -v -o image.bin
# ./diskwrite -v -l kernel.elf idm.elf -o image.bin
qemu-system-i386 -hda image.bin --no-reboot --no-shutdown -m 32m -smp 2 -serial mon:stdio -D intlog.txt -d int
tools:
@ -19,7 +19,16 @@ tools:
bootloader:
$(AS) src/bootloader/main.s $(BL_ASFLAGS) -o bin/bootloader.bin
run:
qemu-system-i386 -hda image.bin --no-reboot --no-shutdown -m 32m -smp 2 -serial mon:stdio -D intlog.txt -d int
test: bootloader tools kernel
cp bin/bootloader.bin image.bin
qemu-img resize -f raw image.bin 512M
./diskwrite -v -o image.bin
fsck.fat image.bin
sudo cp kernel.elf test/kernel.elf
make run
# kernel: $(OBJS)
kernel:
nasm src/kernel/entry.s -o bin/kernel/entry.o -f elf32

View File

@ -61,13 +61,13 @@ fat_bpb:
dd 1048576
fat_ebpb32:
.sectors_per_fat:
dd 1024
dd 2048
.flags:
dw 0
.version:
db 0, 1
.root_dir_cluster:
dd 2
dd 2
.fs_info_sector:
dw 1
.backup_boot:
@ -508,6 +508,7 @@ open_file:
add si, ax
xor cx, cx
mov ax, [fat_ebpb32.root_dir_cluster]
sub ax, 2
mov cl, [fat_bpb.sectors_per_cluster]
mul cx
add si, ax
@ -562,11 +563,13 @@ read_file:
;args: esi = first cluster in chain
;edi = pointer to data
push esi
sub esi, 2
xor ecx, ecx
xor edx, edx
mov eax, esi
mov cl, [fat_bpb.sectors_per_cluster]
mul ecx; eax = offset from file table
; jmp $
push eax
xor eax, eax
@ -895,7 +898,7 @@ fs_module_struct:
.type: dd 2
.flags: db 0
.next_entry: dd 0
kernel_file: db "kernel", 0, 0, "elf"
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"
loaded_fat_block: dd 0

View File

@ -3,12 +3,30 @@
#include "../shared/kstdlib.h"
#define MODULE_PRESENT 0x80
//semver is represented by hexadecimal
//first two digits are the major number
//second two digits are the minor number
//no patch number
#define MODULE_API_VERSION 0x0100
#define MODULE_API_REGISTER 0
#define MODULE_API_ADDFUNC 1
#define MODULE_API_DELFUNC 2
#define MODULE_API_ADDINT 3
#define MODULE_API_DELINT 4
#define MODULE_API_PRINT 5
#define MODULE_API_READ 6 //read from virtual file
#define MODULE_API_WRITE 7 //write to virtual file
#define MODULE_API_CREAT 8 //create a virtual file and assigns it to the the proper module (requires having a read and write function passed)
#define MODULE_API_DELET 9
typedef struct module{
void *init_entry;
uint32_t id;
char name[16];
uint8_t flags;
void (*fini)(void);
}module_t;
void init_modules(kernel_info_t *kernel_info);

View File

@ -3,7 +3,7 @@
KOS_MAPI_FP api;
void init(KOS_MAPI_FP module_api){
void init(KOS_MAPI_FP module_api, uint32_t api_version){
uint16_t *t = (void *)0xb8000;
*t = 0xf000;
return;

View File

@ -3,7 +3,7 @@
KOS_MAPI_FP api;
void init(KOS_MAPI_FP module_api){
void init(KOS_MAPI_FP module_api, uint32_t api_version){
uint16_t *t = (void *)0xb8002;
*t = 0xf000;
return;

View File

@ -1 +1,9 @@
typedef void (*KOS_MAPI_FP)(unsigned int function, ...);
#pragma once
#include <stdint.h>
typedef void (*KOS_MAPI_FP)(unsigned int function, ...);
typedef struct module{
void *init_entry;
uint32_t id;
char name[16];
uint8_t flags;
}module_t;

View File

@ -132,7 +132,7 @@ void write_cluster_value(fat_info *info, uint32_t value, uint32_t cluster){
//read cluster - returns next cluster in chain
uint32_t read_cluster(fat_info *info, uint32_t cluster, buffer_t buffer, size_t size, FILE *disk){
// uint8_t *new_buffer = calloc(4096, 1 * (info->bpb.bytes_per_sector * info->bpb.sectors_per_cluster));
read_sector(disk, info->bpb.reserved_sectors + info->file_table_size_sectors + info->bpb.partition_start + (cluster * info->bpb.sectors_per_cluster), info->bpb.sectors_per_cluster, buffer);
read_sector(disk, info->bpb.reserved_sectors + info->file_table_size_sectors + info->bpb.partition_start + ((cluster - 2) * info->bpb.sectors_per_cluster), info->bpb.sectors_per_cluster, buffer);
// memcpy(buffer, new_buffer, size);
return info->file_table[cluster];
}
@ -154,7 +154,7 @@ int write_file(fat_info *info, file_t *file, buffer_t buffer, size_t size, FILE
printf("start: %d count: %d\n", cluster, clusters);
// return -1;
for(uint32_t i = 0; i < clusters; i++){
write_sector(disk, info->bpb.reserved_sectors + info->file_table_size_sectors + info->bpb.partition_start + (cluster * info->bpb.sectors_per_cluster), info->bpb.sectors_per_cluster, new_buffer + i * info->bpb.sectors_per_cluster * info->bpb.bytes_per_sector);
write_sector(disk, info->bpb.reserved_sectors + info->file_table_size_sectors + info->bpb.partition_start + ((cluster - 2) * info->bpb.sectors_per_cluster), info->bpb.sectors_per_cluster, new_buffer + i * info->bpb.sectors_per_cluster * info->bpb.bytes_per_sector);
uint32_t next_cluster = info->file_table[cluster];
if(next_cluster == -1){
next_cluster = find_free_cluster(info);
@ -169,9 +169,9 @@ int write_file(fat_info *info, file_t *file, buffer_t buffer, size_t size, FILE
}
int create_file(char *path, fat_info *info, FILE *file, FILE *disk){
file_t *root_files = calloc(4096, sizeof(file_t));
file_t *root_files = calloc(info->bpb.sectors_per_cluster * 512, sizeof(file_t));
// read_file(info, 0, (buffer_t)root_files, (uint32_t)4096, disk);
read_cluster(info, info->bpb.root_dir_cluster, (buffer_t)root_files, 4096, disk);
read_cluster(info, info->bpb.root_dir_cluster, (buffer_t)root_files, info->bpb.sectors_per_cluster * 512, disk);
uint32_t index = 0;
while(root_files[index].filename[0]){
index++;
@ -231,6 +231,8 @@ int detect_repair_fs(FILE *file, fat_info *info){
info->file_table = calloc(info->bpb.sectors_per_fat * info->bpb.fat_count * info->bpb.bytes_per_sector, 1);
read_sector(file, info->bpb.reserved_sectors, info->bpb.sectors_per_fat * info->bpb.fat_count, (buffer_t)info->file_table);
if(!info->file_table[info->bpb.root_dir_cluster]){
info->file_table[0] = -1;
info->file_table[1] = -1;
info->file_table[info->bpb.root_dir_cluster] = -1;
}
}
@ -272,11 +274,11 @@ int main(int argc, char **argv){
files[filec++] = argv[i];
verbose && printf("IF: %s\n", argv[i]);
}
if(filec == 0){
printf("Nothing to do, no files to write\n");
return 0;
}
detect_repair_fs(output_file, &info);
// if(filec == 0){
// printf("Nothing to do, no files to write\n");
// return 0;
// }
for(int i = 0; i < filec; i++){
printf("\033[1;34mWriting file %d in list: %s\033[0m\n", i, files[i]);
FILE *f = fopen(files[i], "r");
@ -288,8 +290,8 @@ int main(int argc, char **argv){
fclose(f);
}
if(list_root_dir){
buffer_t buf = calloc(4096, 1);
read_cluster(&info, 2, buf, 4096, output_file);
buffer_t buf = calloc(info.bpb.sectors_per_cluster * 512, 1);
read_cluster(&info, 2, buf, info.bpb.sectors_per_cluster * 512, output_file);
file_t *root = (void *)buf;
uint32_t index = 0;
while(root[index].filename[0]){