1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
11 #include <unistd.h> /* for unlink() */
13 #include <getopt.h> /* for getopt() */
20 #if (__BYTE_ORDER == __BIG_ENDIAN)
21 # define HOST_TO_BE32(x) (x)
22 # define BE32_TO_HOST(x) (x)
23 # define HOST_TO_LE32(x) bswap_32(x)
24 # define LE32_TO_HOST(x) bswap_32(x)
26 # define HOST_TO_BE32(x) bswap_32(x)
27 # define BE32_TO_HOST(x) bswap_32(x)
28 # define HOST_TO_LE32(x) (x)
29 # define LE32_TO_HOST(x) (x)
32 #define MAGIC_FIRMWARE 0x6d726966 /* 'firm' */
33 #define MAGIC_KERNEL 0x676d694b /* 'Kimg' */
34 #define MAGIC_ROOTFS 0x676d6952 /* 'Rimg' */
37 char *file_name
; /* name of the file */
38 uint32_t file_size
; /* length of the file */
46 } __attribute__ ((packed
));
51 } __attribute__ ((packed
));
64 static char *progname
;
66 static char *board_id
;
67 static struct board_info
*board
;
68 static struct file_info kernel_info
;
69 static struct file_info rootfs_info
;
72 static struct board_info boards
[] = {
74 .id
= "ZCN-1523H-2-8",
76 .kernel_len
= 0x170000,
77 .rootfs_len
= 0x610000,
79 .id
= "ZCN-1523H-5-16",
81 .kernel_len
= 0x170000,
82 .rootfs_len
= 0x610000,
84 /* terminating entry */
91 #define ERR(fmt, ...) do { \
93 fprintf(stderr, "[%s] *** error: " fmt "\n", \
94 progname, ## __VA_ARGS__ ); \
97 #define ERRS(fmt, ...) do { \
100 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
101 progname, ## __VA_ARGS__, strerror(save)); \
104 #define DBG(fmt, ...) do { \
105 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
108 static struct board_info
*find_board(char *id
)
110 struct board_info
*ret
;
111 struct board_info
*board
;
114 for (board
= boards
; board
->id
!= NULL
; board
++){
115 if (strcasecmp(id
, board
->id
) == 0) {
124 static void usage(int status
)
126 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
128 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
132 " -B <board> create image for the board specified with <board>\n"
133 " -k <file> read kernel image from the file <file>\n"
134 " -r <file> read rootfs image from the file <file>\n"
135 " -o <file> write output to the file <file>\n"
136 " -h show this screen\n"
142 static int get_file_stat(struct file_info
*fdata
)
147 if (fdata
->file_name
== NULL
)
150 res
= stat(fdata
->file_name
, &st
);
152 ERRS("stat failed on %s", fdata
->file_name
);
156 fdata
->file_size
= st
.st_size
;
160 static int read_to_buf(struct file_info
*fdata
, char *buf
)
163 int ret
= EXIT_FAILURE
;
165 f
= fopen(fdata
->file_name
, "r");
167 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
172 fread(buf
, fdata
->file_size
, 1, f
);
174 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
186 static int check_options(void)
190 if (board_id
== NULL
) {
191 ERR("no board specified");
195 board
= find_board(board_id
);
197 ERR("unknown/unsupported board id \"%s\"", board_id
);
201 if (kernel_info
.file_name
== NULL
) {
202 ERR("no kernel image specified");
206 ret
= get_file_stat(&kernel_info
);
210 if (kernel_info
.file_size
> board
->kernel_len
) {
211 ERR("kernel image is too big");
215 if (rootfs_info
.file_name
== NULL
) {
216 ERR("no rootfs image specified");
220 ret
= get_file_stat(&rootfs_info
);
224 if (rootfs_info
.file_size
> board
->rootfs_len
) {
225 ERR("rootfs image is too big");
229 if (ofname
== NULL
) {
230 ERR("no output file specified");
237 static int write_fw(char *data
, int len
)
240 int ret
= EXIT_FAILURE
;
242 f
= fopen(ofname
, "w");
244 ERRS("could not open \"%s\" for writing", ofname
);
249 fwrite(data
, len
, 1, f
);
251 ERRS("unable to write output file");
255 DBG("firmware file \"%s\" completed", ofname
);
262 if (ret
!= EXIT_SUCCESS
) {
269 static int build_fw(void)
274 int ret
= EXIT_FAILURE
;
275 struct fw_header
*hdr
;
276 struct fw_tail
*tail
;
278 buflen
= 3 * sizeof(struct fw_header
) +
279 kernel_info
.file_size
+ rootfs_info
.file_size
+
280 3 * sizeof(struct fw_tail
);
282 buf
= malloc(buflen
);
284 ERR("no memory for buffer\n");
289 memset(p
, 0, buflen
);
291 /* fill firmware header */
292 hdr
= (struct fw_header
*) p
;
293 hdr
->magic
= HOST_TO_LE32(MAGIC_FIRMWARE
);
294 hdr
->length
= HOST_TO_LE32(buflen
- sizeof(struct fw_header
));
295 p
+= sizeof(struct fw_header
);
297 /* fill kernel block header */
298 hdr
= (struct fw_header
*) p
;
299 hdr
->magic
= HOST_TO_LE32(MAGIC_KERNEL
);
300 hdr
->length
= HOST_TO_LE32(kernel_info
.file_size
+
301 sizeof(struct fw_tail
));
302 p
+= sizeof(struct fw_header
);
304 /* read kernel data */
305 ret
= read_to_buf(&kernel_info
, p
);
309 /* fill firmware tail */
310 tail
= (struct fw_tail
*) (p
+ kernel_info
.file_size
);
311 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
312 tail
->crc
= HOST_TO_BE32(cyg_crc32(p
, kernel_info
.file_size
+
313 sizeof(struct fw_tail
) - 4));
315 p
+= kernel_info
.file_size
+ sizeof(struct fw_tail
);
317 /* fill rootfs block header */
318 hdr
= (struct fw_header
*) p
;
319 hdr
->magic
= HOST_TO_LE32(MAGIC_ROOTFS
);
320 hdr
->length
= HOST_TO_LE32(rootfs_info
.file_size
+
321 sizeof(struct fw_tail
));
322 p
+= sizeof(struct fw_header
);
324 /* read rootfs data */
325 ret
= read_to_buf(&rootfs_info
, p
);
329 /* fill firmware tail */
330 tail
= (struct fw_tail
*) (p
+ rootfs_info
.file_size
);
331 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
332 tail
->crc
= HOST_TO_BE32(cyg_crc32(p
, rootfs_info
.file_size
+
333 sizeof(struct fw_tail
) - 4));
335 p
+= rootfs_info
.file_size
+ sizeof(struct fw_tail
);
337 /* fill firmware tail */
338 tail
= (struct fw_tail
*) p
;
339 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
340 tail
->crc
= HOST_TO_BE32(cyg_crc32(buf
+ sizeof(struct fw_header
),
341 buflen
- sizeof(struct fw_header
) - 4));
343 ret
= write_fw(buf
, buflen
);
355 int main(int argc
, char *argv
[])
357 int ret
= EXIT_FAILURE
;
362 c
= getopt(argc
, argv
, "B:k:r:o:h");
371 kernel_info
.file_name
= optarg
;
374 rootfs_info
.file_name
= optarg
;
388 ret
= check_options();