]> cvs.zerfleddert.de Git - hmcfgusb/blobdiff - flash-hmcfgusb.c
Cleanup: Use := for assignment
[hmcfgusb] / flash-hmcfgusb.c
index aebf78151e4935629296b56967bd3d9b8942713f..8c858f2e2950199684863967f928d23b47663136 100644 (file)
@@ -1,6 +1,6 @@
-/* (not working) flasher for HM-CFG-USB
+/* flasher for HM-CFG-USB
  *
- * Copyright (c) 2013 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
@@ -36,6 +36,8 @@
 #include <libusb-1.0/libusb.h>
 
 #include "hexdump.h"
+#include "firmware.h"
+#include "version.h"
 #include "hmcfgusb.h"
 
 struct recv_data {
@@ -54,130 +56,124 @@ static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
        return 1;
 }
 
-static uint8_t ascii_to_nibble(uint8_t a)
+void flash_hmcfgusb_syntax(char *prog)
 {
-       uint8_t c = 0x00;
-
-       if ((a >= '0') && (a <= '9')) {
-               c = a - '0';
-       } else if ((a >= 'A') && (a <= 'F')) {
-               c = (a - 'A') + 10;
-       } else if ((a >= 'a') && (a <= 'f')) {
-               c = (a - 'a') + 10;
-       }
+       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");
 
-       return c;
 }
 
 int main(int argc, char **argv)
 {
+       const char twiddlie[] = { '-', '\\', '|', '/' };
        struct hmcfgusb_dev *dev;
        struct recv_data rdata;
-       uint8_t out[4096];
-       uint8_t buf[4096];
-       uint8_t *outp;
-       int fd;
+       uint16_t len;
+       struct firmware *fw;
+       char *serial = NULL;
+       char *filename = NULL;
+       int block;
        int pfd;
-       int r;
-       int i;
-       int cnt;
-       int pkt;
+       int opt;
        int debug = 0;
 
-       hmcfgusb_set_debug(debug);
+       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;
+               }
+       }
 
-       memset(&rdata, 0, sizeof(rdata));
+       if (optind == argc - 1) {
+               filename = argv[optind];
+       }
+
+       printf("HM-CFG-USB flasher version " VERSION "\n\n");
 
-       fd = open("hmusbif.enc", O_RDONLY);
-       if (fd < 0) {
-               perror("Can't open hmusbif.enc");
+       if (!filename) {
+               fprintf(stderr, "Missing firmware filename!\n\n");
+               flash_hmcfgusb_syntax(argv[0]);
                exit(EXIT_FAILURE);
        }
 
-       dev = hmcfgusb_init(parse_hmcfgusb, &rdata);
+       fw = firmware_read_firmware(filename, debug);
+       if (!fw)
+               exit(EXIT_FAILURE);
+
+       hmcfgusb_set_debug(debug);
+
+       memset(&rdata, 0, sizeof(rdata));
+
+       dev = hmcfgusb_init(parse_hmcfgusb, &rdata, serial);
        if (!dev) {
                fprintf(stderr, "Can't initialize HM-CFG-USB\n");
                exit(EXIT_FAILURE);
        }
 
        if (!dev->bootloader) {
-               fprintf(stderr, "HM-CFG-USB not in bootloader mode, entering bootloader.\n");
-               hmcfgusb_enter_bootloader(dev);
+               fprintf(stderr, "\nHM-CFG-USB not in bootloader mode, entering bootloader.\n");
                fprintf(stderr, "\nWaiting for device to reappear...\n");
 
                do {
+                       if (dev) {
+                               if (!dev->bootloader)
+                                       hmcfgusb_enter_bootloader(dev);
+                               hmcfgusb_close(dev);
+                       }
                        sleep(1);
-               } while ((dev = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL);
-
-               if (!dev->bootloader) {
-                       fprintf(stderr, "Can't enter bootloader, giving up!\n");
-                       exit(EXIT_FAILURE);
-               }
+               } while (((dev = hmcfgusb_init(parse_hmcfgusb, &rdata, serial)) == NULL) || (!dev->bootloader));
        }
 
-       printf("HM-CFG-USB opened!\n");
+       printf("\nHM-CFG-USB opened.\n\n");
 
-       cnt = 0;
-       pkt = 0;
-       do {
-               int len;
 
-               memset(buf, 0, sizeof(buf));
-               r = read(fd, buf, 4);
-               if (r < 0) {
-                       perror("read");
-                       exit(EXIT_FAILURE);
-               } else if (r == 0) {
-                       break;
-               } else if (r != 4) {
-                       printf("can't get length information!\n");
-                       exit(EXIT_FAILURE);
-               }
+       printf("Flashing %d blocks", fw->fw_blocks);
+       if (debug) {
+               printf("\n");
+       } else {
+               printf(": %c", twiddlie[0]);
+               fflush(stdout);
+       }
 
-               len = (ascii_to_nibble(buf[0]) & 0xf)<< 4;
-               len |= ascii_to_nibble(buf[1]) & 0xf;
-               len <<= 8;
-               len |= (ascii_to_nibble(buf[2]) & 0xf)<< 4;
-               len |= ascii_to_nibble(buf[3]) & 0xf;
+       for (block = 0; block < fw->fw_blocks; block++) {
+               len = fw->fw[block][2] << 8;
+               len |= fw->fw[block][3];
 
-               r = read(fd, buf, len * 2);
-               if (r < 0) {
-                       perror("read");
-                       exit(EXIT_FAILURE);
-               } else if (r < len * 2) {
-                       printf("short read, aborting (%d < %d)\n", r, len * 2);
-                       break;
-               }
+               len += 4; /* block nr., length */
 
