forked from hwangcc23/hexdumpmem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexdumpmem.c
308 lines (260 loc) · 6.86 KB
/
hexdumpmem.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* hexudmp.c
*
* hexdumpmem is hexdump-like tool to dump memory and write memory. When
* developing a system on a SoC, I usually need to dump or write
* memory-map registers of the SoC for debugging. I write this program
* to do it on my developing Android phone.
*
* Copyright (C) 2013 Chih-Chyuan Hwang
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/errno.h>
#include <string.h>
#define DEFAULT_DUMP_LEN 368
#define DEFAULT_DUMP_SZ 4
#define DEV_MEM "/dev/mem"
#define DUMP_BYTE_PER_LINE 16
#define ROUND_DOWN(__value, __align) ((__value) & ~((__align) - 1))
#define ROUND_UP(__value, __align) (((__value) + (__align) - 1) & ~((__align) - 1))
#define LOGV(f, args...) fprintf(stdout, f, ## args)
#define LOGD(f, args...) do { if (debug) fprintf(stderr, f, ## args); } while (0)
#define LOGE(f, args...) fprintf(stderr, "Error: " f, ## args)
#define OUTPUT(f, args...) fprintf(stdout, f, ## args)
static int debug;
static const struct option options[] =
{
{ "address", 1, 0, 'a' },
{ "size", 1, 0, 's' },
{ "len", 1, 0, 'l' },
{ "write", 1, 0, 'w' },
{ "debug", 0, 0, 'd' },
{ "help", 0, 0, 'h' },
{ NULL, 0, 0, 0 },
};
static const char *optstr = "a:s:l:w:dh";
static void usage(void)
{
LOGV("Usage: hexdumpmem [options] -a|--address <memory address (hex) to dump>\n");
LOGV(" or: hexdumpmem -w <fill pattern (hex)> [options] -a|--address <memory address (hex) to fill>\n");
LOGV("Options:\n");
LOGV(" -s, --size <size> Select the size of dump/fill unit\n");
LOGV(" -l, --len <length (hex)> Select the length of the dump/fill region\n");
LOGV(" -d, --debug Enable the debug log\n");
LOGV(" -h, --help Show help message and exit\n");
}
static unsigned char readb(const void *addr)
{
return *(volatile unsigned char *)addr;
}
static void writeb(unsigned char val, void *addr)
{
*(volatile unsigned char *)addr = val;
}
static unsigned short readw(const void *addr)
{
return *(volatile unsigned short *)addr;
}
static void writew(unsigned short val, void *addr)
{
*(volatile unsigned short *)addr = val;
}
static unsigned int readl(const void *addr)
{
return *(volatile unsigned int *)addr;
}
static void writel(unsigned int val, void *addr)
{
*(volatile unsigned int *)addr = val;
}
static int memdump(const void *map, unsigned int addr,
unsigned int len, unsigned int size)
{
unsigned int start, end, line, byte;
if (!map) {
LOGE("Invalid map pointer\n");
return -1;
}
if (!len) {
LOGE("Invalid length\n");
return -1;
}
if (size != 1 && size != 2 && size != 4) {
LOGE("Invalid size\n");
return -1;
}
start = ROUND_DOWN(addr, DUMP_BYTE_PER_LINE);
end = ROUND_UP(addr + len, DUMP_BYTE_PER_LINE);
LOGD("Dump memory 0x%08X ~ 0x%08X\n", start, end);
LOGD("Before adjust, map = %p\n", map);
map += start - ROUND_DOWN(addr, getpagesize());
LOGD("After adjust, map = %p\n", map);
switch (size) {
case 1:
OUTPUT("%-8s %2X %2X %2X %2X %2X %2X %2X %2X ",
"addr", 0, 1, 2, 3, 4, 5, 6, 7);
OUTPUT("%2X %2X %2X %2X %2X %2X %2X %2X\n",
8, 9, 10, 11, 12, 13, 14, 15);
OUTPUT("==============================");
OUTPUT("============================\n");
break;
case 2:
OUTPUT("%-8s %4X %4X %4X %4X %4X %4X %4X %4X\n",
"addr", 0, 2, 4, 6, 8, 10, 12, 14);
OUTPUT("==============================");
OUTPUT("====================\n");
break;
case 4:
OUTPUT("%-8s %8X %8X %8X %8X\n", "addr", 0, 4, 8, 12);
OUTPUT("==============================================\n");
break;
default:
break;
}
for (line = 0; line < (end - start); line += DUMP_BYTE_PER_LINE) {
OUTPUT("%08Xh: ", start + line);
for (byte = 0; byte < DUMP_BYTE_PER_LINE; byte += size) {
switch (size) {
case 1:
OUTPUT("%02X ", readb(map + line + byte));
break;
case 2:
OUTPUT("%04X ", readw(map + line + byte));
break;
case 4:
OUTPUT("%08X ", readl(map + line + byte));
break;
default:
break;
}
}
OUTPUT("\n");
}
return 0;
}
static int memfill(void *map, unsigned int addr, unsigned int len,
unsigned int size, unsigned int val)
{
unsigned int byte;
if (!map) {
LOGE("Invalid map pointer\n");
return -1;
}
if (!len) {
LOGE("Invalid length\n");
return -1;
}
if (size != 1 && size != 2 && size != 4) {
LOGE("Invalid size\n");
return -1;
}
if (addr & (size - 1)) {
LOGE("address and size are not aligned\n");
return -1;
}
LOGD("Before adjust, map = %p\n", map);
map += addr - ROUND_DOWN(addr, getpagesize());
LOGD("After adjust, map = %p\n", map);
for (byte = 0; byte < len; byte += size) {
switch (size) {
case 1:
LOGD("Write 0x%02X to *(%p)\n",
(unsigned char)val, map + byte);
writeb(val, map + byte);
break;
case 2:
LOGD("Write 0x%02X to *(%p)\n",
(unsigned short)val, map + byte);
writew(val, map + byte);
break;
case 4:
LOGD("Write 0x%02X to *(%p)\n",
(unsigned int)val, map + byte);
writel(val, map + byte);
break;
default:
break;
}
}
return 0;
}
int main(int argc, char **argv)
{
int loptidx, c, fd, err = 0;
unsigned int addr = 0xffffffff;
unsigned int len = DEFAULT_DUMP_LEN, size = DEFAULT_DUMP_SZ;
unsigned int wr = 0, val;
void *map;
for (;;) {
c = getopt_long(argc, argv, optstr, options, &loptidx);
if (c == -1)
break;
switch (c) {
case 'a':
addr = strtoul(optarg, NULL, 16);
break;
case 's':
size = atoi(optarg);
break;
case 'l':
len = strtoul(optarg, NULL, 16);
break;
case 'w':
wr = 1;
val = strtoul(optarg, NULL, 16);
break;
case 'd':
debug = 1;
break;
case 'h':
usage();
return 0;
default:
LOGE("Unknown argument: %c\n", c);
break;
}
}
if (argc != optind || addr == 0xffffffff) {
LOGE("Invalid arguments or missing input address\n");
usage();
return -1;
}
LOGV("Memory region 0x%08x ~ 0x%08x\n", addr, addr + len - 1);
LOGD("addr = 0x%08x, len = 0x%08x, size = %d\n", addr, len, size);
LOGD("wr = %d, val = 0x%08x\n", wr, val);
fd = open(DEV_MEM, O_RDWR | O_SYNC);
if (fd == -1) {
LOGE("Fail to open %s (%s)\n", DEV_MEM, strerror(errno));
return -1;
}
LOGD("mmap 0x%08x ~ 0x%08x\n",
ROUND_DOWN(addr, getpagesize()),
ROUND_DOWN(addr, getpagesize()) + ROUND_UP(len, getpagesize()));
map = mmap(NULL, ROUND_UP(len, getpagesize()), PROT_READ | PROT_WRITE,
MAP_SHARED, fd, ROUND_DOWN(addr, getpagesize()));
if (map == MAP_FAILED) {
LOGE("mmap failed (%s)\n", strerror(errno));
err = -1;
goto exit;
}
close(fd);
if (!wr) {
err = memdump(map, addr, len, size);
} else {
err = memfill(map, addr, len, size, val);
}
munmap(map, ROUND_UP(len, getpagesize()));
exit:
return err;
}