2014-01-17 09:33:52 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdbool.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-01-17 09:33:52 +08:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#if __LP64__
|
|
|
|
#define strtoptr strtoull
|
|
|
|
#else
|
|
|
|
#define strtoptr strtoul
|
|
|
|
#endif
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
static int usage()
|
|
|
|
{
|
|
|
|
fprintf(stderr,"r [-b|-s] <address> [<value>]\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int r_main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if(argc < 2) return usage();
|
|
|
|
|
2014-01-17 09:33:52 +08:00
|
|
|
int width = 4;
|
2009-03-04 11:32:55 +08:00
|
|
|
if(!strcmp(argv[1], "-b")) {
|
|
|
|
width = 1;
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
} else if(!strcmp(argv[1], "-s")) {
|
|
|
|
width = 2;
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(argc < 2) return usage();
|
2014-01-17 09:33:52 +08:00
|
|
|
uintptr_t addr = strtoptr(argv[1], 0, 16);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2014-01-17 09:33:52 +08:00
|
|
|
uintptr_t endaddr = 0;
|
|
|
|
char* end = strchr(argv[1], '-');
|
2011-01-20 09:06:25 +08:00
|
|
|
if (end)
|
2014-01-17 09:33:52 +08:00
|
|
|
endaddr = strtoptr(end + 1, 0, 16);
|
2011-01-20 09:06:25 +08:00
|
|
|
|
|
|
|
if (!endaddr)
|
|
|
|
endaddr = addr + width - 1;
|
|
|
|
|
|
|
|
if (endaddr <= addr) {
|
2014-01-17 09:33:52 +08:00
|
|
|
fprintf(stderr, "end address <= start address\n");
|
2011-01-20 09:06:25 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:33:52 +08:00
|
|
|
bool set = false;
|
|
|
|
uint32_t value = 0;
|
2009-03-04 11:32:55 +08:00
|
|
|
if(argc > 2) {
|
2014-01-17 09:33:52 +08:00
|
|
|
set = true;
|
2009-03-04 11:32:55 +08:00
|
|
|
value = strtoul(argv[2], 0, 16);
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:33:52 +08:00
|
|
|
int fd = open("/dev/mem", O_RDWR | O_SYNC);
|
2009-03-04 11:32:55 +08:00
|
|
|
if(fd < 0) {
|
|
|
|
fprintf(stderr,"cannot open /dev/mem\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-01-17 09:33:52 +08:00
|
|
|
|
|
|
|
off64_t mmap_start = addr & ~(PAGE_SIZE - 1);
|
|
|
|
size_t mmap_size = endaddr - mmap_start + 1;
|
2011-01-20 09:06:25 +08:00
|
|
|
mmap_size = (mmap_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
|
|
|
|
|
2014-01-17 09:33:52 +08:00
|
|
|
void* page = mmap64(0, mmap_size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, mmap_start);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
if(page == MAP_FAILED){
|
|
|
|
fprintf(stderr,"cannot mmap region\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-01-20 09:06:25 +08:00
|
|
|
while (addr <= endaddr) {
|
|
|
|
switch(width){
|
|
|
|
case 4: {
|
2014-01-17 09:33:52 +08:00
|
|
|
uint32_t* x = (uint32_t*) (((uintptr_t) page) + (addr & 4095));
|
2011-01-20 09:06:25 +08:00
|
|
|
if(set) *x = value;
|
2014-01-17 09:33:52 +08:00
|
|
|
fprintf(stderr,"%08"PRIxPTR": %08x\n", addr, *x);
|
2011-01-20 09:06:25 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
2014-01-17 09:33:52 +08:00
|
|
|
uint16_t* x = (uint16_t*) (((uintptr_t) page) + (addr & 4095));
|
2011-01-20 09:06:25 +08:00
|
|
|
if(set) *x = value;
|
2014-01-17 09:33:52 +08:00
|
|
|
fprintf(stderr,"%08"PRIxPTR": %04x\n", addr, *x);
|
2011-01-20 09:06:25 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: {
|
2014-01-17 09:33:52 +08:00
|
|
|
uint8_t* x = (uint8_t*) (((uintptr_t) page) + (addr & 4095));
|
2011-01-20 09:06:25 +08:00
|
|
|
if(set) *x = value;
|
2014-01-17 09:33:52 +08:00
|
|
|
fprintf(stderr,"%08"PRIxPTR": %02x\n", addr, *x);
|
2011-01-20 09:06:25 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addr += width;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|