<div dir="ltr"><div dir="ltr"><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 24, 2024 at 7:45 AM enh <<a href="mailto:enh@google.com">enh@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Oct 23, 2024 at 7:02 PM Karthikeyan Ramasubramanian <<a href="mailto:kramasub@google.com" target="_blank">kramasub@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Add --no-mmap flag to indicate seek and read/write access. This allows<br>
accessing devices that do not support mapping into memory - eg.<br>
/dev/nvram, /dev/msr0 etc.<br>
<br>
Also currently only WIDTH bytes are mapped into memory even when more<br>
data is accessed. Fix this by mapping WIDTH * number of data.<br>
<br>
Test: ./post_update.sh && m toybox. Push devmem test into DUT and access<br>
/dev/mem through memory mapped access, /dev/nvram & /dev/msr* through<br>
non memory-mapped access.<br>
<br>
---<br>
android/device/generated/flags.h | 8 +++--<br>
android/device/generated/help.h | 2 +-<br>
android/device/generated/newtoys.h | 2 +-<br>
android/linux/generated/flags.h | 8 +++--<br>
android/linux/generated/help.h | 2 +-<br>
android/linux/generated/newtoys.h | 2 +-<br>
android/mac/generated/flags.h | 8 +++--<br>
android/mac/generated/help.h | 2 +-<br>
android/mac/generated/newtoys.h | 2 +-<br></blockquote><div><br></div><div>(these files only exist in AOSP ... sorry, i should have said to apply your devmem.c change to a fresh `git clone` of upstream!)</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
toys/other/devmem.c | 49 +++++++++++++++++++-----------<br>
10 files changed, 53 insertions(+), 32 deletions(-)<br>
<br>
diff --git a/android/device/generated/flags.h b/android/device/generated/flags.h<br>
index 9c056073..aa13b2c6 100644<br>
--- a/android/device/generated/flags.h<br>
+++ b/android/device/generated/flags.h<br>
@@ -641,13 +641,14 @@<br>
#undef FOR_demo_utf8towc<br>
#endif<br>
<br>
-// devmem <1(no-sync)f: <1(no-sync)f:<br>
+// devmem <1(no-sync)(no-mmap)f: <1(no-sync)(no-mmap)f:<br>
#undef OPTSTR_devmem<br>
-#define OPTSTR_devmem "<1(no-sync)f:"<br>
+#define OPTSTR_devmem "<1(no-sync)(no-mmap)f:"<br>
#ifdef CLEANUP_devmem<br>
#undef CLEANUP_devmem<br>
#undef FOR_devmem<br>
#undef FLAG_f<br>
+#undef FLAG_no_mmap<br>
#undef FLAG_no_sync<br>
#endif<br>
<br>
@@ -4547,7 +4548,8 @@<br>
#define TT this.devmem<br>
#endif<br>
#define FLAG_f (1LL<<0)<br>
-#define FLAG_no_sync (1LL<<1)<br>
+#define FLAG_no_mmap (1LL<<1)<br>
+#define FLAG_no_sync (1LL<<2)<br>
#endif<br>
<br>
#ifdef FOR_df<br>
diff --git a/android/device/generated/help.h b/android/device/generated/help.h<br>
index 92dd3687..87f92453 100644<br>
--- a/android/device/generated/help.h<br>
+++ b/android/device/generated/help.h<br>
@@ -330,7 +330,7 @@<br>
<br>
#define HELP_dos2unix "usage: dos2unix [FILE...]\n\nConvert newline format from dos \"\\r\\n\" to unix \"\\n\".\nIf no files listed copy from stdin, \"-\" is a synonym for stdin."<br>
<br>
-#define HELP_devmem "usage: devmem [-f FILE] ADDR [WIDTH [DATA...]]\n\nRead/write physical addresses. WIDTH is 1, 2, 4, or 8 bytes (default 4).\nPrefix ADDR with 0x for hexadecimal, output is in same base as address.\n\n-f FILE File to operate on (default /dev/mem)\n--no-sync Don't open the file with O_SYNC (for cached access)"<br>
+#define HELP_devmem "usage: devmem [-f FILE] ADDR [WIDTH [DATA...]]\n\nRead/write physical addresses. WIDTH is 1, 2, 4, or 8 bytes (default 4).\nPrefix ADDR with 0x for hexadecimal, output is in same base as address.\n\n-f FILE File to operate on (default /dev/mem)\n--no-sync Don't open the file with O_SYNC (for cached access)\n--no-mmap Don't mmap the file"<br>
<br>
#define HELP_count "usage: count [-l]\n\n-l Long output (total bytes, human readable, transfer rate, elapsed time)\n\nCopy stdin to stdout, displaying simple progress indicator to stderr."<br>
<br>
diff --git a/android/device/generated/newtoys.h b/android/device/generated/newtoys.h<br>
index c41eb83f..e99f4f1c 100644<br>
--- a/android/device/generated/newtoys.h<br>
+++ b/android/device/generated/newtoys.h<br>
@@ -60,7 +60,7 @@ USE_DEMO_MANY_OPTIONS(NEWTOY(demo_many_options, "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwv<br>
USE_DEMO_NUMBER(NEWTOY(demo_number, "D#=3<3M#<0hcdbs", TOYFLAG_BIN))<br>
USE_DEMO_SCANKEY(NEWTOY(demo_scankey, 0, TOYFLAG_BIN))<br>
USE_DEMO_UTF8TOWC(NEWTOY(demo_utf8towc, 0, TOYFLAG_USR|TOYFLAG_BIN))<br>
-USE_DEVMEM(NEWTOY(devmem, "<1(no-sync)f:", TOYFLAG_USR|TOYFLAG_SBIN))<br>
+USE_DEVMEM(NEWTOY(devmem, "<1(no-sync)(no-mmap)f:", TOYFLAG_USR|TOYFLAG_SBIN))<br>
USE_DF(NEWTOY(df, "HPkhit*a[-HPh]", TOYFLAG_BIN))<br>
USE_DHCP(NEWTOY(dhcp, "V:H:F:x*r:O*A#<0=20T#<0=3t#<0=3s:p:i:SBRCaovqnbf", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))<br>
USE_DHCP6(NEWTOY(dhcp6, "r:A#<0T#<0t#<0s:p:i:SRvqnbf", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))<br>
diff --git a/android/linux/generated/flags.h b/android/linux/generated/flags.h<br>
index e73e4bf8..27fcd60d 100644<br>
--- a/android/linux/generated/flags.h<br>
+++ b/android/linux/generated/flags.h<br>
@@ -641,13 +641,14 @@<br>
#undef FOR_demo_utf8towc<br>
#endif<br>
<br>
-// devmem <1(no-sync)f:<br>
+// devmem <1(no-sync)(no-mmap)f:<br>
#undef OPTSTR_devmem<br>
-#define OPTSTR_devmem "<1(no-sync)f:"<br>
+#define OPTSTR_devmem "<1(no-sync)(no-mmap)f:"<br>
#ifdef CLEANUP_devmem<br>
#undef CLEANUP_devmem<br>
#undef FOR_devmem<br>
#undef FLAG_f<br>
+#undef FLAG_no_mmap<br>
#undef FLAG_no_sync<br>
#endif<br>
<br>
@@ -4547,7 +4548,8 @@<br>
#define TT this.devmem<br>
#endif<br>
#define FLAG_f (FORCED_FLAG<<0)<br>
-#define FLAG_no_sync (FORCED_FLAG<<1)<br>
+#define FLAG_no_mmap (FORCED_FLAG<<1)<br>
+#define FLAG_no_sync (FORCED_FLAG<<2)<br>
#endif<br>
<br>
#ifdef FOR_df<br>
diff --git a/android/linux/generated/help.h b/android/linux/generated/help.h<br>
index f25a0785..eccc159a 100644<br>
--- a/android/linux/generated/help.h<br>
+++ b/android/linux/generated/help.h<br>
@@ -332,7 +332,7 @@<br>
<br>
#define HELP_dos2unix "usage: dos2unix [FILE...]\n\nConvert newline format from dos \"\\r\\n\" to unix \"\\n\".\nIf no files listed copy from stdin, \"-\" is a synonym for stdin."<br>
<br>
-#define HELP_devmem "usage: devmem [-f FILE] ADDR [WIDTH [DATA...]]\n\nRead/write physical addresses. WIDTH is 1, 2, 4, or 8 bytes (default 4).\nPrefix ADDR with 0x for hexadecimal, output is in same base as address.\n\n-f FILE File to operate on (default /dev/mem)\n--no-sync Don't open the file with O_SYNC (for cached access)"<br>
+#define HELP_devmem "usage: devmem [-f FILE] ADDR [WIDTH [DATA...]]\n\nRead/write physical addresses. WIDTH is 1, 2, 4, or 8 bytes (default 4).\nPrefix ADDR with 0x for hexadecimal, output is in same base as address.\n\n-f FILE File to operate on (default /dev/mem)\n--no-sync Don't open the file with O_SYNC (for cached access)\n--no-mmap Don't mmap the file"<br>
<br>
#define HELP_count "usage: count [-l]\n\n-l Long output (total bytes, human readable, transfer rate, elapsed time)\n\nCopy stdin to stdout, displaying simple progress indicator to stderr."<br>
<br>
diff --git a/android/linux/generated/newtoys.h b/android/linux/generated/newtoys.h<br>
index c41eb83f..e99f4f1c 100644<br>
--- a/android/linux/generated/newtoys.h<br>
+++ b/android/linux/generated/newtoys.h<br>
@@ -60,7 +60,7 @@ USE_DEMO_MANY_OPTIONS(NEWTOY(demo_many_options, "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwv<br>
USE_DEMO_NUMBER(NEWTOY(demo_number, "D#=3<3M#<0hcdbs", TOYFLAG_BIN))<br>
USE_DEMO_SCANKEY(NEWTOY(demo_scankey, 0, TOYFLAG_BIN))<br>
USE_DEMO_UTF8TOWC(NEWTOY(demo_utf8towc, 0, TOYFLAG_USR|TOYFLAG_BIN))<br>
-USE_DEVMEM(NEWTOY(devmem, "<1(no-sync)f:", TOYFLAG_USR|TOYFLAG_SBIN))<br>
+USE_DEVMEM(NEWTOY(devmem, "<1(no-sync)(no-mmap)f:", TOYFLAG_USR|TOYFLAG_SBIN))<br>
USE_DF(NEWTOY(df, "HPkhit*a[-HPh]", TOYFLAG_BIN))<br>
USE_DHCP(NEWTOY(dhcp, "V:H:F:x*r:O*A#<0=20T#<0=3t#<0=3s:p:i:SBRCaovqnbf", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))<br>
USE_DHCP6(NEWTOY(dhcp6, "r:A#<0T#<0t#<0s:p:i:SRvqnbf", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))<br>
diff --git a/android/mac/generated/flags.h b/android/mac/generated/flags.h<br>
index 504dd782..59fd25af 100644<br>
--- a/android/mac/generated/flags.h<br>
+++ b/android/mac/generated/flags.h<br>
@@ -641,13 +641,14 @@<br>
#undef FOR_demo_utf8towc<br>
#endif<br>
<br>
-// devmem <1(no-sync)f:<br>
+// devmem <1(no-sync)(no-mmap)f:<br>
#undef OPTSTR_devmem<br>
-#define OPTSTR_devmem "<1(no-sync)f:"<br>
+#define OPTSTR_devmem "<1(no-sync)(no-mmap)f:"<br>
#ifdef CLEANUP_devmem<br>
#undef CLEANUP_devmem<br>
#undef FOR_devmem<br>
#undef FLAG_f<br>
+#undef FLAG_no_mmap<br>
#undef FLAG_no_sync<br>
#endif<br>
<br>
@@ -4547,7 +4548,8 @@<br>
#define TT this.devmem<br>
#endif<br>
#define FLAG_f (FORCED_FLAG<<0)<br>
-#define FLAG_no_sync (FORCED_FLAG<<1)<br>
+#define FLAG_no_mmap (FORCED_FLAG<<1)<br>
+#define FLAG_no_sync (FORCED_FLAG<<2)<br>
#endif<br>
<br>
#ifdef FOR_df<br>
diff --git a/android/mac/generated/help.h b/android/mac/generated/help.h<br>
index f25a0785..eccc159a 100644<br>
--- a/android/mac/generated/help.h<br>
+++ b/android/mac/generated/help.h<br>
@@ -332,7 +332,7 @@<br>
<br>
#define HELP_dos2unix "usage: dos2unix [FILE...]\n\nConvert newline format from dos \"\\r\\n\" to unix \"\\n\".\nIf no files listed copy from stdin, \"-\" is a synonym for stdin."<br>
<br>
-#define HELP_devmem "usage: devmem [-f FILE] ADDR [WIDTH [DATA...]]\n\nRead/write physical addresses. WIDTH is 1, 2, 4, or 8 bytes (default 4).\nPrefix ADDR with 0x for hexadecimal, output is in same base as address.\n\n-f FILE File to operate on (default /dev/mem)\n--no-sync Don't open the file with O_SYNC (for cached access)"<br>
+#define HELP_devmem "usage: devmem [-f FILE] ADDR [WIDTH [DATA...]]\n\nRead/write physical addresses. WIDTH is 1, 2, 4, or 8 bytes (default 4).\nPrefix ADDR with 0x for hexadecimal, output is in same base as address.\n\n-f FILE File to operate on (default /dev/mem)\n--no-sync Don't open the file with O_SYNC (for cached access)\n--no-mmap Don't mmap the file"<br>
<br>
#define HELP_count "usage: count [-l]\n\n-l Long output (total bytes, human readable, transfer rate, elapsed time)\n\nCopy stdin to stdout, displaying simple progress indicator to stderr."<br>
<br>
diff --git a/android/mac/generated/newtoys.h b/android/mac/generated/newtoys.h<br>
index c41eb83f..e99f4f1c 100644<br>
--- a/android/mac/generated/newtoys.h<br>
+++ b/android/mac/generated/newtoys.h<br>
@@ -60,7 +60,7 @@ USE_DEMO_MANY_OPTIONS(NEWTOY(demo_many_options, "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwv<br>
USE_DEMO_NUMBER(NEWTOY(demo_number, "D#=3<3M#<0hcdbs", TOYFLAG_BIN))<br>
USE_DEMO_SCANKEY(NEWTOY(demo_scankey, 0, TOYFLAG_BIN))<br>
USE_DEMO_UTF8TOWC(NEWTOY(demo_utf8towc, 0, TOYFLAG_USR|TOYFLAG_BIN))<br>
-USE_DEVMEM(NEWTOY(devmem, "<1(no-sync)f:", TOYFLAG_USR|TOYFLAG_SBIN))<br>
+USE_DEVMEM(NEWTOY(devmem, "<1(no-sync)(no-mmap)f:", TOYFLAG_USR|TOYFLAG_SBIN))<br>
USE_DF(NEWTOY(df, "HPkhit*a[-HPh]", TOYFLAG_BIN))<br>
USE_DHCP(NEWTOY(dhcp, "V:H:F:x*r:O*A#<0=20T#<0=3t#<0=3s:p:i:SBRCaovqnbf", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))<br>
USE_DHCP6(NEWTOY(dhcp6, "r:A#<0T#<0t#<0s:p:i:SRvqnbf", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))<br>
diff --git a/toys/other/devmem.c b/toys/other/devmem.c<br>
index 9f9a9e03..f37e8ef3 100644<br>
--- a/toys/other/devmem.c<br>
+++ b/toys/other/devmem.c<br>
@@ -2,7 +2,7 @@<br>
*<br>
* Copyright 2019 The Android Open Source Project<br>
<br>
-USE_DEVMEM(NEWTOY(devmem, "<1(no-sync)f:", TOYFLAG_USR|TOYFLAG_SBIN))<br>
+USE_DEVMEM(NEWTOY(devmem, "<1(no-sync)(no-mmap)f:", TOYFLAG_USR|TOYFLAG_SBIN))<br>
<br>
config DEVMEM<br>
bool "devmem"<br>
@@ -15,6 +15,7 @@ config DEVMEM<br>
<br>
-f FILE File to operate on (default /dev/mem)<br>
--no-sync Don't open the file with O_SYNC (for cached access)<br>
+ --no-mmap Don't mmap the file<br>
*/<br>
<br>
#define FOR_devmem<br>
@@ -62,32 +63,46 @@ void devmem_main(void)<br>
flags = writing ? O_RDWR : O_RDONLY;<br>
if (!FLAG(no_sync)) flags |= O_SYNC;<br>
fd = xopen(TT.f ?: "/dev/mem", flags);<br>
- map_off = addr & ~(page_size - 1ULL);<br>
- map_len = (addr+bytes-map_off);<br>
- map = xmmap(0, map_len, writing ? PROT_WRITE : PROT_READ, MAP_SHARED, fd,<br>
- map_off);<br>
- p = map + (addr & (page_size - 1));<br>
- close(fd);<br>
+ if (FLAG(no_mmap)) xlseek(fd, addr, SEEK_SET);<br>
+ else {<br>
+ map_off = addr & ~(page_size - 1ULL);<br>
+ map_len = addr + (writing ? (toys.optc - 2) * bytes : bytes) - map_off;<br>
+ map = xmmap(0, map_len, writing ? PROT_WRITE : PROT_READ, MAP_SHARED, fd,<br>
+ map_off);<br>
+ p = map + (addr & (page_size - 1));<br>
+ close(fd);<br>
+ }<br>
} else p = (void *)addr;<br>
<br>
// Not using peek()/poke() because registers care about size of read/write.<br>
if (writing) {<br>
for (int i = 2; i < toys.optc; i++) {<br>
data = xatolu(toys.optargs[i], bytes);<br>
- if (bytes==1) *(char *)p = data;<br>
- else if (bytes==2) *(unsigned short *)p = data;<br>
- else if (bytes==4) *(unsigned int *)p = data;<br>
- else if (sizeof(long)==8 && bytes==8) *(unsigned long *)p = data;<br>
- p += bytes;<br>
+ if (FLAG(no_mmap)) {<br>
+ xwrite(fd, &data, bytes);<br>
+ xlseek(fd, bytes, SEEK_CUR);<br></blockquote><div><br></div><div>is this clearer if you remove the xlseek at the top, and move this xlseek() before the xwrite()? i know that would mean incrementing addr, but at least the reasoning is localized (and you don't have (a) "aren't these the wrong way round?" and (b) "do we actually need a SEEK_CUR? why didn't the write advance the file offset?").</div></div></div></div></blockquote><div>xlseek at the top helps to maintain a consistent code structure. Also have removed the incorrect xlseek after xwrite. That usage is incorrect on my part. I will add test cases to verify in the upcoming patch revision.</div><div><br></div>Thanks and Regards,<div>Karthikeyan. </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ } else {<br>
+ if (bytes==1) *(char *)p = data;<br>
+ else if (bytes==2) *(unsigned short *)p = data;<br>
+ else if (bytes==4) *(unsigned int *)p = data;<br>
+ else if (sizeof(long)==8 && bytes==8) *(unsigned long *)p = data;<br>
+ p += bytes;<br>
+ }<br>
}<br>
} else {<br>
- if (bytes==1) data = *(char *)p;<br>
- else if (bytes==2) data = *(unsigned short *)p;<br>
- else if (bytes==4) data = *(unsigned int *)p;<br>
- else if (sizeof(long)==8 && bytes==8) data = *(unsigned long *)p;<br>
+ if (FLAG(no_mmap)) xread(fd, &data, bytes);<br>
+ else {<br>
+ if (bytes==1) data = *(char *)p;<br>
+ else if (bytes==2) data = *(unsigned short *)p;<br>
+ else if (bytes==4) data = *(unsigned int *)p;<br>
+ else if (sizeof(long)==8 && bytes==8) data = *(unsigned long *)p;<br>
+ }<br>
printf((!strchr(*toys.optargs, 'x')) ? "%0*ld\n" : "0x%0*lx\n",<br>
bytes*2, data);<br>
}<br>
<br>
- if (CFG_TOYBOX_FORK) munmap(map, map_len);<br>
+ if (CFG_TOYBOX_FORK) {<br>
+ if (FLAG(no_mmap)) close(fd);<br>
+ else munmap(map, map_len);<br>
+ }<br>
}<br>
-- <br>
2.47.0.105.g07ac214952-goog<br>
<br>
</blockquote></div></div>
</div>
</blockquote></div></div>