Commit graph

153 commits

Author SHA1 Message Date
Geert Uytterhoeven
ea7a8f6dad libfdt: Fix FDT_ERR_NOTFOUND typos in documentation
The actual error is FDT_ERR_NOTFOUND, not FDT_ERR_NOT_FOUND.

Fixes: d29126c90a ("libfdt: Add iterator over properties")
Fixes: 902d0f0953 ("libfdt: Add a subnodes iterator macro")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-12-23 13:34:57 +11:00
Geert Uytterhoeven
5aafd7ca43 libfdt: Fix fdt_getprop_by_offset() parameter name in documentation
The second parameter of fdt_getprop_by_offset() is called "offset", not
"ffset".

Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-12-22 00:02:58 +11:00
David Gibson
f9c0a425b6 Remove broken objdir / srcdir support
The dtc makefiles have support for building into a separate directory from
the sources... except that it's broken and probably always has been.

Remove the pretense.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-11-23 21:58:42 +11:00
David Gibson
a4629cfaed Refine libfdt_clean target
Move it to the libfdt Makefile piece, use neater make syntax, and remove
redundant command (already included in STD_CLEANFILES).

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-11-23 21:11:47 +11:00
David Gibson
11738cf01f libfdt: Don't use memcpy to handle unaligned reads on ARM
6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced
the fdt32_ld() and fdt64_ld() helpers for loading values from the FDT blob
which might not be naturally aligned.  This matters for ARM, where
attempting a plain unaligned load will often cause an exception.

However, it seems the memcpy() we used here was surprisingly expensive,
making libfdt nearly 6x slower on at least some ARM platforms.

This patch takes an alternative approach, using a bunch of 1-byte loads
and shifts to implement the helpers.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-11-22 23:37:07 +11:00
John Clarke
aa7254d9cb libfdt: return correct value if #size-cells property is not present
According to the device tree specification, the default value for
#size-cells is 1, but fdt_size_cells() was returning 2 if this property
was not present.

This patch also makes fdt_address_cells() and fdt_size_cells() conform
to the behaviour documented in libfdt.h. The defaults are only returned
if fdt_getprop() returns -FDT_ERR_NOTFOUND, otherwise the actual error
is returned.

Signed-off-by: John Clarke <johnc@kirriwa.net>
2018-11-07 14:13:12 +11:00
Sebastian Huber
c12b2b0c20 libfdt: fdt_address_cells() and fdt_size_cells()
Add internal fdt_cells() to avoid copy and paste.  Test error cases and
default values.  Fix typo in fdt_size_cells() documentation comment.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-07-20 11:56:49 +10:00
David Gibson
1087504bb3 libfdt: Add necessary header padding in fdt_create()
At present fdt_create() will succeed if there is exactly enough space to
put in the fdt header.  However, it sets the off_mem_rsvmap field, a few
bytes past that in order to align the memory reservation block.

Having block pointers pointing past the end of the fdt is pretty ugly, even
if it is just a transient state.  Worse, if fdt_resize() is called at
exactly the wrong time, it can end up accessing data past the blob's
allocated space because of this.

So, correct fdt_create() to ensure that there is sufficient space for the
alignment padding as well as the plain header.  For paranoia, also add a
check in fdt_resize() to make sure we don't copy data from outside the
blob's bounds.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-07-09 15:03:14 +10:00
Simon Glass
c72fa777e6 libfdt: Copy the struct region in fdt_resize()
At present this function appears to copy only the data before the struct
region and the data in the string region. It does not seem to copy the
struct region itself.

From the arguments of this function it seems that it should support fdt
and buf being different. This patch attempts to fix this problem.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-07-09 15:03:14 +10:00
David Gibson
6dcb8ba408 libfdt: Add helpers for accessing unaligned words
This adds some helpers to load (32 or 64 bit) words from an fdt blob, even
if they're unaligned and we're on a platform that doesn't like plain
unaligned loads and stores.  We then use the helpers in a number of places.
There are two purposes for this:

1) This makes libfdt more robust against a blob loaded at an unaligned
   address.  It's usually good practice to load a blob at a 64-bit
   alignment, but it's nice to work even then.

2) Users can use these helpers to load integer values from within property
   values.  These can often be unaligned, even if the blob as a whole is
   aligned, since some property encodings have integers and strings mixed
   together without any alignment gaps.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-26 15:09:46 +10:00
