[PATCH] scripts: bareboximd: Fix -c option

Sascha Hauer s.hauer at pengutronix.de
Mon Feb 28 02:15:23 PST 2022


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 at pengutronix.de>
Reported-by: Andrej Picej <andrej.picej at 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




More information about the barebox mailing list