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 | |
parent | d11704fed5fd218b2ed26182de877bc6e5b513a4 (diff) |
adfuload: Improve arguments parsing
Change-Id: If18975f13d20bb7f7232cafdb4ea87fa516b5750
-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) static void usage(char *name) { - printf("usage: (sudo) %s [-e] -s1 stage1.bin -s2 stage2.bin\n", name); - printf("stage1.bin - binary of the stage1 (ADEC_N63.BIN for example)\n"); - printf("stage2.bin - binary of the custom user code\n"); + printf("usage: (sudo) %s [-u vid:pid] [-e] -s1 stage1.bin -s2 stage2.bin\n", name); + printf("stage1.bin Binary of the stage1 (ADEC_N63.BIN for example)\n"); + printf("stage2.bin Binary of the custom user code\n"); printf("\n"); printf("options:\n"); - printf("-e - encode stage1 binary as needed by brom adfu mode\n"); + printf("-u|--usb vid:pid Override device PID and PID (default 0x%04x:0x%04x)\n", + VENDORID, PRODUCTID); + printf("-e Encode stage1 binary as needed by brom adfu mode\n"); } int main (int argc, char **argv) { + uint16_t pid = PRODUCTID; + uint16_t vid = VENDORID; + libusb_device_handle *hdev; int ret = 0; int i = 1; uint8_t *buf; uint32_t filesize, filesize_round; - char *s1_filename, *s2_filename; + char *s1_filename = NULL, *s2_filename = NULL; bool encode = false; - FILE *fp; + FILE *fp = NULL; while (i < argc) { @@ -306,6 +311,35 @@ int main (int argc, char **argv) s2_filename = argv[i]; i++; } + else if ((strcmp(argv[i],"-u")==0) || (strcmp(argv[i],"--usb")==0)) + { + if (i + 1 == argc) + { + fprintf(stderr,"Missing argument for USB IDs\n"); + return -1; + } + char *svid = argv[i + 1]; + char *spid = strchr(svid, ':'); + if(svid == NULL) + { + fprintf(stderr,"Invalid argument for USB IDs (missing ':')\n"); + return -2; + } + char *end; + vid = strtoul(svid, &end, 0); + if(*end != ':') + { + fprintf(stderr,"Invalid argument for USB VID\n"); + return -3; + } + pid = strtoul(spid + 1, &end, 0); + if(*end) + { + fprintf(stderr,"Invalid argument for USB PID\n"); + return -4; + } + i++; + } else { usage(argv[0]); @@ -313,6 +347,12 @@ int main (int argc, char **argv) } } + if (!s1_filename) + { + usage(argv[0]); + return -3; + } + /* print banner */ fprintf(stderr,"adfuload " VERSION "\n"); fprintf(stderr,"(C) Marcin Bukat 2013\n"); @@ -322,10 +362,10 @@ int main (int argc, char **argv) /* initialize libusb */ libusb_init(NULL); - hdev = libusb_open_device_with_vid_pid(NULL, VENDORID, PRODUCTID); + hdev = libusb_open_device_with_vid_pid(NULL, vid, pid); if (hdev == NULL) { - fprintf(stderr, "[error]: can't open device with VID:PID=0x%0x:0x%0x\n", VENDORID, PRODUCTID); + fprintf(stderr, "[error]: can't open device with VID:PID = 0x%0x:0x%0x\n", vid, pid); libusb_exit(NULL); return -4; } @@ -401,7 +441,6 @@ int main (int argc, char **argv) fclose(fp); - fprintf(stderr, "[info]: file %s\n", s2_filename); if (encode) { @@ -420,45 +459,52 @@ int main (int argc, char **argv) adfu_execute(hdev, 0xb4040000); /* Now ADEC_N63.BIN should be operational */ - /* upload custom binary and run it */ - fp = fopen(s2_filename, "rb"); - - if (fp == NULL) + if (s2_filename) { - fprintf(stderr, "[error]: could not open file \"%s\"\n", s2_filename); - ret = -20; - goto end; - } + fprintf(stderr, "[info]: file %s\n", s2_filename); - fseek(fp, 0, SEEK_END); - filesize = (uint32_t)ftell(fp); - fseek(fp, 0, SEEK_SET); + /* upload custom binary and run it */ + fp = fopen(s2_filename, "rb"); - buf = realloc(buf, filesize); + if (fp == NULL) + { + fprintf(stderr, "[error]: could not open file \"%s\"\n", s2_filename); + ret = -20; + goto end; + } - if (buf == NULL) - { - fprintf(stderr, "[error]: can't allocate %d bytes of memory\n", filesize); - ret = -21; - goto end_fclose; - } + fseek(fp, 0, SEEK_END); + filesize = (uint32_t)ftell(fp); + fseek(fp, 0, SEEK_SET); - if (fread(buf, 1, filesize, fp) != filesize) - { - fprintf(stderr, "[error]: can't read file: %s\n", s2_filename); - ret = -22; - goto end_free; - } + buf = realloc(buf, filesize); - fprintf(stderr, "[info]: file %s\n", s2_filename); - /* upload binary to the begining of DRAM */ - adfu_upload(hdev, buf, filesize, 0xa0000000); - adfu_execute(hdev, 0xa0000000); + if (buf == NULL) + { + fprintf(stderr, "[error]: can't allocate %d bytes of memory\n", filesize); + ret = -21; + goto end_fclose; + } + + if (fread(buf, 1, filesize, fp) != filesize) + { + fprintf(stderr, "[error]: can't read file: %s\n", s2_filename); + ret = -22; + goto end_free; + } + + fprintf(stderr, "[info]: file %s\n", s2_filename); + /* upload binary to the begining of DRAM */ + adfu_upload(hdev, buf, filesize, 0xa0000000); + adfu_execute(hdev, 0xa0000000); + } + else + fp = NULL; end_free: - free(buf); + if (buf) {free(buf);} end_fclose: - fclose(fp); + if (fp) {fclose(fp);} end: libusb_close(hdev); libusb_exit(NULL); |