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

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

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.

1 Like

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 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 ?.

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

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.

1 Like

Hi Corona688,

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

$ 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|