In the rest of the FAT driver we want to be able to rely on the fact that
the cluster size is a power of two. So let's check that both the sector
size and the number of sectors per cluster are valid.

Signed-off-by: Heinrich Schuchardt <xypron.g...@gmx.de>
---
 fs/fat/fat.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index b08949d3705..0f69fa4df78 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -547,6 +547,39 @@ static int get_fs_info(fsdata *mydata)
                return ret;
        }
 
+       /* Validate sector and cluster size */
+       switch (bs.sector_size[1]) {
+       case 2:         /*  512 bytes per sector */
+       case 4:         /* 1024 bytes per sector */
+       case 8:         /* 2048 bytes per sector */
+       case 16:        /* 4096 bytes per sector */
+               break;
+       default:
+               ret =  -1;
+       }
+       if (ret || bs.sector_size[0]) {
+               debug("FAT: invalid sector size\n");
+               return -1;
+       }
+       switch (bs.cluster_size) {
+       case 1:
+       case 2:
+       case 4:
+       case 8:
+       case 16:
+       case 32:
+       case 64:
+       case 128:
+               break;
+       default:
+               ret = -1;
+       }
+       /* Check bytes per cluster <= 32768 */
+       if (ret || (u32)bs.sector_size[1] * (u32)bs.cluster_size > 128) {
+               debug("FAT: invalid cluster size\n");
+               return -1;
+       }
+
        if (mydata->fatsize == 32) {
                mydata->fatlength = bs.fat32_length;
                mydata->total_sect = bs.total_sect;
@@ -564,7 +597,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 = bs.sector_size[1] << 8;
        mydata->clust_size = bs.cluster_size;
        if (mydata->sect_size != cur_part_info.blksz) {
                printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
-- 
2.19.1

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
https://lists.denx.de/listinfo/u-boot

Reply via email to