From c6edbfddb078cc36cd0dfbabe79c1b54d6171cf1 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Sat, 5 Aug 2017 21:24:16 +0100 Subject: [PATCH] devmem3 cleanup further --- kernel_module/user/devmem3.c | 57 +++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/kernel_module/user/devmem3.c b/kernel_module/user/devmem3.c index eb1d8e5..2ffd914 100644 --- a/kernel_module/user/devmem3.c +++ b/kernel_module/user/devmem3.c @@ -1,33 +1,36 @@ /* Adapted from: http://free-electrons.com/pub/mirror/devmem2.c + +Forked because of the unpredictable access widths: +https://bugs.busybox.net/show_bug.cgi?id=10171 */ +#include +#include +#include +#include +#include #include #include -#include #include -#include -#include -#include -#include -#include -#include #include -#include - +#include +#include +#include + #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; - uintmax_t read_result, writeval; - off_t target; int access_type = 'w'; - + unsigned long map_size, map_mask; + off_t target; + uintmax_t read_result, writeval; + int fd; + void *map_base, *virt_addr; + + map_size = sysconf(_SC_PAGE_SIZE); + map_mask = map_size - 1; if(argc < 2) { fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n" "\taddress : memory address to act upon\n" @@ -43,16 +46,16 @@ int main(int argc, char **argv) { if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL; - printf("/dev/mem opened.\n"); + 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); + 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); + printf("Memory mapped at address %p.\n", map_base); fflush(stdout); - - virt_addr = map_base + (target & MAP_MASK); + + virt_addr = map_base + (target & map_mask); switch(access_type) { case 'b': read_result = *((uint8_t *) virt_addr); @@ -70,7 +73,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Illegal data type '%c'.\n", access_type); exit(2); } - printf("Value at address 0x%jX (%p): 0x%jX\n", (uintmax_t)target, virt_addr, (uintmax_t)read_result); + printf("Value at address 0x%jX (%p): 0x%jX\n", (uintmax_t)target, virt_addr, (uintmax_t)read_result); fflush(stdout); if(argc > 3) { @@ -93,11 +96,11 @@ int main(int argc, char **argv) { read_result = *((uint64_t *) virt_addr); break; } - printf("Written 0x%X; readback 0x%jX\n", (unsigned int)writeval, (unsigned int)read_result); + printf("Written 0x%jX; readback 0x%jX\n", (uintmax_t)writeval, (uintmax_t)read_result); fflush(stdout); } - - if(munmap(map_base, MAP_SIZE) == -1) FATAL; + + if(munmap(map_base, map_size) == -1) FATAL; close(fd); return 0; }