1 // SPDX-License-Identifier: GPL-2.0+
12 #include <spi_flash.h>
14 #include <asm/mach-imx/image.h>
15 #include <asm/arch/sys_proto.h>
16 #include <asm/mach-imx/boot_mode.h>
21 #define QSPI_NOR_DEV 3
24 int get_container_size(ulong addr, u16 *header_length)
26 struct container_hdr *phdr;
27 struct boot_img_t *img_entry;
28 struct signature_block_hdr *sign_hdr;
30 u32 max_offset = 0, img_end;
32 phdr = (struct container_hdr *)addr;
33 if (phdr->tag != 0x87 && phdr->version != 0x0) {
34 debug("Wrong container header\n");
38 max_offset = phdr->length_lsb + (phdr->length_msb << 8);
40 *header_length = max_offset;
42 img_entry = (struct boot_img_t *)(addr + sizeof(struct container_hdr));
43 for (i = 0; i < phdr->num_images; i++) {
44 img_end = img_entry->offset + img_entry->size;
45 if (img_end > max_offset)
48 debug("img[%u], end = 0x%x\n", i, img_end);
53 if (phdr->sig_blk_offset != 0) {
54 sign_hdr = (struct signature_block_hdr *)(addr + phdr->sig_blk_offset);
55 u16 len = sign_hdr->length_lsb + (sign_hdr->length_msb << 8);
57 if (phdr->sig_blk_offset + len > max_offset)
58 max_offset = phdr->sig_blk_offset + len;
60 debug("sigblk, end = 0x%x\n", phdr->sig_blk_offset + len);
66 static int get_dev_container_size(void *dev, int dev_type, unsigned long offset, u16 *header_length)
68 u8 *buf = malloc(CONTAINER_HDR_ALIGNMENT);
72 printf("Malloc buffer failed\n");
77 if (dev_type == MMC_DEV) {
78 unsigned long count = 0;
79 struct mmc *mmc = (struct mmc *)dev;
81 count = blk_dread(mmc_get_blk_desc(mmc),
82 offset / mmc->read_bl_len,
83 CONTAINER_HDR_ALIGNMENT / mmc->read_bl_len,
86 printf("Read container image from MMC/SD failed\n");
92 #ifdef CONFIG_SPL_SPI_LOAD
93 if (dev_type == QSPI_DEV) {
94 struct spi_flash *flash = (struct spi_flash *)dev;
96 ret = spi_flash_read(flash, offset,
97 CONTAINER_HDR_ALIGNMENT, buf);
99 printf("Read container image from QSPI failed\n");
105 #ifdef CONFIG_SPL_NAND_SUPPORT
106 if (dev_type == NAND_DEV) {
107 ret = nand_spl_load_image(offset, CONTAINER_HDR_ALIGNMENT,
110 printf("Read container image from NAND failed\n");
116 #ifdef CONFIG_SPL_NOR_SUPPORT
117 if (dev_type == QSPI_NOR_DEV)
118 memcpy(buf, (const void *)offset, CONTAINER_HDR_ALIGNMENT);
121 #ifdef CONFIG_SPL_BOOTROM_SUPPORT
122 if (dev_type == ROM_API_DEV) {
123 ret = spl_romapi_raw_seekable_read(offset, CONTAINER_HDR_ALIGNMENT, buf);
125 printf("Read container image from ROM API failed\n");
131 ret = get_container_size((ulong)buf, header_length);
138 static unsigned long get_boot_device_offset(void *dev, int dev_type)
140 unsigned long offset = 0;
142 if (dev_type == MMC_DEV) {
143 struct mmc *mmc = (struct mmc *)dev;
145 if (IS_SD(mmc) || mmc->part_config == MMCPART_NOAVAILABLE) {
146 offset = CONTAINER_HDR_MMCSD_OFFSET;
148 u8 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
150 if (part == 1 || part == 2) {
151 if (is_imx8qxp() && is_soc_rev(CHIP_REV_B))
152 offset = CONTAINER_HDR_MMCSD_OFFSET;
154 offset = CONTAINER_HDR_EMMC_OFFSET;
156 offset = CONTAINER_HDR_MMCSD_OFFSET;
159 } else if (dev_type == QSPI_DEV) {
160 offset = CONTAINER_HDR_QSPI_OFFSET;
161 } else if (dev_type == NAND_DEV) {
162 offset = CONTAINER_HDR_NAND_OFFSET;
163 } else if (dev_type == QSPI_NOR_DEV) {
164 offset = CONTAINER_HDR_QSPI_OFFSET + 0x08000000;
165 } else if (dev_type == ROM_API_DEV) {
166 offset = (unsigned long)dev;
172 static int get_imageset_end(void *dev, int dev_type)
174 unsigned long offset1 = 0, offset2 = 0;
175 int value_container[2];
178 offset1 = get_boot_device_offset(dev, dev_type);
179 offset2 = CONTAINER_HDR_ALIGNMENT + offset1;
181 value_container[0] = get_dev_container_size(dev, dev_type, offset1, &hdr_length);
182 if (value_container[0] < 0) {
183 printf("Parse seco container failed %d\n", value_container[0]);
184 return value_container[0];
187 debug("seco container size 0x%x\n", value_container[0]);
189 value_container[1] = get_dev_container_size(dev, dev_type, offset2, &hdr_length);
190 if (value_container[1] < 0) {
191 debug("Parse scu container failed %d, only seco container\n",
193 /* return seco container total size */
194 return value_container[0] + offset1;
197 debug("scu container size 0x%x\n", value_container[1]);
199 return value_container[1] + offset2;
202 #ifdef CONFIG_SPL_SPI_LOAD
203 unsigned long spl_spi_get_uboot_offs(struct spi_flash *flash)
207 end = get_imageset_end(flash, QSPI_DEV);
208 end = ROUND(end, SZ_1K);
210 printf("Load image from QSPI 0x%x\n", end);
216 #ifdef CONFIG_SPL_MMC
217 unsigned long spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
218 unsigned long raw_sect)
222 end = get_imageset_end(mmc, MMC_DEV);
223 end = ROUND(end, SZ_1K);
225 printf("Load image from MMC/SD 0x%x\n", end);
227 return end / mmc->read_bl_len;
231 #ifdef CONFIG_SPL_NAND_SUPPORT
232 uint32_t spl_nand_get_uboot_raw_page(void)
236 end = get_imageset_end((void *)NULL, NAND_DEV);
237 end = ROUND(end, SZ_16K);
239 printf("Load image from NAND 0x%x\n", end);
245 #ifdef CONFIG_SPL_NOR_SUPPORT
246 unsigned long spl_nor_get_uboot_base(void)
250 /* Calculate the image set end,
251 * if it is less than CONFIG_SYS_UBOOT_BASE(0x8281000),
252 * we use CONFIG_SYS_UBOOT_BASE
253 * Otherwise, use the calculated address
255 end = get_imageset_end((void *)NULL, QSPI_NOR_DEV);
256 if (end <= CONFIG_SYS_UBOOT_BASE)
257 end = CONFIG_SYS_UBOOT_BASE;
259 end = ROUND(end, SZ_1K);
261 printf("Load image from NOR 0x%x\n", end);
267 #ifdef CONFIG_SPL_BOOTROM_SUPPORT
268 u32 __weak spl_arch_boot_image_offset(u32 image_offset, u32 rom_bt_dev)
273 ulong spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev)
277 image_offset = spl_arch_boot_image_offset(image_offset, rom_bt_dev);
279 end = get_imageset_end((void *)(ulong)image_offset, ROM_API_DEV);
280 end = ROUND(end, SZ_1K);
282 printf("Load image from 0x%lx by ROM_API\n", end);