1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 Vasilis Tsiligiannis <b_tsiligiannis@silverton.gr>
14 #include <endian.h> /* for __BYTE_ORDER */
19 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
20 # define HOST_TO_LE16(x) (x)
21 # define HOST_TO_LE32(x) (x)
22 # define HOST_TO_BE16(x) bswap_16(x)
23 # define HOST_TO_BE32(x) bswap_32(x)
25 # define HOST_TO_LE16(x) bswap_16(x)
26 # define HOST_TO_LE32(x) bswap_32(x)
27 # define HOST_TO_BE16(x) (x)
28 # define HOST_TO_BE32(x) (x)
33 unsigned char sign
[4];
36 unsigned char model
[4];
38 } __attribute__ ((packed
));
52 static char *progname
;
53 static int force_be
= FALSE
;
55 static void usage(int status
)
57 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
59 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
63 " -s <sig> set image signature to <sig>\n"
64 " -m <model> set model to <model>\n"
65 " -i <file> read input from file <file>\n"
66 " -o <file> write output to file <file>\n"
67 " -f <flash> set flash address to <flash>\n"
68 " -S <start> set start address to <start>\n"
69 " -b big-endianness mode\n");
74 static int strtou32(char *arg
, unsigned int *val
)
79 *val
= strtoul(arg
, &endptr
, 0);
80 if (errno
|| (endptr
== arg
) || (*endptr
&& (endptr
!= NULL
))) {
87 static unsigned short fwcsum (struct buf
*buf
) {
89 unsigned short ret
= 0;
91 for (i
= 0; i
< buf
->size
/ 2; i
++) {
92 if (force_be
== FALSE
)
93 ret
-= ((unsigned short *) buf
->start
)[i
];
95 ret
-= HOST_TO_BE16(((unsigned short *) buf
->start
)[i
]);
101 static int fwread(struct finfo
*finfo
, struct buf
*buf
)
105 f
= fopen(finfo
->name
, "r");
107 fprintf(stderr
, "could not open \"%s\" for reading\n", finfo
->name
);
111 buf
->size
= fread(buf
->start
, 1, finfo
->size
, f
);
112 if (buf
->size
!= finfo
->size
) {
113 fprintf(stderr
, "unable to read from file \"%s\"\n", finfo
->name
);
122 static int fwwrite(struct finfo
*finfo
, struct buf
*buf
)
126 f
= fopen(finfo
->name
, "w");
128 fprintf(stderr
, "could not open \"%s\" for writing\n", finfo
->name
);
132 buf
->size
= fwrite(buf
->start
, 1, finfo
->size
, f
);
133 if (buf
->size
!= finfo
->size
) {
134 fprintf(stderr
, "unable to write to file \"%s\"\n", finfo
->name
);
143 int main(int argc
, char **argv
)
146 struct header header
;
147 struct buf ibuf
, obuf
;
148 struct finfo ifinfo
, ofinfo
;
152 ifinfo
.name
= ofinfo
.name
= NULL
;
153 header
.flash
= header
.size
= header
.start
= 0;
154 progname
= basename(argv
[0]);
156 while((c
= getopt(argc
, argv
, "i:o:m:s:f:S:h:b")) != -1) {
159 ifinfo
.name
= optarg
;
162 ofinfo
.name
= optarg
;
165 if (strlen(optarg
) != 4) {
166 fprintf(stderr
, "model must be 4 characters long\n");
169 memcpy(header
.model
, optarg
, 4);
172 if (strlen(optarg
) != 4) {
173 fprintf(stderr
, "signature must be 4 characters long\n");
176 memcpy(header
.sign
, optarg
, 4);
182 if (!strtou32(optarg
, &header
.flash
)) {
183 fprintf(stderr
, "invalid flash address specified\n");
188 if (!strtou32(optarg
, &header
.start
)) {
189 fprintf(stderr
, "invalid start address specified\n");
202 if (ifinfo
.name
== NULL
) {
203 fprintf(stderr
, "no input file specified\n");
207 if (ofinfo
.name
== NULL
) {
208 fprintf(stderr
, "no output file specified\n");
212 if (stat(ifinfo
.name
, &st
)) {
213 fprintf(stderr
, "stat failed on %s\n", ifinfo
.name
);
217 if (header
.sign
== NULL
) {
218 fprintf(stderr
, "no signature specified\n");
222 if (header
.model
== NULL
) {
223 fprintf(stderr
, "no model specified\n");
228 fprintf(stderr
, "no flash address specified\n");
233 fprintf(stderr
, "no start address specified\n");
237 ifinfo
.size
= st
.st_size
;
239 obuf
.size
= ifinfo
.size
+ sizeof(struct header
) + sizeof(unsigned short);
240 if (obuf
.size
% sizeof(unsigned short))
243 obuf
.start
= malloc(obuf
.size
);
245 fprintf(stderr
, "no memory for buffer\n");
248 memset(obuf
.start
, 0, obuf
.size
);
250 ibuf
.size
= ifinfo
.size
;
251 ibuf
.start
= obuf
.start
+ sizeof(struct header
);
253 if (fwread(&ifinfo
, &ibuf
))
256 if (force_be
== FALSE
) {
257 header
.flash
= HOST_TO_LE32(header
.flash
);
258 header
.size
= HOST_TO_LE32(obuf
.size
- sizeof(struct header
));
259 header
.start
= HOST_TO_LE32(header
.start
);
261 header
.flash
= HOST_TO_BE32(header
.flash
);
262 header
.size
= HOST_TO_BE32(obuf
.size
- sizeof(struct header
));
263 header
.start
= HOST_TO_BE32(header
.start
);
266 memcpy (obuf
.start
, &header
, sizeof(struct header
));
268 if (force_be
== FALSE
)
269 csum
= HOST_TO_LE16(fwcsum(&ibuf
));
271 csum
= HOST_TO_BE16(fwcsum(&ibuf
));
273 memcpy(obuf
.start
+ obuf
.size
- sizeof(unsigned short),
274 &csum
, sizeof(unsigned short));
276 ofinfo
.size
= obuf
.size
;
278 if (fwwrite(&ofinfo
, &obuf
))