Skip to contentSkip to conversation

AVB using OP-TEE

PATCH Superseded Jens Wiklander 3 participants 38 messages Aug 23, 2018 – Jan 8, 2020

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover AVB using OP-TEE

Hi,

This adds support for storing AVB rollback indexes in the RPMB partition. The RPMB partition (content and key) is managed by OP-TEE
(https://www.op-tee.org/) which is a secure OS leveraging ARM TrustZone.

The Linux kernel can already support OP-TEE with reading and updating rollback indexes in the RPMB partition, the catch is that this is needed before the kernel has booted.

The design here is the same as what is in the Linux kernel, with the exception that the user space daemon tee-supplicant is integrated in the OP-TEE driver here (drivers/tee/optee/supplicant.c) instead. A new uclass (UCLASS_TEE) is introduced to provide an abstraction for interfacing with a Trusted Execution Environment (TEE). There's also the OP-TEE driver using UCLASS_TEE for registration.

A Trusted Application (TA) interface is added to be used by the AVB verify functions which are updated accordingly. The TA is managed by OP-TEE and is executed in a secure TrustZone protected environment.

The header files drivers/tee/optee/optee_{msg,msg_supplicant,smc}.h and include/tee/optee_ta_avb.h are copied from
https://github.com/OP-TEE/optee_os/tree/master more or less unmodified. They may need to be updated from time to time in order to support new features.

In MMC there's a new function, mmc_rpmb_route_frames(), which as the name suggests is used to route RPMB frames to/from the MMC. This saves OP-TEE from implementing an MMC driver which would need to share resources with its counterpart here in U-boot.

This was tested on a Hikey (Kirin 620) board.

I've added myself as maintainer of the TEE stuff.

v2:
* Added sandbox driver and a test in test/dm for the new TEE uclass:
Commit ("test: tee: test TEE uclass") and the enabling commits ("sandbox: dt: add sandbox_tee node") and
("configs: sandbox: enable CONFIG_TEE (TEE uclass)")
* Added descriptions of exported structs and functions
* Added documentation for the TEE uclass and the OP-TEE driver with the new commit ("Documentation: tee uclass and op-tee driver")
* Added documentation for the changes in avb_verify
* Addressed review comments from Simon Glass
* Added the commit ("cmd: avb: print error message if command fails")
* Made a few functions static in the OP-TEE driver
* Commit ("cmd: avb read_rb: print rb_idx in hexadecimal") and ("tee: optee: support AVB trusted application");

Thanks,
Jens

Jens Wiklander (15):
dm: fdt: scan for devices under /firmware too
cmd: avb read_rb: print rb_idx in hexadecimal
cmd: avb: print error message if command fails
mmc: rpmb: add mmc_rpmb_route_frames() Add UCLASS_TEE for Trusted Execution Environment dt/bindings: add bindings for optee
tee: add OP-TEE driver
Documentation: tee uclass and op-tee driver
test: tee: test TEE uclass
sandbox: dt: add sandbox_tee node
configs: sandbox: enable CONFIG_TEE (TEE uclass)
arm: dt: hikey: Add optee node
optee: support routing of rpmb data frames to mmc
tee: optee: support AVB trusted application
avb_verify: support using OP-TEE TA AVB

 MAINTAINERS                                   |   7 +
 arch/arm/dts/hi6220-hikey.dts                 |   7 +
 arch/sandbox/dts/sandbox.dts                  |   4 +
 arch/sandbox/dts/sandbox64.dts                |   4 +
 arch/sandbox/dts/test.dts                     |   4 +
 cmd/avb.c                                     |  19 +-
 common/avb_verify.c                           | 132 +++-
 configs/sandbox64_defconfig                   |   1 +
 configs/sandbox_defconfig                     |   1 +
 configs/sandbox_flattree_defconfig            |   1 +
 configs/sandbox_noblk_defconfig               |   1 +
 configs/sandbox_spl_defconfig                 |   1 +
 doc/README.avb2                               |  13 +
 doc/README.tee                                | 112 ++++
 .../firmware/linaro,optee-tz.txt              |  31 +
 drivers/Kconfig                               |   2 +
 drivers/Makefile                              |   1 +
 drivers/core/root.c                           |  15 +-
 drivers/mmc/rpmb.c                            | 160 +++++
 drivers/tee/Kconfig                           |  17 +
 drivers/tee/Makefile                          |   4 +
 drivers/tee/optee/Kconfig                     |  23 +
 drivers/tee/optee/Makefile                    |   5 +
 drivers/tee/optee/core.c                      | 622 ++++++++++++++++++
 drivers/tee/optee/optee_msg.h                 | 423 ++++++++++++
 drivers/tee/optee/optee_msg_supplicant.h      | 234 +++++++
 drivers/tee/optee/optee_private.h             |  41 ++
 drivers/tee/optee/optee_smc.h                 | 444 +++++++++++++
 drivers/tee/optee/rpmb.c                      | 184 ++++++
 drivers/tee/optee/supplicant.c                |  92 +++
 drivers/tee/tee-uclass.c                      | 192 ++++++
 include/avb_verify.h                          |   4 +
 include/dm/uclass-id.h                        |   1 +
 include/mmc.h                                 |   2 +
 include/tee.h                                 | 297 +++++++++
 include/tee/optee_ta_avb.h                    |  48 ++
 test/dm/Makefile                              |   1 +
 test/dm/tee.c                                 | 182 +++++
 38 files changed, 3325 insertions(+), 7 deletions(-)
 create mode 100644 doc/README.tee
 create mode 100644 doc/device-tree-bindings/firmware/linaro,optee-tz.txt
 create mode 100644 drivers/tee/Kconfig
 create mode 100644 drivers/tee/Makefile
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_msg.h
 create mode 100644 drivers/tee/optee/optee_msg_supplicant.h
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/rpmb.c
 create mode 100644 drivers/tee/optee/supplicant.c
 create mode 100644 drivers/tee/tee-uclass.c
 create mode 100644 include/tee.h
 create mode 100644 include/tee/optee_ta_avb.h
 create mode 100644 test/dm/tee.c
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/15 dm: fdt: scan for devices under /firmware too

Just as /chosen may contain devices /firmware may contain devices, scan for devices under /firmware too.

---
 drivers/core/root.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/core/root.c +10 -5
index 72bcc7d7f2a3..0dca507e1187 100644
@@ -265,9 +265,15 @@ static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
 	for (offset = fdt_first_subnode(blob, offset);
 	     offset > 0;
 	     offset = fdt_next_subnode(blob, offset)) {
-		/* "chosen" node isn't a device itself but may contain some: */
-		if (!strcmp(fdt_get_name(blob, offset, NULL), "chosen")) {
-			pr_debug("parsing subnodes of \"chosen\"\n");
+		const char *node_name = fdt_get_name(blob, offset, NULL);
+
+		/*
+		 * The "chosen" and "firmware" nodes aren't devices
+		 * themselves but may contain some:
+		 */
+		if (!strcmp(node_name, "chosen") ||
+		    !strcmp(node_name, "firmware")) {
+			pr_debug("parsing subnodes of \"%s\"\n", node_name);
 
 			err = dm_scan_fdt_node(parent, blob, offset,
 					       pre_reloc_only);
@@ -286,8 +292,7 @@ static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
 		err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL);
 		if (err && !ret) {
 			ret = err;
-			debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
-			      ret);
+			debug("%s: ret=%d\n", node_name, ret);
 		}
 	}
 
Signature · 1 line
2.17.1
#3 lore raw

PATCH v2 1/15 dm: fdt: scan for devices under /firmware too

In reply to Jens Wiklander

Hi Jens and Simon,

čt 23. 8. 2018 v 12:43 odesílatel Jens Wiklander
<[email protected]> napsal:

Quoted 43 lines

Just as /chosen may contain devices /firmware may contain devices, scan for devices under /firmware too.

---
 drivers/core/root.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/core/root.c +10 -6
index 72bcc7d7f2a3..0dca507e1187 100644
@@ -265,9 +265,15 @@ static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
        for (offset = fdt_first_subnode(blob, offset);
             offset > 0;
             offset = fdt_next_subnode(blob, offset)) {
-               /* "chosen" node isn't a device itself but may contain some: */
-               if (!strcmp(fdt_get_name(blob, offset, NULL), "chosen")) {
-                       pr_debug("parsing subnodes of \"chosen\"\n");
+               const char *node_name = fdt_get_name(blob, offset, NULL);
+
+               /*
+                * The "chosen" and "firmware" nodes aren't devices
+                * themselves but may contain some:
+                */
+               if (!strcmp(node_name, "chosen") ||
+                   !strcmp(node_name, "firmware")) {
+                       pr_debug("parsing subnodes of \"%s\"\n", node_name);
 
                        err = dm_scan_fdt_node(parent, blob, offset,
                                               pre_reloc_only);
@@ -286,8 +292,7 @@ static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
                err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL);
                if (err && !ret) {
                        ret = err;
-                       debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
-                             ret);
+                       debug("%s: ret=%d\n", node_name, ret);
                }
        }
 
--
Quoted 2 lines

2.17.1

I have debug issue which I see on ZynqMP that firmware nodes are listed in dm tree twice.
I was caused by this patch.

 firmware      1  [ + ]   zynqmp-firmware       |-- zynqmp-firmware
 firmware      2  [ + ]   zynqmp-power          |   `-- zynqmp-power
....
 firmware      3  [   ]   zynqmp-firmware       `-- zynqmp-firmware
 firmware      4  [   ]   zynqmp-power              `-- zynqmp-power

On ZynqMP firmware node needs to be populated early that's why u-boot,dm-pre-reloc flag is used.
That's why I am curious how to fix this. Revert this patch or add flags to Jens case or do more checking to avoid creating duplicated entries of nodes present in firmware node.

Thanks,
Michal

Signature · 5 lines
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Xilinx Microblaze
Maintainer of Linux kernel - Xilinx Zynq ARM and ZynqMP ARM64 SoCs
U-Boot custodian - Xilinx Microblaze/Zynq/ZynqMP/Versal SoCs
#4 lore raw

PATCH v2 1/15 dm: fdt: scan for devices under /firmware too

Hi Michal,

Quoted 61 lines · Michal Simek

Hi Jens and Simon,

čt 23. 8. 2018 v 12:43 odesílatel Jens Wiklander
<[email protected]> napsal:

Quoted 43 lines

Just as /chosen may contain devices /firmware may contain devices, scan for devices under /firmware too.

---
 drivers/core/root.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/core/root.c +10 -6
index 72bcc7d7f2a3..0dca507e1187 100644
@@ -265,9 +265,15 @@ static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
        for (offset = fdt_first_subnode(blob, offset);
             offset > 0;
             offset = fdt_next_subnode(blob, offset)) {
-               /* "chosen" node isn't a device itself but may contain some: */
-               if (!strcmp(fdt_get_name(blob, offset, NULL), "chosen")) {
-                       pr_debug("parsing subnodes of \"chosen\"\n");
+               const char *node_name = fdt_get_name(blob, offset, NULL);
+
+               /*
+                * The "chosen" and "firmware" nodes aren't devices
+                * themselves but may contain some:
+                */
+               if (!strcmp(node_name, "chosen") ||
+                   !strcmp(node_name, "firmware")) {
+                       pr_debug("parsing subnodes of \"%s\"\n", node_name);
 
                        err = dm_scan_fdt_node(parent, blob, offset,
                                               pre_reloc_only);
@@ -286,8 +292,7 @@ static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
                err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL);
                if (err && !ret) {
                        ret = err;
-                       debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
-                             ret);
+                       debug("%s: ret=%d\n", node_name, ret);
                }
        }
 
--
Quoted 2 lines

2.17.1

I have debug issue which I see on ZynqMP that firmware nodes are listed in dm tree twice.
I was caused by this patch.

 firmware      1  [ + ]   zynqmp-firmware       |-- zynqmp-firmware
 firmware      2  [ + ]   zynqmp-power          |   `-- zynqmp-power
....
 firmware      3  [   ]   zynqmp-firmware       `-- zynqmp-firmware
 firmware      4  [   ]   zynqmp-power              `-- zynqmp-power
Quoted 5 lines

On ZynqMP firmware node needs to be populated early that's why u-boot,dm-pre-reloc flag is used.
That's why I am curious how to fix this. Revert this patch or add flags to Jens case or do more checking to avoid creating duplicated entries of nodes present in firmware node.

Reverting may solve the ZynqMP problem, but it will cause new problems instead so I'd prefer fixing it properly.
I'm not sure if this is best fixed by more flags or by extra checking when creating the nodes.

Thanks,
Jens

Quoted 10 lines

Thanks,
Michal

--
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Xilinx Microblaze Maintainer of Linux kernel - Xilinx Zynq ARM and ZynqMP ARM64 SoCs U-Boot custodian - Xilinx Microblaze/Zynq/ZynqMP/Versal SoCs

#5 lore raw

PATCH v2 2/15 cmd: avb read_rb: print rb_idx in hexadecimal

In reply to Jens Wiklander

Prior to this patch was do_avb_write_rb() reading supplied rb_idx as a hexadecimal number while do_avb_read_rb() printed the read out rb_idx as decimal number. For consistency change do_avb_read_rb() to print rb_idx as a hexadecimal number too.

---
 cmd/avb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmd/avb.c +1 -1
index f045a0c64c4a..ca4b26d5d7b3 100644
@@ -158,7 +158,7 @@ int do_avb_read_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
 	if (avb_ops->read_rollback_index(avb_ops, index, &rb_idx) ==
 	    AVB_IO_RESULT_OK) {
-		printf("Rollback index: %llu\n", rb_idx);
+		printf("Rollback index: %llx\n", rb_idx);
 		return CMD_RET_SUCCESS;
 	}
 	return CMD_RET_FAILURE;
Signature · 1 line
2.17.1
#6 lore raw

PATCH v2 2/15 cmd: avb read_rb: print rb_idx in hexadecimal

Quoted 10 lines · Jens Wiklander

Prior to this patch was do_avb_write_rb() reading supplied rb_idx as a hexadecimal number while do_avb_read_rb() printed the read out rb_idx as decimal number. For consistency change do_avb_read_rb() to print rb_idx as a hexadecimal number too.

---
 cmd/avb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#7 lore raw

PATCH v2 3/15 cmd: avb: print error message if command fails

In reply to Jens Wiklander
---
 cmd/avb.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
cmd/avb.c +17 -0
index ca4b26d5d7b3..ff00be4cee38 100644
@@ -35,6 +35,8 @@ int do_avb_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	if (avb_ops)
 		return CMD_RET_SUCCESS;
 
+	printf("Failed to initialize avb2\n");
+
 	return CMD_RET_FAILURE;
 }
 
@@ -65,6 +67,8 @@ int do_avb_read_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		return CMD_RET_SUCCESS;
 	}
 
+	printf("Failed to read from partition\n");
+
 	return CMD_RET_FAILURE;
 }
 
@@ -108,6 +112,8 @@ int do_avb_read_part_hex(cmd_tbl_t *cmdtp, int flag, int argc,
 		return CMD_RET_SUCCESS;
 	}
 
+	printf("Failed to read from partition\n");
+
 	free(buffer);
 	return CMD_RET_FAILURE;
 }
@@ -138,6 +144,8 @@ int do_avb_write_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		return CMD_RET_SUCCESS;
 	}
 
+	printf("Failed to write in partition\n");
+
 	return CMD_RET_FAILURE;
 }
 
@@ -161,6 +169,9 @@ int do_avb_read_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		printf("Rollback index: %llx\n", rb_idx);
 		return CMD_RET_SUCCESS;
 	}
+
+	printf("Failed to read rollback index\n");
+
 	return CMD_RET_FAILURE;
 }
 
@@ -184,6 +195,8 @@ int do_avb_write_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	    AVB_IO_RESULT_OK)
 		return CMD_RET_SUCCESS;
 
+	printf("Failed to write rollback index\n");
+
 	return CMD_RET_FAILURE;
 }
 
@@ -210,6 +223,8 @@ int do_avb_get_uuid(cmd_tbl_t *cmdtp, int flag,
 		return CMD_RET_SUCCESS;
 	}
 
+	printf("Failed to read UUID\n");
+
 	return CMD_RET_FAILURE;
 }
 
@@ -320,6 +335,8 @@ int do_avb_is_unlocked(cmd_tbl_t *cmdtp, int flag,
 		return CMD_RET_SUCCESS;
 	}
 
+	printf("Can't determine device lock state.\n");
+
 	return CMD_RET_FAILURE;
 }
 
Signature · 1 line
2.17.1
#9 lore raw

PATCH v2 4/15 mmc: rpmb: add mmc_rpmb_route_frames()

In reply to Jens Wiklander

Adds mmc_rpmb_route_frames() to route RPMB data frames from/to an external entity.

---
 drivers/mmc/rpmb.c | 160 +++++++++++++++++++++++++++++++++++++++++++++
 include/mmc.h      |   2 +
 2 files changed, 162 insertions(+)
drivers/mmc/rpmb.c +160 -0
index dfbdb0deb107..908f19208955 100644
@@ -321,3 +321,163 @@ int mmc_rpmb_write(struct mmc *mmc, void *addr, unsigned short blk,
 	}
 	return i;
 }
+
+static int send_write_mult_block(struct mmc *mmc, const struct s_rpmb *frm,
+				 unsigned short cnt)
+{
+	struct mmc_cmd cmd = {
+		.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK,
+		.resp_type = MMC_RSP_R1b,
+	};
+	struct mmc_data data = {
+		.src = (const void *)frm,
+		.blocks = cnt,
+		.blocksize = sizeof(*frm),
+		.flags = MMC_DATA_WRITE,
+	};
+
+	return mmc_send_cmd(mmc, &cmd, &data);
+}
+
+static int send_read_mult_block(struct mmc *mmc, struct s_rpmb *frm,
+				unsigned short cnt)
+{
+	struct mmc_cmd cmd = {
+		.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK,
+		.resp_type = MMC_RSP_R1,
+	};
+	struct mmc_data data = {
+		.dest = (void *)frm,
+		.blocks = cnt,
+		.blocksize = sizeof(*frm),
+		.flags = MMC_DATA_READ,
+	};
+
+	return mmc_send_cmd(mmc, &cmd, &data);
+}
+
+static int rpmb_route_write_req(struct mmc *mmc, struct s_rpmb *req,
+				unsigned short req_cnt, struct s_rpmb *rsp,
+				unsigned short rsp_cnt)
+{
+	int ret;
+
+	/*
+	 * Send the write request.
+	 */
+	ret = mmc_set_blockcount(mmc, req_cnt, true);
+	if (ret)
+		return ret;
+
+	ret = send_write_mult_block(mmc, req, req_cnt);
+	if (ret)
+		return ret;
+
+	/*
+	 * Read the result of the request.
+	 */
+	ret = mmc_set_blockcount(mmc, 1, false);
+	if (ret)
+		return ret;
+
+	memset(rsp, 0, sizeof(*rsp));
+	rsp->request = cpu_to_be16(RPMB_REQ_STATUS);
+	ret = send_write_mult_block(mmc, rsp, 1);
+	if (ret)
+		return ret;
+
+	ret = mmc_set_blockcount(mmc, 1, false);
+	if (ret)
+		return ret;
+
+	return send_read_mult_block(mmc, rsp, 1);
+}
+
+static int rpmb_route_read_req(struct mmc *mmc, struct s_rpmb *req,
+			       unsigned short req_cnt, struct s_rpmb *rsp,
+			       unsigned short rsp_cnt)
+{
+	int ret;
+
+	/*
+	 * Send the read request.
+	 */
+	ret = mmc_set_blockcount(mmc, 1, false);
+	if (ret)
+		return ret;
+
+	ret = send_write_mult_block(mmc, req, 1);
+	if (ret)
+		return ret;
+
+	/*
+	 * Read the result of the request.
+	 */
+
+	ret = mmc_set_blockcount(mmc, rsp_cnt, false);
+	if (ret)
+		return ret;
+
+	return send_read_mult_block(mmc, rsp, rsp_cnt);
+}
+
+static int rpmb_route_frames(struct mmc *mmc, struct s_rpmb *req,
+			     unsigned short req_cnt, struct s_rpmb *rsp,
+			     unsigned short rsp_cnt)
+{
+	unsigned short n;
+
+	/*
+	 * If multiple request frames are provided, make sure that all are
+	 * of the same type.
+	 */
+	for (n = 1; n < req_cnt; n++)
+		if (req[n].request != req->request)
+			return -EINVAL;
+
+	switch (be16_to_cpu(req->request)) {
+	case RPMB_REQ_KEY:
+		if (req_cnt != 1 || rsp_cnt != 1)
+			return -EINVAL;
+		return rpmb_route_write_req(mmc, req, req_cnt, rsp, rsp_cnt);
+
+	case RPMB_REQ_WRITE_DATA:
+		if (!req_cnt || rsp_cnt != 1)
+			return -EINVAL;
+		return rpmb_route_write_req(mmc, req, req_cnt, rsp, rsp_cnt);
+
+	case RPMB_REQ_WCOUNTER:
+		if (req_cnt != 1 || rsp_cnt != 1)
+			return -EINVAL;
+		return rpmb_route_read_req(mmc, req, req_cnt, rsp, rsp_cnt);
+
+	case RPMB_REQ_READ_DATA:
+		if (req_cnt != 1 || !req_cnt)
+			return -EINVAL;
+		return rpmb_route_read_req(mmc, req, req_cnt, rsp, rsp_cnt);
+
+	default:
+		debug("Unsupported message type: %d\n",
+		      be16_to_cpu(req->request));
+		return -EINVAL;
+	}
+}
+
+int mmc_rpmb_route_frames(struct mmc *mmc, void *req, unsigned long reqlen,
+			  void *rsp, unsigned long rsplen)
+{
+	/*
+	 * Whoever crafted the data supplied to this function knows how to
+	 * format the PRMB frames and which response is expected. If
+	 * there's some unexpected mismatch it's more helpful to report an
+	 * error immediately than trying to guess what was the intention
+	 * and possibly just delay an eventual error which will be harder
+	 * to track down.
+	 */
+
+	if (reqlen % sizeof(struct s_rpmb) || rsplen % sizeof(struct s_rpmb))
+		return -EINVAL;
+
+	return rpmb_route_frames(mmc, req, reqlen / sizeof(struct s_rpmb),
+				 rsp, rsplen / sizeof(struct s_rpmb));
+}
include/mmc.h +2 -0
index df4255b828a7..d6e02af4edea 100644
@@ -748,6 +748,8 @@ int mmc_rpmb_read(struct mmc *mmc, void *addr, unsigned short blk,
 		  unsigned short cnt, unsigned char *key);
 int mmc_rpmb_write(struct mmc *mmc, void *addr, unsigned short blk,
 		   unsigned short cnt, unsigned char *key);
+int mmc_rpmb_route_frames(struct mmc *mmc, void *req, unsigned long reqlen,
+			  void *rsp, unsigned long rsplen);
 #ifdef CONFIG_CMD_BKOPS_ENABLE
 int mmc_set_bkops_enable(struct mmc *mmc);
 #endif
Signature · 1 line
2.17.1
#10 lore raw

PATCH v2 5/15 Add UCLASS_TEE for Trusted Execution Environment

In reply to Jens Wiklander

Adds a uclass to interface with a TEE (Trusted Execution Environment).

A TEE driver is a driver that interfaces with a trusted OS running in some secure environment, for example, TrustZone on ARM cpus, or a separate secure co-processor etc.

The TEE subsystem can serve a TEE driver for a Global Platform compliant TEE, but it's not limited to only Global Platform TEEs.

The over all design is based on the TEE subsystem in the Linux kernel, tailored for U-boot.

---
 MAINTAINERS              |   6 +
 drivers/Kconfig          |   2 +
 drivers/Makefile         |   1 +
 drivers/tee/Kconfig      |   8 ++
 drivers/tee/Makefile     |   3 +
 drivers/tee/tee-uclass.c | 192 ++++++++++++++++++++++++++
 include/dm/uclass-id.h   |   1 +
 include/tee.h            | 290 +++++++++++++++++++++++++++++++++++++++
 8 files changed, 503 insertions(+)
 create mode 100644 drivers/tee/Kconfig
 create mode 100644 drivers/tee/Makefile
 create mode 100644 drivers/tee/tee-uclass.c
 create mode 100644 include/tee.h
MAINTAINERS +6 -0
index 58b61ac05882..7458c606ee92 100644
@@ -571,6 +571,12 @@ TQ GROUP
 S:	Orphaned (Since 2016-02)
 T:	git git://git.denx.de/u-boot-tq-group.git
 
+TEE
+M:	Jens Wiklander <[email protected]>
+S:	Maintained
+F:	drivers/tee/
+F:	include/tee.h
+
 UBI
 M:	Kyungmin Park <[email protected]>
 M:	Heiko Schocher <[email protected]>
drivers/Kconfig +2 -0
index c72abf893297..f3249ab1d143 100644
@@ -94,6 +94,8 @@ source "drivers/spmi/Kconfig"
 
 source "drivers/sysreset/Kconfig"
 
+source "drivers/tee/Kconfig"
+
 source "drivers/thermal/Kconfig"
 
 source "drivers/timer/Kconfig"
drivers/Makefile +1 -0
index d53208540ea6..0fcae36f50f7 100644
@@ -103,6 +103,7 @@ obj-y += smem/
 obj-y += soc/
 obj-$(CONFIG_REMOTEPROC) += remoteproc/
 obj-y += thermal/
+obj-$(CONFIG_TEE) += tee/
 
 obj-$(CONFIG_MACH_PIC32) += ddr/microchip/
 endif
drivers/tee/Kconfig new file +8 -0
new file mode 100644
index 000000000000..817ab331b0f8
@@ -0,0 +1,8 @@
+# Generic Trusted Execution Environment Configuration
+config TEE
+	bool "Trusted Execution Environment support"
+	depends on ARM && (ARM64 || CPU_V7A)
+	select ARM_SMCCC
+	help
+	  This implements a generic interface towards a Trusted Execution
+	  Environment (TEE).
drivers/tee/Makefile new file +3 -0
new file mode 100644
index 000000000000..b6d8e16e6211
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-y += tee-uclass.o
drivers/tee/tee-uclass.c new file +192 -0

192 lines not on this page · raw message

include/dm/uclass-id.h +1 -0
index a39643ec5eef..955e0a915b87 100644
@@ -81,6 +81,7 @@ enum uclass_id {
 	UCLASS_SPI_GENERIC,	/* Generic SPI flash target */
 	UCLASS_SYSCON,		/* System configuration device */
 	UCLASS_SYSRESET,	/* System reset device */
+	UCLASS_TEE,		/* Trusted Execution Environment device */
 	UCLASS_THERMAL,		/* Thermal sensor */
 	UCLASS_TIMER,		/* Timer device */
 	UCLASS_TPM,		/* Trusted Platform Module TIS interface */
include/tee.h new file +290 -0

290 lines not on this page · raw message

Signature · 1 line
2.17.1
#11 lore raw

PATCH v2 5/15 Add UCLASS_TEE for Trusted Execution Environment

Hi Jens,

Quoted 11 lines · Jens Wiklander

Adds a uclass to interface with a TEE (Trusted Execution Environment).

A TEE driver is a driver that interfaces with a trusted OS running in some secure environment, for example, TrustZone on ARM cpus, or a separate secure co-processor etc.

The TEE subsystem can serve a TEE driver for a Global Platform compliant TEE, but it's not limited to only Global Platform TEEs.

The over all design is based on the TEE subsystem in the Linux kernel, tailored for U-boot.

U-Boot

Quoted 17 lines
---
 MAINTAINERS              |   6 +
 drivers/Kconfig          |   2 +
 drivers/Makefile         |   1 +
 drivers/tee/Kconfig      |   8 ++
 drivers/tee/Makefile     |   3 +
 drivers/tee/tee-uclass.c | 192 ++++++++++++++++++++++++++
 include/dm/uclass-id.h   |   1 +
 include/tee.h            | 290 +++++++++++++++++++++++++++++++++++++++
 8 files changed, 503 insertions(+)
 create mode 100644 drivers/tee/Kconfig
 create mode 100644 drivers/tee/Makefile
 create mode 100644 drivers/tee/tee-uclass.c
 create mode 100644 include/tee.h

Various nits below.

Quoted 49 lines
MAINTAINERS +6 -0
index 58b61ac05882..7458c606ee92 100644
@@ -571,6 +571,12 @@ TQ GROUP
 S:     Orphaned (Since 2016-02)
 T:     git git://git.denx.de/u-boot-tq-group.git
 
+TEE
+M:     Jens Wiklander <[email protected]>
+S:     Maintained
+F:     drivers/tee/
+F:     include/tee.h
+
 UBI
 M:     Kyungmin Park <[email protected]>
 M:     Heiko Schocher <[email protected]>
drivers/Kconfig +2 -0
index c72abf893297..f3249ab1d143 100644
@@ -94,6 +94,8 @@ source "drivers/spmi/Kconfig"
 
 source "drivers/sysreset/Kconfig"
 
+source "drivers/tee/Kconfig"
+
 source "drivers/thermal/Kconfig"
 
 source "drivers/timer/Kconfig"
drivers/Makefile +1 -0
index d53208540ea6..0fcae36f50f7 100644
@@ -103,6 +103,7 @@ obj-y += smem/
 obj-y += soc/
 obj-$(CONFIG_REMOTEPROC) += remoteproc/
 obj-y += thermal/
+obj-$(CONFIG_TEE) += tee/
 
 obj-$(CONFIG_MACH_PIC32) += ddr/microchip/
 endif
drivers/tee/Kconfig new file +0 -0
new file mode 100644
index 000000000000..817ab331b0f8

drivers/tee/Kconfig line 8

Quoted 8 lines
drivers/tee/Kconfig new file +8 -0
@@ -0,0 +1,8 @@
+# Generic Trusted Execution Environment Configuration
+config TEE
+       bool "Trusted Execution Environment support"
+       depends on ARM && (ARM64 || CPU_V7A)
+       select ARM_SMCCC
+       help
+         This implements a generic interface towards a Trusted Execution
+         Environment (TEE).

Please expand this. What is it? Why would I use it? Also perhaps add a web link?

Quoted 98 lines
drivers/tee/Makefile new file +3 -0
new file mode 100644
index 000000000000..b6d8e16e6211
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-y += tee-uclass.o
drivers/tee/tee-uclass.c new file +83 -0
new file mode 100644
index 000000000000..0209983491a0
@@ -0,0 +1,192 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <tee.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,
+                    ulong 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,
+                   ulong num_param, struct tee_param *param)
+{
+       return tee_get_ops(dev)->invoke_func(dev, arg, num_param, param);
+}
+
+struct tee_shm *__tee_shm_add(struct udevice *dev, ulong align, void *addr,
+                             ulong size, u32 flags)
+{
+       struct tee_shm *shm;
+       void *p = addr;
+
+       if (flags & TEE_SHM_ALLOC) {
+               if (align)
+                       p = memalign(align, size);
+               else
+                       p = malloc(size);
+       }
+       if (!p)
+               return NULL;
+
+       shm = calloc(1, sizeof(*shm));
+       if (!shm)
+               goto err;
+
+       shm->dev = dev;
+       shm->addr = p;
+       shm->size = size;
+       shm->flags = flags;
+
+       if ((flags & TEE_SHM_SEC_REGISTER) &&
+           tee_get_ops(dev)->shm_register(dev, shm))
+               goto err;
+
+       if (flags & TEE_SHM_REGISTER) {
+               struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
+
+               list_add(&shm->link, &priv->list_shm);
+       }
+       return shm;
+err:
+       free(shm);
+       if (flags & TEE_SHM_ALLOC)

drivers/tee/tee-uclass.c line 93

Quoted 10 lines
drivers/tee/tee-uclass.c new file +10 -0
+               free(p);
+       return NULL;
+}
+
+struct tee_shm *tee_shm_alloc(struct udevice *dev, ulong size,
+                             u32 flags)
+{
+       u32 f = flags;
+
+       f |= TEE_SHM_SEC_REGISTER | TEE_SHM_REGISTER | TEE_SHM_ALLOC;

blank line here

drivers/tee/tee-uclass.c line 102

Quoted 9 lines
drivers/tee/tee-uclass.c new file +9 -0
+       return __tee_shm_add(dev, 0, NULL, size, f);
+}
+
+struct tee_shm *tee_shm_register(struct udevice *dev, void *addr,
+                                ulong size, u32 flags)
+{
+       u32 f = flags & ~TEE_SHM_ALLOC;
+
+       f |= TEE_SHM_SEC_REGISTER | TEE_SHM_REGISTER;

and here

Quoted 27 lines
drivers/tee/tee-uclass.c new file +27 -0
+       return __tee_shm_add(dev, 0, addr, size, f);
+}
+
+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)

drivers/tee/tee-uclass.c line 139

Quoted 10 lines
drivers/tee/tee-uclass.c new file +10 -0
+                       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)

Please add function comment

drivers/tee/tee-uclass.c line 146

