From patchwork Tue Mar 9 07:52:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Weijie Gao X-Patchwork-Id: 1449568 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=mediatek.com header.i=@mediatek.com header.a=rsa-sha256 header.s=dk header.b=i1dK9gFR; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DvnX84rrHz9sW1 for ; Tue, 9 Mar 2021 18:53:44 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id DF60F8219C; Tue, 9 Mar 2021 08:53:29 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=mediatek.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=mediatek.com header.i=@mediatek.com header.b="i1dK9gFR"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5456882625; Tue, 9 Mar 2021 08:53:28 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=0.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MIME_BASE64_TEXT,RDNS_NONE,SPF_HELO_NONE, UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.2 Received: from mailgw02.mediatek.com (unknown [1.203.163.81]) by phobos.denx.de (Postfix) with ESMTP id 7526E80EF2 for ; Tue, 9 Mar 2021 08:53:19 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=mediatek.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=weijie.gao@mediatek.com X-UUID: 3b5ccbd89ab948daa31ec738ee94e7ed-20210309 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=mijplkmurYfYet7eQPGQD6GyyMtN6xMXZKHVAlpy0hM=; b=i1dK9gFRfmkqD5vAud81Q3CdZlOQm3XK0H/NwbxYyncwalZqRZA1YBKTQhmPW0avcbwOQpGVlRmu1VAfALWgK80acX7bPIjWjtaJtK4/99vc+wIthmm1E5QMewyBAFkzGfx7A8ryh4HdcsG/esbnI0Mk2nletBHwRFAEVs3uUfU=; X-UUID: 3b5ccbd89ab948daa31ec738ee94e7ed-20210309 Received: from mtkcas32.mediatek.inc [(172.27.4.253)] by mailgw02.mediatek.com (envelope-from ) (mailgw01.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 23424931; Tue, 09 Mar 2021 15:53:06 +0800 Received: from MTKCAS32.mediatek.inc (172.27.4.184) by MTKMBS31N1.mediatek.inc (172.27.4.69) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 9 Mar 2021 15:52:58 +0800 Received: from mcddlt001.mediatek.inc (10.19.240.15) by MTKCAS32.mediatek.inc (172.27.4.170) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Tue, 9 Mar 2021 15:52:58 +0800 From: Weijie Gao To: CC: GSS_MTK_Uboot_upstream , Weijie Gao Subject: [PATCH] tools: mtk_image: add an option to set device header offset Date: Tue, 9 Mar 2021 15:52:31 +0800 Message-ID: <1615276351-30641-1-git-send-email-weijie.gao@mediatek.com> X-Mailer: git-send-email 1.9.1 MIME-Version: 1.0 X-TM-SNTS-SMTP: B09EA906E69093D91FA73A3F764A0B89D3838DA91A4FA20DA0483EBE19962CA02000:8 X-MTK: N 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.4 at phobos.denx.de X-Virus-Status: Clean This patch adds an option which allows setting the device header offset. This is useful if this tool is used to generate ATF BL2 image of mt7622 for SD cards. Signed-off-by: Weijie Gao --- tools/mtk_image.c | 50 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/tools/mtk_image.c b/tools/mtk_image.c index bde1e5da4b..418c5fd54b 100644 --- a/tools/mtk_image.c +++ b/tools/mtk_image.c @@ -243,8 +243,13 @@ static const struct brom_img_type { } }; +/* Indicates whether we're generating or verifying */ +static bool img_gen; +static uint32_t img_size; + /* Image type selected by user */ static enum brlyt_img_type hdr_media; +static uint32_t hdr_offset; static int use_lk_hdr; static bool is_arm64_image; @@ -275,6 +280,7 @@ static int mtk_brom_parse_imagename(const char *imagename) /* User passed arguments from image name */ static const char *media = ""; + static const char *hdr_offs = ""; static const char *nandinfo = ""; static const char *lk = ""; static const char *arm64_param = ""; @@ -317,6 +323,9 @@ static int mtk_brom_parse_imagename(const char *imagename) if (!strcmp(key, "media")) media = val; + if (!strcmp(key, "hdroffset")) + hdr_offs = val; + if (!strcmp(key, "nandinfo")) nandinfo = val; @@ -359,6 +368,10 @@ static int mtk_brom_parse_imagename(const char *imagename) } } + /* parse device header offset */ + if (hdr_offs && hdr_offs[0]) + hdr_offset = strtoul(hdr_offs, NULL, 0); + if (arm64_param && arm64_param[0] == '1') is_arm64_image = true; @@ -422,6 +435,7 @@ static int mtk_image_vrec_header(struct image_tool_params *params, static int mtk_image_verify_gen_header(const uint8_t *ptr, int print) { union gen_boot_header *gbh = (union gen_boot_header *)ptr; + uint32_t gfh_offset, total_size, devh_size; struct brom_layout_header *bh; struct gfh_header *gfh; const char *bootmedia; @@ -453,7 +467,32 @@ static int mtk_image_verify_gen_header(const uint8_t *ptr, int print) le32_to_cpu(bh->type) != BRLYT_TYPE_SDMMC)) return -1; - gfh = (struct gfh_header *)(ptr + le32_to_cpu(bh->header_size)); + devh_size = sizeof(struct gen_device_header); + + if (img_gen) { + gfh_offset = devh_size; + } else { + gfh_offset = le32_to_cpu(bh->header_size); + + if (gfh_offset + sizeof(struct gfh_header) > img_size) { + /* + * This may happen if the hdr_offset used to generate + * this image is not zero. + * Since device header size is not fixed, we can't + * cover all possible cases. + * Assuming the image is valid only if the real + * device header size equals to devh_size. + */ + total_size = le32_to_cpu(bh->total_size); + + if (total_size - gfh_offset > img_size - devh_size) + return -1; + + gfh_offset = devh_size; + } + } + + gfh = (struct gfh_header *)(ptr + gfh_offset); if (strcmp(gfh->file_info.name, GFH_FILE_INFO_NAME)) return -1; @@ -549,6 +588,8 @@ static int mtk_image_verify_header(unsigned char *ptr, int image_size, if (le32_to_cpu(lk->magic) == LK_PART_MAGIC) return 0; + img_size = image_size; + if (!strcmp((char *)ptr, NAND_BOOT_NAME)) return mtk_image_verify_nand_header(ptr, 0); else @@ -682,8 +723,8 @@ static void mtk_image_set_gen_header(void *ptr, off_t filesize, /* BRLYT header */ put_brom_layout_header(&hdr->brlyt, hdr_media); - hdr->brlyt.header_size = cpu_to_le32(sizeof(struct gen_device_header)); - hdr->brlyt.total_size = cpu_to_le32(filesize); + hdr->brlyt.header_size = cpu_to_le32(hdr_offset + sizeof(*hdr)); + hdr->brlyt.total_size = cpu_to_le32(hdr_offset + filesize); hdr->brlyt.header_size_2 = hdr->brlyt.header_size; hdr->brlyt.total_size_2 = hdr->brlyt.total_size; @@ -747,6 +788,9 @@ static void mtk_image_set_header(void *ptr, struct stat *sbuf, int ifd, return; } + img_gen = true; + img_size = sbuf->st_size; + if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND) mtk_image_set_nand_header(ptr, sbuf->st_size, params->addr); else