Read from disk - myOS

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
Rocobo
Posts: 4
Joined: Wed Jan 02, 2019 5:18 am

Read from disk - myOS

Post 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
Klakap
Member
Member
Posts: 299
Joined: Sat Mar 10, 2018 10:16 am

Re: Read from disk - myOS

Post by Klakap »

Did you acknowledge interrupt with reading from status port(offset 7)? In emulators Primary IDE have irq14 and secondary IDE have irq15.
User avatar
BenLunt
Member
Member
Posts: 941
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Re: Read from disk - myOS

Post 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
Post Reply