]> git.zerfleddert.de Git - hmcfgusb/blame - flash-hmcfgusb.c
Allow the user to specify the compiler via environment variable
[hmcfgusb] / flash-hmcfgusb.c
CommitLineData
19cb9745 1/* flasher for HM-CFG-USB
9fb0f4d2 2 *
7ba4ea19 3 * Copyright (c) 2013-16 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
43struct recv_data {
b5e57d26 44 int ack;
9fb0f4d2
MG
45};
46
47static 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
f51714be
MG
59void flash_hmcfgusb_syntax(char *prog)
60{
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");
65
66}
67
9fb0f4d2
MG
68int main(int argc, char **argv)
69{
19cb9745 70 const char twiddlie[] = { '-', '\\', '|', '/' };
9fb0f4d2
MG
71 struct hmcfgusb_dev *dev;
72 struct recv_data rdata;
19cb9745 73 uint16_t len;
6c5c2df7 74 struct firmware *fw;
7c54cb57 75 char *serial = NULL;
f51714be 76 char *filename = NULL;
19cb9745 77 int block;
b5e57d26 78 int pfd;
f51714be 79 int opt;
b5e57d26 80 int debug = 0;
9fb0f4d2 81
f51714be
MG
82 while((opt = getopt(argc, argv, "S:V")) != -1) {
83 switch (opt) {
84 case 'S':
85 serial = optarg;
86 break;
87 case 'V':
88 printf("flash-hmcfgusb " VERSION "\n");
7ba4ea19 89 printf("Copyright (c) 2013-16 Michael Gernoth\n\n");
f51714be
MG
90 exit(EXIT_SUCCESS);
91 case 'h':
92 case ':':
93 case '?':
94 default:
95 flash_hmcfgusb_syntax(argv[0]);
96 exit(EXIT_FAILURE);
97 break;
98 }
99 }
100
101 if (optind == argc - 1) {
102 filename = argv[optind];
103 }
9fb0f4d2 104
f51714be 105 printf("HM-CFG-USB flasher version " VERSION "\n\n");
9fb0f4d2 106
f51714be
MG
107 if (!filename) {
108 fprintf(stderr, "Missing firmware filename!\n\n");
109 flash_hmcfgusb_syntax(argv[0]);
b5e57d26
MG
110 exit(EXIT_FAILURE);
111 }
112
f51714be 113 fw = firmware_read_firmware(filename, debug);
6c5c2df7 114 if (!fw)
19cb9745 115 exit(EXIT_FAILURE);
19cb9745
MG
116
117 hmcfgusb_set_debug(debug);
118
119 memset(&rdata, 0, sizeof(rdata));
120
f51714be 121 dev = hmcfgusb_init(parse_hmcfgusb, &rdata, serial);
19cb9745
MG
122 if (!dev) {
123 fprintf(stderr, "Can't initialize HM-CFG-USB\n");
124 exit(EXIT_FAILURE);
125 }
126
127 if (!dev->bootloader) {
128 fprintf(stderr, "\nHM-CFG-USB not in bootloader mode, entering bootloader.\n");
19cb9745
MG
129 fprintf(stderr, "\nWaiting for device to reappear...\n");
130
131 do {
2d1f08ac 132 if (dev) {
018f85fa
MG
133 if (!dev->bootloader)
134 hmcfgusb_enter_bootloader(dev);
2d1f08ac
MG
135 hmcfgusb_close(dev);
136 }
137 sleep(1);
f51714be 138 } while (((dev = hmcfgusb_init(parse_hmcfgusb, &rdata, serial)) == NULL) || (!dev->bootloader));
19cb9745
MG
139 }
140
141 printf("\nHM-CFG-USB opened.\n\n");
142
143
6c5c2df7 144 printf("Flashing %d blocks", fw->fw_blocks);
19cb9745
MG
145 if (debug) {
146 printf("\n");
147 } else {
148 printf(": %c", twiddlie[0]);
149 fflush(stdout);
150 }
151
6c5c2df7
MG
152 for (block = 0; block < fw->fw_blocks; block++) {
153 len = fw->fw[block][2] << 8;
154 len |= fw->fw[block][3];
19cb9745
MG
155
156 len += 4; /* block nr., length */
157
158 if (debug)
6c5c2df7 159 hexdump(fw->fw[block], len, "F> ");
b5e57d26
MG
160
161 rdata.ack = 0;
6c5c2df7 162 if (!hmcfgusb_send(dev, fw->fw[block], len, 0)) {
19cb9745 163 perror("\n\nhmcfgusb_send");
b5e57d26
MG
164 exit(EXIT_FAILURE);
165 }
166
19cb9745
MG
167 if (debug)
168 printf("Waiting for ack...\n");
b5e57d26
MG
169 do {
170 errno = 0;
3b35a8c1 171 pfd = hmcfgusb_poll(dev, 1000);
b5e57d26 172 if ((pfd < 0) && errno) {
bedcaba0
MG
173 if (errno != ETIMEDOUT) {
174 perror("\n\nhmcfgusb_poll");
175 exit(EXIT_FAILURE);
176 }
b5e57d26
MG
177 }
178 if (rdata.ack) {
179 break;
180 }
181 } while (pfd < 0);
182
183 if (rdata.ack == 2) {
19cb9745 184 printf("\n\nFirmware update successfull!\n");
b5e57d26
MG
185 break;
186 }
19cb9745
MG
187
188 if (rdata.ack != 1) {
189 fprintf(stderr, "\n\nError flashing block %d, status: %u\n", block, rdata.ack);
190 exit(EXIT_FAILURE);
191 }
192
193 if (!debug) {
194 printf("\b%c", twiddlie[block % sizeof(twiddlie)]);
195 fflush(stdout);
196 }
19cb9745
MG
197 }
198
6c5c2df7 199 firmware_free(fw);
9fb0f4d2
MG
200
201 hmcfgusb_close(dev);
018f85fa 202 hmcfgusb_exit();
9fb0f4d2
MG
203
204 return EXIT_SUCCESS;
205}
Impressum, Datenschutz