forked from VCTLabs/devmem2
-
Notifications
You must be signed in to change notification settings - Fork 13
/
devmem2.c
259 lines (228 loc) · 7.46 KB
/
devmem2.c
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* devmem2.c: Simple program to read/write from/to any location in memory.
*
* Copyright (C) 2000, Jan-Derk Bakker ([email protected])
*
*
* This software has been developed for the LART computing board
* (http://www.lart.tudelft.nl/). The development has been sponsored by
* the Mobile MultiMedia Communications (http://www.mmc.tudelft.nl/)
* and Ubiquitous Communications (http://www.ubicom.tudelft.nl/)
* projects.
*
* Copyright (C) 2015, Trego Ltd.
*
* 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 2 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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <inttypes.h>
#define printerr(fmt,...) do { fprintf(stderr, fmt, ## __VA_ARGS__); fflush(stderr); } while(0)
#define VER_STR "devmem version T/C (http://git.io/vZ5iD) rev.0.3e"
int f_dbg = 0;
static void usage(const char *cmd)
{
fprintf(stderr, "\nUsage:\t%s [-switches] address [ type [ data ] ]\n"
"\taddress : memory address to act upon\n"
"\ttype : access operation type : [b]yte, [h]alfword, [w]ord\n"
"\tdata : data to be written\n\n"
"Switches:\n"
"\t-r : read back after write\n"
"\t-a : do not check alignment\n"
"\t--version | -V : print version\n"
"\n",
cmd);
}
int main(int argc, char **argv)
{
int fd;
void *map_base, *virt_addr;
unsigned long read_result = -1, writeval=-1;
uint64_t target;
int access_type = 'w';
int access_size = 4;
unsigned int pagesize = (unsigned)getpagesize(); /* or sysconf(_SC_PAGESIZE) */
unsigned int map_size = pagesize;
unsigned offset;
char *endp = NULL;
int f_readback = 0; // flag to read back after write
int f_align_check = 1; // flag to require alignment
const char *progname = argv[0];
int opt;
opterr = 0;
while ((opt = getopt(argc, argv, "+raAdV")) != -1) {
switch(opt) {
case 'r':
f_readback = 1;
break;
case 'a':
f_align_check = 0;
break;
case 'A':
// Absolute address mode. Does nothing now, for future compat.;
break;
case 'd':
f_dbg = 1;
break;
case 'V':
printf(VER_STR "\n");
exit(0);
default:
if ( (!argv[optind]) || 0 == strcmp(argv[optind], "--help")) {
usage(progname);
exit(1);
}
if (0 == strncmp(argv[optind], "--vers", 6)) {
printf(VER_STR "\n");
exit(0);
}
printerr("Unknown long option: %s\n", argv[optind]);
exit(2);
}
}
argc -= optind - 1;
argv += optind - 1;
if (argc < 2 || argc > 4) {
usage(progname);
exit(1);
}
if (argc > 2) {
if (!isdigit(argv[1][0])) {
// Allow access_type be 1st arg, then swap 1st and 2nd
char *t = argv[2];
argv[2] = argv[1];
argv[1] = t;
}
access_type = tolower(argv[2][0]);
if (argv[2][1] )
access_type = '?';
}
errno = 0;
target = strtoull(argv[1], &endp, 0);
if (errno != 0 || (endp && 0 != *endp)) {
printerr("Invalid memory address: %s\n", argv[1]);
exit(2);
}
switch (access_type) {
case 'b':
access_size = 1;
break;
case 'w':
access_size = 4;
break;
case 'h':
access_size = 2;
break;
default:
printerr("Illegal data type: %s\n", argv[2]);
exit(2);
}
if ((target + access_size -1) < target) {
printerr("ERROR: rolling over end of memory\n");
exit(2);
}
if ( (sizeof(off_t) < sizeof(int64_t)) && (target > UINT32_MAX) ) {
printerr("The address %s > 32 bits. Try to rebuild in 64-bit mode.\n", argv[1]);
// Consider mmap2() instead of this check
exit(2);
}
offset = (unsigned int)(target & (pagesize-1));
if (offset + access_size > pagesize ) {
// Access straddles page boundary: add another page:
map_size += pagesize;
}
if (f_dbg) {
printerr("Address: %#" PRIX64 " op.size=%d\n", target, access_size);
}
if (f_align_check && offset & (access_size - 1)) {
printerr("ERROR: address not aligned on %d!\n", access_size);
exit(2);
}
fd = open("/dev/mem", O_RDWR | O_SYNC);
if (fd == -1) {
printerr("Error opening /dev/mem (%d) : %s\n", errno, strerror(errno));
exit(1);
}
//printf("/dev/mem opened.\n");
//fflush(stdout);
map_base = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_SHARED,
fd,
target & ~((typeof(target))pagesize-1));
if (map_base == (void *) -1) {
printerr("Error mapping (%d) : %s\n", errno, strerror(errno));
exit(1);
}
//printf("Memory mapped at address %p.\n", map_base);
//fflush(stdout);
virt_addr = map_base + offset;
if (argc > 3) {
errno = 0;
writeval = strtoul(argv[3], &endp, 0);
if (errno || (endp && 0 != *endp)) {
printerr("Invalid data value: %s\n", argv[3]);
exit(2);
}
if (access_size < sizeof(writeval) && 0 != (writeval >> (access_size * 8))) {
printerr("ERROR: Data value %s does not fit in %d byte(s)\n", argv[3], access_size);
exit(2);
}
switch (access_size) {
case 1:
*((volatile uint8_t *) virt_addr) = writeval;
break;
case 2:
*((volatile uint16_t *) virt_addr) = writeval;
break;
case 4:
*((volatile uint32_t *) virt_addr) = writeval;
break;
}
if (f_dbg) {
printerr("Address: %#" PRIX64 " Written: %#lX\n", target, writeval);
fflush(stdout);
}
}
if (argc <= 3 || f_readback) {
switch (access_size) {
case 1:
read_result = *((volatile uint8_t *) virt_addr);
break;
case 2:
read_result = *((volatile uint16_t *) virt_addr);
break;
case 4:
read_result = *((volatile uint32_t *) virt_addr);
break;
}
//printf("Value at address 0x%lld (%p): 0x%lu\n", (long long)target, virt_addr, read_result);
//fflush(stdout);
if (f_readback && argc > 3)
printf("Written 0x%lx; readback 0x%lx\n", writeval, read_result);
else
printf("%08lX\n", read_result);
fflush(stdout);
}
if (munmap(map_base, map_size) != 0) {
printerr("ERROR munmap (%d) %s\n", errno, strerror(errno));
}
close(fd);
return 0;
}