hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
 * (C) Copyright 2017 Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:     GPL-2.0+
 */
 
#include <common.h>
#include <adc.h>
#include <android_bootloader.h>
#include <android_image.h>
#include <bidram.h>
#include <bootm.h>
#include <boot_rkimg.h>
#include <cli.h>
#include <crypto.h>
#include <dm.h>
#include <fs.h>
#include <image.h>
#include <key.h>
#include <mmc.h>
#include <malloc.h>
#include <mp_boot.h>
#include <mtd_blk.h>
#include <nvme.h>
#include <scsi.h>
#include <stdlib.h>
#include <sysmem.h>
#include <asm/io.h>
#include <asm/arch/boot_mode.h>
#include <asm/arch/fit.h>
#include <asm/arch/hotkey.h>
#include <asm/arch/param.h>
#include <asm/arch/resource_img.h>
#include <asm/arch/uimage.h>
#include <dm/ofnode.h>
#include <linux/list.h>
#include <u-boot/sha1.h>
#include <u-boot/sha256.h>
#include <linux/usb/phy-rockchip-usb2.h>
 
DECLARE_GLOBAL_DATA_PTR;
 
__weak int rk_board_early_fdt_fixup(void *blob)
{
   return 0;
}
 
__weak int rk_board_scan_bootdev(void)
{
   const char *devtype_num_set = "run rkimg_bootdev";
 
   return run_command_list(devtype_num_set, -1, 0);
}
 
static int bootdev_init(const char *devtype, const char *devnum)
{
#ifdef CONFIG_MMC
   if (!strcmp("mmc", devtype))
       mmc_initialize(gd->bd);
#endif
#ifdef CONFIG_NVME
   if (!strcmp("nvme", devtype)) {
       pci_init();
       if (nvme_scan_namespace())
           return -ENODEV;
   }
#endif
#if defined(CONFIG_SCSI) && defined(CONFIG_CMD_SCSI) && defined(CONFIG_AHCI)
   if (!strcmp("scsi", devtype)) {
       if (scsi_scan(true))
           return -ENODEV;
   }
#endif
   /* Ok, let's test whether we can get the expected boot device or not */
   if (!blk_get_devnum_by_typename(devtype, atoi(devnum)))
       return -ENODEV;
 
   env_set("devtype", devtype);
   env_set("devnum", devnum);
 
   return 0;
}
 
/*
 * Priority: configuration > atags > scan list.
 */
static void boot_devtype_init(void)
{
   char *devtype = NULL, *devnum = NULL;
   char *src = "scan";
   static int done;    /* static */
   int ret;
 
   if (done)
       return;
 
#ifdef CONFIG_MP_BOOT
   mpb_post(0);
#endif
 
   /* configuration */
   if (!param_parse_assign_bootdev(&devtype, &devnum)) {
       if (!bootdev_init(devtype, devnum)) {
           src = "assign";
           goto finish;
       }
   }
 
   /* atags */
#ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS
   if (!param_parse_atags_bootdev(&devtype, &devnum)) {
       if (!bootdev_init(devtype, devnum)) {
           src = "atags";
           goto finish;
       }
   }
#endif
 
   /* scan list */
#ifdef CONFIG_MMC
   mmc_initialize(gd->bd);
#endif
   ret = rk_board_scan_bootdev();
   if (ret) {
       /* Set default if all failed */
       devtype = "mmc";
       devnum = "0";
       env_set("devtype", devtype);
       env_set("devnum", devnum);
   }
finish:
   done = 1;
   printf("Bootdev(%s): %s %s\n", src,
          env_get("devtype"), env_get("devnum"));
}
 
