Snap for 9550355 from 007e485e205e2d14b2ef1657021a37e95ee3a3d6 to sdk-release

Change-Id: Ibba7ad87971b01760eba2810d463ac9969e93407
diff --git a/Android.bp b/Android.bp
index 97446ee..cb13748 100644
--- a/Android.bp
+++ b/Android.bp
@@ -91,3 +91,21 @@
         "util.c",
     ],
 }
+
+cc_binary_host {
+    name: "fdtoverlay",
+    defaults: ["dt_defaults"],
+    srcs: [
+        "fdtoverlay.c",
+        "util.c",
+    ],
+}
+
+cc_binary_host {
+    name: "fdtdump",
+    defaults: ["dt_defaults"],
+    srcs: [
+        "fdtdump.c",
+        "util.c",
+    ],
+}
diff --git a/METADATA b/METADATA
index 6d8601b..a716954 100644
--- a/METADATA
+++ b/METADATA
@@ -1,3 +1,19 @@
+# This project was upgraded with external_updater.
+# Usage: tools/external_updater/updater.sh update dtc
+# For more info, check https://cs.android.com/android/platform/superproject/+/master:tools/external_updater/README.md
+
+name: "dtc"
+description: "Device tree compiler tools (dtc) and libfdt."
 third_party {
+  url {
+    type: GIT
+    value: "https://git.kernel.org/pub/scm/utils/dtc/dtc.git"
+  }
+  version: "v1.6.1"
   license_type: RESTRICTED
+  last_upgrade_date {
+    year: 2023
+    month: 1
+    day: 18
+  }
 }
diff --git a/fuzzing/libfdt_fuzzer.c b/fuzzing/libfdt_fuzzer.c
index 98e03c8..89fe3c2 100644
--- a/fuzzing/libfdt_fuzzer.c
+++ b/fuzzing/libfdt_fuzzer.c
@@ -55,6 +55,9 @@
 #endif
 }
 
+static bool phandle_is_valid(uint32_t phandle) {
+  return phandle != 0 && phandle != UINT32_MAX;
+}
 
 static void walk_device_tree(const void *device_tree, int parent_node) {
   int len = 0;
@@ -64,8 +67,9 @@
   }
 
   uint32_t phandle = fdt_get_phandle(device_tree, parent_node);
-  if (phandle != 0) {
-    assert(parent_node == fdt_node_offset_by_phandle(device_tree, phandle));
+  if (phandle_is_valid(phandle)) {
+    int node = fdt_node_offset_by_phandle(device_tree, phandle);
+    assert(node >= 0); // it should at least find parent_node
   }
 
   // recursively walk the node's children
diff --git a/libfdt/fdt.c b/libfdt/fdt.c
index 9fe7cf4..c17cad5 100644
--- a/libfdt/fdt.c
+++ b/libfdt/fdt.c
@@ -188,12 +188,20 @@
 		break;
 
 	case FDT_PROP:
-		lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
+		lenp = fdt_offset_ptr(fdt, offset, sizeof(struct fdt_property) - FDT_TAGSIZE);
 		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);
+
+		/* skip name offset, length */
+		offset += sizeof(struct fdt_property) - FDT_TAGSIZE;
+
+		if (!can_assume(VALID_DTB)
+		    && !fdt_offset_ptr(fdt, offset, fdt32_to_cpu(*lenp)))
+			return FDT_END; /* premature end */
+
+		/* skip value */
+		offset += fdt32_to_cpu(*lenp);
+
 		if (!can_assume(LATEST) &&
 		    fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 &&
 		    ((offset - fdt32_to_cpu(*lenp)) % 8) != 0)
@@ -209,7 +217,8 @@
 		return FDT_END;
 	}
 
-	if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
+	if (!can_assume(VALID_DTB) && (offset <= startoffset
+	    || !fdt_offset_ptr(fdt, startoffset, offset - startoffset)))
 		return FDT_END; /* premature end */
 
 	*nextoffset = FDT_TAGALIGN(offset);
diff --git a/libfdt/fdt_ro.c b/libfdt/fdt_ro.c
index 9f6c551..87d736b 100644
--- a/libfdt/fdt_ro.c
+++ b/libfdt/fdt_ro.c
@@ -255,6 +255,12 @@
 
 	FDT_RO_PROBE(fdt);
 
+	if (namelen < 1)
+		return -FDT_ERR_BADPATH;
+
+	if (namelen < 1)
+		return -FDT_ERR_BADPATH;
+
 	/* see if we have an alias */
 	if (*path != '/') {
 		const char *q = memchr(path, '/', end - p);
diff --git a/libfdt/rules.mk b/libfdt/rules.mk
new file mode 100644
index 0000000..c785aa2
--- /dev/null
+++ b/libfdt/rules.mk
@@ -0,0 +1,53 @@
+# Copyright (c) 2022, Google, Inc. All rights reserved
+#
+# 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 HOLDER 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.
+
+# This file is not used in the Android build process! It's used only by Trusty.
+
+
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+MODULE_SRCS := \
+	$(LOCAL_DIR)/fdt.c \
+	$(LOCAL_DIR)/fdt_check.c \
+	$(LOCAL_DIR)/fdt_ro.c \
+	$(LOCAL_DIR)/fdt_wip.c \
+	$(LOCAL_DIR)/fdt_sw.c \
+	$(LOCAL_DIR)/fdt_rw.c \
+	$(LOCAL_DIR)/fdt_strerror.c \
+	$(LOCAL_DIR)/fdt_empty_tree.c \
+	$(LOCAL_DIR)/fdt_addresses.c \
+	$(LOCAL_DIR)/fdt_overlay.c \
+	$(LOCAL_DIR)/acpi.c \
+
+
+MODULE_COMPILEFLAGS += \
+	-Wno-sign-compare \
+	-Wno-macro-redefined \
+
+MODULE_EXPORT_INCLUDES += \
+	$(LOCAL_DIR) \
+
+include make/library.mk