1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
4 * Copyright (C) 2016 FUKAUMI Naoki <naobsd@gmail.com>
13 #include <unistd.h> /* for unlink() */
15 #include <getopt.h> /* for getopt() */
20 #define DNI_HDR_LEN 128
26 static char *progname
;
28 static char *version
= "0.00_0.00";
29 static char *region
= "JP";
30 static char *rootfs_size
;
31 static char *kernel_size
;
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 " -R <rootfs_size> set RootfsSize to <rootfs_size>\n"
64 " -K <kernel_size> set KernelSize to <kernel_size>\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:R:K:h");
109 rootfs_size
= optarg
;
112 kernel_size
= optarg
;
123 if (board_id
== NULL
) {
124 ERR("no board specified");
128 if (rootfs_size
== NULL
) {
129 ERR("no rootfs_size specified");
133 if (kernel_size
== NULL
) {
134 ERR("no kernel_size specified");
138 if (ifname
== NULL
) {
139 ERR("no input file specified");
143 if (ofname
== NULL
) {
144 ERR("no output file specified");
148 err
= stat(ifname
, &st
);
150 ERRS("stat failed on %s", ifname
);
154 buflen
= st
.st_size
+ DNI_HDR_LEN
+ 1;
155 buf
= malloc(buflen
);
157 ERR("no memory for buffer\n");
161 memset(buf
, 0, DNI_HDR_LEN
);
162 snprintf(buf
, DNI_HDR_LEN
, "device:%s\nversion:%s\nregion:%s\n"
163 "RootfsSize:%s\nKernelSize:%s\nInfoHeadSize:128\n",
164 board_id
, version
, region
, rootfs_size
, kernel_size
);
165 buf
[DNI_HDR_LEN
- 2] = 0x12;
166 buf
[DNI_HDR_LEN
- 1] = 0x32;
168 infile
= fopen(ifname
, "r");
169 if (infile
== NULL
) {
170 ERRS("could not open \"%s\" for reading", ifname
);
175 fread(buf
+ DNI_HDR_LEN
, st
.st_size
, 1, infile
);
177 ERRS("unable to read from file %s", ifname
);
182 for (i
= 0; i
< (st
.st_size
+ DNI_HDR_LEN
); i
++)
186 buf
[st
.st_size
+ DNI_HDR_LEN
] = csum
;
188 outfile
= fopen(ofname
, "w");
189 if (outfile
== NULL
) {
190 ERRS("could not open \"%s\" for writing", ofname
);
195 fwrite(buf
, buflen
, 1, outfile
);
197 ERRS("unable to write to file %s", ofname
);
207 if (res
!= EXIT_SUCCESS
) {