ATA PIO driver

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
teodori
Member
Member
Posts: 103
Joined: Wed Nov 14, 2012 4:55 pm

ATA PIO driver

Post by teodori »

Hello, right now I try to read from and write to ata drive, but I got some problems. I downloaded a PDF about ATA and ATAPI, but I still can't manage a simple PIO read or write. Lets say I do this, what shoul I do next to get the bytes from ATA device:

Code: Select all

#define ATA0_MSTR 0x01f0
#define ATA0_SLVE 0x03f0
#define ATA1_MSTR 0x0170
#define ATA1_SLVE 0x0370

	outb(0x40, ATA0_MSTR + 0x0006); // Select Master
	outb(0x00, ATA0_MSTR + 0x0002); // sectorcount high byte
	outb(0x00, ATA0_MSTR + 0x0003); // LBA4
	outb(0x00, ATA0_MSTR + 0x0004); // LBA5
	outb(0x00, ATA0_MSTR + 0x0005); // LBA6
	outb(0x01, ATA0_MSTR + 0x0002); // sectorcount low byte
	outb(0x00, ATA0_MSTR + 0x0003); // LBA1
	outb(0x00, ATA0_MSTR + 0x0004); // LBA2
	outb(0x00, ATA0_MSTR + 0x0005); // LBA3
 	outb(0x24, ATA0_MSTR + 0x0007); // read pio cmd
Read ATA0_MSTR?

Code: Select all

inb(ATA0_MSTR); // read byte
Who can explain ATA in details?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: ATA PIO driver

Post by Combuster »

"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
teodori
Member
Member
Posts: 103
Joined: Wed Nov 14, 2012 4:55 pm

Re: ATA PIO driver

Post by teodori »

Hello yeah I readed this one but it's not very comprehensive. Through the code example I somehow deducted how it works. .. still I get bochs errors and it doesn't work.
justin
Member
Member
Posts: 43
Joined: Sun Jan 11, 2009 2:09 pm

Re: ATA PIO driver

Post by justin »

teodori wrote:Hello yeah I readed this one but it's not very comprehensive. Through the code example I somehow deducted how it works. .. still I get bochs errors and it doesn't work.
What kind of errors do you get from bochs? Post your log. You need to give a lot more details for people to be able to help.
teodori
Member
Member
Posts: 103
Joined: Wed Nov 14, 2012 4:55 pm

Re: ATA PIO driver

Post by teodori »

Ok here is my test example

Code: Select all

void ata_lba_read(){
	outb(0x40, ATA0_MSTR + 0x0006); // Select Master
	outb(0x00, ATA0_MSTR + 0x0002); // sectorcount high byte
	outb(0x00, ATA0_MSTR + 0x0003); // LBA4
	outb(0x00, ATA0_MSTR + 0x0004); // LBA5
	outb(0x00, ATA0_MSTR + 0x0005); // LBA6
	outb(0x01, ATA0_MSTR + 0x0002); // sectorcount low byte
	outb(0x00, ATA0_MSTR + 0x0003); // LBA1
	outb(0x00, ATA0_MSTR + 0x0004); // LBA2
	outb(0x00, ATA0_MSTR + 0x0005); // LBA3
 	outb(0x24, ATA0_MSTR + 0x0007); // read pio cmd

	int i;
	uint8_t data;
	for(i = 0; i < 512; i++){
		data = inb(ATA0_MSTR); // read byte
		vga_write(COLOR_BLACK, COLOR_CYAN, data, 800 + i);
	}
}
and I get:

Code: Select all

00005225966e[DEV  ] read from port 0x01f0 with len 1 returns 0xff
00054400644i[CPU0 ] WARNING: HLT instruction with IF=0!
00214022005i[     ] cpu loop quit, shutting down simulator
00214022005i[CPU0 ] CPU is in long mode (halted)
00214022005i[CPU0 ] CS.mode = 64 bit
00214022005i[CPU0 ] SS.mode = 64 bit
00214022005i[CPU0 ] EFER   = 0x00000501
00214022005i[CPU0 ] | RAX=0000000000000469  RBX=0000000000200003
00214022005i[CPU0 ] | RCX=0000000000000068  RDX=00000000000b80d0
00214022005i[CPU0 ] | RSP=0000000000007e00  RBP=00000000000000ac
00214022005i[CPU0 ] | RSI=0000000000000004  RDI=0000000000000000
00214022005i[CPU0 ] |  R8=0000000000000000   R9=0000000000000000
00214022005i[CPU0 ] | R10=0000000000000000  R11=0000000000000000
00214022005i[CPU0 ] | R12=0000000000000000  R13=0000000000000000
00214022005i[CPU0 ] | R14=0000000000000000  R15=0000000000000000
00214022005i[CPU0 ] | IOPL=0 id vip vif ac vm rf nt of df if tf sf zf af pf cf
00214022005i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00214022005i[CPU0 ] |  CS:0008( 0001| 0|  0) 00000000 ffffffff 1 0
00214022005i[CPU0 ] |  DS:0020( 0004| 0|  0) 00000000 ffffffff 1 0
00214022005i[CPU0 ] |  SS:0020( 0004| 0|  0) 00000000 ffffffff 1 0
00214022005i[CPU0 ] |  ES:0020( 0004| 0|  0) 00000000 ffffffff 1 0
00214022005i[CPU0 ] |  FS:0020( 0004| 0|  0) 00000000 ffffffff 1 0
00214022005i[CPU0 ] |  GS:0020( 0004| 0|  0) 00000000 ffffffff 1 0
00214022005i[CPU0 ] |  MSR_FS_BASE:0000000000000000
00214022005i[CPU0 ] |  MSR_GS_BASE:0000000000000000
00214022005i[CPU0 ] | RIP=0000000000007f10 (0000000000007f10)
00214022005i[CPU0 ] | CR0=0xe0000011 CR2=0x0000000000000000
00214022005i[CPU0 ] | CR3=0x00000008 CR4=0x000000a0
00214022005i[CPU0 ] 0x0000000000007f10>> jmp .-3 (0x0000000000007f0f) : EBFD
00214022005i[CMOS ] Last time is 1384552459 (Fri Nov 15 22:54:19 2013)
00214022005i[XGUI ] Exit
00214022005i[     ] restoring default signal behavior
00214022005i[CTRL ] quit_sim called with exit code 1
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: ATA PIO driver

Post by Combuster »

the wiki wrote:8. Wait for an IRQ or poll.
9. Transfer 256 words, a word at a time
Read properly: You're failing at doing 9 as documented, and you're skipping 8 altogether.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
teodori
Member
Member
Posts: 103
Joined: Wed Nov 14, 2012 4:55 pm

Re: ATA PIO driver

Post by teodori »

Ok so I send the command to the controller, then wait for hardware interrupt. Next I check the status to see if I can read from data port. Does that also mean I will have 256 interrupts where I read the data port 2 times? PIO is very bad...
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: ATA PIO driver

Post by Gigasoft »

Of course not. After receiving an interrupt and checking the status register, you perform 256 word reads from the data port.
teodori
Member
Member
Posts: 103
Joined: Wed Nov 14, 2012 4:55 pm

Re: ATA PIO driver

Post by teodori »

Ok thank you
Post Reply