diff options
author | Marcin Bukat <marcin.bukat@gmail.com> | 2014-11-06 07:56:02 +0100 |
---|---|---|
committer | Marcin Bukat <marcin.bukat@gmail.com> | 2014-11-06 07:56:02 +0100 |
commit | df2ac7428f1ab98ccc2109d4f70521c5f8404c2c (patch) | |
tree | b3eba7eb29f9402dd1bccaa945dcb13c7cc6d2c7 /utils | |
parent | d11704fed5fd218b2ed26182de877bc6e5b513a4 (diff) | |
download | rockbox-df2ac7428f1ab98ccc2109d4f70521c5f8404c2c.tar.gz rockbox-df2ac7428f1ab98ccc2109d4f70521c5f8404c2c.zip |
adfuload: Improve arguments parsing
Change-Id: If18975f13d20bb7f7232cafdb4ea87fa516b5750
Diffstat (limited to 'utils')
-rw-r--r-- | utils/atj2137/adfuload/adfuload.c | 124 |
1 files changed, 85 insertions, 39 deletions
diff --git a/utils/atj2137/adfuload/adfuload.c b/utils/atj2137/adfuload/adfuload.c index 5e32a9354b..2e60c31e4d 100644 --- a/utils/atj2137/adfuload/adfuload.c +++ b/utils/atj2137/adfuload/adfuload.c | |||
@@ -258,24 +258,29 @@ static void adfu_execute(libusb_device_handle *hdev, uint32_t address) | |||
258 | 258 | ||
259 | static void usage(char *name) | 259 | static void usage(char *name) |
260 | { | 260 | { |
261 | printf("usage: (sudo) %s [-e] -s1 stage1.bin -s2 stage2.bin\n", name); | 261 | printf("usage: (sudo) %s [-u vid:pid] [-e] -s1 stage1.bin -s2 stage2.bin\n", name); |
262 | printf("stage1.bin - binary of the stage1 (ADEC_N63.BIN for example)\n"); | 262 | printf("stage1.bin Binary of the stage1 (ADEC_N63.BIN for example)\n"); |
263 | printf("stage2.bin - binary of the custom user code\n"); | 263 | printf("stage2.bin Binary of the custom user code\n"); |
264 | printf("\n"); | 264 | printf("\n"); |
265 | printf("options:\n"); | 265 | printf("options:\n"); |
266 | printf("-e - encode stage1 binary as needed by brom adfu mode\n"); | 266 | printf("-u|--usb vid:pid Override device PID and PID (default 0x%04x:0x%04x)\n", |
267 | VENDORID, PRODUCTID); | ||
268 | printf("-e Encode stage1 binary as needed by brom adfu mode\n"); | ||
267 | } | 269 | } |
268 | 270 | ||
269 | int main (int argc, char **argv) | 271 | int main (int argc, char **argv) |
270 | { | 272 | { |
273 | uint16_t pid = PRODUCTID; | ||
274 | uint16_t vid = VENDORID; | ||
275 | |||
271 | libusb_device_handle *hdev; | 276 | libusb_device_handle *hdev; |
272 | int ret = 0; | 277 | int ret = 0; |
273 | int i = 1; | 278 | int i = 1; |
274 | uint8_t *buf; | 279 | uint8_t *buf; |
275 | uint32_t filesize, filesize_round; | 280 | uint32_t filesize, filesize_round; |
276 | char *s1_filename, *s2_filename; | 281 | char *s1_filename = NULL, *s2_filename = NULL; |
277 | bool encode = false; | 282 | bool encode = false; |
278 | FILE *fp; | 283 | FILE *fp = NULL; |
279 | 284 | ||
280 | while (i < argc) | 285 | while (i < argc) |
281 | { | 286 | { |
@@ -306,6 +311,35 @@ int main (int argc, char **argv) | |||
306 | s2_filename = argv[i]; | 311 | s2_filename = argv[i]; |
307 | i++; | 312 | i++; |
308 | } | 313 | } |
314 | else if ((strcmp(argv[i],"-u")==0) || (strcmp(argv[i],"--usb")==0)) | ||
315 | { | ||
316 | if (i + 1 == argc) | ||
317 | { | ||
318 | fprintf(stderr,"Missing argument for USB IDs\n"); | ||
319 | return -1; | ||
320 | } | ||
321 | char *svid = argv[i + 1]; | ||
322 | char *spid = strchr(svid, ':'); | ||
323 | if(svid == NULL) | ||
324 | { | ||
325 | fprintf(stderr,"Invalid argument for USB IDs (missing ':')\n"); | ||
326 | return -2; | ||
327 | } | ||
328 | char *end; | ||
329 | vid = strtoul(svid, &end, 0); | ||
330 | if(*end != ':') | ||
331 | { | ||
332 | fprintf(stderr,"Invalid argument for USB VID\n"); | ||
333 | return -3; | ||
334 | } | ||
335 | pid = strtoul(spid + 1, &end, 0); | ||
336 | if(*end) | ||
337 | { | ||
338 | fprintf(stderr,"Invalid argument for USB PID\n"); | ||
339 | return -4; | ||
340 | } | ||
341 | i++; | ||
342 | } | ||
309 | else | 343 | else |
310 | { | 344 | { |
311 | usage(argv[0]); | 345 | usage(argv[0]); |
@@ -313,6 +347,12 @@ int main (int argc, char **argv) | |||
313 | } | 347 | } |
314 | } | 348 | } |
315 | 349 | ||
350 | if (!s1_filename) | ||
351 | { | ||
352 | usage(argv[0]); | ||
353 | return -3; | ||
354 | } | ||
355 | |||
316 | /* print banner */ | 356 | /* print banner */ |
317 | fprintf(stderr,"adfuload " VERSION "\n"); | 357 | fprintf(stderr,"adfuload " VERSION "\n"); |
318 | fprintf(stderr,"(C) Marcin Bukat 2013\n"); | 358 | fprintf(stderr,"(C) Marcin Bukat 2013\n"); |
@@ -322,10 +362,10 @@ int main (int argc, char **argv) | |||
322 | /* initialize libusb */ | 362 | /* initialize libusb */ |
323 | libusb_init(NULL); | 363 | libusb_init(NULL); |
324 | 364 | ||
325 | hdev = libusb_open_device_with_vid_pid(NULL, VENDORID, PRODUCTID); | 365 | hdev = libusb_open_device_with_vid_pid(NULL, vid, pid); |
326 | if (hdev == NULL) | 366 | if (hdev == NULL) |
327 | { | 367 | { |
328 | fprintf(stderr, "[error]: can't open device with VID:PID=0x%0x:0x%0x\n", VENDORID, PRODUCTID); | 368 | fprintf(stderr, "[error]: can't open device with VID:PID = 0x%0x:0x%0x\n", vid, pid); |
329 | libusb_exit(NULL); | 369 | libusb_exit(NULL); |
330 | return -4; | 370 | return -4; |
331 | } | 371 | } |
@@ -401,7 +441,6 @@ int main (int argc, char **argv) | |||
401 | 441 | ||
402 | fclose(fp); | 442 | fclose(fp); |
403 | 443 | ||
404 | fprintf(stderr, "[info]: file %s\n", s2_filename); | ||
405 | 444 | ||
406 | if (encode) | 445 | if (encode) |
407 | { | 446 | { |
@@ -420,45 +459,52 @@ int main (int argc, char **argv) | |||
420 | adfu_execute(hdev, 0xb4040000); | 459 | adfu_execute(hdev, 0xb4040000); |
421 | /* Now ADEC_N63.BIN should be operational */ | 460 | /* Now ADEC_N63.BIN should be operational */ |
422 | 461 | ||
423 | /* upload custom binary and run it */ | 462 | if (s2_filename) |
424 | fp = fopen(s2_filename, "rb"); | ||
425 | |||
426 | if (fp == NULL) | ||
427 | { | 463 | { |
428 | fprintf(stderr, "[error]: could not open file \"%s\"\n", s2_filename); | 464 | fprintf(stderr, "[info]: file %s\n", s2_filename); |
429 | ret = -20; | ||
430 | goto end; | ||
431 | } | ||
432 | 465 | ||
433 | fseek(fp, 0, SEEK_END); | 466 | /* upload custom binary and run it */ |
434 | filesize = (uint32_t)ftell(fp); | 467 | fp = fopen(s2_filename, "rb"); |
435 | fseek(fp, 0, SEEK_SET); | ||
436 | 468 | ||
437 | buf = realloc(buf, filesize); | 469 | if (fp == NULL) |
470 | { | ||
471 | fprintf(stderr, "[error]: could not open file \"%s\"\n", s2_filename); | ||
472 | ret = -20; | ||
473 | goto end; | ||
474 | } | ||
438 | 475 | ||
439 | if (buf == NULL) | 476 | fseek(fp, 0, SEEK_END); |
440 | { | 477 | filesize = (uint32_t)ftell(fp); |
441 | fprintf(stderr, "[error]: can't allocate %d bytes of memory\n", filesize); | 478 | fseek(fp, 0, SEEK_SET); |
442 | ret = -21; | ||
443 | goto end_fclose; | ||
444 | } | ||
445 | 479 | ||
446 | if (fread(buf, 1, filesize, fp) != filesize) | 480 | buf = realloc(buf, filesize); |
447 | { | ||
448 | fprintf(stderr, "[error]: can't read file: %s\n", s2_filename); | ||
449 | ret = -22; | ||
450 | goto end_free; | ||
451 | } | ||
452 | 481 | ||
453 | fprintf(stderr, "[info]: file %s\n", s2_filename); | 482 | if (buf == NULL) |
454 | /* upload binary to the begining of DRAM */ | 483 | { |
455 | adfu_upload(hdev, buf, filesize, 0xa0000000); | 484 | fprintf(stderr, "[error]: can't allocate %d bytes of memory\n", filesize); |
456 | adfu_execute(hdev, 0xa0000000); | 485 | ret = -21; |
486 | goto end_fclose; | ||
487 | } | ||
488 | |||
489 | if (fread(buf, 1, filesize, fp) != filesize) | ||
490 | { | ||
491 | fprintf(stderr, "[error]: can't read file: %s\n", s2_filename); | ||
492 | ret = -22; | ||
493 | goto end_free; | ||
494 | } | ||
495 | |||
496 | fprintf(stderr, "[info]: file %s\n", s2_filename); | ||
497 | /* upload binary to the begining of DRAM */ | ||
498 | adfu_upload(hdev, buf, filesize, 0xa0000000); | ||
499 | adfu_execute(hdev, 0xa0000000); | ||
500 | } | ||
501 | else | ||
502 | fp = NULL; | ||
457 | 503 | ||
458 | end_free: | 504 | end_free: |
459 | free(buf); | 505 | if (buf) {free(buf);} |
460 | end_fclose: | 506 | end_fclose: |
461 | fclose(fp); | 507 | if (fp) {fclose(fp);} |
462 | end: | 508 | end: |
463 | libusb_close(hdev); | 509 | libusb_close(hdev); |
464 | libusb_exit(NULL); | 510 | libusb_exit(NULL); |