Add fdt helper component

Add a new component that implements helper functions on top of libfdt.

Signed-off-by: Balint Dobszay <balint.dobszay@arm.com>
Change-Id: I43a4562433b6e81d5f5e4944c2161d5b542f537b
diff --git a/components/common/fdt/component.cmake b/components/common/fdt/component.cmake
new file mode 100644
index 0000000..77656e9
--- /dev/null
+++ b/components/common/fdt/component.cmake
@@ -0,0 +1,15 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+if (NOT DEFINED TGT)
+	message(FATAL_ERROR "mandatory parameter TGT is not defined")
+endif()
+
+target_sources(${TGT} PRIVATE
+	"${CMAKE_CURRENT_LIST_DIR}/fdt_helpers.c"
+)
+
+include(../../../external/libfdt/libfdt.cmake)
diff --git a/components/common/fdt/fdt_helpers.c b/components/common/fdt/fdt_helpers.c
new file mode 100644
index 0000000..eaf7922
--- /dev/null
+++ b/components/common/fdt/fdt_helpers.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ */
+
+#include "fdt_helpers.h"
+
+bool dt_get_u32(const void *fdt, int node, const char *prop_name, uint32_t *prop_val)
+{
+	const fdt32_t *u32_prop = NULL;
+	int len = 0;
+
+	if (!fdt || !prop_name || !prop_val)
+		return false;
+
+	u32_prop = fdt_getprop(fdt, node, prop_name, &len);
+	if (!u32_prop || len != sizeof(*u32_prop))
+		return false;
+
+	*prop_val = fdt32_to_cpu(*u32_prop);
+
+	return true;
+}
+
+bool dt_get_u64(const void *fdt, int node, const char *prop_name, uint64_t *prop_val)
+{
+	const fdt64_t *u64_prop = NULL;
+	int len = 0;
+
+	if (!fdt || !prop_name || !prop_val)
+		return false;
+
+	u64_prop = fdt_getprop(fdt, node, prop_name, &len);
+	if (!u64_prop || len != sizeof(*u64_prop))
+		return false;
+
+	*prop_val = fdt64_to_cpu(*u64_prop);
+
+	return true;
+}
diff --git a/components/common/fdt/fdt_helpers.h b/components/common/fdt/fdt_helpers.h
new file mode 100644
index 0000000..e3fc8fc
--- /dev/null
+++ b/components/common/fdt/fdt_helpers.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ */
+
+#include <libfdt.h>
+#include <stdbool.h>
+
+bool dt_get_u32(const void *fdt, int node, const char *prop_name, uint32_t *prop_val);
+bool dt_get_u64(const void *fdt, int node, const char *prop_name, uint64_t *prop_val);