1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 Vasilis Tsiligiannis <b_tsiligiannis@silverton.gr>
13 #include <endian.h> /* for __BYTE_ORDER */
18 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
19 # define HOST_TO_LE16(x) (x)
20 # define HOST_TO_LE32(x) (x)
21 # define HOST_TO_BE16(x) bswap_16(x)
22 # define HOST_TO_BE32(x) bswap_32(x)
24 # define HOST_TO_LE16(x) bswap_16(x)
25 # define HOST_TO_LE32(x) bswap_32(x)
26 # define HOST_TO_BE16(x) (x)
27 # define HOST_TO_BE32(x) (x)
32 unsigned char sign
[4];
35 unsigned char model
[4];
37 } __attribute__ ((packed
));
51 static char *progname
;
52 static int force_be
= FALSE
;
54 static void usage(int status
)
56 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
58 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
62 " -s <sig> set image signature to <sig>\n"
63 " -m <model> set model to <model>\n"
64 " -i <file> read input from file <file>\n"
65 " -o <file> write output to file <file>\n"
66 " -f <flash> set flash address to <flash>\n"
67 " -S <start> set start address to <start>\n"
68 " -b big-endianness mode\n");
73 static int strtou32(char *arg
, unsigned int *val
)
78 *val
= strtoul(arg
, &endptr
, 0);
79 if (errno
|| (endptr
== arg
) || (*endptr
&& (endptr
!= NULL
))) {
86 static unsigned short fwcsum (struct buf
*buf
) {
88 unsigned short ret
= 0;
90 for (i
= 0; i
< buf
->size
/ 2; i
++) {
91 if (force_be
== FALSE
)
92 ret
-= ((unsigned short *) buf
->start
)[i
];
94 ret
-= HOST_TO_BE16(((unsigned short *) buf
->start
)[i
]);
100 static int fwread(struct finfo
*finfo
, struct buf
*buf
)
104 f
= fopen(finfo
->name
, "r");
106 fprintf(stderr
, "could not open \"%s\" for reading\n", finfo
->name
);
110 buf
->size
= fread(buf
->start
, 1, finfo
->size
, f
);
111 if (buf
->size
!= finfo
->size
) {
112 fprintf(stderr
, "unable to read from file \"%s\"\n", finfo
->name
);
121 static int fwwrite(struct finfo
*finfo
, struct buf
*buf
)
125 f
= fopen(finfo
->name
, "w");
127 fprintf(stderr
, "could not open \"%s\" for writing\n", finfo
->name
);
131 buf
->size
= fwrite(buf
->start
, 1, finfo
->size
, f
);
132 if (buf
->size
!= finfo
->size
) {
133 fprintf(stderr
, "unable to write to file \"%s\"\n", finfo
->name
);
142 int main(int argc
, char **argv
)
145 struct header header
;
146 struct buf ibuf
, obuf
;
147 struct finfo ifinfo
, ofinfo
;
151 ifinfo
.name
= ofinfo
.name
= NULL
;
152 header
.flash
= header
.size
= header
.start
= 0;
153 progname
= basename(argv
[0]);
155 while((c
= getopt(argc
, argv
, "i:o:m:s:f:S:h:b")) != -1) {
158 ifinfo
.name
= optarg
;
161 ofinfo
.name
= optarg
;
164 if (strlen(optarg
) != 4) {
165 fprintf(stderr
, "model must be 4 characters long\n");
168 memcpy(header
.model
, optarg
, 4);
171 if (strlen(optarg
) != 4) {
172 fprintf(stderr
, "signature must be 4 characters long\n");
175 memcpy(header
.sign
, optarg
, 4);
181 if (!strtou32(optarg
, &header
.flash
)) {
182 fprintf(stderr
, "invalid flash address specified\n");
187 if (!strtou32(optarg
, &header
.start
)) {
188 fprintf(stderr
, "invalid start address specified\n");
201 if (ifinfo
.name
== NULL
) {
202 fprintf(stderr
, "no input file specified\n");
206 if (ofinfo
.name
== NULL
) {
207 fprintf(stderr
, "no output file specified\n");
211 if (stat(ifinfo
.name
, &st
)) {
212 fprintf(stderr
, "stat failed on %s\n", ifinfo
.name
);
216 if (header
.sign
== NULL
) {
217 fprintf(stderr
, "no signature specified\n");
221 if (header
.model
== NULL
) {
222 fprintf(stderr
, "no model specified\n");
227 fprintf(stderr
, "no flash address specified\n");
232 fprintf(stderr
, "no start address specified\n");
236 ifinfo
.size
= st
.st_size
;
238 obuf
.size
= ifinfo
.size
+ sizeof(struct header
) + sizeof(unsigned short);
239 if (obuf
.size
% sizeof(unsigned short))
242 obuf
.start
= malloc(obuf
.size
);
244 fprintf(stderr
, "no memory for buffer\n");
247 memset(obuf
.start
, 0, obuf
.size
);
249 ibuf
.size
= ifinfo
.size
;
250 ibuf
.start
= obuf
.start
+ sizeof(struct header
);
252 if (fwread(&ifinfo
, &ibuf
))
255 if (force_be
== FALSE
) {
256 header
.flash
= HOST_TO_LE32(header
.flash
);
257 header
.size
= HOST_TO_LE32(obuf
.size
- sizeof(struct header
));
258 header
.start
= HOST_TO_LE32(header
.start
);
260 header
.flash
= HOST_TO_BE32(header
.flash
);
261 header
.size
= HOST_TO_BE32(obuf
.size
- sizeof(struct header
));
262 header
.start
= HOST_TO_BE32(header
.start
);
265 memcpy (obuf
.start
, &header
, sizeof(struct header
));
267 if (force_be
== FALSE
)
268 csum
= HOST_TO_LE16(fwcsum(&ibuf
));
270 csum
= HOST_TO_BE16(fwcsum(&ibuf
));
272 memcpy(obuf
.start
+ obuf
.size
- sizeof(unsigned short),
273 &csum
, sizeof(unsigned short));
275 ofinfo
.size
= obuf
.size
;
277 if (fwwrite(&ofinfo
, &obuf
))