From 24caa6964a7c0364296a8654041ac875d4ef0ad8 Mon Sep 17 00:00:00 2001 From: Christian Taedcke Date: Wed, 15 Nov 2023 13:44:16 +0100 Subject: fs: fat: use get_unaligned_le16 to convert u8[2] to u16 This reduces code duplications. Signed-off-by: Christian Taedcke --- fs/fat/fat.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'fs') diff --git a/fs/fat/fat.c b/fs/fat/fat.c index 8ff1fd0ec83..a3522340efc 100644 --- a/fs/fat/fat.c +++ b/fs/fat/fat.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -571,7 +572,7 @@ static int get_fs_info(fsdata *mydata) mydata->total_sect = bs.total_sect; } else { mydata->fatlength = bs.fat_length; - mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0]; + mydata->total_sect = get_unaligned_le16(bs.sectors); if (!mydata->total_sect) mydata->total_sect = bs.total_sect; } @@ -583,7 +584,7 @@ static int get_fs_info(fsdata *mydata) mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats; - mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0]; + mydata->sect_size = get_unaligned_le16(bs.sector_size); mydata->clust_size = bs.cluster_size; if (mydata->sect_size != cur_part_info.blksz) { log_err("FAT sector size mismatch (fs=%u, dev=%lu)\n", @@ -607,8 +608,7 @@ static int get_fs_info(fsdata *mydata) (mydata->clust_size * 2); mydata->root_cluster = bs.root_cluster; } else { - mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 + - bs.dir_entries[0]) * + mydata->rootdir_size = (get_unaligned_le16(bs.dir_entries) * sizeof(dir_entry)) / mydata->sect_size; mydata->data_begin = mydata->rootdir_sect + -- cgit v1.3.1 From 08f622a12708a0a787c8345532e0b1665b993cab Mon Sep 17 00:00:00 2001 From: Christian Taedcke Date: Wed, 15 Nov 2023 13:44:18 +0100 Subject: fs: fat: calculate FAT type based on cluster count This fixes an issue where the FAT type (FAT12, FAT16) is not correctly detected, e.g. when the BPB field BS_FilSysType contains the valid value "FAT ". According to the FAT spec the field BS_FilSysType has only informational character and does not determine the FAT type. The logic of this code is based on the linux kernel implementation from the file fs/fat/inode.c function fat_fill_super(). For details about FAT see http://elm-chan.org/docs/fat_e.html Signed-off-by: Christian Taedcke --- fs/fat/fat.c | 48 ++++++++++++++++++++++++++++-------------------- include/fat.h | 6 ------ 2 files changed, 28 insertions(+), 26 deletions(-) (limited to 'fs') diff --git a/fs/fat/fat.c b/fs/fat/fat.c index a3522340efc..c368c3b0765 100644 --- a/fs/fat/fat.c +++ b/fs/fat/fat.c @@ -26,6 +26,9 @@ #include #include +/* maximum number of clusters for FAT12 */ +#define MAX_FAT12 0xFF4 + /* * Convert a string to lowercase. Converts at most 'len' characters, * 'len' may be larger than the length of 'str' if 'str' is NULL @@ -484,6 +487,27 @@ static __u8 mkcksum(struct nameext *nameext) return ret; } +/* + * Determine if the FAT type is FAT12 or FAT16 + * + * Based on fat_fill_super() from the Linux kernel's fs/fat/inode.c + */ +static int determine_legacy_fat_bits(const boot_sector *bs) +{ + u16 fat_start = bs->reserved; + u32 dir_start = fat_start + bs->fats * bs->fat_length; + u32 rootdir_sectors = get_unaligned_le16(bs->dir_entries) * + sizeof(dir_entry) / + get_unaligned_le16(bs->sector_size); + u32 data_start = dir_start + rootdir_sectors; + u16 sectors = get_unaligned_le16(bs->sectors); + u32 total_sectors = sectors ? sectors : bs->total_sect; + u32 total_clusters = (total_sectors - data_start) / + bs->cluster_size; + + return (total_clusters > MAX_FAT12) ? 16 : 12; +} + /* * Read boot sector and volume info from a FAT filesystem */ @@ -518,7 +542,7 @@ read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize) bs->total_sect = FAT2CPU32(bs->total_sect); /* FAT32 entries */ - if (bs->fat_length == 0) { + if (!bs->fat_length && bs->fat32_length) { /* Assume FAT32 */ bs->fat32_length = FAT2CPU32(bs->fat32_length); bs->flags = FAT2CPU16(bs->flags); @@ -529,25 +553,10 @@ read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize) *fatsize = 32; } else { vistart = (volume_info *)&(bs->fat32_length); - *fatsize = 0; + *fatsize = determine_legacy_fat_bits(bs); } memcpy(volinfo, vistart, sizeof(volume_info)); - - if (*fatsize == 32) { - if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0) - goto exit; - } else { - if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) { - *fatsize = 12; - goto exit; - } - if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) { - *fatsize = 16; - goto exit; - } - } - - debug("Error: broken fs_type sign\n"); + goto exit; fail: ret = -1; exit: @@ -1157,9 +1166,8 @@ int file_fat_detectfs(void) memcpy(vol_label, volinfo.volume_label, 11); vol_label[11] = '\0'; - volinfo.fs_type[5] = '\0'; - printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label); + printf("Filesystem: FAT%d \"%s\"\n", fatsize, vol_label); return 0; } diff --git a/include/fat.h b/include/fat.h index a9756fb4cd1..3dce99a23cf 100644 --- a/include/fat.h +++ b/include/fat.h @@ -34,12 +34,6 @@ struct disk_partition; /* Maximum number of entry for long file name according to spec */ #define MAX_LFN_SLOT 20 -/* Filesystem identifiers */ -#define FAT12_SIGN "FAT12 " -#define FAT16_SIGN "FAT16 " -#define FAT32_SIGN "FAT32 " -#define SIGNLEN 8 - /* File attributes */ #define ATTR_RO 1 #define ATTR_HIDDEN 2 -- cgit v1.3.1 From 33daef49b072f4aa7564fbbb80e7cab3629beecd Mon Sep 17 00:00:00 2001 From: Christian Taedcke Date: Wed, 15 Nov 2023 13:44:19 +0100 Subject: fs: fat: simplify gotos from read_bootsectandvi This simplifies the code a little bit. Signed-off-by: Christian Taedcke --- fs/fat/fat.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'fs') diff --git a/fs/fat/fat.c b/fs/fat/fat.c index c368c3b0765..77f225ccd8d 100644 --- a/fs/fat/fat.c +++ b/fs/fat/fat.c @@ -531,7 +531,8 @@ read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize) if (disk_read(0, 1, block) < 0) { debug("Error: reading block\n"); - goto fail; + ret = -1; + goto out_free; } memcpy(bs, block, sizeof(boot_sector)); @@ -556,10 +557,8 @@ read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize) *fatsize = determine_legacy_fat_bits(bs); } memcpy(volinfo, vistart, sizeof(volume_info)); - goto exit; -fail: - ret = -1; -exit: + +out_free: free(block); return ret; } -- cgit v1.3.1 From c489937a6f08af019f5478455ddb4e0b8ecaf6ce Mon Sep 17 00:00:00 2001 From: Christian Taedcke Date: Wed, 15 Nov 2023 13:44:20 +0100 Subject: fs: fat: add bootsector validity check The performed checks are similar to the checks performed by the Linux kernel in the function fat_read_bpb() in the file fs/fat/inode.c. Signed-off-by: Christian Taedcke --- fs/fat/fat.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'fs') diff --git a/fs/fat/fat.c b/fs/fat/fat.c index 77f225ccd8d..14e53cf2d5a 100644 --- a/fs/fat/fat.c +++ b/fs/fat/fat.c @@ -25,6 +25,7 @@ #include #include #include +#include /* maximum number of clusters for FAT12 */ #define MAX_FAT12 0xFF4 @@ -508,6 +509,52 @@ static int determine_legacy_fat_bits(const boot_sector *bs) return (total_clusters > MAX_FAT12) ? 16 : 12; } +/* + * Determines if the boot sector's media field is valid + * + * Based on fat_valid_media() from Linux kernel's include/linux/msdos_fs.h + */ +static int fat_valid_media(u8 media) +{ + return media >= 0xf8 || media == 0xf0; +} + +/* + * Determines if the given boot sector is valid + * + * Based on fat_read_bpb() from the Linux kernel's fs/fat/inode.c + */ +static int is_bootsector_valid(const boot_sector *bs) +{ + u16 sector_size = get_unaligned_le16(bs->sector_size); + u16 dir_per_block = sector_size / sizeof(dir_entry); + + if (!bs->reserved) + return 0; + + if (!bs->fats) + return 0; + + if (!fat_valid_media(bs->media)) + return 0; + + if (!is_power_of_2(sector_size) || + sector_size < 512 || + sector_size > 4096) + return 0; + + if (!is_power_of_2(bs->cluster_size)) + return 0; + + if (!bs->fat_length && !bs->fat32_length) + return 0; + + if (get_unaligned_le16(bs->dir_entries) & (dir_per_block - 1)) + return 0; + + return 1; +} + /* * Read boot sector and volume info from a FAT filesystem */ @@ -542,6 +589,12 @@ read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize) bs->heads = FAT2CPU16(bs->heads); bs->total_sect = FAT2CPU32(bs->total_sect); + if (!is_bootsector_valid(bs)) { + debug("Error: bootsector is invalid\n"); + ret = -1; + goto out_free; + } + /* FAT32 entries */ if (!bs->fat_length && bs->fat32_length) { /* Assume FAT32 */ -- cgit v1.3.1