]>
Commit | Line | Data |
---|---|---|
19cb9745 | 1 | /* flasher for HM-CFG-USB |
9fb0f4d2 | 2 | * |
6c5c2df7 | 3 | * Copyright (c) 2013-14 Michael Gernoth <michael@gernoth.net> |
9fb0f4d2 MG |
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" | |
6c5c2df7 | 39 | #include "firmware.h" |
19cb9745 | 40 | #include "version.h" |
9fb0f4d2 MG |
41 | #include "hmcfgusb.h" |
42 | ||
43 | struct recv_data { | |
b5e57d26 | 44 | int ack; |
9fb0f4d2 MG |
45 | }; |
46 | ||
47 | static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data) | |
48 | { | |
b5e57d26 MG |
49 | struct recv_data *rdata = data; |
50 | ||
51 | if (buf_len != 1) | |
9fb0f4d2 MG |
52 | return 1; |
53 | ||
b5e57d26 | 54 | rdata->ack = buf[0]; |
9fb0f4d2 MG |
55 | |
56 | return 1; | |
57 | } | |
58 | ||
9fb0f4d2 MG |
59 | int main(int argc, char **argv) |
60 | { | |
19cb9745 | 61 | const char twiddlie[] = { '-', '\\', '|', '/' }; |
9fb0f4d2 MG |
62 | struct hmcfgusb_dev *dev; |
63 | struct recv_data rdata; | |
19cb9745 | 64 | uint16_t len; |
6c5c2df7 | 65 | struct firmware *fw; |
19cb9745 | 66 | int block; |
b5e57d26 | 67 | int pfd; |
b5e57d26 | 68 | int debug = 0; |
9fb0f4d2 | 69 | |
19cb9745 | 70 | printf("HM-CFG-USB flasher version " VERSION "\n\n"); |
9fb0f4d2 | 71 | |
19cb9745 MG |
72 | if (argc != 2) { |
73 | if (argc == 1) | |
74 | fprintf(stderr, "Missing firmware filename!\n\n"); | |
9fb0f4d2 | 75 | |
19cb9745 | 76 | fprintf(stderr, "Syntax: %s hmusbif.enc\n\n", argv[0]); |
b5e57d26 MG |
77 | exit(EXIT_FAILURE); |
78 | } | |
79 | ||
6c5c2df7 MG |
80 | fw = firmware_read_firmware(argv[1], debug); |
81 | if (!fw) | |
19cb9745 | 82 | exit(EXIT_FAILURE); |
19cb9745 MG |
83 | |
84 | hmcfgusb_set_debug(debug); | |
85 | ||
86 | memset(&rdata, 0, sizeof(rdata)); | |
87 | ||
88 | dev = hmcfgusb_init(parse_hmcfgusb, &rdata); | |
89 | if (!dev) { | |
90 | fprintf(stderr, "Can't initialize HM-CFG-USB\n"); | |
91 | exit(EXIT_FAILURE); | |
92 | } | |
93 | ||
94 | if (!dev->bootloader) { | |
95 | fprintf(stderr, "\nHM-CFG-USB not in bootloader mode, entering bootloader.\n"); | |
96 | hmcfgusb_enter_bootloader(dev); | |
97 | fprintf(stderr, "\nWaiting for device to reappear...\n"); | |
98 | ||
99 | do { | |
2d1f08ac MG |
100 | if (dev) { |
101 | hmcfgusb_close(dev); | |
102 | } | |
103 | sleep(1); | |
104 | } while (((dev = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (!dev->bootloader)); | |
19cb9745 MG |
105 | } |
106 | ||
107 | printf("\nHM-CFG-USB opened.\n\n"); | |
108 | ||
109 | ||
6c5c2df7 | 110 | printf("Flashing %d blocks", fw->fw_blocks); |
19cb9745 MG |
111 | if (debug) { |
112 | printf("\n"); | |
113 | } else { | |
114 | printf(": %c", twiddlie[0]); | |
115 | fflush(stdout); | |
116 | } | |
117 | ||
6c5c2df7 MG |
118 | for (block = 0; block < fw->fw_blocks; block++) { |
119 | len = fw->fw[block][2] << 8; | |
120 | len |= fw->fw[block][3]; | |
19cb9745 MG |
121 | |
122 | len += 4; /* block nr., length */ | |
123 | ||
124 | if (debug) | |
6c5c2df7 | 125 | hexdump(fw->fw[block], len, "F> "); |
b5e57d26 MG |
126 | |
127 | rdata.ack = 0; | |
6c5c2df7 | 128 | if (!hmcfgusb_send(dev, fw->fw[block], len, 0)) { |
19cb9745 | 129 | perror("\n\nhmcfgusb_send"); |
b5e57d26 MG |
130 | exit(EXIT_FAILURE); |
131 | } | |
132 | ||
19cb9745 MG |
133 | if (debug) |
134 | printf("Waiting for ack...\n"); | |
b5e57d26 MG |
135 | do { |
136 | errno = 0; | |
3b35a8c1 | 137 | pfd = hmcfgusb_poll(dev, 1000); |
b5e57d26 | 138 | if ((pfd < 0) && errno) { |
bedcaba0 MG |
139 | if (errno != ETIMEDOUT) { |
140 | perror("\n\nhmcfgusb_poll"); | |
141 | exit(EXIT_FAILURE); | |
142 | } | |
b5e57d26 MG |
143 | } |
144 | if (rdata.ack) { | |
145 | break; | |
146 | } | |
147 | } while (pfd < 0); | |
148 | ||
149 | if (rdata.ack == 2) { | |
19cb9745 | 150 | printf("\n\nFirmware update successfull!\n"); |
b5e57d26 MG |
151 | break; |
152 | } | |
19cb9745 MG |
153 | |
154 | if (rdata.ack != 1) { | |
155 | fprintf(stderr, "\n\nError flashing block %d, status: %u\n", block, rdata.ack); | |
156 | exit(EXIT_FAILURE); | |
157 | } | |
158 | ||
159 | if (!debug) { | |
160 | printf("\b%c", twiddlie[block % sizeof(twiddlie)]); | |
161 | fflush(stdout); | |
162 | } | |
19cb9745 MG |
163 | } |
164 | ||
6c5c2df7 | 165 | firmware_free(fw); |
9fb0f4d2 MG |
166 | |
167 | hmcfgusb_close(dev); | |
168 | ||
169 | return EXIT_SUCCESS; | |
170 | } |