[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[xen master] Update libfdt to v1.6.1



commit ad9cf6bde5b90d4c1e5a79a2803e98d6344c27d7
Author:     Vikram Garhwal <fnu.vikram@xxxxxxxxxx>
AuthorDate: Thu Nov 11 23:27:20 2021 -0800
Commit:     Julien Grall <jgrall@xxxxxxxxxx>
CommitDate: Wed Dec 1 16:40:19 2021 +0000

    Update libfdt to v1.6.1
    
    Update libfdt to v1.6.1 of libfdt taken from git://github.com/dgibson/dtc.
    This update is done to support device tree overlays.
    
    A few minor changes are done to make it compatible with Xen:
        fdt_overlay.c: overlay_fixup_phandle()
    
            Replace strtoul() with simple_strtoul() as strtoul() is not 
available in
            Xen lib and included lib.h.
    
            Change char *endptr to const char *endptr. This change is required 
for
            using simple_strtoul().
    
        libfdt_env.h:
            Remaining Xen changes to libfdt_env.h carried over from existing
            libfdt (v1.4.0)
    
    Signed-off-by: Vikram Garhwal <fnu.vikram@xxxxxxxxxx>
    Reviewed-by: Luca Fancellu <luca.fancellu@xxxxxxx>
    Tested-by: Luca Fancellu <luca.fancellu@xxxxxxx>
    Reviewed-by: Julien Grall <jgrall@xxxxxxxxxx>
---
 xen/common/libfdt/Makefile.libfdt   |  10 +-
 xen/common/libfdt/fdt.c             | 226 ++++++---
 xen/common/libfdt/fdt_addresses.c   | 101 ++++
 xen/common/libfdt/fdt_check.c       |  93 ++++
 xen/common/libfdt/fdt_empty_tree.c  |  46 +-
 xen/common/libfdt/fdt_overlay.c     | 884 ++++++++++++++++++++++++++++++++++++
 xen/common/libfdt/fdt_ro.c          | 514 ++++++++++++++++-----
 xen/common/libfdt/fdt_rw.c          | 249 +++++-----
 xen/common/libfdt/fdt_strerror.c    |  55 +--
 xen/common/libfdt/fdt_sw.c          | 312 +++++++++----
 xen/common/libfdt/fdt_wip.c         |  88 ++--
 xen/common/libfdt/libfdt_internal.h | 223 ++++++---
 xen/common/libfdt/version.lds       |  24 +-
 xen/include/xen/libfdt/fdt.h        |  51 +--
 xen/include/xen/libfdt/libfdt.h     | 858 +++++++++++++++++++++++++++++-----
 xen/include/xen/libfdt/libfdt_env.h |  98 +++-
 16 files changed, 3046 insertions(+), 786 deletions(-)

diff --git a/xen/common/libfdt/Makefile.libfdt 
b/xen/common/libfdt/Makefile.libfdt
index 91126c000a..b6d8fc02dd 100644
--- a/xen/common/libfdt/Makefile.libfdt
+++ b/xen/common/libfdt/Makefile.libfdt
@@ -1,3 +1,4 @@
+# SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
 # Makefile.libfdt
 #
 # This is not a complete Makefile of itself.  Instead, it is designed to
@@ -6,5 +7,12 @@
 LIBFDT_soname = libfdt.$(SHAREDLIB_EXT).1
 LIBFDT_INCLUDES = fdt.h libfdt.h libfdt_env.h
 LIBFDT_VERSION = version.lds
-LIBFDT_SRCS = fdt.c fdt_ro.c fdt_wip.c fdt_sw.c fdt_rw.c fdt_strerror.c 
fdt_empty_tree.c
+LIBFDT_SRCS = fdt.c fdt_ro.c fdt_wip.c fdt_sw.c fdt_rw.c fdt_strerror.c 
fdt_empty_tree.c \
+       fdt_addresses.c fdt_overlay.c fdt_check.c
 LIBFDT_OBJS = $(LIBFDT_SRCS:%.c=%.o)
+LIBFDT_LIB = libfdt-$(DTC_VERSION).$(SHAREDLIB_EXT)
+
+libfdt_clean:
+       @$(VECHO) CLEAN "(libfdt)"
+       rm -f $(STD_CLEANFILES:%=$(LIBFDT_dir)/%)
+       rm -f $(LIBFDT_dir)/$(LIBFDT_soname)
diff --git a/xen/common/libfdt/fdt.c b/xen/common/libfdt/fdt.c
index bbc77177d9..9fe7cf4b74 100644
--- a/xen/common/libfdt/fdt.c
+++ b/xen/common/libfdt/fdt.c
@@ -1,50 +1,7 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
 /*
  * libfdt - Flat Device Tree manipulation
  * Copyright (C) 2006 David Gibson, IBM Corporation.
- *
- * libfdt is dual licensed: you can use it either under the terms of
- * the GPL, or the BSD license, at your option.
- *
- *  a) This library 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 library 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 library; If not, see 
<http://www.gnu.org/licenses/>.
- *
- * Alternatively,
- *
- *  b) Redistribution and use in source and binary forms, with or
- *     without modification, are permitted provided that the following
- *     conditions are met:
- *
- *     1. Redistributions of source code must retain the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer.
- *     2. Redistributions in binary form must reproduce the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer in the documentation and/or other materials
- *        provided with the distribution.
- *
- *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 #include "libfdt_env.h"
 
@@ -53,40 +10,156 @@
 
 #include "libfdt_internal.h"
 
-int fdt_check_header(const void *fdt)
+/*
+ * Minimal sanity check for a read-only tree. fdt_ro_probe_() checks
+ * that the given buffer contains what appears to be a flattened
+ * device tree with sane information in its header.
+ */
+int32_t fdt_ro_probe_(const void *fdt)
 {
+       uint32_t totalsize = fdt_totalsize(fdt);
+
+       if (can_assume(VALID_DTB))
+               return totalsize;
+
+       /* The device tree must be at an 8-byte aligned address */
+       if ((uintptr_t)fdt & 7)
+               return -FDT_ERR_ALIGNMENT;
+
        if (fdt_magic(fdt) == FDT_MAGIC) {
                /* Complete tree */
-               if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
-                       return -FDT_ERR_BADVERSION;
-               if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
-                       return -FDT_ERR_BADVERSION;
+               if (!can_assume(LATEST)) {
+                       if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
+                               return -FDT_ERR_BADVERSION;
+                       if (fdt_last_comp_version(fdt) >
+                                       FDT_LAST_SUPPORTED_VERSION)
+                               return -FDT_ERR_BADVERSION;
+               }
        } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
                /* Unfinished sequential-write blob */
-               if (fdt_size_dt_struct(fdt) == 0)
+               if (!can_assume(VALID_INPUT) && fdt_size_dt_struct(fdt) == 0)
                        return -FDT_ERR_BADSTATE;
        } else {
                return -FDT_ERR_BADMAGIC;
        }
 
+       if (totalsize < INT32_MAX)
+               return totalsize;
+       else
+               return -FDT_ERR_TRUNCATED;
+}
+
+static int check_off_(uint32_t hdrsize, uint32_t totalsize, uint32_t off)
+{
+       return (off >= hdrsize) && (off <= totalsize);
+}
+
+static int check_block_(uint32_t hdrsize, uint32_t totalsize,
+                       uint32_t base, uint32_t size)
+{
+       if (!check_off_(hdrsize, totalsize, base))
+               return 0; /* block start out of bounds */
+       if ((base + size) < base)
+               return 0; /* overflow */
+       if (!check_off_(hdrsize, totalsize, base + size))
+               return 0; /* block end out of bounds */
+       return 1;
+}
+
+size_t fdt_header_size_(uint32_t version)
+{
+       if (version <= 1)
+               return FDT_V1_SIZE;
+       else if (version <= 2)
+               return FDT_V2_SIZE;
+       else if (version <= 3)
+               return FDT_V3_SIZE;
+       else if (version <= 16)
+               return FDT_V16_SIZE;
+       else
+               return FDT_V17_SIZE;
+}
+
+size_t fdt_header_size(const void *fdt)
+{
+       return can_assume(LATEST) ? FDT_V17_SIZE :
+               fdt_header_size_(fdt_version(fdt));
+}
+
+int fdt_check_header(const void *fdt)
+{
+       size_t hdrsize;
+
+       /* The device tree must be at an 8-byte aligned address */
+       if ((uintptr_t)fdt & 7)
+               return -FDT_ERR_ALIGNMENT;
+
+       if (fdt_magic(fdt) != FDT_MAGIC)
+               return -FDT_ERR_BADMAGIC;
+       if (!can_assume(LATEST)) {
+               if ((fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
+                   || (fdt_last_comp_version(fdt) >
+                       FDT_LAST_SUPPORTED_VERSION))
+                       return -FDT_ERR_BADVERSION;
+               if (fdt_version(fdt) < fdt_last_comp_version(fdt))
+                       return -FDT_ERR_BADVERSION;
+       }
+       hdrsize = fdt_header_size(fdt);
+       if (!can_assume(VALID_DTB)) {
+
+               if ((fdt_totalsize(fdt) < hdrsize)
+                   || (fdt_totalsize(fdt) > INT_MAX))
+                       return -FDT_ERR_TRUNCATED;
+
+               /* Bounds check memrsv block */
+               if (!check_off_(hdrsize, fdt_totalsize(fdt),
+                               fdt_off_mem_rsvmap(fdt)))
+                       return -FDT_ERR_TRUNCATED;
+       }
+
+       if (!can_assume(VALID_DTB)) {
+               /* Bounds check structure block */
+               if (!can_assume(LATEST) && fdt_version(fdt) < 17) {
+                       if (!check_off_(hdrsize, fdt_totalsize(fdt),
+                                       fdt_off_dt_struct(fdt)))
+                               return -FDT_ERR_TRUNCATED;
+               } else {
+                       if (!check_block_(hdrsize, fdt_totalsize(fdt),
+                                         fdt_off_dt_struct(fdt),
+                                         fdt_size_dt_struct(fdt)))
+                               return -FDT_ERR_TRUNCATED;
+               }
+
+               /* Bounds check strings block */
+               if (!check_block_(hdrsize, fdt_totalsize(fdt),
+                                 fdt_off_dt_strings(fdt),
+                                 fdt_size_dt_strings(fdt)))
+                       return -FDT_ERR_TRUNCATED;
+       }
+
        return 0;
 }
 
 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
 {
-       unsigned absoffset = offset + fdt_off_dt_struct(fdt);
+       unsigned int uoffset = offset;
+       unsigned int absoffset = offset + fdt_off_dt_struct(fdt);
 
-       if ((absoffset < offset)
-           || ((absoffset + len) < absoffset)
-           || (absoffset + len) > fdt_totalsize(fdt))
+       if (offset < 0)
                return NULL;
 
-       if (fdt_version(fdt) >= 0x11)
-               if (((offset + len) < offset)
+       if (!can_assume(VALID_INPUT))
+               if ((absoffset < uoffset)
+                   || ((absoffset + len) < absoffset)
+                   || (absoffset + len) > fdt_totalsize(fdt))
+                       return NULL;
+
+       if (can_assume(LATEST) || fdt_version(fdt) >= 0x11)
+               if (((uoffset + len) < uoffset)
                    || ((offset + len) > fdt_size_dt_struct(fdt)))
                        return NULL;
 
-       return _fdt_offset_ptr(fdt, offset);
+       return fdt_offset_ptr_(fdt, offset);
 }
 
 uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
@@ -98,7 +171,7 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int 
*nextoffset)
 
        *nextoffset = -FDT_ERR_TRUNCATED;
        tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
-       if (!tagp)
+       if (!can_assume(VALID_DTB) && !tagp)
                return FDT_END; /* premature end */
        tag = fdt32_to_cpu(*tagp);
        offset += FDT_TAGSIZE;
@@ -110,17 +183,21 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, 
int *nextoffset)
                do {
                        p = fdt_offset_ptr(fdt, offset++, 1);
                } while (p && (*p != '\0'));
