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 = "";
33 static char *board_id;
37 #define ERR(fmt, ...) do { \
39 fprintf(stderr, "[%s] *** error: " fmt "\n", \
40 progname, ## __VA_ARGS__ ); \
43 #define ERRS(fmt, ...) do { \
46 fprintf(stderr, "[%s] *** error: " fmt "\n", \
47 progname, ## __VA_ARGS__, strerror(save)); \
50 void usage(int status)
52 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
53 struct board_info *board;
55 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
59 " -B <board> create image for the board specified with <board>\n"
60 " -i <file> read input from the file <file>\n"
61 " -o <file> write output to the file <file>\n"
62 " -v <version> set image version to <version>\n"
63 " -r <region> set image region to <region>\n"
64 " -H <hd_id> set image hardware id to <hd_id>\n"
65 " -h show this screen\n"
71 int main(int argc, char *argv[])
73 int res = EXIT_FAILURE;
81 FILE *outfile, *infile;
83 progname = basename(argv[0]);
88 c = getopt(argc, argv, "B:i:o:v:r:H:h");
120 if (board_id == NULL) {
121 ERR("no board specified");
125 if (ifname == NULL) {
126 ERR("no input file specified");
130 if (ofname == NULL) {
131 ERR("no output file specified");
135 err = stat(ifname, &st);
137 ERRS("stat failed on %s", ifname);
141 buflen = st.st_size + DNI_HDR_LEN + 1;
142 buf = malloc(buflen);
144 ERR("no memory for buffer\n");
148 memset(buf, 0, DNI_HDR_LEN);
149 pos = snprintf(buf, DNI_HDR_LEN, "device:%s\nversion:V%s\nregion:%s\n",
150 board_id, version, region);
151 rem = DNI_HDR_LEN - pos;
152 if (pos >= 0 && rem > 1 && hd_id) {
153 snprintf(buf + pos, rem, "hd_id:%s\n", hd_id);
156 infile = fopen(ifname, "r");
157 if (infile == NULL) {
158 ERRS("could not open \"%s\" for reading", ifname);
163 fread(buf + DNI_HDR_LEN, st.st_size, 1, infile);
165 ERRS("unable to read from file %s", ifname);
170 for (i = 0; i < (st.st_size + DNI_HDR_LEN); i++)
174 buf[st.st_size + DNI_HDR_LEN] = csum;
176 outfile = fopen(ofname, "w");
177 if (outfile == NULL) {
178 ERRS("could not open \"%s\" for writing", ofname);
183 fwrite(buf, buflen, 1, outfile);
185 ERRS("unable to write to file %s", ofname);
196 if (res != EXIT_SUCCESS) {