]> git.zerfleddert.de Git - hmcfgusb/blobdiff - flash-ota.c
flash-ota: add support for a-culfw
[hmcfgusb] / flash-ota.c
index 99f94878fc11c172a810a64e984e64ea9b6fb532..a93446fc550cf0043ba756008d5d7738ebf4f96c 100644 (file)
@@ -1,6 +1,6 @@
 /* flasher for HomeMatic-devices supporting OTA updates
  *
- * Copyright (c) 2014 Michael Gernoth <michael@gernoth.net>
+ * Copyright (c) 2014-15 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
@@ -51,8 +51,8 @@ extern char *optarg;
 
 uint32_t hmid = 0;
 uint32_t my_hmid = 0;
-char key[16] = {0};
-uint32_t kNo = 0;
+uint8_t key[16] = {0};
+int32_t kNo = -1;
 
 /* Maximum payloadlen supported by IO */
 uint32_t max_payloadlen = NORMAL_MAX_PAYLOAD;
@@ -177,8 +177,25 @@ static int parse_culfw(uint8_t *buf, int buf_len, void *data)
                                *e = '\0';
                                v = atoi(s);
                                rdata->version |= v;
+
+                               s = e + 1;
+                               e = strchr(s, ' ');
+                               if (!e) {
+                                       break;
+                               }
+                               *e = '\0';
+                               if (!strcmp(s, "a-culfw")) {
+                                       rdata->version = 0xffff;
+                               }
                        }
                        break;
+               case 'E':
+                       {
+                               if (!strncmp((char*)buf, "ERR:CCA", 7)) {
+                                       fprintf(stderr, "CCA didn't complete, too much traffic\n");
+                               }
+                               break;
+                       }
                default:
                        fprintf(stderr, "Unknown response from CUL: %s", buf);
                        return 0;
@@ -270,7 +287,7 @@ int send_hm_message(struct ota_dev *dev, struct recv_data *rdata, uint8_t *msg)
                                }
 
                                if (msg[CTL] & 0x20) {
-                                       int cnt = 3;
+                                       int cnt = 5;
                                        int pfd;
                                        do {
                                                errno = 0;
@@ -282,7 +299,47 @@ int send_hm_message(struct ota_dev *dev, struct recv_data *rdata, uint8_t *msg)
                                                        }
                                                }
                                                if (rdata->message_type == MESSAGE_TYPE_E) {
-                                                       break;
+                                                       if (rdata->message[TYPE] == 0x02) {
+                                                               if (rdata->message[PAYLOAD] == 0x04) {
+                                                                       int32_t req_kNo;
+                                                                       uint8_t challenge[6];
+                                                                       uint8_t respbuf[16];
+                                                                       uint8_t *resp;
+
+                                                                       req_kNo = rdata->message[rdata->message[LEN]] / 2;
+                                                                       memcpy(challenge, &(rdata->message[PAYLOAD+1]), 6);
+
+                                                                       if (req_kNo != kNo) {
+                                                                               fprintf(stderr, "AES request for unknown key %d!\n", req_kNo);
+                                                                       } else {
+                                                                               resp = hm_sign(key, challenge, msg, NULL, respbuf);
+                                                                               if (resp) {
+                                                                                       uint8_t rbuf[64];
+
+                                                                                       memset(rbuf, 0, sizeof(rbuf));
+                                                                                       rbuf[MSGID] = rdata->message[MSGID];
+                                                                                       rbuf[CTL] = rdata->message[CTL];
+                                                                                       rbuf[TYPE] = 0x03;
+                                                                                       SET_SRC(rbuf, DST(rdata->message));
+                                                                                       SET_DST(rbuf, SRC(rdata->message));
+                                                                                       memcpy(&(rbuf[PAYLOAD]), resp, 16);
+                                                                                       SET_LEN_FROM_PAYLOADLEN(rbuf, 16);
+
+                                                                                       return send_hm_message(dev, rdata, rbuf);
+                                                                               }
+                                                                       }
+                                                               } else if (rdata->message[PAYLOAD] >= 0x80 && rdata->message[PAYLOAD] <= 0x8f) {
+                                                                       fprintf(stderr, "NACK\n");
+                                                               } else {        /* ACK or ACKinfo */
+                                                                       break;
+                                                               }
+                                                       } else {
+                                                               fprintf(stderr, "Unexpected message received: ");
+                                                               for (i = 0; i < rdata->message[LEN]; i++) {
+                                                                       fprintf(stderr, "%02x", rdata->message[i+1]);
+                                                               }
+                                                               fprintf(stderr, "\n");
+                                                       }
                                                }
                                        } while(cnt--);
 
@@ -344,7 +401,7 @@ void flash_ota_syntax(char *prog)
        fprintf(stderr, "Syntax: %s parameters options\n\n", prog);
        fprintf(stderr, "Mandatory parameters:\n");
        fprintf(stderr, "\t-f firmware.eq3\tfirmware file to flash\n");
