summaryrefslogtreecommitdiff
path: root/util/gen_bmi_config.c
blob: 44456cf1cc5bad16f9f5b1b45e8dd92969f5d306 (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
/*
 * Copyright 2020 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/*
 * TODO(b/160330682): Eliminate or reduce size of BMI260 initialization file.
 *
 * Once the BMI260 initialization file is moved to the kernel's rootfs, this
 * entire file can be deleted.
 */

/*
 * This utility is used to generate a compressed version of the BMI260
 * configuration file.
 *
 * This uses a very simple, but lightweight compression algorithm to detect
 * duplicated 32-bit words in the configuration data.
 *
 * Compression scheme:
 *	Repeated 32-bit words are replaced by a 16-bit key, 16-bit count, and
 *	the 32-bit data word. All values stored big-endian.
 *
 *	For example, if the uncompressed file had the following data words:
 *		0x89ABCDEF 0x89ABCDEF 0x89ABCDEF
 *
 *	This is represented compressed as (key 0xE9EA):
 *		0xE9EA0003 0x89ABCDEF
 *
 *	Key value (0xE9EA) chosen as it wasn't found in the BMI configuration
 *	data.
 */
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "endian.h"

/*
 * This key is chosen because it isn't used by the BMI260 config file.
 */
#define COMPRESS_KEY	0xE9EA

static int word_compress(uint32_t *buffer, int word_length,
			 const char *outfilename)
{
	FILE *file_p;
	const uint16_t key = COMPRESS_KEY;
	uint16_t out16;
	uint16_t in16;
	unsigned int outsize;
	uint32_t prev_word;
	int repeat_count;
	int i;

	file_p = fopen(outfilename, "wb+");
	if (!file_p) {
		fprintf(stderr, "Failed to open output file %s\n", outfilename);
		return -1;
	}

	prev_word = ~buffer[0];
	repeat_count = 1;
	outsize = 0;

	for (i = 0; i < word_length; i++) {
		in16 = *(uint16_t *)&buffer[i];
		in16 = be16toh(in16);

		if (in16 == COMPRESS_KEY) {
			fprintf(stderr, "ERROR: input data contains "
				"compression key value 0x%04x\n",
				COMPRESS_KEY);
			fprintf(stderr, "Compression of input data not "
				"supported.\n");
			outsize = -1;
			goto early_exit;
		}

		if (buffer[i] == prev_word && (repeat_count < 255)) {
			repeat_count++;
		} else {
			if (repeat_count > 2) {
				printf("Offset 0x%08lx: Write repeated "
					"signature: 0x%04x 0x%04x 0x%08x\n",
					ftell(file_p), key, repeat_count,
					prev_word);
				out16 = htobe16(key);
				fwrite(&out16, sizeof(out16), 1, file_p);
				out16 = htobe16(repeat_count);
				fwrite(&out16, sizeof(out16), 1, file_p);
				outsize += 2 * sizeof(out16);

				/*
				 * Write out original data as bytes
				 * to preserve original order.
				 */
				fwrite(&prev_word, 1, sizeof(prev_word),
				       file_p);
				outsize += sizeof(prev_word);
			} else if (i != 0) {
				do {
					fwrite(&prev_word, 1, sizeof(prev_word),
					       file_p);
					outsize += sizeof(prev_word);
				} while (--repeat_count > 0);
			}

			prev_word = buffer[i];
			repeat_count = 1;
		}
	}

	/* Write the last word or repeated words */
	if (repeat_count > 2) {
		printf("Offset 0x%08lx: Write repeated signature: "
		       "0x%04x 0x%04x 0x%08x\n",
		       ftell(file_p), key, repeat_count, prev_word);
		fwrite(&out16, sizeof(out16), 1, file_p);
		out16 = htobe16(repeat_count);
		fwrite(&out16, sizeof(out16), 1, file_p);
		outsize += 2 * sizeof(out16);

		fwrite(&prev_word, 1, sizeof(prev_word), file_p);
		outsize += sizeof(prev_word);
	} else if (i != 0) {
		do {
			fwrite(&prev_word, 1, sizeof(prev_word), file_p);
			outsize += sizeof(prev_word);
		} while (--repeat_count > 0);
	}

	if (outsize != ftell(file_p)) {
		fprintf(stderr, "Compression failed, write %d bytes, but file "
			"size is only %ld bytes\n",
			outsize, ftell(file_p));
		outsize = -1;
		goto early_exit;
	}

early_exit:
	fclose(file_p);

	return outsize;
}

static int word_decompress(uint32_t *buffer,
			   int word_length,
			   const char *outfilename)
{
	FILE *file_p;
	uint16_t *buf16;
	uint16_t key;
	uint16_t repeat_count;
	unsigned int outsize;
	uint32_t out_word;
	int i;

	file_p = fopen(outfilename, "wb+");
	if (!file_p) {
		fprintf(stderr, "Failed to open output file %s\n", outfilename);
		return -1;
	}

	repeat_count = 0;
	outsize = 0;

	for (i = 0; i < word_length; i++) {
		buf16 = (uint16_t *)&buffer[i];
		key = be16toh(*buf16);

		if (key == COMPRESS_KEY) {
			repeat_count = be16toh(*++buf16);

			if (repeat_count == 0) {
				fprintf(stderr, "Incorrect repeat count found "
					"in compressed file\n");
				outsize = -1;
				goto early_exit;
			}

			/*
			 * Note - this advances the loop to the next word
			 * in the buffer.
			 */
			if (++i >= word_length) {
				fprintf(stderr, "Unexpected file end during "
					"decompress\n");
				outsize = -1;
				goto early_exit;
			}

			out_word = buffer[i];
			while (repeat_count-- > 0) {
				fwrite(&out_word, 4, 1, file_p);
				outsize += sizeof(out_word);
			}
		} else {
			fwrite(&buffer[i], 4, 1, file_p);
			outsize += sizeof(buffer[i]);
		}
	}

early_exit:
	fclose(file_p);

	return outsize;
}

static void print_help(char *cmd_name)
{
	printf("\nUsage: %s <compress|decompress> <infile> <outfile>\n"
	       "\n"
	       "Utility to compress/decompress BMI IMU config binaries.\n",
	       cmd_name);
}

int main(int argc, char *argv[])
{
	uint32_t *buffer;
	int outsize;
	char *infilename;
	char *outfilename;
	FILE *f;
	long size;
	size_t read_words;

	if (argc < 4) {
		fprintf(stderr, "Unknown option or missing value\n");
		print_help(argv[0]);
		return EXIT_FAILURE;
	}

	infilename = argv[2];
	outfilename = argv[3];

	printf("Input (%s), output (%s)\n", infilename, outfilename);

	f = fopen(infilename, "rb");

	if (!f) {
		fprintf(stderr, "Failed to open input file %s\n", infilename);
		print_help(argv[0]);
		return EXIT_FAILURE;
	}

	fseek(f, 0, SEEK_END);
	size = ftell(f);
	rewind(f);

	printf("Infile (%s) size %ld (bytes)\n", infilename, size);
	buffer = malloc(size);
	size /= 4;
	if (!buffer) {
		fprintf(stderr, "Failed to allocate memory. "
			"Input file size %ld bytes\n",
			size * 4);
		fclose(f);
		return EXIT_FAILURE;
	}

	read_words = fread(buffer, 4, size, f);
	if (read_words != size) {
		fprintf(stderr, "Unable to read from %s, "
			"words read %ld, needed %ld\n",
			infilename, read_words, size);
		fclose(f);
		free(buffer);
		return EXIT_FAILURE;
	}

	fclose(f);

	printf("Input file closed\n");

	if (!strncmp(argv[1], "compress", sizeof("compress"))) {
		outsize = word_compress(buffer, size, outfilename);
		if (outsize < 0) {
			free(buffer);
			return EXIT_FAILURE;
		}

		printf("Compressed file %s created - %d bytes "
			"(saves %ld bytes)\n",
			outfilename, outsize, (size * 4) - outsize);
	} else if (!strncmp(argv[1], "decompress", sizeof("decompress"))) {
		outsize = word_decompress(buffer, size, outfilename);
		if (outsize < 0) {
			free(buffer);
			return EXIT_FAILURE;
		}

		printf("Decompressed file %s created - %d bytes\n",
			outfilename, outsize);
	} else {
		fprintf(stderr, "Invalid parameter 1, "
			"must be compress/decoompress\n");
		print_help(argv[0]);
		free(buffer);
		return EXIT_FAILURE;
	}

	free(buffer);

	return EXIT_SUCCESS;
}