views:

386

answers:

4

This is a continuation of my question about reading the superblock.

Let's say I want to target the HFS+ file system in Mac OS X. How could I read sector 2 of the boot disk? As far as I know Unix only provides system calls to read from files, which are never stored at that location.

Does this require either 1) the program to run kernel mode, or 2) the program to be written in Assembly? I would prefer to avoid either of these restrictions, particularly the latter.

+3  A: 

In Linux, you can read from the special device file /dev/sda, assuming the hard drive you want to read is the first one. You need to be root to read this file. To read sector 2, you just seek to offset 2*SECTOR_SIZE and read in SECTOR_SIZE bytes.

I don't know if this device file is available on OS X. Check for interestingly named files under /dev such as /dev/sda or /dev/hda.

Adam Rosenfield
Hmm... there is no sda or hda in /dev on my machine. There are some interesting ones like disk0 which I will take a look at.
titaniumdecoy
A: 

I was also going to suggest hitting the /dev/ device file for the volume, but you might want to contact Amit Singh who has written an hfsdebug utility and has probably done just what you want to do.

plinth
A: 

How does this work in terms of permissions? Wouldn't reading from /dev/... be insecure since if you read far enough you would be able to read files for which you do not have read access?

titaniumdecoy
Only the superuser (root) can read the raw disk files (/dev/sda on Linux, /dev/disk0 on OS X).
Adam Rosenfield
+5  A: 

I've done this myself on the Mac, see my disk editor tool: http://ipodlinux.org/wiki/Rohpod

You'd open the drive using the /dev/diskN or /dev/rdiskN (N is a disk index number starting from 0). Then you can use lseek (make sure to use the 64 bit range version!) and read/write calls on the opened file.

Also, use the shell command "ls /dev/disk*" to see which drives exist currently. And note that the drives also exist with a "sM" extension where M is the partition number. That way, could can also read partitions directly.

Or, you could just use the shell tool "xxd" or "dd" to read data and then use their output. Might be easier.

You'll not be able to read your root volume unless you run as root, though. You may be able to access other drives as long as they were mounted by the user, or have their permissions disabled. But you may also need to unmount the drive's volumes first. Look for the unmount command in the shell command "diskutil".

Hope this helps.

Thomas Tempelmann