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 2/7] ubiformat: Add ubiformat write function
Date: Thu, 11 Jan 2018 08:50:07 +0100	[thread overview]
Message-ID: <20180111075012.9050-3-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20180111075012.9050-1-s.hauer@pengutronix.de>

The ubiformat C API expects an image file as argument. With upcoming
Android fastboot sparse image support we can no longer provide a
complete image anymore. With this patch we can write an image in
multiple chunks after we've formatted a MTD device with ubiformat.

ubiformat_write will skip the EC header in both the MTD device we
write to and also the image we read from, so we can flash an image
on a MTD device containing EC headers already.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/ubiformat.c  | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/ubiformat.h |  3 +++
 2 files changed, 64 insertions(+)

diff --git a/common/ubiformat.c b/common/ubiformat.c
index aaa1f5d6bc..4c5f1f5794 100644
--- a/common/ubiformat.c
+++ b/common/ubiformat.c
@@ -679,3 +679,64 @@ out_close:
 	return err;
 }
 
+int ubiformat_write(struct mtd_info *mtd, const void *buf, size_t count,
+		    loff_t offset)
+{
+	int writesize = mtd->writesize >> mtd->subpage_sft;
+	size_t retlen;
+	int ret;
+
+	if (offset & (mtd->writesize - 1))
+		return -EINVAL;
+
+	if (count & (mtd->writesize - 1))
+		return -EINVAL;
+
+	while (count) {
+		size_t now;
+
+		now = ALIGN(offset, mtd->erasesize) - offset;
+		if (now > count)
+			now = count;
+
+		if (!now) {
+			const struct ubi_ec_hdr *ec = buf;
+			const struct ubi_vid_hdr *vid;
+
+			if (be32_to_cpu(ec->magic) != UBI_EC_HDR_MAGIC) {
+				pr_err("bad UBI magic %#08x, should be %#08x",
+					be32_to_cpu(ec->magic), UBI_EC_HDR_MAGIC);
+				return -EINVAL;
+			}
+
+			/* skip ec header */
+			offset += writesize;
+			buf += writesize;
+			count -= writesize;
+
+			if (!count)
+				break;
+
+			vid = buf;
+			if (be32_to_cpu(vid->magic) != UBI_VID_HDR_MAGIC) {
+				pr_err("bad UBI magic %#08x, should be %#08x",
+				       be32_to_cpu(vid->magic), UBI_VID_HDR_MAGIC);
+				return -EINVAL;
+			}
+
+			continue;
+		}
+
+		ret = mtd_write(mtd, offset, now, &retlen, buf);
+		if (ret < 0)
+			return ret;
+		if (retlen != now)
+			return -EIO;
+
+		buf += now;
+		count -= now;
+		offset += now;
+	}
+
+	return 0;
+}
diff --git a/include/ubiformat.h b/include/ubiformat.h
index b195fd8392..8305a853c7 100644
--- a/include/ubiformat.h
+++ b/include/ubiformat.h
@@ -20,4 +20,7 @@ struct ubiformat_args {
 
 int ubiformat(struct mtd_info *mtd, struct ubiformat_args *args);
 
+int ubiformat_write(struct mtd_info *mtd, const void *buf, size_t count,
+		    loff_t offset);
+
 #endif /* __UBIFORMAT_H */
-- 
2.11.0


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

  parent reply	other threads:[~2018-01-11  7:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-11  7:50 fastboot sparse support Sascha Hauer
2018-01-11  7:50 ` [PATCH 1/7] fs: implement ftruncate Sascha Hauer
2018-01-11  7:50 ` Sascha Hauer [this message]
2018-01-11  7:50 ` [PATCH 3/7] Documentation: USB gadget: Add section for partition description Sascha Hauer
2018-01-11  7:50 ` [PATCH 4/7] filetype: Add fastboot sparse format detection Sascha Hauer
2018-01-11  7:50 ` [PATCH 5/7] Add support for fastboot sparse images Sascha Hauer
2018-01-11  7:50 ` [PATCH 6/7] file_list: Add ubi flag Sascha Hauer
2018-01-11  7:50 ` [PATCH 7/7] usb: gadget: fastboot: Add sparse image support Sascha Hauer
2018-01-12  8:03   ` [PATCH] " Sascha Hauer

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=20180111075012.9050-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