--- /dev/null
+From 44165e4c676d266f73fda2e6ba82b4bf3262daf2 Mon Sep 17 00:00:00 2001
+From: Fabien Parent <fparent@baylibre.com>
+Date: Fri, 16 Oct 2020 19:52:37 +0200
+Subject: [PATCH] tools: mtk_image: add support for booting ARM64 images
+
+mkimage is only able to package aarch32 binaries. Add support for
+AArch64 images.
+
+One can create a ARM64 image using the following command line:
+mkimage -T mtk_image -a 0x201000 -e 0x201000 -n "media=emmc;arm64=1"
+-d bl2.bin bl2.img
+
+Signed-off-by: Fabien Parent <fparent@baylibre.com>
+---
+ tools/mtk_image.c | 28 ++++++++++++++++++++++++----
+ tools/mtk_image.h | 6 +++++-
+ 2 files changed, 29 insertions(+), 5 deletions(-)
+
+diff --git a/tools/mtk_image.c b/tools/mtk_image.c
+index 2ca519483d..bde1e5da4b 100644
+--- a/tools/mtk_image.c
++++ b/tools/mtk_image.c
+@@ -246,6 +246,7 @@ static const struct brom_img_type {
+ /* Image type selected by user */
+ static enum brlyt_img_type hdr_media;
+ static int use_lk_hdr;
++static bool is_arm64_image;
+
+ /* LK image name */
+ static char lk_name[32] = "U-Boot";
+@@ -276,6 +277,7 @@ static int mtk_brom_parse_imagename(const char *imagename)
+ static const char *media = "";
+ static const char *nandinfo = "";
+ static const char *lk = "";
++ static const char *arm64_param = "";
+
+ key = buf;
+ while (key) {
+@@ -323,6 +325,9 @@ static int mtk_brom_parse_imagename(const char *imagename)
+
+ if (!strcmp(key, "lkname"))
+ snprintf(lk_name, sizeof(lk_name), "%s", val);
++
++ if (!strcmp(key, "arm64"))
++ arm64_param = val;
+ }
+
+ if (next)
+@@ -354,6 +359,9 @@ static int mtk_brom_parse_imagename(const char *imagename)
+ }
+ }
+
++ if (arm64_param && arm64_param[0] == '1')
++ is_arm64_image = true;
++
+ free(buf);
+
+ if (hdr_media == BRLYT_TYPE_INVALID) {
+@@ -458,6 +466,9 @@ static int mtk_image_verify_gen_header(const uint8_t *ptr, int print)
+ le32_to_cpu(gfh->file_info.load_addr) +
+ le32_to_cpu(gfh->file_info.jump_offset));
+
++ if (print)
++ printf("Architecture: %s\n", is_arm64_image ? "ARM64" : "ARM");
++
+ return 0;
+ }
+
+@@ -523,6 +534,9 @@ static int mtk_image_verify_nand_header(const uint8_t *ptr, int print)
+ le32_to_cpu(gfh->file_info.load_addr) +
+ le32_to_cpu(gfh->file_info.jump_offset));
+
++ if (print)
++ printf("Architecture: %s\n", is_arm64_image ? "ARM64" : "ARM");
++
+ return 0;
+ }
+
+@@ -581,6 +595,8 @@ static void put_ghf_common_header(struct gfh_common_header *gfh, int size,
+ static void put_ghf_header(struct gfh_header *gfh, int file_size,
+ int dev_hdr_size, int load_addr, int flash_type)
+ {
++ uint32_t cfg_bits;
++
+ memset(gfh, 0, sizeof(struct gfh_header));
+
+ /* GFH_FILE_INFO header */
+@@ -608,11 +624,15 @@ static void put_ghf_header(struct gfh_header *gfh, int file_size,
+ /* GFH_BROM_CFG header */
+ put_ghf_common_header(&gfh->brom_cfg.gfh, sizeof(gfh->brom_cfg),
+ GFH_TYPE_BROM_CFG, 3);
+- gfh->brom_cfg.cfg_bits = cpu_to_le32(
+- GFH_BROM_CFG_USBDL_AUTO_DETECT_DIS |
+- GFH_BROM_CFG_USBDL_BY_KCOL0_TIMEOUT_EN |
+- GFH_BROM_CFG_USBDL_BY_FLAG_TIMEOUT_EN);
++ cfg_bits = GFH_BROM_CFG_USBDL_AUTO_DETECT_DIS |
++ GFH_BROM_CFG_USBDL_BY_KCOL0_TIMEOUT_EN |
++ GFH_BROM_CFG_USBDL_BY_FLAG_TIMEOUT_EN;
+ gfh->brom_cfg.usbdl_by_kcol0_timeout_ms = cpu_to_le32(5000);
++ if (is_arm64_image) {
++ gfh->brom_cfg.jump_bl_arm64 = GFH_BROM_CFG_JUMP_BL_ARM64;
++ cfg_bits |= GFH_BROM_CFG_JUMP_BL_ARM64_EN;
++ }
++ gfh->brom_cfg.cfg_bits = cpu_to_le32(cfg_bits);
+
+ /* GFH_BL_SEC_KEY header */
+ put_ghf_common_header(&gfh->bl_sec_key.gfh, sizeof(gfh->bl_sec_key),
+diff --git a/tools/mtk_image.h b/tools/mtk_image.h
+index 4e78b3d0ff..7dda71ce88 100644
+--- a/tools/mtk_image.h
++++ b/tools/mtk_image.h
+@@ -136,7 +136,9 @@ struct gfh_brom_cfg {
+ struct gfh_common_header gfh;
+ uint32_t cfg_bits;
+ uint32_t usbdl_by_auto_detect_timeout_ms;
+- uint8_t unused[0x48];
++ uint8_t unused[0x45];
++ uint8_t jump_bl_arm64;
++ uint8_t unused2[2];
+ uint32_t usbdl_by_kcol0_timeout_ms;
+ uint32_t usbdl_by_flag_timeout_ms;
+ uint32_t pad;
+@@ -146,6 +148,8 @@ struct gfh_brom_cfg {
+ #define GFH_BROM_CFG_USBDL_AUTO_DETECT_DIS 0x10
+ #define GFH_BROM_CFG_USBDL_BY_KCOL0_TIMEOUT_EN 0x80
+ #define GFH_BROM_CFG_USBDL_BY_FLAG_TIMEOUT_EN 0x100
++#define GFH_BROM_CFG_JUMP_BL_ARM64_EN 0x1000
++#define GFH_BROM_CFG_JUMP_BL_ARM64 0x64
+
+ struct gfh_bl_sec_key {
+ struct gfh_common_header gfh;
+--
+2.30.1
+
--- /dev/null
+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 <weijie.gao@mediatek.com>
+X-Patchwork-Id: 1449568
+Return-Path: <u-boot-bounces@lists.denx.de>
+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=<UNKNOWN>)
+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 <incoming@patchwork.ozlabs.org>; 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 <u-boot@lists.denx.de>; 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 <weijie.gao@mediatek.com>)
+ (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 <weijie.gao@mediatek.com>
+To: <u-boot@lists.denx.de>
+CC: GSS_MTK_Uboot_upstream <GSS_MTK_Uboot_upstream@mediatek.com>, Weijie Gao
+ <weijie.gao@mediatek.com>
+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 <u-boot.lists.denx.de>
+List-Unsubscribe: <https://lists.denx.de/options/u-boot>,
+ <mailto:u-boot-request@lists.denx.de?subject=unsubscribe>
+List-Archive: <https://lists.denx.de/pipermail/u-boot/>
+List-Post: <mailto:u-boot@lists.denx.de>
+List-Help: <mailto:u-boot-request@lists.denx.de?subject=help>
+List-Subscribe: <https://lists.denx.de/listinfo/u-boot>,
+ <mailto:u-boot-request@lists.denx.de?subject=subscribe>
+Errors-To: u-boot-bounces@lists.denx.de
+Sender: "U-Boot" <u-boot-bounces@lists.denx.de>
+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 <weijie.gao@mediatek.com>
+---
+ 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