static int get_bootdev_type(void)
{
   char *boot_media = NULL, *devtype = NULL;
   char boot_options[128] = {0};
   static int appended;
   ulong devnum = 0;
   int type = 0;
 
   devtype = env_get("devtype");
   devnum = env_get_ulong("devnum", 10, 0);
 
   /* For current use(Only EMMC support!) */
   if (!devtype) {
       devtype = "mmc";
       printf("Use emmc as default boot media\n");
   }
 
   if (!strcmp(devtype, "mmc")) {
       type = IF_TYPE_MMC;
       if (devnum == 1)
           boot_media = "sd";
       else
           boot_media = "emmc";
   } else if (!strcmp(devtype, "rknand")) {
       type = IF_TYPE_RKNAND;
       boot_media = "nand";
   } else if (!strcmp(devtype, "spinand")) {
       type = IF_TYPE_SPINAND;
       boot_media = "nand"; /* kernel treat sfc nand as nand device */
   } else if (!strcmp(devtype, "spinor")) {
       type = IF_TYPE_SPINOR;
       boot_media = "nor";
   } else if (!strcmp(devtype, "ramdisk")) {
       type = IF_TYPE_RAMDISK;
       boot_media = "ramdisk";
   } else if (!strcmp(devtype, "mtd")) {
       type = IF_TYPE_MTD;
       boot_media = "mtd";
   } else if (!strcmp(devtype, "scsi")) {
       type = IF_TYPE_SCSI;
       boot_media = "scsi";
   } else if (!strcmp(devtype, "nvme")) {
       type = IF_TYPE_NVME;
       boot_media = "nvme";
   } else {
       /* Add new to support */
   }
 
   if (!appended && boot_media) {
       appended = 1;
 
   /*
    * The legacy rockchip Android (SDK < 8.1) requires "androidboot.mode="
    * to be "charger" or boot media which is a rockchip private solution.
    *
    * The official Android rule (SDK >= 8.1) is:
    * "androidboot.mode=normal" or "androidboot.mode=charger".
    *
    * Now that this U-Boot is usually working with higher version
    * Android (SDK >= 8.1), we follow the official rules.
    *
    * Common: androidboot.mode=charger has higher priority, don't override;
    */
#ifdef CONFIG_RKIMG_ANDROID_BOOTMODE_LEGACY
       /* rknand doesn't need "androidboot.mode="; */
       if (env_exist("bootargs", "androidboot.mode=charger") ||
           (type == IF_TYPE_RKNAND) ||
           (type == IF_TYPE_SPINAND) ||
           (type == IF_TYPE_SPINOR))
           snprintf(boot_options, sizeof(boot_options),
                "storagemedia=%s", boot_media);
       else
           snprintf(boot_options, sizeof(boot_options),
                "storagemedia=%s androidboot.mode=%s",
                boot_media, boot_media);
#else
       /*
        * 1. "storagemedia": This is a legacy variable to indicate board
        *    storage media for kernel and android.
        *
        * 2. "androidboot.storagemedia": The same purpose as "storagemedia",
        *    but the android framework will auto create property by
        *    variable with format "androidboot.xxx", eg:
        *
        *    "androidboot.storagemedia" => "ro.boot.storagemedia".
        *
        *    So, U-Boot pass this new variable is only for the convenience
        *    to Android.
        */
       if (env_exist("bootargs", "androidboot.mode=charger"))
           snprintf(boot_options, sizeof(boot_options),
                "storagemedia=%s androidboot.storagemedia=%s",
                boot_media, boot_media);
       else
           snprintf(boot_options, sizeof(boot_options),
                "storagemedia=%s androidboot.storagemedia=%s "
                "androidboot.mode=normal ",
                boot_media, boot_media);
#endif
       env_update("bootargs", boot_options);
   }
 
   return type;
}
 
static struct blk_desc *dev_desc;
 
struct blk_desc *rockchip_get_bootdev(void)
{
   int dev_type;
   int devnum;
 
   if (dev_desc)
       return dev_desc;
 
   boot_devtype_init();
   dev_type = get_bootdev_type();
   devnum = env_get_ulong("devnum", 10, 0);
 
   dev_desc = blk_get_devnum_by_type(dev_type, devnum);
   if (!dev_desc) {
       printf("%s: Can't find dev_desc!\n", __func__);
       return NULL;
   }
 
#ifdef CONFIG_MMC
   if (dev_type == IF_TYPE_MMC) {
       struct mmc *mmc;
       const char *timing[] = {
           "Legacy", "High Speed", "High Speed", "SDR12",
           "SDR25", "SDR50", "SDR104", "DDR50",
           "DDR52", "HS200", "HS400", "HS400 Enhanced Strobe"};
 
       mmc = find_mmc_device(devnum);
       printf("MMC%d: %s, %dMhz\n", devnum,
              timing[mmc->timing], mmc->clock / 1000000);
   }
#endif
 
   printf("PartType: %s\n", part_get_type(dev_desc));
 
#ifdef CONFIG_MTD_BLK
   mtd_blk_map_partitions(dev_desc);
#endif
   return dev_desc;
}
 
