Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 1 | #include <fcntl.h> |
| 2 | #include <inttypes.h> |
| 3 | #include <stdbool.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 6 | #include <string.h> |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 7 | #include <sys/mman.h> |
Elliott Hughes | 0badbd6 | 2014-12-29 12:24:25 -0800 | [diff] [blame] | 8 | #include <unistd.h> |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 9 | |
| 10 | #if __LP64__ |
| 11 | #define strtoptr strtoull |
| 12 | #else |
| 13 | #define strtoptr strtoul |
| 14 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 15 | |
| 16 | static int usage() |
| 17 | { |
| 18 | fprintf(stderr,"r [-b|-s] <address> [<value>]\n"); |
| 19 | return -1; |
| 20 | } |
| 21 | |
Elliott Hughes | 660e750 | 2014-08-28 20:44:20 -0700 | [diff] [blame] | 22 | int main(int argc, char *argv[]) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | if(argc < 2) return usage(); |
| 25 | |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 26 | int width = 4; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | 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 Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 38 | uintptr_t addr = strtoptr(argv[1], 0, 16); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 40 | uintptr_t endaddr = 0; |
| 41 | char* end = strchr(argv[1], '-'); |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 42 | if (end) |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 43 | endaddr = strtoptr(end + 1, 0, 16); |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 44 | |
| 45 | if (!endaddr) |
| 46 | endaddr = addr + width - 1; |
| 47 | |
| 48 | if (endaddr <= addr) { |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 49 | fprintf(stderr, "end address <= start address\n"); |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 50 | return -1; |
| 51 | } |
| 52 | |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 53 | bool set = false; |
| 54 | uint32_t value = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | if(argc > 2) { |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 56 | set = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | value = strtoul(argv[2], 0, 16); |
| 58 | } |
| 59 | |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 60 | int fd = open("/dev/mem", O_RDWR | O_SYNC); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | if(fd < 0) { |
| 62 | fprintf(stderr,"cannot open /dev/mem\n"); |
| 63 | return -1; |
| 64 | } |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 65 | |
| 66 | off64_t mmap_start = addr & ~(PAGE_SIZE - 1); |
| 67 | size_t mmap_size = endaddr - mmap_start + 1; |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 68 | mmap_size = (mmap_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); |
| 69 | |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 70 | void* page = mmap64(0, mmap_size, PROT_READ | PROT_WRITE, |
| 71 | MAP_SHARED, fd, mmap_start); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | |
| 73 | if(page == MAP_FAILED){ |
| 74 | fprintf(stderr,"cannot mmap region\n"); |
| 75 | return -1; |
| 76 | } |
| 77 | |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 78 | while (addr <= endaddr) { |
| 79 | switch(width){ |
| 80 | case 4: { |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 81 | uint32_t* x = (uint32_t*) (((uintptr_t) page) + (addr & 4095)); |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 82 | if(set) *x = value; |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 83 | fprintf(stderr,"%08"PRIxPTR": %08x\n", addr, *x); |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | case 2: { |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 87 | uint16_t* x = (uint16_t*) (((uintptr_t) page) + (addr & 4095)); |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 88 | if(set) *x = value; |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 89 | fprintf(stderr,"%08"PRIxPTR": %04x\n", addr, *x); |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 90 | break; |
| 91 | } |
| 92 | case 1: { |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 93 | uint8_t* x = (uint8_t*) (((uintptr_t) page) + (addr & 4095)); |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 94 | if(set) *x = value; |
Elliott Hughes | 2430a06 | 2014-01-16 17:33:52 -0800 | [diff] [blame] | 95 | fprintf(stderr,"%08"PRIxPTR": %02x\n", addr, *x); |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 96 | break; |
| 97 | } |
| 98 | } |
| 99 | addr += width; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 101 | return 0; |
| 102 | } |