From 7d84978c9cbbb7b06986a14dbdd4f1ee597a6aab Mon Sep 17 00:00:00 2001 From: Ye Li Date: Sat, 7 Aug 2021 16:00:36 +0800 Subject: arm: imx8: Move container parser and image to mach-imx common folder Since we will re-use the container parser on imx8ulp, move the codes to mach-imx Signed-off-by: Ye Li --- arch/arm/mach-imx/image-container.c | 249 ++++++++++++++++++++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 arch/arm/mach-imx/image-container.c (limited to 'arch/arm/mach-imx/image-container.c') diff --git a/arch/arm/mach-imx/image-container.c b/arch/arm/mach-imx/image-container.c new file mode 100644 index 00000000000..5abc0d3a39f --- /dev/null +++ b/arch/arm/mach-imx/image-container.c @@ -0,0 +1,249 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2019 NXP + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MMC_DEV 0 +#define QSPI_DEV 1 +#define NAND_DEV 2 +#define QSPI_NOR_DEV 3 + +static int __get_container_size(ulong addr) +{ + struct container_hdr *phdr; + struct boot_img_t *img_entry; + struct signature_block_hdr *sign_hdr; + u8 i = 0; + u32 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); + u16 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 get_container_size(void *dev, int dev_type, unsigned long offset) +{ + u8 *buf = malloc(CONTAINER_HDR_ALIGNMENT); + int ret = 0; + + if (!buf) { + printf("Malloc buffer failed\n"); + return -ENOMEM; + } + +#ifdef CONFIG_SPL_MMC_SUPPORT + if (dev_type == MMC_DEV) { + unsigned long count = 0; + struct mmc *mmc = (struct mmc *)dev; + + count = blk_dread(mmc_get_blk_desc(mmc), + 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; + } + } +#endif + +#ifdef CONFIG_SPL_SPI_LOAD + if (dev_type == QSPI_DEV) { + struct spi_flash *flash = (struct spi_flash *)dev; + + ret = spi_flash_read(flash, offset, + CONTAINER_HDR_ALIGNMENT, buf); + if (ret != 0) { + printf("Read container image from QSPI failed\n"); + return -EIO; + } + } +#endif + +#ifdef CONFIG_SPL_NAND_SUPPORT + if (dev_type == NAND_DEV) { + ret = nand_spl_load_image(offset, CONTAINER_HDR_ALIGNMENT, + buf); + if (ret != 0) { + printf("Read container image from NAND failed\n"); + return -EIO; + } + } +#endif + +#ifdef CONFIG_SPL_NOR_SUPPORT + if (dev_type == QSPI_NOR_DEV) + memcpy(buf, (const void *)offset, CONTAINER_HDR_ALIGNMENT); +#endif + + ret = __get_container_size((ulong)buf); + + free(buf); + + return ret; +} + +static unsigned long get_boot_device_offset(void *dev, int dev_type) +{ + unsigned long offset = 0; + + if (dev_type == MMC_DEV) { + struct mmc *mmc = (struct mmc *)dev; + + if (IS_SD(mmc) || mmc->part_config == MMCPART_NOAVAILABLE) { + offset = CONTAINER_HDR_MMCSD_OFFSET; + } else { + u8 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config); + + if (part == 1 || part == 2) { + if (is_imx8qxp() && is_soc_rev(CHIP_REV_B)) + offset = CONTAINER_HDR_MMCSD_OFFSET; + else + offset = CONTAINER_HDR_EMMC_OFFSET; + } else { + offset = CONTAINER_HDR_MMCSD_OFFSET; + } + } + } else if (dev_type == QSPI_DEV) { + offset = CONTAINER_HDR_QSPI_OFFSET; + } else if (dev_type == NAND_DEV) { + offset = CONTAINER_HDR_NAND_OFFSET; + } else if (dev_type == QSPI_NOR_DEV) { + offset = CONTAINER_HDR_QSPI_OFFSET + 0x08000000; + } + + return offset; +} + +static int get_imageset_end(void *dev, int dev_type) +{ + unsigned long offset1 = 0, offset2 = 0; + int value_container[2]; + + offset1 = get_boot_device_offset(dev, dev_type); + offset2 = CONTAINER_HDR_ALIGNMENT + offset1; + + value_container[0] = get_container_size(dev, dev_type, offset1); + 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]); + + value_container[1] = get_container_size(dev, dev_type, offset2); + if (value_container[1] < 0) { + debug("Parse scu container failed %d, only seco container\n", + value_container[1]); + /* return seco container total size */ + return value_container[0] + offset1; + } + + debug("scu container size 0x%x\n", value_container[1]); + + return value_container[1] + offset2; +} + +#ifdef CONFIG_SPL_SPI_LOAD +unsigned long spl_spi_get_uboot_offs(struct spi_flash *flash) +{ + int end; + + end = get_imageset_end(flash, QSPI_DEV); + end = ROUND(end, SZ_1K); + + printf("Load image from QSPI 0x%x\n", end); + + return end; +} +#endif + +#ifdef CONFIG_SPL_MMC_SUPPORT +unsigned long spl_mmc_get_uboot_raw_sector(struct mmc *mmc, + unsigned long raw_sect) +{ + int end; + + end = get_imageset_end(mmc, MMC_DEV); + end = ROUND(end, SZ_1K); + + printf("Load image from MMC/SD 0x%x\n", end); + + return end / mmc->read_bl_len; +} +#endif + +#ifdef CONFIG_SPL_NAND_SUPPORT +uint32_t spl_nand_get_uboot_raw_page(void) +{ + int end; + + end = get_imageset_end((void *)NULL, NAND_DEV); + end = ROUND(end, SZ_16K); + + printf("Load image from NAND 0x%x\n", end); + + return end; +} +#endif + +#ifdef CONFIG_SPL_NOR_SUPPORT +unsigned long spl_nor_get_uboot_base(void) +{ + int end; + + /* Calculate the image set end, + * if it is less than CONFIG_SYS_UBOOT_BASE(0x8281000), + * we use CONFIG_SYS_UBOOT_BASE + * Otherwise, use the calculated address + */ + end = get_imageset_end((void *)NULL, QSPI_NOR_DEV); + if (end <= CONFIG_SYS_UBOOT_BASE) + end = CONFIG_SYS_UBOOT_BASE; + else + end = ROUND(end, SZ_1K); + + printf("Load image from NOR 0x%x\n", end); + + return end; +} +#endif -- cgit v1.2.3 From 31f00852855e4d76f4c19ae47732ba416dd21098 Mon Sep 17 00:00:00 2001 From: Ye Li Date: Sat, 7 Aug 2021 16:00:37 +0800 Subject: arm: imx8: Move container image header file to mach-imx Since the container is shared among i.MX platforms, move its header file to mach-imx Signed-off-by: Ye Li Signed-off-by: Peng Fan --- arch/arm/mach-imx/image-container.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/arm/mach-imx/image-container.c') diff --git a/arch/arm/mach-imx/image-container.c b/arch/arm/mach-imx/image-container.c index 5abc0d3a39f..9e18f6630fc 100644 --- a/arch/arm/mach-imx/image-container.c +++ b/arch/arm/mach-imx/image-container.c @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include -- cgit v1.2.3 From 6f3858d732985f13c764ec29c1668d9f06941b6c Mon Sep 17 00:00:00 2001 From: Ye Li Date: Sat, 7 Aug 2021 16:00:39 +0800 Subject: arm: imx8ulp: add container support i.MX8ULP support using ROM API to load container image, it use same ROM API as i.MX8MN/MP, and use same container format as i.MX8QM/QXP. Signed-off-by: Ye Li Signed-off-by: Peng Fan --- arch/arm/mach-imx/image-container.c | 42 +++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'arch/arm/mach-imx/image-container.c') diff --git a/arch/arm/mach-imx/image-container.c b/arch/arm/mach-imx/image-container.c index 9e18f6630fc..c3f62872c6f 100644 --- a/arch/arm/mach-imx/image-container.c +++ b/arch/arm/mach-imx/image-container.c @@ -19,8 +19,9 @@ #define QSPI_DEV 1 #define NAND_DEV 2 #define QSPI_NOR_DEV 3 +#define ROM_API_DEV 4 -static int __get_container_size(ulong addr) +int get_container_size(ulong addr, u16 *header_length) { struct container_hdr *phdr; struct boot_img_t *img_entry; @@ -34,7 +35,9 @@ static int __get_container_size(ulong addr) return -EFAULT; } - max_offset = sizeof(struct container_hdr); + max_offset = phdr->length_lsb + (phdr->length_msb << 8); + if (header_length) + *header_length = max_offset; img_entry = (struct boot_img_t *)(addr + sizeof(struct container_hdr)); for (i = 0; i < phdr->num_images; i++) { @@ -60,7 +63,7 @@ static int __get_container_size(ulong addr) return max_offset; } -static int get_container_size(void *dev, int dev_type, unsigned long offset) +static int get_dev_container_size(void *dev, int dev_type, unsigned long offset, u16 *header_length) { u8 *buf = malloc(CONTAINER_HDR_ALIGNMENT); int ret = 0; @@ -115,7 +118,17 @@ static int get_container_size(void *dev, int dev_type, unsigned long offset) memcpy(buf, (const void *)offset, CONTAINER_HDR_ALIGNMENT); #endif - ret = __get_container_size((ulong)buf); +#ifdef CONFIG_SPL_BOOTROM_SUPPORT + if (dev_type == ROM_API_DEV) { + ret = spl_romapi_raw_seekable_read(offset, CONTAINER_HDR_ALIGNMENT, buf); + if (!ret) { + printf("Read container image from ROM API failed\n"); + return -EIO; + } + } +#endif + + ret = get_container_size((ulong)buf, header_length); free(buf); @@ -149,6 +162,8 @@ static unsigned long get_boot_device_offset(void *dev, int dev_type) offset = CONTAINER_HDR_NAND_OFFSET; } else if (dev_type == QSPI_NOR_DEV) { offset = CONTAINER_HDR_QSPI_OFFSET + 0x08000000; + } else if (dev_type == ROM_API_DEV) { + offset = (unsigned long)dev; } return offset; @@ -158,11 +173,12 @@ static int get_imageset_end(void *dev, int dev_type) { unsigned long offset1 = 0, offset2 = 0; int value_container[2]; + u16 hdr_length; offset1 = get_boot_device_offset(dev, dev_type); offset2 = CONTAINER_HDR_ALIGNMENT + offset1; - value_container[0] = get_container_size(dev, dev_type, offset1); + value_container[0] = get_dev_container_size(dev, dev_type, offset1, &hdr_length); if (value_container[0] < 0) { printf("Parse seco container failed %d\n", value_container[0]); return value_container[0]; @@ -170,7 +186,7 @@ static int get_imageset_end(void *dev, int dev_type) debug("seco container size 0x%x\n", value_container[0]); - value_container[1] = get_container_size(dev, dev_type, offset2); + value_container[1] = get_dev_container_size(dev, dev_type, offset2, &hdr_length); if (value_container[1] < 0) { debug("Parse scu container failed %d, only seco container\n", value_container[1]); @@ -247,3 +263,17 @@ unsigned long spl_nor_get_uboot_base(void) return end; } #endif + +#ifdef CONFIG_SPL_BOOTROM_SUPPORT +ulong spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev) +{ + ulong end; + + end = get_imageset_end((void *)(ulong)image_offset, ROM_API_DEV); + end = ROUND(end, SZ_1K); + + printf("Load image from 0x%lx by ROM_API\n", end); + + return end; +} +#endif -- cgit v1.2.3 From e8b68048e1deca93976a2656faf778f877a208b2 Mon Sep 17 00:00:00 2001 From: Ye Li Date: Sat, 7 Aug 2021 16:01:08 +0800 Subject: imx8ulp: Add workaround for eMMC boot When booting from boot part1/2, the image offset should be 0, but ROM has a bug to return 0x8000. Has to workaround the issue before ROM fix it. Use a ROM function to know boot from emmc boot part or user part So we can set the image offset accordingly. Signed-off-by: Ye Li --- arch/arm/mach-imx/image-container.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'arch/arm/mach-imx/image-container.c') diff --git a/arch/arm/mach-imx/image-container.c b/arch/arm/mach-imx/image-container.c index c3f62872c6f..68b30bcfc59 100644 --- a/arch/arm/mach-imx/image-container.c +++ b/arch/arm/mach-imx/image-container.c @@ -265,10 +265,17 @@ unsigned long spl_nor_get_uboot_base(void) #endif #ifdef CONFIG_SPL_BOOTROM_SUPPORT +u32 __weak spl_arch_boot_image_offset(u32 image_offset, u32 rom_bt_dev) +{ + return image_offset; +} + ulong spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev) { ulong end; + image_offset = spl_arch_boot_image_offset(image_offset, rom_bt_dev); + end = get_imageset_end((void *)(ulong)image_offset, ROM_API_DEV); end = ROUND(end, SZ_1K); -- cgit v1.2.3