From 0b7bcc4aa29c59bd8bb3b3be7143c25ff716d506 Mon Sep 17 00:00:00 2001 From: Bernhard Nortmann Date: Mon, 16 May 2016 19:32:17 +0200 Subject: [PATCH 1/5] fexc: Don't fail silently on parse error A test case for this is "fex2bin a-star_kv49l.fex". This patch fixes an execution path in script_fex.c that would allow script_parse_fex() to fail (i.e. return 0) without providing any feedback to the user. Signed-off-by: Bernhard Nortmann --- script_fex.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/script_fex.c b/script_fex.c index 361e86b..0101ef0 100644 --- a/script_fex.c +++ b/script_fex.c @@ -344,6 +344,8 @@ int script_parse_fex(FILE *in, const char *filename, struct script *script) } else { goto invalid_char_at_p; } + errf("E: %s:%zu: parse error at %zu.\n", + filename, line, p-buffer+1); goto parse_error; invalid_char_at_p: errf("E: %s:%zu: invalid character at %zu.\n", From 6271d370afd2103ec5d2f70bd1315cbf0513a1cd Mon Sep 17 00:00:00 2001 From: Bernhard Nortmann Date: Wed, 25 May 2016 00:36:11 +0200 Subject: [PATCH 2/5] fexc: Fix thinko in script decompiler Both the error output in function decompile_section() and the definition of GPIO_BANK_MAX in script.h suffered from an "off by one" logic. The bank numbering in the .bin is based on 1, so it should be added to ('A' - 1) for human-readable output, and the maximum number corresponding to 'N' is 14. This became apparent when trying to translate a fex2bin-compiled a80_optimus_board.bin back to its original .fex equivalent. Signed-off-by: Bernhard Nortmann --- script.h | 2 +- script_bin.c | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/script.h b/script.h index b4c2aad..7df3151 100644 --- a/script.h +++ b/script.h @@ -19,7 +19,7 @@ #include "list.h" -#define GPIO_BANK_MAX 13 /* N */ +#define GPIO_BANK_MAX 14 /* N, (zero-based) index 13 */ /** head of the data tree */ struct script { diff --git a/script_bin.c b/script_bin.c index ffe9051..6a06684 100644 --- a/script_bin.c +++ b/script_bin.c @@ -269,9 +269,12 @@ static int decompile_section(void *bin, size_t bin_size, } else if (gpio->port == 0xffff) { ; /* port:power */ } else if (gpio->port < 1 || gpio->port > GPIO_BANK_MAX) { - pr_err("%s: %s.%s: unknown GPIO port bank %c (%u)\n", - filename, section->name, entry->name, - 'A'+gpio->port, gpio->port); + pr_err("%s: %s.%s: unknown GPIO port bank ", + filename, section->name, entry->name); + char c = 'A' + gpio->port - 1; + if (c >= 'A' && c <= 'Z') + pr_err("%c ", c); + pr_err("(%u)\n", gpio->port); goto failure; } v[0] = gpio->mul_sel; From a93b5e363664ae427a0462dc335518452cd36647 Mon Sep 17 00:00:00 2001 From: Bernhard Nortmann Date: Thu, 26 May 2016 00:02:01 +0200 Subject: [PATCH 3/5] fexc: More relaxed version data check in script_bin.c Recent original .bin files showed up with a version[0] of 49152 = 0xC000, which won't pass our current sanity checks. Restrict version[0] check to lower 14 bits, to make it pass again. (This might require further investigation in the future, as it's possible that this particular header field actually isn't related to version data at all.) Fixes #51. Signed-off-by: Bernhard Nortmann --- script_bin.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script_bin.c b/script_bin.c index 6a06684..2d9874f 100644 --- a/script_bin.c +++ b/script_bin.c @@ -318,7 +318,7 @@ int script_decompile_bin(void *bin, size_t bin_size, unsigned int i; struct script_bin_head *head = bin; - if ((head->version[0] > SCRIPT_BIN_VERSION_LIMIT) || + if (((head->version[0] & 0x3FFF) > SCRIPT_BIN_VERSION_LIMIT) || (head->version[1] > SCRIPT_BIN_VERSION_LIMIT) || (head->version[2] > SCRIPT_BIN_VERSION_LIMIT)) { pr_err("Malformed data: version %u.%u.%u.\n", @@ -333,7 +333,7 @@ int script_decompile_bin(void *bin, size_t bin_size, } pr_info("%s: version: %u.%u.%u\n", filename, - head->version[0], head->version[1], head->version[2]); + head->version[0] & 0x3FFF, head->version[1], head->version[2]); pr_info("%s: size: %zu (%u sections)\n", filename, bin_size, head->sections); From 80aae9268a05f6fe49d20ec8977f71ffe82ea0a2 Mon Sep 17 00:00:00 2001 From: Bernhard Nortmann Date: Thu, 26 May 2016 07:46:58 +0200 Subject: [PATCH 4/5] fexc: Warn when decompiling a malformed section entry (key string) script_bin.c decompiles section entries even if they have invalid indentifiers. Thus malformed .bin files were observed to result in a .fex that couldn't be processed successfully with the sunxi-fexc compiler. This patch makes bin2fex now issue a warning for this kind of keys, so that they're easier to notice and correct. Signed-off-by: Bernhard Nortmann --- script_bin.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/script_bin.c b/script_bin.c index 2d9874f..3315f67 100644 --- a/script_bin.c +++ b/script_bin.c @@ -17,6 +17,7 @@ #include "common.h" +#include #include #include #include @@ -240,6 +241,13 @@ static int decompile_section(void *bin, size_t bin_size, type = (entry->pattern >> 16) & 0xffff; words = (entry->pattern >> 0) & 0xffff; + for (char *p = entry->name; *p; p++) + if (!(isalnum(*p) || *p == '_')) { + pr_info("Warning: Malformed entry key \"%s\"\n", + entry->name); + break; + } + switch(type) { case SCRIPT_VALUE_TYPE_SINGLE_WORD: { uint32_t *v = data; From 7a0a7012c692315365ad40dd0ee4ac73021c4771 Mon Sep 17 00:00:00 2001 From: Bernhard Nortmann Date: Thu, 26 May 2016 08:20:19 +0200 Subject: [PATCH 5/5] fexc: Ignore lines starting with ':' when compiling .fex Such lines do not conform to any known syntax rules. With this patch, fexc will assume that they represent a special case (where bin2fex extracted a malformed indentifier from a .bin file - likely a remnant from a comment typo), issue a warning and ignore the line. Signed-off-by: Bernhard Nortmann --- script_fex.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/script_fex.c b/script_fex.c index 0101ef0..2975910 100644 --- a/script_fex.c +++ b/script_fex.c @@ -202,7 +202,13 @@ int script_parse_fex(FILE *in, const char *filename, struct script *script) if (pe == s || *s == ';' || *s == '#') continue; /* empty */ - else if (*s == '[') { + if (*s == ':') { + /* see https://github.com/linux-sunxi/sunxi-boards/issues/50 */ + errf("Warning: %s:%zu: invalid line, suspecting typo/malformed comment.\n", + filename, line); + continue; /* ignore this line */ + } + if (*s == '[') { /* section */ char *p = ++s; while (isalnum(*p) || *p == '_')