OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Mar 19, 2019 7:23 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Read from disk - myOS
PostPosted: Sat Mar 16, 2019 2:08 pm 
Offline

Joined: Wed Jan 02, 2019 5:18 am
Posts: 4
this is my disk driver code:

Code:

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


Top
 Profile  
 
 Post subject: Re: Read from disk - myOS
PostPosted: Sat Mar 16, 2019 2:19 pm 
Offline
Member
Member

Joined: Sat Mar 10, 2018 10:16 am
Posts: 111
Did you acknowledge interrupt with reading from status port(offset 7)? In emulators Primary IDE have irq14 and secondary IDE have irq15.


Top
 Profile  
 
 Post subject: Re: Read from disk - myOS
PostPosted: Sat Mar 16, 2019 3:00 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 548
Location: USA
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:
  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:
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot], Sik and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group