diff options
-rw-r--r-- | utils/jz4740_tools/jz4740_usbtool.c | 66 |
1 files changed, 43 insertions, 23 deletions
diff --git a/utils/jz4740_tools/jz4740_usbtool.c b/utils/jz4740_tools/jz4740_usbtool.c index bc26804bdd..14fed4c878 100644 --- a/utils/jz4740_tools/jz4740_usbtool.c +++ b/utils/jz4740_tools/jz4740_usbtool.c | |||
@@ -362,7 +362,7 @@ unsigned int read_file(const char *name, unsigned char **buffer) | |||
362 | int len, n; | 362 | int len, n; |
363 | 363 | ||
364 | fd = fopen(name, "rb"); | 364 | fd = fopen(name, "rb"); |
365 | if (fd < 0) | 365 | if (fd == NULL) |
366 | { | 366 | { |
367 | fprintf(stderr, "[ERR] Could not open %s\n", name); | 367 | fprintf(stderr, "[ERR] Could not open %s\n", name); |
368 | return 0; | 368 | return 0; |
@@ -397,12 +397,16 @@ unsigned int read_file(const char *name, unsigned char **buffer) | |||
397 | SEND_COMMAND(VR_SET_DATA_ADDRESS, a); \ | 397 | SEND_COMMAND(VR_SET_DATA_ADDRESS, a); \ |
398 | fprintf(stderr, " Done!\n"); | 398 | fprintf(stderr, " Done!\n"); |
399 | #define _SEND_FILE(a) fsize = read_file(a, &buffer); \ | 399 | #define _SEND_FILE(a) fsize = read_file(a, &buffer); \ |
400 | if(fsize == 0) \ | ||
401 | return -1; \ | ||
400 | fprintf(stderr, "[INFO] Sending file %s: %d bytes...", a, fsize); \ | 402 | fprintf(stderr, "[INFO] Sending file %s: %d bytes...", a, fsize); \ |
401 | SEND_DATA(buffer, fsize); \ | 403 | SEND_DATA(buffer, fsize); \ |
402 | free(buffer); \ | 404 | free(buffer); \ |
403 | fprintf(stderr, " Done!\n"); | 405 | fprintf(stderr, " Done!\n"); |
404 | #define _VERIFY_DATA(a,c) fprintf(stderr, "[INFO] Verifying data (%s)...", a); \ | 406 | #define _VERIFY_DATA(a,c) fprintf(stderr, "[INFO] Verifying data (%s)...", a); \ |
405 | fsize = read_file(a, &buffer); \ | 407 | fsize = read_file(a, &buffer); \ |
408 | if(fsize == 0) \ | ||
409 | return -1; \ | ||
406 | buffer2 = (unsigned char*)malloc(fsize); \ | 410 | buffer2 = (unsigned char*)malloc(fsize); \ |
407 | SEND_COMMAND(VR_SET_DATA_ADDRESS, c); \ | 411 | SEND_COMMAND(VR_SET_DATA_ADDRESS, c); \ |
408 | SEND_COMMAND(VR_SET_DATA_LENGTH, fsize); \ | 412 | SEND_COMMAND(VR_SET_DATA_LENGTH, fsize); \ |
@@ -424,7 +428,7 @@ unsigned int read_file(const char *name, unsigned char **buffer) | |||
424 | #else | 428 | #else |
425 | #define _SLEEP(x) sleep(x); | 429 | #define _SLEEP(x) sleep(x); |
426 | #endif | 430 | #endif |
427 | int mimic_of(usb_dev_handle *dh) | 431 | int mimic_of(usb_dev_handle *dh, bool vx767) |
428 | { | 432 | { |
429 | int err, fsize; | 433 | int err, fsize; |
430 | unsigned char *buffer, *buffer2; | 434 | unsigned char *buffer, *buffer2; |
@@ -495,12 +499,19 @@ int mimic_of(usb_dev_handle *dh) | |||
495 | _GET_CPU; | 499 | _GET_CPU; |
496 | _FLUSH; | 500 | _FLUSH; |
497 | _GET_CPU; | 501 | _GET_CPU; |
498 | _STAGE2(0x80E00008); | 502 | if(vx767) |
503 | { | ||
504 | _STAGE2(0x80E10008); | ||
505 | } | ||
506 | else | ||
507 | { | ||
508 | _STAGE2(0x80E00008); | ||
509 | } | ||
499 | fprintf(stderr, "[INFO] Done!\n"); | 510 | fprintf(stderr, "[INFO] Done!\n"); |
500 | return 0; | 511 | return 0; |
501 | } | 512 | } |
502 | 513 | ||
503 | void jzconnect(int address, unsigned char* buf, int len, int func) | 514 | int jzconnect(int address, unsigned char* buf, int len, int func) |
504 | { | 515 | { |
505 | struct usb_bus *bus; | 516 | struct usb_bus *bus; |
506 | struct usb_device *tmp_dev; | 517 | struct usb_device *tmp_dev; |
@@ -514,26 +525,24 @@ void jzconnect(int address, unsigned char* buf, int len, int func) | |||
514 | if(usb_find_busses() < 0) | 525 | if(usb_find_busses() < 0) |
515 | { | 526 | { |
516 | fprintf(stderr, "[ERR] Could not find any USB busses.\n"); | 527 | fprintf(stderr, "[ERR] Could not find any USB busses.\n"); |
517 | return; | 528 | return -2; |
518 | } | 529 | } |
519 | 530 | ||
520 | if (usb_find_devices() < 0) | 531 | if (usb_find_devices() < 0) |
521 | { | 532 | { |
522 | fprintf(stderr, "[ERR] USB devices not found(nor hubs!).\n"); | 533 | fprintf(stderr, "[ERR] USB devices not found(nor hubs!).\n"); |
523 | return; | 534 | return -3; |
524 | } | 535 | } |
525 | 536 | ||
526 | for (bus = usb_get_busses(); bus; bus = bus->next) | 537 | for (bus = usb_get_busses(); bus; bus = bus->next) |
527 | { | 538 | { |
528 | for (tmp_dev = bus->devices; tmp_dev; tmp_dev = tmp_dev->next) | 539 | for (tmp_dev = bus->devices; tmp_dev; tmp_dev = tmp_dev->next) |
529 | { | 540 | { |
530 | //printf("Found Vendor %04x Product %04x\n",tmp_dev->descriptor.idVendor, tmp_dev->descriptor.idProduct); | ||
531 | if (tmp_dev->descriptor.idVendor == VID && | 541 | if (tmp_dev->descriptor.idVendor == VID && |
532 | tmp_dev->descriptor.idProduct == PID) | 542 | tmp_dev->descriptor.idProduct == PID) |
533 | { | 543 | { |
534 | dev = tmp_dev; | 544 | dev = tmp_dev; |
535 | goto found; | 545 | goto found; |
536 | |||
537 | } | 546 | } |
538 | } | 547 | } |
539 | } | 548 | } |
@@ -542,14 +551,14 @@ void jzconnect(int address, unsigned char* buf, int len, int func) | |||
542 | { | 551 | { |
543 | fprintf(stderr, "[ERR] Device not found.\n"); | 552 | fprintf(stderr, "[ERR] Device not found.\n"); |
544 | fprintf(stderr, "[ERR] Ensure your device is in USB boot mode and run usbtool again.\n"); | 553 | fprintf(stderr, "[ERR] Ensure your device is in USB boot mode and run usbtool again.\n"); |
545 | return; | 554 | return -4; |
546 | } | 555 | } |
547 | 556 | ||
548 | found: | 557 | found: |
549 | if ( (dh = usb_open(dev)) == NULL) | 558 | if ( (dh = usb_open(dev)) == NULL) |
550 | { | 559 | { |
551 | fprintf(stderr,"[ERR] Unable to open device.\n"); | 560 | fprintf(stderr,"[ERR] Unable to open device.\n"); |
552 | return; | 561 | return -5; |
553 | } | 562 | } |
554 | 563 | ||
555 | err = usb_set_configuration(dh, 1); | 564 | err = usb_set_configuration(dh, 1); |
@@ -558,7 +567,7 @@ found: | |||
558 | { | 567 | { |
559 | fprintf(stderr, "[ERR] usb_set_configuration failed (%d, %s)\n", err, usb_strerror()); | 568 | fprintf(stderr, "[ERR] usb_set_configuration failed (%d, %s)\n", err, usb_strerror()); |
560 | usb_close(dh); | 569 | usb_close(dh); |
561 | return; | 570 | return -6; |
562 | } | 571 | } |
563 | 572 | ||
564 | /* "must be called" written in the libusb documentation */ | 573 | /* "must be called" written in the libusb documentation */ |
@@ -567,7 +576,7 @@ found: | |||
567 | { | 576 | { |
568 | fprintf(stderr, "[ERR] Unable to claim interface (%d, %s)\n", err, usb_strerror()); | 577 | fprintf(stderr, "[ERR] Unable to claim interface (%d, %s)\n", err, usb_strerror()); |
569 | usb_close(dh); | 578 | usb_close(dh); |
570 | return; | 579 | return -7; |
571 | } | 580 | } |
572 | 581 | ||
573 | fprintf(stderr,"[INFO] Found device, uploading application.\n"); | 582 | fprintf(stderr,"[INFO] Found device, uploading application.\n"); |
@@ -590,7 +599,8 @@ found: | |||
590 | err = probe_device(dh); | 599 | err = probe_device(dh); |
591 | break; | 600 | break; |
592 | case 6: | 601 | case 6: |
593 | err = mimic_of(dh); | 602 | case 7: |
603 | err = mimic_of(dh, (func == 7)); | ||
594 | break; | 604 | break; |
595 | } | 605 | } |
596 | 606 | ||
@@ -598,6 +608,8 @@ found: | |||
598 | usb_release_interface(dh, 0); | 608 | usb_release_interface(dh, 0); |
599 | 609 | ||
600 | usb_close(dh); | 610 | usb_close(dh); |
611 | |||
612 | return err; | ||
601 | } | 613 | } |
602 | 614 | ||
603 | void print_usage(void) | 615 | void print_usage(void) |
@@ -608,15 +620,20 @@ void print_usage(void) | |||
608 | fprintf(stderr, "Usage: usbtool [CMD] [FILE] [ADDRESS] [LEN]\n"); | 620 | fprintf(stderr, "Usage: usbtool [CMD] [FILE] [ADDRESS] [LEN]\n"); |
609 | #endif | 621 | #endif |
610 | fprintf(stderr, "\t[ADDRESS] has to be in 0xHEXADECIMAL format\n"); | 622 | fprintf(stderr, "\t[ADDRESS] has to be in 0xHEXADECIMAL format\n"); |
611 | fprintf(stderr, "\t[CMD]:\n\t\t1 -> upload file to specified address and boot from it\n\t\t2 -> read data from [ADDRESS] with length [LEN] to [FILE]\n"); | 623 | fprintf(stderr, "\t[CMD]:\n"); |
612 | fprintf(stderr, "\t\t3 -> read device status\n\t\t4 -> probe keys (only Onda VX747)\n"); | 624 | fprintf(stderr, "\t\t1 -> upload file to specified address and boot from it\n"); |
613 | fprintf(stderr, "\t\t5 -> same as 1 but do a stage 2 boot\n\t\t6 -> mimic OF fw recovery\n"); | 625 | fprintf(stderr, "\t\t2 -> read data from [ADDRESS] with length [LEN] to [FILE]\n"); |
626 | fprintf(stderr, "\t\t3 -> read device status\n"); | ||
627 | fprintf(stderr, "\t\t4 -> probe keys (only Onda VX747)\n"); | ||
628 | fprintf(stderr, "\t\t5 -> same as 1 but do a stage 2 boot\n"); | ||
629 | fprintf(stderr, "\t\t6 -> mimic VX747 OF fw recovery\n"); | ||
630 | fprintf(stderr, "\t\t7 -> mimic VX767 OF fw recovery\n"); | ||
614 | #ifdef _WIN32 | 631 | #ifdef _WIN32 |
615 | fprintf(stderr, "\nExample:\n\t usbtool.exe 1 fw.bin 0x80000000"); | 632 | fprintf(stderr, "\nExample:\n\t usbtool.exe 1 fw.bin 0x80000000\n"); |
616 | fprintf(stderr, "\n\t usbtool.exe 2 save.bin 0x81000000 1024"); | 633 | fprintf(stderr, "\t usbtool.exe 2 save.bin 0x81000000 1024\n"); |
617 | #else | 634 | #else |
618 | fprintf(stderr, "\nExample:\n\t usbtool 1 fw.bin 0x80000000"); | 635 | fprintf(stderr, "\nExample:\n\t usbtool 1 fw.bin 0x80000000\n"); |
619 | fprintf(stderr, "\n\t usbtool 2 save.bin 0x81000000 1024"); | 636 | fprintf(stderr, "\t usbtool 2 save.bin 0x81000000 1024\n"); |
620 | #endif | 637 | #endif |
621 | } | 638 | } |
622 | 639 | ||
@@ -682,7 +699,7 @@ int main(int argc, char* argv[]) | |||
682 | 699 | ||
683 | fprintf(stderr, "[INFO] File size: %d bytes\n", n); | 700 | fprintf(stderr, "[INFO] File size: %d bytes\n", n); |
684 | 701 | ||
685 | jzconnect(address, buf, len, cmd); | 702 | return jzconnect(address, buf, len, cmd); |
686 | break; | 703 | break; |
687 | case 2: | 704 | case 2: |
688 | if (sscanf(argv[3], "0x%x", &address) <= 0) | 705 | if (sscanf(argv[3], "0x%x", &address) <= 0) |
@@ -708,7 +725,7 @@ int main(int argc, char* argv[]) | |||
708 | return 6; | 725 | return 6; |
709 | } | 726 | } |
710 | 727 | ||
711 | jzconnect(address, buf, len, 2); | 728 | int err = jzconnect(address, buf, len, 2); |
712 | 729 | ||
713 | n = fwrite(buf, 1, len, fd); | 730 | n = fwrite(buf, 1, len, fd); |
714 | if (n != len) | 731 | if (n != len) |
@@ -718,11 +735,14 @@ int main(int argc, char* argv[]) | |||
718 | return 7; | 735 | return 7; |
719 | } | 736 | } |
720 | fclose(fd); | 737 | fclose(fd); |
738 | |||
739 | return err; | ||
721 | break; | 740 | break; |
722 | case 3: | 741 | case 3: |
723 | case 4: | 742 | case 4: |
724 | case 6: | 743 | case 6: |
725 | jzconnect(address, NULL, 0, cmd); | 744 | case 7: |
745 | return jzconnect(address, NULL, 0, cmd); | ||
726 | break; | 746 | break; |
727 | default: | 747 | default: |
728 | print_usage(); | 748 | print_usage(); |