Thank you all
I've written some code (inspired by
this) that reads the boot sector from any disk. If I'm not mistaking, it supports every FAT. Now I can write a boot code installer.
Code: Select all
// This application reads FAT boot sectors from any drive and prints the results to the screen.
// Compatible with FAT, FAT12 and FAT16. Not compatible with FAT32 yet.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// this struct contains the whole 512 bytes of the boot sector
typedef struct BPB
{
unsigned char jumpCode[3];
unsigned char oemName[8];
unsigned short bytes_Sector;
unsigned char sec_Cluster;
unsigned short size_Sector_Reserved;
unsigned char fatCount;
unsigned short Max_Root_Entry;
unsigned short Total_Sector_FS;
unsigned char Media_Type;
unsigned short sectors_per_fat;
unsigned short sectors_per_track;
unsigned short total_Head_Count;
unsigned long no_Sectors_Before_Part;
unsigned long no_Sector_FS32;
unsigned char BIOS_13h_Drive_No;
unsigned char reserved;
unsigned char ext_Boot_Part_Signature;
unsigned long vol_Serial_Number;
unsigned char vol_Label_Name[11];
unsigned char FS_Type[8];
unsigned char boot_Code[448];
unsigned short signature;
} __attribute__((__packed__)) BPB;
void printbpb(BPB bpb)
{
printf("Disk Information: \n");
printf("===========================\n");
printf("Assembly Instruction to jump to Boot code: 0x%x\n",
bpb.jumpCode);
printf("OEM Name: \"%.8s\"\n", bpb.oemName); // this string may not be zero terminated!
printf("Bytes per sector: %d\n", bpb.bytes_Sector);
printf("Sector per cluster: %d\n", bpb.sec_Cluster);
printf("Size in sector for reserved area(Boot Sector): %d\n", bpb.size_Sector_Reserved);
printf("Number of FATs(File Allocation Table): %d\n", bpb.fatCount);
printf("Number of files for root directory: %d\n", bpb.Max_Root_Entry);
printf("Number of Sectors in File System: %d\n", bpb.Total_Sector_FS);
printf("Media Type: 0x%x\n", bpb.Media_Type);
printf("Number of Sectors for each FAT: %d\n", bpb.sectors_per_fat);
printf("Sectors per track: %d\n", bpb.sectors_per_track);
printf("Number of head in storage device: %d\n", bpb.total_Head_Count);
printf("BIOS INT13h Drive number: 0x%x\n", bpb.BIOS_13h_Drive_No);
printf("Number of large sectors: 0x%x\n", bpb.no_Sector_FS32);
printf("Volume Serial Number: %d\n", bpb.vol_Serial_Number);
printf("Volume label Name: \"%.11s\"\n", bpb.vol_Label_Name); // this string may not be zero terminated!
printf("File system type: \"%.8s\"\n", bpb.FS_Type); // this string may not be zero terminated!
printf("Boot Sector Signature: 0x%x\n", bpb.signature);
}
int main(int argc, char** argv)
{
FILE* drivefile;
char bootsector[512];
BPB bpb;
if (argc < 2)
{
printf("The following error occurred: Application needs disk location.\n");
return 1;
}
const char* drivename = argv[1]; // /dev/sdf1 on UNIX for example; A: on WINDOWS
drivefile = fopen(drivename, "rb"); // force binary, for WINDOWS
if (!drivefile)
{
perror ("The following error occurred");
return 2;
}
// clear buffer
memset(bootsector, 0, 512);
// read bootsector
fread(bootsector, 512, 1, drivefile);
memcpy(&bpb, bootsector, 512);
printbpb(bpb);
fclose(drivefile);
return 0;
}
I hope other people find this code useful
edit: there are still some errors in the code.. trying to fix them.
edit: I see there is no support for FAT32...
Now is the winter of my disk content.