fs/fat: Avoid corruption of sectors following the FAT
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v2 1/2 fs/fat: Avoid corruption of sectors following the FAT
The FAT is read/flushed in segments of 6 (FATBUFBLOCKS) disk sectors. The last segment may be less than 6 sectors, cap the length.
--- fs/fat/fat.c | 1 + fs/fat/fat_write.c | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-)
fs/fat/fat.c +1 -0
@@ -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;
fs/fat/fat_write.c +12 -10
@@ -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 ;
Signature · 1 line
2.11.0
PATCH v2 1/2 fs/fat: Avoid corruption of sectors following the FAT
Quoted 5 lines · Stefan Br?ns
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 <[email protected]>Reviewed-by Beno?t Th?baudeau <[email protected]>
Applied to u-boot/master, thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 819 bytes Desc: Digital signature URL: <http://lists.denx.de/pipermail/u-boot/attachments/20161227/4323fafe/attachment.sig>
PATCH v2 2/2 fs/fat: merge readwrite get_fatent_value() with readonly get_fatent()
In reply to a message not in the archiveget_fatent_value(...) flushes changed FAT entries to disk when fetching the next FAT blocks, in every other aspect it is identical to get_fatent(...).
Provide a stub implementation for flush_dirty_fat_buffer if CONFIG_FAT_WRITE is not set. Calling flush_dirty_fat_buffer during read only operation is fine as it checks if any buffers needs flushing.
--- fs/fat/fat.c | 25 ++++++++++-- fs/fat/fat_write.c | 118 +++-------------------------------------------------- 2 files changed, 27 insertions(+), 116 deletions(-)
fs/fat/fat.c +22 -3
@@ -162,6 +162,16 @@ static void get_name(dir_entry *dirent, char *s_name)downcase (s_name); } +static int flush_dirty_fat_buffer (fsdata *mydata); +#if !defined(CONFIG_FAT_WRITE ) +/* Stub for read only operation */ +int flush_dirty_fat_buffer (fsdata *mydata) +{ + (void )(mydata); +return 0 ; +} +#endif +/* * Get the entry at index 'entry' in a FAT (12/16/32) table. * On failure 0x00 is returned.
@@ -173,6 +183,11 @@ static __u32 get_fatent(fsdata *mydata, __u32 entry)__u32 ret =0x00 ;__u16 val1, val2; +if (CHECK_CLUST (entry, mydata->fatsize)) { +printf ("Error: Invalid FAT entry: 0x%08x\n" , entry); +return ret; + } +switch (mydata->fatsize) {case 32 : bufnum = entry /FAT32BUFSIZE ;
@@ -192,7 +207,7 @@ static __u32 get_fatent(fsdata *mydata, __u32 entry)return ret; } -debug ("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n" , +debug ("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n" , mydata->fatsize, entry, entry, offset, offset);/* Read a new block of FAT entries into the cache. */
@@ -208,6 +223,10 @@ static __u32 get_fatent(fsdata *mydata, __u32 entry) startblock += mydata->fat_sect;/* Offset from start of disk */ +/* Write back the fatbuf to the disk */ +if (flush_dirty_fat_buffer (mydata) <0 ) +return -1 ; +if (disk_read (startblock, getsize, bufptr) <0 ) {debug ("Error reading FAT blocks\n" );return ret;
@@ -254,8 +273,8 @@ static __u32 get_fatent(fsdata *mydata, __u32 entry) }break ; } -debug ("FAT%d: ret: %08x, offset: %04x\n" , - mydata->fatsize, ret, offset); +debug ("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n" , + mydata->fatsize, ret, entry, offset);return ret; }
fs/fat/fat_write.c +5 -113
154 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH v2 2/2 fs/fat: merge readwrite get_fatent_value() with readonly get_fatent()
Quoted 10 lines · Stefan Br?ns
get_fatent_value(...)flushes changed FAT entries to disk when fetching the next FAT blocks, in every other aspect it is identical toget_fatent(...).Provide a stub implementation for
flush_dirty_fat_bufferifCONFIG_FAT_WRITEis not set. Callingflush_dirty_fat_bufferduring read only operation is fine as it checks if any buffers needs flushing.Signed-off-by Stefan Br?ns <[email protected]>Reviewed-by Beno?t Th?baudeau <[email protected]>
Applied to u-boot/master, thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 819 bytes Desc: Digital signature URL: <http://lists.denx.de/pipermail/u-boot/attachments/20161227/7b93c7cb/attachment.sig>