From 8b750aabe94c810fbeaf8e83ca8653f2c9c81d5d Mon Sep 17 00:00:00 2001 From: Ain <41307858+nero@users.noreply.github.com> Date: Sun, 24 Mar 2019 18:24:25 +0000 Subject: [PATCH] kernel: Generalize startup code into main --- Makefile | 7 ---- kernel/Makefile | 7 ++++ kernel/main.asm | 60 ++++++++++++++++++++++++++++++--- run.sh => kernel/test-pxenbp.sh | 4 +-- nbp.asm | 59 -------------------------------- 5 files changed, 65 insertions(+), 72 deletions(-) delete mode 100644 Makefile create mode 100644 kernel/Makefile rename run.sh => kernel/test-pxenbp.sh (69%) delete mode 100644 nbp.asm diff --git a/Makefile b/Makefile deleted file mode 100644 index 32e8d85..0000000 --- a/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -default: nbp.com - -%.com: %.asm - nasm -s -o $@ $< - -clean: - rm -f *.com diff --git a/kernel/Makefile b/kernel/Makefile new file mode 100644 index 0000000..3f2b3de --- /dev/null +++ b/kernel/Makefile @@ -0,0 +1,7 @@ +default: kernel.com + +kernel.com: *.asm + nasm -s -o kernel.com main.asm + +clean: + rm -f *.com diff --git a/kernel/main.asm b/kernel/main.asm index fe1fa2e..55bb8fb 100644 --- a/kernel/main.asm +++ b/kernel/main.asm @@ -1,3 +1,53 @@ +; assumptions about the starting envionment +cpu 8086 +org 0x0000 + +_startup: + xor ax, ax ; trash ax + mov ax, cs + cmp ax, 0x0000 ; MBR or NBP, CS=0000 IP=7C00 + je .mbr + cmp ax, 0x07C0 ; MBR on weird BIOS, CS=07C0 IP=0000 + je .mbr +.dos: + ; ROP because call only works on immediate or memory + mov ax, cs + push ax ; return CS + mov ax, .dosret + add ax, 0x0100 + push ax ; return IP + mov ax, cs + add ax, 0x0010 + push ax ; main CS + mov ax, main + push ax ; main IP + retf +.dosret: + int 0x20 + jmp .halt +.mbr: + push si ; index of partition table entry + push ds ; segment for ^ + push dx ; dl might contain drive number + push bx ; possible offset for pxeenv+ struct + push es ; segment for ^ + + mov ax, 0x0050 + mov es, ax + mov di, 0x0000 + + xor ax, ax + mov ds, ax + mov si, 0x7C00 + + mov cx, _reloc_end + rep movsb + + call 0x0050:main +.halt: + hlt + jmp .halt + main: mov ax, cs mov ds, ax @@ -10,10 +60,12 @@ main: retf -%include "kernel/heap.asm" -%include "kernel/intr.asm" -%include "kernel/debug.asm" -%include "kernel/kprintf.asm" +%include "heap.asm" +%include "intr.asm" +%include "debug.asm" +%include "kprintf.asm" + +_reloc_end: align 16 heap: diff --git a/run.sh b/kernel/test-pxenbp.sh similarity index 69% rename from run.sh rename to kernel/test-pxenbp.sh index 8d144c3..1bd108f 100755 --- a/run.sh +++ b/kernel/test-pxenbp.sh @@ -1,5 +1,5 @@ #!/bin/sh -exec qemu-system-x86_64 -boot n -fda fd0.img \ +exec qemu-system-x86_64 -boot n \ -option-rom /usr/share/qemu/pxe-rtl8139.rom \ -device e1000,netdev=mynet0,mac=52:54:00:12:34:56 \ - -netdev user,id=mynet0,net=192.168.76.0/24,dhcpstart=192.168.76.9,tftp=$PWD,bootfile=nbp.com + -netdev user,id=mynet0,net=192.168.76.0/24,dhcpstart=192.168.76.9,tftp=$PWD,bootfile=kernel.com diff --git a/nbp.asm b/nbp.asm deleted file mode 100644 index 8039ff6..0000000 --- a/nbp.asm +++ /dev/null @@ -1,59 +0,0 @@ -; assumptions about the starting envionment -cpu 8086 -org 0x0000 -; es:bx pxeenv+ structure -; ss:sp functional stack - -_startup: - xor ax, ax - mov ax, cs - cmp ax, 0x0000 ; MBR or NBP, CS=0000 IP=7C00 - je .mbr - cmp ax, 0x07C0 ; MBR on weird BIOS, CS=07C0 IP=0000 - je .mbr -.dos: - mov ax, cs - push ax ; return CS - mov ax, .dosret - add ax, 0x0100 - push ax ; return IP - - mov ax, cs - add ax, 0x0010 - push ax ; main CS - mov ax, main - push ax ; main IP - retf -.dosret: - int 0x20 - int 0x18 -.mbr: - mov ax, 0x0050 - mov ds, ax - mov [0xFFFE], ss ; save current stack configuration onto new stack - mov [0xFFFC], sp - mov ss, ax - mov sp, 0xFFFC - - push dx - push bx ; possible far ptr to pxeenv+ struct - push es - - mov es, ax ; 0x0050 - mov di, 0x0000 - - xor ax, ax - mov ds, ax - mov si, 0x7C00 - - mov cx, _reloc_end - rep movsb - - call 0x0050:main -.halt: - hlt - jmp .halt - -%include "kernel/main.asm" - -_reloc_end: