[Cryptech-Commits] [core/novena_eim] 01/01: add mmap for eim peeking and poking
git at cryptech.is
git at cryptech.is
Thu Nov 6 21:41:37 UTC 2014
This is an automated email from the git hooks/post-receive script.
paul at psgd.org pushed a commit to branch master
in repository core/novena_eim.
commit 62c914ca659dc7201a70113ac76c4a5582351f0c
Author: Paul Selkirk <paul at psgd.org>
Date: Thu Nov 6 16:41:16 2014 -0500
add mmap for eim peeking and poking
---
src/sw/mmap.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 130 insertions(+)
diff --git a/src/sw/mmap.c b/src/sw/mmap.c
new file mode 100644
index 0000000..6157947
--- /dev/null
+++ b/src/sw/mmap.c
@@ -0,0 +1,130 @@
+/* adapted from https://github.com/bunnie/novena-gpbb-example/blob/master/novena-gpbb.c */
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <time.h>
+#include <sys/ioctl.h>
+#include <arpa/inet.h>
+#include <ctype.h>
+
+static int fd = 0;
+static uint32_t *mem_32 = NULL;
+static uint16_t *mem_16 = NULL;
+static uint8_t *mem_8 = NULL;
+static void *prev_mem_range = NULL;
+
+void map_mem(off_t offset)
+{
+ void *mem_range = (void *)(offset & ~0xFFFF);
+
+ if (mem_range == prev_mem_range)
+ return;
+
+ //fprintf(stderr, "New range detected. Reopening at memory range %p\n", mem_range);
+ prev_mem_range = mem_range;
+
+ if (mem_32)
+ munmap((void *)mem_32, 0xFFFF);
+ if (fd)
+ close(fd);
+
+ fd = open("/dev/mem", O_RDWR);
+ if (fd < 0) {
+ perror("Unable to open /dev/mem");
+ fd = 0;
+ return;
+ }
+
+ mem_32 = (uint32_t *)mmap(NULL, 0xffff, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset&~0xFFFF);
+ if (mem_32 == MAP_FAILED) {
+ perror("Unable to mmap file");
+
+ if (close(fd) < 0)
+ perror("Also couldn't close file");
+
+ fd=0;
+ return;
+ }
+ mem_16 = (uint16_t *)mem_32;
+ mem_8 = (uint8_t *)mem_32;
+}
+
+uint32_t read_kernel_memory(off_t offset, size_t size)
+{
+ uint32_t result;
+
+ //fprintf(stderr, "read_kernel_memory(0x%04x, %d)\n", (unsigned int)offset, (int)size);
+ map_mem(offset);
+
+ off_t scaled_offset = (offset-(offset&~0xFFFF));
+ //fprintf(stderr, "Returning offset 0x%08x\n", scaled_offset);
+ if (size == 1)
+ result = mem_8[scaled_offset/sizeof(uint8_t)];
+ else if (size == 2)
+ result = mem_16[scaled_offset/sizeof(uint16_t)];
+ else
+ result = mem_32[scaled_offset/sizeof(uint32_t)];
+
+ return result;
+}
+
+uint32_t write_kernel_memory(off_t offset, uint32_t value, size_t size)
+{
+ uint32_t old_value = read_kernel_memory(offset, size);
+ off_t scaled_offset = (offset-(offset&~0xFFFF));
+ //printf("write_kernel_memory(%x, %x, %d)\n", (uint32_t)offset, value, size);
+ if (size == 1)
+ mem_8[scaled_offset/sizeof(uint8_t)] = value;
+ else if (size == 2)
+ mem_16[scaled_offset/sizeof(uint16_t)] = value;
+ else
+ mem_32[scaled_offset/sizeof(uint32_t)] = value;
+ return old_value;
+}
+
+int
+main(int argc, char *argv[])
+{
+ off_t offset;
+ size_t length, size = 2;
+ int i, result;
+ unsigned long value;
+
+ if (argc < 4 || argc > 5) {
+ fprintf(stderr, "usage: %s r offset length [size]\n", argv[0]);
+ fprintf(stderr, "usage: %s w offset value [size]\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ offset = (off_t)strtoul(argv[2], NULL, 0);
+ if (argc > 4)
+ size = atoi(argv[4]);
+ if (argv[1][0] == 'r') {
+ length = atoi(argv[3]);
+ for (i = 0; i < length; i += size) {
+ result = read_kernel_memory(offset + i, size);
+ if (size == 1)
+ printf("%02x ", result & 0xff);
+ else if (size == 2)
+ printf("%04x ", result & 0xffff);
+ else if (size == 4)
+ printf("%08x ", result & 0xffffffff);
+ else {
+ fprintf(stderr, "bad size %d\n", (int)size);
+ exit(EXIT_FAILURE);
+ }
+ }
+ printf("\n");
+ }
+ else if (argv[1][0] == 'w') {
+ value = strtoul(argv[3], NULL, 0);
+ write_kernel_memory(offset, value, size);
+ }
+
+ exit(EXIT_SUCCESS);
+}
More information about the Commits
mailing list