]> git.zerfleddert.de Git - hmcfgusb/blobdiff - flash-hmcfgusb.c
Starting to add debian build stuff
[hmcfgusb] / flash-hmcfgusb.c
index c7781a047b2378395a3ed07c0f2eccd94612d036..0f0b9cef707b2b48424942d28b6144bc381d39f1 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-14 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
 #include <libusb-1.0/libusb.h>
 
 #include "hexdump.h"
+#include "firmware.h"
+#include "version.h"
 #include "hmcfgusb.h"
 
 struct recv_data {
+       int ack;
 };
 
 static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
 {
-       if (buf_len < 1)
+       struct recv_data *rdata = data;
+
+       if (buf_len != 1)
                return 1;
 
-       switch(buf[0]) {
-               case 'E':
-               case 'H':
-               case 'R':
-               case 'I':
-                       break;
-               default:
-                       hexdump(buf, buf_len, "Unknown> ");
-                       break;
-       }
+       rdata->ack = buf[0];
 
        return 1;
 }
 
-static uint8_t ascii_to_nibble(uint8_t a)
-{
-       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;
-       }
-
-       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;
-       int r;
-       int i;
-       int cnt;
-       int pkt;
+       uint16_t len;
+       struct firmware *fw;
+       int block;
+       int pfd;
+       int debug = 0;
+
+       printf("HM-CFG-USB flasher version " VERSION "\n\n");
+
+       if (argc != 2) {
+               if (argc == 1)
+                       fprintf(stderr, "Missing firmware filename!\n\n");
+
+               fprintf(stderr, "Syntax: %s hmusbif.enc\n\n", argv[0]);
+               exit(EXIT_FAILURE);
+       }
+
+       fw = firmware_read_firmware(argv[1], debug);
+       if (!fw)
+               exit(EXIT_FAILURE);
 
-       hmcfgusb_set_debug(0);
+       hmcfgusb_set_debug(debug);
 
        memset(&rdata, 0, sizeof(rdata));
 
@@ -97,66 +90,79 @@ int main(int argc, char **argv)
                fprintf(stderr, "Can't initialize HM-CFG-USB\n");
                exit(EXIT_FAILURE);
        }
-       printf("HM-CFG-USB opened!\n");
 
-       fd = open("hmusbif.enc", O_RDONLY);
-       if (fd < 0) {
-               perror("Can't open hmusbif.enc");
-               exit(EXIT_FAILURE);
+       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) {
+                               hmcfgusb_close(dev);
+                       }
+                       sleep(1);
+               } while (((dev = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (!dev->bootloader));
        }
 
-       cnt = 0;
-       pkt = 0;
-       do {
-               int len;
+       printf("\nHM-CFG-USB opened.\n\n");
 
-               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);
-               }
 
-               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;
+       printf("Flashing %d blocks", fw->fw_blocks);
+       if (debug) {
+               printf("\n");
+       } else {
+               printf(": %c", twiddlie[0]);
+               fflush(stdout);
+       }
+
+       for (block = 0; block < fw->fw_blocks; block++) {
+               len = fw->fw[block][2] << 8;
+               len |= fw->fw[block][3];
+
+               len += 4; /* block nr., length */
 
-               printf("packet length: %x\n", len);
+               if (debug)
+                       hexdump(fw->fw[block], len, "F> ");
 
-               r = read(fd, buf, len * 2);
-               if (r < 0) {
-                       perror("read");
+               rdata.ack = 0;
+               if (!hmcfgusb_send(dev, fw->fw[block], len, 0)) {
+                       perror("\n\nhmcfgusb_send");
                        exit(EXIT_FAILURE);
-               } else if (r < len * 2) {
-                       printf("short read, aborting (%d < %d)\n", r, len * 2);
+               }
+
+               if (debug)
+                       printf("Waiting for ack...\n");
+               do {
+                       errno = 0;
+                       pfd = hmcfgusb_poll(dev, 1000);
+                       if ((pfd < 0) && errno) {
+                               if (errno != ETIMEDOUT) {
+                                       perror("\n\nhmcfgusb_poll");
+                                       exit(EXIT_FAILURE);
+                               }
+                       }
+                       if (rdata.ack) {
+                               break;
+                       }
+               } while (pfd < 0);
+
+               if (rdata.ack == 2) {
+                       printf("\n\nFirmware update successfull!\n");
                        break;
                }
 
-               memset(out, 0, sizeof(out));
-               outp = out;
-               *outp++ = 'W';
-               *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++;
+               if (rdata.ack != 1) {
+                       fprintf(stderr, "\n\nError flashing block %d, status: %u\n", block, rdata.ack);
+                       exit(EXIT_FAILURE);
                }
-               cnt += r/2;
-               printf("Flashing %d bytes...\n", cnt);
-               hexdump(out, outp-out, "F> ");
-               //hmcfgusb_send(dev, out, r/2, 1);
-               pkt++;
-       } while (r > 0);
+
+               if (!debug) {
+                       printf("\b%c", twiddlie[block % sizeof(twiddlie)]);
+                       fflush(stdout);
+               }
+       }
+
+       firmware_free(fw);
 
        hmcfgusb_close(dev);
 
Impressum, Datenschutz