]>
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 | |
48 | #define LOWE_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; |
25870f58 | 54 | |
dfe2e5e2 MG |
55 | /* Maximum payloadlen supported by IO */ |
56 | uint32_t max_payloadlen = NORMAL_MAX_PAYLOAD; | |
57 | ||
47ea478b MG |
58 | enum device_type { |
59 | DEVICE_TYPE_HMCFGUSB, | |
60 | DEVICE_TYPE_CULFW, | |
61 | }; | |
62 | ||
63 | struct ota_dev { | |
64 | int type; | |
65 | struct hmcfgusb_dev *hmcfgusb; | |
66 | struct culfw_dev *culfw; | |
67 | }; | |
68 | ||
25870f58 | 69 | enum message_type { |
47ea478b MG |
70 | MESSAGE_TYPE_E = 1, |
71 | MESSAGE_TYPE_R = 2, | |
25870f58 MG |
72 | }; |
73 | ||
74 | struct recv_data { | |
75 | uint8_t message[64]; | |
76 | enum message_type message_type; | |
77 | uint16_t status; | |
78 | int speed; | |
a65c08fc | 79 | uint16_t version; |
25870f58 MG |
80 | }; |
81 | ||
82 | static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data) | |
83 | { | |
84 | struct recv_data *rdata = data; | |
85 | ||
86 | if (buf_len < 1) | |
87 | return 1; | |
88 | ||
89 | switch (buf[0]) { | |
90 | case 'E': | |
91 | if ((!hmid) || | |
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; | |
98 | } | |
99 | break; | |
100 | case 'R': | |
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; | |
105 | break; | |
106 | case 'G': | |
107 | rdata->speed = buf[1]; | |
108 | break; | |
865d5b4c | 109 | case 'H': |
a65c08fc | 110 | rdata->version = (buf[11] << 8) | buf[12]; |
558a94bb | 111 | my_hmid = (buf[0x1b] << 16) | (buf[0x1c] << 8) | buf[0x1d]; |
865d5b4c | 112 | break; |
25870f58 MG |
113 | default: |
114 | break; | |
115 | } | |
116 | ||
117 | if (buf_len != 1) | |
118 | return 1; | |
119 | ||
120 | return 1; | |
121 | } | |
122 | ||
47ea478b MG |
123 | static int parse_culfw(uint8_t *buf, int buf_len, void *data) |
124 | { | |
125 | struct recv_data *rdata = data; | |
126 | int pos = 0; | |
127 | ||
128 | memset(rdata, 0, sizeof(struct recv_data)); | |
129 | ||
130 | if (buf_len <= 3) | |
131 | return 0; | |
132 | ||
a65c08fc MG |
133 | switch(buf[0]) { |
134 | case 'A': | |
135 | if (buf[1] == 's') | |
136 | return 0; | |
137 | ||
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]); | |
143 | pos++; | |
144 | } | |
47ea478b | 145 | |
a65c08fc MG |
146 | if (hmid && (SRC(rdata->message) != hmid)) |
147 | return 0; | |
47ea478b | 148 | |
a65c08fc MG |
149 | rdata->message_type = MESSAGE_TYPE_E; |
150 | break; | |
151 | case 'V': | |
152 | { | |
153 | uint8_t v; | |
154 | char *s; | |
155 | char *e; | |
156 | ||
157 | s = ((char*)buf) + 2; | |
158 | e = strchr(s, '.'); | |
159 | if (!e) { | |
160 | fprintf(stderr, "Unknown response from CUL: %s", buf); | |
161 | return 0; | |
162 | } | |
163 | *e = '\0'; | |
164 | v = atoi(s); | |
165 | rdata->version = v << 8; | |
166 | ||
167 | s = e + 1; | |
168 | e = strchr(s, ' '); | |
169 | if (!e) { | |
170 | fprintf(stderr, "Unknown response from CUL: %s", buf); | |
171 | return 0; | |
172 | } | |
173 | *e = '\0'; | |
174 | v = atoi(s); | |
175 | rdata->version |= v; | |
176 | } | |
177 | break; | |
178 | default: | |
179 | fprintf(stderr, "Unknown response from CUL: %s", buf); | |
180 | return 0; | |
181 | break; | |
47ea478b MG |
182 | } |
183 | ||
47ea478b MG |
184 | return 1; |
185 | } | |
186 | ||
187 | int send_hm_message(struct ota_dev *dev, struct recv_data *rdata, uint8_t *msg) | |
25870f58 MG |
188 | { |
189 | static uint32_t id = 1; | |
190 | struct timeval tv; | |
191 | uint8_t out[0x40]; | |
192 | int pfd; | |
193 | ||
47ea478b MG |
194 | switch(dev->type) { |
195 | case DEVICE_TYPE_HMCFGUSB: | |
196 | if (gettimeofday(&tv, NULL) == -1) { | |
197 | perror("gettimeofay"); | |
198 | return 0; | |
199 | } | |
25870f58 | 200 | |
47ea478b | 201 | memset(out, 0, sizeof(out)); |
25870f58 | 202 | |
47ea478b MG |
203 | out[0] = 'S'; |
204 | out[1] = (id >> 24) & 0xff; | |
205 | out[2] = (id >> 16) & 0xff; | |
206 | out[3] = (id >> 8) & 0xff; | |
207 | out[4] = id & 0xff; | |
208 | out[10] = 0x01; | |
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; | |
213 | ||
214 | memcpy(&out[0x0f], msg, msg[0] + 1); | |
215 | ||
216 | memset(rdata, 0, sizeof(struct recv_data)); | |
217 | hmcfgusb_send(dev->hmcfgusb, out, sizeof(out), 1); | |
218 | ||
219 | while (1) { | |
220 | if (rdata->message_type == MESSAGE_TYPE_R) { | |
221 | if (((rdata->status & 0xff) == 0x01) || | |
222 | ((rdata->status & 0xff) == 0x02)) { | |
223 | break; | |
224 | } else { | |
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"); | |
229 | } else { | |
230 | fprintf(stderr, "\nInvalid status: %04x\n", rdata->status); | |
231 | } | |
232 | return 0; | |
233 | } | |
234 | } | |
235 | errno = 0; | |
3b35a8c1 | 236 | pfd = hmcfgusb_poll(dev->hmcfgusb, 1000); |
47ea478b MG |
237 | if ((pfd < 0) && errno) { |
238 | if (errno != ETIMEDOUT) { | |
239 | perror("\n\nhmcfgusb_poll"); | |
240 | exit(EXIT_FAILURE); | |
241 | } | |
242 | } | |
243 | } | |
244 | break; | |
245 | case DEVICE_TYPE_CULFW: | |
246 | { | |
cda22024 | 247 | char buf[256]; |
47ea478b MG |
248 | int i; |
249 | ||
250 | memset(buf, 0, sizeof(buf)); | |
251 | buf[0] = 'A'; | |
252 | buf[1] = 's'; | |
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); | |
256 | } | |
257 | buf[2 + (i * 2) ] = '\r'; | |
258 | buf[2 + (i * 2) + 1] = '\n'; | |
25870f58 | 259 | |
47ea478b MG |
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"); | |
263 | exit(EXIT_FAILURE); | |
264 | } | |
25870f58 | 265 | |
47ea478b | 266 | if (msg[CTL] & 0x20) { |
9718f9fa | 267 | int cnt = 3; |
47ea478b MG |
268 | int pfd; |
269 | do { | |
270 | errno = 0; | |
3b35a8c1 | 271 | pfd = culfw_poll(dev->culfw, 200); |
47ea478b MG |
272 | if ((pfd < 0) && errno) { |
273 | if (errno != ETIMEDOUT) { | |
9dcbf605 | 274 | perror("\n\nculfw_poll"); |
47ea478b MG |
275 | exit(EXIT_FAILURE); |
276 | } | |
277 | } | |
278 | if (rdata->message_type == MESSAGE_TYPE_E) { | |
279 | break; | |
280 | } | |
281 | } while(cnt--); | |
9718f9fa MG |
282 | |
283 | if (cnt == -1) { | |
284 | fprintf(stderr, "\nMissing ACK!\n"); | |
285 | return 0; | |
286 | } | |
2d1f08ac | 287 | } |
25870f58 | 288 | } |
47ea478b | 289 | break; |
25870f58 MG |
290 | } |
291 | ||
292 | id++; | |
293 | return 1; | |
294 | } | |
295 | ||
47ea478b | 296 | static int switch_speed(struct ota_dev *dev, struct recv_data *rdata, uint8_t speed) |
da4ab971 MG |
297 | { |
298 | uint8_t out[0x40]; | |
299 | int pfd; | |
300 | ||
301 | printf("Entering %uk-mode\n", speed); | |
302 | ||
47ea478b MG |
303 | switch(dev->type) { |
304 | case DEVICE_TYPE_HMCFGUSB: | |
305 | memset(out, 0, sizeof(out)); | |
306 | out[0] = 'G'; | |
307 | out[1] = speed; | |
308 | ||
309 | hmcfgusb_send(dev->hmcfgusb, out, sizeof(out), 1); | |
310 | ||
311 | while (1) { | |
312 | errno = 0; | |
3b35a8c1 | 313 | pfd = hmcfgusb_poll(dev->hmcfgusb, 1000); |
47ea478b MG |
314 | if ((pfd < 0) && errno) { |
315 | if (errno != ETIMEDOUT) { | |
316 | perror("\n\nhmcfgusb_poll"); | |
317 | exit(EXIT_FAILURE); | |
318 | } | |
319 | } | |
320 | if (rdata->speed == speed) | |
321 | break; | |
322 | } | |
323 | break; | |
324 | case DEVICE_TYPE_CULFW: | |
325 | if (speed == 100) { | |
326 | return culfw_send(dev->culfw, "AR\r\n", 4); | |
327 | } else { | |
328 | return culfw_send(dev->culfw, "Ar\r\n", 4); | |
da4ab971 | 329 | } |
da4ab971 MG |
330 | break; |
331 | } | |
332 | ||
333 | return 1; | |
334 | } | |
335 | ||
47ea478b MG |
336 | void flash_ota_syntax(char *prog) |
337 | { | |
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); | |
dfe2e5e2 | 345 | fprintf(stderr, "\t-l\t\tlower payloadlen (required for devices with little RAM, e.g. CUL v2 and CUL v4)\n"); |
47ea478b MG |
346 | fprintf(stderr, "\t-h\t\tthis help\n"); |
347 | } | |
348 | ||
25870f58 MG |
349 | int main(int argc, char **argv) |
350 | { | |
351 | const char twiddlie[] = { '-', '\\', '|', '/' }; | |
f0ed61cc | 352 | const uint8_t cc1101_regs[] = { 0x10, 0x5B, 0x11, 0xF8, 0x15, 0x47 }; |
47ea478b MG |
353 | char *fw_file = NULL; |
354 | char *serial = NULL; | |
355 | char *culfw_dev = NULL; | |
356 | unsigned int bps = DEFAULT_CUL_BPS; | |
357 | struct ota_dev dev; | |
25870f58 MG |
358 | struct recv_data rdata; |
359 | uint8_t out[0x40]; | |
360 | uint8_t *pos; | |
361 | uint8_t msgid = 0x1; | |
362 | uint16_t len; | |
363 | struct firmware *fw; | |
364 | int block; | |
365 | int pfd; | |
366 | int debug = 0; | |
367 | int cnt; | |
da4ab971 | 368 | int switchcnt = 0; |
25870f58 MG |
369 | int msgnum = 0; |
370 | int switched = 0; | |
47ea478b | 371 | int opt; |
25870f58 MG |
372 | |
373 | printf("HomeMatic OTA flasher version " VERSION "\n\n"); | |
374 | ||
dfe2e5e2 | 375 | while((opt = getopt(argc, argv, "b:c:f:hls:")) != -1) { |
47ea478b MG |
376 | switch (opt) { |
377 | case 'b': | |
378 | bps = atoi(optarg); | |
379 | break; | |
380 | case 'c': | |
381 | culfw_dev = optarg; | |
382 | break; | |
383 | case 'f': | |
384 | fw_file = optarg; | |
385 | break; | |
dfe2e5e2 MG |
386 | case 'l': |
387 | printf("Reducing payload-len from %d to %d\n", max_payloadlen, LOWE_MAX_PAYLOAD); | |
388 | max_payloadlen = LOWE_MAX_PAYLOAD; | |
389 | break; | |
47ea478b MG |
390 | case 's': |
391 | serial = optarg; | |
392 | break; | |
393 | case 'h': | |
394 | case ':': | |
395 | case '?': | |
396 | default: | |
397 | flash_ota_syntax(argv[0]); | |
398 | exit(EXIT_FAILURE); | |
399 | break; | |
25870f58 | 400 | |
47ea478b MG |
401 | } |
402 | } | |
25870f58 | 403 | |
47ea478b MG |
404 | if (!fw_file || !serial) { |
405 | flash_ota_syntax(argv[0]); | |
25870f58 MG |
406 | exit(EXIT_FAILURE); |
407 | } | |
408 | ||
47ea478b | 409 | fw = firmware_read_firmware(fw_file, debug); |
25870f58 MG |
410 | if (!fw) |
411 | exit(EXIT_FAILURE); | |
412 | ||
25870f58 | 413 | memset(&rdata, 0, sizeof(rdata)); |
47ea478b | 414 | memset(&dev, 0, sizeof(struct ota_dev)); |
25870f58 | 415 | |
47ea478b | 416 | if (culfw_dev) { |
a65c08fc | 417 | printf("Opening culfw-device at path %s with speed %u\n", culfw_dev, bps); |
47ea478b MG |
418 | dev.culfw = culfw_init(culfw_dev, bps, parse_culfw, &rdata); |
419 | if (!dev.culfw) { | |
420 | fprintf(stderr, "Can't initialize CUL at %s with rate %u\n", culfw_dev, bps); | |
421 | exit(EXIT_FAILURE); | |
422 | } | |
423 | dev.type = DEVICE_TYPE_CULFW; | |
a65c08fc | 424 | |
dfe2e5e2 | 425 | printf("Requesting firmware version\n"); |
a65c08fc MG |
426 | culfw_send(dev.culfw, "\r\n", 2); |
427 | culfw_flush(dev.culfw); | |
428 | ||
429 | while (1) { | |
430 | culfw_send(dev.culfw, "V\r\n", 3); | |
431 | ||
432 | errno = 0; | |
3b35a8c1 | 433 | pfd = culfw_poll(dev.culfw, 1000); |
a65c08fc MG |
434 | if ((pfd < 0) && errno) { |
435 | if (errno != ETIMEDOUT) { | |
436 | perror("\n\nhmcfgusb_poll"); | |
437 | exit(EXIT_FAILURE); | |
438 | } | |
439 | } | |
440 | if (rdata.version) | |
441 | break; | |
442 | } | |
443 | ||
444 | printf("culfw-device firmware version: %u.%02u\n", | |
445 | (rdata.version >> 8) & 0xff, | |
446 | rdata.version & 0xff); | |
447 | ||
57b387ce MG |
448 | if (rdata.version < 0x013a) { |
449 | fprintf(stderr, "\nThis version does _not_ support firmware upgrade mode, you need at least 1.58!\n"); | |
a65c08fc | 450 | exit(EXIT_FAILURE); |
a65c08fc | 451 | } |
47ea478b MG |
452 | } else { |
453 | hmcfgusb_set_debug(debug); | |
25870f58 | 454 | |
47ea478b MG |
455 | dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata); |
456 | if (!dev.hmcfgusb) { | |
457 | fprintf(stderr, "Can't initialize HM-CFG-USB\n"); | |
458 | exit(EXIT_FAILURE); | |
459 | } | |
460 | dev.type = DEVICE_TYPE_HMCFGUSB; | |
2d1f08ac | 461 | |
47ea478b | 462 | printf("\nRebooting HM-CFG-USB to avoid running out of credits\n\n"); |
2d1f08ac | 463 | |
47ea478b MG |
464 | if (!dev.hmcfgusb->bootloader) { |
465 | printf("HM-CFG-USB not in bootloader mode, entering bootloader.\n"); | |
47ea478b | 466 | printf("Waiting for device to reappear...\n"); |
2d1f08ac | 467 | |
47ea478b MG |
468 | do { |
469 | if (dev.hmcfgusb) { | |
018f85fa MG |
470 | if (!dev.hmcfgusb->bootloader) |
471 | hmcfgusb_enter_bootloader(dev.hmcfgusb); | |
47ea478b MG |
472 | hmcfgusb_close(dev.hmcfgusb); |
473 | } | |
474 | sleep(1); | |
475 | } while (((dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (!dev.hmcfgusb->bootloader)); | |
476 | } | |
2d1f08ac | 477 | |
47ea478b MG |
478 | if (dev.hmcfgusb->bootloader) { |
479 | printf("HM-CFG-USB in bootloader mode, rebooting\n"); | |
25870f58 | 480 | |
47ea478b MG |
481 | do { |
482 | if (dev.hmcfgusb) { | |
018f85fa MG |
483 | if (dev.hmcfgusb->bootloader) |
484 | hmcfgusb_leave_bootloader(dev.hmcfgusb); | |
47ea478b MG |
485 | hmcfgusb_close(dev.hmcfgusb); |
486 | } | |
487 | sleep(1); | |
488 | } while (((dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (dev.hmcfgusb->bootloader)); | |
489 | } | |
25870f58 | 490 | |
47ea478b | 491 | printf("\n\nHM-CFG-USB opened\n\n"); |
865d5b4c | 492 | |
47ea478b MG |
493 | memset(out, 0, sizeof(out)); |
494 | out[0] = 'K'; | |
495 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); | |
496 | ||
497 | while (1) { | |
498 | errno = 0; | |
3b35a8c1 | 499 | pfd = hmcfgusb_poll(dev.hmcfgusb, 1000); |
47ea478b MG |
500 | if ((pfd < 0) && errno) { |
501 | if (errno != ETIMEDOUT) { | |
502 | perror("\n\nhmcfgusb_poll"); | |
503 | exit(EXIT_FAILURE); | |
504 | } | |
865d5b4c | 505 | } |
a65c08fc | 506 | if (rdata.version) |
47ea478b | 507 | break; |
865d5b4c | 508 | } |
865d5b4c | 509 | |
a65c08fc MG |
510 | if (rdata.version < 0x3c7) { |
511 | fprintf(stderr, "HM-CFG-USB firmware too low: %u < 967\n", rdata.version); | |
47ea478b MG |
512 | exit(EXIT_FAILURE); |
513 | } | |
865d5b4c | 514 | |
a65c08fc | 515 | printf("HM-CFG-USB firmware version: %u\n", rdata.version); |
47ea478b | 516 | } |
865d5b4c | 517 | |
47ea478b | 518 | if (!switch_speed(&dev, &rdata, 10)) { |
da4ab971 MG |
519 | fprintf(stderr, "Can't switch speed!\n"); |
520 | exit(EXIT_FAILURE); | |
25870f58 MG |
521 | } |
522 | ||
47ea478b | 523 | printf("Waiting for device with serial %s\n", serial); |
25870f58 MG |
524 | |
525 | while (1) { | |
0edcd7f2 | 526 | errno = 0; |
47ea478b | 527 | switch (dev.type) { |
47ea478b | 528 | case DEVICE_TYPE_CULFW: |
3b35a8c1 | 529 | pfd = culfw_poll(dev.culfw, 1000); |
47ea478b MG |
530 | break; |
531 | case DEVICE_TYPE_HMCFGUSB: | |
532 | default: | |
3b35a8c1 | 533 | pfd = hmcfgusb_poll(dev.hmcfgusb, 1000); |
47ea478b MG |
534 | break; |
535 | } | |
536 | ||
25870f58 MG |
537 | if ((pfd < 0) && errno) { |
538 | if (errno != ETIMEDOUT) { | |
47ea478b | 539 | perror("\n\npoll"); |
25870f58 MG |
540 | exit(EXIT_FAILURE); |
541 | } | |
542 | } | |
543 | ||
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 */ | |
47ea478b MG |
549 | (rdata.message[PAYLOAD] == 0x00)) { /* FUP? */ |
550 | if (!strncmp((char*)&(rdata.message[0x0b]), serial, 10)) { | |
25870f58 MG |
551 | hmid = SRC(rdata.message); |
552 | break; | |
553 | } | |
554 | } | |
555 | } | |
556 | ||
47ea478b | 557 | printf("Device with serial %s (hmid: %06x) entered firmware-update-mode\n", serial, hmid); |
25870f58 | 558 | |
47ea478b MG |
559 | if (dev.type == DEVICE_TYPE_HMCFGUSB) { |
560 | printf("Adding HMID\n"); | |
25870f58 | 561 | |
47ea478b MG |
562 | memset(out, 0, sizeof(out)); |
563 | out[0] = '+'; | |
564 | out[1] = (hmid >> 16) & 0xff; | |
565 | out[2] = (hmid >> 8) & 0xff; | |
566 | out[3] = hmid & 0xff; | |
25870f58 | 567 | |
47ea478b MG |
568 | hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); |
569 | } | |
25870f58 | 570 | |
da4ab971 | 571 | switchcnt = 3; |
25870f58 MG |
572 | do { |
573 | printf("Initiating remote switch to 100k\n"); | |
574 | ||
575 | memset(out, 0, sizeof(out)); | |
576 | ||
577 | out[MSGID] = msgid++; | |
578 | out[CTL] = 0x00; | |
579 | out[TYPE] = 0xCB; | |
558a94bb | 580 | SET_SRC(out, my_hmid); |
25870f58 MG |
581 | SET_DST(out, hmid); |
582 | ||
f0ed61cc MG |
583 | memcpy(&out[PAYLOAD], cc1101_regs, sizeof(cc1101_regs)); |
584 | SET_LEN_FROM_PAYLOADLEN(out, sizeof(cc1101_regs)); | |
25870f58 | 585 | |
47ea478b | 586 | if (!send_hm_message(&dev, &rdata, out)) { |
25870f58 MG |
587 | exit(EXIT_FAILURE); |
588 | } | |
589 | ||
47ea478b | 590 | if (!switch_speed(&dev, &rdata, 100)) { |
da4ab971 MG |
591 | fprintf(stderr, "Can't switch speed!\n"); |
592 | exit(EXIT_FAILURE); | |
25870f58 MG |
593 | } |
594 | ||
595 | printf("Has the device switched?\n"); | |
596 | ||
597 | memset(out, 0, sizeof(out)); | |
598 | ||
599 | out[MSGID] = msgid++; | |
600 | out[CTL] = 0x20; | |
601 | out[TYPE] = 0xCB; | |
558a94bb | 602 | SET_SRC(out, my_hmid); |
25870f58 MG |
603 | SET_DST(out, hmid); |
604 | ||
f0ed61cc MG |
605 | memcpy(&out[PAYLOAD], cc1101_regs, sizeof(cc1101_regs)); |
606 | SET_LEN_FROM_PAYLOADLEN(out, sizeof(cc1101_regs)); | |
25870f58 MG |
607 | |
608 | cnt = 3; | |
609 | do { | |
47ea478b | 610 | if (send_hm_message(&dev, &rdata, out)) { |
25870f58 MG |
611 | /* A0A02000221B9AD00000000 */ |
612 | switched = 1; | |
613 | break; | |
25870f58 MG |
614 | } |
615 | } while (cnt--); | |
616 | ||
617 | if (!switched) { | |
da4ab971 | 618 | printf("No!\n"); |
25870f58 | 619 | |
47ea478b | 620 | if (!switch_speed(&dev, &rdata, 10)) { |
da4ab971 MG |
621 | fprintf(stderr, "Can't switch speed!\n"); |
622 | exit(EXIT_FAILURE); | |
25870f58 MG |
623 | } |
624 | } | |
da4ab971 | 625 | } while ((!switched) && (switchcnt--)); |
25870f58 | 626 | |
268d2cc6 MG |
627 | if (!switched) { |
628 | fprintf(stderr, "Too many errors, giving up!\n"); | |
629 | exit(EXIT_FAILURE); | |
630 | } | |
25870f58 | 631 | |
da4ab971 | 632 | printf("Yes!\n"); |
25870f58 MG |
633 | |
634 | printf("Flashing %d blocks", fw->fw_blocks); | |
635 | if (debug) { | |
636 | printf("\n"); | |
637 | } else { | |
638 | printf(": %04u/%04u %c", 0, fw->fw_blocks, twiddlie[0]); | |
639 | fflush(stdout); | |
640 | } | |
641 | ||
642 | for (block = 0; block < fw->fw_blocks; block++) { | |
643 | int first; | |
644 | ||
645 | len = fw->fw[block][2] << 8; | |
646 | len |= fw->fw[block][3]; | |
647 | ||
648 | pos = &(fw->fw[block][2]); | |
649 | ||
650 | len += 2; /* length */ | |
651 | ||
652 | if (debug) | |
653 | hexdump(pos, len, "F> "); | |
654 | ||
655 | first = 1; | |
656 | cnt = 0; | |
657 | do { | |
dfe2e5e2 | 658 | int payloadlen = max_payloadlen - 2; |
25870f58 MG |
659 | int ack = 0; |
660 | ||
661 | if (first) { | |
dfe2e5e2 | 662 | payloadlen = max_payloadlen; |
25870f58 MG |
663 | first = 0; |
664 | } | |
665 | ||
666 | if ((len - (pos - &(fw->fw[block][2]))) < payloadlen) | |
667 | payloadlen = (len - (pos - &(fw->fw[block][2]))); | |
668 | ||
669 | if (((pos + payloadlen) - &(fw->fw[block][2])) == len) | |
670 | ack = 1; | |
671 | ||
672 | memset(&rdata, 0, sizeof(rdata)); | |
673 | ||
674 | memset(out, 0, sizeof(out)); | |
675 | ||
da4ab971 | 676 | out[MSGID] = msgid; |
25870f58 MG |
677 | if (ack) |
678 | out[CTL] = 0x20; | |
679 | out[TYPE] = 0xCA; | |
558a94bb | 680 | SET_SRC(out, my_hmid); |
25870f58 MG |
681 | SET_DST(out, hmid); |
682 | ||
683 | memcpy(&out[PAYLOAD], pos, payloadlen); | |
684 | SET_LEN_FROM_PAYLOADLEN(out, payloadlen); | |
685 | ||
47ea478b | 686 | if (send_hm_message(&dev, &rdata, out)) { |
25870f58 MG |
687 | pos += payloadlen; |
688 | } else { | |
689 | pos = &(fw->fw[block][2]); | |
690 | cnt++; | |
2d1f08ac | 691 | if (cnt == MAX_RETRIES) { |
25870f58 MG |
692 | fprintf(stderr, "\nToo many errors, giving up!\n"); |
693 | exit(EXIT_FAILURE); | |
694 | } else { | |
695 | printf("Flashing %d blocks: %04u/%04u %c", fw->fw_blocks, block + 1, fw->fw_blocks, twiddlie[msgnum % sizeof(twiddlie)]); | |
696 | } | |
697 | } | |
698 | ||
699 | msgnum++; | |
700 | ||
701 | if (!debug) { | |
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)]); | |
704 | fflush(stdout); | |
705 | } | |
706 | } while((pos - &(fw->fw[block][2])) < len); | |
da4ab971 | 707 | msgid++; |
25870f58 MG |
708 | } |
709 | ||
710 | firmware_free(fw); | |
711 | ||
da4ab971 | 712 | printf("\n"); |
25870f58 | 713 | |
47ea478b | 714 | if (!switch_speed(&dev, &rdata, 10)) { |
da4ab971 MG |
715 | fprintf(stderr, "Can't switch speed!\n"); |
716 | exit(EXIT_FAILURE); | |
25870f58 MG |
717 | } |
718 | ||
719 | printf("Waiting for device to reboot\n"); | |
720 | ||
721 | cnt = 10; | |
722 | do { | |
723 | errno = 0; | |
47ea478b MG |
724 | switch(dev.type) { |
725 | case DEVICE_TYPE_CULFW: | |
3b35a8c1 | 726 | pfd = culfw_poll(dev.culfw, 1000); |
47ea478b MG |
727 | break; |
728 | case DEVICE_TYPE_HMCFGUSB: | |
729 | default: | |
3b35a8c1 | 730 | pfd = hmcfgusb_poll(dev.hmcfgusb, 1000); |
47ea478b MG |
731 | break; |
732 | } | |
25870f58 MG |
733 | if ((pfd < 0) && errno) { |
734 | if (errno != ETIMEDOUT) { | |
9dcbf605 | 735 | perror("\n\npoll"); |
25870f58 MG |
736 | exit(EXIT_FAILURE); |
737 | } | |
738 | } | |
739 | if (rdata.message_type == MESSAGE_TYPE_E) { | |
740 | break; | |
741 | } | |
742 | } while(cnt--); | |
743 | ||
744 | if (rdata.message_type == MESSAGE_TYPE_E) { | |
745 | printf("Device rebooted\n"); | |
746 | } | |
747 | ||
47ea478b MG |
748 | switch(dev.type) { |
749 | case DEVICE_TYPE_HMCFGUSB: | |
750 | hmcfgusb_close(dev.hmcfgusb); | |
018f85fa | 751 | hmcfgusb_exit(); |
47ea478b MG |
752 | break; |
753 | case DEVICE_TYPE_CULFW: | |
754 | culfw_close(dev.culfw); | |
755 | break; | |
756 | } | |
25870f58 MG |
757 | |
758 | return EXIT_SUCCESS; | |
759 | } |