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 */
20 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
21 # define HOST_TO_LE16(x) (x)
22 # define HOST_TO_LE32(x) (x)
24 # define HOST_TO_LE16(x) bswap_16(x)
25 # define HOST_TO_LE32(x) bswap_32(x)
30 unsigned char sign
[4];
33 unsigned char model
[4];
35 } __attribute__ ((packed
));
49 static char *progname
;
51 static void usage(int status
)
53 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
55 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
59 " -s <sig> set image signature to <sig>\n"
60 " -m <model> set model to <model>\n"
61 " -i <file> read input from file <file>\n"
62 " -o <file> write output to file <file>\n"
63 " -f <flash> set flash address to <flash>\n"
64 " -S <start> set start address to <start>\n");
69 static int strtou32(char *arg
, unsigned int *val
)
74 *val
= strtoul(arg
, &endptr
, 0);
75 if (errno
|| (endptr
== arg
) || (*endptr
&& (endptr
!= NULL
))) {
82 static unsigned short fwcsum (struct buf
*buf
) {
84 unsigned short ret
= 0;
86 for (i
= 0; i
< buf
->size
/ 2; i
++)
87 ret
-= ((unsigned short *) buf
->start
)[i
];
92 static int fwread(struct finfo
*finfo
, struct buf
*buf
)
96 f
= fopen(finfo
->name
, "r");
98 fprintf(stderr
, "could not open \"%s\" for reading\n", finfo
->name
);
102 buf
->size
= fread(buf
->start
, 1, finfo
->size
, f
);
103 if (buf
->size
!= finfo
->size
) {
104 fprintf(stderr
, "unable to read from file \"%s\"\n", finfo
->name
);
113 static int fwwrite(struct finfo
*finfo
, struct buf
*buf
)
117 f
= fopen(finfo
->name
, "w");
119 fprintf(stderr
, "could not open \"%s\" for writing\n", finfo
->name
);
123 buf
->size
= fwrite(buf
->start
, 1, finfo
->size
, f
);
124 if (buf
->size
!= finfo
->size
) {
125 fprintf(stderr
, "unable to write to file \"%s\"\n", finfo
->name
);
134 int main(int argc
, char **argv
)
137 struct header header
;
138 struct buf ibuf
, obuf
;
139 struct finfo ifinfo
, ofinfo
;
143 ifinfo
.name
= ofinfo
.name
= NULL
;
144 header
.flash
= header
.size
= header
.start
= 0;
145 progname
= basename(argv
[0]);
147 while((c
= getopt(argc
, argv
, "i:o:m:s:f:S:h")) != -1) {
150 ifinfo
.name
= optarg
;
153 ofinfo
.name
= optarg
;
156 if (strlen(optarg
) != 4) {
157 fprintf(stderr
, "model must be 4 characters long\n");
160 memcpy(header
.model
, optarg
, 4);
163 if (strlen(optarg
) != 4) {
164 fprintf(stderr
, "signature must be 4 characters long\n");
167 memcpy(header
.sign
, optarg
, 4);
173 if (!strtou32(optarg
, &header
.flash
)) {
174 fprintf(stderr
, "invalid flash address specified\n");
179 if (!strtou32(optarg
, &header
.start
)) {
180 fprintf(stderr
, "invalid start address specified\n");
190 if (ifinfo
.name
== NULL
) {
191 fprintf(stderr
, "no input file specified\n");
195 if (ofinfo
.name
== NULL
) {
196 fprintf(stderr
, "no output file specified\n");
200 if (stat(ifinfo
.name
, &st
)) {
201 fprintf(stderr
, "stat failed on %s\n", ifinfo
.name
);
205 if (header
.sign
== NULL
) {
206 fprintf(stderr
, "no signature specified\n");
210 if (header
.model
== NULL
) {
211 fprintf(stderr
, "no model specified\n");
216 fprintf(stderr
, "no flash address specified\n");
221 fprintf(stderr
, "no start address specified\n");
225 ifinfo
.size
= st
.st_size
;
227 obuf
.size
= ifinfo
.size
+ sizeof(struct header
) + sizeof(unsigned short);
228 if (obuf
.size
% sizeof(unsigned short))
231 obuf
.start
= malloc(obuf
.size
);
233 fprintf(stderr
, "no memory for buffer\n");
236 memset(obuf
.start
, 0, obuf
.size
);
238 ibuf
.size
= ifinfo
.size
;
239 ibuf
.start
= obuf
.start
+ sizeof(struct header
);
241 if (fwread(&ifinfo
, &ibuf
))
244 header
.flash
= HOST_TO_LE32(header
.flash
);
245 header
.size
= HOST_TO_LE32(obuf
.size
- sizeof(struct header
));
246 header
.start
= HOST_TO_LE32(header
.start
);
247 memcpy (obuf
.start
, &header
, sizeof(struct header
));
249 csum
= HOST_TO_LE16(fwcsum(&ibuf
));
250 memcpy(obuf
.start
+ obuf
.size
- sizeof(unsigned short),
251 &csum
, sizeof(unsigned short));
253 ofinfo
.size
= obuf
.size
;
255 if (fwwrite(&ofinfo
, &obuf
))
This page took 0.056855 seconds and 5 git commands to generate.