1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
10 #include <unistd.h> /* for unlink() */
12 #include <getopt.h> /* for getopt() */
19 #if (__BYTE_ORDER == __BIG_ENDIAN)
20 # define HOST_TO_BE32(x) (x)
21 # define BE32_TO_HOST(x) (x)
23 # define HOST_TO_BE32(x) bswap_32(x)
24 # define BE32_TO_HOST(x) bswap_32(x)
33 } __attribute__ ((packed
));
46 static char *progname
;
48 static char *version
= "1.00.00";
50 static char *board_id
;
51 static struct board_info
*board
;
53 static struct board_info boards
[] = {
65 /* terminating entry */
72 #define ERR(fmt, ...) do { \
74 fprintf(stderr, "[%s] *** error: " fmt "\n", \
75 progname, ## __VA_ARGS__ ); \
78 #define ERRS(fmt, ...) do { \
81 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
82 progname, ## __VA_ARGS__, strerror(save)); \
85 static struct board_info
*find_board(char *id
)
87 struct board_info
*ret
;
88 struct board_info
*board
;
91 for (board
= boards
; board
->id
!= NULL
; board
++){
92 if (strcasecmp(id
, board
->id
) == 0) {
101 void usage(int status
)
103 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
105 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
109 " -B <board> create image for the board specified with <board>\n"
110 " -i <file> read input from the file <file>\n"
111 " -o <file> write output to the file <file>\n"
112 " -v <version> set image version to <version>\n"
113 " -h show this screen\n"
119 int main(int argc
, char *argv
[])
121 int res
= EXIT_FAILURE
;
126 struct planex_hdr
*hdr
;
130 FILE *outfile
, *infile
;
132 progname
= basename(argv
[0]);
137 c
= getopt(argc
, argv
, "B:i:o:v:h");
163 if (board_id
== NULL
) {
164 ERR("no board specified");
168 board
= find_board(board_id
);
170 ERR("unknown board '%s'", board_id
);
174 if (ifname
== NULL
) {
175 ERR("no input file specified");
179 if (ofname
== NULL
) {
180 ERR("no output file specified");
184 err
= stat(ifname
, &st
);
186 ERRS("stat failed on %s", ifname
);
190 if (st
.st_size
> board
->datalen
) {
191 ERR("file '%s' is too big - max size: 0x%08X (exceeds %lu bytes)\n",
192 ifname
, board
->datalen
, st
.st_size
- board
->datalen
);
196 buflen
= board
->datalen
+ 0x10000;
197 buf
= malloc(buflen
);
199 ERR("no memory for buffer\n");
203 memset(buf
, 0xff, buflen
);
204 hdr
= (struct planex_hdr
*)buf
;
206 hdr
->datalen
= HOST_TO_BE32(board
->datalen
);
207 hdr
->unk1
[0] = board
->unk
[0];
208 hdr
->unk1
[1] = board
->unk
[1];
210 snprintf(hdr
->version
, sizeof(hdr
->version
), "%s", version
);
212 infile
= fopen(ifname
, "r");
213 if (infile
== NULL
) {
214 ERRS("could not open \"%s\" for reading", ifname
);
219 fread(buf
+ sizeof(*hdr
), st
.st_size
, 1, infile
);
221 ERRS("unable to read from file %s", ifname
);
225 seed
= HOST_TO_BE32(board
->seed
);
227 sha1_update(&ctx
, (uchar
*) &seed
, sizeof(seed
));
228 sha1_update(&ctx
, buf
+ sizeof(*hdr
), board
->datalen
);
229 sha1_finish(&ctx
, hdr
->sha1sum
);
231 outfile
= fopen(ofname
, "w");
232 if (outfile
== NULL
) {
233 ERRS("could not open \"%s\" for writing", ofname
);
238 fwrite(buf
, buflen
, 1, outfile
);
240 ERRS("unable to write to file %s", ofname
);
250 if (res
!= EXIT_SUCCESS
) {