From 5f153b56214f32c679b7eef18eff59d7052c132c Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 8 Oct 2023 01:41:17 -0500 Subject: [PATCH] Switch devmem to use long instead of long long, to be nicer to 32 bit targets. (Zaps warning, and you can't do an atomic 8 byte write on 32 bit targets.) --- toys/other/devmem.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/toys/other/devmem.c b/toys/other/devmem.c index 137e60c4..ce158c2c 100644 --- a/toys/other/devmem.c +++ b/toys/other/devmem.c @@ -17,36 +17,37 @@ config DEVMEM #define FOR_devmem #include "toys.h" -unsigned long long atollu(char *str) +unsigned long atolu(char *str) { char *end = str; - unsigned long long llu; + unsigned long lu; errno = 0; - llu = strtoull(str, &end, 0); + lu = strtoul(str, &end, 0); if (*end || errno) perror_exit("bad %s", str); - return llu; + return lu; } void devmem_main(void) { int writing = toys.optc == 3, page_size = sysconf(_SC_PAGESIZE), bytes = 4,fd; - unsigned long long data = 0, map_off, map_len, addr = atollu(*toys.optargs); + unsigned long data = 0, map_off, map_len, addr = atolu(*toys.optargs); + char *sizes = sizeof(long)==8 ? "1248" : "124"; void *map, *p; // WIDTH? if (toys.optc>1) { int i; - if ((i=stridx("1248", *toys.optargs[1]))==-1 || toys.optargs[1][1]) + if ((i=stridx(sizes, *toys.optargs[1]))==-1 || toys.optargs[1][1]) error_exit("bad width: %s", toys.optargs[1]); bytes = 1<(~0ULL)>>(64-8*bytes)) - error_exit("%llx>%d bytes", data, bytes); + if (writing && (data = atolu(toys.optargs[2]))>(~0UL)>>(sizeof(long)-bytes)*8) + error_exit("%lx>%d bytes", data, bytes); // Map in just enough. if (CFG_TOYBOX_FORK) { @@ -62,16 +63,16 @@ void devmem_main(void) // Not using peek()/poke() because registers care about size of read/write if (writing) { - if (bytes == 1) *(unsigned char *)p = data; - else if (bytes == 2) *(unsigned short *)p = data; - else if (bytes == 4) *(unsigned int *)p = data; - else if (bytes == 8) *(unsigned long long *)p = data; + if (bytes==1) *(char *)p = data; + else if (bytes==2) *(unsigned short *)p = data; + else if (bytes==4) *(unsigned int *)p = data; + else if (sizeof(long)==8 && bytes==8) *(unsigned long *)p = data; } else { - if (bytes == 1) data = *(unsigned char *)p; - else if (bytes == 2) data = *(unsigned short *)p; - else if (bytes == 4) data = *(unsigned int *)p; - else if (bytes == 8) data = *(unsigned long long *)p; - printf((!strchr(*toys.optargs, 'x')) ? "%0*lld\n" : "0x%0*llx\n", + if (bytes==1) data = *(char *)p; + else if (bytes==2) data = *(unsigned short *)p; + else if (bytes==4) data = *(unsigned int *)p; + else if (sizeof(long)==8 && bytes==8) data = *(unsigned long *)p; + printf((!strchr(*toys.optargs, 'x')) ? "%0*ld\n" : "0x%0*lx\n", bytes*2, data); } -- 2.39.2