Quoted 7 lines
drivers/tee/tee-uclass.c new file +7 -0
+{
+       struct udevice *dev = start;
+       struct tee_version_data lv;
+       struct tee_version_data *v = vers ? vers : &lv;
+
+       if (!dev)
+               uclass_first_device(UCLASS_TEE, &dev);

This can return an error if it finds a device but it tails to probe. If you don't care about that, I suppose it is OK.

Quoted 16 lines
drivers/tee/tee-uclass.c new file +16 -0
+
+       for (; dev; uclass_next_device(&dev)) {
+               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);
+

drivers/tee/tee-uclass.c line 172

Quoted 10 lines
drivers/tee/tee-uclass.c new file +10 -0
+       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

U-Boot

Quoted 38 lines
drivers/tee/tee-uclass.c new file +20 -0
+        * 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_alloc_size = sizeof(struct tee_uclass_priv),
+       .pre_probe = tee_pre_probe,
+       .pre_remove = tee_pre_remove,
+};
include/dm/uclass-id.h +1 -0
index a39643ec5eef..955e0a915b87 100644
@@ -81,6 +81,7 @@ enum uclass_id {
        UCLASS_SPI_GENERIC,     /* Generic SPI flash target */
        UCLASS_SYSCON,          /* System configuration device */
        UCLASS_SYSRESET,        /* System reset device */
+       UCLASS_TEE,             /* Trusted Execution Environment device */
        UCLASS_THERMAL,         /* Thermal sensor */
        UCLASS_TIMER,           /* Timer device */
        UCLASS_TPM,             /* Trusted Platform Module TIS interface */
include/tee.h new file +0 -0
new file mode 100644
index 000000000000..3e6771123ef0

include/tee.h line 10

Quoted 10 lines
include/tee.h new file +10 -0
@@ -0,0 +1,290 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
+#ifndef __TEE_H
+#define __TEE_H
+
+#include <common.h>
+#include <dm.h>

Please drop both of those.

Quoted 48 lines
include/tee.h new file +48 -0
+
+#define TEE_UUID_LEN           16
+
+#define TEE_GEN_CAP_GP          BIT(0) /* GlobalPlatform compliant TEE */
+#define TEE_GEN_CAP_REG_MEM     BIT(1) /* Supports registering shared memory */
+
+#define TEE_SHM_REGISTER       BIT(0)  /* In list of shared memory */
+#define TEE_SHM_SEC_REGISTER   BIT(1)  /* TEE notified of this memory */
+#define TEE_SHM_ALLOC          BIT(2)  /* The memory is malloced() and must */
+                                       /* be freed() */
+
+#define TEE_PARAM_ATTR_TYPE_NONE               0       /* parameter not used */
+#define TEE_PARAM_ATTR_TYPE_VALUE_INPUT                1
+#define TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT       2
+#define TEE_PARAM_ATTR_TYPE_VALUE_INOUT                3       /* input and output */
+#define TEE_PARAM_ATTR_TYPE_MEMREF_INPUT       5
+#define TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT      6
+#define TEE_PARAM_ATTR_TYPE_MEMREF_INOUT       7       /* input and output */
+#define TEE_PARAM_ATTR_TYPE_MASK               0xff
+#define TEE_PARAM_ATTR_META                    0x100
+#define TEE_PARAM_ATTR_MASK                    (TEE_PARAM_ATTR_TYPE_MASK | \
+                                                TEE_PARAM_ATTR_META)
+
+/*
+ * Some Global Platform error codes which has a meaning if the
+ * TEE_GEN_CAP_GP bit is returned by the driver in
+ * struct tee_version_data::gen_caps
+ */
+#define TEE_SUCCESS                    0x00000000
+#define TEE_ERROR_GENERIC              0xffff0000
+#define TEE_ERROR_BAD_PARAMETERS       0xffff0006
+#define TEE_ERROR_ITEM_NOT_FOUND       0xffff0008
+#define TEE_ERROR_NOT_IMPLEMENTED      0xffff0009
+#define TEE_ERROR_NOT_SUPPORTED                0xffff000a
+#define TEE_ERROR_COMMUNICATION                0xffff000e
+#define TEE_ERROR_OUT_OF_MEMORY                0xffff000c
+#define TEE_ERROR_TARGET_DEAD          0xffff3024
+
+#define TEE_ORIGIN_COMMS               0x00000002
+
+/**
+ * struct tee_shm - memory shared with the TEE
+ * @dev:       The TEE device
+ * @link:      List node in the list in struct struct tee_uclass_priv
+ * @addr:      Pointer to the shared memory
+ * @size:      Size of the the shared memory
+ * @flags:     TEE_SHM_* above
+ */

include/tee.h line 68

Quoted 10 lines
include/tee.h new file +10 -0
+struct tee_shm {
+       struct udevice *dev;
+       struct list_head link;
+       void *addr;
+       ulong size;
+       u32 flags;
+};
+
+/**
+ * struct tee_param_memref - parameter memory reference

Used for ...?

include/tee.h line 80

Quoted 12 lines
include/tee.h new file +12 -0
+ * @shm_offs:  Offset in bytes into the shared memory object @shm
+ * @size:      Size in bytes of the memory reference
+ * @shm:       Pointer to a shared memory object for the buffer
+ */
+struct tee_param_memref {
+       ulong shm_offs;
+       ulong size;
+       struct tee_shm *shm;
+};
+
+/**
+ * struct tee_param_value - value parameter

??

This doesn't really explain anything. What is it for?

Quoted 30 lines
include/tee.h new file +30 -0
+ * @a, @b, @c: Parameters passed by value
+ */
+struct tee_param_value {
+       u64 a;
+       u64 b;
+       u64 c;
+};
+
+/**
+ * struct tee_param - invoke parameter
+ * @attr:      Attributes
+ * @u.memref:  Memref parameter if (@attr & TEE_PARAM_ATTR_MASK) is one of
+ *             TEE_PARAM_ATTR_TYPE_MEMREF_* above
+ * @u.value:   Value parameter if (@attr & TEE_PARAM_ATTR_MASK) is one of
+ *             TEE_PARAM_ATTR_TYPE_VALUE_* above
+ */
+struct tee_param {
+       u64 attr;
+       union {
+               struct tee_param_memref memref;
+               struct tee_param_value value;
+       } u;
+};
+
+/**
+ * struct tee_open_session_arg - extra arguments for tee_open_session()
+ * @uuid:      [in] UUID of the Trusted Application
+ * @clnt_uuid: [in] Normally zeroes
+ * @clnt_login:        [in] Normally 0
+ * @session:   [out] Session id

include/tee.h line 120

Quoted 10 lines
include/tee.h new file +10 -0
+ * @ret:       [out] return value
+ * @ret_origin:        [out] origin of the return value
+ */
+struct tee_open_session_arg {
+       u8 uuid[TEE_UUID_LEN];
+       u8 clnt_uuid[TEE_UUID_LEN];
+       u32 clnt_login;
+       u32 session;
+       u32 ret;
+       u32 ret_origin;

Do these use the sized u32 type because it is an API of some sort? Otherwise, why not uint?

Quoted 24 lines
include/tee.h new file +24 -0
+};
+
+/**
+ * struct tee_invoke_arg - extra arguments for tee_invoke_func()
+ * @func:      [in] Trusted Application function, specific to the TA
+ * @session:   [in] Session id, from open session
+ * @ret:       [out] return value
+ * @ret_origin:        [out] origin of the return value
+ */
+struct tee_invoke_arg {
+       u32 func;
+       u32 session;
+       u32 ret;
+       u32 ret_origin;
+};
+
+/**
+ * struct tee_version_data - description of TEE
+ * @gen_caps:  Generic capabilities, TEE_GEN_CAP_* above
+ */
+struct tee_version_data {
+       u32 gen_caps;
+};
+

include/tee.h line 154

Quoted 10 lines
include/tee.h new file +10 -0
+/**
+ * struct tee_driver_ops - TEE driver operations
+ * @get_version:       Query capabilities of TEE device,
+                       see tee_get_version()
+ * @open_session:      Opens a session to a Trusted Application in the TEE,
+ *                     see tee_open_session().
+ * @close_session:     Closes a session to Trusted Application,
+ *                     see tee_close_session()
+ * @invoke_func:       Invokes a function in a Trusted Application,
+                       see tee_invoke_func()

These comments should be against each function in the struct below. You should repeat the comment from the exported functions there too.

Quoted 15 lines
include/tee.h new file +15 -0
+ */
+struct tee_driver_ops {
+       void (*get_version)(struct udevice *dev, struct tee_version_data *vers);
+       int (*open_session)(struct udevice *dev,
+                           struct tee_open_session_arg *arg, ulong num_param,
+                           struct tee_param *param);
+       int (*close_session)(struct udevice *dev, u32 session);
+       int (*invoke_func)(struct udevice *dev, struct tee_invoke_arg *arg,
+                          ulong num_param, struct tee_param *param);
+       /**
+        * shm_register() - Registers memory shared with the TEE
+        * @dev:        The TEE device
+        * @shm:        Pointer to a shared memory object
+        * Returns 0 on success or < 0 on failure.
+        */

include/tee.h line 179

Quoted 10 lines
include/tee.h new file +10 -0
+       int (*shm_register)(struct udevice *dev, struct tee_shm *shm);
+       /**
+        * shm_unregister() - Unregisters memory shared with the TEE
+        * @dev:        The TEE device
+        * @shm:        Pointer to a shared memory object
+        * Returns 0 on success or < 0 on failure.
+        */
+       int (*shm_unregister)(struct udevice *dev, struct tee_shm *shm);
+};
+

[...]

Regards,
Simon

#12 lore raw

PATCH v2 5/15 Add UCLASS_TEE for Trusted Execution Environment

Quoted 91 lines · Simon Glass

Hi Jens,

Quoted 11 lines · Jens Wiklander

Adds a uclass to interface with a TEE (Trusted Execution Environment).

A TEE driver is a driver that interfaces with a trusted OS running in some secure environment, for example, TrustZone on ARM cpus, or a separate secure co-processor etc.

The TEE subsystem can serve a TEE driver for a Global Platform compliant TEE, but it's not limited to only Global Platform TEEs.

The over all design is based on the TEE subsystem in the Linux kernel, tailored for U-boot.

U-Boot

Quoted 17 lines
---
 MAINTAINERS              |   6 +
 drivers/Kconfig          |   2 +
 drivers/Makefile         |   1 +
 drivers/tee/Kconfig      |   8 ++
 drivers/tee/Makefile     |   3 +
 drivers/tee/tee-uclass.c | 192 ++++++++++++++++++++++++++
 include/dm/uclass-id.h   |   1 +
 include/tee.h            | 290 +++++++++++++++++++++++++++++++++++++++
 8 files changed, 503 insertions(+)
 create mode 100644 drivers/tee/Kconfig
 create mode 100644 drivers/tee/Makefile
 create mode 100644 drivers/tee/tee-uclass.c
 create mode 100644 include/tee.h

Various nits below.

Quoted 52 lines
MAINTAINERS +6 -0
index 58b61ac05882..7458c606ee92 100644
@@ -571,6 +571,12 @@ TQ GROUP
 S:     Orphaned (Since 2016-02)
 T:     git git://git.denx.de/u-boot-tq-group.git
 
+TEE
+M:     Jens Wiklander <[email protected]>
+S:     Maintained
+F:     drivers/tee/
+F:     include/tee.h
+
 UBI
 M:     Kyungmin Park <[email protected]>
 M:     Heiko Schocher <[email protected]>
drivers/Kconfig +2 -0
index c72abf893297..f3249ab1d143 100644
@@ -94,6 +94,8 @@ source "drivers/spmi/Kconfig"
 
 source "drivers/sysreset/Kconfig"
 
+source "drivers/tee/Kconfig"
+
 source "drivers/thermal/Kconfig"
 
 source "drivers/timer/Kconfig"
drivers/Makefile +1 -0
index d53208540ea6..0fcae36f50f7 100644
@@ -103,6 +103,7 @@ obj-y += smem/
 obj-y += soc/
 obj-$(CONFIG_REMOTEPROC) += remoteproc/
 obj-y += thermal/
+obj-$(CONFIG_TEE) += tee/
 
 obj-$(CONFIG_MACH_PIC32) += ddr/microchip/
 endif
drivers/tee/Kconfig new file +3 -0
new file mode 100644
index 000000000000..817ab331b0f8
@@ -0,0 +1,8 @@
+# Generic Trusted Execution Environment Configuration
+config TEE
+       bool "Trusted Execution Environment support"
Quoted 8 lines

drivers/tee/Kconfig line 8

Quoted 5 lines
drivers/tee/Kconfig new file +5 -0
+       depends on ARM && (ARM64 || CPU_V7A)
+       select ARM_SMCCC
+       help
+         This implements a generic interface towards a Trusted Execution
+         Environment (TEE).

Please expand this. What is it? Why would I use it? Also perhaps add a web link?

I'll expand it. I'll skip the why part though.

Quoted 155 lines
Quoted 99 lines
drivers/tee/Makefile new file +3 -0
new file mode 100644
index 000000000000..b6d8e16e6211
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-y += tee-uclass.o
drivers/tee/tee-uclass.c new file +83 -0
new file mode 100644
index 000000000000..0209983491a0
@@ -0,0 +1,192 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <tee.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,
+                    ulong 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,
+                   ulong num_param, struct tee_param *param)
+{
+       return tee_get_ops(dev)->invoke_func(dev, arg, num_param, param);
+}
+
+struct tee_shm *__tee_shm_add(struct udevice *dev, ulong align, void *addr,
+                             ulong size, u32 flags)
+{
+       struct tee_shm *shm;
+       void *p = addr;
+
+       if (flags & TEE_SHM_ALLOC) {
+               if (align)
+                       p = memalign(align, size);
+               else
+                       p = malloc(size);
+       }
+       if (!p)
+               return NULL;
+
+       shm = calloc(1, sizeof(*shm));
+       if (!shm)
+               goto err;
+
+       shm->dev = dev;
+       shm->addr = p;
+       shm->size = size;
+       shm->flags = flags;
+
+       if ((flags & TEE_SHM_SEC_REGISTER) &&
+           tee_get_ops(dev)->shm_register(dev, shm))
+               goto err;
+
+       if (flags & TEE_SHM_REGISTER) {
+               struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
+
+               list_add(&shm->link, &priv->list_shm);
+       }
+       return shm;
+err:
+       free(shm);
+       if (flags & TEE_SHM_ALLOC)

drivers/tee/tee-uclass.c line 93

Quoted 10 lines
drivers/tee/tee-uclass.c new file +10 -0
+               free(p);
+       return NULL;
+}
+
+struct tee_shm *tee_shm_alloc(struct udevice *dev, ulong size,
+                             u32 flags)
+{
+       u32 f = flags;
+
+       f |= TEE_SHM_SEC_REGISTER | TEE_SHM_REGISTER | TEE_SHM_ALLOC;

blank line here

drivers/tee/tee-uclass.c line 102

Quoted 9 lines
drivers/tee/tee-uclass.c new file +9 -0
+       return __tee_shm_add(dev, 0, NULL, size, f);
+}
+
+struct tee_shm *tee_shm_register(struct udevice *dev, void *addr,
+                                ulong size, u32 flags)
+{
+       u32 f = flags & ~TEE_SHM_ALLOC;
+
+       f |= TEE_SHM_SEC_REGISTER | TEE_SHM_REGISTER;

and here

Quoted 31 lines
drivers/tee/tee-uclass.c new file +31 -0
+       return __tee_shm_add(dev, 0, addr, size, f);
+}
+
+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;
+}
Quoted 8 lines

drivers/tee/tee-uclass.c line 139

Quoted 6 lines
drivers/tee/tee-uclass.c new file +6 -0
+
+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)

Please add function comment

There is one in include/tee.h

Quoted 10 lines

drivers/tee/tee-uclass.c line 146

