bin2fex.c 4.88 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
/*
 * 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/>.
 */
17
18
19
20
21
22
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/mman.h>

23
24
25
26
27
#define pr_info(F, ...)	do { \
	fprintf(stderr, "bin2fex: " F, __VA_ARGS__); \
	fprintf(out, "; bin2fex: " F, __VA_ARGS__); \
	} while(0)

28
#define pr_err(F, ...)	pr_info("ERROR: " F, __VA_ARGS__)
29

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

32
33
/**
 */
34
static inline size_t strlen2(const char *s)
35
36
{
	size_t l = strlen(s);
37
38
39
40
	const char *p = &s[l-1];
	while (l && *p >= '0' && *p <= '9') {
		l--;
		p--;
41
	}
42
43
	return l;
}
44

45
46
static int find_full_match(const char *s, size_t l, const char **list)
{
47
48
49
50
51
52
53
54
55
	while (*list) {
		if (memcmp(s, *list, l) == 0)
			return 1;
		list++;
	}

	return 0;
}

56
57
/**
 */
58
59
60
static inline int decompile_gpio(struct script_bin_section *section,
				 struct script_bin_entry *entry,
				 struct script_bin_gpio_value *gpio,
61
				 int words, FILE *out)
62
63
64
65
{
	int ok = 1;
	char port = '?';

66
	if (words != 6) {
67
		pr_err("%s.%s: invalid length %d (assuming %d)\n",
68
		       section->name, entry->name, words, 6);
69
70
71
72
73
74
75
76
77
78
79
		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);
	}

80
	fprintf(out, "%s\t= port:P%c%02d", entry->name, port, gpio->port_num);
81
82
83
84
85
86
87
88
89
90
91
	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;
}

92
93
/**
 */
94
static int decompile_single_mode(const char *name)
95
{
96
97
98
99
100
101
102
103
	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 };
104
105
106
107
108
109
110
	size_t l = strlen2(name);

	if (find_full_match(name, l, hexa_entries))
		return 0;
	else
		return -1;
}
111
112
static inline int decompile_single(struct script_bin_section *section,
				   struct script_bin_entry *entry,
113
				   int32_t *d,
114
				   int words, FILE *out)
115
116
117
{
	int ok = 1;
	int mode;
118

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

125
126
	mode = decompile_single_mode(entry->name);

127
	fprintf(out, "%s\t= ", entry->name);
128
	if (mode < 0)
129
		fprintf(out, "%d", *d);
130
131
132
133
134
	else if (mode > 0)
		fprintf(out, "0x%0*x", mode, *d);
	else
		fprintf(out, "0x%x", *d);

135
	fputc('\n', out);
136
137
138
139

	return ok;
}

Alejandro Mery's avatar
Alejandro Mery committed
140
141
/**
 */
142
static int decompile_section(void *bin, size_t UNUSED(bin_size),
143
			     struct script_bin_section *section,
144
			     FILE *out)
Alejandro Mery's avatar
Alejandro Mery committed
145
{
146
	struct script_bin_entry *entry = PTR(bin,  section->offset<<2);
147
	int ok = 1;
148
149

	fprintf(out, "[%s]\n", section->name);
Alejandro Mery's avatar
Alejandro Mery committed
150
	for (int i = section->length; i--; entry++) {
151
		void *data = PTR(bin, entry->offset<<2);
152
		unsigned type, words;
153
		type	= (entry->pattern >> 16) & 0xffff;
154
		words	= (entry->pattern >>  0) & 0xffff;
155

156
		switch(type) {
157
		case SCRIPT_VALUE_TYPE_SINGLE_WORD:
158
			if (!decompile_single(section, entry, data, words, out))
159
				ok = 0;
160
			break;
161
		case SCRIPT_VALUE_TYPE_STRING: {
162
			size_t bytes = words << 2;
163
164
165
166
167
168
169
170
171
			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:
172
			if (!decompile_gpio(section, entry, data, words, out))
173
174
			    ok = 0;
			break;
175
176
177
178
179
180
181
		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);
182
			ok = 0;
183
184
			break;
		}
Alejandro Mery's avatar
Alejandro Mery committed
185
	}
186
	fputc('\n', out);
Alejandro Mery's avatar
Alejandro Mery committed
187

188
	return ok;
Alejandro Mery's avatar
Alejandro Mery committed
189
}
190
191
/**
 */
192
static int decompile(void *bin, size_t bin_size, FILE *out)
Alejandro Mery's avatar
Alejandro Mery committed
193
{
Alejandro Mery's avatar
Alejandro Mery committed
194
	int i;
195
	struct script_bin_head *script = bin;
Alejandro Mery's avatar
Alejandro Mery committed
196

197
198
	pr_info("version: %d.%d.%d\n", script->version[0],
		script->version[1], script->version[2]);
Alejandro Mery's avatar
Alejandro Mery committed
199
	pr_info("size: %zu (%d sections)\n", bin_size,
200
		script->sections);
Alejandro Mery's avatar
Alejandro Mery committed
201
202

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

206
		if (!decompile_section(bin, bin_size, section, out))
Alejandro Mery's avatar
Alejandro Mery committed
207
208
209
210
			return 1; /* failure */
	}
	return 0; /* success */
}