-1

The output below belongs to a FAT32 disk file. I want to display all files and corresponding allocated sectors for each file, with a C program.

I'm unable to even find the root directory by following the guide. (Example 1 - find the root directory in http://www.tavi.co.uk/phobos/fat.html) It seems 0x16 and 0x17 are 00. So they don't point to anything?

I'm reading the document again and again and I'm exhausted. Can someone explain what I'm doing wrong, or how to do this right by simple steps?

Any help is appreciated! Thank you.

Alternative resource: http://wiki.osdev.org/FAT#FAT_32_2

0000000: eb58 906d 6b66 732e 6661 7400 0201 2000  .X.mkfs.fat... .
0000010: 0200 0000 00f8 0000 2000 4000 0000 0000  ........ .@.....
0000020: 400d 0300 0306 0000 0000 0000 0200 0000  @...............
0000030: 0100 0600 0000 0000 0000 0000 0000 0000  ................
0000040: 8000 29c8 a726 5e4e 4f20 4e41 4d45 2020  ..)..&^NO NAME  
0000050: 2020 4641 5433 3220 2020 0e1f be77 7cac    FAT32   ...w|.
0000060: 22c0 740b 56b4 0ebb 0700 cd10 5eeb f032  ".t.V.......^..2
0000070: e4cd 16cd 19eb fe54 6869 7320 6973 206e  .......This is n
0000080: 6f74 2061 2062 6f6f 7461 626c 6520 6469  ot a bootable di
0000090: 736b 2e20 2050 6c65 6173 6520 696e 7365  sk.  Please inse
00000a0: 7274 2061 2062 6f6f 7461 626c 6520 666c  rt a bootable fl
00000b0: 6f70 7079 2061 6e64 0d0a 7072 6573 7320  oppy and..press 
00000c0: 616e 7920 6b65 7920 746f 2074 7279 2061  any key to try a
00000d0: 6761 696e 202e 2e2e 200d 0a00 0000 0000  gain ... .......
00000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000100: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000110: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000120: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000130: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000140: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000150: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000160: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000170: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000180: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000190: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001f0: 0000 0000 0000 0000 0000 0000 0000 55aa  ..............U.
0000200: 5252 6141 0000 0000 0000 0000 0000 0000  RRaA............
0000210: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000220: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000230: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000240: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000250: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000260: 0000 0000 0000 0000 0000 0000 0000 0000  ................
....

I used these to create the file:

dd if=/dev/zero of=disk.img bs=1k count=100000
losetup /dev/loop0 disk.img
mkdosfs -s 1 -F 32 /dev/loop0 100000
mmswe
  • 707
  • 2
  • 8
  • 20
  • What you can't do? You know boot block size. It tells you FAT size (well, with some math) and how many of them you have. You then know where FAT starts (just after boot block) and how long they are. After them you have root directory. Please also note that your tutorial speaks about FAT16, not FAT32. – Adriano Repetti May 14 '15 at 14:10
  • offset: 36. length: 4 btyes. what that section refer to in the data? – mmswe May 14 '15 at 14:19
  • 1
    Offset 32? No, boot record is 512 bytes (actually value at 0x0b * value at 0x0e in boot record). Length 4 bytes? No, it's value from 0x16 multiplied by value from 0x10. Result is in blocks (not bytes!). Then result must be multiplied with value at 0x0b. That's total size of boot rect + reserved blocks (optional) + total fat size. BTW that's NOT FAT32 but FAT16, distiction is pretty important: http://www.easeus.com/resource/fat32-disk-structure.htm – Adriano Repetti May 14 '15 at 14:37
  • I think I really suck at hex. Sorry. I didn't understand what you are saying. Can you give me a simple example in C language? – mmswe May 14 '15 at 15:58

1 Answers1

0

I'm unable to even find the root directory by following the guide. (Example 1 - find the root directory in http://www.tavi.co.uk/phobos/fat.html) It seems 0x16 and 0x17 are 00. So they don't point to anything?

The mentioned document talks about FAT16, so it doesn't apply to your FAT32 file system; refer to the Alternative resource instead.
According to that, the size of the FAT in sectors is at offset 36 (= 0x24, from the start of the standard boot record) with length 4 (in bytes); in C, you could access it (assuming there's an unsigned char *FAT32fs pointing to the boot record, and you're on a little-endian system) with e. g.

#include <stdint.h>
    uint32_t FATsectors = *(uint32_t *)&FAT32fs[36];

In your example file, this size is 0x00000603 (= 1539).

Armali
  • 18,255
  • 14
  • 57
  • 171