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