From patchwork Wed Sep 16 10:00:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaurav Jain X-Patchwork-Id: 312332 Delivered-To: patch@linaro.org Received: by 2002:a92:5ad1:0:0:0:0:0 with SMTP id b78csp336567ilg; Wed, 16 Sep 2020 04:01:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzBV7nb+hhdymz1GZIZgTfrwnEzeNtuQycorarAUN4a9bkzXlKJn22vpukuah8jw2G40Hh7 X-Received: by 2002:a50:875b:: with SMTP id 27mr26487710edv.117.1600254115013; Wed, 16 Sep 2020 04:01:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1600254115; cv=pass; d=google.com; s=arc-20160816; b=c+5j3zbiP/bSpkCl9dXq7UoLvi726gMpqTh4jo2Ic5Kp4sc5+y27uciFW5PXVn2PG5 3f5cUlzKgUkwc3UMb2NCCnapd68c3oDpEEYiBkDl6npDgv6xoIE08vLQoCRZAiLsT78D XklS0SRUTHsrrKoJv66eF7wewhS23s1xH8D//LrlVr5vLkROAOj80DnCsxNP3tZy2zcE B+bGvg3A1DXo7FYcGr/DCtXL0yWgaHXPZ39edyHJWY5hrzRfQsq0ut4iJW1UOMIwNdJo gK4wTYG3WrncWAKaAHYA4H3VPvo0EwdCH/dAkUf+tUmKKE/CcBOz4hzMFvRhkmYH1zjL JvwQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:message-id:date :subject:cc:to:from:dkim-signature; bh=Wmc0zZ1gIu0beOnhgLhG2zm885IO01xS+6pkBb3148Q=; b=QDMSmXzhJe/hBtNRSdypBqEh2oxP7QSe3vnHFKSL1FJZtHfdOS8OKZtn9rE9iLIx0D dkZLJCstjZgr/3zVBFmhJTStKdN3MiKMa+FPLl8iwvKlVoJXfGNJMBym0MDC8UkTBhdE Ukmgm+Z48ry5MJxqSsKfBaKovAVAX9ABSe3O352NWF+tuxg2vSrwU3LYlS1e+amcymSi IL9ZClzcaqau9UrsygHBBus5J/Sej/Uc6M7iChcCFIvOLvOQLqo8Z0thITH/S2YXwNOz azQADlF5H9RI3bVi1dwJbz2QgcL5ICYYzpQbDrrVCqNWqzgr5uFXP0r7VlrWVld6gEaj RUOA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@nxp.com header.s=selector2 header.b="WMB//1UQ"; arc=pass (i=1 spf=pass spfdomain=nxp.com dkim=pass dkdomain=nxp.com dmarc=pass fromdomain=nxp.com); spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id jp1si11504717ejb.371.2020.09.16.04.01.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Sep 2020 04:01:54 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@nxp.com header.s=selector2 header.b="WMB//1UQ"; arc=pass (i=1 spf=pass spfdomain=nxp.com dkim=pass dkdomain=nxp.com dmarc=pass fromdomain=nxp.com); spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2FBF2821F8; Wed, 16 Sep 2020 13:01:53 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=nxp.com header.i=@nxp.com header.b="WMB//1UQ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 58DA3822FF; Wed, 16 Sep 2020 12:01:29 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FORGED_SPF_HELO,MSGID_FROM_MTA_HEADER, SPF_HELO_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from EUR01-DB5-obe.outbound.protection.outlook.com (mail-db5eur01on0601.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe02::601]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 22E1D81260 for ; Wed, 16 Sep 2020 12:01:26 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=gaurav.jain@nxp.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WmYccgvh88LSXIgI+E6U9DW0SRfIxBENLqva5czhgf4fClX997i3F/SeB8wt5SeSql5IslSGVYKtxWnNC7iFk9qgdtYk82/5N5QMjJtLRL3YpximZvuAh0NkjKu1UgOzObKLPMIP3FLP9jyk2yH7lpCflWIDWvIE2RYuaXQQpPZ9ANE9PZbFZZckO69D+EutqsAW+vwAoL8S/GvNl+EpxmtPx7Oejghpqv/mKyWGVbGL/wnfIOrRnaSmXaTF0Dvp4PzSQBrVLYHEKJSPZqjndbJ1t7q63TryAkgZio5qq3FxdXIESTTQS5bZaB0Tbfpzlni8VX6A9Mhozc6WlDrw8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Wmc0zZ1gIu0beOnhgLhG2zm885IO01xS+6pkBb3148Q=; b=KPqxjaeqaMtoCwuMzV1BEEjDdwLsJnn+c6vy3Gh/IS5jqf49/g3AVLBrWB7keAnUT14YqgAUYiLZJbOUQ9qNHBlVQda4EG3rJz78siw9BoOpYymHqLDSqnSKlSgUMzp2aShq3JofCrHfMNpQMhOEbSxNIA+cTxi/3wyQlGgpPLRFgjHPllO7j0XfEg9ENS+fG8zooGuz25YcvlmxC0kMq8tzBPQujumRGZ9zymA89aZWp3ZRsevTITtiZo9cmYwRIXgEYXvM+3+onLnL0wrX+Pig0WgDBMD9DDkO8natzGmTsBN3DRDel+oo2MgLlTQahjOB5+ZPrmn7sX2uuSGrzw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Wmc0zZ1gIu0beOnhgLhG2zm885IO01xS+6pkBb3148Q=; b=WMB//1UQmc44Tust3BRyuz0kTrYJCTHmMMlAIzIDq4Zxlkis+GpzEntbl/4r1KWL4zH8eY3msf3AgUU1cy4DSzRMSYwm693ZETYbadfvZ/lOVn1f1B6h7z9QMxBsfdSLE6OoER/vEquqdKJcYFy0NBrkW8LcfnUYTyFoxm0sQb4= Authentication-Results: lists.denx.de; dkim=none (message not signed) header.d=none; lists.denx.de; dmarc=none action=none header.from=nxp.com; Received: from AM5PR04MB3074.eurprd04.prod.outlook.com (2603:10a6:206:4::16) by AM6PR04MB6408.eurprd04.prod.outlook.com (2603:10a6:20b:fe::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3391.14; Wed, 16 Sep 2020 10:01:24 +0000 Received: from AM5PR04MB3074.eurprd04.prod.outlook.com ([fe80::88b4:6184:c533:7311]) by AM5PR04MB3074.eurprd04.prod.outlook.com ([fe80::88b4:6184:c533:7311%7]) with mapi id 15.20.3370.019; Wed, 16 Sep 2020 10:01:24 +0000 From: Gaurav Jain To: u-boot@lists.denx.de, Priyanka Jain Cc: Ruchika Gupta Subject: [PATCH] cmd: optee_rpmb command for read/write of rpmb from optee Date: Wed, 16 Sep 2020 15:30:58 +0530 Message-Id: <20200916100058.9092-1-gaurav.jain@nxp.com> X-Mailer: git-send-email 2.17.1 X-ClientProxiedBy: SG3P274CA0017.SGPP274.PROD.OUTLOOK.COM (2603:1096:4:be::29) To AM5PR04MB3074.eurprd04.prod.outlook.com (2603:10a6:206:4::16) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from lsv03326.swis.in-blr01.nxp.com (14.142.151.118) by SG3P274CA0017.SGPP274.PROD.OUTLOOK.COM (2603:1096:4:be::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3391.13 via Frontend Transport; Wed, 16 Sep 2020 10:01:22 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [14.142.151.118] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 90679dcc-17a7-4d53-e02b-08d85a277816 X-MS-TrafficTypeDiagnostic: AM6PR04MB6408: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:556; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Z/I87D5iIgzWz6BsPqlrJAXcnmYqOVjlp7Z50ChZz4zLzXqxA5aVLlK4/yfaAE+Uqyd8pFQWoqTMbxrSjZbnEqDi/534rnARr+TZD8mmPUnmmbQO7Mz/SJwXkF36nCtenP579oXtKWnDkslzItLQ11JNwXfQgztVG2t947h6fx6NlBSxQU9AJqdGiHNh8DG5lwp8yMyfpv8MuqpAbF7L/166G+t7yYr4SOlek36QhV9WH0CDsF2l4LdG+XyBJ/hEsTQaFUxtns2p5F5xCZcb8L8GD/ddSITPcoBPkCZZZevTi48xO5qp2GBpgiups/DYYVUScvTAuqMCcc7C3OwDSTZanEX5MPOl/S/ewPPyczYgKDwi6rJa6BIl5AMN2PcM X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM5PR04MB3074.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(39850400004)(396003)(376002)(136003)(346002)(366004)(55236004)(2906002)(1076003)(66476007)(66556008)(66946007)(8936002)(6636002)(52116002)(7696005)(6862004)(8676002)(6486002)(5660300002)(478600001)(4326008)(6666004)(1006002)(956004)(83380400001)(186003)(2616005)(16526019)(44832011)(37006003)(316002)(26005)(86362001)(36756003)(110426005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: lN7WnMRNcwym5QaBrR6wcijE+qce2dpQVQPb7RDYTrCNG5dMkg+kLuqouJyJQywGly5VEN0Zlr7w72f6IfPMWbx4kigpH1RcPAq2ZEI0kMHjjjajuB5AxCkEbGToIgCoM5C8JC9EYOJREDK3fUMFtqcsQRSN/oBsmw7UtytDqo2T22fP2GgHXs2CFCLZl+ieqIg/flpl+JzeRgfTTdJZx25tO8qWfJjpU9+OvyuSI8KO+4L4WaWviib4V/J1MwdexOR71RPWiXIEyx9Cwx8QI4xXfJYnEDiR8+/KeAg/f9fBjqvrcXKhwrK12cm/qXX4SfV0oJW6DTGccQ24fqmyooz7UkSZ16tQMQigZ3t5Gz+lVCBVM50sECNFNDQyLmPepO1Z5Fn5nMkmWOhq2uDbkcOg74p5uKxcTn4OvORxplnlG0vesCKJsWkTsu5qk1XReXs44pSo2eRbmzR+SRSTAxsWqHgK5fWvnE4zt1+AW0DqJ4Ykg70pTXcUSxlnBDW5l4gTymrpZXFR97OK70Rui4apMXa7ZOcQtFWcLZ/KCpqs+DSv3V6CEP6NSdNVgPZFMMKU/KBp6VB1lbuVI9PoP+REbVnZCu0RLLyllTQlLrZfgz9KLc5dQCRqFqw7RA6U1LGzYHr6V+K41iIYk/Ub+A== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 90679dcc-17a7-4d53-e02b-08d85a277816 X-MS-Exchange-CrossTenant-AuthSource: AM5PR04MB3074.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Sep 2020 10:01:24.1322 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: JOEExgrv4RE2L37U/hJlebiDiWJATlqrjgdtlW8WMOeUT0xcNgYEY1MDkjTHHuImdWsIO6VPOZXs+N/kC8thNA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR04MB6408 X-Mailman-Approved-At: Wed, 16 Sep 2020 13:01:52 +0200 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean From: Ruchika Gupta Enable "optee_rpmb" command to write/read named persistent values created on RPMB by opening session with OPTEE AVB TA. This provides easy test for establishing a session with OPTEE TA and storage of persistent data in MMC RPMB. It includes following subcommands: optee_rpmb read_pvalue - read persistent values on rpmb via OPTEE AVB TA optee_rpmb write_pvalue - write persistent values on rpmb via OPTEE AVB TA Signed-off-by: Ruchika Gupta --- cmd/Kconfig | 8 ++ cmd/Makefile | 1 + cmd/optee_rpmb.c | 272 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 281 insertions(+) create mode 100644 cmd/optee_rpmb.c -- 2.17.1 diff --git a/cmd/Kconfig b/cmd/Kconfig index 0761dbb746..f2b631bce3 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -1162,6 +1162,14 @@ config CMD_CLONE initial flashing by external block device without network or usb support. +config CMD_OPTEE_RPMB + bool "Enable read/write support on RPMB via OPTEE" + depends on SUPPORT_EMMC_RPMB && OPTEE + help + Enable the commands for reading, writing persistent named values + in the Replay Protection Memory Block partition in eMMC by + using Persistent Objects in OPTEE + config CMD_MTD bool "mtd" depends on MTD diff --git a/cmd/Makefile b/cmd/Makefile index 3a9c9747c9..acb048d7bf 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -96,6 +96,7 @@ obj-$(CONFIG_CMD_MII) += mii.o obj-$(CONFIG_CMD_MDIO) += mdio.o obj-$(CONFIG_CMD_MISC) += misc.o obj-$(CONFIG_CMD_MMC) += mmc.o +obj-$(CONFIG_CMD_OPTEE_RPMB) += optee_rpmb.o obj-$(CONFIG_MP) += mp.o obj-$(CONFIG_CMD_MTD) += mtd.o obj-$(CONFIG_CMD_MTDPARTS) += mtdparts.o diff --git a/cmd/optee_rpmb.c b/cmd/optee_rpmb.c new file mode 100644 index 0000000000..e13eb45eb3 --- /dev/null +++ b/cmd/optee_rpmb.c @@ -0,0 +1,272 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2020 NXP + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static struct udevice *tee; +static u32 session; + +static int avb_ta_open_session(void) +{ + 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)); + tee_optee_ta_uuid_to_octets(arg.uuid, &uuid); + rc = tee_open_session(tee, &arg, 0, NULL); + if (!rc) + session = arg.session; + + return 0; +} + +static int invoke_func(u32 func, ulong num_param, struct tee_param *param) +{ + struct tee_invoke_arg arg; + + if (!tee) + if (avb_ta_open_session()) + return -ENODEV; + + memset(&arg, 0, sizeof(arg)); + arg.func = func; + arg.session = session; + + if (tee_invoke_func(tee, &arg, num_param, param)) + return -EFAULT; + switch (arg.ret) { + case TEE_SUCCESS: + return 0; + case TEE_ERROR_OUT_OF_MEMORY: + case TEE_ERROR_STORAGE_NO_SPACE: + return -ENOSPC; + case TEE_ERROR_ITEM_NOT_FOUND: + return -EIO; + case TEE_ERROR_TARGET_DEAD: + /* + * The TA has paniced, close the session to reload the TA + * for the next request. + */ + tee_close_session(tee, session); + tee = NULL; + return -EIO; + default: + return -EIO; + } +} + +static int read_persistent_value(const char *name, + size_t buffer_size, + u8 *out_buffer, + size_t *out_num_bytes_read) +{ + int rc = 0; + struct tee_shm *shm_name; + struct tee_shm *shm_buf; + struct tee_param param[2]; + size_t name_size = strlen(name) + 1; + + if (!tee) + if (avb_ta_open_session()) + return -ENODEV; + + rc = tee_shm_alloc(tee, name_size, + TEE_SHM_ALLOC, &shm_name); + if (rc) + return -ENOMEM; + + rc = tee_shm_alloc(tee, buffer_size, + TEE_SHM_ALLOC, &shm_buf); + if (rc) { + rc = -ENOMEM; + goto free_name; + } + + memcpy(shm_name->addr, name, name_size); + + memset(param, 0, sizeof(param)); + param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT; + param[0].u.memref.shm = shm_name; + param[0].u.memref.size = name_size; + param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT; + param[1].u.memref.shm = shm_buf; + param[1].u.memref.size = buffer_size; + + rc = invoke_func(TA_AVB_CMD_READ_PERSIST_VALUE, + 2, param); + if (rc) + goto out; + + if (param[1].u.memref.size > buffer_size) { + rc = -EINVAL; + goto out; + } + + *out_num_bytes_read = param[1].u.memref.size; + + memcpy(out_buffer, shm_buf->addr, *out_num_bytes_read); + +out: + tee_shm_free(shm_buf); +free_name: + tee_shm_free(shm_name); + + return rc; +} + +static int write_persistent_value(const char *name, + size_t value_size, + const u8 *value) +{ + int rc = 0; + struct tee_shm *shm_name; + struct tee_shm *shm_buf; + struct tee_param param[2]; + size_t name_size = strlen(name) + 1; + + if (!tee) { + if (avb_ta_open_session()) + return -ENODEV; + } + if (!value_size) + return -EINVAL; + + rc = tee_shm_alloc(tee, name_size, + TEE_SHM_ALLOC, &shm_name); + if (rc) + return -ENOMEM; + + rc = tee_shm_alloc(tee, value_size, + TEE_SHM_ALLOC, &shm_buf); + if (rc) { + rc = -ENOMEM; + goto free_name; + } + + memcpy(shm_name->addr, name, name_size); + memcpy(shm_buf->addr, value, value_size); + + memset(param, 0, sizeof(param)); + param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT; + param[0].u.memref.shm = shm_name; + param[0].u.memref.size = name_size; + param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT; + param[1].u.memref.shm = shm_buf; + param[1].u.memref.size = value_size; + + rc = invoke_func(TA_AVB_CMD_WRITE_PERSIST_VALUE, + 2, param); + if (rc) + goto out; + +out: + tee_shm_free(shm_buf); +free_name: + tee_shm_free(shm_name); + + return rc; +} + +int do_optee_rpmb_read(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + const char *name; + size_t bytes; + size_t bytes_read; + void *buffer; + char *endp; + + if (argc != 3) + return CMD_RET_USAGE; + + name = argv[1]; + bytes = simple_strtoul(argv[2], &endp, 10); + if (*endp && *endp != '\n') + return CMD_RET_USAGE; + + buffer = malloc(bytes); + if (!buffer) + return CMD_RET_FAILURE; + + if (read_persistent_value(name, bytes, buffer, &bytes_read) == 0) { + printf("Read %zu bytes, value = %s\n", bytes_read, + (char *)buffer); + free(buffer); + return CMD_RET_SUCCESS; + } + + printf("Failed to read persistent value\n"); + + free(buffer); + + return CMD_RET_FAILURE; +} + +int do_optee_rpmb_write(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + const char *name; + const char *value; + + if (argc != 3) + return CMD_RET_USAGE; + + name = argv[1]; + value = argv[2]; + + if (write_persistent_value(name, strlen(value) + 1, + (const uint8_t *)value) == 0) { + printf("Wrote %zu bytes\n", strlen(value) + 1); + return CMD_RET_SUCCESS; + } + + printf("Failed to write persistent value\n"); + + return CMD_RET_FAILURE; +} + +static cmd_tbl_t cmd_optee_rpmb[] = { + U_BOOT_CMD_MKENT(read_pvalue, 3, 0, do_optee_rpmb_read, "", ""), + U_BOOT_CMD_MKENT(write_pvalue, 3, 0, do_optee_rpmb_write, "", ""), +}; + +static int do_optee_rpmb(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + cmd_tbl_t *cp; + + cp = find_cmd_tbl(argv[1], cmd_optee_rpmb, ARRAY_SIZE(cmd_optee_rpmb)); + + argc--; + argv++; + + if (!cp || argc > cp->maxargs) + return CMD_RET_USAGE; + + if (flag == CMD_FLAG_REPEAT) + return CMD_RET_FAILURE; + + return cp->cmd(cmdtp, flag, argc, argv); +} + +U_BOOT_CMD( + optee_rpmb, 29, 0, do_optee_rpmb, + "Provides commands for testing secure storage on RPMB on OPTEE", + "read_pvalue - read a persistent value \n" + "optee_rpmb write_pvalue - write a persistent value \n" + );