Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9644EC05027 for ; Thu, 2 Feb 2023 18:17:53 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A500B85E71; Thu, 2 Feb 2023 19:15:35 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="TjdTfOpR"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 9413685E0B; Thu, 2 Feb 2023 19:15:05 +0100 (CET) Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id BC2B185E2A for ; Thu, 2 Feb 2023 19:14:36 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=clamor95@gmail.com Received: by mail-wm1-x32d.google.com with SMTP id bg13-20020a05600c3c8d00b003d9712b29d2so4351954wmb.2 for ; Thu, 02 Feb 2023 10:14:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HknKmxQjC9dnDOSdTy6LPlNTbO7MXWbpYAnsHFMOxHg=; b=TjdTfOpRyop5FsAJF3/Xuh2laJb/T1xZvRx4GBhvqcJWShh44OiSPRoCvAcl3uJ6uW /MXL4++57Eg5EDw2vP39P4MiHJU+9RFzNnQ6Phcdnj68cXP4c9GarFUAdFACuYE499G7 2mxmq0WrOGx3bvhrZojSa99pDIqEX91qKpUhAv0mKijTJOZGUBBjsDTVrqbZ1ajMbdoo EQGKABxCVud9pVxS2CwUWk1oraQ77ucvKoPm9gVyB18BlNSlJWpnE/rgFYFxxHhdtuZj +ypXvYjelLHNb0XVtB8d2pQIf5GvsBSFfCIIdJo8ED8aTtbqZJWDEypFyF1p6aryjyMV eMHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HknKmxQjC9dnDOSdTy6LPlNTbO7MXWbpYAnsHFMOxHg=; b=k/BXRIQ7GDo/HPxzdCPZVTkLUzlhU0d8jbpMR15JV/PNIHf9zt+nk9udniu99pdYGk U8erQNjgqdfFOkiqtuPN83G+Df0GUZ5Gw4hTQUnhfqFcKDJFds7dUrSA0DJIzvIzCmb7 C2NNt7NjkhXkGwsye4NmUsVyNWDpb7ZNjQcL7sHXXomrfm2ccbFuMZ9DW+8CL54HyphZ xlw8Tml2v5xxFSG/IYrDESPhvy8MLBwtnFuysYHDn5Tyj3s4BjgUp7BeN645gvRTAkwJ 08xNo3I6PkRVMN+ZI1r0ht5hFBEfxcTusEIwyNvjmUGFwwNQi3lRRD/Dk+lMthNCkdPs 2LoA== X-Gm-Message-State: AO0yUKWiR72atqxmgmN7fua+b8bs317B9If3oO577BPSoxLcrQ9RP3Qy EAGUtgxn7jLQJ+H9bSd9CGg= X-Google-Smtp-Source: AK7set+r05/HXiSQ942SS0rAAGBRn5halJxobl1Z7NLVIfAOc58W1Yw2IhO6z6rEKAjsNVgmT+vMeQ== X-Received: by 2002:a05:600c:4f03:b0:3dd:e86e:8827 with SMTP id l3-20020a05600c4f0300b003dde86e8827mr6980804wmq.4.1675361675370; Thu, 02 Feb 2023 10:14:35 -0800 (PST) Received: from xeon.. ([188.163.112.54]) by smtp.gmail.com with ESMTPSA id x14-20020a1c7c0e000000b003daffc2ecdesm5516543wmc.13.2023.02.02.10.14.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Feb 2023 10:14:35 -0800 (PST) From: Svyatoslav Ryhel To: Tom Warren , Alban Bedel , Stephen Warren , Marcel Ziswiler , Allen Martin , Jagan Teki , Lukasz Majewski , Marek Vasut , Svyatoslav Ryhel , Ramin Khonsari , Thierry Reding , Maxim Schwalm , Dmitry Osipenko Cc: u-boot@lists.denx.de Subject: [PATCH v2 16/16] ARM: tegra20: implement BCT patching Date: Thu, 2 Feb 2023 20:13:57 +0200 Message-Id: <20230202181357.4455-17-clamor95@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230202181357.4455-1-clamor95@gmail.com> References: <20230202181357.4455-1-clamor95@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.103.6 at phobos.denx.de X-Virus-Status: Clean This function allows updating bootloader from u-boot on production devices without need in host PC. Be aware! It works only with re-crypt BCT. Tested-by: Robert Eckelmann # ASUS TF101 T20 Signed-off-by: Ramin Khonsari Signed-off-by: Svyatoslav Ryhel --- arch/arm/mach-tegra/Kconfig | 2 +- arch/arm/mach-tegra/tegra20/Makefile | 5 +- arch/arm/mach-tegra/tegra20/bct.c | 78 ++++++++++++++++++++++++++++ arch/arm/mach-tegra/tegra20/bct.h | 42 +++++++++++++++ 4 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 arch/arm/mach-tegra/tegra20/bct.c create mode 100644 arch/arm/mach-tegra/tegra20/bct.h diff --git a/arch/arm/mach-tegra/Kconfig b/arch/arm/mach-tegra/Kconfig index 922e57726f..901235047c 100644 --- a/arch/arm/mach-tegra/Kconfig +++ b/arch/arm/mach-tegra/Kconfig @@ -231,7 +231,7 @@ config CMD_ENTERRCM config CMD_EBTUPDATE bool "Enable 'ebtupdate' command" - depends on TEGRA30 + depends on TEGRA20 || TEGRA30 select TEGRA_CRYPTO default n help diff --git a/arch/arm/mach-tegra/tegra20/Makefile b/arch/arm/mach-tegra/tegra20/Makefile index 67454ff5f4..991cabeec5 100644 --- a/arch/arm/mach-tegra/tegra20/Makefile +++ b/arch/arm/mach-tegra/tegra20/Makefile @@ -2,9 +2,8 @@ # # (C) Copyright 2010,2011 Nvidia Corporation. -ifdef CONFIG_SPL_BUILD -obj-y += cpu.o -endif +obj-$(CONFIG_SPL_BUILD) += cpu.o +obj-$(CONFIG_$(SPL_)CMD_EBTUPDATE) += bct.o # The AVP is ARMv4T architecture so we must use special compiler # flags for any startup files it might use. diff --git a/arch/arm/mach-tegra/tegra20/bct.c b/arch/arm/mach-tegra/tegra20/bct.c new file mode 100644 index 0000000000..4a7cf498dd --- /dev/null +++ b/arch/arm/mach-tegra/tegra20/bct.c @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2022, Ramin + * Copyright (c) 2022, Svyatoslav Ryhel + */ + +#include +#include +#include +#include +#include "bct.h" +#include "uboot_aes.h" + +/* + * \param bct boot config table start in RAM + * \param ect bootloader start in RAM + * \param ebt_size bootloader file size in bytes + */ +static int bct_patch(u8 *bct, u8 *ebt, u32 ebt_size) +{ + struct nvboot_config_table *bct_tbl = NULL; + u8 ebt_hash[AES128_KEY_LENGTH] = { 0 }; + u8 sbk[AES128_KEY_LENGTH] = { 0 }; + u8 *bct_hash = bct; + int ret; + + bct += BCT_HASH; + + memcpy(sbk, (u8 *)(bct + BCT_LENGTH), + NVBOOT_CMAC_AES_HASH_LENGTH * 4); + + ret = decrypt_data_block(bct, BCT_LENGTH, sbk); + if (ret) + return 1; + + ebt_size = roundup(ebt_size, EBT_ALIGNMENT); + + ret = encrypt_data_block(ebt, ebt_size, sbk); + if (ret) + return 1; + + ret = sign_enc_data_block(ebt, ebt_size, ebt_hash, sbk); + if (ret) + return 1; + + bct_tbl = (struct nvboot_config_table *)bct; + + memcpy((u8 *)&bct_tbl->bootloader[0].crypto_hash, + ebt_hash, NVBOOT_CMAC_AES_HASH_LENGTH * 4); + bct_tbl->bootloader[0].entry_point = CONFIG_SPL_TEXT_BASE; + bct_tbl->bootloader[0].load_addr = CONFIG_SPL_TEXT_BASE; + bct_tbl->bootloader[0].length = ebt_size; + + ret = encrypt_data_block(bct, BCT_LENGTH, sbk); + if (ret) + return 1; + + ret = sign_enc_data_block(bct, BCT_LENGTH, bct_hash, sbk); + if (ret) + return 1; + + return 0; +} + +static int do_ebtupdate(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + u32 bct_addr = hextoul(argv[1], NULL); + u32 ebt_addr = hextoul(argv[2], NULL); + u32 ebt_size = hextoul(argv[3], NULL); + + return bct_patch((u8 *)bct_addr, (u8 *)ebt_addr, ebt_size); +} + +U_BOOT_CMD(ebtupdate, 4, 0, do_ebtupdate, + "update bootloader on re-crypted Tegra20 devices", + "" +); diff --git a/arch/arm/mach-tegra/tegra20/bct.h b/arch/arm/mach-tegra/tegra20/bct.h new file mode 100644 index 0000000000..4b78aef7cf --- /dev/null +++ b/arch/arm/mach-tegra/tegra20/bct.h @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ + +#ifndef _BCT_H_ +#define _BCT_H_ + +/* + * Defines the BCT parametres for T20 + */ +#define BCT_LENGTH 0xFE0 +#define BCT_HASH 0x10 +#define EBT_ALIGNMENT 0x10 + +/* + * Defines the CMAC-AES-128 hash length in 32 bit words. (128 bits = 4 words) + */ +#define NVBOOT_CMAC_AES_HASH_LENGTH 4 + +/* + * Defines the maximum number of bootloader descriptions in the BCT. + */ +#define NVBOOT_MAX_BOOTLOADERS 4 + +struct nv_bootloader_info { + u32 version; + u32 start_blk; + u32 start_page; + u32 length; + u32 load_addr; + u32 entry_point; + u32 attribute; + u32 crypto_hash[NVBOOT_CMAC_AES_HASH_LENGTH]; +}; + +struct nvboot_config_table { + u32 unused0[4]; + u32 boot_data_version; + u32 unused1[668]; + struct nv_bootloader_info bootloader[NVBOOT_MAX_BOOTLOADERS]; + u32 unused2[508]; +}; + +#endif /* _BCT_H_ */ -- 2.37.2