xref: /OK3568_Linux_fs/u-boot/cmd/bootrkp.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2017 Rockchip Electronics Co., Ltd
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * SPDX-License-Identifier:     GPL-2.0+
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <common.h>
8*4882a593Smuzhiyun #include <attestation_key.h>
9*4882a593Smuzhiyun #include <bootm.h>
10*4882a593Smuzhiyun #include <boot_rkimg.h>
11*4882a593Smuzhiyun #include <keymaster.h>
12*4882a593Smuzhiyun #include <console.h>
13*4882a593Smuzhiyun #include <image.h>
14*4882a593Smuzhiyun #include <malloc.h>
15*4882a593Smuzhiyun #include <sysmem.h>
16*4882a593Smuzhiyun #include <linux/libfdt.h>
17*4882a593Smuzhiyun #include <asm/arch/hotkey.h>
18*4882a593Smuzhiyun #include <asm/arch/resource_img.h>
19*4882a593Smuzhiyun #include <asm/arch/boot_mode.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #ifdef CONFIG_ROCKCHIP_CRC
24*4882a593Smuzhiyun #define tole(x) cpu_to_le32(x)
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /* Table of CRC-32's of all single-byte values (made by make_crc_table) */
27*4882a593Smuzhiyun static const uint32_t crc_table[256] = {
28*4882a593Smuzhiyun 	tole(0x00000000L), tole(0x04c10db7L), tole(0x09821b6eL), tole(0x0d4316d9L),
29*4882a593Smuzhiyun 	tole(0x130436dcL), tole(0x17c53b6bL), tole(0x1a862db2L), tole(0x1e472005L),
30*4882a593Smuzhiyun 	tole(0x26086db8L), tole(0x22c9600fL), tole(0x2f8a76d6L), tole(0x2b4b7b61L),
31*4882a593Smuzhiyun 	tole(0x350c5b64L), tole(0x31cd56d3L), tole(0x3c8e400aL), tole(0x384f4dbdL),
32*4882a593Smuzhiyun 	tole(0x4c10db70L), tole(0x48d1d6c7L), tole(0x4592c01eL), tole(0x4153cda9L),
33*4882a593Smuzhiyun 	tole(0x5f14edacL), tole(0x5bd5e01bL), tole(0x5696f6c2L), tole(0x5257fb75L),
34*4882a593Smuzhiyun 	tole(0x6a18b6c8L), tole(0x6ed9bb7fL), tole(0x639aada6L), tole(0x675ba011L),
35*4882a593Smuzhiyun 	tole(0x791c8014L), tole(0x7ddd8da3L), tole(0x709e9b7aL), tole(0x745f96cdL),
36*4882a593Smuzhiyun 	tole(0x9821b6e0L), tole(0x9ce0bb57L), tole(0x91a3ad8eL), tole(0x9562a039L),
37*4882a593Smuzhiyun 	tole(0x8b25803cL), tole(0x8fe48d8bL), tole(0x82a79b52L), tole(0x866696e5L),
38*4882a593Smuzhiyun 	tole(0xbe29db58L), tole(0xbae8d6efL), tole(0xb7abc036L), tole(0xb36acd81L),
39*4882a593Smuzhiyun 	tole(0xad2ded84L), tole(0xa9ece033L), tole(0xa4aff6eaL), tole(0xa06efb5dL),
40*4882a593Smuzhiyun 	tole(0xd4316d90L), tole(0xd0f06027L), tole(0xddb376feL), tole(0xd9727b49L),
41*4882a593Smuzhiyun 	tole(0xc7355b4cL), tole(0xc3f456fbL), tole(0xceb74022L), tole(0xca764d95L),
42*4882a593Smuzhiyun 	tole(0xf2390028L), tole(0xf6f80d9fL), tole(0xfbbb1b46L), tole(0xff7a16f1L),
43*4882a593Smuzhiyun 	tole(0xe13d36f4L), tole(0xe5fc3b43L), tole(0xe8bf2d9aL), tole(0xec7e202dL),
44*4882a593Smuzhiyun 	tole(0x34826077L), tole(0x30436dc0L), tole(0x3d007b19L), tole(0x39c176aeL),
45*4882a593Smuzhiyun 	tole(0x278656abL), tole(0x23475b1cL), tole(0x2e044dc5L), tole(0x2ac54072L),
46*4882a593Smuzhiyun 	tole(0x128a0dcfL), tole(0x164b0078L), tole(0x1b0816a1L), tole(0x1fc91b16L),
47*4882a593Smuzhiyun 	tole(0x018e3b13L), tole(0x054f36a4L), tole(0x080c207dL), tole(0x0ccd2dcaL),
48*4882a593Smuzhiyun 	tole(0x7892bb07L), tole(0x7c53b6b0L), tole(0x7110a069L), tole(0x75d1addeL),
49*4882a593Smuzhiyun 	tole(0x6b968ddbL), tole(0x6f57806cL), tole(0x621496b5L), tole(0x66d59b02L),
50*4882a593Smuzhiyun 	tole(0x5e9ad6bfL), tole(0x5a5bdb08L), tole(0x5718cdd1L), tole(0x53d9c066L),
51*4882a593Smuzhiyun 	tole(0x4d9ee063L), tole(0x495fedd4L), tole(0x441cfb0dL), tole(0x40ddf6baL),
52*4882a593Smuzhiyun 	tole(0xaca3d697L), tole(0xa862db20L), tole(0xa521cdf9L), tole(0xa1e0c04eL),
53*4882a593Smuzhiyun 	tole(0xbfa7e04bL), tole(0xbb66edfcL), tole(0xb625fb25L), tole(0xb2e4f692L),
54*4882a593Smuzhiyun 	tole(0x8aabbb2fL), tole(0x8e6ab698L), tole(0x8329a041L), tole(0x87e8adf6L),
55*4882a593Smuzhiyun 	tole(0x99af8df3L), tole(0x9d6e8044L), tole(0x902d969dL), tole(0x94ec9b2aL),
56*4882a593Smuzhiyun 	tole(0xe0b30de7L), tole(0xe4720050L), tole(0xe9311689L), tole(0xedf01b3eL),
57*4882a593Smuzhiyun 	tole(0xf3b73b3bL), tole(0xf776368cL), tole(0xfa352055L), tole(0xfef42de2L),
58*4882a593Smuzhiyun 	tole(0xc6bb605fL), tole(0xc27a6de8L), tole(0xcf397b31L), tole(0xcbf87686L),
59*4882a593Smuzhiyun 	tole(0xd5bf5683L), tole(0xd17e5b34L), tole(0xdc3d4dedL), tole(0xd8fc405aL),
60*4882a593Smuzhiyun 	tole(0x6904c0eeL), tole(0x6dc5cd59L), tole(0x6086db80L), tole(0x6447d637L),
61*4882a593Smuzhiyun 	tole(0x7a00f632L), tole(0x7ec1fb85L), tole(0x7382ed5cL), tole(0x7743e0ebL),
62*4882a593Smuzhiyun 	tole(0x4f0cad56L), tole(0x4bcda0e1L), tole(0x468eb638L), tole(0x424fbb8fL),
63*4882a593Smuzhiyun 	tole(0x5c089b8aL), tole(0x58c9963dL), tole(0x558a80e4L), tole(0x514b8d53L),
64*4882a593Smuzhiyun 	tole(0x25141b9eL), tole(0x21d51629L), tole(0x2c9600f0L), tole(0x28570d47L),
65*4882a593Smuzhiyun 	tole(0x36102d42L), tole(0x32d120f5L), tole(0x3f92362cL), tole(0x3b533b9bL),
66*4882a593Smuzhiyun 	tole(0x031c7626L), tole(0x07dd7b91L), tole(0x0a9e6d48L), tole(0x0e5f60ffL),
67*4882a593Smuzhiyun 	tole(0x101840faL), tole(0x14d94d4dL), tole(0x199a5b94L), tole(0x1d5b5623L),
68*4882a593Smuzhiyun 	tole(0xf125760eL), tole(0xf5e47bb9L), tole(0xf8a76d60L), tole(0xfc6660d7L),
69*4882a593Smuzhiyun 	tole(0xe22140d2L), tole(0xe6e04d65L), tole(0xeba35bbcL), tole(0xef62560bL),
70*4882a593Smuzhiyun 	tole(0xd72d1bb6L), tole(0xd3ec1601L), tole(0xdeaf00d8L), tole(0xda6e0d6fL),
71*4882a593Smuzhiyun 	tole(0xc4292d6aL), tole(0xc0e820ddL), tole(0xcdab3604L), tole(0xc96a3bb3L),
72*4882a593Smuzhiyun 	tole(0xbd35ad7eL), tole(0xb9f4a0c9L), tole(0xb4b7b610L), tole(0xb076bba7L),
73*4882a593Smuzhiyun 	tole(0xae319ba2L), tole(0xaaf09615L), tole(0xa7b380ccL), tole(0xa3728d7bL),
74*4882a593Smuzhiyun 	tole(0x9b3dc0c6L), tole(0x9ffccd71L), tole(0x92bfdba8L), tole(0x967ed61fL),
75*4882a593Smuzhiyun 	tole(0x8839f61aL), tole(0x8cf8fbadL), tole(0x81bbed74L), tole(0x857ae0c3L),
76*4882a593Smuzhiyun 	tole(0x5d86a099L), tole(0x5947ad2eL), tole(0x5404bbf7L), tole(0x50c5b640L),
77*4882a593Smuzhiyun 	tole(0x4e829645L), tole(0x4a439bf2L), tole(0x47008d2bL), tole(0x43c1809cL),
78*4882a593Smuzhiyun 	tole(0x7b8ecd21L), tole(0x7f4fc096L), tole(0x720cd64fL), tole(0x76cddbf8L),
79*4882a593Smuzhiyun 	tole(0x688afbfdL), tole(0x6c4bf64aL), tole(0x6108e093L), tole(0x65c9ed24L),
80*4882a593Smuzhiyun 	tole(0x11967be9L), tole(0x1557765eL), tole(0x18146087L), tole(0x1cd56d30L),
81*4882a593Smuzhiyun 	tole(0x02924d35L), tole(0x06534082L), tole(0x0b10565bL), tole(0x0fd15becL),
82*4882a593Smuzhiyun 	tole(0x379e1651L), tole(0x335f1be6L), tole(0x3e1c0d3fL), tole(0x3add0088L),
83*4882a593Smuzhiyun 	tole(0x249a208dL), tole(0x205b2d3aL), tole(0x2d183be3L), tole(0x29d93654L),
84*4882a593Smuzhiyun 	tole(0xc5a71679L), tole(0xc1661bceL), tole(0xcc250d17L), tole(0xc8e400a0L),
85*4882a593Smuzhiyun 	tole(0xd6a320a5L), tole(0xd2622d12L), tole(0xdf213bcbL), tole(0xdbe0367cL),
86*4882a593Smuzhiyun 	tole(0xe3af7bc1L), tole(0xe76e7676L), tole(0xea2d60afL), tole(0xeeec6d18L),
87*4882a593Smuzhiyun 	tole(0xf0ab4d1dL), tole(0xf46a40aaL), tole(0xf9295673L), tole(0xfde85bc4L),
88*4882a593Smuzhiyun 	tole(0x89b7cd09L), tole(0x8d76c0beL), tole(0x8035d667L), tole(0x84f4dbd0L),
89*4882a593Smuzhiyun 	tole(0x9ab3fbd5L), tole(0x9e72f662L), tole(0x9331e0bbL), tole(0x97f0ed0cL),
90*4882a593Smuzhiyun 	tole(0xafbfa0b1L), tole(0xab7ead06L), tole(0xa63dbbdfL), tole(0xa2fcb668L),
91*4882a593Smuzhiyun 	tole(0xbcbb966dL), tole(0xb87a9bdaL), tole(0xb5398d03L), tole(0xb1f880b4L)
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun #define DO_CRC(x) crc = tab[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
95*4882a593Smuzhiyun 
crc32_rk(uint32_t crc,const unsigned char * s,uint32_t len)96*4882a593Smuzhiyun static uint32_t crc32_rk(uint32_t crc, const unsigned char *s, uint32_t len)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	const uint32_t *tab;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	tab = crc_table;
101*4882a593Smuzhiyun 	crc = cpu_to_le32(crc);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	do {
104*4882a593Smuzhiyun 		DO_CRC(*s++);
105*4882a593Smuzhiyun 	} while (--len);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	return le32_to_cpu(crc);
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun #undef DO_CRC
111*4882a593Smuzhiyun 
crc32_verify(unsigned char * data,u32 size)112*4882a593Smuzhiyun static u32 crc32_verify(unsigned char *data, u32 size)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	u32 crc_check = 0, crc_calc = 0;
115*4882a593Smuzhiyun 	int i = 0;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	if (size <= 4)
118*4882a593Smuzhiyun 		return 0;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	size -= 4;
121*4882a593Smuzhiyun 	for (i = 3; i >= 0; i--)
122*4882a593Smuzhiyun 		crc_check = (crc_check << 8) + (*(data + size + i));
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	crc_calc = crc32_rk(0, data, size);
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	return (crc_calc == crc_check) ? crc_check : 0;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun #endif
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun #if !defined(CONFIG_ARM64)
131*4882a593Smuzhiyun #ifdef CONFIG_LMB
boot_start_lmb(bootm_headers_t * images)132*4882a593Smuzhiyun static void boot_start_lmb(bootm_headers_t *images)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	lmb_init(&images->lmb);
135*4882a593Smuzhiyun #ifdef CONFIG_NR_DRAM_BANKS
136*4882a593Smuzhiyun 	int i;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
139*4882a593Smuzhiyun 		lmb_add(&images->lmb, gd->bd->bi_dram[i].start,
140*4882a593Smuzhiyun 			gd->bd->bi_dram[i].size);
141*4882a593Smuzhiyun 	}
142*4882a593Smuzhiyun #else
143*4882a593Smuzhiyun 	ulong		mem_start;
144*4882a593Smuzhiyun 	phys_size_t	mem_size;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	mem_start = env_get_bootm_low();
147*4882a593Smuzhiyun 	mem_size = env_get_bootm_size();
148*4882a593Smuzhiyun 	lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
149*4882a593Smuzhiyun #endif
150*4882a593Smuzhiyun 	arch_lmb_reserve(&images->lmb);
151*4882a593Smuzhiyun 	board_lmb_reserve(&images->lmb);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun #else
boot_start_lmb(bootm_headers_t * images)154*4882a593Smuzhiyun static inline void boot_start_lmb(bootm_headers_t *images) { }
155*4882a593Smuzhiyun #endif
156*4882a593Smuzhiyun 
boot_lmb_init(bootm_headers_t * images)157*4882a593Smuzhiyun static void boot_lmb_init(bootm_headers_t *images)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun 	boot_start_lmb(images);
160*4882a593Smuzhiyun 	images->state = BOOTM_STATE_OS_GO;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun #endif
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /*
165*4882a593Smuzhiyun  * non-OTA packaged kernel.img & boot.img return the image size on success,
166*4882a593Smuzhiyun  * and a negative value on error.
167*4882a593Smuzhiyun  */
read_rockchip_image(struct blk_desc * dev_desc,disk_partition_t * part,void * dst)168*4882a593Smuzhiyun static int read_rockchip_image(struct blk_desc *dev_desc,
169*4882a593Smuzhiyun 			       disk_partition_t *part, void *dst)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	struct rockchip_image *img = dst;
172*4882a593Smuzhiyun 	int header_len = 8;
173*4882a593Smuzhiyun 	int cnt, ret;
174*4882a593Smuzhiyun #ifdef CONFIG_ROCKCHIP_CRC
175*4882a593Smuzhiyun 	u32 crc32;
176*4882a593Smuzhiyun #endif
177*4882a593Smuzhiyun 	ret = blk_dread(dev_desc, part->start, 1, img);
178*4882a593Smuzhiyun 	if (ret != 1)
179*4882a593Smuzhiyun 		return -EIO;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	if (img->tag != TAG_KERNEL) {
182*4882a593Smuzhiyun 		printf("Invalid %s image tag(0x%x)\n", part->name, img->tag);
183*4882a593Smuzhiyun 		return -EINVAL;
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	/* total size = image size + 8 bytes header + 4 bytes crc32 */
187*4882a593Smuzhiyun 	cnt = DIV_ROUND_UP(img->size + 8 + 4, RK_BLK_SIZE);
188*4882a593Smuzhiyun 	if (!sysmem_alloc_base_by_name((const char *)part->name,
189*4882a593Smuzhiyun 				       (phys_addr_t)dst,
190*4882a593Smuzhiyun 				       cnt * dev_desc->blksz))
191*4882a593Smuzhiyun 		return -ENXIO;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	ret = blk_dread(dev_desc, part->start, cnt, dst);
194*4882a593Smuzhiyun 	if (ret != cnt) {
195*4882a593Smuzhiyun 		printf("Failed to read %s part, blkcnt=%d\n", part->name, ret);
196*4882a593Smuzhiyun 		return -EIO;
197*4882a593Smuzhiyun 	} else {
198*4882a593Smuzhiyun 		ret = img->size;
199*4882a593Smuzhiyun 	}
200*4882a593Smuzhiyun 	memcpy(dst, dst + header_len, img->size);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun #ifdef CONFIG_ROCKCHIP_CRC
203*4882a593Smuzhiyun 	printf("%s image rk crc32 verify... ", part->name);
204*4882a593Smuzhiyun 	crc32 = crc32_verify((uchar *)(ulong)dst, img->size + 4);
205*4882a593Smuzhiyun 	if (!crc32) {
206*4882a593Smuzhiyun 		printf("fail!\n");
207*4882a593Smuzhiyun 		ret = -EINVAL;
208*4882a593Smuzhiyun 	} else {
209*4882a593Smuzhiyun 		printf("okay.\n");
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun #endif
212*4882a593Smuzhiyun 	return ret;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
boot_rockchip_image(struct blk_desc * dev_desc,disk_partition_t * boot_part)215*4882a593Smuzhiyun static int boot_rockchip_image(struct blk_desc *dev_desc,
216*4882a593Smuzhiyun 			       disk_partition_t *boot_part)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	disk_partition_t kernel_part;
219*4882a593Smuzhiyun 	ulong ramdisk_addr_r;
220*4882a593Smuzhiyun 	ulong kernel_addr_r;
221*4882a593Smuzhiyun 	ulong fdt_addr_r;
222*4882a593Smuzhiyun 	int ramdisk_size;
223*4882a593Smuzhiyun 	int kernel_size;
224*4882a593Smuzhiyun 	int fdt_size;
225*4882a593Smuzhiyun 	int ret;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	printf("\n## Booting Rockchip Format Image\n");
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0);
230*4882a593Smuzhiyun 	kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0);
231*4882a593Smuzhiyun 	fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0);
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	ret = part_get_info_by_name(dev_desc, PART_KERNEL, &kernel_part);
234*4882a593Smuzhiyun 	if (ret < 0) {
235*4882a593Smuzhiyun 		printf("Could not find kernel partition, ret=%d\n", ret);
236*4882a593Smuzhiyun 		return -EINVAL;
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	kernel_size = read_rockchip_image(dev_desc, &kernel_part,
240*4882a593Smuzhiyun 					  (void *)kernel_addr_r);
241*4882a593Smuzhiyun 	if (kernel_size < 0) {
242*4882a593Smuzhiyun 		printf("Failed to read kernel image, ret=%d\n", ret);
243*4882a593Smuzhiyun 		return -EINVAL;
244*4882a593Smuzhiyun 	}
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	ramdisk_size = read_rockchip_image(dev_desc, boot_part,
247*4882a593Smuzhiyun 					   (void *)ramdisk_addr_r);
248*4882a593Smuzhiyun 	if (ramdisk_size < 0)
249*4882a593Smuzhiyun 		ramdisk_size = 0;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	if (gd->fdt_blob != (void *)fdt_addr_r) {
252*4882a593Smuzhiyun 		fdt_size = rockchip_read_dtb_file((void *)fdt_addr_r);
253*4882a593Smuzhiyun 		if (fdt_size < 0) {
254*4882a593Smuzhiyun 			printf("Failed to read fdt, ret=%d\n", fdt_size);
255*4882a593Smuzhiyun 			return -EINVAL;
256*4882a593Smuzhiyun 		}
257*4882a593Smuzhiyun 	}
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	env_set("bootm-no-reloc", "y");
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	printf("fdt	 @ 0x%08lx (0x%08x)\n", fdt_addr_r, fdt_totalsize(fdt_addr_r));
262*4882a593Smuzhiyun 	printf("kernel   @ 0x%08lx (0x%08x)\n", kernel_addr_r, kernel_size);
263*4882a593Smuzhiyun 	printf("ramdisk  @ 0x%08lx (0x%08x)\n", ramdisk_addr_r, ramdisk_size);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun #if defined(CONFIG_ARM64)
266*4882a593Smuzhiyun 	char cmdbuf[64];
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	snprintf(cmdbuf, 64, "booti 0x%lx 0x%lx:0x%x 0x%lx",
269*4882a593Smuzhiyun 		 kernel_addr_r, ramdisk_addr_r, ramdisk_size, fdt_addr_r);
270*4882a593Smuzhiyun 	run_command(cmdbuf, 0);
271*4882a593Smuzhiyun #else
272*4882a593Smuzhiyun 	/* We asume it's always zImage on 32-bit platform */
273*4882a593Smuzhiyun 	ulong kaddr_c = env_get_ulong("kernel_addr_c", 16, 0);
274*4882a593Smuzhiyun 	ulong kaddr_r, kaddr, ksize;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	if (kernel_addr_r && !kaddr_c) {
277*4882a593Smuzhiyun 		kaddr_c = kernel_addr_r;
278*4882a593Smuzhiyun 		kaddr_r = CONFIG_SYS_SDRAM_BASE;
279*4882a593Smuzhiyun 	}
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	if (!sysmem_free((phys_addr_t)kaddr_c)) {
282*4882a593Smuzhiyun 		kaddr = kaddr_r;
283*4882a593Smuzhiyun 		ksize = kernel_size * 100 / 45 ; /* Ratio: 45% */
284*4882a593Smuzhiyun 		ksize = ALIGN(ksize, dev_desc->blksz);
285*4882a593Smuzhiyun 		if (!sysmem_alloc_base(MEM_UNCOMP_KERNEL,
286*4882a593Smuzhiyun 				       (phys_addr_t)kaddr, ksize))
287*4882a593Smuzhiyun 			return -ENOMEM;
288*4882a593Smuzhiyun 	}
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	boot_lmb_init(&images);
291*4882a593Smuzhiyun 	images.ep = kernel_addr_r;
292*4882a593Smuzhiyun 	images.initrd_start = ramdisk_addr_r;
293*4882a593Smuzhiyun 	images.initrd_end = ramdisk_addr_r + ramdisk_size;
294*4882a593Smuzhiyun 	images.ft_addr = (void *)fdt_addr_r;
295*4882a593Smuzhiyun 	images.ft_len = fdt_totalsize(fdt_addr_r);
296*4882a593Smuzhiyun 	do_bootm_linux(0, 0, NULL, &images);
297*4882a593Smuzhiyun #endif
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	return 0;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun 
do_boot_rockchip(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])302*4882a593Smuzhiyun static int do_boot_rockchip(cmd_tbl_t *cmdtp, int flag,
303*4882a593Smuzhiyun 			    int argc, char *const argv[])
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	char *part_name = PART_BOOT;
306*4882a593Smuzhiyun 	struct blk_desc *dev_desc;
307*4882a593Smuzhiyun 	disk_partition_t part;
308*4882a593Smuzhiyun 	int ret;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	dev_desc = rockchip_get_bootdev();
311*4882a593Smuzhiyun 	if (!dev_desc) {
312*4882a593Smuzhiyun 		printf("dev_desc is NULL!\n");
313*4882a593Smuzhiyun 		return CMD_RET_FAILURE;
314*4882a593Smuzhiyun 	}
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun #ifdef CONFIG_ANDROID_KEYMASTER_CA
317*4882a593Smuzhiyun 	/* load attestation key from misc partition. */
318*4882a593Smuzhiyun 	ret = part_get_info_by_name(dev_desc, PART_MISC, &part);
319*4882a593Smuzhiyun 	if (ret < 0)
320*4882a593Smuzhiyun 		printf("Could not find misc partition\n");
321*4882a593Smuzhiyun 	else
322*4882a593Smuzhiyun 		load_attestation_key(dev_desc, &part);
323*4882a593Smuzhiyun #endif
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun #ifdef CONFIG_FASTBOOT_OEM_UNLOCK
326*4882a593Smuzhiyun 	/* read oem unlock status and attach to bootargs */
327*4882a593Smuzhiyun 	char oem_unlock[30] = {0};
328*4882a593Smuzhiyun 	TEEC_Result result;
329*4882a593Smuzhiyun 	uint8_t unlock = 0;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	result = trusty_read_oem_unlock(&unlock);
332*4882a593Smuzhiyun 	if (result) {
333*4882a593Smuzhiyun 		printf("Failed to read oem unlock status, ret=%d\n", result);
334*4882a593Smuzhiyun 	} else {
335*4882a593Smuzhiyun 		snprintf(oem_unlock, sizeof(oem_unlock),
336*4882a593Smuzhiyun 			 "androidboot.oem_unlocked=%d", unlock);
337*4882a593Smuzhiyun 		env_update("bootargs", oem_unlock);
338*4882a593Smuzhiyun 	}
339*4882a593Smuzhiyun #endif
340*4882a593Smuzhiyun 	if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY)
341*4882a593Smuzhiyun 		part_name = PART_RECOVERY;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	ret = part_get_info_by_name(dev_desc, part_name, &part);
344*4882a593Smuzhiyun 	if (ret < 0) {
345*4882a593Smuzhiyun 		printf("Could not find %s part\n", part.name);
346*4882a593Smuzhiyun 		return CMD_RET_FAILURE;
347*4882a593Smuzhiyun 	}
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	return boot_rockchip_image(dev_desc, &part) ? CMD_RET_FAILURE : 0;
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun U_BOOT_CMD(
353*4882a593Smuzhiyun 	bootrkp,  1,     1,      do_boot_rockchip,
354*4882a593Smuzhiyun 	"Boot Linux Image from rockchip image type",
355*4882a593Smuzhiyun 	"kernel.img: zImage/Image\n"
356*4882a593Smuzhiyun 	"boot.img: ramdisk\n"
357*4882a593Smuzhiyun 	"resource.img: dtb, u-boot logo, kernel logo"
358*4882a593Smuzhiyun );
359