[PATCH v2 10/18] optee: add helper functions to set/get the optee memory base

Marco Felsch m.felsch at pengutronix.de
Tue Jan 16 09:07:30 PST 2024


Provide helper functions to set/get the optee memory base address
gathered previously from the builtin optee binary header.

Signed-off-by: Marco Felsch <m.felsch at pengutronix.de>
---
 common/optee.c      | 27 +++++++++++++++++++++++++++
 include/tee/optee.h | 18 ++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/common/optee.c b/common/optee.c
index 6a8084839619..2ae28b172bbb 100644
--- a/common/optee.c
+++ b/common/optee.c
@@ -6,8 +6,13 @@
 #include <linux/printk.h>
 #include <linux/errno.h>
 
+static u64 optee_membase = U64_MAX;
+
 int optee_verify_header(struct optee_header *hdr)
 {
+	if (!hdr)
+		return -EINVAL;
+
 	if (hdr->magic != OPTEE_MAGIC) {
 		pr_err("Invalid header magic 0x%08x, expected 0x%08x\n",
 			   hdr->magic, OPTEE_MAGIC);
@@ -32,3 +37,25 @@ int optee_verify_header(struct optee_header *hdr)
 
 	return 0;
 }
+
+int optee_get_membase(u64 *membase)
+{
+	if (optee_membase == U64_MAX)
+		return -EINVAL;
+
+	*membase = optee_membase;
+
+	return 0;
+}
+
+void optee_set_membase(const struct optee_header *hdr)
+{
+	int ret;
+
+	ret = optee_verify_header(hdr);
+	if (ret)
+		return;
+
+	optee_membase = (u64)hdr->init_load_addr_hi << 32;
+	optee_membase |= hdr->init_load_addr_lo;
+}
diff --git a/include/tee/optee.h b/include/tee/optee.h
index b5ba0a71d7c9..0cdf828a6d3a 100644
--- a/include/tee/optee.h
+++ b/include/tee/optee.h
@@ -33,6 +33,24 @@ struct optee_header {
 
 int optee_verify_header (struct optee_header *hdr);
 
+#ifdef CONFIG_HAVE_OPTEE
+
+void optee_set_membase(const struct optee_header *hdr);
+int optee_get_membase(u64 *membase);
+
+#else
+
+static inline void optee_set_membase(const struct optee_header *hdr)
+{
+}
+
+static inline int optee_get_membase(u64 *membase)
+{
+	return -ENOSYS;
+}
+
+#endif /* CONFIG_HAVE_OPTEE */
+
 #ifdef __PBL__
 
 int start_optee_early(void* fdt, void* tee);
-- 
2.39.2




More information about the barebox mailing list