From 16dd9da1bdb4fe7e4348967d0ef3953e532f94be Mon Sep 17 00:00:00 2001 From: Dario48 Date: Tue, 15 Jul 2025 21:04:46 +0200 Subject: [PATCH] reading from disk --- src/initramfs.asm | 161 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 3 deletions(-) diff --git a/src/initramfs.asm b/src/initramfs.asm index 6da1603..b2e02b3 100644 --- a/src/initramfs.asm +++ b/src/initramfs.asm @@ -2,8 +2,40 @@ org 0x7C00 bits 16 + %define ENDL 0x0D, 0x0A +; +; FAT12 Header +; +jmp short start +nop + +bdb_oem: db "mkfs.fat" +bdb_bytes_per_sector: dw 512 +bdb_sectors_per_cluster: db 1 +bdb_reserved_sectors: dw 1 +bdb_fat_count: db 2 +bdb_dir_entries_count: dw 0E0h +bdb_total_sectors: dw 2880 +bdb_media_descriptor_type: db 0F0h +bdb_sectors_per_fat: dw 9 +bdb_sectors_per_track: dw 18 +bdb_heads: dw 2 +bdb_hidden_sectors: dd 0 +bdb_large_sector_count: dd 0 + +; Extended boot record +ebr_drive_number: db 0 + db 0 +ebr_signature: db 29h +ebr_volume_id: db 68h, 6Fh, 6Dh, 6Fh +ebr_volume_laber: db 'zos disk ' +ebr_system_id: db 'FAT12 ' + + + + start: jmp main @@ -47,16 +79,139 @@ main: mov ss, ax mov sp, 0x7C00 ; stack grows downward from where we are loaded in memory - ; print the hello world - mov si, msg_hello + mov si, msg_startup call echo + mov si, msg_reading_from_disk + call echo + mov [ebr_drive_number], dl + + mov ax, 1 ; second sector from disk + mov cl, 1 ; 1 sector to read + mov bx, 0x7E00 ; data should be after the bootloader + call disk_read + + cli + hlt + +; errors + +floppy_error: + mov si, msg_read_failed + call echo + jmp .wait_and_reboot + +.wait_and_reboot: + mov ah, 0 + int 16h + + jmp 0FFFFh:0 ; jump to start of bios, should reboot + hlt .halt: + cli ; disable interrupts, this way we shouldn't be able to get out of halt + hlt jmp .halt -msg_hello: db 'Hello, world!', ENDL, 0 + +; +; args: +; ax = lba adress +; return: +; cx[0..5] = sector number +; cx[6-15] = cilinder number +; dh: head +; +lba_to_chs: + push ax + push dx + + xor dx, dx ; clear dw + div word [bdb_sectors_per_track] ; ax = LBA / SectorsPerTrack + ; dx = LBA % SectorsPerTrack + + inc dx + mov cx, dx ; cx = sector + + xor dx, dx ; clear dx + div word [bdb_heads] ; ax = (LBA / SectorsPerTrack) / Heads = cylinder + ; dx = (LBA / SectorsPerTrack) % Heads = head + mov dh, dl + mov ch, al + shl ah, 6 + or cl, ah ; put upper 2 bits of cylinder in CL + + pop ax + mov dl, al + pop ax + + ret + + +; +; args: +; ax = LBA adress +; cl = number of sectors to read +; dl = drive number +; es:bx = pointer to where to store the data +; + +disk_read: + push ax + push bx + push cx + push dx + push di + + push cx ; cx will be overridden + call lba_to_chs + pop ax ; al = number of sectors to read + mov ah, 02h + mov di, 3 ; hoe many times to try + +.retry: + pusha + stc ; set carry flag + int 13h ; if no carry flag => it work + + jnc .done + + ; read fail + popa + call disk_reset + dec di + + test di, di + jnz .retry + +.fail: + jmp floppy_error + +.done: + popa + + pop di + pop dx + pop cx + pop bx + pop ax + ret + +; arg: dl = drive number + +disk_reset: + pusha + mov ah, 0 + stc ; set carry + int 13h + jc floppy_error + popa + ret + +msg_startup: db 'starting zos, please hold while we check for any problems', ENDL, 0 +msg_reading_from_disk: db 'testing reading from disk', ENDL, 0 +msg_read_failed: db 'error when trying to read from floppy', ENDL, 0 times 510-($-$$) db 0 dw 0AA55h