void rockchip_set_bootdev(struct blk_desc *desc)
{
   dev_desc = desc;
}
 
/*
 * detect download key status by adc, most rockchip
 * based boards use adc sample the download key status,
 * but there are also some use gpio. So it's better to
 * make this a weak function that can be override by
 * some special boards.
 */
#define KEY_DOWN_MIN_VAL    0
#define KEY_DOWN_MAX_VAL    30
 
__weak int rockchip_dnl_key_pressed(void)
{
#if defined(CONFIG_DM_KEY)
   return key_is_pressed(key_read(KEY_VOLUMEUP));
 
#elif defined(CONFIG_ADC)
   const void *blob = gd->fdt_blob;
   int node, ret, channel = 1;
   u32 val, chns[2];
 
   node = fdt_node_offset_by_compatible(blob, 0, "adc-keys");
   if (node >= 0) {
       if (!fdtdec_get_int_array(blob, node, "io-channels", chns, 2))
           channel = chns[1];
   }
 
   ret = adc_channel_single_shot("saradc", channel, &val);
   if (ret) {
       printf("%s: Failed to read saradc, ret=%d\n", __func__, ret);
       return 0;
   }
 
   return ((val >= KEY_DOWN_MIN_VAL) && (val <= KEY_DOWN_MAX_VAL));
#endif
 
   return 0;
}
 
void setup_download_mode(void)
{
   int vbus = 1; /* Assumed 1 in case of no rockusb */
 
   boot_devtype_init();
 
   if (rockchip_dnl_key_pressed() || is_hotkey(HK_ROCKUSB_DNL)) {
       printf("download %skey pressed... ",
              is_hotkey(HK_ROCKUSB_DNL) ? "hot" : "");
#ifdef CONFIG_CMD_ROCKUSB
       vbus = rockchip_u2phy_vbus_detect();
#endif
       if (vbus > 0) {
           printf("%sentering download mode...\n",
                  IS_ENABLED(CONFIG_CMD_ROCKUSB) ?
                  "" : "no rockusb, ");
 
           /* try rockusb download and brom download */
           run_command("download", 0);
       } else {
           printf("entering recovery mode!\n");
           env_set("reboot_mode", "recovery-key");
       }
   } else if (is_hotkey(HK_FASTBOOT)) {
       env_set("reboot_mode", "fastboot");
   }
}
 
void board_run_recovery_wipe_data(void)
{
   struct bootloader_message bmsg;
   struct blk_desc *dev_desc;
   disk_partition_t part_info;
#ifdef CONFIG_ANDROID_BOOT_IMAGE
   u32 bcb_offset = android_bcb_msg_sector_offset();
#else
   u32 bcb_offset = BCB_MESSAGE_BLK_OFFSET;
#endif
   int cnt, ret;
 
   printf("Rebooting into recovery to do wipe_data\n");
   dev_desc = rockchip_get_bootdev();
   if (!dev_desc) {
       printf("%s: dev_desc is NULL!\n", __func__);
       return;
   }
 
   ret = part_get_info_by_name(dev_desc, PART_MISC, &part_info);
   if (ret < 0) {
       printf("%s: Could not found misc partition, just run recovery\n",
              __func__);
       goto out;
   }
 
   memset((char *)&bmsg, 0, sizeof(struct bootloader_message));
   strcpy(bmsg.command, "boot-recovery");
   strcpy(bmsg.recovery, "recovery\n--wipe_data");
   bmsg.status[0] = 0;
   cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), dev_desc->blksz);
   ret = blk_dwrite(dev_desc, part_info.start + bcb_offset, cnt, &bmsg);
   if (ret != cnt)
       printf("Wipe data failed, ret=%d\n", ret);
out:
   /* now reboot to recovery */
   env_set("reboot_mode", "recovery");
   run_command("run bootcmd", 0);
}
 
#if defined(CONFIG_USING_KERNEL_DTB) || defined(CONFIG_CMD_BOOTM) || \
    defined(CONFIG_CMD_BOOTZ) || defined(CONFIG_CMD_BOOTI)
