Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain direct access to raw HD data in C?

I need to do a complete format on a USB stick (FAT16 or FAT32), put a file on the drive, then delete it and recover the file using a C program.

Could anyone give me a hint on what should I use to accomplish this?

I understand the layouts of the FAT16/32 filesystems, the problem is that I don't know how to access the raw HD data using C (since I can't use things like fopen or mmap because the file isn't there anymore).

like image 778
Álvaro Avatar asked Dec 27 '22 09:12

Álvaro


1 Answers

This is highly operating system specific.

For Linux, you would open the raw device /dev/sdxx. Note that there are privilege hoops to manage.

For Windows, you would use something like:

 HANDLE h = CreateFile ("\\\\.\\PhysicalDriveX", GENERIC_READ,
                    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                    OPEN_EXISTING,
                    FILE_FLAG_NO_BUFFERING | FILE_FLAG_RANDOM_ACCESS,
                    NULL);

where X depends on the device.

like image 181
wallyk Avatar answered Jan 13 '23 01:01

wallyk