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 ": %s\n", \
47 progname, ## __VA_ARGS__, strerror(save)); \
50 void usage(int status
)
52 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
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 <hd_id> set image hardware id to <hd_id>\n"
64 " -h show this screen\n"
70 int main(int argc
, char *argv
[])
72 int res
= EXIT_FAILURE
;
80 FILE *outfile
, *infile
;
82 progname
= basename(argv
[0]);
87 c
= getopt(argc
, argv
, "B:i:o:v:r:H:h");
119 if (board_id
== NULL
) {
120 ERR("no board specified");
124 if (ifname
== NULL
) {
125 ERR("no input file specified");
129 if (ofname
== NULL
) {
130 ERR("no output file specified");
134 err
= stat(ifname
, &st
);
136 ERRS("stat failed on %s", ifname
);
140 buflen
= st
.st_size
+ DNI_HDR_LEN
+ 1;
141 buf
= malloc(buflen
);
143 ERR("no memory for buffer\n");
147 memset(buf
, 0, DNI_HDR_LEN
);
148 pos
= snprintf(buf
, DNI_HDR_LEN
, "device:%s\nversion:V%s\nregion:%s\n",
149 board_id
, version
, region
);
150 rem
= DNI_HDR_LEN
- pos
;
151 if (pos
>= 0 && rem
> 1 && hd_id
) {
152 snprintf(buf
+ pos
, rem
, "hd_id:%s\n", hd_id
);
155 infile
= fopen(ifname
, "r");
156 if (infile
== NULL
) {
157 ERRS("could not open \"%s\" for reading", ifname
);
162 fread(buf
+ DNI_HDR_LEN
, st
.st_size
, 1, infile
);
164 ERRS("unable to read from file %s", ifname
);
169 for (i
= 0; i
< (st
.st_size
+ DNI_HDR_LEN
); i
++)
173 buf
[st
.st_size
+ DNI_HDR_LEN
] = csum
;
175 outfile
= fopen(ofname
, "w");
176 if (outfile
== NULL
) {
177 ERRS("could not open \"%s\" for writing", ofname
);
182 fwrite(buf
, buflen
, 1, outfile
);
184 ERRS("unable to write to file %s", ofname
);
194 if (res
!= EXIT_SUCCESS
) {