Simon Glass
9aafa33d99 pylibfdt: Add functions to update properties
Allow updating and creating properties, including special methods for
integers.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-06-12 14:44:20 +10:00
David Gibson
4b8fcc3d01 libfdt: Add fdt_check_full() function
This new function implements a complete and thorough check of an fdt blob's
structure.  Given a buffer containing an fdt, it should return 0 only if
the fdt within is structurally sound in all regards.  It doesn't check
anything about the blob's contents (i.e. the actual values of the nodes and
properties), of course.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Tested-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Simon Glass <sjg@chromium.org>
2018-06-07 11:49:17 +10:00
David Gibson
0112fda03b libfdt: Add fdt_header_size()
We have a couple of places within libfdt and its tests where we need to
find the size of the header, based on the version.  Add a helper function
for it.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
2018-06-07 11:49:14 +10:00
David Gibson
d5db5382c5 libfdt: Safer access to memory reservations
fdt_num_mem_rsv() and fdt_get_mem_rsv() currently don't sanity check their
parameters, or the memory reserve section offset in the header.  That means
that on a corrupted blob they could access outside of the range of memory
that they should.

This improves their safety checking, meaning they shouldn't access outside
the blob's bounds, even if its contents are badly corrupted.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Tested-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Simon Glass <sjg@chromium.org>
2018-06-07 11:48:20 +10:00
David Gibson
719d582e98 libfdt: Propagate name errors in fdt_getprop_by_offset()
fdt_getprop_by_offset() doesn't check for errors from fdt_string() - after
all, until very recently it couldn't fail.  Now it can, so we need to
propagate errors up to the caller.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Tested-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Simon Glass <sjg@chromium.org>
2018-06-07 11:48:20 +10:00
David Gibson
70166d62a2 libfdt: Safer access to strings section
fdt_string() is used to retrieve strings from a DT blob's strings section.
It's rarely used directly, but is widely used internally.

However, it doesn't do any bounds checking, which means in the case of a
corrupted blob it could access bad memory, which libfdt is supposed to
avoid.

This write a safe alternative to fdt_string, fdt_get_string().  It checks
both that the given offset is within the string section and that the string
it points to is properly \0 terminated within the section.  It also returns
the string's length as a convenience (since it needs to determine to do the
checks anyway).

fdt_string() is rewritten in terms of fdt_get_string() for compatibility.

Most of the diff here is actually testing infrastructure.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Tested-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
2018-06-07 11:48:20 +10:00
David Gibson
eb890c0f77 libfdt: Make fdt_check_header() more thorough
Currently fdt_check_header() performs only some rudimentary checks, which
is not really what the name suggests.  This strengthens fdt_check_header()
to check as much about the blob as is possible from the header alone:  as
well as checking the magic number and version, it checks that the total
size is sane, and that all the sub-blocks within the blob lie within the
total size.

 * This broadens the meaning of FDT_ERR_TRUNCATED to cover all sorts of
   improperly terminated blocks as well as just a structure block without
   FDT_END.

 * This makes fdt_check_header() only succeed on "complete" blobs, not
   in-progress sequential write blobs.  The only reason this didn't fail
   before was that this function used to be called by many RO functions
   which are supposed to also work on incomplete SW blobs.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Tested-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Simon Glass <sjg@chromium.org>
2018-06-07 11:47:59 +10:00
David Gibson
899d6fad93 libfdt: Improve sequential write state checking
When creating a tree with the sequential write functions, certain things
have to be done in a certain order.  You must create the memory reserve map
and only then can you create the actual tree structure.

The -FDT_ERR_BADSTATE return code is for if you try to do things out of
order.  However, we weren't checking that very thoroughly, so it was
possible to generate a corrupted blob if, for example, you started calling
fdt_begin_node() etc. before calling fdt_finish_reservemap().

This makes the state checking more thorough disallow that.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Tested-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Simon Glass <sjg@chromium.org>
2018-06-07 11:45:48 +10:00
David Gibson
04b5b4062c libfdt: Clean up header checking functions
Many of the libfdt entry points call some sort of sanity check function
before doing anything else.  These need to do slightly different things for
the various classes of functions.

The read-only version is shared with the exported fdt_check_header(), which
limits us a bit in how we can improve it.  For that reason split the two
functions apart (though the exported one just calls the ro one for now).

We also rename the functions for more consistency - they're all named
fdt_XX_probe_() where the XX indicates which class of functions they're
for.  "probe" is a better "term" than the previous check, since they really
only do minimal validation.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Tested-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
2018-06-07 11:45:44 +10:00
Nathan Whitehorn
f1879e1a50 Add limited read-only support for older (V2 and V3) device tree to libfdt.
This can be useful in particular in the kernel when booting on systems
with FDT-emitting firmware that is out of date. Releases of kexec-tools
on ppc64 prior to the end of 2014 are notable examples of such.

