Skip to contentSkip to conversation

fs: fat: misc fix for FAT16/12

PATCH New Josh Wu 2 participants 7 messages May 8, 2014 – May 13, 2014 VFAT/FAT/msdos filesystem

lore
Share & tools
lore

Josh Wu

author
#0 lore raw

cover fs: fat: misc fix for FAT16/12

This patch series fix the bug when search files in root dir in FAT16/12.

In early time, I've sent a single patch for this:

	http://patchwork.ozlabs.org/patch/336585/

This patch series is updated version compare with the early one in above URL. It not only refined my early fix but also add other misc fixes.

Josh Wu (3):
fs/fat: add fat12 cluster check
fs: fat_write: fix the incorrect last cluster checking fs/fat: correct FAT16/12 file finding in root dir

 fs/fat/fat_write.c |   29 ++++++++++++++++++++++++++++-
 include/fat.h      |    7 ++++++-
 2 files changed, 34 insertions(+), 2 deletions(-)
Signature · 1 line
1.7.9.5
author
#1 lore raw

PATCH 1/3 fs/fat: add fat12 cluster check

---
 include/fat.h |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/fat.h +3 -1
index c8eb7cc..65da733 100644
@@ -85,7 +85,9 @@
 			+ (mydata->fatsize != 32 ? 0 : \
 			  (FAT2CPU16((dent)->starthi) << 16)))
 #define CHECK_CLUST(x, fatsize) ((x) <= 1 || \
-				(x) >= ((fatsize) != 32 ? 0xfff0 : 0xffffff0))
+				(x) >= ((fatsize) != 32 ? \
+					((fatsize) != 16 ? 0xff0 : 0xfff0) : \
+					0xffffff0))
 
 typedef struct boot_sector {
 	__u8	ignored[3];	/* Bootstrap code */
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH 1/3 fs/fat: add fat12 cluster check

Quoted 1 line · Josh

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140512/45bb8308/attachment.pgp>
author
#3 lore raw

PATCH 2/3 fs: fat_write: fix the incorrect last cluster checking

In reply to Josh Wu

In fat_write.c, the last clust condition check is incorrect:

  if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
  	... ...
  }

For example, in FAT32 if curclust is 0x11000. It is a valid clust. But on above condition check, it will be think as a last clust.

So the correct last clust check should be:
in fat32, curclust >= 0xffffff8
in fat16, curclust >= 0xfff8
in fat12, curclust >= 0xff8

This patch correct the last clust check.

---
 fs/fat/fat_write.c |    2 +-
 include/fat.h      |    3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)
fs/fat/fat_write.c +1 -1
index 9f5e911..623d11c 100644
@@ -882,7 +882,7 @@ static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
 		}
 
 		curclust = get_fatent_value(mydata, dir_curclust);
-		if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
+		if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
 			empty_dentptr = dentptr;
 			return NULL;
 		}
include/fat.h +3 -0
index 65da733..81d9790 100644
@@ -84,6 +84,9 @@
 #define START(dent)	(FAT2CPU16((dent)->start) \
 			+ (mydata->fatsize != 32 ? 0 : \
 			  (FAT2CPU16((dent)->starthi) << 16)))
+#define IS_LAST_CLUST(x, fatsize) ((x) >= ((fatsize) != 32 ? \
+					((fatsize) != 16 ? 0xff8 : 0xfff8) : \
+					0xffffff8))
 #define CHECK_CLUST(x, fatsize) ((x) <= 1 || \
 				(x) >= ((fatsize) != 32 ? \
 					((fatsize) != 16 ? 0xff0 : 0xfff0) : \
Signature · 1 line
1.7.9.5
#4 lore raw

PATCH 2/3 fs: fat_write: fix the incorrect last cluster checking

Quoted 9 lines · Josh

In fat_write.c, the last clust condition check is incorrect:

  if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
  	... ...
  }

For example, in FAT32 if curclust is 0x11000. It is a valid clust. But on above condition check, it will be think as a last clust.

Quoted 8 lines

So the correct last clust check should be:
in fat32, curclust >= 0xffffff8
in fat16, curclust >= 0xfff8
in fat12, curclust >= 0xff8

This patch correct the last clust check.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140512/ec1ee111/attachment.pgp>
author
#5 lore raw

PATCH 3/3 fs/fat: correct FAT16/12 file finding in root dir

In reply to Josh Wu

When write a file into FAT file system, it will search a match file in root dir. So the find_directory_entry() will get the first cluster of root dir content and search the directory item one by one. If the file is not found, we will call get_fatent_value() to get next cluster of root dir via lookup the FAT table and continue the search.

The issue is in FAT16/12 system, we cannot get root dir's next clust from FAT table. The FAT table only be use to find the clust of data aera in FAT16/12.

In FAT16/12 if the clust is in root dir, the clust number is a negative number or 0, 1. Since root dir is located in front of the data area. Data area start clust #2. So the root dir clust number should < 2.

This patch will check above situation before call get_fatenv_value(). If curclust is < 2, include minus number, we just increase one on the curclust since root dir is in continous cluster.

The patch also add a sanity check for entry in get_fatenv_value().

---
 fs/fat/fat_write.c |   27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
fs/fat/fat_write.c +27 -0
index 623d11c..da8902a 100644
@@ -139,6 +139,11 @@ static __u32 get_fatent_value(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;
@@ -881,6 +886,28 @@ static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
 			return dentptr;
 		}
 
+		/*
+		 * In FAT16/12, the root dir is locate before data area, shows
+		 * in following:
+		 * -------------------------------------------------------------
+		 * | Boot | FAT1 & 2 | Root dir | Data (start from cluster #2) |
+		 * -------------------------------------------------------------
+		 *
+		 * As a result if curclust is in Root dir, it is a negative
+		 * number or 0, 1.
+		 *
+		 */
+		if (mydata->fatsize != 32 && (int)curclust <= 1) {
+			/* Current clust is in root dir, set to next clust */
+			curclust++;
+			if ((int)curclust <= 1)
+				continue;	/* continue to find */
+
+			/* Reach the end of root dir */
+			empty_dentptr = dentptr;
+			return NULL;
+		}
+
 		curclust = get_fatent_value(mydata, dir_curclust);
 		if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
 			empty_dentptr = dentptr;
Signature · 1 line
1.7.9.5
#6 lore raw

PATCH 3/3 fs/fat: correct FAT16/12 file finding in root dir

Quoted 14 lines · Josh

When write a file into FAT file system, it will search a match file in root dir. So the find_directory_entry() will get the first cluster of root dir content and search the directory item one by one. If the file is not found, we will call get_fatent_value() to get next cluster of root dir via lookup the FAT table and continue the search.

The issue is in FAT16/12 system, we cannot get root dir's next clust from FAT table. The FAT table only be use to find the clust of data aera in FAT16/12.

In FAT16/12 if the clust is in root dir, the clust number is a negative number or 0, 1. Since root dir is located in front of the data area. Data area start clust #2. So the root dir clust number should < 2.

Quoted 7 lines

This patch will check above situation before call get_fatenv_value(). If curclust is < 2, include minus number, we just increase one on the curclust since root dir is in continous cluster.

The patch also add a sanity check for entry in get_fatenv_value().

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140512/26a53bfa/attachment.pgp>