Commit 049f0daf authored by Alejandro Mery's avatar Alejandro Mery
Browse files

fex2bin: refactored generate_bin() as calculate_bin_size() into script_bin.c

parent 4484aac3
......@@ -13,7 +13,7 @@ clean:
$(TOOLS): Makefile sunxi-tools.h
fex2bin: script.c script.h
fex2bin: script.c script.h script_bin.h script_bin.c
bin2fex: script.h
%: %.c %.h
......
......@@ -23,8 +23,13 @@
#include <stdlib.h>
#include <string.h>
#include "script_bin.h"
#define MAX_LINE 255
#define pr_info(...) fprintf(stderr, "fex2bin: " __VA_ARGS__)
#define pr_err(...) pr_info("E: " __VA_ARGS__)
/** find first not blank char */
static inline char *skip_blank(char *p)
{
......@@ -44,63 +49,6 @@ static inline char *rtrim(const char *s, char *p)
return p;
}
#define WORDS(S) (((S)+(sizeof(uint32_t)-1))/(sizeof(uint32_t)))
/**
*/
static int generate_bin(FILE *UNUSED(out), const char *UNUSED(filename),
struct script *script)
{
size_t sections = 0, entries = 0, words = 0;
struct list_entry *ls, *le;
struct script_section *section;
struct script_entry *entry;
struct script_string_entry *string;
/* count */
for (ls = list_first(&script->sections); ls;
ls = list_next(&script->sections, ls)) {
section = container_of(ls, struct script_section, sections);
size_t c = 0;
for (le = list_first(&section->entries); le;
le = list_next(&section->entries, le)) {
size_t size = 0;
entry = container_of(le, struct script_entry, entries);
c++;
switch(entry->type) {
case SCRIPT_VALUE_TYPE_NULL:
case SCRIPT_VALUE_TYPE_SINGLE_WORD:
size = sizeof(uint32_t);
break;
case SCRIPT_VALUE_TYPE_STRING:
string = container_of(entry, struct script_string_entry,
entry);
size = string->l;
break;
case SCRIPT_VALUE_TYPE_GPIO:
size = sizeof(struct script_bin_gpio_value);
break;
default:
abort();
}
words += WORDS(size);
}
if (c>0) {
sections++;
entries += c;
}
}
errf("sections:%zu entries:%zu data:%zu/%zu -> %zu\n", sections, entries,
words, words*sizeof(uint32_t),
sizeof(struct script_bin_head) +
sections*sizeof(struct script_bin_section) +
entries*sizeof(struct script_bin_entry) +
words*sizeof(uint32_t));
return 0;
}
/**
*/
static int parse_fex(FILE *in, const char *filename, struct script *script)
......@@ -277,12 +225,12 @@ parse_error:
*/
int main(int argc, char *argv[])
{
int ret = -1;
int ret = 1;
FILE *in = stdin, *out = stdout;
const char *fn[] = {"stdin", "stdout"};
struct script *script;
errf("WARNING: this tool is still not functional, sorry\n");
pr_info("WARNING: this tool is still not functional, sorry\n");
if (argc>1) {
if (strcmp(argv[1],"-") == 0)
......@@ -308,10 +256,15 @@ int main(int argc, char *argv[])
goto done;
}
if (parse_fex(in, fn[0], script) &&
generate_bin(out, fn[1], script))
if (parse_fex(in, fn[0], script)) {
size_t sections, entries, bin_size;
bin_size = calculate_bin_size(script, &sections, &entries);
ret = 0;
(void)bin_size;
}
script_delete(script);
goto done;
usage:
......
/*
* 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"
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "script.h"
#include "script_bin.h"
#define pr_info(...) fprintf(stderr, "fex2bin: " __VA_ARGS__)
#define pr_err(...) pr_info("E: " __VA_ARGS__)
#define WORDS(S) (((S)+(sizeof(uint32_t)-1))/(sizeof(uint32_t)))
/**
*/
size_t calculate_bin_size(struct script *script,
size_t *sections, size_t *entries)
{
size_t words = 0, bin_size = 0;
struct list_entry *ls, *le;
struct script_section *section;
struct script_entry *entry;
struct script_string_entry *string;
*sections = *entries = 0;
/* count */
for (ls = list_first(&script->sections); ls;
ls = list_next(&script->sections, ls)) {
section = container_of(ls, struct script_section, sections);
size_t c = 0;
for (le = list_first(&section->entries); le;
le = list_next(&section->entries, le)) {
size_t size = 0;
entry = container_of(le, struct script_entry, entries);
c++;
switch(entry->type) {
case SCRIPT_VALUE_TYPE_NULL:
case SCRIPT_VALUE_TYPE_SINGLE_WORD:
size = sizeof(uint32_t);
break;
case SCRIPT_VALUE_TYPE_STRING:
string = container_of(entry, struct script_string_entry,
entry);
size = string->l;
break;
case SCRIPT_VALUE_TYPE_GPIO:
size = sizeof(struct script_bin_gpio_value);
break;
default:
abort();
}
words += WORDS(size);
}
if (c>0) {
*sections += 1;
*entries += c;
}
}
bin_size = sizeof(struct script_bin_head) +
(*sections)*sizeof(struct script_bin_section) +
(*entries)*sizeof(struct script_bin_entry) +
words*sizeof(uint32_t);
pr_info("sections:%zu entries:%zu data:%zu/%zu -> %zu\n",
*sections, *entries, words, words*sizeof(uint32_t),
bin_size);
return bin_size;
}
#undef WORDS
/*
* 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/>.
*/
#ifndef _SUBXI_TOOLS_SCRIPT_BIN_H
#define _SUBXI_TOOLS_SCRIPT_BIN_H
size_t calculate_bin_size(struct script *script,
size_t *sections, size_t *entries);
#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