Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2019 NXP |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <errno.h> |
| 8 | #include <asm/io.h> |
| 9 | #include <mmc.h> |
| 10 | #include <spi_flash.h> |
| 11 | #include <nand.h> |
| 12 | #include <asm/arch/image.h> |
| 13 | #include <asm/arch/sys_proto.h> |
| 14 | #include <asm/mach-imx/boot_mode.h> |
| 15 | |
| 16 | #define MMC_DEV 0 |
| 17 | #define QSPI_DEV 1 |
| 18 | #define NAND_DEV 2 |
| 19 | #define QSPI_NOR_DEV 3 |
| 20 | |
| 21 | static int __get_container_size(ulong addr) |
| 22 | { |
| 23 | struct container_hdr *phdr; |
| 24 | struct boot_img_t *img_entry; |
| 25 | struct signature_block_hdr *sign_hdr; |
| 26 | u8 i = 0; |
| 27 | u32 max_offset = 0, img_end; |
| 28 | |
| 29 | phdr = (struct container_hdr *)addr; |
| 30 | if (phdr->tag != 0x87 && phdr->version != 0x0) { |
| 31 | debug("Wrong container header\n"); |
| 32 | return -EFAULT; |
| 33 | } |
| 34 | |
| 35 | max_offset = sizeof(struct container_hdr); |
| 36 | |
| 37 | img_entry = (struct boot_img_t *)(addr + sizeof(struct container_hdr)); |
| 38 | for (i = 0; i < phdr->num_images; i++) { |
| 39 | img_end = img_entry->offset + img_entry->size; |
| 40 | if (img_end > max_offset) |
| 41 | max_offset = img_end; |
| 42 | |
| 43 | debug("img[%u], end = 0x%x\n", i, img_end); |
| 44 | |
| 45 | img_entry++; |
| 46 | } |
| 47 | |
| 48 | if (phdr->sig_blk_offset != 0) { |
| 49 | sign_hdr = (struct signature_block_hdr *)(addr + phdr->sig_blk_offset); |
| 50 | u16 len = sign_hdr->length_lsb + (sign_hdr->length_msb << 8); |
| 51 | |
| 52 | if (phdr->sig_blk_offset + len > max_offset) |
| 53 | max_offset = phdr->sig_blk_offset + len; |
| 54 | |
| 55 | debug("sigblk, end = 0x%x\n", phdr->sig_blk_offset + len); |
| 56 | } |
| 57 | |
| 58 | return max_offset; |
| 59 | } |
| 60 | |
| 61 | static int get_container_size(void *dev, int dev_type, unsigned long offset) |
| 62 | { |
| 63 | u8 *buf = malloc(CONTAINER_HDR_ALIGNMENT); |
| 64 | int ret = 0; |
| 65 | |
| 66 | if (!buf) { |
| 67 | printf("Malloc buffer failed\n"); |
| 68 | return -ENOMEM; |
| 69 | } |
| 70 | |
| 71 | #ifdef CONFIG_SPL_MMC_SUPPORT |
| 72 | if (dev_type == MMC_DEV) { |
| 73 | unsigned long count = 0; |
| 74 | struct mmc *mmc = (struct mmc *)dev; |
| 75 | |
| 76 | count = blk_dread(mmc_get_blk_desc(mmc), |
| 77 | offset / mmc->read_bl_len, |
| 78 | CONTAINER_HDR_ALIGNMENT / mmc->read_bl_len, |
| 79 | buf); |
| 80 | if (count == 0) { |
| 81 | printf("Read container image from MMC/SD failed\n"); |
| 82 | return -EIO; |
| 83 | } |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | #ifdef CONFIG_SPL_SPI_LOAD |
| 88 | if (dev_type == QSPI_DEV) { |
| 89 | struct spi_flash *flash = (struct spi_flash *)dev; |
| 90 | |
| 91 | ret = spi_flash_read(flash, offset, |
| 92 | CONTAINER_HDR_ALIGNMENT, buf); |
| 93 | if (ret != 0) { |
| 94 | printf("Read container image from QSPI failed\n"); |
| 95 | return -EIO; |
| 96 | } |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | #ifdef CONFIG_SPL_NAND_SUPPORT |
| 101 | if (dev_type == NAND_DEV) { |
| 102 | ret = nand_spl_load_image(offset, CONTAINER_HDR_ALIGNMENT, |
| 103 | buf); |
| 104 | if (ret != 0) { |
| 105 | printf("Read container image from NAND failed\n"); |
| 106 | return -EIO; |
| 107 | } |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | #ifdef CONFIG_SPL_NOR_SUPPORT |
| 112 | if (dev_type == QSPI_NOR_DEV) |
| 113 | memcpy(buf, (const void *)offset, CONTAINER_HDR_ALIGNMENT); |
| 114 | #endif |
| 115 | |
| 116 | ret = __get_container_size((ulong)buf); |
| 117 | |
| 118 | free(buf); |
| 119 | |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | static unsigned long get_boot_device_offset(void *dev, int dev_type) |
| 124 | { |
| 125 | unsigned long offset = 0; |
| 126 | |
| 127 | if (dev_type == MMC_DEV) { |
| 128 | struct mmc *mmc = (struct mmc *)dev; |
| 129 | |
| 130 | if (IS_SD(mmc) || mmc->part_config == MMCPART_NOAVAILABLE) { |
| 131 | offset = CONTAINER_HDR_MMCSD_OFFSET; |
| 132 | } else { |
| 133 | u8 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config); |
| 134 | |
| 135 | if (part == 1 || part == 2) { |
| 136 | if (is_imx8qxp() && is_soc_rev(CHIP_REV_B)) |
| 137 | offset = CONTAINER_HDR_MMCSD_OFFSET; |
| 138 | else |
| 139 | offset = CONTAINER_HDR_EMMC_OFFSET; |
| 140 | } else { |
| 141 | offset = CONTAINER_HDR_MMCSD_OFFSET; |
| 142 | } |
| 143 | } |
| 144 | } else if (dev_type == QSPI_DEV) { |
| 145 | offset = CONTAINER_HDR_QSPI_OFFSET; |
| 146 | } else if (dev_type == NAND_DEV) { |
| 147 | offset = CONTAINER_HDR_NAND_OFFSET; |
| 148 | } else if (dev_type == QSPI_NOR_DEV) { |
| 149 | offset = CONTAINER_HDR_QSPI_OFFSET + 0x08000000; |
| 150 | } |
| 151 | |
| 152 | return offset; |
| 153 | } |
| 154 | |
| 155 | static int get_imageset_end(void *dev, int dev_type) |
| 156 | { |
| 157 | unsigned long offset1 = 0, offset2 = 0; |
| 158 | int value_container[2]; |
| 159 | |
| 160 | offset1 = get_boot_device_offset(dev, dev_type); |
| 161 | offset2 = CONTAINER_HDR_ALIGNMENT + offset1; |
| 162 | |
| 163 | value_container[0] = get_container_size(dev, dev_type, offset1); |
| 164 | if (value_container[0] < 0) { |
| 165 | printf("Parse seco container failed %d\n", value_container[0]); |
| 166 | return value_container[0]; |
| 167 | } |
| 168 | |
| 169 | debug("seco container size 0x%x\n", value_container[0]); |
| 170 | |
| 171 | value_container[1] = get_container_size(dev, dev_type, offset2); |
| 172 | if (value_container[1] < 0) { |
| 173 | debug("Parse scu container failed %d, only seco container\n", |
| 174 | value_container[1]); |
| 175 | /* return seco container total size */ |
| 176 | return value_container[0] + offset1; |
| 177 | } |
| 178 | |
| 179 | debug("scu container size 0x%x\n", value_container[1]); |
| 180 | |
| 181 | return value_container[1] + offset2; |
| 182 | } |
| 183 | |
| 184 | #ifdef CONFIG_SPL_SPI_LOAD |
| 185 | unsigned long spl_spi_get_uboot_offs(struct spi_flash *flash) |
| 186 | { |
| 187 | int end; |
| 188 | |
| 189 | end = get_imageset_end(flash, QSPI_DEV); |
| 190 | end = ROUND(end, SZ_1K); |
| 191 | |
| 192 | printf("Load image from QSPI 0x%x\n", end); |
| 193 | |
| 194 | return end; |
| 195 | } |
| 196 | #endif |
| 197 | |
| 198 | #ifdef CONFIG_SPL_MMC_SUPPORT |
| 199 | unsigned long spl_mmc_get_uboot_raw_sector(struct mmc *mmc) |
| 200 | { |
| 201 | int end; |
| 202 | |
| 203 | end = get_imageset_end(mmc, MMC_DEV); |
| 204 | end = ROUND(end, SZ_1K); |
| 205 | |
| 206 | printf("Load image from MMC/SD 0x%x\n", end); |
| 207 | |
| 208 | return end / mmc->read_bl_len; |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | #ifdef CONFIG_SPL_NAND_SUPPORT |
| 213 | uint32_t spl_nand_get_uboot_raw_page(void) |
| 214 | { |
| 215 | int end; |
| 216 | |
| 217 | end = get_imageset_end((void *)NULL, NAND_DEV); |
| 218 | end = ROUND(end, SZ_16K); |
| 219 | |
| 220 | printf("Load image from NAND 0x%x\n", end); |
| 221 | |
| 222 | return end; |
| 223 | } |
| 224 | #endif |
| 225 | |
| 226 | #ifdef CONFIG_SPL_NOR_SUPPORT |
| 227 | unsigned long spl_nor_get_uboot_base(void) |
| 228 | { |
| 229 | int end; |
| 230 | |
| 231 | /* Calculate the image set end, |
| 232 | * if it is less than CONFIG_SYS_UBOOT_BASE(0x8281000), |
| 233 | * we use CONFIG_SYS_UBOOT_BASE |
| 234 | * Otherwise, use the calculated address |
| 235 | */ |
| 236 | end = get_imageset_end((void *)NULL, QSPI_NOR_DEV); |
| 237 | if (end <= CONFIG_SYS_UBOOT_BASE) |
| 238 | end = CONFIG_SYS_UBOOT_BASE; |
| 239 | else |
| 240 | end = ROUND(end, SZ_1K); |
| 241 | |
| 242 | printf("Load image from NOR 0x%x\n", end); |
| 243 | |
| 244 | return end; |
| 245 | } |
| 246 | #endif |