devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] fdtoverlay, an overlay application tool
@ 2017-06-14 14:51 Pantelis Antoniou
       [not found] ` <1497451872-15289-1-git-send-email-pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: Pantelis Antoniou @ 2017-06-14 14:51 UTC (permalink / raw)
  To: David Gibson
  Cc: Tom Rini, Nishanth Menon, Tero Kristo, Frank Rowand, Rob Herring,
	Simon Glass, Devicetree Compiler,
	devicetree-u79uwXL29TY76Z2rM5mHXA, Pantelis Antoniou

libfdt supports overlays, so it's trivial to create a tool
that applies them on a host using binary blobs.

fdtoverlay is that tool which given a base tree and a number
of overlays generates a target blob as the result.

Pantelis Antoniou (2):
  fdtoverlay: A tool that applies overlays
  tests: fdtoverlay unit test

 Documentation/manual.txt    |  14 ++++
 Makefile                    |   5 ++
 Makefile.utils              |   6 ++
 fdtoverlay.c                | 168 ++++++++++++++++++++++++++++++++++++++++++++
 tests/fdtoverlay-runtest.sh |  40 +++++++++++
 tests/run_tests.sh          |  27 ++++++-
 tests/tests.sh              |   1 +
 7 files changed, 260 insertions(+), 1 deletion(-)
 create mode 100644 fdtoverlay.c
 create mode 100644 tests/fdtoverlay-runtest.sh

-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] fdtoverlay: A tool that applies overlays
       [not found] ` <1497451872-15289-1-git-send-email-pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
