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 <spl.h>
12 #include <spl_rkfw.h>
13 #include <linux/kernel.h>
14 #include <asm/arch/spl_resource_img.h>
15 #include <boot_rkimg.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
file2comp_id(const char * file_name,u32 * comp_id)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
open_image(const char * image_name,tboot_entry * entry,struct tag_tboot_header_2k * hdr)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
check_image(struct tag_tboot_header_2k * hdr)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
load_image(struct spl_load_info * info,struct tag_tboot_header_2k * hdr,u32 image_sector,const char * image_name,uintptr_t * entry_point)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
rkfw_load_trust(struct spl_load_info * info,u32 image_sector,struct spl_image_info * spl_image,int * found_rkfw,u32 try_count)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 /* op-tee */
rkfw_load_trust(struct spl_load_info * info,u32 image_sector,struct spl_image_info * spl_image,int * found_rkfw,u32 try_count)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 trust\n");
248 return -ENONET;
249 }
250
251 return 0;
252 }
253 #endif
254
rkfw_load_uboot(struct spl_load_info * info,u32 image_sector,struct spl_image_info * spl_image,u32 try_count)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
rkfw_load_kernel(struct spl_load_info * info,u32 image_sector,struct spl_image_info * spl_image,u32 try_count)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,
356 CONFIG_SPL_KERNEL_DECOM_LIMIT_SIZE);
357 if (ret)
358 goto out;
359
360 #endif
361
362 /* Load ramdisk image */
363 if (hdr->ramdisk_size) {
364 #ifdef CONFIG_SPL_ROCKCHIP_HW_DECOMPRESS
365 ret = info->read(info, (ramdisk_sector >> 9) + image_sector,
366 ALIGN(hdr->ramdisk_size, hdr->page_size) >> 9,
367 (void *)CONFIG_SPL_RAMDISK_COMPRESS_ADDR);
368 #else
369 ret = info->read(info, (ramdisk_sector >> 9) + image_sector,
370 ALIGN(hdr->ramdisk_size, hdr->page_size) >> 9,
371 (void *)CONFIG_SPL_RAMDISK_ADDR);
372 #endif
373 if (ret != (ALIGN(hdr->ramdisk_size, hdr->page_size) >> 9)) {
374 ret = -EIO;
375 goto out;
376 }
377 #ifdef CONFIG_SPL_ROCKCHIP_HW_DECOMPRESS
378 int timeout = 10000;
379
380 while (!misc_decompress_is_complete(dev)) {
381 if (timeout < 0) {
382 ret = -EIO;
383 goto out;
384 }
385
386 timeout--;
387 udelay(10);
388 }
389
390 ret = misc_decompress_stop(dev);
391 if (ret)
392 goto out;
393
394 ret = misc_decompress_start(dev,
395 CONFIG_SPL_RAMDISK_COMPRESS_ADDR,
396 CONFIG_SPL_RAMDISK_ADDR,
397 CONFIG_SPL_RAMDISK_DECOM_LIMIT_SIZE);
398 if (ret)
399 goto out;
400 #endif
401 }
402 #ifdef CONFIG_SPL_ROCKCHIP_HW_DECOMPRESS
403 else {
404 int timeout = 10000;
405
406 while (!misc_decompress_is_complete(dev)) {
407 if (timeout < 0) {
408 ret = -EIO;
409 goto out;
410 }
411
412 timeout--;
413 udelay(10);
414 }
415 }
416 #endif
417 /* Load resource, and checkout the dtb */
418 if (hdr->second_size) {
419 struct resource_img_hdr *head =
420 (struct resource_img_hdr *)(CONFIG_SPL_FDT_ADDR + 0x100000);
421
422 ret = info->read(info, (resource_sector >> 9) + image_sector,
423 ALIGN(hdr->second_size, hdr->page_size) >> 9,
424 (void *)head);
425 if (ret != (ALIGN(hdr->second_size, hdr->page_size) >> 9)) {
426 ret = -EIO;
427 goto out;
428 }
429 #ifdef CONFIG_SPL_KERNEL_BOOT
430 if (spl_resource_image_check_header(head)) {
431 printf("Can't find kernel dtb in spl.");
432 } else {
433 struct resource_entry *entry;
434 char *dtb_temp;
435
436 entry = spl_resource_image_get_dtb_entry(head);
437 if (!entry) {
438 ret = -EIO;
439 goto out;
440 }
441
442 dtb_temp = (char *)((char *)head + entry->f_offset * 512);
443 memcpy((char *)CONFIG_SPL_FDT_ADDR, dtb_temp,
444 entry->f_size);
445 }
446 #endif
447 } else {
448 /* Load dtb image */
449 ret = info->read(info, (dtb_sector >> 9) + image_sector,
450 ALIGN(hdr->dtb_size, hdr->page_size) >> 9,
451 (void *)CONFIG_SPL_FDT_ADDR);
452 if (ret != (ALIGN(hdr->dtb_size, hdr->page_size) >> 9)) {
453 ret = -EIO;
454 goto out;
455 }
456 }
457
458 spl_image->fdt_addr = (void *)CONFIG_SPL_FDT_ADDR;
459 #ifdef CONFIG_SPL_OPTEE
460 spl_image->entry_point_os = (uintptr_t)CONFIG_SPL_KERNEL_ADDR;
461 #endif
462 #ifdef CONFIG_SPL_ATF
463 spl_image->entry_point_bl33 = CONFIG_SPL_KERNEL_ADDR;
464 #endif
465 ret = 0;
466 out:
467 free(hdr);
468
469 return ret;
470 }
471
spl_load_rkfw_image(struct spl_image_info * spl_image,struct spl_load_info * info)472 int spl_load_rkfw_image(struct spl_image_info *spl_image,
473 struct spl_load_info *info)
474 {
475 u32 uboot_sector = CONFIG_RKFW_U_BOOT_SECTOR;
476 u32 trust_sector = CONFIG_RKFW_TRUST_SECTOR;
477 u32 boot_sector = CONFIG_RKFW_BOOT_SECTOR;
478 int ret, try_count = RKFW_RETRY_SECTOR_TIMES;
479 int found_rkfw = 0;
480 char *part_name;
481 #ifdef CONFIG_SPL_LIBDISK_SUPPORT
482 struct blk_desc *dev_desc = info->dev;
483 disk_partition_t part_info;
484
485 if (dev_desc) {
486 if (part_get_info_by_name(dev_desc, PART_UBOOT, &part_info) > 0)
487 uboot_sector = part_info.start;
488 if (part_get_info_by_name(dev_desc, PART_TRUST, &part_info) > 0)
489 trust_sector = part_info.start;
490 if (part_get_info_by_name(dev_desc, PART_BOOT, &part_info) > 0)
491 boot_sector = part_info.start;
492 }
493 #endif
494 /* u-boot or boot */
495 if (spl_image->next_stage != SPL_NEXT_STAGE_UBOOT)
496 uboot_sector = 0;
497
498 ret = rkfw_load_trust(info, trust_sector, spl_image,
499 &found_rkfw, try_count);
500 if (ret) {
501 part_name = PART_TRUST;
502 goto out;
503 }
504
505 if (uboot_sector) {
506 ret = rkfw_load_uboot(info, uboot_sector, spl_image, try_count);
507 if (ret) {
508 part_name = PART_UBOOT;
509 goto out;
510 }
511 } else {
512 ret = rkfw_load_kernel(info, boot_sector, spl_image, try_count);
513 if (ret) {
514 part_name = PART_BOOT;
515 goto out;
516 }
517 }
518
519 #if CONFIG_IS_ENABLED(LOAD_FIT)
520 spl_image->fdt_addr = 0;
521 #endif
522 #ifdef CONFIG_SPL_ATF
523 spl_image->os = IH_OS_ARM_TRUSTED_FIRMWARE;
524 #else
525 spl_image->os = IH_OS_OP_TEE;
526 #endif
527
528 out:
529 if (ret)
530 printf("Load %s part failed! ret=%d\n", part_name, ret);
531
532 /* If not found rockchip firmware, try others outside */
533 return found_rkfw ? ret : -EAGAIN;
534 }
535