]>
git.zerfleddert.de Git - hmcfgusb/blob - flash-hmcfgusb.c
aebf78151e4935629296b56967bd3d9b8942713f
1 /* (not working) flasher for HM-CFG-USB
3 * Copyright (c) 2013 Michael Gernoth <michael@gernoth.net>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
32 #include <sys/types.h>
36 #include <libusb-1.0/libusb.h>
45 static int parse_hmcfgusb(uint8_t *buf
, int buf_len
, void *data
)
47 struct recv_data
*rdata
= data
;
57 static uint8_t ascii_to_nibble(uint8_t a
)
61 if ((a
>= '0') && (a
<= '9')) {
63 } else if ((a
>= 'A') && (a
<= 'F')) {
65 } else if ((a
>= 'a') && (a
<= 'f')) {
72 int main(int argc
, char **argv
)
74 struct hmcfgusb_dev
*dev
;
75 struct recv_data rdata
;
87 hmcfgusb_set_debug(debug
);
89 memset(&rdata
, 0, sizeof(rdata
));
91 fd
= open("hmusbif.enc", O_RDONLY
);
93 perror("Can't open hmusbif.enc");
97 dev
= hmcfgusb_init(parse_hmcfgusb
, &rdata
);
99 fprintf(stderr
, "Can't initialize HM-CFG-USB\n");
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");
110 } while ((dev
= hmcfgusb_init(parse_hmcfgusb
, &rdata
)) == NULL
);
112 if (!dev
->bootloader
) {
113 fprintf(stderr
, "Can't enter bootloader, giving up!\n");
118 printf("HM-CFG-USB opened!\n");
125 memset(buf
, 0, sizeof(buf
));
126 r
= read(fd
, buf
, 4);
133 printf("can't get length information!\n");
137 len
= (ascii_to_nibble(buf
[0]) & 0xf)<< 4;
138 len
|= ascii_to_nibble(buf
[1]) & 0xf;
140 len
|= (ascii_to_nibble(buf
[2]) & 0xf)<< 4;
141 len
|= ascii_to_nibble(buf
[3]) & 0xf;
143 r
= read(fd
, buf
, len
* 2);
147 } else if (r
< len
* 2) {
148 printf("short read, aborting (%d < %d)\n", r
, len
* 2);
152 memset(out
, 0, sizeof(out
));
154 *outp
++ = (pkt
>> 8) & 0xff;
155 *outp
++ = pkt
& 0xff;
156 *outp
++ = (len
>> 8) & 0xff;
157 *outp
++ = len
& 0xff;
158 for (i
= 0; i
< r
; i
+=2) {
159 *outp
= (ascii_to_nibble(buf
[i
]) & 0xf)<< 4;
160 *outp
|= ascii_to_nibble(buf
[i
+1]) & 0xf;
164 printf("Flashing %d bytes...\n", cnt
);
166 hexdump(out
, cnt
, "F> ");
169 if (!hmcfgusb_send(dev
, out
, cnt
, 0)) {
170 perror("hmcfgusb_send");
174 printf("Waiting for ack...\n");
177 pfd
= hmcfgusb_poll(dev
, 1);
178 if ((pfd
< 0) && errno
) {
179 perror("hmcfgusb_poll");
187 if (rdata
.ack
== 2) {
188 printf("Firmware update successfull!\n");