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