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 6296AC433EF for ; Wed, 11 May 2022 15:36:57 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id D47FC84338; Wed, 11 May 2022 17:36:53 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org 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=linaro.org header.i=@linaro.org header.b="oP/UghSS"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5243A83EAC; Wed, 11 May 2022 17:35:54 +0200 (CEST) Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) (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 8320383EAC for ; Wed, 11 May 2022 17:35:50 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x32a.google.com with SMTP id i20-20020a05600c355400b0039456976dcaso2718237wmq.1 for ; Wed, 11 May 2022 08:35:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=JQOsmuPjcVk/BCohr9SlRkDzwIAZX8y217IooNesHAU=; b=oP/UghSS01tj08ZMirFLwh1ePFnnxrEcXT4MuIkoX21k9euu6BAFy8/+X2P/1ZWm/b jYniIFhfn59h6CJr5m/6yKn3ejNE/Q0Tnv7A2yIuD1m4uajeza6/RwkYGB7u79yS6cmu ZgScwznAhepUt3kaQosfwINiUQ1mqN/VHeEqFqtArUslO0OeCaM+b4Jd2/+ZxGK+6Csx QTwkTxIFMMvbhf8CU4DssniDcn8MP9b9JYuQTW97zFNe5AY9IKQl8w86horJ4Bk5lX+F jbeWN4OpaIfuUKZ35CPkKUUaKnUr9pmaEtVf+iLHQEqiZCcIJu7hZ1P3Ik6+GL97xGy9 xXmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=JQOsmuPjcVk/BCohr9SlRkDzwIAZX8y217IooNesHAU=; b=E5UGfn+4Pn//qU4vu4X1IV9sNEa9Ja8bcbghTwH4EfpgLVDYJGeWBA8yVSbtWRxIzA McOdpnyrd6tAaUZluZt5yVQ0vudRf/sL+YvEGRtbCwqdaE4ollewYOBSuEOY+7jyQwR9 4dGpBPGB6KV9XG2Af5re385FNmVBOSt5/22kBOgOizqiQQYMmodEx7PbYV0ekpezLAy6 76qsYJKedbJkgJ38SYCO9uvA5zJXURuKobiKjmBX60yU0ZLsMVbIKexzVWBFs0IaU4jR 7sNrBsN2YRXa2YipLmfBAJ5BrfCixKGb6v5WIwTZPfj0ztJuWqJXKUDfjGfo3DWx6/JM i18Q== X-Gm-Message-State: AOAM532hJl3eNkql0PMW7Kf7sqnif/1YlBwnXIc9S2i0h61Utdm7PJIw rs1zotlgV5LVygCytNTdJbqA1A== X-Google-Smtp-Source: ABdhPJzA6b3MNKQxsCPFpurgxHceNqrmtFRFtGUnBnV6OpvJ2E40SYvx/lwpQNSAJlG4rWmw9Zdv7w== X-Received: by 2002:a05:600c:3b1f:b0:394:5399:a34 with SMTP id m31-20020a05600c3b1f00b0039453990a34mr5355500wms.40.1652283350086; Wed, 11 May 2022 08:35:50 -0700 (PDT) Received: from jerome-XPS-13-9310.. ([2a01:e0a:3cb:7bb0:d8d7:2d3d:b931:321a]) by smtp.gmail.com with ESMTPSA id o42-20020a05600c512a00b003942a244f49sm141895wms.34.2022.05.11.08.35.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 08:35:49 -0700 (PDT) From: Jerome Forissier To: Simon Glass , Philipp Tomsich , Kever Yang Cc: Jens Wiklander , Jerome Forissier , u-boot@lists.denx.de Subject: [PATCH] mach-rockchip: make_fit_atf.py: support OP-TEE tee.bin v1 format Date: Wed, 11 May 2022 17:35:14 +0200 Message-Id: <20220511153514.71472-1-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailman-Approved-At: Wed, 11 May 2022 17:36:52 +0200 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.5 at phobos.denx.de X-Virus-Status: Clean This commit adds support for the OP-TEE 'tee.bin' v1 format for Rockchip platforms. Since OP-TEE 3.8.0, tee.bin contains meta-data in a proprietary format in addition to the ELF data. They are essential information for proper initialization of the TEE core, such as the size of the memory region covered by the TEE or a compact representation of runtime relocation data when ASLR is enabled. With OP-TEE 3.8.0 onwards, 'tee.elf' MUST NOT be used and 'tee.bin' MUST be used instead. Ignoring this recommendation can lead to crashes as described in [3]. Link: [1] https://github.com/OP-TEE/optee_os/commit/5dd1570ac5b0f6563b1a9c074533a19107b8222d Link: [2] https://github.com/OP-TEE/optee_os/blob/3.17.0/scripts/gen_tee_bin.py#L275-L302 Link: [3] https://github.com/OP-TEE/optee_os/issues/4542 Signed-off-by: Jerome Forissier --- arch/arm/mach-rockchip/make_fit_atf.py | 43 +++++++++++++++++++++----- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/arch/arm/mach-rockchip/make_fit_atf.py b/arch/arm/mach-rockchip/make_fit_atf.py index f3224d2555..fcea652388 100755 --- a/arch/arm/mach-rockchip/make_fit_atf.py +++ b/arch/arm/mach-rockchip/make_fit_atf.py @@ -137,7 +137,7 @@ def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, tee_file_name, dtbs_file num_segments = len(segments) if tee_file_name: - tee_segments = unpack_elf(tee_file_name) + tee_segments = unpack_tee_file(tee_file_name) for index, entry, paddr, data in tee_segments: append_tee_node(fit_file, num_segments + index + 1, paddr, entry) num_segments = num_segments + len(tee_segments) @@ -169,7 +169,7 @@ def generate_atf_binary(bl31_file_name): def generate_tee_binary(tee_file_name): if tee_file_name: - for index, entry, paddr, data in unpack_elf(tee_file_name): + for index, entry, paddr, data in unpack_tee_file(tee_file_name): file_name = 'tee_0x%08x.bin' % paddr with open(file_name, "wb") as atf: atf.write(data) @@ -194,6 +194,31 @@ def unpack_elf(filename): segments.append((index, e_entry, p_paddr, p_data)) return segments +def unpack_tee_file(filename): + if filename.endswith('.elf'): + return unpack_elf(filename) + with open(filename, 'rb') as file: + bin = file.read() + segments = [] + if bin[0:5] == b'OPTE\x01': + # OP-TEE v1 format (tee.bin) + init_sz, start_hi, start_lo, _, paged_sz = struct.unpack_from('<5I', + bin, + 0x8) + if paged_sz != 0: + raise ValueError("OP-TEE paged mode not supported") + e_entry = (start_hi << 32) + start_lo + p_addr = e_entry + p_data = bin[0x1c:] + if len(p_data) != init_sz: + raise ValueError("Invalid file '%s': size mismatch " + "(expected %d, have %d)" % (filename, init_sz, + len(p_data))) + segments.append((0, e_entry, p_addr, p_data)) + else: + raise ValueError("Unknown format for TEE file '%s'" % filename) + return segments + def main(): uboot_elf = "./u-boot" fit_its = sys.stdout @@ -210,11 +235,13 @@ def main(): logging.warning(' Please read Building section in doc/README.rockchip') if "TEE" in os.environ: - tee_elf = os.getenv("TEE") + tee_file = os.getenv("TEE") + elif os.path.isfile("./tee.bin"): + tee_file = "./tee.bin" elif os.path.isfile("./tee.elf"): - tee_elf = "./tee.elf" + tee_file = "./tee.elf" else: - tee_elf = "" + tee_file = "" opts, args = getopt.getopt(sys.argv[1:], "o:u:b:t:h") for opt, val in opts: @@ -225,16 +252,16 @@ def main(): elif opt == "-b": bl31_elf = val elif opt == "-t": - tee_elf = val + tee_file = val elif opt == "-h": print(__doc__) sys.exit(2) dtbs = args - generate_atf_fit_dts(fit_its, bl31_elf, tee_elf, uboot_elf, dtbs) + generate_atf_fit_dts(fit_its, bl31_elf, tee_file, uboot_elf, dtbs) generate_atf_binary(bl31_elf) - generate_tee_binary(tee_elf) + generate_tee_binary(tee_file) if __name__ == "__main__": main() -- 2.17.1