bin2fex.c 6.18 KB
Newer Older
Alejandro Mery's avatar
Alejandro Mery committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (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/>.
 */
#include "sunxi-tools.h"
Alejandro Mery's avatar
Alejandro Mery committed
18
#include "bin2fex.h"
Alejandro Mery's avatar
Alejandro Mery committed
19

20
21
22
23
24
25
26
27
28
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>

29
30
31
32
33
#define pr_info(F, ...)	do { \
	fprintf(stderr, "bin2fex: " F, __VA_ARGS__); \
	fprintf(out, "; bin2fex: " F, __VA_ARGS__); \
	} while(0)

34
#define pr_err(F, ...)	pr_info("ERROR: " F, __VA_ARGS__)
35

Alejandro Mery's avatar
Alejandro Mery committed
36
37
#define PTR(B, OFF)	(void*)((char*)(B)+(OFF))

38
39
/**
 */
40
static inline size_t strlen2(const char *s)
41
42
{
	size_t l = strlen(s);
43
44
45
46
	const char *p = &s[l-1];
	while (l && *p >= '0' && *p <= '9') {
		l--;
		p--;
47
	}
48
49
	return l;
}
50

51
52
static int find_full_match(const char *s, size_t l, const char **list)
{
53
54
55
56
57
58
59
60
61
	while (*list) {
		if (memcmp(s, *list, l) == 0)
			return 1;
		list++;
	}

	return 0;
}

62
63
/**
 */
64
65
66
static inline int decompile_gpio(struct script_bin_section *section,
				 struct script_bin_entry *entry,
				 struct script_bin_gpio_value *gpio,
67
				 int length, FILE *out)
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
{
	int ok = 1;
	char port = '?';

	if (length != 6) {
		pr_err("%s.%s: invalid length %d (assuming %d)\n",
		       section->name, entry->name, length, 6);
		ok = 0;
	}

	if (gpio->port < 1 || gpio->port > 10) {
		pr_err("%s.%s: unknown GPIO port type %d\n",
		       section->name, entry->name, gpio->port);
		ok = 0;
	} else {
		port = 'A' + (gpio->port-1);
	}

86
	fprintf(out, "%s\t= port:P%c%02d", entry->name, port, gpio->port_num);
87
88
89
90
91
92
93
94
95
96
97
	for (const int *p = &gpio->mul_sel, *pe = p+4; p != pe; p++) {
		if (*p == -1)
			fputs("<default>", out);
		else
			fprintf(out, "<%d>", *p);
	}
	fputc('\n', out);

	return ok;
}

98
99
/**
 */
100
static int decompile_single_mode(const char *name)
101
{
102
103
104
105
106
107
108
109
	static const char *hexa_entries[] = {
		"dram_baseaddr", "dram_zq", "dram_tpr", "dram_emr",
		"g2d_size",
		"rtp_press_threshold", "rtp_sensitive_level",
		"ctp_twi_addr", "csi_twi_addr", "csi_twi_addr_b", "tkey_twi_addr",
		"lcd_gamma_tbl_",
		"gsensor_twi_addr",
		NULL };
110
111
112
113
114
115
116
	size_t l = strlen2(name);

	if (find_full_match(name, l, hexa_entries))
		return 0;
	else
		return -1;
}
117
118
static inline int decompile_single(struct script_bin_section *section,
				   struct script_bin_entry *entry,
119
120
121
122
123
				   int32_t *d,
				   int length, FILE *out)
{
	int ok = 1;
	int mode;
124
125
126
127
128
129
130

	if (length != 1) {
		pr_err("%s.%s: invalid length %d (assuming %d)\n",
		section->name, entry->name, length, 1);
		ok = 0;
	}

131
132
	mode = decompile_single_mode(entry->name);

133
	fprintf(out, "%s\t= ", entry->name);
134
	if (mode < 0)
135
		fprintf(out, "%d", *d);
136
137
138
139
140
	else if (mode > 0)
		fprintf(out, "0x%0*x", mode, *d);
	else
		fprintf(out, "0x%x", *d);

141
	fputc('\n', out);
142
143
144
145

	return ok;
}

Alejandro Mery's avatar
Alejandro Mery committed
146
147
/**
 */
148
static int decompile_section(void *bin, size_t UNUSED(bin_size),
149
			     struct script_bin_section *section,
150
			     FILE *out)
