]>
Commit | Line | Data |
---|---|---|
25870f58 MG |
1 | /* flasher for HomeMatic-devices supporting OTA updates |
2 | * | |
3 | * Copyright (c) 2014 Michael Gernoth <michael@gernoth.net> | |
4 | * | |
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: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
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 | |
21 | * IN THE SOFTWARE. | |
22 | */ | |
23 | ||
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <unistd.h> | |
27 | #include <stdint.h> | |
28 | #include <string.h> | |
29 | #include <strings.h> | |
30 | #include <poll.h> | |
31 | #include <errno.h> | |
32 | #include <sys/types.h> | |
33 | #include <sys/stat.h> | |
34 | #include <fcntl.h> | |
35 | #include <sys/time.h> | |
36 | #include <libusb-1.0/libusb.h> | |
37 | ||
38 | #include "hexdump.h" | |
39 | #include "firmware.h" | |
40 | #include "hm.h" | |
41 | #include "version.h" | |
42 | #include "hmcfgusb.h" | |
47ea478b MG |
43 | #include "culfw.h" |
44 | #include "util.h" | |
25870f58 | 45 | |
dfe2e5e2 MG |
46 | #define MAX_RETRIES 5 |
47 | #define NORMAL_MAX_PAYLOAD 37 | |
469ea397 | 48 | #define LOWER_MAX_PAYLOAD 17 |
2d1f08ac | 49 | |
47ea478b MG |
50 | extern char *optarg; |
51 | ||
25870f58 | 52 | uint32_t hmid = 0; |
558a94bb | 53 | uint32_t my_hmid = 0; |
07decdba MG |
54 | char key[16] = {0}; |
55 | uint32_t kNo = 0; | |
25870f58 | 56 | |
dfe2e5e2 MG |
57 | /* Maximum payloadlen supported by IO */ |
58 | uint32_t max_payloadlen = NORMAL_MAX_PAYLOAD; | |
59 | ||
47ea478b MG |
60 | enum device_type { |
61 | DEVICE_TYPE_HMCFGUSB, | |
62 | DEVICE_TYPE_CULFW, | |
63 | }; | |
64 | ||
65 | struct ota_dev { | |
66 | int type; | |
67 | struct hmcfgusb_dev *hmcfgusb; | |
68 | struct culfw_dev *culfw; | |
69 | }; | |
70 | ||
25870f58 | 71 | enum message_type { |
47ea478b MG |
72 | MESSAGE_TYPE_E = 1, |
73 | MESSAGE_TYPE_R = 2, | |
25870f58 MG |
74 | }; |
75 | ||
76 | struct recv_data { | |
77 | uint8_t message[64]; | |
78 | enum message_type message_type; | |
79 | uint16_t status; | |
80 | int speed; | |
a65c08fc | 81 | uint16_t version; |
07decdba | 82 | uint8_t credits; |
25870f58 MG |
83 | }; |
84 | ||
85 | static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data) | |
86 | { | |
87 | struct recv_data *rdata = data; | |
88 | ||
89 | if (buf_len < 1) | |
90 | return 1; | |
91 | ||
92 | switch (buf[0]) { | |
93 | case 'E': | |
94 | if ((!hmid) || | |
95 | ((buf[0x11] == ((hmid >> 16) & 0xff)) && | |
96 | (buf[0x12] == ((hmid >> 8) & 0xff)) && | |
97 | (buf[0x13] == (hmid & 0xff)))) { | |
98 | memset(rdata->message, 0, sizeof(rdata->message)); | |
99 | memcpy(rdata->message, buf + 0x0d, buf[0x0d] + 1); | |
100 | rdata->message_type = MESSAGE_TYPE_E; | |
101 | } | |
102 | break; | |
103 | case 'R': | |
104 | memset(rdata->message, 0, sizeof(rdata->message)); | |
105 | memcpy(rdata->message, buf + 0x0e, buf[0x0e] + 1); | |
106 | rdata->status = (buf[5] << 8) | buf[6]; | |
107 | rdata->message_type = MESSAGE_TYPE_R; | |
108 | break; | |
109 | case 'G': | |
110 | rdata->speed = buf[1]; | |
111 | break; | |
865d5b4c | 112 | case 'H': |
a65c08fc | 113 | rdata->version = (buf[11] << 8) | buf[12]; |
07decdba | 114 | rdata->credits = buf[36]; |
558a94bb | 115 | my_hmid = (buf[0x1b] << 16) | (buf[0x1c] << 8) | buf[0x1d]; |
865d5b4c | 116 | break; |
25870f58 MG |
117 | default: |
118 | break; | |
119 | } | |
120 | ||
121 | if (buf_len != 1) | |
122 | return 1; | |
123 | ||
124 | return 1; | |
125 | } | |
126 | ||
47ea478b MG |
127 | static int parse_culfw(uint8_t *buf, int buf_len, void *data) |
128 | { | |
129 | struct recv_data *rdata = data; | |
130 | int pos = 0; | |
131 | ||
132 | memset(rdata, 0, sizeof(struct recv_data)); | |
133 | ||
134 | if (buf_len <= 3) | |
135 | return 0; | |
136 | ||
a65c08fc MG |
137 | switch(buf[0]) { |
138 | case 'A': | |
139 | if (buf[1] == 's') | |
140 | return 0; | |
141 | ||
142 | while(validate_nibble(buf[(pos * 2) + 1]) && | |
143 | validate_nibble(buf[(pos * 2) + 2]) && | |
144 | (pos + 1 < buf_len)) { | |
145 | rdata->message[pos] = ascii_to_nibble(buf[(pos * 2) + 1]) << 4; | |
146 | rdata->message[pos] |= ascii_to_nibble(buf[(pos * 2) + 2]); | |
147 | pos++; | |
148 | } | |
47ea478b | 149 | |
a65c08fc MG |
150 | if (hmid && (SRC(rdata->message) != hmid)) |
151 | return 0; | |
47ea478b | 152 | |
a65c08fc MG |
153 | rdata->message_type = MESSAGE_TYPE_E; |
154 | break; | |
155 | case 'V': | |
156 | { | |
157 | uint8_t v; | |
158 | char *s; | |
159 | char *e; | |
160 | ||
161 | s = ((char*)buf) + 2; | |
162 | e = strchr(s, '.'); | |
163 | if (!e) { | |
164 | fprintf(stderr, "Unknown response from CUL: %s", buf); | |
165 | return 0; | |
166 | } | |
167 | *e = '\0'; | |
168 | v = atoi(s); | |
169 | rdata->version = v << 8; | |
170 | ||
171 | s = e + 1; | |
172 | e = strchr(s, ' '); | |
173 | if (!e) { | |
174 | fprintf(stderr, "Unknown response from CUL: %s", buf); | |
175 | return 0; | |
176 | } | |
177 | *e = '\0'; | |
178 | v = atoi(s); | |
179 | rdata->version |= v; | |
180 | } | |
181 | break; | |
182 | default: | |
183 | fprintf(stderr, "Unknown response from CUL: %s", buf); | |
184 | return 0; | |
185 | break; | |
47ea478b MG |
186 | } |
187 | ||
47ea478b MG |
188 | return 1; |
189 | } | |
190 | ||
191 | int send_hm_message(struct ota_dev *dev, struct recv_data *rdata, uint8_t *msg) | |
25870f58 MG |
192 | { |
193 | static uint32_t id = 1; | |
194 | struct timeval tv; | |
195 | uint8_t out[0x40]; | |
196 | int pfd; | |
197 | ||
47ea478b MG |
198 | switch(dev->type) { |
199 | case DEVICE_TYPE_HMCFGUSB: | |
200 | if (gettimeofday(&tv, NULL) == -1) { | |
201 | perror("gettimeofay"); | |
202 | return 0; | |
203 | } | |
25870f58 | 204 | |
47ea478b | 205 | memset(out, 0, sizeof(out)); |
25870f58 | 206 | |
47ea478b MG |
207 | out[0] = 'S'; |
208 | out[1] = (id >> 24) & 0xff; | |
209 | out[2] = (id >> 16) & 0xff; | |
210 | out[3] = (id >> 8) & 0xff; | |
211 | out[4] = id & 0xff; | |
212 | out[10] = 0x01; | |
213 | out[11] = (tv.tv_usec >> 24) & 0xff; | |
214 | out[12] = (tv.tv_usec >> 16) & 0xff; | |
215 | out[13] = (tv.tv_usec >> 8) & 0xff; | |
216 | out[14] = tv.tv_usec & 0xff; | |
217 | ||
218 | memcpy(&out[0x0f], msg, msg[0] + 1); | |
219 | ||
220 | memset(rdata, 0, sizeof(struct recv_data)); | |
221 | hmcfgusb_send(dev->hmcfgusb, out, sizeof(out), 1); | |
222 | ||
223 | while (1) { | |
224 | if (rdata->message_type == MESSAGE_TYPE_R) { | |
07decdba MG |
225 | if (((rdata->status & 0xdf) == 0x01) || |
226 | ((rdata->status & 0xdf) == 0x02)) { | |
47ea478b MG |
227 | break; |
228 | } else { | |
229 | if ((rdata->status & 0xff00) == 0x0400) { | |
230 | fprintf(stderr, "\nOut of credits!\n"); | |
231 | } else if ((rdata->status & 0xff) == 0x08) { | |
232 | fprintf(stderr, "\nMissing ACK!\n"); | |
07decdba MG |
233 | } else if ((rdata->status & 0xff) == 0x30) { |
234 | fprintf(stderr, "\nUnknown AES-key requested!\n"); | |
47ea478b MG |
235 | } else { |
236 | fprintf(stderr, "\nInvalid status: %04x\n", rdata->status); | |
237 | } | |
238 | return 0; | |
239 | } | |
240 | } | |
241 | errno = 0; | |
3b35a8c1 | 242 | pfd = hmcfgusb_poll(dev->hmcfgusb, 1000); |
47ea478b MG |
243 | if ((pfd < 0) && errno) { |
244 | if (errno != ETIMEDOUT) { | |
245 | perror("\n\nhmcfgusb_poll"); | |
246 | exit(EXIT_FAILURE); | |
247 | } | |
248 | } | |
249 | } | |
250 | break; | |
251 | case DEVICE_TYPE_CULFW: | |
252 | { | |
cda22024 | 253 | char buf[256]; |
47ea478b MG |
254 | int i; |
255 | ||
256 | memset(buf, 0, sizeof(buf)); | |
257 | buf[0] = 'A'; | |
258 | buf[1] = 's'; | |
259 | for (i = 0; i < msg[0] + 1; i++) { | |
260 | buf[2 + (i * 2)] = nibble_to_ascii((msg[i] >> 4) & 0xf); | |
261 | buf[2 + (i * 2) + 1] = nibble_to_ascii(msg[i] & 0xf); | |
262 | } | |
263 | buf[2 + (i * 2) ] = '\r'; | |
264 | buf[2 + (i * 2) + 1] = '\n'; | |
25870f58 | 265 | |
47ea478b MG |
266 | memset(rdata, 0, sizeof(struct recv_data)); |
267 | if (culfw_send(dev->culfw, buf, 2 + (i * 2) + 1) == 0) { | |
268 | fprintf(stderr, "culfw_send failed!\n"); | |
269 | exit(EXIT_FAILURE); | |
270 | } | |
25870f58 | 271 | |
47ea478b | 272 | if (msg[CTL] & 0x20) { |
9718f9fa | 273 | int cnt = 3; |
47ea478b MG |
274 | int pfd; |
275 | do { | |
276 | errno = 0; | |
3b35a8c1 | 277 | pfd = culfw_poll(dev->culfw, 200); |
47ea478b MG |
278 | if ((pfd < 0) && errno) { |
279 | if (errno != ETIMEDOUT) { | |
9dcbf605 | 280 | perror("\n\nculfw_poll"); |
47ea478b MG |
281 | exit(EXIT_FAILURE); |
282 | } | |
283 | } | |
284 | if (rdata->message_type == MESSAGE_TYPE_E) { | |
285 | break; | |
286 | } | |
287 | } while(cnt--); | |
9718f9fa MG |
288 | |
289 | if (cnt == -1) { | |
290 | fprintf(stderr, "\nMissing ACK!\n"); | |
291 | return 0; | |
292 | } | |
2d1f08ac | 293 | } |
25870f58 | 294 | } |
47ea478b | 295 | break; |
25870f58 MG |
296 | } |
297 | ||
298 | id++; | |
299 | return 1; | |
300 | } | |
301 | ||
47ea478b | 302 | static int switch_speed(struct ota_dev *dev, struct recv_data *rdata, uint8_t speed) |
da4ab971 MG |
303 | { |
304 | uint8_t out[0x40]; | |
305 | int pfd; | |
306 | ||
307 | printf("Entering %uk-mode\n", speed); | |
308 | ||
47ea478b MG |
309 | switch(dev->type) { |
310 | case DEVICE_TYPE_HMCFGUSB: | |
311 | memset(out, 0, sizeof(out)); | |
312 | out[0] = 'G'; | |
313 | out[1] = speed; | |
314 | ||
315 | hmcfgusb_send(dev->hmcfgusb, out, sizeof(out), 1); | |
316 | ||
317 | while (1) { | |
318 | errno = 0; | |
3b35a8c1 | 319 | pfd = hmcfgusb_poll(dev->hmcfgusb, 1000); |
47ea478b MG |
320 | if ((pfd < 0) && errno) { |
321 | if (errno != ETIMEDOUT) { | |
322 | perror("\n\nhmcfgusb_poll"); | |
323 | exit(EXIT_FAILURE); | |
324 | } | |
325 | } | |
326 | if (rdata->speed == speed) | |
327 | break; | |
328 | } | |
329 | break; | |
330 | case DEVICE_TYPE_CULFW: | |
331 | if (speed == 100) { | |
332 | return culfw_send(dev->culfw, "AR\r\n", 4); | |
333 | } else { | |
334 | return culfw_send(dev->culfw, "Ar\r\n", 4); | |
da4ab971 | 335 | } |
da4ab971 MG |
336 | break; |
337 | } | |
338 | ||
339 | return 1; | |
340 | } | |
341 | ||
47ea478b MG |
342 | void flash_ota_syntax(char *prog) |
343 | { | |
344 | fprintf(stderr, "Syntax: %s parameters options\n\n", prog); | |
345 | fprintf(stderr, "Mandatory parameters:\n"); | |
346 | fprintf(stderr, "\t-f firmware.eq3\tfirmware file to flash\n"); | |
347 | fprintf(stderr, "\t-s SERIAL\tserial of device to flash\n"); | |
07decdba | 348 | fprintf(stderr, "\nOptional parameters:\n"); |
47ea478b MG |
349 | fprintf(stderr, "\t-c device\tenable CUL-mode with CUL at path \"device\"\n"); |
350 | fprintf(stderr, "\t-b bps\t\tuse CUL with speed \"bps\" (default: %u)\n", DEFAULT_CUL_BPS); | |
dfe2e5e2 | 351 | fprintf(stderr, "\t-l\t\tlower payloadlen (required for devices with little RAM, e.g. CUL v2 and CUL v4)\n"); |
47ea478b | 352 | fprintf(stderr, "\t-h\t\tthis help\n"); |
07decdba MG |
353 | fprintf(stderr, "\nOptional parameters for automatically sending device to bootloader\n"); |
354 | fprintf(stderr, "\t-C\t\tHMID of central (3 hex-bytes, no prefix, e.g. ABCDEF)\n"); | |
355 | fprintf(stderr, "\t-D\t\tHMID of device (3 hex-bytes, no prefix, e.g. 123456)\n"); | |
356 | fprintf(stderr, "\t-K\t\tKNO:KEY AES key-number and key (hex) separated by colon (Fhem hmKey attribute)\n"); | |
357 | fprintf(stderr, "\t\t\tAES is currently not supported when using a culfw-device!\n"); | |
47ea478b MG |
358 | } |
359 | ||
25870f58 MG |
360 | int main(int argc, char **argv) |
361 | { | |
362 | const char twiddlie[] = { '-', '\\', '|', '/' }; | |
f0ed61cc | 363 | const uint8_t cc1101_regs[] = { 0x10, 0x5B, 0x11, 0xF8, 0x15, 0x47 }; |
47ea478b MG |
364 | char *fw_file = NULL; |
365 | char *serial = NULL; | |
366 | char *culfw_dev = NULL; | |
07decdba | 367 | char *endptr = NULL; |
47ea478b MG |
368 | unsigned int bps = DEFAULT_CUL_BPS; |
369 | struct ota_dev dev; | |
25870f58 MG |
370 | struct recv_data rdata; |
371 | uint8_t out[0x40]; | |
372 | uint8_t *pos; | |
373 | uint8_t msgid = 0x1; | |
374 | uint16_t len; | |
375 | struct firmware *fw; | |
376 | int block; | |
377 | int pfd; | |
378 | int debug = 0; | |
379 | int cnt; | |
da4ab971 | 380 | int switchcnt = 0; |
25870f58 MG |
381 | int msgnum = 0; |
382 | int switched = 0; | |
47ea478b | 383 | int opt; |
25870f58 MG |
384 | |
385 | printf("HomeMatic OTA flasher version " VERSION "\n\n"); | |
386 | ||
07decdba | 387 | while((opt = getopt(argc, argv, "b:c:f:hls:C:D:K:")) != -1) { |
47ea478b MG |
388 | switch (opt) { |
389 | case 'b': | |
390 | bps = atoi(optarg); | |
391 | break; | |
392 | case 'c': | |
393 | culfw_dev = optarg; | |
394 | break; | |
395 | case 'f': | |
396 | fw_file = optarg; | |
397 | break; | |
dfe2e5e2 | 398 | case 'l': |
469ea397 MG |
399 | printf("Reducing payload-len from %d to %d\n", max_payloadlen, LOWER_MAX_PAYLOAD); |
400 | max_payloadlen = LOWER_MAX_PAYLOAD; | |
dfe2e5e2 | 401 | break; |
47ea478b MG |
402 | case 's': |
403 | serial = optarg; | |
404 | break; | |
07decdba MG |
405 | case 'C': |
406 | my_hmid = strtoul(optarg, &endptr, 16); | |
407 | if (*endptr != '\0') { | |
408 | fprintf(stderr, "Invalid central HMID!\n\n"); | |
409 | flash_ota_syntax(argv[0]); | |
410 | exit(EXIT_FAILURE); | |
411 | } | |
412 | break; | |
413 | case 'D': | |
414 | hmid = strtoul(optarg, &endptr, 16); | |
415 | if (*endptr != '\0') { | |
416 | fprintf(stderr, "Invalid device HMID!\n\n"); | |
417 | flash_ota_syntax(argv[0]); | |
418 | exit(EXIT_FAILURE); | |
419 | } | |
420 | break; | |
421 | case 'K': | |
422 | kNo = strtoul(optarg, &endptr, 10); | |
423 | if (*endptr != ':') { | |
424 | fprintf(stderr, "Invalid key number!\n\n"); | |
425 | flash_ota_syntax(argv[0]); | |
426 | exit(EXIT_FAILURE); | |
427 | } | |
428 | endptr++; | |
429 | for (cnt = 0; cnt < 16; cnt++) { | |
430 | if (*endptr == '\0' || *(endptr+1) == '\0' || | |
431 | !validate_nibble(*endptr) || | |
432 | !validate_nibble(*(endptr+1))) { | |
433 | fprintf(stderr, "Invalid key!\n\n"); | |
434 | flash_ota_syntax(argv[0]); | |
435 | exit(EXIT_FAILURE); | |
436 | } | |
437 | key[cnt] = ascii_to_nibble(*endptr) << 4 | ascii_to_nibble(*(endptr+1)); | |
438 | endptr += 2; | |
439 | } | |
440 | break; | |
47ea478b MG |
441 | case 'h': |
442 | case ':': | |
443 | case '?': | |
444 | default: | |
445 | flash_ota_syntax(argv[0]); | |
446 | exit(EXIT_FAILURE); | |
447 | break; | |
25870f58 | 448 | |
47ea478b MG |
449 | } |
450 | } | |
25870f58 | 451 | |
47ea478b MG |
452 | if (!fw_file || !serial) { |
453 | flash_ota_syntax(argv[0]); | |
25870f58 MG |
454 | exit(EXIT_FAILURE); |
455 | } | |
456 | ||
47ea478b | 457 | fw = firmware_read_firmware(fw_file, debug); |
25870f58 MG |
458 | if (!fw) |
459 | exit(EXIT_FAILURE); | |
460 | ||
25870f58 | 461 | memset(&rdata, 0, sizeof(rdata)); |
47ea478b | 462 | memset(&dev, 0, sizeof(struct ota_dev)); |
25870f58 | 463 | |
47ea478b | 464 | if (culfw_dev) { |
07decdba MG |
465 | if (kNo) { |
466 | fprintf(stderr, "\nAES currently not supported with culfw-device!\n"); | |
467 | flash_ota_syntax(argv[0]); | |
468 | exit(EXIT_FAILURE); | |
469 | } | |
470 | ||
a65c08fc | 471 | printf("Opening culfw-device at path %s with speed %u\n", culfw_dev, bps); |
47ea478b MG |
472 | dev.culfw = culfw_init(culfw_dev, bps, parse_culfw, &rdata); |
473 | if (!dev.culfw) { | |
474 | fprintf(stderr, "Can't initialize CUL at %s with rate %u\n", culfw_dev, bps); | |
475 | exit(EXIT_FAILURE); | |
476 | } | |
477 | dev.type = DEVICE_TYPE_CULFW; | |
a65c08fc | 478 | |
dfe2e5e2 | 479 | printf("Requesting firmware version\n"); |
a65c08fc MG |
480 | culfw_send(dev.culfw, "\r\n", 2); |
481 | culfw_flush(dev.culfw); | |
482 | ||
483 | while (1) { | |
484 | culfw_send(dev.culfw, "V\r\n", 3); | |
485 | ||
486 | errno = 0; | |
3b35a8c1 | 487 | pfd = culfw_poll(dev.culfw, 1000); |
a65c08fc MG |
488 | if ((pfd < 0) && errno) { |
489 | if (errno != ETIMEDOUT) { | |
490 | perror("\n\nhmcfgusb_poll"); | |
491 | exit(EXIT_FAILURE); | |
492 | } | |
493 | } | |
494 | if (rdata.version) | |
495 | break; | |
496 | } | |
497 | ||
498 | printf("culfw-device firmware version: %u.%02u\n", | |
499 | (rdata.version >> 8) & 0xff, | |
500 | rdata.version & 0xff); | |
501 | ||
57b387ce MG |
502 | if (rdata.version < 0x013a) { |
503 | fprintf(stderr, "\nThis version does _not_ support firmware upgrade mode, you need at least 1.58!\n"); | |
a65c08fc | 504 | exit(EXIT_FAILURE); |
a65c08fc | 505 | } |
47ea478b | 506 | } else { |
07decdba MG |
507 | uint32_t new_hmid = my_hmid; |
508 | ||
47ea478b | 509 | hmcfgusb_set_debug(debug); |
25870f58 | 510 | |
47ea478b MG |
511 | dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata); |
512 | if (!dev.hmcfgusb) { | |
513 | fprintf(stderr, "Can't initialize HM-CFG-USB\n"); | |
514 | exit(EXIT_FAILURE); | |
515 | } | |
516 | dev.type = DEVICE_TYPE_HMCFGUSB; | |
2d1f08ac | 517 | |
47ea478b MG |
518 | memset(out, 0, sizeof(out)); |
519 | out[0] = 'K'; | |
520 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
521 | ||
522 | while (1) { | |
523 | errno = 0; | |
3b35a8c1 | 524 | pfd = hmcfgusb_poll(dev.hmcfgusb, 1000); |
47ea478b MG |
525 | if ((pfd < 0) && errno) { |
526 | if (errno != ETIMEDOUT) { | |
527 | perror("\n\nhmcfgusb_poll"); | |
528 | exit(EXIT_FAILURE); | |
529 | } | |
865d5b4c | 530 | } |
a65c08fc | 531 | if (rdata.version) |
47ea478b | 532 | break; |
865d5b4c | 533 | } |
865d5b4c | 534 | |
a65c08fc MG |
535 | if (rdata.version < 0x3c7) { |
536 | fprintf(stderr, "HM-CFG-USB firmware too low: %u < 967\n", rdata.version); | |
47ea478b MG |
537 | exit(EXIT_FAILURE); |
538 | } | |
865d5b4c | 539 | |
07decdba MG |
540 | printf("HM-CFG-USB firmware version: %u, used credits: %u%%\n", rdata.version, rdata.credits); |
541 | ||
542 | if (rdata.credits >= 40) { | |
543 | printf("\nRebooting HM-CFG-USB to avoid running out of credits\n\n"); | |
544 | ||
545 | if (!dev.hmcfgusb->bootloader) { | |
546 | printf("HM-CFG-USB not in bootloader mode, entering bootloader.\n"); | |
547 | printf("Waiting for device to reappear...\n"); | |
548 | ||
549 | do { | |
550 | if (dev.hmcfgusb) { | |
551 | if (!dev.hmcfgusb->bootloader) | |
552 | hmcfgusb_enter_bootloader(dev.hmcfgusb); | |
553 | hmcfgusb_close(dev.hmcfgusb); | |
554 | } | |
555 | sleep(1); | |
556 | } while (((dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (!dev.hmcfgusb->bootloader)); | |
557 | } | |
558 | ||
559 | if (dev.hmcfgusb->bootloader) { | |
560 | printf("HM-CFG-USB in bootloader mode, rebooting\n"); | |
561 | ||
562 | do { | |
563 | if (dev.hmcfgusb) { | |
564 | if (dev.hmcfgusb->bootloader) | |
565 | hmcfgusb_leave_bootloader(dev.hmcfgusb); | |
566 | hmcfgusb_close(dev.hmcfgusb); | |
567 | } | |
568 | sleep(1); | |
569 | } while (((dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (dev.hmcfgusb->bootloader)); | |
570 | } | |
571 | } | |
572 | ||
573 | printf("\n\nHM-CFG-USB opened\n\n"); | |
574 | ||
575 | if (new_hmid && (my_hmid != new_hmid)) { | |
576 | printf("Changing hmid from %06x to %06x\n", my_hmid, new_hmid); | |
577 | ||
578 | memset(out, 0, sizeof(out)); | |
579 | out[0] = 'A'; | |
580 | out[1] = (new_hmid >> 16) & 0xff; | |
581 | out[2] = (new_hmid >> 8) & 0xff; | |
582 | out[3] = new_hmid & 0xff; | |
583 | ||
584 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
585 | ||
586 | my_hmid = new_hmid; | |
587 | } | |
588 | ||
589 | if (kNo) { | |
590 | printf("Setting AES-key\n"); | |
591 | ||
592 | memset(out, 0, sizeof(out)); | |
593 | out[0] = 'Y'; | |
594 | out[1] = 0x01; | |
595 | out[2] = kNo; | |
596 | out[3] = sizeof(key); | |
597 | memcpy(&(out[4]), key, sizeof(key)); | |
598 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
599 | ||
600 | memset(out, 0, sizeof(out)); | |
601 | out[0] = 'Y'; | |
602 | out[1] = 0x02; | |
603 | out[2] = 0x00; | |
604 | out[3] = 0x00; | |
605 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
606 | ||
607 | memset(out, 0, sizeof(out)); | |
608 | out[0] = 'Y'; | |
609 | out[1] = 0x03; | |
610 | out[2] = 0x00; | |
611 | out[3] = 0x00; | |
612 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
613 | } | |
47ea478b | 614 | } |
865d5b4c | 615 | |
47ea478b | 616 | if (!switch_speed(&dev, &rdata, 10)) { |
da4ab971 MG |
617 | fprintf(stderr, "Can't switch speed!\n"); |
618 | exit(EXIT_FAILURE); | |
25870f58 MG |
619 | } |
620 | ||
07decdba MG |
621 | if (hmid && my_hmid) { |
622 | printf("Sending device with hmid %06x to bootloader\n", hmid); | |
623 | out[MSGID] = msgid++; | |
624 | out[CTL] = 0x30; | |
625 | out[TYPE] = 0x11; | |
626 | SET_SRC(out, my_hmid); | |
627 | SET_DST(out, hmid); | |
628 | out[PAYLOAD] = 0xCA; | |
629 | SET_LEN_FROM_PAYLOADLEN(out, 1); | |
630 | ||
631 | cnt = 3; | |
632 | do { | |
633 | if (send_hm_message(&dev, &rdata, out)) { | |
634 | break; | |
635 | } | |
636 | } while (cnt--); | |
637 | if (cnt == -1) { | |
638 | printf("Failed to send device to bootloader, please enter bootloader manually.\n"); | |
639 | } | |
640 | } | |
641 | ||
47ea478b | 642 | printf("Waiting for device with serial %s\n", serial); |
25870f58 MG |
643 | |
644 | while (1) { | |
0edcd7f2 | 645 | errno = 0; |
47ea478b | 646 | switch (dev.type) { |
47ea478b | 647 | case DEVICE_TYPE_CULFW: |
3b35a8c1 | 648 | pfd = culfw_poll(dev.culfw, 1000); |
47ea478b MG |
649 | break; |
650 | case DEVICE_TYPE_HMCFGUSB: | |
651 | default: | |
3b35a8c1 | 652 | pfd = hmcfgusb_poll(dev.hmcfgusb, 1000); |
47ea478b MG |
653 | break; |
654 | } | |
655 | ||
25870f58 MG |
656 | if ((pfd < 0) && errno) { |
657 | if (errno != ETIMEDOUT) { | |
47ea478b | 658 | perror("\n\npoll"); |
25870f58 MG |
659 | exit(EXIT_FAILURE); |
660 | } | |
661 | } | |
662 | ||
663 | if ((rdata.message[LEN] == 0x14) && /* Length */ | |
664 | (rdata.message[MSGID] == 0x00) && /* Message ID */ | |
665 | (rdata.message[CTL] == 0x00) && /* Control Byte */ | |
666 | (rdata.message[TYPE] == 0x10) && /* Messagte type: Information */ | |
667 | (DST(rdata.message) == 0x000000) && /* Broadcast */ | |
47ea478b MG |
668 | (rdata.message[PAYLOAD] == 0x00)) { /* FUP? */ |
669 | if (!strncmp((char*)&(rdata.message[0x0b]), serial, 10)) { | |
25870f58 MG |
670 | hmid = SRC(rdata.message); |
671 | break; | |
672 | } | |
673 | } | |
674 | } | |
675 | ||
47ea478b | 676 | printf("Device with serial %s (hmid: %06x) entered firmware-update-mode\n", serial, hmid); |
25870f58 | 677 | |
47ea478b MG |
678 | if (dev.type == DEVICE_TYPE_HMCFGUSB) { |
679 | printf("Adding HMID\n"); | |
25870f58 | 680 | |
47ea478b MG |
681 | memset(out, 0, sizeof(out)); |
682 | out[0] = '+'; | |
683 | out[1] = (hmid >> 16) & 0xff; | |
684 | out[2] = (hmid >> 8) & 0xff; | |
685 | out[3] = hmid & 0xff; | |
25870f58 | 686 | |
47ea478b MG |
687 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); |
688 | } | |
25870f58 | 689 | |
da4ab971 | 690 | switchcnt = 3; |
25870f58 MG |
691 | do { |
692 | printf("Initiating remote switch to 100k\n"); | |
693 | ||
694 | memset(out, 0, sizeof(out)); | |
695 | ||
696 | out[MSGID] = msgid++; | |
697 | out[CTL] = 0x00; | |
698 | out[TYPE] = 0xCB; | |
558a94bb | 699 | SET_SRC(out, my_hmid); |
25870f58 MG |
700 | SET_DST(out, hmid); |
701 | ||
f0ed61cc MG |
702 | memcpy(&out[PAYLOAD], cc1101_regs, sizeof(cc1101_regs)); |
703 | SET_LEN_FROM_PAYLOADLEN(out, sizeof(cc1101_regs)); | |
25870f58 | 704 | |
47ea478b | 705 | if (!send_hm_message(&dev, &rdata, out)) { |
25870f58 MG |
706 | exit(EXIT_FAILURE); |
707 | } | |
708 | ||
47ea478b | 709 | if (!switch_speed(&dev, &rdata, 100)) { |
da4ab971 MG |
710 | fprintf(stderr, "Can't switch speed!\n"); |
711 | exit(EXIT_FAILURE); | |
25870f58 MG |
712 | } |
713 | ||
714 | printf("Has the device switched?\n"); | |
715 | ||
716 | memset(out, 0, sizeof(out)); | |
717 | ||
718 | out[MSGID] = msgid++; | |
719 | out[CTL] = 0x20; | |
720 | out[TYPE] = 0xCB; | |
558a94bb | 721 | SET_SRC(out, my_hmid); |
25870f58 MG |
722 | SET_DST(out, hmid); |
723 | ||
f0ed61cc MG |
724 | memcpy(&out[PAYLOAD], cc1101_regs, sizeof(cc1101_regs)); |
725 | SET_LEN_FROM_PAYLOADLEN(out, sizeof(cc1101_regs)); | |
25870f58 MG |
726 | |
727 | cnt = 3; | |
728 | do { | |
47ea478b | 729 | if (send_hm_message(&dev, &rdata, out)) { |
25870f58 MG |
730 | /* A0A02000221B9AD00000000 */ |
731 | switched = 1; | |
732 | break; | |
25870f58 MG |
733 | } |
734 | } while (cnt--); | |
735 | ||
736 | if (!switched) { | |
da4ab971 | 737 | printf("No!\n"); |
25870f58 | 738 | |
47ea478b | 739 | if (!switch_speed(&dev, &rdata, 10)) { |
da4ab971 MG |
740 | fprintf(stderr, "Can't switch speed!\n"); |
741 | exit(EXIT_FAILURE); | |
25870f58 MG |
742 | } |
743 | } | |
da4ab971 | 744 | } while ((!switched) && (switchcnt--)); |
25870f58 | 745 | |
268d2cc6 MG |
746 | if (!switched) { |
747 | fprintf(stderr, "Too many errors, giving up!\n"); | |
748 | exit(EXIT_FAILURE); | |
749 | } | |
25870f58 | 750 | |
da4ab971 | 751 | printf("Yes!\n"); |
25870f58 MG |
752 | |
753 | printf("Flashing %d blocks", fw->fw_blocks); | |
754 | if (debug) { | |
755 | printf("\n"); | |
756 | } else { | |
757 | printf(": %04u/%04u %c", 0, fw->fw_blocks, twiddlie[0]); | |
758 | fflush(stdout); | |
759 | } | |
760 | ||
761 | for (block = 0; block < fw->fw_blocks; block++) { | |
762 | int first; | |
763 | ||
764 | len = fw->fw[block][2] << 8; | |
765 | len |= fw->fw[block][3]; | |
766 | ||
767 | pos = &(fw->fw[block][2]); | |
768 | ||
769 | len += 2; /* length */ | |
770 | ||
771 | if (debug) | |
772 | hexdump(pos, len, "F> "); | |
773 | ||
774 | first = 1; | |
775 | cnt = 0; | |
776 | do { | |
dfe2e5e2 | 777 | int payloadlen = max_payloadlen - 2; |
25870f58 MG |
778 | int ack = 0; |
779 | ||
780 | if (first) { | |
dfe2e5e2 | 781 | payloadlen = max_payloadlen; |
25870f58 MG |
782 | first = 0; |
783 | } | |
784 | ||
785 | if ((len - (pos - &(fw->fw[block][2]))) < payloadlen) | |
786 | payloadlen = (len - (pos - &(fw->fw[block][2]))); | |
787 | ||
788 | if (((pos + payloadlen) - &(fw->fw[block][2])) == len) | |
789 | ack = 1; | |
790 | ||
791 | memset(&rdata, 0, sizeof(rdata)); | |
792 | ||
793 | memset(out, 0, sizeof(out)); | |
794 | ||
da4ab971 | 795 | out[MSGID] = msgid; |
25870f58 MG |
796 | if (ack) |
797 | out[CTL] = 0x20; | |
798 | out[TYPE] = 0xCA; | |
558a94bb | 799 | SET_SRC(out, my_hmid); |
25870f58 MG |
800 | SET_DST(out, hmid); |
801 | ||
802 | memcpy(&out[PAYLOAD], pos, payloadlen); | |
803 | SET_LEN_FROM_PAYLOADLEN(out, payloadlen); | |
804 | ||
47ea478b | 805 | if (send_hm_message(&dev, &rdata, out)) { |
25870f58 MG |
806 | pos += payloadlen; |
807 | } else { | |
808 | pos = &(fw->fw[block][2]); | |
809 | cnt++; | |
2d1f08ac | 810 | if (cnt == MAX_RETRIES) { |
25870f58 MG |
811 | fprintf(stderr, "\nToo many errors, giving up!\n"); |
812 | exit(EXIT_FAILURE); | |
813 | } else { | |
814 | printf("Flashing %d blocks: %04u/%04u %c", fw->fw_blocks, block + 1, fw->fw_blocks, twiddlie[msgnum % sizeof(twiddlie)]); | |
815 | } | |
816 | } | |
817 | ||
818 | msgnum++; | |
819 | ||
820 | if (!debug) { | |
821 | printf("\b\b\b\b\b\b\b\b\b\b\b%04u/%04u %c", | |
822 | block + 1, fw->fw_blocks, twiddlie[msgnum % sizeof(twiddlie)]); | |
823 | fflush(stdout); | |
824 | } | |
825 | } while((pos - &(fw->fw[block][2])) < len); | |
da4ab971 | 826 | msgid++; |
25870f58 MG |
827 | } |
828 | ||
829 | firmware_free(fw); | |
830 | ||
da4ab971 | 831 | printf("\n"); |
25870f58 | 832 | |
47ea478b | 833 | if (!switch_speed(&dev, &rdata, 10)) { |
da4ab971 MG |
834 | fprintf(stderr, "Can't switch speed!\n"); |
835 | exit(EXIT_FAILURE); | |
25870f58 MG |
836 | } |
837 | ||
838 | printf("Waiting for device to reboot\n"); | |
839 | ||
840 | cnt = 10; | |
841 | do { | |
842 | errno = 0; | |
47ea478b MG |
843 | switch(dev.type) { |
844 | case DEVICE_TYPE_CULFW: | |
3b35a8c1 | 845 | pfd = culfw_poll(dev.culfw, 1000); |
47ea478b MG |
846 | break; |
847 | case DEVICE_TYPE_HMCFGUSB: | |
848 | default: | |
3b35a8c1 | 849 | pfd = hmcfgusb_poll(dev.hmcfgusb, 1000); |
47ea478b MG |
850 | break; |
851 | } | |
25870f58 MG |
852 | if ((pfd < 0) && errno) { |
853 | if (errno != ETIMEDOUT) { | |
9dcbf605 | 854 | perror("\n\npoll"); |
25870f58 MG |
855 | exit(EXIT_FAILURE); |
856 | } | |
857 | } | |
858 | if (rdata.message_type == MESSAGE_TYPE_E) { | |
859 | break; | |
860 | } | |
861 | } while(cnt--); | |
862 | ||
863 | if (rdata.message_type == MESSAGE_TYPE_E) { | |
864 | printf("Device rebooted\n"); | |
865 | } | |
866 | ||
47ea478b MG |
867 | switch(dev.type) { |
868 | case DEVICE_TYPE_HMCFGUSB: | |
869 | hmcfgusb_close(dev.hmcfgusb); | |
018f85fa | 870 | hmcfgusb_exit(); |
47ea478b MG |
871 | break; |
872 | case DEVICE_TYPE_CULFW: | |
873 | culfw_close(dev.culfw); | |
874 | break; | |
875 | } | |
25870f58 MG |
876 | |
877 | return EXIT_SUCCESS; | |
878 | } |