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> |
| 13 | |
| 14 | #include <asm/arch/sys_proto.h> |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | static int is_boot_from_stream_device(u32 boot) |
| 19 | { |
| 20 | u32 interface; |
| 21 | |
| 22 | interface = boot >> 16; |
| 23 | if (interface >= BT_DEV_TYPE_USB) |
| 24 | return 1; |
| 25 | |
| 26 | if (interface == BT_DEV_TYPE_MMC && (boot & 1)) |
| 27 | return 1; |
| 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | static ulong spl_romapi_read_seekable(struct spl_load_info *load, |
| 33 | ulong sector, ulong count, |
| 34 | void *buf) |
| 35 | { |
| 36 | u32 pagesize = *(u32 *)load->priv; |
| 37 | volatile gd_t *pgd = gd; |
| 38 | ulong byte = count * pagesize; |
| 39 | int ret; |
| 40 | u32 offset; |
| 41 | |
| 42 | offset = sector * pagesize; |
| 43 | |
| 44 | debug("ROM API load from 0x%x, size 0x%x\n", offset, (u32)byte); |
| 45 | |
| 46 | ret = g_rom_api->download_image(buf, offset, byte, |
| 47 | ((uintptr_t)buf) ^ offset ^ byte); |
| 48 | gd = pgd; |
| 49 | |
| 50 | if (ret == ROM_API_OKAY) |
| 51 | return count; |
| 52 | |
| 53 | printf("ROM API Failure when load 0x%x\n", offset); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image, |
| 59 | struct spl_boot_device *bootdev, |
| 60 | u32 rom_bt_dev) |
| 61 | { |
| 62 | volatile gd_t *pgd = gd; |
| 63 | int ret; |
| 64 | u32 offset; |
| 65 | u32 pagesize, size; |
| 66 | struct image_header *header; |
| 67 | u32 image_offset; |
| 68 | |
| 69 | ret = g_rom_api->query_boot_infor(QUERY_IVT_OFF, &offset, |
| 70 | ((uintptr_t)&offset) ^ QUERY_IVT_OFF); |
| 71 | ret |= g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize, |
| 72 | ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ); |
| 73 | ret |= g_rom_api->query_boot_infor(QUERY_IMG_OFF, &image_offset, |
| 74 | ((uintptr_t)&image_offset) ^ QUERY_IMG_OFF); |
| 75 | |
| 76 | gd = pgd; |
| 77 | |
| 78 | if (ret != ROM_API_OKAY) { |
| 79 | puts("ROMAPI: Failure query boot infor pagesize/offset\n"); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | header = (struct image_header *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR); |
| 84 | |
| 85 | printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n", |
| 86 | image_offset, pagesize, offset); |
| 87 | |
| 88 | if (((rom_bt_dev >> 16) & 0xff) == BT_DEV_TYPE_FLEXSPINOR) |
| 89 | offset = CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512; |
| 90 | else |
| 91 | offset = image_offset + |
| 92 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512 - 0x8000; |
| 93 | |
| 94 | size = ALIGN(sizeof(struct image_header), pagesize); |
| 95 | ret = g_rom_api->download_image((u8 *)header, offset, size, |
| 96 | ((uintptr_t)header) ^ offset ^ size); |
| 97 | gd = pgd; |
| 98 | |
| 99 | if (ret != ROM_API_OKAY) { |
| 100 | printf("ROMAPI: download failure offset 0x%x size 0x%x\n", |
| 101 | offset, size); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 106 | image_get_magic(header) == FDT_MAGIC) { |
| 107 | struct spl_load_info load; |
| 108 | |
| 109 | memset(&load, 0, sizeof(load)); |
| 110 | load.bl_len = pagesize; |
| 111 | load.read = spl_romapi_read_seekable; |
| 112 | load.priv = &pagesize; |
| 113 | return spl_load_simple_fit(spl_image, &load, |
| 114 | offset / pagesize, header); |
| 115 | } else { |
| 116 | /* TODO */ |
| 117 | puts("Can't support legacy image\n"); |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector, |
| 125 | ulong count, void *buf) |
| 126 | { |
| 127 | memcpy(buf, (void *)(sector), count); |
| 128 | |
| 129 | if (load->priv) { |
| 130 | ulong *p = (ulong *)load->priv; |
| 131 | ulong total = sector + count; |
| 132 | |
| 133 | if (total > *p) |
| 134 | *p = total; |
| 135 | } |
| 136 | |
| 137 | return count; |
| 138 | } |
| 139 | |
| 140 | static ulong get_fit_image_size(void *fit) |
| 141 | { |
| 142 | struct spl_image_info spl_image; |
| 143 | struct spl_load_info spl_load_info; |
| 144 | ulong last = (ulong)fit; |
| 145 | |
| 146 | memset(&spl_load_info, 0, sizeof(spl_load_info)); |
| 147 | spl_load_info.bl_len = 1; |
| 148 | spl_load_info.read = spl_ram_load_read; |
| 149 | spl_load_info.priv = &last; |
| 150 | |
| 151 | spl_load_simple_fit(&spl_image, &spl_load_info, |
| 152 | (uintptr_t)fit, fit); |
| 153 | |
| 154 | return last - (ulong)fit; |
| 155 | } |
| 156 | |
| 157 | u8 *search_fit_header(u8 *p, int size) |
| 158 | { |
| 159 | int i; |
| 160 | |
| 161 | for (i = 0; i < size; i += 4) |
| 162 | if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT) |
| 163 | return p + i; |
| 164 | |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | static int spl_romapi_load_image_stream(struct spl_image_info *spl_image, |
| 169 | struct spl_boot_device *bootdev) |
| 170 | { |
| 171 | struct spl_load_info load; |
| 172 | volatile gd_t *pgd = gd; |
| 173 | u32 pagesize, pg; |
| 174 | int ret; |
| 175 | int i = 0; |
| 176 | u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR; |
| 177 | u8 *pfit = NULL; |
| 178 | int imagesize; |
| 179 | int total; |
| 180 | |
| 181 | ret = g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize, |
| 182 | ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ); |
| 183 | gd = pgd; |
| 184 | |
| 185 | if (ret != ROM_API_OKAY) |
| 186 | puts("failure at query_boot_info\n"); |
| 187 | |
| 188 | pg = pagesize; |
| 189 | if (pg < 1024) |
| 190 | pg = 1024; |
| 191 | |
| 192 | for (i = 0; i < 640; i++) { |
| 193 | ret = g_rom_api->download_image(p, 0, pg, |
| 194 | ((uintptr_t)p) ^ pg); |
| 195 | gd = pgd; |
| 196 | |
| 197 | if (ret != ROM_API_OKAY) { |
| 198 | puts("Steam(USB) download failure\n"); |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | pfit = search_fit_header(p, pg); |
| 203 | p += pg; |
| 204 | |
| 205 | if (pfit) |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | if (!pfit) { |
| 210 | puts("Can't found uboot FIT image in 640K range \n"); |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | if (p - pfit < sizeof(struct fdt_header)) { |
| 215 | ret = g_rom_api->download_image(p, 0, pg, ((uintptr_t)p) ^ pg); |
| 216 | gd = pgd; |
| 217 | |
| 218 | if (ret != ROM_API_OKAY) { |
| 219 | puts("Steam(USB) download failure\n"); |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | p += pg; |
| 224 | } |
| 225 | |
| 226 | imagesize = fit_get_size(pfit); |
| 227 | printf("Find FIT header 0x&%p, size %d\n", pfit, imagesize); |
| 228 | |
| 229 | if (p - pfit < imagesize) { |
| 230 | imagesize -= p - pfit; |
| 231 | /*need pagesize hear after ROM fix USB problme*/ |
| 232 | imagesize += pg - 1; |
| 233 | imagesize /= pg; |
| 234 | imagesize *= pg; |
| 235 | |
| 236 | printf("Need continue download %d\n", imagesize); |
| 237 | |
| 238 | ret = g_rom_api->download_image(p, 0, imagesize, |
| 239 | ((uintptr_t)p) ^ imagesize); |
| 240 | gd = pgd; |
| 241 | |
| 242 | p += imagesize; |
| 243 | |
| 244 | if (ret != ROM_API_OKAY) { |
| 245 | printf("Failure download %d\n", imagesize); |
| 246 | return -1; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | total = get_fit_image_size(pfit); |
| 251 | total += 3; |
| 252 | total &= ~0x3; |
| 253 | |
| 254 | imagesize = total - (p - pfit); |
| 255 | |
| 256 | imagesize += pagesize - 1; |
| 257 | imagesize /= pagesize; |
| 258 | imagesize *= pagesize; |
| 259 | |
| 260 | printf("Download %d, total fit %d\n", imagesize, total); |
| 261 | |
| 262 | ret = g_rom_api->download_image(p, 0, imagesize, |
| 263 | ((uintptr_t)p) ^ imagesize); |
| 264 | if (ret != ROM_API_OKAY) |
| 265 | printf("ROM download failure %d\n", imagesize); |
| 266 | |
| 267 | memset(&load, 0, sizeof(load)); |
| 268 | load.bl_len = 1; |
| 269 | load.read = spl_ram_load_read; |
| 270 | |
| 271 | return spl_load_simple_fit(spl_image, &load, (ulong)pfit, pfit); |
| 272 | } |
| 273 | |
| 274 | int board_return_to_bootrom(struct spl_image_info *spl_image, |
| 275 | struct spl_boot_device *bootdev) |
| 276 | { |
| 277 | volatile gd_t *pgd = gd; |
| 278 | int ret; |
| 279 | u32 boot; |
| 280 | |
| 281 | ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot, |
| 282 | ((uintptr_t)&boot) ^ QUERY_BT_DEV); |
| 283 | gd = pgd; |
| 284 | |
| 285 | if (ret != ROM_API_OKAY) { |
| 286 | puts("ROMAPI: failure at query_boot_info\n"); |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | if (is_boot_from_stream_device(boot)) |
| 291 | return spl_romapi_load_image_stream(spl_image, bootdev); |
| 292 | |
| 293 | return spl_romapi_load_image_seekable(spl_image, bootdev, boot); |
| 294 | } |