1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/mman.h>
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <fcntl.h>
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <string.h>
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int usage()
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr,"r [-b|-s] <address> [<value>]\n");
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return -1;
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint r_main(int argc, char *argv[])
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int width = 4, set = 0, fd;
162ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    unsigned addr, value, endaddr = 0;
172ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    unsigned long mmap_start, mmap_size;
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    void *page;
192ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    char *end;
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(argc < 2) return usage();
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(!strcmp(argv[1], "-b")) {
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        width = 1;
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        argc--;
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        argv++;
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else if(!strcmp(argv[1], "-s")) {
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        width = 2;
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        argc--;
30dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        argv++;
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(argc < 2) return usage();
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    addr = strtoul(argv[1], 0, 16);
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
362ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    end = strchr(argv[1], '-');
372ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    if (end)
382ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        endaddr = strtoul(end + 1, 0, 16);
392ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross
402ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    if (!endaddr)
412ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        endaddr = addr + width - 1;
422ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross
432ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    if (endaddr <= addr) {
442ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        fprintf(stderr, "invalid end address\n");
452ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        return -1;
462ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    }
472ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross
48dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(argc > 2) {
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        set = 1;
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        value = strtoul(argv[2], 0, 16);
51dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fd = open("/dev/mem", O_RDWR | O_SYNC);
54dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(fd < 0) {
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr,"cannot open /dev/mem\n");
56dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
57dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
58dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
592ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    mmap_start = addr & ~(PAGE_SIZE - 1);
602ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    mmap_size = endaddr - mmap_start + 1;
612ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    mmap_size = (mmap_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
622ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross
632ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    page = mmap(0, mmap_size, PROT_READ | PROT_WRITE,
642ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross                MAP_SHARED, fd, mmap_start);
65dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
66dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(page == MAP_FAILED){
67dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr,"cannot mmap region\n");
68dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
69dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
70dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
712ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross    while (addr <= endaddr) {
722ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        switch(width){
732ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        case 4: {
742ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross            unsigned *x = (unsigned*) (((unsigned) page) + (addr & 4095));
752ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross            if(set) *x = value;
762ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross            fprintf(stderr,"%08x: %08x\n", addr, *x);
772ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross            break;
782ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        }
792ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        case 2: {
802ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross            unsigned short *x = (unsigned short*) (((unsigned) page) + (addr & 4095));
812ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross            if(set) *x = value;
822ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross            fprintf(stderr,"%08x: %04x\n", addr, *x);
832ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross            break;
842ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        }
852ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        case 1: {
862ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross            unsigned char *x = (unsigned char*) (((unsigned) page) + (addr & 4095));
872ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross            if(set) *x = value;
882ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross            fprintf(stderr,"%08x: %02x\n", addr, *x);
892ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross            break;
902ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        }
912ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        }
922ad280f07a97e9ed0dfd86202abe659ad0ca9ccdColin Cross        addr += width;
93dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
94dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
95dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
96