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, ulong, ulong, ulong);
86 struct lmb *lmb = images->lmb;
88 #if defined(CONFIG_OF_LIBFDT)
89 char *of_flat_tree = NULL;
92 bootmap_base = getenv_bootm_low();
93 bootm_size = getenv_bootm_size();
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");
103 size = min(bootm_size, get_effective_memsize());
104 size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
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);
113 * Booting a (Linux) kernel image
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
122 debug ("## Current stack ends at 0x%08lx\n", sp);
124 /* adjust sp by 1K to be safe */
126 lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
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);
137 /* allocate space and init command line */
138 ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
140 puts("ERROR with allocation of cmdline\n");
144 /* allocate space for kernel copy of board info */
145 ret = boot_get_kbd (lmb, &kbd, bootmap_base);
147 puts("ERROR with allocation of kernel bd\n");
150 set_clocks_in_mhz(kbd);
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);
161 puts ("Can't get entry point property!\n");
166 puts ("Could not find kernel entry point!\n");
169 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
172 ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_PPC,
173 &rd_data_start, &rd_data_end);
177 rd_len = rd_data_end - rd_data_start;
179 #if defined(CONFIG_OF_LIBFDT)
180 ret = boot_relocate_fdt (lmb, bootmap_base,
181 cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
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).
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");
193 #ifdef CONFIG_OF_BOARD_SETUP
194 /* Call the board-specific fixup routine */
195 ft_board_setup(of_flat_tree, gd->bd);
199 /* Fixup the fdt memreserve now that we know how big it is */
203 int total = fdt_num_mem_rsv(of_flat_tree);
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);
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));
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);
222 /* Make it so the fdt ends on a page boundary */
223 actualsize = ALIGN(actualsize, 0x1000);
224 actualsize = actualsize - ((uint)of_flat_tree & 0xfff);
226 /* Change the fdt header to reflect the correct size */
227 fdt_set_totalsize(of_flat_tree, actualsize);
228 of_size = actualsize;
230 /* Add the new reservation */
231 ret = fdt_add_mem_rsv(of_flat_tree, (uint)of_flat_tree,
234 /* Create a new LMB reservation */
235 lmb_reserve(lmb, (ulong)of_flat_tree, of_size);
237 #endif /* CONFIG_OF_LIBFDT */
239 ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
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)) {
247 int total = fdt_num_mem_rsv(of_flat_tree);
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);
259 ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
260 initrd_end - initrd_start + 1);
262 printf("fdt_chosen: %s\n", fdt_strerror(ret));
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);
272 debug ("## Transferring control to Linux (at address %08lx) ...\n",
275 show_boot_progress (15);
277 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
278 unlock_ram_in_cache();
280 if (!images->autostart)
283 #if defined(CONFIG_OF_LIBFDT)
284 if (of_flat_tree) { /* device tree; boot new style */
286 * Linux Kernel Parameters (passing device tree):
287 * r3: pointer to the fdt, followed by the board info data
288 * r4: physical pointer to the kernel itself
293 debug (" Booting using OF flat tree...\n");
294 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
295 /* does not return */
300 * Linux Kernel Parameters (passing board info data):
301 * r3: ptr to board info data
302 * r4: initrd_start or 0 if no initrd
303 * r5: initrd_end - unused if r4 is 0
304 * r6: Start of command line string
305 * r7: End of command line string
307 debug (" Booting using board info...\n");
308 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
309 /* does not return */
314 if (images->autostart)
315 do_reset (cmdtp, flag, argc, argv);
319 static ulong get_sp (void)
323 asm( "mr %0,1": "=r"(sp) : );
327 static void set_clocks_in_mhz (bd_t *kbd)
331 if ((s = getenv ("clocks_in_mhz")) != NULL) {
332 /* convert all clock information to MHz */
333 kbd->bi_intfreq /= 1000000L;
334 kbd->bi_busfreq /= 1000000L;
335 #if defined(CONFIG_MPC8220)
336 kbd->bi_inpfreq /= 1000000L;
337 kbd->bi_pcifreq /= 1000000L;
338 kbd->bi_pevfreq /= 1000000L;
339 kbd->bi_flbfreq /= 1000000L;
340 kbd->bi_vcofreq /= 1000000L;
342 #if defined(CONFIG_CPM2)
343 kbd->bi_cpmfreq /= 1000000L;
344 kbd->bi_brgfreq /= 1000000L;
345 kbd->bi_sccfreq /= 1000000L;
346 kbd->bi_vco /= 1000000L;
348 #if defined(CONFIG_MPC5xxx)
349 kbd->bi_ipbfreq /= 1000000L;
350 kbd->bi_pcifreq /= 1000000L;
351 #endif /* CONFIG_MPC5xxx */
355 #if defined(CONFIG_OF_LIBFDT)
356 static void fdt_error (const char *msg)
360 puts (" - must RESET the board to recover.\n");
363 static image_header_t *image_get_fdt (ulong fdt_addr)
365 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
367 image_print_contents (fdt_hdr);
369 puts (" Verifying Checksum ... ");
370 if (!image_check_hcrc (fdt_hdr)) {
371 fdt_error ("fdt header checksum invalid");
375 if (!image_check_dcrc (fdt_hdr)) {
376 fdt_error ("fdt checksum invalid");
381 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
382 fdt_error ("uImage is not a fdt");
385 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
386 fdt_error ("uImage is compressed");
389 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
390 fdt_error ("uImage data is not a fdt");
397 * fit_check_fdt - verify FIT format FDT subimage
398 * @fit_hdr: pointer to the FIT header
399 * fdt_noffset: FDT subimage node offset within FIT image
400 * @verify: data CRC verification flag
402 * fit_check_fdt() verifies integrity of the FDT subimage and from
403 * specified FIT image.
409 #if defined(CONFIG_FIT)
410 static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
412 fit_image_print (fit, fdt_noffset, " ");
415 puts (" Verifying Hash Integrity ... ");
416 if (!fit_image_check_hashes (fit, fdt_noffset)) {
417 fdt_error ("Bad Data Hash");
423 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
424 fdt_error ("Not a FDT image");
428 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
429 fdt_error ("FDT image is compressed");
435 #endif /* CONFIG_FIT */
437 static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
438 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
441 image_header_t *fdt_hdr;
442 char *fdt_blob = NULL;
443 ulong image_start, image_end;
444 ulong load_start, load_end;
445 #if defined(CONFIG_FIT)
447 const char *fit_uname_config = NULL;
448 const char *fit_uname_fdt = NULL;
456 *of_flat_tree = NULL;
459 if (argc > 3 || genimg_has_config (images)) {
460 #if defined(CONFIG_FIT)
463 * If the FDT blob comes from the FIT image and the
464 * FIT image address is omitted in the command line
465 * argument, try to use ramdisk or os FIT image
466 * address or default load address.
468 if (images->fit_uname_rd)
469 default_addr = (ulong)images->fit_hdr_rd;
470 else if (images->fit_uname_os)
471 default_addr = (ulong)images->fit_hdr_os;
473 default_addr = load_addr;
475 if (fit_parse_conf (argv[3], default_addr,
476 &fdt_addr, &fit_uname_config)) {
477 debug ("* fdt: config '%s' from image at 0x%08lx\n",
478 fit_uname_config, fdt_addr);
479 } else if (fit_parse_subimage (argv[3], default_addr,
480 &fdt_addr, &fit_uname_fdt)) {
481 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
482 fit_uname_fdt, fdt_addr);
486 fdt_addr = simple_strtoul(argv[3], NULL, 16);
487 debug ("* fdt: cmdline image address = 0x%08lx\n",
490 #if defined(CONFIG_FIT)
492 /* use FIT configuration provided in first bootm
495 fdt_addr = (ulong)images->fit_hdr_os;
496 fit_uname_config = images->fit_uname_cfg;
497 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
498 fit_uname_config, fdt_addr);
501 * Check whether configuration has FDT blob defined,
502 * if not quit silently.
504 fit_hdr = (void *)fdt_addr;
505 cfg_noffset = fit_conf_get_node (fit_hdr,
507 if (cfg_noffset < 0) {
508 debug ("* fdt: no such config\n");
512 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
514 if (fdt_noffset < 0) {
515 debug ("* fdt: no fdt in config\n");
521 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
524 /* copy from dataflash if needed */
525 fdt_addr = genimg_get_image (fdt_addr);
528 * Check if there is an FDT image at the
529 * address provided in the second bootm argument
530 * check image type, for FIT images get a FIT node.
532 switch (genimg_get_format ((void *)fdt_addr)) {
533 case IMAGE_FORMAT_LEGACY:
534 /* verify fdt_addr points to a valid image header */
535 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
537 fdt_hdr = image_get_fdt (fdt_addr);
542 * move image data to the load address,
543 * make sure we don't overwrite initial image
545 image_start = (ulong)fdt_hdr;
546 image_end = image_get_image_end (fdt_hdr);
548 load_start = image_get_load (fdt_hdr);
549 load_end = load_start + image_get_data_size (fdt_hdr);
551 if ((load_start < image_end) && (load_end > image_start)) {
552 fdt_error ("fdt overwritten");
556 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
557 image_get_data (fdt_hdr), load_start);
559 memmove ((void *)load_start,
560 (void *)image_get_data (fdt_hdr),
561 image_get_data_size (fdt_hdr));
563 fdt_blob = (char *)load_start;
565 case IMAGE_FORMAT_FIT:
567 * This case will catch both: new uImage format
568 * (libfdt based) and raw FDT blob (also libfdt
571 #if defined(CONFIG_FIT)
572 /* check FDT blob vs FIT blob */
573 if (fit_check_format ((const void *)fdt_addr)) {
577 fit_hdr = (void *)fdt_addr;
578 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
581 if (!fit_uname_fdt) {
583 * no FDT blob image node unit name,
584 * try to get config node first. If
585 * config unit node name is NULL
586 * fit_conf_get_node() will try to
587 * find default config node
589 cfg_noffset = fit_conf_get_node (fit_hdr,
592 if (cfg_noffset < 0) {
593 fdt_error ("Could not find configuration node\n");
597 fit_uname_config = fdt_get_name (fit_hdr,
599 printf (" Using '%s' configuration\n",
602 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
604 fit_uname_fdt = fit_get_name (fit_hdr,
607 /* get FDT component image node offset */
608 fdt_noffset = fit_image_get_node (fit_hdr,
611 if (fdt_noffset < 0) {
612 fdt_error ("Could not find subimage node\n");
616 printf (" Trying '%s' FDT blob subimage\n",
619 if (!fit_check_fdt (fit_hdr, fdt_noffset,
623 /* get ramdisk image data address and length */
624 if (fit_image_get_data (fit_hdr, fdt_noffset,
626 fdt_error ("Could not find FDT subimage data");
630 /* verift that image data is a proper FDT blob */
631 if (fdt_check_header ((char *)data) != 0) {
632 fdt_error ("Subimage data is not a FTD");
637 * move image data to the load address,
638 * make sure we don't overwrite initial image
640 image_start = (ulong)fit_hdr;
641 image_end = fit_get_end (fit_hdr);
643 if (fit_image_get_load (fit_hdr, fdt_noffset,
645 load_end = load_start + size;
647 if ((load_start < image_end) &&
648 (load_end > image_start)) {
649 fdt_error ("FDT overwritten");
653 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
654 (ulong)data, load_start);
656 memmove ((void *)load_start,
659 fdt_blob = (char *)load_start;
661 fdt_blob = (char *)data;
664 images->fit_hdr_fdt = fit_hdr;
665 images->fit_uname_fdt = fit_uname_fdt;
666 images->fit_noffset_fdt = fdt_noffset;
674 fdt_blob = (char *)fdt_addr;
675 debug ("* fdt: raw FDT blob\n");
676 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
680 fdt_error ("Did not find a cmdline Flattened Device Tree");
684 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
686 } else if (images->legacy_hdr_valid &&
687 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
689 ulong fdt_data, fdt_len;
692 * Now check if we have a legacy multi-component image,
693 * get second entry data start address and len.
695 printf ("## Flattened Device Tree from multi "
696 "component Image at %08lX\n",
697 (ulong)images->legacy_hdr_os);
699 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
702 fdt_blob = (char *)fdt_data;
703 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
705 if (fdt_check_header (fdt_blob) != 0) {
706 fdt_error ("image is not a fdt");
710 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
711 fdt_error ("fdt size != image size");
715 debug ("## No Flattened Device Tree\n");
719 debug ("## No Flattened Device Tree\n");
723 *of_flat_tree = fdt_blob;
724 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
725 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
726 *of_flat_tree, *of_size);
731 do_reset (cmdtp, flag, argc, argv);
735 static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
736 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
737 char **of_flat_tree, ulong *of_size)
739 char *fdt_blob = *of_flat_tree;
747 if (fdt_check_header (fdt_blob) != 0) {
748 fdt_error ("image is not a fdt");
753 /* move the blob if it is in flash (set relocate) */
754 if (addr2info ((ulong)fdt_blob) != NULL)
759 * The blob needs to be inside the boot mapping.
761 if (fdt_blob < (char *)bootmap_base)
764 if ((fdt_blob + *of_size + CFG_FDT_PAD) >=
765 ((char *)CFG_BOOTMAPSZ + bootmap_base))
768 /* move flattend device tree if needed */
773 /* position on a 4K boundary before the alloc_current */
774 /* Pad the FDT by a specified amount */
775 of_len = *of_size + CFG_FDT_PAD;
776 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
777 (CFG_BOOTMAPSZ + bootmap_base));
780 puts("device tree - allocation error\n");
784 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
785 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
788 printf (" Loading Device Tree to %08lx, end %08lx ... ",
789 of_start, of_start + of_len - 1);
791 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
793 fdt_error ("fdt move failed");
798 *of_flat_tree = (char *)of_start;
801 *of_flat_tree = fdt_blob;
802 of_len = (CFG_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
803 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
804 fdt_set_totalsize(*of_flat_tree, of_len);