2 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
3 * Copyright (C) 2016 FUKAUMI Naoki <naobsd@gmail.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation.
17 #include <unistd.h> /* for unlink() */
19 #include <getopt.h> /* for getopt() */
24 #define DNI_HDR_LEN 128
30 static char *progname
;
32 static char *version
= "0.00_0.00";
33 static char *region
= "JP";
34 static char *rootfs_size
;
35 static char *kernel_size
;
37 static char *board_id
;
41 #define ERR(fmt, ...) do { \
43 fprintf(stderr, "[%s] *** error: " fmt "\n", \
44 progname, ## __VA_ARGS__ ); \
47 #define ERRS(fmt, ...) do { \
50 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
51 progname, ## __VA_ARGS__, strerror(save)); \
54 void usage(int status
)
56 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
58 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
62 " -B <board> create image for the board specified with <board>\n"
63 " -i <file> read input from the file <file>\n"
64 " -o <file> write output to the file <file>\n"
65 " -v <version> set image version to <version>\n"
66 " -r <region> set image region to <region>\n"
67 " -R <rootfs_size> set RootfsSize to <rootfs_size>\n"
68 " -K <kernel_size> set KernelSize to <kernel_size>\n"
69 " -h show this screen\n"
75 int main(int argc
, char *argv
[])
77 int res
= EXIT_FAILURE
;
85 FILE *outfile
, *infile
;
87 progname
= basename(argv
[0]);
92 c
= getopt(argc
, argv
, "B:i:o:v:r:R:K:h");
113 rootfs_size
= optarg
;
116 kernel_size
= optarg
;
127 if (board_id
== NULL
) {
128 ERR("no board specified");
132 if (rootfs_size
== NULL
) {
133 ERR("no rootfs_size specified");
137 if (kernel_size
== NULL
) {
138 ERR("no kernel_size specified");
142 if (ifname
== NULL
) {
143 ERR("no input file specified");
147 if (ofname
== NULL
) {
148 ERR("no output file specified");
152 err
= stat(ifname
, &st
);
154 ERRS("stat failed on %s", ifname
);
158 buflen
= st
.st_size
+ DNI_HDR_LEN
+ 1;
159 buf
= malloc(buflen
);
161 ERR("no memory for buffer\n");
165 memset(buf
, 0, DNI_HDR_LEN
);
166 snprintf(buf
, DNI_HDR_LEN
, "device:%s\nversion:%s\nregion:%s\n"
167 "RootfsSize:%s\nKernelSize:%s\nInfoHeadSize:128\n",
168 board_id
, version
, region
, rootfs_size
, kernel_size
);
169 buf
[DNI_HDR_LEN
- 2] = 0x12;
170 buf
[DNI_HDR_LEN
- 1] = 0x32;
172 infile
= fopen(ifname
, "r");
173 if (infile
== NULL
) {
174 ERRS("could not open \"%s\" for reading", ifname
);
179 fread(buf
+ DNI_HDR_LEN
, st
.st_size
, 1, infile
);
181 ERRS("unable to read from file %s", ifname
);
186 for (i
= 0; i
< (st
.st_size
+ DNI_HDR_LEN
); i
++)
190 buf
[st
.st_size
+ DNI_HDR_LEN
] = csum
;
192 outfile
= fopen(ofname
, "w");
193 if (outfile
== NULL
) {
194 ERRS("could not open \"%s\" for writing", ofname
);
199 fwrite(buf
, buflen
, 1, outfile
);
201 ERRS("unable to write to file %s", ofname
);
211 if (res
!= EXIT_SUCCESS
) {