-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe.c
63 lines (51 loc) · 1.58 KB
/
probe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <err.h>
#include <string.h>
#include "regions.c"
void dump_region (const char *region, off_t target, size_t length, fieldlist_t *fields) {
unsigned char *mem;
int fd;
off_t offset, foffset;
int pages;
int i, j;
int bytes;
for (i=0; fields[i].name; i++) {
if (strcmp(fields[i].name, ABSOFF) == 0) continue;
if (fields[i].bits < 1 || fields[i].bits % 8 != 0)
errx(2, "Bad field: %s (%d bits)\n", fields[i].name, fields[i].bits);
}
fd = open("/dev/mem", O_RDONLY);
if (fd < 0)
err(1, "/dev/mem");
offset = target % getpagesize();
pages = (offset + length-1)/getpagesize() + 1;
mem = mmap(NULL, pages*getpagesize(), PROT_READ, MAP_SHARED, fd, target - offset);
if (mem == MAP_FAILED)
err(1, "mmap");
printf("Region: %s (%p) (%zd bytes)\n", region, (void *) target, length);
foffset = 0;
for (i=0; fields[i].name; i++) {
if (strcmp(fields[i].name, ABSOFF) == 0) {
foffset = fields[i].bits;
continue;
}
printf(" %s.%4s:", region, fields[i].name);
bytes = fields[i].bits/8;
for (j=bytes-1; j>=0; j--) {
printf(" %02x", *(mem+offset+foffset+j));
}
foffset += bytes;
printf("\n");
}
munmap(mem, pages*getpagesize());
close(fd);
}
int main (void) {
int i;
for (i=0; regions[i].name; i++)
dump_region(regions[i].name, regions[i].target, regions[i].length, regions[i].fields);
return 0;
}