]> git.ipfire.org Git - people/ms/u-boot.git/blob - lib_ppc/bootm.c
Merge branch 'master' of git://git.denx.de/u-boot-nand-flash
[people/ms/u-boot.git] / lib_ppc / bootm.c
1 /*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26
27 #include <common.h>
28 #include <watchdog.h>
29 #include <command.h>
30 #include <image.h>
31 #include <malloc.h>
32 #include <zlib.h>
33 #include <bzlib.h>
34 #include <environment.h>
35 #include <asm/byteorder.h>
36
37 #if defined(CONFIG_OF_LIBFDT)
38 #include <fdt.h>
39 #include <libfdt.h>
40 #include <fdt_support.h>
41
42 static void fdt_error (const char *msg);
43 static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
44 bootm_headers_t *images, char **of_flat_tree, ulong *of_size);
45 static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
46 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
47 char **of_flat_tree, ulong *of_size);
48 #endif
49
50 #ifdef CFG_INIT_RAM_LOCK
51 #include <asm/cache.h>
52 #endif
53
54 #ifndef CFG_FDT_PAD
55 #define CFG_FDT_PAD 0x3000
56 #endif
57
58 DECLARE_GLOBAL_DATA_PTR;
59
60 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
61 extern ulong get_effective_memsize(void);
62 static ulong get_sp (void);
63 static void set_clocks_in_mhz (bd_t *kbd);
64
65 #ifndef CFG_LINUX_LOWMEM_MAX_SIZE
66 #define CFG_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
67 #endif
68
69 void __attribute__((noinline))
70 do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
71 bootm_headers_t *images)
72 {
73 ulong sp;
74
75 ulong initrd_start, initrd_end;
76 ulong rd_data_start, rd_data_end, rd_len;
77 ulong size;
78 phys_size_t bootm_size;
79
80 ulong cmd_start, cmd_end, bootmap_base;
81 bd_t *kbd;
82 ulong ep = 0;
83 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
84 int ret;
85 ulong of_size = 0;
86 struct lmb *lmb = images->lmb;
87
88 #if defined(CONFIG_OF_LIBFDT)
89 char *of_flat_tree = NULL;
90 #endif
91
92 bootmap_base = getenv_bootm_low();
93 bootm_size = getenv_bootm_size();
94
95 #ifdef DEBUG
96 if (((u64)bootmap_base + bootm_size) >
97 (CFG_SDRAM_BASE + (u64)gd->ram_size))
98 puts("WARNING: bootm_low + bootm_size exceed total memory\n");
99 if ((bootmap_base + bootm_size) > get_effective_memsize())
100 puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
101 #endif
102
103 size = min(bootm_size, get_effective_memsize());
104 size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
105
106 if (size < bootm_size) {
107 ulong base = bootmap_base + size;
108 printf("WARNING: adjusting available memory to %lx\n", size);
109 lmb_reserve(lmb, base, bootm_size - size);
110 }
111
112 /*
113 * Booting a (Linux) kernel image
114 *
115 * Allocate space for command line and board info - the
116 * address should be as high as possible within the reach of
117 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
118 * memory, which means far enough below the current stack
119 * pointer.
120 */
121 sp = get_sp();
122 debug ("## Current stack ends at 0x%08lx\n", sp);
123
124 /* adjust sp by 1K to be safe */
125 sp -= 1024;
126 lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
127
128 #if defined(CONFIG_OF_LIBFDT)
129 /* find flattened device tree */
130 ret = boot_get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
131
132 if (ret)
133 goto error;
134 #endif
135
136 if (!of_size) {
137 /* allocate space and init command line */
138 ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
139 if (ret) {
140 puts("ERROR with allocation of cmdline\n");
141 goto error;
142 }
143
144 /* allocate space for kernel copy of board info */
145 ret = boot_get_kbd (lmb, &kbd, bootmap_base);
146 if (ret) {
147 puts("ERROR with allocation of kernel bd\n");
148 goto error;
149 }
150 set_clocks_in_mhz(kbd);
151 }
152
153 /* find kernel entry point */
154 if (images->legacy_hdr_valid) {
155 ep = image_get_ep (&images->legacy_hdr_os_copy);
156 #if defined(CONFIG_FIT)
157 } else if (images->fit_uname_os) {
158 ret = fit_image_get_entry (images->fit_hdr_os,
159 images->fit_noffset_os, &ep);
160 if (ret) {
161 puts ("Can't get entry point property!\n");
162 goto error;
163 }
164 #endif
165 } else {
166 puts ("Could not find kernel entry point!\n");
167 goto error;
168 }
169 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
170
171 /* find ramdisk */
172 ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_PPC,
173 &rd_data_start, &rd_data_end);
174 if (ret)
175 goto error;
176
177 rd_len = rd_data_end - rd_data_start;
178
179 #if defined(CONFIG_OF_LIBFDT)
180 ret = boot_relocate_fdt (lmb, bootmap_base,
181 cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
182
183 /*
184 * Add the chosen node if it doesn't exist, add the env and bd_t
185 * if the user wants it (the logic is in the subroutines).
186 */
187 if (of_size) {
188 /* pass in dummy initrd info, we'll fix up later */
189 if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
190 fdt_error ("/chosen node create failed");
191 goto error;
192 }
193 #ifdef CONFIG_OF_BOARD_SETUP
194 /* Call the board-specific fixup routine */
195 ft_board_setup(of_flat_tree, gd->bd);
196 #endif
197 }
198
199 /* Fixup the fdt memreserve now that we know how big it is */
200 if (of_flat_tree) {
201 int j;
202 uint64_t addr, size;
203 int total = fdt_num_mem_rsv(of_flat_tree);
204 uint actualsize;
205
206 for (j = 0; j < total; j++) {
207 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
208 if (addr == (uint64_t)(u32)of_flat_tree) {
209 fdt_del_mem_rsv(of_flat_tree, j);
210 break;
211 }
212 }
213
214 /* Delete the old LMB reservation */
215 lmb_free(lmb, (phys_addr_t)(u32)of_flat_tree,
216 (phys_size_t)fdt_totalsize(of_flat_tree));
217
218 /* Calculate the actual size of the fdt */
219 actualsize = fdt_off_dt_strings(of_flat_tree) +
220 fdt_size_dt_strings(of_flat_tree);
221
222 /* Make it so the fdt ends on a page boundary */
223 actualsize = ALIGN(actualsize, 0x1000);
224 actualsize = actualsize - ((uint)of_flat_tree & 0xfff);
225
226 /* Change the fdt header to reflect the correct size */
227 fdt_set_totalsize(of_flat_tree, actualsize);
228 of_size = actualsize;
229
230 /* Add the new reservation */
231 ret = fdt_add_mem_rsv(of_flat_tree, (uint)of_flat_tree,
232 of_size);
233
234 /* Create a new LMB reservation */
235 lmb_reserve(lmb, (ulong)of_flat_tree, of_size);
236 }
237 #endif /* CONFIG_OF_LIBFDT */
238
239 ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
240 if (ret)
241 goto error;
242
243 #if defined(CONFIG_OF_LIBFDT)
244 /* fixup the initrd now that we know where it should be */
245 if ((of_flat_tree) && (initrd_start && initrd_end)) {
246 uint64_t addr, size;
247 int total = fdt_num_mem_rsv(of_flat_tree);
248 int j;
249
250 /* Look for the dummy entry and delete it */
251 for (j = 0; j < total; j++) {
252 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
253 if (addr == rd_data_start) {
254 fdt_del_mem_rsv(of_flat_tree, j);
255 break;
256 }
257 }
258
259 ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
260 initrd_end - initrd_start + 1);
261 if (ret < 0) {
262 printf("fdt_chosen: %s\n", fdt_strerror(ret));
263 goto error;
264 }
265
266 do_fixup_by_path_u32(of_flat_tree, "/chosen",
267 "linux,initrd-start", initrd_start, 0);
268 do_fixup_by_path_u32(of_flat_tree, "/chosen",
269 "linux,initrd-end", initrd_end, 0);
270 }
271 #endif
272 debug ("## Transferring control to Linux (at address %08lx) ...\n",
273 (ulong)kernel);
274
275 show_boot_progress (15);
276
277 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
278 unlock_ram_in_cache();
279 #endif
280
281 #if defined(CONFIG_OF_LIBFDT)
282 if (of_flat_tree) { /* device tree; boot new style */
283 /*
284 * Linux Kernel Parameters (passing device tree):
285 * r3: pointer to the fdt, followed by the board info data
286 * r4: physical pointer to the kernel itself
287 * r5: NULL
288 * r6: NULL
289 * r7: NULL
290 */
291 debug (" Booting using OF flat tree...\n");
292 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
293 /* does not return */
294 } else
295 #endif
296 {
297 /*
298 * Linux Kernel Parameters (passing board info data):
299 * r3: ptr to board info data
300 * r4: initrd_start or 0 if no initrd
301 * r5: initrd_end - unused if r4 is 0
302 * r6: Start of command line string
303 * r7: End of command line string
304 */
305 debug (" Booting using board info...\n");
306 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
307 /* does not return */
308 }
309 return ;
310
311 error:
312 do_reset (cmdtp, flag, argc, argv);
313 return ;
314 }
315
316 static ulong get_sp (void)
317 {
318 ulong sp;
319
320 asm( "mr %0,1": "=r"(sp) : );
321 return sp;
322 }
323
324 static void set_clocks_in_mhz (bd_t *kbd)
325 {
326 char *s;
327
328 if ((s = getenv ("clocks_in_mhz")) != NULL) {
329 /* convert all clock information to MHz */
330 kbd->bi_intfreq /= 1000000L;
331 kbd->bi_busfreq /= 1000000L;
332 #if defined(CONFIG_MPC8220)
333 kbd->bi_inpfreq /= 1000000L;
334 kbd->bi_pcifreq /= 1000000L;
335 kbd->bi_pevfreq /= 1000000L;
336 kbd->bi_flbfreq /= 1000000L;
337 kbd->bi_vcofreq /= 1000000L;
338 #endif
339 #if defined(CONFIG_CPM2)
340 kbd->bi_cpmfreq /= 1000000L;
341 kbd->bi_brgfreq /= 1000000L;
342 kbd->bi_sccfreq /= 1000000L;
343 kbd->bi_vco /= 1000000L;
344 #endif
345 #if defined(CONFIG_MPC5xxx)
346 kbd->bi_ipbfreq /= 1000000L;
347 kbd->bi_pcifreq /= 1000000L;
348 #endif /* CONFIG_MPC5xxx */
349 }
350 }
351
352 #if defined(CONFIG_OF_LIBFDT)
353 static void fdt_error (const char *msg)
354 {
355 puts ("ERROR: ");
356 puts (msg);
357 puts (" - must RESET the board to recover.\n");
358 }
359
360 static image_header_t *image_get_fdt (ulong fdt_addr)
361 {
362 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
363
364 image_print_contents (fdt_hdr);
365
366 puts (" Verifying Checksum ... ");
367 if (!image_check_hcrc (fdt_hdr)) {
368 fdt_error ("fdt header checksum invalid");
369 return NULL;
370 }
371
372 if (!image_check_dcrc (fdt_hdr)) {
373 fdt_error ("fdt checksum invalid");
374 return NULL;
375 }
376 puts ("OK\n");
377
378 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
379 fdt_error ("uImage is not a fdt");
380 return NULL;
381 }
382 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
383 fdt_error ("uImage is compressed");
384 return NULL;
385 }
386 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
387 fdt_error ("uImage data is not a fdt");
388 return NULL;
389 }
390 return fdt_hdr;
391 }
392
393 /**
394 * fit_check_fdt - verify FIT format FDT subimage
395 * @fit_hdr: pointer to the FIT header
396 * fdt_noffset: FDT subimage node offset within FIT image
397 * @verify: data CRC verification flag
398 *
399 * fit_check_fdt() verifies integrity of the FDT subimage and from
400 * specified FIT image.
401 *
402 * returns:
403 * 1, on success
404 * 0, on failure
405 */
406 #if defined(CONFIG_FIT)
407 static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
408 {
409 fit_image_print (fit, fdt_noffset, " ");
410
411 if (verify) {
412 puts (" Verifying Hash Integrity ... ");
413 if (!fit_image_check_hashes (fit, fdt_noffset)) {
414 fdt_error ("Bad Data Hash");
415 return 0;
416 }
417 puts ("OK\n");
418 }
419
420 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
421 fdt_error ("Not a FDT image");
422 return 0;
423 }
424
425 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
426 fdt_error ("FDT image is compressed");
427 return 0;
428 }
429
430 return 1;
431 }
432 #endif /* CONFIG_FIT */
433
434 static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
435 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
436 {
437 ulong fdt_addr;
438 image_header_t *fdt_hdr;
439 char *fdt_blob = NULL;
440 ulong image_start, image_end;
441 ulong load_start, load_end;
442 #if defined(CONFIG_FIT)
443 void *fit_hdr;
444 const char *fit_uname_config = NULL;
445 const char *fit_uname_fdt = NULL;
446 ulong default_addr;
447 int cfg_noffset;
448 int fdt_noffset;
449 const void *data;
450 size_t size;
451 #endif
452
453 *of_flat_tree = NULL;
454 *of_size = 0;
455
456 if (argc > 3 || genimg_has_config (images)) {
457 #if defined(CONFIG_FIT)
458 if (argc > 3) {
459 /*
460 * If the FDT blob comes from the FIT image and the
461 * FIT image address is omitted in the command line
462 * argument, try to use ramdisk or os FIT image
463 * address or default load address.
464 */
465 if (images->fit_uname_rd)
466 default_addr = (ulong)images->fit_hdr_rd;
467 else if (images->fit_uname_os)
468 default_addr = (ulong)images->fit_hdr_os;
469 else
470 default_addr = load_addr;
471
472 if (fit_parse_conf (argv[3], default_addr,
473 &fdt_addr, &fit_uname_config)) {
474 debug ("* fdt: config '%s' from image at 0x%08lx\n",
475 fit_uname_config, fdt_addr);
476 } else if (fit_parse_subimage (argv[3], default_addr,
477 &fdt_addr, &fit_uname_fdt)) {
478 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
479 fit_uname_fdt, fdt_addr);
480 } else
481 #endif
482 {
483 fdt_addr = simple_strtoul(argv[3], NULL, 16);
484 debug ("* fdt: cmdline image address = 0x%08lx\n",
485 fdt_addr);
486 }
487 #if defined(CONFIG_FIT)
488 } else {
489 /* use FIT configuration provided in first bootm
490 * command argument
491 */
492 fdt_addr = (ulong)images->fit_hdr_os;
493 fit_uname_config = images->fit_uname_cfg;
494 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
495 fit_uname_config, fdt_addr);
496
497 /*
498 * Check whether configuration has FDT blob defined,
499 * if not quit silently.
500 */
501 fit_hdr = (void *)fdt_addr;
502 cfg_noffset = fit_conf_get_node (fit_hdr,
503 fit_uname_config);
504 if (cfg_noffset < 0) {
505 debug ("* fdt: no such config\n");
506 return 0;
507 }
508
509 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
510 cfg_noffset);
511 if (fdt_noffset < 0) {
512 debug ("* fdt: no fdt in config\n");
513 return 0;
514 }
515 }
516 #endif
517
518 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
519 fdt_addr);
520
521 /* copy from dataflash if needed */
522 fdt_addr = genimg_get_image (fdt_addr);
523
524 /*
525 * Check if there is an FDT image at the
526 * address provided in the second bootm argument
527 * check image type, for FIT images get a FIT node.
528 */
529 switch (genimg_get_format ((void *)fdt_addr)) {
530 case IMAGE_FORMAT_LEGACY:
531 /* verify fdt_addr points to a valid image header */
532 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
533 fdt_addr);
534 fdt_hdr = image_get_fdt (fdt_addr);
535 if (!fdt_hdr)
536 goto error;
537
538 /*
539 * move image data to the load address,
540 * make sure we don't overwrite initial image
541 */
542 image_start = (ulong)fdt_hdr;
543 image_end = image_get_image_end (fdt_hdr);
544
545 load_start = image_get_load (fdt_hdr);
546 load_end = load_start + image_get_data_size (fdt_hdr);
547
548 if ((load_start < image_end) && (load_end > image_start)) {
549 fdt_error ("fdt overwritten");
550 goto error;
551 }
552
553 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
554 image_get_data (fdt_hdr), load_start);
555
556 memmove ((void *)load_start,
557 (void *)image_get_data (fdt_hdr),
558 image_get_data_size (fdt_hdr));
559
560 fdt_blob = (char *)load_start;
561 break;
562 case IMAGE_FORMAT_FIT:
563 /*
564 * This case will catch both: new uImage format
565 * (libfdt based) and raw FDT blob (also libfdt
566 * based).
567 */
568 #if defined(CONFIG_FIT)
569 /* check FDT blob vs FIT blob */
570 if (fit_check_format ((const void *)fdt_addr)) {
571 /*
572 * FIT image
573 */
574 fit_hdr = (void *)fdt_addr;
575 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
576 fdt_addr);
577
578 if (!fit_uname_fdt) {
579 /*
580 * no FDT blob image node unit name,
581 * try to get config node first. If
582 * config unit node name is NULL
583 * fit_conf_get_node() will try to
584 * find default config node
585 */
586 cfg_noffset = fit_conf_get_node (fit_hdr,
587 fit_uname_config);
588
589 if (cfg_noffset < 0) {
590 fdt_error ("Could not find configuration node\n");
591 goto error;
592 }
593
594 fit_uname_config = fdt_get_name (fit_hdr,
595 cfg_noffset, NULL);
596 printf (" Using '%s' configuration\n",
597 fit_uname_config);
598
599 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
600 cfg_noffset);
601 fit_uname_fdt = fit_get_name (fit_hdr,
602 fdt_noffset, NULL);
603 } else {
604 /* get FDT component image node offset */
605 fdt_noffset = fit_image_get_node (fit_hdr,
606 fit_uname_fdt);
607 }
608 if (fdt_noffset < 0) {
609 fdt_error ("Could not find subimage node\n");
610 goto error;
611 }
612
613 printf (" Trying '%s' FDT blob subimage\n",
614 fit_uname_fdt);
615
616 if (!fit_check_fdt (fit_hdr, fdt_noffset,
617 images->verify))
618 goto error;
619
620 /* get ramdisk image data address and length */
621 if (fit_image_get_data (fit_hdr, fdt_noffset,
622 &data, &size)) {
623 fdt_error ("Could not find FDT subimage data");
624 goto error;
625 }
626
627 /* verift that image data is a proper FDT blob */
628 if (fdt_check_header ((char *)data) != 0) {
629 fdt_error ("Subimage data is not a FTD");
630 goto error;
631 }
632
633 /*
634 * move image data to the load address,
635 * make sure we don't overwrite initial image
636 */
637 image_start = (ulong)fit_hdr;
638 image_end = fit_get_end (fit_hdr);
639
640 if (fit_image_get_load (fit_hdr, fdt_noffset,
641 &load_start) == 0) {
642 load_end = load_start + size;
643
644 if ((load_start < image_end) &&
645 (load_end > image_start)) {
646 fdt_error ("FDT overwritten");
647 goto error;
648 }
649
650 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
651 (ulong)data, load_start);
652
653 memmove ((void *)load_start,
654 (void *)data, size);
655
656 fdt_blob = (char *)load_start;
657 } else {
658 fdt_blob = (char *)data;
659 }
660
661 images->fit_hdr_fdt = fit_hdr;
662 images->fit_uname_fdt = fit_uname_fdt;
663 images->fit_noffset_fdt = fdt_noffset;
664 break;
665 } else
666 #endif
667 {
668 /*
669 * FDT blob
670 */
671 fdt_blob = (char *)fdt_addr;
672 debug ("* fdt: raw FDT blob\n");
673 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
674 }
675 break;
676 default:
677 fdt_error ("Did not find a cmdline Flattened Device Tree");
678 goto error;
679 }
680
681 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
682
683 } else if (images->legacy_hdr_valid &&
684 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
685
686 ulong fdt_data, fdt_len;
687
688 /*
689 * Now check if we have a legacy multi-component image,
690 * get second entry data start address and len.
691 */
692 printf ("## Flattened Device Tree from multi "
693 "component Image at %08lX\n",
694 (ulong)images->legacy_hdr_os);
695
696 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
697 if (fdt_len) {
698
699 fdt_blob = (char *)fdt_data;
700 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
701
702 if (fdt_check_header (fdt_blob) != 0) {
703 fdt_error ("image is not a fdt");
704 goto error;
705 }
706
707 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
708 fdt_error ("fdt size != image size");
709 goto error;
710 }
711 } else {
712 debug ("## No Flattened Device Tree\n");
713 return 0;
714 }
715 } else {
716 debug ("## No Flattened Device Tree\n");
717 return 0;
718 }
719
720 *of_flat_tree = fdt_blob;
721 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
722 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
723 *of_flat_tree, *of_size);
724
725 return 0;
726
727 error:
728 do_reset (cmdtp, flag, argc, argv);
729 return 1;
730 }
731
732 static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
733 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
734 char **of_flat_tree, ulong *of_size)
735 {
736 char *fdt_blob = *of_flat_tree;
737 ulong relocate = 0;
738 ulong of_len = 0;
739
740 /* nothing to do */
741 if (*of_size == 0)
742 return 0;
743
744 if (fdt_check_header (fdt_blob) != 0) {
745 fdt_error ("image is not a fdt");
746 goto error;
747 }
748
749 #ifndef CFG_NO_FLASH
750 /* move the blob if it is in flash (set relocate) */
751 if (addr2info ((ulong)fdt_blob) != NULL)
752 relocate = 1;
753 #endif
754
755 /*
756 * The blob needs to be inside the boot mapping.
757 */
758 if (fdt_blob < (char *)bootmap_base)
759 relocate = 1;
760
761 if ((fdt_blob + *of_size + CFG_FDT_PAD) >=
762 ((char *)CFG_BOOTMAPSZ + bootmap_base))
763 relocate = 1;
764
765 /* move flattend device tree if needed */
766 if (relocate) {
767 int err;
768 ulong of_start = 0;
769
770 /* position on a 4K boundary before the alloc_current */
771 /* Pad the FDT by a specified amount */
772 of_len = *of_size + CFG_FDT_PAD;
773 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
774 (CFG_BOOTMAPSZ + bootmap_base));
775
776 if (of_start == 0) {
777 puts("device tree - allocation error\n");
778 goto error;
779 }
780
781 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
782 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
783 of_len, of_len);
784
785 printf (" Loading Device Tree to %08lx, end %08lx ... ",
786 of_start, of_start + of_len - 1);
787
788 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
789 if (err != 0) {
790 fdt_error ("fdt move failed");
791 goto error;
792 }
793 puts ("OK\n");
794
795 *of_flat_tree = (char *)of_start;
796 *of_size = of_len;
797 } else {
798 *of_flat_tree = fdt_blob;
799 of_len = (CFG_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
800 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
801 fdt_set_totalsize(*of_flat_tree, of_len);
802
803 *of_size = of_len;
804 }
805
806 return 0;
807
808 error:
809 return 1;
810 }
811 #endif