summaryrefslogtreecommitdiffstats
path: root/memory.c
blob: d05cee25a9555a7e765eaf8cd9ae8b60b9f8708d (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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;
}