2 * Copyright (C) 2011 Vasilis Tsiligiannis <b_tsiligiannis@silverton.gr>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
18 #include <endian.h> /* for __BYTE_ORDER */
23 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
24 # define HOST_TO_LE16(x) (x)
25 # define HOST_TO_LE32(x) (x)
26 # define HOST_TO_BE16(x) bswap_16(x)
27 # define HOST_TO_BE32(x) bswap_32(x)
29 # define HOST_TO_LE16(x) bswap_16(x)
30 # define HOST_TO_LE32(x) bswap_32(x)
31 # define HOST_TO_BE16(x) (x)
32 # define HOST_TO_BE32(x) (x)
37 unsigned char sign[4];
40 unsigned char model[4];
42 } __attribute__ ((packed));
56 static char *progname;
57 static int force_be = FALSE;
59 static void usage(int status)
61 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
63 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
67 " -s <sig> set image signature to <sig>\n"
68 " -m <model> set model to <model>\n"
69 " -i <file> read input from file <file>\n"
70 " -o <file> write output to file <file>\n"
71 " -f <flash> set flash address to <flash>\n"
72 " -S <start> set start address to <start>\n"
73 " -b big-endianness mode\n");
78 static int strtou32(char *arg, unsigned int *val)
83 *val = strtoul(arg, &endptr, 0);
84 if (errno || (endptr == arg) || (*endptr && (endptr != NULL))) {
91 static unsigned short fwcsum (struct buf *buf) {
93 unsigned short ret = 0;
95 for (i = 0; i < buf->size / 2; i++) {
96 if (force_be == FALSE)
97 ret -= ((unsigned short *) buf->start)[i];
99 ret -= HOST_TO_BE16(((unsigned short *) buf->start)[i]);
105 static int fwread(struct finfo *finfo, struct buf *buf)
109 f = fopen(finfo->name, "r");
111 fprintf(stderr, "could not open \"%s\" for reading\n", finfo->name);
115 buf->size = fread(buf->start, 1, finfo->size, f);
116 if (buf->size != finfo->size) {
117 fprintf(stderr, "unable to read from file \"%s\"\n", finfo->name);
126 static int fwwrite(struct finfo *finfo, struct buf *buf)
130 f = fopen(finfo->name, "w");
132 fprintf(stderr, "could not open \"%s\" for writing\n", finfo->name);
136 buf->size = fwrite(buf->start, 1, finfo->size, f);
137 if (buf->size != finfo->size) {
138 fprintf(stderr, "unable to write to file \"%s\"\n", finfo->name);
147 int main(int argc, char **argv)
150 struct header header;
151 struct buf ibuf, obuf;
152 struct finfo ifinfo, ofinfo;
156 ifinfo.name = ofinfo.name = NULL;
157 header.flash = header.size = header.start = 0;
158 progname = basename(argv[0]);
160 while((c = getopt(argc, argv, "i:o:m:s:f:S:h:b")) != -1) {
163 ifinfo.name = optarg;
166 ofinfo.name = optarg;
169 if (strlen(optarg) != 4) {
170 fprintf(stderr, "model must be 4 characters long\n");
173 memcpy(header.model, optarg, 4);
176 if (strlen(optarg) != 4) {
177 fprintf(stderr, "signature must be 4 characters long\n");
180 memcpy(header.sign, optarg, 4);
186 if (!strtou32(optarg, &header.flash)) {
187 fprintf(stderr, "invalid flash address specified\n");
192 if (!strtou32(optarg, &header.start)) {
193 fprintf(stderr, "invalid start address specified\n");
206 if (ifinfo.name == NULL) {
207 fprintf(stderr, "no input file specified\n");
211 if (ofinfo.name == NULL) {
212 fprintf(stderr, "no output file specified\n");
216 if (stat(ifinfo.name, &st)) {
217 fprintf(stderr, "stat failed on %s\n", ifinfo.name);
221 if (header.sign == NULL) {
222 fprintf(stderr, "no signature specified\n");
226 if (header.model == NULL) {
227 fprintf(stderr, "no model specified\n");
232 fprintf(stderr, "no flash address specified\n");
237 fprintf(stderr, "no start address specified\n");
241 ifinfo.size = st.st_size;
243 obuf.size = ifinfo.size + sizeof(struct header) + sizeof(unsigned short);
244 if (obuf.size % sizeof(unsigned short))
247 obuf.start = malloc(obuf.size);
249 fprintf(stderr, "no memory for buffer\n");
252 memset(obuf.start, 0, obuf.size);
254 ibuf.size = ifinfo.size;
255 ibuf.start = obuf.start + sizeof(struct header);
257 if (fwread(&ifinfo, &ibuf))
260 if (force_be == FALSE) {
261 header.flash = HOST_TO_LE32(header.flash);
262 header.size = HOST_TO_LE32(obuf.size - sizeof(struct header));
263 header.start = HOST_TO_LE32(header.start);
265 header.flash = HOST_TO_BE32(header.flash);
266 header.size = HOST_TO_BE32(obuf.size - sizeof(struct header));
267 header.start = HOST_TO_BE32(header.start);
270 memcpy (obuf.start, &header, sizeof(struct header));
272 if (force_be == FALSE)
273 csum = HOST_TO_LE16(fwcsum(&ibuf));
275 csum = HOST_TO_BE16(fwcsum(&ibuf));
277 memcpy(obuf.start + obuf.size - sizeof(unsigned short),
278 &csum, sizeof(unsigned short));
280 ofinfo.size = obuf.size;
282 if (fwwrite(&ofinfo, &obuf))