]>
cvs.zerfleddert.de Git - hmcfgusb/blob - flash-hmcfgusb.c
1 /* flasher for HM-CFG-USB
3 * Copyright (c) 2013-16 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>
47 static int parse_hmcfgusb(uint8_t *buf
, int buf_len
, void *data
)
49 struct recv_data
*rdata
= data
;
59 void flash_hmcfgusb_syntax(char *prog
)
61 fprintf(stderr
, "Syntax: %s [options] filename.enc\n\n", prog
);
62 fprintf(stderr
, "Possible options:\n");
63 fprintf(stderr
, "\t-S serial\tuse HM-CFG-USB with given serial\n");
64 fprintf(stderr
, "\t-V\t\tshow version (" VERSION
")\n");
68 int main(int argc
, char **argv
)
70 const char twiddlie
[] = { '-', '\\', '|', '/' };
71 struct hmcfgusb_dev
*dev
;
72 struct recv_data rdata
;
76 char *filename
= NULL
;
82 while((opt
= getopt(argc
, argv
, "S:V")) != -1) {
88 printf("flash-hmcfgusb " VERSION
"\n");
89 printf("Copyright (c) 2013-16 Michael Gernoth\n\n");
95 flash_hmcfgusb_syntax(argv
[0]);
101 if (optind
== argc
- 1) {
102 filename
= argv
[optind
];
105 printf("HM-CFG-USB flasher version " VERSION
"\n\n");
108 fprintf(stderr
, "Missing firmware filename!\n\n");
109 flash_hmcfgusb_syntax(argv
[0]);
113 fw
= firmware_read_firmware(filename
, debug
);
117 hmcfgusb_set_debug(debug
);
119 memset(&rdata
, 0, sizeof(rdata
));
121 dev
= hmcfgusb_init(parse_hmcfgusb
, &rdata
, serial
);
123 fprintf(stderr
, "Can't initialize HM-CFG-USB\n");
127 if (!dev
->bootloader
) {
128 fprintf(stderr
, "\nHM-CFG-USB not in bootloader mode, entering bootloader.\n");
129 fprintf(stderr
, "\nWaiting for device to reappear...\n");
133 if (!dev
->bootloader
)
134 hmcfgusb_enter_bootloader(dev
);
138 } while (((dev
= hmcfgusb_init(parse_hmcfgusb
, &rdata
, serial
)) == NULL
) || (!dev
->bootloader
));
141 printf("\nHM-CFG-USB opened.\n\n");
144 printf("Flashing %d blocks", fw
->fw_blocks
);
148 printf(": %c", twiddlie
[0]);
152 for (block
= 0; block
< fw
->fw_blocks
; block
++) {
153 len
= fw
->fw
[block
][2] << 8;
154 len
|= fw
->fw
[block
][3];
156 len
+= 4; /* block nr., length */
159 hexdump(fw
->fw
[block
], len
, "F> ");
162 if (!hmcfgusb_send(dev
, fw
->fw
[block
], len
, 0)) {
163 perror("\n\nhmcfgusb_send");
168 printf("Waiting for ack...\n");
171 pfd
= hmcfgusb_poll(dev
, 1000);
172 if ((pfd
< 0) && errno
) {
173 if (errno
!= ETIMEDOUT
) {
174 perror("\n\nhmcfgusb_poll");
183 if (rdata
.ack
== 2) {
184 printf("\n\nFirmware update successfull!\n");
188 if (rdata
.ack
!= 1) {
189 fprintf(stderr
, "\n\nError flashing block %d, status: %u\n", block
, rdata
.ack
);
194 printf("\b%c", twiddlie
[block
% sizeof(twiddlie
)]);