Page 1 of 1

Int 13h - Reading track 1?

Posted: Sat Jul 19, 2008 12:21 pm
by Alboin
Hi,

My LBA->CHS function seems to be broken. It works when I've values that stay on track 0, but anything above fails.

It's the regular generic LBA->CHS:

Code: Select all

lba_to_chs:
	push bx
	
	xor dx, dx
	div word [SPT]
	
	mov cx, dx
	inc cx
	
	xor dx, dx
	div word [heads]
	
	mov ch, al
	mov dh, dl
	
	pop bx
	ret
For example, when I give it, 20, it puts 0x0103 in cx, 0 in dx, and ax is appropriate. cx means track 1, sector 3, no?

However, when I check the bochs memory dump, it spits out zeros.
Does anything seem wrong with it? I've checked it with values < 18, and it does read them.

Hopefully this is coherent, I was up til 3 yesterday trying to fix this problem with no luck.

Thanks,
Alboin

Re: Int 13h - Reading track 1?

Posted: Sat Jul 19, 2008 1:34 pm
by thooot
It looks okay to me. Are you reading multiple sectors at once or one at a time? If you're reading more than one you should make sure you don't switch heads/cylinders and that you don't cross 64KB boundaries. And you're sure that the sectors you're trying to read aren't filled with zeros?

Re: Int 13h - Reading track 1?

Posted: Sat Jul 19, 2008 1:41 pm
by Alboin
I'm reading one at a time for simplicity's sake.

Well, I can read the beginning of the elf kernel on sector 13, but I can't seem to find the end which should be after that by a few sectors.

This is my read function:

Code: Select all

;
; Read Sector
; ax: logical sector to read
; es:bx: address to load to
;
read_sector:
	call lba_to_chs
	
	;recalibrate
	xor ah, ah
	int 13h
	
	@@:
		;int 13h
		mov ah, 2
		mov al, 1
		mov dl, 0
		int 13h
		
		jc @b
		ret 

Re: Int 13h - Reading track 1?

Posted: Sat Jul 19, 2008 3:19 pm
by thooot
I just tried the following bootloader in bochs and it loaded more than 18 sectors just fine (this is NASM btw):

Code: Select all

[ORG 0]
[BITS 16]

    jmp 0x07C0:start                ; Goto segment 07C0

start:
    mov ax, 0x2000
    mov es, ax
    mov bx, 0
    
    mov cx, 50                      ; Sectors to read
    mov ax, 0                       ; Current sector
read_loop:
    pusha
    call read_sector
    popa
    add bx, 512
    inc ax
    dec cx
    jnz read_loop
    
    jmp $



;
; Read Sector
; ax: logical sector to read
; es:bx: address to load to
;
read_sector:
    call lba_to_chs
   
    ;recalibrate
    xor ah, ah
    int 13h
   
    mov ah, 2
    mov al, 1
    mov dl, 0
    int 13h
    
    ret 
      
      
lba_to_chs:
    push bx

    xor dx, dx
    mov bx, 18
    div bx

    mov cx, dx
    inc cx

    xor dx, dx
    mov bx, 2
    div bx

    mov ch, al
    mov dh, dl

    pop bx
    ret
   

; fill the remainder of the file with zeros
times 510-($-$$) db 0

; Magic number that lets BIOS know this is a boot disk
dw 0AA55h

Re: Int 13h - Reading track 1?

Posted: Sat Jul 19, 2008 3:57 pm
by simkinggold
[deleted]

Re: Int 13h - Reading track 1?

Posted: Sat Jul 19, 2008 4:02 pm
by cr2
<post deleted>

Re: Int 13h - Reading track 1?

Posted: Sat Jul 19, 2008 6:03 pm
by Adek336
simkinggold wrote:Sorry if i sound noobish but what file do you save that as?
If you mean that asm code, you have to first assemble it to a 512 bytes binary file, and then write it to sector 0 of a floppy disk (using "dd" or something). To sector 0 as opposed to a file. The code snippet, as pointed out, does not have a BPB so it would destroy the files on the floppy. Also, it seems that unless you have further sectors on the floppy filled with an appropriate program, you would really have any use of this code.

Re: Int 13h - Reading track 1?

Posted: Sat Jul 19, 2008 6:42 pm
by thooot
This isn't meant to be a real boot loader. Just to prove that the disk IO routines Albion is using are working. I have a custom file system that doesn't store any information in the boot loader; its in the first sector after the loader. Plus I have a program that will take a list of files and create an image using that custom file system. So I used those tools to create a 30KB disk image and ran it in bochs then verified the memory contained the proper bits from the disk image.

Re: Int 13h - Reading track 1?

Posted: Sat Jul 19, 2008 7:18 pm
by Alboin
Cool, now I'm back to where I was a day ago! I'm gaining ground. (Thanks, by the way.)

As it would turn out the problem seemed to be my detection of [SPT] and [heads]. I've since hard coded them in as 18 and 2.

It works now, however, with my actual kernel, which is ~60kb (Mostly debug info), my loading function dies when its counter hits 0xfe00, due to the 16-bit segmentation flutter. (I hate real mode.) I just have to fix this and I should be set.

EDIT: Fixed. We're all set here. I can successfully read and parse my 51.9 kb elf kernel. :)

Thanks again,
Alboin

Re: Int 13h - Reading track 1?

Posted: Sat Jul 19, 2008 8:26 pm
by bewing
Huh. I'm surprised it works, because it sure looks to me like you are trashing dl. (which should error out on your "recalibrate" step.)
And why are you pushing/popping bx? You never use it.

Re: Int 13h - Reading track 1?

Posted: Sat Jul 19, 2008 9:21 pm
by Alboin
Huh. I'm surprised it works, because it sure looks to me like you are trashing dl. (which should error out on your "recalibrate" step.)
Very true. Fixed and thanks.
And why are you pushing/popping bx? You never use it.
It's the only register pushed in lba_to_chs, because one can expect every other register to be changed, while bx plays no output role.