From: "Benoît Thébaudeau" <benoit@wsystem.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 1/5] fs/fat/fat_write: Fix buffer alignments
Date: Mon, 28 Sep 2015 15:45:28 +0200 [thread overview]
Message-ID: <1443447932-14139-1-git-send-email-benoit@wsystem.com> (raw)
In-Reply-To: <807994631.1713509.1365795559751.JavaMail.root@advansee.com>
set_cluster() was using a temporary buffer without enforcing its
alignment for DMA and cache. Moreover, it did not check the alignment of
the passed buffer, which can come directly from applicative code or from
the user.
This could cause random data corruption, which has been observed on
i.MX25 writing to an SD card.
Fix this by only passing ARCH_DMA_MINALIGN-aligned buffers to
disk_write(), which requires the introduction of a buffer bouncing
mechanism for the misaligned buffers passed to set_cluster().
By the way, improve the handling of the corresponding return values from
disk_write():
- print them with debug() in case of error,
- consider that there is an error is disk_write() returns a smaller
block count than the requested one, not only if its return value is
negative.
After this change, set_cluster() and get_cluster() are almost
symmetrical.
Signed-off-by: Beno?t Th?baudeau <benoit@wsystem.com>
---
fs/fat/fat_write.c | 48 ++++++++++++++++++++++++++++++++++--------------
1 file changed, 34 insertions(+), 14 deletions(-)
diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c
index adb6940..d0d9df7 100644
--- a/fs/fat/fat_write.c
+++ b/fs/fat/fat_write.c
@@ -555,8 +555,9 @@ static int
set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
unsigned long size)
{
- int idx = 0;
+ __u32 idx = 0;
__u32 startsect;
+ int ret;
if (clustnum > 0)
startsect = mydata->data_begin +
@@ -566,26 +567,45 @@ set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
- if ((size / mydata->sect_size) > 0) {
- if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) {
- debug("Error writing data\n");
+ if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
+ ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
+
+ printf("FAT: Misaligned buffer address (%p)\n", buffer);
+
+ while (size >= mydata->sect_size) {
+ memcpy(tmpbuf, buffer, mydata->sect_size);
+ ret = disk_write(startsect++, 1, tmpbuf);
+ if (ret != 1) {
+ debug("Error writing data (got %d)\n", ret);
+ return -1;
+ }
+
+ buffer += mydata->sect_size;
+ size -= mydata->sect_size;
+ }
+ } else if (size >= mydata->sect_size) {
+ idx = size / mydata->sect_size;
+ ret = disk_write(startsect, idx, buffer);
+ if (ret != idx) {
+ debug("Error writing data (got %d)\n", ret);
return -1;
}
+
+ startsect += idx;
+ idx *= mydata->sect_size;
+ buffer += idx;
+ size -= idx;
}
- if (size % mydata->sect_size) {
- __u8 tmpbuf[mydata->sect_size];
+ if (size) {
+ ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
- idx = size / mydata->sect_size;
- buffer += idx * mydata->sect_size;
- memcpy(tmpbuf, buffer, size % mydata->sect_size);
-
- if (disk_write(startsect + idx, 1, tmpbuf) < 0) {
- debug("Error writing data\n");
+ memcpy(tmpbuf, buffer, size);
+ ret = disk_write(startsect, 1, tmpbuf);
+ if (ret != 1) {
+ debug("Error writing data (got %d)\n", ret);
return -1;
}
-
- return 0;
}
return 0;
--
2.1.4
next prev parent reply other threads:[~2015-09-28 13:45 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-04-12 12:46 [U-Boot] fatwrite problem Ruud Commandeur
2013-04-12 14:11 ` Mats Kärrman
2013-04-12 15:06 ` Ruud Commandeur
2013-04-12 15:12 ` Tom Rini
2013-04-12 15:23 ` Ruud Commandeur
2013-04-12 15:43 ` Tom Rini
2013-04-12 19:39 ` Benoît Thébaudeau
2013-04-12 20:42 ` Tom Rini
2013-04-12 21:17 ` Benoît Thébaudeau
2013-04-16 9:32 ` Ruud Commandeur
2013-05-14 15:13 ` Ruud Commandeur
2013-05-14 15:31 ` Tom Rini
2015-09-28 13:45 ` Benoît Thébaudeau [this message]
2015-09-28 13:45 ` [U-Boot] [PATCH 2/5] fs/fat/fat_write: Merge calls to set_cluster() Benoît Thébaudeau
2015-10-12 15:15 ` [U-Boot] [U-Boot, " Tom Rini
2015-09-28 13:45 ` [U-Boot] [PATCH 3/5] fs/fat/fat_write: Fix curclust/newclust mix-up Benoît Thébaudeau
2015-10-12 15:15 ` [U-Boot] [U-Boot, " Tom Rini
2015-09-28 13:45 ` [U-Boot] [PATCH 4/5] fs/fat/fat_write: Factor out duplicate code Benoît Thébaudeau
2015-10-12 15:15 ` [U-Boot] [U-Boot, " Tom Rini
2015-09-28 13:45 ` [U-Boot] [PATCH 5/5] fs/fat/fat_write: Fix management of empty files Benoît Thébaudeau
2015-10-12 15:15 ` [U-Boot] [U-Boot, " Tom Rini
2015-09-28 15:22 ` [U-Boot] [PATCH 1/5] fs/fat/fat_write: Fix buffer alignments Tom Rini
2015-10-07 19:48 ` Benoît Thébaudeau
2015-10-12 15:15 ` [U-Boot] [U-Boot,1/5] " Tom Rini
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=1443447932-14139-1-git-send-email-benoit@wsystem.com \
--to=benoit@wsystem.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox