script_bin.c 9.53 KB
Newer Older
1
2
3
4
5
/*
 * Copyright (C) 2012  Alejandro Mery <amery@geeks.cl>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
Alejandro Mery's avatar
Alejandro Mery committed
6
 * the Free Software Foundation, either version 2 of the License, or
7
8
9
10
11
12
13
14
15
16
17
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

18
#include "common.h"
19

20
#include <ctype.h>
21
#include <errno.h>
22
23
24
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
25
#include <string.h>
26
27
28
29

#include "script.h"
#include "script_bin.h"

30
31
32
33
34
35
36
37
#define pr_info(...)	errf("fexc-bin: " __VA_ARGS__)
#define pr_err(...)	errf("E: fexc-bin: " __VA_ARGS__)

#ifdef DEBUG
#define pr_debug(...)	errf("D: fexc-bin: " __VA_ARGS__)
#else
#define pr_debug(...)
#endif
38

39
#define PTR(B, OFF)	(void*)((char*)(B)+(OFF))
40
41
#define WORDS(S)	(((S)+(sizeof(uint32_t)-1))/(sizeof(uint32_t)))

42
43
/*
 * generator
44
 */
Alejandro Mery's avatar
Alejandro Mery committed
45
46
size_t script_bin_size(struct script *script,
		       size_t *sections, size_t *entries)
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
{
	size_t words = 0, bin_size = 0;
	struct list_entry *ls, *le;
	struct script_section *section;
	struct script_entry *entry;
	struct script_string_entry *string;

	*sections = *entries = 0;

	/* count */
	for (ls = list_first(&script->sections); ls;
	     ls = list_next(&script->sections, ls)) {
		section = container_of(ls, struct script_section, sections);
		size_t c = 0;

		for (le = list_first(&section->entries); le;
		     le = list_next(&section->entries, le)) {
			size_t size = 0;
			entry = container_of(le, struct script_entry, entries);
			c++;

			switch(entry->type) {
			case SCRIPT_VALUE_TYPE_NULL:
			case SCRIPT_VALUE_TYPE_SINGLE_WORD:
				size = sizeof(uint32_t);
				break;
			case SCRIPT_VALUE_TYPE_STRING:
				string = container_of(entry, struct script_string_entry,
						      entry);
				size = string->l;
				break;
			case SCRIPT_VALUE_TYPE_GPIO:
				size = sizeof(struct script_bin_gpio_value);
				break;
			default:
				abort();
			}
			words += WORDS(size);
		}
86
87
		*sections += 1;
		*entries += c;
88
89
90
91
92
93
	}

	bin_size = sizeof(struct script_bin_head) +
		(*sections)*sizeof(struct script_bin_section) +
		(*entries)*sizeof(struct script_bin_entry) +
		words*sizeof(uint32_t);
94
95
96
	pr_debug("sections:%zu entries:%zu data:%zu/%zu -> %zu\n",
		 *sections, *entries, words, words*sizeof(uint32_t),
		 bin_size);
97
98
	return bin_size;
}
99

100
int script_generate_bin(void *bin, size_t bin_size,
Alejandro Mery's avatar
Alejandro Mery committed
101
102
			struct script *script,
			size_t sections, size_t entries)
103
{
104
105
106
107
108
109
110
111
112
113
114
115
	struct script_bin_head *head;
	struct script_bin_section *section;
	struct script_bin_entry *entry;
	void *data;

	struct list_entry *ls, *le;

	head = bin;
	section = head->section;
	entry = (void*)section+sections*sizeof(*section);
	data = (void*)entry+entries*sizeof(*entry);

116
117
118
119
120
121
122
	pr_debug("head....:%p\n", head);
	pr_debug("section.:%p (offset:%zu, each:%zu)\n", section,
		 (void*)section-bin, sizeof(*section));
	pr_debug("entry...:%p (offset:%zu, each:%zu)\n", entry,
		 (void*)entry-bin, sizeof(*entry));
	pr_debug("data....:%p (offset:%zu)\n", data,
		 (void*)data-bin);
123
124

	head->sections = sections;
125
126
127
	head->filesize = bin_size;
	head->version[0] = 1;
	head->version[1] = 2;
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
	for (ls = list_first(&script->sections); ls;
	     ls = list_next(&script->sections, ls)) {
		struct script_section *s;
		size_t c = 0;
		s = container_of(ls, struct script_section, sections);

		memcpy(section->name, s->name, strlen(s->name));
		section->offset = ((void*)entry-bin)>>2;

		for (le = list_first(&s->entries); le;
		     le = list_next(&s->entries, le)) {
			struct script_entry *e;
			e = container_of(le, struct script_entry, entries);
			size_t size = 0;

			memcpy(entry->name, e->name, strlen(e->name));
			entry->offset = ((void*)data-bin)>>2;
			entry->pattern = (e->type<<16);

			switch(e->type) {
			case SCRIPT_VALUE_TYPE_SINGLE_WORD: {
				struct script_single_entry *single;
				int32_t *bdata = data;
				single = container_of(e, struct script_single_entry, entry);

				*bdata = single->value;
				size = sizeof(*bdata);
				}; break;
			case SCRIPT_VALUE_TYPE_STRING: {
				struct script_string_entry *string;
				string = container_of(e, struct script_string_entry, entry);
				size = string->l;
				memcpy(data, string->string, size);
				/* align */
				size += sizeof(uint32_t)-1;
				size /= sizeof(uint32_t);
				size *= sizeof(uint32_t);
				}; break;
			case SCRIPT_VALUE_TYPE_MULTI_WORD:
				abort();
			case SCRIPT_VALUE_TYPE_GPIO: {
				struct script_gpio_entry *gpio;
				struct script_bin_gpio_value *bdata = data;
				gpio = container_of(e, struct script_gpio_entry, entry);
				bdata->port = gpio->port;
				bdata->port_num = gpio->port_num;
				bdata->mul_sel = gpio->data[0];
				bdata->pull = gpio->data[1];
				bdata->drv_level = gpio->data[2];
				bdata->data = gpio->data[3];
				size = sizeof(*bdata);
				}; break;
			case SCRIPT_VALUE_TYPE_NULL:
				size = sizeof(uint32_t);
				break;
			}

			data += size;
			entry->pattern |= (size>>2);
188
189
190
191
192
			pr_debug("%s.%s <%p> (type:%d, words:%d (%zu), offset:%d)\n",
				 section->name, entry->name, entry,
				 (entry->pattern>>16) & 0xffff,
				 (entry->pattern>>0) & 0xffff, size,
				 entry->offset);
193
194
195
196
197
			c++;
			entry++;
		}

		section->length = c;
198
199
		pr_debug("%s <%p> (length:%d, offset:%d)\n",
			 section->name, section, section->length, section->offset);
200
201
202
203

		section++;
	}
	return 1;
204
}
205

206
207
208
/*
 * decompiler
 */
209
static int decompile_section(void *bin, size_t bin_size,
210
211
212
			     const char *filename,
			     struct script_bin_section *section,
			     struct script *script)
