Debian Codegraph @

src/dfu_file.c

raw
1 /*
2 * Load or store DFU files including suffix and prefix
3 *
4 * Copyright 2014-2019 Tormod Volden <[email protected]>
5 * Copyright 2012 Stefan Schmidt <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #define __USE_MINGW_ANSI_STDIO 1
27 #include <stdio.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <time.h>
33 #include <fcntl.h>
34 #include <limits.h>
35
36 #include "portable.h"
37 #include "dfu_file.h"
38
39 #define DFU_SUFFIX_LENGTH 16
40 #define LMDFU_PREFIX_LENGTH 8
41 #define LPCDFU_PREFIX_LENGTH 16
42 #define PROGRESS_BAR_WIDTH 25
43 #define STDIN_CHUNK_SIZE 65536
44
45 static const unsigned long crc32_table[] = {
46 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
47 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
48 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
49 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
50 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
51 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
52 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
53 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
54 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
55 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
56 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
57 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
58 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
59 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
60 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
61 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
62 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
63 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
64 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
65 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
66 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
67 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
68 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
69 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
70 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
71 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
72 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
73 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
74 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
75 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
76 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
77 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
78 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
79 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
80 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
81 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
82 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
83 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
84 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
85 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
86 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
87 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
88 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
89
90 static uint32_t crc32_byte(uint32_t accum, uint8_t delta)
91 {
92 return crc32_table[(accum ^ delta) & 0xff] ^ (accum >> 8);
93 }
94
95 static int probe_prefix(struct dfu_file *file)
96 {
97 uint8_t *prefix = file->firmware;
98
99 if (file->size.total < LMDFU_PREFIX_LENGTH)
100 return 1;
101 if ((prefix[0] == 0x01) && (prefix[1] == 0x00)) {
102 uint32_t payload_length = (prefix[7] << 24) | (prefix[6] << 16) |
103 (prefix[5] << 8) | prefix[4];
104 uint32_t expected_payload_length = (uint32_t) file->size.total - LMDFU_PREFIX_LENGTH - file->size.suffix;
105 if (payload_length != expected_payload_length)
106 return 1;
107 file->prefix_type = LMDFU_PREFIX;
108 file->size.prefix = LMDFU_PREFIX_LENGTH;
109 file->lmdfu_address = 1024 * ((prefix[3] << 8) | prefix[2]);
110 }
111 else if (((prefix[0] & 0x3f) == 0x1a) && ((prefix[1] & 0x3f)== 0x3f)) {
112 file->prefix_type = LPCDFU_UNENCRYPTED_PREFIX;
113 file->size.prefix = LPCDFU_PREFIX_LENGTH;
114 }
115
116 if (file->size.prefix + file->size.suffix > file->size.total)
117 return 1;
118 return 0;
119 }
120
121 void dfu_progress_bar(const char *desc, unsigned long long curr,
122 unsigned long long max)
123 {
124 static char buf[PROGRESS_BAR_WIDTH + 1];
125 static unsigned long long last_progress = -1;
126 static time_t last_time;
127 time_t curr_time = time(NULL);
128 unsigned long long progress;
129 unsigned long long x;
130
131 /* check for not known maximum */
132 if (max < curr)
133 max = curr + 1;
134 /* make none out of none give zero */
135 if (max == 0 && curr == 0)
136 max = 1;
137
138 /* compute completion */
139 progress = (PROGRESS_BAR_WIDTH * curr) / max;
140 if (progress > PROGRESS_BAR_WIDTH)
141 progress = PROGRESS_BAR_WIDTH;
142 if (progress == last_progress &&
143 curr_time == last_time)
144 return;
145 last_progress = progress;
146 last_time = curr_time;
147
148 for (x = 0; x != PROGRESS_BAR_WIDTH; x++) {
149 if (x < progress)
150 buf[x] = '=';
151 else
152 buf[x] = ' ';
153 }
154 buf[x] = 0;
155
156 printf("\r%s\t[%s] %3llu%% %12llu bytes", desc, buf,
157 (100ULL * curr) / max, curr);
158
159 if (progress == PROGRESS_BAR_WIDTH)
160 printf("\n%s done.\n", desc);
161 }
162
163 void *dfu_malloc(size_t size)
164 {
165 void *ptr = malloc(size);
166 if (ptr == NULL)
167 errx(EX_SOFTWARE, "Cannot allocate memory of size %d bytes", (int)size);
168 return (ptr);
169 }
170
171 uint32_t dfu_file_write_crc(int f, uint32_t crc, const void *buf, int size)
172 {
173 int x;
174
175 /* compute CRC */
176 for (x = 0; x != size; x++)
177 crc = crc32_byte(crc, ((uint8_t *)buf)[x]);
178
179 /* write data */
180 if (write(f, buf, size) != size)
181 err(EX_IOERR, "Could not write %d bytes to file %d", size, f);
182
183 return (crc);
184 }
185
186 void dfu_load_file(struct dfu_file *file, enum suffix_req check_suffix, enum prefix_req check_prefix)
187 {
188 off_t offset;
189 int f;
190 int i;
191 int res;
192
193 file->size.prefix = 0;
194 file->size.suffix = 0;
195
196 /* default values, if no valid suffix is found */
197 file->bcdDFU = 0;
198 file->idVendor = 0xffff; /* wildcard value */
199 file->idProduct = 0xffff; /* wildcard value */
200 file->bcdDevice = 0xffff; /* wildcard value */
201
202 /* default values, if no valid prefix is found */
203 file->lmdfu_address = 0;
204
205 free(file->firmware);
206
207 if (!strcmp(file->name, "-")) {
208 size_t read_bytes;
209
210 #ifdef WIN32
211 _setmode( _fileno( stdin ), _O_BINARY );
212 #endif
213 file->firmware = (uint8_t*) dfu_malloc(STDIN_CHUNK_SIZE);
214 read_bytes = fread(file->firmware, 1, STDIN_CHUNK_SIZE, stdin);
215 file->size.total = read_bytes;
216 while (read_bytes == STDIN_CHUNK_SIZE) {
217 file->firmware = (uint8_t*) realloc(file->firmware, file->size.total + STDIN_CHUNK_SIZE);
218 if (!file->firmware)
219 err(EX_SOFTWARE, "Could not allocate firmware buffer");
220 read_bytes = fread(file->firmware + file->size.total, 1, STDIN_CHUNK_SIZE, stdin);
221 file->size.total += read_bytes;
222 }
223 if (verbose)
224 printf("Read %lli bytes from stdin\n", (long long) file->size.total);
225 /* Never require suffix when reading from stdin */
226 check_suffix = MAYBE_SUFFIX;
227 } else {
228 ssize_t read_count;
229 off_t read_total = 0;
230
231 f = open(file->name, O_RDONLY | O_BINARY);
232 if (f < 0)
233 err(EX_NOINPUT, "Could not open file %s for reading", file->name);
234
235 offset = lseek(f, 0, SEEK_END);
236
237 if (offset < 0)
238 err(EX_SOFTWARE, "File size is too big");
239
240 if (lseek(f, 0, SEEK_SET) != 0)
241 err(EX_IOERR, "Could not seek to beginning");
242
243 file->size.total = offset;
244
245 if (file->size.total > SSIZE_MAX) {
246 err(EX_SOFTWARE, "File too large for memory allocation on this platform");
247 }
248 file->firmware = dfu_malloc(file->size.total);
249
250 while (read_total < file->size.total) {
251 off_t to_read = file->size.total - read_total;
252 /* read() limit on Linux, slightly below MAX_INT on Windows */
253 if (to_read > 0x7ffff000)
254 to_read = 0x7ffff000;
255 read_count = read(f, file->firmware + read_total, to_read);
256 if (read_count == 0)
257 break;
258 if (read_count == -1 && errno != EINTR)
259 break;
260 read_total += read_count;
261 }
262 if (read_total != file->size.total) {
263 err(EX_IOERR, "Could only read %lld of %lld bytes from %s",
264 (long long) read_total, (long long) file->size.total, file->name);
265 }
266 close(f);
267 }
268
269 /* Check for possible DFU file suffix by trying to parse one */
270 {
271 uint32_t crc = 0xffffffff;
272 const uint8_t *dfusuffix;
273 int missing_suffix = 0;
274 const char *reason;
275
276 if (file->size.total < DFU_SUFFIX_LENGTH) {
277 reason = "File too short for DFU suffix";
278 missing_suffix = 1;
279 goto checked;
280 }
281
282 dfusuffix = file->firmware + file->size.total -
283 DFU_SUFFIX_LENGTH;
284
285 for (i = 0; i < file->size.total - 4; i++)
286 crc = crc32_byte(crc, file->firmware[i]);
287
288 if (dfusuffix[10] != 'D' ||
289 dfusuffix[9] != 'F' ||
290 dfusuffix[8] != 'U') {
291 reason = "Invalid DFU suffix signature";
292 missing_suffix = 1;
293 goto checked;
294 }
295
296 file->dwCRC = (dfusuffix[15] << 24) +
297 (dfusuffix[14] << 16) +
298 (dfusuffix[13] << 8) +
299 dfusuffix[12];
300
301 if (file->dwCRC != crc) {
302 reason = "DFU suffix CRC does not match";
303 missing_suffix = 1;
304 goto checked;
305 }
306
307 /* At this point we believe we have a DFU suffix
308 so we require further checks to succeed */
309
310 file->bcdDFU = (dfusuffix[7] << 8) + dfusuffix[6];
311
312 if (verbose)
313 printf("DFU suffix version %x\n", file->bcdDFU);
314
315 file->size.suffix = dfusuffix[11];
316
317 if (file->size.suffix < DFU_SUFFIX_LENGTH) {
318 errx(EX_DATAERR, "Unsupported DFU suffix length %d",
319 file->size.suffix);
320 }
321
322 if (file->size.suffix > file->size.total) {
323 errx(EX_DATAERR, "Invalid DFU suffix length %d",
324 file->size.suffix);
325 }
326
327 file->idVendor = (dfusuffix[5] << 8) + dfusuffix[4];
328 file->idProduct = (dfusuffix[3] << 8) + dfusuffix[2];
329 file->bcdDevice = (dfusuffix[1] << 8) + dfusuffix[0];
330
331 checked:
332 if (missing_suffix) {
333 if (check_suffix == NEEDS_SUFFIX) {
334 warnx("%s", reason);
335 errx(EX_DATAERR, "Valid DFU suffix needed");
336 } else if (check_suffix == MAYBE_SUFFIX) {
337 warnx("Warning: %s", reason);
338 warnx("A valid DFU suffix will be required in a future dfu-util release");
339 }
340 } else {
341 if (check_suffix == NO_SUFFIX) {
342 errx(EX_DATAERR, "Please remove existing DFU suffix before adding a new one.\n");
343 }
344 }
345 }
346 res = probe_prefix(file);
347 if ((res || file->size.prefix == 0) && check_prefix == NEEDS_PREFIX)
348 errx(EX_DATAERR, "Valid DFU prefix needed");
349 if (file->size.prefix && check_prefix == NO_PREFIX)
350 errx(EX_DATAERR, "A prefix already exists, please delete it first");
351 if (file->size.prefix && verbose) {
352 uint8_t *data = file->firmware;
353 if (file->prefix_type == LMDFU_PREFIX)
354 printf("Possible TI Stellaris DFU prefix with "
355 "the following properties\n"
356 "Address: 0x%08x\n"
357 "Payload length: %d\n",
358 file->lmdfu_address,
359 data[4] | (data[5] << 8) |
360 (data[6] << 16) | (data[7] << 24));
361 else if (file->prefix_type == LPCDFU_UNENCRYPTED_PREFIX)
362 printf("Possible unencrypted NXP LPC DFU prefix with "
363 "the following properties\n"
364 "Payload length: %d kiByte\n",
365 data[2] >>1 | (data[3] << 7) );
366 else
367 errx(EX_DATAERR, "Unknown DFU prefix type");
368 }
369 }
370
371 void dfu_store_file(struct dfu_file *file, int write_suffix, int write_prefix)
372 {
373 uint32_t crc = 0xffffffff;
374 int f;
375
376 f = open(file->name, O_WRONLY | O_BINARY | O_TRUNC | O_CREAT, 0666);
377 if (f < 0)
378 err(EX_CANTCREAT, "Could not open file %s for writing", file->name);
379
380 /* write prefix, if any */
381 if (write_prefix) {
382 if (file->prefix_type == LMDFU_PREFIX) {
383 uint8_t lmdfu_prefix[LMDFU_PREFIX_LENGTH];
384 uint32_t addr = file->lmdfu_address / 1024;
385
386 /* lmdfu_dfu_prefix payload length excludes prefix and suffix */
387 uint32_t len = file->size.total -
388 file->size.prefix - file->size.suffix;
389
390 lmdfu_prefix[0] = 0x01; /* STELLARIS_DFU_PROG */
391 lmdfu_prefix[1] = 0x00; /* Reserved */
392 lmdfu_prefix[2] = (uint8_t)(addr & 0xff);
393 lmdfu_prefix[3] = (uint8_t)(addr >> 8);
394 lmdfu_prefix[4] = (uint8_t)(len & 0xff);
395 lmdfu_prefix[5] = (uint8_t)(len >> 8) & 0xff;
396 lmdfu_prefix[6] = (uint8_t)(len >> 16) & 0xff;
397 lmdfu_prefix[7] = (uint8_t)(len >> 24);
398
399 crc = dfu_file_write_crc(f, crc, lmdfu_prefix, LMDFU_PREFIX_LENGTH);
400 }
401 if (file->prefix_type == LPCDFU_UNENCRYPTED_PREFIX) {
402 uint8_t lpcdfu_prefix[LPCDFU_PREFIX_LENGTH] = {0};
403 int i;
404
405 /* Payload is firmware and prefix rounded to 512 bytes */
406 uint32_t len = (file->size.total - file->size.suffix + 511) /512;
407
408 lpcdfu_prefix[0] = 0x1a; /* Unencypted*/
409 lpcdfu_prefix[1] = 0x3f; /* Reserved */
410 lpcdfu_prefix[2] = (uint8_t)(len & 0xff);
411 lpcdfu_prefix[3] = (uint8_t)((len >> 8) & 0xff);
412 for (i = 12; i < LPCDFU_PREFIX_LENGTH; i++)
413 lpcdfu_prefix[i] = 0xff;
414
415 crc = dfu_file_write_crc(f, crc, lpcdfu_prefix, LPCDFU_PREFIX_LENGTH);
416 }
417 }
418 /* write firmware binary */
419 crc = dfu_file_write_crc(f, crc, file->firmware + file->size.prefix,
420 file->size.total - file->size.prefix - file->size.suffix);
421
422 /* write suffix, if any */
423 if (write_suffix) {
424 uint8_t dfusuffix[DFU_SUFFIX_LENGTH];
425
426 dfusuffix[0] = file->bcdDevice & 0xff;
427 dfusuffix[1] = file->bcdDevice >> 8;
428 dfusuffix[2] = file->idProduct & 0xff;
429 dfusuffix[3] = file->idProduct >> 8;
430 dfusuffix[4] = file->idVendor & 0xff;
431 dfusuffix[5] = file->idVendor >> 8;
432 dfusuffix[6] = file->bcdDFU & 0xff;
433 dfusuffix[7] = file->bcdDFU >> 8;
434 dfusuffix[8] = 'U';
435 dfusuffix[9] = 'F';
436 dfusuffix[10] = 'D';
437 dfusuffix[11] = DFU_SUFFIX_LENGTH;
438
439 crc = dfu_file_write_crc(f, crc, dfusuffix,
440 DFU_SUFFIX_LENGTH - 4);
441
442 dfusuffix[12] = crc;
443 dfusuffix[13] = crc >> 8;
444 dfusuffix[14] = crc >> 16;
445 dfusuffix[15] = crc >> 24;
446
447 crc = dfu_file_write_crc(f, crc, dfusuffix + 12, 4);
448 }
449 close(f);
450 }
451
452 void show_suffix_and_prefix(struct dfu_file *file)
453 {
454 if (file->size.prefix == LMDFU_PREFIX_LENGTH) {
455 printf("The file %s contains a TI Stellaris DFU prefix with the following properties:\n", file->name);
456 printf("Address:\t0x%08x\n", file->lmdfu_address);
457 } else if (file->size.prefix == LPCDFU_PREFIX_LENGTH) {
458 uint8_t * prefix = file->firmware;
459 printf("The file %s contains a NXP unencrypted LPC DFU prefix with the following properties:\n", file->name);
460 printf("Size:\t%5d kiB\n", prefix[2]>>1|prefix[3]<<7);
461 } else if (file->size.prefix != 0) {
462 printf("The file %s contains an unknown prefix\n", file->name);
463 }
464 if (file->size.suffix > 0) {
465 printf("The file %s contains a DFU suffix with the following properties:\n", file->name);
466 printf("BCD device:\t0x%04X\n", file->bcdDevice);
467 printf("Product ID:\t0x%04X\n",file->idProduct);
468 printf("Vendor ID:\t0x%04X\n", file->idVendor);
469 printf("BCD DFU:\t0x%04X\n", file->bcdDFU);
470 printf("Length:\t\t%i\n", file->size.suffix);
471 printf("CRC:\t\t0x%08X\n", file->dwCRC);
472 }
473 }