How would you write your own disk access function in C?
Printable View
How would you write your own disk access function in C?
Windows, or none.
You can start with the CreateFile function
http://msdn.microsoft.com/en-us/libr...58(VS.85).aspxQuote:
You can use the CreateFile function to open a physical disk drive or a volume, which returns a direct access storage device (DASD) handle that can be used with the DeviceIoControl function. This enables you to access the disk or volume directly, for example such disk metadata as the partition table. However, this type of access also exposes the disk drive or volume to potential data loss, because an incorrect write to a disk using this mechanism could make its contents inaccessible to the operating system. To ensure data integrity, be sure to become familiar with DeviceIoControl and how other APIs behave differently with a direct access handle as opposed to a file system handle.
No, I mean be able to use C language keywords and create something that will write to any location on disk that you tell it.
Well, the functions have to be written, using standard c keywords. Anyway, I could also use inline asm, I just want an idea on how to write such a thing.
Why don't you want to use the Windows API functions which is how you're supposed to access hardware like that?
If you are using Windows (and not inside a piece of driver code), then CreateFile() and it's friends are the way that you access the disk.
If you have "no OS", then it is up to you (as the "OS" designer) and the hardware specs of the type of disk interface how you access the disk.
C in and of itself doesn't have any direct knowledge at all of hard-disks if there isn't enough stuff (OS, drivers, filesystems, etc) there to use the disk as a storage for files.
--
Mats
So I need to write an HDD driver?
Depends on what you actually want to do. If you are running windows or linux, you definitely CAN NOT access the hard-disk directly from a user-process - the OS simply prevents those kind of operations from a user-process. A Hard-disk driver will be on the kernel-side of the wall, so it will be allowed to make such accesses.
Note that writing a bit of code that reads/writes a hard-disk isn't particularly hard - writing a hard-disk driver is MUCH harder.
--
Mats
You have to acess INT 13h, which gives low level access to disk I/O. Your code has to be running at CPL0, which means kernel level.
Int 13 is 16-bit BIOS code. Even at ring zero I don't think you can call that. For that matter, Windows has probably pointed that vector elsewhere.
This looks useful:
http://support.microsoft.com/kb/100027
As stated by brewbuck, int 13h is a 16-bit real-mode interface, and kernel mode doesn't supprt real-mode, nor 16-bit protected mode kernel code. You could, if you have kernel level access and know REALLY WELL what you are doing, get back to real-mode from protected mode, but it's REALLY messy, and if you have kernel mode access in the first place, it's going to be easier to access the disk directly anyways.
On the other hand, if you write a 16-bit DOS application that uses int 13h functionality under Windows, it will just do the corresponding CreateFile, ReadFile and WriteFile that does something close to what the original BIOS calls do.
--
Mats
I have written my own bootloader and starting a keyboard driver in asm. Is there any way I can execute in ring 0 to use my own HDD driver? Also, Vista does not allow 16-bit apps to run.