-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfuse_mem.c
203 lines (176 loc) · 4.79 KB
/
dfuse_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
/*
* Helper functions for reading the memory map of a device
* following the ST DfuSe 1.1a specification.
*
* Copyright 2011-2014 Tormod Volden <debian.tormod@gmail.com>
*
* 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
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "portable.h"
#include "dfu_file.h"
#include "dfuse_mem.h"
int add_segment(struct memsegment **segment_list, struct memsegment segment)
{
struct memsegment *new_element;
new_element = dfu_malloc(sizeof(struct memsegment));
*new_element = segment;
new_element->next = NULL;
if (*segment_list == NULL)
/* list can be empty on first call */
*segment_list = new_element;
else {
struct memsegment *next_element;
/* find last element in list */
next_element = *segment_list;
while (next_element->next != NULL)
next_element = next_element->next;
next_element->next = new_element;
}
return 0;
}
struct memsegment *find_segment(struct memsegment *segment_list,
unsigned int address)
{
while (segment_list != NULL) {
if (segment_list->start <= address &&
segment_list->end >= address)
return segment_list;
segment_list = segment_list->next;
}
return NULL;
}
void free_segment_list(struct memsegment *segment_list)
{
struct memsegment *next_element;
while (segment_list->next != NULL) {
next_element = segment_list->next;
free(segment_list);
segment_list = next_element;
}
free(segment_list);
}
/* Parse memory map from interface descriptor string
* encoded as per ST document UM0424 section 4.3.2.
*/
struct memsegment *parse_memory_layout(char *intf_desc)
{
char multiplier, memtype;
unsigned int address;
int sectors, size;
char *name, *typestring;
int ret;
int count = 0;
char separator;
int scanned;
struct memsegment *segment_list = NULL;
struct memsegment segment;
name = dfu_malloc(strlen(intf_desc));
ret = sscanf(intf_desc, "@%[^/]%n", name, &scanned);
if (ret < 1) {
free(name);
warnx("Could not read name, sscanf returned %d", ret);
return NULL;
}
printf("DfuSe interface name: \"%s\"\n", name);
intf_desc += scanned;
typestring = dfu_malloc(strlen(intf_desc));
while (ret = sscanf(intf_desc, "/0x%x/%n", &address, &scanned),
ret > 0) {
intf_desc += scanned;
while (ret = sscanf(intf_desc, "%d*%d%c%[^,/]%n",
§ors, &size, &multiplier, typestring,
&scanned), ret > 2) {
intf_desc += scanned;
count++;
memtype = 0;
if (ret == 4) {
if (strlen(typestring) == 1
&& typestring[0] != '/')
memtype = typestring[0];
else {
warnx("Parsing type identifier '%s' "
"failed for segment %i",
typestring, count);
continue;
}
}
/* Quirk for STM32F4 devices */
if (strcmp(name, "Device Feature") == 0)
memtype = 'e';
switch (multiplier) {
case 'B':
break;
case 'K':
size *= 1024;
break;
case 'M':
size *= 1024 * 1024;
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
if (!memtype) {
warnx("Non-valid multiplier '%c', "
"interpreted as type "
"identifier instead",
multiplier);
memtype = multiplier;
break;
}
/* if memtype was already set: */
/* fall-through */
default:
warnx("Non-valid multiplier '%c', "
"assuming bytes", multiplier);
}
if (!memtype) {
warnx("No valid type for segment %d\n", count);
continue;
}
segment.start = address;
segment.end = address + sectors * size - 1;
segment.pagesize = size;
segment.memtype = memtype & 7;
add_segment(&segment_list, segment);
if (verbose)
printf("Memory segment at 0x%08x %3d x %4d = "
"%5d (%s%s%s)\n",
address, sectors, size, sectors * size,
memtype & DFUSE_READABLE ? "r" : "",
memtype & DFUSE_ERASABLE ? "e" : "",
memtype & DFUSE_WRITEABLE ? "w" : "");
address += sectors * size;
separator = *intf_desc;
if (separator == ',')
intf_desc += 1;
else
break;
} /* while per segment */
} /* while per address */
free(name);
free(typestring);
return segment_list;
}