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