From 8d19f45da2fd153e3f84e0ae95c9561b5266818d Mon Sep 17 00:00:00 2001 From: justuswolff Date: Sat, 28 Feb 2026 13:51:04 +0100 Subject: [PATCH] revert 64 bit update and some other shit --- makefile | 4 +- src/bootload/longm.asm | 55 --------------- src/headers/stddef.h | 4 +- src/headers/stdlib.h | 6 ++ src/headers/stdmem.h | 4 ++ src/kernel/driverdriver.asm | 5 ++ src/kernel/entry.c | 137 ++++++++++++++++++++++++++++++------ src/kernel/stdlib.c | 25 +++++++ 8 files changed, 160 insertions(+), 80 deletions(-) delete mode 100644 src/bootload/longm.asm create mode 100644 src/headers/stdmem.h create mode 100644 src/kernel/driverdriver.asm create mode 100644 src/kernel/stdlib.c diff --git a/makefile b/makefile index 067baa5..bba2b4a 100644 --- a/makefile +++ b/makefile @@ -1,14 +1,16 @@ build: prepare copygrub nasm -felf32 src/bootload/boot.asm -o build/boot.o # entrypoint for grub + nasm -felf32 src/kernel/driverdriver.asm -o build/drivdriv.o # driver code for drivers gcc -c src/kernel/entry.c -o build/kernel.o -ffreestanding -O2 -Wall -Wextra -m32 # kernel gcc -c src/kernel/registrar.c -o build/registrar.o -ffreestanding -O2 -Wall -Wextra -m32 # registrar + gcc -c src/kernel/stdlib.c -o build/stdlib.o -ffreestanding -O2 -Wall -Wextra -m32 # registrar gcc -c initrd/vga/main.c -o build/initrddir/vga_graph -ffreestanding -O2 -Wall -Wextra -m32 # initrd/vga driver tar -czf build/initrd build/initrddir/* # build initrd - gcc -m32 -T linker.ld -o build/linked -ffreestanding -O2 -nostdlib build/boot.o build/kernel.o build/registrar.o -fno-pie -fno-pic -no-pie # link + gcc -m32 -T linker.ld -o build/linked -ffreestanding -O2 -nostdlib build/boot.o build/kernel.o build/registrar.o build/drivdriv.o build/stdlib.o -fno-pie -fno-pic -no-pie # link cp build/linked build/grub/boot/kernel # copy kernel over to grub template cp build/initrd build/grub/boot/JPOS.initrd # copy initrd to grub template diff --git a/src/bootload/longm.asm b/src/bootload/longm.asm deleted file mode 100644 index f8b66cb..0000000 --- a/src/bootload/longm.asm +++ /dev/null @@ -1,55 +0,0 @@ -; DEPRECATED! - -CPUID_EXTENSIONS equ 0x80000000 -CPUID_EXT_FEATURES equ 0x80000001 -EFLAGS_ID equ 1 << 21 -CPUID_EDX_EXT_FEAT_LM equ 1 << 29 - -enterlong: - call .queryLongMode - - -.NoLongMode - cli - hlt - jmp .NoLongMode - -.checkCPUID: - pushfd - pop eax - - ; The original value should be saved for comparison and restoration later - mov ecx, eax - xor eax, EFLAGS_ID - - ; storing the eflags and then retrieving it again will show whether or not - ; the bit could successfully be flipped - push eax ; save to eflags - popfd - pushfd ; restore from eflags - pop eax - - ; Restore EFLAGS to its original value - push ecx - popfd - - ; if the bit in eax was successfully flipped (eax != ecx), CPUID is supported. - xor eax, ecx - jnz .NoLongMode - ret - -.queryLongMode: - call .checkCPUID - - mov eax, CPUID_EXTENSIONS - cpuid - cmp eax, CPUID_FEATURES - jb .NoLongMode - - mov eax, CPUID_EXT_FEATURES - cpuid - test edx, CPUID_EDX_EXT_FEAT_LM - jz .NoLongMode - - ret - diff --git a/src/headers/stddef.h b/src/headers/stddef.h index d28fa48..660bca6 100644 --- a/src/headers/stddef.h +++ b/src/headers/stddef.h @@ -1,9 +1,11 @@ #define uint8_t unsigned char #define uint16_t unsigned short #define uint32_t unsigned int +#define uint64_t unsigned long #define int8_t signed char #define int16_t signed short #define int32_t signed int +#define int64_t signed long -#define size_t uint32_t \ No newline at end of file +#define size_t uint64_t diff --git a/src/headers/stdlib.h b/src/headers/stdlib.h index e69de29..fd7fd07 100644 --- a/src/headers/stdlib.h +++ b/src/headers/stdlib.h @@ -0,0 +1,6 @@ +#pragma once +#include "stddef.h" + +uint32_t strlen32(char* str); +void strrev32(char* str); +uint8_t uitoa32(uint32_t x, char* str); \ No newline at end of file diff --git a/src/headers/stdmem.h b/src/headers/stdmem.h new file mode 100644 index 0000000..b116f98 --- /dev/null +++ b/src/headers/stdmem.h @@ -0,0 +1,4 @@ +#pragma once +#include "stddef.h" + +void memcpy32(void* dest, void* src, uint32_t length); \ No newline at end of file diff --git a/src/kernel/driverdriver.asm b/src/kernel/driverdriver.asm new file mode 100644 index 0000000..7b03f47 --- /dev/null +++ b/src/kernel/driverdriver.asm @@ -0,0 +1,5 @@ +section .text: +[BITS 32] +DRIV_storage: +resb 16384 ; 16 KiB for driver code/functions +DRIV_storage_end: diff --git a/src/kernel/entry.c b/src/kernel/entry.c index d6ceff6..fe4df07 100644 --- a/src/kernel/entry.c +++ b/src/kernel/entry.c @@ -1,29 +1,39 @@ #include "../headers/stddef.h" #include "../headers/multiboot2.h" #include "../headers/registrar.h" +#include "../headers/stdlib.h" +uint32_t basevga_postooffset(uint16_t x, uint16_t y) { + volatile uint8_t VGA_MAX_COLS = 80*2; + + return (y*VGA_MAX_COLS)+(x*2); +} void basevga_writestdout(char* buf, uint16_t count) { volatile char *video = (volatile char*)0xB8000; volatile uint8_t VGA_MAX_ROWS = 25; volatile uint8_t VGA_MAX_COLS = 80; - uint32_t offset = 0; - uint32_t offsets[VGA_MAX_ROWS]; - for (uint8_t i=0;i count*2%(VGA_MAX_COLS*VGA_MAX_ROWS)) { - offset += (count*2%(VGA_MAX_COLS*VGA_MAX_ROWS))-offsets[i]; - break; - } - } - continue; + y ++; + x = 0; } - video[count*2%(VGA_MAX_COLS*VGA_MAX_ROWS)+offset] = buf[i]; - video[count*2%(VGA_MAX_COLS*VGA_MAX_ROWS)+1+offset] = 15; + uint32_t offset = basevga_postooffset(x,y); + if (buf[i] != '\n') { + x ++; + } + if (x > VGA_MAX_ROWS) { + x = 0; + y ++; + } + if (y > VGA_MAX_COLS) { + y = 0; + } + + video[offset] = buf[i]; + video[offset+1] = 15; } } struct multiboot_info { @@ -49,33 +59,114 @@ void* FU_kupdbuf = basevga_writestdout; // register basevga_writestdout as kupdb char kstdout[8192] = {}; uint16_t kstdout_count = 0; void kputchar(char x) { + if (kstdout_count == 8192) { + for (uint32_t i=0;i<8191;i++) { + kstdout[i] = kstdout[i+1]; + } + kstdout_count --; + } kstdout[kstdout_count++] = x; FUINV_kupdbuf(kstdout, kstdout_count); } void kputstr(char* x) { - while (*x != 0) { kputchar(*x++); } - kputchar('\n'); + //kputchar('\n'); +} + +// basic tar parsing code for the initrd +struct tar_header { + char filename[100]; + char mode[8]; + char uid[8]; + char gid[8]; + char size[12]; + char mtime[12]; + char chksum[8]; + char typeflag[1]; +}; +uint32_t tar_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; + +} +uint32_t tar_parse(uint64_t address, struct tar_header* headers[]) { + uint32_t i; + for (i = 0;;i++) { + struct tar_header* header = (struct tar_header*)address; + if (header->filename[i] == '\0') { // end of archive + break; + } + + uint32_t size = tar_getsize(header->size); + + headers[i] = header; + + address += ((size / 512) + 1) * 512; + + if (size % 512) { + address += 512; + } + } + return i; +} + +// stuff for drivers +struct driver_state_info { + void* addr; + uint32_t size; + uint8_t inuse; +}; +struct driver_state_info driversinfo[32]; +void loaddriv(void* addr) { + } void kernel_main(struct multiboot_info* header, uint32_t magic) { - kputstr("Hello JPOS, World!"); + kputstr("Hello JPOS, World!\n"); + while (1) {} if (magic != MULTIBOOT2_BOOTLOADER_MAGIC) { - kputstr("Invalid magic from bootloader."); + kputstr("Invalid magic from bootloader.\n"); return; } - kputstr("Got past magic check."); + kputstr("Got past magic check.\n"); // load drivers // find boot modules tag struct multiboot_tag_module* bootmodules = (struct multiboot_tag_module*)&header->tags[findtag(3, header)]; - if (bootmodules->type != 3) {kputstr("Boot module failure.");return;} - //memcpy(0x200000, driver_code, size); - //((void(*)())0x200000)(); - + if (bootmodules->type != 3) {kputstr("Boot module failure.\n");return;} + + kputstr("Reading ramdisk...\n"); + char temp[10]; + uitoa32(bootmodules->mod_end-bootmodules->mod_start, temp); + kputstr("Ramdisk size: "); + kputstr(temp); + kputchar('\n'); + + struct tar_header* rd_headers[32]; + uint8_t rd_header_count = tar_parse(bootmodules->mod_start, rd_headers); + uitoa32(rd_header_count, temp); + kputstr("Ramdisk modules: "); + kputstr(temp); + kputchar('\n'); + + kputstr((char*)&rd_headers[0]); + for (uint8_t i=0;ifilename); + kputchar('\n'); + // TODO: actually load ramdisk stuff, first I'd like to enter 64 bit mode. + } + + kputstr("End of Ramdisk\n"); } \ No newline at end of file diff --git a/src/kernel/stdlib.c b/src/kernel/stdlib.c new file mode 100644 index 0000000..347e54a --- /dev/null +++ b/src/kernel/stdlib.c @@ -0,0 +1,25 @@ +#include "../headers/stdlib.h" + +uint32_t strlen32(char* str) { + uint32_t i = 0; + while (*str++ != 0) {i++;} + return i; +} +void strrev32(char* str) { + int c, i, j; + for (i = 0, j = strlen32(str)-1; i < j; i++, j--) { + c = str[i]; + str[i] = str[j]; + str[j] = c; + } +} +uint8_t uitoa32(uint32_t x, char* str) { + uint8_t i=0; + while (x > 0) { + str[i++] = x%10+'0'; + x /= 10; + } + str[i] = 0; + strrev32(str); + return i; +} \ No newline at end of file