X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/hmcfgusb/blobdiff_plain/6c5c2df75ff018417dd6abb38196d998d5fa5b10..HEAD:/flash-hmcfgusb.c diff --git a/flash-hmcfgusb.c b/flash-hmcfgusb.c index 571386f..78a7a80 100644 --- a/flash-hmcfgusb.c +++ b/flash-hmcfgusb.c @@ -1,6 +1,6 @@ /* flasher for HM-CFG-USB * - * Copyright (c) 2013-14 Michael Gernoth + * Copyright (c) 2013-20 Michael Gernoth * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to @@ -56,6 +56,15 @@ static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data) return 1; } +void flash_hmcfgusb_syntax(char *prog) +{ + fprintf(stderr, "Syntax: %s [options] filename.enc\n\n", prog); + fprintf(stderr, "Possible options:\n"); + fprintf(stderr, "\t-S serial\tuse HM-CFG-USB with given serial\n"); + fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n"); + +} + int main(int argc, char **argv) { const char twiddlie[] = { '-', '\\', '|', '/' }; @@ -63,21 +72,45 @@ int main(int argc, char **argv) struct recv_data rdata; uint16_t len; struct firmware *fw; + char *serial = NULL; + char *filename = NULL; int block; int pfd; + int opt; int debug = 0; - printf("HM-CFG-USB flasher version " VERSION "\n\n"); + while((opt = getopt(argc, argv, "S:V")) != -1) { + switch (opt) { + case 'S': + serial = optarg; + break; + case 'V': + printf("flash-hmcfgusb " VERSION "\n"); + printf("Copyright (c) 2013-16 Michael Gernoth\n\n"); + exit(EXIT_SUCCESS); + case 'h': + case ':': + case '?': + default: + flash_hmcfgusb_syntax(argv[0]); + exit(EXIT_FAILURE); + break; + } + } - if (argc != 2) { - if (argc == 1) - fprintf(stderr, "Missing firmware filename!\n\n"); + if (optind == argc - 1) { + filename = argv[optind]; + } - fprintf(stderr, "Syntax: %s hmusbif.enc\n\n", argv[0]); + printf("HM-CFG-USB flasher version " VERSION "\n\n"); + + if (!filename) { + fprintf(stderr, "Missing firmware filename!\n\n"); + flash_hmcfgusb_syntax(argv[0]); exit(EXIT_FAILURE); } - fw = firmware_read_firmware(argv[1], debug); + fw = firmware_read_firmware(filename, ATMEGA_UNKNOWN, debug); if (!fw) exit(EXIT_FAILURE); @@ -85,7 +118,7 @@ int main(int argc, char **argv) memset(&rdata, 0, sizeof(rdata)); - dev = hmcfgusb_init(parse_hmcfgusb, &rdata); + dev = hmcfgusb_init(parse_hmcfgusb, &rdata, serial); if (!dev) { fprintf(stderr, "Can't initialize HM-CFG-USB\n"); exit(EXIT_FAILURE); @@ -93,17 +126,16 @@ int main(int argc, char **argv) if (!dev->bootloader) { fprintf(stderr, "\nHM-CFG-USB not in bootloader mode, entering bootloader.\n"); - hmcfgusb_enter_bootloader(dev); fprintf(stderr, "\nWaiting for device to reappear...\n"); do { - sleep(2); - } while ((dev = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL); - - if (!dev->bootloader) { - fprintf(stderr, "Can't enter bootloader, giving up!\n"); - exit(EXIT_FAILURE); - } + if (dev) { + if (!dev->bootloader) + hmcfgusb_enter_bootloader(dev); + hmcfgusb_close(dev); + } + sleep(1); + } while (((dev = hmcfgusb_init(parse_hmcfgusb, &rdata, serial)) == NULL) || (!dev->bootloader)); } printf("\nHM-CFG-USB opened.\n\n"); @@ -136,10 +168,12 @@ int main(int argc, char **argv) printf("Waiting for ack...\n"); do { errno = 0; - pfd = hmcfgusb_poll(dev, 1); + pfd = hmcfgusb_poll(dev, 1000); if ((pfd < 0) && errno) { - perror("\n\nhmcfgusb_poll"); - exit(EXIT_FAILURE); + if (errno != ETIMEDOUT) { + perror("\n\nhmcfgusb_poll"); + exit(EXIT_FAILURE); + } } if (rdata.ack) { break; @@ -165,6 +199,7 @@ int main(int argc, char **argv) firmware_free(fw); hmcfgusb_close(dev); + hmcfgusb_exit(); return EXIT_SUCCESS; }