blob: b96cdb279f1c13580011e78c39be0ef442916284 [file] [log] [blame]
Elliott Hughes2430a062014-01-16 17:33:52 -08001#include <fcntl.h>
2#include <inttypes.h>
3#include <stdbool.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08004#include <stdio.h>
5#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08006#include <string.h>
Elliott Hughes2430a062014-01-16 17:33:52 -08007#include <sys/mman.h>
Elliott Hughes0badbd62014-12-29 12:24:25 -08008#include <unistd.h>
Elliott Hughes2430a062014-01-16 17:33:52 -08009
10#if __LP64__
11#define strtoptr strtoull
12#else
13#define strtoptr strtoul
14#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015
16static int usage()
17{
18 fprintf(stderr,"r [-b|-s] <address> [<value>]\n");
19 return -1;
20}
21
Elliott Hughes660e7502014-08-28 20:44:20 -070022int main(int argc, char *argv[])
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024 if(argc < 2) return usage();
25
Elliott Hughes2430a062014-01-16 17:33:52 -080026 int width = 4;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027 if(!strcmp(argv[1], "-b")) {
28 width = 1;
29 argc--;
30 argv++;
31 } else if(!strcmp(argv[1], "-s")) {
32 width = 2;
33 argc--;
34 argv++;
35 }
36
37 if(argc < 2) return usage();
Elliott Hughes2430a062014-01-16 17:33:52 -080038 uintptr_t addr = strtoptr(argv[1], 0, 16);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
Elliott Hughes2430a062014-01-16 17:33:52 -080040 uintptr_t endaddr = 0;
41 char* end = strchr(argv[1], '-');
Colin Cross2ad280f2011-01-19 17:06:25 -080042 if (end)
Elliott Hughes2430a062014-01-16 17:33:52 -080043 endaddr = strtoptr(end + 1, 0, 16);
Colin Cross2ad280f2011-01-19 17:06:25 -080044
45 if (!endaddr)
46 endaddr = addr + width - 1;
47
48 if (endaddr <= addr) {
Elliott Hughes2430a062014-01-16 17:33:52 -080049 fprintf(stderr, "end address <= start address\n");
Colin Cross2ad280f2011-01-19 17:06:25 -080050 return -1;
51 }
52
Elliott Hughes2430a062014-01-16 17:33:52 -080053 bool set = false;
54 uint32_t value = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055 if(argc > 2) {
Elliott Hughes2430a062014-01-16 17:33:52 -080056 set = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057 value = strtoul(argv[2], 0, 16);
58 }
59
Elliott Hughes2430a062014-01-16 17:33:52 -080060 int fd = open("/dev/mem", O_RDWR | O_SYNC);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061 if(fd < 0) {
62 fprintf(stderr,"cannot open /dev/mem\n");
63 return -1;
64 }
Elliott Hughes2430a062014-01-16 17:33:52 -080065
66 off64_t mmap_start = addr & ~(PAGE_SIZE - 1);
67 size_t mmap_size = endaddr - mmap_start + 1;
Colin Cross2ad280f2011-01-19 17:06:25 -080068 mmap_size = (mmap_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
69
Elliott Hughes2430a062014-01-16 17:33:52 -080070 void* page = mmap64(0, mmap_size, PROT_READ | PROT_WRITE,
71 MAP_SHARED, fd, mmap_start);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072
73 if(page == MAP_FAILED){
74 fprintf(stderr,"cannot mmap region\n");
75 return -1;
76 }
77
Colin Cross2ad280f2011-01-19 17:06:25 -080078 while (addr <= endaddr) {
79 switch(width){
80 case 4: {
Elliott Hughes2430a062014-01-16 17:33:52 -080081 uint32_t* x = (uint32_t*) (((uintptr_t) page) + (addr & 4095));
Colin Cross2ad280f2011-01-19 17:06:25 -080082 if(set) *x = value;
Elliott Hughes2430a062014-01-16 17:33:52 -080083 fprintf(stderr,"%08"PRIxPTR": %08x\n", addr, *x);
Colin Cross2ad280f2011-01-19 17:06:25 -080084 break;
85 }
86 case 2: {
Elliott Hughes2430a062014-01-16 17:33:52 -080087 uint16_t* x = (uint16_t*) (((uintptr_t) page) + (addr & 4095));
Colin Cross2ad280f2011-01-19 17:06:25 -080088 if(set) *x = value;
Elliott Hughes2430a062014-01-16 17:33:52 -080089 fprintf(stderr,"%08"PRIxPTR": %04x\n", addr, *x);
Colin Cross2ad280f2011-01-19 17:06:25 -080090 break;
91 }
92 case 1: {
Elliott Hughes2430a062014-01-16 17:33:52 -080093 uint8_t* x = (uint8_t*) (((uintptr_t) page) + (addr & 4095));
Colin Cross2ad280f2011-01-19 17:06:25 -080094 if(set) *x = value;
Elliott Hughes2430a062014-01-16 17:33:52 -080095 fprintf(stderr,"%08"PRIxPTR": %02x\n", addr, *x);
Colin Cross2ad280f2011-01-19 17:06:25 -080096 break;
97 }
98 }
99 addr += width;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 return 0;
102}