2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
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.
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.
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,
35 #include <environment.h>
36 #include <asm/byteorder.h>
38 #if defined(CONFIG_OF_LIBFDT)
41 #include <fdt_support.h>
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
);
51 #ifdef CFG_INIT_RAM_LOCK
52 #include <asm/cache.h>
55 DECLARE_GLOBAL_DATA_PTR
;
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
);
62 #ifndef CFG_LINUX_LOWMEM_MAX_SIZE
63 #define CFG_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
66 void __attribute__((noinline
))
67 do_bootm_linux(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[],
68 bootm_headers_t
*images
)
72 ulong initrd_start
, initrd_end
;
73 ulong rd_data_start
, rd_data_end
, rd_len
;
76 ulong cmd_start
, cmd_end
, bootmap_base
;
79 void (*kernel
)(bd_t
*, ulong
, ulong
, ulong
, ulong
);
82 struct lmb
*lmb
= images
->lmb
;
84 #if defined(CONFIG_OF_LIBFDT)
85 char *of_flat_tree
= NULL
;
88 bootmap_base
= getenv_bootm_low();
89 size
= getenv_bootm_size();
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");
98 size
= min(size
, get_effective_memsize());
99 size
= min(size
, CFG_LINUX_LOWMEM_MAX_SIZE
);
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
);
108 * Booting a (Linux) kernel image
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
117 debug ("## Current stack ends at 0x%08lx\n", sp
);
119 /* adjust sp by 1K to be safe */
121 lmb_reserve(lmb
, sp
, (CFG_SDRAM_BASE
+ get_effective_memsize() - sp
));
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
);
132 /* allocate space and init command line */
133 ret
= boot_get_cmdline (lmb
, &cmd_start
, &cmd_end
, bootmap_base
);
135 puts("ERROR with allocation of cmdline\n");
139 /* allocate space for kernel copy of board info */
140 ret
= boot_get_kbd (lmb
, &kbd
, bootmap_base
);
142 puts("ERROR with allocation of kernel bd\n");
145 set_clocks_in_mhz(kbd
);
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 fit_unsupported_reset ("PPC linux bootm");
157 puts ("Could not find kernel entry point!\n");
160 kernel
= (void (*)(bd_t
*, ulong
, ulong
, ulong
, ulong
))ep
;
163 ret
= boot_get_ramdisk (argc
, argv
, images
, IH_ARCH_PPC
,
164 &rd_data_start
, &rd_data_end
);
168 rd_len
= rd_data_end
- rd_data_start
;
170 #if defined(CONFIG_OF_LIBFDT)
171 ret
= boot_relocate_fdt (lmb
, bootmap_base
,
172 cmdtp
, flag
, argc
, argv
, &of_flat_tree
, &of_size
);
175 * Add the chosen node if it doesn't exist, add the env and bd_t
176 * if the user wants it (the logic is in the subroutines).
179 /* pass in dummy initrd info, we'll fix up later */
180 if (fdt_chosen(of_flat_tree
, rd_data_start
, rd_data_end
, 0) < 0) {
181 fdt_error ("/chosen node create failed");
184 #ifdef CONFIG_OF_HAS_UBOOT_ENV
185 if (fdt_env(of_flat_tree
) < 0) {
186 fdt_error ("/u-boot-env node create failed");
190 #ifdef CONFIG_OF_HAS_BD_T
191 if (fdt_bd_t(of_flat_tree
) < 0) {
192 fdt_error ("/bd_t node create failed");
196 #ifdef CONFIG_OF_BOARD_SETUP
197 /* Call the board-specific fixup routine */
198 ft_board_setup(of_flat_tree
, gd
->bd
);
201 #endif /* CONFIG_OF_LIBFDT */
203 ret
= boot_ramdisk_high (lmb
, rd_data_start
, rd_len
, &initrd_start
, &initrd_end
);
207 #if defined(CONFIG_OF_LIBFDT)
208 /* fixup the initrd now that we know where it should be */
209 if ((of_flat_tree
) && (initrd_start
&& initrd_end
)) {
211 int total
= fdt_num_mem_rsv(of_flat_tree
);
214 /* Look for the dummy entry and delete it */
215 for (j
= 0; j
< total
; j
++) {
216 fdt_get_mem_rsv(of_flat_tree
, j
, &addr
, &size
);
217 if (addr
== rd_data_start
) {
218 fdt_del_mem_rsv(of_flat_tree
, j
);
223 ret
= fdt_add_mem_rsv(of_flat_tree
, initrd_start
,
224 initrd_end
- initrd_start
+ 1);
226 printf("fdt_chosen: %s\n", fdt_strerror(ret
));
230 do_fixup_by_path_u32(of_flat_tree
, "/chosen",
231 "linux,initrd-start", initrd_start
, 0);
232 do_fixup_by_path_u32(of_flat_tree
, "/chosen",
233 "linux,initrd-end", initrd_end
, 0);
236 debug ("## Transferring control to Linux (at address %08lx) ...\n",
239 show_boot_progress (15);
241 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
242 unlock_ram_in_cache();
244 if (!images
->autostart
)
247 #if defined(CONFIG_OF_LIBFDT)
248 if (of_flat_tree
) { /* device tree; boot new style */
250 * Linux Kernel Parameters (passing device tree):
251 * r3: pointer to the fdt, followed by the board info data
252 * r4: physical pointer to the kernel itself
257 debug (" Booting using OF flat tree...\n");
258 (*kernel
) ((bd_t
*)of_flat_tree
, (ulong
)kernel
, 0, 0, 0);
259 /* does not return */
264 * Linux Kernel Parameters (passing board info data):
265 * r3: ptr to board info data
266 * r4: initrd_start or 0 if no initrd
267 * r5: initrd_end - unused if r4 is 0
268 * r6: Start of command line string
269 * r7: End of command line string
271 debug (" Booting using board info...\n");
272 (*kernel
) (kbd
, initrd_start
, initrd_end
, cmd_start
, cmd_end
);
273 /* does not return */
278 if (images
->autostart
)
279 do_reset (cmdtp
, flag
, argc
, argv
);
283 static ulong
get_sp (void)
287 asm( "mr %0,1": "=r"(sp
) : );
291 static void set_clocks_in_mhz (bd_t
*kbd
)
295 if ((s
= getenv ("clocks_in_mhz")) != NULL
) {
296 /* convert all clock information to MHz */
297 kbd
->bi_intfreq
/= 1000000L;
298 kbd
->bi_busfreq
/= 1000000L;
299 #if defined(CONFIG_MPC8220)
300 kbd
->bi_inpfreq
/= 1000000L;
301 kbd
->bi_pcifreq
/= 1000000L;
302 kbd
->bi_pevfreq
/= 1000000L;
303 kbd
->bi_flbfreq
/= 1000000L;
304 kbd
->bi_vcofreq
/= 1000000L;
306 #if defined(CONFIG_CPM2)
307 kbd
->bi_cpmfreq
/= 1000000L;
308 kbd
->bi_brgfreq
/= 1000000L;
309 kbd
->bi_sccfreq
/= 1000000L;
310 kbd
->bi_vco
/= 1000000L;
312 #if defined(CONFIG_MPC5xxx)
313 kbd
->bi_ipbfreq
/= 1000000L;
314 kbd
->bi_pcifreq
/= 1000000L;
315 #endif /* CONFIG_MPC5xxx */
319 #if defined(CONFIG_OF_LIBFDT)
320 static void fdt_error (const char *msg
)
324 puts (" - must RESET the board to recover.\n");
327 static image_header_t
*image_get_fdt (ulong fdt_addr
)
329 image_header_t
*fdt_hdr
= (image_header_t
*)fdt_addr
;
331 image_print_contents (fdt_hdr
);
333 puts (" Verifying Checksum ... ");
334 if (!image_check_hcrc (fdt_hdr
)) {
335 fdt_error ("fdt header checksum invalid");
339 if (!image_check_dcrc (fdt_hdr
)) {
340 fdt_error ("fdt checksum invalid");
345 if (!image_check_type (fdt_hdr
, IH_TYPE_FLATDT
)) {
346 fdt_error ("uImage is not a fdt");
349 if (image_get_comp (fdt_hdr
) != IH_COMP_NONE
) {
350 fdt_error ("uImage is compressed");
353 if (fdt_check_header ((char *)image_get_data (fdt_hdr
)) != 0) {
354 fdt_error ("uImage data is not a fdt");
361 * fit_check_fdt - verify FIT format FDT subimage
362 * @fit_hdr: pointer to the FIT header
363 * fdt_noffset: FDT subimage node offset within FIT image
364 * @verify: data CRC verification flag
366 * fit_check_fdt() verifies integrity of the FDT subimage and from
367 * specified FIT image.
373 #if defined(CONFIG_FIT)
374 static int fit_check_fdt (const void *fit
, int fdt_noffset
, int verify
)
376 fit_image_print (fit
, fdt_noffset
, " ");
379 puts (" Verifying Hash Integrity ... ");
380 if (!fit_image_check_hashes (fit
, fdt_noffset
)) {
381 fdt_error ("Bad Data Hash");
387 if (!fit_image_check_type (fit
, fdt_noffset
, IH_TYPE_FLATDT
)) {
388 fdt_error ("Not a FDT image");
392 if (!fit_image_check_comp (fit
, fdt_noffset
, IH_COMP_NONE
)) {
393 fdt_error ("FDT image is compressed");
399 #endif /* CONFIG_FIT */
401 static int boot_get_fdt (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[],
402 bootm_headers_t
*images
, char **of_flat_tree
, ulong
*of_size
)
405 image_header_t
*fdt_hdr
;
406 char *fdt_blob
= NULL
;
407 ulong image_start
, image_end
;
408 ulong load_start
, load_end
;
409 #if defined(CONFIG_FIT)
411 const char *fit_uname_config
= NULL
;
412 const char *fit_uname_fdt
= NULL
;
420 *of_flat_tree
= NULL
;
424 #if defined(CONFIG_FIT)
426 * If the FDT blob comes from the FIT image and the FIT image
427 * address is omitted in the command line argument, try to use
428 * ramdisk or os FIT image address or default load address.
430 if (images
->fit_uname_rd
)
431 default_addr
= (ulong
)images
->fit_hdr_rd
;
432 else if (images
->fit_uname_os
)
433 default_addr
= (ulong
)images
->fit_hdr_os
;
435 default_addr
= load_addr
;
437 if (fit_parse_conf (argv
[3], default_addr
,
438 &fdt_addr
, &fit_uname_config
)) {
439 debug ("* fdt: config '%s' from image at 0x%08lx\n",
440 fit_uname_config
, fdt_addr
);
441 } else if (fit_parse_subimage (argv
[3], default_addr
,
442 &fdt_addr
, &fit_uname_fdt
)) {
443 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
444 fit_uname_fdt
, fdt_addr
);
448 fdt_addr
= simple_strtoul(argv
[3], NULL
, 16);
449 debug ("* fdt: cmdline image address = 0x%08lx\n",
453 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
456 /* copy from dataflash if needed */
457 fdt_addr
= genimg_get_image (fdt_addr
);
460 * Check if there is an FDT image at the
461 * address provided in the second bootm argument
462 * check image type, for FIT images get a FIT node.
464 switch (genimg_get_format ((void *)fdt_addr
)) {
465 case IMAGE_FORMAT_LEGACY
:
466 /* verify fdt_addr points to a valid image header */
467 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
469 fdt_hdr
= image_get_fdt (fdt_addr
);
474 * move image data to the load address,
475 * make sure we don't overwrite initial image
477 image_start
= (ulong
)fdt_hdr
;
478 image_end
= image_get_image_end (fdt_hdr
);
480 load_start
= image_get_load (fdt_hdr
);
481 load_end
= load_start
+ image_get_data_size (fdt_hdr
);
483 if ((load_start
< image_end
) && (load_end
> image_start
)) {
484 fdt_error ("fdt overwritten");
488 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
489 image_get_data (fdt_hdr
), load_start
);
491 memmove ((void *)load_start
,
492 (void *)image_get_data (fdt_hdr
),
493 image_get_data_size (fdt_hdr
));
495 fdt_blob
= (char *)load_start
;
497 case IMAGE_FORMAT_FIT
:
499 * This case will catch both: new uImage format
500 * (libfdt based) and raw FDT blob (also libfdt
503 #if defined(CONFIG_FIT)
504 /* check FDT blob vs FIT blob */
505 if (fit_check_format ((const void *)fdt_addr
)) {
509 fit_hdr
= (void *)fdt_addr
;
510 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
513 if (!fit_uname_fdt
) {
515 * no FDT blob image node unit name,
516 * try to get config node first. If
517 * config unit node name is NULL
518 * fit_conf_get_node() will try to
519 * find default config node
521 conf_noffset
= fit_conf_get_node (fit_hdr
,
523 if (conf_noffset
< 0)
526 fdt_noffset
= fit_conf_get_fdt_node (fit_hdr
,
528 fit_uname_fdt
= fit_get_name (fit_hdr
,
531 /* get FDT component image node offset */
532 fdt_noffset
= fit_image_get_node (fit_hdr
,
538 printf (" Trying '%s' FDT blob subimage\n",
541 if (!fit_check_fdt (fit_hdr
, fdt_noffset
,
545 /* get ramdisk image data address and length */
546 if (fit_image_get_data (fit_hdr
, fdt_noffset
,
548 fdt_error ("Could not find FDT subimage data");
552 /* verift that image data is a proper FDT blob */
553 if (fdt_check_header ((char *)data
) != 0) {
554 fdt_error ("Subimage data is not a FTD");
559 * move image data to the load address,
560 * make sure we don't overwrite initial image
562 image_start
= (ulong
)fit_hdr
;
563 image_end
= fit_get_end (fit_hdr
);
565 if (fit_image_get_load (fit_hdr
, fdt_noffset
,
567 load_end
= load_start
+ size
;
569 if ((load_start
< image_end
) &&
570 (load_end
> image_start
)) {
571 fdt_error ("FDT overwritten");
575 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
576 (ulong
)data
, load_start
);
578 memmove ((void *)load_start
,
581 fdt_blob
= (char *)load_start
;
583 fdt_blob
= (char *)data
;
586 images
->fit_hdr_fdt
= fit_hdr
;
587 images
->fit_uname_fdt
= fit_uname_fdt
;
588 images
->fit_noffset_fdt
= fdt_noffset
;
596 debug ("* fdt: raw FDT blob\n");
597 printf ("## Flattened Device Tree blob at %08lx\n", fdt_blob
);
598 fdt_blob
= (char *)fdt_addr
;
602 fdt_error ("Did not find a cmdline Flattened Device Tree");
606 printf (" Booting using the fdt blob at 0x%x\n", fdt_blob
);
608 } else if (images
->legacy_hdr_valid
&&
609 image_check_type (images
->legacy_hdr_os
, IH_TYPE_MULTI
)) {
611 ulong fdt_data
, fdt_len
;
614 * Now check if we have a legacy multi-component image,
615 * get second entry data start address and len.
617 printf ("## Flattened Device Tree from multi "
618 "component Image at %08lX\n",
619 (ulong
)images
->legacy_hdr_os
);
621 image_multi_getimg (images
->legacy_hdr_os
, 2, &fdt_data
, &fdt_len
);
624 fdt_blob
= (char *)fdt_data
;
625 printf (" Booting using the fdt at 0x%x\n", fdt_blob
);
627 if (fdt_check_header (fdt_blob
) != 0) {
628 fdt_error ("image is not a fdt");
632 if (be32_to_cpu (fdt_totalsize (fdt_blob
)) != fdt_len
) {
633 fdt_error ("fdt size != image size");
637 fdt_error ("Did not find a Flattened Device Tree "
638 "in a legacy multi-component image");
642 debug ("## No Flattened Device Tree\n");
646 *of_flat_tree
= fdt_blob
;
647 *of_size
= be32_to_cpu (fdt_totalsize (fdt_blob
));
648 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
649 *of_flat_tree
, *of_size
);
654 do_reset (cmdtp
, flag
, argc
, argv
);
658 static int boot_relocate_fdt (struct lmb
*lmb
, ulong bootmap_base
,
659 cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[],
660 char **of_flat_tree
, ulong
*of_size
)
662 char *fdt_blob
= *of_flat_tree
;
670 if (fdt_check_header (fdt_blob
) != 0) {
671 fdt_error ("image is not a fdt");
676 /* move the blob if it is in flash (set relocate) */
677 if (addr2info ((ulong
)fdt_blob
) != NULL
)
682 * The blob must be within CFG_BOOTMAPSZ,
683 * so we flag it to be copied if it is not.
685 if (fdt_blob
>= (char *)CFG_BOOTMAPSZ
)
688 of_len
= be32_to_cpu (fdt_totalsize (fdt_blob
));
690 /* move flattend device tree if needed */
695 /* position on a 4K boundary before the alloc_current */
696 of_start
= lmb_alloc_base(lmb
, of_len
, 0x1000,
697 (CFG_BOOTMAPSZ
+ bootmap_base
));
700 puts("device tree - allocation error\n");
704 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
705 (ulong
)fdt_blob
, (ulong
)fdt_blob
+ of_len
- 1,
708 printf (" Loading Device Tree to %08lx, end %08lx ... ",
709 of_start
, of_start
+ of_len
- 1);
711 err
= fdt_open_into (fdt_blob
, (void *)of_start
, of_len
);
713 fdt_error ("fdt move failed");
718 *of_flat_tree
= (char *)of_start
;
720 *of_flat_tree
= fdt_blob
;
721 lmb_reserve(lmb
, (ulong
)fdt
, of_len
);