I'm trying to figure out a problem I'm having with a two-stage bootloader designated to be used on a FAT16 disk.
Bochs throws this error constantly: int13_harddisk: function 42. LBA out of range
So my initial guess is that something is going up either with the DAP setup or with the way I'm setting up the stack (I've read somewhere it needs to be aligned for this to work) or if segments are going crazy (I really hate Real Mode, but everyday I try doing my best to embrace it hahah) or if my BPB is wrong so I'll try to give as much info as I can.
I'm using i386-elf-as for compiling (AT%T Syntax)
For the initial code setup (Including the BPB) I do it this way:
Code: Select all
.code16
.global boot0
.global init0_fat16
.section .text
init0_fat16:
# TODO/HACK: Do we really need: .intel_syntax noprefix and .att_syntax prefix in jmp short boot0 ? (EDIT for osdev forum, I've inspected both outcomes jmp short & normal jmp on a dissasembler and they both seem to produce 0xEB instead of 0xEE; so I'm debating on what to leave this like)
.intel_syntax noprefix
jmp short boot0
.att_syntax prefix
nop
bpb:
oem_string: .ascii "INITIUM1"
sector_size: .word 0x200
sectors_per_cluster: .byte 4
reserved_sectors: .word 4
number_of_fats: .byte 2
total_fat_directory_entries: .word 512
total_sectors: .word 20160
media_descriptor_type: .byte 0xF8
sectors_per_fat: .word 20
spt: .word 63
heads_count: .word 16
hidden_sector_count: .int 0
high_sector_count: .int 0
bios_boot_drive: .byte 0x80
reserved: .byte 0
extended_bpb_signature: .byte 0x29
volumeid_name: .int 0xd7450e5
volume_label: .ascii "BOOTLOAD "
fs_type: .ascii "FAT16 "
boot0:
xor %ax, %ax
mov %ax, %ds
mov %ax, %es
mov $0x0900, %bx
cli
mov %bx, %ss
mov %ax, %sp
mov %sp, %bp
sti
cld
mov %dl, bios_boot_drive
xor %dx, %dx
xor %ax, %ax
mov bios_boot_drive, %dl
mov number_of_fats, %ax
mulw sectors_per_fat
add reserved_sectors, %ax
mov %ax, root_dir_offset
xchg %bx, %ax
mov sector_size, %ax
mov $0x20, %cx
div %cx
xchg %cx, %ax
mov total_fat_directory_entries, %ax
div %cx
add %bx, %ax
mov %ax, data_cluster_offset
xor %dx, %dx
.loop:
xchg %bx, %bx
mov root_dir_offset, %ax
add %dx, %ax
mov $0x200, %bx
mov $0x01, %cx
push %dx
call read_sectors
mov $0x200, %bx
mov %bx, %ax
add sector_size, %ax
.loop_dir_entries:
mov stage2_name, %di
mov $0x06, %cx
mov %bx, %si
rep cmpsb
je .match
add $0x20, %bx
cmp %bx, %ax
jne .loop_dir_entries
pop %dx
inc %dx
cmp $0x80, %dx
jne .loop
jmp error
.match:
movw 0x1A(%bx),%ax
sub $0x02, %ax
mulb sectors_per_cluster
add data_cluster_offset, %ax
mov $1, %cx # STAGE 2 Size, hardcoded for now
mov $0x0400, %bx
call read_sectors
jmp $0x0, $0x1000
error:
cli
hlt
read_sectors:
pusha
mov %eax, dap_sector_low
mov %es, dap_segment
mov %bx, dap_offset
.extended_read:
mov $0x42, %ah
mov bios_boot_drive, %dl
mov dap, %si
int $0x13
jnc .read_ok
mov $0x0e, %ah
mov $0x41, %al
int $0x10
xor %ax, %ax
int $0x13
jmp .extended_read
.read_ok:
popa
inc %eax
add $0x200, %bx
jnc .no_carry
mov %es, %dx
add $0x10, %dh
mov %dx, %es
.no_carry:
dec %cx
jz read_sectors_exit
jmp read_sectors
read_sectors_exit:
ret
root_dir_offset: .word 0x0000
data_cluster_offset: .word 0x0000
stage2_name: .ascii "STAGE2"
stage2_cur_offset: .word 0x0000
stage2_cur_segment: .word 0x0000
.align 4
dap:
dap_size: .byte 0x10
dap_reserved: .byte 0x00
dap_block_count: .word 0x01
dap_offset: .word 0x1000
dap_segment: .word 0x00
dap_sector_low: .int 0x01
dap_sector_high: .int 0x00
.fill 510-(.-init0_fat16), 1, 0
.word 0xAA55
Code: Select all
ENTRY(init0_fat16)
OUTPUT_FORMAT(elf32-i386)
OUTPUT_ARCH(i386)
SECTIONS {
. = 0x7C00;
.text :
{
*(.text)
}
.rodata :
{
*(.rodata)
}
.data :
{
*(.data)
}
.bss :
{
*(.bss)
}
}
bximage -mode=create -hd=10M -q hdd.img
mkfs.fat -F 16 hdd.img
mkdir disk/
dd conv=notrunc if=bootloader/boot0.bin of=hdd.img bs=512 seek=0 status=none
sudo mount -t msdos -o loop hdd.img disk/
sudo cp bootloader/boot1.bin disk/STAGE2
sudo umount disk/
rm -r disk/
I really can't figure exactly what's wrong, but I've double-checked the BPB info using Ultimate64 and a few bits more that I don't remember as of writing but still, same output.
Any directions you guys recommend?
Thanks for your time!
Hope you have a nice day!