1 /* flasher for HM-MOD-UART
3 * Copyright (c) 2016-17 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>
41 #include "hmuartlgw.h"
47 static int parse_hmuartlgw(enum hmuartlgw_dst dst
, uint8_t *buf
, int buf_len
, void *data
)
49 struct recv_data
*rdata
= data
;
54 rdata
->ack
= (buf
[0] << 8) | (buf
[1] & 0xff);
59 void flash_hmmoduart_syntax(char *prog
)
61 fprintf(stderr
, "Syntax: %s [options] -U /dev/ttyAMA0 filename.eq3\n\n", prog
);
62 fprintf(stderr
, "Mandatory parameter:\n");
63 fprintf(stderr
, "\t-U device\tuse HM-MOD-UART on given device\n");
64 fprintf(stderr
, "\nOptional parameters:\n");
65 fprintf(stderr
, "\t-V\t\tshow version (" VERSION
")\n");
69 int main(int argc
, char **argv
)
71 const char twiddlie
[] = { '-', '\\', '|', '/' };
72 struct hmuartlgw_dev
*dev
;
74 struct recv_data rdata
;
78 char *filename
= NULL
;
84 while((opt
= getopt(argc
, argv
, "U:V")) != -1) {
90 printf("flash-hmmoduart " VERSION
"\n");
91 printf("Copyright (c) 2016 Michael Gernoth\n\n");
97 flash_hmmoduart_syntax(argv
[0]);
103 if (optind
== argc
- 1) {
104 filename
= argv
[optind
];
108 flash_hmmoduart_syntax(argv
[0]);
112 printf("HM-MOD-UART flasher version " VERSION
"\n\n");
115 fprintf(stderr
, "Missing firmware filename!\n\n");
116 flash_hmmoduart_syntax(argv
[0]);
120 fw
= firmware_read_firmware(filename
, debug
);
124 hmuartlgw_set_debug(debug
);
126 memset(&rdata
, 0, sizeof(rdata
));
128 printf("\nInitializing HM-MOD-UART...\n");
130 dev
= hmuart_init(uart
, parse_hmuartlgw
, &rdata
, 0);
132 fprintf(stderr
, "Can't initialize HM-MOD-UART\n");
136 printf("HM-MOD-UART opened.\n\n");
138 printf("Flashing %d blocks", fw
->fw_blocks
);
142 printf(": %c", twiddlie
[0]);
146 for (block
= 0; block
< fw
->fw_blocks
; block
++) {
147 len
= fw
->fw
[block
][2] << 8;
148 len
|= fw
->fw
[block
][3];
150 len
-= 1; /* + frametype, - crc crc */
152 framedata
= (fw
->fw
[block
]) + 3;
153 framedata
[0] = HMUARTLGW_OS_UPDATE_FIRMWARE
;
156 hexdump(framedata
, len
, "F> ");
160 if (!hmuartlgw_send(dev
, framedata
, len
, HMUARTLGW_OS
)) {
161 perror("\n\nhmuartlgw_send");
166 printf("Waiting for ack...\n");
169 pfd
= hmuartlgw_poll(dev
, 1000);
170 if ((pfd
< 0) && errno
) {
171 if (errno
!= ETIMEDOUT
) {
172 perror("\n\nhmuartlgw_poll");
181 if (rdata
.ack
!= 0x0401) {
182 fprintf(stderr
, "\n\nError flashing block %d, status: %04x\n", block
, rdata
.ack
);
187 printf("\b%c", twiddlie
[block
% sizeof(twiddlie
)]);
192 if (rdata
.ack
== 0x0401) {
193 printf("\n\nFirmware update successfull!\n");
199 hmuartlgw_close(dev
);