From e4ec1b1724c9aaaf8884f27601b632f03a747931 Mon Sep 17 00:00:00 2001 From: notsomeidiot123 Date: Sat, 14 Sep 2024 10:19:50 -0400 Subject: [PATCH] stage two loading and partition detection --- .gitignore | 3 +- docs/boot.md | 33 +++++++++++ makefile | 4 +- src/bootloader/main.s | 133 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 154 insertions(+), 19 deletions(-) create mode 100644 docs/boot.md diff --git a/.gitignore b/.gitignore index 0d35ace..39dbb38 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ bin mount -image.bin \ No newline at end of file +image.bin +dump.img \ No newline at end of file diff --git a/docs/boot.md b/docs/boot.md new file mode 100644 index 0000000..1a69c5d --- /dev/null +++ b/docs/boot.md @@ -0,0 +1,33 @@ +# Kimi's Bootloader Documentation + +## Basic Information + +Kimi's Bootloader is a custom booting solution and the default bootloader for Kimi's OS on legacy BIOS systems. It loads the default x86 GDT to be used by the Kernel, along with any disk or filesystem modules which may be present in the files `ddrv.mod` and `dfs.mod` + +The bootloader also sets the video mode to the closest mode present to Standard definition, and obtains the BIOS memory map to be passed to the Kernel. + +### A20 Line + +Due to the fact that many x86 PCs POST without enabling the A20 line, the bootloader attempts to enable it through 3 distinct methods, starting with + +1. Enabling through BIOS + +Kimi's Bootloader will first attempt to enable the A20 line via the BIOS before attempting any other method of enabling the A20 line. This method is the least likely to have any unintended side effects, which is why it is the first of the three methods to be attempted + +2. Enabling via Fast A20 method + +The bootloader then attempts to enable the A20 by using the fast A20 method + +3. Enabling through keyboard + +If all else fails, the bootloader will attempt to enable the a20 line by using the legacy way of enabling the A20 line - by using Intel's 8042 Keyboard controller. This is the most likely to succeed on any chipsets with a PS/2 controller still present, but will fail if one is not. + +### GDT + +While any GDT loaded for the Kernel will work, once the Kernel wants to load user processes, it will fail to locate user segments in the GDT if the one loaded is not the one expected. Because of this, the Kernel will need to load it's own if the one it wants is not loaded by this bootloader. To allow for detection, the key "0xaa55" is written at the end of the GDT, marking the last segment invalid, but allowing for the detection of the correct GDT being pre-loaded. This detection will only occurr during boot time, and unexpected writes will cause the Kernel to crash. + +### Error Codes + +|Error Code|Description| +|----------|-----------| +|1 |A20 Line could not be enabled| \ No newline at end of file diff --git a/makefile b/makefile index b3ab604..8b74f57 100644 --- a/makefile +++ b/makefile @@ -4,8 +4,8 @@ BL_ASFLAGS := -f bin all: bootloader cp bin/bootloader.bin image.bin - qemu-img resize image.bin 512M - qemu-system-i386 bin/bootloader.bin --no-reboot --no-shutdown -m 32m -smp 2 + qemu-img resize -f raw image.bin 512M + qemu-system-i386 -hda bin/bootloader.bin --no-reboot --no-shutdown -m 32m -smp 2 -serial mon:stdio bootloader: $(AS) src/bootloader/main.s $(BL_ASFLAGS) -o bin/bootloader.bin diff --git a/src/bootloader/main.s b/src/bootloader/main.s index 79dd455..d49ca58 100644 --- a/src/bootloader/main.s +++ b/src/bootloader/main.s @@ -1,3 +1,8 @@ +; Kimi's Bootloader - Custom Booting solution for Kimi's OS +; (c) 2024 Notsomeidiot123 on Github +; Documentation available at https://github.com/notsomeidiot123/KimisOS + + org 0x7c00 bits 16 @@ -9,18 +14,19 @@ bits 16 %endmacro ;TODO: -;Enable A20 line -;Enable unreal mode -;Load GDT -;Load part2 -;Set video mode -;get memory map -;enable paging -;read filesystem -;load kernel -;parse ELF format -;pass memory and video information to kernel -;relocate and jump to kernel +;Enable A20 line |x| +;Enable unreal mode |x| +;Load GDT |x| +;Load part2 |x| +;Get video mode information |0| +;Set video mode |0| +;get memory map |0| +;enable paging |0| +;read filesystem |0| +;load kernel |0| +;parse ELF format |0| +;pass memory and video information to kernel|0| +;relocate and jump to kernel |0| jump: jmp short start @@ -88,6 +94,7 @@ start: mov ds, bx mov [data.part_seg], ax mov [data.part_off], si + mov [data.boot_disc], dl jmp 0:set_cs set_cs: xor cx, cx @@ -113,7 +120,7 @@ enable_a20: in al, 0x92 or al, 2 out 0x92, al - mov ax, 100000 + mov ax, 10000 .lp: dec ax cmp ax, 0 @@ -159,14 +166,58 @@ enable_a20: test al, 1 jnz .wait2 ret - load_gdt: + lgdt [GDT_desc] + push ds + mov eax, cr0 + or al, 1 + mov cr0, eax + jmp 0x8:.unreal + .unreal: + mov bx, 0x18 + mov ds, bx + and al, 0xfe + mov cr0, eax + jmp 0x0:.real + .real:;no actual code here, just label for readability and organization load_part2: + mov ah, 0x42 + xor edx, edx + mov dx, word [data.part_seg] + shl edx, 4 + xor ecx, ecx + mov cx, word [data.part_off] + add edx, ecx + + mov al, byte [edx] + and al, 0x80 + jz .read + mov ecx, [edx + 0x8] + + add [DAP.sector_start], ecx + .read: + mov dl, byte [data.boot_disc] + push 0 + pop ds + mov si, DAP + int 0x13 + jc load_fail + jmp part_2 + jmp $ no_a20: mov ah, 0xe mov al, 'E' int 0x10 + mov al, '1' + int 0x10 + jmp $ +load_fail: + mov ah, 0xe + mov al, 'E' + int 0x10 + mov al, '2' + int 0x10 jmp $ DAP: .sz: db 0x10 @@ -177,8 +228,56 @@ DAP: .sector_start: dd 1 dd 0 - - +GDT_desc: + .size: dw GD_TABLE-GD_END - 1 + .offset: dd GD_TABLE +GD_TABLE: + .null: + dd 0 + dd 0 + .kcode16: + dw 0xffff + dw 0x0000 + db 0x00 + db 0b10011010 + db 0b00001111 + db 0x00 + .kcode32: + dw 0xffff + dw 0x0000 + db 0x00 + db 0b10011010 + db 0b11001111 + db 0x00 + .kdata32: + dw 0xffff + dw 0x0000 + db 0x00 + db 0b10010010 + db 0b11001111 + db 0x00 + .ucode32: + dw 0xffff + dw 0x0000 + db 0x00 + db 0b11111010 + db 0b11001111 + db 0x00 + .udata32: + dw 0xffff + dw 0x0000 + db 0x00 + db 0b11110010 + db 0b11001111 + db 0x00 + .tss: + dd 0 + dd 0 + .phony: + dw 0xaa55 + dd 0 + dw 0x55aa +GD_END: data: .boot_disc: db 0 .part_seg: dw 0 @@ -196,6 +295,8 @@ fs_info: .trail: dd 0xAA550000 times 1024-($-$$) db 0 part_2: + debug + jmp $ load_part3: get_mmap: set_video_mode: