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