]> git.zerfleddert.de Git - hmcfgusb/commitdiff
flash-hmmoduart: add utility to upgrade firmware on HM-MOD-UART
authorMichael Gernoth <michael@gernoth.net>
Wed, 8 Jun 2016 18:26:47 +0000 (20:26 +0200)
committerMichael Gernoth <michael@gernoth.net>
Wed, 8 Jun 2016 18:26:47 +0000 (20:26 +0200)
.gitignore
Makefile
firmware.c
flash-hmmoduart.c [new file with mode: 0644]
flash-ota.c
hmsniff.c
hmuartlgw.c
hmuartlgw.h

index e1e2a29e0e40c9e95f97a87e5e4998d2b5b93727..eaafd80d02d981f62ddeccd4064be1ebcc714af0 100644 (file)
@@ -5,6 +5,9 @@ culfw.o
 flash-hmcfgusb
 flash-hmcfgusb.d
 flash-hmcfgusb.o
 flash-hmcfgusb
 flash-hmcfgusb.d
 flash-hmcfgusb.o
+flash-hmmoduart
+flash-hmmoduart.d
+flash-hmmoduart.o
 flash-ota
 flash-ota.d
 flash-ota.o
 flash-ota
 flash-ota.d
 flash-ota.o
index 1a29116d81e0e179fe88b3031592275c08e34e0f..9693ca1284a51bce7443fd539e0050003c763579 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -9,11 +9,12 @@ CC=gcc
 HMLAN_OBJS=hmcfgusb.o hmland.o util.o
 HMSNIFF_OBJS=hmcfgusb.o hmuartlgw.o hmsniff.o
 FLASH_HMCFGUSB_OBJS=hmcfgusb.o firmware.o util.o flash-hmcfgusb.o
 HMLAN_OBJS=hmcfgusb.o hmland.o util.o
 HMSNIFF_OBJS=hmcfgusb.o hmuartlgw.o hmsniff.o
 FLASH_HMCFGUSB_OBJS=hmcfgusb.o firmware.o util.o flash-hmcfgusb.o
+FLASH_HMMODUART_OBJS=hmuartlgw.o firmware.o util.o flash-hmmoduart.o
 FLASH_OTA_OBJS=hmcfgusb.o culfw.o hmuartlgw.o firmware.o util.o flash-ota.o hm.o aes.o
 
 FLASH_OTA_OBJS=hmcfgusb.o culfw.o hmuartlgw.o firmware.o util.o flash-ota.o hm.o aes.o
 
-OBJS=$(HMLAN_OBJS) $(HMSNIFF_OBJS) $(FLASH_HMCFGUSB_OBJS) $(FLASH_OTA_OBJS)
+OBJS=$(HMLAN_OBJS) $(HMSNIFF_OBJS) $(FLASH_HMCFGUSB_OBJS) $(FLASH_HMMODUART_OBJS) $(FLASH_OTA_OBJS)
 
 
-all: hmland hmsniff flash-hmcfgusb flash-ota
+all: hmland hmsniff flash-hmcfgusb flash-hmmoduart flash-ota
 
 DEPEND=$(OBJS:.o=.d)
 -include $(DEPEND)
 
 DEPEND=$(OBJS:.o=.d)
 -include $(DEPEND)
@@ -24,6 +25,8 @@ hmsniff: $(HMSNIFF_OBJS)
 
 flash-hmcfgusb: $(FLASH_HMCFGUSB_OBJS)
 
 
 flash-hmcfgusb: $(FLASH_HMCFGUSB_OBJS)
 
+flash-hmmoduart: $(FLASH_HMMODUART_OBJS)
+
 flash-ota: $(FLASH_OTA_OBJS)
 
 clean:
 flash-ota: $(FLASH_OTA_OBJS)
 
 clean:
index a52bccfc24d0ef9f7142b0278dc73641bc87e4fa..6a544a33bb35db42d330b7f313fb21700d669fbc 100644 (file)
@@ -37,7 +37,7 @@
 #include "firmware.h"
 
 /* This might be wrong, but it works for current fw */
 #include "firmware.h"
 
 /* This might be wrong, but it works for current fw */
