xref: /rk3399_rockchip-uboot/arch/arm/mach-rockchip/board.c (revision c5ff44bc8ebec81979fbcd4aacb56e0ca3b2fb53)
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", "kernel_addr_c", "ramdisk_addr_r",
320 	};
321 	const char *env_addr1[] = {
322 		"scriptaddr1", "pxefile_addr1_r",
323 		"fdt_addr1_r", "kernel_addr1_r", "kernel_addr1_c", "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 static int rockchip_dm_late_init(void *blob)
612 {
613 	struct udevice *dev;
614 
615 	/* Prepare for board_rng_seed(), dryrun and ignore result */
616 	if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && IS_ENABLED(CONFIG_DM_RNG))
617 		uclass_get_device(UCLASS_RNG, 0, &dev);
618 
619 	return 0;
620 }
621 
622 int board_fdt_fixup(void *blob)
623 {
624 #ifdef CONFIG_SANITY_CPU_SWAP
625 	sanity_cpu_swap(blob);
626 #endif
627 	/*
628 	 * Device's platdata points to orignal fdt blob property,
629 	 * access DM device before any fdt fixup.
630 	 *
631 	 * Do board specific init and common init.
632 	 */
633 	rk_board_dm_fdt_fixup(blob);
634 	rockchip_dm_late_init(blob);
635 
636 	/* Common fixup for DRM */
637 #ifdef CONFIG_DRM_ROCKCHIP
638 	rockchip_display_fixup(blob);
639 #endif
640 
641 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION
642 	vendor_storage_fixup(blob);
643 #endif
644 
645 	return rk_board_fdt_fixup(blob);
646 }
647 
648 #if defined(CONFIG_ARM64_BOOT_AARCH32) || !defined(CONFIG_ARM64)
649 /*
650  * Common for OP-TEE:
651  *	64-bit & 32-bit mode: share memory dcache is always enabled;
652  *
653  * Common for U-Boot:
654  *	64-bit mode: MMU table is static defined in rkxxx.c file, all memory
655  *		     regions are mapped. That's good to match OP-TEE MMU policy.
656  *
657  *	32-bit mode: MMU table is setup according to gd->bd->bi_dram[..] where
658  *		     the OP-TEE region has been reserved, so it can not be
659  *		     mapped(i.e. dcache is disabled). That's *NOT* good to match
660  *		     OP-TEE MMU policy.
661  *
662  * For the data coherence when communication between U-Boot and OP-TEE, U-Boot
663  * should follow OP-TEE MMU policy.
664  *
665  * So 32-bit mode U-Boot should map OP-TEE share memory as dcache enabled.
666  */
667 int board_initr_caches_fixup(void)
668 {
669 #ifdef CONFIG_OPTEE_CLIENT
670 	struct memblock mem;
671 
672 	mem.base = 0;
673 	mem.size = 0;
674 
675 	optee_get_shm_config(&mem.base, &mem.size);
676 	if (mem.size)
677 		mmu_set_region_dcache_behaviour(mem.base, mem.size,
678 						DCACHE_WRITEBACK);
679 #endif
680 	return 0;
681 }
682 #endif
683 
684 void arch_preboot_os(uint32_t bootm_state, bootm_headers_t *images)
685 {
686 	if (!(bootm_state & BOOTM_STATE_OS_PREP))
687 		return;
688 
689 #ifdef CONFIG_ARM64
690 	u8 *data = (void *)images->ep;
691 
692 	/*
693 	 * Fix kernel 5.10 arm64 boot warning:
694 	 * "[Firmware Bug]: Kernel image misaligned at boot, please fix your bootloader!"
695 	 *
696 	 * kernel: 5.10 commit 120dc60d0bdb ("arm64: get rid of TEXT_OFFSET")
697 	 * arm64 kernel version:
698 	 *	data[10] == 0x00 if kernel version >= 5.10: N*2MB align
699 	 *	data[10] == 0x08 if kernel version <  5.10: N*2MB + 0x80000(TEXT_OFFSET)
700 	 *
701 	 * Why fix here?
702 	 *   1. this is the common and final path for any boot command.
703 	 *   2. don't influence original boot flow, just fix it exactly before
704 	 *	jumping kernel.
705 	 *
706 	 * But relocation is in board_quiesce_devices() until all decompress
707 	 * done, mainly for saving boot time.
708 	 */
709 
710 	orig_images_ep = images->ep;
711 
712 	if (data[10] == 0x00) {
713 		if (round_down(images->ep, SZ_2M) != images->ep)
714 			images->ep = round_down(images->ep, SZ_2M);
715 	} else {
716 		if (IS_ALIGNED(images->ep, SZ_2M))
717 			images->ep += 0x80000;
718 	}
719 #endif
720 	hotkey_run(HK_CLI_OS_PRE);
721 }
722 
723 void enable_caches(void)
724 {
725 	icache_enable();
726 	dcache_enable();
727 }
728 
729 #ifdef CONFIG_LMB
730 /*
731  * Using last bi_dram[...] to initialize "bootm_low" and "bootm_mapsize".
732  * This makes lmb_alloc_base() always alloc from tail of sdram.
733  * If we don't assign it, bi_dram[0] is used by default and it may cause
734  * lmb_alloc_base() fail when bi_dram[0] range is small.
735  */
736 void board_lmb_reserve(struct lmb *lmb)
737 {
738 	char bootm_mapsize[32];
739 	char bootm_low[32];
740 	u64 start, size;
741 	int i;
742 
743 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
744 		if (!gd->bd->bi_dram[i].size)
745 			break;
746 	}
747 
748 	start = gd->bd->bi_dram[i - 1].start;
749 	size = gd->bd->bi_dram[i - 1].size;
750 
751 	/*
752 	 * 32-bit kernel: ramdisk/fdt shouldn't be loaded to highmem area(768MB+),
753 	 * otherwise "Unable to handle kernel paging request at virtual address ...".
754 	 *
755 	 * So that we hope limit highest address at 768M, but there comes the the
756 	 * problem: ramdisk is a compressed image and it expands after descompress,
757 	 * so it accesses 768MB+ and brings the above "Unable to handle kernel ...".
758 	 *
759 	 * We make a appointment that the highest memory address is 512MB, it
760 	 * makes lmb alloc safer.
761 	 */
762 #ifndef CONFIG_ARM64
763 	if (start >= ((u64)CONFIG_SYS_SDRAM_BASE + SZ_512M)) {
764 		start = gd->bd->bi_dram[i - 2].start;
765 		size = gd->bd->bi_dram[i - 2].size;
766 	}
767 
768 	if ((start + size) > ((u64)CONFIG_SYS_SDRAM_BASE + SZ_512M))
769 		size = (u64)CONFIG_SYS_SDRAM_BASE + SZ_512M - start;
770 #endif
771 	sprintf(bootm_low, "0x%llx", start);
772 	sprintf(bootm_mapsize, "0x%llx", size);
773 	env_set("bootm_low", bootm_low);
774 	env_set("bootm_mapsize", bootm_mapsize);
775 }
776 #endif
777 
778 #ifdef CONFIG_BIDRAM
779 int board_bidram_reserve(struct bidram *bidram)
780 {
781 	struct memblock mem;
782 	int ret;
783 
784 	/* ATF */
785 	mem = param_parse_atf_mem();
786 	ret = bidram_reserve(MEM_ATF, mem.base, mem.size);
787 	if (ret)
788 		return ret;
789 
790 	/* PSTORE/ATAGS/SHM */
791 	mem = param_parse_common_resv_mem();
792 	ret = bidram_reserve(MEM_SHM, mem.base, mem.size);
793 	if (ret)
794 		return ret;
795 
796 	/* OP-TEE */
797 	mem = param_parse_optee_mem();
798 	ret = bidram_reserve(MEM_OPTEE, mem.base, mem.size);
799 	if (ret)
800 		return ret;
801 
802 	return 0;
803 }
804 
805 #ifdef CONFIG_SYSMEM
806 int board_sysmem_reserve(struct sysmem *sysmem)
807 {
808 #ifdef CONFIG_SKIP_RELOCATE_UBOOT
809 	if (!sysmem_alloc_base_by_name("NO-RELOC-CODE",
810 	    CONFIG_SYS_TEXT_BASE, SZ_2M)) {
811 		printf("Failed to reserve sysmem for U-Boot code\n");
812 		return -ENOMEM;
813 	}
814 #endif
815 	return 0;
816 }
817 #endif
818 
819 parse_fn_t board_bidram_parse_fn(void)
820 {
821 	return param_parse_ddr_mem;
822 }
823 #endif
824 
825 int board_init_f_boot_flags(void)
826 {
827 	int boot_flags = 0;
828 
829 #if CONFIG_IS_ENABLED(FPGA_ROCKCHIP)
830 	arch_fpga_init();
831 #endif
832 #ifdef CONFIG_PSTORE
833 	param_parse_pstore();
834 #endif
835 	param_parse_pre_serial(&boot_flags);
836 
837 	/* The highest priority to turn off (override) console */
838 #if defined(CONFIG_DISABLE_CONSOLE)
839 	boot_flags |= GD_FLG_DISABLE_CONSOLE;
840 #endif
841 
842 	return boot_flags;
843 }
844 
845 #if defined(CONFIG_USB_GADGET)
846 #include <usb.h>
847 #if defined(CONFIG_USB_GADGET_DWC2_OTG)
848 #include <fdt_support.h>
849 #include <usb/dwc2_udc.h>
850 
851 static struct dwc2_plat_otg_data otg_data = {
852 	.rx_fifo_sz	= 512,
853 	.np_tx_fifo_sz	= 16,
854 	.tx_fifo_sz	= 128,
855 };
856 
857 int board_usb_init(int index, enum usb_init_type init)
858 {
859 	const void *blob = gd->fdt_blob;
860 	const fdt32_t *reg;
861 	fdt_addr_t addr;
862 	int node;
863 
864 	/* find the usb_otg node */
865 	node = fdt_node_offset_by_compatible(blob, -1, "snps,dwc2");
866 
867 retry:
868 	if (node > 0) {
869 		reg = fdt_getprop(blob, node, "reg", NULL);
870 		if (!reg)
871 			return -EINVAL;
872 
873 		addr = fdt_translate_address(blob, node, reg);
874 		if (addr == OF_BAD_ADDR) {
875 			pr_err("Not found usb_otg address\n");
876 			return -EINVAL;
877 		}
878 
879 #if defined(CONFIG_ROCKCHIP_RK3288)
880 		if (addr != 0xff580000) {
881 			node = fdt_node_offset_by_compatible(blob, node,
882 							     "snps,dwc2");
883 			goto retry;
884 		}
885 #endif
886 	} else {
887 		/*
888 		 * With kernel dtb support, rk3288 dwc2 otg node
889 		 * use the rockchip legacy dwc2 driver "dwc_otg_310"
890 		 * with the compatible "rockchip,rk3288_usb20_otg",
891 		 * and rk3368 also use the "dwc_otg_310" driver with
892 		 * the compatible "rockchip,rk3368-usb".
893 		 */
894 #if defined(CONFIG_ROCKCHIP_RK3288)
895 		node = fdt_node_offset_by_compatible(blob, -1,
896 				"rockchip,rk3288_usb20_otg");
897 #elif defined(CONFIG_ROCKCHIP_RK3368)
898 		node = fdt_node_offset_by_compatible(blob, -1,
899 				"rockchip,rk3368-usb");
900 #endif
901 		if (node > 0) {
902 			goto retry;
903 		} else {
904 			pr_err("Not found usb_otg device\n");
905 			return -ENODEV;
906 		}
907 	}
908 
909 	otg_data.regs_otg = (uintptr_t)addr;
910 
911 	return dwc2_udc_probe(&otg_data);
912 }
913 
914 int board_usb_cleanup(int index, enum usb_init_type init)
915 {
916 	return 0;
917 }
918 #elif defined(CONFIG_USB_DWC3_GADGET) /* CONFIG_USB_GADGET_DWC2_OTG */
919 #include <dwc3-uboot.h>
920 
921 int board_usb_cleanup(int index, enum usb_init_type init)
922 {
923 	dwc3_uboot_exit(index);
924 	return 0;
925 }
926 
927 #endif /* CONFIG_USB_DWC3_GADGET */
928 #endif /* CONFIG_USB_GADGET */
929 
930 static void bootm_no_reloc(void)
931 {
932 	char *ramdisk_high;
933 	char *fdt_high;
934 
935 	if (!env_get_yesno("bootm-no-reloc"))
936 		return;
937 
938 	ramdisk_high = env_get("initrd_high");
939 	fdt_high = env_get("fdt_high");
940 
941 	if (!fdt_high) {
942 		env_set_hex("fdt_high", -1UL);
943 		printf("Fdt ");
944 	}
945 
946 	if (!ramdisk_high) {
947 		env_set_hex("initrd_high", -1UL);
948 		printf("Ramdisk ");
949 	}
950 
951 	if (!fdt_high || !ramdisk_high)
952 		printf("skip relocation\n");
953 }
954 
955 int bootm_board_start(void)
956 {
957 	/*
958 	 * print console record data
959 	 *
960 	 * On some rockchip platforms, uart debug and sdmmc pin are multiplex.
961 	 * If boot from sdmmc mode, the console data would be record in buffer,
962 	 * we switch to uart debug function in order to print it after loading
963 	 * images.
964 	 */
965 #if 0
966 	if (!strcmp("mmc", env_get("devtype")) &&
967 	    !strcmp("1", env_get("devnum"))) {
968 		printf("IOMUX: sdmmc => uart debug");
969 		pinctrl_select_state(gd->cur_serial_dev, "default");
970 		console_record_print_purge();
971 	}
972 #endif
973 	/* disable bootm relcation to save boot time */
974 	bootm_no_reloc();
975 
976 	/* PCBA test needs more permission */
977 	if (get_bcb_recovery_msg() == BCB_MSG_RECOVERY_PCBA)
978 		env_update("bootargs", "androidboot.selinux=permissive");
979 
980 	/* sysmem */
981 	hotkey_run(HK_SYSMEM);
982 	sysmem_overflow_check();
983 
984 	return 0;
985 }
986 
987 int bootm_image_populate_dtb(void *img)
988 {
989 	if ((gd->flags & GD_FLG_KDTB_READY) && !gd->fdt_blob_kern)
990 		sysmem_free((phys_addr_t)gd->fdt_blob);
991 	else
992 		gd->fdt_blob = (void *)env_get_ulong("fdt_addr_r", 16, 0);
993 
994 	return rockchip_ram_read_dtb_file(img, (void *)gd->fdt_blob);
995 }
996 
997 /*
998  * Implement it to support CLI command:
999  *   - Android: bootm [aosp addr]
1000  *   - FIT:     bootm [fit addr]
1001  *   - uImage:  bootm [uimage addr]
1002  *
1003  * Purpose:
1004  *   - The original bootm command args require fdt addr on AOSP,
1005  *     which is not flexible on rockchip boot/recovery.img.
1006  *   - Take Android/FIT/uImage image into sysmem management to avoid image
1007  *     memory overlap.
1008  */
1009 #if defined(CONFIG_ANDROID_BOOTLOADER) ||	\
1010 	defined(CONFIG_ROCKCHIP_FIT_IMAGE) ||	\
1011 	defined(CONFIG_ROCKCHIP_UIMAGE)
1012 int board_do_bootm(int argc, char * const argv[])
1013 {
1014 	int format;
1015 	void *img;
1016 
1017 	/* only 'bootm' full image goes further */
1018 	if (argc != 2)
1019 		return 0;
1020 
1021 	img = (void *)simple_strtoul(argv[1], NULL, 16);
1022 	format = (genimg_get_format(img));
1023 
1024 	/* Android */
1025 #ifdef CONFIG_ANDROID_BOOT_IMAGE
1026 	if (format == IMAGE_FORMAT_ANDROID) {
1027 		struct andr_img_hdr *hdr;
1028 		ulong load_addr;
1029 		ulong size;
1030 		int ret;
1031 
1032 		hdr = (struct andr_img_hdr *)img;
1033 		printf("BOOTM: transferring to board Android\n");
1034 
1035 		load_addr = env_get_ulong("kernel_addr_r", 16, 0);
1036 		load_addr -= hdr->page_size;
1037 		size = android_image_get_end(hdr) - (ulong)hdr;
1038 
1039 		if (!sysmem_alloc_base(MEM_ANDROID, (ulong)hdr, size))
1040 			return -ENOMEM;
1041 
1042 		ret = bootm_image_populate_dtb(img);
1043 		if (ret) {
1044 			printf("bootm can't read dtb, ret=%d\n", ret);
1045 			return ret;
1046 		}
1047 
1048 		ret = android_image_memcpy_separate(hdr, &load_addr);
1049 		if (ret) {
1050 			printf("board do bootm failed, ret=%d\n", ret);
1051 			return ret;
1052 		}
1053 
1054 		return android_bootloader_boot_kernel(load_addr);
1055 	}
1056 #endif
1057 
1058 	/* FIT */
1059 #if IMAGE_ENABLE_FIT
1060 	if (format == IMAGE_FORMAT_FIT) {
1061 		char boot_cmd[64];
1062 		int ret;
1063 
1064 		printf("BOOTM: transferring to board FIT\n");
1065 
1066 		ret = bootm_image_populate_dtb(img);
1067 		if (ret) {
1068 			printf("bootm can't read dtb, ret=%d\n", ret);
1069 			return ret;
1070 		}
1071 		snprintf(boot_cmd, sizeof(boot_cmd), "boot_fit %s", argv[1]);
1072 		return run_command(boot_cmd, 0);
1073 	}
1074 #endif
1075 
1076 	/* uImage */
1077 #if 0
1078 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
1079 	if (format == IMAGE_FORMAT_LEGACY &&
1080 	    image_get_type(img) == IH_TYPE_MULTI) {
1081 		char boot_cmd[64];
1082 
1083 		printf("BOOTM: transferring to board uImage\n");
1084 		snprintf(boot_cmd, sizeof(boot_cmd), "boot_uimage %s", argv[1]);
1085 		return run_command(boot_cmd, 0);
1086 	}
1087 #endif
1088 #endif
1089 	return 0;
1090 }
1091 #endif
1092 
1093 void autoboot_command_fail_handle(void)
1094 {
1095 #ifdef CONFIG_ANDROID_AB
1096 	if (rk_avb_ab_have_bootable_slot() == true)
1097 		run_command("reset;", 0);
1098 	else
1099 		run_command("fastboot usb 0;", 0);
1100 #endif
1101 
1102 #ifdef CONFIG_AVB_VBMETA_PUBLIC_KEY_VALIDATE
1103 	run_command("download", 0);
1104 	run_command("fastboot usb 0;", 0);
1105 #endif
1106 
1107 }
1108 
1109 #ifdef CONFIG_FIT_ROLLBACK_PROTECT
1110 
1111 #define FIT_ROLLBACK_INDEX_LOCATION	0x66697472	/* "fitr" */
1112 
1113 int fit_read_otp_rollback_index(uint32_t fit_index, uint32_t *otp_index)
1114 {
1115 #ifdef CONFIG_OPTEE_CLIENT
1116 	u64 index;
1117 	int ret;
1118 
1119 	ret = trusty_read_rollback_index(FIT_ROLLBACK_INDEX_LOCATION, &index);
1120 	if (ret) {
1121 		if (ret != TEE_ERROR_ITEM_NOT_FOUND)
1122 			return ret;
1123 
1124 		index = 0;
1125 		printf("Initial otp index as %d\n", fit_index);
1126 	}
1127 
1128 	*otp_index = (uint32_t)index;
1129 #else
1130 	*otp_index = 0;
1131 #endif
1132 
1133 	return 0;
1134 }
1135 
1136 int fit_write_trusty_rollback_index(u32 trusty_index)
1137 {
1138 	if (!trusty_index)
1139 		return 0;
1140 #ifdef CONFIG_OPTEE_CLIENT
1141 	return trusty_write_rollback_index(FIT_ROLLBACK_INDEX_LOCATION,
1142 					   (u64)trusty_index);
1143 #else
1144 	return 0;
1145 #endif
1146 }
1147 #endif
1148 
1149 void board_quiesce_devices(void *images)
1150 {
1151 #ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS
1152 	/* Destroy atags makes next warm boot safer */
1153 	atags_destroy();
1154 #endif
1155 #ifdef CONFIG_FIT_ROLLBACK_PROTECT
1156 	int ret;
1157 
1158 	ret = fit_write_trusty_rollback_index(gd->rollback_index);
1159 	if (ret) {
1160 		panic("Failed to write fit rollback index %d, ret=%d",
1161 		      gd->rollback_index, ret);
1162 	}
1163 #endif
1164 #ifdef CONFIG_ROCKCHIP_HW_DECOMPRESS
1165 	misc_decompress_cleanup();
1166 #endif
1167 #ifdef CONFIG_ARM64
1168 	bootm_headers_t *bootm_images = (bootm_headers_t *)images;
1169 
1170 	/* relocate kernel after decompress cleanup */
1171 	if (orig_images_ep && orig_images_ep != bootm_images->ep) {
1172 		memmove((char *)bootm_images->ep, (const char *)orig_images_ep,
1173 			bootm_images->os.image_len);
1174 		printf("== DO RELOCATE == Kernel from 0x%08lx to 0x%08lx\n",
1175 		       orig_images_ep, bootm_images->ep);
1176 	}
1177 #endif
1178 
1179 	hotkey_run(HK_CMDLINE);
1180 	hotkey_run(HK_CLI_OS_GO);
1181 #ifdef CONFIG_ROCKCHIP_REBOOT_TEST
1182 	do_reset(NULL, 0, 0, NULL);
1183 #endif
1184 }
1185 
1186 /*
1187  * Use hardware rng to seed Linux random
1188  *
1189  * 'Android_14 + GKI' requires this information.
1190  */
1191 int board_rng_seed(struct abuf *buf)
1192 {
1193 #ifdef CONFIG_DM_RNG
1194 	struct udevice *dev;
1195 #endif
1196 	size_t len = 32;
1197 	u8 *data;
1198 	int i;
1199 
1200 	data = malloc(len);
1201 	if (!data) {
1202 	        printf("Out of memory\n");
1203 	        return -ENOMEM;
1204 	}
1205 
1206 #ifdef CONFIG_DM_RNG
1207 	if (uclass_get_device(UCLASS_RNG, 0, &dev) || dm_rng_read(dev, data, len))
1208 #endif
1209 	{
1210 		printf("board seed: Pseudo\n");
1211 		for (i = 0; i < len; i++)
1212 			data[i] = (u8)rand();
1213 	}
1214 
1215 	abuf_init_set(buf, data, len);
1216 
1217 	return 0;
1218 }
1219 
1220 /*
1221  * Pass fwver when any available.
1222  */
1223 static void bootargs_add_fwver(bool verbose)
1224 {
1225 #ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS
1226 	struct tag *t;
1227 	char *list1 = NULL;
1228 	char *list2 = NULL;
1229 	char *fwver = NULL;
1230 	char *p = PLAIN_VERSION;
1231 	int i, end;
1232 
1233 	t = atags_get_tag(ATAG_FWVER);
1234 	if (t) {
1235 		list1 = calloc(1, sizeof(struct tag_fwver));
1236 		if (!list1)
1237 			return;
1238 		for (i = 0; i < FW_MAX; i++) {
1239 			if (t->u.fwver.ver[i][0] != '\0') {
1240 				strcat(list1, t->u.fwver.ver[i]);
1241 				strcat(list1, ",");
1242 			}
1243 		}
1244 	}
1245 
1246 	list2 = calloc(1, FWVER_LEN);
1247 	if (!list2)
1248 		goto out;
1249 	strcat(list2, "uboot-");
1250 	/* optional */
1251 #ifdef BUILD_TAG
1252 	strcat(list2, BUILD_TAG);
1253 	strcat(list2, "-");
1254 #endif
1255 	/* optional */
1256 	if (strcmp(PLAIN_VERSION, "2017.09")) {
1257 		strncat(list2, p + strlen("2017.09-g"), 10);
1258 		strcat(list2, "-");
1259 	}
1260 	strcat(list2, U_BOOT_DMI_DATE);
1261 
1262 	/* merge ! */
1263 	if (list1 || list2) {
1264 		fwver = calloc(1, sizeof(struct tag_fwver));
1265 		if (!fwver)
1266 			goto out;
1267 
1268 		strcat(fwver, "androidboot.fwver=");
1269 		if (list1)
1270 			strcat(fwver, list1);
1271 		if (list2) {
1272 			strcat(fwver, list2);
1273 		} else {
1274 			end = strlen(fwver) - 1;
1275 			fwver[end] = '\0'; /* omit last ',' */
1276 		}
1277 		if (verbose)
1278 			printf("## fwver: %s\n\n", fwver);
1279 		env_update("bootargs", fwver);
1280 		env_set("fwver", fwver + strlen("androidboot."));
1281 	}
1282 out:
1283 	if (list1)
1284 		free(list1);
1285 	if (list2)
1286 		free(list2);
1287 	if (fwver)
1288 		free(fwver);
1289 #endif
1290 }
1291 
1292 static void bootargs_add_android(bool verbose)
1293 {
1294 #ifdef CONFIG_ANDROID_AB
1295 	ab_update_root_partition();
1296 #endif
1297 
1298 	/* Android header v4+ need this handle */
1299 #ifdef CONFIG_ANDROID_BOOT_IMAGE
1300 	struct andr_img_hdr *hdr;
1301 	char *fwver;
1302 
1303 	hdr = (void *)env_get_ulong("android_addr_r", 16, 0);
1304 	if (hdr && !android_image_check_header(hdr) && hdr->header_version >= 4) {
1305 		if (env_update_extract_subset("bootargs", "andr_bootargs", "androidboot."))
1306 			printf("extract androidboot.xxx error\n");
1307 		if (verbose)
1308 			printf("## bootargs(android): %s\n\n", env_get("andr_bootargs"));
1309 
1310 		/* for kernel cmdline can be read */
1311 		fwver = env_get("fwver");
1312 		if (fwver) {
1313 			env_update("bootargs", fwver);
1314 			env_set("fwver", NULL);
1315 		}
1316 	}
1317 #endif
1318 }
1319 
1320 static void bootargs_add_partition(bool verbose)
1321 {
1322 #if defined(CONFIG_ENVF) || defined(CONFIG_ENV_PARTITION)
1323 	char *part_type[] = { "mtdparts", "blkdevparts" };
1324 	char *part_list;
1325 	char *env;
1326 	int id = 0;
1327 
1328 	env = env_get(part_type[id]);
1329 	if (!env)
1330 		env = env_get(part_type[++id]);
1331 	if (env) {
1332 		if (!strstr(env, part_type[id])) {
1333 			part_list = calloc(1, strlen(env) + strlen(part_type[id]) + 2);
1334 			if (part_list) {
1335 				strcat(part_list, part_type[id]);
1336 				strcat(part_list, "=");
1337 				strcat(part_list, env);
1338 			}
1339 		} else {
1340 			part_list = env;
1341 		}
1342 		env_update("bootargs", part_list);
1343 		if (verbose)
1344 			printf("## parts: %s\n\n", part_list);
1345 	}
1346 
1347 	env = env_get("sys_bootargs");
1348 	if (env) {
1349 		env_update("bootargs", env);
1350 		if (verbose)
1351 			printf("## sys_bootargs: %s\n\n", env);
1352 	}
1353 #endif
1354 
1355 #ifdef CONFIG_MTD_BLK
1356 	if (!env_get("mtdparts")) {
1357 		char *mtd_par_info = mtd_part_parse(NULL);
1358 
1359 		if (mtd_par_info) {
1360 			if (memcmp(env_get("devtype"), "mtd", 3) == 0)
1361 				env_update("bootargs", mtd_par_info);
1362 		}
1363 	}
1364 #endif
1365 }
1366 
1367 static void bootargs_add_dtb_dtbo(void *fdt, bool verbose)
1368 {
1369 	/* bootargs_ext is used when dtbo is applied. */
1370 	const char *arr_bootargs[] = { "bootargs", "bootargs_ext" };
1371 	const char *bootargs;
1372 	char *msg = "kernel";
1373 	int i, noffset;
1374 
1375 	/* find or create "/chosen" node. */
1376 	noffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
1377 	if (noffset < 0)
1378 		return;
1379 
1380 	for (i = 0; i < ARRAY_SIZE(arr_bootargs); i++) {
1381 		bootargs = fdt_getprop(fdt, noffset, arr_bootargs[i], NULL);
1382 		if (!bootargs)
1383 			continue;
1384 		if (verbose)
1385 			printf("## bootargs(%s-%s): %s\n\n",
1386 			       msg, arr_bootargs[i], bootargs);
1387 		/*
1388 		 * Append kernel bootargs
1389 		 * If use AB system, delete default "root=" which route
1390 		 * to rootfs. Then the ab bootctl will choose the
1391 		 * high priority system to boot and add its UUID
1392 		 * to cmdline. The format is "roo=PARTUUID=xxxx...".
1393 		 */
1394 #ifdef CONFIG_ANDROID_AB
1395 		env_update_filter("bootargs", bootargs, "root=");
1396 #else
1397 		env_update("bootargs", bootargs);
1398 #endif
1399 	}
1400 }
1401 
1402 char *board_fdt_chosen_bootargs(void *fdt)
1403 {
1404 	int verbose = is_hotkey(HK_CMDLINE);
1405 	const char *bootargs;
1406 
1407 	/* debug */
1408 	hotkey_run(HK_INITCALL);
1409 	if (verbose)
1410 		printf("## bootargs(u-boot): %s\n\n", env_get("bootargs"));
1411 
1412 	bootargs_add_dtb_dtbo(fdt, verbose);
1413 	bootargs_add_partition(verbose);
1414 	bootargs_add_fwver(verbose);
1415 	bootargs_add_android(verbose);
1416 
1417 	/*
1418 	 * Initrd fixup: remove unused "initrd=0x...,0x...",
1419 	 * this for compatible with legacy parameter.txt
1420 	 */
1421 	env_delete("bootargs", "initrd=", 0);
1422 
1423 	/*
1424 	 * If uart is required to be disabled during
1425 	 * power on, it would be not initialized by
1426 	 * any pre-loader and U-Boot.
1427 	 *
1428 	 * If we don't remove earlycon from commandline,
1429 	 * kernel hangs while using earlycon to putc/getc
1430 	 * which may dead loop for waiting uart status.
1431 	 * (It seems the root cause is baundrate is not
1432 	 * initilalized)
1433 	 *
1434 	 * So let's remove earlycon from commandline.
1435 	 */
1436 	if (gd->flags & GD_FLG_DISABLE_CONSOLE)
1437 		env_delete("bootargs", "earlycon=", 0);
1438 
1439 	bootargs = env_get("bootargs");
1440 	if (verbose)
1441 		printf("## bootargs(merged): %s\n\n", bootargs);
1442 
1443 	return (char *)bootargs;
1444 }
1445 
1446 int ft_verify_fdt(void *fdt)
1447 {
1448 	/* for android header v4+, we load bootparams and fixup initrd */
1449 #if defined(CONFIG_ANDROID_BOOT_IMAGE) && defined(CONFIG_XBC)
1450 	struct andr_img_hdr *hdr;
1451 	uint64_t initrd_start, initrd_end;
1452 	char *bootargs, *p;
1453 	int nodeoffset;
1454 	int is_u64, err;
1455 	u32 len;
1456 
1457 	hdr = (void *)env_get_ulong("android_addr_r", 16, 0);
1458 	if (!hdr || android_image_check_header(hdr) ||
1459 	    hdr->header_version < 4)
1460 		return 1;
1461 
1462 	bootargs = env_get("andr_bootargs");
1463 	if (!bootargs)
1464 		return 1;
1465 
1466 	/* trans character: space to new line */
1467 	p = bootargs;
1468 	while (*p++) {
1469 		if (*p == ' ')
1470 			*p = '\n';
1471 	}
1472 
1473 	debug("## andr_bootargs: %s\n", bootargs);
1474 
1475 	/*
1476 	 * add boot params right after bootconfig
1477 	 *
1478 	 * because we can get final full bootargs in board_fdt_chosen_bootargs(),
1479 	 * android_image_get_ramdisk() is early than that.
1480 	 *
1481 	 * we have to add boot params by now.
1482 	 */
1483 	len = addBootConfigParameters((char *)bootargs, strlen(bootargs),
1484 		(u64)hdr->ramdisk_addr + hdr->ramdisk_size +
1485 		hdr->vendor_ramdisk_size, hdr->vendor_bootconfig_size);
1486 	if (len < 0) {
1487 		printf("error: addBootConfigParameters\n");
1488 		return 0;
1489 	}
1490 
1491 	nodeoffset = fdt_subnode_offset(fdt, 0, "chosen");
1492 	if (nodeoffset < 0) {
1493 		printf("error: No /chosen node\n");
1494 		return 0;
1495 	}
1496 
1497 	/* fixup initrd with real value */
1498 	fdt_delprop(fdt, nodeoffset, "linux,initrd-start");
1499 	fdt_delprop(fdt, nodeoffset, "linux,initrd-end");
1500 
1501 	is_u64 = (fdt_address_cells(fdt, 0) == 2);
1502 	initrd_start = hdr->ramdisk_addr;
1503 	initrd_end = initrd_start + hdr->ramdisk_size +
1504 			hdr->vendor_ramdisk_size +
1505 			hdr->vendor_bootconfig_size + len;
1506 	err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
1507 			      initrd_start, is_u64);
1508 	if (err < 0) {
1509 		printf("WARNING: could not set linux,initrd-start %s.\n",
1510 		       fdt_strerror(err));
1511 		return 0;
1512 	}
1513 	err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
1514 			      initrd_end, is_u64);
1515 	if (err < 0) {
1516 		printf("WARNING: could not set linux,initrd-end %s.\n",
1517 		       fdt_strerror(err));
1518 		return 0;
1519 	}
1520 #endif
1521 	return 1;
1522 }
1523 
1524