From 5382edc2aef6ea70f48f41ee31eacd2e92543cbd Mon Sep 17 00:00:00 2001 From: Ain <41307858+nero@users.noreply.github.com> Date: Sat, 4 Jul 2020 10:35:38 +0200 Subject: [PATCH] Improve ordering of binary data in kernel --- boot/kernel.asm | 133 +++++++++++++++++++++++++----------------------- 1 file changed, 68 insertions(+), 65 deletions(-) diff --git a/boot/kernel.asm b/boot/kernel.asm index 51be791..695d5cf 100644 --- a/boot/kernel.asm +++ b/boot/kernel.asm @@ -1,31 +1,69 @@ - cpu 8086 - org 0x500 - jmp 0:(init+0x7C00-$$) + cpu 8086 + org 0x500 -%include "kernel/fd.asm" -%include "kernel/con.asm" -%include "inc/bpb.asm" -%include "inc/mbr.asm" +%include "inc/bpb.asm" +%include "inc/mbr.asm" - ; Far call via interrupt vector - %macro intcall 1 - call far [cs:%1*4] - %endmacro + ; kernel stack size in words +%define stacksize 512 - ; Far jump via interrupt vector - %macro intjmp 1 - jmp far [cs:%1*4] - %endmacro +init: cli + xor ax, ax + mov ds, ax + mov es, ax + mov ss, ax + mov sp, ( stack+stacksize ) - ; stack size - %define stacks 512 + ; relocate + mov si, 0x7C00 + mov di, $$ + mov cx, (_end-$$) + rep movsb + + jmp 0:main + +main: ; zero out first 64kb except our code + ; bss and boot sector area gets cleared by this + mov cx, di + neg cx + xor ax, ax + rep stosb + + ; install interrupt vectors + mov si, vects + mov di, (0x20*4) + mov cx, 0x0A +intlp: movsw + mov ax, cs + stosw + loop intlp + + ; file descriptor 0 = CON + mov word [fd_table], con_ftab + + ; print banner + mov dx, banner + mov cx, 13 + mov bx, 0 + call fd_write + + call dnconv + mov al, dl + call select + call load_bpb + +loop: int 0x28 + jmp loop + +section .data banner: db "rdos loaded", 0xA, 0xD, '$' +section .text + ; Alias for Int 21h,AH=0h int20h: xor ah, ah - ; allow extenders to get away with only hooking 21h - intjmp 0x21 + jmp far [cs:0x21*4] int21h: ; inside of kernel, direction always goes up ; the iret will restore it to the user value later @@ -53,6 +91,8 @@ iretfl: push ax iret: iret retf: retf +section .data + ; Subfunction ptr ; this is used as extra register in int21h sfptr: dw 0 @@ -78,6 +118,8 @@ sftab: dw sferr, getc, putc, sferr dw sferr, sferr, sferr, sferr dw sferr, sferr, sferr, sferr +section .text + ; OUT al character read getc: xor ax, ax int 0x16 @@ -195,59 +237,20 @@ fputc: push ax pop ax iret + %include "kernel/fd.asm" + %include "kernel/con.asm" + %include "kernel/diskio.asm" %include "kernel/fat.asm" +section .data + vects: dw int20h, int21h, retf, retf dw retf, retf, retf, int20h dw idle, fputc -main: ; zero out first 64kb except our code - ; bss and boot sector area gets cleared by this - mov di, init - mov cx, di - neg cx - xor ax, ax - rep movsb - - ; install interrupt vectors - mov si, vects - mov di, (0x20*4) - mov cx, 0x0A -intlp: movsw - mov ax, cs - stosw - loop intlp - - ; print banner - mov dx, banner - mov cx, 13 - mov bx, 0 - call con_write - - call dnconv - mov al, dl - call select - call load_bpb - -loop: int 0x28 - jmp loop - -init: cli - xor ax, ax - mov ds, ax - mov es, ax - mov ss, ax - mov sp, ( stack+stacks ) - - ; relocate - mov si, 0x7C00 - mov di, $$ - mov cx, (init-$$) - rep movsb - - jmp 0:main +_end: dw 0x55AA, 0x55AA section .bss ; stack to be used during init and disk i/o -stack: resw stacks +stack: resw stacksize