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>
60 struct hmcfgusb_dev
*hmcfgusb
;
61 struct culfw_dev
*culfw
;
71 enum message_type message_type
;
74 uint16_t hmcfgusb_version
;
77 static int parse_hmcfgusb(uint8_t *buf
, int buf_len
, void *data
)
79 struct recv_data
*rdata
= data
;
87 ((buf
[0x11] == ((hmid
>> 16) & 0xff)) &&
88 (buf
[0x12] == ((hmid
>> 8) & 0xff)) &&
89 (buf
[0x13] == (hmid
& 0xff)))) {
90 memset(rdata
->message
, 0, sizeof(rdata
->message
));
91 memcpy(rdata
->message
, buf
+ 0x0d, buf
[0x0d] + 1);
92 rdata
->message_type
= MESSAGE_TYPE_E
;
96 memset(rdata
->message
, 0, sizeof(rdata
->message
));
97 memcpy(rdata
->message
, buf
+ 0x0e, buf
[0x0e] + 1);
98 rdata
->status
= (buf
[5] << 8) | buf
[6];
99 rdata
->message_type
= MESSAGE_TYPE_R
;
102 rdata
->speed
= buf
[1];
105 rdata
->hmcfgusb_version
= (buf
[11] << 8) | buf
[12];
106 my_hmid
= (buf
[0x1b] << 16) | (buf
[0x1c] << 8) | buf
[0x1d];
118 static int parse_culfw(uint8_t *buf
, int buf_len
, void *data
)
120 struct recv_data
*rdata
= data
;
123 memset(rdata
, 0, sizeof(struct recv_data
));
134 while(validate_nibble(buf
[(pos
* 2) + 1]) &&
135 validate_nibble(buf
[(pos
* 2) + 2]) &&
136 (pos
+ 1 < buf_len
)) {
137 rdata
->message
[pos
] = ascii_to_nibble(buf
[(pos
* 2) + 1]) << 4;
138 rdata
->message
[pos
] |= ascii_to_nibble(buf
[(pos
* 2) + 2]);
142 if (hmid
&& (SRC(rdata
->message
) != hmid
))
145 rdata
->message_type
= MESSAGE_TYPE_E
;
150 int send_hm_message(struct ota_dev
*dev
, struct recv_data
*rdata
, uint8_t *msg
)
152 static uint32_t id
= 1;
158 case DEVICE_TYPE_HMCFGUSB
:
159 if (gettimeofday(&tv
, NULL
) == -1) {
160 perror("gettimeofay");
164 memset(out
, 0, sizeof(out
));
167 out
[1] = (id
>> 24) & 0xff;
168 out
[2] = (id
>> 16) & 0xff;
169 out
[3] = (id
>> 8) & 0xff;
172 out
[11] = (tv
.tv_usec
>> 24) & 0xff;
173 out
[12] = (tv
.tv_usec
>> 16) & 0xff;
174 out
[13] = (tv
.tv_usec
>> 8) & 0xff;
175 out
[14] = tv
.tv_usec
& 0xff;
177 memcpy(&out
[0x0f], msg
, msg
[0] + 1);
179 memset(rdata
, 0, sizeof(struct recv_data
));
180 hmcfgusb_send(dev
->hmcfgusb
, out
, sizeof(out
), 1);
183 if (rdata
->message_type
== MESSAGE_TYPE_R
) {
184 if (((rdata
->status
& 0xff) == 0x01) ||
185 ((rdata
->status
& 0xff) == 0x02)) {
188 if ((rdata
->status
& 0xff00) == 0x0400) {
189 fprintf(stderr
, "\nOut of credits!\n");
190 } else if ((rdata
->status
& 0xff) == 0x08) {
191 fprintf(stderr
, "\nMissing ACK!\n");
193 fprintf(stderr
, "\nInvalid status: %04x\n", rdata
->status
);
199 pfd
= hmcfgusb_poll(dev
->hmcfgusb
, 1);
200 if ((pfd
< 0) && errno
) {
201 if (errno
!= ETIMEDOUT
) {
202 perror("\n\nhmcfgusb_poll");
208 case DEVICE_TYPE_CULFW
:
213 memset(buf
, 0, sizeof(buf
));
216 for (i
= 0; i
< msg
[0] + 1; i
++) {
217 buf
[2 + (i
* 2)] = nibble_to_ascii((msg
[i
] >> 4) & 0xf);
218 buf
[2 + (i
* 2) + 1] = nibble_to_ascii(msg
[i
] & 0xf);
220 buf
[2 + (i
* 2) ] = '\r';
221 buf
[2 + (i
* 2) + 1] = '\n';
223 memset(rdata
, 0, sizeof(struct recv_data
));
224 if (culfw_send(dev
->culfw
, buf
, 2 + (i
* 2) + 1) == 0) {
225 fprintf(stderr
, "culfw_send failed!\n");
229 if (msg
[CTL
] & 0x20) {
234 pfd
= culfw_poll(dev
->culfw
, 1);
235 if ((pfd
< 0) && errno
) {
236 if (errno
!= ETIMEDOUT
) {
237 perror("\n\nhmcfgusb_poll");
241 if (rdata
->message_type
== MESSAGE_TYPE_E
) {
254 static int switch_speed(struct ota_dev
*dev
, struct recv_data
*rdata
, uint8_t speed
)
259 printf("Entering %uk-mode\n", speed
);
262 case DEVICE_TYPE_HMCFGUSB
:
263 memset(out
, 0, sizeof(out
));
267 hmcfgusb_send(dev
->hmcfgusb
, out
, sizeof(out
), 1);
271 pfd
= hmcfgusb_poll(dev
->hmcfgusb
, 1);
272 if ((pfd
< 0) && errno
) {
273 if (errno
!= ETIMEDOUT
) {
274 perror("\n\nhmcfgusb_poll");
278 if (rdata
->speed
== speed
)
282 case DEVICE_TYPE_CULFW
:
284 return culfw_send(dev
->culfw
, "AR\r\n", 4);
286 return culfw_send(dev
->culfw
, "Ar\r\n", 4);
294 void flash_ota_syntax(char *prog
)
296 fprintf(stderr
, "Syntax: %s parameters options\n\n", prog
);
297 fprintf(stderr
, "Mandatory parameters:\n");
298 fprintf(stderr
, "\t-f firmware.eq3\tfirmware file to flash\n");
299 fprintf(stderr
, "\t-s SERIAL\tserial of device to flash\n");
300 fprintf(stderr
, "\nPossible options:\n");
301 fprintf(stderr
, "\t-c device\tenable CUL-mode with CUL at path \"device\"\n");
302 fprintf(stderr
, "\t-b bps\t\tuse CUL with speed \"bps\" (default: %u)\n", DEFAULT_CUL_BPS
);
303 fprintf(stderr
, "\t-h\t\tthis help\n");
306 int main(int argc
, char **argv
)
308 const char twiddlie
[] = { '-', '\\', '|', '/' };
309 const uint8_t cc1101_regs
[] = { 0x10, 0x5B, 0x11, 0xF8, 0x15, 0x47 };
310 char *fw_file
= NULL
;
312 char *culfw_dev
= NULL
;
313 unsigned int bps
= DEFAULT_CUL_BPS
;
315 struct recv_data rdata
;
330 printf("HomeMatic OTA flasher version " VERSION
"\n\n");
332 while((opt
= getopt(argc
, argv
, "b:c:f:hs:")) != -1) {
350 flash_ota_syntax(argv
[0]);
357 if (!fw_file
|| !serial
) {
358 flash_ota_syntax(argv
[0]);
362 fw
= firmware_read_firmware(fw_file
, debug
);
366 memset(&rdata
, 0, sizeof(rdata
));
367 memset(&dev
, 0, sizeof(struct ota_dev
));
370 dev
.culfw
= culfw_init(culfw_dev
, bps
, parse_culfw
, &rdata
);
372 fprintf(stderr
, "Can't initialize CUL at %s with rate %u\n", culfw_dev
, bps
);
375 dev
.type
= DEVICE_TYPE_CULFW
;
377 hmcfgusb_set_debug(debug
);
379 dev
.hmcfgusb
= hmcfgusb_init(parse_hmcfgusb
, &rdata
);
381 fprintf(stderr
, "Can't initialize HM-CFG-USB\n");
384 dev
.type
= DEVICE_TYPE_HMCFGUSB
;
386 printf("\nRebooting HM-CFG-USB to avoid running out of credits\n\n");
388 if (!dev
.hmcfgusb
->bootloader
) {
389 printf("HM-CFG-USB not in bootloader mode, entering bootloader.\n");
390 hmcfgusb_enter_bootloader(dev
.hmcfgusb
);
391 printf("Waiting for device to reappear...\n");
395 hmcfgusb_close(dev
.hmcfgusb
);
398 } while (((dev
.hmcfgusb
= hmcfgusb_init(parse_hmcfgusb
, &rdata
)) == NULL
) || (!dev
.hmcfgusb
->bootloader
));
401 if (dev
.hmcfgusb
->bootloader
) {
402 printf("HM-CFG-USB in bootloader mode, rebooting\n");
403 hmcfgusb_leave_bootloader(dev
.hmcfgusb
);
407 hmcfgusb_close(dev
.hmcfgusb
);
410 } while (((dev
.hmcfgusb
= hmcfgusb_init(parse_hmcfgusb
, &rdata
)) == NULL
) || (dev
.hmcfgusb
->bootloader
));
413 printf("\n\nHM-CFG-USB opened\n\n");
415 memset(out
, 0, sizeof(out
));
417 hmcfgusb_send(dev
.hmcfgusb
, out
, sizeof(out
), 1);
421 pfd
= hmcfgusb_poll(dev
.hmcfgusb
, 1);
422 if ((pfd
< 0) && errno
) {
423 if (errno
!= ETIMEDOUT
) {
424 perror("\n\nhmcfgusb_poll");
428 if (rdata
.hmcfgusb_version
)
432 if (rdata
.hmcfgusb_version
< 0x3c7) {
433 fprintf(stderr
, "HM-CFG-USB firmware too low: %u < 967\n", rdata
.hmcfgusb_version
);
437 printf("HM-CFG-USB firmware version: %u\n", rdata
.hmcfgusb_version
);
440 if (!switch_speed(&dev
, &rdata
, 10)) {
441 fprintf(stderr
, "Can't switch speed!\n");
445 printf("Waiting for device with serial %s\n", serial
);
450 case DEVICE_TYPE_CULFW
:
451 pfd
= culfw_poll(dev
.culfw
, 1);
453 case DEVICE_TYPE_HMCFGUSB
:
455 pfd
= hmcfgusb_poll(dev
.hmcfgusb
, 1);
459 if ((pfd
< 0) && errno
) {
460 if (errno
!= ETIMEDOUT
) {
466 if ((rdata
.message
[LEN
] == 0x14) && /* Length */
467 (rdata
.message
[MSGID
] == 0x00) && /* Message ID */
468 (rdata
.message
[CTL
] == 0x00) && /* Control Byte */
469 (rdata
.message
[TYPE
] == 0x10) && /* Messagte type: Information */
470 (DST(rdata
.message
) == 0x000000) && /* Broadcast */
471 (rdata
.message
[PAYLOAD
] == 0x00)) { /* FUP? */
472 if (!strncmp((char*)&(rdata
.message
[0x0b]), serial
, 10)) {
473 hmid
= SRC(rdata
.message
);
479 printf("Device with serial %s (hmid: %06x) entered firmware-update-mode\n", serial
, hmid
);
481 if (dev
.type
== DEVICE_TYPE_HMCFGUSB
) {
482 printf("Adding HMID\n");
484 memset(out
, 0, sizeof(out
));
486 out
[1] = (hmid
>> 16) & 0xff;
487 out
[2] = (hmid
>> 8) & 0xff;
488 out
[3] = hmid
& 0xff;
490 hmcfgusb_send(dev
.hmcfgusb
, out
, sizeof(out
), 1);
495 printf("Initiating remote switch to 100k\n");
497 memset(out
, 0, sizeof(out
));
499 out
[MSGID
] = msgid
++;
502 SET_SRC(out
, my_hmid
);
505 memcpy(&out
[PAYLOAD
], cc1101_regs
, sizeof(cc1101_regs
));
506 SET_LEN_FROM_PAYLOADLEN(out
, sizeof(cc1101_regs
));
508 if (!send_hm_message(&dev
, &rdata
, out
)) {
512 if (!switch_speed(&dev
, &rdata
, 100)) {
513 fprintf(stderr
, "Can't switch speed!\n");
517 printf("Has the device switched?\n");
519 memset(out
, 0, sizeof(out
));
521 out
[MSGID
] = msgid
++;
524 SET_SRC(out
, my_hmid
);
527 memcpy(&out
[PAYLOAD
], cc1101_regs
, sizeof(cc1101_regs
));
528 SET_LEN_FROM_PAYLOADLEN(out
, sizeof(cc1101_regs
));
532 if (send_hm_message(&dev
, &rdata
, out
)) {
533 /* A0A02000221B9AD00000000 */
542 if (!switch_speed(&dev
, &rdata
, 10)) {
543 fprintf(stderr
, "Can't switch speed!\n");
547 } while ((!switched
) && (switchcnt
--));
550 fprintf(stderr
, "Too many errors, giving up!\n");
556 printf("Flashing %d blocks", fw
->fw_blocks
);
560 printf(": %04u/%04u %c", 0, fw
->fw_blocks
, twiddlie
[0]);
564 for (block
= 0; block
< fw
->fw_blocks
; block
++) {
567 len
= fw
->fw
[block
][2] << 8;
568 len
|= fw
->fw
[block
][3];
570 pos
= &(fw
->fw
[block
][2]);
572 len
+= 2; /* length */
575 hexdump(pos
, len
, "F> ");
588 if ((len
- (pos
- &(fw
->fw
[block
][2]))) < payloadlen
)
589 payloadlen
= (len
- (pos
- &(fw
->fw
[block
][2])));
591 if (((pos
+ payloadlen
) - &(fw
->fw
[block
][2])) == len
)
594 memset(&rdata
, 0, sizeof(rdata
));
596 memset(out
, 0, sizeof(out
));
602 SET_SRC(out
, my_hmid
);
605 memcpy(&out
[PAYLOAD
], pos
, payloadlen
);
606 SET_LEN_FROM_PAYLOADLEN(out
, payloadlen
);
608 if (send_hm_message(&dev
, &rdata
, out
)) {
611 pos
= &(fw
->fw
[block
][2]);
613 if (cnt
== MAX_RETRIES
) {
614 fprintf(stderr
, "\nToo many errors, giving up!\n");
617 printf("Flashing %d blocks: %04u/%04u %c", fw
->fw_blocks
, block
+ 1, fw
->fw_blocks
, twiddlie
[msgnum
% sizeof(twiddlie
)]);
624 printf("\b\b\b\b\b\b\b\b\b\b\b%04u/%04u %c",
625 block
+ 1, fw
->fw_blocks
, twiddlie
[msgnum
% sizeof(twiddlie
)]);
628 } while((pos
- &(fw
->fw
[block
][2])) < len
);
636 if (!switch_speed(&dev
, &rdata
, 10)) {
637 fprintf(stderr
, "Can't switch speed!\n");
641 printf("Waiting for device to reboot\n");
647 case DEVICE_TYPE_CULFW
:
648 pfd
= culfw_poll(dev
.culfw
, 1);
650 case DEVICE_TYPE_HMCFGUSB
:
652 pfd
= hmcfgusb_poll(dev
.hmcfgusb
, 1);
655 if ((pfd
< 0) && errno
) {
656 if (errno
!= ETIMEDOUT
) {
657 perror("\n\nhmcfgusb_poll");
661 if (rdata
.message_type
== MESSAGE_TYPE_E
) {
666 if (rdata
.message_type
== MESSAGE_TYPE_E
) {
667 printf("Device rebooted\n");
671 case DEVICE_TYPE_HMCFGUSB
:
672 hmcfgusb_close(dev
.hmcfgusb
);
674 case DEVICE_TYPE_CULFW
:
675 culfw_close(dev
.culfw
);