So, I've been working on a hobby project. Creating my own Operating System. I started a while back but dropped it until maybe a couple nights ago. I just fixed an oversight that caused nothing to be read from the sectors I want to read from. With that error out of the way, a new one has came about and I honestly don't even know where to begin debugging this one.
I am coding a Master Boot Record and debugging it with GDB and QEMU, here is the code to my master boot record (It was assembled using YASM)
Sorry if my code is not very good. I am not an expert at assembly language...
; yasm boot.asm -fbin
bits 16
%define part(n,l) section n vstart=l align=1
%define rpart(n,l) section n start=l align=1
; ----------------------- ;
part(entry, 0x7c00) ;
; --ENTRY---------------- ;
_start:
mov [boot_drive+0x7c00], dl
xor ax, ax
mov ss, ax
mov ds, ax
mov es, ax
mov sp, _start
mov bp, _start
mov cx, 512
mov si, _start
mov di, _strap
rep movsb
jmp 0:_strap+(b_boot_strapper-$$)
b_boot_strapper:
; ----------------------- ;
part(strap, 0x0600) ;
; --BOOT STRAPPER-------- ;
_strap:
xor cx, cx
.find_active_part:
cmp cl, 4
jge .no_active_part
xor ax, ax
mov ah, cl
mov bl, 16
mul bl
mov bx, ax
inc cl
mov al, (1 << 7)
mov ah, [partition_1+0x600+bx]
and ah, al
jnz .load_active_part
jmp .find_active_part
.load_active_part:
xor ax, ax
mov ds, ax
mov ah, 42h
mov dl, [boot_drive+0x600]
mov si, dap+0x600
push bx
mov bx, dap+0x600
mov es, bx
pop bx
mov cx, [partition_1+0x600+bx+8]
mov [dap_startlba+0x600], cx
mov cx, [partition_1+0x600+bx+12]
mov [dap_sectors+0x600], cx
int 13h
jc .disk_error
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, _start
mov bp, _start
mov dl, [boot_drive+0x600]
jmp 0:0x7c00
.no_active_part:
mov si, msg_no_part
call print
jmp halt
.disk_error:
mov si, msg_er_read
call print
jmp halt
print:
mov dx, ax
mov ah, 0Eh
xor bh, bh
mov bl, 0Fh
.rep:
lodsb
or al, al
jz .done
int 10h
jmp .rep
.done:
ret
halt:
cli
hlt
jmp halt
msg_er_read db 'Disk Read Error....', 0
msg_no_part db 'No Active Partition....', 0
; ----------------------- ;
rpart(variables, 300) ;
; --VARIABLES------------ ;
boot_drive db 0
dap: ; Disk Address Packet
db 16, 0
dap_sectors dw 0
dap_offset dw 0x7c00
dap_segment dw 0
dap_startlba dq 0
dap_end:
; ----------------------- ;
rpart(partitions, 446) ;
; --VARIABLES------------ ;
partition_1: ; This file has the following 16 bytes:
; 0x80, 0x01, 0x00, 0x05, 0x17, 0x01, x03, 0x01, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00
%include "part_n1.asm"
partition_2: ; The rest of these files are just 16 null bytes.
%include "part_n2.asm"
partition_3:
%include "part_n3.asm"
partition_4:
%include "part_n4.asm"
; ------------------------------- ;
rpart(signature, 510) ;
db 0x55, 0xAA ;
; ------------------------------- ;
This code works! However, I don't know if this is an issue with QEMU or not but when it reads from the sectors it has a bit of corruption or dataloss...
These are the bytes that were expected to be at 0x7c00
EB 1B B4 0E 30 FF B3 0F
AC 74 04 CD 10 EB F9 C3
48 65 6C 6C 6F 20 57 6F
72 6C 64 21 00 BE 10 7C
E8 DF FF F4
(It's a basic function that prints "Hello World!")
This is what ended up actually being in memory at that location:
EB 1B B4 0E 30 FF B3 0F
AC 74 04 CD 10 EB F9 C3
48 65 6C 6C 6F 20 57 6F
72 6C 64 21 00 BE 10 7C
F0 DF FF F4
If you look closely the 4th byte from the last was changed from E8 to F0, I have no idea why this happened. And in the last run the "E" in "Hello World" was also changed but it wasn't in this debug run.
I need help with even where to begin debugging this...
Edit 1
I realized that my function to print hello world had a few issues, weather or not they were related to this odd thing, I don't really know. In the repeating part of the print function (The one in the code I was loading, not in the mbr code above) I forgot to add or al, al
after I did lodsb
and before I did jz .done
which might have been interfering with things, I am not completely sure, but after I updated that code and run a few more debug sessions it seems this issue doesn't occur anymore...