summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README16
-rw-r--r--tools/COPYING339
-rw-r--r--tools/Makefile67
-rw-r--r--tools/common.h43
-rw-r--r--tools/fexc.c330
-rw-r--r--tools/fexc.h30
-rw-r--r--tools/include/list.h84
-rw-r--r--tools/script.c268
-rw-r--r--tools/script.h120
-rw-r--r--tools/script_bin.c357
-rw-r--r--tools/script_bin.h59
-rw-r--r--tools/script_fex.c367
-rw-r--r--tools/script_fex.h23
-rw-r--r--tools/script_uboot.c256
-rw-r--r--tools/script_uboot.h22
15 files changed, 2381 insertions, 0 deletions
diff --git a/README b/README
new file mode 100644
index 0000000..6db65d7
--- /dev/null
+++ b/README
@@ -0,0 +1,16 @@
+
+This repository contains the base fex files for the A31-uQ7 module (codename
+pangolin) and a source tree for the tools you need to convert them to binary.
+
+
+
+TOOLS
+
+Steps to build the tools:
+
+cd tools
+make
+
+The result will be 2 executable binaries named "fex2bin" and "bin2fex" which
+you can use for the conversion of fex files.
+
diff --git a/tools/COPYING b/tools/COPYING
new file mode 100644
index 0000000..d159169
--- /dev/null
+++ b/tools/COPYING
@@ -0,0 +1,339 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users. This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it. (Some other Free Software Foundation software is covered by
+the GNU Lesser General Public License instead.) You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have. You must make sure that they, too, receive or can get the
+source code. And you must show them these terms so they know their
+rights.
+
+ We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+ Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software. If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary. To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ GNU GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License. The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language. (Hereinafter, translation is included without limitation in
+the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+ 1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+ 2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ b) You must cause any work that you distribute or publish, that in
+ whole or in part contains or is derived from the Program or any
+ part thereof, to be licensed as a whole at no charge to all third
+ parties under the terms of this License.
+
+ c) If the modified program normally reads commands interactively
+ when run, you must cause it, when started running for such
+ interactive use in the most ordinary way, to print or display an
+ announcement including an appropriate copyright notice and a
+ notice that there is no warranty (or else, saying that you provide
+ a warranty) and that users may redistribute the program under
+ these conditions, and telling the user how to view a copy of this
+ License. (Exception: if the Program itself is interactive but
+ does not normally print such an announcement, your work based on
+ the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+ a) Accompany it with the complete corresponding machine-readable
+ source code, which must be distributed under the terms of Sections
+ 1 and 2 above on a medium customarily used for software interchange; or,
+
+ b) Accompany it with a written offer, valid for at least three
+ years, to give any third party, for a charge no more than your
+ cost of physically performing source distribution, a complete
+ machine-readable copy of the corresponding source code, to be
+ distributed under the terms of Sections 1 and 2 above on a medium
+ customarily used for software interchange; or,
+
+ c) Accompany it with the information you received as to the offer
+ to distribute corresponding source code. (This alternative is
+ allowed only for noncommercial distribution and only if you
+ received the program in object code or executable form with such
+ an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it. For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable. However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License. Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+ 5. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Program or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+ 6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+ 7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all. For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation. If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+ 10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission. For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this. Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+ NO WARRANTY
+
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ 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 2 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, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+ Gnomovision version 69, Copyright (C) year name of author
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+ <signature of Ty Coon>, 1 April 1989
+ Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs. If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.
diff --git a/tools/Makefile b/tools/Makefile
new file mode 100644
index 0000000..8749fc7
--- /dev/null
+++ b/tools/Makefile
@@ -0,0 +1,67 @@
+# Copyright (C) 2012 Alejandro Mery <amery@geeks.cl>
+# Copyright (C) 2012,2013 Henrik Nordstrom <henrik@henriknordstrom.net>
+# Copyright (C) 2013 Patrick Wood <patrickhwood@gmail.com>
+# Copyright (C) 2013 Pat Wood <Pat.Wood@efi.com>
+#
+# 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 2 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/>.
+
+CC ?= gcc
+CFLAGS = -g -O0 -Wall -Wextra
+CFLAGS += -std=c99 $(DEFINES)
+CFLAGS += -Iinclude/
+
+DEFINES = -D_POSIX_C_SOURCE=200112L
+# Define _BSD_SOURCE, necessary to expose all endian conversions properly.
+# See http://linux.die.net/man/3/endian
+DEFINES += -D_BSD_SOURCE
+# glibc 2.20+ also requires _DEFAULT_SOURCE
+DEFINES += -D_DEFAULT_SOURCE
+ifeq (NetBSD,$(OS))
+# add explicit _NETBSD_SOURCE, see https://github.com/linux-sunxi/sunxi-tools/pull/22
+DEFINES += -D_NETBSD_SOURCE
+endif
+
+# Tools useful on host and target
+TOOLS = sunxi-fexc
+
+# Symlinks to sunxi-fexc
+FEXC_LINKS = bin2fex fex2bin
+
+.PHONY: all clean
+
+all: tools
+
+tools: $(FEXC_LINKS)
+
+clean:
+ @rm -vf $(TOOLS) $(FEXC_LINKS)
+ @rm -vf *.o *.elf *.sunxi *.bin *.nm *.orig
+
+$(TOOLS): Makefile common.h
+
+fex2bin bin2fex: sunxi-fexc
+ ln -nsf $< $@
+
+sunxi-fexc: fexc.h script.h script.c \
+ script_uboot.h script_uboot.c \
+ script_bin.h script_bin.c \
+ script_fex.h script_fex.c
+
+sunxi-%: %.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(filter %.c,$^) $(LIBS)
+
+.gitignore: Makefile
+ @for x in $(TOOLS) $(FEXC_LINKS) '*.o' '*.swp'; do \
+ echo "$$x"; \
+ done > $@
diff --git a/tools/common.h b/tools/common.h
new file mode 100644
index 0000000..66ca22e
--- /dev/null
+++ b/tools/common.h
@@ -0,0 +1,43 @@
+/*
+ * 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 2 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 _SUNXI_TOOLS_COMMON_H
+#define _SUNXI_TOOLS_COMMON_H
+
+#include <stddef.h> /* offsetof */
+
+/** flag function argument as unused */
+#ifdef UNUSED
+#elif defined(__GNUC__)
+# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
+#else
+# define UNUSED(x) UNUSED_ ## x
+#endif
+
+/** finds the parent of an struct member */
+#ifndef container_of
+#define container_of(P,T,M) (T *)((char *)(P) - offsetof(T, M))
+#endif
+
+/** calculate number of elements of an array */
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(A) (sizeof(A)/sizeof((A)[0]))
+#endif
+
+/** shortcut to printf to stderr */
+#define errf(...) fprintf(stderr, __VA_ARGS__)
+
+#endif /* _SUNXI_TOOLS_COMMON_H */
diff --git a/tools/fexc.c b/tools/fexc.c
new file mode 100644
index 0000000..9f603dd
--- /dev/null
+++ b/tools/fexc.c
@@ -0,0 +1,330 @@
+/*
+ * 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 2 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 "fexc.h"
+
+#include <errno.h>
+#include <libgen.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#define pr_info(...) errf("fexc: " __VA_ARGS__)
+#define pr_err(...) errf("E: fexc: " __VA_ARGS__)
+
+enum script_format {
+ FEX_SCRIPT_FORMAT,
+ BIN_SCRIPT_FORMAT,
+ UBOOT_HEADER_FORMAT,
+};
+
+/*
+ */
+static inline char *read_all(int fd, const char *filename, size_t *size)
+{
+ size_t buf_size = 4096, count = 0;
+ char *p, *buf = malloc(buf_size);
+ if (!buf) {
+ pr_err("%s: %s\n", "malloc", strerror(errno));
+ return NULL;
+ }
+ p = buf;
+ while (1) {
+ ssize_t rc = read(fd, p, buf_size-count);
+ if (rc == 0)
+ break;
+ else if (rc > 0) {
+ count += rc;
+ p += rc;
+
+ if (count == buf_size) {
+ char *new;
+ buf_size *= 2;
+ new = realloc(buf, buf_size);
+ if (!new) {
+ pr_err("%s: %s\n", "realloc",
+ strerror(errno));
+ free(buf);
+ return NULL;
+ } else if (new != buf) {
+ buf = new;
+ p = buf + count;
+ }
+ }
+ } else if (errno != EAGAIN && errno != EINTR) {
+ pr_err("%s: %s: %s\n", filename,
+ "read", strerror(errno));
+ free(buf);
+ return NULL;
+ }
+ }
+
+ *size = count;
+ return buf;
+}
+
+/*
+ */
+static inline int script_parse(enum script_format format,
+ const char *filename,
+ struct script *script)
+{
+ int ret = 0;
+ switch (format) {
+ case FEX_SCRIPT_FORMAT: {
+ FILE *in = stdin;
+ if (!filename)
+ filename = "<stdin>";
+ else if ((in = fopen(filename, "r")) == NULL) {
+ pr_err("%s: %s\n", filename, strerror(errno));
+ break;
+ }
+ ret = script_parse_fex(in, filename, script);
+ fclose(in);
+ }; break;
+ case BIN_SCRIPT_FORMAT: {
+ int in = 0; /* stdin */
+ struct stat sb;
+ void *bin = NULL;
+ size_t bin_size;
+ int allocated = 1;
+
+ if (!filename)
+ filename = "<stdin>";
+ else if ((in = open(filename, O_RDONLY)) < 0) {
+ pr_err("%s: %s\n", filename, strerror(errno));
+ break;
+ }
+
+ if (fstat(in, &sb) == -1) {
+ pr_err("%s: %s: %s\n", filename,
+ "fstat", strerror(errno));
+ goto bin_close;
+ } else if (S_ISREG(sb.st_mode)) {
+ /* regular file, mmap it */
+ bin = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, in, 0);
+ if (bin == MAP_FAILED) {
+ pr_err("%s: %s: %s\n", filename,
+ "mmap", strerror(errno));
+ goto bin_close;
+ }
+ bin_size = sb.st_size;
+ allocated = 0;
+ } else {
+ /* something else... just read it all! */
+ bin = read_all(in, filename, &bin_size);
+ if (bin == NULL)
+ goto bin_close;
+ allocated = 1;
+ }
+
+ ret = script_decompile_bin(bin, bin_size, filename, script);
+ if (allocated)
+ free(bin);
+ else if (munmap(bin, bin_size) == -1) {
+ pr_err("%s: %s: %s\n", filename,
+ "munmap", strerror(errno));
+ }
+bin_close:
+ close(in);
+ }; break;
+ case UBOOT_HEADER_FORMAT: /* not valid input */
+ ;
+ }
+ return ret;
+}
+static inline int script_generate(enum script_format format,
+ const char *filename,
+ struct script *script)
+{
+ int ret = 0;
+ static int (*text_gen[3]) (FILE *, const char *, struct script *) = {
+ [FEX_SCRIPT_FORMAT] = script_generate_fex,
+ [UBOOT_HEADER_FORMAT] = script_generate_uboot,
+ };
+
+ if (text_gen[format]) {
+ FILE *out = stdout;
+
+ if (!filename)
+ filename = "<stdout>";
+ else if ((out = fopen(filename, "w")) == NULL) {
+ pr_err("%s: %s\n", filename, strerror(errno));
+ goto done;
+ }
+
+ ret = text_gen[format](out, filename, script);
+ fclose(out);
+ } else {
+ int out = 1; /* stdout */
+ size_t sections, entries, bin_size;
+ void *bin;
+
+ if (!filename)
+ filename = "<stdout>";
+ else if ((out = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
+ pr_err("%s: %s\n", filename, strerror(errno));
+ goto done;
+ }
+
+ bin_size = script_bin_size(script, &sections, &entries);
+ bin = calloc(1, bin_size);
+ if (!bin)
+ pr_err("%s: %s\n", "malloc", strerror(errno));
+ else if (script_generate_bin(bin, bin_size, script, sections, entries)) {
+ char *p = bin;
+ while(bin_size) {
+ ssize_t wc = write(out, p, bin_size);
+
+ if (wc>0) {
+ p += wc;
+ bin_size -= wc;
+ } else if (wc < 0 && errno != EINTR) {
+ pr_err("%s: %s: %s\n", filename,
+ "write", strerror(errno));
+ break;
+ }
+ }
+ ret = (bin_size == 0);
+ }
+ free(bin);
+ close(out);
+ }
+done:
+ return ret;
+}
+
+/*
+ */
+static inline void app_usage(const char *arg0, int mode)
+{
+ errf("Usage: %s [-vq]%s[<input> [<output>]]\n", arg0,
+ mode ? " " : " [-I <infmt>] [-O <outfmt>] ");
+
+ if (mode == 0)
+ fputs("\ninfmt: fex, bin (default:fex)"
+ "\noutfmt: fex, bin, uboot (default:bin)\n",
+ stderr);
+}
+
+static inline int app_choose_mode(char *arg0)
+{
+ const char *name = basename(arg0);
+ if (strcmp(name, "fex2bin") == 0)
+ return 1;
+ else if (strcmp(name, "bin2fex") == 0)
+ return 2;
+ else
+ return 0;
+}
+
+/*
+ */
+int main(int argc, char *argv[])
+{
+ static const char *formats[] = { "fex", "bin", "uboot", NULL };
+ enum script_format infmt=FEX_SCRIPT_FORMAT;
+ enum script_format outfmt=BIN_SCRIPT_FORMAT;
+ const char *filename[] = { NULL /*stdin*/, NULL /*stdout*/};
+ struct script *script;
+
+ int app_mode = app_choose_mode(argv[0]);
+
+ const char *opt_string = "I:O:vq?";
+ if (app_mode != 0) opt_string += 4; /* disallow -I and -O */
+ int opt, ret = 1;
+ int verbose = 0;
+
+ if (app_mode == 2) { /* bin2fex */
+ infmt = BIN_SCRIPT_FORMAT;
+ outfmt = FEX_SCRIPT_FORMAT;
+ }
+
+ while ((opt = getopt(argc, argv, opt_string)) != -1) {
+ switch (opt) {
+ case 'I':
+ infmt=0;
+ for (const char **f = formats; *f; f++, infmt++) {
+ if (strcmp(*f, optarg) == 0)
+ break;
+ }
+ switch (infmt) {
+ case FEX_SCRIPT_FORMAT:
+ case BIN_SCRIPT_FORMAT:
+ break;
+ default:
+ errf("%s: invalid format -- \"%s\"\n",
+ argv[0], optarg);
+ goto show_usage;
+ }
+ break;
+ case 'O':
+ outfmt=0;
+ for (const char **f = formats; *f; f++, outfmt++) {
+ if (strcmp(*f, optarg) == 0)
+ break;
+ }
+ if (!formats[outfmt]) {
+ errf("%s: invalid format -- \"%s\"\n",
+ argv[0], optarg);
+ goto show_usage;
+ }
+ break;
+ case 'v':
+ verbose++;
+ break;
+ case 'q':
+ verbose--;
+ break;
+ default:
+show_usage:
+ app_usage(argv[0], app_mode);
+ goto done;
+ }
+ }
+
+ switch (argc - optind) {
+ case 2:
+ filename[1] = argv[optind+1]; /* out */
+ case 1:
+ if (strcmp(argv[optind], "-") != 0)
+ filename[0] = argv[optind]; /* in */
+ case 0:
+ break;
+ default:
+ goto show_usage;
+ }
+
+ if (verbose>0)
+ errf("%s: from %s:%s to %s:%s\n", argv[0],
+ formats[infmt], filename[0]?filename[0]:"<stdin>",
+ formats[outfmt], filename[1]?filename[1]:"<stdout>");
+
+ if ((script = script_new()) == NULL) {
+ perror("malloc");
+ goto done;
+ } else if (script_parse(infmt, filename[0], script) &&
+ script_generate(outfmt, filename[1], script)) {
+ ret = 0;
+ }
+ script_delete(script);
+done:
+ return ret;
+}
diff --git a/tools/fexc.h b/tools/fexc.h
new file mode 100644
index 0000000..c5b32a9
--- /dev/null
+++ b/tools/fexc.h
@@ -0,0 +1,30 @@
+/*
+ * 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 2 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 _SUNXI_TOOLS_FEXC_H
+#define _SUNXI_TOOLS_FEXC_H
+
+#include "common.h"
+
+#include <stdint.h>
+#include <stdio.h>
+
+#include "script.h"
+#include "script_bin.h"
+#include "script_fex.h"
+#include "script_uboot.h"
+
+#endif
diff --git a/tools/include/list.h b/tools/include/list.h
new file mode 100644
index 0000000..88185ce
--- /dev/null
+++ b/tools/include/list.h
@@ -0,0 +1,84 @@
+/*
+ * 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 2 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 _SUNXI_TOOLS_LIST_H
+#define _SUNXI_TOOLS_LIST_H
+
+/*
+ * list
+ */
+
+/** a list hook */
+struct list_entry {
+ struct list_entry *prev;
+ struct list_entry *next;
+};
+
+/** initialize an empty list hook */
+static inline void list_init(struct list_entry *self)
+{
+ self->prev = self->next = self;
+}
+
+/** puts an entry between two other on a list */
+static inline void list_inject(struct list_entry *l,
+ struct list_entry *prev,
+ struct list_entry *next)
+{
+ l->prev = prev;
+ l->next = next;
+
+ next->prev = l;
+ prev->next = l;
+}
+
+#define list_insert(H, E) list_inject((E), (H), (H)->next)
+#define list_append(H, E) list_inject((E), (H)->prev, (H))
+
+/** removes an entry for the list where it's contained */
+static inline void list_remove(struct list_entry *l)
+{
+ struct list_entry *prev = l->prev, *next = l->next;
+ next->prev = prev;
+ prev->next = next;
+}
+
+/** returns first element of a list */
+static inline struct list_entry *list_first(struct list_entry *l)
+{
+ return (l->next == l) ? NULL : l->next;
+}
+
+/** returns last element of a list */
+static inline struct list_entry *list_last(struct list_entry *l)
+{
+ return (l->prev == l) ? NULL : l->prev;
+}
+
+/** returns next element on a list */
+static inline struct list_entry *list_next(struct list_entry *l,
+ struct list_entry *e)
+{
+ return (e->next == l) ? NULL : e->next;
+}
+
+/** is list empty? */
+static inline int list_empty(struct list_entry *l)
+{
+ return (l->prev == l);
+}
+
+#endif /* _SUNXI_TOOLS_LIST_H */
diff --git a/tools/script.c b/tools/script.c
new file mode 100644
index 0000000..3a9c7e7
--- /dev/null
+++ b/tools/script.c
@@ -0,0 +1,268 @@
+/*
+ * 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 2 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 "common.h"
+
+#include <assert.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "script.h"
+
+/*
+ */
+struct script *script_new(void)
+{
+ struct script *script;
+ if ((script = malloc(sizeof(*script))))
+ list_init(&script->sections);
+ return script;
+}
+
+void script_delete(struct script *script)
+{
+ struct list_entry *o;
+
+ assert(script);
+
+ while ((o = list_last(&script->sections))) {
+ struct script_section *section = container_of(o,
+ struct script_section, sections);
+
+ script_section_delete(section);
+ }
+
+ free(script);
+}
+
+/*
+ */
+struct script_section *script_section_new(struct script *script,
+ const char *name)
+{
+ struct script_section *section;
+
+ assert(script);
+ assert(name && *name);
+
+ if ((section = malloc(sizeof(*section)))) {
+ size_t l = strlen(name);
+ if (l>31) /* truncate */
+ l=31;
+ memcpy(section->name, name, l);
+ section->name[l] = '\0';
+
+ list_init(&section->entries);
+ list_append(&script->sections, &section->sections);
+ }
+ return section;
+}
+
+void script_section_delete(struct script_section *section)
+{
+ struct list_entry *o;
+
+ assert(section);
+
+ while ((o = list_last(&section->entries))) {
+ struct script_entry *entry = container_of(o,
+ struct script_entry, entries);
+
+ script_entry_delete(entry);
+ }
+
+ if (!list_empty(&section->sections))
+ list_remove(&section->sections);
+}
+
+struct script_section *script_find_section(struct script *script,
+ const char *name)
+{
+ struct list_entry *o;
+ struct script_section *section;
+
+ assert(script);
+ assert(name);
+
+ for (o = list_first(&script->sections); o;
+ o = list_next(&script->sections, o)) {
+ section = container_of(o, struct script_section, sections);
+
+ if (strcmp(section->name, name) == 0)
+ return section;
+ }
+
+ return NULL;
+}
+
+/*
+ */
+static inline void script_entry_append(struct script_section *section,
+ struct script_entry *entry,
+ enum script_value_type type,
+ const char *name)
+{
+ size_t l;
+
+ assert(section);
+ assert(entry);
+ assert(name);
+
+ l = strlen(name);
+ if (l>31) /* truncate */
+ l=31;
+ memcpy(entry->name, name, l);
+ entry->name[l] = '\0';
+
+ entry->type = type;
+
+ list_append(&section->entries, &entry->entries);
+}
+
+void script_entry_delete(struct script_entry *entry)
+{
+ void *container;
+
+ assert(entry);
+ assert(entry->type == SCRIPT_VALUE_TYPE_SINGLE_WORD ||
+ entry->type == SCRIPT_VALUE_TYPE_STRING ||
+ entry->type == SCRIPT_VALUE_TYPE_GPIO ||
+ entry->type == SCRIPT_VALUE_TYPE_NULL);
+
+ if (!list_empty(&entry->entries))
+ list_remove(&entry->entries);
+
+ switch(entry->type) {
+ case SCRIPT_VALUE_TYPE_SINGLE_WORD:
+ container = container_of(entry, struct script_single_entry, entry);
+ break;
+ case SCRIPT_VALUE_TYPE_STRING:
+ container = container_of(entry, struct script_string_entry, entry);
+ break;
+ case SCRIPT_VALUE_TYPE_GPIO:
+ container = container_of(entry, struct script_gpio_entry, entry);
+ break;
+ case SCRIPT_VALUE_TYPE_NULL:
+ container = container_of(entry, struct script_null_entry, entry);
+ break;
+ default:
+ abort();
+ }
+
+ free(container);
+}
+
+struct script_null_entry *script_null_entry_new(struct script_section *section,
+ const char *name)
+{
+ struct script_null_entry *entry;
+
+ assert(section);
+ assert(name && *name);
+
+ if ((entry = malloc(sizeof(*entry)))) {
+ script_entry_append(section, &entry->entry,
+ SCRIPT_VALUE_TYPE_NULL, name);
+ }
+
+ return entry;
+}
+
+struct script_single_entry *script_single_entry_new(struct script_section *section,
+ const char *name,
+ uint32_t value)
+{
+ struct script_single_entry *entry;
+
+ assert(section);
+ assert(name && *name);
+
+ if ((entry = malloc(sizeof(*entry)))) {
+ entry->value = value;
+
+ script_entry_append(section, &entry->entry,
+ SCRIPT_VALUE_TYPE_SINGLE_WORD, name);
+ }
+
+ return entry;
+}
+
+struct script_string_entry *script_string_entry_new(struct script_section *section,
+ const char *name,
+ size_t l, const char *s)
+{
+ struct script_string_entry *entry;
+
+ assert(section);
+ assert(name);
+ assert(s);
+
+ if ((entry = malloc(sizeof(*entry)+l+1))) {
+ entry->l = l;
+ memcpy(entry->string, s, l);
+ entry->string[l] = '\0';
+
+ script_entry_append(section, &entry->entry,
+ SCRIPT_VALUE_TYPE_STRING, name);
+ }
+
+ return entry;
+}
+
+struct script_gpio_entry *script_gpio_entry_new(struct script_section *section,
+ const char *name,
+ unsigned port, unsigned num,
+ int32_t data[4])
+{
+ struct script_gpio_entry *entry;
+
+ assert(section);
+ assert(name && *name);
+
+ if ((entry = malloc(sizeof(*entry)))) {
+ entry->port = port;
+ entry->port_num = num;
+ for (int i=0; i<4; i++)
+ entry->data[i] = data[i];
+
+ script_entry_append(section, &entry->entry,
+ SCRIPT_VALUE_TYPE_GPIO, name);
+ }
+
+ return entry;
+}
+
+struct script_entry *script_find_entry(struct script_section *section,
+ const char *name)
+{
+ struct list_entry *o;
+ struct script_entry *ep;
+
+ assert(section);
+ assert(name);
+
+ for (o = list_first(&section->entries); o;
+ o = list_next(&section->entries, o)) {
+ ep = container_of(o, struct script_entry, entries);
+
+ if (strcmp(ep->name, name) == 0)
+ return ep;
+ }
+
+ return NULL;
+}
diff --git a/tools/script.h b/tools/script.h
new file mode 100644
index 0000000..7df3151
--- /dev/null
+++ b/tools/script.h
@@ -0,0 +1,120 @@
+/*
+ * 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 2 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 _SUNXI_TOOLS_SCRIPT_H
+#define _SUNXI_TOOLS_SCRIPT_H
+
+#include "list.h"
+
+#define GPIO_BANK_MAX 14 /* N, (zero-based) index 13 */
+
+/** head of the data tree */
+struct script {
+ struct list_entry sections;
+};
+
+/** head of each section */
+struct script_section {
+ char name[32];
+
+ struct list_entry sections;
+ struct list_entry entries;
+};
+
+/** types of values */
+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,
+};
+
+/** generic entry */
+struct script_entry {
+ char name[32];
+ enum script_value_type type;
+
+ struct list_entry entries;
+};
+
+/** null entry */
+struct script_null_entry {
+ struct script_entry entry;
+};
+
+/** entry with 32b value */
+struct script_single_entry {
+ struct script_entry entry;
+
+ uint32_t value;
+};
+
+/** entry with string value */
+struct script_string_entry {
+ struct script_entry entry;
+
+ size_t l;
+ char string[];
+};
+
+/** entry describing a GPIO */
+struct script_gpio_entry {
+ struct script_entry entry;
+
+ unsigned port, port_num;
+ int32_t data[4];
+};
+
+/** create a new script tree */
+struct script *script_new(void);
+/** deletes a tree recursively */
+void script_delete(struct script *);
+
+/** create a new section appended to a given tree */
+struct script_section *script_section_new(struct script *script,
+ const char *name);
+/** deletes a section recursvely and removes it from the script */
+void script_section_delete(struct script_section *section);
+
+/** find existing section */
+struct script_section *script_find_section(struct script *script,
+ const char *name);
+
+/** deletes an entry and removes it from the section */
+void script_entry_delete(struct script_entry *entry);
+
+/** create a new empty/null entry appended to a section */
+struct script_null_entry *script_null_entry_new(struct script_section *section,
+ const char *name);
+/** create a new single word entry appended to a section */
+struct script_single_entry *script_single_entry_new(struct script_section *section,
+ const char *name,
+ uint32_t value);
+/** create a new string entry appended to a section */
+struct script_string_entry *script_string_entry_new(struct script_section *section,
+ const char *name,
+ size_t l, const char *s);
+/** create a new GPIO entry appended to a section */
+struct script_gpio_entry *script_gpio_entry_new(struct script_section *script,
+ const char *name,
+ unsigned port, unsigned num,
+ int32_t data[4]);
+
+/** find existing entry in a giving section */
+struct script_entry *script_find_entry(struct script_section *section,
+ const char *name);
+#endif
diff --git a/tools/script_bin.c b/tools/script_bin.c
new file mode 100644
index 0000000..3315f67
--- /dev/null
+++ b/tools/script_bin.c
@@ -0,0 +1,357 @@
+/*
+ * 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 2 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 "common.h"
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "script.h"
+#include "script_bin.h"
+
+#define pr_info(...) errf("fexc-bin: " __VA_ARGS__)
+#define pr_err(...) errf("E: fexc-bin: " __VA_ARGS__)
+
+#ifdef DEBUG
+#define pr_debug(...) errf("D: fexc-bin: " __VA_ARGS__)
+#else
+#define pr_debug(...)
+#endif
+
+#define PTR(B, OFF) (void*)((char*)(B)+(OFF))
+#define WORDS(S) (((S)+(sizeof(uint32_t)-1))/(sizeof(uint32_t)))
+
+/*
+ * generator
+ */
+size_t script_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);
+ }
+ *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_debug("sections:%zu entries:%zu data:%zu/%zu -> %zu\n",
+ *sections, *entries, words, words*sizeof(uint32_t),
+ bin_size);
+ return bin_size;
+}
+
+int script_generate_bin(void *bin, size_t UNUSED(bin_size),
+ struct script *script,
+ size_t sections, size_t entries)
+{
+ struct script_bin_head *head;
+ struct script_bin_section *section;
+ struct script_bin_entry *entry;
+ void *data;
+
+ struct list_entry *ls, *le;
+
+ head = bin;
+ section = head->section;
+ entry = (void*)section+sections*sizeof(*section);
+ data = (void*)entry+entries*sizeof(*entry);
+
+ pr_debug("head....:%p\n", head);
+ pr_debug("section.:%p (offset:%zu, each:%zu)\n", section,
+ (void*)section-bin, sizeof(*section));
+ pr_debug("entry...:%p (offset:%zu, each:%zu)\n", entry,
+ (void*)entry-bin, sizeof(*entry));
+ pr_debug("data....:%p (offset:%zu)\n", data,
+ (void*)data-bin);
+
+ head->sections = sections;
+ head->version[0] = 0;
+ head->version[1] = 1;
+ head->version[2] = 2;
+
+ for (ls = list_first(&script->sections); ls;
+ ls = list_next(&script->sections, ls)) {
+ struct script_section *s;
+ size_t c = 0;
+ s = container_of(ls, struct script_section, sections);
+
+ memcpy(section->name, s->name, strlen(s->name));
+ section->offset = ((void*)entry-bin)>>2;
+
+ for (le = list_first(&s->entries); le;
+ le = list_next(&s->entries, le)) {
+ struct script_entry *e;
+ e = container_of(le, struct script_entry, entries);
+ size_t size = 0;
+
+ memcpy(entry->name, e->name, strlen(e->name));
+ entry->offset = ((void*)data-bin)>>2;
+ entry->pattern = (e->type<<16);
+
+ switch(e->type) {
+ case SCRIPT_VALUE_TYPE_SINGLE_WORD: {
+ struct script_single_entry *single;
+ int32_t *bdata = data;
+ single = container_of(e, struct script_single_entry, entry);
+
+ *bdata = single->value;
+ size = sizeof(*bdata);
+ }; break;
+ case SCRIPT_VALUE_TYPE_STRING: {
+ struct script_string_entry *string;
+ string = container_of(e, struct script_string_entry, entry);
+ size = string->l;
+ memcpy(data, string->string, size);
+ /* align */
+ size += sizeof(uint32_t)-1;
+ size /= sizeof(uint32_t);
+ size *= sizeof(uint32_t);
+ }; break;
+ case SCRIPT_VALUE_TYPE_MULTI_WORD:
+ abort();
+ case SCRIPT_VALUE_TYPE_GPIO: {
+ struct script_gpio_entry *gpio;
+ struct script_bin_gpio_value *bdata = data;
+ gpio = container_of(e, struct script_gpio_entry, entry);
+ bdata->port = gpio->port;
+ bdata->port_num = gpio->port_num;
+ bdata->mul_sel = gpio->data[0];
+ bdata->pull = gpio->data[1];
+ bdata->drv_level = gpio->data[2];
+ bdata->data = gpio->data[3];
+ size = sizeof(*bdata);
+ }; break;
+ case SCRIPT_VALUE_TYPE_NULL:
+ size = sizeof(uint32_t);
+ break;
+ }
+
+ data += size;
+ entry->pattern |= (size>>2);
+ pr_debug("%s.%s <%p> (type:%d, words:%d (%zu), offset:%d)\n",
+ section->name, entry->name, entry,
+ (entry->pattern>>16) & 0xffff,
+ (entry->pattern>>0) & 0xffff, size,
+ entry->offset);
+ c++;
+ entry++;
+ }
+
+ section->length = c;
+ pr_debug("%s <%p> (length:%d, offset:%d)\n",
+ section->name, section, section->length, section->offset);
+
+ section++;
+ }
+ return 1;
+}
+
+/*
+ * decompiler
+ */
+static int decompile_section(void *bin, size_t bin_size,
+ const char *filename,
+ struct script_bin_section *section,
+ struct script *script)
+{
+ struct script_bin_entry *entry;
+ struct script_section *s;
+ int size;
+
+ if ((section->offset < 0) || (section->offset > (int)(bin_size / 4))) {
+ pr_err("Malformed data: invalid section offset: %d\n",
+ section->offset);
+ return 0;
+ }
+
+ size = bin_size - 4 * section->offset;
+
+ if ((section->length < 0) ||
+ (section->length > (size / (int)sizeof(struct script_bin_entry)))) {
+ pr_err("Malformed data: invalid section length: %d\n",
+ section->length);
+ return 0;
+ }
+
+ if ((s = script_section_new(script, section->name)) == NULL)
+ goto malloc_error;
+
+ entry = PTR(bin, section->offset<<2);
+
+ for (int i = section->length; i--; entry++) {
+ void *data = PTR(bin, entry->offset<<2);
+ unsigned type, words;
+ 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;
+ if (words != 1) {
+ pr_err("%s: %s.%s: invalid length %d (assuming %d)\n",
+ filename, section->name, entry->name, words, 1);
+ }
+ if (!script_single_entry_new(s, entry->name, *v))
+ goto malloc_error;
+ }; break;
+ case SCRIPT_VALUE_TYPE_STRING: {
+ size_t bytes = words << 2;
+ const char *p, *pe, *v = data;
+
+ for(p=v, pe=v+bytes; *p && p!=pe; p++)
+ ; /* seek end-of-string */
+
+ if (!script_string_entry_new(s, entry->name, p-v, v))
+ goto malloc_error;
+ }; break;
+ case SCRIPT_VALUE_TYPE_GPIO: {
+ struct script_bin_gpio_value *gpio = data;
+ int32_t v[4];
+ if (words != 6) {
+ pr_err("%s: %s.%s: invalid length %d (assuming %d)\n",
+ filename, section->name, entry->name, words, 6);
+ } 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 ",
+ 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;
+ v[1] = gpio->pull;
+ v[2] = gpio->drv_level;
+ v[3] = gpio->data;
+
+ if (!script_gpio_entry_new(s, entry->name,
+ gpio->port, gpio->port_num,
+ v))
+ goto malloc_error;
+ }; break;
+ case SCRIPT_VALUE_TYPE_NULL:
+ if (!*entry->name) {
+ pr_err("%s: empty entry in section: %s\n", filename, section->name);
+ } else if (!script_null_entry_new(s, entry->name)) {
+ goto malloc_error;
+ }
+ break;
+ default:
+ pr_err("%s: %s.%s: unknown type %d\n",
+ filename, section->name, entry->name, type);
+ goto failure;
+ }
+ }
+ return 1;
+
+malloc_error:
+ pr_err("%s: %s\n", "malloc", strerror(errno));
+failure:
+ return 0;
+}
+
+#define SCRIPT_BIN_VERSION_LIMIT 0x10
+#define SCRIPT_BIN_SECTION_LIMIT 0x100
+
+int script_decompile_bin(void *bin, size_t bin_size,
+ const char *filename,
+ struct script *script)
+{
+ unsigned int i;
+ struct script_bin_head *head = bin;
+
+ 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",
+ head->version[0], head->version[1], head->version[2]);
+ return 0;
+ }
+
+ if (head->sections > SCRIPT_BIN_SECTION_LIMIT) {
+ pr_err("Malformed data: too many sections (%u).\n",
+ head->sections);
+ return 0;
+ }
+
+ pr_info("%s: version: %u.%u.%u\n", filename,
+ head->version[0] & 0x3FFF, head->version[1], head->version[2]);
+ pr_info("%s: size: %zu (%u sections)\n", filename,
+ bin_size, head->sections);
+
+ /* TODO: SANITY: compare head.sections with bin_size */
+ for (i=0; i < head->sections; i++) {
+ struct script_bin_section *section = &head->section[i];
+
+ if (!decompile_section(bin, bin_size, filename,
+ section, script))
+ return 0;
+ }
+ return 1;
+}
diff --git a/tools/script_bin.h b/tools/script_bin.h
new file mode 100644
index 0000000..8f3c1a3
--- /dev/null
+++ b/tools/script_bin.h
@@ -0,0 +1,59 @@
+/*
+ * 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 2 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 _SUNXI_TOOLS_SCRIPT_BIN_H
+#define _SUNXI_TOOLS_SCRIPT_BIN_H
+
+/** binary representation of the head of a section */
+struct script_bin_section {
+ char name[32];
+ int32_t length;
+ int32_t offset;
+};
+
+/** binary representation of the head of the script file */
+struct script_bin_head {
+ uint32_t sections;
+ uint32_t version[3];
+ struct script_bin_section section[];
+};
+
+/** binary representation of the head of an entry */
+struct script_bin_entry {
+ char name[32];
+ int32_t offset;
+ int32_t pattern;
+};
+
+/** binary representation of a GPIO */
+struct script_bin_gpio_value {
+ int32_t port;
+ int32_t port_num;
+ int32_t mul_sel;
+ int32_t pull;
+ int32_t drv_level;
+ int32_t data;
+};
+
+size_t script_bin_size(struct script *script,
+ size_t *sections, size_t *entries);
+
+int script_generate_bin(void *bin, size_t bin_size, struct script *script,
+ size_t sections, size_t entries);
+int script_decompile_bin(void *bin, size_t bin_size,
+ const char *filename,
+ struct script *script);
+#endif
diff --git a/tools/script_fex.c b/tools/script_fex.c
new file mode 100644
index 0000000..2975910
--- /dev/null
+++ b/tools/script_fex.c
@@ -0,0 +1,367 @@
+/*
+ * 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 2 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 "common.h"
+
+#include <ctype.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "script.h"
+#include "script_fex.h"
+
+#define MAX_LINE 255
+
+#define pr_info(...) errf("fexc-fex: " __VA_ARGS__)
+#define pr_err(...) errf("E: fexc-fex: " __VA_ARGS__)
+
+#ifdef DEBUG
+#define pr_debug(...) errf("D: fexc-fex: " __VA_ARGS__)
+#else
+#define pr_debug(...)
+#endif
+
+/*
+ * generator
+ */
+static inline size_t strlen2(const char *s)
+{
+ size_t l = strlen(s);
+ const char *p = &s[l-1];
+ while (l && *p >= '0' && *p <= '9') {
+ l--;
+ p--;
+ }
+ return l;
+}
+
+static int find_full_match(const char *s, size_t l, const char **list)
+{
+ while (*list) {
+ if (memcmp(s, *list, l) == 0)
+ return 1;
+ list++;
+ }
+
+ return 0;
+}
+
+/**
+ */
+static int decompile_single_mode(const char *name)
+{
+ static const char *hexa_entries[] = {
+ "dram_baseaddr", "dram_zq", "dram_tpr", "dram_emr",
+ "g2d_size",
+ "rtp_press_threshold", "rtp_sensitive_level",
+ "ctp_twi_addr", "csi_twi_addr", "csi_twi_addr_b", "tkey_twi_addr",
+ "lcd_gamma_tbl_",
+ "gsensor_twi_addr",
+ NULL };
+ size_t l = strlen2(name);
+
+ if (find_full_match(name, l, hexa_entries))
+ return 0;
+ else
+ return -1;
+}
+
+int script_generate_fex(FILE *out, const char *UNUSED(filename),
+ struct script *script)
+{
+ struct list_entry *ls, *le;
+ struct script_section *section;
+ struct script_entry *entry;
+
+ for (ls = list_first(&script->sections); ls;
+ ls = list_next(&script->sections, ls)) {
+ section = container_of(ls, struct script_section, sections);
+
+ fprintf(out, "[%s]\n", section->name);
+ for (le = list_first(&section->entries); le;
+ le = list_next(&section->entries, le)) {
+ entry = container_of(le, struct script_entry, entries);
+
+ switch(entry->type) {
+ case SCRIPT_VALUE_TYPE_SINGLE_WORD: {
+ int mode = decompile_single_mode(entry->name);
+ struct script_single_entry *single;
+ single = container_of(entry, struct script_single_entry, entry);
+
+ fprintf(out, "%s = ", entry->name);
+ if (mode < 0)
+ fprintf(out, "%d", single->value);
+ else if (mode > 0)
+ fprintf(out, "0x%0*x", mode, single->value);
+ else
+ fprintf(out, "0x%x", single->value);
+ fputc('\n', out);
+ }; break;
+ case SCRIPT_VALUE_TYPE_STRING: {
+ struct script_string_entry *string;
+ string = container_of(entry, struct script_string_entry, entry);
+ fprintf(out, "%s = \"%.*s\"\n", entry->name,
+ (int)string->l, string->string);
+ }; break;
+ case SCRIPT_VALUE_TYPE_MULTI_WORD:
+ abort();
+ case SCRIPT_VALUE_TYPE_GPIO: {
+ char port = 'A'-1;
+ struct script_gpio_entry *gpio;
+ gpio = container_of(entry, struct script_gpio_entry, entry);
+
+ if (gpio->port == 0xffff) {
+ fprintf(out, "%s = port:power%u", entry->name,
+ gpio->port_num);
+ } else {
+ port += gpio->port;
+ fprintf(out, "%s = port:P%c%02u", entry->name,
+ port, gpio->port_num);
+ }
+ for (const int *p = gpio->data, *pe = p+4; p != pe; p++) {
+ if (*p == -1)
+ fputs("<default>", out);
+ else
+ fprintf(out, "<%d>", *p);
+ }
+ fputc('\n', out);
+ }; break;
+ case SCRIPT_VALUE_TYPE_NULL:
+ fprintf(out, "%s =\n", entry->name);
+ break;
+ }
+ }
+ fputc('\n', out);
+ }
+ return 1;
+}
+
+/*
+ * parser
+ */
+
+/** find first not blank char */
+static inline char *skip_blank(char *p)
+{
+ while(isblank(*p))
+ p++;
+ return p;
+}
+
+/** trim out blank chars at the end of a string */
+static inline char *rtrim(const char *s, char *p)
+{
+ if (p>s) {
+ while (p!=s && isblank(*--p))
+ ;
+ *++p='\0';
+ }
+ return p;
+}
+
+/**
+ */
+int script_parse_fex(FILE *in, const char *filename, struct script *script)
+{
+ char buffer[MAX_LINE+1];
+ int ok = 1;
+ struct script_section *last_section = NULL;
+
+ /* TODO: deal with longer lines correctly (specially in comments) */
+ for(size_t line = 1; ok && fgets(buffer, sizeof(buffer), in); line++) {
+ char *s = skip_blank(buffer); /* beginning */
+ char *pe = s; /* \0... to be found */
+
+ if (*pe) while (*++pe)
+ ;
+
+ if (pe>s && pe[-1] == '\n') {
+ if (pe>s+1 && pe[-2] == '\r')
+ pe -= 2;
+ else
+ pe -= 1;
+ *pe = '\0';
+ }
+
+ pe = rtrim(s, pe);
+
+ if (pe == s || *s == ';' || *s == '#')
+ continue; /* empty */
+ 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 == '_')
+ p++;
+
+ if (*p == ']' && *(p+1) == '\0') {
+ *p = '\0';
+ if ((last_section = script_section_new(script, s)))
+ continue;
+
+ perror("malloc");
+ } else if (*p) {
+ errf("E: %s:%zu: invalid character at %zu.\n",
+ filename, line, p-buffer+1);
+ } else {
+ errf("E: %s:%zu: incomplete section declaration.\n",
+ filename, line);
+ }
+ ok = 0;
+ } else {
+ /* key = value */
+ const char *key = s;
+ char *mark, *p = s;
+
+ if (!last_section) {
+ errf("E: %s:%zu: data must follow a section.\n",
+ filename, line);
+ goto parse_error;
+ };
+
+ while (isalnum(*p) || *p == '_')
+ p++;
+ mark = p;
+ p = skip_blank(p);
+ if (*p != '=')
+ goto invalid_char_at_p;
+ *mark = '\0'; /* truncate key */
+ p = skip_blank(p+1);
+
+ if (*p == '\0') {
+ /* NULL */
+ if (script_null_entry_new(last_section, key))
+ continue;
+ perror("malloc");
+ } else if (pe > p+1 && *p == '"' && pe[-1] == '"') {
+ /* string */
+ p++; *--pe = '\0';
+ if (script_string_entry_new(last_section, key, pe-p, p)) {
+ pr_debug("%s.%s = \"%.*s\"\n",
+ last_section->name, key,
+ (int)(pe-p), p);
+ continue;
+ }
+ perror("malloc");
+ } else if (memcmp("port:", p, 5) == 0) {
+ /* GPIO */
+ p += 5;
+ if (p[0] == 'P' &&
+ (p[1] < 'A' || p[1] > ('A' + GPIO_BANK_MAX)))
+ ;
+ else if (*p != 'P' &&
+ memcmp(p, "power", 5) != 0)
+ ;
+ else {
+ char *end;
+ int port;
+ long v;
+
+ if (*p == 'P') {
+ /* port:PXN */
+ port = p[1] - 'A' + 1;
+ p += 2;
+ } else {
+ /* port:powerN */
+ port = 0xffff;
+ p += 5;
+ }
+
+ v = strtol(p, &end, 10);
+ if (end == p)
+ goto invalid_char_at_p;
+ else if (v<0 || v>255) {
+ errf("E: %s:%zu: port out of range at %zu (%ld).\n",
+ filename, line, p-buffer+1, v);
+ } else {
+ int data[] = {-1,-1,-1,-1};
+ int port_num = v;
+ p = end;
+ for (int i=0; *p && i<4; i++) {
+ if (memcmp(p, "<default>", 9) == 0) {
+ p += 9;
+ continue;
+ } else if (*p == '<') {
+ v = strtol(++p, &end, 10);
+ if (end == p) {
+ ;
+ } else if (v<0 || v>INT32_MAX) {
+ errf("E: %s:%zu: value out of range at %zu (%ld).\n",
+ filename, line, p-buffer+1, v);
+ goto parse_error;
+ } else if (*end != '>') {
+ p = end;
+ } else {
+ p = end+1;
+ data[i] = v;
+ continue;
+ }
+ }
+ break;
+ }
+ if (*p)
+ goto invalid_char_at_p;
+ if (script_gpio_entry_new(last_section, key,
+ port, port_num, data)) {
+ pr_debug("%s.%s = GPIO %d.%d (%d,%d,%d,%d)\n",
+ last_section->name, key,
+ port, port_num,
+ data[0], data[1], data[2], data[3]);
+ continue;
+ }
+ perror("malloc");
+ }
+ }
+ } else if (isdigit(*p) || (*p == '-' && isdigit(*(p+1)))) {
+ long long v = 0;
+ char *end;
+ v = strtoll(p, &end, 0);
+ p = end;
+ if (p != pe) {
+ goto invalid_char_at_p;
+ } else if (v > UINT32_MAX) {
+ errf("E: %s:%zu: value out of range %lld.\n",
+ filename, line, v);
+ } else if (script_single_entry_new(last_section, key, v)) {
+ pr_debug("%s.%s = %lld\n",
+ last_section->name, key, v);
+ continue;
+ }
+ } 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",
+ filename, line, p-buffer+1);
+parse_error:
+ ok = 0;
+ }
+ };
+
+ if (ferror(in))
+ ok = 0;
+ return ok;
+}
diff --git a/tools/script_fex.h b/tools/script_fex.h
new file mode 100644
index 0000000..0ada86d
--- /dev/null
+++ b/tools/script_fex.h
@@ -0,0 +1,23 @@
+/*
+ * 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 2 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_FEX_H
+#define _SUBXI_TOOLS_SCRIPT_FEX_H
+
+int script_parse_fex(FILE *in, const char *filename, struct script *script);
+int script_generate_fex(FILE *out, const char *filename, struct script *script);
+
+#endif
diff --git a/tools/script_uboot.c b/tools/script_uboot.c
new file mode 100644
index 0000000..8d5b474
--- /dev/null
+++ b/tools/script_uboot.c
@@ -0,0 +1,256 @@
+/*
+ * 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 2 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 "common.h"
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "script.h"
+#include "script_uboot.h"
+
+#define pr_info(...) errf("fexc-uboot: " __VA_ARGS__)
+#define pr_err(...) errf("E: fexc-uboot: " __VA_ARGS__)
+
+#ifdef DEBUG
+#define pr_debug(...) errf("D: fexc-uboot: " __VA_ARGS__)
+#else
+#define pr_debug(...)
+#endif
+
+struct members {
+ const char *name;
+ const char *translation;
+ int mode;
+};
+#define foreach_member(I, T) for (const struct members *I = T; \
+ I < T+ARRAY_SIZE(T); I++)
+
+/*
+ */
+static inline void out_u32_member(FILE *out, const char *key, int hexa,
+ struct script_single_entry *val)
+{
+ const char *fmt;
+ if (hexa)
+ fmt = "\t.%s = %#x,\n";
+ else
+ fmt = "\t.%s = %u,\n";
+
+ fprintf(out, fmt, key, val->value);
+}
+
+static inline void out_gpio_member(FILE *out, const char *key,
+ struct script_gpio_entry *gpio)
+{
+ fprintf(out, "\t.%s = ", key);
+
+ if (gpio->port == 0xffff)
+ fprintf(out, "GPIO_AXP_CFG(%u", gpio->port_num);
+ else
+ fprintf(out, "GPIO_CFG(%u, %u", gpio->port, gpio->port_num);
+
+ for (const int *p = gpio->data, *pe = p+4; p != pe; p++) {
+ if (*p == -1)
+ fputs(", 0xff", out);
+ else
+ fprintf(out, ", %u", *p);
+ }
+
+ fputs("),\n", out);
+}
+
+static inline void out_null_member(FILE *out, const char *key)
+{
+ fprintf(out, "\t/* %s is NULL */\n", key);
+}
+
+static inline int out_member(FILE *out, const char *key, int mode,
+ struct script_entry *ep)
+{
+ switch (ep->type) {
+ case SCRIPT_VALUE_TYPE_SINGLE_WORD:
+ out_u32_member(out, key, mode,
+ container_of(ep, struct script_single_entry, entry));
+ break;
+ case SCRIPT_VALUE_TYPE_NULL:
+ out_null_member(out, key);
+ break;
+ case SCRIPT_VALUE_TYPE_GPIO:
+ out_gpio_member(out, key,
+ container_of(ep, struct script_gpio_entry, entry));
+ break;
+ default:
+ return 0;
+ }
+ return 1;
+}
+
+/*
+ * DRAM
+ */
+static struct members dram_members[] = {
+ { .name="dram_clock" },
+ { .name="dram_clk", .translation="clock" },
+ { .name="dram_type" },
+ { .name="dram_rank_num" },
+ { .name="dram_density" },
+ { .name="dram_chip_density", .translation="density" },
+ { .name="dram_io_width" },
+ { .name="dram_bus_width" },
+ { .name="dram_cas" },
+ { .name="dram_zq" },
+ { .name="dram_odt_en" },
+ { .name="dram_size" },
+ { .name="dram_tpr0", .mode=1 },
+ { .name="dram_tpr1", .mode=1 },
+ { .name="dram_tpr2", .mode=1 },
+ { .name="dram_tpr3", .mode=1 },
+ { .name="dram_tpr4", .mode=1 },
+ { .name="dram_tpr5", .mode=1 },
+ { .name="dram_emr1", .mode=1 },
+ { .name="dram_emr2", .mode=1 },
+ { .name="dram_emr3", .mode=1 },
+};
+
+static int generate_dram_struct(FILE *out, struct script_section *sp)
+{
+ struct script_entry *ep;
+ const char *key;
+ int ret = 1;
+
+ fprintf(out, "static struct dram_para dram_para = {\n");
+ foreach_member(mp, dram_members) {
+ ep = script_find_entry(sp, mp->name);
+ if (!ep)
+ continue;
+
+ key = (mp->translation) ? mp->translation : mp->name+5;
+ if (!out_member(out, key, mp->mode, ep)) {
+ pr_err("dram_para: %s: invalid field\n", ep->name);
+ ret = 0;
+ }
+
+ }
+ fprintf(out, "};\n");
+ fputs("\nunsigned long sunxi_dram_init(void)\n"
+ "{\n\treturn dramc_init(&dram_para);\n}\n",
+ out);
+
+ return ret;
+}
+
+#if 0
+/*
+ * PMU
+ */
+static struct members pmu_members[] = {
+ { .name = "pmu_used2" },
+ { .name = "pmu_para" },
+ { .name = "pmu_adpdet" },
+ { .name = "pmu_shutdown_chgcur" },
+ { .name = "pmu_shutdown_chgcur2" },
+ { .name = "pmu_pwroff_vol" },
+ { .name = "pmu_pwron_vol" },
+};
+
+static int generate_pmu_struct(FILE *out, struct script_section *target,
+ struct script_section *pmu_para)
+{
+ struct list_entry *le;
+ struct script_section *sp;
+ struct script_entry *ep;
+ const char *key;
+ int ret = 1;
+
+ fputs("\nstatic struct pmu_para pmu_para = {\n", out);
+
+ sp = target;
+ for (le = list_first(&sp->entries); le;
+ le = list_next(&sp->entries, le)) {
+ ep = container_of(le, struct script_entry, entries);
+
+ if (!out_member(out, ep->name, 0, ep)) {
+ pr_err("target: %s: invalid field\n", ep->name);
+ ret = 0;
+ }
+ }
+
+ foreach_member(mp, pmu_members) {
+ ep = script_find_entry(pmu_para, mp->name);
+ if (!ep)
+ continue;
+
+ key = (mp->translation) ? mp->translation : mp->name+4;
+ if (!out_member(out, key, mp->mode, ep)) {
+ pr_err("pmu_para: %s: invalid field\n", mp->name);
+ ret = 0;
+ }
+ }
+
+ fputs("};\n", out);
+ fputs("\nint sunxi_pmu_init(void)\n"
+ "{\n\treturn PMU_init(&pmu_para);\n}\n",
+ out);
+ return ret;
+
+ (void) pmu_para;
+}
+#endif
+
+int script_generate_uboot(FILE *out, const char *UNUSED(filename),
+ struct script *script)
+{
+ struct {
+ const char *name;
+ struct script_section *sp;
+ } sections[] = {
+ { "dram_para", NULL },
+#if 0
+ { "target", NULL },
+ { "pmu_para", NULL },
+#endif
+ };
+
+ for (unsigned i=0; i<ARRAY_SIZE(sections); i++) {
+ struct script_section *sp;
+
+ sp = script_find_section(script, sections[i].name);
+ if (sp)
+ sections[i].sp = sp;
+ else {
+ pr_err("%s: critical section missing",
+ sections[i].name);
+ return 0;
+ }
+ }
+
+ fputs("/* this file is generated, don't edit it yourself */\n\n"
+ "#include <common.h>\n"
+#if 0
+ "#include <asm/arch/pmu.h>\n"
+#endif
+ "#include <asm/arch/dram.h>\n\n",
+ out);
+
+ generate_dram_struct(out, sections[0].sp);
+#if 0
+ generate_pmu_struct(out, sections[1].sp, sections[2].sp);
+#endif
+
+ return 1;
+}
diff --git a/tools/script_uboot.h b/tools/script_uboot.h
new file mode 100644
index 0000000..abfca14
--- /dev/null
+++ b/tools/script_uboot.h
@@ -0,0 +1,22 @@
+/*
+ * 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 2 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_UBOOT_H
+#define _SUBXI_TOOLS_SCRIPT_UBOOT_H
+
+int script_generate_uboot(FILE *out, const char *filename, struct script *script);
+
+#endif