blob: 618367726a6e79a6b7355d60f90059a63b36394d [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>
8
9#if __LP64__
10#define strtoptr strtoull
11#else
12#define strtoptr strtoul
13#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080014
15static int usage()
16{
17 fprintf(stderr,"r [-b|-s] <address> [<value>]\n");
18 return -1;
19}
20
Elliott Hughes660e7502014-08-28 20:44:20 -070021int main(int argc, char *argv[])
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 if(argc < 2) return usage();
24
Elliott Hughes2430a062014-01-16 17:33:52 -080025 int width = 4;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026 if(!strcmp(argv[1], "-b")) {
27 width = 1;
28 argc--;
29 argv++;
30 } else if(!strcmp(argv[1], "-s")) {
31 width = 2;
32 argc--;
33 argv++;
34 }
35
36 if(argc < 2) return usage();
Elliott Hughes2430a062014-01-16 17:33:52 -080037 uintptr_t addr = strtoptr(argv[1], 0, 16);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
Elliott Hughes2430a062014-01-16 17:33:52 -080039 uintptr_t endaddr = 0;
40 char* end = strchr(argv[1], '-');
Colin Cross2ad280f2011-01-19 17:06:25 -080041 if (end)
Elliott Hughes2430a062014-01-16 17:33:52 -080042 endaddr = strtoptr(end + 1, 0, 16);
Colin Cross2ad280f2011-01-19 17:06:25 -080043
44 if (!endaddr)
45 endaddr = addr + width - 1;
46
47 if (endaddr <= addr) {
Elliott Hughes2430a062014-01-16 17:33:52 -080048 fprintf(stderr, "end address <= start address\n");
Colin Cross2ad280f2011-01-19 17:06:25 -080049 return -1;
50 }
51
Elliott Hughes2430a062014-01-16 17:33:52 -080052 bool set = false;
53 uint32_t value = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054 if(argc > 2) {
Elliott Hughes2430a062014-01-16 17:33:52 -080055 set = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056 value = strtoul(argv[2], 0, 16);
57 }
58
Elliott Hughes2430a062014-01-16 17:33:52 -080059 int fd = open("/dev/mem", O_RDWR | O_SYNC);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060 if(fd < 0) {
61 fprintf(stderr,"cannot open /dev/mem\n");
62 return -1;
63 }
Elliott Hughes2430a062014-01-16 17:33:52 -080064
65 off64_t mmap_start = addr & ~(PAGE_SIZE - 1);
66 size_t mmap_size = endaddr - mmap_start + 1;
Colin Cross2ad280f2011-01-19 17:06:25 -080067 mmap_size = (mmap_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
68
Elliott Hughes2430a062014-01-16 17:33:52 -080069 void* page = mmap64(0, mmap_size, PROT_READ | PROT_WRITE,
70 MAP_SHARED, fd, mmap_start);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
72 if(page == MAP_FAILED){
73 fprintf(stderr,"cannot mmap region\n");
74 return -1;
75 }
76
Colin Cross2ad280f2011-01-19 17:06:25 -080077 while (addr <= endaddr) {
78 switch(width){
79 case 4: {
Elliott Hughes2430a062014-01-16 17:33:52 -080080 uint32_t* x = (uint32_t*) (((uintptr_t) page) + (addr & 4095));
Colin Cross2ad280f2011-01-19 17:06:25 -080081 if(set) *x = value;
Elliott Hughes2430a062014-01-16 17:33:52 -080082 fprintf(stderr,"%08"PRIxPTR": %08x\n", addr, *x);
Colin Cross2ad280f2011-01-19 17:06:25 -080083 break;
84 }
85 case 2: {
Elliott Hughes2430a062014-01-16 17:33:52 -080086 uint16_t* x = (uint16_t*) (((uintptr_t) page) + (addr & 4095));
Colin Cross2ad280f2011-01-19 17:06:25 -080087 if(set) *x = value;
Elliott Hughes2430a062014-01-16 17:33:52 -080088 fprintf(stderr,"%08"PRIxPTR": %04x\n", addr, *x);
Colin Cross2ad280f2011-01-19 17:06:25 -080089 break;
90 }
91 case 1: {
Elliott Hughes2430a062014-01-16 17:33:52 -080092 uint8_t* x = (uint8_t*) (((uintptr_t) page) + (addr & 4095));
Colin Cross2ad280f2011-01-19 17:06:25 -080093 if(set) *x = value;
Elliott Hughes2430a062014-01-16 17:33:52 -080094 fprintf(stderr,"%08"PRIxPTR": %02x\n", addr, *x);
Colin Cross2ad280f2011-01-19 17:06:25 -080095 break;
96 }
97 }
98 addr += width;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 return 0;
101}