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() */
21 #define DNI_HDR_LEN 128
27 static char *progname;
29 static char *version = "1.00.00";
30 static char *region ="NA";
32 static char *board_id;
36 #define ERR(fmt, ...) do { \
38 fprintf(stderr, "[%s] *** error: " fmt "\n", \
39 progname, ## __VA_ARGS__ ); \
42 #define ERRS(fmt, ...) do { \
45 fprintf(stderr, "[%s] *** error: " fmt "\n", \
46 progname, ## __VA_ARGS__, strerror(save)); \
49 void usage(int status)
51 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
52 struct board_info *board;
54 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
58 " -B <board> create image for the board specified with <board>\n"
59 " -i <file> read input from the file <file>\n"
60 " -o <file> write output to the file <file>\n"
61 " -v <version> set image version to <version>\n"
62 " -r <region> set image region to <region>\n"
63 " -h show this screen\n"
69 int main(int argc, char *argv[])
71 int res = EXIT_FAILURE;
79 FILE *outfile, *infile;
81 progname = basename(argv[0]);
86 c = getopt(argc, argv, "B:i:o:v:r:h");
115 if (board_id == NULL) {
116 ERR("no board specified");
120 if (ifname == NULL) {
121 ERR("no input file specified");
125 if (ofname == NULL) {
126 ERR("no output file specified");
130 err = stat(ifname, &st);
132 ERRS("stat failed on %s", ifname);
136 buflen = st.st_size + DNI_HDR_LEN + 1;
137 buf = malloc(buflen);
139 ERR("no memory for buffer\n");
143 memset(buf, 0, DNI_HDR_LEN);
144 snprintf(buf, DNI_HDR_LEN, "device:%s\nversion:V%s\nregion:%s\n",
145 board_id, version, region);
147 infile = fopen(ifname, "r");
148 if (infile == NULL) {
149 ERRS("could not open \"%s\" for reading", ifname);
154 fread(buf + DNI_HDR_LEN, st.st_size, 1, infile);
156 ERRS("unable to read from file %s", ifname);
161 for (i = 0; i < (st.st_size + DNI_HDR_LEN); i++)
165 buf[st.st_size + DNI_HDR_LEN] = csum;
167 outfile = fopen(ofname, "w");
168 if (outfile == NULL) {
169 ERRS("could not open \"%s\" for writing", ofname);
174 fwrite(buf, buflen, 1, outfile);
176 ERRS("unable to write to file %s", ofname);
187 if (res != EXIT_SUCCESS) {