1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Gabor Juhos <juhosg@openwrt.org>
10 #include <unistd.h> /* for unlink() */
12 #include <getopt.h> /* for getopt() */
17 #include <arpa/inet.h>
18 #include <netinet/in.h>
20 #define MAX_MAGIC_LEN 16
21 #define MAX_MODEL_LEN 32
22 #define MAX_VERSION_LEN 14
23 #define MAX_MTD_NAME_LEN 16
25 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
27 struct edimax_header
{
28 char magic
[MAX_MAGIC_LEN
];
29 char model
[MAX_MODEL_LEN
];
31 unsigned char header_csum
;
32 unsigned char data_csum
;
36 char fw_version
[MAX_VERSION_LEN
];
38 char mtd_name
[MAX_MTD_NAME_LEN
];
39 } __attribute__ ((packed
));
46 static char *progname
;
49 static char *magic
= "eDiMaX";
50 static char *fw_version
= "";
51 static char *mtd_name
;
53 static uint32_t start_addr
;
54 static uint32_t end_addr
;
55 static uint8_t image_type
;
61 #define ERR(fmt, ...) do { \
63 fprintf(stderr, "[%s] *** error: " fmt "\n", \
64 progname, ## __VA_ARGS__ ); \
67 #define ERRS(fmt, ...) do { \
70 fprintf(stderr, "[%s] *** error: " fmt " (%s)\n", \
71 progname, ## __VA_ARGS__, strerror(save)); \
74 #define DBG(fmt, ...) do { \
75 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
78 static void usage(int status
)
80 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
82 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
86 " -e <addr> set end addr to <addr>\n"
87 " -f set force flag\n"
88 " -h show this screen\n"
89 " -i <file> read input data from the file <file>\n"
90 " -o <file> write output to the file <file>\n"
91 " -m <model> set model to <model>\n"
92 " -M <magic> set image magic to <magic>\n"
93 " -n <name> set MTD device name to <name>\n"
94 " -s <addr> set start address to <addr>\n"
95 " -t <type> set image type to <type>\n"
96 " -v <version> set firmware version to <version>\n"
103 str2u32(char *arg
, uint32_t *val
)
109 t
= strtoul(arg
, &err
, 0);
110 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
119 str2u8(char *arg
, uint8_t *val
)
125 t
= strtoul(arg
, &err
, 0);
126 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
134 static int get_file_size(char *name
)
139 res
= stat(name
, &st
);
141 ERRS("stat failed on %s", name
);
148 static int read_to_buf(char *name
, char *buf
, int buflen
)
151 int ret
= EXIT_FAILURE
;
153 f
= fopen(name
, "r");
155 ERRS("could not open \"%s\" for reading", name
);
160 fread(buf
, buflen
, 1, f
);
162 ERRS("unable to read from file \"%s\"", name
);
174 static int check_options(void)
176 #define CHKSTR(_name, _msg) \
178 if (_name == NULL) { \
179 ERR("no %s specified", _msg); \
184 #define CHKSTRLEN(_name, _msg) \
187 CHKSTR(_name, _msg); \
188 field_len = FIELD_SIZEOF(struct edimax_header, _name) - 1; \
189 if (strlen(_name) > field_len) { \
190 ERR("'%s' is too long, max %s length is %d", \
191 _name, _msg, field_len); \
196 CHKSTR(ofname
, "output file");
197 CHKSTR(ifname
, "input file");
199 CHKSTRLEN(magic
, "magic");
200 CHKSTRLEN(model
, "model");
201 CHKSTRLEN(mtd_name
, "MTD device name");
202 CHKSTRLEN(fw_version
, "firware version");
204 data_size
= get_file_size(ifname
);
211 static int write_fw(char *data
, int len
)
214 int ret
= EXIT_FAILURE
;
216 f
= fopen(ofname
, "w");
218 ERRS("could not open \"%s\" for writing", ofname
);
223 fwrite(data
, len
, 1, f
);
225 ERRS("unable to write output file");
229 DBG("firmware file \"%s\" completed", ofname
);
236 if (ret
!= EXIT_SUCCESS
) {
243 static unsigned char checksum(unsigned char *p
, unsigned len
)
245 unsigned char csum
= 0;
255 static int build_fw(void)
260 struct edimax_header
*hdr
;
261 int ret
= EXIT_FAILURE
;
263 buflen
= sizeof(struct edimax_header
) + data_size
;
265 buf
= malloc(buflen
);
267 ERR("no memory for buffer\n");
271 data
= buf
+ sizeof(struct edimax_header
);
273 /* read input file */
274 ret
= read_to_buf(ifname
, data
, data_size
);
278 /* fill firmware header */
279 hdr
= (struct edimax_header
*)buf
;
280 memset(hdr
, 0, sizeof(struct edimax_header
));
282 strncpy(hdr
->model
, model
, sizeof(hdr
->model
));
283 strncpy(hdr
->magic
, magic
, sizeof(hdr
->magic
));
284 strncpy(hdr
->fw_version
, fw_version
, sizeof(hdr
->fw_version
));
285 strncpy(hdr
->mtd_name
, mtd_name
, sizeof(hdr
->mtd_name
));
288 hdr
->start_addr
= htonl(start_addr
);
289 hdr
->end_addr
= htonl(end_addr
);
290 hdr
->data_size
= htonl(data_size
);
291 hdr
->type
= image_type
;
293 hdr
->data_csum
= checksum((unsigned char *)data
, data_size
);
294 hdr
->header_csum
= checksum((unsigned char *)hdr
,
295 sizeof(struct edimax_header
));
297 ret
= write_fw(buf
, buflen
);
309 int main(int argc
, char *argv
[])
311 int ret
= EXIT_FAILURE
;
313 progname
= basename(argv
[0]);
318 c
= getopt(argc
, argv
, "e:fhi:o:m:M:n:s:t:v:");
324 if (str2u32(optarg
, &end_addr
)) {
325 ERR("%s is invalid '%s'",
326 "end address", optarg
);
352 if (str2u32(optarg
, &start_addr
)) {
353 ERR("%s is invalid '%s'",
354 "start address", optarg
);
359 if (str2u8(optarg
, &image_type
)) {
360 ERR("%s is invalid '%s'",
361 "image type", optarg
);
374 ret
= check_options();