0230fe22528e1763c248bb432ed7d46984392337
2 * Copyright (C) 2009 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)
27 # define HOST_TO_BE32(x) bswap_32(x)
28 # define BE32_TO_HOST(x) bswap_32(x)
37 } __attribute__ ((packed
));
50 static char *progname
;
52 static char *version
= "1.00.00";
54 static char *board_id
;
55 static struct board_info
*board
;
57 static struct board_info boards
[] = {
69 /* terminating entry */
76 #define ERR(fmt, ...) do { \
78 fprintf(stderr, "[%s] *** error: " fmt "\n", \
79 progname, ## __VA_ARGS__ ); \
82 #define ERRS(fmt, ...) do { \
85 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
86 progname, ## __VA_ARGS__, strerror(save)); \
89 static struct board_info
*find_board(char *id
)
91 struct board_info
*ret
;
92 struct board_info
*board
;
95 for (board
= boards
; board
->id
!= NULL
; board
++){
96 if (strcasecmp(id
, board
->id
) == 0) {
105 void usage(int status
)
107 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
109 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
113 " -B <board> create image for the board specified with <board>\n"
114 " -i <file> read input from the file <file>\n"
115 " -o <file> write output to the file <file>\n"
116 " -v <version> set image version to <version>\n"
117 " -h show this screen\n"
123 int main(int argc
, char *argv
[])
125 int res
= EXIT_FAILURE
;
130 struct planex_hdr
*hdr
;
134 FILE *outfile
, *infile
;
136 progname
= basename(argv
[0]);
141 c
= getopt(argc
, argv
, "B:i:o:v:h");
167 if (board_id
== NULL
) {
168 ERR("no board specified");
172 board
= find_board(board_id
);
174 ERR("unknown board '%s'", board_id
);
178 if (ifname
== NULL
) {
179 ERR("no input file specified");
183 if (ofname
== NULL
) {
184 ERR("no output file specified");
188 err
= stat(ifname
, &st
);
190 ERRS("stat failed on %s", ifname
);
194 if (st
.st_size
> board
->datalen
) {
195 ERR("file '%s' is too big - max size: 0x%08X (exceeds %lu bytes)\n",
196 ifname
, board
->datalen
, st
.st_size
- board
->datalen
);
200 buflen
= board
->datalen
+ 0x10000;
201 buf
= malloc(buflen
);
203 ERR("no memory for buffer\n");
207 memset(buf
, 0xff, buflen
);
208 hdr
= (struct planex_hdr
*)buf
;
210 hdr
->datalen
= HOST_TO_BE32(board
->datalen
);
211 hdr
->unk1
[0] = board
->unk
[0];
212 hdr
->unk1
[1] = board
->unk
[1];
214 snprintf(hdr
->version
, sizeof(hdr
->version
), "%s", version
);
216 infile
= fopen(ifname
, "r");
217 if (infile
== NULL
) {
218 ERRS("could not open \"%s\" for reading", ifname
);
223 fread(buf
+ sizeof(*hdr
), st
.st_size
, 1, infile
);
225 ERRS("unable to read from file %s", ifname
);
229 seed
= HOST_TO_BE32(board
->seed
);
231 sha1_update(&ctx
, (uchar
*) &seed
, sizeof(seed
));
232 sha1_update(&ctx
, buf
+ sizeof(*hdr
), board
->datalen
);
233 sha1_finish(&ctx
, hdr
->sha1sum
);
235 outfile
= fopen(ofname
, "w");
236 if (outfile
== NULL
) {
237 ERRS("could not open \"%s\" for writing", ofname
);
242 fwrite(buf
, buflen
, 1, outfile
);
244 ERRS("unable to write to file %s", ofname
);
254 if (res
!= EXIT_SUCCESS
) {