#ifdef CONFIG_ROCKCHIP_DTB_VERIFY
#ifdef CONFIG_DM_CRYPTO
static int crypto_csum(u32 cap, char *input, u32 input_len, u8 *output)
{
   sha_context csha_ctx;
   struct udevice *dev;
 
   dev = crypto_get_device(cap);
   if (!dev) {
       printf("Can't find expected crypto device\n");
       return -ENODEV;
   }
 
   csha_ctx.algo = cap;
   csha_ctx.length = input_len;
   crypto_sha_csum(dev, &csha_ctx, (char *)input,
           input_len, output);
 
   return 0;
}
 
static int fdt_check_hash(void *fdt_addr, u32 fdt_size,
             char *hash_cmp, u32 hash_size)
{
   uchar hash[32];
 
   if (!hash_size)
       return 0;
 
   if (hash_size == 20)
       crypto_csum(CRYPTO_SHA1, fdt_addr, fdt_size, hash);
   else if (hash_size == 32)
       crypto_csum(CRYPTO_SHA256, fdt_addr, fdt_size, hash);
   else
       return -EINVAL;
 
   printf("HASH(c): ");
   if (memcmp(hash, hash_cmp, hash_size)) {
       printf("error\n");
       return -EBADF;
   }
 
   printf("OK\n");
 
   return 0;
}
 
#else
static int fdt_check_hash(void *fdt_addr, u32 fdt_size,
             char *hash_cmp, u32 hash_size)
{
   uchar hash[32];
 
   if (!hash_size)
       return 0;
 
   if (hash_size == 20)
       sha1_csum((const uchar *)fdt_addr, fdt_size, hash);
   else if (hash_size == 32)
       sha256_csum((const uchar *)fdt_addr, fdt_size, hash);
   else
       return -EINVAL;
 
   printf("HASH(s): ");
   if (memcmp(hash, hash_cmp, hash_size)) {
       printf("error\n");
       return -EBADF;
   }
 
   printf("OK\n");
 
   return 0;
}
#endif
#endif    /* CONFIG_ROCKCHIP_DTB_VERIFY */
 
#if defined(CONFIG_ROCKCHIP_EARLY_DISTRO_DTB)
static int rockchip_read_distro_dtb(void *fdt_addr)
{
   const char *cmd = "part list ${devtype} ${devnum} -bootable devplist";
   char *devnum, *devtype, *devplist;
   char devnum_part[12];
   char fdt_hex_str[19];
   char *fs_argv[5];
 
   if (!rockchip_get_bootdev() || !fdt_addr)
       return -ENODEV;
 
   if (run_command_list(cmd, -1, 0)) {
       printf("Failed to find -bootable\n");
       return -EINVAL;
   }
 
   devplist = env_get("devplist");
   if (!devplist)
       devplist = "1";
 
   devtype = env_get("devtype");
   devnum = env_get("devnum");
   sprintf(devnum_part, "%s:%s", devnum, devplist);
   sprintf(fdt_hex_str, "0x%lx", (ulong)fdt_addr);
 
   fs_argv[0] = "load";
   fs_argv[1] = devtype,
   fs_argv[2] = devnum_part;
   fs_argv[3] = fdt_hex_str;
   fs_argv[4] = CONFIG_ROCKCHIP_EARLY_DISTRO_DTB_PATH;
 
   if (do_load(NULL, 0, 5, fs_argv, FS_TYPE_ANY))
       return -EIO;
 
   if (fdt_check_header(fdt_addr))
       return -EBADF;
 
   printf("DTB(Distro): %s\n", CONFIG_ROCKCHIP_EARLY_DISTRO_DTB_PATH);
 
   return 0;
}
#endif
 
enum {
   LOCATE_DISTRO,
   LOCATE_RESOURCE,
   LOCATE_FIT,
   LOCATE_END,
};
 
static int dtb_scan(void *fdt, int where)
{
   if (where == LOCATE_DISTRO) {
#ifdef CONFIG_ROCKCHIP_EARLY_DISTRO_DTB
       return rockchip_read_distro_dtb(fdt);
#endif
   } else if (where == LOCATE_RESOURCE) {
#ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE
       int hash_size = 0;
       char *hash;
       u32 ret;
 
       ret = rockchip_read_resource_dtb(fdt, &hash, &hash_size);
       if (ret) {
           printf("Failed to load DTB, ret=%d\n", ret);
           return ret;
       }
 
       if (fdt_check_header(fdt)) {
           printf("Invalid DTB magic !\n");
           return -EBADF;
       }
#ifdef CONFIG_ROCKCHIP_DTB_VERIFY
       if (hash_size && fdt_check_hash(fdt,
           fdt_totalsize(fdt), hash, hash_size)) {
           printf("Invalid DTB hash !\n");
           return -EBADF;
       }
#endif
       return 0;
#endif
   } else if (where == LOCATE_FIT) {
#if defined(CONFIG_ROCKCHIP_FIT_IMAGE) && !defined(CONFIG_ROCKCHIP_RESOURCE_IMAGE)
       return fit_image_read_dtb(fdt);
#endif
   }
 
   return -EINVAL;
}
 
