diff mbox series

[1/3] optee: Add an helper to add the optee firmware node in the FDT

Message ID 20220516082139.26090-2-alban.bedel@aerq.com
State Superseded
Delegated to: Stefano Babic
Headers show
Series imx8m: Automatically add the optee firmware node to the FDT | expand

Commit Message

Bedel, Alban May 16, 2022, 8:21 a.m. UTC
Some platforms can detect if an optee firmware is running and then
manually add the firmware node to the FDT. Provide a common helper to
avoid code duplication in the board code.

Signed-off-by: Alban Bedel <alban.bedel@aerq.com>
---
 include/tee/optee.h | 11 +++++++++
 lib/optee/optee.c   | 60 +++++++++++++++++++++++++++++++++------------
 2 files changed, 55 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/include/tee/optee.h b/include/tee/optee.h
index 5412bc7386ec..d9a316150522 100644
--- a/include/tee/optee.h
+++ b/include/tee/optee.h
@@ -58,11 +58,22 @@  static inline int optee_verify_bootm_image(unsigned long image_addr,
 
 #if defined(CONFIG_OPTEE_LIB) && defined(CONFIG_OF_LIBFDT)
 int optee_copy_fdt_nodes(void *new_blob);
+
+int optee_add_firmware_node(void *fdt_blob,
+			    const char *compatible,
+			    const char *method);
 #else
 static inline int optee_copy_fdt_nodes(void *new_blob)
 {
 	return 0;
 }
+
+static inline int optee_add_firmware_node(void *fdt_blob,
+					  const char *compatible,
+					  const char *method)
+{
+	return 0;
+}
 #endif
 
 #endif /* _OPTEE_H */
diff --git a/lib/optee/optee.c b/lib/optee/optee.c
index b03622404469..8d4e110dfd04 100644
--- a/lib/optee/optee.c
+++ b/lib/optee/optee.c
@@ -65,10 +65,10 @@  error:
 #endif
 
 #if defined(CONFIG_OF_LIBFDT)
-static int optee_copy_firmware_node(ofnode node, void *fdt_blob)
+static int optee_set_firmware_node(void *fdt_blob, const char *compatible,
+				   const char *method)
 {
-	int offs, ret, len;
-	const void *prop;
+	int offs, ret;
 
 	offs = fdt_path_offset(fdt_blob, "/firmware");
 	if (offs < 0) {
@@ -85,29 +85,32 @@  static int optee_copy_firmware_node(ofnode node, void *fdt_blob)
 	if (offs < 0)
 		return offs;
 
+	ret = fdt_setprop_string(fdt_blob, offs, "compatible", compatible);
+	if (ret < 0)
+		return ret;
+
+	return fdt_setprop_string(fdt_blob, offs, "method", method);
+}
+
+static int optee_copy_firmware_node(ofnode node, void *fdt_blob)
+{
+	const char *compatible, *method;
+
 	/* copy the compatible property */
-	prop = ofnode_get_property(node, "compatible", &len);
-	if (!prop) {
+	compatible = ofnode_read_string(node, "compatible");
+	if (!compatible) {
 		debug("missing OP-TEE compatible property");
 		return -EINVAL;
 	}
 
-	ret = fdt_setprop(fdt_blob, offs, "compatible", prop, len);
-	if (ret < 0)
-		return ret;
-
 	/* copy the method property */
-	prop = ofnode_get_property(node, "method", &len);
-	if (!prop) {
+	method = ofnode_read_string(node, "method");
+	if (!method) {
 		debug("missing OP-TEE method property");
 		return -EINVAL;
 	}
 
-	ret = fdt_setprop(fdt_blob, offs, "method", prop, len);
-	if (ret < 0)
-		return ret;
-
-	return 0;
+	return optee_set_firmware_node(fdt_blob, compatible, method);
 }
 
 int optee_copy_fdt_nodes(void *new_blob)
@@ -190,4 +193,29 @@  int optee_copy_fdt_nodes(void *new_blob)
 
 	return 0;
 }
+
+int optee_add_firmware_node(void *fdt_blob, const char *compatible,
+			    const char *method)
+{
+	int ret;
+
+	/*
+	 * Do not proceed if the target dt already has an OP-TEE node.
+	 * In this case assume that the system knows better somehow,
+	 * so do not interfere.
+	 */
+	if (fdt_check_header(fdt_blob))
+		return -EINVAL;
+
+	if (fdt_path_offset(fdt_blob, "/firmware/optee") >= 0) {
+		debug("OP-TEE Device Tree node already exists in target");
+		return 0;
+	}
+
+	ret = fdt_increase_size(fdt_blob, 512);
+	if (ret)
+		return ret;
+
+	return optee_set_firmware_node(fdt_blob, compatible, method);
+}
 #endif