mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 3/3] ubiformat: handle write errors correctly
Date: Mon, 15 Jul 2019 15:54:34 +0200	[thread overview]
Message-ID: <20190715135434.5666-3-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20190715135434.5666-1-s.hauer@pengutronix.de>

This is a barebox adoption of mtd-utils commit
d9cbf6a ("ubiformat: handle write errors correctly"):

|     ubiformat: handle write errors correctly
|
|     This issue was reported and analyzed by
|     Anton Olofsson <anol.martinsson@gmail.com>:
|
|     when ubiformat encounters a write error while flashing the UBI image (which may
|     come from a file of from stdout), it correctly marks the faulty eraseblock as
|     bad and skips it. However, it also incorrectly drops the data buffer which was
|     supposed to be written, and reads next block of data.
|
|     This patch fixes this issue - in case of a write error, we preserve the current
|     data and write it to the next eraseblock, instead of dropping it.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/ubiformat.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/common/ubiformat.c b/common/ubiformat.c
index 4f0df6fd5c..655c5323ba 100644
--- a/common/ubiformat.c
+++ b/common/ubiformat.c
@@ -188,6 +188,7 @@ static int flash_image(struct ubiformat_args *args, struct mtd_info *mtd,
 		       const struct ubigen_info *ui, struct ubi_scan_info *si)
 {
 	int fd = 0, img_ebs, eb, written_ebs = 0, ret = -1, eb_cnt;
+	int skip_data_read = 0;
 	off_t st_size;
 	char *buf = NULL;
 	uint64_t lastprint = 0;
@@ -266,17 +267,20 @@ static int flash_image(struct ubiformat_args *args, struct mtd_info *mtd,
 			continue;
 		}
 
-		if (args->image) {
-			err = read_full(fd, buf, mtd->erasesize);
-			if (err < 0) {
-				sys_errmsg("failed to read eraseblock %d from image",
-					   written_ebs);
-				goto out_close;
+		if (!skip_data_read) {
+			if (args->image) {
+				err = read_full(fd, buf, mtd->erasesize);
+				if (err < 0) {
+					sys_errmsg("failed to read eraseblock %d from image",
+						written_ebs);
+					goto out_close;
+				}
+			} else {
+				memcpy(buf, inbuf, mtd->erasesize);
+				inbuf += mtd->erasesize;
 			}
-		} else {
-			memcpy(buf, inbuf, mtd->erasesize);
-			inbuf += mtd->erasesize;
 		}
+		skip_data_read = 0;
 
 		if (args->override_ec)
 			ec = args->ec;
@@ -318,6 +322,13 @@ static int flash_image(struct ubiformat_args *args, struct mtd_info *mtd,
 				goto out_close;
 			}
 
+			/*
+			 * We have to make sure that we do not read next block
+			 * of data from the input image or stdin - we have to
+			 * write buf first instead.
+			 */
+                        skip_data_read = 1;
+
 			continue;
 		}
 		if (++written_ebs >= img_ebs)
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

      parent reply	other threads:[~2019-07-15 13:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-15 13:54 [PATCH 1/3] mtd: ubi: mark PEBs as bad on erase failure Sascha Hauer
2019-07-15 13:54 ` [PATCH 2/3] mtd: peb: Do not mark as bad in mtd_peb_torture() Sascha Hauer
2019-07-15 14:48   ` Ahmad Fatoum
2019-07-15 13:54 ` Sascha Hauer [this message]

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=20190715135434.5666-3-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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