-       fprintf(stderr, "\t-s SERIAL\tserial of device to flash\n");
+       fprintf(stderr, "\t-s SERIAL\tserial of device to flash (optional when using -D)\n");
        fprintf(stderr, "\nOptional parameters:\n");
        fprintf(stderr, "\t-c device\tenable CUL-mode with CUL at path \"device\"\n");
        fprintf(stderr, "\t-b bps\t\tuse CUL with speed \"bps\" (default: %u)\n", DEFAULT_CUL_BPS);
@@ -354,7 +411,6 @@ void flash_ota_syntax(char *prog)
        fprintf(stderr, "\t-C\t\tHMID of central (3 hex-bytes, no prefix, e.g. ABCDEF)\n");
        fprintf(stderr, "\t-D\t\tHMID of device (3 hex-bytes, no prefix, e.g. 123456)\n");
        fprintf(stderr, "\t-K\t\tKNO:KEY AES key-number and key (hex) separated by colon (Fhem hmKey attribute)\n");
-       fprintf(stderr, "\t\t\tAES is currently not supported when using a culfw-device!\n");
 }
 
 int main(int argc, char **argv)
@@ -449,7 +505,7 @@ int main(int argc, char **argv)
                }
        }
 
-       if (!fw_file || !serial) {
+       if (!fw_file || (!serial && !hmid)) {
                flash_ota_syntax(argv[0]);
                exit(EXIT_FAILURE);
        }
@@ -462,12 +518,6 @@ int main(int argc, char **argv)
        memset(&dev, 0, sizeof(struct ota_dev));
 
        if (culfw_dev) {
-               if (kNo) {
-                       fprintf(stderr, "\nAES currently not supported with culfw-device!\n");
-                       flash_ota_syntax(argv[0]);
-                       exit(EXIT_FAILURE);
-               }
-
                printf("Opening culfw-device at path %s with speed %u\n", culfw_dev, bps);
                dev.culfw = culfw_init(culfw_dev, bps, parse_culfw, &rdata);
                if (!dev.culfw) {
@@ -495,9 +545,14 @@ int main(int argc, char **argv)
                                break;
                }
 
-               printf("culfw-device firmware version: %u.%02u\n", 
-                       (rdata.version >> 8) & 0xff,
-                       rdata.version & 0xff);
+               printf("culfw-device firmware version: ");
+               if (rdata.version != 0xffff) {
+                       printf("%u.%02u\n",
+                               (rdata.version >> 8) & 0xff,
+                               rdata.version & 0xff);
+               } else {
+                       printf("a-culfw\n");
+               }
 
                if (rdata.version < 0x013a) {
                        fprintf(stderr, "\nThis version does _not_ support firmware upgrade mode, you need at least 1.58!\n");
@@ -586,7 +641,7 @@ int main(int argc, char **argv)
                        my_hmid = new_hmid;
                }
 
-               if (kNo) {
+               if (kNo > 0) {
                        printf("Setting AES-key\n");
 
                        memset(out, 0, sizeof(out));
@@ -620,7 +675,6 @@ int main(int argc, char **argv)
 
        if (hmid && my_hmid) {
                printf("Sending device with hmid %06x to bootloader\n", hmid);
-               out[MSGID] = msgid++;
                out[CTL] = 0x30;
                out[TYPE] = 0x11;
                SET_SRC(out, my_hmid);
@@ -630,6 +684,7 @@ int main(int argc, char **argv)
 
                cnt = 3;
                do {
+                       out[MSGID] = msgid++;
                        if (send_hm_message(&dev, &rdata, out)) {
                                break;
                        }
@@ -639,7 +694,11 @@ int main(int argc, char **argv)
                }
        }
 
-       printf("Waiting for device with serial %s\n", serial);
+       if (serial) {
+               printf("Waiting for device with serial %s\n", serial);
+       } else {
+               printf("Waiting for device with HMID %06x\n", hmid);
+       }
 
        while (1) {
                errno = 0;
@@ -666,14 +725,17 @@ int main(int argc, char **argv)
                    (rdata.message[TYPE] == 0x10) && /* Messagte type: Information */
                    (DST(rdata.message) == 0x000000) && /* Broadcast */
                    (rdata.message[PAYLOAD] == 0x00)) { /* FUP? */
-                       if (!strncmp((char*)&(rdata.message[0x0b]), serial, 10)) {
+                       if (serial && !strncmp((char*)&(rdata.message[0x0b]), serial, 10)) {
                                hmid = SRC(rdata.message);
                                break;
+                       } else if (!serial && SRC(rdata.message) == hmid) {
+                               serial = (char*)&(rdata.message[0x0b]);
+                               break;
                        }
                }
        }
 
-       printf("Device with serial %s (hmid: %06x) entered firmware-update-mode\n", serial, hmid);
+       printf("Device with serial %s (HMID: %06x) entered firmware-update-mode\n", serial, hmid);
 
        if (dev.type == DEVICE_TYPE_HMCFGUSB) {
                printf("Adding HMID\n");
Impressum, Datenschutz