]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
44d3a306 SG |
2 | /* |
3 | * Copyright (c) 2013, Google Inc. | |
4 | * | |
5 | * (C) Copyright 2008 Semihalf | |
6 | * | |
7 | * (C) Copyright 2000-2006 | |
8 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
44d3a306 SG |
9 | */ |
10 | ||
11 | #include <common.h> | |
e91d6607 | 12 | #include <command.h> |
44d3a306 | 13 | #include <fdt_support.h> |
e2237a2c | 14 | #include <fdtdec.h> |
cdbff9fc | 15 | #include <env.h> |
44d3a306 SG |
16 | #include <errno.h> |
17 | #include <image.h> | |
4d72caa5 | 18 | #include <lmb.h> |
f7ae49fc | 19 | #include <log.h> |
336d4615 | 20 | #include <malloc.h> |
401d1c4f | 21 | #include <asm/global_data.h> |
b08c8c48 | 22 | #include <linux/libfdt.h> |
0eb25b61 | 23 | #include <mapmem.h> |
44d3a306 | 24 | #include <asm/io.h> |
98887ab8 | 25 | #include <dm/ofnode.h> |
6ccb05ea | 26 | #include <tee/optee.h> |
44d3a306 | 27 | |
44d3a306 SG |
28 | DECLARE_GLOBAL_DATA_PTR; |
29 | ||
30 | static void fdt_error(const char *msg) | |
31 | { | |
32 | puts("ERROR: "); | |
33 | puts(msg); | |
34 | puts(" - must RESET the board to recover.\n"); | |
35 | } | |
36 | ||
c76c93a3 | 37 | #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT) |
f3543e69 | 38 | static const struct legacy_img_hdr *image_get_fdt(ulong fdt_addr) |
44d3a306 | 39 | { |
f3543e69 | 40 | const struct legacy_img_hdr *fdt_hdr = map_sysmem(fdt_addr, 0); |
44d3a306 SG |
41 | |
42 | image_print_contents(fdt_hdr); | |
43 | ||
44 | puts(" Verifying Checksum ... "); | |
45 | if (!image_check_hcrc(fdt_hdr)) { | |
46 | fdt_error("fdt header checksum invalid"); | |
47 | return NULL; | |
48 | } | |
49 | ||
50 | if (!image_check_dcrc(fdt_hdr)) { | |
51 | fdt_error("fdt checksum invalid"); | |
52 | return NULL; | |
53 | } | |
54 | puts("OK\n"); | |
55 | ||
56 | if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) { | |
57 | fdt_error("uImage is not a fdt"); | |
58 | return NULL; | |
59 | } | |
60 | if (image_get_comp(fdt_hdr) != IH_COMP_NONE) { | |
61 | fdt_error("uImage is compressed"); | |
62 | return NULL; | |
63 | } | |
2f0877c7 | 64 | if (fdt_check_header((void *)image_get_data(fdt_hdr)) != 0) { |
44d3a306 SG |
65 | fdt_error("uImage data is not a fdt"); |
66 | return NULL; | |
67 | } | |
68 | return fdt_hdr; | |
69 | } | |
21d29f7f | 70 | #endif |
44d3a306 | 71 | |
e2237a2c | 72 | static void boot_fdt_reserve_region(struct lmb *lmb, uint64_t addr, |
f46959ce | 73 | uint64_t size, enum lmb_flags flags) |
e2237a2c | 74 | { |
e1d7ed34 | 75 | long ret; |
e2237a2c | 76 | |
f46959ce | 77 | ret = lmb_reserve_flags(lmb, addr, size, flags); |
e1d7ed34 | 78 | if (ret >= 0) { |
f46959ce PD |
79 | debug(" reserving fdt memory region: addr=%llx size=%llx flags=%x\n", |
80 | (unsigned long long)addr, | |
81 | (unsigned long long)size, flags); | |
e2237a2c SG |
82 | } else { |
83 | puts("ERROR: reserving fdt memory region failed "); | |
f46959ce PD |
84 | printf("(addr=%llx size=%llx flags=%x)\n", |
85 | (unsigned long long)addr, | |
86 | (unsigned long long)size, flags); | |
e2237a2c SG |
87 | } |
88 | } | |
89 | ||
44d3a306 | 90 | /** |
e2237a2c SG |
91 | * boot_fdt_add_mem_rsv_regions - Mark the memreserve and reserved-memory |
92 | * sections as unusable | |
44d3a306 SG |
93 | * @lmb: pointer to lmb handle, will be used for memory mgmt |
94 | * @fdt_blob: pointer to fdt blob base address | |
95 | * | |
e2237a2c SG |
96 | * Adds the and reserved-memorymemreserve regions in the dtb to the lmb block. |
97 | * Adding the memreserve regions prevents u-boot from using them to store the | |
98 | * initrd or the fdt blob. | |
44d3a306 SG |
99 | */ |
100 | void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob) | |
101 | { | |
102 | uint64_t addr, size; | |
e2237a2c SG |
103 | int i, total, ret; |
104 | int nodeoffset, subnode; | |
105 | struct fdt_resource res; | |
f46959ce | 106 | enum lmb_flags flags; |
44d3a306 SG |
107 | |
108 | if (fdt_check_header(fdt_blob) != 0) | |
109 | return; | |
110 | ||
e2237a2c | 111 | /* process memreserve sections */ |
44d3a306 SG |
112 | total = fdt_num_mem_rsv(fdt_blob); |
113 | for (i = 0; i < total; i++) { | |
114 | if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0) | |
115 | continue; | |
f46959ce | 116 | boot_fdt_reserve_region(lmb, addr, size, LMB_NONE); |
e2237a2c SG |
117 | } |
118 | ||
119 | /* process reserved-memory */ | |
120 | nodeoffset = fdt_subnode_offset(fdt_blob, 0, "reserved-memory"); | |
121 | if (nodeoffset >= 0) { | |
122 | subnode = fdt_first_subnode(fdt_blob, nodeoffset); | |
123 | while (subnode >= 0) { | |
124 | /* check if this subnode has a reg property */ | |
125 | ret = fdt_get_resource(fdt_blob, subnode, "reg", 0, | |
126 | &res); | |
28b417ce | 127 | if (!ret && fdtdec_get_is_enabled(fdt_blob, subnode)) { |
f46959ce PD |
128 | flags = LMB_NONE; |
129 | if (fdtdec_get_bool(fdt_blob, subnode, | |
130 | "no-map")) | |
131 | flags = LMB_NOMAP; | |
e2237a2c SG |
132 | addr = res.start; |
133 | size = res.end - res.start + 1; | |
f46959ce | 134 | boot_fdt_reserve_region(lmb, addr, size, flags); |
e2237a2c SG |
135 | } |
136 | ||
137 | subnode = fdt_next_subnode(fdt_blob, subnode); | |
138 | } | |
44d3a306 SG |
139 | } |
140 | } | |
141 | ||
142 | /** | |
143 | * boot_relocate_fdt - relocate flat device tree | |
144 | * @lmb: pointer to lmb handle, will be used for memory mgmt | |
145 | * @of_flat_tree: pointer to a char* variable, will hold fdt start address | |
146 | * @of_size: pointer to a ulong variable, will hold fdt length | |
147 | * | |
148 | * boot_relocate_fdt() allocates a region of memory within the bootmap and | |
149 | * relocates the of_flat_tree into that region, even if the fdt is already in | |
150 | * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD | |
151 | * bytes. | |
152 | * | |
153 | * of_flat_tree and of_size are set to final (after relocation) values | |
154 | * | |
155 | * returns: | |
156 | * 0 - success | |
157 | * 1 - failure | |
158 | */ | |
159 | int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size) | |
160 | { | |
161 | void *fdt_blob = *of_flat_tree; | |
162 | void *of_start = NULL; | |
a96d5657 | 163 | u64 start, size, usable; |
44d3a306 | 164 | char *fdt_high; |
a96d5657 | 165 | ulong mapsize, low; |
44d3a306 | 166 | ulong of_len = 0; |
a96d5657 | 167 | int bank; |
44d3a306 SG |
168 | int err; |
169 | int disable_relocation = 0; | |
170 | ||
171 | /* nothing to do */ | |
172 | if (*of_size == 0) | |
173 | return 0; | |
174 | ||
175 | if (fdt_check_header(fdt_blob) != 0) { | |
176 | fdt_error("image is not a fdt"); | |
177 | goto error; | |
178 | } | |
179 | ||
180 | /* position on a 4K boundary before the alloc_current */ | |
181 | /* Pad the FDT by a specified amount */ | |
182 | of_len = *of_size + CONFIG_SYS_FDT_PAD; | |
183 | ||
184 | /* If fdt_high is set use it to select the relocation address */ | |
00caae6d | 185 | fdt_high = env_get("fdt_high"); |
44d3a306 | 186 | if (fdt_high) { |
19511935 SG |
187 | ulong desired_addr = hextoul(fdt_high, NULL); |
188 | ulong addr; | |
44d3a306 | 189 | |
19511935 | 190 | if (desired_addr == ~0UL) { |
44d3a306 SG |
191 | /* All ones means use fdt in place */ |
192 | of_start = fdt_blob; | |
19511935 | 193 | lmb_reserve(lmb, map_to_sysmem(of_start), of_len); |
44d3a306 SG |
194 | disable_relocation = 1; |
195 | } else if (desired_addr) { | |
19511935 SG |
196 | addr = lmb_alloc_base(lmb, of_len, 0x1000, |
197 | desired_addr); | |
198 | of_start = map_sysmem(addr, of_len); | |
44d3a306 SG |
199 | if (of_start == NULL) { |
200 | puts("Failed using fdt_high value for Device Tree"); | |
201 | goto error; | |
202 | } | |
203 | } else { | |
19511935 SG |
204 | addr = lmb_alloc(lmb, of_len, 0x1000); |
205 | of_start = map_sysmem(addr, of_len); | |
44d3a306 SG |
206 | } |
207 | } else { | |
a96d5657 MV |
208 | mapsize = env_get_bootm_mapsize(); |
209 | low = env_get_bootm_low(); | |
210 | of_start = NULL; | |
211 | ||
212 | for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) { | |
213 | start = gd->bd->bi_dram[bank].start; | |
214 | size = gd->bd->bi_dram[bank].size; | |
215 | ||
216 | /* DRAM bank addresses are too low, skip it. */ | |
217 | if (start + size < low) | |
218 | continue; | |
219 | ||
d545fe3b | 220 | usable = min(start + size, (u64)(low + mapsize)); |
a96d5657 MV |
221 | |
222 | /* | |
223 | * At least part of this DRAM bank is usable, try | |
224 | * using it for LMB allocation. | |
225 | */ | |
19511935 | 226 | of_start = map_sysmem((ulong)lmb_alloc_base(lmb, |
d545fe3b | 227 | of_len, 0x1000, usable), of_len); |
a96d5657 MV |
228 | /* Allocation succeeded, use this block. */ |
229 | if (of_start != NULL) | |
230 | break; | |
231 | ||
232 | /* | |
233 | * Reduce the mapping size in the next bank | |
234 | * by the size of attempt in current bank. | |
235 | */ | |
236 | mapsize -= usable - max(start, (u64)low); | |
237 | if (!mapsize) | |
238 | break; | |
239 | } | |
44d3a306 SG |
240 | } |
241 | ||
242 | if (of_start == NULL) { | |
243 | puts("device tree - allocation error\n"); | |
244 | goto error; | |
245 | } | |
246 | ||
247 | if (disable_relocation) { | |
248 | /* | |
249 | * We assume there is space after the existing fdt to use | |
250 | * for padding | |
251 | */ | |
252 | fdt_set_totalsize(of_start, of_len); | |
253 | printf(" Using Device Tree in place at %p, end %p\n", | |
254 | of_start, of_start + of_len - 1); | |
255 | } else { | |
256 | debug("## device tree at %p ... %p (len=%ld [0x%lX])\n", | |
257 | fdt_blob, fdt_blob + *of_size - 1, of_len, of_len); | |
258 | ||
259 | printf(" Loading Device Tree to %p, end %p ... ", | |
260 | of_start, of_start + of_len - 1); | |
261 | ||
262 | err = fdt_open_into(fdt_blob, of_start, of_len); | |
263 | if (err != 0) { | |
264 | fdt_error("fdt move failed"); | |
265 | goto error; | |
266 | } | |
267 | puts("OK\n"); | |
268 | } | |
269 | ||
270 | *of_flat_tree = of_start; | |
271 | *of_size = of_len; | |
272 | ||
3a09f38d | 273 | if (IS_ENABLED(CONFIG_CMD_FDT)) |
596be5f3 | 274 | set_working_fdt_addr(map_to_sysmem(*of_flat_tree)); |
44d3a306 SG |
275 | return 0; |
276 | ||
277 | error: | |
278 | return 1; | |
279 | } | |
280 | ||
44d3a306 | 281 | /** |
4cb35b7a | 282 | * select_fdt() - Select and locate the FDT to use |
44d3a306 | 283 | * |
4cb35b7a SG |
284 | * @images: pointer to the bootm images structure |
285 | * @select: name of FDT to select, or NULL for any | |
286 | * @arch: expected FDT architecture | |
287 | * @fdt_addrp: pointer to a ulong variable, will hold FDT pointer | |
185f812c | 288 | * Return: 0 if OK, -ENOPKG if no FDT (but an error should not be reported), |
4cb35b7a | 289 | * other -ve value on other error |
44d3a306 | 290 | */ |
44d3a306 | 291 | |
d9d7c20b | 292 | static int select_fdt(struct bootm_headers *images, const char *select, u8 arch, |
4cb35b7a SG |
293 | ulong *fdt_addrp) |
294 | { | |
295 | const char *buf; | |
296 | ulong fdt_addr; | |
6a7b406a | 297 | |
73223f0e | 298 | #if CONFIG_IS_ENABLED(FIT) |
4cb35b7a SG |
299 | const char *fit_uname_config = images->fit_uname_cfg; |
300 | const char *fit_uname_fdt = NULL; | |
301 | ulong default_addr; | |
302 | int fdt_noffset; | |
a2198cd0 | 303 | |
4cb35b7a | 304 | if (select) { |
44d3a306 SG |
305 | /* |
306 | * If the FDT blob comes from the FIT image and the | |
307 | * FIT image address is omitted in the command line | |
308 | * argument, try to use ramdisk or os FIT image | |
309 | * address or default load address. | |
310 | */ | |
311 | if (images->fit_uname_rd) | |
312 | default_addr = (ulong)images->fit_hdr_rd; | |
313 | else if (images->fit_uname_os) | |
314 | default_addr = (ulong)images->fit_hdr_os; | |
315 | else | |
bb872dd9 | 316 | default_addr = image_load_addr; |
44d3a306 | 317 | |
4cb35b7a SG |
318 | if (fit_parse_conf(select, default_addr, &fdt_addr, |
319 | &fit_uname_config)) { | |
44d3a306 SG |
320 | debug("* fdt: config '%s' from image at 0x%08lx\n", |
321 | fit_uname_config, fdt_addr); | |
4cb35b7a SG |
322 | } else if (fit_parse_subimage(select, default_addr, &fdt_addr, |
323 | &fit_uname_fdt)) { | |
44d3a306 SG |
324 | debug("* fdt: subimage '%s' from image at 0x%08lx\n", |
325 | fit_uname_fdt, fdt_addr); | |
326 | } else | |
327 | #endif | |
4cb35b7a SG |
328 | { |
329 | fdt_addr = hextoul(select, NULL); | |
330 | debug("* fdt: cmdline image address = 0x%08lx\n", | |
331 | fdt_addr); | |
44d3a306 | 332 | } |
4cb35b7a SG |
333 | #if CONFIG_IS_ENABLED(FIT) |
334 | } else { | |
335 | /* use FIT configuration provided in first bootm | |
336 | * command argument | |
44d3a306 | 337 | */ |
4cb35b7a SG |
338 | fdt_addr = map_to_sysmem(images->fit_hdr_os); |
339 | fdt_noffset = fit_get_node_from_config(images, FIT_FDT_PROP, | |
340 | fdt_addr); | |
341 | if (fdt_noffset == -ENOENT) | |
342 | return -ENOPKG; | |
343 | else if (fdt_noffset < 0) | |
344 | return fdt_noffset; | |
345 | } | |
346 | #endif | |
347 | debug("## Checking for 'FDT'/'FDT Image' at %08lx\n", | |
348 | fdt_addr); | |
349 | ||
350 | /* | |
351 | * Check if there is an FDT image at the | |
352 | * address provided in the second bootm argument | |
353 | * check image type, for FIT images get a FIT node. | |
354 | */ | |
355 | buf = map_sysmem(fdt_addr, 0); | |
356 | switch (genimg_get_format(buf)) { | |
c76c93a3 | 357 | #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT) |
4cb35b7a | 358 | case IMAGE_FORMAT_LEGACY: { |
f3543e69 | 359 | const struct legacy_img_hdr *fdt_hdr; |
a2198cd0 SG |
360 | ulong load, load_end; |
361 | ulong image_start, image_data, image_end; | |
362 | ||
44d3a306 SG |
363 | /* verify fdt_addr points to a valid image header */ |
364 | printf("## Flattened Device Tree from Legacy Image at %08lx\n", | |
365 | fdt_addr); | |
366 | fdt_hdr = image_get_fdt(fdt_addr); | |
367 | if (!fdt_hdr) | |
4cb35b7a | 368 | return -ENOPKG; |
44d3a306 SG |
369 | |
370 | /* | |
371 | * move image data to the load address, | |
372 | * make sure we don't overwrite initial image | |
373 | */ | |
374 | image_start = (ulong)fdt_hdr; | |
375 | image_data = (ulong)image_get_data(fdt_hdr); | |
376 | image_end = image_get_image_end(fdt_hdr); | |
377 | ||
53f375fa SG |
378 | load = image_get_load(fdt_hdr); |
379 | load_end = load + image_get_data_size(fdt_hdr); | |
44d3a306 | 380 | |
53f375fa SG |
381 | if (load == image_start || |
382 | load == image_data) { | |
2ea47be0 | 383 | fdt_addr = load; |
44d3a306 SG |
384 | break; |
385 | } | |
386 | ||
53f375fa | 387 | if ((load < image_end) && (load_end > image_start)) { |
44d3a306 | 388 | fdt_error("fdt overwritten"); |
4cb35b7a | 389 | return -EFAULT; |
44d3a306 SG |
390 | } |
391 | ||
392 | debug(" Loading FDT from 0x%08lx to 0x%08lx\n", | |
53f375fa | 393 | image_data, load); |
44d3a306 | 394 | |
53f375fa | 395 | memmove((void *)load, |
44d3a306 SG |
396 | (void *)image_data, |
397 | image_get_data_size(fdt_hdr)); | |
398 | ||
53f375fa | 399 | fdt_addr = load; |
44d3a306 | 400 | break; |
a2198cd0 | 401 | } |
21d29f7f | 402 | #endif |
4cb35b7a SG |
403 | case IMAGE_FORMAT_FIT: |
404 | /* | |
405 | * This case will catch both: new uImage format | |
406 | * (libfdt based) and raw FDT blob (also libfdt | |
407 | * based). | |
408 | */ | |
73223f0e | 409 | #if CONFIG_IS_ENABLED(FIT) |
44d3a306 | 410 | /* check FDT blob vs FIT blob */ |
c5819701 | 411 | if (!fit_check_format(buf, IMAGE_SIZE_INVAL)) { |
53f375fa | 412 | ulong load, len; |
44d3a306 | 413 | |
4cb35b7a SG |
414 | fdt_noffset = boot_get_fdt_fit(images, fdt_addr, |
415 | &fit_uname_fdt, | |
416 | &fit_uname_config, | |
417 | arch, &load, &len); | |
44d3a306 | 418 | |
9883df1b | 419 | if (fdt_noffset < 0) |
4cb35b7a | 420 | return -ENOENT; |
9883df1b | 421 | |
53f375fa | 422 | images->fit_hdr_fdt = map_sysmem(fdt_addr, 0); |
44d3a306 SG |
423 | images->fit_uname_fdt = fit_uname_fdt; |
424 | images->fit_noffset_fdt = fdt_noffset; | |
53f375fa | 425 | fdt_addr = load; |
169043d8 | 426 | |
44d3a306 | 427 | break; |
4cb35b7a | 428 | } else |
44d3a306 | 429 | #endif |
4cb35b7a SG |
430 | { |
431 | /* | |
432 | * FDT blob | |
433 | */ | |
434 | debug("* fdt: raw FDT blob\n"); | |
435 | printf("## Flattened Device Tree blob at %08lx\n", | |
436 | (long)fdt_addr); | |
44d3a306 | 437 | } |
4cb35b7a SG |
438 | break; |
439 | default: | |
440 | puts("ERROR: Did not find a cmdline Flattened Device Tree\n"); | |
441 | return -ENOENT; | |
442 | } | |
443 | *fdt_addrp = fdt_addr; | |
444 | ||
445 | return 0; | |
446 | } | |
447 | ||
0aa923ab | 448 | int boot_get_fdt(void *buf, const char *select, uint arch, |
ba5e3f7f SG |
449 | struct bootm_headers *images, char **of_flat_tree, |
450 | ulong *of_size) | |
4cb35b7a | 451 | { |
0aa923ab SG |
452 | char *fdt_blob = NULL; |
453 | ulong fdt_addr; | |
4cb35b7a SG |
454 | |
455 | *of_flat_tree = NULL; | |
456 | *of_size = 0; | |
457 | ||
4cb35b7a SG |
458 | if (select || genimg_has_config(images)) { |
459 | int ret; | |
44d3a306 | 460 | |
4cb35b7a SG |
461 | ret = select_fdt(images, select, arch, &fdt_addr); |
462 | if (ret == -ENOPKG) | |
463 | goto no_fdt; | |
464 | else if (ret) | |
465 | return 1; | |
53f375fa SG |
466 | printf(" Booting using the fdt blob at %#08lx\n", fdt_addr); |
467 | fdt_blob = map_sysmem(fdt_addr, 0); | |
44d3a306 SG |
468 | } else if (images->legacy_hdr_valid && |
469 | image_check_type(&images->legacy_hdr_os_copy, | |
470 | IH_TYPE_MULTI)) { | |
471 | ulong fdt_data, fdt_len; | |
472 | ||
473 | /* | |
474 | * Now check if we have a legacy multi-component image, | |
475 | * get second entry data start address and len. | |
476 | */ | |
477 | printf("## Flattened Device Tree from multi component Image at %08lX\n", | |
478 | (ulong)images->legacy_hdr_os); | |
479 | ||
480 | image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data, | |
481 | &fdt_len); | |
482 | if (fdt_len) { | |
483 | fdt_blob = (char *)fdt_data; | |
484 | printf(" Booting using the fdt at 0x%p\n", fdt_blob); | |
485 | ||
486 | if (fdt_check_header(fdt_blob) != 0) { | |
487 | fdt_error("image is not a fdt"); | |
488 | goto error; | |
489 | } | |
490 | ||
491 | if (fdt_totalsize(fdt_blob) != fdt_len) { | |
492 | fdt_error("fdt size != image size"); | |
493 | goto error; | |
494 | } | |
495 | } else { | |
496 | debug("## No Flattened Device Tree\n"); | |
48aead71 | 497 | goto no_fdt; |
44d3a306 | 498 | } |
6a7b406a SG |
499 | #ifdef CONFIG_ANDROID_BOOT_IMAGE |
500 | } else if (genimg_get_format(buf) == IMAGE_FORMAT_ANDROID) { | |
636da203 | 501 | void *hdr = buf; |
6e314584 | 502 | ulong fdt_data, fdt_len; |
503 | u32 fdt_size, dtb_idx; | |
504 | /* | |
505 | * Firstly check if this android boot image has dtb field. | |
506 | */ | |
507 | dtb_idx = (u32)env_get_ulong("adtb_idx", 10, 0); | |
e058176b SO |
508 | if (android_image_get_dtb_by_index((ulong)hdr, 0, |
509 | dtb_idx, &fdt_addr, &fdt_size)) { | |
6e314584 | 510 | fdt_blob = (char *)map_sysmem(fdt_addr, 0); |
511 | if (fdt_check_header(fdt_blob)) | |
512 | goto no_fdt; | |
6a7b406a | 513 | |
6e314584 | 514 | debug("## Using FDT in Android image dtb area with idx %u\n", dtb_idx); |
515 | } else if (!android_image_get_second(hdr, &fdt_data, &fdt_len) && | |
516 | !fdt_check_header((char *)fdt_data)) { | |
18b8f2c4 ER |
517 | fdt_blob = (char *)fdt_data; |
518 | if (fdt_totalsize(fdt_blob) != fdt_len) | |
519 | goto error; | |
6a7b406a | 520 | |
18b8f2c4 ER |
521 | debug("## Using FDT in Android image second area\n"); |
522 | } else { | |
62392675 ER |
523 | fdt_addr = env_get_hex("fdtaddr", 0); |
524 | if (!fdt_addr) | |
525 | goto no_fdt; | |
6a7b406a | 526 | |
62392675 ER |
527 | fdt_blob = map_sysmem(fdt_addr, 0); |
528 | if (fdt_check_header(fdt_blob)) | |
529 | goto no_fdt; | |
6a7b406a | 530 | |
62392675 | 531 | debug("## Using FDT at ${fdtaddr}=Ox%lx\n", fdt_addr); |
18b8f2c4 | 532 | } |
6a7b406a | 533 | #endif |
44d3a306 SG |
534 | } else { |
535 | debug("## No Flattened Device Tree\n"); | |
48aead71 | 536 | goto no_fdt; |
44d3a306 SG |
537 | } |
538 | ||
539 | *of_flat_tree = fdt_blob; | |
540 | *of_size = fdt_totalsize(fdt_blob); | |
541 | debug(" of_flat_tree at 0x%08lx size 0x%08lx\n", | |
542 | (ulong)*of_flat_tree, *of_size); | |
543 | ||
544 | return 0; | |
545 | ||
48aead71 | 546 | no_fdt: |
80281829 ER |
547 | debug("Continuing to boot without FDT\n"); |
548 | return 0; | |
44d3a306 | 549 | error: |
44d3a306 SG |
550 | return 1; |
551 | } | |
13d06981 SG |
552 | |
553 | /* | |
554 | * Verify the device tree. | |
555 | * | |
556 | * This function is called after all device tree fix-ups have been enacted, | |
557 | * so that the final device tree can be verified. The definition of "verified" | |
558 | * is up to the specific implementation. However, it generally means that the | |
559 | * addresses of some of the devices in the device tree are compared with the | |
560 | * actual addresses at which U-Boot has placed them. | |
561 | * | |
a187559e | 562 | * Returns 1 on success, 0 on failure. If 0 is returned, U-Boot will halt the |
13d06981 SG |
563 | * boot process. |
564 | */ | |
565 | __weak int ft_verify_fdt(void *fdt) | |
566 | { | |
567 | return 1; | |
568 | } | |
569 | ||
4280342a AB |
570 | __weak int arch_fixup_fdt(void *blob) |
571 | { | |
572 | return 0; | |
573 | } | |
574 | ||
d9d7c20b | 575 | int image_setup_libfdt(struct bootm_headers *images, void *blob, |
1de1a034 | 576 | struct lmb *lmb) |
13d06981 SG |
577 | { |
578 | ulong *initrd_start = &images->initrd_start; | |
579 | ulong *initrd_end = &images->initrd_end; | |
e91d6607 MS |
580 | int ret, fdt_ret, of_size; |
581 | ||
582 | if (IS_ENABLED(CONFIG_OF_ENV_SETUP)) { | |
583 | const char *fdt_fixup; | |
584 | ||
585 | fdt_fixup = env_get("fdt_fixup"); | |
586 | if (fdt_fixup) { | |
587 | set_working_fdt_addr(map_to_sysmem(blob)); | |
588 | ret = run_command_list(fdt_fixup, -1, 0); | |
589 | if (ret) | |
590 | printf("WARNING: fdt_fixup command returned %d\n", | |
591 | ret); | |
592 | } | |
593 | } | |
594 | ||
595 | ret = -EPERM; | |
13d06981 | 596 | |
10be5b5d PK |
597 | if (fdt_root(blob) < 0) { |
598 | printf("ERROR: root node setup failed\n"); | |
599 | goto err; | |
600 | } | |
bc6ed0f9 | 601 | if (fdt_chosen(blob) < 0) { |
6f4dbc21 SG |
602 | printf("ERROR: /chosen node create failed\n"); |
603 | goto err; | |
13d06981 | 604 | } |
e29607ed | 605 | if (arch_fixup_fdt(blob) < 0) { |
6f4dbc21 SG |
606 | printf("ERROR: arch-specific fdt fixup failed\n"); |
607 | goto err; | |
608 | } | |
5f9070a4 | 609 | |
a2535243 | 610 | fdt_ret = optee_copy_fdt_nodes(blob); |
5f9070a4 EC |
611 | if (fdt_ret) { |
612 | printf("ERROR: transfer of optee nodes to new fdt failed: %s\n", | |
613 | fdt_strerror(fdt_ret)); | |
614 | goto err; | |
615 | } | |
616 | ||
5f2d5915 DG |
617 | /* Store name of configuration node as u-boot,bootconf in /chosen node */ |
618 | if (images->fit_uname_cfg) | |
619 | fdt_find_and_setprop(blob, "/chosen", "u-boot,bootconf", | |
620 | images->fit_uname_cfg, | |
621 | strlen(images->fit_uname_cfg) + 1, 1); | |
622 | ||
26d61195 TR |
623 | /* Update ethernet nodes */ |
624 | fdt_fixup_ethernet(blob); | |
39c0da41 | 625 | #if IS_ENABLED(CONFIG_CMD_PSTORE) |
9ea0a1ee FD |
626 | /* Append PStore configuration */ |
627 | fdt_fixup_pstore(blob); | |
628 | #endif | |
30ba2828 | 629 | if (IS_ENABLED(CONFIG_OF_BOARD_SETUP)) { |
402558b1 WK |
630 | const char *skip_board_fixup; |
631 | ||
632 | skip_board_fixup = env_get("skip_board_fixup"); | |
633 | if (skip_board_fixup && ((int)simple_strtol(skip_board_fixup, NULL, 10) == 1)) { | |
634 | printf("skip board fdt fixup\n"); | |
635 | } else { | |
636 | fdt_ret = ft_board_setup(blob, gd->bd); | |
637 | if (fdt_ret) { | |
638 | printf("ERROR: board-specific fdt fixup failed: %s\n", | |
639 | fdt_strerror(fdt_ret)); | |
640 | goto err; | |
641 | } | |
6f4dbc21 | 642 | } |
e29607ed | 643 | } |
3ac0f504 | 644 | if (IS_ENABLED(CONFIG_OF_SYSTEM_SETUP)) { |
d89212b7 MK |
645 | fdt_ret = ft_system_setup(blob, gd->bd); |
646 | if (fdt_ret) { | |
c654b517 SG |
647 | printf("ERROR: system-specific fdt fixup failed: %s\n", |
648 | fdt_strerror(fdt_ret)); | |
649 | goto err; | |
650 | } | |
651 | } | |
f2cbe6e4 SG |
652 | |
653 | if (fdt_initrd(blob, *initrd_start, *initrd_end)) | |
654 | goto err; | |
655 | ||
dbdc9c6a SG |
656 | if (!ft_verify_fdt(blob)) |
657 | goto err; | |
658 | ||
659 | /* after here we are using a livetree */ | |
33ba7270 | 660 | if (!of_live_active() && CONFIG_IS_ENABLED(EVENT)) { |
98887ab8 SG |
661 | struct event_ft_fixup fixup; |
662 | ||
33ba7270 | 663 | fixup.tree = oftree_from_fdt(blob); |
b215b603 | 664 | fixup.images = images; |
33ba7270 SG |
665 | if (oftree_valid(fixup.tree)) { |
666 | ret = event_notify(EVT_FT_FIXUP, &fixup, sizeof(fixup)); | |
667 | if (ret) { | |
668 | printf("ERROR: fdt fixup event failed: %d\n", | |
669 | ret); | |
670 | goto err; | |
671 | } | |
98887ab8 SG |
672 | } |
673 | } | |
13d06981 SG |
674 | |
675 | /* Delete the old LMB reservation */ | |
dea2174d | 676 | if (lmb) |
79e9727f | 677 | lmb_free(lmb, map_to_sysmem(blob), fdt_totalsize(blob)); |
13d06981 | 678 | |
ef476836 | 679 | ret = fdt_shrink_to_minimum(blob, 0); |
13d06981 | 680 | if (ret < 0) |
6f4dbc21 | 681 | goto err; |
13d06981 SG |
682 | of_size = ret; |
683 | ||
13d06981 | 684 | /* Create a new LMB reservation */ |
dea2174d | 685 | if (lmb) |
79e9727f | 686 | lmb_reserve(lmb, map_to_sysmem(blob), of_size); |
13d06981 | 687 | |
f899cc14 | 688 | #if defined(CONFIG_ARCH_KEYSTONE) |
30ba2828 | 689 | if (IS_ENABLED(CONFIG_OF_BOARD_SETUP)) |
00c200f1 VA |
690 | ft_board_setup_ex(blob, gd->bd); |
691 | #endif | |
692 | ||
13d06981 | 693 | return 0; |
6f4dbc21 SG |
694 | err: |
695 | printf(" - must RESET the board to recover.\n\n"); | |
696 | ||
697 | return ret; | |
13d06981 | 698 | } |