From patchwork Wed Jun 19 02:05:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Fan X-Patchwork-Id: 1118414 X-Patchwork-Delegate: sbabic@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=nxp.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=nxp.com header.i=@nxp.com header.b="VffCrTN5"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 45T7bF4PN3z9sN6 for ; Wed, 19 Jun 2019 12:06:01 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 40271C2205C; Wed, 19 Jun 2019 02:05:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=SPF_HELO_PASS, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id AEF7DC22036; Wed, 19 Jun 2019 02:05:41 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 51F03C22060; Wed, 19 Jun 2019 02:05:36 +0000 (UTC) Received: from EUR03-AM5-obe.outbound.protection.outlook.com (mail-eopbgr30083.outbound.protection.outlook.com [40.107.3.83]) by lists.denx.de (Postfix) with ESMTPS id 2FD6AC2205D for ; Wed, 19 Jun 2019 02:05:33 +0000 (UTC) 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=nJ2adgsRLTcr1/LxeBzOZ9zSjm/uJz4InU2qiLSS7Bc=; b=VffCrTN5r6F3S3J5u87oJ6LO01hh0pqgy3cFDlBF3nvaCHt71PdrQ8wFbhC4OE6bxLusFlyvAP8NwFvzPwnqu/OoRkdo/Dos9Ajr8oOzd1wZSCl4Ex6kldswEPr/IBwlmEKlhRHK1mXIyDQCdUm7yJRt0LWDZS18EH5cn4zOpwA= Received: from AM0PR04MB4481.eurprd04.prod.outlook.com (52.135.147.15) by AM0PR04MB5682.eurprd04.prod.outlook.com (20.178.202.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1987.12; Wed, 19 Jun 2019 02:05:32 +0000 Received: from AM0PR04MB4481.eurprd04.prod.outlook.com ([fe80::6090:1f0b:b85b:8015]) by AM0PR04MB4481.eurprd04.prod.outlook.com ([fe80::6090:1f0b:b85b:8015%3]) with mapi id 15.20.1987.014; Wed, 19 Jun 2019 02:05:32 +0000 From: Peng Fan To: "sbabic@denx.de" Thread-Topic: [PATCH V2 2/5] imx8: support parsing i.MX8 Container file Thread-Index: AQHVJkN5/S8rvVaDekKJG2n9WpcKvA== Date: Wed, 19 Jun 2019 02:05:32 +0000 Message-ID: <20190619021958.31790-3-peng.fan@nxp.com> References: <20190619021958.31790-1-peng.fan@nxp.com> In-Reply-To: <20190619021958.31790-1-peng.fan@nxp.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.16.4 x-clientproxiedby: HK0PR01CA0062.apcprd01.prod.exchangelabs.com (2603:1096:203:a6::26) To AM0PR04MB4481.eurprd04.prod.outlook.com (2603:10a6:208:70::15) authentication-results: spf=none (sender IP is ) smtp.mailfrom=peng.fan@nxp.com; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [119.31.174.71] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 873c7d95-84d8-4ae3-704a-08d6f45a9be4 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(4618075)(2017052603328)(7193020); SRVR:AM0PR04MB5682; x-ms-traffictypediagnostic: AM0PR04MB5682: x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:138; x-forefront-prvs: 0073BFEF03 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(136003)(366004)(376002)(346002)(396003)(39860400002)(199004)(189003)(25786009)(36756003)(6512007)(6116002)(52116002)(81156014)(7736002)(4326008)(6436002)(8676002)(1730700003)(81166006)(476003)(2616005)(86362001)(44832011)(486006)(3846002)(14454004)(54906003)(66556008)(6506007)(66446008)(66476007)(386003)(64756008)(73956011)(478600001)(6916009)(66946007)(2351001)(305945005)(71190400001)(102836004)(2501003)(316002)(446003)(8936002)(256004)(5640700003)(50226002)(6486002)(66066001)(2906002)(11346002)(71200400001)(53936002)(5660300002)(1076003)(99286004)(186003)(68736007)(26005)(76176011); DIR:OUT; SFP:1101; SCL:1; SRVR:AM0PR04MB5682; H:AM0PR04MB4481.eurprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; received-spf: None (protection.outlook.com: nxp.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: Sgf9XQxLUJoCzN6sgeJijX81Q6nSmIa2lRs+wsQC6y4oABMKgj19A5pLgRD8hYGirC/bAF6Nj9op3zKwhcnHEVhYW5ZN5ALIn+GR0/5pdqlQzce5sJ+QDgaaEGJgVmxBOz4+Ym0mNsYg16FGoD5aCQxCJg6OH7lUdhWvjkCCUlTM1YUgh8dkSLDWJx3ULlm8bjKsftWE7UbwTjM9RINlCra47bxZGmoMlZ8Zs7hbI9IS0lTduqzgfYy8Z7F49h7rMz5mO1Ojt4ZYNAxv6n823JjHwRM1G91rgeC0DB+K//1RllXmRaRgV51sTMgLxQ6HgMRPP6f7d1lCGHouJcTBIXHXOfwjq5CIny4EahVjPyX2YmxDCewcRhjLOA/bz7H5GaKsPp2OOPfIjWGqQKFnweV8DCr7fPY1YBMs4ACFoyw= MIME-Version: 1.0 X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 873c7d95-84d8-4ae3-704a-08d6f45a9be4 X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Jun 2019 02:05:32.1945 (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: peng.fan@nxp.com X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR04MB5682 Cc: "u-boot@lists.denx.de" , dl-uboot-imx Subject: [U-Boot] [PATCH V2 2/5] imx8: support parsing i.MX8 Container file X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 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" Add parsing i.MX8 Container file support, this is to let SPL could load images in a container file to destination address. Signed-off-by: Peng Fan --- arch/arm/include/asm/arch-imx8/image.h | 56 +++++++++++++++ arch/arm/mach-imx/imx8/Kconfig | 6 ++ arch/arm/mach-imx/imx8/Makefile | 4 ++ arch/arm/mach-imx/imx8/parse-container.c | 120 +++++++++++++++++++++++++++++++ 4 files changed, 186 insertions(+) create mode 100644 arch/arm/include/asm/arch-imx8/image.h create mode 100644 arch/arm/mach-imx/imx8/parse-container.c diff --git a/arch/arm/include/asm/arch-imx8/image.h b/arch/arm/include/asm/arch-imx8/image.h new file mode 100644 index 0000000000..c1e5700859 --- /dev/null +++ b/arch/arm/include/asm/arch-imx8/image.h @@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright 2018-2019 NXP + */ + +#ifndef __CONTAINER_HEADER_H_ +#define __CONTAINER_HEADER_H_ + +#include +#include + +#define IV_MAX_LEN 32 +#define HASH_MAX_LEN 64 + +#define CONTAINER_HDR_ALIGNMENT 0x400 +#define CONTAINER_HDR_EMMC_OFFSET 0 +#define CONTAINER_HDR_MMCSD_OFFSET SZ_32K +#define CONTAINER_HDR_QSPI_OFFSET SZ_4K +#define CONTAINER_HDR_NAND_OFFSET SZ_128M + +struct container_hdr { + u8 version; + u8 length_lsb; + u8 length_msb; + u8 tag; + u32 flags; + u16 sw_version; + u8 fuse_version; + u8 num_images; + u16 sig_blk_offset; + u16 reserved; +} __packed; + +struct boot_img_t { + u32 offset; + u32 size; + u64 dst; + u64 entry; + u32 hab_flags; + u32 meta; + u8 hash[HASH_MAX_LEN]; + u8 iv[IV_MAX_LEN]; +} __packed; + +struct signature_block_hdr { + u8 version; + u8 length_lsb; + u8 length_msb; + u8 tag; + u16 srk_table_offset; + u16 cert_offset; + u16 blob_offset; + u16 signature_offset; + u32 reserved; +} __packed; +#endif diff --git a/arch/arm/mach-imx/imx8/Kconfig b/arch/arm/mach-imx/imx8/Kconfig index bbe323d5ca..e9496cb611 100644 --- a/arch/arm/mach-imx/imx8/Kconfig +++ b/arch/arm/mach-imx/imx8/Kconfig @@ -23,6 +23,12 @@ config IMX8QXP config SYS_SOC default "imx8" +config SPL_LOAD_IMX_CONTAINER + bool "Enable SPL loading U-Boot as a i.MX Container image" + depends on SPL + help + This is to let SPL could load i.MX8 Container image + choice prompt "i.MX8 board select" optional diff --git a/arch/arm/mach-imx/imx8/Makefile b/arch/arm/mach-imx/imx8/Makefile index 31ad169ccf..19690110a5 100644 --- a/arch/arm/mach-imx/imx8/Makefile +++ b/arch/arm/mach-imx/imx8/Makefile @@ -5,3 +5,7 @@ # obj-y += cpu.o iomux.o + +ifdef CONFIG_SPL_BUILD +obj-$(CONFIG_SPL_LOAD_IMX_CONTAINER) += parse-container.o +endif diff --git a/arch/arm/mach-imx/imx8/parse-container.c b/arch/arm/mach-imx/imx8/parse-container.c new file mode 100644 index 0000000000..32f78bdddf --- /dev/null +++ b/arch/arm/mach-imx/imx8/parse-container.c @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2018-2019 NXP + */ + +#include +#include +#include +#include + +static struct boot_img_t *read_auth_image(struct spl_image_info *spl_image, + struct spl_load_info *info, + struct container_hdr *container, + int image_index, + u32 container_sector) +{ + struct boot_img_t *images; + ulong sector; + u32 sectors; + + if (image_index > container->num_images) { + debug("Invalid image number\n"); + return NULL; + } + + images = (struct boot_img_t *)((u8 *)container + + sizeof(struct container_hdr)); + + if (images[image_index].offset % info->bl_len) { + printf("%s: image%d offset not aligned to %u\n", + __func__, image_index, info->bl_len); + return NULL; + } + + sectors = roundup(images[image_index].size, info->bl_len) / + info->bl_len; + sector = images[image_index].offset / info->bl_len + + container_sector; + + debug("%s: container: %p sector: %lu sectors: %u\n", __func__, + container, sector, sectors); + if (info->read(info, sector, sectors, + (void *)images[image_index].entry) != sectors) { + printf("%s wrong\n", __func__); + return NULL; + } + + return &images[image_index]; +} + +static int read_auth_container(struct spl_image_info *spl_image, + struct spl_load_info *info, ulong sector) +{ + struct container_hdr *container = NULL; + u16 length; + u32 sectors; + int i, size; + + size = roundup(CONTAINER_HDR_ALIGNMENT, info->bl_len); + sectors = size / info->bl_len; + + /* + * It will not override the ATF code, so safe to use it here, + * no need malloc + */ + container = (struct container_hdr *)spl_get_load_buffer(-size, size); + + debug("%s: container: %p sector: %lu sectors: %u\n", __func__, + container, sector, sectors); + if (info->read(info, sector, sectors, container) != sectors) + return -EIO; + + if (container->tag != 0x87 && container->version != 0x0) { + printf("Wrong container header"); + return -ENOENT; + } + + if (!container->num_images) { + printf("Wrong container, no image found"); + return -ENOENT; + } + + length = container->length_lsb + (container->length_msb << 8); + debug("Container length %u\n", length); + + if (length > CONTAINER_HDR_ALIGNMENT) { + size = roundup(length, info->bl_len); + sectors = size / info->bl_len; + + container = (struct container_hdr *)spl_get_load_buffer(-size, size); + + debug("%s: container: %p sector: %lu sectors: %u\n", + __func__, container, sector, sectors); + if (info->read(info, sector, sectors, container) != + sectors) + return -EIO; + } + + for (i = 0; i < container->num_images; i++) { + struct boot_img_t *image = read_auth_image(spl_image, info, + container, i, + sector); + + if (!image) + return -EINVAL; + + if (i == 0) { + spl_image->load_addr = image->dst; + spl_image->entry_point = image->entry; + } + } + + return 0; +} + +int spl_load_imx_container(struct spl_image_info *spl_image, + struct spl_load_info *info, ulong sector) +{ + return read_auth_container(spl_image, info, sector); +}