Skip to content

Commit

Permalink
Implemented file system 3/3
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag-bit committed Oct 21, 2023
1 parent 351d695 commit 30d0056
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 4 deletions.
Binary file modified anurag-os-dev/build/bootloader.bin
Binary file not shown.
Binary file modified anurag-os-dev/build/main_floppy.img
Binary file not shown.
156 changes: 152 additions & 4 deletions anurag-os-dev/src/bootloader/boot.asm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bdb_total_sectors: dw 2880 ; 2880*512 = 1.4
bdb_media_descriptor_type: db 0F0h ; f0 =m 3.5" floppy disk
bdb_sectors_per_fat: dw 9 ; 9 sectos/fat
bdb_sectors_per_track: dw 18
bdb_heads: dw 2
bdb_heads: dw 2
bdb_hidden_sectors: dd 0
bdb_large_sector_count: dd 0

Expand All @@ -33,6 +33,10 @@ ebr_volume_label: db 'ANURAG OS' ; 11 bytes, padd
ebr_system_id: db 'FAT12 ' ; 8 bytes



;
;code here
;
start:
jmp main

Expand Down Expand Up @@ -77,17 +81,161 @@ main:
mov ss, ax
mov sp, 0x7C00 ; stack grows downwards from where loaded into memory

; print message
; read something from floppy disk
; BIOS should set DL to drive number
mov [ebr_drive_number], dl

mov ax, 1 ; LBA-1, second sector from disk
mov cl, 1 ; 1 sector to read
mov bx, 0x7E00 ; data should be after the bootloader
call disk_read





; print message
mov si, msg_hello
call puts

cli
hlt

floppy_error:
mov si, msg_read_failed
call puts
jmp wait_key_and_reboot


wait_key_and_reboot:
mov ah, 0
int 16h ; wait for keypress
jmp 0FFFFh:0 ; jump to beginning of BIOS, should reboot

.halt:
jmp .halt
cli ; disable interrupts, this way we can't get out of of "halt" state
hlt



;
;Disk routines
;
; Converts an LBA address to a CHS address
; Parameters:
; - ax: LBA address
; Returns:
; - cx [bits 0-5]: sector nu mber
; - cx [bits 6-15]: cylinder
; dh: head




lba_to_chs:

push ax
push dx

xor dx, dx ; dx = 0
div word [bdb_sectors_per_track] ; ax = LBA / Sectors PerTrack
; dx = LBA % SectorsPerTrack

inc dx ; dx = (LBA % SectorsPerTrack + 1) = sector
mov cx, dx ; cx = sector

xor dx, dx ; dx = 0
div word [bdb_heads] ; ax = (LBA / SectorsPerTrack) / Heads = cylinder
; dx = (LBA / SectorsPerTrack) % Heads = head

mov dh, dl ; dh = head
mov ch, al ; ch = cylinder (lower 8 bits)
shl ah, 6
or cl, ah ; put upper 2 bits of cylinder in CL

pop ax
mov dl, al ; restore DL
pop ax
ret



;
;
;Reads sectors from a disk
;Parameters:
; - ax: LBA address
; - cl: number of sectors to read(upto 128)
; - dl: drive number
; - es:bs: memory address where to store read data.
;

disk_read:

push ax ; save registers will modify
push bx
push cx
push dx
push di



push cx ; temporarily save CL (number of sectors to read)
call lba_to_chs ; compute CHS
pop ax ; AL = number of sectors to read

mov ah, 02h
mov di, 3 ; retry count


msg_hello: db 'Hello world!', ENDL, 0
.retry:
pusha ; save all registers, we don't know what bios modifies
stc ; set carry flag, some BIOS'es don't set it
int 13h ; carry flag cleared = succes
jnc .done ; jump if carry not set

;read failed
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 ; restore registers modified
ret

;
;Resets disk controller
;Parameters:
; dl: drive number
;
;
disk_reset:
pusha
mov ah, 0
stc
int 13h
jc floppy_error
popa
ret

msg_hello: db 'Hello world!', ENDL, 0
msg_read_failed: db 'Read from disk failed', ENDL, 0


times 510-($-$$) db 0
Expand Down
6 changes: 6 additions & 0 deletions bochs_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
megs: 128
romimage: file=C:\Program Files\Bochs-2.7\BIOS-bochs-latest, address=0xfffe0000
vgaromimage: file=C:\Program Files\Bochs-2.7\VGABIOS-lgpl-latest
floppya: 1_44=\\wsl.localhost\Debian\home\anurag\os-project\anurag-os-dev\build\main_floppy.img, status=inserted
boot: floppy

0 comments on commit 30d0056

Please sign in to comment.