1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd 4 */ 5 6 #include <common.h> 7 #include <android_image.h> 8 #include <errno.h> 9 #include <malloc.h> 10 #include <misc.h> 11 #include <misc_decompress.h> 12 #include <spl.h> 13 #include <spl_rkfw.h> 14 #include <linux/kernel.h> 15 #include <asm/arch/spl_resource_img.h> 16 17 #ifdef CONFIG_SPL_ATF 18 static const __aligned(16) struct s_fip_name_id fip_name_id[] = { 19 { BL30_IMAGE_NAME, UUID_SCP_FIRMWARE_BL30 }, /* optional */ 20 { BL31_IMAGE_NAME, UUID_EL3_RUNTIME_FIRMWARE_BL31 }, /* mandatory */ 21 { BL32_IMAGE_NAME, UUID_SECURE_PAYLOAD_BL32 }, /* optional */ 22 }; 23 24 static int file2comp_id(const char *file_name, u32 *comp_id) 25 { 26 int i; 27 28 for (i = 0; i < ARRAY_SIZE(fip_name_id); i++) { 29 if (!strcmp(file_name, fip_name_id[i].name)) { 30 *comp_id = fip_name_id[i].id; 31 return 0; 32 } 33 } 34 35 return -ENOENT; 36 } 37 38 static int open_image(const char *image_name, tboot_entry *entry, 39 struct tag_tboot_header_2k *hdr) 40 { 41 u32 i, component_num, sign_offset; 42 component_data *pcompdata; 43 boot_component *pcomp; 44 int n_found = 0; 45 u32 comp_id; 46 int ret; 47 48 ret = file2comp_id(image_name, &comp_id); 49 if (ret) { 50 printf("Can't find unknown image: %s\n", image_name); 51 return ret; 52 } 53 54 component_num = (hdr->size >> 16) & 0xffff; 55 sign_offset = (hdr->size & 0xffff) << 2; 56 pcompdata = (component_data *)((char *)hdr + sizeof(tboot_header)); 57 pcomp = (boot_component *)((char *)hdr + sign_offset + SIGNATURE_SIZE); 58 59 for (i = 0; i < component_num; i++) { 60 if (comp_id == pcomp->component_id) { 61 if (n_found < MAX_BL_CODE_NUM) { 62 memcpy(&entry[n_found].component, pcomp, 63 sizeof(boot_component)); 64 memcpy(&entry[n_found].compdata, pcompdata, 65 sizeof(component_data)); 66 n_found++; 67 } else { 68 printf("Image num excess max: %d!\n", 69 MAX_BL_CODE_NUM); 70 return -EINVAL; 71 } 72 } else { 73 if (n_found > 0) 74 break; 75 } 76 77 pcomp++; 78 pcompdata++; 79 } 80 81 if (!n_found) { 82 printf("No find %s\n", image_name); 83 return -ENONET; 84 } 85 86 return n_found; 87 } 88 89 static int check_image(struct tag_tboot_header_2k *hdr) 90 { 91 u32 hash_format[] = { 0, 160, 256, 256 }; 92 93 /* HASH format identifier */ 94 return (hash_format[hdr->flags & 0x3] == 0) ? -EINVAL : 0; 95 } 96 97 static int load_image(struct spl_load_info *info, 98 struct tag_tboot_header_2k *hdr, 99 u32 image_sector, 100 const char *image_name, 101 uintptr_t *entry_point) 102 { 103 tboot_entry entry[MAX_BL_CODE_NUM]; 104 void *image_buf = NULL; 105 ulong load_addr; 106 u32 sect_off; 107 u32 sect_cnt; 108 int image_num; 109 int i, ret; 110 111 /* Parse components from image header */ 112 image_num = open_image(image_name, entry, hdr); 113 if (image_num < 0) 114 return image_num; 115 116 /* Get all component */ 117 for (i = 0; i < image_num; i++) { 118 load_addr = entry[i].compdata.load_addr; 119 sect_cnt = entry[i].component.image_size; 120 sect_off = entry[i].component.storage_addr; 121 122 printf("%s[%d]: addr=0x%lx, size=0x%lx\n", 123 image_name, i, load_addr, (ulong)sect_cnt * 512); 124 125 /* 126 * MMC/NAND controller DMA can't access sram region, so: 127 * data -> ddr buffer -> memcpy to sram region. 128 */ 129 if (load_addr < CONFIG_SYS_SDRAM_BASE || 130 load_addr >= CONFIG_SYS_SDRAM_BASE + SDRAM_MAX_SIZE) { 131 image_buf = memalign(ARCH_DMA_MINALIGN, sect_cnt * 512); 132 if (!image_buf) { 133 printf("%s: malloc failed\n", __func__); 134 return -ENOMEM; 135 } 136 } else { 137 image_buf = (void *)load_addr; 138 } 139 140 ret = info->read(info, image_sector + sect_off, 141 sect_cnt, image_buf); 142 if (ret != sect_cnt) { 143 printf("Read '%s' failed at sector: %ld, ret=%d\n", 144 image_name, (ulong)image_sector + sect_off, ret); 145 return -EIO; 146 } 147 148 /* Verify component */ 149 ret = check_image(hdr); 150 if (ret) { 151 printf("%s[%d]: verify image fail!\n", image_name, i); 152 return ret; 153 } 154 155 /* Handle sram region */ 156 if ((ulong)image_buf != load_addr) { 157 memcpy((void *)load_addr, image_buf, sect_cnt << 9); 158 free(image_buf); 159 } 160 161 /* Fill entry_point by first component */ 162 if (i == 0) 163 *entry_point = (uintptr_t)load_addr; 164 } 165 166 return ret; 167 } 168 169 static int rkfw_load_trust(struct spl_load_info *info, u32 image_sector, 170 struct spl_image_info *spl_image, 171 int *found_rkfw, u32 try_count) 172 { 173 struct tag_tboot_header_2k hdr; 174 u32 sect_addr = image_sector; 175 int blkcnt = 4; /* header sectors, 2KB */ 176 int i, ret = 0; 177 178 /* Find valid image header */ 179 for (i = 0; i < try_count; i++) { 180 sect_addr = image_sector + (i * RKFW_RETRY_SECTOR_SIZE); 181 if (blkcnt != info->read(info, sect_addr, blkcnt, &hdr)) 182 continue; 183 184 if (hdr.tag == TBOOT_HEAD_TAG) { 185 /* Mark it */ 186 *found_rkfw = 1; 187 188 /* bl31 is mandatory */ 189 ret = load_image(info, &hdr, sect_addr, 190 BL31_IMAGE_NAME, &spl_image->entry_point); 191 if (ret) 192 continue; 193 194 /* bl32 is optional */ 195 ret = load_image(info, &hdr, sect_addr, 196 BL32_IMAGE_NAME, &spl_image->entry_point_bl32); 197 if (ret) { 198 if (ret == -ENONET) { 199 spl_image->entry_point_bl32 = -1; /* Not exist */ 200 ret = 0; 201 } else { 202 continue; 203 } 204 } 205 break; 206 } 207 } 208 209 return ret; 210 } 211 #else 212 static int rkfw_load_trust(struct spl_load_info *info, u32 image_sector, 213 struct spl_image_info *spl_image, 214 int *found_rkfw, u32 try_count) 215 { 216 struct tag_second_loader_hdr hdr; 217 int i, ret, blkcnt = 4; /* header sectors, 2KB */ 218 char *load_addr; 219 u32 sect_addr; 220 221 /* Detect valid image header */ 222 for (i = 0; i < try_count; i++) { 223 sect_addr = image_sector + (i * RKFW_RETRY_SECTOR_SIZE); 224 ret = info->read(info, sect_addr, blkcnt, &hdr); 225 if (ret != blkcnt) 226 continue; 227 228 if (!memcmp(hdr.magic, TBOOT_HEAD_TAG, 6)) { 229 *found_rkfw = 1; 230 spl_image->entry_point = (uintptr_t)hdr.loader_load_addr; 231 /* Load full binary image(right behind header) */ 232 sect_addr += blkcnt; 233 load_addr = (char *)((size_t)hdr.loader_load_addr); 234 blkcnt = DIV_ROUND_UP(hdr.loader_load_size, 512); 235 236 printf("tee.bin: addr=0x%lx, size=0x%lx\n", 237 (ulong)load_addr, (ulong)blkcnt * 512); 238 ret = info->read(info, sect_addr, blkcnt, load_addr); 239 if (ret != blkcnt) 240 continue; 241 242 break; 243 } 244 } 245 246 if (i == try_count) { 247 printf("Can not find usable uboot\n"); 248 return -ENONET; 249 } 250 251 return 0; 252 } 253 #endif 254 255 static int rkfw_load_uboot(struct spl_load_info *info, u32 image_sector, 256 struct spl_image_info *spl_image, u32 try_count) 257 { 258 struct tag_second_loader_hdr hdr; 259 int i, ret, blkcnt = 4; /* header sectors, 2KB */ 260 char *load_addr; 261 u32 sect_addr; 262 263 /* Detect valid image header */ 264 for (i = 0; i < try_count; i++) { 265 sect_addr = image_sector + (i * RKFW_RETRY_SECTOR_SIZE); 266 ret = info->read(info, sect_addr, blkcnt, &hdr); 267 if (ret != blkcnt) 268 continue; 269 270 if (!memcmp(hdr.magic, LOADER_HARD_STR, 6)) { 271 /* Load full binary image(right behind header) */ 272 sect_addr += blkcnt; 273 load_addr = (char *)((size_t)hdr.loader_load_addr); 274 blkcnt = DIV_ROUND_UP(hdr.loader_load_size, 512); 275 276 printf("u-boot.bin: addr=0x%lx, size=0x%lx\n", 277 (ulong)load_addr, (ulong)blkcnt * 512); 278 ret = info->read(info, sect_addr, blkcnt, load_addr); 279 if (ret != blkcnt) 280 continue; 281 282 break; 283 } 284 } 285 286 if (i == try_count) { 287 printf("Can not find usable uboot\n"); 288 return -ENONET; 289 } 290 291 /* Fill entry point */ 292 #ifdef CONFIG_SPL_ATF 293 spl_image->entry_point_bl33 = (uintptr_t)hdr.loader_load_addr; 294 #endif 295 #ifdef CONFIG_SPL_OPTEE 296 spl_image->entry_point_os = (uintptr_t)hdr.loader_load_addr; 297 #endif 298 return 0; 299 } 300 301 static int rkfw_load_kernel(struct spl_load_info *info, u32 image_sector, 302 struct spl_image_info *spl_image, u32 try_count) 303 { 304 struct andr_img_hdr *hdr; 305 int ret, cnt; 306 int dtb_sector, ramdisk_sector, resource_sector; 307 308 cnt = ALIGN(sizeof(struct andr_img_hdr), 512) >> 9; 309 hdr = malloc(cnt * 512); 310 if (!hdr) 311 return -ENOMEM; 312 313 ret = info->read(info, image_sector, cnt, (void *)hdr); 314 if (ret != cnt) { 315 ret = -EIO; 316 goto out; 317 } 318 319 if (memcmp(hdr->magic, ANDR_BOOT_MAGIC, strlen(ANDR_BOOT_MAGIC)) != 0) { 320 printf("SPL: boot image head magic error\n"); 321 ret = -EINVAL; 322 goto out; 323 } 324 325 ramdisk_sector = ALIGN(hdr->kernel_size, hdr->page_size); 326 resource_sector = ALIGN(hdr->kernel_size, hdr->page_size) 327 + ALIGN(hdr->ramdisk_size, hdr->page_size); 328 dtb_sector = ALIGN(hdr->kernel_size, hdr->page_size) 329 + ALIGN(hdr->ramdisk_size, hdr->page_size) 330 + ALIGN(hdr->second_size, hdr->page_size); 331 image_sector = image_sector + cnt; 332 cnt = ALIGN(hdr->kernel_size, hdr->page_size) >> 9; 333 334 /* Load kernel image */ 335 #ifdef CONFIG_SPL_ROCKCHIP_HW_DECOMPRESS 336 ret = info->read(info, image_sector, cnt, 337 (void *)CONFIG_SPL_KERNEL_COMPRESS_ADDR); 338 #else 339 ret = info->read(info, image_sector, cnt, (void *)CONFIG_SPL_KERNEL_ADDR); 340 #endif 341 if (ret != cnt) { 342 ret = -EIO; 343 goto out; 344 } 345 #ifdef CONFIG_SPL_ROCKCHIP_HW_DECOMPRESS 346 struct udevice *dev; 347 u32 cap = DECOM_GZIP; 348 349 dev = misc_decompress_get_device(cap); 350 351 if (!dev) 352 goto out; 353 354 ret = misc_decompress_start(dev, CONFIG_SPL_KERNEL_COMPRESS_ADDR, 355 CONFIG_SPL_KERNEL_ADDR, hdr->kernel_size); 356 if (ret) 357 goto out; 358 359 #endif 360 361 /* Load ramdisk image */ 362 if (hdr->ramdisk_size) { 363 #ifdef CONFIG_SPL_ROCKCHIP_HW_DECOMPRESS 364 ret = info->read(info, (ramdisk_sector >> 9) + image_sector, 365 ALIGN(hdr->ramdisk_size, hdr->page_size) >> 9, 366 (void *)CONFIG_SPL_RAMDISK_COMPRESS_ADDR); 367 #else 368 ret = info->read(info, (ramdisk_sector >> 9) + image_sector, 369 ALIGN(hdr->ramdisk_size, hdr->page_size) >> 9, 370 (void *)CONFIG_SPL_RAMDISK_ADDR); 371 #endif 372 if (ret != (ALIGN(hdr->ramdisk_size, hdr->page_size) >> 9)) { 373 ret = -EIO; 374 goto out; 375 } 376 #ifdef CONFIG_SPL_ROCKCHIP_HW_DECOMPRESS 377 int timeout = 10000; 378 379 while (misc_decompress_is_complete(dev)) { 380 if (timeout < 0) { 381 ret = -EIO; 382 goto out; 383 } 384 385 timeout--; 386 udelay(10); 387 } 388 389 ret = misc_decompress_start(dev, 390 CONFIG_SPL_RAMDISK_COMPRESS_ADDR, 391 CONFIG_SPL_RAMDISK_ADDR, 392 hdr->kernel_size); 393 if (ret) 394 goto out; 395 #endif 396 } 397 398 /* Load resource, and checkout the dtb */ 399 if (hdr->second_size) { 400 struct resource_img_hdr *head = 401 (struct resource_img_hdr *)(CONFIG_SPL_FDT_ADDR + 0x100000); 402 403 ret = info->read(info, (resource_sector >> 9) + image_sector, 404 ALIGN(hdr->second_size, hdr->page_size) >> 9, 405 (void *)head); 406 if (ret != (ALIGN(hdr->second_size, hdr->page_size) >> 9)) { 407 ret = -EIO; 408 goto out; 409 } 410 #ifdef CONFIG_SPL_KERNEL_BOOT 411 if (spl_resource_image_check_header(head)) { 412 printf("Can't find kernel dtb in spl."); 413 } else { 414 struct resource_entry *entry; 415 char *dtb_temp; 416 417 entry = spl_resource_image_get_dtb_entry(head); 418 if (!entry) { 419 ret = -EIO; 420 goto out; 421 } 422 423 dtb_temp = (char *)((char *)head + entry->f_offset * 512); 424 memcpy((char *)CONFIG_SPL_FDT_ADDR, dtb_temp, 425 entry->f_size); 426 } 427 #endif 428 } else { 429 /* Load dtb image */ 430 ret = info->read(info, (dtb_sector >> 9) + image_sector, 431 ALIGN(hdr->dtb_size, hdr->page_size) >> 9, 432 (void *)CONFIG_SPL_FDT_ADDR); 433 if (ret != (ALIGN(hdr->dtb_size, hdr->page_size) >> 9)) { 434 ret = -EIO; 435 goto out; 436 } 437 } 438 439 spl_image->fdt_addr = (void *)CONFIG_SPL_FDT_ADDR; 440 #ifdef CONFIG_SPL_OPTEE 441 spl_image->entry_point_os = (uintptr_t)CONFIG_SPL_KERNEL_ADDR; 442 #endif 443 #ifdef CONFIG_SPL_ATF 444 spl_image->entry_point_bl33 = CONFIG_SPL_KERNEL_ADDR; 445 #endif 446 ret = 0; 447 out: 448 free(hdr); 449 450 return ret; 451 } 452 453 int spl_load_rkfw_image(struct spl_image_info *spl_image, 454 struct spl_load_info *info, 455 u32 trust_sector, u32 uboot_sector, 456 u32 boot_sector) 457 { 458 int ret, try_count = RKFW_RETRY_SECTOR_TIMES; 459 int found_rkfw = 0; 460 461 ret = rkfw_load_trust(info, trust_sector, spl_image, 462 &found_rkfw, try_count); 463 if (ret) { 464 printf("Load trust image failed! ret=%d\n", ret); 465 goto out; 466 } 467 #ifdef CONFIG_SPL_KERNEL_BOOT 468 if (spl_image->next_stage == SPL_NEXT_STAGE_UBOOT) { 469 #endif 470 ret = rkfw_load_uboot(info, uboot_sector, spl_image, try_count); 471 if (ret) 472 printf("Load uboot image failed! ret=%d\n", ret); 473 else 474 goto boot; 475 #ifdef CONFIG_SPL_KERNEL_BOOT 476 } else if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL) { 477 #endif 478 ret = rkfw_load_kernel(info, boot_sector, spl_image, try_count); 479 if (ret) { 480 printf("Load kernel image failed! ret=%d\n", ret); 481 goto out; 482 } 483 #ifdef CONFIG_SPL_KERNEL_BOOT 484 } 485 #endif 486 487 boot: 488 #if CONFIG_IS_ENABLED(LOAD_FIT) 489 spl_image->fdt_addr = 0; 490 #endif 491 #ifdef CONFIG_SPL_ATF 492 spl_image->os = IH_OS_ARM_TRUSTED_FIRMWARE; 493 #else 494 spl_image->os = IH_OS_OP_TEE; 495 #endif 496 497 out: 498 /* If not found rockchip firmware, try others outside */ 499 return found_rkfw ? ret : -EAGAIN; 500 } 501