-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrz-svd.c
393 lines (354 loc) · 8.17 KB
/
rz-svd.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <rz_core.h>
#include <yxml.h>
#define XMLBUFSIZE 4096
#define VALUESIZE 2048
/**
* \brief The main module - iterator
*
* svd_iter contains the information
* that a single Rizin comment needs.
*
* Upon parsing information about a register,
* the existinng information about it are added
* as a Rizin comment and flag and are overwritten
* on the next iteration.
*
*/
typedef struct svd_iter {
uint8_t yxml_buf[XMLBUFSIZE];
yxml_t x;
const char *start;
const char *ptr;
const char *end;
char regname[50];
char baseaddress[50];
char bitoffset[10];
char bitwidth[20];
char description[200];
} svd_iter;
/**
* Used to denote the type of
* information that we're parsing
*/
enum { REGISTER_NAME,
REGISTER_SIZE,
REGISTER_OFFSET,
REGISTER_DESCRIPTION,
PERIPHERAL_BASEADDRESS } elem_type;
static const RzCmdDescArg cmd_svd_args[] = {
{
.name = "Path to the SVD file",
.type = RZ_CMD_ARG_TYPE_FILE,
},
{ 0 },
};
static const RzCmdDescHelp svd_usage = {
.summary = "SVD Plugin for Rizin",
.args = cmd_svd_args,
};
static inline int svd_iter_done(svd_iter *ta) {
return *ta->ptr == 0 || ta->ptr >= ta->end;
}
static svd_iter *svd_iter_return(svd_iter *ta, RzCore *core);
static svd_iter *svd_iter_init_vars(svd_iter *ta);
static inline int svd_iter_find_register(svd_iter *ta);
static inline int svd_iter_parse_register(svd_iter *ta, RzCore *core);
static inline int svd_iter_find_baseaddress(svd_iter *ta);
/**
* \brief Handles a single parse operation
*
* From this function, calls are made to
* parse the respective fields and stores
* the parsed information to the iterator
* (svd_iter pointer).
*/
static svd_iter *svd_iter_next(svd_iter *ta, RzCore *core) {
int ret;
if (!ta->baseaddress[0]) {
svd_iter_done(ta);
}
while (!svd_iter_done(ta) &&
(yxml_parse(&ta->x, *ta->ptr) != YXML_ELEMSTART ||
strcasecmp(ta->x.elem, "peripherals"))) {
ta->ptr++;
}
if (svd_iter_done(ta)) {
return NULL;
}
ret = svd_iter_find_baseaddress(ta);
if (ret == 1) {
return svd_iter_next(ta, core);
}
if (svd_iter_done(ta)) {
return NULL;
}
if (svd_iter_find_register(ta)) {
return svd_iter_next(ta, core);
}
if (svd_iter_done(ta)) {
return NULL;
}
svd_iter_init_vars(ta);
ret = svd_iter_parse_register(ta, core);
if (ret == 1) {
return svd_iter_next(ta, core);
} else if (ret == -1) {
return NULL;
}
return svd_iter_return(ta, core);
}
/**
* \brief Parses the base address
*/
static inline int svd_iter_find_baseaddress(svd_iter *ta) {
int level = 0;
yxml_ret_t r = YXML_OK;
char *cur, *tmp;
char value[VALUESIZE];
cur = value;
value[0] = 0;
while (!svd_iter_done(ta) && !ta->baseaddress[0]) {
switch ((r = yxml_parse(&ta->x, *ta->ptr))) {
case YXML_ELEMSTART:
level += 1;
if (level == 2 && strcasecmp(ta->x.elem, "baseAddress") == 0) {
cur = value;
*cur = 0;
}
break;
case YXML_ELEMEND:
level -= 1;
if (level < 0) {
return 1;
} else if (level == 2 && cur) {
rz_str_ncpy(ta->baseaddress, value, sizeof(ta->baseaddress));
cur = NULL;
}
break;
case YXML_CONTENT:
if (!cur || level != 2) {
break;
}
tmp = ta->x.data;
while (*tmp && cur < value + sizeof(value)) {
*cur++ = *tmp++;
}
if (cur >= value + sizeof(value)) {
cur = NULL;
} else {
*cur = 0;
}
break;
default:
break;
}
ta->ptr++;
}
return 0;
}
/**
* \brief Parses the register
*
* After parsing the particulaaras of the
* register, flags and comments are added.
*
*/
static inline int svd_iter_parse_register(svd_iter *ta, RzCore *core) {
int level = 3;
int address;
char *cur = NULL, *tmp;
char value[VALUESIZE];
value[0] = 0;
elem_type = -1;
yxml_ret_t r = YXML_OK;
for (;;) {
switch (r) {
case YXML_ELEMSTART:
level += 1;
if (strcmp(ta->x.elem, "baseAddress") == 0 && level == 2) {
elem_type = PERIPHERAL_BASEADDRESS;
cur = value;
*cur = 0;
}
if (level != 4) {
break;
}
if (strcasecmp(ta->x.elem, "displayName") == 0) {
elem_type = REGISTER_NAME;
} else if (strcasecmp(ta->x.elem, "description") == 0) {
elem_type = REGISTER_DESCRIPTION;
} else if (strcasecmp(ta->x.elem, "addressOffset") == 0) {
elem_type = REGISTER_OFFSET;
} else if (strcasecmp(ta->x.elem, "size") == 0) {
elem_type = REGISTER_SIZE;
} else {
break;
}
cur = value;
*cur = 0;
break;
case YXML_ELEMEND:
level -= 1;
if (elem_type == PERIPHERAL_BASEADDRESS) {
rz_str_ncpy(ta->baseaddress, value, sizeof(ta->baseaddress));
cur = NULL;
break;
}
if (level < 0) {
ta->baseaddress[0] = 0;
return 1;
} else if (level != 3 || !cur) {
break;
}
cur = NULL;
switch (elem_type) {
case REGISTER_OFFSET:
rz_str_ncpy(ta->bitoffset, value, sizeof(ta->bitoffset));
break;
case REGISTER_SIZE:
rz_str_ncpy(ta->bitwidth, value, sizeof(ta->bitwidth));
address = rz_num_math(NULL, ta->bitoffset) + rz_num_math(NULL, ta->baseaddress);
rz_flag_set(core->flags, ta->regname, address, rz_num_math(NULL, ta->bitwidth));
rz_meta_set_string(core->analysis, RZ_META_TYPE_COMMENT, address, ta->description);
break;
case REGISTER_NAME:
rz_str_ncpy(ta->regname, value, sizeof(ta->regname));
break;
case REGISTER_DESCRIPTION:
rz_str_replace_char(value, '\n', ' ');
rz_str_ncpy(ta->description, value, sizeof(ta->description));
break;
default:
break;
}
break;
case YXML_CONTENT:
if (!cur) {
break;
}
tmp = ta->x.data;
while (*tmp && cur < value + sizeof(value)) {
*cur++ = *tmp++;
}
if (cur >= value + sizeof(value)) {
cur = NULL;
} else {
*cur = 0;
}
break;
default:
break;
}
if (level == -1) {
break;
}
ta->ptr++;
if (svd_iter_done(ta)) {
return -1;
}
r = yxml_parse(&ta->x, *ta->ptr);
}
return 0;
}
/**
* \brief Finds the register
*
*/
static inline int svd_iter_find_register(svd_iter *ta) {
yxml_ret_t r = YXML_OK;
int level = 0;
while (!svd_iter_done(ta)) {
r = yxml_parse(&ta->x, *ta->ptr);
if (r == YXML_ELEMSTART) {
level += 1;
if (level == 1 &&
strcasecmp(ta->x.elem, "register") == 0) {
break;
}
} else if (r == YXML_ELEMEND) {
level -= 1;
if (level < -1) {
ta->baseaddress[0] = 0;
return 1;
}
}
ta->ptr++;
}
return 0;
}
static svd_iter *svd_iter_return(svd_iter *ta, RzCore *core) {
if (!ta) {
return NULL;
}
return *ta->bitoffset && *ta->regname && *ta->baseaddress && *ta->bitwidth && *ta->description ? ta : svd_iter_next(ta, core);
}
static svd_iter *svd_iter_init_vars(svd_iter *ta) {
if (!ta) {
return NULL;
}
*ta->bitoffset = *ta->regname = *ta->bitwidth = *ta->description = 0;
return ta;
}
static svd_iter *svd_iter_init(svd_iter *ta, const char *file) {
char *doc = rz_file_slurp(file, NULL);
if (!doc) {
eprintf("Failed to open file \"%s\"\n", file);
return NULL;
}
int doc_len = rz_file_size(file);
if (!doc_len) {
eprintf("Failed to read the file size \"%s\"\n", file);
return NULL;
}
ta->ptr = ta->start = doc;
ta->end = ta->start + doc_len;
yxml_init(&ta->x, ta->yxml_buf, sizeof(ta->yxml_buf));
ta->baseaddress[0] = 0;
return ta;
}
static int parse_svd(RzCore *core, const char *file) {
svd_iter ta_spc, *ta;
ta = svd_iter_init(&ta_spc, file);
ta = svd_iter_next(ta, core);
while (ta) {
ta = svd_iter_next(ta, core);
}
return 1;
}
RZ_IPI RzCmdStatus rz_cmd_svd_handler(RzCore *core, int argc,
const char **argv) {
if (argc < 2) {
return RZ_CMD_STATUS_WRONG_ARGS;
}
parse_svd(core, argv[1]);
return RZ_CMD_STATUS_OK;
}
static bool rz_cmd_svd_init(RzCore *core) {
RzCmd *rcmd = core->rcmd;
RzCmdDesc *root_cd = rz_cmd_get_root(rcmd);
if (!root_cd) {
rz_warn_if_reached();
return false;
}
RzCmdDesc *svd = rz_cmd_desc_argv_new(rcmd, root_cd, "svd",
rz_cmd_svd_handler, &svd_usage);
if (!svd) {
rz_warn_if_reached();
return false;
}
return true;
}
RzCorePlugin rz_core_plugin_svd = {
.name = "rz-svd",
.desc = "SVD Plugin for Rizin",
.license = "LGPL3",
.version = "0.1.0",
.author = "officialcjunior",
.init = rz_cmd_svd_init,
};
#ifndef CORELIB
RZ_API RzLibStruct rizin_plugin = { .type = RZ_LIB_TYPE_CORE,
.data = &rz_core_plugin_svd,
.version = RZ_VERSION,
.pkgname = "rz-svd" };
#endif