OSDev.org

The Place to Start for Operating System Developers
It is currently Sun May 24, 2020 5:29 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: My kernel can't read hard disk on the real machine.
PostPosted: Fri Dec 25, 2015 8:02 am 
Offline
Member
Member

Joined: Mon Oct 26, 2015 7:24 am
Posts: 25
Location: Guilin,China
My kernel can read hard disk on qemu or bochs, but can't read hard disk on real machine.

I always get 0xFF when I execute inb(0x1F7) under the real machine.


Code:
#define IDE_BSY      0x80
#define IDE_DRDY   0x40
#define IDE_DF      0x20
#define IDE_ERR      0x01

ide_wait_ready(bool check_error)
{
   int num = 0x10000;
   int r;

   while (((r = inb(0x1F7)) & (IDE_BSY|IDE_DRDY)) != IDE_DRDY) {
      if (--num == 0) {
           return -1;
   }
   if (check_error && (r & (IDE_DF|IDE_ERR)) != 0)
      return -1;
   return 0;
}


Code:
int
ide_read(uint32_t secno, void *dst, uint32_t driver ,size_t nsecs)
{
   int r;

   assert(nsecs <= 256);
   
   ide_wait_ready(0);

   outb(0x1F6, 0xE0 | (driver << 4) | ((secno >> 24) & 0x0F));
   outb(0x1F1, 0x00);
   outb(0x1F2, nsecs);
   outb(0x1F3, secno & 0xFF);
   outb(0x1F4, (secno >> 8) & 0xFF);
   outb(0x1F5, (secno >> 16) & 0xFF);
   outb(0x1F6, 0xE0 | ((driver&1)<<4) | ((secno>>24)&0x0F));
   outb(0x1F7, 0x20);   // CMD 0x20 means read sector
   
   for (; nsecs > 0; nsecs--, dst += SECTSIZE) {
      if ((r = ide_wait_ready(1)) < 0)
         return r;
      insl(0x1F0, dst, SECTSIZE/4);
   }
   
   return 0;
}


Top
 Profile  
 
 Post subject: Re: My kernel can't read hard disk on the real machine.
PostPosted: Fri Dec 25, 2015 12:06 pm 
Offline
Member
Member
User avatar

Joined: Fri Apr 03, 2015 9:41 am
Posts: 492
Maybe your HDD is not IDE?

_________________
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.


Top
 Profile  
 
 Post subject: Re: My kernel can't read hard disk on the real machine.
PostPosted: Fri Dec 25, 2015 7:53 pm 
Offline
Member
Member

Joined: Mon Oct 26, 2015 7:24 am
Posts: 25
Location: Guilin,China
catnikita255 wrote:
Maybe your HDD is not IDE?

Do I need  initialize IDE before use it ?


Top
 Profile  
 
 Post subject: Re: My kernel can't read hard disk on the real machine.
PostPosted: Fri Dec 25, 2015 9:09 pm 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

626788149 wrote:
catnikita255 wrote:
Maybe your HDD is not IDE?

Do I need  initialize IDE before use it ?


You need to find out if IDE/ATA exists before you use it. Most real computers use SATA/AHCI instead (and some use SCSI, and some might be using NVMe already).


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: My kernel can't read hard disk on the real machine.
PostPosted: Fri Dec 25, 2015 9:27 pm 
Offline
Member
Member
User avatar

Joined: Tue Mar 06, 2007 11:17 am
Posts: 1224
Maybe you are using SATA disks/controllers, and if that's the case, you will most likely need to use a different set of registers than the typical 0x1F0-0x1F7 or 0x170-0x177 port ranges.

You would need to detect the port ranges for the disk controllers (using tye PCI bus functionality, I/O ports and/or memory mapped registers).

See this to see if it can help you. You can also use programs like PCI32 for Windows to manually scan for port addresses and use that like an initial hint for the addresses of all your PCI devices:


Craig Hart's PCI Programs:
http://devel.archefire.org/mirrors/members.datafast.net.au/dft0802/downloads.htm

http://wiki.osdev.org/PCI_IDE_Controller

_________________
http://www.ebay.com/usr/udocproject3 (Updated IP)
http://190.53.102.175/api/doc/en/ (My OS compatible with DOS)

(udocproject@yahoo.com)
-----------------------------
IP for hosts file (all domains):
190.53.102.175 api.exe


Last edited by ~ on Wed May 17, 2017 11:24 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: My kernel can't read hard disk on the real machine.
PostPosted: Sat Dec 26, 2015 9:47 pm 
Offline
Member
Member

Joined: Mon Oct 26, 2015 7:24 am
Posts: 25
Location: Guilin,China
Brendan wrote:
Hi,

626788149 wrote:
catnikita255 wrote:
Maybe your HDD is not IDE?

Do I need  initialize IDE before use it ?


You need to find out if IDE/ATA exists before you use it. Most real computers use SATA/AHCI instead (and some use SCSI, and some might be using NVMe already).


Cheers,

Brendan


yes, your are right, My hard disk is SATA/AHCI and I changed my hard disk to compatible mode and I get an ECC error.


Top
 Profile  
 
 Post subject: Re: My kernel can't read hard disk on the real machine.
PostPosted: Sat Dec 26, 2015 9:48 pm 
Offline
Member
Member

Joined: Mon Oct 26, 2015 7:24 am
Posts: 25
Location: Guilin,China
~ wrote:
Maybe you are using SATA disks/controllers, and if that's the case, you will most likely need to use a different set of registers than the typical 0x1F0-0x1F7 or 0x170-0x177 port ranges.

You would need to detect the port ranges for the disk controllers (using tye PCI bus functionality, I/O ports and/or memory mapped registers).

See this to see if it can help you. You can also use programs like PCI32 for Windows to manually scan for port addresses and use that like an initial hint for the addresses of all your PCI devices:


Craig Hart's PCI Programs:
http://www.archive.org/download/devel.archefire.org/mirrors/members.datafast.net.au/dft0802/downloads.htm

http://wiki.osdev.org/PCI_IDE_Controller



I changed my hard dsk to compatible mode and I get an ECC error ,Maybe I have to find out other way.


Top
 Profile  
 
 Post subject: Re: My kernel can't read hard disk on the real machine.
PostPosted: Sun Dec 27, 2015 12:38 am 
Offline
Member
Member
User avatar

Joined: Tue Mar 06, 2007 11:17 am
Posts: 1224
First you still need to make sure that you're actually using the actual BAR (base address registers) from the PCI device, as most likely they aren't the typical 0x1F0 or 0x170.

Then you must make sure that you aren't sending CD/DVD commands to a hard disk or hard disk commands to a CD/DVD, since they send ABORT signals when doing things like trying to identify them and sending the command for their counterpart (as in IDENTIFY PACKET DEVICE on a hard disk instead of IDENTIFY DEVICE).

_________________
http://www.ebay.com/usr/udocproject3 (Updated IP)
http://190.53.102.175/api/doc/en/ (My OS compatible with DOS)

(udocproject@yahoo.com)
-----------------------------
IP for hosts file (all domains):
190.53.102.175 api.exe


Top
 Profile  
 
 Post subject: Re: My kernel can't read hard disk on the real machine.
PostPosted: Sun Dec 27, 2015 3:03 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

~ wrote:
First you still need to make sure that you're actually using the actual BAR (base address registers) from the PCI device, as most likely they aren't the typical 0x1F0 or 0x170.

Then you must make sure that you aren't sending CD/DVD commands to a hard disk or hard disk commands to a CD/DVD, since they send ABORT signals when doing things like trying to identify them and sending the command for their counterpart (as in IDENTIFY PACKET DEVICE on a hard disk instead of IDENTIFY DEVICE).


More specifically; you'd want to start with PCI bus enumeration; and when you find a "mass storage controller" device (base class = 0x01) determine if it's IDE (sub-class = 0x01) or something else (SATA, RAID, SCSI, etc), and if it is IDE examine the "interface" part of the class code to determine what sort of programming interface it's using. The "interface" part of the class code will tell you if the controller is in "legacy ISA emulation mode" or in "native PCI mode". It will also tell you if the controller allows you to change it from one mode to the other.

If it's using "legacy ISA emulation mode" then it will use the ancient ISA resources (base IO port 0x01F0 or 0x0170, ISA IRQ 14 or 15) regardless of what anything else says (and the BARs shouldn't be used); and if it's using "native PCI mode" then you must use BARs (and PCI IRQs and not ISA IRQs 14 or 15).

Note that wherever possible I'd recommend trying to switch the controller into "native PCI mode" if necessary/possible; partly because it's more flexible and better for performance (especially when you start thinking about IO APICs and/or MSI); and partly because it allows you to support systems with 2 or more IDE controller devices without resource conflicts (e.g. 2 or more PCI devices that both think they "own" IO port 0x01F0, etc).


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 9 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