From 1211ecfbb1336936e14780b3c55e4af3dc38c171 Mon Sep 17 00:00:00 2001 From: pavel Date: Tue, 22 Sep 2015 20:17:39 +0300 Subject: [PATCH] Partial merge from TC branch. Non-functional. --- .gitattributes | 1 + Makefile | 4 ++- README.md | 1 + devmem2.c | 68 +++++++++++++++++++++++++++++++------------------- 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/.gitattributes b/.gitattributes index 4e715cd..f04ed16 100644 --- a/.gitattributes +++ b/.gitattributes @@ -9,3 +9,4 @@ *.c eol=lf *.cpp eol=lf *.h eol=lf +*.md eol=lf \ No newline at end of file diff --git a/Makefile b/Makefile index cd2aeba..67e2c65 100644 --- a/Makefile +++ b/Makefile @@ -12,12 +12,14 @@ SRC=devmem2.c devmem2: $(SRC) $(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) -o $@ $(SRC) +# Install as "devmem2" install: mkdir -p $(BINDIR) install devmem2 $(BINDIR) +# Rename to "mem" : we like it this way. installmem: - install -s -D $(BINDIR)/mem + install -s -D devmem2 $(BINDIR)/mem clean: rm -f devmem2 *.o *~ diff --git a/README.md b/README.md index 7a302ec..ff832ff 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ Program to read/write from/to any location in physical memory +This is enhanced variant of "devmem" or "devmem2" utility. Source and documentation: http://git.io/vZ5iD For Linux 2.6 - 3.x - 4.x diff --git a/devmem2.c b/devmem2.c index 8cb1209..061af56 100644 --- a/devmem2.c +++ b/devmem2.c @@ -10,6 +10,7 @@ * and Ubiquitous Communications (http://www.ubicom.tudelft.nl/) * projects. * + * Copyright (C) 2015, Trego Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,9 +39,11 @@ #define printerr(fmt,...) do { fprintf(stderr, fmt, ## __VA_ARGS__); fflush(stderr); } while(0) -#define VER_STR "devmem version T/C (http://git.io/vZ5iD) rev.0.3" +#define VER_STR "devmem version T/C (http://git.io/vZ5iD) rev.0.3b" -void usage(const char *progname) +int f_dbg = 0; + +static void usage(const char *cmd) { fprintf(stderr, "\nUsage:\t%s [-switches] address [ type [ data ] ]\n" "\taddress : memory address to act upon\n" @@ -51,7 +54,7 @@ void usage(const char *progname) "\t-a : do not check alignment\n" "\t--version | -V : print version\n" "\n", - progname); + cmd); } int main(int argc, char **argv) @@ -69,44 +72,52 @@ int main(int argc, char **argv) int f_readback = 0; // flag to read back after write int f_align_check = 1; // flag to require alignment const char *progname = argv[0]; + int opt; - for ( ; argc > 1 && argv[1][0] == '-'; argv++, argc--) { - if (0 == strcmp(argv[1], "-r")) { + opterr = 0; + while ((opt = getopt(argc, argv, "+raAdV")) != -1) { + switch(opt) { + case 'r': f_readback = 1; - continue; - } - - if (0 == strcmp(argv[1], "-a")) { + break; + case 'a': f_align_check = 0; - continue; - } - - if (0 == strcmp(argv[1], "-A")) { - // Absolute address mode. Does nothing now, for future compat. - continue; + break; + case 'A': + // Absolute address mode. Does nothing now, for future compat.; + break; + case 'd': + f_dbg = 1; + break; + case 'V': + printf(VER_STR "\n"); + exit(0); + default: + if ( (!argv[optind]) || 0 == strcmp(argv[optind], "--help")) { + usage(progname); + exit(1); } - if (0 == strncmp(argv[1], "--vers", 6) || 0 == strcmp(argv[1], "-V")) { + if (0 == strncmp(argv[optind], "--vers", 6)) { printf(VER_STR "\n"); exit(0); } - if (0 == strcmp(argv[1], "--help")) { - argc = 0; - break; - } - printerr("Unknown option: %s\n", argv[1]); + printerr("Unknown long option: %s\n", argv[optind]); exit(2); + } } - if (argc < 2) { + if (optind >= argc) { usage(progname); exit(1); } + argc -= optind - 1; + argv += optind - 1; if (argc > 2) { - if (isalpha(argv[1][0])) { + if (!isdigit(argv[1][0])) { // Allow access_type be 1st arg, then swap 1st and 2nd char *t = argv[2]; argv[2] = argv[1]; @@ -157,6 +168,9 @@ int main(int argc, char **argv) map_size += pagesize; } + if (f_dbg) { + printerr("Address: %#" PRIX64 " op.size=%d\n", target, access_size); + } if (f_align_check && offset & (access_size - 1)) { printerr("ERROR: address not aligned on %d!\n", access_size); exit(2); @@ -207,8 +221,10 @@ int main(int argc, char **argv) break; } - // printf("Written 0x%lu\n", writeval); - //fflush(stdout); + if (f_dbg) { + printerr("Address: %#" PRIX64 " Written: %#lX\n", target, writeval); + fflush(stdout); + } } if (argc <= 3 || f_readback) { @@ -226,7 +242,7 @@ int main(int argc, char **argv) //printf("Value at address 0x%lld (%p): 0x%lu\n", (long long)target, virt_addr, read_result); //fflush(stdout); - if (f_readback) + if (f_readback && argc > 3) printf("Written 0x%lx; readback 0x%lx\n", writeval, read_result); else printf("%08lX\n", read_result);