Home | History | Annotate | Download | only in toolbox
      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;
     16     unsigned addr, value, endaddr = 0;
     17     unsigned long mmap_start, mmap_size;
     18     void *page;
     19     char *end;
     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 
     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 
     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 
     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);
     65 
     66     if(page == MAP_FAILED){
     67         fprintf(stderr,"cannot mmap region\n");
     68         return -1;
     69     }
     70 
     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;
     93     }
     94     return 0;
     95 }
     96