-               if (!p)
+               if (!can_assume(VALID_DTB) && !p)
                        return FDT_END; /* premature end */
                break;
 
        case FDT_PROP:
                lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
-               if (!lenp)
+               if (!can_assume(VALID_DTB) && !lenp)
                        return FDT_END; /* premature end */
                /* skip-name offset, length and value */
                offset += sizeof(struct fdt_property) - FDT_TAGSIZE
                        + fdt32_to_cpu(*lenp);
+               if (!can_assume(LATEST) &&
+                   fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 &&
+                   ((offset - fdt32_to_cpu(*lenp)) % 8) != 0)
+                       offset += 4;
                break;
 
        case FDT_END:
@@ -139,19 +216,25 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, 
int *nextoffset)
        return tag;
 }
 
-int _fdt_check_node_offset(const void *fdt, int offset)
+int fdt_check_node_offset_(const void *fdt, int offset)
 {
-       if ((offset < 0) || (offset % FDT_TAGSIZE)
-           || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
+       if (!can_assume(VALID_INPUT)
+           && ((offset < 0) || (offset % FDT_TAGSIZE)))
+               return -FDT_ERR_BADOFFSET;
+
+       if (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)
                return -FDT_ERR_BADOFFSET;
 
        return offset;
 }
 
-int _fdt_check_prop_offset(const void *fdt, int offset)
+int fdt_check_prop_offset_(const void *fdt, int offset)
 {
-       if ((offset < 0) || (offset % FDT_TAGSIZE)
-           || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP))
+       if (!can_assume(VALID_INPUT)
+           && ((offset < 0) || (offset % FDT_TAGSIZE)))
+               return -FDT_ERR_BADOFFSET;
+
+       if (fdt_next_tag(fdt, offset, &offset) != FDT_PROP)
                return -FDT_ERR_BADOFFSET;
 
        return offset;
@@ -163,7 +246,7 @@ int fdt_next_node(const void *fdt, int offset, int *depth)
        uint32_t tag;
 
        if (offset >= 0)
-               if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
+               if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0)
                        return nextoffset;
 
        do {
@@ -225,7 +308,7 @@ int fdt_next_subnode(const void *fdt, int offset)
        return offset;
 }
 
