# C++ Code to Access Linux Hard Disk Sectors (with a LoopBack Virtual Hard Disk)

**URL:** <https://community.unix.com/t/c-code-to-access-linux-hard-disk-sectors-with-a-loopback-virtual-hard-disk/281417>\
**Category:** Linux\
**Created:** [January 26, 2011, 5:23am UTC](https://community.unix.com/t/c-code-to-access-linux-hard-disk-sectors-with-a-loopback-virtual-hard-disk/281417 "2011-01-26T05:23:56Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [January 27, 2011, 1:38pm UTC](https://community.unix.com/t/c-code-to-access-linux-hard-disk-sectors-with-a-loopback-virtual-hard-disk/281417/21 "2011-01-27T13:38:15Z")

</div>

> [@shen747](#):
>
> Hi Corona688,
> 
> Thank you very very much for the immense support you've given me throughout this thread. I've learned a lot from you since I started this thread :). I understand what I should do and how I should proceed. Thanks you very much sir.
> 
> I've tried the HexDump Command you gave me on my Virtual MS-DOS(FAT-16) Floppy Drive & as follows :

Your virtual floppy disk, like any floppy disk, is FAT12, not FAT16. Look closely at your dump:

```plaintext
00000000 eb 3c 90 6d 6b 64 6f 73 66 73 00 00 02 01 01 00 |.<.mkdosfs......|
00000010 02 e0 00 40 0b f0 09 00 12 00 02 00 00 00 00 00 |...@............|
00000020 00 00 00 00 00 00 29 26 7a d7 50 20 20 20 20 20 |......)&z.P |
00000030 20 20 20 20 20 20 46 41 54 31 32 20 20 20 0e 1f | FAT12 ..|
00000040 be 5b 7c ac 22 c0 74 0b 56 b4 0e bb 07 00 cd 10 |.[|.".t.V.......|
00000050 5e eb f0 32 e4 cd 16 cd 19 eb fe 54 68 69 73 20 |^..2.......This |
00000060 69 73 20 6e 6f 74 20 61 20 62 6f 6f 74 61 62 6c |is not a bootabl|
00000070 65 20 64 69 73 6b 2e 20 20 50 6c 65 61 73 65 20 |e disk. Please |
00000080 69 6e 73 65 72 74 20 61 20 62 6f 6f 74 61 62 6c |insert a bootabl|
00000090 65 20 66 6c 6f 70 70 79 20 61 6e 64 0d 0a 70 72 |e floppy and..pr|

```

To make a FAT16 partition you might want to use the instructions I gave you on a blank file of a couple megabytes in size.

You don't need to use the loop device for anything but mounting the partition (something you intend to avoid doing anyway). As far as mkfs.msdos, read(), and write() are concerned they act the same.

---

<div class="post-metadata">

**Author:** ![shen747](https://community.unix.com/letter_avatar/shen747/32/5_5575768a8748004e209b776fc1b2916d.png) [@shen747](https://community.unix.com/u/shen747)\
**Post date:** [January 28, 2011, 5:49am UTC](https://community.unix.com/t/c-code-to-access-linux-hard-disk-sectors-with-a-loopback-virtual-hard-disk/281417/22 "2011-01-28T05:49:44Z")

</div>

Hi Corona688,

Thanks a lot for pointing that out. I've been doing some reading & now I feel going for the FAT-12(classical floppy disk) format file system would be the best idea as it was the 1st form of FAT introduced to the world & I guess therefore it would be much simpler & easier to implement. I read that in FAT-12 the Floppy Disk Drive's blocks are stored in a linked list style data structure & that there will be a root block containing the logical address of the next contiguous block of a file & that contiguous block containing the logical address of the next block on the floppy disk drive & so on. I guess I could try to implement this using a simple linked list data structure using c together with the FAT :).

```
            I'll be going through the pointers you've given me so far & getting started on this. Thank you so much for helping me get this far :\).

```

---------- Post updated 01-28-11 at 05:49 AM ---------- Previous update was 01-27-11 at 11:20 PM ----------

Hi All,

I found [this](http://www.c-jump.com/CIS24/Slides/FAT/lecture.html#F01_0260_new_file) explaining how to write a file to a FAT-12 file system. But I'm still finding trouble finding a basic code to get me started on this. Can anyone of you help me out here please ?.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [January 28, 2011, 11:05am UTC](https://community.unix.com/t/c-code-to-access-linux-hard-disk-sectors-with-a-loopback-virtual-hard-disk/281417/23 "2011-01-28T11:05:38Z")

</div>

> [@shen747](#):
>
> Hi Corona688,
> 
> Thanks a lot for pointing that out. I've been doing some reading & now I feel going for the FAT-12(classical floppy disk) format file system would be the best idea as it was the 1st form of FAT introduced to the world & I guess therefore it would be much simpler & easier to implement.

It's not. I already explained why. But if you're willing to deal with the 12-bit strangeness, all right...

> [@](#):
>
> I found [this](http://www.c-jump.com/CIS24/Slides/FAT/lecture.html#F01_0260_new_file) explaining how to write a file to a FAT-12 file system. But I'm still finding trouble finding a basic code to get me started on this.

If you don't want to write the code, you can just mount the filesystem and use it...

I think you're getting too far ahead of yourself. You can't write a file if you haven't even built things to deal with FAT tables, sectors, directory entries... Work from the ground up. Read up on the structure and try to find and use everything they tell you about.

Look at the boot block structure I gave you. Load the boot sector from your floppy into it and see if the values in it make any sense -- if not, either you loaded the wrong data or I made a mistake in that structure.

Once you get that looking okay, use the values in it to find the FAT table. Write a function to get a 12-bit values out of it(you can't just use an array because of the weird 12-bit size). Use hexdump to see what values you should be getting.

Once you can read the FAT table, start checking what things these FAT entries are pointing to. (If your FAT entries are scrambled somehow you could end up reading garbage that makes no sense, so make really sure they're right. Test a bunch of them.) Read in data from where they direct you and these will be file and directory entry structures. Their size might depend on values you read somewhere else. Try following a chain of them to get a complete file or directory listing.

Once you've done all that, the steps you see in that course information will make a whole lot more sense to you.

---

<div class="post-metadata">

**Author:** ![shen747](https://community.unix.com/letter_avatar/shen747/32/5_5575768a8748004e209b776fc1b2916d.png) [@shen747](https://community.unix.com/u/shen747)\
**Post date:** [January 28, 2011, 9:27pm UTC](https://community.unix.com/t/c-code-to-access-linux-hard-disk-sectors-with-a-loopback-virtual-hard-disk/281417/24 "2011-01-28T21:27:12Z")

</div>

Hi Corona688,

Thanks for pointing the FAT-12 issue out. I changed my disk format to FAT-16. Following is my new HEXDUMP:

```nohighlight
$ sudo hexdump -C /dev/loop0 | head

00000000 eb 3c 90 6d 6b 64 6f 73 66 73 00 00 02 01 01 00 |.<.mkdosfs......|
00000010 02 e0 00 40 0b f0 0c 00 12 00 02 00 00 00 00 00 |...@............|
00000020 00 00 00 00 00 00 29 89 47 06 64 20 20 20 20 20 |......).G.d |
00000030 20 20 20 20 20 20 46 41 54 31 36 20 20 20 0e 1f | FAT16 ..|
00000040 be 5b 7c ac 22 c0 74 0b 56 b4 0e bb 07 00 cd 10 |.[|.".t.V.......|
00000050 5e eb f0 32 e4 cd 16 cd 19 eb fe 54 68 69 73 20 |^..2.......This |
00000060 69 73 20 6e 6f 74 20 61 20 62 6f 6f 74 61 62 6c |is not a bootabl|
00000070 65 20 64 69 73 6b 2e 20 20 50 6c 65 61 73 65 20 |e disk. Please |
00000080 69 6e 73 65 72 74 20 61 20 62 6f 6f 74 61 62 6c |insert a bootabl|
00000090 65 20 66 6c 6f 70 70 79 20 61 6e 64 0d 0a 70 72 |e floppy and..pr|

```

[Previous page](https://community.unix.com/t/c-code-to-access-linux-hard-disk-sectors-with-a-loopback-virtual-hard-disk/281417.md?page=1)
