#include #include #include #include #include #include #include #include #include #include #include int main(int argc, char **argv) { uint8_t *data; int fd; unsigned long long offset; pid_t pid; int status; int opt; bool opt_fork = false; while ((opt = getopt(argc, argv, "f")) != -1) { switch (opt) { case 'f': opt_fork = true; break; } } if (argc <= optind) { fprintf(stderr, "%s: [-f] address\n", argv[0]); fprintf(stderr, "\t-f specifices if we should fork with the mmap\n"); exit(EXIT_FAILURE); } if (sscanf(argv[optind], "%lli", &offset) != 1) { fprintf(stderr, "Cannot determine mmap address from %s\n", argv[optind]); exit(EXIT_FAILURE); } if ((fd = open("/dev/mem", O_RDONLY)) < 0) { fprintf(stderr, "Cannot open /dev/mem\n"); exit(EXIT_FAILURE); } printf("mmap: 0x%llx..0x%llx\n", offset, offset + 4095); if ((data = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, fd, (off_t)offset)) == MAP_FAILED) { fprintf(stderr, "Cannot mmap 0x%llx\n", offset); exit(EXIT_FAILURE); } close(fd); if (opt_fork) { pid = fork(); if (pid == 0) { /* child */ _exit(0); } else { /* parent */ waitpid(pid, &status, 0); } } if (munmap(data, 4096) < 0) { fprintf(stderr, "Cannot munmap %p\n", data); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }