summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2019-08-22 08:54:07 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2019-08-30 09:26:07 +0200
commit3d0bbc226abd972df3123594959ec06582671e6a (patch)
treed7c13060c3b0d0ca38de5a6ad2c9ef67bca09f0b
parentaba7cdb357e3d442359c29f7caefc49464bfd812 (diff)
downloadbarebox-3d0bbc226abd972df3123594959ec06582671e6a.tar.gz
barebox-3d0bbc226abd972df3123594959ec06582671e6a.tar.xz
ARM: i.MX: bbu: early exit when partition too small
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> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--arch/arm/mach-imx/imx-bbu-internal.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/arch/arm/mach-imx/imx-bbu-internal.c b/arch/arm/mach-imx/imx-bbu-internal.c
index a563b3bc29..946a3e9a77 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)