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.
14 #include <unistd.h> /* for unlink() */
16 #include <getopt.h> /* for getopt() */
23 #if (__BYTE_ORDER == __BIG_ENDIAN)
24 # define HOST_TO_BE32(x) (x)
25 # define BE32_TO_HOST(x) (x)
26 # define HOST_TO_LE32(x) bswap_32(x)
27 # define LE32_TO_HOST(x) bswap_32(x)
29 # define HOST_TO_BE32(x) bswap_32(x)
30 # define BE32_TO_HOST(x) bswap_32(x)
31 # define HOST_TO_LE32(x) (x)
32 # define LE32_TO_HOST(x) (x)
35 #define MAGIC_FIRMWARE 0x6d726966 /* 'firm' */
36 #define MAGIC_KERNEL 0x676d694b /* 'Kimg' */
37 #define MAGIC_ROOTFS 0x676d6952 /* 'Rimg' */
40 char *file_name
; /* name of the file */
41 uint32_t file_size
; /* length of the file */
49 } __attribute__ ((packed
));
54 } __attribute__ ((packed
));
67 static char *progname
;
69 static char *board_id
;
70 static struct board_info
*board
;
71 static struct file_info kernel_info
;
72 static struct file_info rootfs_info
;
75 static struct board_info boards
[] = {
77 .id
= "ZCN-1523H-2-8",
79 .kernel_len
= 0x170000,
80 .rootfs_len
= 0x610000,
82 .id
= "ZCN-1523H-5-16",
84 .kernel_len
= 0x170000,
85 .rootfs_len
= 0x610000,
87 /* terminating entry */
94 #define ERR(fmt, ...) do { \
96 fprintf(stderr, "[%s] *** error: " fmt "\n", \
97 progname, ## __VA_ARGS__ ); \
100 #define ERRS(fmt, ...) do { \
103 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
104 progname, ## __VA_ARGS__, strerror(save)); \
107 #define DBG(fmt, ...) do { \
108 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
111 static struct board_info
*find_board(char *id
)
113 struct board_info
*ret
;
114 struct board_info
*board
;
117 for (board
= boards
; board
->id
!= NULL
; board
++){
118 if (strcasecmp(id
, board
->id
) == 0) {
127 static void usage(int status
)
129 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
131 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
135 " -B <board> create image for the board specified with <board>\n"
136 " -k <file> read kernel image from the file <file>\n"
137 " -r <file> read rootfs image from the file <file>\n"
138 " -o <file> write output to the file <file>\n"
139 " -h show this screen\n"
145 static int get_file_stat(struct file_info
*fdata
)
150 if (fdata
->file_name
== NULL
)
153 res
= stat(fdata
->file_name
, &st
);
155 ERRS("stat failed on %s", fdata
->file_name
);
159 fdata
->file_size
= st
.st_size
;
163 static int read_to_buf(struct file_info
*fdata
, char *buf
)
166 int ret
= EXIT_FAILURE
;
168 f
= fopen(fdata
->file_name
, "r");
170 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
175 fread(buf
, fdata
->file_size
, 1, f
);
177 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
189 static int check_options(void)
193 if (board_id
== NULL
) {
194 ERR("no board specified");
198 board
= find_board(board_id
);
200 ERR("unknown/unsupported board id \"%s\"", board_id
);
204 if (kernel_info
.file_name
== NULL
) {
205 ERR("no kernel image specified");
209 ret
= get_file_stat(&kernel_info
);
213 if (kernel_info
.file_size
> board
->kernel_len
) {
214 ERR("kernel image is too big");
218 if (rootfs_info
.file_name
== NULL
) {
219 ERR("no rootfs image specified");
223 ret
= get_file_stat(&rootfs_info
);
227 if (rootfs_info
.file_size
> board
->rootfs_len
) {
228 ERR("rootfs image is too big");
232 if (ofname
== NULL
) {
233 ERR("no output file specified");
240 static int write_fw(char *data
, int len
)
243 int ret
= EXIT_FAILURE
;
245 f
= fopen(ofname
, "w");
247 ERRS("could not open \"%s\" for writing", ofname
);
252 fwrite(data
, len
, 1, f
);
254 ERRS("unable to write output file");
258 DBG("firmware file \"%s\" completed", ofname
);
265 if (ret
!= EXIT_SUCCESS
) {
272 static int build_fw(void)
277 int ret
= EXIT_FAILURE
;
278 struct fw_header
*hdr
;
279 struct fw_tail
*tail
;
281 buflen
= 3 * sizeof(struct fw_header
) +
282 kernel_info
.file_size
+ rootfs_info
.file_size
+
283 3 * sizeof(struct fw_tail
);
285 buf
= malloc(buflen
);
287 ERR("no memory for buffer\n");
292 memset(p
, 0, buflen
);
294 /* fill firmware header */
295 hdr
= (struct fw_header
*) p
;
296 hdr
->magic
= HOST_TO_LE32(MAGIC_FIRMWARE
);
297 hdr
->length
= HOST_TO_LE32(buflen
- sizeof(struct fw_header
));
298 p
+= sizeof(struct fw_header
);
300 /* fill kernel block header */
301 hdr
= (struct fw_header
*) p
;
302 hdr
->magic
= HOST_TO_LE32(MAGIC_KERNEL
);
303 hdr
->length
= HOST_TO_LE32(kernel_info
.file_size
+
304 sizeof(struct fw_tail
));
305 p
+= sizeof(struct fw_header
);
307 /* read kernel data */
308 ret
= read_to_buf(&kernel_info
, p
);
312 /* fill firmware tail */
313 tail
= (struct fw_tail
*) (p
+ kernel_info
.file_size
);
314 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
315 tail
->crc
= HOST_TO_BE32(cyg_crc32(p
, kernel_info
.file_size
+
316 sizeof(struct fw_tail
) - 4));
318 p
+= kernel_info
.file_size
+ sizeof(struct fw_tail
);
320 /* fill rootfs block header */
321 hdr
= (struct fw_header
*) p
;
322 hdr
->magic
= HOST_TO_LE32(MAGIC_ROOTFS
);
323 hdr
->length
= HOST_TO_LE32(rootfs_info
.file_size
+
324 sizeof(struct fw_tail
));
325 p
+= sizeof(struct fw_header
);
327 /* read rootfs data */
328 ret
= read_to_buf(&rootfs_info
, p
);
332 /* fill firmware tail */
333 tail
= (struct fw_tail
*) (p
+ rootfs_info
.file_size
);
334 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
335 tail
->crc
= HOST_TO_BE32(cyg_crc32(p
, rootfs_info
.file_size
+
336 sizeof(struct fw_tail
) - 4));
338 p
+= rootfs_info
.file_size
+ sizeof(struct fw_tail
);
340 /* fill firmware tail */
341 tail
= (struct fw_tail
*) p
;
342 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
343 tail
->crc
= HOST_TO_BE32(cyg_crc32(buf
+ sizeof(struct fw_header
),
344 buflen
- sizeof(struct fw_header
) - 4));
346 ret
= write_fw(buf
, buflen
);
358 int main(int argc
, char *argv
[])
360 int ret
= EXIT_FAILURE
;
365 c
= getopt(argc
, argv
, "B:k:r:o:h");
374 kernel_info
.file_name
= optarg
;
377 rootfs_info
.file_name
= optarg
;
391 ret
= check_options();