Finished bootloader version 1.0.0

This commit is contained in:
notsomeidiot123 2024-10-09 18:15:12 -04:00
parent 7a94090b55
commit c15434078c
5 changed files with 137 additions and 28 deletions

1
.gitignore vendored
View File

@ -6,4 +6,5 @@ diskwrite
*.bin
*.elf
*.dump
intlog.*
*.img

View File

@ -9,8 +9,7 @@ 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
qemu-system-i386 -hda image.bin --no-reboot --no-shutdown -m 32m -smp 2 -serial mon:stdio
qemu-system-i386 -hda image.bin --no-reboot --no-shutdown -m 32m -smp 2 -serial mon:stdio -D intlog.txt -d int
tools:
$(CC) tools/diskwrite.c -o diskwrite

View File

@ -312,7 +312,30 @@ part_2:
mov esi, kload_paddr
call load_elf
push eax
cli
mov eax, page_table
mov cr3, eax
mov eax, cr4
or eax, 1 << 4
mov cr4, eax
mov eax, cr0
or eax, 0x8000_0001
mov cr0, eax
jmp 0x10:protected_mode
protected_mode:
bits 32
mov bx, 0x18
mov ds, bx
mov es, bx
mov ss, bx
mov gs, bx
mov fs, bx
pop eax
jmp eax
jmp $
bits 16
load_kernel:
get_mmap:
pushad
@ -615,45 +638,105 @@ load_elf:
;parse elf and relocate each section to it's appropriate location
;esi: address of loaded kernel;
;buffer = $esi
; debug
push ebp
mov ebp, esp;set up stack frame for function (i'll need a few local variables)
mov [ebp - 4], esi;char *file_buffer = buffer
.ret_err:
mov eax, 1
.ret:
pop ebp
mov eax, esi
xor ecx, ecx
mov cx, [eax + 44];program header count
add eax, [eax + 28];eax = elf.prgm_hdr
push esi
xor ebx, ebx
mov ebx, eax
; mov edi, esi
.loop_head:
dec cx
mov edx, [ebx]
cmp edx, 2
je .err
cmp edx, 3
je .err
cmp edx, 0
je .loop_next
; mov edi, esi
mov edx, esi
pop esi
mov edi, esi
push esi
add edi, [ebx + 4]
mov esi, [ebx + 8]
push ecx
mov ecx, [ebx + 20]
shr ecx, 12
.map_loop:
call map_addr
add edi, 0x1000
add esi, 0x1000
dec ecx
cmp ecx, 0
jne .map_loop
.loop_next:
pop ecx
add ebx, 32
cmp cx, 0
jne .loop_head
.loop_end:
pop esi
mov eax, [esi + 24]
ret
jmp $
.err:
mov ah, 0xe
mov al, 'E'
int 0x10
mov al, '3'
int 0x10
jmp $
map_addr:
;map address in esi to physical address in edi
push ebx
push eax
mov eax, esi
xor ebx, ebx
mov ebx, page_table
shr eax, 22
add ebx, esi
and ebx, ebx
shl eax, 2
add ebx, eax
; and [ebx], [ebx]
mov eax, [ebx]
and eax, 0xffff_f000
cmp [ebx], eax
jnz .map
.allocate_table:
mov edx, [last_allocated_pgtb]
add edx, 0x1000
mov [last_allocated_pgtb], edx
; or edx, 3
mov [ebx], edx
or dword [ebx], 0b0_0001_0000_0001
mov edx, [ebx];
; jmp $
or dword [ebx], 0b0_0000_0000_0011
mov eax, edx
.map:
mov ebx, edx
; debug
mov ebx, eax
mov eax, esi
shr eax, 12
and eax, 0x3ff
add ebx, eax
lea ebx, [ebx + eax*4]
; jmp $
mov [ebx], edi
or dword [ebx], 0b0001_0000_0001
; debug
or dword [ebx], 0b0000_0000_0011
; mov ecx, [ebx]
; jmp $
pop eax
pop ebx
ret
jmp $
@ -682,5 +765,5 @@ kload_paddr EQU 0x10000
file_buffer EQU 0xa000
root_dir EQU 0x1000
file_table EQU 0xf000
page_table EQU 0x4000
page_table EQU 0x00004000
times 4096-($-$$) db 0

View File

@ -2,9 +2,34 @@ bits 32
global _start
section .text
_start:
mov ebx, 0xb8000
mov word [ebx], 0x0f41
cli
hlt
jmp $
mov esi, string
mov ecx, 0
mov ah, 0x8f
; mov ax, 0x55aaaa55
.lp:
lodsb
cmp al, 0
je .end
cmp al, 0xa
je .nl
cmp al, 0xd
je .cr
mov [ebx + ecx], ax
add ecx, 2
jmp .lp
.nl:
add ebx, 160
; mov word [ebx + ecx], 0xf41
jmp .lp
.cr:
xor ecx, ecx
jmp .lp
.end:
jmp $
section .data
string: db "Why even use grub at this point?", 0xa, 0xd, "https://github.com/notsomeidiot123 | someidiot321 on Reddit :3", 0xa, 0xd
db "Check comments for more info ^^", 0x0

View File

@ -157,7 +157,7 @@ int create_file(char *path, fat_info *info, FILE *file){
verbose && printf("file ext: %s\n", path + extoffset);
memmove(dirent.ext, path + extoffset + 1, 3);
dirent.size_bytes = ifsize;
verbose && printf("Writing %u sectors\n", ifsize/(info->bpb.bytes_per_sector * info->bpb.sectors_per_cluster) + 1);
verbose && printf("Writing %u clusters\n", ifsize/(info->bpb.bytes_per_sector * info->bpb.sectors_per_cluster) + 1);
uint32_t last_cluster = 0;
for(int i = 0; i <= ifsize/(info->bpb.bytes_per_sector * info->bpb.sectors_per_cluster); i++){
uint32_t cluster = find_free_cluster(info);
@ -171,10 +171,11 @@ int create_file(char *path, fat_info *info, FILE *file){
verbose && printf("wrote to sector %x %x sectors of data from buffer\n", (info->bpb.reserved_sectors + info->bpb.fat_count * info->bpb.sectors_per_fat) + cluster * info->bpb.sectors_per_cluster, info->bpb.sectors_per_cluster);
if(last_cluster != 0) write_cluster_value(info, cluster, last_cluster);
write_cluster_value(info, FILE_END, cluster);
last_cluster = cluster;
}
verbose && printf("last cluster: %d\n", last_cluster);
write_cluster_value(info, FILE_END, last_cluster);
// write_cluster_value(info, FILE_END, last_cluster);
buffer_t root_dir_buffer = 0;
uint32_t dirent_count = read_file(info, info->bpb.root_dir_cluster, 0, &root_dir_buffer, file)/sizeof(file_t);