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
28 #define DEFAULT_BLOCK_SIZE 65535
30 #define DEFAULT_HEAD_VALUE 0x0
31 #define DEFAULT_VERSION "123"
32 #define DEFAULT_MAGIC 0x12345678
38 uint8_t version[VERSION_SIZE];
39 uint32_t firmware_type;
42 uint8_t md5sum[MD5_SIZE];
43 uint8_t pad[PAD_SIZE];
57 static firmware_type FIRMWARE_TYPES[] = {
58 { 0x01, "bootloader" },
60 { 0x03, "kernelapp" },
62 /* The types below this line vary by manufacturer */
63 { 0x05, "littleapps (D-Link)/factoryapps (EnGenius)" },
64 { 0x06, "sounds (D-Link)/littleapps (EnGenius)" },
65 { 0x07, "userconfig (D-Link)/appdata (EnGenius)" },
66 { 0x08, "userconfig (EnGenius)"},
67 { 0x09, "odmapps (EnGenius)"},
68 { 0x0a, "factoryapps (D-Link)" },
69 { 0x0b, "odmapps (D-Link)" },
70 { 0x0c, "langpack (D-Link)" }
73 static long get_file_size(const char *filename)
78 fp_file = fopen(filename, "r");
81 fseek(fp_file, 0, SEEK_END);
82 result = ftell(fp_file);
87 static int header_checksum(void *data, int len)
93 if (data != NULL && len >= 0) {
94 for (i = 0; i < len; ++i)
95 sum += *(unsigned char *) (data + i);
102 static int md5_file(const char *filename, uint8_t *dst)
111 fp_src = fopen(filename, "r+b");
115 while (!feof(fp_src)) {
116 bytes_read = fread(&buf, 1, BUF_SIZE, fp_src);
117 MD5_Update(&ctx, &buf, bytes_read);
121 MD5_Final(dst, &ctx);
126 static int encode_image(const char *input_file_name,
127 const char *output_file_name, img_header *header, int block_size)
140 fp_input = fopen(input_file_name, "r+b");
142 fprintf(stderr, "Cannot open %s !!\n", input_file_name);
146 fp_output = fopen(output_file_name, "w+b");
148 fprintf(stderr, "Cannot open %s !!\n", output_file_name);
153 header->filesize = get_file_size(input_file_name);
154 if (!header->filesize) {
155 fprintf(stderr, "File %s open/size error!\n", input_file_name);
163 if (block_size > 0) {
164 pad_len = block_size - (header->filesize % block_size);
167 if (md5_file(input_file_name, (uint8_t *) &header->md5sum) < 0) {
168 fprintf(stderr, "MD5 failed on file %s\n", input_file_name);
174 header->chksum = header_checksum(header, HDR_LEN);
175 header->head = htonl(header->head);
176 header->vendor_id = htonl(header->vendor_id);
177 header->product_id = htonl(header->product_id);
178 header->firmware_type = htonl(header->firmware_type);
179 header->filesize = htonl(header->filesize);
180 header->chksum = htonl(header->chksum);
181 magic = header->magic;
182 header->magic = htonl(header->magic);
184 fwrite(header, HDR_LEN, 1, fp_output);
186 while (!feof(fp_input) || pad_len > 0) {
189 bytes_read = fread(&buf, 1, BUF_SIZE, fp_input);
194 * No more bytes read, start padding
196 if (bytes_read < BUF_SIZE && pad_len > 0) {
197 bytes_avail = BUF_SIZE - bytes_read;
198 memset( &buf[bytes_read], 0, bytes_avail);
199 bytes_read += bytes_avail < pad_len ? bytes_avail : pad_len;
200 pad_len -= bytes_avail < pad_len ? bytes_avail : pad_len;
203 for (i = 0; i < bytes_read; i++)
204 buf[i] ^= magic >> (i % 8) & 0xff;
205 fwrite(&buf, bytes_read, 1, fp_output);
213 int decode_image(const char *input_file_name, const char *output_file_name)
223 size_t bytes_written;
225 fp_input = fopen(input_file_name, "r+b");
227 fprintf(stderr, "Cannot open %s !!\n", input_file_name);
232 fp_output = fopen(output_file_name, "w+b");
234 fprintf(stderr, "Cannot open %s !!\n", output_file_name);
239 if (fread(&header, 1, HDR_LEN, fp_input) != HDR_LEN) {
240 fprintf(stderr, "Incorrect header size!!");
246 header.head = ntohl(header.head);
247 header.vendor_id = ntohl(header.vendor_id);
248 header.product_id = ntohl(header.product_id);
249 header.firmware_type = ntohl(header.firmware_type);
250 header.filesize = ntohl(header.filesize);
251 header.chksum = ntohl(header.chksum);
252 header.magic = ntohl(header.magic);
255 while (!feof(fp_input)) {
257 bytes_read = fread(&buf, 1, BUF_SIZE, fp_input);
258 for (i = 0; i < bytes_read; i++)
259 buf[i] ^= header.magic >> (i % 8) & 0xff;
262 * Handle padded source file
264 if (bytes_written + bytes_read > header.filesize) {
265 bytes_read = header.filesize - bytes_written;
267 fwrite(&buf, bytes_read, 1, fp_output);
271 fwrite(&buf, bytes_read, 1, fp_output);
272 bytes_written += bytes_read;
281 static void usage(const char *progname, int status)
283 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
286 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
289 " -e <file> encode image file <file>\n"
290 " -d <file> decode image file <file>\n"
291 " -o <file> write output to the file <file>\n"
292 " -t <type> set image type to <type>\n"
293 " valid image <type> values:\n");
294 for (i = 0; i < sizeof(FIRMWARE_TYPES) / sizeof(firmware_type); i++) {
295 fprintf(stream, " %-5i= %s\n", FIRMWARE_TYPES[i].id,
296 FIRMWARE_TYPES[i].name);
298 fprintf(stream, " -v <version> set image version to <version>\n"
299 " -r <vendor> set image vendor id to <vendor>\n"
300 " -p <product> set image product id to <product>\n"
301 " -m <magic> set encoding magic <magic>\n"
302 " -z enable image padding to <blocksize>\n"
303 " -b <blocksize> set image <blocksize>, defaults to %u\n"
304 " -h show this screen\n", DEFAULT_BLOCK_SIZE);
308 int main(int argc, char *argv[])
311 char *input_file, *output_file, *progname = NULL;
317 block_size = DEFAULT_BLOCK_SIZE;
318 progname = basename(argv[0]);
320 memset(&header, 0, sizeof( img_header ));
321 header.magic = DEFAULT_MAGIC;
322 header.head = DEFAULT_HEAD_VALUE;
323 strncpy( (char*)&header.version, DEFAULT_VERSION, VERSION_SIZE - 1);
325 while ((opt = getopt(argc, argv, ":o:e:d:t:v:r:p:m:b:h?z")) != -1) {
336 output_file = optarg;
339 tmp = strtol(optarg, 0, 10);
340 for (i = 0; i < sizeof(FIRMWARE_TYPES) / sizeof(firmware_type);
342 if (FIRMWARE_TYPES[i].id == tmp) {
343 header.firmware_type = FIRMWARE_TYPES[i].id;
347 if (header.firmware_type == 0) {
348 fprintf(stderr, "Invalid firmware type \"0\"!\n");
349 usage(progname, EXIT_FAILURE);
353 strncpy( (char*)&header.version, optarg,
357 header.vendor_id = strtol(optarg, 0, 0);
360 header.product_id = strtol(optarg, 0, 0);
363 header.magic = strtoul(optarg, 0, 16);
369 block_size = strtol(optarg, 0, 10);
372 usage(progname, EXIT_SUCCESS);
375 fprintf(stderr, "Option -%c requires an operand\n", optopt);
376 usage(progname, EXIT_FAILURE);
379 fprintf(stderr, "Unrecognized option: -%c\n", optopt);
380 usage(progname, EXIT_FAILURE);
383 usage(progname, EXIT_FAILURE);
388 /* Check required arguments */
390 fprintf(stderr, "A mode must be defined\n");
391 usage(progname, EXIT_FAILURE);
394 if (input_file == NULL || output_file == NULL) {
395 fprintf(stderr, "Input and output files must be defined\n");
396 usage(progname, EXIT_FAILURE);
399 if (mode == DECODE) {
400 if (decode_image(input_file, output_file) < 0)
406 if (header.firmware_type == 0) {
407 fprintf(stderr, "Firmware type must be defined\n");
408 usage(progname, EXIT_FAILURE);
411 if (header.vendor_id == 0 || header.product_id == 0) {
412 fprintf(stderr, "Vendor ID and Product ID must be defined and non-zero\n");
413 usage(progname, EXIT_FAILURE);
416 if (encode_image(input_file, output_file, &header, pad ? block_size : 0) < 0)