summaryrefslogtreecommitdiff
path: root/extra/usb_updater/desc_parser.c
blob: 5bd996bdda79bdcc5e9a30f76d9622959a5d39dd (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
367
368
369
370
371
372
373
374
375
376
377
/*
 * Copyright 2018 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.
 */

#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#include "desc_parser.h"

static FILE *hash_file_;
static int line_count_;
static int section_count_;

/*
 * This is used to verify consistency of the description database, namely that
 * all hash sections include the same number of hash variants.
 */
static size_t variant_count;

/* Size of the retrieved string or negative OS error value. */
static ssize_t get_next_line(char *next_line, size_t line_size)
{
	size_t index = 0;

	while (fgets(next_line + index, line_size - index, hash_file_)) {
		line_count_++;

		if (next_line[index] == '#')
			continue; /* Skip the comment */

		if (next_line[index] == '\n') {
			/*
			 * This is an empty line, return all collected data,
			 * pontintially an array of size zero if this is a
			 * repeated empty line.
			 */
			next_line[index] = '\0';
			return index;
		}

		/* Make sure next string overwrites this string's newline. */
		index += strlen(next_line + index) - 1;

		if (index >= (line_size - 1)) {
			fprintf(stderr, "%s: Input overflow in line %d\n",
				__func__, line_count_);
			return -EOVERFLOW;
		}
	}

	if (index) {
		/*
		 * This must be the last line in the file with no empty line
		 * after it. Drop the closing newline, if it is there.
		 */
		if (next_line[index] == '\n')
			next_line[index--] = '\0';

		return index;
	}
	return errno ? -errno : -ENODATA;
}

static int get_next_token(char *input, size_t expected_size, char **output)
{
	char *next_colon;

	next_colon = strchr(input, ':');
	if (next_colon)
		*next_colon = '\0';
	if (!next_colon || (expected_size &&
			    strlen(input) != expected_size)) {
		fprintf(stderr, "Invalid entry in section %d\n",
			section_count_);
		return -EINVAL;
	}

	*output = next_colon + 1;
	return 0;
}

static int get_hex_value(char *input, char **output)
{
	char *e;
	long int value;

	if (strchr(input, ':'))
		get_next_token(input, 0, output);
	else
		*output = NULL;

	value = strtol(input, &e, 16);
	if ((e && *e) || (strlen(input) > 8)) {
		fprintf(stderr, "Invalid hex value %s in section %d\n",
			input, section_count_);
		return -EINVAL;
	}

	return value;
}

static int parse_range(char *next_line,
		       size_t line_len,
		       struct addr_range *parsed_range)
{
	char *line_cursor;
	char *next_token;
	int is_a_hash_range;
	struct result_node *node;
	int value;

	section_count_++;
	line_cursor = next_line;

	/* Range type. */
	if (get_next_token(line_cursor, 1, &next_token))
		return -EINVAL;

	switch (*line_cursor) {
	case 'a':
		parsed_range->range_type = AP_RANGE;
		break;
	case 'e':
		parsed_range->range_type = EC_RANGE;
		break;
	case 'g':
		parsed_range->range_type = EC_GANG_RANGE;
		break;
	default:
		fprintf(stderr, "Invalid range type %c in section %d\n",
			*line_cursor, section_count_);
		return -EINVAL;
	}
	line_cursor = next_token;

	/* Hash or dump? */
	if (get_next_token(line_cursor, 1, &next_token))
		return -EINVAL;

	switch (*line_cursor) {
	case 'd':
		is_a_hash_range = 0;
		break;
	case 'h':
		is_a_hash_range = 1;
		break;
	default:
		fprintf(stderr, "Invalid entry kind %c in section %d\n",
			*line_cursor, section_count_);
		return -EINVAL;
	}
	line_cursor = next_token;

	/* Range base address. */
	value = get_hex_value(line_cursor, &next_token);
	if (value < 0)
		return -EINVAL;
	parsed_range->base_addr = value;

	/* Range size. */
	line_cursor = next_token;
	value = get_hex_value(line_cursor, &next_token);
	if (value < 0)
		return -EINVAL;
	parsed_range->range_size = value;

	if (!next_token && is_a_hash_range) {
		fprintf(stderr, "Missing hash in section %d\n", section_count_);
		return -EINVAL;
	}

	if (next_token && !is_a_hash_range) {
		fprintf(stderr, "Unexpected data in section %d\n",
			section_count_);
		return -EINVAL;
	}

	parsed_range->variant_count = 0;
	if (!is_a_hash_range)
		return 0; /* No more input for dump ranges. */

	node = parsed_range->variants;
	do { /* While line is not over. */
		char c;
		int i = 0;

		line_cursor = next_token;
		next_token = strchr(line_cursor, ':');
		if (next_token)
			*next_token++ = '\0';
		if (strlen(line_cursor) != (2 * sizeof(*node))) {
			fprintf(stderr,
				"Invalid hash %zd size %zd in section %d\n",
				parsed_range->variant_count + 1,
				strlen(line_cursor), section_count_);
			return -EINVAL;
		}

		while ((c = *line_cursor++) != 0) {
			uint8_t nibble;

			if (!isxdigit(c)) {
				fprintf(stderr,
					"Invalid hash %zd value in section %d\n",
					parsed_range->variant_count + 1,
					section_count_);
				return -EINVAL;
			}

			if (c <= '9')
				nibble = c - '0';
			else if (c >= 'a')
				nibble = c - 'a' + 10;
			else
				nibble = c - 'A' + 10;

			if (i & 1)
				node->expected_result[i / 2] |= nibble;
			else
				node->expected_result[i / 2] = nibble << 4;

			i++;
		}

		node++;
		parsed_range->variant_count++;

	} while (next_token);

	return 0;
}

int parser_get_next_range(struct addr_range **range)
{
	char next_line[1000]; /* Should be enough for the largest descriptor. */
	ssize_t entry_size;
	struct addr_range *new_range;
	int rv;

	/*
	 * We come here after hash descriptor database file was opened and the
	 * current board's section has been found. Just in case check if the
	 * file has been opened.
	 */
	if (!hash_file_ || !range)
		return -EIO;

	*range = NULL;
	do {
		entry_size = get_next_line(next_line, sizeof(next_line));
		if (entry_size < 0)
			return entry_size;
	} while (!entry_size); /* Skip empty lines. */

	if (entry_size == 4) /* Next board's entry must have been reached. */
		return -ENODATA;

	/* This sure will be enough to fit parsed structure contents. */
	new_range = malloc(sizeof(*new_range) + entry_size);
	if (!new_range) {
		fprintf(stderr, "Failed to allocate %zd bytes\n",
			sizeof(*new_range) + entry_size);
		return -ENOMEM;
	}

	/* This must be a new descriptor section, lets parse it. */
	rv = parse_range(next_line, entry_size, new_range);

	if (rv) {
		free(new_range);
		return rv;
	}

	if (new_range->variant_count) {
		/*
		 * A new range was found, if this is the first hash range we
		 * encountered, save its dimensions for future reference.
		 *
		 * If this is not the first one - verify that it has the same
		 * number of hash variants as all previous hash blocks.
		 */
		if (!variant_count) {
			variant_count = new_range->variant_count;
		} else if (variant_count != new_range->variant_count) {
			fprintf(stderr,
				"Unexpected number of variants in section %d\n",
				section_count_);
			free(new_range);
			return -EINVAL;
		}
	}

	*range = new_range;
	return 0;

}

int parser_find_board(const char *hash_file_name, const char *board_id)
{
	char next_line[1000]; /* Should be enough for the largest descriptor. */
	ssize_t id_len = strlen(board_id);

	if (!hash_file_) {
		hash_file_ = fopen(hash_file_name, "r");
		if (!hash_file_) {
			fprintf(stderr, "Error:%s can not open file '%s'\n",
				strerror(errno), hash_file_name);
			return errno;
		}
	}

	while (1) {
		ssize_t entry_size;

		entry_size = get_next_line(next_line, sizeof(next_line));
		if (entry_size < 0) {
			return entry_size;
		}

		if ((entry_size == id_len) &&
		    !memcmp(next_line, board_id, id_len)) {
			variant_count = 0;
			return 0;
		}
	}

	return -ENODATA;
}

void parser_done(void)
{
	if (!hash_file_)
		return;

	fclose(hash_file_);
	hash_file_ = NULL;
}

#ifdef TEST_PARSER
int main(int argc, char **argv)
{
	const char *board_name = "QZUX";
	char next_line[1000]; /* Should be enough for the largest descriptor. */
	int rv;
	int count;

	if (argc < 2) {
		fprintf(stderr, "Name of the file to parse is required.\n");
		return -1;
	}

	if (parser_find_board(argv[1], board_name)) {
		fprintf(stderr, "Board %s NOT found\n", board_name);
		return -1;
	}

	count = 0;
	do {
		struct addr_range *range;

		rv = parser_get_next_range(&range);
		count++;
		printf("Section %d, rv %d\n", count, rv);
		free(range); /* Freeing NULL is OK. */

	} while (rv != -ENODATA);

	return 0;
}
#endif