1 /* 2 * (C) Copyright 2017 Rockchip Electronics Co., Ltd. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <version.h> 9 #include <abuf.h> 10 #include <amp.h> 11 #include <android_ab.h> 12 #include <android_bootloader.h> 13 #include <android_image.h> 14 #include <bidram.h> 15 #include <boot_rkimg.h> 16 #include <cli.h> 17 #include <clk.h> 18 #include <console.h> 19 #include <debug_uart.h> 20 #include <dm.h> 21 #include <dvfs.h> 22 #include <fdt_support.h> 23 #include <io-domain.h> 24 #include <image.h> 25 #include <key.h> 26 #include <memblk.h> 27 #include <misc.h> 28 #include <of_live.h> 29 #include <mtd_blk.h> 30 #include <ram.h> 31 #include <rng.h> 32 #include <rockchip_debugger.h> 33 #include <syscon.h> 34 #include <sysmem.h> 35 #include <video_rockchip.h> 36 #include <xbc.h> 37 #include <asm/io.h> 38 #include <asm/gpio.h> 39 #include <android_avb/rk_avb_ops_user.h> 40 #include <dm/uclass-internal.h> 41 #include <dm/root.h> 42 #include <power/charge_display.h> 43 #include <power/regulator.h> 44 #include <optee_include/OpteeClientInterface.h> 45 #include <optee_include/OpteeClientApiLib.h> 46 #include <optee_include/tee_api_defines.h> 47 #include <asm/arch/boot_mode.h> 48 #include <asm/arch/clock.h> 49 #include <asm/arch/cpu.h> 50 #include <asm/arch/hotkey.h> 51 #include <asm/arch/param.h> 52 #include <asm/arch/periph.h> 53 #include <asm/arch/resource_img.h> 54 #include <asm/arch/rk_atags.h> 55 #include <asm/arch/vendor.h> 56 #ifdef CONFIG_ROCKCHIP_EINK_DISPLAY 57 #include <rk_eink.h> 58 #endif 59 #ifdef CONFIG_ROCKCHIP_MINIDUMP 60 #include <rk_mini_dump.h> 61 #endif 62 63 #ifdef CONFIG_ARM64 64 static ulong orig_images_ep; 65 #endif 66 67 __weak int rk_board_late_init(void) 68 { 69 return 0; 70 } 71 72 __weak int rk_board_fdt_fixup(void *blob) 73 { 74 return 0; 75 } 76 77 __weak int rk_board_dm_fdt_fixup(void *blob) 78 { 79 return 0; 80 } 81 82 __weak int soc_clk_dump(void) 83 { 84 return 0; 85 } 86 87 __weak int set_armclk_rate(void) 88 { 89 return 0; 90 } 91 92 __weak int rk_board_init(void) 93 { 94 return 0; 95 } 96 97 #ifdef CONFIG_ROCKCHIP_SET_ETHADDR 98 /* 99 * define serialno max length, the max length is 512 Bytes 100 * The remaining bytes are used to ensure that the first 512 bytes 101 * are valid when executing 'env_set("serial#", value)'. 102 */ 103 #define VENDOR_SN_MAX 513 104 #define CPUID_LEN 0x10 105 106 #define MAX_ETHERNET 0x2 107 108 static int rockchip_set_ethaddr(void) 109 { 110 __maybe_unused bool need_write = false; 111 bool randomed = false; 112 char buf[ARP_HLEN_ASCII + 1], mac[16]; 113 u8 ethaddr[ARP_HLEN * MAX_ETHERNET] = {0}; 114 int i, ret = -EINVAL; 115 116 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 117 ret = vendor_storage_read(LAN_MAC_ID, ethaddr, sizeof(ethaddr)); 118 #endif 119 for (i = 0; i < MAX_ETHERNET; i++) { 120 if (ret <= 0 || !is_valid_ethaddr(ðaddr[i * ARP_HLEN])) { 121 if (!randomed) { 122 net_random_ethaddr(ðaddr[i * ARP_HLEN]); 123 randomed = true; 124 } else { 125 if (i > 0) { 126 memcpy(ðaddr[i * ARP_HLEN], 127 ðaddr[(i - 1) * ARP_HLEN], 128 ARP_HLEN); 129 ethaddr[i * ARP_HLEN] |= 0x02; 130 ethaddr[i * ARP_HLEN] += (i << 2); 131 } 132 } 133 134 need_write = true; 135 } 136 137 if (is_valid_ethaddr(ðaddr[i * ARP_HLEN])) { 138 snprintf(buf, ARP_HLEN_ASCII + 1, "%pM", ðaddr[i * ARP_HLEN]); 139 if (i == 0) 140 memcpy(mac, "ethaddr", sizeof("ethaddr")); 141 else 142 sprintf(mac, "eth%daddr", i); 143 env_set(mac, buf); 144 } 145 } 146 147 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 148 if (need_write) { 149 ret = vendor_storage_write(LAN_MAC_ID, 150 ethaddr, sizeof(ethaddr)); 151 if (ret < 0) 152 printf("%s: vendor_storage_write failed %d\n", 153 __func__, ret); 154 } 155 #endif 156 return 0; 157 } 158 #endif 159 160 #ifdef CONFIG_ROCKCHIP_SET_SN 161 static int rockchip_set_serialno(void) 162 { 163 u8 low[CPUID_LEN / 2], high[CPUID_LEN / 2]; 164 u8 cpuid[CPUID_LEN] = {0}; 165 char serialno_str[VENDOR_SN_MAX]; 166 int ret = 0, i; 167 u64 serialno; 168 169 /* Read serial number from vendor storage part */ 170 memset(serialno_str, 0, VENDOR_SN_MAX); 171 172 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 173 int j; 174 175 ret = vendor_storage_read(SN_ID, serialno_str, (VENDOR_SN_MAX-1)); 176 if (ret > 0) { 177 j = strlen(serialno_str); 178 for (i = 0; i < j; i++) { 179 if ((serialno_str[i] >= 'a' && serialno_str[i] <= 'z') || 180 (serialno_str[i] >= 'A' && serialno_str[i] <= 'Z') || 181 (serialno_str[i] >= '0' && serialno_str[i] <= '9')) { 182 continue; 183 } else { 184 if (i > 0) 185 serialno_str[i] = 0x0; 186 break; 187 } 188 } 189 190 /* valid character count > 0 */ 191 if (i > 0) { 192 serialno_str[i + 1] = 0x0; 193 env_set("serial#", serialno_str); 194 } 195 } 196 #endif 197 if (!env_get("serial#")) { 198 #if defined(CONFIG_ROCKCHIP_EFUSE) || defined(CONFIG_ROCKCHIP_OTP) 199 struct udevice *dev; 200 201 /* retrieve the device */ 202 if (IS_ENABLED(CONFIG_ROCKCHIP_EFUSE)) 203 ret = uclass_get_device_by_driver(UCLASS_MISC, 204 DM_GET_DRIVER(rockchip_efuse), 205 &dev); 206 else 207 ret = uclass_get_device_by_driver(UCLASS_MISC, 208 DM_GET_DRIVER(rockchip_otp), 209 &dev); 210 211 if (ret) { 212 printf("%s: could not find efuse/otp device\n", __func__); 213 return ret; 214 } 215 216 /* read the cpu_id range from the efuses */ 217 ret = misc_read(dev, CFG_CPUID_OFFSET, &cpuid, sizeof(cpuid)); 218 if (ret) { 219 printf("%s: read cpuid from efuse/otp failed, ret=%d\n", 220 __func__, ret); 221 return ret; 222 } 223 #else 224 /* generate random cpuid */ 225 for (i = 0; i < CPUID_LEN; i++) 226 cpuid[i] = (u8)(rand()); 227 #endif 228 /* Generate the serial number based on CPU ID */ 229 for (i = 0; i < 8; i++) { 230 low[i] = cpuid[1 + (i << 1)]; 231 high[i] = cpuid[i << 1]; 232 } 233 234 serialno = crc32_no_comp(0, low, 8); 235 serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32; 236 snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno); 237 238 env_set("serial#", serialno_str); 239 } 240 241 return ret; 242 } 243 #endif 244 245 #if defined(CONFIG_USB_FUNCTION_FASTBOOT) 246 int fb_set_reboot_flag(void) 247 { 248 printf("Setting reboot to fastboot flag ...\n"); 249 writel(BOOT_FASTBOOT, CONFIG_ROCKCHIP_BOOT_MODE_REG); 250 251 return 0; 252 } 253 #endif 254 255 #ifdef CONFIG_ROCKCHIP_USB_BOOT 256 static int boot_from_udisk(void) 257 { 258 struct blk_desc *desc; 259 struct udevice *dev; 260 void *fdt_addr; 261 u32 fdt_size; 262 int devnum = -1; 263 char buf[32]; 264 265 /* Booting priority: mmc1 > udisk */ 266 if (!strcmp(env_get("devtype"), "mmc") && !strcmp(env_get("devnum"), "1")) 267 return 0; 268 269 if (!run_command("usb start", -1)) { 270 for (blk_first_device(IF_TYPE_USB, &dev); 271 dev; 272 blk_next_device(&dev)) { 273 desc = dev_get_uclass_platdata(dev); 274 printf("Scanning usb %d ...\n", desc->devnum); 275 if (desc->type == DEV_TYPE_UNKNOWN) 276 continue; 277 278 if (desc->lba > 0L && desc->blksz > 0L) { 279 devnum = desc->devnum; 280 break; 281 } 282 } 283 if (devnum < 0) { 284 printf("No usb mass storage found\n"); 285 return -ENODEV; 286 } 287 288 desc = blk_get_devnum_by_type(IF_TYPE_USB, devnum); 289 if (!desc) { 290 printf("No usb %d found\n", devnum); 291 return -ENODEV; 292 } 293 294 snprintf(buf, 32, "rkimgtest usb %d", devnum); 295 if (!run_command(buf, -1)) { 296 snprintf(buf, 32, "%d", devnum); 297 rockchip_set_bootdev(desc); 298 env_set("devtype", "usb"); 299 env_set("devnum", buf); 300 printf("=== Booting from usb %d ===\n", devnum); 301 302 /* 303 * If current kernel dtb is not from embedded dtb: 304 * 305 * Use 'gd->fdt_blob_kern' to reload the kernel dtb 306 * from current bootdev(udisk) in the late bootflow. 307 */ 308 if (!gd->fdt_blob_kern) { 309 fdt_size = fdt_totalsize(gd->fdt_blob); 310 fdt_addr = memalign(ARCH_DMA_MINALIGN, fdt_size); 311 if (!fdt_addr) 312 return -ENOMEM; 313 314 /* destroy kernel dtb and resource list */ 315 memcpy(fdt_addr, gd->fdt_blob, fdt_size); 316 fdt_set_magic((void *)gd->fdt_blob, ~0); 317 sysmem_free((phys_addr_t)gd->fdt_blob); 318 resource_destroy(); 319 320 gd->fdt_blob_kern = fdt_addr; 321 gd->fdt_blob = fdt_addr; 322 } 323 } else { 324 printf("No available udisk image on usb %d\n", devnum); 325 return -ENODEV; 326 } 327 } 328 329 return 0; 330 } 331 #endif 332 333 static void env_fixup(void) 334 { 335 struct memblock mem; 336 ulong u_addr_r; 337 phys_size_t end; 338 char *addr_r; 339 340 #ifdef ENV_MEM_LAYOUT_SETTINGS1 341 const char *env_addr0[] = { 342 "scriptaddr", "pxefile_addr_r", 343 "fdt_addr_r", "kernel_addr_r", "kernel_addr_c", "ramdisk_addr_r", 344 }; 345 const char *env_addr1[] = { 346 "scriptaddr1", "pxefile_addr1_r", 347 "fdt_addr1_r", "kernel_addr1_r", "kernel_addr1_c", "ramdisk_addr1_r", 348 }; 349 int i; 350 351 /* 128M is a typical ram size for most platform, so as default here */ 352 if (gd->ram_size <= SZ_128M) { 353 /* Replace orignal xxx_addr_r */ 354 for (i = 0; i < ARRAY_SIZE(env_addr1); i++) { 355 addr_r = env_get(env_addr1[i]); 356 if (addr_r) 357 env_set(env_addr0[i], addr_r); 358 } 359 } 360 #endif 361 /* No BL32 ? */ 362 if (!(gd->flags & GD_FLG_BL32_ENABLED)) { 363 /* 364 * [1] Move kernel to lower address if possible. 365 */ 366 addr_r = env_get("kernel_addr_no_low_bl32_r"); 367 if (addr_r) 368 env_set("kernel_addr_r", addr_r); 369 370 /* 371 * [2] Move ramdisk at BL32 position if need. 372 * 373 * 0x0a200000 and 0x08400000 are rockchip traditional address 374 * of BL32 and ramdisk: 375 * 376 * |------------|------------| 377 * | BL32 | ramdisk | 378 * |------------|------------| 379 * 380 * Move ramdisk to BL32 address to fix sysmem alloc failed 381 * issue on the board with critical memory(ie. 256MB). 382 */ 383 if (gd->ram_size > SZ_128M && gd->ram_size <= SZ_256M) { 384 u_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0); 385 if (u_addr_r == 0x0a200000) 386 env_set("ramdisk_addr_r", "0x08400000"); 387 } 388 } else { 389 mem = param_parse_optee_mem(); 390 391 /* 392 * [1] Move kernel forward if possible. 393 */ 394 if (mem.base > SZ_128M) { 395 addr_r = env_get("kernel_addr_no_low_bl32_r"); 396 if (addr_r) 397 env_set("kernel_addr_r", addr_r); 398 } 399 400 /* 401 * [2] Move ramdisk backward if optee enlarge. 402 */ 403 end = mem.base + mem.size; 404 u_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0); 405 if (u_addr_r >= mem.base && u_addr_r < end) 406 env_set_hex("ramdisk_addr_r", end); 407 } 408 } 409 410 static void cmdline_handle(void) 411 { 412 struct blk_desc *dev_desc; 413 int if_type; 414 int devnum; 415 416 param_parse_pubkey_fuse_programmed(); 417 418 dev_desc = rockchip_get_bootdev(); 419 if (!dev_desc) 420 return; 421 422 /* 423 * 1. From rk356x, the sd/udisk recovery update flag was moved from 424 * IDB to Android BCB. 425 * 426 * 2. Udisk is init at the late boot_from_udisk(), but 427 * rockchip_get_boot_mode() actually only read once, 428 * we need to update boot mode according to udisk BCB. 429 */ 430 if_type = dev_desc->if_type; 431 devnum = dev_desc->devnum; 432 if ((if_type == IF_TYPE_MMC && devnum == 1) || (if_type == IF_TYPE_USB)) { 433 if (get_bcb_recovery_msg() == BCB_MSG_RECOVERY_RK_FWUPDATE) { 434 if (if_type == IF_TYPE_MMC && devnum == 1) { 435 env_update("bootargs", "sdfwupdate"); 436 } else if (if_type == IF_TYPE_USB) { 437 env_update("bootargs", "usbfwupdate"); 438 env_set("reboot_mode", "recovery-usb"); 439 } 440 } else { 441 if (if_type == IF_TYPE_USB) 442 env_set("reboot_mode", "normal"); 443 } 444 } 445 446 if (rockchip_get_boot_mode() == BOOT_MODE_QUIESCENT) 447 env_update("bootargs", "androidboot.quiescent=1 pwm_bl.quiescent=1"); 448 } 449 450 static void scan_run_cmd(void) 451 { 452 char *config = CONFIG_ROCKCHIP_CMD; 453 char *cmd, *key; 454 455 key = strchr(config, ' '); 456 if (!key) 457 return; 458 459 cmd = strdup(config); 460 cmd[key - config] = 0; 461 key++; 462 463 if (!strcmp(key, "-")) { 464 run_command(cmd, 0); 465 } else { 466 #ifdef CONFIG_DM_KEY 467 ulong map; 468 469 map = simple_strtoul(key, NULL, 10); 470 if (key_is_pressed(key_read(map))) { 471 printf("## Key<%ld> pressed... run cmd '%s'\n", map, cmd); 472 run_command(cmd, 0); 473 } 474 #endif 475 } 476 } 477 478 int board_late_init(void) 479 { 480 #ifdef CONFIG_ROCKCHIP_SET_ETHADDR 481 rockchip_set_ethaddr(); 482 #endif 483 #ifdef CONFIG_ROCKCHIP_SET_SN 484 rockchip_set_serialno(); 485 #endif 486 setup_download_mode(); 487 scan_run_cmd(); 488 #ifdef CONFIG_ROCKCHIP_USB_BOOT 489 boot_from_udisk(); 490 #endif 491 #ifdef CONFIG_DM_CHARGE_DISPLAY 492 charge_display(); 493 #endif 494 495 #ifdef CONFIG_ROCKCHIP_MINIDUMP 496 rk_minidump_init(); 497 #endif 498 499 #ifdef CONFIG_DRM_ROCKCHIP 500 if (rockchip_get_boot_mode() != BOOT_MODE_QUIESCENT) 501 rockchip_show_logo(); 502 #endif 503 #ifdef CONFIG_ROCKCHIP_EINK_DISPLAY 504 rockchip_eink_show_uboot_logo(); 505 #endif 506 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG > 0) 507 setup_boot_mode(); 508 #endif 509 env_fixup(); 510 soc_clk_dump(); 511 cmdline_handle(); 512 #ifdef CONFIG_AMP 513 amp_cpus_on(); 514 #endif 515 return rk_board_late_init(); 516 } 517 518 static void early_download(void) 519 { 520 #if defined(CONFIG_PWRKEY_DNL_TRIGGER_NUM) && \ 521 (CONFIG_PWRKEY_DNL_TRIGGER_NUM > 0) 522 if (pwrkey_download_init()) 523 printf("Pwrkey download init failed\n"); 524 #endif 525 526 #if (CONFIG_ROCKCHIP_BOOT_MODE_REG > 0) 527 if (is_hotkey(HK_BROM_DNL)) { 528 printf("Enter bootrom download..."); 529 flushc(); 530 writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG); 531 do_reset(NULL, 0, 0, NULL); 532 printf("failed!\n"); 533 } 534 #endif 535 } 536 537 static void board_debug_init(void) 538 { 539 if (!gd->serial.using_pre_serial && 540 !(gd->flags & GD_FLG_DISABLE_CONSOLE)) 541 debug_uart_init(); 542 543 if (tstc()) { 544 gd->console_evt = getc(); 545 if (gd->console_evt <= 0x1a) /* 'z' */ 546 printf("Hotkey: ctrl+%c\n", gd->console_evt + 'a' - 1); 547 } 548 549 if (IS_ENABLED(CONFIG_CONSOLE_DISABLE_CLI)) 550 printf("Cmd interface: disabled\n"); 551 } 552 553 int board_init(void) 554 { 555 board_debug_init(); 556 #ifdef DEBUG 557 soc_clk_dump(); 558 #endif 559 #ifdef CONFIG_OPTEE_CLIENT 560 optee_client_init(); 561 #endif 562 #ifdef CONFIG_USING_KERNEL_DTB 563 init_kernel_dtb(); 564 #endif 565 early_download(); 566 567 clks_probe(); 568 #ifdef CONFIG_DM_REGULATOR 569 regulators_enable_boot_on(is_hotkey(HK_REGULATOR)); 570 #endif 571 #ifdef CONFIG_ROCKCHIP_IO_DOMAIN 572 io_domain_init(); 573 #endif 574 set_armclk_rate(); 575 #ifdef CONFIG_DM_DVFS 576 dvfs_init(true); 577 #endif 578 #ifdef CONFIG_ANDROID_AB 579 if (ab_decrease_tries()) 580 printf("Decrease ab tries count fail!\n"); 581 #endif 582 return rk_board_init(); 583 } 584 585 int interrupt_debugger_init(void) 586 { 587 #ifdef CONFIG_ROCKCHIP_DEBUGGER 588 return rockchip_debugger_init(); 589 #else 590 return 0; 591 #endif 592 } 593 594 #ifdef CONFIG_SANITY_CPU_SWAP 595 static void sanity_cpu_swap(void *blob) 596 { 597 int cpus_offset; 598 int noffset; 599 ulong mpidr; 600 ulong reg; 601 602 cpus_offset = fdt_path_offset(blob, "/cpus"); 603 if (cpus_offset < 0) 604 return; 605 606 for (noffset = fdt_first_subnode(blob, cpus_offset); 607 noffset >= 0; 608 noffset = fdt_next_subnode(blob, noffset)) { 609 const struct fdt_property *prop; 610 int len; 611 612 prop = fdt_get_property(blob, noffset, "device_type", &len); 613 if (!prop) 614 continue; 615 if (len < 4) 616 continue; 617 if (strcmp(prop->data, "cpu")) 618 continue; 619 620 /* only sanity first cpu */ 621 reg = (ulong)fdtdec_get_addr_size_auto_parent(blob, cpus_offset, noffset, 622 "reg", 0, NULL, false); 623 mpidr = read_mpidr() & 0xfff; 624 if ((mpidr & reg) != reg) { 625 printf("CPU swap error: Loader and Kernel firmware mismatch! " 626 "Current cpu0 \"reg\" is 0x%lx but kernel dtb requires 0x%lx\n", 627 mpidr, reg); 628 run_command("download", 0); 629 } 630 return; 631 } 632 } 633 #endif 634 635 static int rockchip_dm_late_init(void *blob) 636 { 637 struct udevice *dev; 638 639 /* Prepare for board_rng_seed(), dryrun and ignore result */ 640 if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && IS_ENABLED(CONFIG_DM_RNG)) 641 uclass_get_device(UCLASS_RNG, 0, &dev); 642 643 return 0; 644 } 645 646 int board_fdt_fixup(void *blob) 647 { 648 #ifdef CONFIG_SANITY_CPU_SWAP 649 sanity_cpu_swap(blob); 650 #endif 651 /* 652 * Device's platdata points to orignal fdt blob property, 653 * access DM device before any fdt fixup. 654 * 655 * Do board specific init and common init. 656 */ 657 rk_board_dm_fdt_fixup(blob); 658 rockchip_dm_late_init(blob); 659 660 /* Common fixup for DRM */ 661 #ifdef CONFIG_DRM_ROCKCHIP 662 rockchip_display_fixup(blob); 663 #endif 664 665 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 666 vendor_storage_fixup(blob); 667 #endif 668 669 return rk_board_fdt_fixup(blob); 670 } 671 672 #if defined(CONFIG_ARM64_BOOT_AARCH32) || !defined(CONFIG_ARM64) 673 /* 674 * Common for OP-TEE: 675 * 64-bit & 32-bit mode: share memory dcache is always enabled; 676 * 677 * Common for U-Boot: 678 * 64-bit mode: MMU table is static defined in rkxxx.c file, all memory 679 * regions are mapped. That's good to match OP-TEE MMU policy. 680 * 681 * 32-bit mode: MMU table is setup according to gd->bd->bi_dram[..] where 682 * the OP-TEE region has been reserved, so it can not be 683 * mapped(i.e. dcache is disabled). That's *NOT* good to match 684 * OP-TEE MMU policy. 685 * 686 * For the data coherence when communication between U-Boot and OP-TEE, U-Boot 687 * should follow OP-TEE MMU policy. 688 * 689 * So 32-bit mode U-Boot should map OP-TEE share memory as dcache enabled. 690 */ 691 int board_initr_caches_fixup(void) 692 { 693 #ifdef CONFIG_OPTEE_CLIENT 694 struct memblock mem; 695 696 mem.base = 0; 697 mem.size = 0; 698 699 optee_get_shm_config(&mem.base, &mem.size); 700 if (mem.size) 701 mmu_set_region_dcache_behaviour(mem.base, mem.size, 702 DCACHE_WRITEBACK); 703 #endif 704 return 0; 705 } 706 #endif 707 708 void arch_preboot_os(uint32_t bootm_state, bootm_headers_t *images) 709 { 710 if (!(bootm_state & BOOTM_STATE_OS_PREP)) 711 return; 712 713 #ifdef CONFIG_ARM64 714 u8 *data = (void *)images->ep; 715 716 /* 717 * Fix kernel 5.10 arm64 boot warning: 718 * "[Firmware Bug]: Kernel image misaligned at boot, please fix your bootloader!" 719 * 720 * kernel: 5.10 commit 120dc60d0bdb ("arm64: get rid of TEXT_OFFSET") 721 * arm64 kernel version: 722 * data[10] == 0x00 if kernel version >= 5.10: N*2MB align 723 * data[10] == 0x08 if kernel version < 5.10: N*2MB + 0x80000(TEXT_OFFSET) 724 * 725 * Why fix here? 726 * 1. this is the common and final path for any boot command. 727 * 2. don't influence original boot flow, just fix it exactly before 728 * jumping kernel. 729 * 730 * But relocation is in board_quiesce_devices() until all decompress 731 * done, mainly for saving boot time. 732 */ 733 734 orig_images_ep = images->ep; 735 736 if (data[10] == 0x00) { 737 if (round_down(images->ep, SZ_2M) != images->ep) 738 images->ep = round_down(images->ep, SZ_2M); 739 } else { 740 if (IS_ALIGNED(images->ep, SZ_2M)) 741 images->ep += 0x80000; 742 } 743 #endif 744 hotkey_run(HK_CLI_OS_PRE); 745 } 746 747 void enable_caches(void) 748 { 749 icache_enable(); 750 dcache_enable(); 751 } 752 753 #ifdef CONFIG_LMB 754 /* 755 * Using last bi_dram[...] to initialize "bootm_low" and "bootm_mapsize". 756 * This makes lmb_alloc_base() always alloc from tail of sdram. 757 * If we don't assign it, bi_dram[0] is used by default and it may cause 758 * lmb_alloc_base() fail when bi_dram[0] range is small. 759 */ 760 void board_lmb_reserve(struct lmb *lmb) 761 { 762 char bootm_mapsize[32]; 763 char bootm_low[32]; 764 u64 start, size; 765 int i; 766 767 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { 768 if (!gd->bd->bi_dram[i].size) 769 break; 770 } 771 772 start = gd->bd->bi_dram[i - 1].start; 773 size = gd->bd->bi_dram[i - 1].size; 774 775 /* 776 * 32-bit kernel: ramdisk/fdt shouldn't be loaded to highmem area(768MB+), 777 * otherwise "Unable to handle kernel paging request at virtual address ...". 778 * 779 * So that we hope limit highest address at 768M, but there comes the the 780 * problem: ramdisk is a compressed image and it expands after descompress, 781 * so it accesses 768MB+ and brings the above "Unable to handle kernel ...". 782 * 783 * We make a appointment that the highest memory address is 512MB, it 784 * makes lmb alloc safer. 785 */ 786 #ifndef CONFIG_ARM64 787 if (start >= ((u64)CONFIG_SYS_SDRAM_BASE + SZ_512M)) { 788 start = gd->bd->bi_dram[i - 2].start; 789 size = gd->bd->bi_dram[i - 2].size; 790 } 791 792 if ((start + size) > ((u64)CONFIG_SYS_SDRAM_BASE + SZ_512M)) 793 size = (u64)CONFIG_SYS_SDRAM_BASE + SZ_512M - start; 794 #endif 795 sprintf(bootm_low, "0x%llx", start); 796 sprintf(bootm_mapsize, "0x%llx", size); 797 env_set("bootm_low", bootm_low); 798 env_set("bootm_mapsize", bootm_mapsize); 799 } 800 #endif 801 802 #ifdef CONFIG_BIDRAM 803 int board_bidram_reserve(struct bidram *bidram) 804 { 805 struct memblock mem; 806 int ret; 807 808 /* ATF */ 809 mem = param_parse_atf_mem(); 810 ret = bidram_reserve(MEM_ATF, mem.base, mem.size); 811 if (ret) 812 return ret; 813 814 /* PSTORE/ATAGS/SHM */ 815 mem = param_parse_common_resv_mem(); 816 ret = bidram_reserve(MEM_SHM, mem.base, mem.size); 817 if (ret) 818 return ret; 819 820 /* OP-TEE */ 821 mem = param_parse_optee_mem(); 822 ret = bidram_reserve(MEM_OPTEE, mem.base, mem.size); 823 if (ret) 824 return ret; 825 826 return 0; 827 } 828 829 #ifdef CONFIG_SYSMEM 830 int board_sysmem_reserve(struct sysmem *sysmem) 831 { 832 #ifdef CONFIG_SKIP_RELOCATE_UBOOT 833 if (!sysmem_alloc_base_by_name("NO-RELOC-CODE", 834 CONFIG_SYS_TEXT_BASE, SZ_2M)) { 835 printf("Failed to reserve sysmem for U-Boot code\n"); 836 return -ENOMEM; 837 } 838 #endif 839 return 0; 840 } 841 #endif 842 843 parse_fn_t board_bidram_parse_fn(void) 844 { 845 return param_parse_ddr_mem; 846 } 847 #endif 848 849 int board_init_f_boot_flags(void) 850 { 851 int boot_flags = 0; 852 853 #ifdef CONFIG_ARM64 854 asm volatile("mrs %0, cntfrq_el0" : "=r" (gd->arch.timer_rate_hz)); 855 #else 856 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (gd->arch.timer_rate_hz)); 857 #endif 858 859 #if CONFIG_IS_ENABLED(FPGA_ROCKCHIP) 860 arch_fpga_init(); 861 #endif 862 #ifdef CONFIG_PSTORE 863 param_parse_pstore(); 864 #endif 865 param_parse_pre_serial(&boot_flags); 866 867 /* The highest priority to turn off (override) console */ 868 #if defined(CONFIG_DISABLE_CONSOLE) 869 boot_flags |= GD_FLG_DISABLE_CONSOLE; 870 #endif 871 872 return boot_flags; 873 } 874 875 #if defined(CONFIG_USB_GADGET) 876 #include <usb.h> 877 #if defined(CONFIG_USB_GADGET_DWC2_OTG) 878 #include <fdt_support.h> 879 #include <usb/dwc2_udc.h> 880 881 static struct dwc2_plat_otg_data otg_data = { 882 .rx_fifo_sz = 512, 883 .np_tx_fifo_sz = 16, 884 .tx_fifo_sz = 128, 885 }; 886 887 int board_usb_init(int index, enum usb_init_type init) 888 { 889 const void *blob = gd->fdt_blob; 890 const fdt32_t *reg; 891 fdt_addr_t addr; 892 int node; 893 894 /* find the usb_otg node */ 895 node = fdt_node_offset_by_compatible(blob, -1, "snps,dwc2"); 896 897 retry: 898 if (node > 0) { 899 reg = fdt_getprop(blob, node, "reg", NULL); 900 if (!reg) 901 return -EINVAL; 902 903 addr = fdt_translate_address(blob, node, reg); 904 if (addr == OF_BAD_ADDR) { 905 pr_err("Not found usb_otg address\n"); 906 return -EINVAL; 907 } 908 909 #if defined(CONFIG_ROCKCHIP_RK3288) 910 if (addr != 0xff580000) { 911 node = fdt_node_offset_by_compatible(blob, node, 912 "snps,dwc2"); 913 goto retry; 914 } 915 #endif 916 } else { 917 /* 918 * With kernel dtb support, rk3288 dwc2 otg node 919 * use the rockchip legacy dwc2 driver "dwc_otg_310" 920 * with the compatible "rockchip,rk3288_usb20_otg", 921 * and rk3368 also use the "dwc_otg_310" driver with 922 * the compatible "rockchip,rk3368-usb". 923 */ 924 #if defined(CONFIG_ROCKCHIP_RK3288) 925 node = fdt_node_offset_by_compatible(blob, -1, 926 "rockchip,rk3288_usb20_otg"); 927 #elif defined(CONFIG_ROCKCHIP_RK3368) 928 node = fdt_node_offset_by_compatible(blob, -1, 929 "rockchip,rk3368-usb"); 930 #endif 931 if (node > 0) { 932 goto retry; 933 } else { 934 pr_err("Not found usb_otg device\n"); 935 return -ENODEV; 936 } 937 } 938 939 otg_data.regs_otg = (uintptr_t)addr; 940 941 return dwc2_udc_probe(&otg_data); 942 } 943 944 int board_usb_cleanup(int index, enum usb_init_type init) 945 { 946 return 0; 947 } 948 #elif defined(CONFIG_USB_DWC3_GADGET) /* CONFIG_USB_GADGET_DWC2_OTG */ 949 #include <dwc3-uboot.h> 950 951 int board_usb_cleanup(int index, enum usb_init_type init) 952 { 953 dwc3_uboot_exit(index); 954 return 0; 955 } 956 957 #endif /* CONFIG_USB_DWC3_GADGET */ 958 #endif /* CONFIG_USB_GADGET */ 959 960 static void bootm_no_reloc(void) 961 { 962 char *ramdisk_high; 963 char *fdt_high; 964 965 if (!env_get_yesno("bootm-no-reloc")) 966 return; 967 968 ramdisk_high = env_get("initrd_high"); 969 fdt_high = env_get("fdt_high"); 970 971 if (!fdt_high) { 972 env_set_hex("fdt_high", -1UL); 973 printf("Fdt "); 974 } 975 976 if (!ramdisk_high) { 977 env_set_hex("initrd_high", -1UL); 978 printf("Ramdisk "); 979 } 980 981 if (!fdt_high || !ramdisk_high) 982 printf("skip relocation\n"); 983 } 984 985 int bootm_board_start(void) 986 { 987 /* 988 * print console record data 989 * 990 * On some rockchip platforms, uart debug and sdmmc pin are multiplex. 991 * If boot from sdmmc mode, the console data would be record in buffer, 992 * we switch to uart debug function in order to print it after loading 993 * images. 994 */ 995 #if 0 996 if (!strcmp("mmc", env_get("devtype")) && 997 !strcmp("1", env_get("devnum"))) { 998 printf("IOMUX: sdmmc => uart debug"); 999 pinctrl_select_state(gd->cur_serial_dev, "default"); 1000 console_record_print_purge(); 1001 } 1002 #endif 1003 /* disable bootm relcation to save boot time */ 1004 bootm_no_reloc(); 1005 1006 /* PCBA test needs more permission */ 1007 if (get_bcb_recovery_msg() == BCB_MSG_RECOVERY_PCBA) 1008 env_update("bootargs", "androidboot.selinux=permissive"); 1009 1010 /* sysmem */ 1011 hotkey_run(HK_SYSMEM); 1012 sysmem_overflow_check(); 1013 1014 return 0; 1015 } 1016 1017 int bootm_image_populate_dtb(void *img) 1018 { 1019 if ((gd->flags & GD_FLG_KDTB_READY) && !gd->fdt_blob_kern) 1020 sysmem_free((phys_addr_t)gd->fdt_blob); 1021 else 1022 gd->fdt_blob = (void *)env_get_ulong("fdt_addr_r", 16, 0); 1023 1024 return rockchip_ram_read_dtb_file(img, (void *)gd->fdt_blob); 1025 } 1026 1027 /* 1028 * Implement it to support CLI command: 1029 * - Android: bootm [aosp addr] 1030 * - FIT: bootm [fit addr] 1031 * - uImage: bootm [uimage addr] 1032 * 1033 * Purpose: 1034 * - The original bootm command args require fdt addr on AOSP, 1035 * which is not flexible on rockchip boot/recovery.img. 1036 * - Take Android/FIT/uImage image into sysmem management to avoid image 1037 * memory overlap. 1038 */ 1039 #if defined(CONFIG_ANDROID_BOOTLOADER) || \ 1040 defined(CONFIG_ROCKCHIP_FIT_IMAGE) || \ 1041 defined(CONFIG_ROCKCHIP_UIMAGE) 1042 int board_do_bootm(int argc, char * const argv[]) 1043 { 1044 int format; 1045 void *img; 1046 1047 /* only 'bootm' full image goes further */ 1048 if (argc != 2) 1049 return 0; 1050 1051 img = (void *)simple_strtoul(argv[1], NULL, 16); 1052 format = (genimg_get_format(img)); 1053 1054 /* Android */ 1055 #ifdef CONFIG_ANDROID_BOOT_IMAGE 1056 if (format == IMAGE_FORMAT_ANDROID) { 1057 struct andr_img_hdr *hdr; 1058 ulong load_addr; 1059 ulong size; 1060 int ret; 1061 1062 hdr = (struct andr_img_hdr *)img; 1063 printf("BOOTM: transferring to board Android\n"); 1064 1065 load_addr = env_get_ulong("kernel_addr_r", 16, 0); 1066 load_addr -= hdr->page_size; 1067 size = android_image_get_end(hdr) - (ulong)hdr; 1068 1069 if (!sysmem_alloc_base(MEM_ANDROID, (ulong)hdr, size)) 1070 return -ENOMEM; 1071 1072 ret = bootm_image_populate_dtb(img); 1073 if (ret) { 1074 printf("bootm can't read dtb, ret=%d\n", ret); 1075 return ret; 1076 } 1077 1078 ret = android_image_memcpy_separate(hdr, &load_addr); 1079 if (ret) { 1080 printf("board do bootm failed, ret=%d\n", ret); 1081 return ret; 1082 } 1083 1084 return android_bootloader_boot_kernel(load_addr); 1085 } 1086 #endif 1087 1088 /* FIT */ 1089 #if IMAGE_ENABLE_FIT 1090 if (format == IMAGE_FORMAT_FIT) { 1091 char boot_cmd[64]; 1092 int ret; 1093 1094 printf("BOOTM: transferring to board FIT\n"); 1095 1096 ret = bootm_image_populate_dtb(img); 1097 if (ret) { 1098 printf("bootm can't read dtb, ret=%d\n", ret); 1099 return ret; 1100 } 1101 snprintf(boot_cmd, sizeof(boot_cmd), "boot_fit %s", argv[1]); 1102 return run_command(boot_cmd, 0); 1103 } 1104 #endif 1105 1106 /* uImage */ 1107 #if 0 1108 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 1109 if (format == IMAGE_FORMAT_LEGACY && 1110 image_get_type(img) == IH_TYPE_MULTI) { 1111 char boot_cmd[64]; 1112 1113 printf("BOOTM: transferring to board uImage\n"); 1114 snprintf(boot_cmd, sizeof(boot_cmd), "boot_uimage %s", argv[1]); 1115 return run_command(boot_cmd, 0); 1116 } 1117 #endif 1118 #endif 1119 return 0; 1120 } 1121 #endif 1122 1123 void autoboot_command_fail_handle(void) 1124 { 1125 #ifdef CONFIG_ANDROID_AB 1126 if (rk_avb_ab_have_bootable_slot() == true) 1127 run_command("reset;", 0); 1128 else 1129 run_command("fastboot usb 0;", 0); 1130 #endif 1131 1132 #ifdef CONFIG_AVB_VBMETA_PUBLIC_KEY_VALIDATE 1133 run_command("download", 0); 1134 run_command("fastboot usb 0;", 0); 1135 #endif 1136 1137 } 1138 1139 #ifdef CONFIG_FIT_ROLLBACK_PROTECT 1140 1141 #define FIT_ROLLBACK_INDEX_LOCATION 0x66697472 /* "fitr" */ 1142 1143 int fit_read_otp_rollback_index(uint32_t fit_index, uint32_t *otp_index) 1144 { 1145 #ifdef CONFIG_OPTEE_CLIENT 1146 u64 index; 1147 int ret; 1148 1149 ret = trusty_read_rollback_index(FIT_ROLLBACK_INDEX_LOCATION, &index); 1150 if (ret) { 1151 if (ret != TEE_ERROR_ITEM_NOT_FOUND) 1152 return ret; 1153 1154 index = 0; 1155 printf("Initial otp index as %d\n", fit_index); 1156 } 1157 1158 *otp_index = (uint32_t)index; 1159 #else 1160 *otp_index = 0; 1161 #endif 1162 1163 return 0; 1164 } 1165 1166 int fit_write_trusty_rollback_index(u32 trusty_index) 1167 { 1168 if (!trusty_index) 1169 return 0; 1170 #ifdef CONFIG_OPTEE_CLIENT 1171 return trusty_write_rollback_index(FIT_ROLLBACK_INDEX_LOCATION, 1172 (u64)trusty_index); 1173 #else 1174 return 0; 1175 #endif 1176 } 1177 #endif 1178 1179 void board_quiesce_devices(void *images) 1180 { 1181 #ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS 1182 /* Destroy atags makes next warm boot safer */ 1183 atags_destroy(); 1184 #endif 1185 #ifdef CONFIG_FIT_ROLLBACK_PROTECT 1186 int ret; 1187 1188 ret = fit_write_trusty_rollback_index(gd->rollback_index); 1189 if (ret) { 1190 panic("Failed to write fit rollback index %d, ret=%d", 1191 gd->rollback_index, ret); 1192 } 1193 #endif 1194 #ifdef CONFIG_ROCKCHIP_HW_DECOMPRESS 1195 misc_decompress_cleanup(); 1196 #endif 1197 #ifdef CONFIG_ARM64 1198 bootm_headers_t *bootm_images = (bootm_headers_t *)images; 1199 1200 /* relocate kernel after decompress cleanup */ 1201 if (orig_images_ep && orig_images_ep != bootm_images->ep) { 1202 memmove((char *)bootm_images->ep, (const char *)orig_images_ep, 1203 bootm_images->os.image_len); 1204 printf("== DO RELOCATE == Kernel from 0x%08lx to 0x%08lx\n", 1205 orig_images_ep, bootm_images->ep); 1206 } 1207 #endif 1208 1209 hotkey_run(HK_CMDLINE); 1210 hotkey_run(HK_CLI_OS_GO); 1211 #ifdef CONFIG_ROCKCHIP_REBOOT_TEST 1212 do_reset(NULL, 0, 0, NULL); 1213 #endif 1214 } 1215 1216 /* 1217 * Use hardware rng to seed Linux random 1218 * 1219 * 'Android_14 + GKI' requires this information. 1220 */ 1221 int board_rng_seed(struct abuf *buf) 1222 { 1223 #ifdef CONFIG_DM_RNG 1224 struct udevice *dev; 1225 #endif 1226 size_t len = 32; 1227 u8 *data; 1228 int i; 1229 1230 data = malloc(len); 1231 if (!data) { 1232 printf("Out of memory\n"); 1233 return -ENOMEM; 1234 } 1235 1236 #ifdef CONFIG_DM_RNG 1237 if (uclass_get_device(UCLASS_RNG, 0, &dev) || dm_rng_read(dev, data, len)) 1238 #endif 1239 { 1240 printf("board seed: Pseudo\n"); 1241 for (i = 0; i < len; i++) 1242 data[i] = (u8)rand(); 1243 } 1244 1245 abuf_init_set(buf, data, len); 1246 1247 return 0; 1248 } 1249 1250 /* 1251 * Pass fwver when any available. 1252 */ 1253 static void bootargs_add_fwver(bool verbose) 1254 { 1255 #ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS 1256 struct tag *t; 1257 char *list1 = NULL; 1258 char *list2 = NULL; 1259 char *fwver = NULL; 1260 char *p = PLAIN_VERSION; 1261 int i, end; 1262 1263 t = atags_get_tag(ATAG_FWVER); 1264 if (t) { 1265 list1 = calloc(1, sizeof(struct tag_fwver)); 1266 if (!list1) 1267 return; 1268 for (i = 0; i < FW_MAX; i++) { 1269 if (t->u.fwver.ver[i][0] != '\0') { 1270 strcat(list1, t->u.fwver.ver[i]); 1271 strcat(list1, ","); 1272 } 1273 } 1274 } 1275 1276 list2 = calloc(1, FWVER_LEN); 1277 if (!list2) 1278 goto out; 1279 strcat(list2, "uboot-"); 1280 /* optional */ 1281 #ifdef BUILD_TAG 1282 strcat(list2, BUILD_TAG); 1283 strcat(list2, "-"); 1284 #endif 1285 /* optional */ 1286 if (strcmp(PLAIN_VERSION, "2017.09")) { 1287 strncat(list2, p + strlen("2017.09-g"), 10); 1288 strcat(list2, "-"); 1289 } 1290 strcat(list2, U_BOOT_DMI_DATE); 1291 1292 /* merge ! */ 1293 if (list1 || list2) { 1294 fwver = calloc(1, sizeof(struct tag_fwver)); 1295 if (!fwver) 1296 goto out; 1297 1298 strcat(fwver, "androidboot.fwver="); 1299 if (list1) 1300 strcat(fwver, list1); 1301 if (list2) { 1302 strcat(fwver, list2); 1303 } else { 1304 end = strlen(fwver) - 1; 1305 fwver[end] = '\0'; /* omit last ',' */ 1306 } 1307 if (verbose) 1308 printf("## fwver: %s\n\n", fwver); 1309 env_update("bootargs", fwver); 1310 env_set("fwver", fwver + strlen("androidboot.")); 1311 } 1312 out: 1313 if (list1) 1314 free(list1); 1315 if (list2) 1316 free(list2); 1317 if (fwver) 1318 free(fwver); 1319 #endif 1320 } 1321 1322 static void bootargs_add_android(bool verbose) 1323 { 1324 #ifdef CONFIG_ANDROID_AB 1325 ab_update_root_partition(); 1326 #endif 1327 1328 /* Android header v4+ need this handle */ 1329 #ifdef CONFIG_ANDROID_BOOT_IMAGE 1330 struct andr_img_hdr *hdr; 1331 char *fwver; 1332 1333 hdr = (void *)env_get_ulong("android_addr_r", 16, 0); 1334 if (hdr && !android_image_check_header(hdr) && hdr->header_version >= 4) { 1335 if (env_update_extract_subset("bootargs", "andr_bootargs", "androidboot.")) 1336 printf("extract androidboot.xxx error\n"); 1337 if (verbose) 1338 printf("## bootargs(android): %s\n\n", env_get("andr_bootargs")); 1339 1340 /* for kernel cmdline can be read */ 1341 fwver = env_get("fwver"); 1342 if (fwver) { 1343 env_update("bootargs", fwver); 1344 env_set("fwver", NULL); 1345 } 1346 } 1347 #endif 1348 } 1349 1350 static void bootargs_add_partition(bool verbose) 1351 { 1352 #if defined(CONFIG_ENVF) || defined(CONFIG_ENV_PARTITION) 1353 char *part_type[] = { "mtdparts", "blkdevparts" }; 1354 char *part_list; 1355 char *env; 1356 int id = 0; 1357 1358 env = env_get(part_type[id]); 1359 if (!env) 1360 env = env_get(part_type[++id]); 1361 if (env) { 1362 if (!strstr(env, part_type[id])) { 1363 part_list = calloc(1, strlen(env) + strlen(part_type[id]) + 2); 1364 if (part_list) { 1365 strcat(part_list, part_type[id]); 1366 strcat(part_list, "="); 1367 strcat(part_list, env); 1368 } 1369 } else { 1370 part_list = env; 1371 } 1372 env_update("bootargs", part_list); 1373 if (verbose) 1374 printf("## parts: %s\n\n", part_list); 1375 } 1376 1377 env = env_get("sys_bootargs"); 1378 if (env) { 1379 env_update("bootargs", env); 1380 if (verbose) 1381 printf("## sys_bootargs: %s\n\n", env); 1382 } 1383 #endif 1384 1385 #ifdef CONFIG_MTD_BLK 1386 if (!env_get("mtdparts")) { 1387 char *mtd_par_info = mtd_part_parse(NULL); 1388 1389 if (mtd_par_info) { 1390 if (memcmp(env_get("devtype"), "mtd", 3) == 0) 1391 env_update("bootargs", mtd_par_info); 1392 } 1393 } 1394 #endif 1395 } 1396 1397 static void bootargs_add_dtb_dtbo(void *fdt, bool verbose) 1398 { 1399 /* bootargs_ext is used when dtbo is applied. */ 1400 const char *arr_bootargs[] = { "bootargs", "bootargs_ext" }; 1401 const char *bootargs; 1402 char *msg = "kernel"; 1403 int i, noffset; 1404 1405 /* find or create "/chosen" node. */ 1406 noffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); 1407 if (noffset < 0) 1408 return; 1409 1410 for (i = 0; i < ARRAY_SIZE(arr_bootargs); i++) { 1411 bootargs = fdt_getprop(fdt, noffset, arr_bootargs[i], NULL); 1412 if (!bootargs) 1413 continue; 1414 if (verbose) 1415 printf("## bootargs(%s-%s): %s\n\n", 1416 msg, arr_bootargs[i], bootargs); 1417 /* 1418 * Append kernel bootargs 1419 * If use AB system, delete default "root=" which route 1420 * to rootfs. Then the ab bootctl will choose the 1421 * high priority system to boot and add its UUID 1422 * to cmdline. The format is "roo=PARTUUID=xxxx...". 1423 */ 1424 #ifdef CONFIG_ANDROID_AB 1425 env_update_filter("bootargs", bootargs, "root="); 1426 #else 1427 env_update("bootargs", bootargs); 1428 #endif 1429 } 1430 } 1431 1432 char *board_fdt_chosen_bootargs(void *fdt) 1433 { 1434 int verbose = is_hotkey(HK_CMDLINE); 1435 const char *bootargs; 1436 1437 /* debug */ 1438 hotkey_run(HK_INITCALL); 1439 if (verbose) 1440 printf("## bootargs(u-boot): %s\n\n", env_get("bootargs")); 1441 1442 bootargs_add_dtb_dtbo(fdt, verbose); 1443 bootargs_add_partition(verbose); 1444 bootargs_add_fwver(verbose); 1445 bootargs_add_android(verbose); 1446 1447 /* 1448 * Initrd fixup: remove unused "initrd=0x...,0x...", 1449 * this for compatible with legacy parameter.txt 1450 */ 1451 env_delete("bootargs", "initrd=", 0); 1452 1453 /* 1454 * If uart is required to be disabled during 1455 * power on, it would be not initialized by 1456 * any pre-loader and U-Boot. 1457 * 1458 * If we don't remove earlycon from commandline, 1459 * kernel hangs while using earlycon to putc/getc 1460 * which may dead loop for waiting uart status. 1461 * (It seems the root cause is baundrate is not 1462 * initilalized) 1463 * 1464 * So let's remove earlycon from commandline. 1465 */ 1466 if (gd->flags & GD_FLG_DISABLE_CONSOLE) 1467 env_delete("bootargs", "earlycon=", 0); 1468 1469 bootargs = env_get("bootargs"); 1470 if (verbose) 1471 printf("## bootargs(merged): %s\n\n", bootargs); 1472 1473 return (char *)bootargs; 1474 } 1475 1476 int ft_verify_fdt(void *fdt) 1477 { 1478 /* for android header v4+, we load bootparams and fixup initrd */ 1479 #if defined(CONFIG_ANDROID_BOOT_IMAGE) && defined(CONFIG_XBC) 1480 struct andr_img_hdr *hdr; 1481 uint64_t initrd_start, initrd_end; 1482 char *bootargs, *p; 1483 int nodeoffset; 1484 int is_u64, err; 1485 u32 len; 1486 1487 hdr = (void *)env_get_ulong("android_addr_r", 16, 0); 1488 if (!hdr || android_image_check_header(hdr) || 1489 hdr->header_version < 4) 1490 return 1; 1491 1492 bootargs = env_get("andr_bootargs"); 1493 if (!bootargs) 1494 return 1; 1495 1496 /* trans character: space to new line */ 1497 p = bootargs; 1498 while (*p++) { 1499 if (*p == ' ') 1500 *p = '\n'; 1501 } 1502 1503 debug("## andr_bootargs: %s\n", bootargs); 1504 1505 /* 1506 * add boot params right after bootconfig 1507 * 1508 * because we can get final full bootargs in board_fdt_chosen_bootargs(), 1509 * android_image_get_ramdisk() is early than that. 1510 * 1511 * we have to add boot params by now. 1512 */ 1513 len = addBootConfigParameters((char *)bootargs, strlen(bootargs), 1514 (u64)hdr->ramdisk_addr + hdr->ramdisk_size + 1515 hdr->vendor_ramdisk_size, hdr->vendor_bootconfig_size); 1516 if (len < 0) { 1517 printf("error: addBootConfigParameters\n"); 1518 return 0; 1519 } 1520 1521 nodeoffset = fdt_subnode_offset(fdt, 0, "chosen"); 1522 if (nodeoffset < 0) { 1523 printf("error: No /chosen node\n"); 1524 return 0; 1525 } 1526 1527 /* fixup initrd with real value */ 1528 fdt_delprop(fdt, nodeoffset, "linux,initrd-start"); 1529 fdt_delprop(fdt, nodeoffset, "linux,initrd-end"); 1530 1531 is_u64 = (fdt_address_cells(fdt, 0) == 2); 1532 initrd_start = hdr->ramdisk_addr; 1533 initrd_end = initrd_start + hdr->ramdisk_size + 1534 hdr->vendor_ramdisk_size + 1535 hdr->vendor_bootconfig_size + len; 1536 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start", 1537 initrd_start, is_u64); 1538 if (err < 0) { 1539 printf("WARNING: could not set linux,initrd-start %s.\n", 1540 fdt_strerror(err)); 1541 return 0; 1542 } 1543 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end", 1544 initrd_end, is_u64); 1545 if (err < 0) { 1546 printf("WARNING: could not set linux,initrd-end %s.\n", 1547 fdt_strerror(err)); 1548 return 0; 1549 } 1550 #endif 1551 return 1; 1552 } 1553 1554