Quoted 7 lines
drivers/tee/tee-uclass.c new file +7 -0
+{
+       struct udevice *dev = start;
+       struct tee_version_data lv;
+       struct tee_version_data *v = vers ? vers : &lv;
+
+       if (!dev)
+               uclass_first_device(UCLASS_TEE, &dev);

This can return an error if it finds a device but it tails to probe. If you don't care about that, I suppose it is OK.

Thanks. I need the first device that probes OK and then the next devices that probes OK and so on. I'll need to make some small changes here.

Quoted 156 lines
Quoted 16 lines
drivers/tee/tee-uclass.c new file +16 -0
+
+       for (; dev; uclass_next_device(&dev)) {
+               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);
+

drivers/tee/tee-uclass.c line 172

Quoted 10 lines
drivers/tee/tee-uclass.c new file +10 -0
+       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

U-Boot

Quoted 38 lines
drivers/tee/tee-uclass.c new file +20 -0
+        * 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_alloc_size = sizeof(struct tee_uclass_priv),
+       .pre_probe = tee_pre_probe,
+       .pre_remove = tee_pre_remove,
+};
include/dm/uclass-id.h +1 -0
index a39643ec5eef..955e0a915b87 100644
@@ -81,6 +81,7 @@ enum uclass_id {
        UCLASS_SPI_GENERIC,     /* Generic SPI flash target */
        UCLASS_SYSCON,          /* System configuration device */
        UCLASS_SYSRESET,        /* System reset device */
+       UCLASS_TEE,             /* Trusted Execution Environment device */
        UCLASS_THERMAL,         /* Thermal sensor */
        UCLASS_TIMER,           /* Timer device */
        UCLASS_TPM,             /* Trusted Platform Module TIS interface */
include/tee.h new file +0 -0
new file mode 100644
index 000000000000..3e6771123ef0

include/tee.h line 10

Quoted 10 lines
include/tee.h new file +10 -0
@@ -0,0 +1,290 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
+#ifndef __TEE_H
+#define __TEE_H
+
+#include <common.h>
+#include <dm.h>

Please drop both of those.

Quoted 48 lines
include/tee.h new file +48 -0
+
+#define TEE_UUID_LEN           16
+
+#define TEE_GEN_CAP_GP          BIT(0) /* GlobalPlatform compliant TEE */
+#define TEE_GEN_CAP_REG_MEM     BIT(1) /* Supports registering shared memory */
+
+#define TEE_SHM_REGISTER       BIT(0)  /* In list of shared memory */
+#define TEE_SHM_SEC_REGISTER   BIT(1)  /* TEE notified of this memory */
+#define TEE_SHM_ALLOC          BIT(2)  /* The memory is malloced() and must */
+                                       /* be freed() */
+
+#define TEE_PARAM_ATTR_TYPE_NONE               0       /* parameter not used */
+#define TEE_PARAM_ATTR_TYPE_VALUE_INPUT                1
+#define TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT       2
+#define TEE_PARAM_ATTR_TYPE_VALUE_INOUT                3       /* input and output */
+#define TEE_PARAM_ATTR_TYPE_MEMREF_INPUT       5
+#define TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT      6
+#define TEE_PARAM_ATTR_TYPE_MEMREF_INOUT       7       /* input and output */
+#define TEE_PARAM_ATTR_TYPE_MASK               0xff
+#define TEE_PARAM_ATTR_META                    0x100
+#define TEE_PARAM_ATTR_MASK                    (TEE_PARAM_ATTR_TYPE_MASK | \
+                                                TEE_PARAM_ATTR_META)
+
+/*
+ * Some Global Platform error codes which has a meaning if the
+ * TEE_GEN_CAP_GP bit is returned by the driver in
+ * struct tee_version_data::gen_caps
+ */
+#define TEE_SUCCESS                    0x00000000
+#define TEE_ERROR_GENERIC              0xffff0000
+#define TEE_ERROR_BAD_PARAMETERS       0xffff0006
+#define TEE_ERROR_ITEM_NOT_FOUND       0xffff0008
+#define TEE_ERROR_NOT_IMPLEMENTED      0xffff0009
+#define TEE_ERROR_NOT_SUPPORTED                0xffff000a
+#define TEE_ERROR_COMMUNICATION                0xffff000e
+#define TEE_ERROR_OUT_OF_MEMORY                0xffff000c
+#define TEE_ERROR_TARGET_DEAD          0xffff3024
+
+#define TEE_ORIGIN_COMMS               0x00000002
+
+/**
+ * struct tee_shm - memory shared with the TEE
+ * @dev:       The TEE device
+ * @link:      List node in the list in struct struct tee_uclass_priv
+ * @addr:      Pointer to the shared memory
+ * @size:      Size of the the shared memory
+ * @flags:     TEE_SHM_* above
+ */

include/tee.h line 68

Quoted 10 lines
include/tee.h new file +10 -0
+struct tee_shm {
+       struct udevice *dev;
+       struct list_head link;
+       void *addr;
+       ulong size;
+       u32 flags;
+};
+
+/**
+ * struct tee_param_memref - parameter memory reference

Used for ...?

include/tee.h line 80

Quoted 12 lines
include/tee.h new file +12 -0
+ * @shm_offs:  Offset in bytes into the shared memory object @shm
+ * @size:      Size in bytes of the memory reference
+ * @shm:       Pointer to a shared memory object for the buffer
+ */
+struct tee_param_memref {
+       ulong shm_offs;
+       ulong size;
+       struct tee_shm *shm;
+};
+
+/**
+ * struct tee_param_value - value parameter

??

Quoted 1 line

This doesn't really explain anything. What is it for?

I'll expand and refer to struct tee_param below.

Quoted 36 lines
Quoted 36 lines
include/tee.h new file +35 -0
+ * @a, @b, @c: Parameters passed by value
+ */
+struct tee_param_value {
+       u64 a;
+       u64 b;
+       u64 c;
+};
+
+/**
+ * struct tee_param - invoke parameter
+ * @attr:      Attributes
+ * @u.memref:  Memref parameter if (@attr & TEE_PARAM_ATTR_MASK) is one of
+ *             TEE_PARAM_ATTR_TYPE_MEMREF_* above
+ * @u.value:   Value parameter if (@attr & TEE_PARAM_ATTR_MASK) is one of
+ *             TEE_PARAM_ATTR_TYPE_VALUE_* above
+ */
+struct tee_param {
+       u64 attr;
+       union {
+               struct tee_param_memref memref;
+               struct tee_param_value value;
+       } u;
+};
+
+/**
+ * struct tee_open_session_arg - extra arguments for tee_open_session()
+ * @uuid:      [in] UUID of the Trusted Application
+ * @clnt_uuid: [in] Normally zeroes
+ * @clnt_login:        [in] Normally 0
+ * @session:   [out] Session id
+ * @ret:       [out] return value
+ * @ret_origin:        [out] origin of the return value
+ */
+struct tee_open_session_arg {
+       u8 uuid[TEE_UUID_LEN];
Quoted 8 lines

include/tee.h line 120

Quoted 5 lines
include/tee.h new file +5 -0
+       u8 clnt_uuid[TEE_UUID_LEN];
+       u32 clnt_login;
+       u32 session;
+       u32 ret;
+       u32 ret_origin;

Do these use the sized u32 type because it is an API of some sort? Otherwise, why not uint?

Yes, the minimum width is 32-bits and wider than that is waste.

Quoted 30 lines
Quoted 30 lines
include/tee.h new file +29 -0
+};
+
+/**
+ * struct tee_invoke_arg - extra arguments for tee_invoke_func()
+ * @func:      [in] Trusted Application function, specific to the TA
+ * @session:   [in] Session id, from open session
+ * @ret:       [out] return value
+ * @ret_origin:        [out] origin of the return value
+ */
+struct tee_invoke_arg {
+       u32 func;
+       u32 session;
+       u32 ret;
+       u32 ret_origin;
+};
+
+/**
+ * struct tee_version_data - description of TEE
+ * @gen_caps:  Generic capabilities, TEE_GEN_CAP_* above
+ */
+struct tee_version_data {
+       u32 gen_caps;
+};
+
+/**
+ * struct tee_driver_ops - TEE driver operations
+ * @get_version:       Query capabilities of TEE device,
+                       see tee_get_version()
+ * @open_session:      Opens a session to a Trusted Application in the TEE,
Quoted 8 lines

include/tee.h line 154

Quoted 5 lines
include/tee.h new file +5 -0
+ *                     see tee_open_session().
+ * @close_session:     Closes a session to Trusted Application,
+ *                     see tee_close_session()
+ * @invoke_func:       Invokes a function in a Trusted Application,
+                       see tee_invoke_func()

These comments should be against each function in the struct below. You should repeat the comment from the exported functions there too.

I'll fix.

Quoted 19 lines
Quoted 19 lines
include/tee.h new file +18 -0
+ */
+struct tee_driver_ops {
+       void (*get_version)(struct udevice *dev, struct tee_version_data *vers);
+       int (*open_session)(struct udevice *dev,
+                           struct tee_open_session_arg *arg, ulong num_param,
+                           struct tee_param *param);
+       int (*close_session)(struct udevice *dev, u32 session);
+       int (*invoke_func)(struct udevice *dev, struct tee_invoke_arg *arg,
+                          ulong num_param, struct tee_param *param);
+       /**
+        * shm_register() - Registers memory shared with the TEE
+        * @dev:        The TEE device
+        * @shm:        Pointer to a shared memory object
+        * Returns 0 on success or < 0 on failure.
+        */
+       int (*shm_register)(struct udevice *dev, struct tee_shm *shm);
+       /**
+        * shm_unregister() - Unregisters memory shared with the TEE
Quoted 8 lines
Quoted 7 lines
include/tee.h new file +7 -0
+        * @dev:        The TEE device
+        * @shm:        Pointer to a shared memory object
+        * Returns 0 on success or < 0 on failure.
+        */
+       int (*shm_unregister)(struct udevice *dev, struct tee_shm *shm);
+};
+

[...]

Thanks for the review.
--
Jens

#13 lore raw

PATCH v2 6/15 dt/bindings: add bindings for optee

In reply to Jens Wiklander

Sync with c8bfafb15944 ("dt/bindings: add bindings for optee") from Linux kernel.

Introduces linaro prefix and adds bindings for ARM TrustZone based OP-TEE implementation.

---
 .../firmware/linaro,optee-tz.txt              | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 doc/device-tree-bindings/firmware/linaro,optee-tz.txt
doc/device-tree-bindings/firmware/linaro,optee-tz.txt new file +31 -0
new file mode 100644
index 000000000000..d38834c67dff
@@ -0,0 +1,31 @@
+OP-TEE Device Tree Bindings
+
+OP-TEE is a piece of software using hardware features to provide a Trusted
+Execution Environment. The security can be provided with ARM TrustZone, but
+also by virtualization or a separate chip.
+
+We're using "linaro" as the first part of the compatible property for
+the reference implementation maintained by Linaro.
+
+* OP-TEE based on ARM TrustZone required properties:
+
+- compatible     : should contain "linaro,optee-tz"
+
+- method         : The method of calling the OP-TEE Trusted OS. Permitted
+                   values are:
+
+                   "smc" : SMC #0, with the register assignments specified
+		           in drivers/tee/optee/optee_smc.h
+
+                   "hvc" : HVC #0, with the register assignments specified
+		           in drivers/tee/optee/optee_smc.h
+
+
+
+Example:
+	firmware {
+		optee {
+			compatible = "linaro,optee-tz";
+			method = "smc";
+		};
+	};
Signature · 1 line
2.17.1
#14 lore raw

PATCH v2 7/15 tee: add OP-TEE driver

In reply to Jens Wiklander

Adds a OP-TEE driver.

* Targets ARM and ARM64
* Supports using any u-boot memory as shared memory
* Probes OP-TEE version using SMCs
* Uses OPTEE message protocol version 2 to communicate with secure world

---
 drivers/tee/Kconfig                      |  10 +
 drivers/tee/Makefile                     |   1 +
 drivers/tee/optee/Kconfig                |   7 +
 drivers/tee/optee/Makefile               |   4 +
 drivers/tee/optee/core.c                 | 614 +++++++++++++++++++++++
 drivers/tee/optee/optee_msg.h            | 423 ++++++++++++++++
 drivers/tee/optee/optee_msg_supplicant.h | 234 +++++++++
 drivers/tee/optee/optee_private.h        |  12 +
 drivers/tee/optee/optee_smc.h            | 444 ++++++++++++++++
 drivers/tee/optee/supplicant.c           |  89 ++++
 10 files changed, 1838 insertions(+)
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_msg.h
 create mode 100644 drivers/tee/optee/optee_msg_supplicant.h
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/supplicant.c
drivers/tee/Kconfig +10 -0
index 817ab331b0f8..3e7fe6ddcc5d 100644
@@ -6,3 +6,13 @@ config TEE
 	help
 	  This implements a generic interface towards a Trusted Execution
 	  Environment (TEE).
+
+if TEE
+
+menu "TEE drivers"
+
+source "drivers/tee/optee/Kconfig"
+
+endmenu
+
+endif
drivers/tee/Makefile +1 -0
index b6d8e16e6211..19633b60f235 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0+
 
 obj-y += tee-uclass.o
+obj-$(CONFIG_OPTEE) += optee/
drivers/tee/optee/Kconfig new file +7 -0
new file mode 100644
index 000000000000..8f7ebe161111
@@ -0,0 +1,7 @@
+# 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.
drivers/tee/optee/Makefile new file +4 -0
new file mode 100644
index 000000000000..6148feb474a5
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-y += core.o
+obj-y += supplicant.o
drivers/tee/optee/core.c new file +614 -0

614 lines not on this page · raw message

drivers/tee/optee/optee_msg.h new file +423 -0

423 lines not on this page · raw message

drivers/tee/optee/optee_msg_supplicant.h new file +234 -0

234 lines not on this page · raw message

drivers/tee/optee/optee_private.h new file +12 -0
new file mode 100644
index 000000000000..daa470f812a9
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
+#ifndef __OPTEE_PRIVATE_H
+#define __OPTEE_PRIVATE_H
+
+void *optee_alloc_and_init_page_list(void *buf, ulong len, u64 *phys_buf_ptr);
+void optee_suppl_cmd(struct udevice *dev, void *shm, void **page_list);
+
+#endif /*__OPTEE_PRIVATE_H*/
drivers/tee/optee/optee_smc.h new file +444 -0

444 lines not on this page · raw message

drivers/tee/optee/supplicant.c new file +89 -0

89 lines not on this page · raw message

Signature · 1 line
2.17.1
#15 lore raw

PATCH v2 7/15 tee: add OP-TEE driver

Hi Jens,

Quoted 4 lines · Jens Wiklander

Adds a OP-TEE driver.

* Targets ARM and ARM64
* Supports using any u-boot memory as shared memory

U-Boot

Please use this consistent.

Quoted 17 lines

* Probes OP-TEE version using SMCs
* Uses OPTEE message protocol version 2 to communicate with secure world

---
 drivers/tee/Kconfig                      |  10 +
 drivers/tee/Makefile                     |   1 +
 drivers/tee/optee/Kconfig                |   7 +
 drivers/tee/optee/Makefile               |   4 +
 drivers/tee/optee/core.c                 | 614 +++++++++++++++++++++++
 drivers/tee/optee/optee_msg.h            | 423 ++++++++++++++++
 drivers/tee/optee/optee_msg_supplicant.h | 234 +++++++++
 drivers/tee/optee/optee_private.h        |  12 +
 drivers/tee/optee/optee_smc.h            | 444 ++++++++++++++++
 drivers/tee/optee/supplicant.c           |  89 ++++
 10 files changed, 1838 insertions(+)
Quoted 9 lines
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_msg.h
 create mode 100644 drivers/tee/optee/optee_msg_supplicant.h
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/supplicant.c
Quoted 33 lines
drivers/tee/Kconfig +10 -0
index 817ab331b0f8..3e7fe6ddcc5d 100644
@@ -6,3 +6,13 @@ config TEE
        help
          This implements a generic interface towards a Trusted Execution
          Environment (TEE).
+
+if TEE
+
+menu "TEE drivers"
+
+source "drivers/tee/optee/Kconfig"
+
+endmenu
+
+endif
drivers/tee/Makefile +1 -0
index b6d8e16e6211..19633b60f235 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0+
 
 obj-y += tee-uclass.o
+obj-$(CONFIG_OPTEE) += optee/
drivers/tee/optee/Kconfig new file +0 -0
new file mode 100644
index 000000000000..8f7ebe161111

drivers/tee/optee/Kconfig line 7

Quoted 7 lines
drivers/tee/optee/Kconfig new file +7 -0
@@ -0,0 +1,7 @@
+# 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.

for ARM? I think you should expand this help. What does the driver support / do?

Quoted 17 lines
drivers/tee/optee/Makefile new file +4 -0
new file mode 100644
index 000000000000..6148feb474a5
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-y += core.o
+obj-y += supplicant.o
drivers/tee/optee/core.c new file +1 -0
new file mode 100644
index 000000000000..f2d92d96551b
@@ -0,0 +1,614 @@
+// SPDX-License-Identifier: GPL-2.0+

drivers/tee/optee/core.c line 11

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <linux/arm-smccc.h>
+#include <linux/io.h>
+#include <log.h>
+#include <tee.h>

Put linux/ ones after tee.h

Quoted 21 lines
drivers/tee/optee/core.c new file +21 -0
+
+#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;

drivers/tee/optee/core.c line 42

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+       u32     a3;
+       u32     a4;
+       u32     a5;
+       u32     a6;
+       u32     a7;
+};
+
+static void *reg_pair_to_ptr(u32 reg0, u32 reg1)
+{
+       return (void *)(ulong)(((u64)reg0 << 32) | reg1);

Can you not remove the u64 here?

E.g.

(void *)((ulong)reg0 << 32) | reg1)

