-
Notifications
You must be signed in to change notification settings - Fork 1
/
mem.c
206 lines (172 loc) · 3.09 KB
/
mem.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
/*
* Copyright (C) 2016 Masahiro Yamada <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include "cmd.h"
#include "err.h"
#include "errno.h"
#include "io.h"
#include "utils.h"
#define MAPSIZE (4 * 1024)
#define MEMDEV "/dev/mem"
static void *xmmap(unsigned long offset)
{
int fd;
void *addr;
fd = open(MEMDEV, O_RDWR);
if (fd < 0)
ERR(MEMDEV);
addr = mmap(NULL, MAPSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
offset);
if (addr == MAP_FAILED)
ERR("mmap");
return addr;
}
static int do_md(int argc, char *argv[])
{
int width;
unsigned long phys_addr;
size_t size = 0x100;
void *virt_addr;
char *ep;
size_t left, cnt_in_line;
if (argc < 2)
return -EINVAL;
switch (*argv[0]) {
case 'b':
width = 1;
break;
case 'w':
width = 2;
break;
case 'd':
default:
width = 4;
break;
};
phys_addr = strtoul(argv[1], &ep, 16);
if (*ep) {
fprintf(stderr, "invalid address format \"%s\"\n", argv[1]);
return -EINVAL;
}
if (argc >= 3) {
size = strtoul(argv[2], &ep, 16);
if (*ep) {
fprintf(stderr, "invalid size format \"%s\"\n",
argv[2]);
return -EINVAL;
}
}
virt_addr = xmmap(phys_addr);
left = size / width;
cnt_in_line = 16 / width;
while (left) {
int i, cnt;
cnt = min(cnt_in_line, left);
printf("%08lx:", phys_addr);
for (i = 0; i < cnt; i++) {
switch (width) {
case 1:
printf(" %02x", read8(virt_addr));
break;
case 2:
printf(" %04x", read16(virt_addr));
break;
case 4:
printf(" %08x", read32(virt_addr));
break;
default:
BUG();
}
phys_addr += width;
virt_addr += width;
}
left -= cnt;
printf("\n");
}
return 0;
}
struct command cmd_md = {
.name = "md",
.help = "memory display",
.long_help = "md[.b/w/l] address size",
.func = do_md,
};
int do_mm(int argc, char *argv[])
{
return 0;
}
struct command cmd_mm = {
.name = "mm",
.help = "memory modify",
.long_help = "mm[.b/w/l] address size",
.func = do_md,
};
int do_mw(int argc, char *argv[])
{
int width;
unsigned long phys_addr;
uint32_t value;
size_t size = 0x100;
void *virt_addr;
char *ep;
size_t left;
if (argc < 2)
return -EINVAL;
switch (*argv[0]) {
case 'b':
width = 1;
break;
case 'w':
width = 2;
break;
case 'd':
default:
width = 4;
break;
};
phys_addr = strtoul(argv[1], &ep, 16);
if (*ep) {
fprintf(stderr, "invalid address format \"%s\"\n", argv[1]);
return -EINVAL;
}
value = strtoul(argv[2], &ep, 16);
if (argc >= 4) {
size = strtoul(argv[3], &ep, 16);
if (*ep) {
fprintf(stderr, "invalid size format \"%s\"\n",
argv[2]);
return -EINVAL;
}
}
virt_addr = xmmap(phys_addr);
left = size / width;
while (left--) {
switch (width) {
case 1:
write8(value, virt_addr);
break;
case 2:
write16(value, virt_addr);
break;
case 4:
write32(value, virt_addr);
break;
default:
BUG();
}
virt_addr += width;
}
return 0;
}
struct command cmd_mw = {
.name = "mw",
.help = "memory write",
.long_help = "mw[.b/w/l] address value count",
.func = do_mw,
};