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.