Signed-off-by: Nathan Whitehorn <nwhitehorn@freebsd.org>
[dwg: Some whitespace cleanups]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-01-27 18:43:21 +11:00
Maxime Ripard
65893da4ae libfdt: overlay: Add missing license
The overlay support has been introduced, but the copyright and license
header was missing. Make sure that this is no longer the case.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-01-10 19:56:14 +11:00
John Arbuckle
c8f8194d76 implement strnlen for systems that need it
Prior the Mac OS 10.7, the function strnlen() was not available. This patch
implements strnlen() on Mac OS X versions that are below 10.7.

Signed-off-by: John Arbuckle <programmingkidx@gmail.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-10-27 15:47:52 +02:00
David Gibson
c8b38f65fd libfdt: Remove leading underscores from identifiers
In a lot of places libfdt uses a leading _ character to mark an identifier
as "internal" (not part of the published libfdt API).  This is a bad idea,
because identifiers with a leading _ are generally reserved by the C
library or system.  It's particularly dangerous for libfdt, because it's
designed to be able to be integrated into lots of different environments.

In some cases the leading _ has no purpose, so we simply drop it.  In most
cases we move it to the end, as our new convention for marking internal
identifiers.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-10-26 09:25:14 +02:00
David Gibson
2e6fe5a107 Fix some errors in comments
A comment in tests/stringlist.c refers to fdt_get_string(), which is not a
function that exists.  From the content, it's supposed to be referring to
fdt_getprop_string().

A comment in libfdt.h has an extraneous space in a function name.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-10-26 09:25:14 +02:00
Reiner Huober
48c91c08bc libfdt: add stringlist functions to linker script
The fdt_stringlist_count, fdt_stringslist_search, and fdt_stringlist_get
are added to the libfdt linker script as global symbols

Signed-off-by: Reiner Huober <reiner.huober@nokia.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-10-05 17:33:54 +11:00
Pantelis Antoniou
1bb00655d3 fdt: Allow stacked overlays phandle references
This patch enables an overlay to refer to a previous overlay's
labels by performing a merge of symbol information at application
time.

In a nutshell it allows an overlay to refer to a symbol that a previous
overlay has defined. It requires both the base and all the overlays
to be compiled with the -@ command line switch so that symbol
information is included.

base.dts
--------

	/dts-v1/;
	/ {
		foo: foonode {
			foo-property;
		};
	};

	$ dtc -@ -I dts -O dtb -o base.dtb base.dts

bar.dts
-------

	/dts-v1/;
	/plugin/;
	/ {
		fragment@1 {
			target = <&foo>;
			__overlay__ {
				overlay-1-property;
				bar: barnode {
					bar-property;
				};
			};
		};
	};

	$ dtc -@ -I dts -O dtb -o bar.dtb bar.dts

baz.dts
-------

	/dts-v1/;
	/plugin/;
	/ {
		fragment@1 {
			target = <&bar>;
			__overlay__ {
				overlay-2-property;
				baz: baznode {
					baz-property;
				};
			};
		};
	};

	$ dtc -@ -I dts -O dtb -o baz.dtb baz.dts

Applying the overlays:

	$ fdtoverlay -i base.dtb -o target.dtb bar.dtb baz.dtb

Dumping:

	$ fdtdump target.dtb
	/ {
            foonode {
                overlay-1-property;
                foo-property;
                linux,phandle = <0x00000001>;
                phandle = <0x00000001>;
                barnode {
                    overlay-2-property;
                    phandle = <0x00000002>;
                    linux,phandle = <0x00000002>;
                    bar-property;
                    baznode {
                        phandle = <0x00000003>;
                        linux,phandle = <0x00000003>;
                        baz-property;
                    };
                };
            };
            __symbols__ {
                baz = "/foonode/barnode/baznode";
                bar = "/foonode/barnode";
                foo = "/foonode";
            };
	};

Signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-08-09 17:18:47 +10:00
Pantelis Antoniou
a33c2247ac Introduce fdt_setprop_placeholder() method
In some cases you need to add a property but the contents of it
are not known at creation time, merely the extend of it.

This method allows you to create a property of a given size (filled
with garbage) while a pointer to the property data will be provided.

Signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
[dwg: Corrected commit message]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-07-28 14:34:36 +10:00
Simon Glass
9067ee4be0 Fix a few whitespace and style nits
These were noticed when synching with U-Boot's downstream tree.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-04-09 20:56:37 +10:00
Simon Glass
580a9f6c28 Add a libfdt function to write a property placeholder
The existing function to add a new property to a tree being built requires
that the entire contents of the new property be passed in. For some
applications it is more convenient to be able to add the property contents
later, perhaps by reading from a file. This avoids double-buffering of the
contents.

Add a new function to support this and adjust the existing fdt_property() to
use it.
Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-04-02 13:38:46 +10:00
Simon Glass
8cb3896358 Adjust libfdt.h to work with swig
There are a few places where libfdt.h cannot be used as is with swig:

- macros like fdt_totalsize() have to be defined as C declarations
- fdt_offset_ptr() and fdt_getprop_namelen() need special treatment due to
    a TODO in the wrapper for fdt_getprop(). However they are not useful to
    Python so can be removed

Add #ifdefs to work around these problem.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-03-21 16:21:58 +11:00
Nicolas Iooss
921cc17fec libfdt: overlay: Check the value of the right variable
overlay_update_local_node_references() saves the result of
fdt_subnode_offset() into variable tree_child but checks for variable
ret afterwards. As this does not make sense, check tree_child instead of
ret.

This bug has been found by compiling with clang. The compiler reported
the following warning:

    libfdt/fdt_overlay.c:275:7: error: variable 'ret' may be
    uninitialized when used here
          [-Werror,-Wconditional-uninitialized]
                    if (ret == -FDT_ERR_NOTFOUND)
                        ^~~
    libfdt/fdt_overlay.c:210:9: note: initialize the variable 'ret' to
    silence this
          warning
            int ret;
                   ^
                    = 0

Signed-off-by: Nicolas Iooss <nicolas.iooss_linux@m4x.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-03-06 14:19:26 +11:00
David Gibson
881012e443 libfdt: Change names of sparse helper macros
The default libfdt_env.h (for POSIXish userland builds) supports sparse
checking.  It has a couple of helper macros, __force and __bitwise which
expand the relevant sparse attributes to enable checking for incorrect
or missing endian conversions.

Those are bad names: for one, leading underscores are supposed to be
reserved for the system libraries, and worse, some systems (including
RHEL7) do define those names already.

So change them to FDT_FORCE and FDT_BITWISE which are far less likely to
have collisions.

Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-03-06 12:16:56 +11:00
David Gibson
bad5b28049 Fix assorted sparse warnings
This fixes a great many sparse warnings on the fdt and libfdt sources.
These are mostly due to incorrect mixing of endian annotated and native
integer types.

This includes fixing a couple of quasi-bugs where we had endian conversions
the wrong way around (this will have the right effect in practice, but is
certainly conceptually incorrect).

This doesn't make the whole tree sparse clean: there are many warnings in
bison and lex generated code, and there are a handful of other remaining
warnings that are (for now) more trouble than they're worth to fix (and
are not genuine bugs).

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-03-06 12:08:53 +11:00
David Gibson
397d5ef020 libfdt: Add fdt_setprop_empty()
Device trees can contain empty (zero length) properties, which are often
used as boolean flags.  These can already be created using fdt_setprop()
passing a length of zero and a pointer which is ignored.  It is safe to
pass NULL, but that may not be obvious from the interface.  To make it
clearer, add an fdt_setprop_empty() helper macro.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-02-24 11:12:50 +11:00
David Gibson
69a1bd6ad3 libfdt: Remove undefined behaviour setting empty properties
The standard way of setting an empty property using libfdt is:
    fdt_setprop(fdt, nodeoffset, propname, NULL, 0);
However, the implementation of this includes an unconditional:
     memcpy(prop->data, NULL, 0);
Which although it will be a no-op (which is what we want) on many platforms
is technically undefined behaviour.  Correct this, so that when passing
a 0 length, passing a NULL pointer as the value to fdt_setprop() is
definitely safe.  This should quiet static checkers which complain about
this.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2017-02-24 10:51:05 +11:00
David Gibson
7d8ef6e1db tests: Correct fdt handling of overlays without fixups and base trees without symbols
The fdt_overlay_apply() function purports to support the edge cases where
an overlay has no fixups to be applied, or a base tree which has no
symbols (the latter can only work if the former is also true).  However it
gets it wrong in a couple of small ways:

  * In the no fixups case, it doesn't fail immediately, but will attempt
    fdt_for_each_property_offset() giving -FDT_ERR_NOTFOUND as the node
    offset, which will fail.  Instead it should succeed immediately, since
    there's nothing to do.
  * In the case of no symbols, it again doesn't fail immediately.  However
    if there is an actual fixup it will fail with an unexpected error,
    because -FDT_ERR_NOTFOUND is passed to fdt_getprop() when attempting to
    look up the symbols.  We should instead return -FDT_ERR_NOTFOUND
    directly.

