I'm unable to initialize IDE controller on bochs

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
zielonykid1234
Posts: 1
Joined: Sun Aug 20, 2023 2:05 am

I'm unable to initialize IDE controller on bochs

Post by zielonykid1234 »

My code:

Code: Select all

%macro outb 2
	mov dx, %1
	mov al, %2
	out dx, al
%endmacro

%macro inb 2
	mov dx, %1
	in al, dx
	mov %2, al
%endmacro

[bits 32]

section .text

extern bootloader
extern disk_error
extern kernel_not_loaded_error

global load_kernel_test
load_kernel_test:
	mov byte [0x2000], 0xEB
	mov byte [0x2001], 0xFE
	mov byte [0x2002], 0x00
	mov byte [0x2003], 0x00
	ret

global load_kernel
load_kernel:
	outb 0x1F6, 0xA0

	outb 0x1F2, 1
	outb 0x1F3, 0x00
	outb 0x1F4, 0x49
	outb 0x1F5, 0x00

	outb 0x1F7, 0x20

	inb 0x1F7, al
	test al, 0x40
	jz .take_a_nap
	
	mov edi, kernel_start
.read_loop:
	inb 0x1F0, al
	stosb
	inb 0x1F7, al
	test al, 0x40
	jz .read_loop

	inb 0x1F7, al
	test al, 0x01
	jnz .error_occured
.take_a_nap:
	sub edi, kernel_start
	ret
.error_occured:
	call disk_error
	ret

global call_kernel
call_kernel:
	jmp kernel_start
	ret

global check_kernel
check_kernel:
	cmp byte [kernel_start], 0xEB
	jne kernel_not_loaded_error
	jmp call_kernel

section .rodata:
kernel_start equ 0x2000
I'm reading 50th sector from the disk 0 on the IDE controller. disk_error doesn't run on real hardware but kernel_not_loaded_error does. On bochs both error labels are being executed. load_kernel_test is for loading my kernel from the code and it works so I assume that it's the IDE loading problem. My bochs configuration:

Code: Select all

cpu: count=2, reset_on_triple_fault=0

display_library: x, options="gui_debug"

megs: 512

clock: sync=realtime, time0=local

ata0-master: type=disk, path="build/floppy.img", mode=flat
boot: c

log: ./bochsout.txt

mouse: enabled=0

magic_break: enabled=1
floppy.img is fat12 file system that contains bootsector, stage2.bin and kernel.bin. kernel.bin is on offset 0x6400 on the file system (sector size is 512B).
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: I'm unable to initialize IDE controller on bochs

Post by Octocontrabass »

zielonykid1234 wrote:

Code: Select all

	outb 0x1F6, 0xA0

	outb 0x1F2, 1
	outb 0x1F3, 0x00
	outb 0x1F4, 0x49
	outb 0x1F5, 0x00
You selected CHS, but 73/0/0 isn't a valid CHS address. How did you come up with these values? Were you trying to use LBA?
zielonykid1234 wrote:

Code: Select all

	inb 0x1F0, al
Using byte accesses on this port is (usually) undefined behavior. You must use word accesses here.
Post Reply