0

I have disk.img which is a fat32 file system in Linux. I want to reach its FAT and figure out used blocks for each file. I've read http://wiki.osdev.org/FAT#FAT_32_3 but did not understand how to reach the table. Maybe I'm missing something.

How can I reach the FAT?

2 Answers2

0

You need to read the boot sector. Once you have that information http://wiki.osdev.org/FAT#Reading_the_Boot_Sector tells how to reach the FAT:

The first sector in the File Allocation Table: first_fat_sector = fat_boot->reserved_sector_count;

Paul V
  • 123
  • 1
  • 6
0

This is an example taken from video. Video link is under this code.

#include <stdio.h>
#include <stdbool.h>
// The stdbool.h is a POSIX library that defines type bool, and constants true and false that stands for 1 and 0 consecutively. 

typedef struct {
    uint8_t BootJumpInstruction[3];
    uint8_t OemIdentifier[8];
    uint16_t BytesPerSector;
    uint8_t SectorsPerCluster;
    uint16_t ReservedSectors;
    uint8_t FatCount;
    uint16_t DirEntryCount;
    uint16_t TotalSectors;
    uint8_t MediaDescriptorType;
    uint16_t SectorsPerFat;
    uint16_t SectorsPerTrack;
    uint16_t Heads;
    uint32_t HiddenSectors;
    uint32_t LargeSectors;

    // extended boot record
    uint8_t DriveNumber;
    uint8_t WindowsNTFlags;
    uint8_t Signature;
    uint32_t VolumeId;
    uint8_t VolumeLabel[11];
    uint8_t SystemIdentifier[8];
} __attribute__((packed)) BootSector;
/* (Note: here can be found `__attribute__((packed))`, it works for gcc, for other compilers and/or debuggers - IDK (I don't know) ... */

BootSector g_BootSector;
uint8_t *g_Fat = NULL;

bool readBootSector(FILE *disk)
{
    return fread(&g_BootSector, sizeof(g_BootSector), 1, disk);
}

bool readSectors(FILE *disk, uint32_t lba, uint32_t count, void *bufferOut)
{
    bool ok = true;
    ok = ok && (fseek(disk, lba * g_BootSector.BytesPerSector, SEEK_SET == 0));
    ok = ok && (fread(bufferOut, g_BootSector.BytesPerSector, count, disk) == count);
    return ok;
}

bool readFat(FILE *disk)
{
    g_Fat = (uint8_t *)malloc(g_BootSector.SectorsPerFat * g_BootSector.BytesPerSector);
    return readSectors(disk, g_BootSector.ReservedSectors, g_BootSector.SectorsPerFat, g_Fat);
}

int main(int argc, char **argv)
{

    if (argc < 3)
    {

        printf("Syntax: %s <disk image> <file name>\n", argv[0]);
        return -1;

    }

    FILE *disk = fopen(argv[1], "rb");

    if (!disk)
    {

        fprintf(stderr, "Cannot open disk image %s!", argv[1]);
        return -1;

    }

    if (!readBootSector(disk))
    {

        fprintf(stderr, "Could not read boot sector!");
        return -2;

    }

    if (!readFat(disk))
    {

        fprintf(stderr, "Could not read FAT!");
        free(g_Fat);
        return -3;

    }

    free(g_Fat);
    return 0;
}

Here is a video (really it's a playlist, the needed video for your case is number 3, about FAT filesystem) that I am watching and making an operating system.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Link only answers are not acceptable on SO. To attract votes/accept, you actually have to answer the question. You could take what _you_ have learned from that video, and use it to _answer_ the question. As a new contributor, I see no advantage in downvoting this answer, but others are likely to do so. – Clifford Aug 26 '23 at 20:42