bin2fex.c 5.27 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
29
#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>

#define errf(...)	fprintf(stderr, __VA_ARGS__)
30
#define pr_info(F, ...)	fprintf(out, "; bin2fex: " F, __VA_ARGS__)
31
#define pr_err(F, ...)	pr_info("ERROR: " F, __VA_ARGS__)
32

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

35
36
/**
 */
37
38
39
40
static inline int decompile_gpio(struct script_section *section,
				 struct script_section_entry *entry,
				 struct script_gpio_value *gpio,
				 int length, FILE *out)
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
{
	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);
	}

	fprintf(out, "%s\t= port:P%c%d", entry->name, port, gpio->port_num);
	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;
}

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 */
static inline int decompile_single(struct script_section *section,
				   struct script_section_entry *entry,
				   int32_t *d,
				   int length, FILE *out)
{
	int ok = 1;

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

	fprintf(out, "%s\t= %d\n", entry->name, *d);

	return ok;
}

Alejandro Mery's avatar
Alejandro Mery committed
91
92
/**
 */
Alejandro Mery's avatar
Alejandro Mery committed
93
94
static int decompile_section(void *bin, size_t bin_size,
			     struct script_section *section,
95
			     FILE *out)
Alejandro Mery's avatar
Alejandro Mery committed
96
{
97
98
	struct script_section_entry *entry = PTR(bin,  section->offset<<2);
	int i = section->length;
99
	int ok = 1;
100
101

	fprintf(out, "[%s]\n", section->name);
102
	for (; i--; entry++) {
103
		void *data = PTR(bin, entry->offset<<2);
104
105
106
107
		unsigned type, length;
		type	= (entry->pattern >> 16) & 0xffff;
		length	= (entry->pattern >>  0) & 0xffff;

108
		switch(type) {
109
110
		case SCRIPT_VALUE_TYPE_SINGLE_WORD:
			if (!decompile_single(section, entry, data, length, out))
111
				ok = 0;
112
			break;
113
114
115
116
117
118
119
120
121
122
123
		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:
124
125
126
			if (!decompile_gpio(section, entry, data, length, out))
			    ok = 0;
			break;
127
128
129
130
131
132
133
		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);
134
			ok = 0;
135
136
			break;
		}
Alejandro Mery's avatar
Alejandro Mery committed
137
	}
138
	fputc('\n', out);
Alejandro Mery's avatar
Alejandro Mery committed
139

140
	return ok;
Alejandro Mery's avatar
Alejandro Mery committed
141
}
142
143
/**
 */
144
static int decompile(void *bin, size_t bin_size, FILE *out)
Alejandro Mery's avatar
Alejandro Mery committed
145
{
Alejandro Mery's avatar
Alejandro Mery committed
146
147
148
149
150
151
	int i;
	struct {
		struct script_head head;
		struct script_section sections[];
	} *script = bin;

Alejandro Mery's avatar
Alejandro Mery committed
152
153
154
155
	pr_info("version: %d.%d.%d\n", script->head.version[0],
		script->head.version[1], script->head.version[2]);
	pr_info("size: %zu (%d sections)\n", bin_size,
		script->head.sections);
Alejandro Mery's avatar
Alejandro Mery committed
156
157
158
159
160

	/* TODO: SANITY: compare head.sections with bin_size */
	for (i=0; i < script->head.sections; i++) {
		struct script_section *section = &script->sections[i];

161
		if (!decompile_section(bin, bin_size, section, out))
Alejandro Mery's avatar
Alejandro Mery committed
162
163
164
165
166
			return 1; /* failure */
	}
	return 0; /* success */
}

167
168
169
170
171
172
/**
 */
int main(int argc, char *argv[])
{
	struct stat sb;
	int ret = -1;
173
174
175
	int in = 0;
	FILE *out = stdout;
	const char *filename[] = {"stdin", "stdout"};
176
177
178
179
180
181
	void *p;

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

182
		if ((in = open(filename[0], O_RDONLY)) < 0) {
183
184
185
186
187
188
			errf("%s: %s\n", filename[0], strerror(errno));
			goto usage;
		}
		if (argc > 2) {
			filename[1] = argv[2];

189
			if ((out = fopen(filename[1], "w")) == NULL) {
190
191
192
193
194
195
196
				errf("%s: %s\n", filename[1], strerror(errno));
				goto usage;
			}
		}
	}

	/* mmap input */
197
	if (fstat(in, &sb) == -1)
198
199
		errf("fstat: %s: %s\n", filename[0], strerror(errno));
	else if (!S_ISREG(sb.st_mode))
200
		errf("%s: not a regular file (mode:%d).\n", filename[0], sb.st_mode);
201
	else if ((p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, in, 0)) == MAP_FAILED)
202
203
		errf("mmap: %s: %s\n", filename[0], strerror(errno));
	else {
204
205
		/* close and decompile mmap */
		close(in);
206

207
		ret = decompile(p, sb.st_size, out);
208
209
210
211
212
213
214
215
216
		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]);

217
	if (in > 2) close(in);
218
done:
219
	if (out != stdout) fclose(out);
220
221
	return ret;
}