drivers/tee/optee/core.c line 45

Quoted 3 lines
drivers/tee/optee/core.c new file +3 -0
+}
+
+static void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)

Function comment

drivers/tee/optee/core.c line 51

Quoted 6 lines
drivers/tee/optee/core.c new file +6 -0
+{
+       *reg0 = val >> 32;
+       *reg1 = val;
+}
+
+void *optee_alloc_and_init_page_list(void *buf, ulong len, u64 *phys_buf_ptr)

Function comment

Quoted 14 lines
drivers/tee/optee/core.c new file +14 -0
+{
+       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;
+

drivers/tee/optee/core.c line 75

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+       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));

Drop extra pair of ()

Quoted 42 lines
drivers/tee/optee/core.c new file +42 -0
+       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 struct tee_shm *get_msg_arg(struct udevice *dev, ulong num_params,
+                                  struct optee_msg_arg **msg_arg)
+{
+       struct tee_shm *shm;
+       struct optee_msg_arg *ma;
+
+       shm = __tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
+                           OPTEE_MSG_GET_ARG_SIZE(num_params), TEE_SHM_ALLOC);
+       if (!shm)
+               return NULL;
+
+       ma = shm->addr;
+       memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));

drivers/tee/optee/core.c line 127

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+       ma->num_params = num_params;
+       *msg_arg = ma;
+
+       return shm;
+}
+
+static int to_msg_param(struct optee_msg_param *msg_params, ulong num_params,
+                       const struct tee_param *params)
+{
+       ulong n;

uint? int?

Save below

Quoted 164 lines
drivers/tee/optee/core.c new file +164 -0
+
+       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, ulong num_params,
+                         const struct optee_msg_param *msg_params)
+{
+       ulong 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:
+               shm = __tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
+                                   param->a1,
+                                   TEE_SHM_ALLOC | TEE_SHM_REGISTER);
+               if (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_platdata(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 {
+                       return call_err_to_res(res.a0);
+               }
+       }
+}
+
+static int optee_close_session(struct udevice *dev, u32 session)
+{
+       struct tee_shm *shm;
+       struct optee_msg_arg *msg_arg;

drivers/tee/optee/core.c line 301

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+
+       shm = get_msg_arg(dev, 0, &msg_arg);
+       if (!shm)
+               return -ENOMEM;
+
+       msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
+       msg_arg->session = session;
+       do_call_with_arg(dev, msg_arg);
+
+       tee_shm_free(shm);

blank line before return

Quoted 3 lines
drivers/tee/optee/core.c new file +3 -0
+       return 0;
+}
+

[..]

Quoted 10 lines
drivers/tee/optee/optee_msg.h new file +4 -0
new file mode 100644
index 000000000000..ebc16aa8cc31
@@ -0,0 +1,423 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2015-2018, Linaro Limited
+ */

drivers/tee/optee/optee_msg.h line 14

Quoted 10 lines
drivers/tee/optee/optee_msg.h new file +10 -0
+
+#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.

Does this file have an upstream, or is this specific to U-Boot? If the former, please add a reference.

drivers/tee/optee/optee_msg.h line 22

Quoted 8 lines
drivers/tee/optee/optee_msg.h new file +8 -0
+ *
+ * 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.
+ */
+

[...]

Regards,
Simon

#16 lore raw

PATCH v2 7/15 tee: add OP-TEE driver

Quoted 11 lines · Simon Glass

Hi Jens,

Quoted 4 lines · Jens Wiklander

Adds a OP-TEE driver.

* Targets ARM and ARM64
* Supports using any u-boot memory as shared memory

U-Boot

Please use this consistent.

Sorry, it seems I've found lots of ways of doing that wrong.

Quoted 65 lines
Quoted 18 lines

* Probes OP-TEE version using SMCs
* Uses OPTEE message protocol version 2 to communicate with secure world

---
 drivers/tee/Kconfig                      |  10 +
 drivers/tee/Makefile                     |   1 +
 drivers/tee/optee/Kconfig                |   7 +
 drivers/tee/optee/Makefile               |   4 +
 drivers/tee/optee/core.c                 | 614 +++++++++++++++++++++++
 drivers/tee/optee/optee_msg.h            | 423 ++++++++++++++++
 drivers/tee/optee/optee_msg_supplicant.h | 234 +++++++++
 drivers/tee/optee/optee_private.h        |  12 +
 drivers/tee/optee/optee_smc.h            | 444 ++++++++++++++++
 drivers/tee/optee/supplicant.c           |  89 ++++
 10 files changed, 1838 insertions(+)
Quoted 9 lines
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_msg.h
 create mode 100644 drivers/tee/optee/optee_msg_supplicant.h
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/supplicant.c
Quoted 35 lines
drivers/tee/Kconfig +10 -0
index 817ab331b0f8..3e7fe6ddcc5d 100644
@@ -6,3 +6,13 @@ config TEE
        help
          This implements a generic interface towards a Trusted Execution
          Environment (TEE).
+
+if TEE
+
+menu "TEE drivers"
+
+source "drivers/tee/optee/Kconfig"
+
+endmenu
+
+endif
drivers/tee/Makefile +1 -0
index b6d8e16e6211..19633b60f235 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0+
 
 obj-y += tee-uclass.o
+obj-$(CONFIG_OPTEE) += optee/
drivers/tee/optee/Kconfig new file +2 -0
new file mode 100644
index 000000000000..8f7ebe161111
@@ -0,0 +1,7 @@
+# OP-TEE Trusted Execution Environment Configuration
+config OPTEE
Quoted 8 lines

drivers/tee/optee/Kconfig line 7

Quoted 5 lines
drivers/tee/optee/Kconfig new file +5 -0
+       bool "OP-TEE"
+       depends on ARM_SMCCC
+       help
+         This implements the OP-TEE Trusted Execution Environment (TEE)
+         driver.

for ARM? I think you should expand this help. What does the driver support / do?

I'll expand this.

Quoted 21 lines
Quoted 21 lines
drivers/tee/optee/Makefile new file +4 -0
new file mode 100644
index 000000000000..6148feb474a5
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-y += core.o
+obj-y += supplicant.o
drivers/tee/optee/core.c new file +5 -0
new file mode 100644
index 000000000000..f2d92d96551b
@@ -0,0 +1,614 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
Quoted 8 lines

drivers/tee/optee/core.c line 11

Quoted 6 lines
drivers/tee/optee/core.c new file +6 -0
+#include <common.h>
+#include <dm.h>
+#include <linux/arm-smccc.h>
+#include <linux/io.h>
+#include <log.h>
+#include <tee.h>

Put linux/ ones after tee.h

OK

Quoted 34 lines
Quoted 21 lines
drivers/tee/optee/core.c new file +21 -0
+
+#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;

drivers/tee/optee/core.c line 42

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+       u32     a3;
+       u32     a4;
+       u32     a5;
+       u32     a6;
+       u32     a7;
+};
+
+static void *reg_pair_to_ptr(u32 reg0, u32 reg1)
+{
+       return (void *)(ulong)(((u64)reg0 << 32) | reg1);

Can you not remove the u64 here?

Quoted 3 lines

E.g.

(void *)((ulong)reg0 << 32) | reg1)

No, that will cause:
drivers/tee/optee/core.c:42:38: warning: left shift count >= width of type [-Wshift-count-overflow]

  return (void *)(ulong)(((ulong)reg0 << 32) | reg1);
                                      ^~

on ILP32

Quoted 98 lines

drivers/tee/optee/core.c line 45

Quoted 4 lines
drivers/tee/optee/core.c new file +3 -0
+}
+
+static void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)

Function comment

drivers/tee/optee/core.c line 51

Quoted 6 lines
drivers/tee/optee/core.c new file +6 -0
+{
+       *reg0 = val >> 32;
+       *reg1 = val;
+}
+
+void *optee_alloc_and_init_page_list(void *buf, ulong len, u64 *phys_buf_ptr)

Function comment

Quoted 14 lines
drivers/tee/optee/core.c new file +14 -0
+{
+       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;
+

drivers/tee/optee/core.c line 75

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+       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));

Drop extra pair of ()

Quoted 42 lines
drivers/tee/optee/core.c new file +42 -0
+       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 struct tee_shm *get_msg_arg(struct udevice *dev, ulong num_params,
+                                  struct optee_msg_arg **msg_arg)
+{
+       struct tee_shm *shm;
+       struct optee_msg_arg *ma;
+
+       shm = __tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
+                           OPTEE_MSG_GET_ARG_SIZE(num_params), TEE_SHM_ALLOC);
+       if (!shm)
+               return NULL;
+
+       ma = shm->addr;
+       memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));

drivers/tee/optee/core.c line 127

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+       ma->num_params = num_params;
+       *msg_arg = ma;
+
+       return shm;
+}
+
+static int to_msg_param(struct optee_msg_param *msg_params, ulong num_params,
+                       const struct tee_param *params)
+{
+       ulong n;

uint? int?

Quoted 1 line

Save below

OK, I'll change the type for holding number of parameters to uint here and everywhere else.

Quoted 199 lines
Quoted 165 lines
drivers/tee/optee/core.c new file +164 -0
+
+       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, ulong num_params,
+                         const struct optee_msg_param *msg_params)
+{
+       ulong 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:
+               shm = __tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
+                                   param->a1,
+                                   TEE_SHM_ALLOC | TEE_SHM_REGISTER);
+               if (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_platdata(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 {
+                       return call_err_to_res(res.a0);
+               }
+       }
+}
+
+static int optee_close_session(struct udevice *dev, u32 session)
+{
+       struct tee_shm *shm;
+       struct optee_msg_arg *msg_arg;

drivers/tee/optee/core.c line 301

Quoted 10 lines
drivers/tee/optee/core.c new file +10 -0
+
+       shm = get_msg_arg(dev, 0, &msg_arg);
+       if (!shm)
+               return -ENOMEM;
+
+       msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
+       msg_arg->session = session;
+       do_call_with_arg(dev, msg_arg);
+
+       tee_shm_free(shm);

blank line before return

Quoted 3 lines
drivers/tee/optee/core.c new file +3 -0
+       return 0;
+}
+

[..]

Quoted 15 lines
drivers/tee/optee/optee_msg.h new file +9 -0
new file mode 100644
index 000000000000..ebc16aa8cc31
@@ -0,0 +1,423 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2015-2018, Linaro Limited
+ */
+
+#ifndef _OPTEE_MSG_H
+#define _OPTEE_MSG_H
+
+#include <linux/bitops.h>
Quoted 8 lines

drivers/tee/optee/optee_msg.h line 14

Quoted 5 lines
drivers/tee/optee/optee_msg.h new file +5 -0
+#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.

Does this file have an upstream, or is this specific to U-Boot? If the former, please add a reference.

Yes, there's an upstream. I'll update.

Quoted 11 lines
Quoted 9 lines
drivers/tee/optee/optee_msg.h new file +8 -0
+ *
+ * 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.
+ */
+

[...]

Thanks for the review,
Jens

#17 lore raw

PATCH v2 8/15 Documentation: tee uclass and op-tee driver

In reply to Jens Wiklander
---
 doc/README.tee | 112 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
 create mode 100644 doc/README.tee
doc/README.tee new file +112 -0
new file mode 100644
index 000000000000..e9c9ef67877a
@@ -0,0 +1,112 @@
+=============
+TEE uclass
+=============
+
+This document describes the TEE uclass in U-boot
+
+A TEE (Trusted Execution Environment) is a trusted OS running in some
+secure environment, for example, TrustZone on ARM CPUs, or a separate
+secure co-processor etc. A TEE driver handles the details needed to
+communicate with the TEE.
+
+This uclass deals with:
+
+- Registration of TEE drivers
+
+- Managing shared memory between U-boot and the TEE
+
+- Providing a generic API to the TEE
+
+The TEE interface
+=================
+
+include/tee.h defines the generic interface to a TEE.
+
+A client finds the TEE device via tee_find_device(). Other important functions
+when interfacing with a TEE are:
+
+- tee_shm_alloc(), tee_shm_register() and tee_shm_free() to manage shared
+  memory objects often needed when communicating with the TEE.
+
+- tee_get_version() lets the client know which the capabilities of the TEE
+  device.
+
+- tee_open_session() opens a session to a Trusted Application
+
+- tee_invoke_func() invokes a function in a Trusted Application
+
+- tee_close_session() closes a session to a Trusted Application
+
+Much of the communication between clients and the TEE is opaque to the
+driver. The main job for the driver is to receive requests from the
+clients, forward them to the TEE and send back the results.
+
+OP-TEE driver
+=============
+
+The OP-TEE driver handles OP-TEE [1] based TEEs. Currently it is only the ARM
+TrustZone based OP-TEE solution that is supported.
+
+Lowest level of communication with OP-TEE builds on ARM SMC Calling
+Convention (SMCCC) [2], which is the foundation for OP-TEE's SMC interface
+[3] used internally by the driver. Stacked on top of that is OP-TEE Message
+Protocol [4].
+
+OP-TEE SMC interface provides the basic functions required by SMCCC and some
+additional functions specific for OP-TEE. The most interesting functions are:
+
+- OPTEE_SMC_FUNCID_CALLS_UID (part of SMCCC) returns the version information
+  which is then returned by TEE_IOC_VERSION
+
+- OPTEE_SMC_CALL_GET_OS_UUID returns the particular OP-TEE implementation, used
+  to tell, for instance, a TrustZone OP-TEE apart from an OP-TEE running on a
+  separate secure co-processor.
+
+- OPTEE_SMC_CALL_WITH_ARG drives the OP-TEE message protocol
+
+- OPTEE_SMC_GET_SHM_CONFIG lets the driver and OP-TEE agree on which memory
+  range to used for shared memory between Linux and OP-TEE.
+
+The GlobalPlatform TEE Client API [5] is implemented on top of the generic
+TEE API.
+
+Picture of the relationship between the different components in the
+OP-TEE architecture:
+
+                   U-boot                  Secure world
+                   ~~~~~~                  ~~~~~~~~~~~~
+                 +------------+           +-------------+
+                 | Client     |           | Trusted     |
+                 |            |           | Application |
+                 +------------+           +-------------+
+                       /\                       /\
+                       ||                       ||
+                       \/                       \/
+                 +------------+           +-------------+
+                 | TEE        |           | TEE Internal|
+                 | uclass     |           | API         |
+                 +------------+           +-------------+
+                 | OP-TEE     |           | OP-TEE      |
+                 | driver     |           | Trusted OS  |
+                 +------------+-----------+-------------+
+                 |             OP-TEE MSG               |
+                 |      SMCCC (OPTEE_SMC_CALL_*)        |
+                 +--------------------------------------+
+
+RPC (Remote Procedure Call) are requests from secure world to the driver.
+An RPC is identified by a special range of SMCCC return values from
+OPTEE_SMC_CALL_WITH_ARG.
+
+References
+==========
+
+[1] https://github.com/OP-TEE/optee_os
+
+[2] http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html
+
+[3] drivers/tee/optee/optee_smc.h
+
+[4] drivers/tee/optee/optee_msg.h
+
+[5] http://www.globalplatform.org/specificationsdevice.asp look for
+    "TEE Client API Specification v1.0" and click download.
Signature · 1 line
2.17.1
#18 lore raw

PATCH v2 8/15 Documentation: tee uclass and op-tee driver

Hi Jens,

Quoted 5 lines · Jens Wiklander
---
 doc/README.tee | 112 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
 create mode 100644 doc/README.tee

Looks good, nits below.

doc/README.tee line 5

Quoted 12 lines
doc/README.tee new file +5 -0
new file mode 100644
index 000000000000..e9c9ef67877a
@@ -0,0 +1,112 @@
+=============
+TEE uclass
+=============
+
+This document describes the TEE uclass in U-boot

U-Boot

(please can you check all your patches for that? There are more below)

Quoted 33 lines
doc/README.tee new file +33 -0
+
+A TEE (Trusted Execution Environment) is a trusted OS running in some
+secure environment, for example, TrustZone on ARM CPUs, or a separate
+secure co-processor etc. A TEE driver handles the details needed to
+communicate with the TEE.
+
+This uclass deals with:
+
+- Registration of TEE drivers
+
+- Managing shared memory between U-boot and the TEE
+
+- Providing a generic API to the TEE
+
+The TEE interface
+=================
+
+include/tee.h defines the generic interface to a TEE.
+
+A client finds the TEE device via tee_find_device(). Other important functions
+when interfacing with a TEE are:
+
+- tee_shm_alloc(), tee_shm_register() and tee_shm_free() to manage shared
+  memory objects often needed when communicating with the TEE.
+
+- tee_get_version() lets the client know which the capabilities of the TEE
+  device.
+
+- tee_open_session() opens a session to a Trusted Application
+
+- tee_invoke_func() invokes a function in a Trusted Application
+
+- tee_close_session() closes a session to a Trusted Application

