1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
10 #include <unistd.h> /* for unlink() */
12 #include <getopt.h> /* for getopt() */
17 #define DNI_HDR_LEN 128
23 static char *progname
;
25 static char *version
= "1.00.00";
26 static char *region
= "";
29 static char *board_id
;
33 #define ERR(fmt, ...) do { \
35 fprintf(stderr, "[%s] *** error: " fmt "\n", \
36 progname, ## __VA_ARGS__ ); \
39 #define ERRS(fmt, ...) do { \
42 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
43 progname, ## __VA_ARGS__, strerror(save)); \
46 void usage(int status
)
48 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
50 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
54 " -B <board> create image for the board specified with <board>\n"
55 " -i <file> read input from the file <file>\n"
56 " -o <file> write output to the file <file>\n"
57 " -v <version> set image version to <version>\n"
58 " -r <region> set image region to <region>\n"
59 " -H <hd_id> set image hardware id to <hd_id>\n"
60 " -h show this screen\n"
66 int main(int argc
, char *argv
[])
68 int res
= EXIT_FAILURE
;
76 FILE *outfile
, *infile
;
78 progname
= basename(argv
[0]);
83 c
= getopt(argc
, argv
, "B:i:o:v:r:H: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 pos
= snprintf(buf
, DNI_HDR_LEN
, "device:%s\nversion:V%s\nregion:%s\n",
145 board_id
, version
, region
);
146 rem
= DNI_HDR_LEN
- pos
;
147 if (pos
>= 0 && rem
> 1 && hd_id
) {
148 snprintf(buf
+ pos
, rem
, "hd_id:%s\n", hd_id
);
151 infile
= fopen(ifname
, "r");
152 if (infile
== NULL
) {
153 ERRS("could not open \"%s\" for reading", ifname
);
158 fread(buf
+ DNI_HDR_LEN
, st
.st_size
, 1, infile
);
160 ERRS("unable to read from file %s", ifname
);
165 for (i
= 0; i
< (st
.st_size
+ DNI_HDR_LEN
); i
++)
169 buf
[st
.st_size
+ DNI_HDR_LEN
] = csum
;
171 outfile
= fopen(ofname
, "w");
172 if (outfile
== NULL
) {
173 ERRS("could not open \"%s\" for writing", ofname
);
178 fwrite(buf
, buflen
, 1, outfile
);
180 ERRS("unable to write to file %s", ofname
);
190 if (res
!= EXIT_SUCCESS
) {