Page 1 of 1

lba28 freeze

Posted: Sat May 12, 2007 7:53 am
by supagu
my lba28 hard drive reading code is getting stuck

in the while loop:
while ((inportb(device->portStart + HD_PORT_STATUS) & 0x08) != 0x08)
{
}

Code: Select all

#define HD_PORT_DATA         0
#define HD_PORT_STATUS       7

#define HD_READ              0x20
#define HD_WRITE             0x30

void BeginDriveAccess(const Device* device, unsigned int lbaAddr, unsigned int sectorCount, AccessType accessType)
{
	// lba28
	// http://www.osdever.net/tutorials/lba.php
	// http://mattise.cvs.sourceforge.net/mattise/hdd_pio/hdd_main.c?view=markup

	unsigned char drive = device->driveNum;
	unsigned long addr = lbaAddr;
	outportb(device->portStart + 1, 0x00);
	outportb(device->portStart + 2, sectorCount);
	outportb(device->portStart + 3, (unsigned char)addr);
	outportb(device->portStart + 4, (unsigned char)(addr >> 8));
	outportb(device->portStart + 5, (unsigned char)(addr >> 16));
	outportb(device->portStart + 6, 0xE0 | (drive << 4) | ((addr >> 24) & 0x0F));

	if (accessType == AT_Read)
		outportb(device->portStart + 7, HD_READ);
	else if (accessType == AT_Write)
		outportb(device->portStart + 7, HD_WRITE);

	// check for error
	unsigned char errReg =  inportb(device->portStart + 1);
	if (errReg)
	{
		Video_Format("Error reading drive '%u'\n", errReg);
		return;
	}

	while ((inportb(device->portStart + HD_PORT_STATUS) & 0x08) != 0x08) 
	{
		//Video_Format(".");
	}
}

void ReadSector(const Device* device, unsigned int lbaAddr, char* buffer, unsigned int sectorCount)
{
	Video_Format("ReadSector '%u'\n", lbaAddr);

	BeginDriveAccess(device, lbaAddr, sectorCount, AT_Read);

	Video_Format("ReadSector mid\n");

	for (unsigned short idx = 0; idx < (512 * sectorCount); idx += sizeof(unsigned short))
	{
		unsigned short tmpword = inportw(device->portStart + HD_PORT_DATA);
		*((short*) &buffer[idx]) = tmpword;

		/*
		if (idx % 32 == 0)
			Video_Format("\n");
		else if (idx % 16 == 0)
			Video_Format(" ");

		Video_Format("%x", tmpword);*/		
	}

	Video_Format("ReadSector END\n");
}
this only happens when i do a lot of read and writing to the disk. im try to write out 4mb, but always freezes some where after about 40k