3 * Copyright (C) 2012 OpenWrt.org
4 * Copyright (C) 2012 Mikko Hissa <mikko.hissa@uta.fi>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
18 #include <arpa/inet.h>
23 #define BUF_SIZE 0x200
24 #define VERSION_SIZE 0x10
27 #define DEFAULT_MAGIC 305419896
28 #define DEFAULT_VERSION "123"
29 #define DEFAULT_VENDOR_ID 536
30 #define DEFAULT_HEAD_VALUE 0x0
31 #define DEFAULT_BLOCK_SIZE 65535
32 #define DEFAULT_PRODUCT_ID 48
33 #define DEFAULT_FIRMWARE_TYPE 0x03
39 uint8_t version
[VERSION_SIZE
];
40 uint32_t firmware_type
;
43 uint8_t md5sum
[MD5_SIZE
];
44 uint8_t pad
[PAD_SIZE
];
58 static firmware_type FIRMWARE_TYPES
[] = {
59 { 0x01, "bootloader" },
61 { 0x03, "kernelapp" },
63 { 0x05, "littleapps" },
65 { 0x07, "userconfig" },
66 { 0x0a, "factoryapps" },
71 static long get_file_size(const char *filename
)
76 fp_file
= fopen(filename
, "r");
79 fseek(fp_file
, 0, SEEK_END
);
80 result
= ftell(fp_file
);
85 static int header_checksum(void *data
, int len
)
91 if (data
!= NULL
&& len
>= 0) {
92 for (i
= 0; i
< len
; ++i
)
93 sum
+= *(unsigned char *) (data
+ i
);
100 static int md5_file(const char *filename
, uint8_t *dst
)
109 fp_src
= fopen(filename
, "r+b");
113 while (!feof(fp_src
)) {
114 bytes_read
= fread(&buf
, 1, BUF_SIZE
, fp_src
);
115 MD5_Update(&ctx
, &buf
, bytes_read
);
119 MD5_Final(dst
, &ctx
);
124 static int encode_image(const char *input_file_name
,
125 const char *output_file_name
, img_header
*header
, int block_size
)
138 fp_input
= fopen(input_file_name
, "r+b");
140 fprintf(stderr
, "Cannot open %s !!\n", input_file_name
);
144 fp_output
= fopen(output_file_name
, "w+b");
146 fprintf(stderr
, "Cannot open %s !!\n", output_file_name
);
151 header
->filesize
= get_file_size(input_file_name
);
152 if (!header
->filesize
) {
153 fprintf(stderr
, "File %s open/size error!\n", input_file_name
);
161 if (block_size
> 0) {
162 pad_len
= block_size
- (header
->filesize
% block_size
);
165 if (md5_file(input_file_name
, (uint8_t *) &header
->md5sum
) < 0) {
166 fprintf(stderr
, "Md5 failed on file %s\n", input_file_name
);
172 header
->chksum
= header_checksum(header
, HDR_LEN
);
173 header
->head
= htonl(header
->head
);
174 header
->vendor_id
= htonl(header
->vendor_id
);
175 header
->product_id
= htonl(header
->product_id
);
176 header
->firmware_type
= htonl(header
->firmware_type
);
177 header
->filesize
= htonl(header
->filesize
);
178 header
->chksum
= htonl(header
->chksum
);
179 magic
= header
->magic
;
180 header
->magic
= htonl(header
->magic
);
182 fwrite(header
, HDR_LEN
, 1, fp_output
);
184 while (!feof(fp_input
) || pad_len
> 0) {
187 bytes_read
= fread(&buf
, 1, BUF_SIZE
, fp_input
);
192 * No more bytes read, start padding
194 if (bytes_read
< BUF_SIZE
&& pad_len
> 0) {
195 bytes_avail
= BUF_SIZE
- bytes_read
;
196 memset( &buf
[bytes_read
], 0, bytes_avail
);
197 bytes_read
+= bytes_avail
< pad_len
? bytes_avail
: pad_len
;
198 pad_len
-= bytes_avail
< pad_len
? bytes_avail
: pad_len
;
201 for (i
= 0; i
< bytes_read
; i
++)
202 buf
[i
] ^= magic
>> (i
% 8) & 0xff;
203 fwrite(&buf
, bytes_read
, 1, fp_output
);
211 int decode_image(const char *input_file_name
, const char *output_file_name
)
221 size_t bytes_written
;
223 fp_input
= fopen(input_file_name
, "r+b");
225 fprintf(stderr
, "Cannot open %s !!\n", input_file_name
);
230 fp_output
= fopen(output_file_name
, "w+b");
232 fprintf(stderr
, "Cannot open %s !!\n", output_file_name
);
237 if (fread(&header
, 1, HDR_LEN
, fp_input
) != HDR_LEN
) {
238 fprintf(stderr
, "Incorrect header size!!");
244 header
.head
= ntohl(header
.head
);
245 header
.vendor_id
= ntohl(header
.vendor_id
);
246 header
.product_id
= ntohl(header
.product_id
);
247 header
.firmware_type
= ntohl(header
.firmware_type
);
248 header
.filesize
= ntohl(header
.filesize
);
249 header
.chksum
= ntohl(header
.chksum
);
250 header
.magic
= ntohl(header
.magic
);
253 while (!feof(fp_input
)) {
255 bytes_read
= fread(&buf
, 1, BUF_SIZE
, fp_input
);
256 for (i
= 0; i
< bytes_read
; i
++)
257 buf
[i
] ^= header
.magic
>> (i
% 8) & 0xff;
260 * Handle padded source file
262 if (bytes_written
+ bytes_read
> header
.filesize
) {
263 bytes_read
= header
.filesize
- bytes_written
;
265 fwrite(&buf
, bytes_read
, 1, fp_output
);
269 fwrite(&buf
, bytes_read
, 1, fp_output
);
270 bytes_written
+= bytes_read
;
279 static void usage(const char *progname
, int status
)
281 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
284 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
287 " -e <file> encode image file <file>\n"
288 " -d <file> decode image file <file>\n"
289 " -o <file> write output to the file <file>\n"
290 " -t <type> set image type to <type>, defaults to 3\n"
291 " valid image <type> values:\n");
292 for (i
= 0; i
< sizeof(FIRMWARE_TYPES
) / sizeof(firmware_type
); i
++) {
293 fprintf(stream
, " %-5i= %s\n", FIRMWARE_TYPES
[i
].id
,
294 FIRMWARE_TYPES
[i
].name
);
296 fprintf(stream
, " -v <version> set image version to <version>\n"
297 " -r <vendor> set image vendor id to <vendor>\n"
298 " -p <product> set image product id to <product>\n"
299 " -m <magic> set encoding magic <magic>\n"
300 " -z enable image padding to <blocksize>\n"
301 " -b <blocksize> set image <blocksize>, defaults to 65535\n"
302 " -h show this screen\n");
306 int main(int argc
, char *argv
[])
309 char *input_file
, *output_file
, *progname
= NULL
;
315 block_size
= DEFAULT_BLOCK_SIZE
;
316 progname
= basename(argv
[0]);
318 memset(&header
, 0, sizeof( img_header
));
319 header
.magic
= DEFAULT_MAGIC
;
320 header
.head
= DEFAULT_HEAD_VALUE
;
321 header
.vendor_id
= DEFAULT_VENDOR_ID
;
322 header
.product_id
= DEFAULT_PRODUCT_ID
;
323 header
.firmware_type
= DEFAULT_FIRMWARE_TYPE
;
324 strncpy( (char*)&header
.version
, DEFAULT_VERSION
, VERSION_SIZE
- 1);
326 while ((opt
= getopt(argc
, argv
, ":o:e:d:t:v:r:p:m:b:h?z")) != -1) {
337 output_file
= optarg
;
340 tmp
= strtol(optarg
, 0, 10);
341 for (i
= 0; i
< sizeof(FIRMWARE_TYPES
) / sizeof(firmware_type
);
343 if (FIRMWARE_TYPES
[i
].id
== tmp
) {
344 header
.firmware_type
= FIRMWARE_TYPES
[i
].id
;
348 if (header
.firmware_type
== 0) {
349 fprintf(stderr
, "Invalid firmware <type>!\n");
350 usage(progname
, EXIT_FAILURE
);
354 strncpy( (char*)&header
.version
, optarg
,
358 header
.vendor_id
= strtol(optarg
, 0, 10);
361 header
.product_id
= strtol(optarg
, 0, 10);
364 header
.magic
= strtoul(optarg
, 0, 16);
370 block_size
= strtol(optarg
, 0, 10);
373 usage(progname
, EXIT_SUCCESS
);
376 fprintf(stderr
, "Option -%c requires an operand\n", optopt
);
377 usage(progname
, EXIT_FAILURE
);
380 fprintf(stderr
, "Unrecognized option: -%c\n", optopt
);
381 usage(progname
, EXIT_FAILURE
);
384 usage(progname
, EXIT_FAILURE
);
389 if (input_file
== NULL
|| output_file
== NULL
) {
390 fprintf(stderr
, "Input and output files must be defined\n");
391 usage(progname
, EXIT_FAILURE
);
396 usage(progname
, EXIT_FAILURE
);
399 if (encode_image(input_file
, output_file
, &header
, pad
? block_size
: 0)
404 if (decode_image(input_file
, output_file
) < 0)