Commit graph

565 commits

Author SHA1 Message Date
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
aea8860d83 tests: Add tests cases for the overlay code
Add some test infrastructure to test that the overlay can be merged, but
also that poorly formatted fixups would fail as expected.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
[dwg: Don't execute bad overlay tests without overlay aware dtc]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-10-06 20:20:02 +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
David Gibson
6d1832c9e6 dtc: Remove "home page" link
The supposed home page hasn't existed for some time.  We don't really have
anything else, so remove the link from the README.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-09-28 11:29:07 +10: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
David Gibson
a59be4939c DTC 1.4.2
Boring release.  This has a few bugfixes, and most importantly, it
 includes a number of symbols in version.lds which should have been
 exported all along, but were left out previously.
 -----BEGIN PGP SIGNATURE-----
 
 iQIcBAABAgAGBQJXyq4AAAoJEGw4ysog2bOSSxkQAKSY9mTfceT5Hvtf7/wkf9IS
 Q1qdmaA8W1zKUPDHlzgIQ44Nz9m2+MVhl3nzlIDsjbAjLZlOfIEaBzmXUDE8MVPv
 wCN+dEcMupN8nTFxeu72/69IXgpKZuNsMRGadiakvH4ja7X2TGmAgLAsAxNndaje
 CPcPPZ50I3WCK/z80b3gc1M1Un/TxvZ++npdTJUidi/+UkokVSR4MDOp95uDPbFl
 rgqUUuUPUx8gI65BX20UFWWj7PE/WIFsPDNRH9jXlHnd3Vs8QC4gxE1N34K64wl2
 BZaUGBOVQisBn8Hx6q1s66T5J1VdMhBvy8vIiZr9CMSEl4cWe3ngxuRXnYWPz0Xz
 /rHUSp+RVu0Zy5YWIr9c5+tJUPlKeb6zaycSNOlYV9WXvD9G2Ho6kVQIUxALMamj
 5Lv4Kq5GwyofQuSOPlNVC/8m9akDMj0NPKWW7O6e1ihFTAq7XudwOBbm2+MdXmSG
 dGHAxSLLCiFBnRbGt64KasAY6AfoHvswK5WCYr1+nAnhz15wksWpGp3qD+KITPzP
 NogJcoqwYSV/YPPuKwThTgnJFw95SLOzfkXpsGSObP40TTisTSnHeHF6Tb3EXSW5
 ajuWXGNFUUonV/p4D2OKdUSYZ0XXEjHXLXDXxMdr6FQ/CcQnbRUMSDRnP7/9F1Y2
 NI1+XT3iOCQMMUBus2QC
 =/ZUB
 -----END PGP SIGNATURE-----

Merge tag 'v1.4.2'

Fix a mistake where the v1.4.2 release tag wasn't actually merged to master.
2016-09-24 11:16:44 +10:00
David Gibson
a34bb721ca dtc: Fix assorted problems in the testcases for the -a option
We just added the -a option to allow padding of the output dtb's size to
a specified alignment.  Unfortunately the test cases for this had several
bugs:
    * Didn't actually test anything since "alignbase" instead of $alignbase
      was passed to the checker function
    * Introduced an unnecessary run_local_test wrapper
    * Didn't provide very helpful output on failure
    * Only attempted to check one alignment value

This patch fixes up these problems.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-09-22 14:42:42 +10:00
Tim Wang
874f40588d Implement the -a option to pad dtb aligned
There is one condition that need cat the dtb files
into one dtb.img which can support several boards
use same SoC platform.

And the original dtb file size is not aligned to any base.
This may cause "Synchronous Abort" when load from a unligned
address on some SoC machine, such as ARM.

So this patch implement the -a <aligned number> option to
pad zero at the end of dtb files and make the dtb size aligned
to <aligned number>.

Then, the aligned dtbs can cat together and load without "Synchronous
Abort".

Signed-off-by: Tim Wang <timwang@asrmicro.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-09-22 14:25:50 +10:00
David Gibson
ec02b34c05 dtc: Makefile improvements for release uploading
This has some fixes to the make dist target, and a new make kup target for
maintainer convenience uploading new releases.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-09-03 21:02:30 +10:00
David Gibson
1ed45d40a1 dtc: Bump version to 1.4.2
Bump version in preparation for a new release.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-09-03 19:34:45 +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
Maxime Ripard
d877364e4a libfdt: Add fdt_setprop_inplace_namelen_partial
Add a function to modify inplace only a portion of a property..

This is especially useful when the property is an array of values, and you
want to update one of them without changing the DT size.

Acked-by: Simon Glass <sjg@chromium.org>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
[dwg: Remove unnecessary unsigned qualifier, correct a comment]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-08-01 13:34:39 +10:00
Maxime Ripard
3e9037aaad libfdt: Add fdt_getprop_namelen_w
Add a function to retrieve a writeable property only by the first
characters of its name.

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-07-28 15:29:16 +10:00
Maxime Ripard
84e0e1346c libfdt: Add max phandle retrieval function
Add a function to retrieve the highest phandle in a given device tree.

Acked-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Stefan Agner <stefan@agner.ch>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-07-28 15:29:16 +10:00
Maxime Ripard
d29126c90a libfdt: Add iterator over properties
Implement a macro based on fdt_first_property_offset and
fdt_next_property_offset that provides a convenience to iterate over all
the properties of a given node.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-by: Simon Glass <sjg@chromium.org>
[dwg: Removed a stray trailing blank line]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-07-28 15:29:13 +10:00
Thierry Reding
902d0f0953 libfdt: Add a subnodes iterator macro
The fdt_for_each_subnode() iterator macro provided by this patch can be
used to iterate over a device tree node's subnodes. At each iteration a
loop variable will be set to the next subnode.

Signed-off-by: Thierry Reding <treding@nvidia.com>
Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-07-28 15:27:57 +10:00
Jean-Christophe Dubois
c539075ba8 fdtput.c: Fix memory leak.
CID 132821 (#1 of 1): Resource leak (RESOURCE_LEAK)
12. leaked_storage: Variable value going out of scope leaks the storage it points to.

Signed-off-by: Jean-Christophe Dubois <jcd@tribudubois.net>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-07-24 01:05:05 +10:00
Jean-Christophe Dubois
f79ddb83e1 fdtget.c: Fix memory leak
CID 132823 (#1 of 1): Resource leak (RESOURCE_LEAK)
5. leaked_storage: Variable blob going out of scope leaks the storage it points to.

Signed-off-by: Jean-Christophe Dubois <jcd@tribudubois.net>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-07-24 01:05:05 +10:00
David Gibson
1074ee54b6 convert-dtsv0-lexer.l: fix memory leak
CID 132822 (#1 of 1): Resource leak (RESOURCE_LEAK)
9. leaked_storage: Variable newname going out of scope leaks the storage it points to

Signed-off-by: Jean-Christophe Dubois <jcd@tribudubois.net>
[dwg: Removed unnecessary hunk]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-07-24 00:50:30 +10:00
Jean-Christophe Dubois
e24d39a024 fdtdump.c: make sure size_t argument to memchr is always unsigned.
CID 132817 (#1 of 1): Integer overflowed argument (INTEGER_OVERFLOW)
15. overflow_sink: Overflowed or truncated value (or a value computed from an overflowed or truncated value) endp - p - 4L used as critical argument to function.

Signed-off-by: Jean-Christophe Dubois <jcd@tribudubois.net>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-07-24 00:38:00 +10:00
David Gibson
44a59713cf Remove unused srcpos_dump() function
srcpos_dump() has no current users, and I have no plans to use it.  So
remove it.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-07-13 00:44:53 +10:00
Jean-Christophe Dubois
cb9241ae34 DTC: Fix memory leak on flatname.
If flatname was not referenced by the "node" structure, the reference to the
allocated string is lost at function exit.

We need to free it if is not used by "node".

Signed-off-by: Jean-Christophe Dubois <jcd@tribudubois.net>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-07-11 11:28:28 +10:00
David Gibson
1ee0ae24ea Simplify check field and macro names
Now that "node" checks are the only type of checks, simplify some names
accordingly.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-05-27 15:30:24 +10:00
David Gibson
9d97527a86 Remove property check functions
Property checking functions aren't particularly useful.  They're used only
in a handful of cases, and most of those really only check a small handful
of specific properties.  This patches converts the few cases to node check
functions and removes property check functions entirely.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-05-27 15:23:54 +10:00
David Gibson
2e709d158e Remove tree check functions
The tree check functions from the checking infrastructure aren't very
useful.  There were only two examples using them, and they're basically
equivalent to a node check which is applied only to the root node, so those
are easily replaced.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-05-27 15:23:54 +10:00
David Gibson
c4cb12e193 Alter grammar to allow multiple /dts-v1/ tags
This patch allows dtc to accept multiple /dts-v1/ tags (provided they're
all at the beginning of the input), rather than giving a syntax error.

This makes it more convenient to include one .dts file from another without
having to be careful that the /dts-v1/ tag is in exactly one of them.

We a couple of existing testcases to take advantage of this, which
simplifies them slightly.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-05-27 15:21:04 +10:00
David Gibson
d71d25d760 Use xasprintf() in srcpos
Now that we have an xasprintf() helper function, use it to simplify the
srcpos_string() implementation.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-05-25 15:15:36 +10:00
Pantelis Antoniou
9dc404958e util: Add xasprintf portable asprintf variant
Include a portable asprintf variant that works on any C99
conforming platform.

Signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-05-25 14:55:17 +10:00
Simon Glass
beef80b8b5 Correct a missing space in a fdt_header cast
The code style here is slightly incorrect. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-03-07 15:34:00 +11:00
Simon Glass
68d43cec12 Correct line lengths in libfdt.h
There are a few lines that are over 80 columns. Fix these.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-03-07 15:33:59 +11:00
Simon Glass
b0dbceafd4 Correct space-after-tab in libfdt.h
There are a few places with a space before a tab in this file. Fix them.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-03-07 15:33:59 +11:00
Masahiro Yamada
53bf130b1c libfdt: simplify fdt_node_check_compatible()
Because fdt_stringlist_contains() returns 1 or 0,
fdt_node_check_compatible() can just return the inverted value.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-02-20 11:29:14 +11:00
Stephen Warren
c9d9121683 Warn on node name unit-address presence/absence mismatch
ePAPR 1.1 section 2.2.1.1 "Node Name Requirements" specifies that any
node that has a reg property must include a unit address in its name
with value matching the first entry in its reg property. Conversely, if
a node does not have a reg property, the node name must not include a
unit address. Also allow ranges property as it is deemed valid, but ePAPR
is not clear about it.

Implement a check for this. The code doesn't validate the format of the
unit address; ePAPR implies this may vary from (containing bus) binding
to binding, so doing so would be much more complex.

Signed-off-by: Stephen Warren <swarren@nvidia.com>
[robh: also allow non-empty ranges]
Signed-off-by: Rob Herring <robh@kernel.org>
[moved new test in check_table]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-02-19 16:00:05 +11:00
Anton Blanchard
2e53f9d2f0 Catch unsigned 32bit overflow when parsing flattened device tree offsets
We have a couple of checks of the form:

    if (offset+size > totalsize)
        die();

We need to check that offset+size doesn't overflow, otherwise the check
will pass, and we may access past totalsize.

Found with AFL.

Signed-off-by: Anton Blanchard <anton@samba.org>
[Added a testcase]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-02-19 01:08:46 +11:00
David Gibson
b06e55c88b Prevent crash on modulo by zero
1937095 "Prevent crash on division by zero" fixed a crash when attempting
a division by zero using the / operator in a dts.  However, it missed the
precisely equivalent crash with the % (modulus) operator.  This patch fixes
the oversight.

Reported-by: Anton Blanchard <anton@samba.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-01-12 19:27:25 +11:00
David Gibson
b43345039b Fix some bugs in processing of line directives
In order to work with preprocessed dts files more easily, dts will parse
line number information in the form emitted by cpp.

Anton Blanchard (using a fuzzer) reported that including a line number
directive with a nul character (a literal nul in the input file, not a \0
sequence) would cause dtc to SEGV.  I spotted several more problems on
examining the code:
    * It modified yytext in place which seems to work, but is ugly and I'm
      not sure if it's safe on all lex/flex versions
    * The regexp used in the lexer to recognize line number information
      accepts strings with escape characters, but it won't process these
      escapes.
        - GNU cpp at least, will generate \ escapes in line number
          information, at least with files containing " or \ in the name

This patch reworks the handling of line number information to address
these problems.  \ escapes should now be handled directly.  nuls in file
names (either with a literal nul in the input file, or with a \0 escape
sequence) are still not permitted, but will now result in a lexical error
rather than a SEGV.

Reported-by: Anton Blanchard <anton@samba.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-01-04 22:56:39 +11:00
David Gibson
d728ad59f5 Fix crash on nul character in string escape sequence
If a dts file contains a string with \ followed by a nul byte - an actual
nul in the input file, not the \\0 escape - then the assert() in
get_escape_char() will trip, crashing dtc.

As far as I can tell, there isn't any valid need for this assert(), so just
remove it.

Reported-by: Anton Blanchard <anton@samba.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-01-03 23:01:24 +11:00
David Gibson
1ab2205a6f Gracefully handle bad octal literals
The code handling integer literals in dtc-lexer.l assumes that the flex
regexp means that strtoull() can't fail to interpret the string as a valid
integer (either decimal, octal, or hexadecimal).  This is not true for
octals.  For example '09' is accepted as a literal by the regexp,
strtoull() attempts to handle it as octal, but it has a bad digit.

This changes the code to give a more useful error in this case.

Reported-by: Anton Blanchard <anton@samba.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-01-03 22:54:37 +11:00
David Gibson
1937095588 Prevent crash on division by zero
Currently, attempting to divide by zero in an integer expression in a dts
file will cause dtc to crash with a division by zero (SIGFPE).

This patch corrects this to properly detect this case and raise an error.

Reported-by: Anton Blanchard <anton@samba.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2016-01-03 22:27:32 +11:00
David Gibson
d0b3ab0a0f libfdt: Fix undefined behaviour in fdt_offset_ptr()
Using pointer arithmetic to generate a pointer outside a known object is,
technically, undefined behaviour in C.  Unfortunately, we were using that
in fdt_offset_ptr() to detect overflows.

To fix this we need to do our bounds / overflow checking on the offsets
before constructing pointers from them.

Reported-by: David Binderman <dcb314@hotmail.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2015-12-17 17:19:11 +11:00
Courtney Cavin
d4c7c25c9e libfdt: check for potential overrun in _fdt_splice()
This patch catches the conditions where:
 - 'splicepoint' is set to a point outside of [ fdt, fdt_totalsize(fdt) )
 - 'newlen' is negative, or 'splicepoint' plus 'newlen' results in overflow

Either of these cases can be caused by math which overflows in calling
functions, or by sizes specified through dynamic means.

Signed-off-by: Courtney Cavin <courtney.cavin@sonymobile.com>
Signed-off-by: Bjorn Andersson <bjorn.andersson@sonymobile.com>
2015-12-02 13:11:11 +11:00
David Gibson
f58799be13 libfdt: Add some missing symbols to version.lds
Several functions in the header file were missing from the version.lds
script, meaning that they couldn't be used from a libfdt shared library.

Reported by Ken Aaker, via github issue tracker.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2015-12-01 12:55:21 +11:00
David Gibson
af9f26d1e5 Remove duplicated -Werror in dtc Makefile
The "-Werror" compiler flag is currently declared twice in the
Makefile, one time in WARNINGS, and one time in CFLAGS. Let's
remove one of them.

Signed-off-by: Thomas Huth <thuth@redhat.com>
[Moved remaining -Werror from WARNINGS to CFLAGS --dwg]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2015-11-16 23:45:36 +11:00
Thierry Reding
604e61e081 fdt: Add functions to retrieve strings
Given a device tree node, a property name and an index, the new function
fdt_stringlist_get() will return a pointer to the index'th string in the
property's value and return its length (or an error code on failure) in
an output argument.

Signed-off-by: Thierry Reding <treding@nvidia.com>
[Fix some -Wshadow warnings --dwg]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2015-09-30 13:26:31 +10:00
Thierry Reding
8702bd1d3b fdt: Add a function to get the index of a string
The new fdt_stringlist_search() function will look up a given string in
the list contained in the value of a named property of a given device
tree node and return its index.

Signed-off-by: Thierry Reding <treding@nvidia.com>
[Fix some -Wshadow warnings --dwg]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2015-09-30 13:26:18 +10:00
Thierry Reding
2218387a8c fdt: Add a function to count strings
Given a device tree node and a property name, the fdt_stringlist_count()
function counts the number of strings found in the property value.

This also adds a new error code, FDT_ERR_BADVALUE, that the function
returns when it encounters a non-NUL-terminated string list.

Signed-off-by: Thierry Reding <treding@nvidia.com>
[Changed testcase name --dwg]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2015-09-30 13:16:35 +10:00
Masahiro Yamada
554fde2c77 libfdt: fix comment block of fdt_get_property_namelen()
The statement "Identical to fdt_get_property_namelen() ..." does not
make sense for the comment of fdt_get_property_namelen() itself.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2015-08-27 17:14:15 +10:00
David Gibson
e5e6df7c37 fdtdump: Fix bug printing bytestrings with negative values
On systems where 'char' is signed, fdtdump will currently print the wrong
thing on properties containing bytestring values with "negative" bytes
(that is with values from 0x80..0xff).  The fdtdump testcase is extended
to cover this case too.

This corrects the problem by forcing use of unsigned char - although this
is perhaps another indication that fdtdump is a buggy hack and if you want
to do real work you should use dtc -O dts.

Reported-by: Igor Prusov <Igor.V.Prusov@mcst.ru>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2015-07-09 13:47:19 +10:00