Page 1 of 1
[need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 6:28 am
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
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 6:58 am
by iansjack
Where exactly on the hard disk have you placed this code, and how did you do so?
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 7:56 am
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?
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 8:53 am
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...
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 8:56 am
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.
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 9:13 am
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.
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 9:15 am
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.
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 9:20 am
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.
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 9:22 am
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?
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 9:27 am
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
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 9:45 am
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.
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 10:59 am
by williderwurm
Any ideas why it still does not work properly?
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 11:09 am
by Kevin
How do you build and run this for the "jmp 0x1000" to make any sense?
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 11:15 am
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]
Re: [need help] ata-pio bootloader
Posted: Wed Mar 18, 2015 11:27 am
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
]