Merge tag 'u-boot-atmel-fixes-2021.01-b' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / scripts / dtc / libfdt / fdt_overlay.c
index bf75388..7a65c35 100644 (file)
@@ -1,53 +1,8 @@
+// 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.
- *
- * 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, write to the Free
- *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
- *     MA 02110-1301 USA
- *
- * 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"
 
@@ -93,11 +48,11 @@ static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
  * @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 targetting is
+ * device tree of a fragment, no matter how the actual targeting is
  * done (through a phandle or a path)
  *
  * returns:
- *      the targetted node offset in the base device tree
+ *      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,
@@ -286,6 +241,7 @@ static int overlay_update_local_node_references(void *fdto,
 
                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) {
@@ -295,7 +251,7 @@ static int overlay_update_local_node_references(void *fdto,
                        return tree_len;
                }
 
-               for (i = 0; i < (fixup_len / sizeof(uint32_t)); i++) {
+               for (i = 0; i < fixup_len; i++) {
                        fdt32_t adj_val;
                        uint32_t poffset;
 
@@ -697,7 +653,7 @@ static int get_path_len(const void *fdt, int nodeoffset)
        int len = 0, namelen;
        const char *name;
 
-       FDT_CHECK_HEADER(fdt);
+       FDT_RO_PROBE(fdt);
 
        for (;;) {
                name = fdt_get_name(fdt, nodeoffset, &namelen);
@@ -778,26 +734,36 @@ static int overlay_symbol_update(void *fdt, void *fdto)
                /* keep end marker to avoid strlen() */
                e = path + path_len;
 
-               /* format: /<fragment-name>/__overlay__/<relative-subnode-path> */
-
                if (*path != '/')
                        return -FDT_ERR_BADVALUE;
 
                /* get fragment name first */
                s = strchr(path + 1, '/');
-               if (!s)
-                       return -FDT_ERR_BADOVERLAY;
+               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))
-                       return -FDT_ERR_BADOVERLAY;
-
-               rel_path = s + len;
-               rel_path_len = e - rel_path;
+               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;
+               } else if ((e - s) == len
+                          && (memcmp(s, "/__overlay__", len - 1) == 0)) {
+                       /* /<fragment-name>/__overlay__ */
+                       rel_path = "";
+                       rel_path_len = 1; /* Include NUL character */
+               } 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,
@@ -829,7 +795,7 @@ static int overlay_symbol_update(void *fdt, void *fdto)
                }
 
                ret = fdt_setprop_placeholder(fdt, root_sym, name,
-                               len + (len > 1) + rel_path_len + 1, &p);
+                               len + (len > 1) + rel_path_len, &p);
                if (ret < 0)
                        return ret;
 
@@ -855,7 +821,6 @@ static int overlay_symbol_update(void *fdt, void *fdto)
 
                buf[len] = '/';
                memcpy(buf + len + 1, rel_path, rel_path_len);
-               buf[len + 1 + rel_path_len] = '\0';
        }
 
        return 0;
@@ -863,11 +828,15 @@ static int overlay_symbol_update(void *fdt, void *fdto)
 
 int fdt_overlay_apply(void *fdt, void *fdto)
 {
-       uint32_t delta = fdt_get_max_phandle(fdt);
+       uint32_t delta;
        int ret;
 
-       FDT_CHECK_HEADER(fdt);
-       FDT_CHECK_HEADER(fdto);
+       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)
@@ -910,3 +879,8 @@ err:
 
        return ret;
 }
+
+int fdt_overlay_apply_node(void *fdt, int target, void *fdto, int node)
+{
+       return overlay_apply_node(fdt, target, fdto, node);
+}