xref: /rk3399_rockchip-uboot/common/spl/spl_rkfw.c (revision 8dd9db5d1cd5826638c3cdb5f681300ff2f29f3b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
4  */
5 
6 #include <common.h>
7 #include <malloc.h>
8 #include <spl_rkfw.h>
9 
10 static const __aligned(16) struct s_fip_name_id fip_name_id[] = {
11 	{ BL30_IMAGE_NAME, UUID_SCP_FIRMWARE_BL30 },		/* optional */
12 	{ BL31_IMAGE_NAME, UUID_EL3_RUNTIME_FIRMWARE_BL31 },	/* mandatory */
13 	{ BL32_IMAGE_NAME, UUID_SECURE_PAYLOAD_BL32 },		/* optional */
14 };
15 
16 static int file2comp_id(const char *file_name, u32 *comp_id)
17 {
18 	int i;
19 
20 	for (i = 0; i < ARRAY_SIZE(fip_name_id); i++) {
21 		if (!strcmp(file_name, fip_name_id[i].name)) {
22 			*comp_id = fip_name_id[i].id;
23 			return 0;
24 		}
25 	}
26 
27 	return -ENOENT;
28 }
29 
30 static int open_image(const char *image_name, tboot_entry *entry,
31 		      struct tag_tboot_header_2k *hdr)
32 {
33 	u32 i, component_num, sign_offset;
34 	component_data *pcompdata;
35 	boot_component *pcomp;
36 	int n_found = 0;
37 	u32 comp_id;
38 	int ret;
39 
40 	ret = file2comp_id(image_name, &comp_id);
41 	if (ret) {
42 		printf("Can't find unknown image: %s\n", image_name);
43 		return ret;
44 	}
45 
46 	component_num = (hdr->size >> 16) & 0xffff;
47 	sign_offset = (hdr->size & 0xffff) << 2;
48 	pcompdata = (component_data *)((char *)hdr + sizeof(tboot_header));
49 	pcomp = (boot_component *)((char *)hdr + sign_offset + SIGNATURE_SIZE);
50 
51 	for (i = 0; i < component_num; i++) {
52 		if (comp_id == pcomp->component_id) {
53 			if (n_found < MAX_BL_CODE_NUM) {
54 				memcpy(&entry[n_found].component, pcomp,
55 				       sizeof(boot_component));
56 				memcpy(&entry[n_found].compdata, pcompdata,
57 				       sizeof(component_data));
58 				n_found++;
59 			} else {
60 				printf("Image num excess max: %d!\n",
61 				       MAX_BL_CODE_NUM);
62 				return -EINVAL;
63 			}
64 		} else {
65 			if (n_found > 0)
66 				break;
67 		}
68 
69 		pcomp++;
70 		pcompdata++;
71 	}
72 
73 	if (!n_found) {
74 		printf("No find %s\n", image_name);
75 		return -ENONET;
76 	}
77 
78 	return n_found;
79 }
80 
81 static int check_image(struct tag_tboot_header_2k *hdr)
82 {
83 	u32 hash_format[] = { 0, 160, 256, 256 };
84 
85 	/* HASH format identifier */
86 	return (hash_format[hdr->flags & 0x3] == 0) ? -EINVAL : 0;
87 }
88 
89 static int load_image(struct spl_load_info *info,
90 		      struct tag_tboot_header_2k *hdr,
91 		      u32 image_sector,
92 		      const char *image_name,
93 		      uintptr_t *entry_point)
94 {
95 	tboot_entry entry[MAX_BL_CODE_NUM];
96 	void *image_buf = NULL;
97 	ulong load_addr;
98 	u32 sect_off;
99 	u32 sect_cnt;
100 	int image_num;
101 	int i, ret;
102 
103 	/* Parse components from image header */
104 	image_num = open_image(image_name, entry, hdr);
105 	if (image_num < 0)
106 		return image_num;
107 
108 	/* Get all component */
109 	for (i = 0; i < image_num; i++) {
110 		load_addr = entry[i].compdata.load_addr;
111 		sect_cnt = entry[i].component.image_size;
112 		sect_off = entry[i].component.storage_addr;
113 
114 		printf("%s[%d]: addr=0x%lx, size=0x%lx\n",
115 		       image_name, i, load_addr, (ulong)sect_cnt * 512);
116 
117 		/*
118 		 * MMC/NAND controller DMA can't access sram region, so:
119 		 * data -> ddr buffer -> memcpy to sram region.
120 		 */
121 		if (load_addr >= SDRAM_MAX_SIZE) {
122 			image_buf = memalign(ARCH_DMA_MINALIGN, sect_cnt * 512);
123 			if (!image_buf) {
124 				printf("%s: malloc failed\n", __func__);
125 				return -ENOMEM;
126 			}
127 		} else {
128 			image_buf = (void *)load_addr;
129 		}
130 
131 		ret = info->read(info, image_sector + sect_off,
132 				 sect_cnt, image_buf);
133 		if (ret != sect_cnt) {
134 			printf("Read '%s' failed at sector: %ld, ret=%d\n",
135 			       image_name, (ulong)image_sector + sect_off, ret);
136 			return -EIO;
137 		}
138 
139 		/* Verify component */
140 		ret = check_image(hdr);
141 		if (ret) {
142 			printf("%s[%d]: verify image fail!\n", image_name, i);
143 			return ret;
144 		}
145 
146 		/* Handle sram region */
147 		if ((ulong)image_buf != load_addr) {
148 			memcpy((void *)load_addr, image_buf, sect_cnt << 9);
149 			free(image_buf);
150 		}
151 
152 		/* Fill entry_point by first component */
153 		if (i == 0)
154 			*entry_point = (uintptr_t)load_addr;
155 	}
156 
157 	return ret;
158 }
159 
160 static int rkfw_load_trust(struct spl_load_info *info, u32 image_sector,
161 			   uintptr_t *bl31_entry, uintptr_t *bl32_entry,
162 			   int *found_rkfw, u32 try_count)
163 {
164 	struct tag_tboot_header_2k hdr;
165 	u32 sect_addr = image_sector;
166 	int blkcnt = 4;	/* header sectors, 2KB */
167 	int i, ret = 0;
168 
169 	/* Find valid image header */
170 	for (i = 0; i < try_count; i++) {
171 		sect_addr = image_sector + (i * RKFW_RETRY_SECTOR_SIZE);
172 		if (blkcnt != info->read(info, sect_addr, blkcnt, &hdr))
173 			continue;
174 
175 		if (hdr.tag == TBOOT_HEAD_TAG) {
176 			/* Mark it */
177 			*found_rkfw = 1;
178 
179 			/* bl31 is mandatory */
180 			ret = load_image(info, &hdr, sect_addr,
181 					 BL31_IMAGE_NAME, bl31_entry);
182 			if (ret)
183 				continue;
184 
185 			/* bl32 is optional */
186 			ret = load_image(info, &hdr, sect_addr,
187 					 BL32_IMAGE_NAME, bl32_entry);
188 			if (ret) {
189 				if (ret == -ENONET) {
190 					*bl32_entry = -1;	/* Not exist */
191 					ret = 0;
192 				} else {
193 					continue;
194 				}
195 			}
196 			break;
197 		}
198 	}
199 
200 	return ret;
201 }
202 
203 static int rkfw_load_uboot(struct spl_load_info *info, u32 image_sector,
204 			   uintptr_t *bl33_entry, u32 try_count)
205 {
206 	struct tag_second_loader_hdr hdr;
207 	int i, ret, blkcnt = 4;	/* header sectors, 2KB */
208 	char *load_addr;
209 	u32 sect_addr;
210 
211 	/* Detect valid image header */
212 	for (i = 0; i < try_count; i++) {
213 		sect_addr = image_sector + (i * RKFW_RETRY_SECTOR_SIZE);
214 		ret = info->read(info, sect_addr, blkcnt, &hdr);
215 		if (ret != blkcnt)
216 			continue;
217 
218 		if (!memcmp(hdr.magic, LOADER_HARD_STR, 6)) {
219 			/* Load full binary image(right behind header) */
220 			sect_addr += blkcnt;
221 			load_addr = (char *)((size_t)hdr.loader_load_addr);
222 			blkcnt = DIV_ROUND_UP(hdr.loader_load_size, 512);
223 
224 			printf("u-boot.bin: addr=0x%lx, size=0x%lx\n",
225 			       (ulong)load_addr, (ulong)blkcnt * 512);
226 			ret = info->read(info, sect_addr, blkcnt, load_addr);
227 			if (ret != blkcnt)
228 				continue;
229 
230 			break;
231 		}
232 	}
233 
234 	if (i == try_count) {
235 		printf("Can not find usable uboot\n");
236 		return -ENONET;
237 	}
238 
239 	/* Fill entry point */
240 	*bl33_entry = (uintptr_t)hdr.loader_load_addr;
241 
242 	return 0;
243 }
244 
245 int spl_load_rkfw_image(struct spl_image_info *spl_image,
246 			struct spl_load_info *info,
247 			u32 trust_sector, u32 uboot_sector)
248 {
249 	int ret, try_count = RKFW_RETRY_SECTOR_TIMES;
250 	int found_rkfw = 0;
251 
252 	ret = rkfw_load_trust(info, trust_sector,
253 			      &spl_image->entry_point,
254 			      &spl_image->entry_point_bl32,
255 			      &found_rkfw, try_count);
256 	if (ret) {
257 		printf("Load trust image failed! ret=%d\n", ret);
258 		goto out;
259 	}
260 
261 	ret = rkfw_load_uboot(info, uboot_sector,
262 			      &spl_image->entry_point_bl33, try_count);
263 	if (ret) {
264 		printf("Load uboot image failed! ret=%d\n", ret);
265 		goto out;
266 	}
267 
268 #if CONFIG_IS_ENABLED(LOAD_FIT)
269 	spl_image->fdt_addr = 0;
270 #endif
271 	spl_image->os = IH_OS_ARM_TRUSTED_FIRMWARE;
272 
273 out:
274 	/* If not found rockchip firmware, try others outside */
275 	return found_rkfw ? ret : -EAGAIN;
276 }
277