bin2fex.c 3.46 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__)
Alejandro Mery's avatar
Alejandro Mery committed
30
#define pr_info(F, ...)	fprintf(stderr, "bin2fex: " F, __VA_ARGS__)
31

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

/**
 */
Alejandro Mery's avatar
Alejandro Mery committed
36
37
static int decompile_section(void *bin, size_t bin_size,
			     struct script_section *section,
38
			     FILE *out)
Alejandro Mery's avatar
Alejandro Mery committed
39
{
40
41
42
43
44
45
46
47
48
49
	struct script_section_entry *entry = PTR(bin,  section->offset<<2);
	int i = section->length;
	for (; i--; entry++) {
		unsigned type, length;
		type	= (entry->pattern >> 16) & 0xffff;
		length	= (entry->pattern >>  0) & 0xffff;

		pr_info("%s.%s\t(offset:%d, type:%d, length:%d)\n",
			section->name, entry->name,
			entry->offset, type, length);
Alejandro Mery's avatar
Alejandro Mery committed
50
51
52
53
	}

	return 1; /* success */
}
54
55
/**
 */
56
static int decompile(void *bin, size_t bin_size, FILE *out)
Alejandro Mery's avatar
Alejandro Mery committed
57
{
Alejandro Mery's avatar
Alejandro Mery committed
58
59
60
61
62
63
	int i;
	struct {
		struct script_head head;
		struct script_section sections[];
	} *script = bin;

Alejandro Mery's avatar
Alejandro Mery committed
64
65
66
67
	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
68
69
70
71
72

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

Alejandro Mery's avatar
Alejandro Mery committed
73
74
		pr_info("%s:\t(section:%d, length:%d, offset:%d)\n",
			section->name, i+1, section->length, section->offset);
Alejandro Mery's avatar
Alejandro Mery committed
75

76
		if (!decompile_section(bin, bin_size, section, out))
Alejandro Mery's avatar
Alejandro Mery committed
77
78
79
80
81
			return 1; /* failure */
	}
	return 0; /* success */
}

82
83
84
85
86
87
/**
 */
int main(int argc, char *argv[])
{
	struct stat sb;
	int ret = -1;
88
89
90
	int in = 0;
	FILE *out = stdout;
	const char *filename[] = {"stdin", "stdout"};
91
92
93
94
95
96
	void *p;

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

97
		if ((in = open(filename[0], O_RDONLY)) < 0) {
98
99
100
101
102
103
			errf("%s: %s\n", filename[0], strerror(errno));
			goto usage;
		}
		if (argc > 2) {
			filename[1] = argv[2];

104
			if ((out = fopen(filename[1], "w")) == NULL) {
105
106
107
108
109
110
111
				errf("%s: %s\n", filename[1], strerror(errno));
				goto usage;
			}
		}
	}

	/* mmap input */
112
	if (fstat(in, &sb) == -1)
113
114
		errf("fstat: %s: %s\n", filename[0], strerror(errno));
	else if (!S_ISREG(sb.st_mode))
115
		errf("%s: not a regular file (mode:%d).\n", filename[0], sb.st_mode);
116
	else if ((p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, in, 0)) == MAP_FAILED)
117
118
		errf("mmap: %s: %s\n", filename[0], strerror(errno));
	else {
119
120
		/* close and decompile mmap */
		close(in);
121

122
		ret = decompile(p, sb.st_size, out);
123
124
125
126
127
128
129
130
131
		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]);

132
	if (in > 2) close(in);
133
done:
134
	if (out != stdout) fclose(out);
135
136
	return ret;
}