213
{
214
	struct script_bin_entry *entry;
215
	struct script_section *s;
216
217
218
219
220
221
222
223
224
225
	int size;

	if ((section->offset < 0) || (section->offset > (int)(bin_size / 4))) {
		pr_err("Malformed data: invalid section offset: %d\n",
		       section->offset);
		return 0;
	}

	size = bin_size - 4 * section->offset;

226
	if ((section->length < 0) ||
227
228
229
230
231
	    (section->length > (size / (int)sizeof(struct script_bin_entry)))) {
		pr_err("Malformed data: invalid section length: %d\n",
		       section->length);
		return 0;
	}
232
233
234
235

	if ((s = script_section_new(script, section->name)) == NULL)
		goto malloc_error;

236
237
	entry = PTR(bin, section->offset<<2);

238
239
240
241
242
243
	for (int i = section->length; i--; entry++) {
		void *data = PTR(bin, entry->offset<<2);
		unsigned type, words;
		type	= (entry->pattern >> 16) & 0xffff;
		words	= (entry->pattern >>  0) & 0xffff;

244
245
246
247
248
249
250
		for (char *p = entry->name; *p; p++)
			if (!(isalnum(*p) || *p == '_')) {
				pr_info("Warning: Malformed entry key \"%s\"\n",
					entry->name);
				break;
			}

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
		switch(type) {
		case SCRIPT_VALUE_TYPE_SINGLE_WORD: {
			uint32_t *v = data;
			if (words != 1) {
				pr_err("%s: %s.%s: invalid length %d (assuming %d)\n",
				filename, section->name, entry->name, words, 1);
			}
			if (!script_single_entry_new(s, entry->name, *v))
				goto malloc_error;
			}; break;
		case SCRIPT_VALUE_TYPE_STRING: {
			size_t bytes = words << 2;
			const char *p, *pe, *v = data;

			for(p=v, pe=v+bytes; *p && p!=pe; p++)
				; /* seek end-of-string */

			if (!script_string_entry_new(s, entry->name, p-v, v))
				goto malloc_error;
			}; break;
		case SCRIPT_VALUE_TYPE_GPIO: {
			struct script_bin_gpio_value *gpio = data;
			int32_t v[4];
			if (words != 6) {
				pr_err("%s: %s.%s: invalid length %d (assuming %d)\n",
				       filename, section->name, entry->name, words, 6);
Alejandro Mery's avatar
Alejandro Mery committed
277
278
			} else if (gpio->port == 0xffff) {
				; /* port:power */
279
			} else if (gpio->port < 1 || gpio->port > GPIO_BANK_MAX) {
280
281
282
283
284
285
				pr_err("%s: %s.%s: unknown GPIO port bank ",
				       filename, section->name, entry->name);
				char c = 'A' + gpio->port - 1;
				if (c >= 'A' && c <= 'Z')
					pr_err("%c ", c);
				pr_err("(%u)\n", gpio->port);
286
287
288
289
290
291
292
293
294
295
296
297
298
				goto failure;
			}
			v[0] = gpio->mul_sel;
			v[1] = gpio->pull;
			v[2] = gpio->drv_level;
			v[3] = gpio->data;

			if (!script_gpio_entry_new(s, entry->name,
						   gpio->port, gpio->port_num,
						   v))
				goto malloc_error;
			}; break;
		case SCRIPT_VALUE_TYPE_NULL:
299
300
301
			if (!*entry->name) {
				pr_err("%s: empty entry in section: %s\n", filename, section->name);
			} else if (!script_null_entry_new(s, entry->name)) {
302
				goto malloc_error;
303
			}
304
305
306
307
308
309
310
311
312
313
314
315
			break;
		default:
			pr_err("%s: %s.%s: unknown type %d\n",
			       filename, section->name, entry->name, type);
			goto failure;
		}
	}
	return 1;

malloc_error:
	pr_err("%s: %s\n", "malloc", strerror(errno));
failure:
316
317
	return 0;
}
318

319
320
321
#define SCRIPT_BIN_VERSION_LIMIT 0x10
#define SCRIPT_BIN_SECTION_LIMIT 0x100

322
323
324
325
int script_decompile_bin(void *bin, size_t bin_size,
			 const char *filename,
			 struct script *script)
{
326
	unsigned int i;
327
328
	struct script_bin_head *head = bin;

329
330
331
332
	if ((head->version[0] > SCRIPT_BIN_VERSION_LIMIT) ||
	    (head->version[1] > SCRIPT_BIN_VERSION_LIMIT)) {
		pr_err("Malformed data: version %u.%u.\n",
		       head->version[0], head->version[1]);
333
334
335
		return 0;
	}

336
337
338
339
340
341
	if (head->sections > SCRIPT_BIN_SECTION_LIMIT) {
		pr_err("Malformed data: too many sections (%u).\n",
		       head->sections);
		return 0;
	}

342
343
344
345
	pr_info("%s: version: %u.%u\n", filename,
		head->version[0], head->version[1]);
	pr_info("%s: size: %zu (%u sections), header value: %u\n", filename,
		bin_size, head->sections, head->filesize);
346

347
348
349
350
351
352
353
354
355
356
	/* TODO: SANITY: compare head.sections with bin_size */
	for (i=0; i < head->sections; i++) {
		struct script_bin_section *section = &head->section[i];

		if (!decompile_section(bin, bin_size, filename,
				       section, script))
			return 0;
	}
	return 1;
}