]> Git Repo - J-u-boot.git/blobdiff - tools/mtk_image.c
tools: kwbimage: Set BOOT_FROM by default to SPI
[J-u-boot.git] / tools / mtk_image.c
index 2ca519483d33dd7ae7ed8d264afd4d92684e064c..418c5fd54b472e3b21549fbddb93f1131cbb7959 100644 (file)
@@ -243,9 +243,15 @@ 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;
 
 /* LK image name */
 static char lk_name[32] = "U-Boot";
@@ -274,8 +280,10 @@ 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 = "";
 
        key = buf;
        while (key) {
@@ -315,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;
 
@@ -323,6 +334,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 +368,13 @@ 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;
+
        free(buf);
 
        if (hdr_media == BRLYT_TYPE_INVALID) {
@@ -414,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;
@@ -445,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;
@@ -458,6 +505,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 +573,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;
 }
 
@@ -535,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
@@ -581,6 +636,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 +665,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),
@@ -662,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;
 
@@ -727,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
This page took 0.031836 seconds and 4 git commands to generate.