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));
43 static char *progname;
45 static char *version = "1.00.00";
50 #define ERR(fmt, ...) do { \
52 fprintf(stderr, "[%s] *** error: " fmt "\n", \
53 progname, ## __VA_ARGS__ ); \
56 #define ERRS(fmt, ...) do { \
59 fprintf(stderr, "[%s] *** error: " fmt "\n", \
60 progname, ## __VA_ARGS__, strerror(save)); \
63 void usage(int status)
65 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
66 struct board_info *board;
68 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
72 " -i <file> read input from the file <file>\n"
73 " -o <file> write output to the file <file>\n"
74 " -v <version> set image version to <version>\n"
75 " -h show this screen\n"
81 int main(int argc, char *argv[])
83 int res = EXIT_FAILURE;
88 struct planex_hdr *hdr;
90 uint32_t t = HOST_TO_BE32(2);
92 FILE *outfile, *infile;
94 progname = basename(argv[0]);
99 c = getopt(argc, argv, "i:o:v:h");
122 if (ifname == NULL) {
123 ERR("no input file specified");
127 if (ofname == NULL) {
128 ERR("no output file specified");
132 err = stat(ifname, &st);
134 ERRS("stat failed on %s", ifname);
138 buflen = (st.st_size + 3) & ~3;
139 buflen += sizeof(*hdr);
141 buf = malloc(buflen);
143 ERR("no memory for buffer\n");
147 memset(buf, 0xff, buflen);
148 hdr = (struct planex_hdr *)buf;
150 hdr->datalen = HOST_TO_BE32(buflen - sizeof(*hdr));
154 snprintf(hdr->version, sizeof(hdr->version), "%s", version);
156 infile = fopen(ifname, "r");
157 if (infile == NULL) {
158 ERRS("could not open \"%s\" for reading", ifname);
163 fread(buf + sizeof(*hdr), st.st_size, 1, infile);
165 ERRS("unable to read from file %s", ifname);
170 sha1_update(&ctx, (uchar *) &t, sizeof(t));
171 sha1_update(&ctx, buf + sizeof(*hdr), buflen - sizeof(*hdr));
172 sha1_finish(&ctx, hdr->sha1sum);
174 outfile = fopen(ofname, "w");
175 if (outfile == NULL) {
176 ERRS("could not open \"%s\" for writing", ofname);
181 fwrite(buf, buflen, 1, outfile);
183 ERRS("unable to write to file %s", ofname);
194 if (res != EXIT_SUCCESS) {