summaryrefslogtreecommitdiffstats
path: root/src/mkh3cimg.c
blob: d5660537f1967509867db880e95baaf1cb51d69f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// SPDX-License-Identifier: GPL-2.0-only

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include <byteswap.h>
#include <endian.h>
#include <getopt.h>


#if !defined(__BYTE_ORDER)
#error "Unknown byte order"
#endif

#if __BYTE_ORDER == __BIG_ENDIAN
#define cpu_to_be16(x)  (x)
#define cpu_to_be32(x)  (x)
#elif __BYTE_ORDER == __LITTLE_ENDIAN
#define cpu_to_be16(x)  bswap_16(x)
#define cpu_to_be32(x)  bswap_32(x)
#else
#error "Unsupported endianness"
#endif


#define IMAGE_VERSION            1
#define FILE_VERSION             1
#define FILE_DESCRIPTION         "OpenWrt"
#define FILE_TYPE_MASK           0x1


#define PACKAGE_FLAG             2

#define FILE_TYPE_APPLICATION    0x04000000

#define VERSION_OFFSET_INVALID   0xffffffff

#define COMPRESSION_TYPE_NONE    0xffffffff
#define COMPRESSION_TYPE_7Z      0x00000002


struct file_header {
	uint8_t res1[4];

	uint32_t header_crc;

	uint32_t file_type;
	uint32_t version;
	uint32_t product_id;
	uint32_t device_id;

	uint32_t length_unpadded;

	uint32_t version_offset;

	uint16_t year;
	uint8_t month;
	uint8_t day;
	uint8_t res2[1];
	uint8_t hour;
	uint8_t minute;
	uint8_t second;

	uint8_t res3[64];

	char description[224];

	uint32_t length;

	uint32_t file_crc;

	uint32_t compression_type;
} __attribute__ ((packed));

struct file_desc {
	uint32_t file_type;
	uint32_t offset;
	uint32_t length;
	uint32_t file_crc;
	uint32_t version;
	uint32_t type_mask;
} __attribute__ ((packed));

struct image_header {
	uint32_t version;

	uint32_t file_count;

	uint32_t product_id;
	uint32_t device_id;

	uint16_t year;
	uint8_t month;
	uint8_t day;
	uint8_t res1[1];
	uint8_t hour;
	uint8_t minute;
	uint8_t second;

	uint16_t package_crc;
	uint16_t package_flag;

	uint32_t length;

	struct file_desc files[128];

	/* RSA signature, not required */
	uint8_t res2[3072];

	uint32_t header_crc;
} __attribute__ ((packed));


static void *buf;
static size_t buflen;

static size_t length_unpadded;
static size_t length;


static uint32_t crc16_xmodem(char *buf, size_t len) {
	uint32_t poly = 0x1021;
	uint32_t crc = 0;
	char b;
	int i, j;

	for (i = 0; i < len; i++) {
		b = buf[i];
		crc = crc ^ (b << 8);

		for (j = 0; j < 8; j++) {
			crc = crc << 1;
			if (crc & 0x10000) {
				crc = (crc ^ poly) & 0xffff;
			}
		}
	}

	return crc;
}

static int create_buffer_and_read_file(char *filename) {
	FILE *f;

	f = fopen(filename, "r");
	if (f == NULL) {
		fprintf(stderr, "failed to open input file\n");
		goto err;
	}

	fseek(f, 0L, SEEK_END);
	length_unpadded = ftell(f);
	rewind(f);

	length = length_unpadded;
	if (length_unpadded % 8 != 0) {
		length += 8 - length_unpadded % 8;
	}

	buflen = sizeof(struct file_header) + sizeof(struct image_header) + length;
	buf = malloc(buflen);
	if (!buf) {
		fprintf(stderr, "failed to allocate buffer\n");
		goto err_close;
	}

	memset(buf, 0, buflen);

	if (fread(buf + sizeof(struct file_header) + sizeof(struct image_header), length_unpadded, 1, f) != 1) {
		fprintf(stderr, "failed to read input file\n");
		goto err_free;
	}

	fclose(f);
	return 0;

err_free:
	free(buf);
err_close:
	fclose(f);
err:
	return -1;
}

