2

I am writing a program and I need to access some information in the boot sector about the FAT32 file system that I have mounted.

Here is what I did, fully commented.

int main (void) {
    unsigned short *i;                  //2 byte unsigned integer pointer
    char tmp[1024];                     //buffer
    int fd;                             //file descriptor from opening device
    fd =  open("/dev/sdf", O_RDONLY);   //open the device file
    lseek(fd, 14, SEEK_SET);            //set offset to 14 (0x0E), i.e. storing "no. of reserved sectors" of the FAT32 system
    read(fd, tmp, 2);                   //read off 2 bytes, "no. of reserved sectors" is represented by 2 bytes
    i = &tmp;                           //point j at those 2 bytes
    printf("*j: %d\n", *j);             //print *j out
    close(fd);                          //close device
    return 0;
}

The output of *i is 38, which is nonsense. I formatted the file system with mkfs.vfat. I set the "no. of reserved sectors" to 32.

What I have tried:

  • i = (unsigned short *) &tmp, do a casting, this removes the warning when I compile, but doesn't help

  • read(fd, tmp, 512), load the whole boot sector (512 bytes) into tmp, then read from the buffer, but doesn't help, result still 38 though.

  • fiddle with the offset, i.e. change 14 to 13 or 15, in case I get the index wrong. It prints out 9744 for 13 and 512 for 15 respectively, so doesn't work.

I'm not sure whether I'm doing it the right way. Can someone please point me in the right direction?

Thanks in advance,

Felastine.

Felastine
  • 793
  • 2
  • 8
  • 18
  • You're opening `/dev/sdf`, which is the whole disk. Did you try opening only the partition you formatted as FAT32, e.g. `/dev/sdf1`? – Frédéric Hamidi Dec 21 '11 at 10:29
  • What makes you think that 38 is wrong? It sounds like a sensible number for this field to me. Is it possible that mkfs.vfat is not doing what you tell it to do? – Stewart Dec 21 '11 at 10:29
  • 1
    @FrédéricHamidi Actually, "/dev/sdf", the whole disk, is in FAT32. – Felastine Dec 21 '11 at 10:34
  • @Stewart I did a dosfsck check on the disk I'm opening. It said 32. So I formatted it to have 32 reserved sectors. dosfsck verified that it has 32 reserved sectors. It's only possible that something's wrong with the code... – Felastine Dec 21 '11 at 10:36
  • 1
    My mental compiler is having trouble with your code snippet. I can't see the definition of j. Wherever it is, it is presumably a char* judging by the assignment from &tmp, which is a bit odd since it doesn't match the comment. – Stewart Dec 21 '11 at 20:03
  • @Stewart Ooops. Sorry. That j should be i. So I'm pointing i to &tmp. – Felastine Dec 22 '11 at 05:04

2 Answers2

0

Try running:

$ dd if=/dev/sdf of=/tmp/x.bin bs=512 count=1

And then:

$ hd /tmp/x.bin

Or

$ od -tx2 /tmp/x.bin

And post the first lines.

Chances are that your fattools are adding 6 extra reserved sectors of their own. And then they substract them before showing the data.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
-1
unsigned short *i;                  //2 byte unsigned integer pointer
char tmp[1024];  
 [...] 
i = &tmp;                           //point j at those 2 bytes

tmp is char[], &tmp something of order char**. Think again, you don't want the & here.

J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84