1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
11 #include <unistd.h> /* for unlink() */
13 #include <getopt.h> /* for getopt() */
20 #if (__BYTE_ORDER == __BIG_ENDIAN)
21 # define HOST_TO_BE32(x) (x)
22 # define BE32_TO_HOST(x) (x)
24 # define HOST_TO_BE32(x) bswap_32(x)
25 # define BE32_TO_HOST(x) bswap_32(x)
34 } __attribute__ ((packed
));
47 static char *progname
;
49 static char *version
= "1.00.00";
51 static char *board_id
;
52 static struct board_info
*board
;
54 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 ": %s\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
;
106 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
110 " -B <board> create image for the board specified with <board>\n"
111 " -i <file> read input from the file <file>\n"
112 " -o <file> write output to the file <file>\n"
113 " -v <version> set image version to <version>\n"
114 " -h show this screen\n"
120 int main(int argc
, char *argv
[])
122 int res
= EXIT_FAILURE
;
127 struct planex_hdr
*hdr
;
131 FILE *outfile
, *infile
;
133 progname
= basename(argv
[0]);
138 c
= getopt(argc
, argv
, "B:i:o:v:h");
164 if (board_id
== NULL
) {
165 ERR("no board specified");
169 board
= find_board(board_id
);
171 ERR("unknown board '%s'", board_id
);
175 if (ifname
== NULL
) {
176 ERR("no input file specified");
180 if (ofname
== NULL
) {
181 ERR("no output file specified");
185 err
= stat(ifname
, &st
);
187 ERRS("stat failed on %s", ifname
);
191 if (st
.st_size
> board
->datalen
) {
192 ERR("file '%s' is too big - max size: 0x%08X (exceeds %lu bytes)\n",
193 ifname
, board
->datalen
, st
.st_size
- board
->datalen
);
197 buflen
= board
->datalen
+ 0x10000;
198 buf
= malloc(buflen
);
200 ERR("no memory for buffer\n");
204 memset(buf
, 0xff, buflen
);
205 hdr
= (struct planex_hdr
*)buf
;
207 hdr
->datalen
= HOST_TO_BE32(board
->datalen
);
208 hdr
->unk1
[0] = board
->unk
[0];
209 hdr
->unk1
[1] = board
->unk
[1];
211 snprintf(hdr
->version
, sizeof(hdr
->version
), "%s", version
);
213 infile
= fopen(ifname
, "r");
214 if (infile
== NULL
) {
215 ERRS("could not open \"%s\" for reading", ifname
);
220 fread(buf
+ sizeof(*hdr
), st
.st_size
, 1, infile
);
222 ERRS("unable to read from file %s", ifname
);
226 seed
= HOST_TO_BE32(board
->seed
);
228 sha1_update(&ctx
, (uchar
*) &seed
, sizeof(seed
));
229 sha1_update(&ctx
, buf
+ sizeof(*hdr
), board
->datalen
);
230 sha1_finish(&ctx
, hdr
->sha1sum
);
232 outfile
= fopen(ofname
, "w");
233 if (outfile
== NULL
) {
234 ERRS("could not open \"%s\" for writing", ofname
);
239 fwrite(buf
, buflen
, 1, outfile
);
241 ERRS("unable to write to file %s", ofname
);
251 if (res
!= EXIT_SUCCESS
) {