4

I was wondering how hard disk access works. Ex, how could I view/modify sectors? Im targeting Windows if that helps. Thanks

jmasterx
  • 52,639
  • 96
  • 311
  • 557

2 Answers2

5

This page seems to have some relevant information on the subject:

You can open a physical or logical drive using the CreateFile() application programming interface (API) with these device names provided that you have the appropriate access rights to the drive (that is, you must be an administrator). You must use both the CreateFile() FILE_SHARE_READ and FILE_SHARE_WRITE flags to gain access to the drive.

Once the logical or physical drive has been opened, you can then perform direct I/O to the data on the entire drive. When performing direct disk I/O, you must seek, read, and write in multiples of sector sizes of the device and on sector boundaries. Call DeviceIoControl() using IOCTL_DISK_GET_DRIVE_GEOMETRY to get the bytes per sector, number of sectors, sectors per track, and so forth, so that you can compute the size of the buffer that you will need.

The documentation of CreateFile also offers some clues:

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.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
1

You can open a logical volume (e.g. c: drive) or a physical drive using win32's CreateFile() function. With the returned handle you can read and write sectors as needed. This page at MSDN should get you started: CreateFile Function

I take no responsibility for damaged caused :-)

  • I got the impression that @user wanted *direct* access to the drive. – John Dibling Apr 24 '10 at 03:44
  • @John: do you mean that he might want to bypass Windows altogether? When opening a physical drive you do have direct access to the contents of it... it's not soldering your own SATA controller but it's at a lower level than people usually go :-) I'm pretty sure the Windows Driver Development Kit has samples for disk drivers if that helps. – Jacob O'Reilly Apr 24 '10 at 12:28