diff --git a/Makefile b/Makefile index a5201dc9..5ebcefc0 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ thor.flp: bootloader/stage1.bin bootloader/stage2.bin kernel/kernel.bin dd if=bootloader/stage1.bin of=hdd.img conv=notrunc dd if=bootloader/stage2.bin of=hdd.img seek=1 conv=notrunc sudo /sbin/losetup -o1048576 /dev/loop0 hdd.img + sudo mkdosfs -F32 /dev/loop0 sudo /bin/mount -t vfat /dev/loop0 /mnt/fake_cdrom/ sudo /bin/cp kernel/kernel.bin /mnt/fake_cdrom/ sleep 0.1 diff --git a/bootloader/stage2.asm b/bootloader/stage2.asm index 91d6436a..758bf24c 100644 --- a/bootloader/stage2.asm +++ b/bootloader/stage2.asm @@ -12,7 +12,7 @@ jmp second_step %include "intel_16.asm" %include "sectors.asm" -KERNEL_BASE equ 0x100 ; 0x100:0x0 = 0x1000 +KERNEL_BASE equ 0x200 ; 0x200:0x0 = 0x2000 DAP: .size db 0x10 @@ -215,15 +215,109 @@ second_step: jmp error_end - ; TODO Find kernel.bin in the directory - .found: mov si, kernel_found call print_line_16 + ; 4. Load the kernel into memory + + mov ax, [cluster_low] + mov [current_cluster], ax + mov word [current_segment], KERNEL_BASE + +.next_cluster: + mov si, star + call print_16 + + mov ah, [sectors_per_cluster] + mov byte [DAP.count], ah + mov word [DAP.offset], 0x0 + + mov ax, [current_segment] + mov word [DAP.segment], ax + + ; Compute LBA from current_cluster + mov ax, [current_cluster] + sub ax, 2 + movzx bx, byte [sectors_per_cluster] + mul bx + mov bx, [cluster_begin] + add ax, bx + + mov word [DAP.lba], ax + + mov ah, 0x42 + mov si, DAP + mov dl, 0x80 + int 0x13 + + jc read_failed + + ; Compute next cluster + + ; Compute the sector of the FAT to read + mov ax, [current_cluster] + shl ax, 2 ; current_cluster * 4 + shr ax, 9 ; (current_cluster * 4) / 512 + mov bx, [fat_begin] + add ax, bx ; fat_sector + + ; Read the FAT sector + mov byte [DAP.count], 1 + mov word [DAP.offset], 0x100 + mov word [DAP.segment], 0x0 + mov word [DAP.lba], ax + + mov ah, 0x42 + mov si, DAP + mov dl, 0x80 + int 0x13 + + jc read_failed + + mov si, [current_cluster] + and si, 512 - 1 ; current_cluster % 512 + shl si, 2 + + ; cluster low + mov ax, [gs:(0x100 + si)] + ; cluster high + mov bx, [gs:(0x100 + si + 2)] + + cmp bx, 0x0FFF + jl .ok + + cmp ax, 0xFFF7 + je corrupted + + cmp ax, 0xFFF8 + jge .fully_loaded + +.ok: + mov [current_cluster], ax + + mov ax, sectors_per_cluster + mov bx, 0x20 + mul bx + mov bx, [current_segment] + add ax, bx + mov [current_segment], ax + + jmp .next_cluster + +.fully_loaded: + mov si, kernel_loaded + call print_line_16 + jmp $ +corrupted: + mov si, corrupted_disk + call print_line_16 + + jmp error_end + read_failed: mov si, read_failed_msg call print_line_16 @@ -251,12 +345,16 @@ error_end: size_high dw 0 size_low dw 0 + current_cluster dw 0 + current_segment dw 0 + ; Constant Datas load_kernel db 'Attempt to load the kernel...', 0 kernel_found db 'Kernel found. Starting kernel loading...', 0 kernel_not_found db 'Kernel not found...', 0 kernel_loaded db 'Kernel fully loaded', 0 + corrupted_disk db 'The disk seeems to be corrupted', 0 star db '*', 0 read_failed_msg db 'Read disk failed', 0 diff --git a/kernel/src/fat32.cpp b/kernel/src/fat32.cpp index ec12053b..299fa355 100644 --- a/kernel/src/fat32.cpp +++ b/kernel/src/fat32.cpp @@ -93,10 +93,10 @@ struct long_entry { static_assert(sizeof(cluster_entry) == 32, "A cluster entry is 32 bytes"); static_assert(sizeof(long_entry) == 32, "A cluster entry is 32 bytes"); -constexpr const bool CLUSTER_FREE = 0x0; -constexpr const bool CLUSTER_RESERVED= 0x1; -constexpr const bool CLUSTER_CORRUPTED = 0x0FFFFFF7; -constexpr const bool CLUSTER_END = 0x0FFFFFF8; +constexpr const uint32_t CLUSTER_FREE = 0x0; +constexpr const uint32_t CLUSTER_RESERVED= 0x1; +constexpr const uint32_t CLUSTER_CORRUPTED = 0x0FFFFFF7; +constexpr const uint32_t CLUSTER_END = 0x0FFFFFF8; uint64_t cached_disk = -1; uint64_t cached_partition = -1;