relocating file during load to allow for use of more memory

This commit is contained in:
notsomeidiot123 2024-10-02 17:38:15 -04:00
parent e91d50ac6b
commit 24d85843ac
5 changed files with 112 additions and 20 deletions

35
linker.ld Normal file
View File

@ -0,0 +1,35 @@
ENTRY(_start)
/* OUTPUT_FORMAT(elf32-i386) */
OUTPUT_FORMAT(binary)
OUTPUT(kernel.elf)
SEARCH_DIR(bin/kernel)
SECTIONS{
. = 0xc0000000;
.text : AT(ADDR(.text) - 0xc0000000){
_code = .;
*(.text)
*(.rodata)
. = ALIGN(4096);
}
.data : AT(ADDR(.data) - 0xc0000000){
_data = .;
*(.data)
}
.eh_frame : AT(ADDR(.eh_frame) - 0xc0000000){
_eh_frame = .;
*(.eh_frame)
. = ALIGN(4096);
}
.bss : AT(ADDR(.bss) - 0xc0000000){
_bss = .;
*(.bss)
*(.COMMON)
. = ALIGN(4096);
}
_end = .;
/DISCARD/ :
{
*(.comment)
}
}

View File

@ -1,11 +1,14 @@
CC := gcc
AS := nasm
CFLAGS := -c -mno-sse -mno-sse2 -mno-red-zone -ffreestanding -fno-pie -fno-stack-protector -mno-mmx
BL_ASFLAGS := -f bin
SRCS := $(wildcard src/kernel/*.c)
OBJS := $(patsubst src/kernel/%.c, bin/kernel/%.o, $(SRCS))
all: bootloader tools
all: bootloader tools kernel
cp bin/bootloader.bin image.bin
qemu-img resize -f raw image.bin 512M
./diskwrite -v kernel.elf -o image.bin
./diskwrite -v kernel.elf -o image.bin
qemu-system-i386 -hda image.bin --no-reboot --no-shutdown -m 32m -smp 2 -serial mon:stdio
tools:
@ -13,6 +16,14 @@ tools:
bootloader:
$(AS) src/bootloader/main.s $(BL_ASFLAGS) -o bin/bootloader.bin
kernel: $(OBJS)
nasm src/kernel/entry.s -o bin/kernel/entry.o -f elf32
ld -T linker.ld bin/kernel/entry.o -melf_i386
%.o: $(SRCS)
$(CC) $(CFLAGS) $< -o $@
init:
@mkdir mount
@mkdir bin

View File

@ -202,7 +202,7 @@ load_part2:
push 0
pop ds
mov si, DAP
; jmp $
int 0x13
jc load_fail
jmp part_2
@ -305,7 +305,7 @@ part_2:
mov edi, kernel_file
call open_file
mov esi, eax
mov edi, file_buffer
mov edi, 0x10000
call read_file
jc load_fail
@ -449,9 +449,8 @@ read_file:
xor edx, edx
mov eax, esi
mov cl, [fat_bpb.sectors_per_cluster]
; jmp $
mul ecx; eax = offset from file table
; jmp $
push eax
xor eax, eax
xor ecx, ecx
@ -471,11 +470,13 @@ read_file:
xor ecx, ecx
mov cl, [fat_bpb.sectors_per_cluster]
mov [DAP.read_count], cx
push edi
shr edi, 16
mov [DAP.segment], di
pop edi
mov [DAP.offset], di
; push edi
; shr edi, 16
; mov [DAP.segment], di
; pop edi
; mov [DAP.offset], di
mov word [DAP.segment], 0
mov word [DAP.offset], file_buffer
mov ah, 0x42
mov dl, [data.boot_disc]
@ -483,11 +484,7 @@ read_file:
int 0x13
jc .exit_err
; debug
pop esi
call read_fat
cmp eax, 0xffffffff
je .exit_success
; debug
xor eax, eax
mov al, [fat_bpb.sectors_per_cluster]
@ -496,6 +493,17 @@ read_file:
mov cx, [fat_bpb.bytes_per_sector]
mul ecx
mov esi, file_buffer
mov ebx, eax
call mmove
pop esi
call read_fat
cmp eax, 0xffffffff
je .exit_success
add edi, eax
jmp read_file
@ -517,19 +525,19 @@ read_fat:
shr esi, 12
cmp esi, [loaded_fat_block]
mov eax, [loaded_fat_block]
; jmp $
je .read
call cache_file_table
; mov eax, [loaded_fat_block]
; mov esi, [file_table + 0x4 * 3]
; jmp $
.read:
pop esi
and esi, 0xfff
; jmp $
mov eax, [esi * 0x4 + file_table]
; jmp $
ret
cache_file_table:
;args:
@ -569,6 +577,30 @@ cache_file_table:
pop edx
ret
jmp $
mmove:
;esi = src
;edi = dest
;ebx = count
push eax
push ecx
; debug
xor ecx, ecx
.mlp:
cmp ecx, ebx
jge .ret
mov al, [ds:esi + ecx]
mov [ds:edi + ecx], al
inc ecx
jmp .mlp
.ret:
; debug
pop ecx
pop eax
ret
load_elf:
;parse elf and relocate each section to it's appropriate location
;kernel info
@ -588,6 +620,7 @@ k_info:
;.type: db 0
kernel_file: db "kernel", 0x0, 0x0, "elf"
loaded_fat_block: dd 0
kload_paddr: dd 0x10000
file_buffer EQU 0xa000
root_dir EQU 0x1000
file_table EQU 0xf000

10
src/kernel/entry.s Normal file
View File

@ -0,0 +1,10 @@
bits 16
global _start
_start:
mov ebx, 0xb8000
mov word [ds:ebx], 0x0f41
cli
hlt
jmp $

3
src/kernel/kmain.c Normal file
View File

@ -0,0 +1,3 @@
void kmain(){
return;
}