@ 2017-06-14 14:51   ` Pantelis Antoniou
  2017-06-14 14:51   ` [PATCH 2/2] tests: fdtoverlay unit test Pantelis Antoniou
  2017-06-15  7:40   ` [PATCH 0/2] fdtoverlay, an overlay application tool David Gibson
  2 siblings, 0 replies; 4+ messages in thread
From: Pantelis Antoniou @ 2017-06-14 14:51 UTC (permalink / raw)
  To: David Gibson
  Cc: Tom Rini, Nishanth Menon, Tero Kristo, Frank Rowand, Rob Herring,
	Simon Glass, Devicetree Compiler,
	devicetree-u79uwXL29TY76Z2rM5mHXA, Pantelis Antoniou

Since libfdt support overlay application on FDT blobs, provide
a command line tool that applies an arbitrary number of
overlays, one after another to a base fdt blob and output
the result in the given file.

Signed-off-by: Pantelis Antoniou <pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
---
 Documentation/manual.txt |  14 ++++
 Makefile                 |   5 ++
 Makefile.utils           |   6 ++
 fdtoverlay.c             | 168 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 193 insertions(+)
 create mode 100644 fdtoverlay.c

diff --git a/Documentation/manual.txt b/Documentation/manual.txt
index d4e99e3..72403ac 100644
--- a/Documentation/manual.txt
+++ b/Documentation/manual.txt
@@ -679,3 +679,17 @@ The syntax of the fdtdump command line is:
 Where options are:
     -d,--debug          Dump debug information while decoding the file
     -s,--scan           Scan for an embedded fdt in given file
+
+3) fdtoverlay -- Flat Device Tree overlay applicator
+
+The fdtoverlay applies an arbitrary number of FDT overlays to a base FDT blob
+to a given output file.
+
+The syntax of the fdtoverlay command line is:
+
+    fdtoverlay -i <base-blob> -o <output-blob> <overlay-blob0> [<overlay-blob1> ...]
+
+Where options are:
+    -i, --input         Input base DT blob
+    -o, --output        Output DT blob
+    -v, --verbose       Verbose message output
diff --git a/Makefile b/Makefile
index 43957e6..fe482b9 100644
--- a/Makefile
+++ b/Makefile
@@ -120,6 +120,7 @@ BIN += dtc
 BIN += fdtdump
 BIN += fdtget
 BIN += fdtput
+BIN += fdtoverlay
 
 SCRIPTS = dtdiff
 
@@ -150,6 +151,7 @@ ifneq ($(DEPTARGETS),)
 -include $(FDTDUMP_OBJS:%.o=%.d)
 -include $(FDTGET_OBJS:%.o=%.d)
 -include $(FDTPUT_OBJS:%.o=%.d)
+-include $(FDTOVERLAY_OBJS:%.o=%.d)
 endif
 
 
@@ -226,6 +228,8 @@ fdtget:	$(FDTGET_OBJS) $(LIBFDT_archive)
 
 fdtput:	$(FDTPUT_OBJS) $(LIBFDT_archive)
 
+fdtoverlay: $(FDTOVERLAY_OBJS) $(LIBFDT_archive)
+
 dist:
 	git archive --format=tar --prefix=dtc-$(dtc_version)/ HEAD \
 		> ../dtc-$(dtc_version).tar
@@ -280,6 +284,7 @@ TESTS_BIN += convert-dtsv0
 TESTS_BIN += fdtput
 TESTS_BIN += fdtget
 TESTS_BIN += fdtdump
+TESTS_BIN += fdtoverlay
 ifeq ($(NO_PYTHON),)
 TESTS_PYLIBFDT += maybe_pylibfdt
 endif
diff --git a/Makefile.utils b/Makefile.utils
index 48ece49..e028922 100644
--- a/Makefile.utils
+++ b/Makefile.utils
@@ -22,3 +22,9 @@ FDTPUT_SRCS = \
 	util.c
 
 FDTPUT_OBJS = $(FDTPUT_SRCS:%.c=%.o)
+
+FDTOVERLAY_SRCS = \
+	fdtoverlay.c \
+	util.c
+
+FDTOVERLAY_OBJS = $(FDTOVERLAY_SRCS:%.c=%.o)
diff --git a/fdtoverlay.c b/fdtoverlay.c
new file mode 100644
index 0000000..9c5618c
--- /dev/null
+++ b/fdtoverlay.c
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2017 Konsulko Group Inc. All rights reserved.
+ *
+ * Author:
+ *	 Pantelis Antoniou <pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
+ *
+ * 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., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <assert.h>
+#include <ctype.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <alloca.h>
+
+#include <libfdt.h>
+
+#include "util.h"
+
+/* Usage related data. */
+static const char usage_synopsis[] =
+	"apply a number of overlays to a base blob\n"
+	"	fdtoverlay <options> [<overlay.dtbo> [<overlay.dtbo>]]\n"
+	"\n"
+	USAGE_TYPE_MSG;
+static const char usage_short_opts[] = "i:o:v" USAGE_COMMON_SHORT_OPTS;
+static struct option const usage_long_opts[] = {
+	{"input",            required_argument, NULL, 'i'},
+	{"output",	     required_argument, NULL, 'o'},
+	{"verbose",	           no_argument, NULL, 'v'},
+	USAGE_COMMON_LONG_OPTS,
+};
+static const char * const usage_opts_help[] = {
+	"Input base DT blob",
+	"Output DT blob",
+	"Verbose messages",
+	USAGE_COMMON_OPTS_HELP
+};
+
+int verbose = 0;
+
+static int do_fdtoverlay(const char *input_filename,
+			 const char *output_filename,
+			 int argc, char *argv[])
+{
+	char *blob = NULL;
+	char **ovblob = NULL;
+	off_t blob_len, ov_len, total_len;
+	int i, ret = -1;
+
+	blob = utilfdt_read_len(input_filename, &blob_len);
+	if (!blob) {
+		fprintf(stderr, "\nFailed to read base blob %s\n",
+				input_filename);
+		goto out_err;
+	}
+	ret = 0;
+
+	/* allocate blob pointer array */
+	ovblob = alloca(sizeof(*ovblob) * argc);
+	memset(ovblob, 0, sizeof(*ovblob) * argc);
+
+	/* read and keep track of the overlay blobs */
+	total_len = 0;
+	for (i = 0; i < argc; i++) {
+		ovblob[i] = utilfdt_read_len(argv[i], &ov_len);
+		if (!ovblob[i]) {
+			fprintf(stderr, "\nFailed to read overlay %s\n",
+					argv[i]);
+			goto out_err;
+		}
+		total_len += ov_len;
+	}
+
+	/* grow the blob to worst case */
+	blob_len = fdt_totalsize(blob) + total_len;
+	blob = xrealloc(blob, blob_len);
+	fdt_open_into(blob, blob, blob_len);
+
+	/* apply the overlays in sequence */
+	for (i = 0; i < argc; i++) {
+		ret = fdt_overlay_apply(blob, ovblob[i]);
+		if (ret) {
+			fprintf(stderr, "\nFailed to apply %s (%d)\n",
+					argv[i], ret);
+			goto out_err;
+		}
+	}
+
+	fdt_pack(blob);
+	ret = utilfdt_write(output_filename, blob);
+	if (ret)
+		fprintf(stderr, "\nFailed to write output blob %s\n",
+				output_filename);
+
+out_err:
+	if (ovblob) {
+		for (i = 0; i < argc; i++) {
+			if (ovblob[i])
+				free(ovblob[i]);
+		}
+	}
+	free(blob);
+
+	return ret;
+}
+
+int main(int argc, char *argv[])
+{
+	int opt, i;
+	char *input_filename = NULL;
+	char *output_filename = NULL;
+
+	while ((opt = util_getopt_long()) != EOF) {
+		switch (opt) {
+		case_USAGE_COMMON_FLAGS
+
+		case 'i':
+			input_filename = optarg;
+			break;
+		case 'o':
+			output_filename = optarg;
+			break;
+		case 'v':
+			verbose = 1;
+			break;
+		}
+	}
+
+	if (!input_filename)
+		usage("missing input file");
+
+	if (!output_filename)
+		usage("missing output file");
+
+	argv += optind;
+	argc -= optind;
+
+	if (argc <= 0)
+		usage("missing overlay file(s)");
+
+	if (verbose) {
+		printf("input  = %s\n", input_filename);
+		printf("output = %s\n", output_filename);
+		for (i = 0; i < argc; i++)
+			printf("overlay[%d] = %s\n", i, argv[i]);
+	}
+
+	if (do_fdtoverlay(input_filename, output_filename, argc, argv))
+		return 1;
+
+	return 0;
+}
-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] tests: fdtoverlay unit test
       [not found] ` <1497451872-15289-1-git-send-email-pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
  2017-06-14 14:51   ` [PATCH 1/2] fdtoverlay: A tool that applies overlays Pantelis Antoniou
@ 2017-06-14 14:51   ` Pantelis Antoniou
  2017-06-15  7:40   ` [PATCH 0/2] fdtoverlay, an overlay application tool David Gibson
  2 siblings, 0 replies; 4+ messages in thread
From: Pantelis Antoniou @ 2017-06-14 14:51 UTC (permalink / raw)
  To: David Gibson
  Cc: Tom Rini, Nishanth Menon, Tero Kristo, Frank Rowand, Rob Herring,
	Simon Glass, Devicetree Compiler,
	devicetree-u79uwXL29TY76Z2rM5mHXA, Pantelis Antoniou

Add an fdtoverlay unit test. Applies applies overlay(s) and then
retrieves an inserted property to verify.

Signed-off-by: Pantelis Antoniou <pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
---
 tests/fdtoverlay-runtest.sh | 40 ++++++++++++++++++++++++++++++++++++++++
 tests/run_tests.sh          | 27 ++++++++++++++++++++++++++-
 tests/tests.sh              |  1 +
 3 files changed, 67 insertions(+), 1 deletion(-)
 create mode 100644 tests/fdtoverlay-runtest.sh

diff --git a/tests/fdtoverlay-runtest.sh b/tests/fdtoverlay-runtest.sh
new file mode 100644
index 0000000..06c1169
--- /dev/null
+++ b/tests/fdtoverlay-runtest.sh
@@ -0,0 +1,40 @@
+#! /bin/sh
+
+# Run script for fdtoverlay tests
+# We run fdtoverlay to generate a target device tree, thn fdtget to check it
+
+# Usage
+#    fdtoverlay-runtest.sh name expected_output dtb_file node property flags value
+
+. ./tests.sh
+
+LOG=tmp.log.$$
+EXPECT=tmp.expect.$$
+rm -f $LOG $EXPECT
+trap "rm -f $LOG $EXPECT" 0
+
+expect="$1"
+echo $expect >$EXPECT
+node="$2"
+property="$3"
+flags="$4"
+basedtb="$5"
+targetdtb="$6"
+shift 6
+overlays="$@"
+
+# First run fdtoverlay
+verbose_run_check $VALGRIND "$FDTOVERLAY" -i "$basedtb" -o "$targetdtb" $overlays
+
+# Now fdtget to read the value
+verbose_run_log_check "$LOG" $VALGRIND "$DTGET" "$targetdtb" "$node" "$property" $flags
+
+if cmp $EXPECT $LOG >/dev/null; then
+    PASS
+else
+    if [ -z "$QUIET_TEST" ]; then
+	echo "EXPECTED :-:"
+	cat $EXPECT
+    fi
+    FAIL "Results differ from expected"
+fi
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 5df1480..d20729c 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -160,6 +160,14 @@ run_fdtdump_test() {
     base_run_test sh fdtdump-runtest.sh "$file" 2>/dev/null
 }
 
+run_fdtoverlay_test() {
+    expect="$1"
+    shift
+    shorten_echo fdtoverlay-runtest.sh "$expect" "$@"
+    printf ":	"
+    base_run_test sh fdtoverlay-runtest.sh "$expect" "$@"
+}
+
 BAD_FIXUP_TREES="bad_index \
 		empty \
 		empty_index \
@@ -771,6 +779,20 @@ fdtdump_tests () {
     run_fdtdump_test fdtdump.dts
 }
 
+fdtoverlay_tests() {
+    base=overlay_base.dts
+    basedtb=overlay_base.fdoverlay.test.dtb
+    overlay=overlay_overlay_manual_fixups.dts
+    overlaydtb=overlay_overlay_manual_fixups.fdoverlay.test.dtb
+    targetdtb=target.fdoverlay.test.dtb
+
+    run_dtc_test -@ -I dts -O dtb -o $basedtb $base
+    run_dtc_test -@ -I dts -O dtb -o $overlaydtb $overlay
+
+    # test that the new property is installed
+    run_fdtoverlay_test foobar "/test-node" "test-str-property" "-ts" ${basedtb} ${targetdtb} ${overlaydtb}
+}
+
 pylibfdt_tests () {
     TMP=/tmp/tests.stderr.$$
     python pylibfdt_tests.py -v 2> $TMP
@@ -809,7 +831,7 @@ while getopts "vt:me" ARG ; do
 done
 
 if [ -z "$TESTSETS" ]; then
-    TESTSETS="libfdt utilfdt dtc dtbs_equal fdtget fdtput fdtdump"
+    TESTSETS="libfdt utilfdt dtc dtbs_equal fdtget fdtput fdtdump fdtoverlay"
 
     # Test pylibfdt if the libfdt Python module is available.
     if [ -f ../pylibfdt/_libfdt.so ]; then
@@ -846,6 +868,9 @@ for set in $TESTSETS; do
 	"pylibfdt")
 	    pylibfdt_tests
 	    ;;
+        "fdtoverlay")
+	    fdtoverlay_tests
+	    ;;
     esac
 done
 
diff --git a/tests/tests.sh b/tests/tests.sh
index 818fd09..8dda6e1 100644
--- a/tests/tests.sh
+++ b/tests/tests.sh
@@ -22,6 +22,7 @@ DTC=../dtc
 DTGET=../fdtget
 DTPUT=../fdtput
 FDTDUMP=../fdtdump
+FDTOVERLAY=../fdtoverlay
 
 verbose_run () {
     if [ -z "$QUIET_TEST" ]; then
-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] fdtoverlay, an overlay application tool
       [not found] ` <1497451872-15289-1-git-send-email-pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
  2017-06-14 14:51   ` [PATCH 1/2] fdtoverlay: A tool that applies overlays Pantelis Antoniou
  2017-06-14 14:51   ` [PATCH 2/2] tests: fdtoverlay unit test Pantelis Antoniou
