1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
10 #include <unistd.h> /* for unlink() */
12 #include <getopt.h> /* for getopt() */
19 #if (__BYTE_ORDER == __BIG_ENDIAN)
20 # define HOST_TO_BE32(x) (x)
21 # define BE32_TO_HOST(x) (x)
22 # define HOST_TO_LE32(x) bswap_32(x)
23 # define LE32_TO_HOST(x) bswap_32(x)
25 # define HOST_TO_BE32(x) bswap_32(x)
26 # define BE32_TO_HOST(x) bswap_32(x)
27 # define HOST_TO_LE32(x) (x)
28 # define LE32_TO_HOST(x) (x)
31 #define MAGIC_FIRMWARE 0x6d726966 /* 'firm' */
32 #define MAGIC_KERNEL 0x676d694b /* 'Kimg' */
33 #define MAGIC_ROOTFS 0x676d6952 /* 'Rimg' */
36 char *file_name
; /* name of the file */
37 uint32_t file_size
; /* length of the file */
45 } __attribute__ ((packed
));
50 } __attribute__ ((packed
));
63 static char *progname
;
65 static char *board_id
;
66 static struct board_info
*board
;
67 static struct file_info kernel_info
;
68 static struct file_info rootfs_info
;
71 static struct board_info boards
[] = {
73 .id
= "ZCN-1523H-2-8",
75 .kernel_len
= 0x170000,
76 .rootfs_len
= 0x610000,
78 .id
= "ZCN-1523H-5-16",
80 .kernel_len
= 0x170000,
81 .rootfs_len
= 0x610000,
83 /* terminating entry */
90 #define ERR(fmt, ...) do { \
92 fprintf(stderr, "[%s] *** error: " fmt "\n", \
93 progname, ## __VA_ARGS__ ); \
96 #define ERRS(fmt, ...) do { \
99 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
100 progname, ## __VA_ARGS__, strerror(save)); \
103 #define DBG(fmt, ...) do { \
104 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
107 static struct board_info
*find_board(char *id
)
109 struct board_info
*ret
;
110 struct board_info
*board
;
113 for (board
= boards
; board
->id
!= NULL
; board
++){
114 if (strcasecmp(id
, board
->id
) == 0) {
123 static void usage(int status
)
125 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
127 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
131 " -B <board> create image for the board specified with <board>\n"
132 " -k <file> read kernel image from the file <file>\n"
133 " -r <file> read rootfs image from the file <file>\n"
134 " -o <file> write output to the file <file>\n"
135 " -h show this screen\n"
141 static int get_file_stat(struct file_info
*fdata
)
146 if (fdata
->file_name
== NULL
)
149 res
= stat(fdata
->file_name
, &st
);
151 ERRS("stat failed on %s", fdata
->file_name
);
155 fdata
->file_size
= st
.st_size
;
159 static int read_to_buf(struct file_info
*fdata
, char *buf
)
162 int ret
= EXIT_FAILURE
;
164 f
= fopen(fdata
->file_name
, "r");
166 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
171 fread(buf
, fdata
->file_size
, 1, f
);
173 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
185 static int check_options(void)
189 if (board_id
== NULL
) {
190 ERR("no board specified");
194 board
= find_board(board_id
);
196 ERR("unknown/unsupported board id \"%s\"", board_id
);
200 if (kernel_info
.file_name
== NULL
) {
201 ERR("no kernel image specified");
205 ret
= get_file_stat(&kernel_info
);
209 if (kernel_info
.file_size
> board
->kernel_len
) {
210 ERR("kernel image is too big");
214 if (rootfs_info
.file_name
== NULL
) {
215 ERR("no rootfs image specified");
219 ret
= get_file_stat(&rootfs_info
);
223 if (rootfs_info
.file_size
> board
->rootfs_len
) {
224 ERR("rootfs image is too big");
228 if (ofname
== NULL
) {
229 ERR("no output file specified");
236 static int write_fw(char *data
, int len
)
239 int ret
= EXIT_FAILURE
;
241 f
= fopen(ofname
, "w");
243 ERRS("could not open \"%s\" for writing", ofname
);
248 fwrite(data
, len
, 1, f
);
250 ERRS("unable to write output file");
254 DBG("firmware file \"%s\" completed", ofname
);
261 if (ret
!= EXIT_SUCCESS
) {
268 static int build_fw(void)
273 int ret
= EXIT_FAILURE
;
274 struct fw_header
*hdr
;
275 struct fw_tail
*tail
;
277 buflen
= 3 * sizeof(struct fw_header
) +
278 kernel_info
.file_size
+ rootfs_info
.file_size
+
279 3 * sizeof(struct fw_tail
);
281 buf
= malloc(buflen
);
283 ERR("no memory for buffer\n");
288 memset(p
, 0, buflen
);
290 /* fill firmware header */
291 hdr
= (struct fw_header
*) p
;
292 hdr
->magic
= HOST_TO_LE32(MAGIC_FIRMWARE
);
293 hdr
->length
= HOST_TO_LE32(buflen
- sizeof(struct fw_header
));
294 p
+= sizeof(struct fw_header
);
296 /* fill kernel block header */
297 hdr
= (struct fw_header
*) p
;
298 hdr
->magic
= HOST_TO_LE32(MAGIC_KERNEL
);
299 hdr
->length
= HOST_TO_LE32(kernel_info
.file_size
+
300 sizeof(struct fw_tail
));
301 p
+= sizeof(struct fw_header
);
303 /* read kernel data */
304 ret
= read_to_buf(&kernel_info
, p
);
308 /* fill firmware tail */
309 tail
= (struct fw_tail
*) (p
+ kernel_info
.file_size
);
310 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
311 tail
->crc
= HOST_TO_BE32(cyg_crc32(p
, kernel_info
.file_size
+
312 sizeof(struct fw_tail
) - 4));
314 p
+= kernel_info
.file_size
+ sizeof(struct fw_tail
);
316 /* fill rootfs block header */
317 hdr
= (struct fw_header
*) p
;
318 hdr
->magic
= HOST_TO_LE32(MAGIC_ROOTFS
);
319 hdr
->length
= HOST_TO_LE32(rootfs_info
.file_size
+
320 sizeof(struct fw_tail
));
321 p
+= sizeof(struct fw_header
);
323 /* read rootfs data */
324 ret
= read_to_buf(&rootfs_info
, p
);
328 /* fill firmware tail */
329 tail
= (struct fw_tail
*) (p
+ rootfs_info
.file_size
);
330 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
331 tail
->crc
= HOST_TO_BE32(cyg_crc32(p
, rootfs_info
.file_size
+
332 sizeof(struct fw_tail
) - 4));
334 p
+= rootfs_info
.file_size
+ sizeof(struct fw_tail
);
336 /* fill firmware tail */
337 tail
= (struct fw_tail
*) p
;
338 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
339 tail
->crc
= HOST_TO_BE32(cyg_crc32(buf
+ sizeof(struct fw_header
),
340 buflen
- sizeof(struct fw_header
) - 4));
342 ret
= write_fw(buf
, buflen
);
354 int main(int argc
, char *argv
[])
356 int ret
= EXIT_FAILURE
;
361 c
= getopt(argc
, argv
, "B:k:r:o:h");
370 kernel_info
.file_name
= optarg
;
373 rootfs_info
.file_name
= optarg
;
387 ret
= check_options();