How could I detect what type File System Present in a Disk?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
gamingjam60
Member
Member
Posts: 70
Joined: Sat Aug 24, 2024 10:06 pm
Libera.chat IRC: gamingjam60
Location: India
GitHub: https://github.com/baponkar
Contact:

How could I detect what type File System Present in a Disk?

Post by gamingjam60 »

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

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*)&sector[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(&sector[0x52], "FAT32", 5) == 0 ||
        memcmp(&sector[0x36], "FAT32", 5) == 0)
        return VFS_FAT32;

    if (memcmp(&sector[0x36], "FAT16", 5) == 0)
        return VFS_FAT16;

    if (memcmp(&sector[0x36], "FAT12", 5) == 0)
        return VFS_FAT12;

    // --- exFAT ---
    if (memcmp(&sector[0x03], "EXFAT   ", 8) == 0)
        return VFS_EXFAT;

    // --- NTFS ---
    if (memcmp(&sector[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(&sector16[0x01], "CD001", 5) == 0)
            return VFS_ISO9660;
    }

    return VFS_RAW;
}
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

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));
}
What wrong in here?
Octocontrabass
Member
Member
Posts: 6247
Joined: Mon Mar 25, 2013 7:01 pm

Re: How could I detect what type File System Present in a Disk?

Post by Octocontrabass »

What debugging have you tried so far? I'd start by examining the disk with a hex editor to make sure it actually contains what you expect to see.
User avatar
gamingjam60
Member
Member
Posts: 70
Joined: Sat Aug 24, 2024 10:06 pm
Libera.chat IRC: gamingjam60
Location: India
GitHub: https://github.com/baponkar
Contact:

Re: How could I detect what type File System Present in a Disk?

Post by gamingjam60 »

Octocontrabass wrote: Mon Oct 06, 2025 10:56 am What debugging have you tried so far? I'd start by examining the disk with a hex editor to make sure it actually contains what you expect to see.
Hex editor may works if I want to examine the disk but how do my OS could detect Filesystem?
Octocontrabass
Member
Member
Posts: 6247
Joined: Mon Mar 25, 2013 7:01 pm

Re: How could I detect what type File System Present in a Disk?

Post by Octocontrabass »

Did you examine the disk? Does the disk contain a filesystem?

Once you're sure the disk is correct, attach a debugger to your virtual machine and start debugging your code. Which part of your code should detect the filesystem on your disk? Set a breakpoint there and step through your code to see why it doesn't detect the filesystem.
User avatar
BenLunt
Member
Member
Posts: 1029
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Re: How could I detect what type File System Present in a Disk?

Post by BenLunt »

gamingjam60 wrote: Tue Oct 07, 2025 12:20 pm
Octocontrabass wrote: Mon Oct 06, 2025 10:56 am What debugging have you tried so far? I'd start by examining the disk with a hex editor to make sure it actually contains what you expect to see.
Hex editor may works if I want to examine the disk but how do my OS could detect Filesystem?
I'm with Octocontrabass with this one. Make sure you have a valid file system on the test image before you assume your code isn't working. There are many ways to verify this.

One way would be to test your code on a known to be valid test image. There are many to try, starting here. With this, you can determine if your code works or not.

On that same page, there are utilities to create/modify/check test images. If you have a Windows system, I prefer the this one.

Once you have determined that your test image is accurate, but your code still doesn't detect the file system, you can then put your time and efforts into your code.

As for detecting the partitioning scheme and each partition's file system on the media device, I suggest a more robust check than just checking the FAT_TYPE field in the BPB. The official MS FAT specs has a section on determining the FAT type installed.

First, determine what partitioning scheme is being use. Most modern partitions use the UEFI GPT partitioning scheme. It will have a Protected MBR (PMBR) at LBA 0, then start at LBA 1 having MAGIC markers within this primary table and a matching marker in the backup table. It is good practice to check both, each pointing to one another.

If you do not find a PMBR, then checking for a MBR would be next. However, your code only checks for the first partition's type field to be non-zero, and you only check the first partition entry. There are four entries to check. Second, you should also check the "boot indicator" field, usually being 0x00 or 0x80, as well. If you wish to be even further robust, check the starting CHS value against the Starting LBA value to see if it matches.

Further tests are to see if any of the partition entries are Extended Partition entries. I once made a test to check the validity of your MBR code.

If you don't find either the PMBR or MBR, then (though it is rare on fixed media devices), you can then check for a single partition starting at LBA 0. Your code should know if it is a fixed media device or a floppy device, therefore determining the necessity to check for the PMBR and MBR.

Once you have determined where each found partition starts, it is good practice to call a routine to detect the file system, whether that routine checks each on its own, or it itself calls multiple routines, one for each known filesystem. This way, each detection can be more robust than simply checking for a string of characters at a (presumably) known location.

For example, the FAT detection routine can check for the fat size as explained in the FAT Specs, as well as determine if the sector size field is valid, the count of FATs, etc.

For interest's sake only, there were a few variants to the MBR in the past. For example, the NEC DOS 3.+ MBR had eight entries. There were a few other systems that had six and some with sixteen.

I once made my own partitioning scheme, simply to do something different, to test my partition detection scheme, and for the enjoyment of it.
Post Reply