Repository navigation
Expand file tree
/
Copy pathdevmem2.c
More file actions
175 lines (145 loc) · 5.44 KB
/
Copy pathdevmem2.c
File metadata and controls
175 lines (145 loc) · 5.44 KB
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
/*
* devmem2.c: Simple program to read/write from/to any location in memory.
*
* Copyright (C) 2000, Jan-Derk Bakker (jdb@lartmaker.nl)
*
*
* 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.
*
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
// based on...
// http://stackoverflow.com/a/3974138/3152071
//assumes little endian
void int2binstr(char *buffer, size_t const size, void const * const ptr)
{
unsigned char *b = (unsigned char*) ptr;
unsigned char byte;
int i, j;
for (i=size-1;i>=0;i--)
{
for (j=7;j>=0;j--)
{
byte = (b[i] >> j) & 1;
*buffer = '0' + byte;
buffer++;
}
*buffer = ' '; // makes it easier to see
buffer++;
}
*buffer = 0x00; // null terminal string
}
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int main(int argc, char **argv) {
int fd;
void *map_base, *virt_addr;
unsigned long read_result, writeval;
off_t target;
int access_type = 'w';
char binstrbuffer[ (sizeof( unsigned long )*9) + 1]; // 8 bits per byte, space between bytes, plus null terminator
if(argc < 2) {
fprintf(stderr, "\nUsage:\t%s { 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",
argv[0]);
exit(1);
}
target = strtoul(argv[1], 0, 0);
if(argc > 2)
access_type = tolower(argv[2][0]);
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
// printf("/dev/mem opened.\n");
fflush(stdout);
/* Map one page */
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
if(map_base == (void *) -1) FATAL;
// printf("Memory mapped at address %p.\n", map_base);
virt_addr = map_base + (target & MAP_MASK);
printf("Address 0x%llx (%p):\n", (long long)target, virt_addr );
fflush(stdout);
switch(access_type) {
case 'b':
read_result = *((unsigned char *) virt_addr);
break;
case 'h':
read_result = *((unsigned short *) virt_addr);
break;
case 'w':
read_result = *((unsigned long *) virt_addr);
break;
default:
fprintf(stderr, "Illegal data type '%c'.\n", access_type);
exit(2);
}
int2binstr( binstrbuffer , sizeof( read_result ) , &read_result );
printf("Read : 0x%08lx 0b%s\n" , read_result , binstrbuffer );
fflush(stdout);
if(argc > 3) {
writeval = strtoul(argv[3], 0, 0);
switch(access_type) {
case 'b':
*((unsigned char *) virt_addr) = writeval;
read_result = *((unsigned char *) virt_addr);
break;
case 'h':
*((unsigned short *) virt_addr) = writeval;
read_result = *((unsigned short *) virt_addr);
break;
case 'w':
*((unsigned long *) virt_addr) = writeval;
read_result = *((unsigned long *) virt_addr);
break;
}
int2binstr( binstrbuffer , sizeof( writeval ) , &writeval );
printf("Written : 0x%08lx 0b%s\n" , writeval , binstrbuffer );
read_result = *((unsigned long *) virt_addr);
int2binstr( binstrbuffer , sizeof( read_result ) , &read_result );
printf("Readback: 0x%08lx 0b%s\n" , read_result , binstrbuffer );
read_result = *((unsigned long *) virt_addr);
int2binstr( binstrbuffer , sizeof( read_result ) , &read_result );
printf("Readback: 0x%08lx 0b%s\n" , read_result , binstrbuffer );
read_result = *((unsigned long *) virt_addr);
int2binstr( binstrbuffer , sizeof( read_result ) , &read_result );
printf("Readback: 0x%08lx 0b%s\n" , read_result , binstrbuffer );
read_result = *((unsigned long *) virt_addr);
int2binstr( binstrbuffer , sizeof( read_result ) , &read_result );
printf("Readback: 0x%08lx 0b%s\n" , read_result , binstrbuffer );
fflush(stdout);
}
if(munmap(map_base, MAP_SIZE) == -1) FATAL;
close(fd);
return 0;
}