[need help] ata-pio bootloader

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
User avatar
williderwurm
Posts: 13
Joined: Wed Mar 18, 2015 5:28 am
Location: Hyperspace

[need help] ata-pio bootloader

Post by williderwurm »

Good day!

I am trying to load code from a harddrive using ata-pio. As an emulator i use qemu x86.
The code is 16bit.
But it does not display the 'R' as I would have expected...

Code: Select all

[BITS 16]


;________________________________ load_ata:
load_ata:

MOV     DX, 0x1F6
MOV     AL, 0x0A0       ;Drive 0, head 0
OUT     DX, AL

MOV     DX, 0x1F2
MOV     AL, 0x1         ;read 1 sector
OUT     DX, AL

MOV     DX, 0x1F3       ;sector 1
MOV     AL, 0x2
OUT     DX, AL

MOV     DX, 0x1F4
MOV     AL, 0x0         ;cylinder 0
OUT     DX, AL

MOV     DX, 0x1F5
MOV     AL, 0x0         ;rest of cylinder 0
OUT     DX, AL

MOV     DX, 0x1F7
MOV     AL, 0x20        ;command read with retry
OUT     DX, AL

.load:

IN      AL, DX
TEST    AL, 0x8         ;wait for it being ready
JZ      load_ata.load

MOV    CX, 512/2               ;data comes as 16bit
MOV    DI, 0x1000		;buffer
MOV     DX, 0x1F0
REP     INSW


;__________________________________ end_ata.

JMP	0x1000
CLI
HLT

times	510-($-$$) db 0x0
dw	0xaa55


codelabel:

MOV	AH, 0x0e
MOV	AL, 'R'
INT	0x10


CLI
HLT


times 2048	db 0
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: [need help] ata-pio bootloader

Post by iansjack »

Where exactly on the hard disk have you placed this code, and how did you do so?
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: [need help] ata-pio bootloader

Post by Kevin »

Stupid question: If you're in a bootloader, and even more if it's running in Real Mode anyway, why don't you use the BIOS functions?
Developer of tyndur - community OS of Lowlevel (German)
User avatar
williderwurm
Posts: 13
Joined: Wed Mar 18, 2015 5:28 am
Location: Hyperspace

Re: [need help] ata-pio bootloader

Post by williderwurm »

Kevin wrote:Stupid question: If you're in a bootloader, and even more if it's running in Real Mode anyway, why don't you use the BIOS functions?
This does not answer my question and i am familiar with the bios functions. I asked for a solution to get the code on the top working. I could of course also use it in protected mode. And no question is stupid, its the answer, Kevin...
User avatar
williderwurm
Posts: 13
Joined: Wed Mar 18, 2015 5:28 am
Location: Hyperspace

Re: [need help] ata-pio bootloader

Post by williderwurm »

iansjack wrote:Where exactly on the hard disk have you placed this code, and how did you do so?
As I have said above, I use the qemu-x86 emulator. I compile the code you can see at the top and qemu treats it as a harddrive.
Compilation: nasm test.asm -f bin -o test.bin
Emulation: qemu-system-x86_64 test.bin or qemu-system-x86_64 -hda test.bin -> -hda means use test.bin as primary hard drive
This means the code (if it would be on a real harddrive) would be in the 1st sector and load the code to print the 'R' from the second sector.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: [need help] ata-pio bootloader

Post by jnc100 »

Does the code run past the 'wait for data ready' bit? You can insert debugging instructions to be sure. Also, you do not set ES anywhere, and the insw instruction implies data is read to ES:DI.

Regards,
John.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: [need help] ata-pio bootloader

Post by Kevin »

williderwurm wrote:This does not answer my question and i am familiar with the bios functions. I asked for a solution to get the code on the top working.
I know that you didn't directly ask for it, but sometimes it's useful to check if you're doing the right thing before you bother with doing the thing right. Writing a full-blown IDE driver in assembly is something I would have a hard time calling the right thing.

Anyway, directly related to the code you posted: Your comments suggest that you expect to be using CHS addressing here, but you're setting the LBA flag. Probably not the reason for your hang, though.
Developer of tyndur - community OS of Lowlevel (German)
User avatar
williderwurm
Posts: 13
Joined: Wed Mar 18, 2015 5:28 am
Location: Hyperspace

Re: [need help] ata-pio bootloader

Post by williderwurm »

jnc100 wrote:Does the code run past the 'wait for data ready' bit? You can insert debugging instructions to be sure. Also, you do not set ES anywhere, and the insw instruction implies data is read to ES:DI.

