Page 1 of 1

Read from disk - myOS

Posted: Sat Mar 16, 2019 2:08 pm
by Rocobo
this is my disk driver code:

Code: Select all


// 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

Re: Read from disk - myOS

Posted: Sat Mar 16, 2019 2:19 pm
by Klakap
Did you acknowledge interrupt with reading from status port(offset 7)? In emulators Primary IDE have irq14 and secondary IDE have irq15.

Re: Read from disk - myOS

Posted: Sat Mar 16, 2019 3:00 pm
by BenLunt
Hi,

As Klakap stated, you need to acknowledge the interrupt by reading from the STATUS register after the read.

Most likely, you are reading from this status register every loop iteration at

Code: Select all

  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.

On a side note, the:

Code: Select all

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.

Ben
- http://www.fysnet.net/osdesign_book_series.htm