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 "\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
;
130 struct board_info
*board
;
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
;
281 struct fw_header
*hdr
;
282 struct fw_tail
*tail
;
284 buflen
= 3 * sizeof(struct fw_header
) +
285 kernel_info
.file_size
+ rootfs_info
.file_size
+
286 3 * sizeof(struct fw_tail
);
288 buf
= malloc(buflen
);
290 ERR("no memory for buffer\n");
295 memset(p
, 0, buflen
);
297 /* fill firmware header */
298 hdr
= (struct fw_header
*) p
;
299 hdr
->magic
= HOST_TO_LE32(MAGIC_FIRMWARE
);
300 hdr
->length
= HOST_TO_LE32(buflen
- sizeof(struct fw_header
));
301 p
+= sizeof(struct fw_header
);
303 /* fill kernel block header */
304 hdr
= (struct fw_header
*) p
;
305 hdr
->magic
= HOST_TO_LE32(MAGIC_KERNEL
);
306 hdr
->length
= HOST_TO_LE32(kernel_info
.file_size
+
307 sizeof(struct fw_tail
));
308 p
+= sizeof(struct fw_header
);
310 /* read kernel data */
311 ret
= read_to_buf(&kernel_info
, p
);
315 /* fill firmware tail */
316 tail
= (struct fw_tail
*) (p
+ kernel_info
.file_size
);
317 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
318 tail
->crc
= HOST_TO_BE32(cyg_crc32(p
, kernel_info
.file_size
+
319 sizeof(struct fw_tail
) - 4));
321 p
+= kernel_info
.file_size
+ sizeof(struct fw_tail
);
323 /* fill rootfs block header */
324 hdr
= (struct fw_header
*) p
;
325 hdr
->magic
= HOST_TO_LE32(MAGIC_ROOTFS
);
326 hdr
->length
= HOST_TO_LE32(rootfs_info
.file_size
+
327 sizeof(struct fw_tail
));
328 p
+= sizeof(struct fw_header
);
330 /* read rootfs data */
331 ret
= read_to_buf(&rootfs_info
, p
);
335 /* fill firmware tail */
336 tail
= (struct fw_tail
*) (p
+ rootfs_info
.file_size
);
337 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
338 tail
->crc
= HOST_TO_BE32(cyg_crc32(p
, rootfs_info
.file_size
+
339 sizeof(struct fw_tail
) - 4));
341 p
+= rootfs_info
.file_size
+ sizeof(struct fw_tail
);
343 /* fill firmware tail */
344 tail
= (struct fw_tail
*) p
;
345 tail
->hw_id
= HOST_TO_BE32(board
->hw_id
);
346 tail
->crc
= HOST_TO_BE32(cyg_crc32(buf
+ sizeof(struct fw_header
),
347 buflen
- sizeof(struct fw_header
) - 4));
349 ret
= write_fw(buf
, buflen
);
361 int main(int argc
, char *argv
[])
363 int ret
= EXIT_FAILURE
;
368 progname
= basename(argv
[0]);
373 c
= getopt(argc
, argv
, "B:k:r:o:h");
382 kernel_info
.file_name
= optarg
;
385 rootfs_info
.file_name
= optarg
;
399 ret
= check_options();
This page took 0.079684 seconds and 5 git commands to generate.