1 /* flasher for HomeMatic-devices supporting OTA updates
3 * Copyright (c) 2014-16 Michael Gernoth <michael@gernoth.net>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 #include <sys/types.h>
36 #include <libusb-1.0/libusb.h>
44 #include "hmuartlgw.h"
48 #define NORMAL_MAX_PAYLOAD 37
49 #define LOWER_MAX_PAYLOAD 17
55 uint8_t key
[16] = {0};
58 /* Maximum payloadlen supported by IO */
59 uint32_t max_payloadlen
= NORMAL_MAX_PAYLOAD
;
66 enum hmuartlgw_state
{
67 HMUARTLGW_STATE_GET_HMID
,
68 HMUARTLGW_STATE_GET_FIRMWARE
,
69 HMUARTLGW_STATE_GET_CREDITS
,
71 HMUARTLGW_STATE_WAIT_APP
,
72 HMUARTLGW_STATE_ACK_APP
,
77 enum message_type message_type
;
82 enum hmuartlgw_state uartlgw_state
;
83 uint8_t uartlgw_version
[3];
86 static int parse_hmcfgusb(uint8_t *buf
, int buf_len
, void *data
)
88 struct recv_data
*rdata
= data
;
96 ((buf
[0x11] == ((hmid
>> 16) & 0xff)) &&
97 (buf
[0x12] == ((hmid
>> 8) & 0xff)) &&
98 (buf
[0x13] == (hmid
& 0xff)))) {
99 memset(rdata
->message
, 0, sizeof(rdata
->message
));
100 memcpy(rdata
->message
, buf
+ 0x0d, buf
[0x0d] + 1);
101 rdata
->message_type
= MESSAGE_TYPE_E
;
105 memset(rdata
->message
, 0, sizeof(rdata
->message
));
106 memcpy(rdata
->message
, buf
+ 0x0e, buf
[0x0e] + 1);
107 rdata
->status
= (buf
[5] << 8) | buf
[6];
108 rdata
->message_type
= MESSAGE_TYPE_R
;
111 rdata
->speed
= buf
[1];
114 rdata
->version
= (buf
[11] << 8) | buf
[12];
115 rdata
->credits
= buf
[36];
116 my_hmid
= (buf
[0x1b] << 16) | (buf
[0x1c] << 8) | buf
[0x1d];
128 static int parse_culfw(uint8_t *buf
, int buf_len
, void *data
)
130 struct recv_data
*rdata
= data
;
133 memset(rdata
, 0, sizeof(struct recv_data
));
143 while(validate_nibble(buf
[(pos
* 2) + 1]) &&
144 validate_nibble(buf
[(pos
* 2) + 2]) &&
145 (pos
+ 1 < buf_len
)) {
146 rdata
->message
[pos
] = ascii_to_nibble(buf
[(pos
* 2) + 1]) << 4;
147 rdata
->message
[pos
] |= ascii_to_nibble(buf
[(pos
* 2) + 2]);
151 if (hmid
&& (SRC(rdata
->message
) != hmid
))
154 rdata
->message_type
= MESSAGE_TYPE_E
;
162 s
= ((char*)buf
) + 2;
165 fprintf(stderr
, "Unknown response from CUL: %s", buf
);
170 rdata
->version
= v
<< 8;
175 fprintf(stderr
, "Unknown response from CUL: %s", buf
);
188 if (!strcmp(s
, "a-culfw")) {
189 rdata
->version
= 0xffff;
195 if (!strncmp((char*)buf
, "ERR:CCA", 7)) {
196 fprintf(stderr
, "CCA didn't complete, too much traffic\n");
201 fprintf(stderr
, "Unknown response from CUL: %s", buf
);
209 static int parse_hmuartlgw(enum hmuartlgw_dst dst
, uint8_t *buf
, int buf_len
, void *data
)
211 struct recv_data
*rdata
= data
;
213 if (dst
== HMUARTLGW_OS
) {
214 switch (rdata
->uartlgw_state
) {
215 case HMUARTLGW_STATE_GET_FIRMWARE
:
216 if (buf
[0] == HMUARTLGW_OS_ACK
) {
217 rdata
->uartlgw_version
[0] = buf
[5];
218 rdata
->uartlgw_version
[1] = buf
[6];
219 rdata
->uartlgw_version
[2] = buf
[7];
220 rdata
->uartlgw_state
= HMUARTLGW_STATE_DONE
;
223 case HMUARTLGW_STATE_GET_CREDITS
:
224 if (buf
[0] == HMUARTLGW_OS_ACK
) {
225 rdata
->credits
= buf
[2] / 2;
226 rdata
->uartlgw_state
= HMUARTLGW_STATE_DONE
;
236 case HMUARTLGW_APP_ACK
:
237 if (rdata
->uartlgw_state
== HMUARTLGW_STATE_GET_HMID
) {
238 my_hmid
= (buf
[4] << 16) | (buf
[5] << 8) | buf
[6];
241 rdata
->status
= buf
[1];
242 rdata
->message_type
= MESSAGE_TYPE_R
;
243 rdata
->uartlgw_state
= HMUARTLGW_STATE_ACK_APP
;
245 hexdump(buf
, buf_len
, "ACK Status: ");
249 case HMUARTLGW_APP_RECV
:
251 ((buf
[7] == ((hmid
>> 16) & 0xff)) &&
252 (buf
[8] == ((hmid
>> 8) & 0xff)) &&
253 (buf
[9] == (hmid
& 0xff)))) {
254 memset(rdata
->message
, 0, sizeof(rdata
->message
));
255 memcpy(rdata
->message
+ 1, buf
+ 4, buf_len
- 4);
256 rdata
->message
[LEN
] = buf_len
- 4;
257 rdata
->message_type
= MESSAGE_TYPE_E
;
267 int send_hm_message(struct hm_dev
*dev
, struct recv_data
*rdata
, uint8_t *msg
)
269 static uint32_t id
= 1;
275 case DEVICE_TYPE_HMCFGUSB
:
276 if (gettimeofday(&tv
, NULL
) == -1) {
277 perror("gettimeofay");
281 memset(out
, 0, sizeof(out
));
284 out
[1] = (id
>> 24) & 0xff;
285 out
[2] = (id
>> 16) & 0xff;
286 out
[3] = (id
>> 8) & 0xff;
289 out
[11] = (tv
.tv_usec
>> 24) & 0xff;
290 out
[12] = (tv
.tv_usec
>> 16) & 0xff;
291 out
[13] = (tv
.tv_usec
>> 8) & 0xff;
292 out
[14] = tv
.tv_usec
& 0xff;
294 memcpy(&out
[0x0f], msg
, msg
[0] + 1);
296 memset(rdata
, 0, sizeof(struct recv_data
));
297 hmcfgusb_send(dev
->hmcfgusb
, out
, sizeof(out
), 1);
300 if (rdata
->message_type
== MESSAGE_TYPE_R
) {
301 if (((rdata
->status
& 0xdf) == 0x01) ||
302 ((rdata
->status
& 0xdf) == 0x02)) {
305 if ((rdata
->status
& 0xff00) == 0x0400) {
306 fprintf(stderr
, "\nOut of credits!\n");
307 } else if ((rdata
->status
& 0xff) == 0x08) {
308 fprintf(stderr
, "\nMissing ACK!\n");
309 } else if ((rdata
->status
& 0xff) == 0x30) {
310 fprintf(stderr
, "\nUnknown AES-key requested!\n");
312 fprintf(stderr
, "\nInvalid status: %04x\n", rdata
->status
);
318 pfd
= hmcfgusb_poll(dev
->hmcfgusb
, 1000);
319 if ((pfd
< 0) && errno
) {
320 if (errno
!= ETIMEDOUT
) {
321 perror("\n\nhmcfgusb_poll");
327 case DEVICE_TYPE_CULFW
:
332 memset(buf
, 0, sizeof(buf
));
335 for (i
= 0; i
< msg
[0] + 1; i
++) {
336 buf
[2 + (i
* 2)] = nibble_to_ascii((msg
[i
] >> 4) & 0xf);
337 buf
[2 + (i
* 2) + 1] = nibble_to_ascii(msg
[i
] & 0xf);
339 buf
[2 + (i
* 2) ] = '\r';
340 buf
[2 + (i
* 2) + 1] = '\n';
342 memset(rdata
, 0, sizeof(struct recv_data
));
343 if (culfw_send(dev
->culfw
, buf
, 2 + (i
* 2) + 1) == 0) {
344 fprintf(stderr
, "culfw_send failed!\n");
348 if (msg
[CTL
] & 0x20) {
353 pfd
= culfw_poll(dev
->culfw
, 200);
354 if ((pfd
< 0) && errno
) {
355 if (errno
!= ETIMEDOUT
) {
356 perror("\n\nculfw_poll");
360 if (rdata
->message_type
== MESSAGE_TYPE_E
) {
361 if (rdata
->message
[TYPE
] == 0x02) {
362 if (rdata
->message
[PAYLOAD
] == 0x04) {
364 uint8_t challenge
[6];
368 req_kNo
= rdata
->message
[rdata
->message
[LEN
]] / 2;
369 memcpy(challenge
, &(rdata
->message
[PAYLOAD
+1]), 6);
371 if (req_kNo
!= kNo
) {
372 fprintf(stderr
, "AES request for unknown key %d!\n", req_kNo
);
374 resp
= hm_sign(key
, challenge
, msg
, NULL
, respbuf
);
378 memset(rbuf
, 0, sizeof(rbuf
));
379 rbuf
[MSGID
] = rdata
->message
[MSGID
];
380 rbuf
[CTL
] = rdata
->message
[CTL
];
382 SET_SRC(rbuf
, DST(rdata
->message
));
383 SET_DST(rbuf
, SRC(rdata
->message
));
384 memcpy(&(rbuf
[PAYLOAD
]), resp
, 16);
385 SET_LEN_FROM_PAYLOADLEN(rbuf
, 16);
387 usleep(110000); /* Determined by a fair dice roll */
388 return send_hm_message(dev
, rdata
, rbuf
);
391 } else if (rdata
->message
[PAYLOAD
] >= 0x80 && rdata
->message
[PAYLOAD
] <= 0x8f) {
392 fprintf(stderr
, "NACK\n");
393 } else { /* ACK or ACKinfo */
397 fprintf(stderr
, "Unexpected message received: ");
398 for (i
= 0; i
< rdata
->message
[LEN
]; i
++) {
399 fprintf(stderr
, "%02x", rdata
->message
[i
+1]);
401 fprintf(stderr
, "\n");
407 fprintf(stderr
, "\nMissing ACK!\n");
413 case DEVICE_TYPE_HMUARTLGW
:
414 memset(out
, 0, sizeof(out
));
416 out
[0] = HMUARTLGW_APP_SEND
;
419 out
[3] = (msg
[CTL
] & 0x10) ? 0x01 : 0x00; /* Burst?! */
420 memcpy(&out
[4], &msg
[1], msg
[0]);
422 memset(rdata
, 0, sizeof(struct recv_data
));
423 hmuartlgw_send(dev
->hmuartlgw
, out
, msg
[0] + 4, HMUARTLGW_APP
);
426 if (rdata
->message_type
== MESSAGE_TYPE_R
) {
427 if ((rdata
->status
== 0x02) ||
428 (rdata
->status
== 0x03) ||
429 (rdata
->status
== 0x0c)) {
432 if (rdata
->status
== 0x0d) {
433 fprintf(stderr
, "\nAES handshake failed!\n");
434 } else if (rdata
->status
== 0x04 || rdata
->status
== 0x06) {
435 fprintf(stderr
, "\nMissing ACK!\n");
437 fprintf(stderr
, "\nInvalid status: %04x\n", rdata
->status
);
443 pfd
= hmuartlgw_poll(dev
->hmuartlgw
, 1000);
444 if ((pfd
< 0) && errno
) {
445 if (errno
!= ETIMEDOUT
) {
446 perror("\n\nhmcfgusb_poll");
458 static int switch_speed(struct hm_dev
*dev
, struct recv_data
*rdata
, uint8_t speed
)
463 printf("Entering %uk-mode\n", speed
);
466 case DEVICE_TYPE_HMCFGUSB
:
467 memset(out
, 0, sizeof(out
));
471 hmcfgusb_send(dev
->hmcfgusb
, out
, sizeof(out
), 1);
475 pfd
= hmcfgusb_poll(dev
->hmcfgusb
, 1000);
476 if ((pfd
< 0) && errno
) {
477 if (errno
!= ETIMEDOUT
) {
478 perror("\n\nhmcfgusb_poll");
482 if (rdata
->speed
== speed
)
486 case DEVICE_TYPE_CULFW
:
488 return culfw_send(dev
->culfw
, "AR\r\n", 4);
490 return culfw_send(dev
->culfw
, "Ar\r\n", 4);
493 case DEVICE_TYPE_HMUARTLGW
:
495 out
[0] = HMUARTLGW_OS_UPDATE_MODE
;
498 hmuartlgw_send(dev
->hmuartlgw
, out
, 3, HMUARTLGW_OS
);
500 out
[0] = HMUARTLGW_OS_NORMAL_MODE
;
501 hmuartlgw_send(dev
->hmuartlgw
, out
, 1, HMUARTLGW_OS
);
509 void flash_ota_syntax(char *prog
)
511 fprintf(stderr
, "Syntax: %s parameters options\n\n", prog
);
512 fprintf(stderr
, "Mandatory parameters:\n");
513 fprintf(stderr
, "\t-f firmware.eq3\tfirmware file to flash\n");
514 fprintf(stderr
, "\t-s SERIAL\tserial of device to flash (optional when using -D)\n");
515 fprintf(stderr
, "\nOptional parameters:\n");
516 fprintf(stderr
, "\t-c device\tenable CUL-mode with CUL at path \"device\"\n");
517 fprintf(stderr
, "\t-b bps\t\tuse CUL with speed \"bps\" (default: %u)\n", DEFAULT_CUL_BPS
);
518 fprintf(stderr
, "\t-l\t\tlower payloadlen (required for devices with little RAM, e.g. CUL v2 and CUL v4)\n");
519 fprintf(stderr
, "\t-S serial\tuse HM-CFG-USB with given serial\n");
520 fprintf(stderr
, "\t-U device\tuse HM-MOD-UART on given device\n");
521 fprintf(stderr
, "\t-h\t\tthis help\n");
522 fprintf(stderr
, "\nOptional parameters for automatically sending device to bootloader\n");
523 fprintf(stderr
, "\t-C\t\tHMID of central (3 hex-bytes, no prefix, e.g. ABCDEF)\n");
524 fprintf(stderr
, "\t-D\t\tHMID of device (3 hex-bytes, no prefix, e.g. 123456)\n");
525 fprintf(stderr
, "\t-K\t\tKNO:KEY AES key-number and key (hex) separated by colon (Fhem hmKey attribute)\n");
528 int main(int argc
, char **argv
)
530 const char twiddlie
[] = { '-', '\\', '|', '/' };
531 const uint8_t cc1101_regs
[] = { 0x10, 0x5B, 0x11, 0xF8, 0x15, 0x47 };
532 char *fw_file
= NULL
;
534 char *culfw_dev
= NULL
;
536 unsigned int bps
= DEFAULT_CUL_BPS
;
538 struct recv_data rdata
;
544 char *hmcfgusb_serial
= NULL
;
555 printf("HomeMatic OTA flasher version " VERSION
"\n\n");
557 while((opt
= getopt(argc
, argv
, "b:c:f:hls:C:D:K:S:U:")) != -1) {
569 printf("Reducing payload-len from %d to %d\n", max_payloadlen
, LOWER_MAX_PAYLOAD
);
570 max_payloadlen
= LOWER_MAX_PAYLOAD
;
576 my_hmid
= strtoul(optarg
, &endptr
, 16);
577 if (*endptr
!= '\0') {
578 fprintf(stderr
, "Invalid central HMID!\n\n");
579 flash_ota_syntax(argv
[0]);
584 hmid
= strtoul(optarg
, &endptr
, 16);
585 if (*endptr
!= '\0') {
586 fprintf(stderr
, "Invalid device HMID!\n\n");
587 flash_ota_syntax(argv
[0]);
592 kNo
= strtoul(optarg
, &endptr
, 10);
593 if (*endptr
!= ':') {
594 fprintf(stderr
, "Invalid key number!\n\n");
595 flash_ota_syntax(argv
[0]);
599 for (cnt
= 0; cnt
< 16; cnt
++) {
600 if (*endptr
== '\0' || *(endptr
+1) == '\0' ||
601 !validate_nibble(*endptr
) ||
602 !validate_nibble(*(endptr
+1))) {
603 fprintf(stderr
, "Invalid key!\n\n");
604 flash_ota_syntax(argv
[0]);
607 key
[cnt
] = ascii_to_nibble(*endptr
) << 4 | ascii_to_nibble(*(endptr
+1));
612 hmcfgusb_serial
= optarg
;
621 flash_ota_syntax(argv
[0]);
628 if (!fw_file
|| (!serial
&& !hmid
)) {
629 flash_ota_syntax(argv
[0]);
633 fw
= firmware_read_firmware(fw_file
, debug
);
637 memset(&rdata
, 0, sizeof(rdata
));
638 memset(&dev
, 0, sizeof(struct hm_dev
));
641 printf("Opening culfw-device at path %s with speed %u\n", culfw_dev
, bps
);
642 dev
.culfw
= culfw_init(culfw_dev
, bps
, parse_culfw
, &rdata
);
644 fprintf(stderr
, "Can't initialize CUL at %s with rate %u\n", culfw_dev
, bps
);
647 dev
.type
= DEVICE_TYPE_CULFW
;
649 printf("Requesting firmware version\n");
650 culfw_send(dev
.culfw
, "\r\n", 2);
651 culfw_flush(dev
.culfw
);
654 culfw_send(dev
.culfw
, "V\r\n", 3);
657 pfd
= culfw_poll(dev
.culfw
, 1000);
658 if ((pfd
< 0) && errno
) {
659 if (errno
!= ETIMEDOUT
) {
660 perror("\n\nhmcfgusb_poll");
668 printf("culfw-device firmware version: ");
669 if (rdata
.version
!= 0xffff) {
671 (rdata
.version
>> 8) & 0xff,
672 rdata
.version
& 0xff);
677 if (rdata
.version
< 0x013a) {
678 fprintf(stderr
, "\nThis version does _not_ support firmware upgrade mode, you need at least 1.58!\n");
682 uint32_t new_hmid
= my_hmid
;
684 hmuartlgw_set_debug(debug
);
686 dev
.hmuartlgw
= hmuart_init(uart
, parse_hmuartlgw
, &rdata
);
687 if (!dev
.hmuartlgw
) {
688 fprintf(stderr
, "Can't initialize HM-MOD-UART\n");
691 dev
.type
= DEVICE_TYPE_HMUARTLGW
;
693 out
[0] = HMUARTLGW_APP_GET_HMID
;
695 rdata
.uartlgw_state
= HMUARTLGW_STATE_GET_HMID
;
696 hmuartlgw_send(dev
.hmuartlgw
, out
, 1, HMUARTLGW_APP
);
697 do { hmuartlgw_poll(dev
.hmuartlgw
, 500); } while (rdata
.uartlgw_state
!= HMUARTLGW_STATE_ACK_APP
);
698 } while (rdata
.status
== 0x08);
700 out
[0] = HMUARTLGW_OS_GET_FIRMWARE
;
702 rdata
.uartlgw_state
= HMUARTLGW_STATE_GET_FIRMWARE
;
703 hmuartlgw_send(dev
.hmuartlgw
, out
, 1, HMUARTLGW_OS
);
704 do { hmuartlgw_poll(dev
.hmuartlgw
, 500); } while (rdata
.uartlgw_state
!= HMUARTLGW_STATE_DONE
);
705 } while (rdata
.status
== 0x08);
707 out
[0] = HMUARTLGW_OS_GET_CREDITS
;
709 rdata
.uartlgw_state
= HMUARTLGW_STATE_GET_CREDITS
;
710 hmuartlgw_send(dev
.hmuartlgw
, out
, 1, HMUARTLGW_OS
);
711 do { hmuartlgw_poll(dev
.hmuartlgw
, 500); } while (rdata
.uartlgw_state
!= HMUARTLGW_STATE_DONE
);
712 } while (rdata
.status
== 0x08);
714 printf("HM-MOD-UART firmware version: %u.%u.%u, used credits: %u%%\n",
715 rdata
.uartlgw_version
[0],
716 rdata
.uartlgw_version
[1],
717 rdata
.uartlgw_version
[2],
720 if (rdata
.credits
>= 40) {
721 printf("\nRebooting HM-MOD-UART to avoid running out of credits\n");
723 hmuartlgw_enter_bootloader(dev
.hmuartlgw
);
724 hmuartlgw_enter_app(dev
.hmuartlgw
);
727 printf("\nHM-MOD-UART opened\n\n");
729 if (new_hmid
&& (my_hmid
!= new_hmid
)) {
730 printf("Changing hmid from %06x to %06x\n", my_hmid
, new_hmid
);
732 out
[0] = HMUARTLGW_APP_SET_HMID
;
733 out
[1] = (new_hmid
>> 16) & 0xff;
734 out
[2] = (new_hmid
>> 8) & 0xff;
735 out
[3] = new_hmid
& 0xff;
737 rdata
.uartlgw_state
= HMUARTLGW_STATE_WAIT_APP
;
738 hmuartlgw_send(dev
.hmuartlgw
, out
, 4, HMUARTLGW_APP
);
739 do { hmuartlgw_poll(dev
.hmuartlgw
, 500); } while (rdata
.uartlgw_state
!= HMUARTLGW_STATE_ACK_APP
);
740 } while (rdata
.status
== 0x08);
746 printf("Setting AES-key\n");
748 memset(out
, 0, sizeof(out
));
749 out
[0] = HMUARTLGW_APP_SET_CURRENT_KEY
;
750 memcpy(&(out
[1]), key
, 16);
754 rdata
.uartlgw_state
= HMUARTLGW_STATE_WAIT_APP
;
755 hmuartlgw_send(dev
.hmuartlgw
, out
, 18, HMUARTLGW_APP
);
756 do { hmuartlgw_poll(dev
.hmuartlgw
, 500); } while (rdata
.uartlgw_state
!= HMUARTLGW_STATE_ACK_APP
);
757 } while (rdata
.status
== 0x08);
759 memset(out
, 0, sizeof(out
));
760 out
[0] = HMUARTLGW_APP_SET_OLD_KEY
;
761 memcpy(&(out
[1]), key
, 16);
765 rdata
.uartlgw_state
= HMUARTLGW_STATE_WAIT_APP
;
766 hmuartlgw_send(dev
.hmuartlgw
, out
, 18, HMUARTLGW_APP
);
767 do { hmuartlgw_poll(dev
.hmuartlgw
, 500); } while (rdata
.uartlgw_state
!= HMUARTLGW_STATE_ACK_APP
);
768 } while (rdata
.status
== 0x08);
771 uint32_t new_hmid
= my_hmid
;
773 hmcfgusb_set_debug(debug
);
775 dev
.hmcfgusb
= hmcfgusb_init(parse_hmcfgusb
, &rdata
, hmcfgusb_serial
);
777 fprintf(stderr
, "Can't initialize HM-CFG-USB\n");
780 dev
.type
= DEVICE_TYPE_HMCFGUSB
;
782 memset(out
, 0, sizeof(out
));
784 hmcfgusb_send(dev
.hmcfgusb
, out
, sizeof(out
), 1);
788 pfd
= hmcfgusb_poll(dev
.hmcfgusb
, 1000);
789 if ((pfd
< 0) && errno
) {
790 if (errno
!= ETIMEDOUT
) {
791 perror("\n\nhmcfgusb_poll");
799 if (rdata
.version
< 0x3c7) {
800 fprintf(stderr
, "HM-CFG-USB firmware too low: %u < 967\n", rdata
.version
);
804 printf("HM-CFG-USB firmware version: %u, used credits: %u%%\n", rdata
.version
, rdata
.credits
);
806 if (rdata
.credits
>= 40) {
807 printf("\nRebooting HM-CFG-USB to avoid running out of credits\n\n");
809 if (!dev
.hmcfgusb
->bootloader
) {
810 printf("HM-CFG-USB not in bootloader mode, entering bootloader.\n");
811 printf("Waiting for device to reappear...\n");
815 if (!dev
.hmcfgusb
->bootloader
)
816 hmcfgusb_enter_bootloader(dev
.hmcfgusb
);
817 hmcfgusb_close(dev
.hmcfgusb
);
820 } while (((dev
.hmcfgusb
= hmcfgusb_init(parse_hmcfgusb
, &rdata
, hmcfgusb_serial
)) == NULL
) || (!dev
.hmcfgusb
->bootloader
));
823 if (dev
.hmcfgusb
->bootloader
) {
824 printf("HM-CFG-USB in bootloader mode, rebooting\n");
828 if (dev
.hmcfgusb
->bootloader
)
829 hmcfgusb_leave_bootloader(dev
.hmcfgusb
);
830 hmcfgusb_close(dev
.hmcfgusb
);
833 } while (((dev
.hmcfgusb
= hmcfgusb_init(parse_hmcfgusb
, &rdata
, hmcfgusb_serial
)) == NULL
) || (dev
.hmcfgusb
->bootloader
));
837 printf("\n\nHM-CFG-USB opened\n\n");
839 if (new_hmid
&& (my_hmid
!= new_hmid
)) {
840 printf("Changing hmid from %06x to %06x\n", my_hmid
, new_hmid
);
842 memset(out
, 0, sizeof(out
));
844 out
[1] = (new_hmid
>> 16) & 0xff;
845 out
[2] = (new_hmid
>> 8) & 0xff;
846 out
[3] = new_hmid
& 0xff;
848 hmcfgusb_send(dev
.hmcfgusb
, out
, sizeof(out
), 1);
854 printf("Setting AES-key\n");
856 memset(out
, 0, sizeof(out
));
860 out
[3] = sizeof(key
);
861 memcpy(&(out
[4]), key
, sizeof(key
));
862 hmcfgusb_send(dev
.hmcfgusb
, out
, sizeof(out
), 1);
864 memset(out
, 0, sizeof(out
));
869 hmcfgusb_send(dev
.hmcfgusb
, out
, sizeof(out
), 1);
871 memset(out
, 0, sizeof(out
));
876 hmcfgusb_send(dev
.hmcfgusb
, out
, sizeof(out
), 1);
880 if (!switch_speed(&dev
, &rdata
, 10)) {
881 fprintf(stderr
, "Can't switch speed!\n");
885 if (hmid
&& my_hmid
) {
887 case DEVICE_TYPE_HMCFGUSB
:
888 printf("Adding HMID\n");
890 memset(out
, 0, sizeof(out
));
892 out
[1] = (hmid
>> 16) & 0xff;
893 out
[2] = (hmid
>> 8) & 0xff;
894 out
[3] = hmid
& 0xff;
896 hmcfgusb_send(dev
.hmcfgusb
, out
, sizeof(out
), 1);
898 case DEVICE_TYPE_HMUARTLGW
:
899 printf("Adding HMID\n");
901 memset(out
, 0, sizeof(out
));
902 out
[0] = HMUARTLGW_APP_ADD_PEER
;
903 out
[1] = (hmid
>> 16) & 0xff;
904 out
[2] = (hmid
>> 8) & 0xff;
905 out
[3] = hmid
& 0xff;
906 out
[4] = (kNo
> 0) ? kNo
: 0x00; /* KeyIndex */
907 out
[5] = 0x00; /* WakeUp? */
908 out
[6] = 0x00; /* WakeUp? */
911 rdata
.uartlgw_state
= HMUARTLGW_STATE_WAIT_APP
;
912 hmuartlgw_send(dev
.hmuartlgw
, out
, 7, HMUARTLGW_APP
);
913 do { hmuartlgw_poll(dev
.hmuartlgw
, 500); } while (rdata
.uartlgw_state
!= HMUARTLGW_STATE_ACK_APP
);
914 } while (rdata
.status
== 0x08);
918 printf("Sending device with hmid %06x to bootloader\n", hmid
);
921 SET_SRC(out
, my_hmid
);
924 SET_LEN_FROM_PAYLOADLEN(out
, 1);
928 out
[MSGID
] = msgid
++;
929 if (send_hm_message(&dev
, &rdata
, out
)) {
934 printf("Failed to send device to bootloader, please enter bootloader manually.\n");
939 printf("Waiting for device with serial %s\n", serial
);
941 printf("Waiting for device with HMID %06x\n", hmid
);
947 case DEVICE_TYPE_CULFW
:
948 pfd
= culfw_poll(dev
.culfw
, 1000);
950 case DEVICE_TYPE_HMCFGUSB
:
951 pfd
= hmcfgusb_poll(dev
.hmcfgusb
, 1000);
953 case DEVICE_TYPE_HMUARTLGW
:
954 pfd
= hmuartlgw_poll(dev
.hmuartlgw
, 1000);
961 if ((pfd
< 0) && errno
) {
962 if (errno
!= ETIMEDOUT
) {
968 if ((rdata
.message
[LEN
] == 0x14) && /* Length */
969 (rdata
.message
[MSGID
] == 0x00) && /* Message ID */
970 (rdata
.message
[CTL
] == 0x00) && /* Control Byte */
971 (rdata
.message
[TYPE
] == 0x10) && /* Messagte type: Information */
972 (DST(rdata
.message
) == 0x000000) && /* Broadcast */
973 (rdata
.message
[PAYLOAD
] == 0x00)) { /* FUP? */
974 if (serial
&& !strncmp((char*)&(rdata
.message
[0x0b]), serial
, 10)) {
975 hmid
= SRC(rdata
.message
);
977 } else if (!serial
&& SRC(rdata
.message
) == hmid
) {
978 serial
= (char*)&(rdata
.message
[0x0b]);
984 printf("Device with serial %s (HMID: %06x) entered firmware-update-mode\n", serial
, hmid
);
987 case DEVICE_TYPE_HMCFGUSB
:
988 printf("Adding HMID\n");
990 memset(out
, 0, sizeof(out
));
992 out
[1] = (hmid
>> 16) & 0xff;
993 out
[2] = (hmid
>> 8) & 0xff;
994 out
[3] = hmid
& 0xff;
996 hmcfgusb_send(dev
.hmcfgusb
, out
, sizeof(out
), 1);
998 case DEVICE_TYPE_HMUARTLGW
:
999 printf("Adding HMID\n");
1001 memset(out
, 0, sizeof(out
));
1002 out
[0] = HMUARTLGW_APP_ADD_PEER
;
1003 out
[1] = (hmid
>> 16) & 0xff;
1004 out
[2] = (hmid
>> 8) & 0xff;
1005 out
[3] = hmid
& 0xff;
1006 out
[4] = 0x00; /* KeyIndex */
1007 out
[5] = 0x00; /* WakeUp? */
1008 out
[6] = 0x00; /* WakeUp? */
1011 rdata
.uartlgw_state
= HMUARTLGW_STATE_WAIT_APP
;
1012 hmuartlgw_send(dev
.hmuartlgw
, out
, 7, HMUARTLGW_APP
);
1013 do { hmuartlgw_poll(dev
.hmuartlgw
, 500); } while (rdata
.uartlgw_state
!= HMUARTLGW_STATE_ACK_APP
);
1014 } while (rdata
.status
== 0x08);
1021 printf("Initiating remote switch to 100k\n");
1023 memset(out
, 0, sizeof(out
));
1025 out
[MSGID
] = msgid
++;
1028 SET_SRC(out
, my_hmid
);
1031 memcpy(&out
[PAYLOAD
], cc1101_regs
, sizeof(cc1101_regs
));
1032 SET_LEN_FROM_PAYLOADLEN(out
, sizeof(cc1101_regs
));
1034 if (!send_hm_message(&dev
, &rdata
, out
)) {
1038 if (!switch_speed(&dev
, &rdata
, 100)) {
1039 fprintf(stderr
, "Can't switch speed!\n");
1043 printf("Has the device switched?\n");
1045 memset(out
, 0, sizeof(out
));
1047 out
[MSGID
] = msgid
++;
1050 SET_SRC(out
, my_hmid
);
1053 memcpy(&out
[PAYLOAD
], cc1101_regs
, sizeof(cc1101_regs
));
1054 SET_LEN_FROM_PAYLOADLEN(out
, sizeof(cc1101_regs
));
1058 if (send_hm_message(&dev
, &rdata
, out
)) {
1059 /* A0A02000221B9AD00000000 */
1068 if (!switch_speed(&dev
, &rdata
, 10)) {
1069 fprintf(stderr
, "Can't switch speed!\n");
1073 } while ((!switched
) && (switchcnt
--));
1076 fprintf(stderr
, "Too many errors, giving up!\n");
1082 printf("Flashing %d blocks", fw
->fw_blocks
);
1086 printf(": %04u/%04u %c", 0, fw
->fw_blocks
, twiddlie
[0]);
1090 for (block
= 0; block
< fw
->fw_blocks
; block
++) {
1093 len
= fw
->fw
[block
][2] << 8;
1094 len
|= fw
->fw
[block
][3];
1096 pos
= &(fw
->fw
[block
][2]);
1098 len
+= 2; /* length */
1101 hexdump(pos
, len
, "F> ");
1106 int payloadlen
= max_payloadlen
- 2;
1110 payloadlen
= max_payloadlen
;
1114 if ((len
- (pos
- &(fw
->fw
[block
][2]))) < payloadlen
)
1115 payloadlen
= (len
- (pos
- &(fw
->fw
[block
][2])));
1117 if (((pos
+ payloadlen
) - &(fw
->fw
[block
][2])) == len
)
1120 memset(&rdata
, 0, sizeof(rdata
));
1122 memset(out
, 0, sizeof(out
));
1128 SET_SRC(out
, my_hmid
);
1131 memcpy(&out
[PAYLOAD
], pos
, payloadlen
);
1132 SET_LEN_FROM_PAYLOADLEN(out
, payloadlen
);
1134 if (send_hm_message(&dev
, &rdata
, out
)) {
1137 pos
= &(fw
->fw
[block
][2]);
1139 if (cnt
== MAX_RETRIES
) {
1140 fprintf(stderr
, "\nToo many errors, giving up!\n");
1143 printf("Flashing %d blocks: %04u/%04u %c", fw
->fw_blocks
, block
+ 1, fw
->fw_blocks
, twiddlie
[msgnum
% sizeof(twiddlie
)]);
1150 printf("\b\b\b\b\b\b\b\b\b\b\b%04u/%04u %c",
1151 block
+ 1, fw
->fw_blocks
, twiddlie
[msgnum
% sizeof(twiddlie
)]);
1154 } while((pos
- &(fw
->fw
[block
][2])) < len
);
1162 if (!switch_speed(&dev
, &rdata
, 10)) {
1163 fprintf(stderr
, "Can't switch speed!\n");
1167 printf("Waiting for device to reboot\n");
1168 rdata
.message_type
= MESSAGE_TYPE_R
;
1171 if (dev
.type
== DEVICE_TYPE_HMUARTLGW
)
1172 cnt
= 200; /* FIXME */
1176 case DEVICE_TYPE_CULFW
:
1177 pfd
= culfw_poll(dev
.culfw
, 1000);
1179 case DEVICE_TYPE_HMCFGUSB
:
1180 pfd
= hmcfgusb_poll(dev
.hmcfgusb
, 1000);
1182 case DEVICE_TYPE_HMUARTLGW
:
1183 pfd
= hmuartlgw_poll(dev
.hmuartlgw
, 1000);
1189 if ((pfd
< 0) && errno
) {
1190 if (errno
!= ETIMEDOUT
) {
1195 if (rdata
.message_type
== MESSAGE_TYPE_E
) {
1200 if (rdata
.message_type
== MESSAGE_TYPE_E
) {
1201 printf("Device rebooted\n");
1205 case DEVICE_TYPE_HMCFGUSB
:
1206 hmcfgusb_close(dev
.hmcfgusb
);
1209 case DEVICE_TYPE_CULFW
:
1210 culfw_close(dev
.culfw
);
1214 return EXIT_SUCCESS
;