From c5fb41ceb4edaee21ddbe208e43262093628a7b6 Mon Sep 17 00:00:00 2001 From: HimbeerserverDE Date: Sun, 11 Sep 2022 23:17:44 +0200 Subject: [PATCH] Broken stage 2 loader --- Makefile | 4 ++-- stage1/boot.asm | 29 +++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 9856b63..d6a4fe5 100644 --- a/Makefile +++ b/Makefile @@ -11,9 +11,9 @@ stage2/target/x86_64-loadnothing/debug/stage2: stage2/src/main.rs cd stage2 && cargo build nothing.img: magic.bin stage1/boot.bin stage2/target/x86_64-loadnothing/debug/stage2 - dd if=/dev/zero of=nothing.img bs=32M count=1 + dd if=/dev/zero of=nothing.img bs=16M count=1 parted -s nothing.img mklabel msdos - parted -s -a optimal nothing.img mkpart primary fat32 16M 100% + parted -s -a optimal nothing.img mkpart primary fat32 256s 100% doas losetup /dev/loop1 nothing.img doas mkfs.fat /dev/loop1p1 doas losetup -d /dev/loop1 diff --git a/stage1/boot.asm b/stage1/boot.asm index 096a401..9848469 100644 --- a/stage1/boot.asm +++ b/stage1/boot.asm @@ -1,6 +1,9 @@ [bits 16] [org 0x7c00] +%define STAGE2START 0x7e00 +%define STAGE2SIZE 0xFF + ; Initialize registers xor ax, ax mov ds, ax @@ -33,6 +36,13 @@ print_bytes_si_loop: ret +stage2_error: + mov ch, 33 ; Our string is 33 characters long + mov si, error + call print_bytes_si + + jmp $ ; Infinite loop + ; Main boot: ; Clear the screen @@ -56,8 +66,23 @@ boot: mov si, hello call print_bytes_si - jmp $ ; Infinite loop + mov ah, 0x02 ; Read sectors + mov al, STAGE2SIZE ; Stage 2 size (16 MiB) in sectors + xor ch, ch ; Cylinder 0 + mov cl, 2 ; Second sector, they start at 1 + xor dh, dh ; Head 0 + xor dl, dl ; Drive 0 + mov bx, STAGE2START ; Memory address to load stage 2 into + int 0x13 + + jc stage2_error ; Carry flag is set if there was an error + + cmp al, STAGE2SIZE ; Have we read as many sectors as we requested? + jne stage2_error + + jmp STAGE2START ; Hand over control to stage 2 -hello db 'Welcome to loadnothing stage 1!', 13, 10 ; \r\n +hello db 'Welcome to loadnothing stage 1!', 13, 10 ; \r\n +error db 'Error reading stage 2 from disk', 13, 10 ; \r\n times (446 - ($ - $$)) db 0x00 -- 2.44.0