doc/README.tee line 48

Quoted 10 lines
doc/README.tee new file +10 -0
+
+Much of the communication between clients and the TEE is opaque to the
+driver. The main job for the driver is to receive requests from the
+clients, forward them to the TEE and send back the results.
+
+OP-TEE driver
+=============
+
+The OP-TEE driver handles OP-TEE [1] based TEEs. Currently it is only the ARM
+TrustZone based OP-TEE solution that is supported.

In fact, wouldn't other things be supported by different drivers?

Perhaps you should name your driver to indicate it is only for ARM?

Quoted 65 lines
doc/README.tee new file +64 -1
+
+Lowest level of communication with OP-TEE builds on ARM SMC Calling
+Convention (SMCCC) [2], which is the foundation for OP-TEE's SMC interface
+[3] used internally by the driver. Stacked on top of that is OP-TEE Message
+Protocol [4].
+
+OP-TEE SMC interface provides the basic functions required by SMCCC and some
+additional functions specific for OP-TEE. The most interesting functions are:
+
+- OPTEE_SMC_FUNCID_CALLS_UID (part of SMCCC) returns the version information
+  which is then returned by TEE_IOC_VERSION
+
+- OPTEE_SMC_CALL_GET_OS_UUID returns the particular OP-TEE implementation, used
+  to tell, for instance, a TrustZone OP-TEE apart from an OP-TEE running on a
+  separate secure co-processor.
+
+- OPTEE_SMC_CALL_WITH_ARG drives the OP-TEE message protocol
+
+- OPTEE_SMC_GET_SHM_CONFIG lets the driver and OP-TEE agree on which memory
+  range to used for shared memory between Linux and OP-TEE.
+
+The GlobalPlatform TEE Client API [5] is implemented on top of the generic
+TEE API.
+
+Picture of the relationship between the different components in the
+OP-TEE architecture:
+
+                   U-boot                  Secure world
+                   ~~~~~~                  ~~~~~~~~~~~~
+                 +------------+           +-------------+
+                 | Client     |           | Trusted     |
+                 |            |           | Application |
+                 +------------+           +-------------+
+                       /\                       /\
+                       ||                       ||
+                       \/                       \/
+                 +------------+           +-------------+
+                 | TEE        |           | TEE Internal|
+                 | uclass     |           | API         |
+                 +------------+           +-------------+
+                 | OP-TEE     |           | OP-TEE      |
+                 | driver     |           | Trusted OS  |
+                 +------------+-----------+-------------+
+                 |             OP-TEE MSG               |
+                 |      SMCCC (OPTEE_SMC_CALL_*)        |
+                 +--------------------------------------+
+
+RPC (Remote Procedure Call) are requests from secure world to the driver.
+An RPC is identified by a special range of SMCCC return values from
+OPTEE_SMC_CALL_WITH_ARG.
+
+References
+==========
+
+[1] https://github.com/OP-TEE/optee_os
+
+[2] http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html
+
+[3] drivers/tee/optee/optee_smc.h
+
+[4] drivers/tee/optee/optee_msg.h
+
+[5] http://www.globalplatform.org/specificationsdevice.asp look for
+    "TEE Client API Specification v1.0" and click download.
--
Quoted 2 lines

2.17.1

Regards,
Simon

#19 lore raw

PATCH v2 8/15 Documentation: tee uclass and op-tee driver

Hi Simon,

Quoted 29 lines · Simon Glass

Hi Jens,

Quoted 5 lines · Jens Wiklander
---
 doc/README.tee | 112 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
 create mode 100644 doc/README.tee

Looks good, nits below.

doc/README.tee line 5

Quoted 12 lines
doc/README.tee new file +5 -0
new file mode 100644
index 000000000000..e9c9ef67877a
@@ -0,0 +1,112 @@
+=============
+TEE uclass
+=============
+
+This document describes the TEE uclass in U-boot

U-Boot

Quoted 1 line

(please can you check all your patches for that? There are more below)

Yes, I'll check them all.

Quoted 47 lines
Quoted 34 lines
doc/README.tee new file +33 -0
+
+A TEE (Trusted Execution Environment) is a trusted OS running in some
+secure environment, for example, TrustZone on ARM CPUs, or a separate
+secure co-processor etc. A TEE driver handles the details needed to
+communicate with the TEE.
+
+This uclass deals with:
+
+- Registration of TEE drivers
+
+- Managing shared memory between U-boot and the TEE
+
+- Providing a generic API to the TEE
+
+The TEE interface
+=================
+
+include/tee.h defines the generic interface to a TEE.
+
+A client finds the TEE device via tee_find_device(). Other important functions
+when interfacing with a TEE are:
+
+- tee_shm_alloc(), tee_shm_register() and tee_shm_free() to manage shared
+  memory objects often needed when communicating with the TEE.
+
+- tee_get_version() lets the client know which the capabilities of the TEE
+  device.
+
+- tee_open_session() opens a session to a Trusted Application
+
+- tee_invoke_func() invokes a function in a Trusted Application
+
+- tee_close_session() closes a session to a Trusted Application

doc/README.tee line 48

Quoted 10 lines
doc/README.tee new file +10 -0
+
+Much of the communication between clients and the TEE is opaque to the
+driver. The main job for the driver is to receive requests from the
+clients, forward them to the TEE and send back the results.
+
+OP-TEE driver
+=============
+
+The OP-TEE driver handles OP-TEE [1] based TEEs. Currently it is only the ARM
+TrustZone based OP-TEE solution that is supported.

In fact, wouldn't other things be supported by different drivers?

Quoted 1 line

Perhaps you should name your driver to indicate it is only for ARM?

The OP-TEE Message prototol isn't tied to ARM only and OP-TEE has been or is used on at least one other architecture (not open sourced though). I think that only small changes will be needed in the the OP-TEE driver to support a different architecture as long as shared memory still can be used. I'd rather keep the name as just "optee" until we know what we need to adapt to.

Quoted 60 lines
Quoted 60 lines
doc/README.tee new file +59 -0
+
+Lowest level of communication with OP-TEE builds on ARM SMC Calling
+Convention (SMCCC) [2], which is the foundation for OP-TEE's SMC interface
+[3] used internally by the driver. Stacked on top of that is OP-TEE Message
+Protocol [4].
+
+OP-TEE SMC interface provides the basic functions required by SMCCC and some
+additional functions specific for OP-TEE. The most interesting functions are:
+
+- OPTEE_SMC_FUNCID_CALLS_UID (part of SMCCC) returns the version information
+  which is then returned by TEE_IOC_VERSION
+
+- OPTEE_SMC_CALL_GET_OS_UUID returns the particular OP-TEE implementation, used
+  to tell, for instance, a TrustZone OP-TEE apart from an OP-TEE running on a
+  separate secure co-processor.
+
+- OPTEE_SMC_CALL_WITH_ARG drives the OP-TEE message protocol
+
+- OPTEE_SMC_GET_SHM_CONFIG lets the driver and OP-TEE agree on which memory
+  range to used for shared memory between Linux and OP-TEE.
+
+The GlobalPlatform TEE Client API [5] is implemented on top of the generic
+TEE API.
+
+Picture of the relationship between the different components in the
+OP-TEE architecture:
+
+                   U-boot                  Secure world
+                   ~~~~~~                  ~~~~~~~~~~~~
+                 +------------+           +-------------+
+                 | Client     |           | Trusted     |
+                 |            |           | Application |
+                 +------------+           +-------------+
+                       /\                       /\
+                       ||                       ||
+                       \/                       \/
+                 +------------+           +-------------+
+                 | TEE        |           | TEE Internal|
+                 | uclass     |           | API         |
+                 +------------+           +-------------+
+                 | OP-TEE     |           | OP-TEE      |
+                 | driver     |           | Trusted OS  |
+                 +------------+-----------+-------------+
+                 |             OP-TEE MSG               |
+                 |      SMCCC (OPTEE_SMC_CALL_*)        |
+                 +--------------------------------------+
+
+RPC (Remote Procedure Call) are requests from secure world to the driver.
+An RPC is identified by a special range of SMCCC return values from
+OPTEE_SMC_CALL_WITH_ARG.
+
+References
+==========
+
+[1] https://github.com/OP-TEE/optee_os
+
+[2] http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html
+
+[3] drivers/tee/optee/optee_smc.h
Quoted 8 lines
Quoted 8 lines
doc/README.tee new file +5 -1
+
+[4] drivers/tee/optee/optee_msg.h
+
+[5] http://www.globalplatform.org/specificationsdevice.asp look for
+    "TEE Client API Specification v1.0" and click download.
--

2.17.1

Thanks for the review,
Jens

#20 lore raw

PATCH v2 9/15 test: tee: test TEE uclass

In reply to Jens Wiklander

Tests the TEE uclass with a sandbox tee driver.

---
 drivers/tee/Kconfig |   5 +-
 test/dm/Makefile    |   1 +
 test/dm/tee.c       | 182 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 185 insertions(+), 3 deletions(-)
 create mode 100644 test/dm/tee.c
drivers/tee/Kconfig +2 -3
index 3e7fe6ddcc5d..032aff4777c6 100644
@@ -1,8 +1,8 @@
 # Generic Trusted Execution Environment Configuration
 config TEE
 	bool "Trusted Execution Environment support"
-	depends on ARM && (ARM64 || CPU_V7A)
-	select ARM_SMCCC
+	depends on (ARM && (ARM64 || CPU_V7A)) || SANDBOX
+	select ARM_SMCCC if ARM
 	help
 	  This implements a generic interface towards a Trusted Execution
 	  Environment (TEE).
@@ -14,5 +14,4 @@ menu "TEE drivers"
 source "drivers/tee/optee/Kconfig"
 
 endmenu
-
 endif
test/dm/Makefile +1 -0
index d2ed96c61533..272374b92fb0 100644
@@ -44,4 +44,5 @@ obj-$(CONFIG_DM_VIDEO) += video.o
 obj-$(CONFIG_ADC) += adc.o
 obj-$(CONFIG_SPMI) += spmi.o
 obj-$(CONFIG_WDT) += wdt.o
+obj-$(CONFIG_TEE) += tee.o
 endif
test/dm/tee.c new file +182 -0

182 lines not on this page · raw message

Signature · 1 line
2.17.1
#21 lore raw

PATCH v2 9/15 test: tee: test TEE uclass

Quoted 9 lines · Jens Wiklander

Tests the TEE uclass with a sandbox tee driver.

---
 drivers/tee/Kconfig |   5 +-
 test/dm/Makefile    |   1 +
 test/dm/tee.c       | 182 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 185 insertions(+), 3 deletions(-)
 create mode 100644 test/dm/tee.c
#22 lore raw

PATCH v2 10/15 sandbox: dt: add sandbox_tee node

In reply to Jens Wiklander

Adds a sandbox_tee node to enable the sandbox tee driver in all the sandbox dts files.

---
 arch/sandbox/dts/sandbox.dts   | 4 ++++
 arch/sandbox/dts/sandbox64.dts | 4 ++++
 arch/sandbox/dts/test.dts      | 4 ++++
 3 files changed, 12 insertions(+)
arch/sandbox/dts/sandbox.dts +4 -0
index 9f444c96a9ec..f5c02e5396ff 100644
@@ -311,6 +311,10 @@
 			};
 		};
 	};
+
+	sandbox_tee {
+		compatible = "sandbox,tee";
+	};
 };
 
 #include "cros-ec-keyboard.dtsi"
arch/sandbox/dts/sandbox64.dts +4 -0
index 9e65d2fda3d3..0e32fdad9d81 100644
@@ -311,6 +311,10 @@
 			};
 		};
 	};
+
+	sandbox_tee {
+		compatible = "sandbox,tee";
+	};
 };
 
 #include "cros-ec-keyboard.dtsi"
arch/sandbox/dts/test.dts +4 -0
index 70356461959d..eee7518be823 100644
@@ -604,6 +604,10 @@
 			};
 		};
 	};
+
+	sandbox_tee {
+		compatible = "sandbox,tee";
+	};
 };
 
 #include "sandbox_pmic.dtsi"
Signature · 1 line
2.17.1
#23 lore raw

PATCH v2 10/15 sandbox: dt: add sandbox_tee node

Quoted 10 lines · Jens Wiklander

Adds a sandbox_tee node to enable the sandbox tee driver in all the sandbox dts files.

---
 arch/sandbox/dts/sandbox.dts   | 4 ++++
 arch/sandbox/dts/sandbox64.dts | 4 ++++
 arch/sandbox/dts/test.dts      | 4 ++++
 3 files changed, 12 insertions(+)

BTW it's only the test.dts one that matters for tests. But it's fine to add this in the others.

#24 lore raw

PATCH v2 10/15 sandbox: dt: add sandbox_tee node

Quoted 12 lines · Simon Glass
Quoted 10 lines · Jens Wiklander

Adds a sandbox_tee node to enable the sandbox tee driver in all the sandbox dts files.

---
 arch/sandbox/dts/sandbox.dts   | 4 ++++
 arch/sandbox/dts/sandbox64.dts | 4 ++++
 arch/sandbox/dts/test.dts      | 4 ++++
 3 files changed, 12 insertions(+)
Quoted 4 lines

BTW it's only the test.dts one that matters for tests. But it's fine to add this in the others.

OK.

Thanks,
Jens

#25 lore raw

PATCH v2 11/15 configs: sandbox: enable CONFIG_TEE (TEE uclass)

In reply to Jens Wiklander
---
 configs/sandbox64_defconfig        | 1 +
 configs/sandbox_defconfig          | 1 +
 configs/sandbox_flattree_defconfig | 1 +
 configs/sandbox_noblk_defconfig    | 1 +
 configs/sandbox_spl_defconfig      | 1 +
 5 files changed, 5 insertions(+)
configs/sandbox64_defconfig +1 -0
index 5bd5927b410b..213755589382 100644
@@ -199,3 +199,4 @@ CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
 CONFIG_UT_ENV=y
 CONFIG_UT_OVERLAY=y
+CONFIG_TEE=y
configs/sandbox_defconfig +1 -0
index afc34298ed9f..e9cba7895c33 100644
@@ -200,3 +200,4 @@ CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
 CONFIG_UT_ENV=y
 CONFIG_UT_OVERLAY=y
+CONFIG_TEE=y
configs/sandbox_flattree_defconfig +1 -0
index 84b0756a4dc7..80f3055fd079 100644
@@ -174,3 +174,4 @@ CONFIG_UNIT_TEST=y
 CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
 CONFIG_UT_ENV=y
+CONFIG_TEE=y
configs/sandbox_noblk_defconfig +1 -0
index d1ca61bff2e8..54ba5bc7dd36 100644
@@ -175,3 +175,4 @@ CONFIG_UNIT_TEST=y
 CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
 CONFIG_UT_ENV=y
+CONFIG_TEE=y
configs/sandbox_spl_defconfig +1 -0
index c00672f6cc0d..87cbd67c6d21 100644
@@ -194,3 +194,4 @@ CONFIG_UNIT_TEST=y
 CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
 CONFIG_UT_ENV=y
+CONFIG_TEE=y
Signature · 1 line
2.17.1
#26 lore raw

PATCH v2 11/15 configs: sandbox: enable CONFIG_TEE (TEE uclass)

Quoted 9 lines · Jens Wiklander
---
 configs/sandbox64_defconfig        | 1 +
 configs/sandbox_defconfig          | 1 +
 configs/sandbox_flattree_defconfig | 1 +
 configs/sandbox_noblk_defconfig    | 1 +
 configs/sandbox_spl_defconfig      | 1 +
 5 files changed, 5 insertions(+)

Can you move this to an 'imply' in arch/Kconfig?

Regards,
Simon

#27 lore raw

PATCH v2 11/15 configs: sandbox: enable CONFIG_TEE (TEE uclass)

Quoted 11 lines · Simon Glass
Quoted 9 lines · Jens Wiklander
---
 configs/sandbox64_defconfig        | 1 +
 configs/sandbox_defconfig          | 1 +
 configs/sandbox_flattree_defconfig | 1 +
 configs/sandbox_noblk_defconfig    | 1 +
 configs/sandbox_spl_defconfig      | 1 +
 5 files changed, 5 insertions(+)
Quoted 1 line

Can you move this to an 'imply' in arch/Kconfig?

Will do.

Thanks,
Jens

#28 lore raw

PATCH v2 12/15 arm: dt: hikey: Add optee node

In reply to Jens Wiklander

Sync with 14e21cb8f811 ("arm64: dt: hikey: Add optee node" from Linux kernel.

---
 arch/arm/dts/hi6220-hikey.dts | 7 +++++++
 1 file changed, 7 insertions(+)
arch/arm/dts/hi6220-hikey.dts +7 -0
index 818525197508..24f09257af00 100644
@@ -31,6 +31,13 @@
 		device_type = "memory";
 		reg = <0x0 0x0 0x0 0x40000000>;
 	};
