]> git.zerfleddert.de Git - hmcfgusb/blobdiff - flash-hmcfgusb.c
Suppress warnings about non-literal format strings
[hmcfgusb] / flash-hmcfgusb.c
index 0f0b9cef707b2b48424942d28b6144bc381d39f1..8c858f2e2950199684863967f928d23b47663136 100644 (file)
@@ -1,6 +1,6 @@
 /* flasher for HM-CFG-USB
  *
 /* flasher for HM-CFG-USB
  *
- * Copyright (c) 2013-14 Michael Gernoth <michael@gernoth.net>
+ * Copyright (c) 2013-16 Michael Gernoth <michael@gernoth.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
  *
  * 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;
 }
 
        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[] = { '-', '\\', '|', '/' };
 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;
        struct recv_data rdata;
        uint16_t len;
        struct firmware *fw;
+       char *serial = NULL;
+       char *filename = NULL;
        int block;
        int pfd;
        int block;
        int pfd;
+       int opt;
        int debug = 0;
 
        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];
+       }
+
+       printf("HM-CFG-USB flasher version " VERSION "\n\n");
 
 
-               fprintf(stderr, "Syntax: %s hmusbif.enc\n\n", argv[0]);
+       if (!filename) {
+               fprintf(stderr, "Missing firmware filename!\n\n");
+               flash_hmcfgusb_syntax(argv[0]);
                exit(EXIT_FAILURE);
        }
 
                exit(EXIT_FAILURE);
        }
 
-       fw = firmware_read_firmware(argv[1], debug);
+       fw = firmware_read_firmware(filename, debug);
        if (!fw)
                exit(EXIT_FAILURE);
 
        if (!fw)
                exit(EXIT_FAILURE);
 
@@ -85,7 +118,7 @@ int main(int argc, char **argv)
 
        memset(&rdata, 0, sizeof(rdata));
 
 
        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);
        if (!dev) {
                fprintf(stderr, "Can't initialize HM-CFG-USB\n");
                exit(EXIT_FAILURE);
@@ -93,15 +126,16 @@ int main(int argc, char **argv)
 
        if (!dev->bootloader) {
                fprintf(stderr, "\nHM-CFG-USB not in bootloader mode, entering bootloader.\n");
 
        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 {
                        if (dev) {
                fprintf(stderr, "\nWaiting for device to reappear...\n");
 
                do {
                        if (dev) {
+                               if (!dev->bootloader)
+                                       hmcfgusb_enter_bootloader(dev);
                                hmcfgusb_close(dev);
                        }
                        sleep(1);
                                hmcfgusb_close(dev);
                        }
                        sleep(1);
-               } while (((dev = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (!dev->bootloader));
+               } while (((dev = hmcfgusb_init(parse_hmcfgusb, &rdata, serial)) == NULL) || (!dev->bootloader));
        }
 
        printf("\nHM-CFG-USB opened.\n\n");
        }
 
        printf("\nHM-CFG-USB opened.\n\n");
@@ -165,6 +199,7 @@ int main(int argc, char **argv)
        firmware_free(fw);
 
        hmcfgusb_close(dev);
        firmware_free(fw);
 
        hmcfgusb_close(dev);
+       hmcfgusb_exit();
 
        return EXIT_SUCCESS;
 }
 
        return EXIT_SUCCESS;
 }
Impressum, Datenschutz