From: Duje Mihanović Date: Sat, 21 Aug 2021 16:24:44 +0000 (+0200) Subject: Read bytes from boot drive X-Git-Tag: 0.1.1~13^2 X-Git-Url: http://git.dujemihanovic.xyz/%22http:/www.sics.se/static/%7B%7B%20%24.Site.BaseURL%20%7D%7Dposts/%7B%7B%20.RelPermalink%20%7D%7D?a=commitdiff_plain;h=d826097f3b032247dd0396f8cf64b2fa775cea68;p=nameless-os.git Read bytes from boot drive --- diff --git a/Makefile b/Makefile index 9d95b1e..a85a94a 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ AS = yasm # choose yasm or nasm here ASFLAGS = -f bin # compile a raw binary -boot: boot.s print.s +boot: boot.s print.s disk.s $(AS) $(ASFLAGS) -o $@ boot.s clean: diff --git a/boot.s b/boot.s index 51be5d9..3a8adc7 100644 --- a/boot.s +++ b/boot.s @@ -2,70 +2,32 @@ org 7c00h ; boot sector is loaded at 0x7c00 _start - mov di, printing_numbers - call print + mov [boot_drive], dl ; BIOS sets dl to number of boot drive, we don't want to forget this - or bl, 1 - mov dx, 0DEADh ; number to print - call print_word - - mov di, space - call print - - xor bl, bl - mov dx, 0BEEFh - call print_word - - mov di, waiting - call print + mov bp, 8000h ; Move the stack away + mov sp, bp - mov di, keystroke - mov bl, 1 + push cs + pop es + mov bx, 9000h ; Load at 0000:9000 + mov dh, 2 ; Load 5 sectors + mov dl, [boot_drive] ; Load from boot drive + call read_sectors -.loop - mov ah, 0 - int 16h - cmp ah, 1Ch - je .enterpressed - jmp .continue - cmp ah, 0Eh - je .backspace - jmp .continue -.enterpressed - pusha - mov ah, 0Eh - mov al, 0Dh - int 10h - mov al, 0Ah - int 10h - popa - jmp .loop -.backspace - pusha - mov ah, 03h - mov bh, 0 - int 10h - dec dl - mov ah, 02h - int 10h - mov ah, 0Eh - mov al, ' ' - int 10h - mov ah, 02h - int 10h - popa - jmp .loop -.continue - mov [keystroke], al - call print - jmp .loop + mov dx, [9000h] ; Print value at 9000h, should be 0xDEAD + call print_word + mov dx, [9000h + 512] ; Print value at 9200h, should be 0xBEEF + call print_word -printing_numbers db "Printing some random hex numbers", 0 -waiting db "I await keystrokes", 0 -keystroke db "$", 0 -space db " ", 0 + jmp $ ; Endless loop %include "print.s" +%include "disk.s" + +boot_drive resb 1 ; Reserve 1 byte for boot drive number + +times 510-($-$$) db 0 ; fill with 0 until 0x1fc is reached +dw 0AA55h ; BIOS MBR signature - times 510-($-$$) db 0 ; fill with 0 until 0x1fc is reached - dw 0AA55h ; BIOS MBR signature +times 256 dw 0DEADh ; Fill in 2nd sector with 0xDEAD +times 256 dw 0BEEFh ; Fill in 3rd sector with 0xBEEF diff --git a/disk.s b/disk.s new file mode 100644 index 0000000..b281c3a --- /dev/null +++ b/disk.s @@ -0,0 +1,39 @@ +; ES:BX - where to load +; DH - sector count +; DL - drive number + +read_sectors: + xor bl, bl + mov di, disk_reading + call print + push dx + mov ah, 02h + mov al, dh + mov ch, 0 + mov dh, 0 + mov cl, 2 + + int 13h + + jc read_error + + pop dx + cmp dh, al + jne read_error + ret + +read_error: + xor bl, bl + mov di, disk_error + call print + mov dl, ah + xor dh, dh + call print_word + mov dl, al + call print_word + jmp $ + +disk_reading db "Reading from disk...", 0 +disk_error db "Failed to read from disk! Take a look at this error code:", 0 + + ret