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