-const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
+const char *fdt_find_string_(const char *strtab, int tabsize, const char *s)
 {
        int len = strlen(s) + 1;
        const char *last = strtab + tabsize - len;
@@ -239,9 +322,12 @@ const char *_fdt_find_string(const char *strtab, int 
tabsize, const char *s)
 
 int fdt_move(const void *fdt, void *buf, int bufsize)
 {
-       FDT_CHECK_HEADER(fdt);
+       if (!can_assume(VALID_INPUT) && bufsize < 0)
+               return -FDT_ERR_NOSPACE;
+
+       FDT_RO_PROBE(fdt);
 
-       if (fdt_totalsize(fdt) > bufsize)
+       if (fdt_totalsize(fdt) > (unsigned int)bufsize)
                return -FDT_ERR_NOSPACE;
 
        memmove(buf, fdt, fdt_totalsize(fdt));
diff --git a/xen/common/libfdt/fdt_addresses.c 
b/xen/common/libfdt/fdt_addresses.c
new file mode 100644
index 0000000000..9a82cd0ba2
--- /dev/null
+++ b/xen/common/libfdt/fdt_addresses.c
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2014 David Gibson <david@xxxxxxxxxxxxxxxxxxxxx>
+ * Copyright (C) 2018 embedded brains GmbH
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+static int fdt_cells(const void *fdt, int nodeoffset, const char *name)
+{
+       const fdt32_t *c;
+       uint32_t val;
+       int len;
+
+       c = fdt_getprop(fdt, nodeoffset, name, &len);
+       if (!c)
+               return len;
+
+       if (len != sizeof(*c))
+               return -FDT_ERR_BADNCELLS;
+
+       val = fdt32_to_cpu(*c);
+       if (val > FDT_MAX_NCELLS)
+               return -FDT_ERR_BADNCELLS;
+
+       return (int)val;
+}
+
+int fdt_address_cells(const void *fdt, int nodeoffset)
+{
+       int val;
+
+       val = fdt_cells(fdt, nodeoffset, "#address-cells");
+       if (val == 0)
+               return -FDT_ERR_BADNCELLS;
+       if (val == -FDT_ERR_NOTFOUND)
+               return 2;
+       return val;
+}
+
+int fdt_size_cells(const void *fdt, int nodeoffset)
+{
+       int val;
+
+       val = fdt_cells(fdt, nodeoffset, "#size-cells");
+       if (val == -FDT_ERR_NOTFOUND)
+               return 1;
+       return val;
+}
+
+/* This function assumes that [address|size]_cells is 1 or 2 */
+int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset,
+                            const char *name, uint64_t addr, uint64_t size)
+{
+       int addr_cells, size_cells, ret;
+       uint8_t data[sizeof(fdt64_t) * 2], *prop;
+
+       ret = fdt_address_cells(fdt, parent);
+       if (ret < 0)
+               return ret;
+       addr_cells = ret;
+
+       ret = fdt_size_cells(fdt, parent);
+       if (ret < 0)
+               return ret;
+       size_cells = ret;
+
+       /* check validity of address */
+       prop = data;
+       if (addr_cells == 1) {
+               if ((addr > UINT32_MAX) || ((UINT32_MAX + 1 - addr) < size))
+                       return -FDT_ERR_BADVALUE;
+
+               fdt32_st(prop, (uint32_t)addr);
+       } else if (addr_cells == 2) {
+               fdt64_st(prop, addr);
+       } else {
+               return -FDT_ERR_BADNCELLS;
+       }
+
+       /* check validity of size */
+       prop += addr_cells * sizeof(fdt32_t);
+       if (size_cells == 1) {
+               if (size > UINT32_MAX)
+                       return -FDT_ERR_BADVALUE;
+
+               fdt32_st(prop, (uint32_t)size);
+       } else if (size_cells == 2) {
+               fdt64_st(prop, size);
+       } else {
+               return -FDT_ERR_BADNCELLS;
+       }
+
+       return fdt_appendprop(fdt, nodeoffset, name, data,
+                             (addr_cells + size_cells) * sizeof(fdt32_t));
+}
diff --git a/xen/common/libfdt/fdt_check.c b/xen/common/libfdt/fdt_check.c
new file mode 100644
index 0000000000..fa410a86e2
--- /dev/null
+++ b/xen/common/libfdt/fdt_check.c
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+int fdt_check_full(const void *fdt, size_t bufsize)
+{
+       int err;
+       int num_memrsv;
+       int offset, nextoffset = 0;
+       uint32_t tag;
+       unsigned int depth = 0;
+       const void *prop;
+       const char *propname;
+       bool expect_end = false;
+
+       if (bufsize < FDT_V1_SIZE)
+               return -FDT_ERR_TRUNCATED;
+       if (bufsize < fdt_header_size(fdt))
+               return -FDT_ERR_TRUNCATED;
+       err = fdt_check_header(fdt);
+       if (err != 0)
+               return err;
+       if (bufsize < fdt_totalsize(fdt))
+               return -FDT_ERR_TRUNCATED;
+
+       num_memrsv = fdt_num_mem_rsv(fdt);
+       if (num_memrsv < 0)
+               return num_memrsv;
+
+       while (1) {
+               offset = nextoffset;
+               tag = fdt_next_tag(fdt, offset, &nextoffset);
+
+               if (nextoffset < 0)
+                       return nextoffset;
+
+               /* If we see two root nodes, something is wrong */
+               if (expect_end && tag != FDT_END)
+                       return -FDT_ERR_BADSTRUCTURE;
+
+               switch (tag) {
+               case FDT_NOP:
+                       break;
+
+               case FDT_END:
+                       if (depth != 0)
+                               return -FDT_ERR_BADSTRUCTURE;
+                       return 0;
+
+               case FDT_BEGIN_NODE:
+                       depth++;
+                       if (depth > INT_MAX)
+                               return -FDT_ERR_BADSTRUCTURE;
+
+                       /* The root node must have an empty name */
+                       if (depth == 1) {
+                               const char *name;
+                               int len;
+
+                               name = fdt_get_name(fdt, offset, &len);
+                               if (*name || len)
+                                       return -FDT_ERR_BADSTRUCTURE;
+                       }
+                       break;
+
+               case FDT_END_NODE:
+                       if (depth == 0)
+                               return -FDT_ERR_BADSTRUCTURE;
+                       depth--;
+                       if (depth == 0)
+                               expect_end = true;
+                       break;
+
+               case FDT_PROP:
+                       prop = fdt_getprop_by_offset(fdt, offset, &propname,
+                                                    &err);
+                       if (!prop)
+                               return err;
+                       break;
+
+               default:
+                       return -FDT_ERR_INTERNAL;
+               }
+       }
+}
diff --git a/xen/common/libfdt/fdt_empty_tree.c 
b/xen/common/libfdt/fdt_empty_tree.c
index d50561101f..49d54d44b8 100644
--- a/xen/common/libfdt/fdt_empty_tree.c
+++ b/xen/common/libfdt/fdt_empty_tree.c
@@ -1,50 +1,7 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
 /*
  * libfdt - Flat Device Tree manipulation
  * Copyright (C) 2012 David Gibson, IBM Corporation.
- *
- * libfdt is dual licensed: you can use it either under the terms of
- * the GPL, or the BSD license, at your option.
- *
- *  a) This library 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 library 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 library; If not, see 
<http://www.gnu.org/licenses/>.
- *
- * Alternatively,
- *
- *  b) Redistribution and use in source and binary forms, with or
- *     without modification, are permitted provided that the following
- *     conditions are met:
- *
- *     1. Redistributions of source code must retain the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer.
- *     2. Redistributions in binary form must reproduce the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer in the documentation and/or other materials
- *        provided with the distribution.
- *
- *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 #include "libfdt_env.h"
 
@@ -79,4 +36,3 @@ int fdt_create_empty_tree(void *buf, int bufsize)
 
        return fdt_open_into(buf, buf, bufsize);
 }
-
diff --git a/xen/common/libfdt/fdt_overlay.c b/xen/common/libfdt/fdt_overlay.c
new file mode 100644
index 0000000000..7b95e2b639
--- /dev/null
+++ b/xen/common/libfdt/fdt_overlay.c
@@ -0,0 +1,884 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2016 Free Electrons
+ * Copyright (C) 2016 NextThing Co.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+#include <xen/lib.h>
+
+#include "libfdt_internal.h"
+
+/**
+ * overlay_get_target_phandle - retrieves the target phandle of a fragment
+ * @fdto: pointer to the device tree overlay blob
+ * @fragment: node offset of the fragment in the overlay
+ *
+ * overlay_get_target_phandle() retrieves the target phandle of an
+ * overlay fragment when that fragment uses a phandle (target
+ * property) instead of a path (target-path property).
+ *
+ * returns:
+ *      the phandle pointed by the target property
+ *      0, if the phandle was not found
+ *     -1, if the phandle was malformed
+ */
+static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
+{
+       const fdt32_t *val;
+       int len;
+
+       val = fdt_getprop(fdto, fragment, "target", &len);
+       if (!val)
+               return 0;
+
+       if ((len != sizeof(*val)) || (fdt32_to_cpu(*val) == (uint32_t)-1))
+               return (uint32_t)-1;
+
+       return fdt32_to_cpu(*val);
+}
+
+/**
+ * overlay_get_target - retrieves the offset of a fragment's target
+ * @fdt: Base device tree blob
+ * @fdto: Device tree overlay blob
+ * @fragment: node offset of the fragment in the overlay
+ * @pathp: pointer which receives the path of the target (or NULL)
+ *
+ * overlay_get_target() retrieves the target offset in the base
+ * device tree of a fragment, no matter how the actual targeting is
+ * done (through a phandle or a path)
+ *
+ * returns:
+ *      the targeted node offset in the base device tree
+ *      Negative error code on error
+ */
+static int overlay_get_target(const void *fdt, const void *fdto,
+                             int fragment, char const **pathp)
+{
+       uint32_t phandle;
+       const char *path = NULL;
+       int path_len = 0, ret;
+
+       /* Try first to do a phandle based lookup */
+       phandle = overlay_get_target_phandle(fdto, fragment);
+       if (phandle == (uint32_t)-1)
+               return -FDT_ERR_BADPHANDLE;
+
+       /* no phandle, try path */
+       if (!phandle) {
+               /* And then a path based lookup */
+               path = fdt_getprop(fdto, fragment, "target-path", &path_len);
+               if (path)
+                       ret = fdt_path_offset(fdt, path);
+               else
+                       ret = path_len;
+       } else
+               ret = fdt_node_offset_by_phandle(fdt, phandle);
+
+       /*
+       * If we haven't found either a target or a
+       * target-path property in a node that contains a
+       * __overlay__ subnode (we wouldn't be called
+       * otherwise), consider it a improperly written
+       * overlay
+       */
+       if (ret < 0 && path_len == -FDT_ERR_NOTFOUND)
+               ret = -FDT_ERR_BADOVERLAY;
+
+       /* return on error */
+       if (ret < 0)
+               return ret;
+
+       /* return pointer to path (if available) */
+       if (pathp)
+               *pathp = path ? path : NULL;
+
+       return ret;
+}
+
+/**
+ * overlay_phandle_add_offset - Increases a phandle by an offset
+ * @fdt: Base device tree blob
+ * @node: Device tree overlay blob
+ * @name: Name of the property to modify (phandle or linux,phandle)
+ * @delta: offset to apply
+ *
+ * overlay_phandle_add_offset() increments a node phandle by a given
+ * offset.
+ *
+ * returns:
+ *      0 on success.
+ *      Negative error code on error
+ */
+static int overlay_phandle_add_offset(void *fdt, int node,
+                                     const char *name, uint32_t delta)
+{
+       const fdt32_t *val;
+       uint32_t adj_val;
+       int len;
+
+       val = fdt_getprop(fdt, node, name, &len);
+       if (!val)
+               return len;
+
+       if (len != sizeof(*val))
+               return -FDT_ERR_BADPHANDLE;
+
+       adj_val = fdt32_to_cpu(*val);
+       if ((adj_val + delta) < adj_val)
+               return -FDT_ERR_NOPHANDLES;
+
+       adj_val += delta;
+       if (adj_val == (uint32_t)-1)
+               return -FDT_ERR_NOPHANDLES;
+
+       return fdt_setprop_inplace_u32(fdt, node, name, adj_val);
+}
+
+/**
+ * overlay_adjust_node_phandles - Offsets the phandles of a node
+ * @fdto: Device tree overlay blob
+ * @node: Offset of the node we want to adjust
+ * @delta: Offset to shift the phandles of
+ *
+ * overlay_adjust_node_phandles() adds a constant to all the phandles
+ * of a given node. This is mainly use as part of the overlay
+ * application process, when we want to update all the overlay
+ * phandles to not conflict with the overlays of the base device tree.
+ *
+ * returns:
+ *      0 on success
+ *      Negative error code on failure
+ */
+static int overlay_adjust_node_phandles(void *fdto, int node,
+                                       uint32_t delta)
+{
+       int child;
+       int ret;
+
+       ret = overlay_phandle_add_offset(fdto, node, "phandle", delta);
+       if (ret && ret != -FDT_ERR_NOTFOUND)
+               return ret;
+
+       ret = overlay_phandle_add_offset(fdto, node, "linux,phandle", delta);
+       if (ret && ret != -FDT_ERR_NOTFOUND)
+               return ret;
+
+       fdt_for_each_subnode(child, fdto, node) {
+               ret = overlay_adjust_node_phandles(fdto, child, delta);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+
+/**
+ * overlay_adjust_local_phandles - Adjust the phandles of a whole overlay
+ * @fdto: Device tree overlay blob
+ * @delta: Offset to shift the phandles of
+ *
+ * overlay_adjust_local_phandles() adds a constant to all the
+ * phandles of an overlay. This is mainly use as part of the overlay
+ * application process, when we want to update all the overlay
+ * phandles to not conflict with the overlays of the base device tree.
+ *
+ * returns:
+ *      0 on success
+ *      Negative error code on failure
+ */
+static int overlay_adjust_local_phandles(void *fdto, uint32_t delta)
+{
+       /*
+        * Start adjusting the phandles from the overlay root
+        */
+       return overlay_adjust_node_phandles(fdto, 0, delta);
+}
+
+/**
+ * overlay_update_local_node_references - Adjust the overlay references
+ * @fdto: Device tree overlay blob
+ * @tree_node: Node offset of the node to operate on
+ * @fixup_node: Node offset of the matching local fixups node
+ * @delta: Offset to shift the phandles of
+ *
+ * overlay_update_local_nodes_references() update the phandles
+ * pointing to a node within the device tree overlay by adding a
+ * constant delta.
+ *
+ * This is mainly used as part of a device tree application process,
+ * where you want the device tree overlays phandles to not conflict
+ * with the ones from the base device tree before merging them.
+ *
+ * returns:
+ *      0 on success
+ *      Negative error code on failure
+ */
+static int overlay_update_local_node_references(void *fdto,
+                                               int tree_node,
+                                               int fixup_node,
+                                               uint32_t delta)
+{
+       int fixup_prop;
+       int fixup_child;
+       int ret;
+
+       fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
+               const fdt32_t *fixup_val;
+               const char *tree_val;
+               const char *name;
+               int fixup_len;
+               int tree_len;
+               int i;
+
+               fixup_val = fdt_getprop_by_offset(fdto, fixup_prop,
+                                                 &name, &fixup_len);
+               if (!fixup_val)
+                       return fixup_len;
+
+               if (fixup_len % sizeof(uint32_t))
+                       return -FDT_ERR_BADOVERLAY;
+               fixup_len /= sizeof(uint32_t);
+
+               tree_val = fdt_getprop(fdto, tree_node, name, &tree_len);
+               if (!tree_val) {
+                       if (tree_len == -FDT_ERR_NOTFOUND)
+                               return -FDT_ERR_BADOVERLAY;
+
+                       return tree_len;
+               }
+
+               for (i = 0; i < fixup_len; i++) {
+                       fdt32_t adj_val;
+                       uint32_t poffset;
+
+                       poffset = fdt32_to_cpu(fixup_val[i]);
+
+                       /*
+                        * phandles to fixup can be unaligned.
+                        *
+                        * Use a memcpy for the architectures that do
+                        * not support unaligned accesses.
+                        */
+                       memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
+
+                       adj_val = cpu_to_fdt32(fdt32_to_cpu(adj_val) + delta);
+
+                       ret = fdt_setprop_inplace_namelen_partial(fdto,
+                                                                 tree_node,
+                                                                 name,
+                                                                 strlen(name),
+                                                                 poffset,
+                                                                 &adj_val,
+                                                                 
sizeof(adj_val));
+                       if (ret == -FDT_ERR_NOSPACE)
+                               return -FDT_ERR_BADOVERLAY;
+
+                       if (ret)
+                               return ret;
+               }
+       }
+
+       fdt_for_each_subnode(fixup_child, fdto, fixup_node) {
+               const char *fixup_child_name = fdt_get_name(fdto, fixup_child,
+                                                           NULL);
+               int tree_child;
+
+               tree_child = fdt_subnode_offset(fdto, tree_node,
+                                               fixup_child_name);
+               if (tree_child == -FDT_ERR_NOTFOUND)
+                       return -FDT_ERR_BADOVERLAY;
+               if (tree_child < 0)
+                       return tree_child;
+
+               ret = overlay_update_local_node_references(fdto,
+                                                          tree_child,
+                                                          fixup_child,
+                                                          delta);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+
+/**
+ * overlay_update_local_references - Adjust the overlay references
+ * @fdto: Device tree overlay blob
+ * @delta: Offset to shift the phandles of
+ *
+ * overlay_update_local_references() update all the phandles pointing
+ * to a node within the device tree overlay by adding a constant
+ * delta to not conflict with the base overlay.
+ *
+ * This is mainly used as part of a device tree application process,
+ * where you want the device tree overlays phandles to not conflict
+ * with the ones from the base device tree before merging them.
+ *
+ * returns:
+ *      0 on success
+ *      Negative error code on failure
+ */
+static int overlay_update_local_references(void *fdto, uint32_t delta)
+{
+       int fixups;
+
+       fixups = fdt_path_offset(fdto, "/__local_fixups__");
+       if (fixups < 0) {
+               /* There's no local phandles to adjust, bail out */
+               if (fixups == -FDT_ERR_NOTFOUND)
+                       return 0;
+
+               return fixups;
+       }
+
+       /*
+        * Update our local references from the root of the tree
+        */
+       return overlay_update_local_node_references(fdto, 0, fixups,
+                                                   delta);
+}
+
+/**
+ * overlay_fixup_one_phandle - Set an overlay phandle to the base one
+ * @fdt: Base Device Tree blob
+ * @fdto: Device tree overlay blob
+ * @symbols_off: Node offset of the symbols node in the base device tree
+ * @path: Path to a node holding a phandle in the overlay
+ * @path_len: number of path characters to consider
+ * @name: Name of the property holding the phandle reference in the overlay
+ * @name_len: number of name characters to consider
+ * @poffset: Offset within the overlay property where the phandle is stored
+ * @label: Label of the node referenced by the phandle
+ *
+ * overlay_fixup_one_phandle() resolves an overlay phandle pointing to
+ * a node in the base device tree.
+ *
+ * This is part of the device tree overlay application process, when
+ * you want all the phandles in the overlay to point to the actual
+ * base dt nodes.
+ *
+ * returns:
+ *      0 on success
+ *      Negative error code on failure
+ */
+static int overlay_fixup_one_phandle(void *fdt, void *fdto,
+                                    int symbols_off,
+                                    const char *path, uint32_t path_len,
+                                    const char *name, uint32_t name_len,
+                                    int poffset, const char *label)
+{
+       const char *symbol_path;
+       uint32_t phandle;
+       fdt32_t phandle_prop;
+       int symbol_off, fixup_off;
+       int prop_len;
+
+       if (symbols_off < 0)
+               return symbols_off;
+
+       symbol_path = fdt_getprop(fdt, symbols_off, label,
+                                 &prop_len);
+       if (!symbol_path)
+               return prop_len;
+
+       symbol_off = fdt_path_offset(fdt, symbol_path);
+       if (symbol_off < 0)
+               return symbol_off;
+
+       phandle = fdt_get_phandle(fdt, symbol_off);
+       if (!phandle)
+               return -FDT_ERR_NOTFOUND;
+
+       fixup_off = fdt_path_offset_namelen(fdto, path, path_len);
+       if (fixup_off == -FDT_ERR_NOTFOUND)
+               return -FDT_ERR_BADOVERLAY;
+       if (fixup_off < 0)
+               return fixup_off;
+
+       phandle_prop = cpu_to_fdt32(phandle);
+       return fdt_setprop_inplace_namelen_partial(fdto, fixup_off,
+                                                  name, name_len, poffset,
+                                                  &phandle_prop,
+                                                  sizeof(phandle_prop));
+};
+
+/**
+ * overlay_fixup_phandle - Set an overlay phandle to the base one
+ * @fdt: Base Device Tree blob
+ * @fdto: Device tree overlay blob
+ * @symbols_off: Node offset of the symbols node in the base device tree
+ * @property: Property offset in the overlay holding the list of fixups
+ *
+ * overlay_fixup_phandle() resolves all the overlay phandles pointed
+ * to in a __fixups__ property, and updates them to match the phandles
+ * in use in the base device tree.
+ *
+ * This is part of the device tree overlay application process, when
+ * you want all the phandles in the overlay to point to the actual
+ * base dt nodes.
+ *
+ * returns:
+ *      0 on success
+ *      Negative error code on failure
+ */
+static int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off,
+                                int property)
+{
+       const char *value;
+       const char *label;
+       int len;
+
+       value = fdt_getprop_by_offset(fdto, property,
+                                     &label, &len);
+       if (!value) {
+               if (len == -FDT_ERR_NOTFOUND)
+                       return -FDT_ERR_INTERNAL;
+
+               return len;
+       }
+
+       do {
+               const char *path, *name, *fixup_end;
+               const char *fixup_str = value;
+               uint32_t path_len, name_len;
+               uint32_t fixup_len;
+               char *sep;
+               const char *endptr;
+               int poffset, ret;
+
+               fixup_end = memchr(value, '\0', len);
+               if (!fixup_end)
+                       return -FDT_ERR_BADOVERLAY;
+               fixup_len = fixup_end - fixup_str;
+
+               len -= fixup_len + 1;
+               value += fixup_len + 1;
+
+               path = fixup_str;
+               sep = memchr(fixup_str, ':', fixup_len);
+               if (!sep || *sep != ':')
+                       return -FDT_ERR_BADOVERLAY;
+
+               path_len = sep - path;
+               if (path_len == (fixup_len - 1))
+                       return -FDT_ERR_BADOVERLAY;
+
+               fixup_len -= path_len + 1;
+               name = sep + 1;
+               sep = memchr(name, ':', fixup_len);
+               if (!sep || *sep != ':')
+                       return -FDT_ERR_BADOVERLAY;
+
+               name_len = sep - name;
+               if (!name_len)
+                       return -FDT_ERR_BADOVERLAY;
+
+               poffset = simple_strtoul(sep + 1, &endptr, 10);
+               if ((*endptr != '\0') || (endptr <= (sep + 1)))
+                       return -FDT_ERR_BADOVERLAY;
+
+               ret = overlay_fixup_one_phandle(fdt, fdto, symbols_off,
+                                               path, path_len, name, name_len,
+                                               poffset, label);
+               if (ret)
+                       return ret;
+       } while (len > 0);
+
+       return 0;
+}
+
+/**
+ * overlay_fixup_phandles - Resolve the overlay phandles to the base
+ *                          device tree
+ * @fdt: Base Device Tree blob
+ * @fdto: Device tree overlay blob
+ *
+ * overlay_fixup_phandles() resolves all the overlay phandles pointing
+ * to nodes in the base device tree.
+ *
+ * This is one of the steps of the device tree overlay application
+ * process, when you want all the phandles in the overlay to point to
+ * the actual base dt nodes.
+ *
+ * returns:
+ *      0 on success
+ *      Negative error code on failure
+ */
+static int overlay_fixup_phandles(void *fdt, void *fdto)
+{
+       int fixups_off, symbols_off;
+       int property;
+
+       /* We can have overlays without any fixups */
+       fixups_off = fdt_path_offset(fdto, "/__fixups__");
+       if (fixups_off == -FDT_ERR_NOTFOUND)
+               return 0; /* nothing to do */
+       if (fixups_off < 0)
+               return fixups_off;
+
+       /* And base DTs without symbols */
+       symbols_off = fdt_path_offset(fdt, "/__symbols__");
+       if ((symbols_off < 0 && (symbols_off != -FDT_ERR_NOTFOUND)))
+               return symbols_off;
+
+       fdt_for_each_property_offset(property, fdto, fixups_off) {
+               int ret;
+
+               ret = overlay_fixup_phandle(fdt, fdto, symbols_off, property);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+
+/**
+ * overlay_apply_node - Merges a node into the base device tree
+ * @fdt: Base Device Tree blob
+ * @target: Node offset in the base device tree to apply the fragment to
+ * @fdto: Device tree overlay blob
+ * @node: Node offset in the overlay holding the changes to merge
+ *
+ * overlay_apply_node() merges a node into a target base device tree
+ * node pointed.
+ *
+ * This is part of the final step in the device tree overlay
+ * application process, when all the phandles have been adjusted and
+ * resolved and you just have to merge overlay into the base device
+ * tree.
+ *
+ * returns:
+ *      0 on success
+ *      Negative error code on failure
+ */
+static int overlay_apply_node(void *fdt, int target,
+                             void *fdto, int node)
+{
+       int property;
+       int subnode;
+
+       fdt_for_each_property_offset(property, fdto, node) {
+               const char *name;
+               const void *prop;
+               int prop_len;
+               int ret;
+
+               prop = fdt_getprop_by_offset(fdto, property, &name,
+                                            &prop_len);
+               if (prop_len == -FDT_ERR_NOTFOUND)
+                       return -FDT_ERR_INTERNAL;
+               if (prop_len < 0)
+                       return prop_len;
+
+               ret = fdt_setprop(fdt, target, name, prop, prop_len);
+               if (ret)
+                       return ret;
+       }
+
+       fdt_for_each_subnode(subnode, fdto, node) {
+               const char *name = fdt_get_name(fdto, subnode, NULL);
+               int nnode;
+               int ret;
+
+               nnode = fdt_add_subnode(fdt, target, name);
+               if (nnode == -FDT_ERR_EXISTS) {
+                       nnode = fdt_subnode_offset(fdt, target, name);
+                       if (nnode == -FDT_ERR_NOTFOUND)
+                               return -FDT_ERR_INTERNAL;
+               }
+
+               if (nnode < 0)
+                       return nnode;
+
+               ret = overlay_apply_node(fdt, nnode, fdto, subnode);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+
+/**
+ * overlay_merge - Merge an overlay into its base device tree
+ * @fdt: Base Device Tree blob
+ * @fdto: Device tree overlay blob
+ *
+ * overlay_merge() merges an overlay into its base device tree.
+ *
+ * This is the next to last step in the device tree overlay application
+ * process, when all the phandles have been adjusted and resolved and
+ * you just have to merge overlay into the base device tree.
+ *
+ * returns:
+ *      0 on success
+ *      Negative error code on failure
+ */
+static int overlay_merge(void *fdt, void *fdto)
+{
+       int fragment;
+
+       fdt_for_each_subnode(fragment, fdto, 0) {
+               int overlay;
+               int target;
+               int ret;
+
+               /*
+                * Each fragments will have an __overlay__ node. If
+                * they don't, it's not supposed to be merged
+                */
+               overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
+               if (overlay == -FDT_ERR_NOTFOUND)
+                       continue;
+
+               if (overlay < 0)
+                       return overlay;
+
+               target = overlay_get_target(fdt, fdto, fragment, NULL);
+               if (target < 0)
+                       return target;
+
+               ret = overlay_apply_node(fdt, target, fdto, overlay);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+
+static int get_path_len(const void *fdt, int nodeoffset)
+{
+       int len = 0, namelen;
+       const char *name;
+
+       FDT_RO_PROBE(fdt);
+
+       for (;;) {
+               name = fdt_get_name(fdt, nodeoffset, &namelen);
+               if (!name)
+                       return namelen;
+
+               /* root? we're done */
+               if (namelen == 0)
+                       break;
+
+               nodeoffset = fdt_parent_offset(fdt, nodeoffset);
+               if (nodeoffset < 0)
+                       return nodeoffset;
+               len += namelen + 1;
+       }
+
+       /* in case of root pretend it's "/" */
+       if (len == 0)
+               len++;
+       return len;
+}
+
+/**
+ * overlay_symbol_update - Update the symbols of base tree after a merge
+ * @fdt: Base Device Tree blob
+ * @fdto: Device tree overlay blob
+ *
+ * overlay_symbol_update() updates the symbols of the base tree with the
+ * symbols of the applied overlay
+ *
+ * This is the last step in the device tree overlay application
+ * process, allowing the reference of overlay symbols by subsequent
+ * overlay operations.
+ *
+ * returns:
+ *      0 on success
+ *      Negative error code on failure
+ */
+static int overlay_symbol_update(void *fdt, void *fdto)
+{
+       int root_sym, ov_sym, prop, path_len, fragment, target;
+       int len, frag_name_len, ret, rel_path_len;
+       const char *s, *e;
+       const char *path;
+       const char *name;
+       const char *frag_name;
+       const char *rel_path;
+       const char *target_path;
+       char *buf;
+       void *p;
+
+       ov_sym = fdt_subnode_offset(fdto, 0, "__symbols__");
+
+       /* if no overlay symbols exist no problem */
+       if (ov_sym < 0)
+               return 0;
+
+       root_sym = fdt_subnode_offset(fdt, 0, "__symbols__");
+
+       /* it no root symbols exist we should create them */
+       if (root_sym == -FDT_ERR_NOTFOUND)
+               root_sym = fdt_add_subnode(fdt, 0, "__symbols__");
+
+       /* any error is fatal now */
+       if (root_sym < 0)
+               return root_sym;
+
+       /* iterate over each overlay symbol */
+       fdt_for_each_property_offset(prop, fdto, ov_sym) {
+               path = fdt_getprop_by_offset(fdto, prop, &name, &path_len);
+               if (!path)
+                       return path_len;
+
+               /* verify it's a string property (terminated by a single \0) */
+               if (path_len < 1 || memchr(path, '\0', path_len) != 
&path[path_len - 1])
+                       return -FDT_ERR_BADVALUE;
+
+               /* keep end marker to avoid strlen() */
+               e = path + path_len;
+
+               if (*path != '/')
+                       return -FDT_ERR_BADVALUE;
+
+               /* get fragment name first */
+               s = strchr(path + 1, '/');
+               if (!s) {
+                       /* Symbol refers to something that won't end
+                        * up in the target tree */
+                       continue;
+               }
+
+               frag_name = path + 1;
+               frag_name_len = s - path - 1;
+
+               /* verify format; safe since "s" lies in \0 terminated prop */
+               len = sizeof("/__overlay__/") - 1;
+               if ((e - s) > len && (memcmp(s, "/__overlay__/", len) == 0)) {
+                       /* /<fragment-name>/__overlay__/<relative-subnode-path> 
*/
+                       rel_path = s + len;
+                       rel_path_len = e - rel_path - 1;
+               } else if ((e - s) == len
+                          && (memcmp(s, "/__overlay__", len - 1) == 0)) {
+                       /* /<fragment-name>/__overlay__ */
+                       rel_path = "";
+                       rel_path_len = 0;
+               } else {
+                       /* Symbol refers to something that won't end
+                        * up in the target tree */
+                       continue;
+               }
+
+               /* find the fragment index in which the symbol lies */
+               ret = fdt_subnode_offset_namelen(fdto, 0, frag_name,
+                                              frag_name_len);
+               /* not found? */
+               if (ret < 0)
+                       return -FDT_ERR_BADOVERLAY;
+               fragment = ret;
+
+               /* an __overlay__ subnode must exist */
+               ret = fdt_subnode_offset(fdto, fragment, "__overlay__");
+               if (ret < 0)
+                       return -FDT_ERR_BADOVERLAY;
+
+               /* get the target of the fragment */
+               ret = overlay_get_target(fdt, fdto, fragment, &target_path);
+               if (ret < 0)
+                       return ret;
+               target = ret;
+
+               /* if we have a target path use */
+               if (!target_path) {
+                       ret = get_path_len(fdt, target);
+                       if (ret < 0)
+                               return ret;
+                       len = ret;
+               } else {
+                       len = strlen(target_path);
+               }
+
+               ret = fdt_setprop_placeholder(fdt, root_sym, name,
+                               len + (len > 1) + rel_path_len + 1, &p);
+               if (ret < 0)
+                       return ret;
+
+               if (!target_path) {
+                       /* again in case setprop_placeholder changed it */
+                       ret = overlay_get_target(fdt, fdto, fragment, 
&target_path);
+                       if (ret < 0)
+                               return ret;
+                       target = ret;
+               }
+
+               buf = p;
+               if (len > 1) { /* target is not root */
+                       if (!target_path) {
+                               ret = fdt_get_path(fdt, target, buf, len + 1);
+                               if (ret < 0)
+                                       return ret;
+                       } else
+                               memcpy(buf, target_path, len + 1);
+
+               } else
+                       len--;
+
+               buf[len] = '/';
+               memcpy(buf + len + 1, rel_path, rel_path_len);
+               buf[len + 1 + rel_path_len] = '\0';
+       }
+
+       return 0;
+}
+
+int fdt_overlay_apply(void *fdt, void *fdto)
+{
+       uint32_t delta;
+       int ret;
+
+       FDT_RO_PROBE(fdt);
+       FDT_RO_PROBE(fdto);
+
+       ret = fdt_find_max_phandle(fdt, &delta);
+       if (ret)
+               goto err;
+
+       ret = overlay_adjust_local_phandles(fdto, delta);
+       if (ret)
+               goto err;
+
+       ret = overlay_update_local_references(fdto, delta);
+       if (ret)
+               goto err;
+
+       ret = overlay_fixup_phandles(fdt, fdto);
+       if (ret)
+               goto err;
+
+       ret = overlay_merge(fdt, fdto);
+       if (ret)
+               goto err;
+
+       ret = overlay_symbol_update(fdt, fdto);
+       if (ret)
+               goto err;
+
+       /*
+        * The overlay has been damaged, erase its magic.
+        */
+       fdt_set_magic(fdto, ~0);
+
+       return 0;
+
+err:
+       /*
+        * The overlay might have been damaged, erase its magic.
+        */
+       fdt_set_magic(fdto, ~0);
+
+       /*
+        * The base device tree might have been damaged, erase its
+        * magic.
+        */
+       fdt_set_magic(fdt, ~0);
+
+       return ret;
+}
diff --git a/xen/common/libfdt/fdt_ro.c b/xen/common/libfdt/fdt_ro.c
index 36f9b480d1..17584da257 100644
--- a/xen/common/libfdt/fdt_ro.c
+++ b/xen/common/libfdt/fdt_ro.c
@@ -1,50 +1,7 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
 /*
  * libfdt - Flat Device Tree manipulation
  * Copyright (C) 2006 David Gibson, IBM Corporation.
- *
- * libfdt is dual licensed: you can use it either under the terms of
- * the GPL, or the BSD license, at your option.
- *
- *  a) This library 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 library 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 library; If not, see 
<http://www.gnu.org/licenses/>.
- *
- * Alternatively,
- *
- *  b) Redistribution and use in source and binary forms, with or
- *     without modification, are permitted provided that the following
- *     conditions are met:
- *
- *     1. Redistributions of source code must retain the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer.
- *     2. Redistributions in binary form must reproduce the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer in the documentation and/or other materials
- *        provided with the distribution.
- *
- *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 #include "libfdt_env.h"
 
@@ -53,12 +10,13 @@
 
 #include "libfdt_internal.h"
 
-static int _fdt_nodename_eq(const void *fdt, int offset,
+static int fdt_nodename_eq_(const void *fdt, int offset,
                            const char *s, int len)
 {
-       const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
+       int olen;
+       const char *p = fdt_get_name(fdt, offset, &olen);
 
-       if (! p)
+       if (!p || olen < len)
                /* short match */
                return 0;
 
@@ -73,37 +31,174 @@ static int _fdt_nodename_eq(const void *fdt, int offset,
                return 0;
 }
 
+const char *fdt_get_string(const void *fdt, int stroffset, int *lenp)
+{
+       int32_t totalsize;
+       uint32_t absoffset;
+       size_t len;
+       int err;
+       const char *s, *n;
+
+       if (can_assume(VALID_INPUT)) {
+               s = (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
+
+               if (lenp)
+                       *lenp = strlen(s);
+               return s;
+       }
+       totalsize = fdt_ro_probe_(fdt);
+       err = totalsize;
+       if (totalsize < 0)
+               goto fail;
+
+       err = -FDT_ERR_BADOFFSET;
+       absoffset = stroffset + fdt_off_dt_strings(fdt);
+       if (absoffset >= (unsigned)totalsize)
+               goto fail;
+       len = totalsize - absoffset;
+
+       if (fdt_magic(fdt) == FDT_MAGIC) {
+               if (stroffset < 0)
+                       goto fail;
+               if (can_assume(LATEST) || fdt_version(fdt) >= 17) {
+                       if ((unsigned)stroffset >= fdt_size_dt_strings(fdt))
+                               goto fail;
+                       if ((fdt_size_dt_strings(fdt) - stroffset) < len)
+                               len = fdt_size_dt_strings(fdt) - stroffset;
+               }
+       } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
+               unsigned int sw_stroffset = -stroffset;
+
+               if ((stroffset >= 0) ||
+                   (sw_stroffset > fdt_size_dt_strings(fdt)))
+                       goto fail;
+               if (sw_stroffset < len)
+                       len = sw_stroffset;
+       } else {
+               err = -FDT_ERR_INTERNAL;
+               goto fail;
+       }
+
+       s = (const char *)fdt + absoffset;
+       n = memchr(s, '\0', len);
+       if (!n) {
+               /* missing terminating NULL */
+               err = -FDT_ERR_TRUNCATED;
+               goto fail;
+       }
+
+       if (lenp)
+               *lenp = n - s;
+       return s;
+
+fail:
+       if (lenp)
+               *lenp = err;
+       return NULL;
+}
+
 const char *fdt_string(const void *fdt, int stroffset)
 {
-       return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
+       return fdt_get_string(fdt, stroffset, NULL);
 }
 
-static int _fdt_string_eq(const void *fdt, int stroffset,
+static int fdt_string_eq_(const void *fdt, int stroffset,
                          const char *s, int len)
 {
-       const char *p = fdt_string(fdt, stroffset);
+       int slen;
+       const char *p = fdt_get_string(fdt, stroffset, &slen);
+
+       return p && (slen == len) && (memcmp(p, s, len) == 0);
+}
+
+int fdt_find_max_phandle(const void *fdt, uint32_t *phandle)
+{
+       uint32_t max = 0;
+       int offset = -1;
+
+       while (true) {
+               uint32_t value;
+
+               offset = fdt_next_node(fdt, offset, NULL);
+               if (offset < 0) {
+                       if (offset == -FDT_ERR_NOTFOUND)
+                               break;
+
+                       return offset;
+               }
+
+               value = fdt_get_phandle(fdt, offset);
+
+               if (value > max)
+                       max = value;
+       }
+
+       if (phandle)
+               *phandle = max;
+
+       return 0;
+}
+
+int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
+{
+       uint32_t max;
+       int err;
+
+       err = fdt_find_max_phandle(fdt, &max);
+       if (err < 0)
+               return err;
+
+       if (max == FDT_MAX_PHANDLE)
+               return -FDT_ERR_NOPHANDLES;
+
+       if (phandle)
+               *phandle = max + 1;
+
+       return 0;
+}
 
-       return (strlen(p) == len) && (memcmp(p, s, len) == 0);
+static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+{
+       unsigned int offset = n * sizeof(struct fdt_reserve_entry);
+       unsigned int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
+
+       if (!can_assume(VALID_INPUT)) {
+               if (absoffset < fdt_off_mem_rsvmap(fdt))
+                       return NULL;
+               if (absoffset > fdt_totalsize(fdt) -
+                   sizeof(struct fdt_reserve_entry))
+                       return NULL;
+       }
+       return fdt_mem_rsv_(fdt, n);
 }
 
 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
 {
-       FDT_CHECK_HEADER(fdt);
-       *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
-       *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
+       const struct fdt_reserve_entry *re;
+
+       FDT_RO_PROBE(fdt);
+       re = fdt_mem_rsv(fdt, n);
+       if (!can_assume(VALID_INPUT) && !re)
+               return -FDT_ERR_BADOFFSET;
+
+       *address = fdt64_ld_(&re->address);
+       *size = fdt64_ld_(&re->size);
        return 0;
 }
 
 int fdt_num_mem_rsv(const void *fdt)
 {
-       int i = 0;
+       int i;
+       const struct fdt_reserve_entry *re;
 
-       while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
-               i++;
-       return i;
+       for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
+               if (fdt64_ld_(&re->size) == 0)
+                       return i;
+       }
+       return -FDT_ERR_TRUNCATED;
 }
 
-static int _nextprop(const void *fdt, int offset)
+static int nextprop_(const void *fdt, int offset)
 {
        uint32_t tag;
        int nextoffset;
@@ -132,13 +227,13 @@ int fdt_subnode_offset_namelen(const void *fdt, int 
offset,
 {
        int depth;
 
-       FDT_CHECK_HEADER(fdt);
+       FDT_RO_PROBE(fdt);
 
        for (depth = 0;
             (offset >= 0) && (depth >= 0);
             offset = fdt_next_node(fdt, offset, &depth))
                if ((depth == 1)
-                   && _fdt_nodename_eq(fdt, offset, name, namelen))
+                   && fdt_nodename_eq_(fdt, offset, name, namelen))
                        return offset;
 
        if (depth < 0)
@@ -152,17 +247,17 @@ int fdt_subnode_offset(const void *fdt, int parentoffset,
        return fdt_subnode_offset_namelen(fdt, parentoffset, name, 
strlen(name));
 }
 
-int fdt_path_offset(const void *fdt, const char *path)
+int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
 {
-       const char *end = path + strlen(path);
+       const char *end = path + namelen;
        const char *p = path;
        int offset = 0;
 
-       FDT_CHECK_HEADER(fdt);
+       FDT_RO_PROBE(fdt);
 
        /* see if we have an alias */
        if (*path != '/') {
-               const char *q = strchr(path, '/');
+               const char *q = memchr(path, '/', end - p);
 
                if (!q)
                        q = end;
@@ -175,14 +270,15 @@ int fdt_path_offset(const void *fdt, const char *path)
                p = q;
        }
 
-       while (*p) {
+       while (p < end) {
                const char *q;
 
-               while (*p == '/')
+               while (*p == '/') {
                        p++;
-               if (! *p)
-                       return offset;
-               q = strchr(p, '/');
+                       if (p == end)
+                               return offset;
+               }
+               q = memchr(p, '/', end - p);
                if (! q)
                        q = end;
 
@@ -196,19 +292,42 @@ int fdt_path_offset(const void *fdt, const char *path)
        return offset;
 }
 
+int fdt_path_offset(const void *fdt, const char *path)
+{
+       return fdt_path_offset_namelen(fdt, path, strlen(path));
+}
+
 const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
 {
-       const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
+       const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
+       const char *nameptr;
        int err;
 
-       if (((err = fdt_check_header(fdt)) != 0)
-           || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
+       if (((err = fdt_ro_probe_(fdt)) < 0)
+           || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0))
+                       goto fail;
+
+       nameptr = nh->name;
+
+       if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
+               /*
+                * For old FDT versions, match the naming conventions of V16:
+                * give only the leaf name (after all /). The actual tree
+                * contents are loosely checked.
+                */
+               const char *leaf;
+               leaf = strrchr(nameptr, '/');
+               if (leaf == NULL) {
+                       err = -FDT_ERR_BADSTRUCTURE;
                        goto fail;
+               }
+               nameptr = leaf+1;
+       }
 
        if (len)
-               *len = strlen(nh->name);
+               *len = strlen(nameptr);
 
-       return nh->name;
+       return nameptr;
 
  fail:
        if (len)
@@ -220,58 +339,81 @@ int fdt_first_property_offset(const void *fdt, int 
nodeoffset)
 {
        int offset;
 
-       if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
+       if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
                return offset;
 
-       return _nextprop(fdt, offset);
+       return nextprop_(fdt, offset);
 }
 
 int fdt_next_property_offset(const void *fdt, int offset)
 {
-       if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0)
+       if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
                return offset;
 
-       return _nextprop(fdt, offset);
+       return nextprop_(fdt, offset);
 }
 
-const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
-                                                     int offset,
-                                                     int *lenp)
+static const struct fdt_property *fdt_get_property_by_offset_(const void *fdt,
+                                                             int offset,
+                                                             int *lenp)
 {
        int err;
        const struct fdt_property *prop;
 
-       if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) {
+       if (!can_assume(VALID_INPUT) &&
+           (err = fdt_check_prop_offset_(fdt, offset)) < 0) {
                if (lenp)
                        *lenp = err;
                return NULL;
        }
 
-       prop = _fdt_offset_ptr(fdt, offset);
+       prop = fdt_offset_ptr_(fdt, offset);
 
        if (lenp)
-               *lenp = fdt32_to_cpu(prop->len);
+               *lenp = fdt32_ld_(&prop->len);
 
        return prop;
 }
 
-const struct fdt_property *fdt_get_property_namelen(const void *fdt,
-                                                   int offset,
-                                                   const char *name,
-                                                   int namelen, int *lenp)
+const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
+                                                     int offset,
+                                                     int *lenp)
+{
+       /* Prior to version 16, properties may need realignment
+        * and this API does not work. fdt_getprop_*() will, however. */
+
+       if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
+               if (lenp)
+                       *lenp = -FDT_ERR_BADVERSION;
+               return NULL;
+       }
+
+       return fdt_get_property_by_offset_(fdt, offset, lenp);
+}
+
+static const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
+                                                           int offset,
+                                                           const char *name,
+                                                           int namelen,
+                                                           int *lenp,
+                                                           int *poffset)
 {
        for (offset = fdt_first_property_offset(fdt, offset);
             (offset >= 0);
             (offset = fdt_next_property_offset(fdt, offset))) {
                const struct fdt_property *prop;
 
-               if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
+               prop = fdt_get_property_by_offset_(fdt, offset, lenp);
+               if (!can_assume(LIBFDT_FLAWLESS) && !prop) {
                        offset = -FDT_ERR_INTERNAL;
                        break;
                }
-               if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
-                                  name, namelen))
+               if (fdt_string_eq_(fdt, fdt32_ld_(&prop->nameoff),
+                                  name, namelen)) {
+                       if (poffset)
+                               *poffset = offset;
                        return prop;
+               }
        }
 
        if (lenp)
@@ -279,6 +421,25 @@ const struct fdt_property *fdt_get_property_namelen(const 
void *fdt,
        return NULL;
 }
 
+
+const struct fdt_property *fdt_get_property_namelen(const void *fdt,
+                                                   int offset,
+                                                   const char *name,
+                                                   int namelen, int *lenp)
+{
+       /* Prior to version 16, properties may need realignment
+        * and this API does not work. fdt_getprop_*() will, however. */
+       if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
+               if (lenp)
+                       *lenp = -FDT_ERR_BADVERSION;
+               return NULL;
+       }
+
+       return fdt_get_property_namelen_(fdt, offset, name, namelen, lenp,
+                                        NULL);
+}
+
+
 const struct fdt_property *fdt_get_property(const void *fdt,
                                            int nodeoffset,
                                            const char *name, int *lenp)
@@ -290,12 +451,18 @@ const struct fdt_property *fdt_get_property(const void 
*fdt,
 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
                                const char *name, int namelen, int *lenp)
 {
+       int poffset;
        const struct fdt_property *prop;
 
-       prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
-       if (! prop)
+       prop = fdt_get_property_namelen_(fdt, nodeoffset, name, namelen, lenp,
+                                        &poffset);
+       if (!prop)
                return NULL;
 
+       /* Handle realignment */
+       if (!can_assume(LATEST) && fdt_version(fdt) < 0x10 &&
+           (poffset + sizeof(*prop)) % 8 && fdt32_ld_(&prop->len) >= 8)
+               return prop->data + 4;
        return prop->data;
 }
 
@@ -304,11 +471,31 @@ const void *fdt_getprop_by_offset(const void *fdt, int 
offset,
 {
        const struct fdt_property *prop;
 
-       prop = fdt_get_property_by_offset(fdt, offset, lenp);
+       prop = fdt_get_property_by_offset_(fdt, offset, lenp);
        if (!prop)
                return NULL;
-       if (namep)
-               *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
+       if (namep) {
+               const char *name;
+               int namelen;
+
+               if (!can_assume(VALID_INPUT)) {
+                       name = fdt_get_string(fdt, fdt32_ld_(&prop->nameoff),
+                                             &namelen);
+                       if (!name) {
+                               if (lenp)
+                                       *lenp = namelen;
+                               return NULL;
+                       }
+                       *namep = name;
+               } else {
+                       *namep = fdt_string(fdt, fdt32_ld_(&prop->nameoff));
+               }
+       }
+
+       /* Handle realignment */
+       if (!can_assume(LATEST) && fdt_version(fdt) < 0x10 &&
+           (offset + sizeof(*prop)) % 8 && fdt32_ld_(&prop->len) >= 8)
+               return prop->data + 4;
        return prop->data;
 }
 
@@ -332,7 +519,7 @@ uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
                        return 0;
        }
 
-       return fdt32_to_cpu(*php);
+       return fdt32_ld_(php);
 }
 
 const char *fdt_get_alias_namelen(const void *fdt,
@@ -358,7 +545,7 @@ int fdt_get_path(const void *fdt, int nodeoffset, char 
*buf, int buflen)
        int offset, depth, namelen;
        const char *name;
 
-       FDT_CHECK_HEADER(fdt);
+       FDT_RO_PROBE(fdt);
 
        if (buflen < 2)
                return -FDT_ERR_NOSPACE;
@@ -410,7 +597,7 @@ int fdt_supernode_atdepth_offset(const void *fdt, int 
nodeoffset,
        int offset, depth;
        int supernodeoffset = -FDT_ERR_INTERNAL;
 
-       FDT_CHECK_HEADER(fdt);
+       FDT_RO_PROBE(fdt);
 
        if (supernodedepth < 0)
                return -FDT_ERR_NOTFOUND;
@@ -432,10 +619,12 @@ int fdt_supernode_atdepth_offset(const void *fdt, int 
nodeoffset,
                }
        }
 
-       if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
-               return -FDT_ERR_BADOFFSET;
-       else if (offset == -FDT_ERR_BADOFFSET)
-               return -FDT_ERR_BADSTRUCTURE;
+       if (!can_assume(VALID_INPUT)) {
+               if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
+                       return -FDT_ERR_BADOFFSET;
+               else if (offset == -FDT_ERR_BADOFFSET)
+                       return -FDT_ERR_BADSTRUCTURE;
+       }
 
        return offset; /* error from fdt_next_node() */
 }
@@ -447,7 +636,8 @@ int fdt_node_depth(const void *fdt, int nodeoffset)
 
        err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
        if (err)
-               return (err < 0) ? err : -FDT_ERR_INTERNAL;
+               return (can_assume(LIBFDT_FLAWLESS) || err < 0) ? err :
+                       -FDT_ERR_INTERNAL;
        return nodedepth;
 }
 
@@ -469,7 +659,7 @@ int fdt_node_offset_by_prop_value(const void *fdt, int 
startoffset,
        const void *val;
        int len;
 
-       FDT_CHECK_HEADER(fdt);
+       FDT_RO_PROBE(fdt);
 
        /* FIXME: The algorithm here is pretty horrible: we scan each
         * property of a node in fdt_getprop(), then if that didn't
@@ -492,10 +682,10 @@ int fdt_node_offset_by_phandle(const void *fdt, uint32_t 
phandle)
 {
        int offset;
 
-       if ((phandle == 0) || (phandle == -1))
+       if ((phandle == 0) || (phandle == ~0U))
                return -FDT_ERR_BADPHANDLE;
 
-       FDT_CHECK_HEADER(fdt);
+       FDT_RO_PROBE(fdt);
 
        /* FIXME: The algorithm here is pretty horrible: we
         * potentially scan each property of a node in
@@ -530,6 +720,106 @@ int fdt_stringlist_contains(const char *strlist, int 
listlen, const char *str)
        return 0;
 }
 
+int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
+{
+       const char *list, *end;
+       int length, count = 0;
+
+       list = fdt_getprop(fdt, nodeoffset, property, &length);
+       if (!list)
+               return length;
+
+       end = list + length;
+
+       while (list < end) {
+               length = strnlen(list, end - list) + 1;
+
+               /* Abort if the last string isn't properly NUL-terminated. */
+               if (list + length > end)
+                       return -FDT_ERR_BADVALUE;
+
+               list += length;
+               count++;
+       }
+
+       return count;
+}
+
+int fdt_stringlist_search(const void *fdt, int nodeoffset, const char 
*property,
+                         const char *string)
+{
+       int length, len, idx = 0;
+       const char *list, *end;
+
+       list = fdt_getprop(fdt, nodeoffset, property, &length);
+       if (!list)
+               return length;
+
+       len = strlen(string) + 1;
+       end = list + length;
+
+       while (list < end) {
+               length = strnlen(list, end - list) + 1;
+
+               /* Abort if the last string isn't properly NUL-terminated. */
+               if (list + length > end)
+                       return -FDT_ERR_BADVALUE;
+
+               if (length == len && memcmp(list, string, length) == 0)
+                       return idx;
+
+               list += length;
+               idx++;
+       }
+
+       return -FDT_ERR_NOTFOUND;
+}
+
+const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
+                              const char *property, int idx,
+                              int *lenp)
+{
+       const char *list, *end;
+       int length;
+
+       list = fdt_getprop(fdt, nodeoffset, property, &length);
+       if (!list) {
+               if (lenp)
+                       *lenp = length;
+
+               return NULL;
+       }
+
+       end = list + length;
+
+       while (list < end) {
+               length = strnlen(list, end - list) + 1;
+
+               /* Abort if the last string isn't properly NUL-terminated. */
+               if (list + length > end) {
+                       if (lenp)
+                               *lenp = -FDT_ERR_BADVALUE;
+
+                       return NULL;
+               }
+
+               if (idx == 0) {
+                       if (lenp)
+                               *lenp = length - 1;
+
+                       return list;
+               }
+
+               list += length;
+               idx--;
+       }
+
+       if (lenp)
+               *lenp = -FDT_ERR_NOTFOUND;
+
+       return NULL;
+}
+
 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
                              const char *compatible)
 {
@@ -539,10 +829,8 @@ int fdt_node_check_compatible(const void *fdt, int 
nodeoffset,
        prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
        if (!prop)
                return len;
-       if (fdt_stringlist_contains(prop, len, compatible))
-               return 0;
-       else
-               return 1;
+
+       return !fdt_stringlist_contains(prop, len, compatible);
 }
 
 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
@@ -550,7 +838,7 @@ int fdt_node_offset_by_compatible(const void *fdt, int 
startoffset,
 {
        int offset, err;
 
-       FDT_CHECK_HEADER(fdt);
+       FDT_RO_PROBE(fdt);
 
        /* FIXME: The algorithm here is pretty horrible: we scan each
         * property of a node in fdt_node_check_compatible(), then if
diff --git a/xen/common/libfdt/fdt_rw.c b/xen/common/libfdt/fdt_rw.c
index 8b8cd25cc2..3621d3651d 100644
--- a/xen/common/libfdt/fdt_rw.c
+++ b/xen/common/libfdt/fdt_rw.c
@@ -1,50 +1,7 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
 /*
  * libfdt - Flat Device Tree manipulation
  * Copyright (C) 2006 David Gibson, IBM Corporation.
- *
- * libfdt is dual licensed: you can use it either under the terms of
- * the GPL, or the BSD license, at your option.
- *
- *  a) This library 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 library 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 library; If not, see 
<http://www.gnu.org/licenses/>.
- *
- * Alternatively,
- *
- *  b) Redistribution and use in source and binary forms, with or
- *     without modification, are permitted provided that the following
- *     conditions are met:
- *
- *     1. Redistributions of source code must retain the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer.
- *     2. Redistributions in binary form must reproduce the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer in the documentation and/or other materials
- *        provided with the distribution.
- *
- *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 #include "libfdt_env.h"
 
@@ -53,8 +10,8 @@
 
 #include "libfdt_internal.h"
 
-static int _fdt_blocks_misordered(const void *fdt,
-                             int mem_rsv_size, int struct_size)
+static int fdt_blocks_misordered_(const void *fdt,
+                                 int mem_rsv_size, int struct_size)
 {
        return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 
8))
                || (fdt_off_dt_struct(fdt) <
@@ -65,40 +22,44 @@ static int _fdt_blocks_misordered(const void *fdt,
                    (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
 }
 
-static int _fdt_rw_check_header(void *fdt)
+static int fdt_rw_probe_(void *fdt)
 {
-       FDT_CHECK_HEADER(fdt);
+       if (can_assume(VALID_DTB))
+               return 0;
+       FDT_RO_PROBE(fdt);
 
-       if (fdt_version(fdt) < 17)
+       if (!can_assume(LATEST) && fdt_version(fdt) < 17)
                return -FDT_ERR_BADVERSION;
-       if (_fdt_blocks_misordered(fdt, sizeof(struct fdt_reserve_entry),
+       if (fdt_blocks_misordered_(fdt, sizeof(struct fdt_reserve_entry),
                                   fdt_size_dt_struct(fdt)))
                return -FDT_ERR_BADLAYOUT;
-       if (fdt_version(fdt) > 17)
+       if (!can_assume(LATEST) && fdt_version(fdt) > 17)
                fdt_set_version(fdt, 17);
 
        return 0;
 }
 
-#define FDT_RW_CHECK_HEADER(fdt) \
+#define FDT_RW_PROBE(fdt) \
        { \
-               int err; \
-               if ((err = _fdt_rw_check_header(fdt)) != 0) \
-                       return err; \
+               int err_; \
+               if ((err_ = fdt_rw_probe_(fdt)) != 0) \
+                       return err_; \
        }
 
-static inline unsigned int _fdt_data_size(void *fdt)
+static inline unsigned int fdt_data_size_(void *fdt)
 {
        return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
 }
 
-static int _fdt_splice(void *fdt, void *splicepoint, int oldlen, int newlen)
+static int fdt_splice_(void *fdt, void *splicepoint, int oldlen, int newlen)
 {
        char *p = splicepoint;
-       unsigned int dsize = _fdt_data_size(fdt);
+       unsigned int dsize = fdt_data_size_(fdt);
        size_t soff = p - (char *)fdt;
 
-       if (oldlen < 0 || soff + oldlen < soff || soff + oldlen > dsize)
+       if ((oldlen < 0) || (soff + oldlen < soff) || (soff + oldlen > dsize))
+               return -FDT_ERR_BADOFFSET;
+       if ((p < (char *)fdt) || (dsize + newlen < (unsigned)oldlen))
                return -FDT_ERR_BADOFFSET;
        if (dsize - oldlen + newlen > fdt_totalsize(fdt))
                return -FDT_ERR_NOSPACE;
@@ -106,12 +67,12 @@ static int _fdt_splice(void *fdt, void *splicepoint, int 
oldlen, int newlen)
        return 0;
 }
 
-static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p,
+static int fdt_splice_mem_rsv_(void *fdt, struct fdt_reserve_entry *p,
                               int oldn, int newn)
 {
        int delta = (newn - oldn) * sizeof(*p);
        int err;
-       err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
+       err = fdt_splice_(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
        if (err)
                return err;
        fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
@@ -119,13 +80,13 @@ static int _fdt_splice_mem_rsv(void *fdt, struct 
fdt_reserve_entry *p,
        return 0;
 }
 
-static int _fdt_splice_struct(void *fdt, void *p,
+static int fdt_splice_struct_(void *fdt, void *p,
                              int oldlen, int newlen)
 {
        int delta = newlen - oldlen;
        int err;
 
-       if ((err = _fdt_splice(fdt, p, oldlen, newlen)))
+       if ((err = fdt_splice_(fdt, p, oldlen, newlen)))
                return err;
 
        fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
@@ -133,20 +94,37 @@ static int _fdt_splice_struct(void *fdt, void *p,
        return 0;
 }
 
-static int _fdt_splice_string(void *fdt, int newlen)
+/* Must only be used to roll back in case of error */
+static void fdt_del_last_string_(void *fdt, const char *s)
+{
+       int newlen = strlen(s) + 1;
+
+       fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) - newlen);
+}
+
+static int fdt_splice_string_(void *fdt, int newlen)
 {
        void *p = (char *)fdt
                + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
        int err;
 
-       if ((err = _fdt_splice(fdt, p, 0, newlen)))
+       if ((err = fdt_splice_(fdt, p, 0, newlen)))
                return err;
 
        fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
        return 0;
 }
 
-static int _fdt_find_add_string(void *fdt, const char *s)
+/**
+ * fdt_find_add_string_() - Find or allocate a string
+ *
+ * @fdt: pointer to the device tree to check/adjust
+ * @s: string to find/add
+ * @allocated: Set to 0 if the string was found, 1 if not found and so
+ *     allocated. Ignored if can_assume(NO_ROLLBACK)
+ * @return offset of string in the string table (whether found or added)
+ */
+static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
 {
        char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
        const char *p;
@@ -154,16 +132,22 @@ static int _fdt_find_add_string(void *fdt, const char *s)
        int len = strlen(s) + 1;
        int err;
 
-       p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
+       if (!can_assume(NO_ROLLBACK))
+               *allocated = 0;
+
+       p = fdt_find_string_(strtab, fdt_size_dt_strings(fdt), s);
        if (p)
                /* found it */
                return (p - strtab);
 
        new = strtab + fdt_size_dt_strings(fdt);
-       err = _fdt_splice_string(fdt, len);
+       err = fdt_splice_string_(fdt, len);
        if (err)
                return err;
 
+       if (!can_assume(NO_ROLLBACK))
+               *allocated = 1;
+
        memcpy(new, s, len);
        return (new - strtab);
 }
@@ -173,10 +157,10 @@ int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t 
size)
        struct fdt_reserve_entry *re;
        int err;
 
-       FDT_RW_CHECK_HEADER(fdt);
+       FDT_RW_PROBE(fdt);
 
-       re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt));
-       err = _fdt_splice_mem_rsv(fdt, re, 0, 1);
+       re = fdt_mem_rsv_w_(fdt, fdt_num_mem_rsv(fdt));
+       err = fdt_splice_mem_rsv_(fdt, re, 0, 1);
        if (err)
                return err;
 
