2 * Copyright (C) 2011 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.
23 #define ERR(fmt, ...) do { \
25 fprintf(stderr, "[%s] *** error: " fmt "\n", \
26 progname, ## __VA_ARGS__ ); \
29 #define ERRS(fmt, ...) do { \
32 fprintf(stderr, "[%s] *** error: " fmt ", %s\n", \
33 progname, ## __VA_ARGS__, strerror(save)); \
36 #define WRG_MAGIC 0x20040220
46 } __attribute__ ((packed
));
48 static char *progname
;
51 static char *signature
;
52 static char *dev_name
;
53 static uint32_t offset
;
54 static int big_endian
;
56 void usage(int status
)
58 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
60 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
64 " -b create image in big endian format\n"
65 " -i <file> read input from the file <file>\n"
66 " -d <name> set device name to <name>\n"
67 " -o <file> write output to the file <file>\n"
68 " -O <offset> set offset to <offset>\n"
69 " -s <sig> set image signature to <sig>\n"
70 " -h show this screen\n"
76 static void put_u32(void *data
, uint32_t val
)
78 unsigned char *p
= data
;
81 p
[0] = (val
>> 24) & 0xff;
82 p
[1] = (val
>> 16) & 0xff;
83 p
[2] = (val
>> 8) & 0xff;
86 p
[3] = (val
>> 24) & 0xff;
87 p
[2] = (val
>> 16) & 0xff;
88 p
[1] = (val
>> 8) & 0xff;
93 static void get_digest(struct wrg_header
*header
, char *data
, int size
)
99 MD5_Update(&ctx
, (char *)&header
->offset
, sizeof(header
->offset
));
100 MD5_Update(&ctx
, (char *)&header
->devname
, sizeof(header
->devname
));
101 MD5_Update(&ctx
, data
, size
);
103 MD5_Final(header
->digest
, &ctx
);
106 int main(int argc
, char *argv
[])
108 struct wrg_header
*header
;
113 int res
= EXIT_FAILURE
;
115 FILE *outfile
, *infile
;
117 progname
= basename(argv
[0]);
122 c
= getopt(argc
, argv
, "bd:i:o:s:O:h");
143 offset
= strtoul(optarg
, NULL
, 0);
155 if (signature
== NULL
) {
156 ERR("no signature specified");
160 if (ifname
== NULL
) {
161 ERR("no input file specified");
165 if (ofname
== NULL
) {
166 ERR("no output file specified");
170 if (dev_name
== NULL
) {
171 ERR("no device name specified");
175 err
= stat(ifname
, &st
);
177 ERRS("stat failed on %s", ifname
);
181 buflen
= st
.st_size
+ sizeof(struct wrg_header
);
182 buf
= malloc(buflen
);
184 ERR("no memory for buffer\n");
188 infile
= fopen(ifname
, "r");
189 if (infile
== NULL
) {
190 ERRS("could not open \"%s\" for reading", ifname
);
195 fread(buf
+ sizeof(struct wrg_header
), st
.st_size
, 1, infile
);
197 ERRS("unable to read from file %s", ifname
);
201 header
= (struct wrg_header
*) buf
;
202 memset(header
, '\0', sizeof(struct wrg_header
));
204 strncpy(header
->signature
, signature
, sizeof(header
->signature
));
205 strncpy(header
->devname
, dev_name
, sizeof(header
->signature
));
206 put_u32(&header
->magic1
, WRG_MAGIC
);
207 put_u32(&header
->magic2
, WRG_MAGIC
);
208 put_u32(&header
->size
, st
.st_size
);
209 put_u32(&header
->offset
, offset
);
211 get_digest(header
, buf
+ sizeof(struct wrg_header
), st
.st_size
);
213 outfile
= fopen(ofname
, "w");
214 if (outfile
== NULL
) {
215 ERRS("could not open \"%s\" for writing", ofname
);
220 fwrite(buf
, buflen
, 1, outfile
);
222 ERRS("unable to write to file %s", ofname
);
232 if (res
!= EXIT_SUCCESS
)
This page took 0.051254 seconds and 5 git commands to generate.