]>
Commit | Line | Data |
---|---|---|
1 | /* flasher for HomeMatic-devices supporting OTA updates | |
2 | * | |
3 | * Copyright (c) 2014-17 Michael Gernoth <michael@gernoth.net> | |
4 | * Copyright (c) 2017 noansi (TSCULFW-support) | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to | |
8 | * deal in the Software without restriction, including without limitation the | |
9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
10 | * sell copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
22 | * IN THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <unistd.h> | |
28 | #include <stdint.h> | |
29 | #include <string.h> | |
30 | #include <strings.h> | |
31 | #include <poll.h> | |
32 | #include <errno.h> | |
33 | #include <sys/types.h> | |
34 | #include <sys/stat.h> | |
35 | #include <fcntl.h> | |
36 | #include <sys/time.h> | |
37 | #include <libusb-1.0/libusb.h> | |
38 | ||
39 | #include "hexdump.h" | |
40 | #include "firmware.h" | |
41 | #include "hm.h" | |
42 | #include "version.h" | |
43 | #include "hmcfgusb.h" | |
44 | #include "culfw.h" | |
45 | #include "hmuartlgw.h" | |
46 | #include "util.h" | |
47 | ||
48 | #define MAX_RETRIES 5 | |
49 | #define NORMAL_MAX_PAYLOAD 37 | |
50 | #define LOWER_MAX_PAYLOAD 17 | |
51 | ||
52 | extern char *optarg; | |
53 | ||
54 | uint32_t hmid = 0; | |
55 | uint32_t my_hmid = 0; | |
56 | uint8_t key[16] = {0}; | |
57 | int32_t kNo = -1; | |
58 | ||
59 | /* Maximum payloadlen supported by IO */ | |
60 | uint32_t max_payloadlen = NORMAL_MAX_PAYLOAD; | |
61 | ||
62 | enum message_type { | |
63 | MESSAGE_TYPE_E = 1, | |
64 | MESSAGE_TYPE_R = 2, | |
65 | MESSAGE_TYPE_B = 3, | |
66 | }; | |
67 | ||
68 | enum hmuartlgw_state { | |
69 | HMUARTLGW_STATE_GET_HMID, | |
70 | HMUARTLGW_STATE_GET_FIRMWARE, | |
71 | HMUARTLGW_STATE_GET_CREDITS, | |
72 | HMUARTLGW_STATE_DONE, | |
73 | HMUARTLGW_STATE_WAIT_APP, | |
74 | HMUARTLGW_STATE_ACK_APP, | |
75 | }; | |
76 | ||
77 | struct recv_data { | |
78 | uint8_t message[64]; | |
79 | enum message_type message_type; | |
80 | uint16_t status; | |
81 | int speed; | |
82 | uint16_t version; | |
83 | uint8_t credits; | |
84 | enum hmuartlgw_state uartlgw_state; | |
85 | uint8_t uartlgw_version[3]; | |
86 | uint8_t is_TSCUL; // tsculfw | |
87 | }; | |
88 | ||
89 | static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data) | |
90 | { | |
91 | struct recv_data *rdata = data; | |
92 | ||
93 | if (buf_len < 1) | |
94 | return 1; | |
95 | ||
96 | switch (buf[0]) { | |
97 | case 'E': | |
98 | if ((!hmid) || | |
99 | ((buf[0x11] == ((hmid >> 16) & 0xff)) && | |
100 | (buf[0x12] == ((hmid >> 8) & 0xff)) && | |
101 | (buf[0x13] == (hmid & 0xff)))) { | |
102 | memset(rdata->message, 0, sizeof(rdata->message)); | |
103 | memcpy(rdata->message, buf + 0x0d, buf[0x0d] + 1); | |
104 | rdata->message_type = MESSAGE_TYPE_E; | |
105 | } | |
106 | break; | |
107 | case 'R': | |
108 | memset(rdata->message, 0, sizeof(rdata->message)); | |
109 | memcpy(rdata->message, buf + 0x0e, buf[0x0e] + 1); | |
110 | rdata->status = (buf[5] << 8) | buf[6]; | |
111 | rdata->message_type = MESSAGE_TYPE_R; | |
112 | break; | |
113 | case 'G': | |
114 | rdata->speed = buf[1]; | |
115 | break; | |
116 | case 'H': | |
117 | rdata->version = (buf[11] << 8) | buf[12]; | |
118 | rdata->credits = buf[36]; | |
119 | my_hmid = (buf[0x1b] << 16) | (buf[0x1c] << 8) | buf[0x1d]; | |
120 | break; | |
121 | default: | |
122 | break; | |
123 | } | |
124 | ||
125 | if (buf_len != 1) | |
126 | return 1; | |
127 | ||
128 | return 1; | |
129 | } | |
130 | ||
131 | static int parse_culfw(uint8_t *buf, int buf_len, void *data) | |
132 | { | |
133 | struct recv_data *rdata = data; | |
134 | int pos = 0; | |
135 | int rpos = 0; // read index | |
136 | ||
137 | memset(rdata->message, 0, sizeof(rdata->message)); | |
138 | rdata->message_type = 0; | |
139 | ||
140 | if (buf_len <= 3) | |
141 | return 0; | |
142 | ||
143 | switch(buf[0]) { | |
144 | case 'A': | |
145 | if (buf[1] == 's') | |
146 | return 0; | |
147 | ||
148 | if ((buf[1] == 'p') || (buf[1] == 't')) // tsculfw: ping or set timestamp command echoed? | |
149 | return 0; | |
150 | ||
151 | if (buf[1] == '?') {// tsculfw: unknown command | |
152 | fprintf(stderr, "unknown ASKSIN command sent\n"); | |
153 | return 0; | |
154 | } | |
155 | ||
156 | if (buf[1] == 'F') { // tsculfw: timestamp message? | |
157 | rdata->is_TSCUL = 1; | |
158 | if (buf_len <= (3+14)) // tsculfw: reasonable len? | |
159 | return 0; | |
160 | if (!validate_nibble(buf[3]) || !validate_nibble(buf[4])) // tsculfw: hex? | |
161 | return 0; | |
162 | ||
163 | rdata->credits = ascii_to_nibble(buf[3]); // tsculfw: coarse credits info, 0 = full credits (1800 x10ms) available | |
164 | ||
165 | //AFF1B000053A1010F0520CB1122334BD57110 | |
166 | switch(ascii_to_nibble(buf[4]) & 0x7) { // tsculfw: message type? | |
167 | case 0: // tsculfw: send fail message repeat fail or AES Auth error | |
168 | fprintf(stderr, "send didn't complete, repeat fail or AES Auth error\n"); | |
169 | return 0; | |
170 | case 1: // tsculfw: received message | |
171 | rpos += 7; // tsculfw: ignore timestamp data for now | |
172 | break; | |
173 | case 2: // tsculfw: ping answer | |
174 | return 0; | |
175 | case 3: // tsculfw: send success | |
176 | rdata->message_type = MESSAGE_TYPE_B; | |
177 | return 0; | |
178 | case 4: // tsculfw: send fail channel busy message | |
179 | fprintf(stderr, "CCA didn't complete, too much traffic\n"); | |
180 | return 0; | |
181 | case 5: // tsculfw: send fail credits message | |
182 | fprintf(stderr, "send didn't complete, not enough credits left\n"); | |
183 | return 0; | |
184 | case 6: // tsculfw: send timestamp fail message no buffer or send message length error | |
185 | fprintf(stderr, "send didn't complete, not enough credits left -> wait 30 minutes with TSCUL powered and not reset\n"); | |
186 | return 0; | |
187 | case 7: // tsculfw: send fail due to cc1101 TX-FIFO underflow error message | |
188 | fprintf(stderr, "send didn't complete, cc1101 TX-FIFO underflow\n"); | |
189 | return 0; | |
190 | default: | |
191 | break; | |
192 | } | |
193 | } | |
194 | ||
195 | while(validate_nibble(buf[(rpos * 2) + 1]) && | |
196 | validate_nibble(buf[(rpos * 2) + 2]) && | |
197 | (rpos + 1 < buf_len)) { | |
198 | rdata->message[pos] = ascii_to_nibble(buf[(rpos * 2) + 1]) << 4; | |
199 | rdata->message[pos] |= ascii_to_nibble(buf[(rpos * 2) + 2]); | |
200 | pos++; | |
201 | rpos++; | |
202 | } | |
203 | ||
204 | if (hmid && (SRC(rdata->message) != hmid)) | |
205 | return 0; | |
206 | ||
207 | rdata->message_type = MESSAGE_TYPE_E; | |
208 | break; | |
209 | case 'V': | |
210 | { | |
211 | uint8_t v; | |
212 | char *s; | |
213 | char *e; | |
214 | ||
215 | if (!strncmp((char*)buf, "VTS", 3)) { // tsculfw: "VTS x.xx NNNNNN" | |
216 | rdata->is_TSCUL = 1; | |
217 | rdata->version = 0xffff; | |
218 | break; | |
219 | } | |
220 | ||
221 | s = ((char*)buf) + 2; | |
222 | e = strchr(s, '.'); | |
223 | if (!e) { | |
224 | fprintf(stderr, "Unknown response from CUL: %s", buf); | |
225 | return 0; | |
226 | } | |
227 | *e = '\0'; | |
228 | v = atoi(s); | |
229 | rdata->version = v << 8; | |
230 | ||
231 | s = e + 1; | |
232 | e = strchr(s, ' '); | |
233 | if (!e) { | |
234 | fprintf(stderr, "Unknown response from CUL: %s", buf); | |
235 | return 0; | |
236 | } | |
237 | *e = '\0'; | |
238 | v = atoi(s); | |
239 | rdata->version |= v; | |
240 | ||
241 | s = e + 1; | |
242 | e = strchr(s, ' '); | |
243 | if (!e) { | |
244 | break; | |
245 | } | |
246 | *e = '\0'; | |
247 | if (!strcmp(s, "a-culfw")) { | |
248 | rdata->version = 0xffff; | |
249 | } | |
250 | } | |
251 | break; | |
252 | case 'E': | |
253 | { | |
254 | if (!strncmp((char*)buf, "ERR:CCA", 7)) { | |
255 | fprintf(stderr, "CCA didn't complete, too much traffic\n"); | |
256 | } | |
257 | break; | |
258 | } | |
259 | default: | |
260 | fprintf(stderr, "Unknown response from CUL: %s", buf); | |
261 | return 0; | |
262 | break; | |
263 | } | |
264 | ||
265 | return 1; | |
266 | } | |
267 | ||
268 | static int parse_hmuartlgw(enum hmuartlgw_dst dst, uint8_t *buf, int buf_len, void *data) | |
269 | { | |
270 | struct recv_data *rdata = data; | |
271 | ||
272 | if (dst == HMUARTLGW_OS) { | |
273 | switch (rdata->uartlgw_state) { | |
274 | case HMUARTLGW_STATE_GET_FIRMWARE: | |
275 | if (buf[0] == HMUARTLGW_OS_ACK) { | |
276 | rdata->uartlgw_version[0] = buf[5]; | |
277 | rdata->uartlgw_version[1] = buf[6]; | |
278 | rdata->uartlgw_version[2] = buf[7]; | |
279 | rdata->uartlgw_state = HMUARTLGW_STATE_DONE; | |
280 | } | |
281 | break; | |
282 | case HMUARTLGW_STATE_GET_CREDITS: | |
283 | if (buf[0] == HMUARTLGW_OS_ACK) { | |
284 | rdata->credits = buf[2] / 2; | |
285 | rdata->uartlgw_state = HMUARTLGW_STATE_DONE; | |
286 | } | |
287 | break; | |
288 | default: | |
289 | break; | |
290 | } | |
291 | return 0; | |
292 | } | |
293 | ||
294 | switch(buf[0]) { | |
295 | case HMUARTLGW_APP_ACK: | |
296 | if (rdata->uartlgw_state == HMUARTLGW_STATE_GET_HMID) { | |
297 | my_hmid = (buf[4] << 16) | (buf[5] << 8) | buf[6]; | |
298 | } | |
299 | ||
300 | rdata->status = buf[1]; | |
301 | rdata->message_type = MESSAGE_TYPE_R; | |
302 | rdata->uartlgw_state = HMUARTLGW_STATE_ACK_APP; | |
303 | #if 0 | |
304 | hexdump(buf, buf_len, "ACK Status: "); | |
305 | #endif | |
306 | ||
307 | break; | |
308 | case HMUARTLGW_APP_RECV: | |
309 | if ((!hmid) || | |
310 | ((buf[7] == ((hmid >> 16) & 0xff)) && | |
311 | (buf[8] == ((hmid >> 8) & 0xff)) && | |
312 | (buf[9] == (hmid & 0xff)))) { | |
313 | memset(rdata->message, 0, sizeof(rdata->message)); | |
314 | memcpy(rdata->message + 1, buf + 4, buf_len - 4); | |
315 | rdata->message[LEN] = buf_len - 4; | |
316 | rdata->message_type = MESSAGE_TYPE_E; | |
317 | } | |
318 | break; | |
319 | default: | |
320 | break; | |
321 | } | |
322 | ||
323 | return 1; | |
324 | } | |
325 | ||
326 | int send_wait_hmuartlgw(struct hm_dev *dev, struct recv_data *rdata, uint8_t *data, int data_len, | |
327 | enum hmuartlgw_dst dst, enum hmuartlgw_state srcstate, | |
328 | enum hmuartlgw_state dststate) | |
329 | { | |
330 | int cnt = 5; | |
331 | ||
332 | do { | |
333 | rdata->uartlgw_state = srcstate; | |
334 | hmuartlgw_send(dev->hmuartlgw, data, data_len, dst); | |
335 | do { hmuartlgw_poll(dev->hmuartlgw, 500); } while (rdata->uartlgw_state != dststate); | |
336 | if (rdata->status != HMUARTLGW_ACK_EINPROGRESS) | |
337 | break; | |
338 | usleep(200*1000); | |
339 | } while (cnt--); | |
340 | if (rdata->status == HMUARTLGW_ACK_EINPROGRESS) { | |
341 | fprintf(stderr, "IO thinks it is busy, you might have to reset it!\n"); | |
342 | return 0; | |
343 | } | |
344 | ||
345 | return 1; | |
346 | } | |
347 | ||
348 | int send_hm_message(struct hm_dev *dev, struct recv_data *rdata, uint8_t *msg) | |
349 | { | |
350 | static uint32_t id = 1; | |
351 | struct timeval tv; | |
352 | uint8_t out[0x40]; | |
353 | int pfd; | |
354 | ||
355 | switch(dev->type) { | |
356 | case DEVICE_TYPE_HMCFGUSB: | |
357 | if (gettimeofday(&tv, NULL) == -1) { | |
358 | perror("gettimeofay"); | |
359 | return 0; | |
360 | } | |
361 | ||
362 | memset(out, 0, sizeof(out)); | |
363 | ||
364 | out[0] = 'S'; | |
365 | out[1] = (id >> 24) & 0xff; | |
366 | out[2] = (id >> 16) & 0xff; | |
367 | out[3] = (id >> 8) & 0xff; | |
368 | out[4] = id & 0xff; | |
369 | out[10] = 0x01; | |
370 | out[11] = (tv.tv_usec >> 24) & 0xff; | |
371 | out[12] = (tv.tv_usec >> 16) & 0xff; | |
372 | out[13] = (tv.tv_usec >> 8) & 0xff; | |
373 | out[14] = tv.tv_usec & 0xff; | |
374 | ||
375 | memcpy(&out[0x0f], msg, msg[0] + 1); | |
376 | ||
377 | memset(rdata->message, 0, sizeof(rdata->message)); | |
378 | rdata->message_type = 0; | |
379 | hmcfgusb_send(dev->hmcfgusb, out, sizeof(out), 1); | |
380 | ||
381 | while (1) { | |
382 | if (rdata->message_type == MESSAGE_TYPE_R) { | |
383 | if (((rdata->status & 0xdf) == 0x01) || | |
384 | ((rdata->status & 0xdf) == 0x02)) { | |
385 | break; | |
386 | } else { | |
387 | if ((rdata->status & 0xff00) == 0x0400) { | |
388 | fprintf(stderr, "\nOut of credits!\n"); | |
389 | } else if ((rdata->status & 0xff) == 0x08) { | |
390 | fprintf(stderr, "\nMissing ACK!\n"); | |
391 | } else if ((rdata->status & 0xff) == 0x30) { | |
392 | fprintf(stderr, "\nUnknown AES-key requested!\n"); | |
393 | } else { | |
394 | fprintf(stderr, "\nInvalid status: %04x\n", rdata->status); | |
395 | } | |
396 | return 0; | |
397 | } | |
398 | } | |
399 | errno = 0; | |
400 | pfd = hmcfgusb_poll(dev->hmcfgusb, 1000); | |
401 | if ((pfd < 0) && errno) { | |
402 | if (errno != ETIMEDOUT) { | |
403 | perror("\n\nhmcfgusb_poll"); | |
404 | exit(EXIT_FAILURE); | |
405 | } | |
406 | } | |
407 | } | |
408 | break; | |
409 | case DEVICE_TYPE_CULFW: | |
410 | { | |
411 | char buf[256]; | |
412 | int i; | |
413 | ||
414 | memset(buf, 0, sizeof(buf)); | |
415 | buf[0] = 'A'; | |
416 | buf[1] = 's'; | |
417 | for (i = 0; i < msg[0] + 1; i++) { | |
418 | buf[2 + (i * 2)] = nibble_to_ascii((msg[i] >> 4) & 0xf); | |
419 | buf[2 + (i * 2) + 1] = nibble_to_ascii(msg[i] & 0xf); | |
420 | } | |
421 | buf[2 + (i * 2) ] = '\r'; | |
422 | buf[2 + (i * 2) + 1] = '\n'; | |
423 | ||
424 | memset(rdata->message, 0, sizeof(rdata->message)); | |
425 | rdata->message_type = 0; | |
426 | if (culfw_send(dev->culfw, buf, 2 + (i * 2) + 1) == 0) { | |
427 | fprintf(stderr, "culfw_send failed!\n"); | |
428 | exit(EXIT_FAILURE); | |
429 | } | |
430 | ||
431 | /* Wait for TSCUL to ACK send */ | |
432 | if (rdata->is_TSCUL) { | |
433 | do { | |
434 | errno = 0; | |
435 | pfd = culfw_poll(dev->culfw, 200); | |
436 | if ((pfd < 0) && errno) { | |
437 | if (errno != ETIMEDOUT) { | |
438 | perror("\n\nculfw_poll"); | |
439 | exit(EXIT_FAILURE); | |
440 | } | |
441 | } | |
442 | } while (rdata->message_type != MESSAGE_TYPE_B); | |
443 | } | |
444 | ||
445 | if (msg[CTL] & 0x20) { | |
446 | int cnt = 5; | |
447 | int pfd; | |
448 | do { | |
449 | errno = 0; | |
450 | pfd = culfw_poll(dev->culfw, 200); | |
451 | if ((pfd < 0) && errno) { | |
452 | if (errno != ETIMEDOUT) { | |
453 | perror("\n\nculfw_poll"); | |
454 | exit(EXIT_FAILURE); | |
455 | } | |
456 | } | |
457 | if (rdata->message_type == MESSAGE_TYPE_E) { | |
458 | if (rdata->message[TYPE] == 0x02) { | |
459 | if (rdata->message[PAYLOAD] == 0x04) { | |
460 | int32_t req_kNo; | |
461 | uint8_t challenge[6]; | |
462 | uint8_t respbuf[16]; | |
463 | uint8_t *resp; | |
464 | ||
465 | if (rdata->is_TSCUL) { | |
466 | printf("AES handled by TSCUL\n"); | |
467 | break; | |
468 | } | |
469 | ||
470 | req_kNo = rdata->message[rdata->message[LEN]] / 2; | |
471 | memcpy(challenge, &(rdata->message[PAYLOAD+1]), 6); | |
472 | ||
473 | if (req_kNo != kNo) { | |
474 | fprintf(stderr, "AES request for unknown key %d!\n", req_kNo); | |
475 | } else { | |
476 | resp = hm_sign(key, challenge, msg, NULL, respbuf); | |
477 | if (resp) { | |
478 | uint8_t rbuf[64]; | |
479 | ||
480 | memset(rbuf, 0, sizeof(rbuf)); | |
481 | rbuf[MSGID] = rdata->message[MSGID]; | |
482 | rbuf[CTL] = rdata->message[CTL]; | |
483 | rbuf[TYPE] = 0x03; | |
484 | SET_SRC(rbuf, DST(rdata->message)); | |
485 | SET_DST(rbuf, SRC(rdata->message)); | |
486 | memcpy(&(rbuf[PAYLOAD]), resp, 16); | |
487 | SET_LEN_FROM_PAYLOADLEN(rbuf, 16); | |
488 | ||
489 | usleep(110000); /* Determined by a fair dice roll */ | |
490 | return send_hm_message(dev, rdata, rbuf); | |
491 | } | |
492 | } | |
493 | } else if (rdata->message[PAYLOAD] >= 0x80 && rdata->message[PAYLOAD] <= 0x8f) { | |
494 | fprintf(stderr, "NACK\n"); | |
495 | } else { /* ACK or ACKinfo */ | |
496 | break; | |
497 | } | |
498 | } else { | |
499 | fprintf(stderr, "Unexpected message received: "); | |
500 | for (i = 0; i < rdata->message[LEN]; i++) { | |
501 | fprintf(stderr, "%02x", rdata->message[i+1]); | |
502 | } | |
503 | fprintf(stderr, "\n"); | |
504 | } | |
505 | } | |
506 | } while(cnt--); | |
507 | ||
508 | if (cnt == -1) { | |
509 | fprintf(stderr, "\nMissing ACK!\n"); | |
510 | return 0; | |
511 | } | |
512 | } | |
513 | ||
514 | /* Delay for non-TSCUL */ | |
515 | if (!rdata->is_TSCUL) { | |
516 | usleep(50*1000); | |
517 | } | |
518 | } | |
519 | break; | |
520 | case DEVICE_TYPE_HMUARTLGW: | |
521 | memset(out, 0, sizeof(out)); | |
522 | ||
523 | out[0] = HMUARTLGW_APP_SEND; | |
524 | out[1] = 0x00; | |
525 | out[2] = 0x00; | |
526 | out[3] = (msg[CTL] & 0x10) ? 0x01 : 0x00; /* Burst?! */ | |
527 | memcpy(&out[4], &msg[1], msg[0]); | |
528 | ||
529 | memset(rdata->message, 0, sizeof(rdata->message)); | |
530 | rdata->message_type = 0; | |
531 | hmuartlgw_send(dev->hmuartlgw, out, msg[0] + 4, HMUARTLGW_APP); | |
532 | ||
533 | while (1) { | |
534 | if (rdata->message_type == MESSAGE_TYPE_R) { | |
535 | if ((rdata->status == 0x02) || | |
536 | (rdata->status == 0x03) || | |
537 | (rdata->status == 0x0c)) { | |
538 | break; | |
539 | } else { | |
540 | if (rdata->status == 0x0d) { | |
541 | fprintf(stderr, "\nAES handshake failed!\n"); | |
542 | } else if (rdata->status == 0x04 || rdata->status == 0x06) { | |
543 | fprintf(stderr, "\nMissing ACK!\n"); | |
544 | } else { | |
545 | fprintf(stderr, "\nInvalid status: %04x\n", rdata->status); | |
546 | } | |
547 | return 0; | |
548 | } | |
549 | } | |
550 | errno = 0; | |
551 | pfd = hmuartlgw_poll(dev->hmuartlgw, 1000); | |
552 | if ((pfd < 0) && errno) { | |
553 | if (errno != ETIMEDOUT) { | |
554 | perror("\n\nhmcfgusb_poll"); | |
555 | exit(EXIT_FAILURE); | |
556 | } | |
557 | } | |
558 | } | |
559 | break; | |
560 | } | |
561 | ||
562 | id++; | |
563 | return 1; | |
564 | } | |
565 | ||
566 | static int switch_speed(struct hm_dev *dev, struct recv_data *rdata, uint8_t speed) | |
567 | { | |
568 | uint8_t out[0x40]; | |
569 | int pfd; | |
570 | ||
571 | printf("Entering %uk-mode\n", speed); | |
572 | ||
573 | switch(dev->type) { | |
574 | case DEVICE_TYPE_HMCFGUSB: | |
575 | memset(out, 0, sizeof(out)); | |
576 | out[0] = 'G'; | |
577 | out[1] = speed; | |
578 | ||
579 | hmcfgusb_send(dev->hmcfgusb, out, sizeof(out), 1); | |
580 | ||
581 | while (1) { | |
582 | errno = 0; | |
583 | pfd = hmcfgusb_poll(dev->hmcfgusb, 1000); | |
584 | if ((pfd < 0) && errno) { | |
585 | if (errno != ETIMEDOUT) { | |
586 | perror("\n\nhmcfgusb_poll"); | |
587 | exit(EXIT_FAILURE); | |
588 | } | |
589 | } | |
590 | if (rdata->speed == speed) | |
591 | break; | |
592 | } | |
593 | break; | |
594 | case DEVICE_TYPE_CULFW: | |
595 | if (speed == 100) { | |
596 | return culfw_send(dev->culfw, "AR\r\n", 4); | |
597 | } else { | |
598 | return culfw_send(dev->culfw, "Ar\r\n", 4); | |
599 | } | |
600 | break; | |
601 | case DEVICE_TYPE_HMUARTLGW: | |
602 | if (speed == 100) { | |
603 | out[0] = HMUARTLGW_OS_UPDATE_MODE; | |
604 | out[1] = 0xe9; | |
605 | out[2] = 0xca; | |
606 | hmuartlgw_send(dev->hmuartlgw, out, 3, HMUARTLGW_OS); | |
607 | } else { | |
608 | out[0] = HMUARTLGW_OS_NORMAL_MODE; | |
609 | hmuartlgw_send(dev->hmuartlgw, out, 1, HMUARTLGW_OS); | |
610 | } | |
611 | break; | |
612 | } | |
613 | ||
614 | return 1; | |
615 | } | |
616 | ||
617 | void flash_ota_syntax(char *prog) | |
618 | { | |
619 | fprintf(stderr, "Syntax: %s parameters options\n\n", prog); | |
620 | fprintf(stderr, "Mandatory parameters:\n"); | |
621 | fprintf(stderr, "\t-f firmware.eq3\tfirmware file to flash\n"); | |
622 | fprintf(stderr, "\t-s SERIAL\tserial of device to flash (optional when using -D)\n"); | |
623 | fprintf(stderr, "\nOptional parameters:\n"); | |
624 | fprintf(stderr, "\t-c device\tenable CUL-mode with CUL at path \"device\"\n"); | |
625 | fprintf(stderr, "\t-b bps\t\tuse CUL with speed \"bps\" (default: %u)\n", DEFAULT_CUL_BPS); | |
626 | fprintf(stderr, "\t-l\t\tlower payloadlen (required for devices with little RAM, e.g. CUL v2 and CUL v4)\n"); | |
627 | fprintf(stderr, "\t-S serial\tuse HM-CFG-USB with given serial\n"); | |
628 | fprintf(stderr, "\t-U device\tuse HM-MOD-UART on given device\n"); | |
629 | fprintf(stderr, "\t-h\t\tthis help\n"); | |
630 | fprintf(stderr, "\nOptional parameters for automatically sending device to bootloader\n"); | |
631 | fprintf(stderr, "\t-C\t\tHMID of central (3 hex-bytes, no prefix, e.g. ABCDEF)\n"); | |
632 | fprintf(stderr, "\t-D\t\tHMID of device (3 hex-bytes, no prefix, e.g. 123456)\n"); | |
633 | fprintf(stderr, "\t-K\t\tKNO:KEY AES key-number and key (hex) separated by colon (Fhem hmKey attribute)\n"); | |
634 | } | |
635 | ||
636 | int main(int argc, char **argv) | |
637 | { | |
638 | const char twiddlie[] = { '-', '\\', '|', '/' }; | |
639 | const uint8_t cc1101_regs[] = { 0x10, 0x5B, 0x11, 0xF8, 0x15, 0x47 }; | |
640 | char *fw_file = NULL; | |
641 | char *serial = NULL; | |
642 | char *culfw_dev = NULL; | |
643 | char *endptr = NULL; | |
644 | unsigned int bps = DEFAULT_CUL_BPS; | |
645 | struct hm_dev dev; | |
646 | struct recv_data rdata; | |
647 | uint8_t out[0x40]; | |
648 | uint8_t *pos; | |
649 | uint8_t msgid = 0x1; | |
650 | uint16_t len; | |
651 | struct firmware *fw; | |
652 | char *hmcfgusb_serial = NULL; | |
653 | char *uart = NULL; | |
654 | int block; | |
655 | int pfd; | |
656 | int debug = 0; | |
657 | int cnt; | |
658 | int switchcnt = 0; | |
659 | int msgnum = 0; | |
660 | int switched = 0; | |
661 | int opt; | |
662 | ||
663 | printf("HomeMatic OTA flasher version " VERSION "\n\n"); | |
664 | ||
665 | while((opt = getopt(argc, argv, "b:c:f:hls:C:D:K:S:U:")) != -1) { | |
666 | switch (opt) { | |
667 | case 'b': | |
668 | bps = atoi(optarg); | |
669 | break; | |
670 | case 'c': | |
671 | culfw_dev = optarg; | |
672 | break; | |
673 | case 'f': | |
674 | fw_file = optarg; | |
675 | break; | |
676 | case 'l': | |
677 | printf("Reducing payload-len from %d to %d\n", max_payloadlen, LOWER_MAX_PAYLOAD); | |
678 | max_payloadlen = LOWER_MAX_PAYLOAD; | |
679 | break; | |
680 | case 's': | |
681 | serial = optarg; | |
682 | break; | |
683 | case 'C': | |
684 | my_hmid = strtoul(optarg, &endptr, 16); | |
685 | if (*endptr != '\0') { | |
686 | fprintf(stderr, "Invalid central HMID!\n\n"); | |
687 | flash_ota_syntax(argv[0]); | |
688 | exit(EXIT_FAILURE); | |
689 | } | |
690 | break; | |
691 | case 'D': | |
692 | hmid = strtoul(optarg, &endptr, 16); | |
693 | if (*endptr != '\0') { | |
694 | fprintf(stderr, "Invalid device HMID!\n\n"); | |
695 | flash_ota_syntax(argv[0]); | |
696 | exit(EXIT_FAILURE); | |
697 | } | |
698 | break; | |
699 | case 'K': | |
700 | kNo = strtoul(optarg, &endptr, 10); | |
701 | if (*endptr != ':') { | |
702 | fprintf(stderr, "Invalid key number!\n\n"); | |
703 | flash_ota_syntax(argv[0]); | |
704 | exit(EXIT_FAILURE); | |
705 | } | |
706 | endptr++; | |
707 | for (cnt = 0; cnt < 16; cnt++) { | |
708 | if (*endptr == '\0' || *(endptr+1) == '\0' || | |
709 | !validate_nibble(*endptr) || | |
710 | !validate_nibble(*(endptr+1))) { | |
711 | fprintf(stderr, "Invalid key!\n\n"); | |
712 | flash_ota_syntax(argv[0]); | |
713 | exit(EXIT_FAILURE); | |
714 | } | |
715 | key[cnt] = ascii_to_nibble(*endptr) << 4 | ascii_to_nibble(*(endptr+1)); | |
716 | endptr += 2; | |
717 | } | |
718 | break; | |
719 | case 'S': | |
720 | hmcfgusb_serial = optarg; | |
721 | break; | |
722 | case 'U': | |
723 | uart = optarg; | |
724 | break; | |
725 | case 'h': | |
726 | case ':': | |
727 | case '?': | |
728 | default: | |
729 | flash_ota_syntax(argv[0]); | |
730 | exit(EXIT_FAILURE); | |
731 | break; | |
732 | ||
733 | } | |
734 | } | |
735 | ||
736 | if (!fw_file || (!serial && !hmid)) { | |
737 | flash_ota_syntax(argv[0]); | |
738 | exit(EXIT_FAILURE); | |
739 | } | |
740 | ||
741 | fw = firmware_read_firmware(fw_file, debug); | |
742 | if (!fw) | |
743 | exit(EXIT_FAILURE); | |
744 | ||
745 | memset(&rdata, 0, sizeof(rdata)); | |
746 | memset(&dev, 0, sizeof(struct hm_dev)); | |
747 | ||
748 | if (culfw_dev) { | |
749 | printf("Opening culfw-device at path %s with speed %u\n", culfw_dev, bps); | |
750 | dev.culfw = culfw_init(culfw_dev, bps, parse_culfw, &rdata); | |
751 | if (!dev.culfw) { | |
752 | fprintf(stderr, "Can't initialize CUL at %s with rate %u\n", culfw_dev, bps); | |
753 | exit(EXIT_FAILURE); | |
754 | } | |
755 | dev.type = DEVICE_TYPE_CULFW; | |
756 | ||
757 | printf("Requesting firmware version\n"); | |
758 | culfw_send(dev.culfw, "\r\n", 2); | |
759 | culfw_flush(dev.culfw); | |
760 | ||
761 | while (1) { | |
762 | culfw_send(dev.culfw, "V\r\n", 3); | |
763 | ||
764 | errno = 0; | |
765 | pfd = culfw_poll(dev.culfw, 1000); | |
766 | if ((pfd < 0) && errno) { | |
767 | if (errno != ETIMEDOUT) { | |
768 | perror("\n\nhmcfgusb_poll"); | |
769 | exit(EXIT_FAILURE); | |
770 | } | |
771 | } | |
772 | if (rdata.version) | |
773 | break; | |
774 | } | |
775 | ||
776 | printf("culfw-device firmware version: "); | |
777 | if (rdata.version != 0xffff) { | |
778 | printf("%u.%02u\n", | |
779 | (rdata.version >> 8) & 0xff, | |
780 | rdata.version & 0xff); | |
781 | } else { | |
782 | if (rdata.is_TSCUL) { | |
783 | culfw_send(dev.culfw, "At1\r\n", 5); // tsculfw: try switch on timestamp protocol | |
784 | printf("tsculfw\n"); | |
785 | culfw_flush(dev.culfw); | |
786 | culfw_send(dev.culfw, "ApTiMeStAmP\r\n", 13); // tsculfw: send ping to get credits info | |
787 | pfd = culfw_poll(dev.culfw, 1000); | |
788 | if ((pfd < 0) && errno) { | |
789 | if (errno != ETIMEDOUT) { | |
790 | perror("\n\nhmcfgusb_poll"); | |
791 | exit(EXIT_FAILURE); | |
792 | } | |
793 | } | |
794 | if (rdata.credits) { // tsculfw: maximum credits available? | |
795 | fprintf(stderr, "\n\ntsculfw does not report full credits, try again later\n"); | |
796 | exit(EXIT_FAILURE); | |
797 | } | |
798 | ||
799 | if (kNo > 0) { | |
800 | char keybuf[64] = { 0 }; | |
801 | int i; | |
802 | ||
803 | printf("Setting AES-key\n"); | |
804 | snprintf(keybuf, sizeof(keybuf) - 1, "Ak%02x", kNo - 1); | |
805 | ||
806 | for (i = 0; i < 16; i++) { | |
807 | keybuf[4 + (i * 2)] = nibble_to_ascii((key[i] >> 4) & 0xf); | |
808 | keybuf[4 + (i * 2) + 1] = nibble_to_ascii(key[i] & 0xf); | |
809 | } | |
810 | keybuf[4 + (i * 2) ] = '\r'; | |
811 | keybuf[4 + (i * 2) + 1] = '\n'; | |
812 | culfw_send(dev.culfw, keybuf, strlen(keybuf)); // tsculfw: send ping to get credits info | |
813 | pfd = culfw_poll(dev.culfw, 1000); | |
814 | if ((pfd < 0) && errno) { | |
815 | if (errno != ETIMEDOUT) { | |
816 | perror("\n\nhmcfgusb_poll"); | |
817 | exit(EXIT_FAILURE); | |
818 | } | |
819 | } | |
820 | } | |
821 | } | |
822 | else { | |
823 | printf("a-culfw\n"); | |
824 | } | |
825 | } | |
826 | ||
827 | if (rdata.version < 0x013a) { | |
828 | fprintf(stderr, "\nThis version does _not_ support firmware upgrade mode, you need at least 1.58!\n"); | |
829 | exit(EXIT_FAILURE); | |
830 | } | |
831 | } else if (uart) { | |
832 | uint32_t new_hmid = my_hmid; | |
833 | ||
834 | hmuartlgw_set_debug(debug); | |
835 | ||
836 | dev.hmuartlgw = hmuart_init(uart, parse_hmuartlgw, &rdata, 1); | |
837 | if (!dev.hmuartlgw) { | |
838 | fprintf(stderr, "Can't initialize HM-MOD-UART\n"); | |
839 | exit(EXIT_FAILURE); | |
840 | } | |
841 | dev.type = DEVICE_TYPE_HMUARTLGW; | |
842 | ||
843 | out[0] = HMUARTLGW_APP_GET_HMID; | |
844 | send_wait_hmuartlgw(&dev, &rdata, out, 1, HMUARTLGW_APP, HMUARTLGW_STATE_GET_HMID, HMUARTLGW_STATE_ACK_APP); | |
845 | ||
846 | out[0] = HMUARTLGW_OS_GET_FIRMWARE; | |
847 | send_wait_hmuartlgw(&dev, &rdata, out, 1, HMUARTLGW_OS, HMUARTLGW_STATE_GET_FIRMWARE, HMUARTLGW_STATE_DONE); | |
848 | ||
849 | out[0] = HMUARTLGW_OS_GET_CREDITS; | |
850 | send_wait_hmuartlgw(&dev, &rdata, out, 1, HMUARTLGW_OS, HMUARTLGW_STATE_GET_CREDITS, HMUARTLGW_STATE_DONE); | |
851 | ||
852 | printf("HM-MOD-UART firmware version: %u.%u.%u, used credits: %u%%\n", | |
853 | rdata.uartlgw_version[0], | |
854 | rdata.uartlgw_version[1], | |
855 | rdata.uartlgw_version[2], | |
856 | rdata.credits); | |
857 | ||
858 | if (rdata.credits >= 40) { | |
859 | printf("\nRebooting HM-MOD-UART to avoid running out of credits\n"); | |
860 | ||
861 | hmuartlgw_enter_bootloader(dev.hmuartlgw); | |
862 | hmuartlgw_enter_app(dev.hmuartlgw); | |
863 | } | |
864 | ||
865 | printf("\nHM-MOD-UART opened\n\n"); | |
866 | ||
867 | if (new_hmid && (my_hmid != new_hmid)) { | |
868 | printf("Changing hmid from %06x to %06x\n", my_hmid, new_hmid); | |
869 | ||
870 | out[0] = HMUARTLGW_APP_SET_HMID; | |
871 | out[1] = (new_hmid >> 16) & 0xff; | |
872 | out[2] = (new_hmid >> 8) & 0xff; | |
873 | out[3] = new_hmid & 0xff; | |
874 | send_wait_hmuartlgw(&dev, &rdata, out, 4, HMUARTLGW_APP, HMUARTLGW_STATE_WAIT_APP, HMUARTLGW_STATE_ACK_APP); | |
875 | ||
876 | my_hmid = new_hmid; | |
877 | } | |
878 | ||
879 | if (kNo > 0) { | |
880 | printf("Setting AES-key\n"); | |
881 | ||
882 | memset(out, 0, sizeof(out)); | |
883 | out[0] = HMUARTLGW_APP_SET_CURRENT_KEY; | |
884 | memcpy(&(out[1]), key, 16); | |
885 | out[17] = kNo; | |
886 | send_wait_hmuartlgw(&dev, &rdata, out, 18, HMUARTLGW_APP, HMUARTLGW_STATE_WAIT_APP, HMUARTLGW_STATE_ACK_APP); | |
887 | ||
888 | memset(out, 0, sizeof(out)); | |
889 | out[0] = HMUARTLGW_APP_SET_OLD_KEY; | |
890 | memcpy(&(out[1]), key, 16); | |
891 | out[17] = kNo; | |
892 | send_wait_hmuartlgw(&dev, &rdata, out, 18, HMUARTLGW_APP, HMUARTLGW_STATE_WAIT_APP, HMUARTLGW_STATE_ACK_APP); | |
893 | } | |
894 | } else { | |
895 | uint32_t new_hmid = my_hmid; | |
896 | ||
897 | hmcfgusb_set_debug(debug); | |
898 | ||
899 | dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata, hmcfgusb_serial); | |
900 | if (!dev.hmcfgusb) { | |
901 | fprintf(stderr, "Can't initialize HM-CFG-USB\n"); | |
902 | exit(EXIT_FAILURE); | |
903 | } | |
904 | dev.type = DEVICE_TYPE_HMCFGUSB; | |
905 | ||
906 | memset(out, 0, sizeof(out)); | |
907 | out[0] = 'K'; | |
908 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
909 | ||
910 | while (1) { | |
911 | errno = 0; | |
912 | pfd = hmcfgusb_poll(dev.hmcfgusb, 1000); | |
913 | if ((pfd < 0) && errno) { | |
914 | if (errno != ETIMEDOUT) { | |
915 | perror("\n\nhmcfgusb_poll"); | |
916 | exit(EXIT_FAILURE); | |
917 | } | |
918 | } | |
919 | if (rdata.version) | |
920 | break; | |
921 | } | |
922 | ||
923 | if (rdata.version < 0x3c7) { | |
924 | fprintf(stderr, "HM-CFG-USB firmware too low: %u < 967\n", rdata.version); | |
925 | exit(EXIT_FAILURE); | |
926 | } | |
927 | ||
928 | printf("HM-CFG-USB firmware version: %u, used credits: %u%%\n", rdata.version, rdata.credits); | |
929 | ||
930 | if (rdata.credits >= 40) { | |
931 | printf("\nRebooting HM-CFG-USB to avoid running out of credits\n\n"); | |
932 | ||
933 | if (!dev.hmcfgusb->bootloader) { | |
934 | printf("HM-CFG-USB not in bootloader mode, entering bootloader.\n"); | |
935 | printf("Waiting for device to reappear...\n"); | |
936 | ||
937 | do { | |
938 | if (dev.hmcfgusb) { | |
939 | if (!dev.hmcfgusb->bootloader) | |
940 | hmcfgusb_enter_bootloader(dev.hmcfgusb); | |
941 | hmcfgusb_close(dev.hmcfgusb); | |
942 | } | |
943 | sleep(1); | |
944 | } while (((dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata, hmcfgusb_serial)) == NULL) || (!dev.hmcfgusb->bootloader)); | |
945 | } | |
946 | ||
947 | if (dev.hmcfgusb->bootloader) { | |
948 | printf("HM-CFG-USB in bootloader mode, rebooting\n"); | |
949 | ||
950 | do { | |
951 | if (dev.hmcfgusb) { | |
952 | if (dev.hmcfgusb->bootloader) | |
953 | hmcfgusb_leave_bootloader(dev.hmcfgusb); | |
954 | hmcfgusb_close(dev.hmcfgusb); | |
955 | } | |
956 | sleep(1); | |
957 | } while (((dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata, hmcfgusb_serial)) == NULL) || (dev.hmcfgusb->bootloader)); | |
958 | } | |
959 | } | |
960 | ||
961 | printf("\n\nHM-CFG-USB opened\n\n"); | |
962 | ||
963 | if (new_hmid && (my_hmid != new_hmid)) { | |
964 | printf("Changing hmid from %06x to %06x\n", my_hmid, new_hmid); | |
965 | ||
966 | memset(out, 0, sizeof(out)); | |
967 | out[0] = 'A'; | |
968 | out[1] = (new_hmid >> 16) & 0xff; | |
969 | out[2] = (new_hmid >> 8) & 0xff; | |
970 | out[3] = new_hmid & 0xff; | |
971 | ||
972 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
973 | ||
974 | my_hmid = new_hmid; | |
975 | } | |
976 | ||
977 | if (kNo > 0) { | |
978 | printf("Setting AES-key\n"); | |
979 | ||
980 | memset(out, 0, sizeof(out)); | |
981 | out[0] = 'Y'; | |
982 | out[1] = 0x01; | |
983 | out[2] = kNo; | |
984 | out[3] = sizeof(key); | |
985 | memcpy(&(out[4]), key, sizeof(key)); | |
986 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
987 | ||
988 | memset(out, 0, sizeof(out)); | |
989 | out[0] = 'Y'; | |
990 | out[1] = 0x02; | |
991 | out[2] = 0x00; | |
992 | out[3] = 0x00; | |
993 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
994 | ||
995 | memset(out, 0, sizeof(out)); | |
996 | out[0] = 'Y'; | |
997 | out[1] = 0x03; | |
998 | out[2] = 0x00; | |
999 | out[3] = 0x00; | |
1000 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | if (!switch_speed(&dev, &rdata, 10)) { | |
1005 | fprintf(stderr, "Can't switch speed!\n"); | |
1006 | exit(EXIT_FAILURE); | |
1007 | } | |
1008 | ||
1009 | if (hmid && my_hmid) { | |
1010 | switch (dev.type) { | |
1011 | case DEVICE_TYPE_HMCFGUSB: | |
1012 | printf("Adding HMID\n"); | |
1013 | ||
1014 | memset(out, 0, sizeof(out)); | |
1015 | out[0] = '+'; | |
1016 | out[1] = (hmid >> 16) & 0xff; | |
1017 | out[2] = (hmid >> 8) & 0xff; | |
1018 | out[3] = hmid & 0xff; | |
1019 | ||
1020 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
1021 | break; | |
1022 | case DEVICE_TYPE_HMUARTLGW: | |
1023 | printf("Adding HMID\n"); | |
1024 | ||
1025 | memset(out, 0, sizeof(out)); | |
1026 | out[0] = HMUARTLGW_APP_ADD_PEER; | |
1027 | out[1] = (hmid >> 16) & 0xff; | |
1028 | out[2] = (hmid >> 8) & 0xff; | |
1029 | out[3] = hmid & 0xff; | |
1030 | out[4] = (kNo > 0) ? kNo : 0x00; /* KeyIndex */ | |
1031 | out[5] = 0x00; /* WakeUp? */ | |
1032 | out[6] = 0x00; /* WakeUp? */ | |
1033 | ||
1034 | send_wait_hmuartlgw(&dev, &rdata, out, 7, HMUARTLGW_APP, HMUARTLGW_STATE_WAIT_APP, HMUARTLGW_STATE_ACK_APP); | |
1035 | ||
1036 | break; | |
1037 | } | |
1038 | printf("Sending device with hmid %06x to bootloader\n", hmid); | |
1039 | out[CTL] = 0x30; | |
1040 | out[TYPE] = 0x11; | |
1041 | SET_SRC(out, my_hmid); | |
1042 | SET_DST(out, hmid); | |
1043 | out[PAYLOAD] = 0xCA; | |
1044 | SET_LEN_FROM_PAYLOADLEN(out, 1); | |
1045 | ||
1046 | cnt = 3; | |
1047 | do { | |
1048 | out[MSGID] = msgid++; | |
1049 | if (send_hm_message(&dev, &rdata, out)) { | |
1050 | break; | |
1051 | } | |
1052 | } while (cnt--); | |
1053 | if (cnt == -1) { | |
1054 | printf("Failed to send device to bootloader, please enter bootloader manually.\n"); | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | if (serial) { | |
1059 | printf("Waiting for device with serial %s\n", serial); | |
1060 | } else { | |
1061 | printf("Waiting for device with HMID %06x\n", hmid); | |
1062 | } | |
1063 | ||
1064 | while (1) { | |
1065 | errno = 0; | |
1066 | switch (dev.type) { | |
1067 | case DEVICE_TYPE_CULFW: | |
1068 | pfd = culfw_poll(dev.culfw, 1000); | |
1069 | break; | |
1070 | case DEVICE_TYPE_HMCFGUSB: | |
1071 | pfd = hmcfgusb_poll(dev.hmcfgusb, 1000); | |
1072 | break; | |
1073 | case DEVICE_TYPE_HMUARTLGW: | |
1074 | pfd = hmuartlgw_poll(dev.hmuartlgw, 1000); | |
1075 | break; | |
1076 | default: | |
1077 | pfd = -1; | |
1078 | break; | |
1079 | } | |
1080 | ||
1081 | if ((pfd < 0) && errno) { | |
1082 | if (errno != ETIMEDOUT) { | |
1083 | perror("\n\npoll"); | |
1084 | exit(EXIT_FAILURE); | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | if ((rdata.message[LEN] == 0x14) && /* Length */ | |
1089 | (rdata.message[MSGID] == 0x00) && /* Message ID */ | |
1090 | (rdata.message[CTL] == 0x00) && /* Control Byte */ | |
1091 | (rdata.message[TYPE] == 0x10) && /* Messagte type: Information */ | |
1092 | (DST(rdata.message) == 0x000000) && /* Broadcast */ | |
1093 | (rdata.message[PAYLOAD] == 0x00)) { /* FUP? */ | |
1094 | if (serial && !strncmp((char*)&(rdata.message[0x0b]), serial, 10)) { | |
1095 | hmid = SRC(rdata.message); | |
1096 | break; | |
1097 | } else if (!serial && SRC(rdata.message) == hmid) { | |
1098 | serial = (char*)&(rdata.message[0x0b]); | |
1099 | break; | |
1100 | } | |
1101 | } | |
1102 | } | |
1103 | ||
1104 | printf("Device with serial %s (HMID: %06x) entered firmware-update-mode\n", serial, hmid); | |
1105 | ||
1106 | switch (dev.type) { | |
1107 | case DEVICE_TYPE_HMCFGUSB: | |
1108 | printf("Adding HMID\n"); | |
1109 | ||
1110 | memset(out, 0, sizeof(out)); | |
1111 | out[0] = '+'; | |
1112 | out[1] = (hmid >> 16) & 0xff; | |
1113 | out[2] = (hmid >> 8) & 0xff; | |
1114 | out[3] = hmid & 0xff; | |
1115 | ||
1116 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
1117 | break; | |
1118 | case DEVICE_TYPE_HMUARTLGW: | |
1119 | printf("Adding HMID\n"); | |
1120 | ||
1121 | memset(out, 0, sizeof(out)); | |
1122 | out[0] = HMUARTLGW_APP_ADD_PEER; | |
1123 | out[1] = (hmid >> 16) & 0xff; | |
1124 | out[2] = (hmid >> 8) & 0xff; | |
1125 | out[3] = hmid & 0xff; | |
1126 | out[4] = 0x00; /* KeyIndex */ | |
1127 | out[5] = 0x00; /* WakeUp? */ | |
1128 | out[6] = 0x00; /* WakeUp? */ | |
1129 | ||
1130 | send_wait_hmuartlgw(&dev, &rdata, out, 7, HMUARTLGW_APP, HMUARTLGW_STATE_WAIT_APP, HMUARTLGW_STATE_ACK_APP); | |
1131 | ||
1132 | break; | |
1133 | } | |
1134 | ||
1135 | switchcnt = 3; | |
1136 | do { | |
1137 | printf("Initiating remote switch to 100k\n"); | |
1138 | ||
1139 | memset(out, 0, sizeof(out)); | |
1140 | ||
1141 | out[MSGID] = msgid++; | |
1142 | out[CTL] = 0x00; | |
1143 | out[TYPE] = 0xCB; | |
1144 | SET_SRC(out, my_hmid); | |
1145 | SET_DST(out, hmid); | |
1146 | ||
1147 | memcpy(&out[PAYLOAD], cc1101_regs, sizeof(cc1101_regs)); | |
1148 | SET_LEN_FROM_PAYLOADLEN(out, sizeof(cc1101_regs)); | |
1149 | ||
1150 | if (!send_hm_message(&dev, &rdata, out)) { | |
1151 | exit(EXIT_FAILURE); | |
1152 | } | |
1153 | ||
1154 | if (!switch_speed(&dev, &rdata, 100)) { | |
1155 | fprintf(stderr, "Can't switch speed!\n"); | |
1156 | exit(EXIT_FAILURE); | |
1157 | } | |
1158 | ||
1159 | printf("Has the device switched?\n"); | |
1160 | ||
1161 | memset(out, 0, sizeof(out)); | |
1162 | ||
1163 | out[MSGID] = msgid++; | |
1164 | out[CTL] = 0x20; | |
1165 | out[TYPE] = 0xCB; | |
1166 | SET_SRC(out, my_hmid); | |
1167 | SET_DST(out, hmid); | |
1168 | ||
1169 | memcpy(&out[PAYLOAD], cc1101_regs, sizeof(cc1101_regs)); | |
1170 | SET_LEN_FROM_PAYLOADLEN(out, sizeof(cc1101_regs)); | |
1171 | ||
1172 | cnt = 3; | |
1173 | do { | |
1174 | if (send_hm_message(&dev, &rdata, out)) { | |
1175 | /* A0A02000221B9AD00000000 */ | |
1176 | switched = 1; | |
1177 | break; | |
1178 | } | |
1179 | } while (cnt--); | |
1180 | ||
1181 | if (!switched) { | |
1182 | printf("No!\n"); | |
1183 | ||
1184 | if (!switch_speed(&dev, &rdata, 10)) { | |
1185 | fprintf(stderr, "Can't switch speed!\n"); | |
1186 | exit(EXIT_FAILURE); | |
1187 | } | |
1188 | } | |
1189 | } while ((!switched) && (switchcnt--)); | |
1190 | ||
1191 | if (!switched) { | |
1192 | fprintf(stderr, "Too many errors, giving up!\n"); | |
1193 | exit(EXIT_FAILURE); | |
1194 | } | |
1195 | ||
1196 | printf("Yes!\n"); | |
1197 | ||
1198 | printf("Flashing %d blocks", fw->fw_blocks); | |
1199 | if (debug) { | |
1200 | printf("\n"); | |
1201 | } else { | |
1202 | printf(": %04u/%04u %c", 0, fw->fw_blocks, twiddlie[0]); | |
1203 | fflush(stdout); | |
1204 | } | |
1205 | ||
1206 | for (block = 0; block < fw->fw_blocks; block++) { | |
1207 | int first; | |
1208 | ||
1209 | len = fw->fw[block][2] << 8; | |
1210 | len |= fw->fw[block][3]; | |
1211 | ||
1212 | pos = &(fw->fw[block][2]); | |
1213 | ||
1214 | len += 2; /* length */ | |
1215 | ||
1216 | if (debug) | |
1217 | hexdump(pos, len, "F> "); | |
1218 | ||
1219 | first = 1; | |
1220 | cnt = 0; | |
1221 | do { | |
1222 | int payloadlen = max_payloadlen - 2; | |
1223 | int ack = 0; | |
1224 | ||
1225 | if (first) { | |
1226 | payloadlen = max_payloadlen; | |
1227 | first = 0; | |
1228 | } | |
1229 | ||
1230 | if ((len - (pos - &(fw->fw[block][2]))) < payloadlen) | |
1231 | payloadlen = (len - (pos - &(fw->fw[block][2]))); | |
1232 | ||
1233 | if (((pos + payloadlen) - &(fw->fw[block][2])) == len) | |
1234 | ack = 1; | |
1235 | ||
1236 | memset(rdata.message, 0, sizeof(rdata.message)); | |
1237 | rdata.message_type = 0; | |
1238 | ||
1239 | memset(out, 0, sizeof(out)); | |
1240 | ||
1241 | out[MSGID] = msgid; | |
1242 | if (ack) | |
1243 | out[CTL] = 0x20; | |
1244 | out[TYPE] = 0xCA; | |
1245 | SET_SRC(out, my_hmid); | |
1246 | SET_DST(out, hmid); | |
1247 | ||
1248 | memcpy(&out[PAYLOAD], pos, payloadlen); | |
1249 | SET_LEN_FROM_PAYLOADLEN(out, payloadlen); | |
1250 | ||
1251 | if (send_hm_message(&dev, &rdata, out)) { | |
1252 | pos += payloadlen; | |
1253 | } else { | |
1254 | pos = &(fw->fw[block][2]); | |
1255 | cnt++; | |
1256 | if (cnt == MAX_RETRIES) { | |
1257 | fprintf(stderr, "\nToo many errors, giving up!\n"); | |
1258 | exit(EXIT_FAILURE); | |
1259 | } else { | |
1260 | printf("Flashing %d blocks: %04u/%04u %c", fw->fw_blocks, block + 1, fw->fw_blocks, twiddlie[msgnum % sizeof(twiddlie)]); | |
1261 | } | |
1262 | } | |
1263 | ||
1264 | msgnum++; | |
1265 | ||
1266 | if (!debug) { | |
1267 | printf("\b\b\b\b\b\b\b\b\b\b\b%04u/%04u %c", | |
1268 | block + 1, fw->fw_blocks, twiddlie[msgnum % sizeof(twiddlie)]); | |
1269 | fflush(stdout); | |
1270 | } | |
1271 | } while((pos - &(fw->fw[block][2])) < len); | |
1272 | msgid++; | |
1273 | } | |
1274 | ||
1275 | firmware_free(fw); | |
1276 | ||
1277 | printf("\n"); | |
1278 | ||
1279 | if (!switch_speed(&dev, &rdata, 10)) { | |
1280 | fprintf(stderr, "Can't switch speed!\n"); | |
1281 | exit(EXIT_FAILURE); | |
1282 | } | |
1283 | ||
1284 | printf("Waiting for device to reboot\n"); | |
1285 | rdata.message_type = MESSAGE_TYPE_R; | |
1286 | ||
1287 | cnt = 10; | |
1288 | if (dev.type == DEVICE_TYPE_HMUARTLGW) | |
1289 | cnt = 200; /* FIXME */ | |
1290 | do { | |
1291 | errno = 0; | |
1292 | switch(dev.type) { | |
1293 | case DEVICE_TYPE_CULFW: | |
1294 | pfd = culfw_poll(dev.culfw, 1000); | |
1295 | break; | |
1296 | case DEVICE_TYPE_HMCFGUSB: | |
1297 | pfd = hmcfgusb_poll(dev.hmcfgusb, 1000); | |
1298 | break; | |
1299 | case DEVICE_TYPE_HMUARTLGW: | |
1300 | pfd = hmuartlgw_poll(dev.hmuartlgw, 1000); | |
1301 | break; | |
1302 | default: | |
1303 | pfd = -1; | |
1304 | break; | |
1305 | } | |
1306 | if ((pfd < 0) && errno) { | |
1307 | if (errno != ETIMEDOUT) { | |
1308 | perror("\n\npoll"); | |
1309 | exit(EXIT_FAILURE); | |
1310 | } | |
1311 | } | |
1312 | if (rdata.message_type == MESSAGE_TYPE_E) { | |
1313 | break; | |
1314 | } | |
1315 | } while(cnt--); | |
1316 | ||
1317 | if (rdata.message_type == MESSAGE_TYPE_E) { | |
1318 | printf("Device rebooted\n"); | |
1319 | } | |
1320 | ||
1321 | switch(dev.type) { | |
1322 | case DEVICE_TYPE_HMCFGUSB: | |
1323 | hmcfgusb_close(dev.hmcfgusb); | |
1324 | hmcfgusb_exit(); | |
1325 | break; | |
1326 | case DEVICE_TYPE_CULFW: | |
1327 | culfw_close(dev.culfw); | |
1328 | break; | |
1329 | } | |
1330 | ||
1331 | return EXIT_SUCCESS; | |
1332 | } |