int rockchip_read_dtb_file(void *fdt)
{
   int locate, ret;
   int size;
 
   for (locate = 0; locate < LOCATE_END; locate++) {
       ret = dtb_scan(fdt, locate);
       if (!ret)
           break;
   }
   if (ret) {
       printf("No valid DTB, ret=%d\n", ret);
       return ret;
   }
 
   /* reserved memory */
   size = fdt_totalsize(fdt);
   if (!sysmem_alloc_base(MEM_FDT, (phys_addr_t)fdt,
       ALIGN(size, RK_BLK_SIZE) + CONFIG_SYS_FDT_PAD))
       return -ENOMEM;
 
   /* fixup/overlay */
   rk_board_early_fdt_fixup(fdt);
#if defined(CONFIG_ANDROID_BOOT_IMAGE) && defined(CONFIG_OF_LIBFDT_OVERLAY)
   android_fdt_overlay_apply((void *)fdt);
#endif
 
   return 0;
}
#endif
 
int rockchip_ram_read_dtb_file(void *img, void *fdt)
{
   int format;
   int ret;
 
   format = (genimg_get_format(img));
#ifdef CONFIG_ANDROID_BOOT_IMAGE
   if (format == IMAGE_FORMAT_ANDROID) {
       struct andr_img_hdr *hdr = img;
       struct blk_desc *dev_desc;
       ulong offset;
 
       dev_desc = rockchip_get_bootdev();
       if (!dev_desc)
           return -ENODEV;
 
       offset = hdr->page_size + ALIGN(hdr->kernel_size, hdr->page_size) +
           ALIGN(hdr->ramdisk_size, hdr->page_size);
#ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE
       ret = resource_setup_ram_list(dev_desc, (void *)hdr + offset);
       if (ret)
           return ret;
 
       return rockchip_read_dtb_file((void *)fdt);
#else
       if (fdt_check_header((void *)offset))
           return -EINVAL;
 
       memcpy(fdt, (char *)offset, fdt_totalsize(offset));
       if (!sysmem_alloc_base(MEM_FDT, (phys_addr_t)fdt,
           ALIGN(fdt_totalsize(fdt), RK_BLK_SIZE) + CONFIG_SYS_FDT_PAD))
           return -ENOMEM;
 
       return 0;
#endif
   }
#endif
#if IMAGE_ENABLE_FIT
   if (format == IMAGE_FORMAT_FIT) {
       const void *data;
       size_t size;
       int noffset;
#ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE
       const char *path = "/images/resource";
#else
       const char *path = "/images/fdt";
#endif
 
       noffset = fdt_path_offset(img, path);
       if (noffset < 0)
           return noffset;
 
#ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE
       ret = fit_image_get_data(img, noffset, &data, &size);
       if (ret < 0)
           return ret;
 
       dev_desc = rockchip_get_bootdev();
       if (!dev_desc)
           return -ENODEV;
 
       ret = resource_setup_ram_list(dev_desc, (void *)data);
       if (ret) {
           printf("resource_setup_ram_list fail, ret=%d\n", ret);
           return ret;
       }
 
       return rockchip_read_dtb_file((void *)fdt);
#else
 
       ret = fit_image_get_data(img, noffset, &data, &size);
       if (ret)
           return ret;
 
       if (fdt_check_header(data))
           return -EINVAL;
 
       memcpy(fdt, data, size);
       if (!sysmem_alloc_base(MEM_FDT, (phys_addr_t)fdt,
           ALIGN(fdt_totalsize(fdt), RK_BLK_SIZE) + CONFIG_SYS_FDT_PAD))
           return -ENOMEM;
 
       printf("Load DTB from 'images/fdt'\n");
 
       return 0;
#endif
   }
#endif
 
   return -EINVAL;
}