-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpcanflash.c
365 lines (305 loc) · 8.9 KB
/
pcanflash.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
/*
* pcanflash.c - flash program for PCAN routers
*
* Copyright (C) 2021 PEAK System-Technik GmbH
*
* www.peak-system.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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Author: Oliver Hartkopp ([email protected])
* Maintainer(s): Stephane Grosjean ([email protected])
*
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <libgen.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include "pcanflash.h"
#include "pcanfunc.h"
#include "pcanhw.h"
#define PCF_MIN_TX_QUEUE 500
#define BUFSZ 512 /* max. known block size */
extern int optind, opterr, optopt;
void print_usage(char *prg)
{
fprintf(stderr, "\nUsage: %s <options> <interface>\n\n", prg);
fprintf(stderr, "Options: -f <file.bin> (binary file to flash)\n");
fprintf(stderr, " -i <module_id> (skip question when discovering multiple ids)\n");
fprintf(stderr, " -q (just query modules and quit)\n");
fprintf(stderr, " -r (reset module after flashing/query)\n");
fprintf(stderr, " -R (reset all modules after flashing/query)\n");
fprintf(stderr, " -d (dry run - skip erase/write commands)\n");
fprintf(stderr, "\n");
}
int main(int argc, char **argv)
{
static uint8_t buf[BUFSZ+2];
struct ifreq ifr;
struct sockaddr_can addr;
static struct can_frame modules[MAX_MODULES];
struct can_filter rfilter;
int s; /* CAN_RAW socket */
static FILE *infile;
static int query;
static int do_reset;
static int do_reset_all;
static int dry_run;
int module_id = NO_MODULE_ID;
int alternating_xor_flip;
uint32_t crc_start;
uint32_t floffset;
uint32_t blksz;
int opt, i;
uint8_t hw_type = 0;
long foffset;
int entries;
while ((opt = getopt(argc, argv, "f:i:qRrd?")) != -1) {
switch (opt) {
case 'f':
infile = fopen(optarg, "r");
if (!infile) {
perror("infile");
return 1;
}
/* check the file length to fit into 16 MB */
fseek(infile, 0L, SEEK_END);
if (ftell(infile) > 0x1000000) {
printf("binary flash file too long!\n");
fclose(infile);
return 1;
}
rewind(infile);
break;
case 'i':
module_id = strtoul(optarg, NULL, 10);
break;
case 'q':
query = 1;
break;
case 'R':
do_reset_all = 1;
/* fallthrough */
case 'r':
do_reset = 1;
break;
case 'd':
dry_run = 1;
break;
case '?':
default:
print_usage(basename(argv[0]));
return 1;
break;
}
}
if ((argc - optind) != 1 || (infile && query) || (!(infile || query))) {
print_usage(basename(argv[0]));
return 0;
}
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("socket");
return 1;
}
/* set single CAN ID raw filters for RX and TX frames */
rfilter.can_id = CAN_ID & CAN_SFF_MASK;
rfilter.can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
/* copy netdev name for ioctl request */
strncpy(ifr.ifr_name, argv[optind], sizeof(ifr.ifr_name)-1);
/* check tx queue length ... */
if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
perror("SIOCGIFTXQLEN");
return 1;
}
/* ... to be at least PCF_MIN_TX_QUEUE CAN frames */
if (ifr.ifr_qlen < PCF_MIN_TX_QUEUE) {
fprintf(stderr, "tx queue len %d is too small! Must be at least %d.\n",
ifr.ifr_qlen, PCF_MIN_TX_QUEUE);
return 1;
}
/* get interface index for bind() */
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
perror("SIOCGIFINDEX");
return 1;
}
addr.can_ifindex = ifr.ifr_ifindex;
addr.can_family = AF_CAN;
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
return 1;
}
entries = query_modules(s, modules);
if (!entries) {
fprintf(stderr, "module query failed!\n");
return 1;
}
/* print module list */
printf("\nfound modules:\n\n");
for (i = 0; i < MAX_MODULES; i++) {
if (modules[i].can_id) {
if (eval_modules(s, i, &modules[i]))
return 1;
}
}
if (query) {
printf("\n");
/* provide reset option after query */
if (!do_reset)
return 0;
/* no module identification needed to perform a reset */
if (do_reset_all)
goto out_reset;
}
if (module_id == NO_MODULE_ID) {
if (entries == 1) {
/* catch first and only module */
for (i = 0; i < MAX_MODULES; i++) {
if (modules[i].can_id) {
module_id = i;
break;
}
}
} else {
printf("\nmultiple modules found - please provide module id : ");
scanf("%d", &module_id);
module_id &= MAX_MODULES_MASK;
}
}
if (!(modules[module_id].can_id)) {
fprintf(stderr, "\nmodule id not found in module list!\n\n");
exit(1);
}
/* at this point we can properly address a module to perform a reset */
if (query && do_reset)
goto out_reset;
/* restore hw_type of this module_id index from data[7] */
hw_type = modules[module_id].data[7];
if (get_hw(hw_type) == NULL) {
fprintf(stderr, "\nno flash configuration available for hardware type %d!\n\n",
hw_type);
exit(1);
}
if (check_ch_name(infile, hw_type)) {
fprintf(stderr, "\nno ch_filename in bin-file for hardware type %d (%s)!\n\n",
hw_type, get_hw_name(hw_type));
exit(1);
}
/* take default values when not provided by JSON config */
if (modules[module_id].can_dlc == NO_DATA_LEN) {
if (has_hw_flags(hw_type, DATA_MODE8))
modules[module_id].can_dlc = DATA_LEN8;
else
modules[module_id].can_dlc = DATA_LEN6;
}
blksz = get_max_blocksize(hw_type);
if ((blksz > BUFSZ) || (blksz < 32)) {
fprintf(stderr, "\nmax_blocksize %d out of range!\n\n", blksz);
exit(1);
}
printf("\nflashing module id %d with flash transfer data len %d and block size %d\n",
module_id, modules[module_id].can_dlc, blksz);
if (has_hw_flags(hw_type, SWITCH_TO_BOOTLOADER)) { /* PPCAN mode modules */
printf("\nswitch module into bootloader ... ");
fflush(stdout);
switch_to_bootloader(s, module_id);
sleep(1);
get_status(s, module_id, NULL);
printf("done\n");
}
printf("\nerasing flash sectors:\n");
entries = get_num_flashblocks(hw_type);
if (!(entries)) {
fprintf(stderr, "no flashblocks found for hardware type %d (%s)!\n",
hw_type, get_hw_name(hw_type));
goto out_leave_bootloader;
}
for (i = 0; i < entries; i++)
erase_flashblocks(s, dry_run, infile, module_id, hw_type, i);
printf("\nwriting flash blocks:\n");
foffset = get_file_skip(hw_type);
alternating_xor_flip = has_hw_flags(hw_type, FDATA_INVERT);
crc_start = get_crc_startpos(hw_type);
floffset = get_flash_offset(hw_type);
while (1) {
if (fseek(infile, foffset, SEEK_SET))
break;
memset(&buf, 0xFF, blksz);
fread(buf, 1, blksz, infile);
for (i = 0; i < blksz; i++) {
if (buf[i] != EMPTY)
break;
}
/* non-empty block (not all bytes are EMPTY / 0xFFU) */
if (i != blksz) {
/* check whether we need to patch the CRC array */
if ((crc_start) && (crc_start >= foffset) && (crc_start < foffset + blksz))
write_crc_array(&buf[crc_start - foffset], infile, crc_start);
/* write non-empty block */
write_block(s, dry_run, module_id, foffset + floffset, blksz,
buf, alternating_xor_flip, modules[module_id].can_dlc);
}
if (feof(infile))
break;
foffset += blksz;
} /* while (1) */
out_leave_bootloader:
if (has_hw_flags(hw_type, END_PROGRAMMING)) { /* recent hw modules */
printf("\nend programming ... ");
fflush(stdout);
end_programming(s, module_id);
sleep(1);
get_status(s, module_id, NULL);
printf("done\n");
}
out_reset:
if (has_hw_flags(hw_type, RESET_AFTER_FLASH) || do_reset) {
if (do_reset_all) {
printf("\nreset all modules ... ");
fflush(stdout);
reset_module(s, 0xFF); /* module_id for all modules */
} else {
printf("\nreset module id %d ... ", module_id);
fflush(stdout);
reset_module(s, module_id);
}
sleep(1);
/* a reset which is issued by a command line option
* likely leads into starting the application which
* does not know about this status message. Therefore
* only get the status when this is used in an original
* PCAN flashing process, e.g. the PCAN Router Pro
*/
if (has_hw_flags(hw_type, RESET_AFTER_FLASH))
get_status(s, module_id, NULL);
printf("done\n");
}
printf("\ndone.\n\n");
close(s);
if (infile)
fclose(infile);
return 0;
}