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.
// function read data from disk info buffer and return if succeed
void read28(unsigned int lba, char* buffer, unsigned sector_count)
{
unsigned int offset = 0;
unsigned int i = 0;
// prepare the hard disk controller for reading (by specefic protocol - taken from ATA PIO Mode osDev)
out_byte(ATA_ERR_INFO_PORT, 0x00);
out_byte(ATA_SECTOR_COUNT_REGISTER_PORT, (unsigned char)sector_count); // sector count
out_byte(ATA_LBA_LOW_REGISTER_PORT, (unsigned char)lba); // Send the low 8 bits of the LBA
out_byte(ATA_LBA_MID_REGISTER_PORT, (unsigned char)(lba>>8)); // Send the mid 8 bits of the LBA
out_byte(ATA_LBA_HIGH_REGISTER_PORT, (unsigned char)(lba>>16)); // Send the high 8 bits of the LBA
out_byte(ATA_DRIVE_REGISTER_PORT, 0xE0 | 0x40 | ((lba>>24) & 0x0F)); // master drive + lba + high lba bits
out_byte(ATA_COMMAND_REGISTER_PORT, ATA_READ); // read command
// for every sector - read into buffer
while(sector_count--) {
ata_wait_for_rdy(ATA_REGULAR_STATUS_REGISTER_PORT); // Spun up
ata_wait_for_drq(ATA_REGULAR_STATUS_REGISTER_PORT); // Ready for transfer
for(i = 0;i < 256;i++)
{
// write data from disk into buffer
*( (unsigned short *)buffer + offset + i ) = in_word(ATA_DATA_REGISTER_PORT);
}
offset += 256;
}
}
During my OS execution there is sometimes when my OS freeze becouse of reading some sector from disk...
How can i fix this probelm ? I use this function in my FS FAT32
ata_wait_for_rdy(ATA_REGULAR_STATUS_REGISTER_PORT); // Spun up
ata_wait_for_drq(ATA_REGULAR_STATUS_REGISTER_PORT); // Ready for transfer
but after the last sector read, you do not read from it.
The ata_wait_for_rdy (and _drq) should be reading from the ALT Status register, and then you should read from the Status register after the for() loop, checking for errors.
ata_wait_for_rdy(ATA_REGULAR_STATUS_REGISTER_PORT); // Spun up
is not required since your drive should already be spinning. A CD-ROM is most likely an ATAPI device and you can't read from it using this form of read command.