-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupditool.c
284 lines (261 loc) · 7.09 KB
/
upditool.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
/*
upditool.c
This is part of c_updi, programmer
Copyright (C) 2020 Peter Popovec, [email protected]
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 3 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, see <http://www.gnu.org/licenses/>.
collect data drom user (commandline switches etc) and data from
mcu database, create input structure for programmer
*/
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "prog.h"
#include "helpers.h"
// find MCU parameters (based on -p switch)
// 0 OK
static int
search_mcu_spec (struct prog *prog)
{
struct mcu *mcu;
if (prog->part_name == NULL)
{
fprintf (stderr,
"Please specify part by -p switch (-p help to list available devices)\n");
return -1;
}
for (mcu = prog->mcu_table; mcu->name[0] != '\0'; mcu++)
{
if (0 == strcmp (prog->part_name, mcu->name))
{
prog->mcu = mcu;
return 0;
}
}
fprintf (stderr, "Unknonwn device %s, available device names:\n",
prog->part_name);
for (mcu = prog->mcu_table; mcu->name[0] != '\0'; mcu++)
fprintf (stderr, "'%s'\n", mcu->name);
return -1;
}
// parse/check all -U arguments
// 0 OK
static int
parse_U (struct prog *prog)
{
struct U *u;
struct mem *mem;
char *tmp;
for (u = prog->U_first; u != NULL; u = u->next)
{
// here full -U optarg is copied, extract memory name here
tmp = strchr (u->name, ':');
if (NULL == tmp)
{
fprintf (stderr, "unable to parse memory type\n");
return -1;
}
*(tmp) = '\0';
mem = search_memory_by_name (prog->mcu, u->name);
if (NULL == mem)
{
fprintf (stderr,
"Unable to found '%s' memory on device %s, available memories:\n",
u->name, prog->mcu->name);
for (mem = get_1st_memory (prog->mcu); mem != NULL;
mem = get_next_memory (mem))
fprintf (stderr,
"'%s'%s is %s, start at: 0x%06x size % 7d %s\n",
mem->name,
strlen (mem->name) >= 6 ? "\t" : "\t\t",
mem->rw ? "RW" : "RO",
mem->start, mem->size,
mem->rw ? (mem->write_algo ==
C_WRITE_ALGO_NONE ?
"(Write not implemented for now)" : "") : "");
return -1;
}
u->mem = mem;
tmp++;
u->op = *(tmp);
tmp++;
if (*(tmp) != ':')
{
fprintf (stderr,
"unable to parse filename for memory operation, missing collon?\n");
return -1;
}
tmp++;
u->filename = strdup (tmp);
tmp = strchr (u->filename, ':');
if (NULL != tmp)
{
*(tmp) = '\0';
tmp++;
u->fmt = *(tmp);
}
if (strlen (u->filename) < 1)
{
fprintf (stderr, "unable to parse filename\n");
return -1;
}
switch (u->op)
{
case 'w':
if (mem->rw != 1)
{
fprintf (stderr,
"Unable to write to memory %s, this memory is read only\n",
u->name);
return -1;
}
break;
case 'r':
if (u->fmt == 'm')
{
fprintf (stderr,
"specified format 'm' can ot be used for read operation\n");
return -1;
}
case 'v':
break;
default:
fprintf (stderr, "Unknown operation '%c'\n", u->op);
return -1;
}
}
return 0;
}
int
main (int argc, char *argv[])
{
struct prog p, *prog = &p;
// array for memory read/write
uint8_t r[MAX_MEMORY_LEN + 10]; // raw dat from device
uint8_t v[MAX_MEMORY_LEN + 10]; // verify data form file
int opt;
struct U *u;
// initialize prog structure, load static MCU specification...
struct mcu mcu_table[] = { //
#include "device_avr.h"
{
.name = "",
},
};
memset (prog, 0, sizeof (struct prog));
prog->serial_speed = 115200; // default serial port speed
prog->mcu_table = mcu_table;
//
prog->r = r;
prog->v = v;
while ((opt = getopt (argc, argv, "hVFeb:p:P:U:")) != -1)
{
switch (opt)
{
case 'h':
{
fprintf (stderr, //
"Options:\n");
fprintf (stderr, //
" -p <partno> Required, Specify AVR device.\n");
fprintf (stderr, //
" -b <baudrate> default 115200\n");
fprintf (stderr, //
" -D Disable auto erase for flash memory (TODO)\n");
fprintf (stderr, //
" -P <port> Required, Specify connection port.\n");
fprintf (stderr, //
" -F Disable signature check.\n");
fprintf (stderr, //
" -V Disable verify.\n");
fprintf (stderr, //
" -e Perform a chip erase.\n");
fprintf (stderr, //
" -U <memtype>:r|w|v:<filename>[:format]> \n");
fprintf (stderr, //
" Memory operation specification.\n");
fprintf (stderr, //
" multiple -U options are allowed\n");
fprintf (stderr, //
"(Warning this code was tested only on avr128DA32 and megaAVR 4808)\n");
return 0;
}
case 'e':
prog->do_chip_erase = 1;
break;
case 'b':
prog->serial_speed = atoi (optarg);
if (prog->serial_speed < 75000)
fprintf (stderr,
"Warning, requested speed below recommended speed for UPDI\n");
if (prog->serial_speed > 16000000)
fprintf (stderr,
"Warning, requested speed over recommended speed for UPDI\n");
break;
case 'F':
prog->skip_signature_check = 1;
break;
case 'V':
prog->skip_verify = 1;
break;
case 'D':
prog->disable_autoerase = 1;
break;
case 'P':
if (prog->serial_port)
free (prog->serial_port);
prog->serial_port = strdup (optarg);
break;
case 'p':
if (prog->part_name != NULL)
{
fprintf (stderr, "Multiple -p switches ?\n");
exit (EXIT_FAILURE);
}
else
prog->part_name = strdup (optarg);
break;
case 'U':
// -U memtype:op:filename[:format]
// create linked list here, we can't check the memory name, because the device can be specified later with the -p switch
u = calloc (sizeof (struct U), 1);
if (u == NULL)
exit (1);
if (prog->U_first == NULL)
prog->U_first = u;
else
prog->U_last->next = u;
prog->U_last = u;
u->name = strdup (optarg);
u->fmt = 'r'; // default raw format
break;
default:
fprintf (stderr, "Usage: %s [-P serial port] [-p partname] \n",
argv[0]);
exit (EXIT_FAILURE);
}
}
if (prog->serial_port == NULL)
{
fprintf (stderr,
"Please specify serial port by -P switch, for example /dev/ttyUSB2 .. \n");
exit (EXIT_FAILURE);
}
// search for device parameters
if (search_mcu_spec (prog))
exit (EXIT_FAILURE);
// parse/check all -U arguments
if (parse_U (prog))
exit (EXIT_FAILURE);
return run_prog (prog);
}