Page 1 of 2

Hard disk access in pm

Posted: Wed Mar 12, 2008 11:15 pm
by Philip
i wonder how hard disk can be accessed in protected mode?
cause int 13h can't be used :?

Posted: Thu Mar 13, 2008 12:00 am
by eddyb
hi, Philip
this is pretty simple via in/out ports, like any other peripherals communication. I recommend to get some ATA/SATA documentation, where it is explaneid how u can do this. I saw somewhere some docs, but i'm not remember now...

Posted: Thu Mar 13, 2008 1:31 am
by zaleschiemilgabriel
Actually it's not that simple to implement. Most people think it is because they use code that's written by someone else, and that's always simple. :roll: Any way, if you want simple, find a library and use it. If you want compatibility and performance, write it yourself. :wink:

Posted: Thu Mar 13, 2008 1:36 am
by pcmattman
Actually it's not that simple to implement. Most people think it is because they use code that's written by someone else, and that's always simple.
That's wrong. Hard drive drivers using in/out are extremely simple. All you need is the spec (t13.org) and a spare hard disk image to test with (of course, you should put known values at known sectors so you can try it out).

Posted: Thu Mar 13, 2008 1:38 am
by zaleschiemilgabriel
I think I burst a bubble... Yup, in concept it's that easy, but wait till you actually read the specs...

Posted: Thu Mar 13, 2008 1:41 am
by pcmattman
zaleschiemilgabriel wrote:I think I burst a bubble... Yup, in concept it's that easy, but wait till you actually read the specs...
Ever read a network card's spec?

Seriously though, if you read the spec and are still confused, you can still ask. The tutorial on LBA HDD access on osdever.net can help when you've read the spec and still don't get it, because you then get an idea of register offsets and the like.

Posted: Thu Mar 13, 2008 6:33 am
by Dex
I agree hdd is easy compared to most things in OS dev. see my post here to see how easy it is to destroy your main OS :lol:
http://www.osdev.org/phpBB2/viewtopic.p ... hlight=hdd

Posted: Thu Mar 13, 2008 8:29 am
by piranha
Check out the LBA access tutorial over at osdever.net.

Thats a good one.

-JL

Posted: Fri Mar 14, 2008 11:47 pm
by Philip
sorry a bit off topic but
seems like we have lots of expert here with osdev
i'm glad to be a member

Posted: Sat Mar 15, 2008 12:21 am
by Ready4Dis
zaleschiemilgabriel wrote:I think I burst a bubble... Yup, in concept it's that easy, but wait till you actually read the specs...
Yes, writing an LBA 'driver' that can read/write a sector (or multiple sectors) is very easy actually. Now, writing a driver that can detect installed drives gets to be harder (especially when the hardware doesn't conform to the specs properly like I have witnessed, I spent a good amount of time writing a very by the spec detection routine that just failed to run properly, after extensive debugging, i found the IDE controller was not giving me the proper values it should have according to the specifications, so I reverted back to my not-so-by-the-spec-but-works detection method, which detects all drives it should). Then once you start adding things like PCI config space, DMA, etc things start getting very far outside of the simple window. But to get a basic read primary disk on controller in LBA28 or LBA48 mode is very simple, even following the specs or examples on the web.

Posted: Sun Mar 16, 2008 5:33 am
by zaleschiemilgabriel
I know. I have implemented reading data from an IDE using PIO mode (without using interrupts), and that, I must agree, was pretty easy.
1. You have to program the DMA. Last time I checked, there were only 3 DMA channels available... who gets them? That means you must write some kind of resource manager for the DMA channels. If they're all busy at the moment, you have to wait...
2. Using interrupts is just weird. What do you do when an interrupt form the IDE you sent a "READ DMA" to interrupts? The processor might have done a lot of things ever since you sent the command, so there are a lot of synchronization issues there...

On the other hand, I found PCI device detection to be a lot easier than everything I just said. :roll:

Posted: Sun Mar 16, 2008 7:27 am
by Philip
anybody has a sample code to access hdd using port instructions like in and out in assembly?

Posted: Sun Mar 16, 2008 7:32 am
by t0xic
*cough* www.osdever.net *cough*

Posted: Sun Mar 16, 2008 9:01 am
by Ready4Dis
zaleschiemilgabriel wrote:I know. I have implemented reading data from an IDE using PIO mode (without using interrupts), and that, I must agree, was pretty easy.
1. You have to program the DMA. Last time I checked, there were only 3 DMA channels available... who gets them? That means you must write some kind of resource manager for the DMA channels. If they're all busy at the moment, you have to wait...
2. Using interrupts is just weird. What do you do when an interrupt form the IDE you sent a "READ DMA" to interrupts? The processor might have done a lot of things ever since you sent the command, so there are a lot of synchronization issues there...

On the other hand, I found PCI device detection to be a lot easier than everything I just said. :roll:
Yes, like I said, basic PIO is simple, so getting a very basic driver is not to difficult, now turning the device from legacy 'isa ide' mode, to 'pci ide' mode, reading the pci space, enabling the device, programming a dma driver, etc, etc is a lot more difficult. So, depending on what you consider a 'good enough for now' driver, means that writing a HD driver can be easy, or very difficult. I think that a PIO fall-back is a must... unless you plan on supporting only advanced machines, and know that you will never have to revert back to PIO, and can write drivers very well without needing a way to read the hard-drive while in PMODE while testing said drivers). So, I suggest that you, like many others, start with some code that scans the standard locations for devices, and write some code to read/write to them at a specific sector. Then something that can parse and MBR for partitions, then a FS driver so you can read files. Writing isn't important until you get a lot of bugs worked out (and I would suggest leaving it disabled until that point, unless you absolutely know it works, and you need to log something to disk for debugging, in which case I would suggest writing a serial port driver with another machine cnostantly reading inputs).

Posted: Sun Mar 16, 2008 9:10 am
by piranha
anybody has a sample code to access hdd using port instructions like in and out in assembly?
I just gave it to you!

-JL