diff options
| author | Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org> | 2021-06-02 18:24:37 +0200 |
|---|---|---|
| committer | Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org> | 2021-06-02 18:26:35 +0200 |
| commit | 5f4911cae305631190c9df1cef37a52a1e79bc8c (patch) | |
| tree | 9eaef61c4811d67e560f62d275da09db482a400a | |
| parent | 88d426f440716f57bb9f60459514db52fd06dcac (diff) | |
| download | exynos-gpio-tool-5f4911cae305631190c9df1cef37a52a1e79bc8c.tar.gz exynos-gpio-tool-5f4911cae305631190c9df1cef37a52a1e79bc8c.tar.bz2 exynos-gpio-tool-5f4911cae305631190c9df1cef37a52a1e79bc8c.zip | |
Start adding abstract memory access for better tracing
Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
| -rw-r--r-- | Makefile | 1 | ||||
| -rw-r--r-- | memory.c | 104 | ||||
| -rw-r--r-- | memory.h | 28 |
3 files changed, 133 insertions, 0 deletions
@@ -5,6 +5,7 @@ SOURCE := \ exynos4412_gpios.c \ exynos4412_gpios_data.c \ main.c \ + memory.c \ modem_gpios_data.c \ tests.c \ diff --git a/memory.c b/memory.c new file mode 100644 index 0000000..d05cee2 --- /dev/null +++ b/memory.c @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include <sys/mman.h> +#include <sys/stat.h> + +#include "memory.h" + +struct memory_mapping { + unsigned int *mmap_phys_base; + unsigned int *mmap_virt_base; + unsigned int mmap_length; +}; + +static bool addr_is_valid(struct memory_mapping *memory_mapping, + unsigned int *addr) +{ + if (addr < memory_mapping->mmap_phys_base) + return false; + + if (addr > (memory_mapping->mmap_phys_base + memory_mapping->mmap_length)) + return false; + + return true; +} + +int init_memory_mapping(int debug, struct memory_mapping *memory_mapping, + off_t addr, size_t length) +{ + int fd; + int rc; + char *devmem = "/dev/mem"; + + fd = open(devmem, O_RDWR | O_SYNC); + if (fd == -1) { + rc = errno; + printf("%s: opening %s failed with error %d: %s\n", + __func__, devmem, rc, strerror(rc)); + return -1; + } + + memory_mapping->mmap_virt_base = mmap(0, length, PROT_READ|PROT_WRITE, + MAP_SHARED, fd, addr); + if (memory_mapping->mmap_virt_base == MAP_FAILED) { + rc = errno; + printf("%s: mmap on %s failed with error %d: %s\n", + __func__, devmem, rc, strerror(rc)); + return -1; + } + + rc = close(fd); + if (rc == -1) { + rc = errno; + printf("Closing %s failed with error %d: %s\n", + devmem, rc, strerror(rc)); + return -1; + } + + if (debug) + printf("%s: mapped address 0 @ %p\n", __func__, + memory_mapping->mmap_virt_base); + + return 0; +} + +int read_word(int debug, struct memory_mapping *memory_mapping, + unsigned int addr, unsigned int *result) +{ + unsigned int *virt_addr; + + if (!addr_is_valid(memory_mapping, (unsigned int *)addr)) + return -EDOM; + + virt_addr = memory_mapping->mmap_virt_base + addr; + + *result = *virt_addr; + + if (debug) + printf("%s: debug=%d, addr=0x%x, result=0x%x\n", + __func__, debug, addr, *result); + + return 0; +} diff --git a/memory.h b/memory.h new file mode 100644 index 0000000..5a03e4d --- /dev/null +++ b/memory.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef MEMORY_H +#define MEMORY_H + +struct memory_mapping; + +int init_memory_mapping(int debug, struct memory_mapping *memory_mapping, + off_t addr, size_t length); +int read_word(int debug, struct memory_mapping *memory_mapping, + unsigned int addr, unsigned int *result); + +#endif /* MEMORY_H */ |
