mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 3/4] ARM: i.MX: bbu: early exit when partition too small
Date: Thu, 22 Aug 2019 08:54:07 +0200	[thread overview]
Message-ID: <20190822065408.29279-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20190822065408.29279-1-a.fatoum@pengutronix.de>

So far, writing a barebox image exceeding the partition size aborts with
EPERM as truncate isn't implemented:

  ERROR: writing to /dev/flash-boot.barebox failed with Operation
  not permitted
  update failed
  ERROR: fastboot: update barebox: Operation not permitted

This is unfortunate because by the time the truncation fails, erasing
the partition had already occurred. Avoid this by checking prior to the
pwrite_all whether the file to be written is big enough. This is valid
here because barebox update wouldn't be called on a regular file.

While at it, present callers with a more helpful ENOSPC error.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/arm/mach-imx/imx-bbu-internal.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/arch/arm/mach-imx/imx-bbu-internal.c b/arch/arm/mach-imx/imx-bbu-internal.c
index a563b3bc2906..946a3e9a779b 100644
--- a/arch/arm/mach-imx/imx-bbu-internal.c
+++ b/arch/arm/mach-imx/imx-bbu-internal.c
@@ -87,6 +87,7 @@ static int imx_bbu_write_device(struct imx_internal_bbu_handler *imx_handler,
 		const void *buf, int image_len)
 {
 	int fd, ret, offset = 0;
+	struct stat st;
 
 	fd = open(devicefile, O_RDWR | O_CREAT);
 	if (fd < 0)
@@ -101,6 +102,15 @@ static int imx_bbu_write_device(struct imx_internal_bbu_handler *imx_handler,
 	if (imx_handler->handler.flags & IMX_BBU_FLAG_KEEP_HEAD)
 		offset += imx_handler->flash_header_offset;
 
+	ret = fstat(fd, &st);
+	if (ret)
+		goto err_close;
+
+	if (image_len > st.st_size) {
+		ret = -ENOSPC;
+		goto err_close;
+	}
+
 	ret = imx_bbu_protect(fd, imx_handler, devicefile, offset,
 			      image_len, 0);
 	if (ret)
-- 
2.20.1


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

  parent reply	other threads:[~2019-08-22  6:54 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-22  6:54 [PATCH 1/4] startup: allow ctrl+c abort during boot sequence Ahmad Fatoum
2019-08-22  6:54 ` [PATCH 2/4] libfile: have write_full return a descriptive error code Ahmad Fatoum
2019-08-22  6:54 ` Ahmad Fatoum [this message]
2019-08-22  6:54 ` [PATCH 4/4] fs: omap4_usbbootfs: remove commented out code Ahmad Fatoum
2019-08-23  7:55 ` [PATCH 1/4] startup: allow ctrl+c abort during boot sequence 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=20190822065408.29279-3-a.fatoum@pengutronix.de \
    --to=a.fatoum@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