The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <sys/mman.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | static int usage() |
| 8 | { |
| 9 | fprintf(stderr,"r [-b|-s] <address> [<value>]\n"); |
| 10 | return -1; |
| 11 | } |
| 12 | |
| 13 | int r_main(int argc, char *argv[]) |
| 14 | { |
| 15 | int width = 4, set = 0, fd; |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 16 | unsigned addr, value, endaddr = 0; |
| 17 | unsigned long mmap_start, mmap_size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 18 | void *page; |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 19 | char *end; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 20 | |
| 21 | if(argc < 2) return usage(); |
| 22 | |
| 23 | if(!strcmp(argv[1], "-b")) { |
| 24 | width = 1; |
| 25 | argc--; |
| 26 | argv++; |
| 27 | } else if(!strcmp(argv[1], "-s")) { |
| 28 | width = 2; |
| 29 | argc--; |
| 30 | argv++; |
| 31 | } |
| 32 | |
| 33 | if(argc < 2) return usage(); |
| 34 | addr = strtoul(argv[1], 0, 16); |
| 35 | |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 36 | end = strchr(argv[1], '-'); |
| 37 | if (end) |
| 38 | endaddr = strtoul(end + 1, 0, 16); |
| 39 | |
| 40 | if (!endaddr) |
| 41 | endaddr = addr + width - 1; |
| 42 | |
| 43 | if (endaddr <= addr) { |
| 44 | fprintf(stderr, "invalid end address\n"); |
| 45 | return -1; |
| 46 | } |
| 47 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | if(argc > 2) { |
| 49 | set = 1; |
| 50 | value = strtoul(argv[2], 0, 16); |
| 51 | } |
| 52 | |
| 53 | fd = open("/dev/mem", O_RDWR | O_SYNC); |
| 54 | if(fd < 0) { |
| 55 | fprintf(stderr,"cannot open /dev/mem\n"); |
| 56 | return -1; |
| 57 | } |
| 58 | |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 59 | mmap_start = addr & ~(PAGE_SIZE - 1); |
| 60 | mmap_size = endaddr - mmap_start + 1; |
| 61 | mmap_size = (mmap_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); |
| 62 | |
| 63 | page = mmap(0, mmap_size, PROT_READ | PROT_WRITE, |
| 64 | MAP_SHARED, fd, mmap_start); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | |
| 66 | if(page == MAP_FAILED){ |
| 67 | fprintf(stderr,"cannot mmap region\n"); |
| 68 | return -1; |
| 69 | } |
| 70 | |
Colin Cross | 2ad280f | 2011-01-19 17:06:25 -0800 | [diff] [blame] | 71 | while (addr <= endaddr) { |
| 72 | switch(width){ |
| 73 | case 4: { |
| 74 | unsigned *x = (unsigned*) (((unsigned) page) + (addr & 4095)); |
| 75 | if(set) *x = value; |
| 76 | fprintf(stderr,"%08x: %08x\n", addr, *x); |
| 77 | break; |
| 78 | } |
| 79 | case 2: { |
| 80 | unsigned short *x = (unsigned short*) (((unsigned) page) + (addr & 4095)); |
| 81 | if(set) *x = value; |
| 82 | fprintf(stderr,"%08x: %04x\n", addr, *x); |
| 83 | break; |
| 84 | } |
| 85 | case 1: { |
| 86 | unsigned char *x = (unsigned char*) (((unsigned) page) + (addr & 4095)); |
| 87 | if(set) *x = value; |
| 88 | fprintf(stderr,"%08x: %02x\n", addr, *x); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | addr += width; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 94 | return 0; |
| 95 | } |