1 /* flasher for HomeMatic-devices supporting OTA updates
3 * Copyright (c) 2014 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>
47 #define NORMAL_MAX_PAYLOAD 37
48 #define LOWE_MAX_PAYLOAD 17
55 /* Maximum payloadlen supported by IO */
56 uint32_t max_payloadlen
= NORMAL_MAX_PAYLOAD
;
65 struct hmcfgusb_dev
*hmcfgusb
;
66 struct culfw_dev
*culfw
;
76 enum message_type message_type
;
82 static int parse_hmcfgusb(uint8_t *buf
, int buf_len
, void *data
)
84 struct recv_data
*rdata
= data
;
92 ((buf
[0x11] == ((hmid
>> 16) & 0xff)) &&
93 (buf
[0x12] == ((hmid
>> 8) & 0xff)) &&
94 (buf
[0x13] == (hmid
& 0xff)))) {
95 memset(rdata
->message
, 0, sizeof(rdata
->message
));
96 memcpy(rdata
->message
, buf
+ 0x0d, buf
[0x0d] + 1);
97 rdata
->message_type
= MESSAGE_TYPE_E
;
101 memset(rdata
->message
, 0, sizeof(rdata
->message
));
102 memcpy(rdata
->message
, buf
+ 0x0e, buf
[0x0e] + 1);
103 rdata
->status
= (buf
[5] << 8) | buf
[6];
104 rdata
->message_type
= MESSAGE_TYPE_R
;
107 rdata
->speed
= buf
[1];
110 rdata
->version
= (buf
[11] << 8) | buf
[12];
111 my_hmid
= (buf
[0x1b] << 16) | (buf
[0x1c] << 8) | buf
[0x1d];
123 static int parse_culfw(uint8_t *buf
, int buf_len
, void *data
)
125 struct recv_data
*rdata
= data
;
128 memset(rdata
, 0, sizeof(struct recv_data
));
138 while(validate_nibble(buf
[(pos
* 2) + 1]) &&
139 validate_nibble(buf
[(pos
* 2) + 2]) &&
140 (pos
+ 1 < buf_len
)) {
141 rdata
->message
[pos
] = ascii_to_nibble(buf
[(pos
* 2) + 1]) << 4;
142 rdata
->message
[pos
] |= ascii_to_nibble(buf
[(pos
* 2) + 2]);
146 if (hmid
&& (SRC(rdata
->message
) != hmid
))
149 rdata
->message_type
= MESSAGE_TYPE_E
;
157 s
= ((char*)buf
) + 2;
160 fprintf(stderr
, "Unknown response from CUL: %s", buf
);
165 rdata
->version
= v
<< 8;
170 fprintf(stderr
, "Unknown response from CUL: %s", buf
);
179 fprintf(stderr
, "Unknown response from CUL: %s", buf
);
187 int send_hm_message(struct ota_dev
*dev
, struct recv_data
*rdata
, uint8_t *msg
)
189 static uint32_t id
= 1;
195 case DEVICE_TYPE_HMCFGUSB
:
196 if (gettimeofday(&tv
, NULL
) == -1) {
197 perror("gettimeofay");
201 memset(out
, 0, sizeof(out
));
204 out
[1] = (id
>> 24) & 0xff;
205 out
[2] = (id
>> 16) & 0xff;
206 out
[3] = (id
>> 8) & 0xff;
209 out
[11] = (tv
.tv_usec
>> 24) & 0xff;
210 out
[12] = (tv
.tv_usec
>> 16) & 0xff;
211 out
[13] = (tv
.tv_usec
>> 8) & 0xff;
212 out
[14] = tv
.tv_usec
& 0xff;
214 memcpy(&out
[0x0f], msg
, msg
[0] + 1);
216 memset(rdata
, 0, sizeof(struct recv_data
));
217 hmcfgusb_send(dev
->hmcfgusb
, out
, sizeof(out
), 1);
220 if (rdata
->message_type
== MESSAGE_TYPE_R
) {
221 if (((rdata
->status
& 0xff) == 0x01) ||
222 ((rdata
->status
& 0xff) == 0x02)) {
225 if ((rdata
->status
& 0xff00) == 0x0400) {
226 fprintf(stderr
, "\nOut of credits!\n");
227 } else if ((rdata
->status
& 0xff) == 0x08) {
228 fprintf(stderr
, "\nMissing ACK!\n");
230 fprintf(stderr
, "\nInvalid status: %04x\n", rdata
->status
);
236 pfd
= hmcfgusb_poll(dev
->hmcfgusb
, 1000);
237 if ((pfd
< 0) && errno
) {
238 if (errno
!= ETIMEDOUT
) {
239 perror("\n\nhmcfgusb_poll");
245 case DEVICE_TYPE_CULFW
:
250 memset(buf
, 0, sizeof(buf
));
253 for (i
= 0; i
< msg
[0] + 1; i
++) {
254 buf
[2 + (i
* 2)] = nibble_to_ascii((msg
[i
] >> 4) & 0xf);
255 buf
[2 + (i
* 2) + 1] = nibble_to_ascii(msg
[i
] & 0xf);
257 buf
[2 + (i
* 2) ] = '\r';
258 buf
[2 + (i
* 2) + 1] = '\n';
260 memset(rdata
, 0, sizeof(struct recv_data
));
261 if (culfw_send(dev
->culfw
, buf
, 2 + (i
* 2) + 1) == 0) {
262 fprintf(stderr
, "culfw_send failed!\n");
266 if (msg
[CTL
] & 0x20) {
271 pfd
= culfw_poll(dev
->culfw
, 200);
272 if ((pfd
< 0) && errno
) {
273 if (errno
!= ETIMEDOUT
) {
274 perror("\n\nculfw_poll");
278 if (rdata
->message_type
== MESSAGE_TYPE_E
) {
284 fprintf(stderr
, "\nMissing ACK!\n");
296 static int switch_speed(struct ota_dev
*dev
, struct recv_data
*rdata
, uint8_t speed
)
301 printf("Entering %uk-mode\n", speed
);
304 case DEVICE_TYPE_HMCFGUSB
:
305 memset(out
, 0, sizeof(out
));
309 hmcfgusb_send(dev
->hmcfgusb
, out
, sizeof(out
), 1);
313 pfd
= hmcfgusb_poll(dev
->hmcfgusb
, 1000);
314 if ((pfd
< 0) && errno
) {
315 if (errno
!= ETIMEDOUT
) {
316 perror("\n\nhmcfgusb_poll");
320 if (rdata
->speed
== speed
)
324 case DEVICE_TYPE_CULFW
:
326 return culfw_send(dev
->culfw
, "AR\r\n", 4);
328 return culfw_send(dev
->culfw
, "Ar\r\n", 4);
336 void flash_ota_syntax(char *prog
)
338 fprintf(stderr
, "Syntax: %s parameters options\n\n", prog
);
339 fprintf(stderr
, "Mandatory parameters:\n");
340 fprintf(stderr
, "\t-f firmware.eq3\tfirmware file to flash\n");
341 fprintf(stderr
, "\t-s SERIAL\tserial of device to flash\n");
342 fprintf(stderr
, "\nPossible options:\n");
343 fprintf(stderr
, "\t-c device\tenable CUL-mode with CUL at path \"device\"\n");
344 fprintf(stderr
, "\t-b bps\t\tuse CUL with speed \"bps\" (default: %u)\n", DEFAULT_CUL_BPS
);
345 fprintf(stderr
, "\t-l\t\tlower payloadlen (required for devices with little RAM, e.g. CUL v2 and CUL v4)\n");
346 fprintf(stderr
, "\t-h\t\tthis help\n");
349 int main(int argc
, char **argv
)
351 const char twiddlie
[] = { '-', '\\', '|', '/' };
352 const uint8_t cc1101_regs
[] = { 0x10, 0x5B, 0x11, 0xF8, 0x15, 0x47 };
353 char *fw_file
= NULL
;
355 char *culfw_dev
= NULL
;
356 unsigned int bps
= DEFAULT_CUL_BPS
;
358 struct recv_data rdata
;
373 printf("HomeMatic OTA flasher version " VERSION
"\n\n");
375 while((opt
= getopt(argc
, argv
, "b:c:f:hls:")) != -1) {
387 printf("Reducing payload-len from %d to %d\n", max_payloadlen
, LOWE_MAX_PAYLOAD
);
388 max_payloadlen
= LOWE_MAX_PAYLOAD
;
397 flash_ota_syntax(argv
[0]);
404 if (!fw_file
|| !serial
) {
405 flash_ota_syntax(argv
[0]);
409 fw
= firmware_read_firmware(fw_file
, debug
);
413 memset(&rdata
, 0, sizeof(rdata
));
414 memset(&dev
, 0, sizeof(struct ota_dev
));
417 printf("Opening culfw-device at path %s with speed %u\n", culfw_dev
, bps
);
418 dev
.culfw
= culfw_init(culfw_dev
, bps
, parse_culfw
, &rdata
);
420 fprintf(stderr
, "Can't initialize CUL at %s with rate %u\n", culfw_dev
, bps
);
423 dev
.type
= DEVICE_TYPE_CULFW
;
425 printf("Requesting firmware version\n");
426 culfw_send(dev
.culfw
, "\r\n", 2);
427 culfw_flush(dev
.culfw
);
430 culfw_send(dev
.culfw
, "V\r\n", 3);
433 pfd
= culfw_poll(dev
.culfw
, 1000);
434 if ((pfd
< 0) && errno
) {
435 if (errno
!= ETIMEDOUT
) {
436 perror("\n\nhmcfgusb_poll");
444 printf("culfw-device firmware version: %u.%02u\n",
445 (rdata
.version
>> 8) & 0xff,
446 rdata
.version
& 0xff);
448 if (rdata
.version
< 0x013a) {
449 fprintf(stderr
, "\nThis version does _not_ support firmware upgrade mode, you need at least 1.58!\n");
453 hmcfgusb_set_debug(debug
);
455 dev
.hmcfgusb
= hmcfgusb_init(parse_hmcfgusb
, &rdata
);
457 fprintf(stderr
, "Can't initialize HM-CFG-USB\n");
460 dev
.type
= DEVICE_TYPE_HMCFGUSB
;
462 printf("\nRebooting HM-CFG-USB to avoid running out of credits\n\n");
464 if (!dev
.hmcfgusb
->bootloader
) {
465 printf("HM-CFG-USB not in bootloader mode, entering bootloader.\n");
466 printf("Waiting for device to reappear...\n");
470 if (!dev
.hmcfgusb
->bootloader
)
471 hmcfgusb_enter_bootloader(dev
.hmcfgusb
);
472 hmcfgusb_close(dev
.hmcfgusb
);
475 } while (((dev
.hmcfgusb
= hmcfgusb_init(parse_hmcfgusb
, &rdata
)) == NULL
) || (!dev
.hmcfgusb
->bootloader
));
478 if (dev
.hmcfgusb
->bootloader
) {
479 printf("HM-CFG-USB in bootloader mode, rebooting\n");
483 if (dev
.hmcfgusb
->bootloader
)
484 hmcfgusb_leave_bootloader(dev
.hmcfgusb
);
485 hmcfgusb_close(dev
.hmcfgusb
);
488 } while (((dev
.hmcfgusb
= hmcfgusb_init(parse_hmcfgusb
, &rdata
)) == NULL
) || (dev
.hmcfgusb
->bootloader
));
491 printf("\n\nHM-CFG-USB opened\n\n");
493 memset(out
, 0, sizeof(out
));
495 hmcfgusb_send(dev
.hmcfgusb
, out
, sizeof(out
), 1);
499 pfd
= hmcfgusb_poll(dev
.hmcfgusb
, 1000);
500 if ((pfd
< 0) && errno
) {
501 if (errno
!= ETIMEDOUT
) {
502 perror("\n\nhmcfgusb_poll");
510 if (rdata
.version
< 0x3c7) {
511 fprintf(stderr
, "HM-CFG-USB firmware too low: %u < 967\n", rdata
.version
);
515 printf("HM-CFG-USB firmware version: %u\n", rdata
.version
);
518 if (!switch_speed(&dev
, &rdata
, 10)) {
519 fprintf(stderr
, "Can't switch speed!\n");
523 printf("Waiting for device with serial %s\n", serial
);
528 case DEVICE_TYPE_CULFW
:
529 pfd
= culfw_poll(dev
.culfw
, 1000);
531 case DEVICE_TYPE_HMCFGUSB
:
533 pfd
= hmcfgusb_poll(dev
.hmcfgusb
, 1000);
537 if ((pfd
< 0) && errno
) {
538 if (errno
!= ETIMEDOUT
) {
544 if ((rdata
.message
[LEN
] == 0x14) && /* Length */
545 (rdata
.message
[MSGID
] == 0x00) && /* Message ID */
546 (rdata
.message
[CTL
] == 0x00) && /* Control Byte */
547 (rdata
.message
[TYPE
] == 0x10) && /* Messagte type: Information */
548 (DST(rdata
.message
) == 0x000000) && /* Broadcast */
549 (rdata
.message
[PAYLOAD
] == 0x00)) { /* FUP? */
550 if (!strncmp((char*)&(rdata
.message
[0x0b]), serial
, 10)) {
551 hmid
= SRC(rdata
.message
);
557 printf("Device with serial %s (hmid: %06x) entered firmware-update-mode\n", serial
, hmid
);
559 if (dev
.type
== DEVICE_TYPE_HMCFGUSB
) {
560 printf("Adding HMID\n");
562 memset(out
, 0, sizeof(out
));
564 out
[1] = (hmid
>> 16) & 0xff;
565 out
[2] = (hmid
>> 8) & 0xff;
566 out
[3] = hmid
& 0xff;
568 hmcfgusb_send(dev
.hmcfgusb
, out
, sizeof(out
), 1);
573 printf("Initiating remote switch to 100k\n");
575 memset(out
, 0, sizeof(out
));
577 out
[MSGID
] = msgid
++;
580 SET_SRC(out
, my_hmid
);
583 memcpy(&out
[PAYLOAD
], cc1101_regs
, sizeof(cc1101_regs
));
584 SET_LEN_FROM_PAYLOADLEN(out
, sizeof(cc1101_regs
));
586 if (!send_hm_message(&dev
, &rdata
, out
)) {
590 if (!switch_speed(&dev
, &rdata
, 100)) {
591 fprintf(stderr
, "Can't switch speed!\n");
595 printf("Has the device switched?\n");
597 memset(out
, 0, sizeof(out
));
599 out
[MSGID
] = msgid
++;
602 SET_SRC(out
, my_hmid
);
605 memcpy(&out
[PAYLOAD
], cc1101_regs
, sizeof(cc1101_regs
));
606 SET_LEN_FROM_PAYLOADLEN(out
, sizeof(cc1101_regs
));
610 if (send_hm_message(&dev
, &rdata
, out
)) {
611 /* A0A02000221B9AD00000000 */
620 if (!switch_speed(&dev
, &rdata
, 10)) {
621 fprintf(stderr
, "Can't switch speed!\n");
625 } while ((!switched
) && (switchcnt
--));
628 fprintf(stderr
, "Too many errors, giving up!\n");
634 printf("Flashing %d blocks", fw
->fw_blocks
);
638 printf(": %04u/%04u %c", 0, fw
->fw_blocks
, twiddlie
[0]);
642 for (block
= 0; block
< fw
->fw_blocks
; block
++) {
645 len
= fw
->fw
[block
][2] << 8;
646 len
|= fw
->fw
[block
][3];
648 pos
= &(fw
->fw
[block
][2]);
650 len
+= 2; /* length */
653 hexdump(pos
, len
, "F> ");
658 int payloadlen
= max_payloadlen
- 2;
662 payloadlen
= max_payloadlen
;
666 if ((len
- (pos
- &(fw
->fw
[block
][2]))) < payloadlen
)
667 payloadlen
= (len
- (pos
- &(fw
->fw
[block
][2])));
669 if (((pos
+ payloadlen
) - &(fw
->fw
[block
][2])) == len
)
672 memset(&rdata
, 0, sizeof(rdata
));
674 memset(out
, 0, sizeof(out
));
680 SET_SRC(out
, my_hmid
);
683 memcpy(&out
[PAYLOAD
], pos
, payloadlen
);
684 SET_LEN_FROM_PAYLOADLEN(out
, payloadlen
);
686 if (send_hm_message(&dev
, &rdata
, out
)) {
689 pos
= &(fw
->fw
[block
][2]);
691 if (cnt
== MAX_RETRIES
) {
692 fprintf(stderr
, "\nToo many errors, giving up!\n");
695 printf("Flashing %d blocks: %04u/%04u %c", fw
->fw_blocks
, block
+ 1, fw
->fw_blocks
, twiddlie
[msgnum
% sizeof(twiddlie
)]);
702 printf("\b\b\b\b\b\b\b\b\b\b\b%04u/%04u %c",
703 block
+ 1, fw
->fw_blocks
, twiddlie
[msgnum
% sizeof(twiddlie
)]);
706 } while((pos
- &(fw
->fw
[block
][2])) < len
);
714 if (!switch_speed(&dev
, &rdata
, 10)) {
715 fprintf(stderr
, "Can't switch speed!\n");
719 printf("Waiting for device to reboot\n");
725 case DEVICE_TYPE_CULFW
:
726 pfd
= culfw_poll(dev
.culfw
, 1000);
728 case DEVICE_TYPE_HMCFGUSB
:
730 pfd
= hmcfgusb_poll(dev
.hmcfgusb
, 1000);
733 if ((pfd
< 0) && errno
) {
734 if (errno
!= ETIMEDOUT
) {
739 if (rdata
.message_type
== MESSAGE_TYPE_E
) {
744 if (rdata
.message_type
== MESSAGE_TYPE_E
) {
745 printf("Device rebooted\n");
749 case DEVICE_TYPE_HMCFGUSB
:
750 hmcfgusb_close(dev
.hmcfgusb
);
753 case DEVICE_TYPE_CULFW
:
754 culfw_close(dev
.culfw
);