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