+
+	firmware {
+		optee {
+			compatible = "linaro,optee-tz";
+			method = "smc";
+		};
+	};
 };
 
 &uart2 {
Signature · 1 line
2.17.1
#30 lore raw

PATCH v2 13/15 optee: support routing of rpmb data frames to mmc

In reply to Jens Wiklander

Adds support in optee supplicant to route signed (MACed) RPMB frames from OP-TEE Secure OS to MMC and vice versa to manipulate the RPMB partition.

---
 drivers/tee/optee/Makefile        |   1 +
 drivers/tee/optee/core.c          |   8 ++
 drivers/tee/optee/optee_private.h |  31 ++++-
 drivers/tee/optee/rpmb.c          | 184 ++++++++++++++++++++++++++++++
 drivers/tee/optee/supplicant.c    |   3 +
 5 files changed, 226 insertions(+), 1 deletion(-)
 create mode 100644 drivers/tee/optee/rpmb.c
drivers/tee/optee/Makefile +1 -0
index 6148feb474a5..928d3f80027f 100644
@@ -2,3 +2,4 @@
 
 obj-y += core.o
 obj-y += supplicant.o
+obj-$(CONFIG_SUPPORT_EMMC_RPMB) += rpmb.o
drivers/tee/optee/core.c +8 -0
index f2d92d96551b..e297d206af3a 100644
@@ -280,6 +280,13 @@ static u32 do_call_with_arg(struct udevice *dev, struct optee_msg_arg *arg)
 			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);
 		}
 	}
@@ -611,4 +618,5 @@ U_BOOT_DRIVER(optee) = {
 	.probe = optee_probe,
 	.ops = &optee_ops,
 	.platdata_auto_alloc_size = sizeof(struct optee_pdata),
+	.priv_auto_alloc_size = sizeof(struct optee_private),
 };
drivers/tee/optee/optee_private.h +30 -1
index daa470f812a9..b76979d21011 100644
@@ -6,7 +6,36 @@
 #ifndef __OPTEE_PRIVATE_H
 #define __OPTEE_PRIVATE_H
 
+#include <tee.h>
+#include <log.h>
+
+struct optee_private {
+	struct mmc *rpmb_mmc;
+	int rpmb_dev_id;
+	char 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
+void optee_suppl_cmd_rpmb(struct udevice *dev, struct optee_msg_arg *arg);
+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
+
 void *optee_alloc_and_init_page_list(void *buf, ulong len, u64 *phys_buf_ptr);
-void optee_suppl_cmd(struct udevice *dev, void *shm, void **page_list);
 
 #endif /*__OPTEE_PRIVATE_H*/
drivers/tee/optee/rpmb.c new file +184 -0

184 lines not on this page · raw message

drivers/tee/optee/supplicant.c +3 -0
index 6965055bd1b5..14cb8717522c 100644
@@ -81,6 +81,9 @@ void optee_suppl_cmd(struct udevice *dev, struct tee_shm *shm_arg,
 		debug("OPTEE_MSG_RPC_CMD_FS not implemented\n");
 		arg->ret = TEE_ERROR_NOT_IMPLEMENTED;
 		break;
+	case OPTEE_MSG_RPC_CMD_RPMB:
+		optee_suppl_cmd_rpmb(dev, arg);
+		break;
 	default:
 		arg->ret = TEE_ERROR_NOT_IMPLEMENTED;
 	}
Signature · 1 line
2.17.1
#31 lore raw

PATCH v2 13/15 optee: support routing of rpmb data frames to mmc

Hi Jens,

Quoted 53 lines · Jens Wiklander

Adds support in optee supplicant to route signed (MACed) RPMB frames from OP-TEE Secure OS to MMC and vice versa to manipulate the RPMB partition.

---
 drivers/tee/optee/Makefile        |   1 +
 drivers/tee/optee/core.c          |   8 ++
 drivers/tee/optee/optee_private.h |  31 ++++-
 drivers/tee/optee/rpmb.c          | 184 ++++++++++++++++++++++++++++++
 drivers/tee/optee/supplicant.c    |   3 +
 5 files changed, 226 insertions(+), 1 deletion(-)
 create mode 100644 drivers/tee/optee/rpmb.c
drivers/tee/optee/Makefile +1 -0
index 6148feb474a5..928d3f80027f 100644
@@ -2,3 +2,4 @@
 
 obj-y += core.o
 obj-y += supplicant.o
+obj-$(CONFIG_SUPPORT_EMMC_RPMB) += rpmb.o
drivers/tee/optee/core.c +8 -0
index f2d92d96551b..e297d206af3a 100644
@@ -280,6 +280,13 @@ static u32 do_call_with_arg(struct udevice *dev, struct optee_msg_arg *arg)
                        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);
                }
        }
@@ -611,4 +618,5 @@ U_BOOT_DRIVER(optee) = {
        .probe = optee_probe,
        .ops = &optee_ops,
        .platdata_auto_alloc_size = sizeof(struct optee_pdata),
+       .priv_auto_alloc_size = sizeof(struct optee_private),
 };
drivers/tee/optee/optee_private.h +0 -0
index daa470f812a9..b76979d21011 100644

drivers/tee/optee/optee_private.h line 12

Quoted 7 lines
drivers/tee/optee/optee_private.h +4 -0
@@ -6,7 +6,36 @@
 #ifndef __OPTEE_PRIVATE_H
 #define __OPTEE_PRIVATE_H
 
+#include <tee.h>
+#include <log.h>
+
+struct optee_private {

doc comment

drivers/tee/optee/optee_private.h line 15

Quoted 3 lines
drivers/tee/optee/optee_private.h +3 -0
+       struct mmc *rpmb_mmc;
+       int rpmb_dev_id;
+       char rpmb_original_part;

Why is this char? Are you trying to save memory? I doubt it will work :-)

drivers/tee/optee/optee_private.h line 21

Quoted 6 lines
drivers/tee/optee/optee_private.h +6 -0
+};
+
+struct optee_msg_arg;
+
+void optee_suppl_cmd(struct udevice *dev, struct tee_shm *shm_arg,
+                    void **page_list);

Function comments.

Quoted 100 lines
drivers/tee/optee/optee_private.h +17 -1
+
+#ifdef CONFIG_SUPPORT_EMMC_RPMB
+void optee_suppl_cmd_rpmb(struct udevice *dev, struct optee_msg_arg *arg);
+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
+
 void *optee_alloc_and_init_page_list(void *buf, ulong len, u64 *phys_buf_ptr);
-void optee_suppl_cmd(struct udevice *dev, void *shm, void **page_list);
 
 #endif /*__OPTEE_PRIVATE_H*/
drivers/tee/optee/rpmb.c new file +73 -0
new file mode 100644
index 000000000000..c1447a5561c2
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
+#include <common.h>
+#include <log.h>
+#include <tee.h>
+#include <mmc.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)) {

drivers/tee/optee/rpmb.c line 83

Quoted 10 lines
drivers/tee/optee/rpmb.c new file +10 -0
+               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;
+       }
+
+#ifdef CONFIG_BLK

You shouldn't support !CONFIG_BLK. It is going away in the next release.

Quoted 116 lines
drivers/tee/optee/rpmb.c new file +101 -0
+       priv->rpmb_original_part = mmc_get_blk_desc(mmc)->hwpart;
+#else
+       priv->rpmb_original_part = mmc->block_dev.hwpart;
+#endif
+
+       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);
+
+       if (!mmc)
+               return TEE_ERROR_ITEM_NOT_FOUND;
+
+       if (!mmc->ext_csd)
+               return TEE_ERROR_GENERIC;
+
+       memcpy(info->cid, mmc->cid, sizeof(info->cid));
+       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));
+}
drivers/tee/optee/supplicant.c +3 -1
index 6965055bd1b5..14cb8717522c 100644
@@ -81,6 +81,9 @@ void optee_suppl_cmd(struct udevice *dev, struct tee_shm *shm_arg,
                debug("OPTEE_MSG_RPC_CMD_FS not implemented\n");
                arg->ret = TEE_ERROR_NOT_IMPLEMENTED;
                break;
+       case OPTEE_MSG_RPC_CMD_RPMB:
+               optee_suppl_cmd_rpmb(dev, arg);
+               break;
        default:
                arg->ret = TEE_ERROR_NOT_IMPLEMENTED;
        }
--
Quoted 2 lines

2.17.1

Regards,
Simon

#32 lore raw

PATCH v2 13/15 optee: support routing of rpmb data frames to mmc

Hi Simon,

Quoted 59 lines · Simon Glass

Hi Jens,

Quoted 56 lines · Jens Wiklander

Adds support in optee supplicant to route signed (MACed) RPMB frames from OP-TEE Secure OS to MMC and vice versa to manipulate the RPMB partition.

---
 drivers/tee/optee/Makefile        |   1 +
 drivers/tee/optee/core.c          |   8 ++
 drivers/tee/optee/optee_private.h |  31 ++++-
 drivers/tee/optee/rpmb.c          | 184 ++++++++++++++++++++++++++++++
 drivers/tee/optee/supplicant.c    |   3 +
 5 files changed, 226 insertions(+), 1 deletion(-)
 create mode 100644 drivers/tee/optee/rpmb.c
drivers/tee/optee/Makefile +1 -0
index 6148feb474a5..928d3f80027f 100644
@@ -2,3 +2,4 @@
 
 obj-y += core.o
 obj-y += supplicant.o
+obj-$(CONFIG_SUPPORT_EMMC_RPMB) += rpmb.o
drivers/tee/optee/core.c +8 -0
index f2d92d96551b..e297d206af3a 100644
@@ -280,6 +280,13 @@ static u32 do_call_with_arg(struct udevice *dev, struct optee_msg_arg *arg)
                        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);
                }
        }
@@ -611,4 +618,5 @@ U_BOOT_DRIVER(optee) = {
        .probe = optee_probe,
        .ops = &optee_ops,
        .platdata_auto_alloc_size = sizeof(struct optee_pdata),
+       .priv_auto_alloc_size = sizeof(struct optee_private),
 };
drivers/tee/optee/optee_private.h +0 -0
index daa470f812a9..b76979d21011 100644
@@ -6,7 +6,36 @@
 #ifndef __OPTEE_PRIVATE_H
 #define __OPTEE_PRIVATE_H
 
Quoted 6 lines

drivers/tee/optee/optee_private.h line 12

Quoted 4 lines
drivers/tee/optee/optee_private.h +4 -0
+#include <tee.h>
+#include <log.h>
+
+struct optee_private {

doc comment

OK, I'll fix.

Quoted 6 lines

drivers/tee/optee/optee_private.h line 15

Quoted 4 lines
drivers/tee/optee/optee_private.h +3 -0
+       struct mmc *rpmb_mmc;
+       int rpmb_dev_id;
+       char rpmb_original_part;

Why is this char? Are you trying to save memory? I doubt it will work :-)

Good question. I think I got it from struct blk_desc::hwpart, but still got it slightly wrong. I'll change it into an int instead.

Quoted 115 lines

drivers/tee/optee/optee_private.h line 21

Quoted 8 lines
drivers/tee/optee/optee_private.h +6 -0
+};
+
+struct optee_msg_arg;
+
+void optee_suppl_cmd(struct udevice *dev, struct tee_shm *shm_arg,
+                    void **page_list);

Function comments.

Quoted 104 lines
drivers/tee/optee/optee_private.h +17 -1
+
+#ifdef CONFIG_SUPPORT_EMMC_RPMB
+void optee_suppl_cmd_rpmb(struct udevice *dev, struct optee_msg_arg *arg);
+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
+
 void *optee_alloc_and_init_page_list(void *buf, ulong len, u64 *phys_buf_ptr);
-void optee_suppl_cmd(struct udevice *dev, void *shm, void **page_list);
 
 #endif /*__OPTEE_PRIVATE_H*/
drivers/tee/optee/rpmb.c new file +77 -0
new file mode 100644
index 000000000000..c1447a5561c2
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2018 Linaro Limited
+ */
+
+#include <common.h>
+#include <log.h>
+#include <tee.h>
+#include <mmc.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) {
Quoted 8 lines

drivers/tee/optee/rpmb.c line 83

Quoted 6 lines
drivers/tee/optee/rpmb.c new file +6 -0
+               debug("Device id %d: RPMB not supported before version 4.41\n",
+                     dev_id);
+               return NULL;
+       }
+
+#ifdef CONFIG_BLK

You shouldn't support !CONFIG_BLK. It is going away in the next release.

OK, I'll remove the ifdef and just keep the
mmc_get_blk_desc(mmc)->hwpart line then.

Quoted 111 lines
Quoted 111 lines
drivers/tee/optee/rpmb.c new file +101 -0
+       priv->rpmb_original_part = mmc_get_blk_desc(mmc)->hwpart;
+#else
+       priv->rpmb_original_part = mmc->block_dev.hwpart;
+#endif
+
+       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);
+
+       if (!mmc)
+               return TEE_ERROR_ITEM_NOT_FOUND;
+
+       if (!mmc->ext_csd)
+               return TEE_ERROR_GENERIC;
+
+       memcpy(info->cid, mmc->cid, sizeof(info->cid));
+       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));
+}
drivers/tee/optee/supplicant.c +1 -0
index 6965055bd1b5..14cb8717522c 100644
@@ -81,6 +81,9 @@ void optee_suppl_cmd(struct udevice *dev, struct tee_shm *shm_arg,
                debug("OPTEE_MSG_RPC_CMD_FS not implemented\n");
                arg->ret = TEE_ERROR_NOT_IMPLEMENTED;
                break;
+       case OPTEE_MSG_RPC_CMD_RPMB:
Quoted 8 lines
Quoted 8 lines
drivers/tee/optee/supplicant.c +2 -1
+               optee_suppl_cmd_rpmb(dev, arg);
+               break;
        default:
                arg->ret = TEE_ERROR_NOT_IMPLEMENTED;
        }
--

2.17.1

Thanks for the review,
Jens

#33 lore raw

PATCH v2 14/15 tee: optee: support AVB trusted application

In reply to Jens Wiklander

Adds configuration option OPTEE_TA_AVB and a header file describing the interface to the AVB trusted application provided by OP-TEE.

---
 MAINTAINERS                |  1 +
 drivers/tee/optee/Kconfig  | 16 +++++++++++++
 include/tee.h              |  7 ++++++
 include/tee/optee_ta_avb.h | 48 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+)
 create mode 100644 include/tee/optee_ta_avb.h
MAINTAINERS +1 -0
index 7458c606ee92..cb36c45d74ea 100644
@@ -576,6 +576,7 @@ M:	Jens Wiklander <[email protected]>
 S:	Maintained
 F:	drivers/tee/
 F:	include/tee.h
+F:	include/tee/
 
 UBI
 M:	Kyungmin Park <[email protected]>
drivers/tee/optee/Kconfig +16 -0
index 8f7ebe161111..a5dc08439629 100644
@@ -5,3 +5,19 @@ config OPTEE
 	help
 	  This implements the OP-TEE Trusted Execution Environment (TEE)
 	  driver.
+
+if OPTEE
+
+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".
+
+endmenu
+
+endif
include/tee.h +7 -0
index 3e6771123ef0..b851d718d32f 100644
@@ -48,6 +48,13 @@
 
 #define TEE_ORIGIN_COMMS		0x00000002
 
