1 /* flasher for HM-MOD-UART
3 * Copyright (c) 2016 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 dev
= hmuart_init(uart
, parse_hmuartlgw
, &rdata
, 0);
130 fprintf(stderr
, "Can't initialize HM-MOD-UART\n");
134 printf("\nHM-MOD-UART opened.\n\n");
136 printf("Flashing %d blocks", fw
->fw_blocks
);
140 printf(": %c", twiddlie
[0]);
144 for (block
= 0; block
< fw
->fw_blocks
; block
++) {
145 len
= fw
->fw
[block
][2] << 8;
146 len
|= fw
->fw
[block
][3];
148 len
-= 1; /* + frametype, - crc crc */
150 framedata
= (fw
->fw
[block
]) + 3;
151 framedata
[0] = HMUARTLGW_OS_UPDATE_FIRMWARE
;
154 hexdump(framedata
, len
, "F> ");
158 if (!hmuartlgw_send(dev
, framedata
, len
, HMUARTLGW_OS
)) {
159 perror("\n\nhmuartlgw_send");
164 printf("Waiting for ack...\n");
167 pfd
= hmuartlgw_poll(dev
, 1000);
168 if ((pfd
< 0) && errno
) {
169 if (errno
!= ETIMEDOUT
) {
170 perror("\n\nhmuartlgw_poll");
179 if (rdata
.ack
!= 0x0401) {
180 fprintf(stderr
, "\n\nError flashing block %d, status: %04x\n", block
, rdata
.ack
);
185 printf("\b%c", twiddlie
[block
% sizeof(twiddlie
)]);
190 if (rdata
.ack
== 0x0401) {
191 printf("\n\nFirmware update successfull!\n");
197 hmuartlgw_close(dev
);