-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.c
More file actions
37 lines (30 loc) · 971 Bytes
/
reader.c
File metadata and controls
37 lines (30 loc) · 971 Bytes
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
/* Privileged helper tool to read data from /proc/mem */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/mman.h>
#include <fcntl.h>
#define PAGE_SIZE 4096
int main(int argc, char** argv) {
if(argc < 4) {
fprintf(stderr, "usage %s <address> <size> <file>", argv[0]);
return -1;
}
uint64_t phys_addr = strtoull(argv[1], 0, 0);
uint64_t size = strtoull(argv[2], 0, 0);
char* filename = argv[3];
int mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
uint8_t *map_base = mmap(NULL,
(size + 2 * PAGE_SIZE) - (size % PAGE_SIZE),
PROT_READ | PROT_WRITE,
MAP_SHARED,
mem_fd,
phys_addr & 0x7ffffffff000ull); // phys_addr should be page-aligned.
uint8_t* virt_addr = map_base + (phys_addr & 0xfff);
printf("reading %lld bytes @ 0x%llx\n", size, phys_addr);
FILE* f = fopen(filename, "wb");
fwrite(virt_addr, 1, size, f);
fclose(f);
return 0;
}