+struct tee_optee_ta_uuid {
+	u32 time_low;
+	u16 time_mid;
+	u16 time_hi_and_version;
+	u8 clock_seq_and_node[8];
+};
+
 /**
  * struct tee_shm - memory shared with the TEE
  * @dev:	The TEE device
include/tee/optee_ta_avb.h new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.17.1
#34 lore raw

PATCH v2 14/15 tee: optee: support AVB trusted application

Hi Jens,

Quoted 2 lines · Jens Wiklander

Adds configuration option OPTEE_TA_AVB and a header file describing the interface to the AVB trusted application provided by OP-TEE.

What is AVB? Can you please write it out in full?

Quoted 53 lines
---
 MAINTAINERS                |  1 +
 drivers/tee/optee/Kconfig  | 16 +++++++++++++
 include/tee.h              |  7 ++++++
 include/tee/optee_ta_avb.h | 48 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+)
 create mode 100644 include/tee/optee_ta_avb.h
MAINTAINERS +1 -0
index 7458c606ee92..cb36c45d74ea 100644
@@ -576,6 +576,7 @@ M:  Jens Wiklander <[email protected]>
 S:     Maintained
 F:     drivers/tee/
 F:     include/tee.h
+F:     include/tee/
 
 UBI
 M:     Kyungmin Park <[email protected]>
drivers/tee/optee/Kconfig +16 -0
index 8f7ebe161111..a5dc08439629 100644
@@ -5,3 +5,19 @@ config OPTEE
        help
          This implements the OP-TEE Trusted Execution Environment (TEE)
          driver.
+
+if OPTEE
+
+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".
+
+endmenu
+
+endif
include/tee.h +0 -0
index 3e6771123ef0..b851d718d32f 100644

include/tee.h line 51

Quoted 4 lines
include/tee.h +1 -0
@@ -48,6 +48,13 @@
 
 #define TEE_ORIGIN_COMMS               0x00000002
 
+struct tee_optee_ta_uuid {

Comment on this struct. What is it for?

Quoted 53 lines
include/tee.h +6 -0
+       u32 time_low;
+       u16 time_mid;
+       u16 time_hi_and_version;
+       u8 clock_seq_and_node[8];
+};
+
 /**
  * struct tee_shm - memory shared with the TEE
  * @dev:       The TEE device
include/tee/optee_ta_avb.h new file +38 -0
new file mode 100644
index 000000000000..0e1da084e09d
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2018, Linaro Limited */
+
+#ifndef __TA_AVB_H
+#define __TA_AVB_H
+
+#define TA_AVB_UUID { 0x023f8f1a, 0x292a, 0x432b, \
+                     { 0x8f, 0xc4, 0xde, 0x84, 0x71, 0x35, 0x80, 0x67 } }
+
+#define TA_AVB_MAX_ROLLBACK_LOCATIONS  256
+
+/*
+ * Gets the rollback index corresponding to the given rollback index slot.
+ *
+ * in  params[0].value.a:      rollback index slot
+ * out params[1].value.a:      upper 32 bits of rollback index
+ * out params[1].value.b:      lower 32 bits of rollback index
+ */
+#define TA_AVB_CMD_READ_ROLLBACK_INDEX 0
+
+/*
+ * Updates the rollback index corresponding to the given rollback index slot.
+ *
+ * Will refuse to update a slot with a lower value.
+ *
+ * in  params[0].value.a:      rollback index slot
+ * in  params[1].value.a:      upper 32 bits of rollback index
+ * in  params[1].value.b:      lower 32 bits of rollback index
+ */
+#define TA_AVB_CMD_WRITE_ROLLBACK_INDEX        1
+
+/*
+ * Gets the lock state of the device.
+ *
+ * out params[0].value.a:      lock state
+ */
+#define TA_AVB_CMD_READ_LOCK_STATE     2
+

include/tee/optee_ta_avb.h line 48

Quoted 10 lines
include/tee/optee_ta_avb.h new file +10 -0
+/*
+ * Sets the lock state of the device.
+ *
+ * If the lock state is changed all rollback slots will be reset to 0
+ *
+ * in  params[0].value.a:      lock state
+ */
+#define TA_AVB_CMD_WRITE_LOCK_STATE    3
+
+#endif /*__TA_AVB_H*/

Space before */

Quoted 3 lines

--
2.17.1

Regards,
Simon

#35 lore raw

PATCH v2 14/15 tee: optee: support AVB trusted application

Hi Simon,

Quoted 7 lines · Simon Glass

Hi Jens,

Quoted 2 lines · Jens Wiklander

Adds configuration option OPTEE_TA_AVB and a header file describing the interface to the AVB trusted application provided by OP-TEE.

What is AVB? Can you please write it out in full?

AVB stands for Android Verified Boot 2.0. However, Google is a bit picky about how the name Android is used so I'm trying to avoid using it as much as possible to stay out of trouble. I'll write it out in the commit message.

Quoted 55 lines
Quoted 55 lines
---
 MAINTAINERS                |  1 +
 drivers/tee/optee/Kconfig  | 16 +++++++++++++
 include/tee.h              |  7 ++++++
 include/tee/optee_ta_avb.h | 48 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+)
 create mode 100644 include/tee/optee_ta_avb.h
MAINTAINERS +1 -0
index 7458c606ee92..cb36c45d74ea 100644
@@ -576,6 +576,7 @@ M:  Jens Wiklander <[email protected]>
 S:     Maintained
 F:     drivers/tee/
 F:     include/tee.h
+F:     include/tee/
 
 UBI
 M:     Kyungmin Park <[email protected]>
drivers/tee/optee/Kconfig +16 -0
index 8f7ebe161111..a5dc08439629 100644
@@ -5,3 +5,19 @@ config OPTEE
        help
          This implements the OP-TEE Trusted Execution Environment (TEE)
          driver.
+
+if OPTEE
+
+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".
+
+endmenu
+
+endif
include/tee.h +0 -0
index 3e6771123ef0..b851d718d32f 100644
@@ -48,6 +48,13 @@
 
Quoted 5 lines

include/tee.h line 51

Quoted 3 lines
include/tee.h +1 -0
 #define TEE_ORIGIN_COMMS               0x00000002
 
+struct tee_optee_ta_uuid {

Comment on this struct. What is it for?

I'll fix.

Quoted 58 lines
Quoted 58 lines
include/tee.h +6 -0
+       u32 time_low;
+       u16 time_mid;
+       u16 time_hi_and_version;
+       u8 clock_seq_and_node[8];
+};
+
 /**
  * struct tee_shm - memory shared with the TEE
  * @dev:       The TEE device
include/tee/optee_ta_avb.h new file +42 -0
new file mode 100644
index 000000000000..0e1da084e09d
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2018, Linaro Limited */
+
+#ifndef __TA_AVB_H
+#define __TA_AVB_H
+
+#define TA_AVB_UUID { 0x023f8f1a, 0x292a, 0x432b, \
+                     { 0x8f, 0xc4, 0xde, 0x84, 0x71, 0x35, 0x80, 0x67 } }
+
+#define TA_AVB_MAX_ROLLBACK_LOCATIONS  256
+
+/*
+ * Gets the rollback index corresponding to the given rollback index slot.
+ *
+ * in  params[0].value.a:      rollback index slot
+ * out params[1].value.a:      upper 32 bits of rollback index
+ * out params[1].value.b:      lower 32 bits of rollback index
+ */
+#define TA_AVB_CMD_READ_ROLLBACK_INDEX 0
+
+/*
+ * Updates the rollback index corresponding to the given rollback index slot.
+ *
+ * Will refuse to update a slot with a lower value.
+ *
+ * in  params[0].value.a:      rollback index slot
+ * in  params[1].value.a:      upper 32 bits of rollback index
+ * in  params[1].value.b:      lower 32 bits of rollback index
+ */
+#define TA_AVB_CMD_WRITE_ROLLBACK_INDEX        1
+
+/*
+ * Gets the lock state of the device.
+ *
+ * out params[0].value.a:      lock state
+ */
+#define TA_AVB_CMD_READ_LOCK_STATE     2
+
+/*
+ * Sets the lock state of the device.
+ *
+ * If the lock state is changed all rollback slots will be reset to 0
Quoted 12 lines

include/tee/optee_ta_avb.h line 48

Quoted 6 lines
include/tee/optee_ta_avb.h new file +6 -0
+ *
+ * in  params[0].value.a:      lock state
+ */
+#define TA_AVB_CMD_WRITE_LOCK_STATE    3
+
+#endif /*__TA_AVB_H*/

Space before */

Quoted 3 lines

--
2.17.1

Thanks for the review,
Jens

#36 lore raw

PATCH v2 14/15 tee: optee: support AVB trusted application

Hi Jens,

Quoted 10 lines · Jens Wiklander

Hi Simon,

Quoted 6 lines · Simon Glass

Hi Jens,

Quoted 2 lines · Jens Wiklander

Adds configuration option OPTEE_TA_AVB and a header file describing the interface to the AVB trusted application provided by OP-TEE.

Quoted 6 lines
Quoted 1 line

What is AVB? Can you please write it out in full?

AVB stands for Android Verified Boot 2.0. However, Google is a bit picky about how the name Android is used so I'm trying to avoid using it as much as possible to stay out of trouble. I'll write it out in the commit message.

I think it should be somewhere in the docs at least. It looks like this is what you need to follow:

https://developer.android.com/distribute/marketing-tools/brand-guidelines

Regards,
Simon

#37 lore raw

PATCH v2 15/15 avb_verify: support using OP-TEE TA AVB

In reply to Jens Wiklander

With CONFIG_OPTEE_TA_AVB use the trusted application AVB provided by OP-TEE to manage rollback indexes and device lock status.

---
 common/avb_verify.c  | 132 ++++++++++++++++++++++++++++++++++++++++++-
 doc/README.avb2      |  13 +++++
 include/avb_verify.h |   4 ++
 3 files changed, 148 insertions(+), 1 deletion(-)
common/avb_verify.c +131 -1
index 20e35ade3029..a50c05bf7847 100644
@@ -10,6 +10,8 @@
 #include <image.h>
 #include <malloc.h>
 #include <part.h>
+#include <tee.h>
+#include <tee/optee_ta_avb.h>
 
 const unsigned char avb_root_pub[1032] = {
 	0x0, 0x0, 0x10, 0x0, 0x55, 0xd9, 0x4, 0xad, 0xd8, 0x4,
@@ -594,6 +596,79 @@ static AvbIOResult validate_vbmeta_public_key(AvbOps *ops,
 	return AVB_IO_RESULT_OK;
 }
 
+#ifdef CONFIG_OPTEE_TA_AVB
+static void 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));
+}
+
+static int get_open_session(struct AvbOpsData *ops_data)
+{
+	struct udevice *tee = NULL;
+
+	while (!ops_data->tee) {
+		const struct tee_optee_ta_uuid uuid = TA_AVB_UUID;
+		struct tee_open_session_arg arg;
+		int rc;
+
+		tee = tee_find_device(tee, NULL, NULL, NULL);
+		if (!tee)
+			return -ENODEV;
+
+		memset(&arg, 0, sizeof(arg));
+		uuid_to_octets(arg.uuid, &uuid);
+		rc = tee_open_session(tee, &arg, 0, NULL);
+		if (!rc) {
+			ops_data->tee = tee;
+			ops_data->session = arg.session;
+		}
+	}
+
+	return 0;
+}
+
+static AvbIOResult invoke_func(struct AvbOpsData *ops_data, u32 func,
+			       ulong num_param, struct tee_param *param)
+{
+	struct tee_invoke_arg arg;
+
+	if (get_open_session(ops_data))
+		return AVB_IO_RESULT_ERROR_IO;
+
+	memset(&arg, 0, sizeof(arg));
+	arg.func = func;
+	arg.session = ops_data->session;
+
+	if (tee_invoke_func(ops_data->tee, &arg, num_param, param))
+		return AVB_IO_RESULT_ERROR_IO;
+	switch (arg.ret) {
+	case TEE_SUCCESS:
+		return AVB_IO_RESULT_OK;
+	case TEE_ERROR_OUT_OF_MEMORY:
+		return AVB_IO_RESULT_ERROR_OOM;
+	case TEE_ERROR_TARGET_DEAD:
+		/*
+		 * The TA has paniced, close the session to reload the TA
+		 * for the next request.
+		 */
+		tee_close_session(ops_data->tee, ops_data->session);
+		ops_data->tee = NULL;
+		return AVB_IO_RESULT_ERROR_IO;
+	default:
+		return AVB_IO_RESULT_ERROR_IO;
+	}
+}
+#endif
+
 /**
  * read_rollback_index() - gets the rollback index corresponding to the
  * location of given by @out_rollback_index.
@@ -609,6 +684,7 @@ static AvbIOResult read_rollback_index(AvbOps *ops,
 				       size_t rollback_index_slot,
 				       u64 *out_rollback_index)
 {
+#ifndef CONFIG_OPTEE_TA_AVB
 	/* For now we always return 0 as the stored rollback index. */
 	printf("%s not supported yet\n", __func__);
 
@@ -616,6 +692,27 @@ static AvbIOResult read_rollback_index(AvbOps *ops,
 		*out_rollback_index = 0;
 
 	return AVB_IO_RESULT_OK;
+#else
+	AvbIOResult rc;
+	struct tee_param param[2];
+
+	if (rollback_index_slot >= TA_AVB_MAX_ROLLBACK_LOCATIONS)
+		return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
+
+	memset(param, 0, sizeof(param));
+	param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
+	param[0].u.value.a = rollback_index_slot;
+	param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT;
+
+	rc = invoke_func(ops->user_data, TA_AVB_CMD_READ_ROLLBACK_INDEX,
+			 ARRAY_SIZE(param), param);
+	if (rc)
+		return rc;
+
+	*out_rollback_index = (u64)param[1].u.value.a << 32 |
+			      (u32)param[1].u.value.b;
+	return AVB_IO_RESULT_OK;
+#endif
 }
 
 /**
@@ -633,10 +730,27 @@ static AvbIOResult write_rollback_index(AvbOps *ops,
 					size_t rollback_index_slot,
 					u64 rollback_index)
 {
+#ifndef CONFIG_OPTEE_TA_AVB
 	/* For now this is a no-op. */
 	printf("%s not supported yet\n", __func__);
 
 	return AVB_IO_RESULT_OK;
+#else
+	struct tee_param param[2];
+
+	if (rollback_index_slot >= TA_AVB_MAX_ROLLBACK_LOCATIONS)
+		return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
+
+	memset(param, 0, sizeof(param));
+	param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
+	param[0].u.value.a = rollback_index_slot;
+	param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
+	param[1].u.value.a = (u32)(rollback_index >> 32);
+	param[1].u.value.b = (u32)rollback_index;
+
+	return invoke_func(ops->user_data, TA_AVB_CMD_WRITE_ROLLBACK_INDEX,
+			   ARRAY_SIZE(param), param);
+#endif
 }
 
 /**
@@ -652,6 +766,7 @@ static AvbIOResult write_rollback_index(AvbOps *ops,
  */
 static AvbIOResult read_is_device_unlocked(AvbOps *ops, bool *out_is_unlocked)
 {
+#ifndef CONFIG_OPTEE_TA_AVB
 	/* For now we always return that the device is unlocked. */
 
 	printf("%s not supported yet\n", __func__);
@@ -659,6 +774,16 @@ static AvbIOResult read_is_device_unlocked(AvbOps *ops, bool *out_is_unlocked)
 	*out_is_unlocked = true;
 
 	return AVB_IO_RESULT_OK;
+#else
+	AvbIOResult rc;
+	struct tee_param param = { .attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT };
+
+	rc = invoke_func(ops->user_data, TA_AVB_CMD_READ_LOCK_STATE, 1, &param);
+	if (rc)
+		return rc;
+	*out_is_unlocked = !param.u.value.a;
+	return AVB_IO_RESULT_OK;
+#endif
 }
 
 /**
@@ -737,6 +862,11 @@ void avb_ops_free(AvbOps *ops)
 
 	ops_data = ops->user_data;
 
-	if (ops_data)
+	if (ops_data) {
+#ifdef CONFIG_OPTEE_TA_AVB
+		if (ops_data->tee)
+			tee_close_session(ops_data->tee, ops_data->session);
+#endif
 		avb_free(ops_data);
+	}
 }
doc/README.avb2 +13 -0
index 120279fedbe2..a29cee1b6f50 100644
@@ -18,6 +18,13 @@ Integrity of the bootloader (U-boot BLOB and environment) is out of scope.
 For additional details check:
 https://android.googlesource.com/platform/external/avb/+/master/README.md
 
+1.1. AVB using OP-TEE (optional)
+---------------------------------
+If AVB is configured to use OP-TEE (see 4. below) rollback indexes and
+device lock state are stored in RPMB. The RPMB partition is managed by
+OP-TEE (https://www.op-tee.org/) which is a secure OS leveraging ARM
+TrustZone.
+
 
 2. AVB 2.0 U-BOOT SHELL COMMANDS
 -----------------------------------
@@ -61,6 +68,12 @@ CONFIG_LIBAVB=y
 CONFIG_AVB_VERIFY=y
 CONFIG_CMD_AVB=y
 
+In addtion optionally if storing rollback indexes in RPMB with help of
+OP-TEE:
+CONFIG_TEE=y
+CONFIG_OPTEE=y
+CONFIG_OPTEE_TA_AVB=y
+CONFIG_SUPPORT_EMMC_RPMB=y
 
 Then add `avb verify` invocation to your android boot sequence of commands,
 e.g.:
include/avb_verify.h +4 -0
index eaa60f5393ef..a532a2331aea 100644
@@ -27,6 +27,10 @@ struct AvbOpsData {
 	struct AvbOps ops;
 	int mmc_dev;
 	enum avb_boot_state boot_state;
+#ifdef CONFIG_OPTEE_TA_AVB
+	struct udevice *tee;
+	u32 session;
+#endif
 };
 
 struct mmc_part {
Signature · 1 line
2.17.1