-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmmap_test.c
More file actions
72 lines (53 loc) · 1.33 KB
/
mmap_test.c
File metadata and controls
72 lines (53 loc) · 1.33 KB
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
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int physmem_fd;
int devmem_fd;
void hexdump(void *data, int size) {
for (int i = 0; i < size; i++) {
printf("%02x ", *(unsigned char *)(data + i));
if ((i+1) % 8 == 0 && i != 0) {
printf(" ");
}
if ((i+1) % 16 == 0 && i != 0 ) {
printf("\n");
}
}
}
void map_physmem(off_t offset) {
void *physmem_addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, physmem_fd, offset);
void *devmem_addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, devmem_fd, offset);
if (physmem_addr != MAP_FAILED) {
printf("/dev/physmem offset: %lx\n", offset);
hexdump(physmem_addr, 64);
} else {
perror("/dev/physmem mmap");
}
if (devmem_addr != MAP_FAILED) {
printf("/dev/mem offset: %lx\n", offset);
hexdump(devmem_addr, 64);
} else {
perror("/dev/mem mmap");
}
}
int main(int argc, char *argv[]) {
physmem_fd = open("/dev/physmem", O_RDWR);
devmem_fd = open("/dev/mem", O_RDWR);
if (devmem_fd == -1) {
perror("open /dev/mem");
exit(-1);
}
if (physmem_fd == -1) {
perror("open /dev/physmem");
exit(-1);
}
printf("Trying at offset 0x00\n\n");
map_physmem(0x00);
printf("\nTrying beyond 1mb...\n\n");
map_physmem(0x100000);
close(physmem_fd);
close(devmem_fd);
return 0;
}