blob: eb8ea0b9b699874a3c73c3e5da784047de3b4886 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <stdio.h>
2#include <stdlib.h>
3#include <sys/mman.h>
4#include <fcntl.h>
5#include <string.h>
6
7static int usage()
8{
9 fprintf(stderr,"r [-b|-s] <address> [<value>]\n");
10 return -1;
11}
12
13int r_main(int argc, char *argv[])
14{
15 int width = 4, set = 0, fd;
Colin Cross2ad280f2011-01-19 17:06:25 -080016 unsigned addr, value, endaddr = 0;
17 unsigned long mmap_start, mmap_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018 void *page;
Colin Cross2ad280f2011-01-19 17:06:25 -080019 char *end;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080020
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 Cross2ad280f2011-01-19 17:06:25 -080036 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 Projectdd7bc332009-03-03 19:32:55 -080048 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 Cross2ad280f2011-01-19 17:06:25 -080059 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 Projectdd7bc332009-03-03 19:32:55 -080065
66 if(page == MAP_FAILED){
67 fprintf(stderr,"cannot mmap region\n");
68 return -1;
69 }
70
Colin Cross2ad280f2011-01-19 17:06:25 -080071 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 Projectdd7bc332009-03-03 19:32:55 -080093 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094 return 0;
95}