From c70f388548d65f03537c21fed76551c85f78df33 Mon Sep 17 00:00:00 2001 From: Ain <41307858+nero@users.noreply.github.com> Date: Tue, 17 Sep 2019 22:41:39 +0000 Subject: [PATCH] Get MBR ready, add hdimage target --- Makefile | 15 ++++++++--- boot/fatvbr.asm | 4 +++ boot/mbr.asm | 67 ++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 79 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index a4f3f67..c2c2836 100644 --- a/Makefile +++ b/Makefile @@ -29,18 +29,27 @@ kernel.com: kernel/*.asm lib/*.inc debug.rom: debug/*.asm lib/*.inc nasm -s -o $@ -I lib -I debug debug/main.asm && scripts/fix-rom.sh $@ -floppy.bs: boot/fatvbr.asm +boot/floppy.bs: boot/fatvbr.asm nasm -s -DCMDLINE='"KERNEL.COM"' -o $@ $< +boot/mbr.bs: boot/mbr.asm + nasm -s -DFLOPPY=$(FLOPPY) -o $@ $< + programs/%.com: programs/%.asm nasm -s -I lib -o $@ $< -fdimage.img: floppy.bs $(DISTFILES) - mformat -C -i $@ -f $(FLOPPY) -B floppy.bs :: +fdimage.img: boot/floppy.bs $(DISTFILES) + mformat -C -i $@ -f $(FLOPPY) -B boot/floppy.bs :: mcopy -i $@ $(DISTFILES) :: +hdimage.img: boot/mbr.bs fdimage.img + cat boot/mbr.bs fdimage.img >$@ + clean: rm -f *.com *.bin *.rom *.img *.log *.bs *.lst programs/*.com qemu-floppy: fdimage.img $(DEBUGROM) $(QEMU) $(QEMU_ARGS) -boot c -fda fdimage.img + +qemu-hdd: hdimage.img $(DEBUGROM) + $(QEMU) $(QEMU_ARGS) -boot c -hda hdimage.img diff --git a/boot/fatvbr.asm b/boot/fatvbr.asm index a75338e..321454f 100644 --- a/boot/fatvbr.asm +++ b/boot/fatvbr.asm @@ -151,6 +151,10 @@ load_sectors: push cx push dx + ; add partition offset (required for HDD) + add ax, [fdc.po] + adc dx, [fdc.po+2] + ; calculate CHS data div word [cs:fdc.spt] ; ax:temp = (lba / spt) inc dx ; dx:sector = (lba % spt) + 1 diff --git a/boot/mbr.asm b/boot/mbr.asm index 48109f0..c7cb2e2 100644 --- a/boot/mbr.asm +++ b/boot/mbr.asm @@ -2,6 +2,11 @@ %define self 0x00600 ; 1 sector %define prog 0x07C00 ; 1 sector +; FDC fields in VBR +%define spt (prog + 0x18) +%define nos (prog + 0x1A) +%define po (prog + 0x1C) + org self cpu 8086 @@ -26,15 +31,69 @@ init: jmp 0:main main: - mov bp, 0x0035 - mov WORD [dap.blocknum], 1 + mov bp, 0x3335 + mov si, part1 + mov cx, 4 +.loop: + test BYTE [si], 0x80 + jnz loadpart + add si, 0x10 + loop .loop + jmp error + +loadpart: + ; transfer starting address into DAP + push si + add si, 0x08 + mov di, dap.blocknum + movsw + movsw + pop si + + ; load sector + push si mov si, dap + mov bp, 0x3336 mov ah, 0x42 stc int 0x13 jc error - mov cx, [dap.blocknum] - int3 + pop si + + cmp BYTE [si+4], 0x01 + jne jump + +adjust: + push dx + + mov bp, 0x3337 + mov ah, 0x08 + stc + int 0x13 + jc error + + ; update sectors per track + xor ax, ax + mov al, cl + mov [spt], ax + + ; update number of sides + xor ax, ax + mov al, dh + mov [nos], ax + + ; update partition offset + push si + add si, 0x08 + mov di, po + movsw + movsw + pop si + + pop dx + +jump: + jmp 0:prog error: mov ax, bp