Drake Johnson
Drake Johnson

Reputation: 664

Is there a way to code data directly to the hard drive (similar to how one can do with RAM)?

My question concerns C/C++. It is possible to manipulate the data on the RAM with pretty great flexibility. You can also give the GPU direct commands using OpenGL, allowing one to manipulate VRAM as well.

My curiosity is whether it is possible to do this to the hard drive (even though this would likely be a horrible idea with many, many possibilities of corrupting existing data). The logic of my question comes from an assumption that the hard drive is similar to RAM and VRAM (bytes of data), but just accesses data slower.

I'm not asking about how to perform file IO, but instead how to directly modify bytes of memory on the hard drive (maybe via some sort of "hard-drive pointer").

If my assumption is totally off, a detailed correction about how the hard drive's data storage is different from RAM or VRAM would be very helpful. Thank you!

Upvotes: 3

Views: 1438

Answers (3)

iunknown
iunknown

Reputation: 11

YES there are ways to code and access HDD internal storage meta data, read and write to its storage like an array. In order to communicate with HDD you need to learn ATA protocol and its successors PATA and SATA protocols which are supported by most of the HDD manufacturers, i think it doesn't matter mechanical or SSD. Mechanical once deals storage in blocks sectors and cylinders, SSD not sure but it should be a sequential array and all nitty gritty is behind the front ATA interface protocols. As long as you can communicate with standard ATA protocol than you dont need to learn manufactures specific internal implementations. if you can code in C++ to communicate with USB devices using USB protocols libraries then you can use USB to SATA Cable and connect with your SATA HDD then its half the battle, rest is SATA protocol. I havent done it recently but have successfully done that with BIOS interrupt calls with older PCs with ATA drives.

Upvotes: 0

Mike Nakis
Mike Nakis

Reputation: 61949

Modern operating systems in combination with modern CPUs offer the ability to memory-map disk clusters to memory pages.

The memory pages are initially marked as invalid, and as soon as you try to access them an invalid page "trap" or "interrupt" occurs, which is handled by the operating system, which loads the corresponding cluster into that memory page.

If you write to that page there is either a hardware-supported "dirty" bit, or another interrupt mechanism: the memory page is initially marked as read-only, so the first time you try to write to it there is another interrupt, which simply marks the page as dirty and turns it read-write. Then, you know that the page needs to be flushed to disk at a convenient time.

Note that reading and writing is usually done via Direct Memory Access (DMA) so the CPU is free to do other things while the pages are being transferred.

So, yes, you can do it, either with the help of the operating system, or by writing all that very complex code yourself.

Upvotes: 4

gnasher729
gnasher729

Reputation: 52530

Not for you. Being able to write directly to the hard drive would give you infinite potential to mess up things beyond all recognition. (The technical term is FUBAR, and the F doesn't stand for Mess).

And if you write hard disk drivers, I sincerely hope you are not trying to ask for help here.

Upvotes: 2

Related Questions