2 * Copyright (C) 2010 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.
15 #include <unistd.h> /* for unlink() */
17 #include <getopt.h> /* for getopt() */
24 #if (__BYTE_ORDER == __BIG_ENDIAN)
25 # define HOST_TO_BE32(x) (x)
26 # define BE32_TO_HOST(x) (x)
27 # define HOST_TO_LE32(x) bswap_32(x)
28 # define LE32_TO_HOST(x) bswap_32(x)
30 # define HOST_TO_BE32(x) bswap_32(x)
31 # define BE32_TO_HOST(x) bswap_32(x)
32 # define HOST_TO_LE32(x) (x)
33 # define LE32_TO_HOST(x) (x)
36 #define MAGIC_FIRMWARE 0x6d726966 /* 'firm' */
37 #define MAGIC_KERNEL 0x676d694b /* 'Kimg' */
38 #define MAGIC_ROOTFS 0x676d6952 /* 'Rimg' */
41 char *file_name
; /* name of the file */
42 uint32_t file_size
; /* length of the file */
50 } __attribute__ ((packed
));
55 } __attribute__ ((packed
));
68 static char *progname
;
70 static char *board_id
;
71 static struct board_info
*board
;
72 static struct file_info kernel_info
;
73 static struct file_info rootfs_info
;
76 static struct board_info boards
[] = {
78 .id
= "ZCN-1523H-2-8",
80 .kernel_len
= 0x170000,
81 .rootfs_len
= 0x610000,
83 .id
= "ZCN-1523H-5-16",
85 .kernel_len
= 0x170000,
86 .rootfs_len
= 0x610000,
88 /* terminating entry */
95 #define ERR(fmt, ...) do { \
97 fprintf(stderr, "[%s] *** error: " fmt "\n", \
98 progname, ## __VA_ARGS__ ); \
101 #define ERRS(fmt, ...) do { \
104 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
105 progname, ## __VA_ARGS__, strerror(save)); \
108 #define DBG(fmt, ...) do { \
109 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
112 static struct board_info
*find_board(char *id
)
114 struct board_info
*ret
;
115 struct board_info
*board
;
118 for (board
= boards
; board
->id
!= NULL
; board
++){
119 if (strcasecmp(id
, board
->id
) == 0) {
128 static void usage(int status
)
130 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
132 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
136 " -B <board> create image for the board specified with <board>\n"
137 " -k <file> read kernel image from the file <file>\n"
138 " -r <file> read rootfs image from the file <file>\n"
139 " -o <file> write output to the file <file>\n"
140 " -h show this screen\n"
146 static int get_file_stat(struct file_info
*fdata
)
151 if (fdata
->file_name
== NULL
)
154 res
= stat(fdata
->file_name
, &st
);
156 ERRS("stat failed on %s", fdata
->file_name
);
160 fdata
->file_size
= st
.st_size
;
164 static int read_to_buf(struct file_info
*fdata
, char *buf
)
167 int ret
= EXIT_FAILURE
;
169 f
= fopen(fdata
->file_name
, "r");
171 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
176 fread(buf
, fdata
->file_size
, 1, f
);
178 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
190 static int check_options(void)
194 if (board_id
== NULL
) {
195 ERR("no board specified");
199 board
= find_board(board_id
);
201 ERR("unknown/unsupported board id \"%s\"", board_id
);
205 if (kernel_info
.file_name
== NULL
) {
206 ERR("no kernel image specified");
210 ret
= get_file_stat(&kernel_info
);
214 if (kernel_info
.file_size
> board
->kernel_len
) {
215 ERR("kernel image is too big");
219 if (rootfs_info
.file_name
== NULL
) {
220 ERR("no rootfs image specified");
224 ret
= get_file_stat(&rootfs_info
);
228 if (rootfs_info
.file_size
> board
->rootfs_len
) {
229 ERR("rootfs image is too big");
233 if (ofname
== NULL
) {
234 ERR("no output file specified");
241 static int write_fw(char *data
, int len
)
244 int ret
= EXIT_FAILURE
;
246 f
= fopen(ofname
, "w");
248 ERRS("could not open \"%s\" for writing", ofname
);
253 fwrite(data
, len
, 1, f
);
255 ERRS("unable to write output file");
259 DBG("firmware file \"%s\" completed", ofname
);
266 if (ret
!= EXIT_SUCCESS
) {
273 static int build_fw(void)
278 int ret
= EXIT_FAILURE
;
279 struct fw_header
*hdr
;
280 struct fw_tail
*tail
;
282 buflen
= 3 * sizeof(struct fw_header
) +
283 kernel_info
.file_size
+ rootfs_info
.file_size
+
284 3 * sizeof(struct fw_tail
);
286 buf
= malloc(buflen
);
288 ERR("no memory for buffer\n");
293 memset(p
, 0, buflen
);
295 /* fill firmware header */
296 hdr
= (struct fw_header
*) p
;
297 hdr
->magic
= HOST_TO_LE32(MAGIC_FIRMWARE
);
298 hdr
->length
= HOST_TO_LE32(buflen
- sizeof(struct fw_header
));
299 p
+= sizeof(struct fw_header
);
301 /* fill kernel block header */
302 hdr
= (struct fw_header
*) p
;
303 hdr
->magic
= HOST_TO_LE32(MAGIC_KERNEL
);
304 hdr
->length
= HOST_TO_LE32(kernel_info
.file_size
+
305 sizeof(struct fw_tail
));
306 p
+= sizeof(struct fw_header
);
308 /* read kernel data */
309 ret
= read_to_buf(&kernel_info
, p
);
313 /* fill firmware tail */
314 tail
= (struct fw_tail
*) (p
+ kernel_info
.file_size
);
315 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
316 tail
->crc
= HOST_TO_BE32(cyg_crc32(p
, kernel_info
.file_size
+
317 sizeof(struct fw_tail
) - 4));
319 p
+= kernel_info
.file_size
+ sizeof(struct fw_tail
);
321 /* fill rootfs block header */
322 hdr
= (struct fw_header
*) p
;
323 hdr
->magic
= HOST_TO_LE32(MAGIC_ROOTFS
);
324 hdr
->length
= HOST_TO_LE32(rootfs_info
.file_size
+
325 sizeof(struct fw_tail
));
326 p
+= sizeof(struct fw_header
);
328 /* read rootfs data */
329 ret
= read_to_buf(&rootfs_info
, p
);
333 /* fill firmware tail */
334 tail
= (struct fw_tail
*) (p
+ rootfs_info
.file_size
);
335 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
336 tail
->crc
= HOST_TO_BE32(cyg_crc32(p
, rootfs_info
.file_size
+
337 sizeof(struct fw_tail
) - 4));
339 p
+= rootfs_info
.file_size
+ sizeof(struct fw_tail
);
341 /* fill firmware tail */
342 tail
= (struct fw_tail
*) p
;
343 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
344 tail
->crc
= HOST_TO_BE32(cyg_crc32(buf
+ sizeof(struct fw_header
),
345 buflen
- sizeof(struct fw_header
) - 4));
347 ret
= write_fw(buf
, buflen
);
359 int main(int argc
, char *argv
[])
361 int ret
= EXIT_FAILURE
;
366 c
= getopt(argc
, argv
, "B:k:r:o:h");
375 kernel_info
.file_name
= optarg
;
378 rootfs_info
.file_name
= optarg
;
392 ret
= check_options();