Commit 3ad61094 authored by Alejandro Mery's avatar Alejandro Mery
Browse files

bin2fex: decompile integer, string and NULL entries. also reduces stderr noise

parent fc036082
...@@ -27,7 +27,8 @@ ...@@ -27,7 +27,8 @@
#include <fcntl.h> #include <fcntl.h>
#define errf(...) fprintf(stderr, __VA_ARGS__) #define errf(...) fprintf(stderr, __VA_ARGS__)
#define pr_info(F, ...) fprintf(stderr, "bin2fex: " F, __VA_ARGS__) #define pr_info(F, ...) fprintf(out, "; bin2fex: " F, __VA_ARGS__)
#define pr_err(F, ...) pr_info("ERROR" F, __VA_ARGS__)
#define PTR(B, OFF) (void*)((char*)(B)+(OFF)) #define PTR(B, OFF) (void*)((char*)(B)+(OFF))
...@@ -39,15 +40,47 @@ static int decompile_section(void *bin, size_t bin_size, ...@@ -39,15 +40,47 @@ static int decompile_section(void *bin, size_t bin_size,
{ {
struct script_section_entry *entry = PTR(bin, section->offset<<2); struct script_section_entry *entry = PTR(bin, section->offset<<2);
int i = section->length; int i = section->length;
fprintf(out, "[%s]\n", section->name);
for (; i--; entry++) { for (; i--; entry++) {
void *data = PTR(bin, entry->offset<<2);
unsigned type, length; unsigned type, length;
type = (entry->pattern >> 16) & 0xffff; type = (entry->pattern >> 16) & 0xffff;
length = (entry->pattern >> 0) & 0xffff; length = (entry->pattern >> 0) & 0xffff;
pr_info("%s.%s\t(offset:%d, type:%d, length:%d)\n", switch(type) {
section->name, entry->name, case SCRIPT_VALUE_TYPE_SINGLE_WORD: {
entry->offset, type, length); int32_t *d = data;
if (length != 1)
pr_err("%s.%s: invalid length %d (assuming 1)\n",
section->name, entry->name, length);
/* TODO: some are preferred in hexa */
fprintf(out, "%s\t= %d\n", entry->name, *d);
}; break;
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:
fprintf(out, "%s\t= GPIO\n", entry->name); break;
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);
break;
}
} }
fputc('\n', out);
return 1; /* success */ return 1; /* success */
} }
...@@ -70,9 +103,6 @@ static int decompile(void *bin, size_t bin_size, FILE *out) ...@@ -70,9 +103,6 @@ static int decompile(void *bin, size_t bin_size, FILE *out)
for (i=0; i < script->head.sections; i++) { for (i=0; i < script->head.sections; i++) {
struct script_section *section = &script->sections[i]; struct script_section *section = &script->sections[i];
pr_info("%s:\t(section:%d, length:%d, offset:%d)\n",
section->name, i+1, section->length, section->offset);
if (!decompile_section(bin, bin_size, section, out)) if (!decompile_section(bin, bin_size, section, out))
return 1; /* failure */ return 1; /* failure */
} }
......
...@@ -36,4 +36,11 @@ struct script_section_entry { ...@@ -36,4 +36,11 @@ struct script_section_entry {
int32_t pattern; int32_t pattern;
}; };
enum script_value_type {
SCRIPT_VALUE_TYPE_SINGLE_WORD = 1,
SCRIPT_VALUE_TYPE_STRING,
SCRIPT_VALUE_TYPE_MULTI_WORD,
SCRIPT_VALUE_TYPE_GPIO,
SCRIPT_VALUE_TYPE_NULL,
};
#endif #endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment