How do they read clusters/cylinders/sectors from the disk?


How do they read clusters/cylinders/sectors from the disk?



I needed to recover the partition table I deleted accidentally. I used an application named TestDisk. Its simply mind blowing. I reads each cylinder from the disk. I've seen similar such applications which work with MBR & partitioning.

I'm curious.

How do they read clusters/cylinders/sectors from the disk? Is there some kind of API for this?

Is it again OS dependent? If so whats the way to for Linux & for windows?

EDIT: Well, I'm not just curious I want a hands on experience. I want to write a simple application which displays each LBA.


Find if file data is an image (php)

1:

Code assistance in Netbeans on Linux
Cylinders and sectors (wiki explanation) are largely obsoleted by the newer LBA (logical block addressing) scheme for addressing drives.. No warning from gcc when function definition in linked source different from function prototype in header If you're curious around the history, use the Wikipedia article as a starting point. Program to open large MySQL dumps If you're just wondering how it works now, code is expected to simply use the LBA address (which works largely the same way as a file does - a linear array of bytes arranged in blocks). Finding an available network port on the machine
Monitor file in Java on Linux 64bitsPreventing multiple process instances on Linux

2:

Control Debug Level in C++ Library - Linux
It's easy due to the magic of *nix special device files. You must open and read /dev/sda the same way you'd read any another file.. Just use open, lseek, read, write (or pread, pwrite). If you want to make sure you're physically fetching data from a drive and not from kernel buffers you must open with the flag O_DIRECT (though you need perform aligned reads/writes of 512 byte chunks for this to work)..

3:

For *nix, there have been already answers (/dev directory); for Windows, there are the special objects \\.\PhisicalDriveX, with X as the number of the drive, which must be opened using the normal CreateFile API. To actually perform reads or writes you have then to use the DeviceIoControl function.. More info must be found in "Physical Disks and Volumes" section of the CreateFile API documentation..

4:

I'm the OP. I'm combining Eric Seppanen's & Matteo Italia's answers to make it complete.. *NIX Platforms:. It's easy due to the magic of *nix special device files. You must open and read /dev/sda the same way you'd read any another file.. Just use open, lseek, read, write (or pread, pwrite). If you want to make sure you're physically fetching data from a drive and not from kernel buffers you must open with the flag O_DIRECT (though you need perform aligned reads/writes of 512 byte chunks for this to work).. Windows Platform. For Windows, there are the special objects \\.\PhisicalDriveX, with X as the number of the drive, which must be opened using the normal CreateFile API. To perform reads or writes simply call ReadFile and WriteFile (buffer need be aligned on sector size). . More info must be found in "Physical Disks and Volumes" section of the CreateFile API documentation.. Alternatively you must also you DeviceIoControl function which sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation..

5:

On linux, as root, you must save your MBR like this (Assuming you drive is /dev/sda):.
dd if=/dev/sda of=mbr bs=512 count=1 
If you wanted to read 1Mb from you drive, starting at the 10th MB:.
dd if=/dev/sda of=1Mb bs=1Mb count=1 skip=10 


76 out of 100 based on 51 user ratings 846 reviews

@