1 // SPDX-License-Identifier: GPL-2.0+
11 #include <spi_flash.h>
13 #include <asm/arch/image.h>
14 #include <asm/arch/sys_proto.h>
15 #include <asm/mach-imx/boot_mode.h>
20 #define QSPI_NOR_DEV 3
22 static int __get_container_size(ulong addr)
24 struct container_hdr *phdr;
25 struct boot_img_t *img_entry;
26 struct signature_block_hdr *sign_hdr;
28 u32 max_offset = 0, img_end;
30 phdr = (struct container_hdr *)addr;
31 if (phdr->tag != 0x87 && phdr->version != 0x0) {
32 debug("Wrong container header\n");
36 max_offset = sizeof(struct container_hdr);
38 img_entry = (struct boot_img_t *)(addr + sizeof(struct container_hdr));
39 for (i = 0; i < phdr->num_images; i++) {
40 img_end = img_entry->offset + img_entry->size;
41 if (img_end > max_offset)
44 debug("img[%u], end = 0x%x\n", i, img_end);
49 if (phdr->sig_blk_offset != 0) {
50 sign_hdr = (struct signature_block_hdr *)(addr + phdr->sig_blk_offset);
51 u16 len = sign_hdr->length_lsb + (sign_hdr->length_msb << 8);
53 if (phdr->sig_blk_offset + len > max_offset)
54 max_offset = phdr->sig_blk_offset + len;
56 debug("sigblk, end = 0x%x\n", phdr->sig_blk_offset + len);
62 static int get_container_size(void *dev, int dev_type, unsigned long offset)
64 u8 *buf = malloc(CONTAINER_HDR_ALIGNMENT);
68 printf("Malloc buffer failed\n");
72 #ifdef CONFIG_SPL_MMC_SUPPORT
73 if (dev_type == MMC_DEV) {
74 unsigned long count = 0;
75 struct mmc *mmc = (struct mmc *)dev;
77 count = blk_dread(mmc_get_blk_desc(mmc),
78 offset / mmc->read_bl_len,
79 CONTAINER_HDR_ALIGNMENT / mmc->read_bl_len,
82 printf("Read container image from MMC/SD failed\n");
88 #ifdef CONFIG_SPL_SPI_LOAD
89 if (dev_type == QSPI_DEV) {
90 struct spi_flash *flash = (struct spi_flash *)dev;
92 ret = spi_flash_read(flash, offset,
93 CONTAINER_HDR_ALIGNMENT, buf);
95 printf("Read container image from QSPI failed\n");
101 #ifdef CONFIG_SPL_NAND_SUPPORT
102 if (dev_type == NAND_DEV) {
103 ret = nand_spl_load_image(offset, CONTAINER_HDR_ALIGNMENT,
106 printf("Read container image from NAND failed\n");
112 #ifdef CONFIG_SPL_NOR_SUPPORT
113 if (dev_type == QSPI_NOR_DEV)
114 memcpy(buf, (const void *)offset, CONTAINER_HDR_ALIGNMENT);
117 ret = __get_container_size((ulong)buf);
124 static unsigned long get_boot_device_offset(void *dev, int dev_type)
126 unsigned long offset = 0;
128 if (dev_type == MMC_DEV) {
129 struct mmc *mmc = (struct mmc *)dev;
131 if (IS_SD(mmc) || mmc->part_config == MMCPART_NOAVAILABLE) {
132 offset = CONTAINER_HDR_MMCSD_OFFSET;
134 u8 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
136 if (part == 1 || part == 2) {
137 if (is_imx8qxp() && is_soc_rev(CHIP_REV_B))
138 offset = CONTAINER_HDR_MMCSD_OFFSET;
140 offset = CONTAINER_HDR_EMMC_OFFSET;
142 offset = CONTAINER_HDR_MMCSD_OFFSET;
145 } else if (dev_type == QSPI_DEV) {
146 offset = CONTAINER_HDR_QSPI_OFFSET;
147 } else if (dev_type == NAND_DEV) {
148 offset = CONTAINER_HDR_NAND_OFFSET;
149 } else if (dev_type == QSPI_NOR_DEV) {
150 offset = CONTAINER_HDR_QSPI_OFFSET + 0x08000000;
156 static int get_imageset_end(void *dev, int dev_type)
158 unsigned long offset1 = 0, offset2 = 0;
159 int value_container[2];
161 offset1 = get_boot_device_offset(dev, dev_type);
162 offset2 = CONTAINER_HDR_ALIGNMENT + offset1;
164 value_container[0] = get_container_size(dev, dev_type, offset1);
165 if (value_container[0] < 0) {
166 printf("Parse seco container failed %d\n", value_container[0]);
167 return value_container[0];
170 debug("seco container size 0x%x\n", value_container[0]);
172 value_container[1] = get_container_size(dev, dev_type, offset2);
173 if (value_container[1] < 0) {
174 debug("Parse scu container failed %d, only seco container\n",
176 /* return seco container total size */
177 return value_container[0] + offset1;
180 debug("scu container size 0x%x\n", value_container[1]);
182 return value_container[1] + offset2;
185 #ifdef CONFIG_SPL_SPI_LOAD
186 unsigned long spl_spi_get_uboot_offs(struct spi_flash *flash)
190 end = get_imageset_end(flash, QSPI_DEV);
191 end = ROUND(end, SZ_1K);
193 printf("Load image from QSPI 0x%x\n", end);
199 #ifdef CONFIG_SPL_MMC_SUPPORT
200 unsigned long spl_mmc_get_uboot_raw_sector(struct mmc *mmc)
204 end = get_imageset_end(mmc, MMC_DEV);
205 end = ROUND(end, SZ_1K);
207 printf("Load image from MMC/SD 0x%x\n", end);
209 return end / mmc->read_bl_len;
213 #ifdef CONFIG_SPL_NAND_SUPPORT
214 uint32_t spl_nand_get_uboot_raw_page(void)
218 end = get_imageset_end((void *)NULL, NAND_DEV);
219 end = ROUND(end, SZ_16K);
221 printf("Load image from NAND 0x%x\n", end);
227 #ifdef CONFIG_SPL_NOR_SUPPORT
228 unsigned long spl_nor_get_uboot_base(void)
232 /* Calculate the image set end,
233 * if it is less than CONFIG_SYS_UBOOT_BASE(0x8281000),
234 * we use CONFIG_SYS_UBOOT_BASE
235 * Otherwise, use the calculated address
237 end = get_imageset_end((void *)NULL, QSPI_NOR_DEV);
238 if (end <= CONFIG_SYS_UBOOT_BASE)
239 end = CONFIG_SYS_UBOOT_BASE;
241 end = ROUND(end, SZ_1K);
243 printf("Load image from NOR 0x%x\n", end);