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 "\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
;
108 struct board_info
*board
;
110 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
114 " -B <board> create image for the board specified with <board>\n"
115 " -i <file> read input from the file <file>\n"
116 " -o <file> write output to the file <file>\n"
117 " -v <version> set image version to <version>\n"
118 " -h show this screen\n"
124 int main(int argc
, char *argv
[])
126 int res
= EXIT_FAILURE
;
131 struct planex_hdr
*hdr
;
135 FILE *outfile
, *infile
;
137 progname
= basename(argv
[0]);
142 c
= getopt(argc
, argv
, "B:i:o:v:h");
168 if (board_id
== NULL
) {
169 ERR("no board specified");
173 board
= find_board(board_id
);
175 ERR("unknown board '%s'", board_id
);
179 if (ifname
== NULL
) {
180 ERR("no input file specified");
184 if (ofname
== NULL
) {
185 ERR("no output file specified");
189 err
= stat(ifname
, &st
);
191 ERRS("stat failed on %s", ifname
);
195 if (st
.st_size
> board
->datalen
) {
196 ERR("file '%s' is too big - max size: 0x%08X (exceeds %lu bytes)\n",
197 ifname
, board
->datalen
, st
.st_size
- board
->datalen
);
201 buflen
= board
->datalen
+ 0x10000;
202 buf
= malloc(buflen
);
204 ERR("no memory for buffer\n");
208 memset(buf
, 0xff, buflen
);
209 hdr
= (struct planex_hdr
*)buf
;
211 hdr
->datalen
= HOST_TO_BE32(board
->datalen
);
212 hdr
->unk1
[0] = board
->unk
[0];
213 hdr
->unk1
[1] = board
->unk
[1];
215 snprintf(hdr
->version
, sizeof(hdr
->version
), "%s", version
);
217 infile
= fopen(ifname
, "r");
218 if (infile
== NULL
) {
219 ERRS("could not open \"%s\" for reading", ifname
);
224 fread(buf
+ sizeof(*hdr
), st
.st_size
, 1, infile
);
226 ERRS("unable to read from file %s", ifname
);
230 seed
= HOST_TO_BE32(board
->seed
);
232 sha1_update(&ctx
, (uchar
*) &seed
, sizeof(seed
));
233 sha1_update(&ctx
, buf
+ sizeof(*hdr
), board
->datalen
);
234 sha1_finish(&ctx
, hdr
->sha1sum
);
236 outfile
= fopen(ofname
, "w");
237 if (outfile
== NULL
) {
238 ERRS("could not open \"%s\" for writing", ofname
);
243 fwrite(buf
, buflen
, 1, outfile
);
245 ERRS("unable to write to file %s", ofname
);
256 if (res
!= EXIT_SUCCESS
) {
This page took 0.069112 seconds and 5 git commands to generate.