Alejandro Mery's avatar
Alejandro Mery committed
151
{
152
	struct script_bin_entry *entry = PTR(bin,  section->offset<<2);
153
	int ok = 1;
154
155

	fprintf(out, "[%s]\n", section->name);
Alejandro Mery's avatar
Alejandro Mery committed
156
	for (int i = section->length; i--; entry++) {
157
		void *data = PTR(bin, entry->offset<<2);
158
159
160
161
		unsigned type, length;
		type	= (entry->pattern >> 16) & 0xffff;
		length	= (entry->pattern >>  0) & 0xffff;

162
		switch(type) {
163
164
		case SCRIPT_VALUE_TYPE_SINGLE_WORD:
			if (!decompile_single(section, entry, data, length, out))
165
				ok = 0;
166
			break;
167
168
169
170
171
172
173
174
175
176
177
		case SCRIPT_VALUE_TYPE_STRING: {
			size_t bytes = length << 2;
			const char *p, *pe, *s = data;

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

			fprintf(out, "%s\t= \"%.*s\"\n", entry->name,
				(int)(p-s), s);
			}; break;
		case SCRIPT_VALUE_TYPE_GPIO:
178
179
180
			if (!decompile_gpio(section, entry, data, length, out))
			    ok = 0;
			break;
181
182
183
184
185
186
187
		case SCRIPT_VALUE_TYPE_NULL:
			fprintf(out, "%s\t=\n", entry->name);
			break;
		default:
			pr_err("%s.%s: unknown type %d\n",
			       section->name, entry->name, type);
			fprintf(out, "%s\t=\n", entry->name);
188
			ok = 0;
189
190
			break;
		}
Alejandro Mery's avatar
Alejandro Mery committed
191
	}
192
	fputc('\n', out);
Alejandro Mery's avatar
Alejandro Mery committed
193

194
	return ok;
Alejandro Mery's avatar
Alejandro Mery committed
195
}
196
197
/**
 */
198
static int decompile(void *bin, size_t bin_size, FILE *out)
Alejandro Mery's avatar
Alejandro Mery committed
199
{
Alejandro Mery's avatar
Alejandro Mery committed
200
	int i;
201
	struct script_bin_head *script = bin;
Alejandro Mery's avatar
Alejandro Mery committed
202

203
204
	pr_info("version: %d.%d.%d\n", script->version[0],
		script->version[1], script->version[2]);
Alejandro Mery's avatar
Alejandro Mery committed
205
	pr_info("size: %zu (%d sections)\n", bin_size,
206
		script->sections);
Alejandro Mery's avatar
Alejandro Mery committed
207
208

	/* TODO: SANITY: compare head.sections with bin_size */
209
210
	for (i=0; i < script->sections; i++) {
		struct script_bin_section *section = &script->section[i];
Alejandro Mery's avatar
Alejandro Mery committed
211

212
		if (!decompile_section(bin, bin_size, section, out))
Alejandro Mery's avatar
Alejandro Mery committed
213
214
215
216
217
			return 1; /* failure */
	}
	return 0; /* success */
}

218
219
220
221
222
223
/**
 */
int main(int argc, char *argv[])
{
	struct stat sb;
	int ret = -1;
224
225
226
	int in = 0;
	FILE *out = stdout;
	const char *filename[] = {"stdin", "stdout"};
227
228
229
230
231
232
	void *p;

	/* open */
	if (argc>1) {
		filename[0] = argv[1];

233
		if ((in = open(filename[0], O_RDONLY)) < 0) {
234
235
236
237
238
239
			errf("%s: %s\n", filename[0], strerror(errno));
			goto usage;
		}
		if (argc > 2) {
			filename[1] = argv[2];

240
			if ((out = fopen(filename[1], "w")) == NULL) {
241
242
243
244
245
246
247
				errf("%s: %s\n", filename[1], strerror(errno));
				goto usage;
			}
		}
	}

	/* mmap input */
248
	if (fstat(in, &sb) == -1)
249
250
		errf("fstat: %s: %s\n", filename[0], strerror(errno));
	else if (!S_ISREG(sb.st_mode))
251
		errf("%s: not a regular file (mode:%d).\n", filename[0], sb.st_mode);
252
	else if ((p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, in, 0)) == MAP_FAILED)
253
254
		errf("mmap: %s: %s\n", filename[0], strerror(errno));
	else {
255
256
		/* close and decompile mmap */
		close(in);
257

258
		ret = decompile(p, sb.st_size, out);
259
260
261
262
263
264
265
266
267
		if (munmap(p, sb.st_size) == -1)
			errf("munmap: %s: %s\n", filename[0], strerror(errno));

		goto done;
	}

usage:
	errf("Usage: %s [<script.bin> [<script.fex>]]\n", argv[0]);

268
	if (in > 2) close(in);
269
done:
270
	if (out && out != stdout) fclose(out);
271
272
	return ret;
}