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
));
49 static char *progname
;
51 static char *version
= "1.00.00";
53 static char *board_id
;
54 static struct board_info
*board
;
56 static struct board_info boards
[] = {
66 /* terminating entry */
73 #define ERR(fmt, ...) do { \
75 fprintf(stderr, "[%s] *** error: " fmt "\n", \
76 progname, ## __VA_ARGS__ ); \
79 #define ERRS(fmt, ...) do { \
82 fprintf(stderr, "[%s] *** error: " fmt "\n", \
83 progname, ## __VA_ARGS__, strerror(save)); \
86 static struct board_info
*find_board(char *id
)
88 struct board_info
*ret
;
89 struct board_info
*board
;
92 for (board
= boards
; board
->id
!= NULL
; board
++){
93 if (strcasecmp(id
, board
->id
) == 0) {
102 void usage(int status
)
104 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
105 struct board_info
*board
;
107 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
111 " -B <board> create image for the board specified with <board>\n"
112 " -i <file> read input from the file <file>\n"
113 " -o <file> write output to the file <file>\n"
114 " -v <version> set image version to <version>\n"
115 " -h show this screen\n"
121 int main(int argc
, char *argv
[])
123 int res
= EXIT_FAILURE
;
128 struct planex_hdr
*hdr
;
132 FILE *outfile
, *infile
;
134 progname
= basename(argv
[0]);
139 c
= getopt(argc
, argv
, "B:i:o:v:h");
165 if (board_id
== NULL
) {
166 ERR("no board specified");
170 board
= find_board(board_id
);
172 ERR("unknown board '%s'", board_id
);
176 if (ifname
== NULL
) {
177 ERR("no input file specified");
181 if (ofname
== NULL
) {
182 ERR("no output file specified");
186 err
= stat(ifname
, &st
);
188 ERRS("stat failed on %s", ifname
);
192 buflen
= (st
.st_size
+ 3) & ~3;
193 buflen
+= sizeof(*hdr
);
195 buf
= malloc(buflen
);
197 ERR("no memory for buffer\n");
201 memset(buf
, 0xff, buflen
);
202 hdr
= (struct planex_hdr
*)buf
;
204 hdr
->datalen
= HOST_TO_BE32(buflen
- sizeof(*hdr
));
205 hdr
->unk1
[0] = board
->unk
[0];
206 hdr
->unk1
[1] = board
->unk
[1];
208 snprintf(hdr
->version
, sizeof(hdr
->version
), "%s", version
);
210 infile
= fopen(ifname
, "r");
211 if (infile
== NULL
) {
212 ERRS("could not open \"%s\" for reading", ifname
);
217 fread(buf
+ sizeof(*hdr
), st
.st_size
, 1, infile
);
219 ERRS("unable to read from file %s", ifname
);
223 seed
= HOST_TO_BE32(board
->seed
);
225 sha1_update(&ctx
, (uchar
*) &seed
, sizeof(seed
));
226 sha1_update(&ctx
, buf
+ sizeof(*hdr
), buflen
- sizeof(*hdr
));
227 sha1_finish(&ctx
, hdr
->sha1sum
);
229 outfile
= fopen(ofname
, "w");
230 if (outfile
== NULL
) {
231 ERRS("could not open \"%s\" for writing", ofname
);
236 fwrite(buf
, buflen
, 1, outfile
);
238 ERRS("unable to write to file %s", ofname
);
249 if (res
!= EXIT_SUCCESS
) {
This page took 0.058336 seconds and 5 git commands to generate.