-               memset(out, 0, sizeof(out));
-               outp = out;
-               *outp++ = (pkt >> 8) & 0xff;
-               *outp++ = pkt & 0xff;
-               *outp++ = (len >> 8) & 0xff;
-               *outp++ = len & 0xff;
-               for (i = 0; i < r; i+=2) {
-                       *outp = (ascii_to_nibble(buf[i]) & 0xf)<< 4;
-                       *outp |= ascii_to_nibble(buf[i+1]) & 0xf;
-                       outp++;
-               }
-               cnt = outp - out;
-               printf("Flashing %d bytes...\n", cnt);
                if (debug)
-                       hexdump(out, cnt, "F> ");
+                       hexdump(fw->fw[block], len, "F> ");
 
                rdata.ack = 0;
-               if (!hmcfgusb_send(dev, out, cnt, 0)) {
-                       perror("hmcfgusb_send");
+               if (!hmcfgusb_send(dev, fw->fw[block], len, 0)) {
+                       perror("\n\nhmcfgusb_send");
                        exit(EXIT_FAILURE);
                }
 
-               printf("Waiting for ack...\n");
+               if (debug)
+                       printf("Waiting for ack...\n");
                do {
                        errno = 0;
-                       pfd = hmcfgusb_poll(dev, 1);
+                       pfd = hmcfgusb_poll(dev, 1000);
                        if ((pfd < 0) && errno) {
-                               perror("hmcfgusb_poll");
-                               exit(EXIT_FAILURE);
+                               if (errno != ETIMEDOUT) {
+                                       perror("\n\nhmcfgusb_poll");
+                                       exit(EXIT_FAILURE);
+                               }
                        }
                        if (rdata.ack) {
                                break;
@@ -185,13 +181,25 @@ int main(int argc, char **argv)
                } while (pfd < 0);
 
                if (rdata.ack == 2) {
-                       printf("Firmware update successfull!\n");
+                       printf("\n\nFirmware update successfull!\n");
                        break;
                }
-               pkt++;
-       } while (r > 0);
+
+               if (rdata.ack != 1) {
+                       fprintf(stderr, "\n\nError flashing block %d, status: %u\n", block, rdata.ack);
+                       exit(EXIT_FAILURE);
+               }
+
+               if (!debug) {
+                       printf("\b%c", twiddlie[block % sizeof(twiddlie)]);
+                       fflush(stdout);
+               }
+       }
+
+       firmware_free(fw);
 
        hmcfgusb_close(dev);
+       hmcfgusb_exit();
 
        return EXIT_SUCCESS;
 }
Impressum, Datenschutz