bin2fex.c 3.3 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
30
#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
31
32
33
34
static int decompile(void *bin, size_t bin_size, int out, const char *out_name);
static int decompile_section(void *bin, size_t bin_size,
			     struct script_section *section,
			     int out, const char* out_named);
35
36
/**
 */
Alejandro Mery's avatar
Alejandro Mery committed
37
static int decompile(void *bin, size_t bin_size, int out, const char *out_name)
Alejandro Mery's avatar
Alejandro Mery committed
38
{
Alejandro Mery's avatar
Alejandro Mery committed
39
40
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
	int i;
	struct {
		struct script_head head;
		struct script_section sections[];
	} *script = bin;

	errf("bin format: %d.%d.%d\n", script->head.version[0],
	     script->head.version[1], script->head.version[2]);
	errf("bin size: %zu (%d sections)\n", bin_size,
	     script->head.sections);

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

		errf("%s: (section:%d, length:%d, offset:%d)\n",
		     section->name, i+1, section->length, section->offset);

		if (!decompile_section(bin, bin_size, section, out, out_name))
			return 1; /* failure */
	}
	return 0; /* success */
}

/**
 */
static int decompile_section(void *bin, size_t bin_size,
			     struct script_section *section,
			     int out, const char* out_named)
{
	return 1; /* success */
Alejandro Mery's avatar
Alejandro Mery committed
70
}
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

/**
 */
int main(int argc, char *argv[])
{
	struct stat sb;
	int ret = -1;
	int fd[] = {0, 1};
	const char *filename[] = { "stdin", "stdout" };
	void *p;

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

		if ((fd[0] = open(filename[0], O_RDONLY)) < 0) {
			errf("%s: %s\n", filename[0], strerror(errno));
			goto usage;
		}
		if (argc > 2) {
			filename[1] = argv[2];

			if ((fd[1] = open(filename[1], O_WRONLY|O_CREAT, 0666)) < 0) {
				errf("%s: %s\n", filename[1], strerror(errno));
				goto usage;
			}
		}
	}

	/* mmap input */
	if (fstat(fd[0], &sb) == -1)
		errf("fstat: %s: %s\n", filename[0], strerror(errno));
	else if (!S_ISREG(sb.st_mode))
104
		errf("%s: not a regular file (mode:%d).\n", filename[0], sb.st_mode);
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
	else if ((p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, fd[0], 0)) == MAP_FAILED)
		errf("mmap: %s: %s\n", filename[0], strerror(errno));
	else {
		/* decompile mmap */
		close(fd[0]);

		ret = decompile(p, sb.st_size, fd[1], filename[1]);
		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]);

	if (fd[0] > 2) close(fd[0]);
done:
	if (fd[1] > 2) close(fd[1]);
	return ret;
}