开发者

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

开发者 https://www.devze.com 2022-12-31 06:18 出处:网络
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

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 开发者_如何学JAVALinux & 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.


Cylinders and sectors (wiki explanation) are largely obsoleted by the newer LBA (logical block addressing) scheme for addressing drives.

If you're curious about the history, use the Wikipedia article as a starting point. 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)


It's easy due to the magic of *nix special device files. You can open and read /dev/sda the same way you'd read any other 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 can open with the flag O_DIRECT (though you must perform aligned reads/writes of 512 byte chunks for this to work).


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 can be opened using the normal CreateFile API. To actually perform reads or writes you have then to use the DeviceIoControl function.

More info can be found in "Physical Disks and Volumes" section of the CreateFile API documentation.


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 can open and read /dev/sda the same way you'd read any other 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 can open with the flag O_DIRECT (though you must 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 can be opened using the normal CreateFile API. To perform reads or writes simply call ReadFile and WriteFile (buffer must be aligned on sector size).

More info can be found in "Physical Disks and Volumes" section of the CreateFile API documentation.

Alternatively you can also you DeviceIoControl function which sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.


On linux, as root, you can 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
0

精彩评论

暂无评论...
验证码 换一张
取 消