From patchwork Thu Aug 23 10:43:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 144914 Delivered-To: patches@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp30646ljw; Thu, 23 Aug 2018 03:43:53 -0700 (PDT) X-Received: by 2002:a2e:144f:: with SMTP id 15-v6mr41863581lju.122.1535021033027; Thu, 23 Aug 2018 03:43:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535021033; cv=none; d=google.com; s=arc-20160816; b=hXb3WR2PxC2v5mXIabHp0dWfwGfZ757G7IDdzdKl+vsvAaKwpOzDFMuld3ai4JR+C9 TknRE3cLqYGgYDA+aMzQOHpqsVBWBu7slE2QuvBQlYS/0GMlAIcOCrm0NN4FFvlwAw7n znig+I/jzG8ABsaDgECvE8HC2Kmpry47Kkguo5eZzhmL1g4uCUM87TksIxOn6BoZfz3M ycB7lGF2d+YpDtV0LshKGj30mk6uke1AOeLCN4jmkEepqgGQ6I1Tl6UXgdvrwcJNIqBC 9Lz/e+QIVfiXX0OOPv7SrkcyYS9LKHmieRIbGXxkhZvNDvqhB4XzvCdMZZsVcG/6VUWg hsEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=R8wsh0/e6a4BeUAQHHKEJ3CQyeqFAddJuZgJMMwghlI=; b=R008rHQOpGbT4Bmq2raqUGWqoBmJSEdnButhtDgfOiZtQDyogEZGLr9XVb4iW3+Jd2 Vdh0nXqf5XqZ9Y/QL/gHpUpn77K5y7hI7FyIahdto6joRcI4sOYm7gAu5jDKDQV247Ic FQWCmcgUX0dywJFXfN0PfMYjhdmfmsMyjWoCLE0VrIODCBw61BRqvNpqAJ+9ilqbHPsB JQABy/BkF1OYi72d3KjzA8+HKJO9AxFnWmnJLbZ9n52LLJyNIkyQ3FG82OPsW7Zt+x3S N2AdCc0GveGMorLJD2to1MlaXBrXjzp23c6KDtmb0xjfPB6nY1MrBuadUnzgAvQbbBel PJ0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EW5ggUQ6; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id y124-v6sor118676lff.131.2018.08.23.03.43.52 for (Google Transport Security); Thu, 23 Aug 2018 03:43:53 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EW5ggUQ6; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=R8wsh0/e6a4BeUAQHHKEJ3CQyeqFAddJuZgJMMwghlI=; b=EW5ggUQ6eyW6znDQgGwNCG723mnziAMZ0cmlNDUrIPywUp92xQk9tzo6K0XW/G5auj WJO/eeT8/EGP3wo2+Bb7mM9Q+WknLE7ZYmZPddwYHf5YCB3kRDAXAxBwk5U/w/AFh4SA nQRMqUMy5iwu4oy995N1QpUd416Kkua2B3WrQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=R8wsh0/e6a4BeUAQHHKEJ3CQyeqFAddJuZgJMMwghlI=; b=MxAMvkMxaiyIpKWpkaYhlbYYFLZ/JyVb3yJPXnsB0TmJV+dPs1+3KrfNOiYvgh3H8K JvtRjyDkTk2SZL0QoPh7ugeCYJsaloE5d3vd0uqwf++5wbT2rGVjY8hJcxtagV322aq+ BcWEa2RcLB0Xylb1ifOTuIjHww83Xj0z37OdHpbFGt928OZZSaXTJtZquzsChaGYwMHU U1U8IR7BpwRjS1zqH7wDrcl1QOaXWjVxSjuyaiHJ6rYzIOQBhmP3x44B53AabUTZ+cpC cuiH/IY+JSQnugUCALYFTmGanDu4rUPG++0O23UIpBVlBAa5mUF1HKAbjzjtrE42WjWR 898Q== X-Gm-Message-State: AOUpUlE4XEM+6s7iRxmDV0vpMCdtMrOzuqwPZbFnM40PMY57tx6F1rhX S5bFgreyTerU6JhhS3emAZxDDxCl X-Google-Smtp-Source: AA+uWPyBB+StZV+6nZE7n/Xlrhv90vkefjRO8LFteiOb6PPDrl76SdRJ3QNqit1GYNFCChz+Wtwo3A== X-Received: by 2002:a19:c94a:: with SMTP id z71-v6mr16759634lff.34.1535021032742; Thu, 23 Aug 2018 03:43:52 -0700 (PDT) Return-Path: Received: from jax.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id y5-v6sm679771ljj.75.2018.08.23.03.43.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Aug 2018 03:43:52 -0700 (PDT) From: Jens Wiklander To: u-boot@lists.denx.de Cc: Simon Glass , Igor Opaniuk , Tom Rini , Jaehoon Chung , Pierre Aubert , Albert Aribaud , Peter Griffin , Michal Simek , Jens Wiklander Subject: [PATCH v2 09/15] test: tee: test TEE uclass Date: Thu, 23 Aug 2018 12:43:28 +0200 Message-Id: <20180823104334.16083-10-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180823104334.16083-1-jens.wiklander@linaro.org> References: <20180823104334.16083-1-jens.wiklander@linaro.org> Tests the TEE uclass with a sandbox tee driver. Signed-off-by: Jens Wiklander --- 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 -- 2.17.1 Reviewed-by: Simon Glass diff --git a/drivers/tee/Kconfig b/drivers/tee/Kconfig index 3e7fe6ddcc5d..032aff4777c6 100644 --- a/drivers/tee/Kconfig +++ b/drivers/tee/Kconfig @@ -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 diff --git a/test/dm/Makefile b/test/dm/Makefile index d2ed96c61533..272374b92fb0 100644 --- a/test/dm/Makefile +++ b/test/dm/Makefile @@ -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 diff --git a/test/dm/tee.c b/test/dm/tee.c new file mode 100644 index 000000000000..bf9db0c130ac --- /dev/null +++ b/test/dm/tee.c @@ -0,0 +1,182 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018 Linaro Limited + */ + +#include +#include +#include +#include +#include + +struct sandbox_tee_state { + u32 session; + int num_shms; +}; + +static void sandbox_tee_get_version(struct udevice *dev, + struct tee_version_data *vers) +{ + memset(vers, 0, sizeof(*vers)); +} + +static int sandbox_tee_close_session(struct udevice *dev, u32 session) +{ + struct sandbox_tee_state *state = dev_get_priv(dev); + + if (!state->session || state->session != session) + return -EINVAL; + + state->session = 0; + + return 0; +} + +static int sandbox_tee_open_session(struct udevice *dev, + struct tee_open_session_arg *arg, + ulong num_params, struct tee_param *params) +{ + struct sandbox_tee_state *state = dev_get_priv(dev); + + if (state->session) + return -EBUSY; + + state->session = 1; + arg->session = state->session; + + return 0; +} + +static int sandbox_tee_invoke_func(struct udevice *dev, + struct tee_invoke_arg *arg, + ulong num_params, struct tee_param *params) +{ + struct sandbox_tee_state *state = dev_get_priv(dev); + + if (!arg->session) + return -EINVAL; + + if (arg->session != state->session) + return -EINVAL; + + if (arg->func != 1) + return -ENOENT; + + return 0; +} + +static int sandbox_tee_shm_register(struct udevice *dev, struct tee_shm *shm) +{ + struct sandbox_tee_state *state = dev_get_priv(dev); + + state->num_shms++; + + return 0; +} + +static int sandbox_tee_shm_unregister(struct udevice *dev, struct tee_shm *shm) +{ + struct sandbox_tee_state *state = dev_get_priv(dev); + + state->num_shms--; + + return 0; +} + +static const struct tee_driver_ops sandbox_tee_ops = { + .get_version = sandbox_tee_get_version, + .open_session = sandbox_tee_open_session, + .close_session = sandbox_tee_close_session, + .invoke_func = sandbox_tee_invoke_func, + .shm_register = sandbox_tee_shm_register, + .shm_unregister = sandbox_tee_shm_unregister, +}; + +static const struct udevice_id sandbox_tee_match[] = { + { .compatible = "sandbox,tee" }, + {}, +}; + +U_BOOT_DRIVER(sandbox_tee) = { + .name = "sandbox_tee", + .id = UCLASS_TEE, + .of_match = sandbox_tee_match, + .ops = &sandbox_tee_ops, + .priv_auto_alloc_size = sizeof(struct sandbox_tee_state), +}; + +static int open_session(struct udevice *dev, u32 *session) +{ + struct tee_open_session_arg arg; + int rc; + + memset(&arg, 0, sizeof(arg)); + rc = tee_open_session(dev, &arg, 0, NULL); + if (rc) + return rc; + if (arg.ret) + return -EIO; + *session = arg.session; + + return 0; +} + +static int invoke_func(struct udevice *dev, u32 session, u32 func) +{ + struct tee_invoke_arg arg = { .session = session, .func = func }; + + return tee_invoke_func(dev, &arg, 0, NULL); +} + +static int match(struct tee_version_data *vers, const void *data) +{ + return !vers->gen_caps; +} + +static int dm_test_tee(struct unit_test_state *uts) +{ + struct tee_version_data vers; + struct udevice *dev; + struct sandbox_tee_state *state; + u32 session; + int rc; + u8 data[128]; + struct tee_shm *reg_shm; + struct tee_shm *alloc_shm; + + dev = tee_find_device(NULL, match, NULL, &vers); + ut_assert(dev); + state = dev_get_priv(dev); + ut_assert(!state->session); + + rc = open_session(dev, &session); + ut_assert(!rc); + ut_assert(session == state->session); + + rc = invoke_func(dev, session, 1); + ut_assert(!rc); + + rc = tee_close_session(dev, session); + ut_assert(!rc); + ut_assert(!state->session); + + ut_assert(!state->num_shms); + reg_shm = tee_shm_register(dev, data, sizeof(data), 0); + ut_assert(reg_shm); + ut_assert(state->num_shms == 1); + + alloc_shm = tee_shm_alloc(dev, 256, 0); + ut_assert(alloc_shm); + ut_assert(state->num_shms == 2); + + ut_assert(tee_shm_is_registered(reg_shm, dev)); + ut_assert(tee_shm_is_registered(alloc_shm, dev)); + + tee_shm_free(reg_shm); + tee_shm_free(alloc_shm); + ut_assert(!state->num_shms); + + return 0; +} + +DM_TEST(dm_test_tee, DM_TESTF_SCAN_FDT);