@ 2017-06-15  7:40   ` David Gibson
  2 siblings, 0 replies; 4+ messages in thread
From: David Gibson @ 2017-06-15  7:40 UTC (permalink / raw)
  To: Pantelis Antoniou
  Cc: Tom Rini, Nishanth Menon, Tero Kristo, Frank Rowand, Rob Herring,
	Simon Glass, Devicetree Compiler,
	devicetree-u79uwXL29TY76Z2rM5mHXA

[-- Attachment #1: Type: text/plain, Size: 1123 bytes --]

On Wed, Jun 14, 2017 at 05:51:10PM +0300, Pantelis Antoniou wrote:
> libfdt supports overlays, so it's trivial to create a tool
> that applies them on a host using binary blobs.
> 
> fdtoverlay is that tool which given a base tree and a number
> of overlays generates a target blob as the result.

Applied, thanks.

> 
> Pantelis Antoniou (2):
>   fdtoverlay: A tool that applies overlays
>   tests: fdtoverlay unit test
> 
>  Documentation/manual.txt    |  14 ++++
>  Makefile                    |   5 ++
>  Makefile.utils              |   6 ++
>  fdtoverlay.c                | 168 ++++++++++++++++++++++++++++++++++++++++++++
>  tests/fdtoverlay-runtest.sh |  40 +++++++++++
>  tests/run_tests.sh          |  27 ++++++-
>  tests/tests.sh              |   1 +
>  7 files changed, 260 insertions(+), 1 deletion(-)
>  create mode 100644 fdtoverlay.c
>  create mode 100644 tests/fdtoverlay-runtest.sh
> 

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-06-15  7:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-06-14 14:51 [PATCH 0/2] fdtoverlay, an overlay application tool Pantelis Antoniou
     [not found] ` <1497451872-15289-1-git-send-email-pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
2017-06-14 14:51   ` [PATCH 1/2] fdtoverlay: A tool that applies overlays Pantelis Antoniou
2017-06-14 14:51   ` [PATCH 2/2] tests: fdtoverlay unit test Pantelis Antoniou
2017-06-15  7:40   ` [PATCH 0/2] fdtoverlay, an overlay application tool David Gibson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).