]>
Commit | Line | Data |
---|---|---|
9fb0f4d2 MG |
1 | /* (not working) flasher for HM-CFG-USB |
2 | * | |
3 | * Copyright (c) 2013 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 "hmcfgusb.h" | |
40 | ||
41 | struct recv_data { | |
b5e57d26 | 42 | int ack; |
9fb0f4d2 MG |
43 | }; |
44 | ||
45 | static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data) | |
46 | { | |
b5e57d26 MG |
47 | struct recv_data *rdata = data; |
48 | ||
49 | if (buf_len != 1) | |
9fb0f4d2 MG |
50 | return 1; |
51 | ||
b5e57d26 | 52 | rdata->ack = buf[0]; |
9fb0f4d2 MG |
53 | |
54 | return 1; | |
55 | } | |
56 | ||
57 | static uint8_t ascii_to_nibble(uint8_t a) | |
58 | { | |
59 | uint8_t c = 0x00; | |
60 | ||
61 | if ((a >= '0') && (a <= '9')) { | |
62 | c = a - '0'; | |
63 | } else if ((a >= 'A') && (a <= 'F')) { | |
64 | c = (a - 'A') + 10; | |
65 | } else if ((a >= 'a') && (a <= 'f')) { | |
66 | c = (a - 'a') + 10; | |
67 | } | |
68 | ||
69 | return c; | |
70 | } | |
71 | ||
72 | int main(int argc, char **argv) | |
73 | { | |
74 | struct hmcfgusb_dev *dev; | |
75 | struct recv_data rdata; | |
04e76de6 MG |
76 | uint8_t out[4096]; |
77 | uint8_t buf[4096]; | |
78 | uint8_t *outp; | |
9fb0f4d2 | 79 | int fd; |
b5e57d26 | 80 | int pfd; |
9fb0f4d2 MG |
81 | int r; |
82 | int i; | |
83 | int cnt; | |
04e76de6 | 84 | int pkt; |
b5e57d26 | 85 | int debug = 0; |
9fb0f4d2 | 86 | |
b5e57d26 | 87 | hmcfgusb_set_debug(debug); |
9fb0f4d2 MG |
88 | |
89 | memset(&rdata, 0, sizeof(rdata)); | |
90 | ||
b5e57d26 MG |
91 | fd = open("hmusbif.enc", O_RDONLY); |
92 | if (fd < 0) { | |
93 | perror("Can't open hmusbif.enc"); | |
94 | exit(EXIT_FAILURE); | |
95 | } | |
96 | ||
9fb0f4d2 MG |
97 | dev = hmcfgusb_init(parse_hmcfgusb, &rdata); |
98 | if (!dev) { | |
99 | fprintf(stderr, "Can't initialize HM-CFG-USB\n"); | |
100 | exit(EXIT_FAILURE); | |
101 | } | |
9fb0f4d2 | 102 | |
b5e57d26 MG |
103 | if (!dev->bootloader) { |
104 | fprintf(stderr, "HM-CFG-USB not in bootloader mode, entering bootloader.\n"); | |
105 | hmcfgusb_enter_bootloader(dev); | |
106 | fprintf(stderr, "\nWaiting for device to reappear...\n"); | |
107 | ||
108 | do { | |
109 | sleep(1); | |
110 | } while ((dev = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL); | |
111 | ||
112 | if (!dev->bootloader) { | |
113 | fprintf(stderr, "Can't enter bootloader, giving up!\n"); | |
114 | exit(EXIT_FAILURE); | |
115 | } | |
9fb0f4d2 MG |
116 | } |
117 | ||
b5e57d26 MG |
118 | printf("HM-CFG-USB opened!\n"); |
119 | ||
9fb0f4d2 | 120 | cnt = 0; |
04e76de6 | 121 | pkt = 0; |
9fb0f4d2 | 122 | do { |
04e76de6 MG |
123 | int len; |
124 | ||
9fb0f4d2 | 125 | memset(buf, 0, sizeof(buf)); |
04e76de6 | 126 | r = read(fd, buf, 4); |
9fb0f4d2 MG |
127 | if (r < 0) { |
128 | perror("read"); | |
129 | exit(EXIT_FAILURE); | |
130 | } else if (r == 0) { | |
131 | break; | |
04e76de6 MG |
132 | } else if (r != 4) { |
133 | printf("can't get length information!\n"); | |
134 | exit(EXIT_FAILURE); | |
9fb0f4d2 | 135 | } |
04e76de6 MG |
136 | |
137 | len = (ascii_to_nibble(buf[0]) & 0xf)<< 4; | |
138 | len |= ascii_to_nibble(buf[1]) & 0xf; | |
139 | len <<= 8; | |
140 | len |= (ascii_to_nibble(buf[2]) & 0xf)<< 4; | |
141 | len |= ascii_to_nibble(buf[3]) & 0xf; | |
142 | ||
04e76de6 MG |
143 | r = read(fd, buf, len * 2); |
144 | if (r < 0) { | |
145 | perror("read"); | |
146 | exit(EXIT_FAILURE); | |
147 | } else if (r < len * 2) { | |
148 | printf("short read, aborting (%d < %d)\n", r, len * 2); | |
149 | break; | |
150 | } | |
151 | ||
9fb0f4d2 | 152 | memset(out, 0, sizeof(out)); |
04e76de6 | 153 | outp = out; |
04e76de6 MG |
154 | *outp++ = (pkt >> 8) & 0xff; |
155 | *outp++ = pkt & 0xff; | |
156 | *outp++ = (len >> 8) & 0xff; | |
157 | *outp++ = len & 0xff; | |
9fb0f4d2 | 158 | for (i = 0; i < r; i+=2) { |
04e76de6 MG |
159 | *outp = (ascii_to_nibble(buf[i]) & 0xf)<< 4; |
160 | *outp |= ascii_to_nibble(buf[i+1]) & 0xf; | |
161 | outp++; | |
9fb0f4d2 | 162 | } |
b5e57d26 | 163 | cnt = outp - out; |
9fb0f4d2 | 164 | printf("Flashing %d bytes...\n", cnt); |
b5e57d26 MG |
165 | if (debug) |
166 | hexdump(out, cnt, "F> "); | |
167 | ||
168 | rdata.ack = 0; | |
169 | if (!hmcfgusb_send(dev, out, cnt, 0)) { | |
170 | perror("hmcfgusb_send"); | |
171 | exit(EXIT_FAILURE); | |
172 | } | |
173 | ||
174 | printf("Waiting for ack...\n"); | |
175 | do { | |
176 | errno = 0; | |
177 | pfd = hmcfgusb_poll(dev, 1); | |
178 | if ((pfd < 0) && errno) { | |
179 | perror("hmcfgusb_poll"); | |
180 | exit(EXIT_FAILURE); | |
181 | } | |
182 | if (rdata.ack) { | |
183 | break; | |
184 | } | |
185 | } while (pfd < 0); | |
186 | ||
187 | if (rdata.ack == 2) { | |
188 | printf("Firmware update successfull!\n"); | |
189 | break; | |
190 | } | |
04e76de6 | 191 | pkt++; |
9fb0f4d2 MG |
192 | } while (r > 0); |
193 | ||
194 | hmcfgusb_close(dev); | |
195 | ||
196 | return EXIT_SUCCESS; | |
197 | } |