All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kyle Moffett <Kyle.D.Moffett@boeing.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 2/3] fs/fat: Improve error handling
Date: Wed, 21 Dec 2011 12:08:11 -0500	[thread overview]
Message-ID: <1324487292-7299-3-git-send-email-Kyle.D.Moffett@boeing.com> (raw)
In-Reply-To: <1324487292-7299-1-git-send-email-Kyle.D.Moffett@boeing.com>

The FAT filesystem fails silently in inexplicable ways when given a
filesystem with a block-size that does not match the device sector size.
In theory this is not an unsupportable combination but requires a major
rewrite of a lot of the filesystem.  Until that occurs, the filesystem
should detect that scenario and display a helpful error message.

This scenario in particular occurred on a 512-byte blocksize FAT fs
stored in an El-Torito boot volume on a CD-ROM (2048-byte sector size).

Additionally, in many circumstances the ->block_read method will not
return a negative number to indicate an error but instead return 0 to
indicate the number of blocks successfully read (IE: None).

The FAT filesystem should defensively check to ensure that it got all of
the sectors that it asked for when reading.

Signed-off-by: Kyle Moffett <Kyle.D.Moffett@boeing.com>

--

v2: No change

---
 fs/fat/fat.c |   18 ++++++++++++++----
 1 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index 9ceb3ec..afe486f 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -276,6 +276,8 @@ get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
 {
 	__u32 idx = 0;
 	__u32 startsect;
+	__u32 nr_sect;
+	int ret;
 
 	if (clustnum > 0) {
 		startsect = mydata->data_begin +
@@ -286,16 +288,19 @@ get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
 
 	debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
 
-	if (disk_read(startsect, size / mydata->sect_size, buffer) < 0) {
-		debug("Error reading data\n");
+	nr_sect = size / mydata->sect_size;
+	ret = disk_read(startsect, nr_sect, buffer);
+	if (ret != nr_sect) {
+		debug("Error reading data (got %d)\n", ret);
 		return -1;
 	}
 	if (size % mydata->sect_size) {
 		__u8 tmpbuf[mydata->sect_size];
 
 		idx = size / mydata->sect_size;
-		if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
-			debug("Error reading data\n");
+		ret = disk_read(startsect + idx, 1, tmpbuf);
+		if (ret != 1) {
+			debug("Error reading data (got %d)\n", ret);
 			return -1;
 		}
 		buffer += idx * mydata->sect_size;
@@ -804,6 +809,11 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
 
 	mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
 	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",
+				mydata->sect_size, cur_part_info.blksz);
+		return -1;
+	}
 
 	if (mydata->fatsize == 32) {
 		mydata->data_begin = mydata->rootdir_sect -
-- 
1.7.7.3

  parent reply	other threads:[~2011-12-21 17:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-21 17:08 [U-Boot] [PATCH v2 0/3] Enable access to FAT filesystem in an ISO9660 El-Torito volume Kyle Moffett
2011-12-21 17:08 ` [U-Boot] [PATCH v2 1/3] fs/fat: Fix FAT detection to support non-DOS partition tables Kyle Moffett
2012-01-05 16:14   ` Wolfgang Denk
2012-01-05 19:12   ` Wolfgang Denk
2011-12-21 17:08 ` Kyle Moffett [this message]
2012-01-05 16:15   ` [U-Boot] [PATCH v2 2/3] fs/fat: Improve error handling Wolfgang Denk
2011-12-21 17:08 ` [U-Boot] [PATCH v2 3/3] usb_storage: Fix EHCI "out of buffer pointers" with CD-ROM Kyle Moffett
2012-01-05 16:16   ` Wolfgang Denk
2012-02-26 23:08 ` [U-Boot] [PATCH v2 0/3] Enable access to FAT filesystem in an ISO9660 El-Torito volume Marek Vasut
2012-03-30 21:03   ` Wolfgang Denk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1324487292-7299-3-git-send-email-Kyle.D.Moffett@boeing.com \
    --to=kyle.d.moffett@boeing.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.