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>
Cc: Andrej Picej <andrej.picej@norik.com>
Subject: [PATCH] scripts: bareboximd: Fix -c option
Date: Mon, 28 Feb 2022 11:15:23 +0100	[thread overview]
Message-ID: <20220228101523.1058032-1-s.hauer@pengutronix.de> (raw)

Using mmap in read_file doesn't work when the same file is written
afterwards with write_file. This problem was fixed in 738601e125
("scripts/common: fix write_file when opened with mmap"). Using mmap in
read_file was removed in 07b87a0908 ("scripts/common: Do not mmap in
read_file_2()") but then re-introduced for bareboximd in 013e8ea757
("scripts: bareboximd: Use mmap when possible"). I'll put my brown paper
bag on for this :(

This patch fixes the issue by explicitly avoiding mmap when the file
is written later as done with the -c option to bareboximd

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Reported-by: Andrej Picej <andrej.picej@norik.com>
---
 common/imd.c         | 7 +++++--
 scripts/bareboximd.c | 9 ++++++---
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/common/imd.c b/common/imd.c
index 9ca0248523..1100e6878a 100644
--- a/common/imd.c
+++ b/common/imd.c
@@ -22,7 +22,8 @@ static inline void read_file_2_free(void *buf)
 	free(buf);
 }
 
-static int imd_read_file(const char *filename, size_t *size, void **outbuf)
+static int imd_read_file(const char *filename, size_t *size, void **outbuf,
+			 bool allow_mmap)
 {
 	return read_file_2(filename, size, outbuf, 0x100000);
 }
@@ -439,6 +440,7 @@ int imd_command(int argc, char *argv[])
 	char *str;
 	uint32_t checksum = 0;
 	uint32_t verify = 0;
+	bool allow_mmap = true;
 
 	imd_command_verbose = 0;
 
@@ -462,6 +464,7 @@ int imd_command(int argc, char *argv[])
 			break;
 		case 'c':
 			checksum = 1;
+			allow_mmap = false;
 			break;
 		case 'V':
 			verify = 1;
@@ -478,7 +481,7 @@ int imd_command(int argc, char *argv[])
 
 	filename = argv[optind];
 
-	ret = imd_read_file(filename, &size, &buf);
+	ret = imd_read_file(filename, &size, &buf, allow_mmap);
 	if (ret && ret != -EFBIG)
 		return -errno;
 
diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c
index 2d4750d7fb..d01bd27007 100644
--- a/scripts/bareboximd.c
+++ b/scripts/bareboximd.c
@@ -55,9 +55,10 @@ static unsigned long simple_strtoul(const char *cp, char **endp, unsigned int ba
 	return strtoul(cp, endp, base);
 }
 
-static int imd_read_file(const char *filename, size_t *size, void **outbuf)
+static int imd_read_file(const char *filename, size_t *size, void **outbuf,
+			 bool allow_mmap)
 {
-	void *buf;
+	void *buf = MAP_FAILED;
 	int fd, ret;
 	size_t fsize;
 
@@ -74,7 +75,9 @@ static int imd_read_file(const char *filename, size_t *size, void **outbuf)
 		goto close;
 	}
 
-	buf = mmap(NULL, fsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+	if (allow_mmap)
+		buf = mmap(NULL, fsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+
 	if (buf == MAP_FAILED) {
 		close(fd);
 		return read_file_2(filename, size, outbuf, 0x100000);
-- 
2.30.2


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


             reply	other threads:[~2022-02-28 10:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-28 10:15 Sascha Hauer [this message]
2022-02-28 11:48 ` Andrej Picej

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=20220228101523.1058032-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=andrej.picej@norik.com \
    --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