MLK-19219-2 imx8qm/qxp: Add support to get container image set size
authorYe Li <ye.li@nxp.com>
Tue, 14 Aug 2018 10:45:30 +0000 (03:45 -0700)
committerYe Li <ye.li@nxp.com>
Fri, 24 May 2019 09:36:38 +0000 (02:36 -0700)
Add relevant functions and files to parse the container image set from mmc/sd
and get the total size of it. So we can get the offset of u-boot-atf.bin image
when it is padded to container image set at 1KB alignment position.

Signed-off-by: Ye Li <ye.li@nxp.com>
(cherry picked from commit 02b9874427beffe6536e38519037e36f76b2ec9e)

arch/arm/mach-imx/imx8/Makefile
arch/arm/mach-imx/imx8/image.c [new file with mode: 0644]

index e5404c6..2e7bcd9 100644 (file)
@@ -5,6 +5,7 @@
 #
 
 obj-y += cpu.o iomux.o fuse.o lpcg.o clock.o
+obj-y += image.o
 ifneq ($(CONFIG_SPL_BUILD),y)
 obj-y += partition.o
 endif
diff --git a/arch/arm/mach-imx/imx8/image.c b/arch/arm/mach-imx/imx8/image.c
new file mode 100644 (file)
index 0000000..1997a3c
--- /dev/null
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2018 NXP
+ */
+
+#include <common.h>
+#include <errno.h>
+#include <asm/io.h>
+#include <dm.h>
+#include <mmc.h>
+#include <asm/arch/image.h>
+
+static int get_container_size(ulong addr)
+{
+       struct container_hdr *phdr;
+       struct boot_img_t *img_entry;
+       struct signature_block_hdr *sign_hdr;
+       uint8_t i = 0;
+       uint32_t max_offset = 0, img_end;
+
+       phdr = (struct container_hdr *)addr;
+       if (phdr->tag != 0x87 && phdr->version != 0x0) {
+               debug("Wrong container header\n");
+               return -EFAULT;
+       }
+
+       max_offset = sizeof(struct container_hdr);
+
+       img_entry = (struct boot_img_t *)(addr + sizeof(struct container_hdr));
+       for (i=0; i< phdr->num_images; i++) {
+               img_end = img_entry->offset + img_entry->size;
+               if (img_end > max_offset)
+                       max_offset = img_end;
+
+               debug("img[%u], end = 0x%x\n", i, img_end);
+
+               img_entry++;
+       }
+
+       if (phdr->sig_blk_offset != 0) {
+               sign_hdr = (struct signature_block_hdr *)(addr + phdr->sig_blk_offset);
+               uint16_t len = sign_hdr->length_lsb + (sign_hdr->length_msb << 8);
+
+               if (phdr->sig_blk_offset + len > max_offset)
+                       max_offset = phdr->sig_blk_offset + len;
+
+               debug("sigblk, end = 0x%x\n", phdr->sig_blk_offset + len);
+       }
+
+       return max_offset;
+}
+
+static int mmc_get_imageset_end(struct mmc *mmc)
+{
+       int value_container[2];
+       unsigned long count;
+       uint8_t *buf = malloc(CONTAINER_HDR_ALIGNMENT);
+       if (!buf) {
+               printf("Malloc buffer failed\n");
+               return -ENOMEM;
+       }
+
+       count = blk_dread(mmc_get_blk_desc(mmc), CONTAINER_HDR_MMCSD_OFFSET/mmc->read_bl_len, CONTAINER_HDR_ALIGNMENT/mmc->read_bl_len, buf);
+       if (count == 0) {
+               printf("Read container image from MMC/SD failed\n");
+               return -EIO;
+       }
+
+       value_container[0] = get_container_size((ulong)buf);
+       if (value_container[0] < 0) {
+               printf("Parse seco container failed %d\n", value_container[0]);
+               return value_container[0];
+       }
+
+       debug("seco container size 0x%x\n", value_container[0]);
+
+       count = blk_dread(mmc_get_blk_desc(mmc), (CONTAINER_HDR_ALIGNMENT + CONTAINER_HDR_MMCSD_OFFSET)/mmc->read_bl_len,
+               CONTAINER_HDR_ALIGNMENT/mmc->read_bl_len, buf);
+       if (count == 0) {
+               printf("Read container image from MMC/SD failed\n");
+               return -EIO;
+       }
+
+       value_container[1] = get_container_size((ulong)buf);
+       if (value_container[1] < 0) {
+               debug("Parse scu container image failed %d, only seco container\n", value_container[1]);
+               return value_container[0] + CONTAINER_HDR_MMCSD_OFFSET; /* return seco container total size */
+       }
+
+       debug("scu container size 0x%x\n", value_container[1]);
+
+       return value_container[1] + (CONTAINER_HDR_ALIGNMENT + CONTAINER_HDR_MMCSD_OFFSET);
+}
+
+#ifdef CONFIG_SPL_MMC_SUPPORT
+unsigned long spl_mmc_get_uboot_raw_sector(struct mmc *mmc)
+{
+       int end;
+
+       end = mmc_get_imageset_end(mmc);
+       end = ROUND(end, SZ_1K);
+
+       printf("Load image from MMC/SD 0x%x\n", end);
+
+       return end/mmc->read_bl_len;
+}
+#endif