[Help] Reading a hard drive (LBA).

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
binsky3333
Posts: 14
Joined: Sat Jan 01, 2011 12:34 am

[Help] Reading a hard drive (LBA).

Post by binsky3333 »

Hi guys! Lately i have been trying to add drive reading support to my kernel. Here is what i have so far:

Code: Select all

void write_read_test()
{
	outportb(0x1F6, 0x40);
	outportb(0x1F1, 0x00);
	outportb(0x1F2, (unsigned char) 1);
	outportb(0x1F3, (unsigned char) 000000000000000000000000);
	outportb(0x1F4, (unsigned char) 000000000000000000000000 >> 8);
	outportb(0x1F5, (unsigned char) 000000000000000000000000 >> 16);
	outportb(0x1F7, 0x20);
	inportb(0x3F6);
	inportb(0x3F6);
	inportb(0x3F6);
	inportb(0x3F6);
	k_printf("\n");
	k_printf("Reading...\n");
	char *buffer;
	int i = 0;
	while(buffer != 256)
	{
		buffer[i] = inportw(0x1F0);
		i++;
	}
	k_printf(buffer);
}
Unfortuantely this code doesnt work... In bochs, i get the error... read sectors issued to non-disk. I belive i am getting this because i am either not putting in the right drive id, or it cannot find the drive. The error pops up in the console on this command outportb(0x1F7, 0x20); (Tells the controller to read). This is probably a simple issue. Any support would be greatly appreciated!

Thanks!
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: [Help] Reading a hard drive (LBA).

Post by bewing »

If bochs is giving you that message, it is most likely because the drive was never defined.
Are you sure you have activated ATA channel 0 and defined a primary master drive in your bochsrc file?

I also see a potential future mistake in your code, but it has no negative effect in this case. A cast has a much higher precedence than a shift operator, so it happens first. So your LBA calculations are truncating your LBA to 8 bits and THEN shifting down 8, 16, or 24. Which will always result in a value of 0. You need parentheses after the cast.
binsky3333
Posts: 14
Joined: Sat Jan 01, 2011 12:34 am

Re: [Help] Reading a hard drive (LBA).

Post by binsky3333 »

Ok i added a hd(whoops forgot haha). Now i get the error... ata0-0 read from 0/0/0, aborting command.

LOG:

Code: Select all

00218432312i[HD   ] ata0-0: : read from 0/0/0, aborting command
00218435215e[HD   ] IO read(0x01f0) with drq == 0: last command was 00h
00218435241e[HD   ] IO read(0x01f0) with drq == 0: last command was 00h
00218435267e[HD   ] IO read(0x01f0) with drq == 0: last command was 00h
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: [Help] Reading a hard drive (LBA).

Post by Combuster »

either use proper CHS (which doesn't start from all zeroes), or set the LBA bit.
"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 ]
binsky3333
Posts: 14
Joined: Sat Jan 01, 2011 12:34 am

Re: [Help] Reading a hard drive (LBA).

Post by binsky3333 »

Combuster wrote:either use proper CHS (which doesn't start from all zeroes), or set the LBA bit.
outportb(0x1F6, 0x40) Is this where the lBA bit is set? Is it wrong?

Also Isnt LBA 000000000000000000000000 Cylinder 0, Head 0, Sector 1?
Post Reply