Regards,
John.
Thank you for your solution. I indeed forgot to set the ES register. It may work now, but it displays 3 'R's instead of only 1.
User avatar
williderwurm
Posts: 13
Joined: Wed Mar 18, 2015 5:28 am
Location: Hyperspace

Re: [need help] ata-pio bootloader

Post by williderwurm »

Kevin wrote:
williderwurm wrote:This does not answer my question and i am familiar with the bios functions. I asked for a solution to get the code on the top working.
I know that you didn't directly ask for it, but sometimes it's useful to check if you're doing the right thing before you bother with doing the thing right. Writing a full-blown IDE driver in assembly is something I would have a hard time calling the right thing.

Anyway, directly related to the code you posted: Your comments suggest that you expect to be using CHS addressing here, but you're setting the LBA flag. Probably not the reason for your hang, though.
Danke für deine Antwort. At which part of the code am I setting the LBA flag?
User avatar
williderwurm
Posts: 13
Joined: Wed Mar 18, 2015 5:28 am
Location: Hyperspace

Re: [need help] ata-pio bootloader

Post by williderwurm »

Hmmm strange... After changed the code to print another letter after the 'R' it does only print the 3 Rs. Maybe this is caused by using the wrong addressing mode?
It currently looks like this:

Code: Select all

[BITS 16]


;________________________________ load_ata:
load_ata:

MOV     DX, 0x1F6
MOV     AL, 0x0A0       ;Drive 0, head 0
OUT     DX, AL

MOV     DX, 0x1F2
MOV     AL, 0x1         ;read 1 sector
OUT     DX, AL

MOV     DX, 0x1F3       ;sector 1
MOV     AL, 0x2
OUT     DX, AL

MOV     DX, 0x1F4
MOV     AL, 0x0         ;cylinder 0
OUT     DX, AL

MOV     DX, 0x1F5
MOV     AL, 0x0         ;rest of cylinder 0
OUT     DX, AL

MOV     DX, 0x1F7
MOV     AL, 0x20        ;command read with retry
OUT     DX, AL

.load:

IN      AL, DX
TEST    AL, 0x8         ;wait for it being ready
JZ      load_ata.load

MOV    CX, 512/2               ;data comes as 16bit
MOV	BX, 0x1000
MOV	ES, BX
MOV	BX, 0x0
;MOV    DI, 0x1000		;buffer
MOV     DX, 0x1F0
REP     INSW


;__________________________________ end_ata.

JMP	0x1000
CLI
HLT

times	510-($-$$) db 0x0
dw	0xaa55


codelabel:

MOV	AH, 0x0e
MOV	AL, 'R'
INT	0x10

MOV	AH, 0x0e
MOV	AL, 'E'
INT	0x10

CLI
HLT


times 2048	db 0
The output of qemu is:
RRR
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: [need help] ata-pio bootloader

Post by Kevin »

williderwurm wrote:Danke für deine Antwort. At which part of the code am I setting the LBA flag?
Sorry, I misread. The LBA flag in the device/head register is 0x40, which isn't contained in 0xa0, so that's okay.
Developer of tyndur - community OS of Lowlevel (German)
User avatar
williderwurm
Posts: 13
Joined: Wed Mar 18, 2015 5:28 am
Location: Hyperspace

Re: [need help] ata-pio bootloader

Post by williderwurm »

Any ideas why it still does not work properly?
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: [need help] ata-pio bootloader

Post by Kevin »

How do you build and run this for the "jmp 0x1000" to make any sense?
Developer of tyndur - community OS of Lowlevel (German)
User avatar
williderwurm
Posts: 13
Joined: Wed Mar 18, 2015 5:28 am
Location: Hyperspace

Re: [need help] ata-pio bootloader

Post by williderwurm »

Kevin wrote:How do you build and run this for the "jmp 0x1000" to make any sense?
Oops i know what you mean... HAHAHAH sometimes my brain just needs a break. HA. Anyway thank you all for your help I greatly appreciate it! The last error was me mixing up ES and DI register.

[SOLVED]
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: [need help] ata-pio bootloader

Post by Kevin »

You don't. You load the program to 1000:0000 (actually the offset is undefined because you don't set di any more; in qemu it happens to be 0) and then you jump to 0000:8c00 (here, the segment is undefined because you don't set cs anywhere; in qemu it happens to be 0 as well). After executing some garbage, your code is actually reached, but the BIOS code fails. With all segment registers correctly set up, the code works for me, though.

[edit: Okay, you were faster ;) ]
Developer of tyndur - community OS of Lowlevel (German)
Post Reply