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,
34 #include <environment.h>
35 #include <asm/byteorder.h>
37 #if defined(CONFIG_OF_LIBFDT)
40 #include <fdt_support.h>
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);
50 #ifdef CFG_INIT_RAM_LOCK
51 #include <asm/cache.h>
55 #define CFG_FDT_PAD 0x3000
58 DECLARE_GLOBAL_DATA_PTR;
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);
65 #ifndef CFG_LINUX_LOWMEM_MAX_SIZE
66 #define CFG_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
69 void __attribute__((noinline))
70 do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
71 bootm_headers_t *images)
75 ulong initrd_start, initrd_end;
76 ulong rd_data_start, rd_data_end, rd_len;
78 phys_size_t bootm_size;
80 ulong cmd_start, cmd_end, bootmap_base;
83 void (*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
84 ulong r7, ulong r8, ulong r9);
87 struct lmb *lmb = images->lmb;
89 #if defined(CONFIG_OF_LIBFDT)
90 char *of_flat_tree = NULL;
93 bootmap_base = getenv_bootm_low();
94 bootm_size = getenv_bootm_size();
97 if (((u64)bootmap_base + bootm_size) >
98 (CFG_SDRAM_BASE + (u64)gd->ram_size))
99 puts("WARNING: bootm_low + bootm_size exceed total memory\n");
100 if ((bootmap_base + bootm_size) > get_effective_memsize())
101 puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
104 size = min(bootm_size, get_effective_memsize());
105 size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
107 if (size < bootm_size) {
108 ulong base = bootmap_base + size;
109 printf("WARNING: adjusting available memory to %lx\n", size);
110 lmb_reserve(lmb, base, bootm_size - size);
114 * Booting a (Linux) kernel image
116 * Allocate space for command line and board info - the
117 * address should be as high as possible within the reach of
118 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
119 * memory, which means far enough below the current stack
123 debug ("## Current stack ends at 0x%08lx\n", sp);
125 /* adjust sp by 1K to be safe */
127 lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
129 #if defined(CONFIG_OF_LIBFDT)
130 /* find flattened device tree */
131 ret = boot_get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
138 /* allocate space and init command line */
139 ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
141 puts("ERROR with allocation of cmdline\n");
145 /* allocate space for kernel copy of board info */
146 ret = boot_get_kbd (lmb, &kbd, bootmap_base);
148 puts("ERROR with allocation of kernel bd\n");
151 set_clocks_in_mhz(kbd);
154 /* find kernel entry point */
155 if (images->legacy_hdr_valid) {
156 ep = image_get_ep (&images->legacy_hdr_os_copy);
157 #if defined(CONFIG_FIT)
158 } else if (images->fit_uname_os) {
159 ret = fit_image_get_entry (images->fit_hdr_os,
160 images->fit_noffset_os, &ep);
162 puts ("Can't get entry point property!\n");
167 puts ("Could not find kernel entry point!\n");
170 kernel = (void (*)(bd_t *, ulong, ulong, ulong,
171 ulong, ulong, ulong))ep;
173 ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_PPC,
174 &rd_data_start, &rd_data_end);
178 rd_len = rd_data_end - rd_data_start;
180 #if defined(CONFIG_OF_LIBFDT)
181 ret = boot_relocate_fdt (lmb, bootmap_base,
182 cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
185 * Add the chosen node if it doesn't exist, add the env and bd_t
186 * if the user wants it (the logic is in the subroutines).
189 /* pass in dummy initrd info, we'll fix up later */
190 if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
191 fdt_error ("/chosen node create failed");
194 #ifdef CONFIG_OF_BOARD_SETUP
195 /* Call the board-specific fixup routine */
196 ft_board_setup(of_flat_tree, gd->bd);
200 /* Fixup the fdt memreserve now that we know how big it is */
204 int total = fdt_num_mem_rsv(of_flat_tree);
207 for (j = 0; j < total; j++) {
208 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
209 if (addr == (uint64_t)(u32)of_flat_tree) {
210 fdt_del_mem_rsv(of_flat_tree, j);
215 /* Delete the old LMB reservation */
216 lmb_free(lmb, (phys_addr_t)(u32)of_flat_tree,
217 (phys_size_t)fdt_totalsize(of_flat_tree));
219 /* Calculate the actual size of the fdt */
220 actualsize = fdt_off_dt_strings(of_flat_tree) +
221 fdt_size_dt_strings(of_flat_tree);
223 /* Make it so the fdt ends on a page boundary */
224 actualsize = ALIGN(actualsize, 0x1000);
225 actualsize = actualsize - ((uint)of_flat_tree & 0xfff);
227 /* Change the fdt header to reflect the correct size */
228 fdt_set_totalsize(of_flat_tree, actualsize);
229 of_size = actualsize;
231 /* Add the new reservation */
232 ret = fdt_add_mem_rsv(of_flat_tree, (uint)of_flat_tree,
235 /* Create a new LMB reservation */
236 lmb_reserve(lmb, (ulong)of_flat_tree, of_size);
238 #endif /* CONFIG_OF_LIBFDT */
240 ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
244 #if defined(CONFIG_OF_LIBFDT)
245 /* fixup the initrd now that we know where it should be */
246 if ((of_flat_tree) && (initrd_start && initrd_end)) {
248 int total = fdt_num_mem_rsv(of_flat_tree);
251 /* Look for the dummy entry and delete it */
252 for (j = 0; j < total; j++) {
253 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
254 if (addr == rd_data_start) {
255 fdt_del_mem_rsv(of_flat_tree, j);
260 ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
261 initrd_end - initrd_start + 1);
263 printf("fdt_chosen: %s\n", fdt_strerror(ret));
267 do_fixup_by_path_u32(of_flat_tree, "/chosen",
268 "linux,initrd-start", initrd_start, 0);
269 do_fixup_by_path_u32(of_flat_tree, "/chosen",
270 "linux,initrd-end", initrd_end, 0);
273 debug ("## Transferring control to Linux (at address %08lx) ...\n",
276 show_boot_progress (15);
278 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
279 unlock_ram_in_cache();
282 #if defined(CONFIG_OF_LIBFDT)
283 if (of_flat_tree) { /* device tree; boot new style */
285 * Linux Kernel Parameters (passing device tree):
286 * r3: pointer to the fdt
290 * r7: size of IMA in bytes
294 #if defined(CONFIG_85xx) || defined(CONFIG_440)
295 #define EPAPR_MAGIC (0x45504150)
297 #define EPAPR_MAGIC (0x65504150)
300 debug (" Booting using OF flat tree...\n");
301 (*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
302 CFG_BOOTMAPSZ, 0, 0);
303 /* does not return */
308 * Linux Kernel Parameters (passing board info data):
309 * r3: ptr to board info data
310 * r4: initrd_start or 0 if no initrd
311 * r5: initrd_end - unused if r4 is 0
312 * r6: Start of command line string
313 * r7: End of command line string
317 debug (" Booting using board info...\n");
318 (*kernel) (kbd, initrd_start, initrd_end,
319 cmd_start, cmd_end, 0, 0);
320 /* does not return */
325 do_reset (cmdtp, flag, argc, argv);
329 static ulong get_sp (void)
333 asm( "mr %0,1": "=r"(sp) : );
337 static void set_clocks_in_mhz (bd_t *kbd)
341 if ((s = getenv ("clocks_in_mhz")) != NULL) {
342 /* convert all clock information to MHz */
343 kbd->bi_intfreq /= 1000000L;
344 kbd->bi_busfreq /= 1000000L;
345 #if defined(CONFIG_MPC8220)
346 kbd->bi_inpfreq /= 1000000L;
347 kbd->bi_pcifreq /= 1000000L;
348 kbd->bi_pevfreq /= 1000000L;
349 kbd->bi_flbfreq /= 1000000L;
350 kbd->bi_vcofreq /= 1000000L;
352 #if defined(CONFIG_CPM2)
353 kbd->bi_cpmfreq /= 1000000L;
354 kbd->bi_brgfreq /= 1000000L;
355 kbd->bi_sccfreq /= 1000000L;
356 kbd->bi_vco /= 1000000L;
358 #if defined(CONFIG_MPC5xxx)
359 kbd->bi_ipbfreq /= 1000000L;
360 kbd->bi_pcifreq /= 1000000L;
361 #endif /* CONFIG_MPC5xxx */
365 #if defined(CONFIG_OF_LIBFDT)
366 static void fdt_error (const char *msg)
370 puts (" - must RESET the board to recover.\n");
373 static image_header_t *image_get_fdt (ulong fdt_addr)
375 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
377 image_print_contents (fdt_hdr);
379 puts (" Verifying Checksum ... ");
380 if (!image_check_hcrc (fdt_hdr)) {
381 fdt_error ("fdt header checksum invalid");
385 if (!image_check_dcrc (fdt_hdr)) {
386 fdt_error ("fdt checksum invalid");
391 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
392 fdt_error ("uImage is not a fdt");
395 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
396 fdt_error ("uImage is compressed");
399 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
400 fdt_error ("uImage data is not a fdt");
407 * fit_check_fdt - verify FIT format FDT subimage
408 * @fit_hdr: pointer to the FIT header
409 * fdt_noffset: FDT subimage node offset within FIT image
410 * @verify: data CRC verification flag
412 * fit_check_fdt() verifies integrity of the FDT subimage and from
413 * specified FIT image.
419 #if defined(CONFIG_FIT)
420 static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
422 fit_image_print (fit, fdt_noffset, " ");
425 puts (" Verifying Hash Integrity ... ");
426 if (!fit_image_check_hashes (fit, fdt_noffset)) {
427 fdt_error ("Bad Data Hash");
433 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
434 fdt_error ("Not a FDT image");
438 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
439 fdt_error ("FDT image is compressed");
445 #endif /* CONFIG_FIT */
447 static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
448 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
451 image_header_t *fdt_hdr;
452 char *fdt_blob = NULL;
453 ulong image_start, image_end;
454 ulong load_start, load_end;
455 #if defined(CONFIG_FIT)
457 const char *fit_uname_config = NULL;
458 const char *fit_uname_fdt = NULL;
466 *of_flat_tree = NULL;
469 if (argc > 3 || genimg_has_config (images)) {
470 #if defined(CONFIG_FIT)
473 * If the FDT blob comes from the FIT image and the
474 * FIT image address is omitted in the command line
475 * argument, try to use ramdisk or os FIT image
476 * address or default load address.
478 if (images->fit_uname_rd)
479 default_addr = (ulong)images->fit_hdr_rd;
480 else if (images->fit_uname_os)
481 default_addr = (ulong)images->fit_hdr_os;
483 default_addr = load_addr;
485 if (fit_parse_conf (argv[3], default_addr,
486 &fdt_addr, &fit_uname_config)) {
487 debug ("* fdt: config '%s' from image at 0x%08lx\n",
488 fit_uname_config, fdt_addr);
489 } else if (fit_parse_subimage (argv[3], default_addr,
490 &fdt_addr, &fit_uname_fdt)) {
491 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
492 fit_uname_fdt, fdt_addr);
496 fdt_addr = simple_strtoul(argv[3], NULL, 16);
497 debug ("* fdt: cmdline image address = 0x%08lx\n",
500 #if defined(CONFIG_FIT)
502 /* use FIT configuration provided in first bootm
505 fdt_addr = (ulong)images->fit_hdr_os;
506 fit_uname_config = images->fit_uname_cfg;
507 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
508 fit_uname_config, fdt_addr);
511 * Check whether configuration has FDT blob defined,
512 * if not quit silently.
514 fit_hdr = (void *)fdt_addr;
515 cfg_noffset = fit_conf_get_node (fit_hdr,
517 if (cfg_noffset < 0) {
518 debug ("* fdt: no such config\n");
522 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
524 if (fdt_noffset < 0) {
525 debug ("* fdt: no fdt in config\n");
531 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
534 /* copy from dataflash if needed */
535 fdt_addr = genimg_get_image (fdt_addr);
538 * Check if there is an FDT image at the
539 * address provided in the second bootm argument
540 * check image type, for FIT images get a FIT node.
542 switch (genimg_get_format ((void *)fdt_addr)) {
543 case IMAGE_FORMAT_LEGACY:
544 /* verify fdt_addr points to a valid image header */
545 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
547 fdt_hdr = image_get_fdt (fdt_addr);
552 * move image data to the load address,
553 * make sure we don't overwrite initial image
555 image_start = (ulong)fdt_hdr;
556 image_end = image_get_image_end (fdt_hdr);
558 load_start = image_get_load (fdt_hdr);
559 load_end = load_start + image_get_data_size (fdt_hdr);
561 if ((load_start < image_end) && (load_end > image_start)) {
562 fdt_error ("fdt overwritten");
566 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
567 image_get_data (fdt_hdr), load_start);
569 memmove ((void *)load_start,
570 (void *)image_get_data (fdt_hdr),
571 image_get_data_size (fdt_hdr));
573 fdt_blob = (char *)load_start;
575 case IMAGE_FORMAT_FIT:
577 * This case will catch both: new uImage format
578 * (libfdt based) and raw FDT blob (also libfdt
581 #if defined(CONFIG_FIT)
582 /* check FDT blob vs FIT blob */
583 if (fit_check_format ((const void *)fdt_addr)) {
587 fit_hdr = (void *)fdt_addr;
588 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
591 if (!fit_uname_fdt) {
593 * no FDT blob image node unit name,
594 * try to get config node first. If
595 * config unit node name is NULL
596 * fit_conf_get_node() will try to
597 * find default config node
599 cfg_noffset = fit_conf_get_node (fit_hdr,
602 if (cfg_noffset < 0) {
603 fdt_error ("Could not find configuration node\n");
607 fit_uname_config = fdt_get_name (fit_hdr,
609 printf (" Using '%s' configuration\n",
612 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
614 fit_uname_fdt = fit_get_name (fit_hdr,
617 /* get FDT component image node offset */
618 fdt_noffset = fit_image_get_node (fit_hdr,
621 if (fdt_noffset < 0) {
622 fdt_error ("Could not find subimage node\n");
626 printf (" Trying '%s' FDT blob subimage\n",
629 if (!fit_check_fdt (fit_hdr, fdt_noffset,
633 /* get ramdisk image data address and length */
634 if (fit_image_get_data (fit_hdr, fdt_noffset,
636 fdt_error ("Could not find FDT subimage data");
640 /* verift that image data is a proper FDT blob */
641 if (fdt_check_header ((char *)data) != 0) {
642 fdt_error ("Subimage data is not a FTD");
647 * move image data to the load address,
648 * make sure we don't overwrite initial image
650 image_start = (ulong)fit_hdr;
651 image_end = fit_get_end (fit_hdr);
653 if (fit_image_get_load (fit_hdr, fdt_noffset,
655 load_end = load_start + size;
657 if ((load_start < image_end) &&
658 (load_end > image_start)) {
659 fdt_error ("FDT overwritten");
663 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
664 (ulong)data, load_start);
666 memmove ((void *)load_start,
669 fdt_blob = (char *)load_start;
671 fdt_blob = (char *)data;
674 images->fit_hdr_fdt = fit_hdr;
675 images->fit_uname_fdt = fit_uname_fdt;
676 images->fit_noffset_fdt = fdt_noffset;
684 fdt_blob = (char *)fdt_addr;
685 debug ("* fdt: raw FDT blob\n");
686 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
690 fdt_error ("Did not find a cmdline Flattened Device Tree");
694 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
696 } else if (images->legacy_hdr_valid &&
697 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
699 ulong fdt_data, fdt_len;
702 * Now check if we have a legacy multi-component image,
703 * get second entry data start address and len.
705 printf ("## Flattened Device Tree from multi "
706 "component Image at %08lX\n",
707 (ulong)images->legacy_hdr_os);
709 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
712 fdt_blob = (char *)fdt_data;
713 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
715 if (fdt_check_header (fdt_blob) != 0) {
716 fdt_error ("image is not a fdt");
720 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
721 fdt_error ("fdt size != image size");
725 debug ("## No Flattened Device Tree\n");
729 debug ("## No Flattened Device Tree\n");
733 *of_flat_tree = fdt_blob;
734 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
735 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
736 *of_flat_tree, *of_size);
741 do_reset (cmdtp, flag, argc, argv);
745 static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
746 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
747 char **of_flat_tree, ulong *of_size)
749 char *fdt_blob = *of_flat_tree;
757 if (fdt_check_header (fdt_blob) != 0) {
758 fdt_error ("image is not a fdt");
763 /* move the blob if it is in flash (set relocate) */
764 if (addr2info ((ulong)fdt_blob) != NULL)
769 * The blob needs to be inside the boot mapping.
771 if (fdt_blob < (char *)bootmap_base)
774 if ((fdt_blob + *of_size + CFG_FDT_PAD) >=
775 ((char *)CFG_BOOTMAPSZ + bootmap_base))
778 /* move flattend device tree if needed */
783 /* position on a 4K boundary before the alloc_current */
784 /* Pad the FDT by a specified amount */
785 of_len = *of_size + CFG_FDT_PAD;
786 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
787 (CFG_BOOTMAPSZ + bootmap_base));
790 puts("device tree - allocation error\n");
794 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
795 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
798 printf (" Loading Device Tree to %08lx, end %08lx ... ",
799 of_start, of_start + of_len - 1);
801 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
803 fdt_error ("fdt move failed");
808 *of_flat_tree = (char *)of_start;
811 *of_flat_tree = fdt_blob;
812 of_len = (CFG_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
813 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
814 fdt_set_totalsize(*of_flat_tree, of_len);