Both of these errors lead to the code returning misleading error codes in
failing cases.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-12-09 16:13:29 +11:00
Benjamin Fair
ea10f95387 libfdt: add missing errors to fdt_strerror()
Some error values were missing from the table which meant that they could
not be translated by fdt_strerror().

Signed-off-by: Benjamin Fair <b-fair@ti.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-11-04 07:17:20 +11:00
Masahiro Yamada
daa75e8fa5 libfdt: fix fdt_stringlist_search()
If fdt_getprop() fails, negative error code should be returned.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-10-17 20:56:58 +11:00
Masahiro Yamada
e28eff5b78 libfdt: fix fdt_stringlist_count()
If fdt_getprop() fails, negative error code should be returned.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-10-17 20:56:58 +11:00
Maxime Ripard
5ce8634733 libfdt: Add fdt_overlay_apply to the exported symbols
fdt_overlay_apply was not usable in the shared library. Export it to allow
its use.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-10-11 20:08:38 +11:00
Maxime Ripard
804a9db90a fdt: strerr: Remove spurious BADOVERLAY
There's one FDT_ERR_BADOVERLAY too many in the fdt error table.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-10-11 20:08:38 +11:00
Maxime Ripard
7a72d89d3f libfdt: overlay: Fix symbols and fixups nodes condition
Some base device tree might not have any __symbols__ nodes, since they
might not have any phandle at all.

Similarly, if an overlay doesn't use any base device tree phandles, its
__fixups__ node will be empty.

In such cases, we don't want to stop the phandle parsing, but rather just
ignore the error reported about the missing node.

If it's actually an issue for the overlay we're trying to apply on a given
base device tree, it will be caught later on, but we cannot make the
assumption that early in the application process.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-10-07 12:28:58 +11:00
Maxime Ripard
cabbaa972c libfdt: overlay: Report a bad overlay for mismatching local fixups
The __local_fixups__ node as a structure that mimics the structure of the
main overlay part.

This means that if we have a child node somewhere in the local fixups
sub-tree and if that node is not present in the main tree, the overlay is
poorly formatted, and we should report it as such.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-10-07 12:28:58 +11:00
Maxime Ripard
deb0a5c1ae libfdt: Add BADPHANDLE error string
The BADPHANDLE error was missing a string, leading to an <unknown error>
string being returned if you were to call fdt_strerror.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-10-07 12:28:58 +11:00
David Gibson
7b7a6be9ba libfdt: Don't use 'index' as a local variable name
Using 'index' as a local variable name shadows the standard library index()
function.  This causes warnings on at least some compiler versions.  The
recently added overlay code has a number of instances of this.

This patch replaces 'index' with 'poffset', since 'index' is being used to
mean "offset within a property value" in these cases.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-10-06 20:45:14 +11:00
Maxime Ripard
0cdd06c513 libfdt: Add overlay application function
The device tree overlays are a good way to deal with user-modifyable
boards or boards with some kind of an expansion mechanism where we can
easily plug new board in (like the BBB, the Raspberry Pi or the CHIP).

Add a new function to merge overlays with a base device tree.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-10-06 19:22:41 +11:00
Maxime Ripard
39240cc865 libfdt: Extend the reach of FDT_ERR_BADPHANDLE
So far, the BADPHANDLE error was only used for incorrect phandle values.
Extend that meaning to an improperly formatted phandle property.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-10-06 19:22:41 +11:00
Maxime Ripard
4aa3a6f5e6 libfdt: Add new errors for the overlay code
Add a few new error codes to report the failure conditions we might
encounter in the overlay application code:
   - FDT_ERR_BADOVERLAY, when an overlay cannot be parsed, even though its
     structure is correct
   - FDT_ERR_NOPHANDLES, when we ran out of available phandles and we
     cannot use a new phandle without either using an invalid one (-1 or
     0), or one already used.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-10-06 19:22:41 +11:00
Thomas Huth
45fd440a95 Fix some typing errors in libfdt.h and livetree.c
Correct some typos discovered with the codespell utility.

Signed-off-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-09-27 11:32:46 +10:00
Masahiro Yamada
36fd7331fb libfdt: simplify fdt_del_mem_rsv()
The variable "err" is unneeded.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-08-22 06:53:54 -04:00