From: Stefan Brüns <stefan.bru...@rwth-aachen.de>

The FAT is read/flushed in segments of 6 (FATBUFBLOCKS) disk sectors. The
last segment may be less than 6 sectors, cap the length.

Signed-off-by: Stefan Brüns <stefan.bru...@rwth-aachen.de>
---
 fs/fat/fat.c       |  1 +
 fs/fat/fat_write.c | 22 ++++++++++++----------
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index df9f2b5656..6319581406 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -202,6 +202,7 @@ static __u32 get_fatent(fsdata *mydata, __u32 entry)
                __u32 fatlength = mydata->fatlength;
                __u32 startblock = bufnum * FATBUFBLOCKS;
 
+               /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
                if (startblock + getsize > fatlength)
                        getsize = fatlength - startblock;
 
diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c
index 40a3860e47..0be60ecd99 100644
--- a/fs/fat/fat_write.c
+++ b/fs/fat/fat_write.c
@@ -117,10 +117,11 @@ static int flush_dirty_fat_buffer(fsdata *mydata)
        if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
                return 0;
 
-       startblock += mydata->fat_sect;
+       /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
+       if (startblock + getsize > fatlength)
+               getsize = fatlength - startblock;
 
-       if (getsize > fatlength)
-               getsize = fatlength;
+       startblock += mydata->fat_sect;
 
        /* Write FAT buf */
        if (disk_write(startblock, getsize, bufptr) < 0) {
@@ -187,8 +188,9 @@ static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
                __u32 fatlength = mydata->fatlength;
                __u32 startblock = bufnum * FATBUFBLOCKS;
 
-               if (getsize > fatlength)
-                       getsize = fatlength;
+               /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
+               if (startblock + getsize > fatlength)
+                       getsize = fatlength - startblock;
 
                startblock += mydata->fat_sect; /* Offset from start of disk */
 
@@ -494,15 +496,15 @@ static int set_fatent_value(fsdata *mydata, __u32 entry, 
__u32 entry_value)
                __u32 fatlength = mydata->fatlength;
                __u32 startblock = bufnum * FATBUFBLOCKS;
 
-               fatlength *= mydata->sect_size;
-               startblock += mydata->fat_sect;
-
-               if (getsize > fatlength)
-                       getsize = fatlength;
+               /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
+               if (startblock + getsize > fatlength)
+                       getsize = fatlength - startblock;
 
                if (flush_dirty_fat_buffer(mydata) < 0)
                        return -1;
 
+               startblock += mydata->fat_sect;
+
                if (disk_read(startblock, getsize, bufptr) < 0) {
                        debug("Error reading FAT blocks\n");
                        return -1;
-- 
2.11.0

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

Reply via email to