45f8f7c1c15d412a27ade021af21a3aed239cf5d
[openwrt/staging/chunkeey.git] / tools / mkimage / patches / 081-mtk_image-add-an-option-to-set-device-header-offset.patch
1 From patchwork Tue Mar 9 07:52:31 2021
2 Content-Type: text/plain; charset="utf-8"
3 MIME-Version: 1.0
4 Content-Transfer-Encoding: 7bit
5 X-Patchwork-Submitter: Weijie Gao <weijie.gao@mediatek.com>
6 X-Patchwork-Id: 1449568
7 Return-Path: <u-boot-bounces@lists.denx.de>
8 X-Original-To: incoming@patchwork.ozlabs.org
9 Delivered-To: patchwork-incoming@bilbo.ozlabs.org
10 Authentication-Results: ozlabs.org;
11 spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de
12 (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de;
13 envelope-from=u-boot-bounces@lists.denx.de; receiver=<UNKNOWN>)
14 Authentication-Results: ozlabs.org;
15 dkim=pass (1024-bit key;
16 unprotected) header.d=mediatek.com header.i=@mediatek.com header.a=rsa-sha256
17 header.s=dk header.b=i1dK9gFR;
18 dkim-atps=neutral
19 Received: from phobos.denx.de (phobos.denx.de
20 [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01])
21 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
22 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest
23 SHA256)
24 (No client certificate requested)
25 by ozlabs.org (Postfix) with ESMTPS id 4DvnX84rrHz9sW1
26 for <incoming@patchwork.ozlabs.org>; Tue, 9 Mar 2021 18:53:44 +1100 (AEDT)
27 Received: from h2850616.stratoserver.net (localhost [IPv6:::1])
28 by phobos.denx.de (Postfix) with ESMTP id DF60F8219C;
29 Tue, 9 Mar 2021 08:53:29 +0100 (CET)
30 Authentication-Results: phobos.denx.de;
31 dmarc=pass (p=none dis=none) header.from=mediatek.com
32 Authentication-Results: phobos.denx.de;
33 spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de
34 Authentication-Results: phobos.denx.de;
35 dkim=pass (1024-bit key;
36 unprotected) header.d=mediatek.com header.i=@mediatek.com
37 header.b="i1dK9gFR";
38 dkim-atps=neutral
39 Received: by phobos.denx.de (Postfix, from userid 109)
40 id 5456882625; Tue, 9 Mar 2021 08:53:28 +0100 (CET)
41 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de
42 X-Spam-Level:
43 X-Spam-Status: No, score=0.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,
44 DKIM_VALID,DKIM_VALID_AU,MIME_BASE64_TEXT,RDNS_NONE,SPF_HELO_NONE,
45 UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.2
46 Received: from mailgw02.mediatek.com (unknown [1.203.163.81])
47 by phobos.denx.de (Postfix) with ESMTP id 7526E80EF2
48 for <u-boot@lists.denx.de>; Tue, 9 Mar 2021 08:53:19 +0100 (CET)
49 Authentication-Results: phobos.denx.de;
50 dmarc=pass (p=none dis=none) header.from=mediatek.com
51 Authentication-Results: phobos.denx.de;
52 spf=pass smtp.mailfrom=weijie.gao@mediatek.com
53 X-UUID: 3b5ccbd89ab948daa31ec738ee94e7ed-20210309
54 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed;
55 d=mediatek.com;
56 s=dk;
57 h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From;
58 bh=mijplkmurYfYet7eQPGQD6GyyMtN6xMXZKHVAlpy0hM=;
59 b=i1dK9gFRfmkqD5vAud81Q3CdZlOQm3XK0H/NwbxYyncwalZqRZA1YBKTQhmPW0avcbwOQpGVlRmu1VAfALWgK80acX7bPIjWjtaJtK4/99vc+wIthmm1E5QMewyBAFkzGfx7A8ryh4HdcsG/esbnI0Mk2nletBHwRFAEVs3uUfU=;
60 X-UUID: 3b5ccbd89ab948daa31ec738ee94e7ed-20210309
61 Received: from mtkcas32.mediatek.inc [(172.27.4.253)] by mailgw02.mediatek.com
62 (envelope-from <weijie.gao@mediatek.com>)
63 (mailgw01.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256)
64 with ESMTP id 23424931; Tue, 09 Mar 2021 15:53:06 +0800
65 Received: from MTKCAS32.mediatek.inc (172.27.4.184) by MTKMBS31N1.mediatek.inc
66 (172.27.4.69) with Microsoft SMTP Server (TLS) id 15.0.1497.2;
67 Tue, 9 Mar 2021 15:52:58 +0800
68 Received: from mcddlt001.mediatek.inc (10.19.240.15) by MTKCAS32.mediatek.inc
69 (172.27.4.170) with Microsoft SMTP Server id 15.0.1497.2 via Frontend
70 Transport; Tue, 9 Mar 2021 15:52:58 +0800
71 From: Weijie Gao <weijie.gao@mediatek.com>
72 To: <u-boot@lists.denx.de>
73 CC: GSS_MTK_Uboot_upstream <GSS_MTK_Uboot_upstream@mediatek.com>, Weijie Gao
74 <weijie.gao@mediatek.com>
75 Subject: [PATCH] tools: mtk_image: add an option to set device header offset
76 Date: Tue, 9 Mar 2021 15:52:31 +0800
77 Message-ID: <1615276351-30641-1-git-send-email-weijie.gao@mediatek.com>
78 X-Mailer: git-send-email 1.9.1
79 MIME-Version: 1.0
80 X-TM-SNTS-SMTP:
81 B09EA906E69093D91FA73A3F764A0B89D3838DA91A4FA20DA0483EBE19962CA02000:8
82 X-MTK: N
83 X-BeenThere: u-boot@lists.denx.de
84 X-Mailman-Version: 2.1.34
85 Precedence: list
86 List-Id: U-Boot discussion <u-boot.lists.denx.de>
87 List-Unsubscribe: <https://lists.denx.de/options/u-boot>,
88 <mailto:u-boot-request@lists.denx.de?subject=unsubscribe>
89 List-Archive: <https://lists.denx.de/pipermail/u-boot/>
90 List-Post: <mailto:u-boot@lists.denx.de>
91 List-Help: <mailto:u-boot-request@lists.denx.de?subject=help>
92 List-Subscribe: <https://lists.denx.de/listinfo/u-boot>,
93 <mailto:u-boot-request@lists.denx.de?subject=subscribe>
94 Errors-To: u-boot-bounces@lists.denx.de
95 Sender: "U-Boot" <u-boot-bounces@lists.denx.de>
96 X-Virus-Scanned: clamav-milter 0.102.4 at phobos.denx.de
97 X-Virus-Status: Clean
98
99 This patch adds an option which allows setting the device header offset.
100 This is useful if this tool is used to generate ATF BL2 image of mt7622 for
101 SD cards.
102
103 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
104 ---
105 tools/mtk_image.c | 50 ++++++++++++++++++++++++++++++++++++++++++++---
106 1 file changed, 47 insertions(+), 3 deletions(-)
107
108 diff --git a/tools/mtk_image.c b/tools/mtk_image.c
109 index bde1e5da4b..418c5fd54b 100644
110 --- a/tools/mtk_image.c
111 +++ b/tools/mtk_image.c
112 @@ -243,8 +243,13 @@ static const struct brom_img_type {
113 }
114 };
115
116 +/* Indicates whether we're generating or verifying */
117 +static bool img_gen;
118 +static uint32_t img_size;
119 +
120 /* Image type selected by user */
121 static enum brlyt_img_type hdr_media;
122 +static uint32_t hdr_offset;
123 static int use_lk_hdr;
124 static bool is_arm64_image;
125
126 @@ -275,6 +280,7 @@ static int mtk_brom_parse_imagename(const char *imagename)
127
128 /* User passed arguments from image name */
129 static const char *media = "";
130 + static const char *hdr_offs = "";
131 static const char *nandinfo = "";
132 static const char *lk = "";
133 static const char *arm64_param = "";
134 @@ -317,6 +323,9 @@ static int mtk_brom_parse_imagename(const char *imagename)
135 if (!strcmp(key, "media"))
136 media = val;
137
138 + if (!strcmp(key, "hdroffset"))
139 + hdr_offs = val;
140 +
141 if (!strcmp(key, "nandinfo"))
142 nandinfo = val;
143
144 @@ -359,6 +368,10 @@ static int mtk_brom_parse_imagename(const char *imagename)
145 }
146 }
147
148 + /* parse device header offset */
149 + if (hdr_offs && hdr_offs[0])
150 + hdr_offset = strtoul(hdr_offs, NULL, 0);
151 +
152 if (arm64_param && arm64_param[0] == '1')
153 is_arm64_image = true;
154
155 @@ -422,6 +435,7 @@ static int mtk_image_vrec_header(struct image_tool_params *params,
156 static int mtk_image_verify_gen_header(const uint8_t *ptr, int print)
157 {
158 union gen_boot_header *gbh = (union gen_boot_header *)ptr;
159 + uint32_t gfh_offset, total_size, devh_size;
160 struct brom_layout_header *bh;
161 struct gfh_header *gfh;
162 const char *bootmedia;
163 @@ -453,7 +467,32 @@ static int mtk_image_verify_gen_header(const uint8_t *ptr, int print)
164 le32_to_cpu(bh->type) != BRLYT_TYPE_SDMMC))
165 return -1;
166
167 - gfh = (struct gfh_header *)(ptr + le32_to_cpu(bh->header_size));
168 + devh_size = sizeof(struct gen_device_header);
169 +
170 + if (img_gen) {
171 + gfh_offset = devh_size;
172 + } else {
173 + gfh_offset = le32_to_cpu(bh->header_size);
174 +
175 + if (gfh_offset + sizeof(struct gfh_header) > img_size) {
176 + /*
177 + * This may happen if the hdr_offset used to generate
178 + * this image is not zero.
179 + * Since device header size is not fixed, we can't
180 + * cover all possible cases.
181 + * Assuming the image is valid only if the real
182 + * device header size equals to devh_size.
183 + */
184 + total_size = le32_to_cpu(bh->total_size);
185 +
186 + if (total_size - gfh_offset > img_size - devh_size)
187 + return -1;
188 +
189 + gfh_offset = devh_size;
190 + }
191 + }
192 +
193 + gfh = (struct gfh_header *)(ptr + gfh_offset);
194
195 if (strcmp(gfh->file_info.name, GFH_FILE_INFO_NAME))
196 return -1;
197 @@ -549,6 +588,8 @@ static int mtk_image_verify_header(unsigned char *ptr, int image_size,
198 if (le32_to_cpu(lk->magic) == LK_PART_MAGIC)
199 return 0;
200
201 + img_size = image_size;
202 +
203 if (!strcmp((char *)ptr, NAND_BOOT_NAME))
204 return mtk_image_verify_nand_header(ptr, 0);
205 else
206 @@ -682,8 +723,8 @@ static void mtk_image_set_gen_header(void *ptr, off_t filesize,
207
208 /* BRLYT header */
209 put_brom_layout_header(&hdr->brlyt, hdr_media);
210 - hdr->brlyt.header_size = cpu_to_le32(sizeof(struct gen_device_header));
211 - hdr->brlyt.total_size = cpu_to_le32(filesize);
212 + hdr->brlyt.header_size = cpu_to_le32(hdr_offset + sizeof(*hdr));
213 + hdr->brlyt.total_size = cpu_to_le32(hdr_offset + filesize);
214 hdr->brlyt.header_size_2 = hdr->brlyt.header_size;
215 hdr->brlyt.total_size_2 = hdr->brlyt.total_size;
216
217 @@ -747,6 +788,9 @@ static void mtk_image_set_header(void *ptr, struct stat *sbuf, int ifd,
218 return;
219 }
220
221 + img_gen = true;
222 + img_size = sbuf->st_size;
223 +
224 if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
225 mtk_image_set_nand_header(ptr, sbuf->st_size, params->addr);
226 else