static void build_file_header(uint32_t product_id, uint32_t device_id, uint32_t compression_type) {
	struct file_header *header = buf + sizeof(struct image_header);
	uint32_t crc;

	header->file_type = cpu_to_be32(FILE_TYPE_APPLICATION);

	header->version = cpu_to_be32(FILE_VERSION);

	header->product_id = cpu_to_be32(product_id);
	header->device_id = cpu_to_be32(device_id);

	header->length_unpadded = cpu_to_be32(length_unpadded);

	header->version_offset = cpu_to_be32(VERSION_OFFSET_INVALID);

	header->year = cpu_to_be16(1970);
	header->month = 1;
	header->day = 1;
	header->hour = 0;
	header->minute = 0;
	header->second = 0;

	snprintf(header->description, sizeof(header->description), "%s", FILE_DESCRIPTION);

	header->length = cpu_to_be32(length);

	crc = crc16_xmodem(buf + sizeof(struct image_header) + sizeof(struct file_header), length);
	header->file_crc = cpu_to_be32(crc);

	header->compression_type = cpu_to_be32(compression_type);

	crc = crc16_xmodem((char *)header + sizeof(header->res1) + sizeof(header->header_crc),
		sizeof(struct file_header) - sizeof(header->res1) - sizeof(header->header_crc));
	header->header_crc = cpu_to_be32(crc);
}

static void build_image_header(uint32_t product_id, uint32_t device_id) {
	struct image_header *header = buf;
	struct file_header *file_header = buf + sizeof(struct image_header);
	uint32_t crc;

	header->version = cpu_to_be32(IMAGE_VERSION);

	header->file_count = cpu_to_be32(1);

	header->product_id = cpu_to_be32(product_id);
	header->device_id = cpu_to_be32(device_id);

	header->year = cpu_to_be16(1970);
	header->month = 1;
	header->day = 1;
	header->hour = 0;
	header->minute = 0;
	header->second = 0;

	crc = crc16_xmodem(buf + sizeof(struct file_header), buflen - sizeof(struct file_header));
	header->package_crc = cpu_to_be16(crc);
	header->package_flag = cpu_to_be16(PACKAGE_FLAG);

	header->length = cpu_to_be32(buflen - sizeof(struct image_header));

	header->files[0].file_type = file_header->file_type;
	header->files[0].offset = cpu_to_be32(sizeof(struct image_header));
	header->files[0].length = cpu_to_be32(sizeof(struct file_header) + length);
	header->files[0].file_crc = file_header->file_crc;
	header->files[0].version = file_header->version;
	header->files[0].type_mask = cpu_to_be32(FILE_TYPE_MASK);

	crc = crc16_xmodem((char *)header, sizeof(struct image_header) - sizeof(header->header_crc));
	header->header_crc = cpu_to_be32(crc);
}

static int write_output_file(char *filename) {
	int ret = 0;
	FILE *f;

	f = fopen(filename, "w");
	if (f == NULL) {
		fprintf(stderr, "failed to open output file\n");
		ret = -1;
		goto err;
	}

	if (fwrite(buf, buflen, 1, f) != 1) {
		fprintf(stderr, "failed to write output file\n");
		ret = -1;
	}

	fclose(f);

err:
	return ret;
}

static void usage(char* argv[]) {
	printf("Usage: %s [OPTIONS...]\n"
	       "\n"
	       "Options:\n"
	       "  -p <product id>   product id (32-bit unsigned integer)\n"
	       "  -d <device id>    device id (32-bit unsigned integer)\n"
	       "  -c <compression>  compression type of the input file (7z or none)\n"
	       "                    (in case of 7z only LZMA compression is allowed)\n"
	       "  -i <file>         input filename\n"
	       "  -o <file>         output filename\n"
	       , argv[0]);
}

int main(int argc, char* argv[]) {
	int ret = EXIT_FAILURE;

	static uint32_t product_id = 0;
	static uint32_t device_id = 0;
	static uint32_t compression_type = COMPRESSION_TYPE_NONE;
	static char *input_filename = NULL;
	static char *output_filename = NULL;

	while ( 1 ) {
		int c;

		c = getopt(argc, argv, "p:d:c:i:o:");
		if (c == -1)
			break;

		switch (c) {
		case 'p':
			product_id = strtoul(optarg, NULL, 0);
			break;
		case 'd':
			device_id = strtoul(optarg, NULL, 0);
			break;
		case 'c':
			if (strcmp(optarg, "none") == 0) {
				compression_type = COMPRESSION_TYPE_NONE;
			} else if (strcmp(optarg, "7z") == 0) {
				compression_type = COMPRESSION_TYPE_7Z;
			} else {
				usage(argv);
				return EXIT_FAILURE;
			}
			break;
		case 'i':
			input_filename = optarg;
			break;
		case 'o':
			output_filename = optarg;
			break;
		default:
			usage(argv);
			goto err;
		}
	}

	if (!product_id || !device_id ||
		!input_filename || strlen(input_filename) == 0 ||
		!output_filename || strlen(output_filename) == 0) {

		usage(argv);
		goto err;
	}

	if (create_buffer_and_read_file(input_filename)) {
		goto err;
	}

	build_file_header(product_id, device_id, compression_type);

	build_image_header(product_id, device_id);

	if (write_output_file(output_filename)) {
		goto err_free;
	}

	ret = EXIT_SUCCESS;

err_free:
	free(buf);
err:
	return ret;
}