-#define MAX_BLOCK_LENGTH       512
+#define MAX_BLOCK_LENGTH       2048
 
 struct firmware* firmware_read_firmware(char *filename, int debug)
 {
 
 struct firmware* firmware_read_firmware(char *filename, int debug)
 {
diff --git a/flash-hmmoduart.c b/flash-hmmoduart.c
new file mode 100644 (file)
index 0000000..9f2f29a
--- /dev/null
@@ -0,0 +1,200 @@
+/* flasher for HM-CFG-USB
+ *
+ * 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
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <string.h>
+#include <strings.h>
+#include <poll.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/time.h>
+#include <libusb-1.0/libusb.h>
+
+#include "hexdump.h"
+#include "firmware.h"
+#include "version.h"
+#include "hmuartlgw.h"
+
+struct recv_data {
+       uint16_t ack;
+};
+
+static int parse_hmuartlgw(enum hmuartlgw_dst dst, uint8_t *buf, int buf_len, void *data)
+{
+       struct recv_data *rdata = data;
+
+       if (buf_len != 2)
+               return 1;
+
+       rdata->ack = (buf[0] << 8) | (buf[1] & 0xff);
+
+       return 1;
+}
+
+void flash_hmmoduart_syntax(char *prog)
+{
+       fprintf(stderr, "Syntax: %s [options] -U /dev/ttyAMA0 filename.eq3\n\n", prog);
+       fprintf(stderr, "Mandatory parameter:\n");
+       fprintf(stderr, "\t-U device\tuse HM-MOD-UART on given device\n");
+       fprintf(stderr, "Possible options:\n");
+       fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n");
+
+}
+
+int main(int argc, char **argv)
+{
+       const char twiddlie[] = { '-', '\\', '|', '/' };
+       struct hmuartlgw_dev *dev;
+       uint8_t *framedata;
+       struct recv_data rdata;
+       uint16_t len;
+       struct firmware *fw;
+       char *uart = NULL;
+       char *filename = NULL;
+       int block;
+       int pfd;
+       int opt;
+       int debug = 0;
+
+       while((opt = getopt(argc, argv, "U:V")) != -1) {
+               switch (opt) {
+                       case 'U':
+                               uart = 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_hmmoduart_syntax(argv[0]);
+                               exit(EXIT_FAILURE);
+                               break;
+               }
+       }
+
+       if (optind == argc - 1) {
+               filename = argv[optind];
+       }
+
+       if (!uart) {
+               flash_hmmoduart_syntax(argv[0]);
+               exit(EXIT_FAILURE);
+       }
+
+       printf("HM-MOD-UART flasher version " VERSION "\n\n");
+
+       if (!filename) {
+               fprintf(stderr, "Missing firmware filename!\n\n");
+               flash_hmmoduart_syntax(argv[0]);
+               exit(EXIT_FAILURE);
+       }
+
+       fw = firmware_read_firmware(filename, debug);
+       if (!fw)
+               exit(EXIT_FAILURE);
+
+       hmuartlgw_set_debug(debug);
+
+       memset(&rdata, 0, sizeof(rdata));
+
+       dev = hmuart_init(uart, parse_hmuartlgw, &rdata, 0);
+       if (!dev) {
+               fprintf(stderr, "Can't initialize HM-MOD-UART\n");
+               exit(EXIT_FAILURE);
+       }
+
+       printf("\nHM-MOD-UART opened.\n\n");
+
+       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 -= 1; /* + frametype, - crc crc */
+
+               framedata = (fw->fw[block]) + 3;
+               framedata[0] = HMUARTLGW_OS_UPDATE_FIRMWARE;
+
+               if (debug)
+                       hexdump(framedata, len, "F> ");
+
+               rdata.ack = 0;
+
+               if (!hmuartlgw_send(dev, framedata, len, HMUARTLGW_OS)) {
+                       perror("\n\nhmuartlgw_send");
+                       exit(EXIT_FAILURE);
+               }
+
+               if (debug)
+                       printf("Waiting for ack...\n");
+               do {
+                       errno = 0;
+                       pfd = hmuartlgw_poll(dev, 1000);
+                       if ((pfd < 0) && errno) {
+                               if (errno != ETIMEDOUT) {
+                                       perror("\n\nhmuartlgw_poll");
+                                       exit(EXIT_FAILURE);
+                               }
+                       }
+                       if (rdata.ack) {
+                               break;
+                       }
+               } while (pfd < 0);
+
+               if (rdata.ack != 0x0401) {
+                       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);
+               }
+       }
+
+       if (rdata.ack == 0x0401) {
+               printf("\n\nFirmware update successfull!\n");
+       }
+
+
+       firmware_free(fw);
+
+       hmuartlgw_close(dev);
+
+       return EXIT_SUCCESS;
+}
index a4a0fef50611c4f79a34159e23108b2990edef63..2a7a815ce59cc78a646575c3fe20ad042e6abc2c 100644 (file)
@@ -705,7 +705,7 @@ int main(int argc, char **argv)
 
                hmuartlgw_set_debug(debug);
 
 
                hmuartlgw_set_debug(debug);
 
