2 * Copyright (C) 2014 Gabor Juhos <juhosg@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
14 #include <unistd.h> /* for unlink() */
16 #include <getopt.h> /* for getopt() */
21 #include <arpa/inet.h>
22 #include <netinet/in.h>
24 #define MAX_MAGIC_LEN 16
25 #define MAX_MODEL_LEN 32
26 #define MAX_VERSION_LEN 14
27 #define MAX_MTD_NAME_LEN 16
29 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
31 struct edimax_header
{
32 char magic
[MAX_MAGIC_LEN
];
33 char model
[MAX_MODEL_LEN
];
35 unsigned char header_csum
;
36 unsigned char data_csum
;
40 char fw_version
[MAX_VERSION_LEN
];
42 char mtd_name
[MAX_MTD_NAME_LEN
];
43 } __attribute__ ((packed
));
50 static char *progname
;
53 static char *magic
= "eDiMaX";
54 static char *fw_version
= "";
55 static char *mtd_name
;
57 static uint32_t start_addr
;
58 static uint32_t end_addr
;
59 static uint8_t image_type
;
65 #define ERR(fmt, ...) do { \
67 fprintf(stderr, "[%s] *** error: " fmt "\n", \
68 progname, ## __VA_ARGS__ ); \
71 #define ERRS(fmt, ...) do { \
74 fprintf(stderr, "[%s] *** error: " fmt " (%s)\n", \
75 progname, ## __VA_ARGS__, strerror(save)); \
78 #define DBG(fmt, ...) do { \
79 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
82 static void usage(int status
)
84 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
86 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
90 " -e <addr> set end addr to <addr>\n"
91 " -f set force flag\n"
92 " -h show this screen\n"
93 " -i <file> read input data from the file <file>\n"
94 " -o <file> write output to the file <file>\n"
95 " -m <model> set model to <model>\n"
96 " -M <magic> set image magic to <magic>\n"
97 " -n <name> set MTD device name to <name>\n"
98 " -s <addr> set start address to <addr>\n"
99 " -t <type> set image type to <type>\n"
100 " -v <version> set firmware version to <version>\n"
107 str2u32(char *arg
, uint32_t *val
)
113 t
= strtoul(arg
, &err
, 0);
114 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
123 str2u8(char *arg
, uint8_t *val
)
129 t
= strtoul(arg
, &err
, 0);
130 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
138 static int get_file_size(char *name
)
143 res
= stat(name
, &st
);
145 ERRS("stat failed on %s", name
);
152 static int read_to_buf(char *name
, char *buf
, int buflen
)
155 int ret
= EXIT_FAILURE
;
157 f
= fopen(name
, "r");
159 ERRS("could not open \"%s\" for reading", name
);
164 fread(buf
, buflen
, 1, f
);
166 ERRS("unable to read from file \"%s\"", name
);
178 static int check_options(void)
180 #define CHKSTR(_name, _msg) \
182 if (_name == NULL) { \
183 ERR("no %s specified", _msg); \
188 #define CHKSTRLEN(_name, _msg) \
191 CHKSTR(_name, _msg); \
192 field_len = FIELD_SIZEOF(struct edimax_header, _name) - 1; \
193 if (strlen(_name) > field_len) { \
194 ERR("'%s' is too long, max %s length is %d", \
195 _name, _msg, field_len); \
200 CHKSTR(ofname
, "output file");
201 CHKSTR(ifname
, "input file");
203 CHKSTRLEN(magic
, "magic");
204 CHKSTRLEN(model
, "model");
205 CHKSTRLEN(mtd_name
, "MTD device name");
206 CHKSTRLEN(fw_version
, "firware version");
208 data_size
= get_file_size(ifname
);
215 static int write_fw(char *data
, int len
)
218 int ret
= EXIT_FAILURE
;
220 f
= fopen(ofname
, "w");
222 ERRS("could not open \"%s\" for writing", ofname
);
227 fwrite(data
, len
, 1, f
);
229 ERRS("unable to write output file");
233 DBG("firmware file \"%s\" completed", ofname
);
240 if (ret
!= EXIT_SUCCESS
) {
247 static unsigned char checksum(unsigned char *p
, unsigned len
)
249 unsigned char csum
= 0;
259 static int build_fw(void)
264 struct edimax_header
*hdr
;
265 int ret
= EXIT_FAILURE
;
267 buflen
= sizeof(struct edimax_header
) + data_size
;
269 buf
= malloc(buflen
);
271 ERR("no memory for buffer\n");
275 data
= buf
+ sizeof(struct edimax_header
);
277 /* read input file */
278 ret
= read_to_buf(ifname
, data
, data_size
);
282 /* fill firmware header */
283 hdr
= (struct edimax_header
*)buf
;
284 memset(hdr
, 0, sizeof(struct edimax_header
));
286 strncpy(hdr
->model
, model
, sizeof(hdr
->model
));
287 strncpy(hdr
->magic
, magic
, sizeof(hdr
->magic
));
288 strncpy(hdr
->fw_version
, fw_version
, sizeof(hdr
->fw_version
));
289 strncpy(hdr
->mtd_name
, mtd_name
, sizeof(hdr
->mtd_name
));
292 hdr
->start_addr
= htonl(start_addr
);
293 hdr
->end_addr
= htonl(end_addr
);
294 hdr
->data_size
= htonl(data_size
);
295 hdr
->type
= image_type
;
297 hdr
->data_csum
= checksum((unsigned char *)data
, data_size
);
298 hdr
->header_csum
= checksum((unsigned char *)hdr
,
299 sizeof(struct edimax_header
));
301 ret
= write_fw(buf
, buflen
);
313 int main(int argc
, char *argv
[])
315 int ret
= EXIT_FAILURE
;
317 progname
= basename(argv
[0]);
322 c
= getopt(argc
, argv
, "e:fhi:o:m:M:n:s:t:v:");
328 if (str2u32(optarg
, &end_addr
)) {
329 ERR("%s is invalid '%s'",
330 "end address", optarg
);
356 if (str2u32(optarg
, &start_addr
)) {
357 ERR("%s is invalid '%s'",
358 "start address", optarg
);
363 if (str2u8(optarg
, &image_type
)) {
364 ERR("%s is invalid '%s'",
365 "image type", optarg
);
378 ret
= check_options();