summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUwe Kleine-König <u.kleine-koenig@pengutronix.de>2016-07-13 14:37:26 +0200
committerUwe Kleine-König <u.kleine-koenig@pengutronix.de>2016-07-14 09:07:41 +0200
commite4415f32afe5589d29e2bea96d1b8ea07cc00f3c (patch)
treeefd9452d0bd4c6295a072eb9b2289a9c20ca3e6e
parent7ef16721acad7cfaabd3bf187e945caf0a938f16 (diff)
downloadmemtool-e4415f32afe5589d29e2bea96d1b8ea07cc00f3c.tar.gz
memtool-e4415f32afe5589d29e2bea96d1b8ea07cc00f3c.tar.xz
open source file for md command readonly
When read-write mode is used, using md on readonly files fails with permission denied. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
-rw-r--r--memtool.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/memtool.c b/memtool.c
index d0b957d..a6c9d8a 100644
--- a/memtool.c
+++ b/memtool.c
@@ -196,7 +196,7 @@ static int memory_display(const void *addr, off_t offs,
static int memfd;
-static void *memmap(const char *file, off_t addr, size_t size)
+static void *memmap(const char *file, off_t addr, size_t size, int readonly)
{
off_t mmap_start;
size_t ofs;
@@ -206,7 +206,7 @@ static void *memmap(const char *file, off_t addr, size_t size)
if (pagesize < 0)
pagesize = 4096;
- memfd = open(file, O_RDWR);
+ memfd = open(file, readonly ? O_RDONLY : O_RDWR);
if (memfd < 0) {
perror("open");
exit(1);
@@ -215,8 +215,8 @@ static void *memmap(const char *file, off_t addr, size_t size)
mmap_start = addr & ~((off_t)pagesize - 1);
ofs = addr - mmap_start;
- mem = mmap(NULL, size + ofs, PROT_READ | PROT_WRITE, MAP_SHARED,
- memfd, mmap_start);
+ mem = mmap(NULL, size + ofs, PROT_READ | (readonly ? 0 : PROT_WRITE),
+ MAP_SHARED, memfd, mmap_start);
if (mem == MAP_FAILED) {
perror("mmap");
goto out;
@@ -299,7 +299,7 @@ static int cmd_memory_display(int argc, char **argv)
size = 0x100;
}
- mem = memmap(file, start, size);
+ mem = memmap(file, start, size, 1);
memory_display(mem, start, size, width, swap);
@@ -362,7 +362,7 @@ static int cmd_memory_write(int argc, char *argv[])
adr = strtoull_suffix(argv[optind++], NULL, 0);
- mem = memmap(file, adr, argc * width);
+ mem = memmap(file, adr, argc * width, 0);
if (!mem)
return 1;