@@ -187,31 +171,27 @@ int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t 
size)
 
 int fdt_del_mem_rsv(void *fdt, int n)
 {
-       struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n);
-       int err;
+       struct fdt_reserve_entry *re = fdt_mem_rsv_w_(fdt, n);
 
-       FDT_RW_CHECK_HEADER(fdt);
+       FDT_RW_PROBE(fdt);
 
        if (n >= fdt_num_mem_rsv(fdt))
                return -FDT_ERR_NOTFOUND;
 
-       err = _fdt_splice_mem_rsv(fdt, re, 1, 0);
-       if (err)
-               return err;
-       return 0;
+       return fdt_splice_mem_rsv_(fdt, re, 1, 0);
 }
 
-static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name,
+static int fdt_resize_property_(void *fdt, int nodeoffset, const char *name,
                                int len, struct fdt_property **prop)
 {
        int oldlen;
        int err;
 
        *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
-       if (! (*prop))
+       if (!*prop)
                return oldlen;
 
-       if ((err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
+       if ((err = fdt_splice_struct_(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
                                      FDT_TAGALIGN(len))))
                return err;
 
@@ -219,27 +199,32 @@ static int _fdt_resize_property(void *fdt, int 
nodeoffset, const char *name,
        return 0;
 }
 
-static int _fdt_add_property(void *fdt, int nodeoffset, const char *name,
+static int fdt_add_property_(void *fdt, int nodeoffset, const char *name,
                             int len, struct fdt_property **prop)
 {
        int proplen;
        int nextoffset;
        int namestroff;
        int err;
+       int allocated;
 
-       if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
+       if ((nextoffset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
                return nextoffset;
 
-       namestroff = _fdt_find_add_string(fdt, name);
+       namestroff = fdt_find_add_string_(fdt, name, &allocated);
        if (namestroff < 0)
                return namestroff;
 
-       *prop = _fdt_offset_ptr_w(fdt, nextoffset);
+       *prop = fdt_offset_ptr_w_(fdt, nextoffset);
        proplen = sizeof(**prop) + FDT_TAGALIGN(len);
 
-       err = _fdt_splice_struct(fdt, *prop, 0, proplen);
-       if (err)
+       err = fdt_splice_struct_(fdt, *prop, 0, proplen);
+       if (err) {
+               /* Delete the string if we failed to add it */
+               if (!can_assume(NO_ROLLBACK) && allocated)
+                       fdt_del_last_string_(fdt, name);
                return err;
+       }
 
        (*prop)->tag = cpu_to_fdt32(FDT_PROP);
        (*prop)->nameoff = cpu_to_fdt32(namestroff);
@@ -253,7 +238,7 @@ int fdt_set_name(void *fdt, int nodeoffset, const char 
*name)
        int oldlen, newlen;
        int err;
 
-       FDT_RW_CHECK_HEADER(fdt);
+       FDT_RW_PROBE(fdt);
 
        namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
        if (!namep)
@@ -261,7 +246,7 @@ int fdt_set_name(void *fdt, int nodeoffset, const char 
*name)
 
        newlen = strlen(name);
 
-       err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1),
+       err = fdt_splice_struct_(fdt, namep, FDT_TAGALIGN(oldlen+1),
                                 FDT_TAGALIGN(newlen+1));
        if (err)
                return err;
@@ -270,21 +255,36 @@ int fdt_set_name(void *fdt, int nodeoffset, const char 
*name)
        return 0;
 }
 
-int fdt_setprop(void *fdt, int nodeoffset, const char *name,
-               const void *val, int len)
+int fdt_setprop_placeholder(void *fdt, int nodeoffset, const char *name,
+                           int len, void **prop_data)
 {
        struct fdt_property *prop;
        int err;
 
-       FDT_RW_CHECK_HEADER(fdt);
+       FDT_RW_PROBE(fdt);
 
-       err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop);
+       err = fdt_resize_property_(fdt, nodeoffset, name, len, &prop);
        if (err == -FDT_ERR_NOTFOUND)
-               err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
+               err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
+       if (err)
+               return err;
+
+       *prop_data = prop->data;
+       return 0;
+}
+
+int fdt_setprop(void *fdt, int nodeoffset, const char *name,
+               const void *val, int len)
+{
+       void *prop_data;
+       int err;
+
+       err = fdt_setprop_placeholder(fdt, nodeoffset, name, len, &prop_data);
        if (err)
                return err;
 
-       memcpy(prop->data, val, len);
+       if (len)
+               memcpy(prop_data, val, len);
        return 0;
 }
 
@@ -294,12 +294,12 @@ int fdt_appendprop(void *fdt, int nodeoffset, const char 
*name,
        struct fdt_property *prop;
        int err, oldlen, newlen;
 
-       FDT_RW_CHECK_HEADER(fdt);
+       FDT_RW_PROBE(fdt);
 
        prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
        if (prop) {
                newlen = len + oldlen;
-               err = _fdt_splice_struct(fdt, prop->data,
+               err = fdt_splice_struct_(fdt, prop->data,
                                         FDT_TAGALIGN(oldlen),
                                         FDT_TAGALIGN(newlen));
                if (err)
@@ -307,7 +307,7 @@ int fdt_appendprop(void *fdt, int nodeoffset, const char 
*name,
                prop->len = cpu_to_fdt32(newlen);
                memcpy(prop->data + oldlen, val, len);
        } else {
-               err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
+               err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
                if (err)
                        return err;
                memcpy(prop->data, val, len);
@@ -320,14 +320,14 @@ int fdt_delprop(void *fdt, int nodeoffset, const char 
*name)
        struct fdt_property *prop;
        int len, proplen;
 
-       FDT_RW_CHECK_HEADER(fdt);
+       FDT_RW_PROBE(fdt);
 
        prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
-       if (! prop)
+       if (!prop)
                return len;
 
        proplen = sizeof(*prop) + FDT_TAGALIGN(len);
-       return _fdt_splice_struct(fdt, prop, proplen, 0);
+       return fdt_splice_struct_(fdt, prop, proplen, 0);
 }
 
 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
@@ -340,7 +340,7 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,
        uint32_t tag;
        fdt32_t *endtag;
 
-       FDT_RW_CHECK_HEADER(fdt);
+       FDT_RW_PROBE(fdt);
 
        offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
        if (offset >= 0)
@@ -349,16 +349,19 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,
                return offset;
 
        /* Try to place the new node after the parent's properties */
-       fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
+       tag = fdt_next_tag(fdt, parentoffset, &nextoffset);
+       /* the fdt_subnode_offset_namelen() should ensure this never hits */
+       if (!can_assume(LIBFDT_FLAWLESS) && (tag != FDT_BEGIN_NODE))
+               return -FDT_ERR_INTERNAL;
        do {
                offset = nextoffset;
                tag = fdt_next_tag(fdt, offset, &nextoffset);
        } while ((tag == FDT_PROP) || (tag == FDT_NOP));
 
-       nh = _fdt_offset_ptr_w(fdt, offset);
+       nh = fdt_offset_ptr_w_(fdt, offset);
        nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
 
-       err = _fdt_splice_struct(fdt, nh, 0, nodelen);
+       err = fdt_splice_struct_(fdt, nh, 0, nodelen);
        if (err)
                return err;
 
@@ -380,18 +383,20 @@ int fdt_del_node(void *fdt, int nodeoffset)
 {
        int endoffset;
 
-       FDT_RW_CHECK_HEADER(fdt);
+       FDT_RW_PROBE(fdt);
 
-       endoffset = _fdt_node_end_offset(fdt, nodeoffset);
+       endoffset = fdt_node_end_offset_(fdt, nodeoffset);
        if (endoffset < 0)
                return endoffset;
 
-       return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
+       return fdt_splice_struct_(fdt, fdt_offset_ptr_w_(fdt, nodeoffset),
                                  endoffset - nodeoffset, 0);
 }
 
-static void _fdt_packblocks(const char *old, char *new,
-                           int mem_rsv_size, int struct_size)
+static void fdt_packblocks_(const char *old, char *new,
+                           int mem_rsv_size,
+                           int struct_size,
+                           int strings_size)
 {
        int mem_rsv_off, struct_off, strings_off;
 
@@ -406,8 +411,7 @@ static void _fdt_packblocks(const char *old, char *new,
        fdt_set_off_dt_struct(new, struct_off);
        fdt_set_size_dt_struct(new, struct_size);
 
-       memmove(new + strings_off, old + fdt_off_dt_strings(old),
-               fdt_size_dt_strings(old));
+       memmove(new + strings_off, old + fdt_off_dt_strings(old), strings_size);
        fdt_set_off_dt_strings(new, strings_off);
        fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
 }
@@ -421,22 +425,25 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
        const char *fdtend = fdtstart + fdt_totalsize(fdt);
        char *tmp;
 
-       FDT_CHECK_HEADER(fdt);
+       FDT_RO_PROBE(fdt);
 
        mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
                * sizeof(struct fdt_reserve_entry);
 
-       if (fdt_version(fdt) >= 17) {
+       if (can_assume(LATEST) || fdt_version(fdt) >= 17) {
                struct_size = fdt_size_dt_struct(fdt);
-       } else {
+       } else if (fdt_version(fdt) == 16) {
                struct_size = 0;
                while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
                        ;
                if (struct_size < 0)
                        return struct_size;
+       } else {
+               return -FDT_ERR_BADVERSION;
        }
 
-       if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) {
+       if (can_assume(LIBFDT_ORDER) ||
+           !fdt_blocks_misordered_(fdt, mem_rsv_size, struct_size)) {
                /* no further work necessary */
                err = fdt_move(fdt, buf, bufsize);
                if (err)
@@ -464,7 +471,8 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
                        return -FDT_ERR_NOSPACE;
        }
 
-       _fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size);
+       fdt_packblocks_(fdt, tmp, mem_rsv_size, struct_size,
+                       fdt_size_dt_strings(fdt));
        memmove(buf, tmp, newsize);
 
        fdt_set_magic(buf, FDT_MAGIC);
@@ -480,12 +488,13 @@ int fdt_pack(void *fdt)
 {
        int mem_rsv_size;
 
-       FDT_RW_CHECK_HEADER(fdt);
+       FDT_RW_PROBE(fdt);
 
        mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
                * sizeof(struct fdt_reserve_entry);
-       _fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
-       fdt_set_totalsize(fdt, _fdt_data_size(fdt));
+       fdt_packblocks_(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt),
+                       fdt_size_dt_strings(fdt));
+       fdt_set_totalsize(fdt, fdt_data_size_(fdt));
 
        return 0;
 }
diff --git a/xen/common/libfdt/fdt_strerror.c b/xen/common/libfdt/fdt_strerror.c
index 8d0289cb38..b4356931b0 100644
--- a/xen/common/libfdt/fdt_strerror.c
+++ b/xen/common/libfdt/fdt_strerror.c
@@ -1,49 +1,7 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
 /*
  * libfdt - Flat Device Tree manipulation
  * Copyright (C) 2006 David Gibson, IBM Corporation.
- *
- * libfdt is dual licensed: you can use it either under the terms of
- * the GPL, or the BSD license, at your option.
- *
- *  a) This library 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 library 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 library; If not, see 
<http://www.gnu.org/licenses/>.
- *
- * Alternatively,
- *
- *  b) Redistribution and use in source and binary forms, with or
- *     without modification, are permitted provided that the following
- *     conditions are met:
- *
- *     1. Redistributions of source code must retain the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer.
- *     2. Redistributions in binary form must reproduce the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer in the documentation and/or other materials
- *        provided with the distribution.
- *
- *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 #include "libfdt_env.h"
@@ -67,6 +25,7 @@ static struct fdt_errtabent fdt_errtable[] = {
 
        FDT_ERRTABENT(FDT_ERR_BADOFFSET),
        FDT_ERRTABENT(FDT_ERR_BADPATH),
+       FDT_ERRTABENT(FDT_ERR_BADPHANDLE),
        FDT_ERRTABENT(FDT_ERR_BADSTATE),
 
        FDT_ERRTABENT(FDT_ERR_TRUNCATED),
@@ -74,8 +33,14 @@ static struct fdt_errtabent fdt_errtable[] = {
        FDT_ERRTABENT(FDT_ERR_BADVERSION),
        FDT_ERRTABENT(FDT_ERR_BADSTRUCTURE),
        FDT_ERRTABENT(FDT_ERR_BADLAYOUT),
+       FDT_ERRTABENT(FDT_ERR_INTERNAL),
+       FDT_ERRTABENT(FDT_ERR_BADNCELLS),
+       FDT_ERRTABENT(FDT_ERR_BADVALUE),
+       FDT_ERRTABENT(FDT_ERR_BADOVERLAY),
+       FDT_ERRTABENT(FDT_ERR_NOPHANDLES),
+       FDT_ERRTABENT(FDT_ERR_BADFLAGS),
 };
-#define FDT_ERRTABSIZE (sizeof(fdt_errtable) / sizeof(fdt_errtable[0]))
+#define FDT_ERRTABSIZE ((int)(sizeof(fdt_errtable) / sizeof(fdt_errtable[0])))
 
 const char *fdt_strerror(int errval)
 {
@@ -83,7 +48,7 @@ const char *fdt_strerror(int errval)
                return "<valid offset/length>";
        else if (errval == 0)
                return "<no error>";
-       else if (errval > -FDT_ERRTABSIZE) {
+       else if (-errval < FDT_ERRTABSIZE) {
                const char *s = fdt_errtable[-errval].str;
 
                if (s)
diff --git a/xen/common/libfdt/fdt_sw.c b/xen/common/libfdt/fdt_sw.c
index c7d93d3bba..4c569ee7eb 100644
--- a/xen/common/libfdt/fdt_sw.c
+++ b/xen/common/libfdt/fdt_sw.c
@@ -1,50 +1,7 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
 /*
  * libfdt - Flat Device Tree manipulation
  * Copyright (C) 2006 David Gibson, IBM Corporation.
- *
- * libfdt is dual licensed: you can use it either under the terms of
- * the GPL, or the BSD license, at your option.
- *
- *  a) This library 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 library 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 library; If not, see 
<http://www.gnu.org/licenses/>.
- *
- * Alternatively,
- *
- *  b) Redistribution and use in source and binary forms, with or
- *     without modification, are permitted provided that the following
- *     conditions are met:
- *
- *     1. Redistributions of source code must retain the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer.
- *     2. Redistributions in binary form must reproduce the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer in the documentation and/or other materials
- *        provided with the distribution.
- *
- *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 #include "libfdt_env.h"
 
@@ -53,25 +10,91 @@
 
 #include "libfdt_internal.h"
 
-static int _fdt_sw_check_header(void *fdt)
+static int fdt_sw_probe_(void *fdt)



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.