From 8126975176ff62f572f717933427d072eea4c2f5 Mon Sep 17 00:00:00 2001 From: Tom-on64 Date: Thu, 28 Dec 2023 18:30:42 +0100 Subject: [PATCH] Added disk.asm include file --- build.sh | 31 ++--- src/boot.asm | 2 +- src/filetable.asm | 6 +- src/fs/edit.asm | 11 +- src/kernel.asm | 11 +- src/lib/disk.asm | 281 +++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 312 insertions(+), 30 deletions(-) diff --git a/build.sh b/build.sh index da64c24..a63ba66 100644 --- a/build.sh +++ b/build.sh @@ -1,29 +1,32 @@ #!/bin/bash -# Create build dir if it doesn't exist -if !(test -d ./build); then - mkdir ./build +# Create ./build dir if it doesn't exist +if !(test -d ././build); then + mkdir ././build + echo "Created ./build/ directory!" fi # Bootloader -nasm -fbin src/boot.asm -o build/boot.bin -nasm -fbin src/filetable.asm -o build/filetable.bin -cat build/boot.bin build/filetable.bin > build/bootloader.bin +nasm -fbin src/boot.asm -o ./build/boot.bin +nasm -fbin src/filetable.asm -o ./build/filetable.bin # Kernel -nasm -fbin src/kernel.asm -o build/kernel.bin +nasm -fbin src/kernel.asm -o ./build/kernel.bin # Programs -nasm -fbin src/fs/calc.asm -o build/calc.bin -nasm -fbin src/fs/edit.asm -o build/edit.bin +nasm -fbin src/fs/calc.asm -o ./build/calc.bin +nasm -fbin src/fs/edit.asm -o ./build/edit.bin -cat build/calc.bin build/edit.bin > build/files.bin +cat ./build/calc.bin ./build/edit.bin > ./build/files.bin # Final binary -cat build/bootloader.bin build/kernel.bin build/files.bin > build/kern.bin +cat ./build/boot.bin ./build/filetable.bin ./build/kernel.bin ./build/files.bin > ./build/temp.bin +dd if=/dev/zero of=./build/kern.iso bs=512 count=2880 +dd if=./build/temp.bin of=./build/kern.iso conv=notrunc +rm -rf ./build/**.bin -# Cleanup -rm build/*[!kern].bin +echo "Built kern.iso!" # Run QEMU -qemu-system-x86_64 -drive format=raw,file="./build/kern.bin",index=0,media=disk -m 256M +echo "Running QEMU..." +qemu-system-x86_64 -drive format=raw,file="./build/kern.iso",index=0,media=disk -m 256M diff --git a/src/boot.asm b/src/boot.asm index 604b675..efeec90 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -43,7 +43,7 @@ start: xor bx, bx mov ah, 2 ; Read from disk - mov al, 3 ; Read 3 sectors (1.5kB) + mov al, 4 ; Read 4 sectors (2kB) mov dl, byte [driveNumber] ; Drive number mov ch, 0 ; Cylinder diff --git a/src/filetable.asm b/src/filetable.asm index f53c21c..b83d9a4 100644 --- a/src/filetable.asm +++ b/src/filetable.asm @@ -15,8 +15,8 @@ filetable: ; db 'all',0,0,0,0,0,0,0,'txt',0,1,7 ; all - start: 1, size: 7 db 'boot',0,0,0,0,0,0, 'bin',0,1,1 ; boot.bin - start: 1, size: 1 db 'filetable',0, 'txt',0,2,1 ; filetable.txt - start: 2, size: 1 - db 'kernel',0,0,0,0, 'bin',0,3,3 ; kernel.bin - start: 3, size: 3 - db 'calc',0,0,0,0,0,0, 'bin',0,6,1 ; calc.bin - start: 6, size: 1 - db 'edit',0,0,0,0,0,0, 'bin',0,7,2 ; edit.bin - start: 6, size: 2 + db 'kernel',0,0,0,0, 'bin',0,3,4 ; kernel.bin - start: 3, size: 4 + db 'calc',0,0,0,0,0,0, 'bin',0,7,1 ; calc.bin - start: 7, size: 1 + db 'edit',0,0,0,0,0,0, 'bin',0,8,3 ; edit.bin - start: 8, size: 2 times 512-($-$$) db 0 diff --git a/src/fs/edit.asm b/src/fs/edit.asm index fa11b32..2d8244a 100644 --- a/src/fs/edit.asm +++ b/src/fs/edit.asm @@ -104,12 +104,15 @@ return: mov gs, ax jmp 0x2000:0x0000 -; - Includes - +;; Includes %include "./src/lib/print.asm" -; ------------ +%include "./src/lib/disk.asm" +;; Variables statusBarMsg: db " -- HEX -- ", 0 -controlsMsg: db "'$' to execute code; '!' to quit", 0 +controlsMsg: db "'$' to execute code; '!' to quit; 's' to save", 0 +fileChoiceMsg: db "'n' for new file; 'o' to open a file", 0 +chooseModeMsg: db "'x' for Hex mode; 't' for Text mode", 0 runMsg: db ENDL, ENDL, 0 ranMsg: db ENDL, ENDL, " - Finished Execution - ", ENDL, 0 @@ -121,4 +124,4 @@ codeLen: db 0 hexCode: times 256 db 0 ; Code buffer ret -times 2*512-($-$$) db 0 +times 3*512-($-$$) db 0 diff --git a/src/kernel.asm b/src/kernel.asm index 3fb55e1..7c68e39 100644 --- a/src/kernel.asm +++ b/src/kernel.asm @@ -402,15 +402,11 @@ graphicsTest: ; reboot: jmp 0xffff:0x0000 -; ---------- -; Includes -; ---------- +;; Includes %include "./src/lib/print.asm" %include "./src/lib/screen.asm" -; -------- -; Data -; -------- +;; Variables driveNumber: db 0 startMsg: db "kern.", ENDL, ENDL, 0 ; The bootup message @@ -450,7 +446,8 @@ txtExt: db "txt", 0 inputLen: db 0 userInput: db 0 +;; Constants FILETAB_LOC equ 0x1000 PROGRAM_LOC equ 0x8000 -times 3*512-($-$$) db 0 +times 4*512-($-$$) db 0 diff --git a/src/lib/disk.asm b/src/lib/disk.asm index e568ae2..59864a0 100644 --- a/src/lib/disk.asm +++ b/src/lib/disk.asm @@ -1,5 +1,284 @@ ; ; Loads a file from the filetable into a memory location +; dl - Disk number +; Params: +; Filename (Null terminated) +; Destination location segment +; Destination location offset +; Disk number +; +; Returns: +; al - Exit code : 0 - Success; 1 - File not found; 2 - Disk error ; loadFile: - \ No newline at end of file + ; Func prologue + push bp + mov bp, sp + pusha + + mov byte [driveNum], dl + mov bx, [bp+4] ; dest Offset + mov fs, [bp+6] ; dest Segment + mov si, [bp+8] ; Filename + + mov cx, 10 + mov di, filename +.getFilename: + lodsb ; mov byte al, [si]; inc si + cmp al, 0 ; End of filename + dec si ; Fill the rest with Null + stosb + loop .getFilename + + mov ax, FILETAB_LOC + mov es, ax + xor di, di ; es:di - Filetable location + mov si, filename +.filenameCheck: + mov al, [di] + cmp al, 0 ; End of filetable + je .notFound + + cmp al, [si] + je .beginCmp + + add di, 16 + jmp .filenameCheck +.beginCmp: + push di + mov cl, 10 +.cmpLoop: + mov al, [es:di] + inc di + cmp al, [si] + jne .redoCheck + + dec cl + jz .found + inc si + jmp .cmpLoop +.redoCheck: + mov si, filename + pop di + inc di + jmp .filenameCheck +.notFound: + mov byte [exitCode], NOT_FOUND + jmp .done +.found: + mov al, [es:di] + inc di + cmp al, 0 ; Skip NULLs untill extension + je .found + + add di, 3 ; Skip extension + inc di ; Skip Number of entries + + xor ax, ax ; Reset disk + mov dl, [driveNum] + int 0x13 + + push fs + mov ch, 0 ; Cylinder + mov dh, 0 ; Head + mov cl, [es:di] ; Sector + inc di + mov al, [es:di] ; Length in sectors + mov ah, 2 ; Read from disk + pop es ; es:bx - Destination + int 0x13 + + jnc .done + + mov byte [exitCode], DISK_ERROR +.done: + ; Func epilogue + popa + mov sp, bp + pop bp + ret + +; +; Saves a file to the disk and updates filetable +; dl - Disk number +; Params: +; Filename (Null terminated) +; Filetype (Null terminated) +; Filesize (Byte) +; Source location segment +; Source location offset +; +; Returns: +; al - Exit code : 0 - Success; 1 - File not found; 2 - Disk error +; +saveFile: + ; Func prologue + push bp + mov bp, sp + pusha + + mov byte [driveNum], dl + pop bx ; src Offset + pop fs ; src Segment + pop cx ; Filesize + pop ax ; Filetype + + push bx + + mov byte [filesize], cl + + mov di, filetype + mov cx, 3 +.filetype: + lodsb + cmp al, 0 ; End of filename + dec si ; Fill the rest with Null + stosb + loop .getFilename + + pop si ; Filename + mov cx, 10 + mov di, filename +.getFilename: + lodsb ; mov byte al, [si]; inc si + cmp al, 0 ; End of filename + dec si ; Fill the rest with Null + stosb + loop .getFilename + + mov ax, FILETAB_LOC + mov es, ax + xor di, di ; es:di - Filetable location + mov si, filename +.filenameCheck: + mov byte al, [es:di+14] + mov byte [availableSector], al + mov byte al, [es:di+15] + add byte [availableSector], al + + mov al, [di] + cmp al, 0 ; End of filetable + je createFile ; File doesn't exist, create new one + + cmp al, [si] + je .beginCmp + + add di, 16 + jmp .filenameCheck +.beginCmp: + push di + mov cl, 10 +.cmpLoop: + mov al, [es:di] + inc di + cmp al, [si] + jne .redoCheck + + dec cl + jz .found + inc si + jmp .cmpLoop +.redoCheck: + mov si, filename + pop di + add di, 16 + jmp .filenameCheck +.found: ; File already exists (override) + ; TODO: Ask user if they want to override file + mov al, [es:di] + inc di + cmp al, 0 ; Skip NULLs untill extension + je .found + + add di, 3 ; Skip extension + inc di ; Skip Number of entries + + xor ax, ax ; Reset disk + mov dl, [driveNum] + int 0x13 + + ; TODO: Override current file + ; push fs + ; mov ch, 0 ; Cylinder + ; mov dh, 0 ; Head + ; mov cl, [es:di] ; Sector + ; inc di + ; mov al, [es:di] ; Length in sectors + ; mov ah, 3 ; Write to disk + ; pop es ; es:bx - Destination + ; int 0x13 + + ; jnc returnToCaller + + mov byte [exitCode], DISK_ERROR + jmp return ; TODO: Fix this + +createFile: ; Appends a file to the filetable. + mov cx, 10 + mov si, filename + rep movsb ; Append the filename + mov cx, 3 + mov si, filetype + rep movsb ; Append the extension + mov byte [es:di], 0 ; Number of entries + mov byte al, [availableSector] + stosb ; Starting sector + mov byte al, [filesize] + stosb ; File length in sectors + + ; Write changed filetable to disk + ;! If filetable size changes, this will have to change (probably) + xor bx, bx ; es:bx - Filetable location + mov dl, [driveNum] + mov ah, 3 ; Write to disk + mov al, 1 ; Write 1 sector + mov ch, 0 ; Cylinder + mov dh, 0 ; Head + mov cl, 1 ; Sector + int 0x13 + + jnc writeFileData + + mov byte [exitCode], DISK_ERROR + jmp returnToCaller + +writeFileData: ; Writes file data to the disk + mov ax, fs ; Source segment + mov es, ax + pop bx ; es:bx - Source + mov ah, 3 ; Write to disk + mov byte al, [filesize] + mov dl, [driveNum] + mov ch, 0 ; Cylinder + mov dh, 0 ; Head + mov byte cl, [availableSector] + int 0x13 + + jnc returnToCaller + + mov byte [exitCode], DISK_ERROR ; Exit code - Disk Error + +returnToCaller: + ; Func epilogue + popa + mov sp, bp + pop bp + mov byte al, [exitCode] + ret + +;; Variables +availableSector: db 0 +exitCode: db 0 + +driveNum: db 0 +filesize: db 0 +filetype: times 3 db 0 +filename: times 10 db 0 + +;; Constants +FILETAB_LOC equ 0x1000 + +; Exit codes +SUCCESS equ 0 +NOT_FOUND equ 1 +DISK_ERROR equ 2