From 567e09f7e9c8c7fa5899d0f755398ef392d893b3 Mon Sep 17 00:00:00 2001 From: Ain <41307858+nero@users.noreply.github.com> Date: Sun, 15 Sep 2019 17:26:09 +0000 Subject: [PATCH] Bootloader: parse cmdline field --- boot/floppy.asm | 104 ++++++++++++++++++++++++++++++------------------ 1 file changed, 65 insertions(+), 39 deletions(-) diff --git a/boot/floppy.asm b/boot/floppy.asm index 1453f4e..3005cba 100644 --- a/boot/floppy.asm +++ b/boot/floppy.asm @@ -1,17 +1,24 @@ ; Memory layout: -%define mbr 0x00600 ; 1 sector -%define self 0x00800 ; 1 sector -%define fattab 0x01000 ; variable size -%define rootdir 0x06000 ; variable size -%define psp 0x07B00 ; 0x100 bytes -%define prog 0x07C00 ; 64k - psp +%define mbr 0x00600 ; 1 sector +%define self 0x00800 ; 1 sector +%define fattab 0x01000 ; variable size +%define rootdir 0x06000 ; variable size +%define psp (prog - 0x100) +%define prog 0x07C00 + +%define filename (psp + 0x5C + 1) +%define arguments (psp + 0x81) org self +jmp short init + +cluster_offset: + dw 0 + +times (0x0B - ($-$$)) db 0 + fdc: -.jmp: - jmp short main - times (0x0B - ($-$$)) db 0 .ss: dw 0x200 ; sector size .sc: @@ -49,7 +56,7 @@ fdc: db "FAT12" times (62 - ($-$$)) db " " -main: +init: cli ; Stack grows down from PSP + 64k @@ -66,10 +73,9 @@ main: mov cx, 0x100 rep movsw - ; Jump into relocated code - jmp 0:.relocated -.relocated: + jmp 0:main +main: mov [fdc.drv], dl ; save drive number in fd sti @@ -98,26 +104,12 @@ main: ; clusters start after rootdir mov [cluster_offset], ax + call parse_cmdline + call load_file - mov bp, 0x0032 + mov bp, 0x3332 jc error - ; get length of arguments - mov cx, 0x007F - mov di, arguments - xor ax, ax - repne scasb - dec di - sub di, arguments - mov cx, di - - ; setup arguments field - mov [psp+0x080], cl - mov di, 0x081 - mov si, arguments - rep movsb - mov BYTE [di], 0x0D - ; setup int 19h call at 0000, and push its address to stack mov WORD [psp], 0x19CD push sp @@ -142,7 +134,7 @@ main: load_sectors: ; fail instantly if reading sectors > 16 bit test dx, dx - mov bp, 0x0030 + mov bp, 0x3330 jnz error .loop: @@ -162,7 +154,7 @@ load_sectors: mov dl, [cs:fdc.drv] ; driver number mov ax, 0x0201 ; ah=0x02 al=0x01 int 0x13 - mov bp, 0x0031 + mov bp, 0x3331 jc error pop dx @@ -186,6 +178,7 @@ load_file: je read_clusters add si, 0x20 loop .loop + stc ret ; Compares the FAT dirent against [filename] @@ -252,14 +245,47 @@ error: int 0x16 int 0x19 -cluster_offset: - dw 0 +parse_cmdline: + mov si, cmdline + mov di, filename +.cleanout: + push di + mov cx, 0x0A + mov al, 0x20 + rep stosb + pop di +.base_loop: + call .read + cmp al, 0x2E + je .ext_start + cmp al, 0x20 + je .args_start + stosb + jmp .base_loop +.ext_start: + mov di, (filename + 8) +.ext_loop: + call .read + cmp al, 0x20 + je .args_start + stosb + jmp .ext_loop +.args_start: + mov di, (psp + 0x81) +.args_loop: + call .read + stosb + jmp .args_loop +.read: + lodsb + test al, al + jnz .ret + pop ax +.ret: + ret -filename: - db "KERNEL COM" - -arguments: - db "", 0 +cmdline: + db "KERNEL.COM /?", 0 times (0x1FE - ($-$$)) db 0