-               dev.hmuartlgw = hmuart_init(uart, parse_hmuartlgw, &rdata);
+               dev.hmuartlgw = hmuart_init(uart, parse_hmuartlgw, &rdata, 1);
                if (!dev.hmuartlgw) {
                        fprintf(stderr, "Can't initialize HM-MOD-UART\n");
                        exit(EXIT_FAILURE);
                if (!dev.hmuartlgw) {
                        fprintf(stderr, "Can't initialize HM-MOD-UART\n");
                        exit(EXIT_FAILURE);
index c35b29dd0ef0cc8458d31fff023626aceeeb0c77..01afe05788f00b41597bb0c02d691de687708174 100644 (file)
--- a/hmsniff.c
+++ b/hmsniff.c
@@ -323,7 +323,7 @@ int main(int argc, char **argv)
                        buf[1] = speed;
                        hmcfgusb_send(dev.hmcfgusb, buf, 2, 1);
                } else {
                        buf[1] = speed;
                        hmcfgusb_send(dev.hmcfgusb, buf, 2, 1);
                } else {
-                       dev.hmuartlgw = hmuart_init(uart, parse_hmuartlgw, &rdata);
+                       dev.hmuartlgw = hmuart_init(uart, parse_hmuartlgw, &rdata, 1);
                        if (!dev.hmuartlgw) {
                                fprintf(stderr, "Can't initialize HM-MOD-UART!\n");
                                exit(1);
                        if (!dev.hmuartlgw) {
                                fprintf(stderr, "Can't initialize HM-MOD-UART!\n");
                                exit(1);
index 4c274cf93e85ea558c0995684c29363dbc112cb7..2fa755c03eb9c880c4c7ca5db00080a0b16bfa39 100644 (file)
@@ -142,7 +142,7 @@ static int hmuartlgw_init_parse(enum hmuartlgw_dst dst, uint8_t *buf, int buf_le
         return 1;
 }
 
         return 1;
 }
 
-struct hmuartlgw_dev *hmuart_init(char *device, hmuartlgw_cb_fn cb, void *data)
+struct hmuartlgw_dev *hmuart_init(char *device, hmuartlgw_cb_fn cb, void *data, int app)
 {
        struct hmuartlgw_dev *dev = NULL;
        struct termios oldtio, tio;
 {
        struct hmuartlgw_dev *dev = NULL;
        struct termios oldtio, tio;
@@ -191,7 +191,11 @@ struct hmuartlgw_dev *hmuart_init(char *device, hmuartlgw_cb_fn cb, void *data)
 
        hmuartlgw_flush(dev);
 
 
        hmuartlgw_flush(dev);
 
-       hmuartlgw_enter_app(dev);
+       if (app) {
+               hmuartlgw_enter_app(dev);
+       } else {
+               hmuartlgw_enter_bootloader(dev);
+       }
 
        dev->cb = cb;
        dev->cb_data = data;
 
        dev->cb = cb;
        dev->cb_data = data;
@@ -329,7 +333,7 @@ int hmuartlgw_send_raw(struct hmuartlgw_dev *dev, uint8_t *frame, int framelen)
 int hmuartlgw_send(struct hmuartlgw_dev *dev, uint8_t *cmd, int cmdlen, enum hmuartlgw_dst dst)
 {
        static uint8_t cnt = 0;
 int hmuartlgw_send(struct hmuartlgw_dev *dev, uint8_t *cmd, int cmdlen, enum hmuartlgw_dst dst)
 {
        static uint8_t cnt = 0;
-       uint8_t frame[1024] = { 0 };
+       uint8_t frame[4096] = { 0 };
        uint16_t crc;
 
        frame[0] = 0xfd;
        uint16_t crc;
 
        frame[0] = 0xfd;
index 09b299300f9b239242562ac9833ec83240913d01..9b324287833a52bf71b47596d627c5a1d090a6d7 100644 (file)
@@ -25,6 +25,7 @@
 #define HMUARTLGW_OS_GET_FIRMWARE      0x02
 #define HMUARTLGW_OS_CHANGE_APP                0x03
 #define HMUARTLGW_OS_ACK               0x04
 #define HMUARTLGW_OS_GET_FIRMWARE      0x02
 #define HMUARTLGW_OS_CHANGE_APP                0x03
 #define HMUARTLGW_OS_ACK               0x04
+#define HMUARTLGW_OS_UPDATE_FIRMWARE   0x05
 #define HMUARTLGW_OS_UNSOL_CREDITS     0x05
 #define HMUARTLGW_OS_NORMAL_MODE       0x06
 #define HMUARTLGW_OS_UPDATE_MODE       0x07
 #define HMUARTLGW_OS_UNSOL_CREDITS     0x05
 #define HMUARTLGW_OS_NORMAL_MODE       0x06
 #define HMUARTLGW_OS_UPDATE_MODE       0x07
@@ -65,7 +66,7 @@ struct hmuartlgw_dev {
        int unescape_next;
 };
 
        int unescape_next;
 };
 
-struct hmuartlgw_dev *hmuart_init(char *device, hmuartlgw_cb_fn cb, void *data);
+struct hmuartlgw_dev *hmuart_init(char *device, hmuartlgw_cb_fn cb, void *data, int app);
 struct hmuartlgw_dev *hmlgw_init(char *device, hmuartlgw_cb_fn cb, void *data);
 int hmuartlgw_send_raw(struct hmuartlgw_dev *dev, uint8_t *frame, int framelen);
 int hmuartlgw_send(struct hmuartlgw_dev *dev, uint8_t *cmd, int cmdlen, enum hmuartlgw_dst dst);
 struct hmuartlgw_dev *hmlgw_init(char *device, hmuartlgw_cb_fn cb, void *data);
 int hmuartlgw_send_raw(struct hmuartlgw_dev *dev, uint8_t *frame, int framelen);
 int hmuartlgw_send(struct hmuartlgw_dev *dev, uint8_t *cmd, int cmdlen, enum hmuartlgw_dst dst);
Impressum, Datenschutz