summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Muellner <christoph.muellner@theobroma-systems.com>2019-11-06 17:40:40 +0100
committerChristoph Muellner <christoph.muellner@theobroma-systems.com>2019-11-06 17:52:39 +0100
commitde5c5e786e178218c825759865cf3485df04a51a (patch)
tree67e7d67882a418d737821b6fa7cdf79b5209b942
parent06ae183b7fa91269ecd1cc41cd693f6e2bc790bb (diff)
Make devmem2 less verbose.HEADmaster
This patch removes a lot of irrelevant output from devmem2 such as the virtual address, which is used to access the target address. In case of read operation devmem2 now just prints the read value. In case of write operation devmem2 prints the read-back value. Signed-off-by: Christoph Muellner <christoph.muellner@theobroma-systems.com>
-rw-r--r--devmem2.c109
1 files changed, 61 insertions, 48 deletions
diff --git a/devmem2.c b/devmem2.c
index 6003c4d..b4ecb18 100644
--- a/devmem2.c
+++ b/devmem2.c
@@ -50,26 +50,31 @@
#include <sys/mman.h>
#include <stdint.h>
#include <inttypes.h>
-
-#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
- __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
-
+
+#define FATAL \
+ do { \
+ fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
+ __LINE__, __FILE__, errno, strerror(errno)); \
+ exit(1); \
+ } while(0)
+
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int main(int argc, char **argv) {
- int fd;
- void *map_base, *virt_addr;
+ int fd;
+ void *map_base, *virt_addr;
uint64_t read_result, writeval;
+ int do_write = 0;
off_t target;
int access_type = 'w';
-
+
if(argc < 2) {
fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
- "\taddress : memory address to act upon\n"
- "\ttype : access operation type : [b]yte, [h]alfword, [w]ord, [d]oubleword\n"
- "\tdata : data to be written\n\n",
- argv[0]);
+ "\taddress : memory address to act upon\n"
+ "\ttype : access operation type : [b]yte, [h]alfword, [w]ord, [d]oubleword\n"
+ "\tdata : data to be written\n\n",
+ argv[0]);
exit(1);
}
target = strtoull(argv[1], 0, 0);
@@ -77,41 +82,44 @@ int main(int argc, char **argv) {
if(argc > 2)
access_type = tolower(argv[2][0]);
+ if (argc > 3) {
+ writeval = strtoul(argv[3], 0, 0);
+ do_write = 1;
+ }
- if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
- printf("/dev/mem opened.\n");
- fflush(stdout);
-
- /* Map one page */
- map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
- if(map_base == (void *) -1) FATAL;
- printf("Memory mapped at address %p.\n", map_base);
- fflush(stdout);
-
- virt_addr = map_base + (target & MAP_MASK);
- switch(access_type) {
- case 'b':
- read_result = *((uint8_t *) virt_addr);
- break;
- case 'h':
- read_result = *((uint16_t *) virt_addr);
- break;
- case 'w':
- read_result = *((uint32_t *) virt_addr);
- break;
- case 'd':
- read_result = *((uint64_t *) virt_addr);
- break;
- default:
- fprintf(stderr, "Illegal data type '%c'.\n", access_type);
- exit(2);
- }
- printf("Value at address 0x%X (%p): 0x%" PRIX64 "\n", target, virt_addr, read_result);
- fflush(stdout);
+ if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
+ FATAL;
- if(argc > 3) {
- writeval = strtoul(argv[3], 0, 0);
+ /* Map one page */
+ map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
+ if(map_base == (void *) -1)
+ FATAL;
+
+ virt_addr = map_base + (target & MAP_MASK);
+
+ if (!do_write) {
+ switch(access_type) {
+ case 'b':
+ read_result = *((uint8_t *) virt_addr);
+ break;
+ case 'h':
+ read_result = *((uint16_t *) virt_addr);
+ break;
+ case 'w':
+ read_result = *((uint32_t *) virt_addr);
+ break;
+ case 'd':
+ read_result = *((uint64_t *) virt_addr);
+ break;
+ default:
+ fprintf(stderr, "Illegal data type '%c'.\n", access_type);
+ exit(2);
+ }
+
+ printf("0x%" PRIX64 "\n", read_result);
+ fflush(stdout);
+ } else {
switch(access_type) {
case 'b':
*((uint8_t *) virt_addr) = writeval;
@@ -129,12 +137,17 @@ int main(int argc, char **argv) {
*((uint64_t *) virt_addr) = writeval;
read_result = *((uint64_t *) virt_addr);
break;
+ default:
+ fprintf(stderr, "Illegal data type '%c'.\n", access_type);
+ exit(2);
}
- printf("Written 0x%X; readback 0x%X\n", writeval, read_result);
+ printf("0x%" PRIX64 "\n", read_result);
fflush(stdout);
}
-
- if(munmap(map_base, MAP_SIZE) == -1) FATAL;
- close(fd);
- return 0;
+
+ if(munmap(map_base, MAP_SIZE) == -1)
+ FATAL;
+ close(fd);
+
+ return 0;
}