aboutsummaryrefslogtreecommitdiffstats
path: root/roms/u-boot/drivers/tee
diff options
context:
space:
mode:
Diffstat (limited to 'roms/u-boot/drivers/tee')
-rw-r--r--roms/u-boot/drivers/tee/Kconfig36
-rw-r--r--roms/u-boot/drivers/tee/Makefile8
-rw-r--r--roms/u-boot/drivers/tee/broadcom/Kconfig7
-rw-r--r--roms/u-boot/drivers/tee/broadcom/Makefile3
-rw-r--r--roms/u-boot/drivers/tee/broadcom/chimp_optee.c183
-rw-r--r--roms/u-boot/drivers/tee/optee/Kconfig42
-rw-r--r--roms/u-boot/drivers/tee/optee/Makefile6
-rw-r--r--roms/u-boot/drivers/tee/optee/core.c666
-rw-r--r--roms/u-boot/drivers/tee/optee/i2c.c90
-rw-r--r--roms/u-boot/drivers/tee/optee/optee_msg.h446
-rw-r--r--roms/u-boot/drivers/tee/optee/optee_msg_supplicant.h245
-rw-r--r--roms/u-boot/drivers/tee/optee/optee_private.h82
-rw-r--r--roms/u-boot/drivers/tee/optee/optee_smc.h450
-rw-r--r--roms/u-boot/drivers/tee/optee/rpmb.c185
-rw-r--r--roms/u-boot/drivers/tee/optee/supplicant.c100
-rw-r--r--roms/u-boot/drivers/tee/sandbox.c577
-rw-r--r--roms/u-boot/drivers/tee/tee-uclass.c235
17 files changed, 3361 insertions, 0 deletions
diff --git a/roms/u-boot/drivers/tee/Kconfig b/roms/u-boot/drivers/tee/Kconfig
new file mode 100644
index 000000000..5ca5a0836
--- /dev/null
+++ b/roms/u-boot/drivers/tee/Kconfig
@@ -0,0 +1,36 @@
+# Generic Trusted Execution Environment Configuration
+config TEE
+ bool "Trusted Execution Environment support"
+ depends on (ARM && (ARM64 || CPU_V7A)) || SANDBOX
+ select ARM_SMCCC if ARM
+ help
+ This implements a generic interface towards a Trusted Execution
+ Environment (TEE). A TEE is a trusted OS running in some secure
+ environment, for example, TrustZone on ARM cpus, or a separate
+ secure co-processor etc. See also:
+ https://en.wikipedia.org/wiki/Trusted_execution_environment
+
+if TEE
+
+menu "TEE drivers"
+
+config SANDBOX_TEE
+ bool "Sandbox TEE emulator"
+ depends on SANDBOX
+ default y
+ help
+ This emulates a generic TEE needed for testing including the AVB
+ TA. The emulation provides all callbacks of a regular TEE and
+ supports session and shared memory management. The AVB TA is
+ emulated with rollback indexes and device lock-state, the state
+ of the TA is only kept in RAM and will be reset on each boot.
+ The emulation only supports one open session at a time.
+ Interaction from the U-Boot command line in possible via the
+ "avb" commands.
+
+source "drivers/tee/optee/Kconfig"
+source "drivers/tee/broadcom/Kconfig"
+
+endmenu
+
+endif
diff --git a/roms/u-boot/drivers/tee/Makefile b/roms/u-boot/drivers/tee/Makefile
new file mode 100644
index 000000000..ff844195a
--- /dev/null
+++ b/roms/u-boot/drivers/tee/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-y += tee-uclass.o
+obj-$(CONFIG_SANDBOX) += sandbox.o
+obj-$(CONFIG_OPTEE_TA_RPC_TEST) += optee/supplicant.o
+obj-$(CONFIG_OPTEE_TA_RPC_TEST) += optee/i2c.o
+obj-$(CONFIG_OPTEE) += optee/
+obj-y += broadcom/
diff --git a/roms/u-boot/drivers/tee/broadcom/Kconfig b/roms/u-boot/drivers/tee/broadcom/Kconfig
new file mode 100644
index 000000000..ce95072d4
--- /dev/null
+++ b/roms/u-boot/drivers/tee/broadcom/Kconfig
@@ -0,0 +1,7 @@
+config CHIMP_OPTEE
+ bool "Enable secure ChiMP firmware loading"
+ depends on OPTEE
+ default y
+ help
+ This driver is used to load bnxt firmware binary using OpTEE.
+ bnxt is Broadcom NetXtreme controller Ethernet card.
diff --git a/roms/u-boot/drivers/tee/broadcom/Makefile b/roms/u-boot/drivers/tee/broadcom/Makefile
new file mode 100644
index 000000000..cb3cef16d
--- /dev/null
+++ b/roms/u-boot/drivers/tee/broadcom/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-y += chimp_optee.o
diff --git a/roms/u-boot/drivers/tee/broadcom/chimp_optee.c b/roms/u-boot/drivers/tee/broadcom/chimp_optee.c
new file mode 100644
index 000000000..37f9b094f
--- /dev/null
+++ b/roms/u-boot/drivers/tee/broadcom/chimp_optee.c
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright 2020 Broadcom.
+ */
+
+#include <common.h>
+#include <tee.h>
+#include <broadcom/chimp.h>
+
+#ifdef CONFIG_CHIMP_OPTEE
+
+#define CHMIP_BOOT_UUID { 0x6272636D, 0x2019, 0x0716, \
+ { 0x42, 0x43, 0x4D, 0x5F, 0x53, 0x43, 0x48, 0x49 } }
+
+enum {
+ TEE_CHIMP_FASTBOOT = 0,
+ TEE_CHIMP_HEALTH_STATUS,
+ TEE_CHIMP_HANDSHAKE_STATUS,
+} tee_chmip_cmd;
+
+struct bcm_chimp_data {
+ struct udevice *tee;
+ u32 session;
+} chimp_data;
+
+static int get_open_session(struct bcm_chimp_data *b_data)
+{
+ const struct tee_optee_ta_uuid uuid = CHMIP_BOOT_UUID;
+ struct tee_open_session_arg arg;
+ struct udevice *tee = NULL;
+ int rc;
+
+ tee = tee_find_device(NULL, NULL, NULL, NULL);
+ if (!tee)
+ return -ENODEV;
+
+ memset(&arg, 0, sizeof(arg));
+ tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
+ rc = tee_open_session(tee, &arg, 0, NULL);
+ if (rc < 0)
+ return -ENODEV;
+
+ b_data->tee = tee;
+ b_data->session = arg.session;
+
+ return 0;
+}
+
+static int init_arg(struct tee_invoke_arg *arg, u32 func)
+{
+ if (get_open_session(&chimp_data))
+ return -EINVAL;
+
+ memset(arg, 0, sizeof(struct tee_invoke_arg));
+ arg->func = func;
+ arg->session = chimp_data.session;
+
+ return 0;
+}
+
+int chimp_handshake_status_optee(u32 timeout, u32 *hs)
+{
+ struct tee_invoke_arg arg;
+ struct tee_param param[1];
+ int ret;
+
+ ret = init_arg(&arg, TEE_CHIMP_HANDSHAKE_STATUS);
+ if (ret < 0)
+ return ret;
+
+ param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INOUT;
+ param[0].u.value.a = timeout;
+
+ ret = tee_invoke_func(chimp_data.tee, &arg, ARRAY_SIZE(param), param);
+ if (ret < 0) {
+ printf("Handshake status command failed\n");
+ goto out;
+ }
+
+ switch (arg.ret) {
+ case TEE_SUCCESS:
+ *hs = param[0].u.value.a;
+ ret = 0;
+ break;
+ default:
+ ret = -EINVAL;
+ break;
+ }
+
+out:
+ tee_close_session(chimp_data.tee, chimp_data.session);
+ chimp_data.tee = NULL;
+
+ return ret;
+}
+
+int chimp_health_status_optee(u32 *health)
+{
+ struct tee_invoke_arg arg;
+ struct tee_param param[1];
+ int ret;
+
+ ret = init_arg(&arg, TEE_CHIMP_HEALTH_STATUS);
+ if (ret < 0)
+ return ret;
+
+ param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT;
+
+ ret = tee_invoke_func(chimp_data.tee, &arg, ARRAY_SIZE(param), param);
+ if (ret < 0) {
+ printf("Helath status command failed\n");
+ goto out;
+ }
+
+ switch (arg.ret) {
+ case TEE_SUCCESS:
+ *health = param[0].u.value.a;
+ ret = 0;
+ break;
+ default:
+ ret = -EINVAL;
+ break;
+ }
+
+out:
+ tee_close_session(chimp_data.tee, chimp_data.session);
+ chimp_data.tee = NULL;
+
+ return ret;
+}
+
+int chimp_fastboot_optee(void)
+{
+ struct tee_invoke_arg arg;
+ int ret;
+
+ ret = init_arg(&arg, TEE_CHIMP_FASTBOOT);
+ if (ret < 0)
+ return ret;
+
+ ret = tee_invoke_func(chimp_data.tee, &arg, 0, NULL);
+ if (ret < 0) {
+ printf("Chimp boot_fail\n");
+ goto out;
+ }
+
+ switch (arg.ret) {
+ case TEE_SUCCESS:
+ ret = 0;
+ break;
+ default:
+ ret = -EINVAL;
+ break;
+ }
+
+out:
+ tee_close_session(chimp_data.tee, chimp_data.session);
+ chimp_data.tee = NULL;
+
+ return ret;
+}
+#else
+int chimp_handshake_status_optee(u32 timeout, u32 *status)
+{
+ printf("ChiMP handshake status fail (OPTEE not enabled)\n");
+
+ return -EINVAL;
+}
+
+int chimp_health_status_optee(u32 *status)
+{
+ printf("ChiMP health status fail (OPTEE not enabled)\n");
+
+ return -EINVAL;
+}
+
+int chimp_fastboot_optee(void)
+{
+ printf("ChiMP secure boot fail (OPTEE not enabled)\n");
+
+ return -EINVAL;
+}
+#endif /* CONFIG_CHIMP_OPTEE */
diff --git a/roms/u-boot/drivers/tee/optee/Kconfig b/roms/u-boot/drivers/tee/optee/Kconfig
new file mode 100644
index 000000000..d03028070
--- /dev/null
+++ b/roms/u-boot/drivers/tee/optee/Kconfig
@@ -0,0 +1,42 @@
+# OP-TEE Trusted Execution Environment Configuration
+config OPTEE
+ bool "OP-TEE"
+ depends on ARM_SMCCC
+ help
+ This implements the OP-TEE Trusted Execution Environment (TEE)
+ driver. OP-TEE is a Trusted OS designed primarily to rely on the
+ ARM TrustZone(R) technology as the underlying hardware isolation
+ mechanism. This driver can request services from OP-TEE, but also
+ handle Remote Procedure Calls (RPC) from OP-TEE needed to
+ execute a service. For more information see: https://www.op-tee.org
+
+if OPTEE || SANDBOX
+
+menu "OP-TEE options"
+
+config OPTEE_TA_AVB
+ bool "Support AVB TA"
+ default y
+ help
+ Enables support for the AVB Trusted Application (TA) in OP-TEE.
+ The TA can support the "avb" subcommands "read_rb", "write"rb"
+ and "is_unlocked".
+
+config OPTEE_TA_RPC_TEST
+ bool "Support RPC TEST TA"
+ depends on SANDBOX_TEE
+ default y
+ help
+ Enables support for RPC test trusted application emulation, which
+ permits to test reverse RPC calls to TEE supplicant. Should
+ be used only in sandbox env.
+
+config OPTEE_TA_SCP03
+ bool "Support SCP03 TA"
+ default y
+ help
+ Enables support for controlling (enabling, provisioning) the
+ Secure Channel Protocol 03 operation in the OP-TEE SCP03 TA.
+endmenu
+
+endif
diff --git a/roms/u-boot/drivers/tee/optee/Makefile b/roms/u-boot/drivers/tee/optee/Makefile
new file mode 100644
index 000000000..068c6e7aa
--- /dev/null
+++ b/roms/u-boot/drivers/tee/optee/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-y += core.o
+obj-y += supplicant.o
+obj-$(CONFIG_DM_I2C) += i2c.o
+obj-$(CONFIG_SUPPORT_EMMC_RPMB) += rpmb.o
diff --git a/roms/u-boot/drivers/tee/optee/core.c b/roms/u-boot/drivers/tee/optee/core.c
new file mode 100644
index 000000000..73dbb22ba
--- /dev/null
+++ b/roms/u-boot/drivers/tee/optee/core.c
@@ -0,0 +1,666 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <dm/device_compat.h>
+#include <log.h>
+#include <malloc.h>
+#include <tee.h>
+#include <linux/arm-smccc.h>
+#include <linux/err.h>
+#include <linux/io.h>
+
+#include "optee_smc.h"
+#include "optee_msg.h"
+#include "optee_private.h"
+
+#define PAGELIST_ENTRIES_PER_PAGE \
+ ((OPTEE_MSG_NONCONTIG_PAGE_SIZE / sizeof(u64)) - 1)
+
+typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
+ unsigned long, unsigned long, unsigned long,
+ unsigned long, unsigned long,
+ struct arm_smccc_res *);
+
+struct optee_pdata {
+ optee_invoke_fn *invoke_fn;
+};
+
+struct rpc_param {
+ u32 a0;
+ u32 a1;
+ u32 a2;
+ u32 a3;
+ u32 a4;
+ u32 a5;
+ u32 a6;
+ u32 a7;
+};
+
+/**
+ * reg_pair_to_ptr() - Make a pointer of 2 32-bit values
+ * @reg0: High bits of the pointer
+ * @reg1: Low bits of the pointer
+ *
+ * Returns the combined result, note that if a pointer is 32-bit wide @reg0
+ * will be discarded.
+ */
+static void *reg_pair_to_ptr(u32 reg0, u32 reg1)
+{
+ return (void *)(ulong)(((u64)reg0 << 32) | reg1);
+}
+
+/**
+ * reg_pair_from_64() - Split a 64-bit value into two 32-bit values
+ * @reg0: High bits of @val
+ * @reg1: Low bits of @val
+ * @val: The value to split
+ */
+static void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
+{
+ *reg0 = val >> 32;
+ *reg1 = val;
+}
+
+/**
+ * optee_alloc_and_init_page_list() - Provide page list of memory buffer
+ * @buf: Start of buffer
+ * @len: Length of buffer
+ * @phys_buf_ptr Physical pointer with coded offset to page list
+ *
+ * Secure world doesn't share mapping with Normal world (U-Boot in this case)
+ * so physical pointers are needed when sharing pointers.
+ *
+ * Returns a pointer page list on success or NULL on failure
+ */
+void *optee_alloc_and_init_page_list(void *buf, ulong len, u64 *phys_buf_ptr)
+{
+ const unsigned int page_size = OPTEE_MSG_NONCONTIG_PAGE_SIZE;
+ const phys_addr_t page_mask = page_size - 1;
+ u8 *buf_base;
+ unsigned int page_offset;
+ unsigned int num_pages;
+ unsigned int list_size;
+ unsigned int n;
+ void *page_list;
+ struct {
+ u64 pages_list[PAGELIST_ENTRIES_PER_PAGE];
+ u64 next_page_data;
+ } *pages_data;
+
+ /*
+ * A Memory buffer is described in chunks of 4k. The list of
+ * physical addresses has to be represented by a physical pointer
+ * too and a single list has to start at a 4k page and fit into
+ * that page. In order to be able to describe large memory buffers
+ * these 4k pages carrying physical addresses are linked together
+ * in a list. See OPTEE_MSG_ATTR_NONCONTIG in
+ * drivers/tee/optee/optee_msg.h for more information.
+ */
+
+ page_offset = (ulong)buf & page_mask;
+ num_pages = roundup(page_offset + len, page_size) / page_size;
+ list_size = DIV_ROUND_UP(num_pages, PAGELIST_ENTRIES_PER_PAGE) *
+ page_size;
+ page_list = memalign(page_size, list_size);
+ if (!page_list)
+ return NULL;
+
+ pages_data = page_list;
+ buf_base = (u8 *)rounddown((ulong)buf, page_size);
+ n = 0;
+ while (num_pages) {
+ pages_data->pages_list[n] = virt_to_phys(buf_base);
+ n++;
+ buf_base += page_size;
+ num_pages--;
+
+ if (n == PAGELIST_ENTRIES_PER_PAGE) {
+ pages_data->next_page_data =
+ virt_to_phys(pages_data + 1);
+ pages_data++;
+ n = 0;
+ }
+ }
+
+ *phys_buf_ptr = virt_to_phys(page_list) | page_offset;
+ return page_list;
+}
+
+static void optee_get_version(struct udevice *dev,
+ struct tee_version_data *vers)
+{
+ struct tee_version_data v = {
+ .gen_caps = TEE_GEN_CAP_GP | TEE_GEN_CAP_REG_MEM,
+ };
+
+ *vers = v;
+}
+
+static int get_msg_arg(struct udevice *dev, uint num_params,
+ struct tee_shm **shmp, struct optee_msg_arg **msg_arg)
+{
+ int rc;
+ struct optee_msg_arg *ma;
+
+ rc = __tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
+ OPTEE_MSG_GET_ARG_SIZE(num_params), TEE_SHM_ALLOC,
+ shmp);
+ if (rc)
+ return rc;
+
+ ma = (*shmp)->addr;
+ memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
+ ma->num_params = num_params;
+ *msg_arg = ma;
+
+ return 0;
+}
+
+static int to_msg_param(struct optee_msg_param *msg_params, uint num_params,
+ const struct tee_param *params)
+{
+ uint n;
+
+ for (n = 0; n < num_params; n++) {
+ const struct tee_param *p = params + n;
+ struct optee_msg_param *mp = msg_params + n;
+
+ switch (p->attr) {
+ case TEE_PARAM_ATTR_TYPE_NONE:
+ mp->attr = OPTEE_MSG_ATTR_TYPE_NONE;
+ memset(&mp->u, 0, sizeof(mp->u));
+ break;
+ case TEE_PARAM_ATTR_TYPE_VALUE_INPUT:
+ case TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT:
+ case TEE_PARAM_ATTR_TYPE_VALUE_INOUT:
+ mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
+ TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
+ mp->u.value.a = p->u.value.a;
+ mp->u.value.b = p->u.value.b;
+ mp->u.value.c = p->u.value.c;
+ break;
+ case TEE_PARAM_ATTR_TYPE_MEMREF_INPUT:
+ case TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
+ case TEE_PARAM_ATTR_TYPE_MEMREF_INOUT:
+ mp->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr -
+ TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
+ mp->u.rmem.shm_ref = (ulong)p->u.memref.shm;
+ mp->u.rmem.size = p->u.memref.size;
+ mp->u.rmem.offs = p->u.memref.shm_offs;
+ break;
+ default:
+ return -EINVAL;
+ }
+ }
+ return 0;
+}
+
+static int from_msg_param(struct tee_param *params, uint num_params,
+ const struct optee_msg_param *msg_params)
+{
+ uint n;
+ struct tee_shm *shm;
+
+ for (n = 0; n < num_params; n++) {
+ struct tee_param *p = params + n;
+ const struct optee_msg_param *mp = msg_params + n;
+ u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK;
+
+ switch (attr) {
+ case OPTEE_MSG_ATTR_TYPE_NONE:
+ p->attr = TEE_PARAM_ATTR_TYPE_NONE;
+ memset(&p->u, 0, sizeof(p->u));
+ break;
+ case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT:
+ case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT:
+ case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT:
+ p->attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT + attr -
+ OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
+ p->u.value.a = mp->u.value.a;
+ p->u.value.b = mp->u.value.b;
+ p->u.value.c = mp->u.value.c;
+ break;
+ case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT:
+ case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT:
+ case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT:
+ p->attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT + attr -
+ OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
+ p->u.memref.size = mp->u.rmem.size;
+ shm = (struct tee_shm *)(ulong)mp->u.rmem.shm_ref;
+
+ if (!shm) {
+ p->u.memref.shm_offs = 0;
+ p->u.memref.shm = NULL;
+ break;
+ }
+ p->u.memref.shm_offs = mp->u.rmem.offs;
+ p->u.memref.shm = shm;
+ break;
+ default:
+ return -EINVAL;
+ }
+ }
+ return 0;
+}
+
+static void handle_rpc(struct udevice *dev, struct rpc_param *param,
+ void *page_list)
+{
+ struct tee_shm *shm;
+
+ switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) {
+ case OPTEE_SMC_RPC_FUNC_ALLOC:
+ if (!__tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
+ param->a1, TEE_SHM_ALLOC | TEE_SHM_REGISTER,
+ &shm)) {
+ reg_pair_from_64(&param->a1, &param->a2,
+ virt_to_phys(shm->addr));
+ /* "cookie" */
+ reg_pair_from_64(&param->a4, &param->a5, (ulong)shm);
+ } else {
+ param->a1 = 0;
+ param->a2 = 0;
+ param->a4 = 0;
+ param->a5 = 0;
+ }
+ break;
+ case OPTEE_SMC_RPC_FUNC_FREE:
+ shm = reg_pair_to_ptr(param->a1, param->a2);
+ tee_shm_free(shm);
+ break;
+ case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR:
+ break;
+ case OPTEE_SMC_RPC_FUNC_CMD:
+ shm = reg_pair_to_ptr(param->a1, param->a2);
+ optee_suppl_cmd(dev, shm, page_list);
+ break;
+ default:
+ break;
+ }
+
+ param->a0 = OPTEE_SMC_CALL_RETURN_FROM_RPC;
+}
+
+static u32 call_err_to_res(u32 call_err)
+{
+ switch (call_err) {
+ case OPTEE_SMC_RETURN_OK:
+ return TEE_SUCCESS;
+ default:
+ return TEE_ERROR_BAD_PARAMETERS;
+ }
+}
+
+static u32 do_call_with_arg(struct udevice *dev, struct optee_msg_arg *arg)
+{
+ struct optee_pdata *pdata = dev_get_plat(dev);
+ struct rpc_param param = { .a0 = OPTEE_SMC_CALL_WITH_ARG };
+ void *page_list = NULL;
+
+ reg_pair_from_64(&param.a1, &param.a2, virt_to_phys(arg));
+ while (true) {
+ struct arm_smccc_res res;
+
+ pdata->invoke_fn(param.a0, param.a1, param.a2, param.a3,
+ param.a4, param.a5, param.a6, param.a7, &res);
+
+ free(page_list);
+ page_list = NULL;
+
+ if (OPTEE_SMC_RETURN_IS_RPC(res.a0)) {
+ param.a0 = res.a0;
+ param.a1 = res.a1;
+ param.a2 = res.a2;
+ param.a3 = res.a3;
+ handle_rpc(dev, &param, &page_list);
+ } else {
+ /*
+ * In case we've accessed RPMB to serve an RPC
+ * request we need to restore the previously
+ * selected partition as the caller may expect it
+ * to remain unchanged.
+ */
+ optee_suppl_rpmb_release(dev);
+ return call_err_to_res(res.a0);
+ }
+ }
+}
+
+static int optee_close_session(struct udevice *dev, u32 session)
+{
+ int rc;
+ struct tee_shm *shm;
+ struct optee_msg_arg *msg_arg;
+
+ rc = get_msg_arg(dev, 0, &shm, &msg_arg);
+ if (rc)
+ return rc;
+
+ msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
+ msg_arg->session = session;
+ do_call_with_arg(dev, msg_arg);
+
+ tee_shm_free(shm);
+
+ return 0;
+}
+
+static int optee_open_session(struct udevice *dev,
+ struct tee_open_session_arg *arg,
+ uint num_params, struct tee_param *params)
+{
+ int rc;
+ struct tee_shm *shm;
+ struct optee_msg_arg *msg_arg;
+
+ rc = get_msg_arg(dev, num_params + 2, &shm, &msg_arg);
+ if (rc)
+ return rc;
+
+ msg_arg->cmd = OPTEE_MSG_CMD_OPEN_SESSION;
+ /*
+ * Initialize and add the meta parameters needed when opening a
+ * session.
+ */
+ msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
+ OPTEE_MSG_ATTR_META;
+ msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
+ OPTEE_MSG_ATTR_META;
+ memcpy(&msg_arg->params[0].u.value, arg->uuid, sizeof(arg->uuid));
+ memcpy(&msg_arg->params[1].u.value, arg->uuid, sizeof(arg->clnt_uuid));
+ msg_arg->params[1].u.value.c = arg->clnt_login;
+
+ rc = to_msg_param(msg_arg->params + 2, num_params, params);
+ if (rc)
+ goto out;
+
+ arg->ret = do_call_with_arg(dev, msg_arg);
+ if (arg->ret) {
+ arg->ret_origin = TEE_ORIGIN_COMMS;
+ goto out;
+ }
+
+ if (from_msg_param(params, num_params, msg_arg->params + 2)) {
+ arg->ret = TEE_ERROR_COMMUNICATION;
+ arg->ret_origin = TEE_ORIGIN_COMMS;
+ /* Close session again to avoid leakage */
+ optee_close_session(dev, msg_arg->session);
+ goto out;
+ }
+
+ arg->session = msg_arg->session;
+ arg->ret = msg_arg->ret;
+ arg->ret_origin = msg_arg->ret_origin;
+out:
+ tee_shm_free(shm);
+
+ return rc;
+}
+
+static int optee_invoke_func(struct udevice *dev, struct tee_invoke_arg *arg,
+ uint num_params, struct tee_param *params)
+{
+ struct tee_shm *shm;
+ struct optee_msg_arg *msg_arg;
+ int rc;
+
+ rc = get_msg_arg(dev, num_params, &shm, &msg_arg);
+ if (rc)
+ return rc;
+ msg_arg->cmd = OPTEE_MSG_CMD_INVOKE_COMMAND;
+ msg_arg->func = arg->func;
+ msg_arg->session = arg->session;
+
+ rc = to_msg_param(msg_arg->params, num_params, params);
+ if (rc)
+ goto out;
+
+ arg->ret = do_call_with_arg(dev, msg_arg);
+ if (arg->ret) {
+ arg->ret_origin = TEE_ORIGIN_COMMS;
+ goto out;
+ }
+
+ if (from_msg_param(params, num_params, msg_arg->params)) {
+ arg->ret = TEE_ERROR_COMMUNICATION;
+ arg->ret_origin = TEE_ORIGIN_COMMS;
+ goto out;
+ }
+
+ arg->ret = msg_arg->ret;
+ arg->ret_origin = msg_arg->ret_origin;
+out:
+ tee_shm_free(shm);
+ return rc;
+}
+
+static int optee_shm_register(struct udevice *dev, struct tee_shm *shm)
+{
+ struct tee_shm *shm_arg;
+ struct optee_msg_arg *msg_arg;
+ void *pl;
+ u64 ph_ptr;
+ int rc;
+
+ rc = get_msg_arg(dev, 1, &shm_arg, &msg_arg);
+ if (rc)
+ return rc;
+
+ pl = optee_alloc_and_init_page_list(shm->addr, shm->size, &ph_ptr);
+ if (!pl) {
+ rc = -ENOMEM;
+ goto out;
+ }
+
+ msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM;
+ msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
+ OPTEE_MSG_ATTR_NONCONTIG;
+ msg_arg->params->u.tmem.buf_ptr = ph_ptr;
+ msg_arg->params->u.tmem.shm_ref = (ulong)shm;
+ msg_arg->params->u.tmem.size = shm->size;
+
+ if (do_call_with_arg(dev, msg_arg) || msg_arg->ret)
+ rc = -EINVAL;
+
+ free(pl);
+out:
+ tee_shm_free(shm_arg);
+
+ return rc;
+}
+
+static int optee_shm_unregister(struct udevice *dev, struct tee_shm *shm)
+{
+ struct tee_shm *shm_arg;
+ struct optee_msg_arg *msg_arg;
+ int rc;
+
+ rc = get_msg_arg(dev, 1, &shm_arg, &msg_arg);
+ if (rc)
+ return rc;
+
+ msg_arg->cmd = OPTEE_MSG_CMD_UNREGISTER_SHM;
+ msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
+ msg_arg->params[0].u.rmem.shm_ref = (ulong)shm;
+
+ if (do_call_with_arg(dev, msg_arg) || msg_arg->ret)
+ rc = -EINVAL;
+ tee_shm_free(shm_arg);
+
+ return rc;
+}
+
+static const struct tee_driver_ops optee_ops = {
+ .get_version = optee_get_version,
+ .open_session = optee_open_session,
+ .close_session = optee_close_session,
+ .invoke_func = optee_invoke_func,
+ .shm_register = optee_shm_register,
+ .shm_unregister = optee_shm_unregister,
+};
+
+static bool is_optee_api(optee_invoke_fn *invoke_fn)
+{
+ struct arm_smccc_res res;
+
+ invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res);
+
+ return res.a0 == OPTEE_MSG_UID_0 && res.a1 == OPTEE_MSG_UID_1 &&
+ res.a2 == OPTEE_MSG_UID_2 && res.a3 == OPTEE_MSG_UID_3;
+}
+
+static void print_os_revision(struct udevice *dev, optee_invoke_fn *invoke_fn)
+{
+ union {
+ struct arm_smccc_res smccc;
+ struct optee_smc_call_get_os_revision_result result;
+ } res = {
+ .result = {
+ .build_id = 0
+ }
+ };
+
+ invoke_fn(OPTEE_SMC_CALL_GET_OS_REVISION, 0, 0, 0, 0, 0, 0, 0,
+ &res.smccc);
+
+ if (res.result.build_id)
+ dev_info(dev, "OP-TEE: revision %lu.%lu (%08lx)\n",
+ res.result.major, res.result.minor,
+ res.result.build_id);
+ else
+ dev_info(dev, "OP-TEE: revision %lu.%lu\n",
+ res.result.major, res.result.minor);
+}
+
+static bool api_revision_is_compatible(optee_invoke_fn *invoke_fn)
+{
+ union {
+ struct arm_smccc_res smccc;
+ struct optee_smc_calls_revision_result result;
+ } res;
+
+ invoke_fn(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
+
+ return res.result.major == OPTEE_MSG_REVISION_MAJOR &&
+ (int)res.result.minor >= OPTEE_MSG_REVISION_MINOR;
+}
+
+static bool exchange_capabilities(optee_invoke_fn *invoke_fn, u32 *sec_caps)
+{
+ union {
+ struct arm_smccc_res smccc;
+ struct optee_smc_exchange_capabilities_result result;
+ } res;
+
+ invoke_fn(OPTEE_SMC_EXCHANGE_CAPABILITIES,
+ OPTEE_SMC_NSEC_CAP_UNIPROCESSOR, 0, 0, 0, 0, 0, 0,
+ &res.smccc);
+
+ if (res.result.status != OPTEE_SMC_RETURN_OK)
+ return false;
+
+ *sec_caps = res.result.capabilities;
+
+ return true;
+}
+
+/* Simple wrapper functions to be able to use a function pointer */
+static void optee_smccc_smc(unsigned long a0, unsigned long a1,
+ unsigned long a2, unsigned long a3,
+ unsigned long a4, unsigned long a5,
+ unsigned long a6, unsigned long a7,
+ struct arm_smccc_res *res)
+{
+ arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
+}
+
+static void optee_smccc_hvc(unsigned long a0, unsigned long a1,
+ unsigned long a2, unsigned long a3,
+ unsigned long a4, unsigned long a5,
+ unsigned long a6, unsigned long a7,
+ struct arm_smccc_res *res)
+{
+ arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
+}
+
+static optee_invoke_fn *get_invoke_func(struct udevice *dev)
+{
+ const char *method;
+
+ debug("optee: looking for conduit method in DT.\n");
+ method = ofnode_get_property(dev_ofnode(dev), "method", NULL);
+ if (!method) {
+ debug("optee: missing \"method\" property\n");
+ return ERR_PTR(-ENXIO);
+ }
+
+ if (!strcmp("hvc", method))
+ return optee_smccc_hvc;
+ else if (!strcmp("smc", method))
+ return optee_smccc_smc;
+
+ debug("optee: invalid \"method\" property: %s\n", method);
+ return ERR_PTR(-EINVAL);
+}
+
+static int optee_of_to_plat(struct udevice *dev)
+{
+ struct optee_pdata *pdata = dev_get_plat(dev);
+
+ pdata->invoke_fn = get_invoke_func(dev);
+ if (IS_ERR(pdata->invoke_fn))
+ return PTR_ERR(pdata->invoke_fn);
+
+ return 0;
+}
+
+static int optee_probe(struct udevice *dev)
+{
+ struct optee_pdata *pdata = dev_get_plat(dev);
+ u32 sec_caps;
+
+ if (!is_optee_api(pdata->invoke_fn)) {
+ dev_err(dev, "OP-TEE api uid mismatch\n");
+ return -ENOENT;
+ }
+
+ print_os_revision(dev, pdata->invoke_fn);
+
+ if (!api_revision_is_compatible(pdata->invoke_fn)) {
+ dev_err(dev, "OP-TEE api revision mismatch\n");
+ return -ENOENT;
+ }
+
+ /*
+ * OP-TEE can use both shared memory via predefined pool or as
+ * dynamic shared memory provided by normal world. To keep things
+ * simple we're only using dynamic shared memory in this driver.
+ */
+ if (!exchange_capabilities(pdata->invoke_fn, &sec_caps) ||
+ !(sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)) {
+ dev_err(dev, "OP-TEE capabilities mismatch\n");
+ return -ENOENT;
+ }
+
+ return 0;
+}
+
+static const struct udevice_id optee_match[] = {
+ { .compatible = "linaro,optee-tz" },
+ {},
+};
+
+U_BOOT_DRIVER(optee) = {
+ .name = "optee",
+ .id = UCLASS_TEE,
+ .of_match = optee_match,
+ .of_to_plat = optee_of_to_plat,
+ .probe = optee_probe,
+ .ops = &optee_ops,
+ .plat_auto = sizeof(struct optee_pdata),
+ .priv_auto = sizeof(struct optee_private),
+};
diff --git a/roms/u-boot/drivers/tee/optee/i2c.c b/roms/u-boot/drivers/tee/optee/i2c.c
new file mode 100644
index 000000000..ef4e10f99
--- /dev/null
+++ b/roms/u-boot/drivers/tee/optee/i2c.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2020 Foundries.io Ltd
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <i2c.h>
+#include <tee.h>
+#include "optee_msg.h"
+#include "optee_private.h"
+
+static int check_xfer_flags(struct udevice *chip, uint tee_flags)
+{
+ uint flags;
+ int ret;
+
+ ret = i2c_get_chip_flags(chip, &flags);
+ if (ret)
+ return ret;
+
+ if (tee_flags & OPTEE_MSG_RPC_CMD_I2C_FLAGS_TEN_BIT) {
+ if (!(flags & DM_I2C_CHIP_10BIT))
+ return -EINVAL;
+ } else {
+ if (flags & DM_I2C_CHIP_10BIT)
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+void optee_suppl_cmd_i2c_transfer(struct optee_msg_arg *arg)
+{
+ const u8 attr[] = {
+ OPTEE_MSG_ATTR_TYPE_VALUE_INPUT,
+ OPTEE_MSG_ATTR_TYPE_VALUE_INPUT,
+ OPTEE_MSG_ATTR_TYPE_RMEM_INOUT,
+ OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT,
+ };
+ struct udevice *chip_dev;
+ struct tee_shm *shm;
+ u8 *buf;
+ int ret;
+
+ if (arg->num_params != ARRAY_SIZE(attr) ||
+ arg->params[0].attr != attr[0] ||
+ arg->params[1].attr != attr[1] ||
+ arg->params[2].attr != attr[2] ||
+ arg->params[3].attr != attr[3]) {
+ goto bad;
+ }
+
+ shm = (struct tee_shm *)(unsigned long)arg->params[2].u.rmem.shm_ref;
+ buf = shm->addr;
+ if (!buf)
+ goto bad;
+
+ if (i2c_get_chip_for_busnum((int)arg->params[0].u.value.b,
+ (int)arg->params[0].u.value.c,
+ 0, &chip_dev))
+ goto bad;
+
+ if (check_xfer_flags(chip_dev, arg->params[1].u.value.a))
+ goto bad;
+
+ switch (arg->params[0].u.value.a) {
+ case OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD:
+ ret = dm_i2c_read(chip_dev, 0, buf,
+ (size_t)arg->params[2].u.rmem.size);
+ break;
+ case OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR:
+ ret = dm_i2c_write(chip_dev, 0, buf,
+ (size_t)arg->params[2].u.rmem.size);
+ break;
+ default:
+ goto bad;
+ }
+
+ if (ret) {
+ arg->ret = TEE_ERROR_COMMUNICATION;
+ } else {
+ arg->params[3].u.value.a = arg->params[2].u.rmem.size;
+ arg->ret = TEE_SUCCESS;
+ }
+
+ return;
+bad:
+ arg->ret = TEE_ERROR_BAD_PARAMETERS;
+}
diff --git a/roms/u-boot/drivers/tee/optee/optee_msg.h b/roms/u-boot/drivers/tee/optee/optee_msg.h
new file mode 100644
index 000000000..8d40ce60c
--- /dev/null
+++ b/roms/u-boot/drivers/tee/optee/optee_msg.h
@@ -0,0 +1,446 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2015-2018, Linaro Limited
+ */
+
+#ifndef _OPTEE_MSG_H
+#define _OPTEE_MSG_H
+
+#include <linux/bitops.h>
+#include <linux/types.h>
+
+/*
+ * This file defines the OP-TEE message protocol used to communicate with
+ * an instance of OP-TEE running in secure world. This file is based on
+ * https://github.com/OP-TEE/optee_os/blob/master/core/include/optee_msg.h
+ * and may need to be updated when introducing new features.
+ *
+ * This file is divided into three sections.
+ * 1. Formatting of messages.
+ * 2. Requests from normal world
+ * 3. Requests from secure world, Remote Procedure Call (RPC), handled by
+ * tee-supplicant.
+ */
+
+/*****************************************************************************
+ * Part 1 - formatting of messages
+ *****************************************************************************/
+
+#define OPTEE_MSG_ATTR_TYPE_NONE 0x0
+#define OPTEE_MSG_ATTR_TYPE_VALUE_INPUT 0x1
+#define OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT 0x2
+#define OPTEE_MSG_ATTR_TYPE_VALUE_INOUT 0x3
+#define OPTEE_MSG_ATTR_TYPE_RMEM_INPUT 0x5
+#define OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT 0x6
+#define OPTEE_MSG_ATTR_TYPE_RMEM_INOUT 0x7
+#define OPTEE_MSG_ATTR_TYPE_TMEM_INPUT 0x9
+#define OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT 0xa
+#define OPTEE_MSG_ATTR_TYPE_TMEM_INOUT 0xb
+
+#define OPTEE_MSG_ATTR_TYPE_MASK GENMASK(7, 0)
+
+/*
+ * Meta parameter to be absorbed by the Secure OS and not passed
+ * to the Trusted Application.
+ *
+ * Currently only used with OPTEE_MSG_CMD_OPEN_SESSION.
+ */
+#define OPTEE_MSG_ATTR_META BIT(8)
+
+/*
+ * Pointer to a list of pages used to register user-defined SHM buffer.
+ * Used with OPTEE_MSG_ATTR_TYPE_TMEM_*.
+ * buf_ptr should point to the beginning of the buffer. Buffer will contain
+ * list of page addresses. OP-TEE core can reconstruct contiguous buffer from
+ * that page addresses list. Page addresses are stored as 64 bit values.
+ * Last entry on a page should point to the next page of buffer.
+ * Every entry in buffer should point to a 4k page beginning (12 least
+ * significant bits must be equal to zero).
+ *
+ * 12 least significant bints of optee_msg_param.u.tmem.buf_ptr should hold page
+ * offset of the user buffer.
+ *
+ * So, entries should be placed like members of this structure:
+ *
+ * struct page_data {
+ * uint64_t pages_array[OPTEE_MSG_NONCONTIG_PAGE_SIZE/sizeof(uint64_t) - 1];
+ * uint64_t next_page_data;
+ * };
+ *
+ * Structure is designed to exactly fit into the page size
+ * OPTEE_MSG_NONCONTIG_PAGE_SIZE which is a standard 4KB page.
+ *
+ * The size of 4KB is chosen because this is the smallest page size for ARM
+ * architectures. If REE uses larger pages, it should divide them to 4KB ones.
+ */
+#define OPTEE_MSG_ATTR_NONCONTIG BIT(9)
+
+/*
+ * Memory attributes for caching passed with temp memrefs. The actual value
+ * used is defined outside the message protocol with the exception of
+ * OPTEE_MSG_ATTR_CACHE_PREDEFINED which means the attributes already
+ * defined for the memory range should be used. If optee_smc.h is used as
+ * bearer of this protocol OPTEE_SMC_SHM_* is used for values.
+ */
+#define OPTEE_MSG_ATTR_CACHE_SHIFT 16
+#define OPTEE_MSG_ATTR_CACHE_MASK GENMASK(2, 0)
+#define OPTEE_MSG_ATTR_CACHE_PREDEFINED 0
+
+/*
+ * Same values as TEE_LOGIN_* from TEE Internal API
+ */
+#define OPTEE_MSG_LOGIN_PUBLIC 0x00000000
+#define OPTEE_MSG_LOGIN_USER 0x00000001
+#define OPTEE_MSG_LOGIN_GROUP 0x00000002
+#define OPTEE_MSG_LOGIN_APPLICATION 0x00000004
+#define OPTEE_MSG_LOGIN_APPLICATION_USER 0x00000005
+#define OPTEE_MSG_LOGIN_APPLICATION_GROUP 0x00000006
+
+/*
+ * Page size used in non-contiguous buffer entries
+ */
+#define OPTEE_MSG_NONCONTIG_PAGE_SIZE 4096
+
+/**
+ * struct optee_msg_param_tmem - temporary memory reference parameter
+ * @buf_ptr: Address of the buffer
+ * @size: Size of the buffer
+ * @shm_ref: Temporary shared memory reference, pointer to a struct tee_shm
+ *
+ * Secure and normal world communicates pointers as physical address
+ * instead of the virtual address. This is because secure and normal world
+ * have completely independent memory mapping. Normal world can even have a
+ * hypervisor which need to translate the guest physical address (AKA IPA
+ * in ARM documentation) to a real physical address before passing the
+ * structure to secure world.
+ */
+struct optee_msg_param_tmem {
+ u64 buf_ptr;
+ u64 size;
+ u64 shm_ref;
+};
+
+/**
+ * struct optee_msg_param_rmem - registered memory reference parameter
+ * @offs: Offset into shared memory reference
+ * @size: Size of the buffer
+ * @shm_ref: Shared memory reference, pointer to a struct tee_shm
+ */
+struct optee_msg_param_rmem {
+ u64 offs;
+ u64 size;
+ u64 shm_ref;
+};
+
+/**
+ * struct optee_msg_param_value - opaque value parameter
+ *
+ * Value parameters are passed unchecked between normal and secure world.
+ */
+struct optee_msg_param_value {
+ u64 a;
+ u64 b;
+ u64 c;
+};
+
+/**
+ * struct optee_msg_param - parameter used together with struct optee_msg_arg
+ * @attr: attributes
+ * @tmem: parameter by temporary memory reference
+ * @rmem: parameter by registered memory reference
+ * @value: parameter by opaque value
+ *
+ * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in
+ * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value,
+ * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates @tmem and
+ * OPTEE_MSG_ATTR_TYPE_RMEM_* indicates @rmem,
+ * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.
+ */
+struct optee_msg_param {
+ u64 attr;
+ union {
+ struct optee_msg_param_tmem tmem;
+ struct optee_msg_param_rmem rmem;
+ struct optee_msg_param_value value;
+ } u;
+};
+
+/**
+ * struct optee_msg_arg - call argument
+ * @cmd: Command, one of OPTEE_MSG_CMD_* or OPTEE_MSG_RPC_CMD_*
+ * @func: Trusted Application function, specific to the Trusted Application,
+ * used if cmd == OPTEE_MSG_CMD_INVOKE_COMMAND
+ * @session: In parameter for all OPTEE_MSG_CMD_* except
+ * OPTEE_MSG_CMD_OPEN_SESSION where it's an output parameter instead
+ * @cancel_id: Cancellation id, a unique value to identify this request
+ * @ret: return value
+ * @ret_origin: origin of the return value
+ * @num_params: number of parameters supplied to the OS Command
+ * @params: the parameters supplied to the OS Command
+ *
+ * All normal calls to Trusted OS uses this struct. If cmd requires further
+ * information than what these field holds it can be passed as a parameter
+ * tagged as meta (setting the OPTEE_MSG_ATTR_META bit in corresponding
+ * attrs field). All parameters tagged as meta has to come first.
+ *
+ * Temp memref parameters can be fragmented if supported by the Trusted OS
+ * (when optee_smc.h is bearer of this protocol this is indicated with
+ * OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM). If a logical memref parameter is
+ * fragmented then has all but the last fragment the
+ * OPTEE_MSG_ATTR_FRAGMENT bit set in attrs. Even if a memref is fragmented
+ * it will still be presented as a single logical memref to the Trusted
+ * Application.
+ */
+struct optee_msg_arg {
+ u32 cmd;
+ u32 func;
+ u32 session;
+ u32 cancel_id;
+ u32 pad;
+ u32 ret;
+ u32 ret_origin;
+ u32 num_params;
+
+ /* num_params tells the actual number of element in params */
+ struct optee_msg_param params[0];
+};
+
+/**
+ * OPTEE_MSG_GET_ARG_SIZE - return size of struct optee_msg_arg
+ *
+ * @num_params: Number of parameters embedded in the struct optee_msg_arg
+ *
+ * Returns the size of the struct optee_msg_arg together with the number
+ * of embedded parameters.
+ */
+#define OPTEE_MSG_GET_ARG_SIZE(num_params) \
+ (sizeof(struct optee_msg_arg) + \
+ sizeof(struct optee_msg_param) * (num_params))
+
+/*****************************************************************************
+ * Part 2 - requests from normal world
+ *****************************************************************************/
+
+/*
+ * Return the following UID if using API specified in this file without
+ * further extensions:
+ * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.
+ * Represented in 4 32-bit words in OPTEE_MSG_UID_0, OPTEE_MSG_UID_1,
+ * OPTEE_MSG_UID_2, OPTEE_MSG_UID_3.
+ */
+#define OPTEE_MSG_UID_0 0x384fb3e0
+#define OPTEE_MSG_UID_1 0xe7f811e3
+#define OPTEE_MSG_UID_2 0xaf630002
+#define OPTEE_MSG_UID_3 0xa5d5c51b
+#define OPTEE_MSG_FUNCID_CALLS_UID 0xFF01
+
+/*
+ * Returns 2.0 if using API specified in this file without further
+ * extensions. Represented in 2 32-bit words in OPTEE_MSG_REVISION_MAJOR
+ * and OPTEE_MSG_REVISION_MINOR
+ */
+#define OPTEE_MSG_REVISION_MAJOR 2
+#define OPTEE_MSG_REVISION_MINOR 0
+#define OPTEE_MSG_FUNCID_CALLS_REVISION 0xFF03
+
+/*
+ * Get UUID of Trusted OS.
+ *
+ * Used by non-secure world to figure out which Trusted OS is installed.
+ * Note that returned UUID is the UUID of the Trusted OS, not of the API.
+ *
+ * Returns UUID in 4 32-bit words in the same way as
+ * OPTEE_MSG_FUNCID_CALLS_UID described above.
+ */
+#define OPTEE_MSG_OS_OPTEE_UUID_0 0x486178e0
+#define OPTEE_MSG_OS_OPTEE_UUID_1 0xe7f811e3
+#define OPTEE_MSG_OS_OPTEE_UUID_2 0xbc5e0002
+#define OPTEE_MSG_OS_OPTEE_UUID_3 0xa5d5c51b
+#define OPTEE_MSG_FUNCID_GET_OS_UUID 0x0000
+
+/*
+ * Get revision of Trusted OS.
+ *
+ * Used by non-secure world to figure out which version of the Trusted OS
+ * is installed. Note that the returned revision is the revision of the
+ * Trusted OS, not of the API.
+ *
+ * Returns revision in 2 32-bit words in the same way as
+ * OPTEE_MSG_CALLS_REVISION described above.
+ */
+#define OPTEE_MSG_FUNCID_GET_OS_REVISION 0x0001
+
+/*
+ * Do a secure call with struct optee_msg_arg as argument
+ * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd
+ *
+ * OPTEE_MSG_CMD_OPEN_SESSION opens a session to a Trusted Application.
+ * The first two parameters are tagged as meta, holding two value
+ * parameters to pass the following information:
+ * param[0].u.value.a-b uuid of Trusted Application
+ * param[1].u.value.a-b uuid of Client
+ * param[1].u.value.c Login class of client OPTEE_MSG_LOGIN_*
+ *
+ * OPTEE_MSG_CMD_INVOKE_COMMAND invokes a command a previously opened
+ * session to a Trusted Application. struct optee_msg_arg::func is Trusted
+ * Application function, specific to the Trusted Application.
+ *
+ * OPTEE_MSG_CMD_CLOSE_SESSION closes a previously opened session to
+ * Trusted Application.
+ *
+ * OPTEE_MSG_CMD_CANCEL cancels a currently invoked command.
+ *
+ * OPTEE_MSG_CMD_REGISTER_SHM registers a shared memory reference. The
+ * information is passed as:
+ * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
+ * [| OPTEE_MSG_ATTR_FRAGMENT]
+ * [in] param[0].u.tmem.buf_ptr physical address (of first fragment)
+ * [in] param[0].u.tmem.size size (of first fragment)
+ * [in] param[0].u.tmem.shm_ref holds shared memory reference
+ * ...
+ * The shared memory can optionally be fragmented, temp memrefs can follow
+ * each other with all but the last with the OPTEE_MSG_ATTR_FRAGMENT bit set.
+ *
+ * OPTEE_MSG_CMD_UNREGISTER_SHM unregisteres a previously registered shared
+ * memory reference. The information is passed as:
+ * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
+ * [in] param[0].u.rmem.shm_ref holds shared memory reference
+ * [in] param[0].u.rmem.offs 0
+ * [in] param[0].u.rmem.size 0
+ */
+#define OPTEE_MSG_CMD_OPEN_SESSION 0
+#define OPTEE_MSG_CMD_INVOKE_COMMAND 1
+#define OPTEE_MSG_CMD_CLOSE_SESSION 2
+#define OPTEE_MSG_CMD_CANCEL 3
+#define OPTEE_MSG_CMD_REGISTER_SHM 4
+#define OPTEE_MSG_CMD_UNREGISTER_SHM 5
+#define OPTEE_MSG_FUNCID_CALL_WITH_ARG 0x0004
+
+/*****************************************************************************
+ * Part 3 - Requests from secure world, RPC
+ *****************************************************************************/
+
+/*
+ * All RPC is done with a struct optee_msg_arg as bearer of information,
+ * struct optee_msg_arg::arg holds values defined by OPTEE_MSG_RPC_CMD_* below
+ *
+ * RPC communication with tee-supplicant is reversed compared to normal
+ * client communication desribed above. The supplicant receives requests
+ * and sends responses.
+ */
+
+/*
+ * Load a TA into memory, defined in tee-supplicant
+ */
+#define OPTEE_MSG_RPC_CMD_LOAD_TA 0
+
+/*
+ * Reserved
+ */
+#define OPTEE_MSG_RPC_CMD_RPMB 1
+
+/*
+ * File system access, defined in tee-supplicant
+ */
+#define OPTEE_MSG_RPC_CMD_FS 2
+
+/*
+ * Get time
+ *
+ * Returns number of seconds and nano seconds since the Epoch,
+ * 1970-01-01 00:00:00 +0000 (UTC).
+ *
+ * [out] param[0].u.value.a Number of seconds
+ * [out] param[0].u.value.b Number of nano seconds.
+ */
+#define OPTEE_MSG_RPC_CMD_GET_TIME 3
+
+/*
+ * Wait queue primitive, helper for secure world to implement a wait queue.
+ *
+ * If secure world need to wait for a secure world mutex it issues a sleep
+ * request instead of spinning in secure world. Conversely is a wakeup
+ * request issued when a secure world mutex with a thread waiting thread is
+ * unlocked.
+ *
+ * Waiting on a key
+ * [in] param[0].u.value.a OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP
+ * [in] param[0].u.value.b wait key
+ *
+ * Waking up a key
+ * [in] param[0].u.value.a OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP
+ * [in] param[0].u.value.b wakeup key
+ */
+#define OPTEE_MSG_RPC_CMD_WAIT_QUEUE 4
+#define OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP 0
+#define OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP 1
+
+/*
+ * Suspend execution
+ *
+ * [in] param[0].value .a number of milliseconds to suspend
+ */
+#define OPTEE_MSG_RPC_CMD_SUSPEND 5
+
+/*
+ * Allocate a piece of shared memory
+ *
+ * Shared memory can optionally be fragmented, to support that additional
+ * spare param entries are allocated to make room for eventual fragments.
+ * The spare param entries has .attr = OPTEE_MSG_ATTR_TYPE_NONE when
+ * unused. All returned temp memrefs except the last should have the
+ * OPTEE_MSG_ATTR_FRAGMENT bit set in the attr field.
+ *
+ * [in] param[0].u.value.a type of memory one of
+ * OPTEE_MSG_RPC_SHM_TYPE_* below
+ * [in] param[0].u.value.b requested size
+ * [in] param[0].u.value.c required alignment
+ *
+ * [out] param[0].u.tmem.buf_ptr physical address (of first fragment)
+ * [out] param[0].u.tmem.size size (of first fragment)
+ * [out] param[0].u.tmem.shm_ref shared memory reference
+ * ...
+ * [out] param[n].u.tmem.buf_ptr physical address
+ * [out] param[n].u.tmem.size size
+ * [out] param[n].u.tmem.shm_ref shared memory reference (same value
+ * as in param[n-1].u.tmem.shm_ref)
+ */
+#define OPTEE_MSG_RPC_CMD_SHM_ALLOC 6
+/* Memory that can be shared with a non-secure user space application */
+#define OPTEE_MSG_RPC_SHM_TYPE_APPL 0
+/* Memory only shared with non-secure kernel */
+#define OPTEE_MSG_RPC_SHM_TYPE_KERNEL 1
+
+/*
+ * Free shared memory previously allocated with OPTEE_MSG_RPC_CMD_SHM_ALLOC
+ *
+ * [in] param[0].u.value.a type of memory one of
+ * OPTEE_MSG_RPC_SHM_TYPE_* above
+ * [in] param[0].u.value.b value of shared memory reference
+ * returned in param[0].u.tmem.shm_ref
+ * above
+ */
+#define OPTEE_MSG_RPC_CMD_SHM_FREE 7
+
+/*
+ * Access a device on an i2c bus
+ *
+ * [in] param[0].u.value.a mode: RD(0), WR(1)
+ * [in] param[0].u.value.b i2c adapter
+ * [in] param[0].u.value.c i2c chip
+ *
+ * [in] param[1].u.value.a i2c control flags
+ *
+ * [in/out] memref[2] buffer to exchange the transfer data
+ * with the secure world
+ *
+ * [out] param[3].u.value.a bytes transferred by the driver
+ */
+#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER 21
+/* I2C master transfer modes */
+#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD 0
+#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR 1
+/* I2C master control flags */
+#define OPTEE_MSG_RPC_CMD_I2C_FLAGS_TEN_BIT BIT(0)
+
+#endif /* _OPTEE_MSG_H */
diff --git a/roms/u-boot/drivers/tee/optee/optee_msg_supplicant.h b/roms/u-boot/drivers/tee/optee/optee_msg_supplicant.h
new file mode 100644
index 000000000..963cfd478
--- /dev/null
+++ b/roms/u-boot/drivers/tee/optee/optee_msg_supplicant.h
@@ -0,0 +1,245 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2016-2018, Linaro Limited
+ */
+
+#ifndef __OPTEE_MSG_SUPPLICANT_H
+#define __OPTEE_MSG_SUPPLICANT_H
+
+/*
+ * This file is based on
+ * https://github.com/OP-TEE/optee_os/blob/master/core/include/optee_msg_supplicant.h
+ * and may need to be updated when introducing new features.
+ */
+
+/*
+ * Load a TA into memory
+ */
+#define OPTEE_MSG_RPC_CMD_LOAD_TA 0
+
+/*
+ * Replay Protected Memory Block access
+ */
+#define OPTEE_MSG_RPC_CMD_RPMB 1
+
+/*
+ * File system access
+ */
+#define OPTEE_MSG_RPC_CMD_FS 2
+
+/*
+ * Define protocol for messages with .cmd == OPTEE_MSG_RPC_CMD_FS and first
+ * parameter has the attribute OPTEE_MSG_ATTR_TYPE_VALUE_INPUT.
+ */
+
+/*
+ * Open a file
+ *
+ * [in] param[0].u.value.a OPTEE_MRF_OPEN
+ * [in] param[1].u.tmem a string holding the file name
+ * [out] param[2].u.value.a file descriptor of open file
+ */
+#define OPTEE_MRF_OPEN 0
+
+/*
+ * Create a file
+ *
+ * [in] param[0].u.value.a OPTEE_MRF_CREATE
+ * [in] param[1].u.tmem a string holding the file name
+ * [out] param[2].u.value.a file descriptor of open file
+ */
+#define OPTEE_MRF_CREATE 1
+
+/*
+ * Close a file
+ *
+ * [in] param[0].u.value.a OPTEE_MRF_CLOSE
+ * [in] param[0].u.value.b file descriptor of open file.
+ */
+#define OPTEE_MRF_CLOSE 2
+
+/*
+ * Read from a file
+ *
+ * [in] param[0].u.value.a OPTEE_MRF_READ
+ * [in] param[0].u.value.b file descriptor of open file
+ * [in] param[0].u.value.c offset into file
+ * [out] param[1].u.tmem buffer to hold returned data
+ */
+#define OPTEE_MRF_READ 3
+
+/*
+ * Write to a file
+ *
+ * [in] param[0].u.value.a OPTEE_MRF_WRITE
+ * [in] param[0].u.value.b file descriptor of open file
+ * [in] param[0].u.value.c offset into file
+ * [in] param[1].u.tmem buffer holding data to be written
+ */
+#define OPTEE_MRF_WRITE 4
+
+/*
+ * Truncate a file
+ *
+ * [in] param[0].u.value.a OPTEE_MRF_TRUNCATE
+ * [in] param[0].u.value.b file descriptor of open file
+ * [in] param[0].u.value.c length of file.
+ */
+#define OPTEE_MRF_TRUNCATE 5
+
+/*
+ * Remove a file
+ *
+ * [in] param[0].u.value.a OPTEE_MRF_REMOVE
+ * [in] param[1].u.tmem a string holding the file name
+ */
+#define OPTEE_MRF_REMOVE 6
+
+/*
+ * Rename a file
+ *
+ * [in] param[0].u.value.a OPTEE_MRF_RENAME
+ * [in] param[0].u.value.b true if existing target should be removed
+ * [in] param[1].u.tmem a string holding the old file name
+ * [in] param[2].u.tmem a string holding the new file name
+ */
+#define OPTEE_MRF_RENAME 7
+
+/*
+ * Opens a directory for file listing
+ *
+ * [in] param[0].u.value.a OPTEE_MRF_OPENDIR
+ * [in] param[1].u.tmem a string holding the name of the directory
+ * [out] param[2].u.value.a handle to open directory
+ */
+#define OPTEE_MRF_OPENDIR 8
+
+/*
+ * Closes a directory handle
+ *
+ * [in] param[0].u.value.a OPTEE_MRF_CLOSEDIR
+ * [in] param[0].u.value.b handle to open directory
+ */
+#define OPTEE_MRF_CLOSEDIR 9
+
+/*
+ * Read next file name of directory
+ *
+ *
+ * [in] param[0].u.value.a OPTEE_MRF_READDIR
+ * [in] param[0].u.value.b handle to open directory
+ * [out] param[1].u.tmem a string holding the file name
+ */
+#define OPTEE_MRF_READDIR 10
+
+/*
+ * End of definitions for messages with .cmd == OPTEE_MSG_RPC_CMD_FS
+ */
+
+/*
+ * Command Ids 3, 4 and 5 of OPTEE_MSG_RPC_CMD_xxx macros are reserved for use
+ * by the kernel driver.
+ */
+
+/*
+ * Shared memory allocation
+ */
+#define OPTEE_MSG_RPC_CMD_SHM_ALLOC 6
+#define OPTEE_MSG_RPC_CMD_SHM_FREE 7
+
+/*
+ * I2C bus access
+ */
+#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER 21
+
+/*
+ * Was OPTEE_MSG_RPC_CMD_SQL_FS, which isn't supported any longer
+ */
+#define OPTEE_MSG_RPC_CMD_SQL_FS_RESERVED 8
+
+/*
+ * GPROF support management commands
+ */
+#define OPTEE_MSG_RPC_CMD_GPROF 9
+
+/*
+ * Socket commands
+ */
+#define OPTEE_MSG_RPC_CMD_SOCKET 10
+
+/*
+ * Define protocol for messages with .cmd == OPTEE_MSG_RPC_CMD_SOCKET
+ */
+
+#define OPTEE_MRC_SOCKET_TIMEOUT_NONBLOCKING 0
+#define OPTEE_MRC_SOCKET_TIMEOUT_BLOCKING 0xffffffff
+
+/*
+ * Open socket
+ *
+ * [in] param[0].u.value.a OPTEE_MRC_SOCKET_OPEN
+ * [in] param[0].u.value.b TA instance id
+ * [in] param[1].u.value.a server port number
+ * [in] param[1].u.value.b protocol, TEE_ISOCKET_PROTOCOLID_*
+ * [in] param[1].u.value.c ip version TEE_IP_VERSION_* from tee_ipsocket.h
+ * [in] param[2].u.tmem server address
+ * [out] param[3].u.value.a socket handle (32-bit)
+ */
+#define OPTEE_MRC_SOCKET_OPEN 0
+
+/*
+ * Close socket
+ *
+ * [in] param[0].u.value.a OPTEE_MRC_SOCKET_CLOSE
+ * [in] param[0].u.value.b TA instance id
+ * [in] param[0].u.value.c socket handle
+ */
+#define OPTEE_MRC_SOCKET_CLOSE 1
+
+/*
+ * Close all sockets
+ *
+ * [in] param[0].u.value.a OPTEE_MRC_SOCKET_CLOSE_ALL
+ * [in] param[0].u.value.b TA instance id
+ */
+#define OPTEE_MRC_SOCKET_CLOSE_ALL 2
+
+/*
+ * Send data on socket
+ *
+ * [in] param[0].u.value.a OPTEE_MRC_SOCKET_SEND
+ * [in] param[0].u.value.b TA instance id
+ * [in] param[0].u.value.c socket handle
+ * [in] param[1].u.tmem buffer to transmit
+ * [in] param[2].u.value.a timeout ms or OPTEE_MRC_SOCKET_TIMEOUT_*
+ * [out] param[2].u.value.b number of transmitted bytes
+ */
+#define OPTEE_MRC_SOCKET_SEND 3
+
+/*
+ * Receive data on socket
+ *
+ * [in] param[0].u.value.a OPTEE_MRC_SOCKET_RECV
+ * [in] param[0].u.value.b TA instance id
+ * [in] param[0].u.value.c socket handle
+ * [out] param[1].u.tmem buffer to receive
+ * [in] param[2].u.value.a timeout ms or OPTEE_MRC_SOCKET_TIMEOUT_*
+ */
+#define OPTEE_MRC_SOCKET_RECV 4
+
+/*
+ * Perform IOCTL on socket
+ *
+ * [in] param[0].u.value.a OPTEE_MRC_SOCKET_IOCTL
+ * [in] param[0].u.value.b TA instance id
+ * [in] param[0].u.value.c socket handle
+ * [in/out] param[1].u.tmem buffer
+ * [in] param[2].u.value.a ioctl command
+ */
+#define OPTEE_MRC_SOCKET_IOCTL 5
+
+/*
+ * End of definitions for messages with .cmd == OPTEE_MSG_RPC_CMD_SOCKET
+ */
+
+#endif /* __OPTEE_MSG_SUPPLICANT_H */
diff --git a/roms/u-boot/drivers/tee/optee/optee_private.h b/roms/u-boot/drivers/tee/optee/optee_private.h
new file mode 100644
index 000000000..1f07a27ee
--- /dev/null
+++ b/roms/u-boot/drivers/tee/optee/optee_private.h
@@ -0,0 +1,82 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
+#ifndef __OPTEE_PRIVATE_H
+#define __OPTEE_PRIVATE_H
+
+#include <tee.h>
+#include <log.h>
+
+/**
+ * struct optee_private - OP-TEE driver private data
+ * @rpmb_mmc: mmc device for the RPMB partition
+ * @rpmb_dev_id: mmc device id matching @rpmb_mmc
+ * @rpmb_original_part: the previosly active partition on the mmc device,
+ * used to restore active the partition when the RPMB
+ * accesses are finished
+ */
+struct optee_private {
+ struct mmc *rpmb_mmc;
+ int rpmb_dev_id;
+ int rpmb_original_part;
+};
+
+struct optee_msg_arg;
+
+void optee_suppl_cmd(struct udevice *dev, struct tee_shm *shm_arg,
+ void **page_list);
+
+#ifdef CONFIG_SUPPORT_EMMC_RPMB
+/**
+ * optee_suppl_cmd_rpmb() - route RPMB frames to mmc
+ * @dev: device with the selected RPMB partition
+ * @arg: OP-TEE message holding the frames to transmit to the mmc
+ * and space for the response frames.
+ *
+ * Routes signed (MACed) RPMB frames from OP-TEE Secure OS to MMC and vice
+ * versa to manipulate the RPMB partition.
+ */
+void optee_suppl_cmd_rpmb(struct udevice *dev, struct optee_msg_arg *arg);
+
+/**
+ * optee_suppl_rpmb_release() - release mmc device
+ * @dev: mmc device
+ *
+ * Releases the mmc device and restores the previously selected partition.
+ */
+void optee_suppl_rpmb_release(struct udevice *dev);
+#else
+static inline void optee_suppl_cmd_rpmb(struct udevice *dev,
+ struct optee_msg_arg *arg)
+{
+ debug("OPTEE_MSG_RPC_CMD_RPMB not implemented\n");
+ arg->ret = TEE_ERROR_NOT_IMPLEMENTED;
+}
+
+static inline void optee_suppl_rpmb_release(struct udevice *dev)
+{
+}
+#endif
+
+#ifdef CONFIG_DM_I2C
+/**
+ * optee_suppl_cmd_i2c_transfer() - route I2C requests to an I2C chip
+ * @arg: OP-TEE message (layout specified in optee_msg.h) defining the
+ * transfer mode (read/write), adapter, chip and control flags.
+ *
+ * Handles OP-TEE requests to transfer data to the I2C chip on the I2C adapter.
+ */
+void optee_suppl_cmd_i2c_transfer(struct optee_msg_arg *arg);
+#else
+static inline void optee_suppl_cmd_i2c_transfer(struct optee_msg_arg *arg)
+{
+ debug("OPTEE_MSG_RPC_CMD_I2C_TRANSFER not implemented\n");
+ arg->ret = TEE_ERROR_NOT_IMPLEMENTED;
+}
+#endif
+
+void *optee_alloc_and_init_page_list(void *buf, ulong len, u64 *phys_buf_ptr);
+
+#endif /* __OPTEE_PRIVATE_H */
diff --git a/roms/u-boot/drivers/tee/optee/optee_smc.h b/roms/u-boot/drivers/tee/optee/optee_smc.h
new file mode 100644
index 000000000..4d643b259
--- /dev/null
+++ b/roms/u-boot/drivers/tee/optee/optee_smc.h
@@ -0,0 +1,450 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2015-2018, Linaro Limited
+ */
+
+#ifndef OPTEE_SMC_H
+#define OPTEE_SMC_H
+
+#include <linux/arm-smccc.h>
+#include <linux/bitops.h>
+
+/*
+ * This file is based on
+ * https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/include/sm/optee_smc.h
+ * and may need to be updated when introducing new features.
+ */
+
+#define OPTEE_SMC_STD_CALL_VAL(func_num) \
+ ARM_SMCCC_CALL_VAL(ARM_SMCCC_STD_CALL, ARM_SMCCC_SMC_32, \
+ ARM_SMCCC_OWNER_TRUSTED_OS, (func_num))
+#define OPTEE_SMC_FAST_CALL_VAL(func_num) \
+ ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
+ ARM_SMCCC_OWNER_TRUSTED_OS, (func_num))
+
+/*
+ * Function specified by SMC Calling convention.
+ */
+#define OPTEE_SMC_FUNCID_CALLS_COUNT 0xFF00
+#define OPTEE_SMC_CALLS_COUNT \
+ ARM_SMCCC_CALL_VAL(OPTEE_SMC_FAST_CALL, SMCCC_SMC_32, \
+ SMCCC_OWNER_TRUSTED_OS_END, \
+ OPTEE_SMC_FUNCID_CALLS_COUNT)
+
+/*
+ * Normal cached memory (write-back), shareable for SMP systems and not
+ * shareable for UP systems.
+ */
+#define OPTEE_SMC_SHM_CACHED 1
+
+/*
+ * a0..a7 is used as register names in the descriptions below, on arm32
+ * that translates to r0..r7 and on arm64 to w0..w7. In both cases it's
+ * 32-bit registers.
+ */
+
+/*
+ * Function specified by SMC Calling convention
+ *
+ * Return one of the following UIDs if using API specified in this file
+ * without further extentions:
+ * 65cb6b93-af0c-4617-8ed6-644a8d1140f8
+ * see also OPTEE_SMC_UID_* in optee_msg.h
+ */
+#define OPTEE_SMC_FUNCID_CALLS_UID OPTEE_MSG_FUNCID_CALLS_UID
+#define OPTEE_SMC_CALLS_UID \
+ ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
+ ARM_SMCCC_OWNER_TRUSTED_OS_END, \
+ OPTEE_SMC_FUNCID_CALLS_UID)
+
+/*
+ * Function specified by SMC Calling convention
+ *
+ * Returns 2.0 if using API specified in this file without further extentions.
+ * see also OPTEE_MSG_REVISION_* in optee_msg.h
+ */
+#define OPTEE_SMC_FUNCID_CALLS_REVISION OPTEE_MSG_FUNCID_CALLS_REVISION
+#define OPTEE_SMC_CALLS_REVISION \
+ ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
+ ARM_SMCCC_OWNER_TRUSTED_OS_END, \
+ OPTEE_SMC_FUNCID_CALLS_REVISION)
+
+struct optee_smc_calls_revision_result {
+ unsigned long major;
+ unsigned long minor;
+ unsigned long reserved0;
+ unsigned long reserved1;
+};
+
+/*
+ * Get UUID of Trusted OS.
+ *
+ * Used by non-secure world to figure out which Trusted OS is installed.
+ * Note that returned UUID is the UUID of the Trusted OS, not of the API.
+ *
+ * Returns UUID in a0-4 in the same way as OPTEE_SMC_CALLS_UID
+ * described above.
+ */
+#define OPTEE_SMC_FUNCID_GET_OS_UUID OPTEE_MSG_FUNCID_GET_OS_UUID
+#define OPTEE_SMC_CALL_GET_OS_UUID \
+ OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_OS_UUID)
+
+/*
+ * Get revision of Trusted OS.
+ *
+ * Used by non-secure world to figure out which version of the Trusted OS
+ * is installed. Note that the returned revision is the revision of the
+ * Trusted OS, not of the API.
+ *
+ * Returns revision in a0-1 in the same way as OPTEE_SMC_CALLS_REVISION
+ * described above. May optionally return a 32-bit build identifier in a2,
+ * with zero meaning unspecified.
+ */
+#define OPTEE_SMC_FUNCID_GET_OS_REVISION OPTEE_MSG_FUNCID_GET_OS_REVISION
+#define OPTEE_SMC_CALL_GET_OS_REVISION \
+ OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_OS_REVISION)
+
+struct optee_smc_call_get_os_revision_result {
+ unsigned long major;
+ unsigned long minor;
+ unsigned long build_id;
+ unsigned long reserved1;
+};
+
+/*
+ * Call with struct optee_msg_arg as argument
+ *
+ * Call register usage:
+ * a0 SMC Function ID, OPTEE_SMC*CALL_WITH_ARG
+ * a1 Upper 32bit of a 64bit physical pointer to a struct optee_msg_arg
+ * a2 Lower 32bit of a 64bit physical pointer to a struct optee_msg_arg
+ * a3 Cache settings, not used if physical pointer is in a predefined shared
+ * memory area else per OPTEE_SMC_SHM_*
+ * a4-6 Not used
+ * a7 Hypervisor Client ID register
+ *
+ * Normal return register usage:
+ * a0 Return value, OPTEE_SMC_RETURN_*
+ * a1-3 Not used
+ * a4-7 Preserved
+ *
+ * OPTEE_SMC_RETURN_ETHREAD_LIMIT return register usage:
+ * a0 Return value, OPTEE_SMC_RETURN_ETHREAD_LIMIT
+ * a1-3 Preserved
+ * a4-7 Preserved
+ *
+ * RPC return register usage:
+ * a0 Return value, OPTEE_SMC_RETURN_IS_RPC(val)
+ * a1-2 RPC parameters
+ * a3-7 Resume information, must be preserved
+ *
+ * Possible return values:
+ * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION Trusted OS does not recognize this
+ * function.
+ * OPTEE_SMC_RETURN_OK Call completed, result updated in
+ * the previously supplied struct
+ * optee_msg_arg.
+ * OPTEE_SMC_RETURN_ETHREAD_LIMIT Number of Trusted OS threads exceeded,
+ * try again later.
+ * OPTEE_SMC_RETURN_EBADADDR Bad physcial pointer to struct
+ * optee_msg_arg.
+ * OPTEE_SMC_RETURN_EBADCMD Bad/unknown cmd in struct optee_msg_arg
+ * OPTEE_SMC_RETURN_IS_RPC() Call suspended by RPC call to normal
+ * world.
+ */
+#define OPTEE_SMC_FUNCID_CALL_WITH_ARG OPTEE_MSG_FUNCID_CALL_WITH_ARG
+#define OPTEE_SMC_CALL_WITH_ARG \
+ OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_CALL_WITH_ARG)
+
+/*
+ * Get Shared Memory Config
+ *
+ * Returns the Secure/Non-secure shared memory config.
+ *
+ * Call register usage:
+ * a0 SMC Function ID, OPTEE_SMC_GET_SHM_CONFIG
+ * a1-6 Not used
+ * a7 Hypervisor Client ID register
+ *
+ * Have config return register usage:
+ * a0 OPTEE_SMC_RETURN_OK
+ * a1 Physical address of start of SHM
+ * a2 Size of of SHM
+ * a3 Cache settings of memory, as defined by the
+ * OPTEE_SMC_SHM_* values above
+ * a4-7 Preserved
+ *
+ * Not available register usage:
+ * a0 OPTEE_SMC_RETURN_ENOTAVAIL
+ * a1-3 Not used
+ * a4-7 Preserved
+ */
+#define OPTEE_SMC_FUNCID_GET_SHM_CONFIG 7
+#define OPTEE_SMC_GET_SHM_CONFIG \
+ OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_SHM_CONFIG)
+
+struct optee_smc_get_shm_config_result {
+ unsigned long status;
+ unsigned long start;
+ unsigned long size;
+ unsigned long settings;
+};
+
+/*
+ * Exchanges capabilities between normal world and secure world
+ *
+ * Call register usage:
+ * a0 SMC Function ID, OPTEE_SMC_EXCHANGE_CAPABILITIES
+ * a1 bitfield of normal world capabilities OPTEE_SMC_NSEC_CAP_*
+ * a2-6 Not used
+ * a7 Hypervisor Client ID register
+ *
+ * Normal return register usage:
+ * a0 OPTEE_SMC_RETURN_OK
+ * a1 bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
+ * a2-7 Preserved
+ *
+ * Error return register usage:
+ * a0 OPTEE_SMC_RETURN_ENOTAVAIL, can't use the capabilities from normal world
+ * a1 bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
+ * a2-7 Preserved
+ */
+/* Normal world works as a uniprocessor system */
+#define OPTEE_SMC_NSEC_CAP_UNIPROCESSOR BIT(0)
+/* Secure world has reserved shared memory for normal world to use */
+#define OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM BIT(0)
+/* Secure world can communicate via previously unregistered shared memory */
+#define OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM BIT(1)
+
+/*
+ * Secure world supports commands "register/unregister shared memory",
+ * secure world accepts command buffers located in any parts of non-secure RAM
+ */
+#define OPTEE_SMC_SEC_CAP_DYNAMIC_SHM BIT(2)
+
+#define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES 9
+#define OPTEE_SMC_EXCHANGE_CAPABILITIES \
+ OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES)
+
+struct optee_smc_exchange_capabilities_result {
+ unsigned long status;
+ unsigned long capabilities;
+ unsigned long reserved0;
+ unsigned long reserved1;
+};
+
+/*
+ * Disable and empties cache of shared memory objects
+ *
+ * Secure world can cache frequently used shared memory objects, for
+ * example objects used as RPC arguments. When secure world is idle this
+ * function returns one shared memory reference to free. To disable the
+ * cache and free all cached objects this function has to be called until
+ * it returns OPTEE_SMC_RETURN_ENOTAVAIL.
+ *
+ * Call register usage:
+ * a0 SMC Function ID, OPTEE_SMC_DISABLE_SHM_CACHE
+ * a1-6 Not used
+ * a7 Hypervisor Client ID register
+ *
+ * Normal return register usage:
+ * a0 OPTEE_SMC_RETURN_OK
+ * a1 Upper 32bit of a 64bit Shared memory cookie
+ * a2 Lower 32bit of a 64bit Shared memory cookie
+ * a3-7 Preserved
+ *
+ * Cache empty return register usage:
+ * a0 OPTEE_SMC_RETURN_ENOTAVAIL
+ * a1-7 Preserved
+ *
+ * Not idle return register usage:
+ * a0 OPTEE_SMC_RETURN_EBUSY
+ * a1-7 Preserved
+ */
+#define OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE 10
+#define OPTEE_SMC_DISABLE_SHM_CACHE \
+ OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE)
+
+struct optee_smc_disable_shm_cache_result {
+ unsigned long status;
+ unsigned long shm_upper32;
+ unsigned long shm_lower32;
+ unsigned long reserved0;
+};
+
+/*
+ * Enable cache of shared memory objects
+ *
+ * Secure world can cache frequently used shared memory objects, for
+ * example objects used as RPC arguments. When secure world is idle this
+ * function returns OPTEE_SMC_RETURN_OK and the cache is enabled. If
+ * secure world isn't idle OPTEE_SMC_RETURN_EBUSY is returned.
+ *
+ * Call register usage:
+ * a0 SMC Function ID, OPTEE_SMC_ENABLE_SHM_CACHE
+ * a1-6 Not used
+ * a7 Hypervisor Client ID register
+ *
+ * Normal return register usage:
+ * a0 OPTEE_SMC_RETURN_OK
+ * a1-7 Preserved
+ *
+ * Not idle return register usage:
+ * a0 OPTEE_SMC_RETURN_EBUSY
+ * a1-7 Preserved
+ */
+#define OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE 11
+#define OPTEE_SMC_ENABLE_SHM_CACHE \
+ OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE)
+
+/*
+ * Resume from RPC (for example after processing a foreign interrupt)
+ *
+ * Call register usage:
+ * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC
+ * a1-3 Value of a1-3 when OPTEE_SMC_CALL_WITH_ARG returned
+ * OPTEE_SMC_RETURN_RPC in a0
+ *
+ * Return register usage is the same as for OPTEE_SMC_*CALL_WITH_ARG above.
+ *
+ * Possible return values
+ * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION Trusted OS does not recognize this
+ * function.
+ * OPTEE_SMC_RETURN_OK Original call completed, result
+ * updated in the previously supplied.
+ * struct optee_msg_arg
+ * OPTEE_SMC_RETURN_RPC Call suspended by RPC call to normal
+ * world.
+ * OPTEE_SMC_RETURN_ERESUME Resume failed, the opaque resume
+ * information was corrupt.
+ */
+#define OPTEE_SMC_FUNCID_RETURN_FROM_RPC 3
+#define OPTEE_SMC_CALL_RETURN_FROM_RPC \
+ OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_RETURN_FROM_RPC)
+
+#define OPTEE_SMC_RETURN_RPC_PREFIX_MASK 0xFFFF0000
+#define OPTEE_SMC_RETURN_RPC_PREFIX 0xFFFF0000
+#define OPTEE_SMC_RETURN_RPC_FUNC_MASK 0x0000FFFF
+
+#define OPTEE_SMC_RETURN_GET_RPC_FUNC(ret) \
+ ((ret) & OPTEE_SMC_RETURN_RPC_FUNC_MASK)
+
+#define OPTEE_SMC_RPC_VAL(func) ((func) | OPTEE_SMC_RETURN_RPC_PREFIX)
+
+/*
+ * Allocate memory for RPC parameter passing. The memory is used to hold a
+ * struct optee_msg_arg.
+ *
+ * "Call" register usage:
+ * a0 This value, OPTEE_SMC_RETURN_RPC_ALLOC
+ * a1 Size in bytes of required argument memory
+ * a2 Not used
+ * a3 Resume information, must be preserved
+ * a4-5 Not used
+ * a6-7 Resume information, must be preserved
+ *
+ * "Return" register usage:
+ * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
+ * a1 Upper 32bits of 64bit physical pointer to allocated
+ * memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
+ * be allocated.
+ * a2 Lower 32bits of 64bit physical pointer to allocated
+ * memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
+ * be allocated
+ * a3 Preserved
+ * a4 Upper 32bits of 64bit Shared memory cookie used when freeing
+ * the memory or doing an RPC
+ * a5 Lower 32bits of 64bit Shared memory cookie used when freeing
+ * the memory or doing an RPC
+ * a6-7 Preserved
+ */
+#define OPTEE_SMC_RPC_FUNC_ALLOC 0
+#define OPTEE_SMC_RETURN_RPC_ALLOC \
+ OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_ALLOC)
+
+/*
+ * Free memory previously allocated by OPTEE_SMC_RETURN_RPC_ALLOC
+ *
+ * "Call" register usage:
+ * a0 This value, OPTEE_SMC_RETURN_RPC_FREE
+ * a1 Upper 32bits of 64bit shared memory cookie belonging to this
+ * argument memory
+ * a2 Lower 32bits of 64bit shared memory cookie belonging to this
+ * argument memory
+ * a3-7 Resume information, must be preserved
+ *
+ * "Return" register usage:
+ * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
+ * a1-2 Not used
+ * a3-7 Preserved
+ */
+#define OPTEE_SMC_RPC_FUNC_FREE 2
+#define OPTEE_SMC_RETURN_RPC_FREE \
+ OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FREE)
+
+/*
+ * Deliver foreign interrupt to normal world.
+ *
+ * "Call" register usage:
+ * a0 OPTEE_SMC_RETURN_RPC_FOREIGN_INTR
+ * a1-7 Resume information, must be preserved
+ *
+ * "Return" register usage:
+ * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
+ * a1-7 Preserved
+ */
+#define OPTEE_SMC_RPC_FUNC_FOREIGN_INTR 4
+#define OPTEE_SMC_RETURN_RPC_FOREIGN_INTR \
+ OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FOREIGN_INTR)
+
+/*
+ * Do an RPC request. The supplied struct optee_msg_arg tells which
+ * request to do and the parameters for the request. The following fields
+ * are used (the rest are unused):
+ * - cmd the Request ID
+ * - ret return value of the request, filled in by normal world
+ * - num_params number of parameters for the request
+ * - params the parameters
+ * - param_attrs attributes of the parameters
+ *
+ * "Call" register usage:
+ * a0 OPTEE_SMC_RETURN_RPC_CMD
+ * a1 Upper 32bit of a 64bit Shared memory cookie holding a
+ * struct optee_msg_arg, must be preserved, only the data should
+ * be updated
+ * a2 Lower 32bit of a 64bit Shared memory cookie holding a
+ * struct optee_msg_arg, must be preserved, only the data should
+ * be updated
+ * a3-7 Resume information, must be preserved
+ *
+ * "Return" register usage:
+ * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
+ * a1-2 Not used
+ * a3-7 Preserved
+ */
+#define OPTEE_SMC_RPC_FUNC_CMD 5
+#define OPTEE_SMC_RETURN_RPC_CMD \
+ OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_CMD)
+
+/* Returned in a0 */
+#define OPTEE_SMC_RETURN_UNKNOWN_FUNCTION 0xFFFFFFFF
+
+/* Returned in a0 only from Trusted OS functions */
+#define OPTEE_SMC_RETURN_OK 0x0
+#define OPTEE_SMC_RETURN_ETHREAD_LIMIT 0x1
+#define OPTEE_SMC_RETURN_EBUSY 0x2
+#define OPTEE_SMC_RETURN_ERESUME 0x3
+#define OPTEE_SMC_RETURN_EBADADDR 0x4
+#define OPTEE_SMC_RETURN_EBADCMD 0x5
+#define OPTEE_SMC_RETURN_ENOMEM 0x6
+#define OPTEE_SMC_RETURN_ENOTAVAIL 0x7
+#define OPTEE_SMC_RETURN_IS_RPC(ret) __optee_smc_return_is_rpc((ret))
+
+static inline bool __optee_smc_return_is_rpc(u32 ret)
+{
+ return ret != OPTEE_SMC_RETURN_UNKNOWN_FUNCTION &&
+ (ret & OPTEE_SMC_RETURN_RPC_PREFIX_MASK) ==
+ OPTEE_SMC_RETURN_RPC_PREFIX;
+}
+
+#endif /* OPTEE_SMC_H */
diff --git a/roms/u-boot/drivers/tee/optee/rpmb.c b/roms/u-boot/drivers/tee/optee/rpmb.c
new file mode 100644
index 000000000..0804fc963
--- /dev/null
+++ b/roms/u-boot/drivers/tee/optee/rpmb.c
@@ -0,0 +1,185 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <log.h>
+#include <tee.h>
+#include <mmc.h>
+#include <dm/device_compat.h>
+
+#include "optee_msg.h"
+#include "optee_private.h"
+
+/*
+ * Request and response definitions must be in sync with the secure side of
+ * OP-TEE.
+ */
+
+/* Request */
+struct rpmb_req {
+ u16 cmd;
+#define RPMB_CMD_DATA_REQ 0x00
+#define RPMB_CMD_GET_DEV_INFO 0x01
+ u16 dev_id;
+ u16 block_count;
+ /* Optional data frames (rpmb_data_frame) follow */
+};
+
+#define RPMB_REQ_DATA(req) ((void *)((struct rpmb_req *)(req) + 1))
+
+/* Response to device info request */
+struct rpmb_dev_info {
+ u8 cid[16];
+ u8 rpmb_size_mult; /* EXT CSD-slice 168: RPMB Size */
+ u8 rel_wr_sec_c; /* EXT CSD-slice 222: Reliable Write Sector */
+ /* Count */
+ u8 ret_code;
+#define RPMB_CMD_GET_DEV_INFO_RET_OK 0x00
+#define RPMB_CMD_GET_DEV_INFO_RET_ERROR 0x01
+};
+
+static void release_mmc(struct optee_private *priv)
+{
+ int rc;
+
+ if (!priv->rpmb_mmc)
+ return;
+
+ rc = blk_select_hwpart_devnum(IF_TYPE_MMC, priv->rpmb_dev_id,
+ priv->rpmb_original_part);
+ if (rc)
+ debug("%s: blk_select_hwpart_devnum() failed: %d\n",
+ __func__, rc);
+
+ priv->rpmb_mmc = NULL;
+}
+
+static struct mmc *get_mmc(struct optee_private *priv, int dev_id)
+{
+ struct mmc *mmc;
+ int rc;
+
+ if (priv->rpmb_mmc && priv->rpmb_dev_id == dev_id)
+ return priv->rpmb_mmc;
+
+ release_mmc(priv);
+
+ mmc = find_mmc_device(dev_id);
+ if (!mmc) {
+ debug("Cannot find RPMB device\n");
+ return NULL;
+ }
+ if (!(mmc->version & MMC_VERSION_MMC)) {
+ debug("Device id %d is not an eMMC device\n", dev_id);
+ return NULL;
+ }
+ if (mmc->version < MMC_VERSION_4_41) {
+ debug("Device id %d: RPMB not supported before version 4.41\n",
+ dev_id);
+ return NULL;
+ }
+
+ priv->rpmb_original_part = mmc_get_blk_desc(mmc)->hwpart;
+
+ rc = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_id, MMC_PART_RPMB);
+ if (rc) {
+ debug("Device id %d: cannot select RPMB partition: %d\n",
+ dev_id, rc);
+ return NULL;
+ }
+
+ priv->rpmb_mmc = mmc;
+ priv->rpmb_dev_id = dev_id;
+ return mmc;
+}
+
+static u32 rpmb_get_dev_info(u16 dev_id, struct rpmb_dev_info *info)
+{
+ struct mmc *mmc = find_mmc_device(dev_id);
+ int i;
+
+ if (!mmc)
+ return TEE_ERROR_ITEM_NOT_FOUND;
+
+ if (!mmc->ext_csd)
+ return TEE_ERROR_GENERIC;
+
+ for (i = 0; i < ARRAY_SIZE(mmc->cid); i++)
+ ((u32 *) info->cid)[i] = cpu_to_be32(mmc->cid[i]);
+
+ info->rel_wr_sec_c = mmc->ext_csd[222];
+ info->rpmb_size_mult = mmc->ext_csd[168];
+ info->ret_code = RPMB_CMD_GET_DEV_INFO_RET_OK;
+
+ return TEE_SUCCESS;
+}
+
+static u32 rpmb_process_request(struct optee_private *priv, void *req,
+ ulong req_size, void *rsp, ulong rsp_size)
+{
+ struct rpmb_req *sreq = req;
+ struct mmc *mmc;
+
+ if (req_size < sizeof(*sreq))
+ return TEE_ERROR_BAD_PARAMETERS;
+
+ switch (sreq->cmd) {
+ case RPMB_CMD_DATA_REQ:
+ mmc = get_mmc(priv, sreq->dev_id);
+ if (!mmc)
+ return TEE_ERROR_ITEM_NOT_FOUND;
+ if (mmc_rpmb_route_frames(mmc, RPMB_REQ_DATA(req),
+ req_size - sizeof(struct rpmb_req),
+ rsp, rsp_size))
+ return TEE_ERROR_BAD_PARAMETERS;
+ return TEE_SUCCESS;
+
+ case RPMB_CMD_GET_DEV_INFO:
+ if (req_size != sizeof(struct rpmb_req) ||
+ rsp_size != sizeof(struct rpmb_dev_info)) {
+ debug("Invalid req/rsp size\n");
+ return TEE_ERROR_BAD_PARAMETERS;
+ }
+ return rpmb_get_dev_info(sreq->dev_id, rsp);
+
+ default:
+ debug("Unsupported RPMB command: %d\n", sreq->cmd);
+ return TEE_ERROR_BAD_PARAMETERS;
+ }
+}
+
+void optee_suppl_cmd_rpmb(struct udevice *dev, struct optee_msg_arg *arg)
+{
+ struct tee_shm *req_shm;
+ struct tee_shm *rsp_shm;
+ void *req_buf;
+ void *rsp_buf;
+ ulong req_size;
+ ulong rsp_size;
+
+ if (arg->num_params != 2 ||
+ arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_RMEM_INPUT ||
+ arg->params[1].attr != OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT) {
+ arg->ret = TEE_ERROR_BAD_PARAMETERS;
+ return;
+ }
+
+ req_shm = (struct tee_shm *)(ulong)arg->params[0].u.rmem.shm_ref;
+ req_buf = (u8 *)req_shm->addr + arg->params[0].u.rmem.offs;
+ req_size = arg->params[0].u.rmem.size;
+
+ rsp_shm = (struct tee_shm *)(ulong)arg->params[1].u.rmem.shm_ref;
+ rsp_buf = (u8 *)rsp_shm->addr + arg->params[1].u.rmem.offs;
+ rsp_size = arg->params[1].u.rmem.size;
+
+ arg->ret = rpmb_process_request(dev_get_priv(dev), req_buf, req_size,
+ rsp_buf, rsp_size);
+}
+
+void optee_suppl_rpmb_release(struct udevice *dev)
+{
+ release_mmc(dev_get_priv(dev));
+}
diff --git a/roms/u-boot/drivers/tee/optee/supplicant.c b/roms/u-boot/drivers/tee/optee/supplicant.c
new file mode 100644
index 000000000..f9dd874b5
--- /dev/null
+++ b/roms/u-boot/drivers/tee/optee/supplicant.c
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2018, Linaro Limited
+ */
+
+#include <common.h>
+#include <log.h>
+#include <malloc.h>
+#include <tee.h>
+#include <linux/types.h>
+
+#include "optee_msg.h"
+#include "optee_msg_supplicant.h"
+#include "optee_private.h"
+#include "optee_smc.h"
+
+static void cmd_shm_alloc(struct udevice *dev, struct optee_msg_arg *arg,
+ void **page_list)
+{
+ int rc;
+ struct tee_shm *shm;
+ void *pl;
+ u64 ph_ptr;
+
+ arg->ret_origin = TEE_ORIGIN_COMMS;
+
+ if (arg->num_params != 1 ||
+ arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
+ arg->ret = TEE_ERROR_BAD_PARAMETERS;
+ return;
+ }
+
+ rc = __tee_shm_add(dev, 0, NULL, arg->params[0].u.value.b,
+ TEE_SHM_REGISTER | TEE_SHM_ALLOC, &shm);
+ if (rc) {
+ if (rc == -ENOMEM)
+ arg->ret = TEE_ERROR_OUT_OF_MEMORY;
+ else
+ arg->ret = TEE_ERROR_GENERIC;
+ return;
+ }
+
+ pl = optee_alloc_and_init_page_list(shm->addr, shm->size, &ph_ptr);
+ if (!pl) {
+ arg->ret = TEE_ERROR_OUT_OF_MEMORY;
+ tee_shm_free(shm);
+ return;
+ }
+
+ *page_list = pl;
+ arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
+ OPTEE_MSG_ATTR_NONCONTIG;
+ arg->params[0].u.tmem.buf_ptr = ph_ptr;
+ arg->params[0].u.tmem.size = shm->size;
+ arg->params[0].u.tmem.shm_ref = (ulong)shm;
+ arg->ret = TEE_SUCCESS;
+}
+
+static void cmd_shm_free(struct optee_msg_arg *arg)
+{
+ arg->ret_origin = TEE_ORIGIN_COMMS;
+
+ if (arg->num_params != 1 ||
+ arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
+ arg->ret = TEE_ERROR_BAD_PARAMETERS;
+ return;
+ }
+
+ tee_shm_free((struct tee_shm *)(ulong)arg->params[0].u.value.b);
+ arg->ret = TEE_SUCCESS;
+}
+
+void optee_suppl_cmd(struct udevice *dev, struct tee_shm *shm_arg,
+ void **page_list)
+{
+ struct optee_msg_arg *arg = shm_arg->addr;
+
+ switch (arg->cmd) {
+ case OPTEE_MSG_RPC_CMD_SHM_ALLOC:
+ cmd_shm_alloc(dev, arg, page_list);
+ break;
+ case OPTEE_MSG_RPC_CMD_SHM_FREE:
+ cmd_shm_free(arg);
+ break;
+ case OPTEE_MSG_RPC_CMD_FS:
+ debug("REE FS storage isn't available\n");
+ arg->ret = TEE_ERROR_STORAGE_NOT_AVAILABLE;
+ break;
+ case OPTEE_MSG_RPC_CMD_RPMB:
+ optee_suppl_cmd_rpmb(dev, arg);
+ break;
+ case OPTEE_MSG_RPC_CMD_I2C_TRANSFER:
+ optee_suppl_cmd_i2c_transfer(arg);
+ break;
+ default:
+ arg->ret = TEE_ERROR_NOT_IMPLEMENTED;
+ }
+
+ arg->ret_origin = TEE_ORIGIN_COMMS;
+}
diff --git a/roms/u-boot/drivers/tee/sandbox.c b/roms/u-boot/drivers/tee/sandbox.c
new file mode 100644
index 000000000..35e8542fa
--- /dev/null
+++ b/roms/u-boot/drivers/tee/sandbox.c
@@ -0,0 +1,577 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018 Linaro Limited
+ */
+#include <common.h>
+#include <dm.h>
+#include <sandboxtee.h>
+#include <tee.h>
+#include <tee/optee_ta_avb.h>
+#include <tee/optee_ta_rpc_test.h>
+#include <tee/optee_ta_scp03.h>
+
+#include "optee/optee_msg.h"
+#include "optee/optee_private.h"
+
+/*
+ * The sandbox tee driver tries to emulate a generic Trusted Exectution
+ * Environment (TEE) with the Trusted Applications (TA) OPTEE_TA_AVB and
+ * OPTEE_TA_RPC_TEST available.
+ */
+
+static const u32 pstorage_max = 16;
+/**
+ * struct ta_entry - TA entries
+ * @uuid: UUID of an emulated TA
+ * @open_session Called when a session is openened to the TA
+ * @invoke_func Called when a function in the TA is to be invoked
+ *
+ * This struct is used to register TAs in this sandbox emulation of a TEE.
+ */
+struct ta_entry {
+ struct tee_optee_ta_uuid uuid;
+ u32 (*open_session)(struct udevice *dev, uint num_params,
+ struct tee_param *params);
+ u32 (*invoke_func)(struct udevice *dev,
+ u32 func, uint num_params,
+ struct tee_param *params);
+};
+
+static int get_msg_arg(struct udevice *dev, uint num_params,
+ struct tee_shm **shmp, struct optee_msg_arg **msg_arg)
+{
+ int rc;
+ struct optee_msg_arg *ma;
+
+ rc = __tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
+ OPTEE_MSG_GET_ARG_SIZE(num_params), TEE_SHM_ALLOC,
+ shmp);
+ if (rc)
+ return rc;
+
+ ma = (*shmp)->addr;
+ memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
+ ma->num_params = num_params;
+ *msg_arg = ma;
+
+ return 0;
+}
+
+void *optee_alloc_and_init_page_list(void *buf, ulong len,
+ u64 *phys_buf_ptr)
+{
+ /*
+ * An empty stub is added just to fix linking issues.
+ * This function isn't supposed to be called in sandbox
+ * setup, otherwise replace this with a proper
+ * implementation from optee/core.c
+ */
+ return NULL;
+}
+
+#if defined(CONFIG_OPTEE_TA_SCP03) || defined(CONFIG_OPTEE_TA_AVB)
+static u32 get_attr(uint n, uint num_params, struct tee_param *params)
+{
+ if (n >= num_params)
+ return TEE_PARAM_ATTR_TYPE_NONE;
+
+ return params[n].attr;
+}
+
+static u32 check_params(u8 p0, u8 p1, u8 p2, u8 p3, uint num_params,
+ struct tee_param *params)
+{
+ u8 p[] = { p0, p1, p2, p3 };
+ uint n;
+
+ for (n = 0; n < ARRAY_SIZE(p); n++)
+ if (p[n] != get_attr(n, num_params, params))
+ goto bad_params;
+
+ for (; n < num_params; n++)
+ if (get_attr(n, num_params, params))
+ goto bad_params;
+
+ return TEE_SUCCESS;
+
+bad_params:
+ printf("Bad param attrs\n");
+
+ return TEE_ERROR_BAD_PARAMETERS;
+}
+#endif
+
+#ifdef CONFIG_OPTEE_TA_SCP03
+static u32 pta_scp03_open_session(struct udevice *dev, uint num_params,
+ struct tee_param *params)
+{
+ /*
+ * We don't expect additional parameters when opening a session to
+ * this TA.
+ */
+ return check_params(TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
+ num_params, params);
+}
+
+static u32 pta_scp03_invoke_func(struct udevice *dev, u32 func, uint num_params,
+ struct tee_param *params)
+{
+ u32 res;
+ static bool enabled;
+
+ switch (func) {
+ case PTA_CMD_ENABLE_SCP03:
+ res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ num_params, params);
+ if (res)
+ return res;
+
+ if (!enabled) {
+ enabled = true;
+ } else {
+ }
+
+ if (params[0].u.value.a)
+
+ return TEE_SUCCESS;
+ default:
+ return TEE_ERROR_NOT_SUPPORTED;
+ }
+}
+#endif
+
+#ifdef CONFIG_OPTEE_TA_AVB
+static u32 ta_avb_open_session(struct udevice *dev, uint num_params,
+ struct tee_param *params)
+{
+ /*
+ * We don't expect additional parameters when opening a session to
+ * this TA.
+ */
+ return check_params(TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
+ num_params, params);
+}
+
+static u32 ta_avb_invoke_func(struct udevice *dev, u32 func, uint num_params,
+ struct tee_param *params)
+{
+ struct sandbox_tee_state *state = dev_get_priv(dev);
+ struct env_entry e, *ep;
+ char *name;
+ u32 res;
+ uint slot;
+ u64 val;
+ char *value;
+ u32 value_sz;
+
+ switch (func) {
+ case TA_AVB_CMD_READ_ROLLBACK_INDEX:
+ res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
+ TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ num_params, params);
+ if (res)
+ return res;
+
+ slot = params[0].u.value.a;
+ if (slot >= ARRAY_SIZE(state->ta_avb_rollback_indexes)) {
+ printf("Rollback index slot out of bounds %u\n", slot);
+ return TEE_ERROR_BAD_PARAMETERS;
+ }
+
+ val = state->ta_avb_rollback_indexes[slot];
+ params[1].u.value.a = val >> 32;
+ params[1].u.value.b = val;
+ return TEE_SUCCESS;
+
+ case TA_AVB_CMD_WRITE_ROLLBACK_INDEX:
+ res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
+ TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ num_params, params);
+ if (res)
+ return res;
+
+ slot = params[0].u.value.a;
+ if (slot >= ARRAY_SIZE(state->ta_avb_rollback_indexes)) {
+ printf("Rollback index slot out of bounds %u\n", slot);
+ return TEE_ERROR_BAD_PARAMETERS;
+ }
+
+ val = (u64)params[1].u.value.a << 32 | params[1].u.value.b;
+ if (val < state->ta_avb_rollback_indexes[slot])
+ return TEE_ERROR_SECURITY;
+
+ state->ta_avb_rollback_indexes[slot] = val;
+ return TEE_SUCCESS;
+
+ case TA_AVB_CMD_READ_LOCK_STATE:
+ res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ num_params, params);
+ if (res)
+ return res;
+
+ params[0].u.value.a = state->ta_avb_lock_state;
+ return TEE_SUCCESS;
+
+ case TA_AVB_CMD_WRITE_LOCK_STATE:
+ res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ num_params, params);
+ if (res)
+ return res;
+
+ if (state->ta_avb_lock_state != params[0].u.value.a) {
+ state->ta_avb_lock_state = params[0].u.value.a;
+ memset(state->ta_avb_rollback_indexes, 0,
+ sizeof(state->ta_avb_rollback_indexes));
+ }
+
+ return TEE_SUCCESS;
+ case TA_AVB_CMD_READ_PERSIST_VALUE:
+ res = check_params(TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
+ TEE_PARAM_ATTR_TYPE_MEMREF_INOUT,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ num_params, params);
+ if (res)
+ return res;
+
+ name = params[0].u.memref.shm->addr;
+
+ value = params[1].u.memref.shm->addr;
+ value_sz = params[1].u.memref.size;
+
+ e.key = name;
+ e.data = NULL;
+ hsearch_r(e, ENV_FIND, &ep, &state->pstorage_htab, 0);
+ if (!ep)
+ return TEE_ERROR_ITEM_NOT_FOUND;
+
+ value_sz = strlen(ep->data) + 1;
+ memcpy(value, ep->data, value_sz);
+
+ return TEE_SUCCESS;
+ case TA_AVB_CMD_WRITE_PERSIST_VALUE:
+ res = check_params(TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
+ TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ num_params, params);
+ if (res)
+ return res;
+
+ name = params[0].u.memref.shm->addr;
+
+ value = params[1].u.memref.shm->addr;
+ value_sz = params[1].u.memref.size;
+
+ e.key = name;
+ e.data = NULL;
+ hsearch_r(e, ENV_FIND, &ep, &state->pstorage_htab, 0);
+ if (ep)
+ hdelete_r(e.key, &state->pstorage_htab, 0);
+
+ e.key = name;
+ e.data = value;
+ hsearch_r(e, ENV_ENTER, &ep, &state->pstorage_htab, 0);
+ if (!ep)
+ return TEE_ERROR_OUT_OF_MEMORY;
+
+ return TEE_SUCCESS;
+
+ default:
+ return TEE_ERROR_NOT_SUPPORTED;
+ }
+}
+#endif /* OPTEE_TA_AVB */
+
+#ifdef CONFIG_OPTEE_TA_RPC_TEST
+static u32 ta_rpc_test_open_session(struct udevice *dev, uint num_params,
+ struct tee_param *params)
+{
+ /*
+ * We don't expect additional parameters when opening a session to
+ * this TA.
+ */
+ return check_params(TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
+ num_params, params);
+}
+
+static void fill_i2c_rpc_params(struct optee_msg_arg *msg_arg, u64 bus_num,
+ u64 chip_addr, u64 xfer_flags, u64 op,
+ struct tee_param_memref memref)
+{
+ msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
+ msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
+ msg_arg->params[2].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INOUT;
+ msg_arg->params[3].attr = OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT;
+
+ /* trigger I2C services of TEE supplicant */
+ msg_arg->cmd = OPTEE_MSG_RPC_CMD_I2C_TRANSFER;
+
+ msg_arg->params[0].u.value.a = op;
+ msg_arg->params[0].u.value.b = bus_num;
+ msg_arg->params[0].u.value.c = chip_addr;
+ msg_arg->params[1].u.value.a = xfer_flags;
+
+ /* buffer to read/write data */
+ msg_arg->params[2].u.rmem.shm_ref = (ulong)memref.shm;
+ msg_arg->params[2].u.rmem.size = memref.size;
+ msg_arg->params[2].u.rmem.offs = memref.shm_offs;
+
+ msg_arg->num_params = 4;
+}
+
+static u32 ta_rpc_test_invoke_func(struct udevice *dev, u32 func,
+ uint num_params,
+ struct tee_param *params)
+{
+ struct tee_shm *shm;
+ struct tee_param_memref memref_data;
+ struct optee_msg_arg *msg_arg;
+ int chip_addr, bus_num, op, xfer_flags;
+ int res;
+
+ res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
+ TEE_PARAM_ATTR_TYPE_MEMREF_INOUT,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ TEE_PARAM_ATTR_TYPE_NONE,
+ num_params, params);
+ if (res)
+ return TEE_ERROR_BAD_PARAMETERS;
+
+ bus_num = params[0].u.value.a;
+ chip_addr = params[0].u.value.b;
+ xfer_flags = params[0].u.value.c;
+ memref_data = params[1].u.memref;
+
+ switch (func) {
+ case TA_RPC_TEST_CMD_I2C_READ:
+ op = OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD;
+ break;
+ case TA_RPC_TEST_CMD_I2C_WRITE:
+ op = OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR;
+ break;
+ default:
+ return TEE_ERROR_NOT_SUPPORTED;
+ }
+
+ /*
+ * Fill params for an RPC call to tee supplicant
+ */
+ res = get_msg_arg(dev, 4, &shm, &msg_arg);
+ if (res)
+ goto out;
+
+ fill_i2c_rpc_params(msg_arg, bus_num, chip_addr, xfer_flags, op,
+ memref_data);
+
+ /* Make an RPC call to tee supplicant */
+ optee_suppl_cmd(dev, shm, 0);
+ res = msg_arg->ret;
+out:
+ tee_shm_free(shm);
+
+ return res;
+}
+#endif /* CONFIG_OPTEE_TA_RPC_TEST */
+
+static const struct ta_entry ta_entries[] = {
+#ifdef CONFIG_OPTEE_TA_AVB
+ { .uuid = TA_AVB_UUID,
+ .open_session = ta_avb_open_session,
+ .invoke_func = ta_avb_invoke_func,
+ },
+#endif
+#ifdef CONFIG_OPTEE_TA_RPC_TEST
+ { .uuid = TA_RPC_TEST_UUID,
+ .open_session = ta_rpc_test_open_session,
+ .invoke_func = ta_rpc_test_invoke_func,
+ },
+#endif
+#ifdef CONFIG_OPTEE_TA_SCP03
+ { .uuid = PTA_SCP03_UUID,
+ .open_session = pta_scp03_open_session,
+ .invoke_func = pta_scp03_invoke_func,
+ },
+#endif
+};
+
+static void sandbox_tee_get_version(struct udevice *dev,
+ struct tee_version_data *vers)
+{
+ struct tee_version_data v = {
+ .gen_caps = TEE_GEN_CAP_GP | TEE_GEN_CAP_REG_MEM,
+ };
+
+ *vers = v;
+}
+
+static int sandbox_tee_close_session(struct udevice *dev, u32 session)
+{
+ struct sandbox_tee_state *state = dev_get_priv(dev);
+
+ if (!state->ta || state->session != session)
+ return -EINVAL;
+
+ state->session = 0;
+ state->ta = NULL;
+
+ return 0;
+}
+
+static const struct ta_entry *find_ta_entry(u8 uuid[TEE_UUID_LEN])
+{
+ struct tee_optee_ta_uuid u;
+ uint n;
+
+ tee_optee_ta_uuid_from_octets(&u, uuid);
+
+ for (n = 0; n < ARRAY_SIZE(ta_entries); n++)
+ if (!memcmp(&u, &ta_entries[n].uuid, sizeof(u)))
+ return ta_entries + n;
+
+ return NULL;
+}
+
+static int sandbox_tee_open_session(struct udevice *dev,
+ struct tee_open_session_arg *arg,
+ uint num_params, struct tee_param *params)
+{
+ struct sandbox_tee_state *state = dev_get_priv(dev);
+ const struct ta_entry *ta;
+
+ if (state->ta) {
+ printf("A session is already open\n");
+ return -EBUSY;
+ }
+
+ ta = find_ta_entry(arg->uuid);
+ if (!ta) {
+ printf("Cannot find TA\n");
+ arg->ret = TEE_ERROR_ITEM_NOT_FOUND;
+ arg->ret_origin = TEE_ORIGIN_TEE;
+
+ return 0;
+ }
+
+ arg->ret = ta->open_session(dev, num_params, params);
+ arg->ret_origin = TEE_ORIGIN_TRUSTED_APP;
+
+ if (!arg->ret) {
+ state->ta = (void *)ta;
+ state->session = 1;
+ arg->session = state->session;
+ } else {
+ printf("Cannot open session, TA returns error\n");
+ }
+
+ return 0;
+}
+
+static int sandbox_tee_invoke_func(struct udevice *dev,
+ struct tee_invoke_arg *arg,
+ uint num_params, struct tee_param *params)
+{
+ struct sandbox_tee_state *state = dev_get_priv(dev);
+ struct ta_entry *ta = state->ta;
+
+ if (!arg->session) {
+ printf("Missing session\n");
+ return -EINVAL;
+ }
+
+ if (!ta) {
+ printf("TA session not available\n");
+ return -EINVAL;
+ }
+
+ if (arg->session != state->session) {
+ printf("Session mismatch\n");
+ return -EINVAL;
+ }
+
+ arg->ret = ta->invoke_func(dev, arg->func, num_params, params);
+ arg->ret_origin = TEE_ORIGIN_TRUSTED_APP;
+
+ return 0;
+}
+
+static int sandbox_tee_shm_register(struct udevice *dev, struct tee_shm *shm)
+{
+ struct sandbox_tee_state *state = dev_get_priv(dev);
+
+ state->num_shms++;
+
+ return 0;
+}
+
+static int sandbox_tee_shm_unregister(struct udevice *dev, struct tee_shm *shm)
+{
+ struct sandbox_tee_state *state = dev_get_priv(dev);
+
+ state->num_shms--;
+
+ return 0;
+}
+
+static int sandbox_tee_remove(struct udevice *dev)
+{
+ struct sandbox_tee_state *state = dev_get_priv(dev);
+
+ hdestroy_r(&state->pstorage_htab);
+
+ return 0;
+}
+
+static int sandbox_tee_probe(struct udevice *dev)
+{
+ struct sandbox_tee_state *state = dev_get_priv(dev);
+ /*
+ * With this hastable we emulate persistent storage,
+ * which should contain persistent values
+ * between different sessions/command invocations.
+ */
+ if (!hcreate_r(pstorage_max, &state->pstorage_htab))
+ return TEE_ERROR_OUT_OF_MEMORY;
+
+ return 0;
+}
+
+static const struct tee_driver_ops sandbox_tee_ops = {
+ .get_version = sandbox_tee_get_version,
+ .open_session = sandbox_tee_open_session,
+ .close_session = sandbox_tee_close_session,
+ .invoke_func = sandbox_tee_invoke_func,
+ .shm_register = sandbox_tee_shm_register,
+ .shm_unregister = sandbox_tee_shm_unregister,
+};
+
+static const struct udevice_id sandbox_tee_match[] = {
+ { .compatible = "sandbox,tee" },
+ {},
+};
+
+U_BOOT_DRIVER(sandbox_tee) = {
+ .name = "sandbox_tee",
+ .id = UCLASS_TEE,
+ .of_match = sandbox_tee_match,
+ .ops = &sandbox_tee_ops,
+ .priv_auto = sizeof(struct sandbox_tee_state),
+ .probe = sandbox_tee_probe,
+ .remove = sandbox_tee_remove,
+};
diff --git a/roms/u-boot/drivers/tee/tee-uclass.c b/roms/u-boot/drivers/tee/tee-uclass.c
new file mode 100644
index 000000000..2cc6b6c40
--- /dev/null
+++ b/roms/u-boot/drivers/tee/tee-uclass.c
@@ -0,0 +1,235 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <log.h>
+#include <malloc.h>
+#include <tee.h>
+#include <dm/device-internal.h>
+#include <dm/uclass-internal.h>
+
+/**
+ * struct tee_uclass_priv - information of a TEE, stored by the uclass
+ *
+ * @list_shm: list of structe tee_shm representing memory blocks shared
+ * with the TEE.
+ */
+struct tee_uclass_priv {
+ struct list_head list_shm;
+};
+
+static const struct tee_driver_ops *tee_get_ops(struct udevice *dev)
+{
+ return device_get_ops(dev);
+}
+
+void tee_get_version(struct udevice *dev, struct tee_version_data *vers)
+{
+ tee_get_ops(dev)->get_version(dev, vers);
+}
+
+int tee_open_session(struct udevice *dev, struct tee_open_session_arg *arg,
+ uint num_param, struct tee_param *param)
+{
+ return tee_get_ops(dev)->open_session(dev, arg, num_param, param);
+}
+
+int tee_close_session(struct udevice *dev, u32 session)
+{
+ return tee_get_ops(dev)->close_session(dev, session);
+}
+
+int tee_invoke_func(struct udevice *dev, struct tee_invoke_arg *arg,
+ uint num_param, struct tee_param *param)
+{
+ return tee_get_ops(dev)->invoke_func(dev, arg, num_param, param);
+}
+
+int __tee_shm_add(struct udevice *dev, ulong align, void *addr, ulong size,
+ u32 flags, struct tee_shm **shmp)
+{
+ struct tee_shm *shm;
+ void *p = addr;
+ int rc;
+
+ if (flags & TEE_SHM_ALLOC) {
+ if (align)
+ p = memalign(align, size);
+ else
+ p = malloc(size);
+ }
+ if (!p)
+ return -ENOMEM;
+
+ shm = calloc(1, sizeof(*shm));
+ if (!shm) {
+ rc = -ENOMEM;
+ goto err;
+ }
+
+ shm->dev = dev;
+ shm->addr = p;
+ shm->size = size;
+ shm->flags = flags;
+
+ if (flags & TEE_SHM_SEC_REGISTER) {
+ rc = tee_get_ops(dev)->shm_register(dev, shm);
+ if (rc)
+ goto err;
+ }
+
+ if (flags & TEE_SHM_REGISTER) {
+ struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
+
+ list_add(&shm->link, &priv->list_shm);
+ }
+
+ *shmp = shm;
+
+ return 0;
+err:
+ free(shm);
+ if (flags & TEE_SHM_ALLOC)
+ free(p);
+
+ return rc;
+}
+
+int tee_shm_alloc(struct udevice *dev, ulong size, u32 flags,
+ struct tee_shm **shmp)
+{
+ u32 f = flags;
+
+ f |= TEE_SHM_SEC_REGISTER | TEE_SHM_REGISTER | TEE_SHM_ALLOC;
+
+ return __tee_shm_add(dev, 0, NULL, size, f, shmp);
+}
+
+int tee_shm_register(struct udevice *dev, void *addr, ulong size, u32 flags,
+ struct tee_shm **shmp)
+{
+ u32 f = flags & ~TEE_SHM_ALLOC;
+
+ f |= TEE_SHM_SEC_REGISTER | TEE_SHM_REGISTER;
+
+ return __tee_shm_add(dev, 0, addr, size, f, shmp);
+}
+
+void tee_shm_free(struct tee_shm *shm)
+{
+ if (!shm)
+ return;
+
+ if (shm->flags & TEE_SHM_SEC_REGISTER)
+ tee_get_ops(shm->dev)->shm_unregister(shm->dev, shm);
+
+ if (shm->flags & TEE_SHM_REGISTER)
+ list_del(&shm->link);
+
+ if (shm->flags & TEE_SHM_ALLOC)
+ free(shm->addr);
+
+ free(shm);
+}
+
+bool tee_shm_is_registered(struct tee_shm *shm, struct udevice *dev)
+{
+ struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
+ struct tee_shm *s;
+
+ list_for_each_entry(s, &priv->list_shm, link)
+ if (s == shm)
+ return true;
+
+ return false;
+}
+
+struct udevice *tee_find_device(struct udevice *start,
+ int (*match)(struct tee_version_data *vers,
+ const void *data),
+ const void *data,
+ struct tee_version_data *vers)
+{
+ struct udevice *dev = start;
+ struct tee_version_data lv;
+ struct tee_version_data *v = vers ? vers : &lv;
+
+ if (!dev)
+ uclass_find_first_device(UCLASS_TEE, &dev);
+ else
+ uclass_find_next_device(&dev);
+
+ for (; dev; uclass_find_next_device(&dev)) {
+ if (device_probe(dev))
+ continue;
+ tee_get_ops(dev)->get_version(dev, v);
+ if (!match || match(v, data))
+ return dev;
+ }
+
+ return NULL;
+}
+
+static int tee_pre_probe(struct udevice *dev)
+{
+ struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
+
+ INIT_LIST_HEAD(&priv->list_shm);
+
+ return 0;
+}
+
+static int tee_pre_remove(struct udevice *dev)
+{
+ struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
+ struct tee_shm *shm;
+
+ /*
+ * Any remaining shared memory must be unregistered now as U-Boot
+ * is about to hand over to the next stage and that memory will be
+ * reused.
+ */
+ while (!list_empty(&priv->list_shm)) {
+ shm = list_first_entry(&priv->list_shm, struct tee_shm, link);
+ debug("%s: freeing leftover shm %p (size %lu, flags %#x)\n",
+ __func__, (void *)shm, shm->size, shm->flags);
+ tee_shm_free(shm);
+ }
+
+ return 0;
+}
+
+UCLASS_DRIVER(tee) = {
+ .id = UCLASS_TEE,
+ .name = "tee",
+ .per_device_auto = sizeof(struct tee_uclass_priv),
+ .pre_probe = tee_pre_probe,
+ .pre_remove = tee_pre_remove,
+};
+
+void tee_optee_ta_uuid_from_octets(struct tee_optee_ta_uuid *d,
+ const u8 s[TEE_UUID_LEN])
+{
+ d->time_low = ((u32)s[0] << 24) | ((u32)s[1] << 16) |
+ ((u32)s[2] << 8) | s[3],
+ d->time_mid = ((u32)s[4] << 8) | s[5];
+ d->time_hi_and_version = ((u32)s[6] << 8) | s[7];
+ memcpy(d->clock_seq_and_node, s + 8, sizeof(d->clock_seq_and_node));
+}
+
+void tee_optee_ta_uuid_to_octets(u8 d[TEE_UUID_LEN],
+ const struct tee_optee_ta_uuid *s)
+{
+ d[0] = s->time_low >> 24;
+ d[1] = s->time_low >> 16;
+ d[2] = s->time_low >> 8;
+ d[3] = s->time_low;
+ d[4] = s->time_mid >> 8;
+ d[5] = s->time_mid;
+ d[6] = s->time_hi_and_version >> 8;
+ d[7] = s->time_hi_and_version;
+ memcpy(d + 8, s->clock_seq_and_node, sizeof(s->clock_seq_and_node));
+}