diff --git a/Makefile b/Makefile index 11741ba..b0a9026 100644 --- a/Makefile +++ b/Makefile @@ -2,18 +2,18 @@ FD_CYLINDERS = 40 FD_HEADS = 2 FD_SECTORS = 9 -default: kernel.com +default: kernel.rom -kernel.bin: kernel/*.asm - nasm -s -o $@ -I kernel kernel/main.asm +kernel.rom: kernel/*.asm + nasm -s -o $@ -I kernel kernel/main.asm && ./fix-rom.sh $@ boot/%.bin: boot/%.asm nasm -s -o $@ -I lib $< -fdimage.img: boot/floppy.bin kernel.bin +fdimage.img: boot/floppy.bin kernel.rom dd if=/dev/zero bs=512 count=$$(( $(FD_CYLINDERS) * $(FD_HEADS) * $(FD_SECTORS) )) of=$@ mformat -i $@ -t $(FD_CYLINDERS) -h $(FD_HEADS) -n $(FD_SECTORS) -B boot/floppy.bin :: - mcopy -i $@ kernel.bin ::boot.bin + mcopy -i $@ kernel.rom ::kernel.rom clean: - rm -f *.com *.bin *.img boot/*.bin + rm -f *.com *.bin *.rom *.img boot/*.bin diff --git a/boot/floppy.asm b/boot/floppy.asm index 0cfcba1..3cce55f 100644 --- a/boot/floppy.asm +++ b/boot/floppy.asm @@ -48,10 +48,6 @@ _startup: ; setup stack area growing down from 0x10000 mov ss, ax mov sp, ax - ; backup important values - push bx - push cx - push dx ; adjust CS jmp 0x07C0:main @@ -63,6 +59,11 @@ rootdir: dw 0 main: + ; backup important values + push bx + push cx + push dx + ; setup buffer area directly at 4k mov ax, 0x0100 mov ds, ax @@ -78,13 +79,16 @@ main: ; restore important variables xor ax, ax - mov bp, ax - mov si, ax - mov di, ax pop dx pop cx pop bx - jmp 0x0100:0000 + mov di, [0x001A] + mov ax, [di+0x1A] + push ds + push ax + retf + call error + db "ROM ERROR", 0 ; Write AX, a string and bail out ; Return pointer is the string start @@ -228,9 +232,9 @@ loadblk: times (0x1F0 - ($-$$)) db 0 bootfilename: - db "BOOT" + db "KERNEL" times (0x1F8 - ($-$$)) db " " - db "BIN" + db "ROM" times (0x1FE - ($-$$)) db 0 diff --git a/fix-rom.sh b/fix-rom.sh new file mode 100755 index 0000000..9fdeb65 --- /dev/null +++ b/fix-rom.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +setbyte() { + printf "\x$3"|dd status=none seek="$2" bs=1 count=1 conv=notrunc of="$1" +} + +[ -n "$1" ] || exit 1 + +setbyte "$1" 2 $(stat -c "%s" "$1"|awk '{printf("%02x",$1/512)}') + +setbyte "$1" 41 00 +checksum=$(xxd -p -c 1 "$1"|sed 's/^/0x/'|awk '{s+=$1} END {printf("%02x\n",256-s%256)}') +setbyte "$1" 41 "$checksum" \ No newline at end of file diff --git a/kernel/main.asm b/kernel/main.asm index daba3b3..2ad6d00 100644 --- a/kernel/main.asm +++ b/kernel/main.asm @@ -1,36 +1,119 @@ -; assumptions about the starting envionment -cpu 8086 -org 0x0000 - -_startup: - mov ax, cs - mov ss, ax - mov sp, 0x0000 - ; <0x0010 is BIOS segment when relocated to HMA - ; so we need a nop sled - times (0x10 - ($-$$)) nop -main: - call intr_init - - int 3 - - mov ah, 0x01 - int 0x21 - mov dl, al - mov ah, 0x02 - int 0x21 - - sti -.halt: - hlt - jmp .halt - -%include "intr.asm" -%include "debug.asm" -%include "kprintf.asm" -%include "dosapi.asm" - -_reloc_end: - -align 16 -heap: +cpu 8086 +org 0x0000 + db 0x55, 0xAA, 0x00 + jmp optrom_init + +times (0x1A - ($-$$)) db 0 + dw pnp + +align 16 +pnp: + db "$PnP" + db 1 ; version 1 + db 2 ; 2 * 16 length + dw 0 ; offset of next header + db 0 + db 0 ; checksum (filled by fix-rom) + dd 0 ; device identifier + dw .vendor ; manufacturer string + dw .product ; product name string + db 0,0,0 ; device type string + db 0x20 ; device indicator, bit for "read cacheable" set + dw 0 ; boot connection vector + dw 0 ; boot disconnect vector + dw start ; bootstrap entry point + dw 0 ; reserved + dw 0 +.vendor: + db "Nero", 0 +.product: + db "2B", 0 + +align 16 +optrom_init: + push di + push es + xor ax, ax + mov es, ax + mov di, 0x0060 ; vector (0x18 * 4 bytes) + mov ax, start + stosw + mov ax, cs + stosw + pop es + pop di + retf + +start: + call intr_init + + sti + + mov si, 0x0000 + mov ds, si + +.loop: + push bx + mov dx, 0x0000 + mov ah, 0x02 + mov bx, 0x0000 + int 0x10 + pop bx + + call dump + + mov ah, 0x01 + int 0x21 + + cmp al, 0x30 + jne .notup + sub bx, 0x0100 +.notup: + cmp al, 0x31 + jne .notdown + add bx, 0x0100 +.notdown: + jmp .loop + +.halt: + hlt + jmp .halt + +dump: + mov si, bx + mov cx, 0x10 +.nextline: + mov ax, si + call kprint16 + + mov al, 0x3A + call kputc + mov al, 0x20 + call kputc + + push cx + mov cx, 0x0020 +.bytes: + lodsb + call kprint8 + loop .bytes + pop cx + + mov al,0x20 + call kputc + mov al,0x0A + call kputc + mov al,0x0D + call kputc + + loop .nextline + ret + +%include "intr.asm" +%include "debug.asm" +%include "kprintf.asm" +%include "dosapi.asm" + +_reloc_end: + +align 512 \ No newline at end of file