Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [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 <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 11 | #include <linux/libfdt.h> |
| 12 | #include <spl.h> |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 13 | #include <asm/mach-imx/image.h> |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 14 | #include <asm/arch/sys_proto.h> |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 18 | /* Caller need ensure the offset and size to align with page size */ |
| 19 | ulong spl_romapi_raw_seekable_read(u32 offset, u32 size, void *buf) |
| 20 | { |
| 21 | volatile gd_t *pgd = gd; |
| 22 | int ret; |
| 23 | |
| 24 | debug("%s 0x%x, size 0x%x\n", __func__, offset, size); |
| 25 | |
| 26 | ret = g_rom_api->download_image(buf, offset, size, |
| 27 | ((uintptr_t)buf) ^ offset ^ size); |
| 28 | |
| 29 | set_gd(pgd); |
| 30 | |
| 31 | if (ret == ROM_API_OKAY) |
| 32 | return size; |
| 33 | |
| 34 | printf("%s Failure when load 0x%x, size 0x%x\n", __func__, offset, size); |
| 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | ulong __weak spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev) |
| 40 | { |
Marek Vasut | 8510756 | 2022-03-09 17:09:45 +0100 | [diff] [blame] | 41 | return image_offset + |
| 42 | (CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512 - 0x8000); |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 43 | } |
| 44 | |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 45 | static int is_boot_from_stream_device(u32 boot) |
| 46 | { |
| 47 | u32 interface; |
| 48 | |
| 49 | interface = boot >> 16; |
| 50 | if (interface >= BT_DEV_TYPE_USB) |
| 51 | return 1; |
| 52 | |
| 53 | if (interface == BT_DEV_TYPE_MMC && (boot & 1)) |
| 54 | return 1; |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static ulong spl_romapi_read_seekable(struct spl_load_info *load, |
| 60 | ulong sector, ulong count, |
| 61 | void *buf) |
| 62 | { |
| 63 | u32 pagesize = *(u32 *)load->priv; |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 64 | ulong byte = count * pagesize; |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 65 | u32 offset; |
| 66 | |
| 67 | offset = sector * pagesize; |
| 68 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 69 | return spl_romapi_raw_seekable_read(offset, byte, buf) / pagesize; |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image, |
| 73 | struct spl_boot_device *bootdev, |
| 74 | u32 rom_bt_dev) |
| 75 | { |
| 76 | volatile gd_t *pgd = gd; |
| 77 | int ret; |
| 78 | u32 offset; |
| 79 | u32 pagesize, size; |
| 80 | struct image_header *header; |
| 81 | u32 image_offset; |
| 82 | |
| 83 | ret = g_rom_api->query_boot_infor(QUERY_IVT_OFF, &offset, |
| 84 | ((uintptr_t)&offset) ^ QUERY_IVT_OFF); |
| 85 | ret |= g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize, |
| 86 | ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ); |
| 87 | ret |= g_rom_api->query_boot_infor(QUERY_IMG_OFF, &image_offset, |
| 88 | ((uintptr_t)&image_offset) ^ QUERY_IMG_OFF); |
| 89 | |
Marek Behún | a8cf155 | 2021-05-20 13:24:10 +0200 | [diff] [blame] | 90 | set_gd(pgd); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 91 | |
| 92 | if (ret != ROM_API_OKAY) { |
| 93 | puts("ROMAPI: Failure query boot infor pagesize/offset\n"); |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | header = (struct image_header *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR); |
| 98 | |
| 99 | printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n", |
| 100 | image_offset, pagesize, offset); |
| 101 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 102 | offset = spl_romapi_get_uboot_base(image_offset, rom_bt_dev); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 103 | |
| 104 | size = ALIGN(sizeof(struct image_header), pagesize); |
| 105 | ret = g_rom_api->download_image((u8 *)header, offset, size, |
| 106 | ((uintptr_t)header) ^ offset ^ size); |
Marek Behún | a8cf155 | 2021-05-20 13:24:10 +0200 | [diff] [blame] | 107 | set_gd(pgd); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 108 | |
| 109 | if (ret != ROM_API_OKAY) { |
| 110 | printf("ROMAPI: download failure offset 0x%x size 0x%x\n", |
| 111 | offset, size); |
| 112 | return -1; |
| 113 | } |
| 114 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 115 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && image_get_magic(header) == FDT_MAGIC) { |
| 116 | struct spl_load_info load; |
| 117 | |
| 118 | memset(&load, 0, sizeof(load)); |
| 119 | load.bl_len = pagesize; |
| 120 | load.read = spl_romapi_read_seekable; |
| 121 | load.priv = &pagesize; |
| 122 | return spl_load_simple_fit(spl_image, &load, offset / pagesize, header); |
| 123 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) { |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 124 | struct spl_load_info load; |
| 125 | |
| 126 | memset(&load, 0, sizeof(load)); |
| 127 | load.bl_len = pagesize; |
| 128 | load.read = spl_romapi_read_seekable; |
| 129 | load.priv = &pagesize; |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 130 | |
| 131 | ret = spl_load_imx_container(spl_image, &load, offset / pagesize); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 132 | } else { |
| 133 | /* TODO */ |
| 134 | puts("Can't support legacy image\n"); |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector, |
| 142 | ulong count, void *buf) |
| 143 | { |
| 144 | memcpy(buf, (void *)(sector), count); |
| 145 | |
| 146 | if (load->priv) { |
| 147 | ulong *p = (ulong *)load->priv; |
| 148 | ulong total = sector + count; |
| 149 | |
| 150 | if (total > *p) |
| 151 | *p = total; |
| 152 | } |
| 153 | |
| 154 | return count; |
| 155 | } |
| 156 | |
| 157 | static ulong get_fit_image_size(void *fit) |
| 158 | { |
| 159 | struct spl_image_info spl_image; |
| 160 | struct spl_load_info spl_load_info; |
| 161 | ulong last = (ulong)fit; |
| 162 | |
| 163 | memset(&spl_load_info, 0, sizeof(spl_load_info)); |
| 164 | spl_load_info.bl_len = 1; |
| 165 | spl_load_info.read = spl_ram_load_read; |
| 166 | spl_load_info.priv = &last; |
| 167 | |
| 168 | spl_load_simple_fit(&spl_image, &spl_load_info, |
| 169 | (uintptr_t)fit, fit); |
| 170 | |
| 171 | return last - (ulong)fit; |
| 172 | } |
| 173 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 174 | static u8 *search_fit_header(u8 *p, int size) |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 175 | { |
| 176 | int i; |
| 177 | |
| 178 | for (i = 0; i < size; i += 4) |
| 179 | if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT) |
| 180 | return p + i; |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 181 | |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | static u8 *search_container_header(u8 *p, int size) |
| 186 | { |
| 187 | int i = 0; |
| 188 | u8 *hdr; |
| 189 | |
| 190 | for (i = 0; i < size; i += 4) { |
| 191 | hdr = p + i; |
| 192 | if (*(hdr + 3) == 0x87 && *hdr == 0 && (*(hdr + 1) != 0 || *(hdr + 2) != 0)) |
| 193 | return p + i; |
| 194 | } |
| 195 | |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | static u8 *search_img_header(u8 *p, int size) |
| 200 | { |
| 201 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) |
| 202 | return search_fit_header(p, size); |
| 203 | else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) |
| 204 | return search_container_header(p, size); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 205 | |
| 206 | return NULL; |
| 207 | } |
| 208 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 209 | static u32 img_header_size(void) |
| 210 | { |
| 211 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) |
| 212 | return sizeof(struct fdt_header); |
| 213 | else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) |
| 214 | return sizeof(struct container_hdr); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int img_info_size(void *img_hdr) |
| 220 | { |
| 221 | #ifdef CONFIG_SPL_LOAD_FIT |
| 222 | return fit_get_size(img_hdr); |
| 223 | #elif defined CONFIG_SPL_LOAD_IMX_CONTAINER |
| 224 | struct container_hdr *container = img_hdr; |
| 225 | |
| 226 | return (container->length_lsb + (container->length_msb << 8)); |
| 227 | #else |
| 228 | return 0; |
| 229 | #endif |
| 230 | } |
| 231 | |
| 232 | static int img_total_size(void *img_hdr) |
| 233 | { |
| 234 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) { |
| 235 | return get_fit_image_size(img_hdr); |
| 236 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) { |
| 237 | int total = get_container_size((ulong)img_hdr, NULL); |
| 238 | |
| 239 | if (total < 0) { |
| 240 | printf("invalid container image\n"); |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | return total; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 250 | static int spl_romapi_load_image_stream(struct spl_image_info *spl_image, |
| 251 | struct spl_boot_device *bootdev) |
| 252 | { |
| 253 | struct spl_load_info load; |
| 254 | volatile gd_t *pgd = gd; |
| 255 | u32 pagesize, pg; |
| 256 | int ret; |
| 257 | int i = 0; |
| 258 | u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR; |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 259 | u8 *phdr = NULL; |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 260 | int imagesize; |
| 261 | int total; |
| 262 | |
| 263 | ret = g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize, |
| 264 | ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ); |
Marek Behún | a8cf155 | 2021-05-20 13:24:10 +0200 | [diff] [blame] | 265 | set_gd(pgd); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 266 | |
| 267 | if (ret != ROM_API_OKAY) |
| 268 | puts("failure at query_boot_info\n"); |
| 269 | |
| 270 | pg = pagesize; |
| 271 | if (pg < 1024) |
| 272 | pg = 1024; |
| 273 | |
| 274 | for (i = 0; i < 640; i++) { |
| 275 | ret = g_rom_api->download_image(p, 0, pg, |
| 276 | ((uintptr_t)p) ^ pg); |
Marek Behún | a8cf155 | 2021-05-20 13:24:10 +0200 | [diff] [blame] | 277 | set_gd(pgd); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 278 | |
| 279 | if (ret != ROM_API_OKAY) { |
| 280 | puts("Steam(USB) download failure\n"); |
| 281 | return -1; |
| 282 | } |
| 283 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 284 | phdr = search_img_header(p, pg); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 285 | p += pg; |
| 286 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 287 | if (phdr) |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 291 | if (!phdr) { |
| 292 | puts("Can't found uboot image in 640K range\n"); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 293 | return -1; |
| 294 | } |
| 295 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 296 | if (p - phdr < img_header_size()) { |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 297 | ret = g_rom_api->download_image(p, 0, pg, ((uintptr_t)p) ^ pg); |
Marek Behún | a8cf155 | 2021-05-20 13:24:10 +0200 | [diff] [blame] | 298 | set_gd(pgd); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 299 | |
| 300 | if (ret != ROM_API_OKAY) { |
| 301 | puts("Steam(USB) download failure\n"); |
| 302 | return -1; |
| 303 | } |
| 304 | |
| 305 | p += pg; |
| 306 | } |
| 307 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 308 | imagesize = img_info_size(phdr); |
| 309 | printf("Find img info 0x&%p, size %d\n", phdr, imagesize); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 310 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 311 | if (p - phdr < imagesize) { |
| 312 | imagesize -= p - phdr; |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 313 | /*need pagesize hear after ROM fix USB problme*/ |
| 314 | imagesize += pg - 1; |
| 315 | imagesize /= pg; |
| 316 | imagesize *= pg; |
| 317 | |
| 318 | printf("Need continue download %d\n", imagesize); |
| 319 | |
| 320 | ret = g_rom_api->download_image(p, 0, imagesize, |
| 321 | ((uintptr_t)p) ^ imagesize); |
Marek Behún | a8cf155 | 2021-05-20 13:24:10 +0200 | [diff] [blame] | 322 | set_gd(pgd); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 323 | |
| 324 | p += imagesize; |
| 325 | |
| 326 | if (ret != ROM_API_OKAY) { |
| 327 | printf("Failure download %d\n", imagesize); |
| 328 | return -1; |
| 329 | } |
| 330 | } |
| 331 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 332 | total = img_total_size(phdr); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 333 | total += 3; |
| 334 | total &= ~0x3; |
| 335 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 336 | imagesize = total - (p - phdr); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 337 | |
| 338 | imagesize += pagesize - 1; |
| 339 | imagesize /= pagesize; |
| 340 | imagesize *= pagesize; |
| 341 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 342 | printf("Download %d, Total size %d\n", imagesize, total); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 343 | |
| 344 | ret = g_rom_api->download_image(p, 0, imagesize, |
| 345 | ((uintptr_t)p) ^ imagesize); |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 346 | set_gd(pgd); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 347 | if (ret != ROM_API_OKAY) |
| 348 | printf("ROM download failure %d\n", imagesize); |
| 349 | |
| 350 | memset(&load, 0, sizeof(load)); |
| 351 | load.bl_len = 1; |
| 352 | load.read = spl_ram_load_read; |
| 353 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 354 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) |
| 355 | return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr); |
| 356 | else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) |
| 357 | return spl_load_imx_container(spl_image, &load, (ulong)phdr); |
| 358 | |
| 359 | return -1; |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | int board_return_to_bootrom(struct spl_image_info *spl_image, |
| 363 | struct spl_boot_device *bootdev) |
| 364 | { |
| 365 | volatile gd_t *pgd = gd; |
| 366 | int ret; |
| 367 | u32 boot; |
| 368 | |
| 369 | ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot, |
| 370 | ((uintptr_t)&boot) ^ QUERY_BT_DEV); |
Marek Behún | a8cf155 | 2021-05-20 13:24:10 +0200 | [diff] [blame] | 371 | set_gd(pgd); |
Peng Fan | 3698679 | 2019-09-16 03:09:31 +0000 | [diff] [blame] | 372 | |
| 373 | if (ret != ROM_API_OKAY) { |
| 374 | puts("ROMAPI: failure at query_boot_info\n"); |
| 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | if (is_boot_from_stream_device(boot)) |
| 379 | return spl_romapi_load_image_stream(spl_image, bootdev); |
| 380 | |
| 381 | return spl_romapi_load_image_seekable(spl_image, bootdev, boot); |
| 382 | } |