How could I detect what type File System Present in a Disk?
Posted: Mon Oct 06, 2025 7:43 am
Suppose a Disk either have MBR/GPT Table and File System Table or Only File System Table. Now I want to know how could I get information about the File System information present in the Disk. I have following function
I am using FATFS library to format the disk with FAT32 filesystem and want to get information about the filesystem present in the disk but getting RAW Disk before and after Format the disk by using below fatfs powered function
What wrong in here?
Code: Select all
FS_TYPE detect_filesystem(int disk_no) {
uint8_t sector[SECTOR_SIZE];
// --- Step 1: read LBA 0 (MBR or Boot Sector) ---
if (!kebla_disk_read(disk_no, 0, 1, sector))
return VFS_UNKNOWN;
// --- Step 2: check MBR signature (0x55AA) ---
bool has_mbr = (sector[510] == 0x55 && sector[511] == 0xAA);
if (has_mbr && sector[0x1BE + 4] != 0x00) {
// Partition type field
uint8_t ptype = sector[0x1BE + 4];
uint32_t start_lba = *(uint32_t*)§or[0x1BE + 8];
// Read first sector of partition
uint8_t pboot[SECTOR_SIZE];
if (kebla_disk_read(disk_no, start_lba, 1, pboot)) {
if (memcmp(&pboot[0x52], "FAT32", 5) == 0 ||
memcmp(&pboot[0x36], "FAT32", 5) == 0)
return VFS_FAT32;
if (memcmp(&pboot[0x36], "FAT16", 5) == 0)
return VFS_FAT16;
if (memcmp(&pboot[0x36], "FAT12", 5) == 0)
return VFS_FAT12;
}
}
// --- Step 3: Check raw boot sector (superfloppy case) ---
if (memcmp(§or[0x52], "FAT32", 5) == 0 ||
memcmp(§or[0x36], "FAT32", 5) == 0)
return VFS_FAT32;
if (memcmp(§or[0x36], "FAT16", 5) == 0)
return VFS_FAT16;
if (memcmp(§or[0x36], "FAT12", 5) == 0)
return VFS_FAT12;
// --- exFAT ---
if (memcmp(§or[0x03], "EXFAT ", 8) == 0)
return VFS_EXFAT;
// --- NTFS ---
if (memcmp(§or[0x03], "NTFS ", 8) == 0)
return VFS_NTFS;
// --- ext2/3/4 (superblock at 1024 bytes) ---
uint8_t extbuf[1024 + SECTOR_SIZE];
if (kebla_disk_read(disk_no, 2, 2, extbuf)) {
uint16_t magic = *(uint16_t*)&extbuf[1024 + 0x38];
if (magic == 0xEF53) return VFS_EXT2;
}
// --- ISO9660 ---
uint8_t sector16[SECTOR_SIZE];
if (kebla_disk_read(disk_no, 16, 1, sector16)) {
if (memcmp(§or16[0x01], "CD001", 5) == 0)
return VFS_ISO9660;
}
return VFS_RAW;
}
Code: Select all
int fatfs_mkfs(int disk_no, int fs_type){
MKFS_PARM opt;
opt.fmt = fs_type; // FAT32
opt.n_fat = 1; // One FAT
opt.align = 0; // Default alignment
opt.n_root = 0; // Not used for FAT32
opt.au_size= 0; // Auto cluster size
BYTE work[4096]; // 4K buffer, enough for 512-byte sectors
char root_path[16];
sprintf(root_path, "%d:",disk_no);
return f_mkfs(root_path, &opt, work, sizeof(work));
}