2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
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>
54 DECLARE_GLOBAL_DATA_PTR;
56 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
57 extern ulong get_effective_memsize(void);
58 static ulong get_sp (void);
59 static void set_clocks_in_mhz (bd_t *kbd);
61 #ifndef CFG_LINUX_LOWMEM_MAX_SIZE
62 #define CFG_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
65 void __attribute__((noinline))
66 do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
67 bootm_headers_t *images)
71 ulong initrd_start, initrd_end;
72 ulong rd_data_start, rd_data_end, rd_len;
74 phys_size_t bootm_size;
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 bootm_size = getenv_bootm_size();
92 if (((u64)bootmap_base + bootm_size) >
93 (CFG_SDRAM_BASE + (u64)gd->ram_size))
94 puts("WARNING: bootm_low + bootm_size exceed total memory\n");
95 if ((bootmap_base + bootm_size) > get_effective_memsize())
96 puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
99 size = min(bootm_size, get_effective_memsize());
100 size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
102 if (size < bootm_size) {
103 ulong base = bootmap_base + size;
104 printf("WARNING: adjusting available memory to %x\n", size);
105 lmb_reserve(lmb, base, bootm_size - size);
109 * Booting a (Linux) kernel image
111 * Allocate space for command line and board info - the
112 * address should be as high as possible within the reach of
113 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
114 * memory, which means far enough below the current stack
118 debug ("## Current stack ends at 0x%08lx\n", sp);
120 /* adjust sp by 1K to be safe */
122 lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
124 #if defined(CONFIG_OF_LIBFDT)
125 /* find flattened device tree */
126 ret = boot_get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
133 /* allocate space and init command line */
134 ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
136 puts("ERROR with allocation of cmdline\n");
140 /* allocate space for kernel copy of board info */
141 ret = boot_get_kbd (lmb, &kbd, bootmap_base);
143 puts("ERROR with allocation of kernel bd\n");
146 set_clocks_in_mhz(kbd);
149 /* find kernel entry point */
150 if (images->legacy_hdr_valid) {
151 ep = image_get_ep (&images->legacy_hdr_os_copy);
152 #if defined(CONFIG_FIT)
153 } else if (images->fit_uname_os) {
154 ret = fit_image_get_entry (images->fit_hdr_os,
155 images->fit_noffset_os, &ep);
157 puts ("Can't get entry point property!\n");
162 puts ("Could not find kernel entry point!\n");
165 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
168 ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_PPC,
169 &rd_data_start, &rd_data_end);
173 rd_len = rd_data_end - rd_data_start;
175 #if defined(CONFIG_OF_LIBFDT)
176 ret = boot_relocate_fdt (lmb, bootmap_base,
177 cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
180 * Add the chosen node if it doesn't exist, add the env and bd_t
181 * if the user wants it (the logic is in the subroutines).
184 /* pass in dummy initrd info, we'll fix up later */
185 if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
186 fdt_error ("/chosen node create failed");
189 #ifdef CONFIG_OF_BOARD_SETUP
190 /* Call the board-specific fixup routine */
191 ft_board_setup(of_flat_tree, gd->bd);
194 #endif /* CONFIG_OF_LIBFDT */
196 ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
200 #if defined(CONFIG_OF_LIBFDT)
201 /* fixup the initrd now that we know where it should be */
202 if ((of_flat_tree) && (initrd_start && initrd_end)) {
204 int total = fdt_num_mem_rsv(of_flat_tree);
207 /* Look for the dummy entry and delete it */
208 for (j = 0; j < total; j++) {
209 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
210 if (addr == rd_data_start) {
211 fdt_del_mem_rsv(of_flat_tree, j);
216 ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
217 initrd_end - initrd_start + 1);
219 printf("fdt_chosen: %s\n", fdt_strerror(ret));
223 do_fixup_by_path_u32(of_flat_tree, "/chosen",
224 "linux,initrd-start", initrd_start, 0);
225 do_fixup_by_path_u32(of_flat_tree, "/chosen",
226 "linux,initrd-end", initrd_end, 0);
229 debug ("## Transferring control to Linux (at address %08lx) ...\n",
232 show_boot_progress (15);
234 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
235 unlock_ram_in_cache();
237 if (!images->autostart)
240 #if defined(CONFIG_OF_LIBFDT)
241 if (of_flat_tree) { /* device tree; boot new style */
243 * Linux Kernel Parameters (passing device tree):
244 * r3: pointer to the fdt, followed by the board info data
245 * r4: physical pointer to the kernel itself
250 debug (" Booting using OF flat tree...\n");
251 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
252 /* does not return */
257 * Linux Kernel Parameters (passing board info data):
258 * r3: ptr to board info data
259 * r4: initrd_start or 0 if no initrd
260 * r5: initrd_end - unused if r4 is 0
261 * r6: Start of command line string
262 * r7: End of command line string
264 debug (" Booting using board info...\n");
265 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
266 /* does not return */
271 if (images->autostart)
272 do_reset (cmdtp, flag, argc, argv);
276 static ulong get_sp (void)
280 asm( "mr %0,1": "=r"(sp) : );
284 static void set_clocks_in_mhz (bd_t *kbd)
288 if ((s = getenv ("clocks_in_mhz")) != NULL) {
289 /* convert all clock information to MHz */
290 kbd->bi_intfreq /= 1000000L;
291 kbd->bi_busfreq /= 1000000L;
292 #if defined(CONFIG_MPC8220)
293 kbd->bi_inpfreq /= 1000000L;
294 kbd->bi_pcifreq /= 1000000L;
295 kbd->bi_pevfreq /= 1000000L;
296 kbd->bi_flbfreq /= 1000000L;
297 kbd->bi_vcofreq /= 1000000L;
299 #if defined(CONFIG_CPM2)
300 kbd->bi_cpmfreq /= 1000000L;
301 kbd->bi_brgfreq /= 1000000L;
302 kbd->bi_sccfreq /= 1000000L;
303 kbd->bi_vco /= 1000000L;
305 #if defined(CONFIG_MPC5xxx)
306 kbd->bi_ipbfreq /= 1000000L;
307 kbd->bi_pcifreq /= 1000000L;
308 #endif /* CONFIG_MPC5xxx */
312 #if defined(CONFIG_OF_LIBFDT)
313 static void fdt_error (const char *msg)
317 puts (" - must RESET the board to recover.\n");
320 static image_header_t *image_get_fdt (ulong fdt_addr)
322 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
324 image_print_contents (fdt_hdr);
326 puts (" Verifying Checksum ... ");
327 if (!image_check_hcrc (fdt_hdr)) {
328 fdt_error ("fdt header checksum invalid");
332 if (!image_check_dcrc (fdt_hdr)) {
333 fdt_error ("fdt checksum invalid");
338 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
339 fdt_error ("uImage is not a fdt");
342 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
343 fdt_error ("uImage is compressed");
346 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
347 fdt_error ("uImage data is not a fdt");
354 * fit_check_fdt - verify FIT format FDT subimage
355 * @fit_hdr: pointer to the FIT header
356 * fdt_noffset: FDT subimage node offset within FIT image
357 * @verify: data CRC verification flag
359 * fit_check_fdt() verifies integrity of the FDT subimage and from
360 * specified FIT image.
366 #if defined(CONFIG_FIT)
367 static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
369 fit_image_print (fit, fdt_noffset, " ");
372 puts (" Verifying Hash Integrity ... ");
373 if (!fit_image_check_hashes (fit, fdt_noffset)) {
374 fdt_error ("Bad Data Hash");
380 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
381 fdt_error ("Not a FDT image");
385 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
386 fdt_error ("FDT image is compressed");
392 #endif /* CONFIG_FIT */
394 static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
395 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
398 image_header_t *fdt_hdr;
399 char *fdt_blob = NULL;
400 ulong image_start, image_end;
401 ulong load_start, load_end;
402 #if defined(CONFIG_FIT)
404 const char *fit_uname_config = NULL;
405 const char *fit_uname_fdt = NULL;
413 *of_flat_tree = NULL;
416 if (argc > 3 || genimg_has_config (images)) {
417 #if defined(CONFIG_FIT)
420 * If the FDT blob comes from the FIT image and the
421 * FIT image address is omitted in the command line
422 * argument, try to use ramdisk or os FIT image
423 * address or default load address.
425 if (images->fit_uname_rd)
426 default_addr = (ulong)images->fit_hdr_rd;
427 else if (images->fit_uname_os)
428 default_addr = (ulong)images->fit_hdr_os;
430 default_addr = load_addr;
432 if (fit_parse_conf (argv[3], default_addr,
433 &fdt_addr, &fit_uname_config)) {
434 debug ("* fdt: config '%s' from image at 0x%08lx\n",
435 fit_uname_config, fdt_addr);
436 } else if (fit_parse_subimage (argv[3], default_addr,
437 &fdt_addr, &fit_uname_fdt)) {
438 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
439 fit_uname_fdt, fdt_addr);
443 fdt_addr = simple_strtoul(argv[3], NULL, 16);
444 debug ("* fdt: cmdline image address = 0x%08lx\n",
447 #if defined(CONFIG_FIT)
449 /* use FIT configuration provided in first bootm
452 fdt_addr = (ulong)images->fit_hdr_os;
453 fit_uname_config = images->fit_uname_cfg;
454 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
455 fit_uname_config, fdt_addr);
458 * Check whether configuration has FDT blob defined,
459 * if not quit silently.
461 fit_hdr = (void *)fdt_addr;
462 cfg_noffset = fit_conf_get_node (fit_hdr,
464 if (cfg_noffset < 0) {
465 debug ("* fdt: no such config\n");
469 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
471 if (fdt_noffset < 0) {
472 debug ("* fdt: no fdt in config\n");
478 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
481 /* copy from dataflash if needed */
482 fdt_addr = genimg_get_image (fdt_addr);
485 * Check if there is an FDT image at the
486 * address provided in the second bootm argument
487 * check image type, for FIT images get a FIT node.
489 switch (genimg_get_format ((void *)fdt_addr)) {
490 case IMAGE_FORMAT_LEGACY:
491 /* verify fdt_addr points to a valid image header */
492 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
494 fdt_hdr = image_get_fdt (fdt_addr);
499 * move image data to the load address,
500 * make sure we don't overwrite initial image
502 image_start = (ulong)fdt_hdr;
503 image_end = image_get_image_end (fdt_hdr);
505 load_start = image_get_load (fdt_hdr);
506 load_end = load_start + image_get_data_size (fdt_hdr);
508 if ((load_start < image_end) && (load_end > image_start)) {
509 fdt_error ("fdt overwritten");
513 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
514 image_get_data (fdt_hdr), load_start);
516 memmove ((void *)load_start,
517 (void *)image_get_data (fdt_hdr),
518 image_get_data_size (fdt_hdr));
520 fdt_blob = (char *)load_start;
522 case IMAGE_FORMAT_FIT:
524 * This case will catch both: new uImage format
525 * (libfdt based) and raw FDT blob (also libfdt
528 #if defined(CONFIG_FIT)
529 /* check FDT blob vs FIT blob */
530 if (fit_check_format ((const void *)fdt_addr)) {
534 fit_hdr = (void *)fdt_addr;
535 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
538 if (!fit_uname_fdt) {
540 * no FDT blob image node unit name,
541 * try to get config node first. If
542 * config unit node name is NULL
543 * fit_conf_get_node() will try to
544 * find default config node
546 cfg_noffset = fit_conf_get_node (fit_hdr,
549 if (cfg_noffset < 0) {
550 fdt_error ("Could not find configuration node\n");
554 fit_uname_config = fdt_get_name (fit_hdr,
556 printf (" Using '%s' configuration\n",
559 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
561 fit_uname_fdt = fit_get_name (fit_hdr,
564 /* get FDT component image node offset */
565 fdt_noffset = fit_image_get_node (fit_hdr,
568 if (fdt_noffset < 0) {
569 fdt_error ("Could not find subimage node\n");
573 printf (" Trying '%s' FDT blob subimage\n",
576 if (!fit_check_fdt (fit_hdr, fdt_noffset,
580 /* get ramdisk image data address and length */
581 if (fit_image_get_data (fit_hdr, fdt_noffset,
583 fdt_error ("Could not find FDT subimage data");
587 /* verift that image data is a proper FDT blob */
588 if (fdt_check_header ((char *)data) != 0) {
589 fdt_error ("Subimage data is not a FTD");
594 * move image data to the load address,
595 * make sure we don't overwrite initial image
597 image_start = (ulong)fit_hdr;
598 image_end = fit_get_end (fit_hdr);
600 if (fit_image_get_load (fit_hdr, fdt_noffset,
602 load_end = load_start + size;
604 if ((load_start < image_end) &&
605 (load_end > image_start)) {
606 fdt_error ("FDT overwritten");
610 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
611 (ulong)data, load_start);
613 memmove ((void *)load_start,
616 fdt_blob = (char *)load_start;
618 fdt_blob = (char *)data;
621 images->fit_hdr_fdt = fit_hdr;
622 images->fit_uname_fdt = fit_uname_fdt;
623 images->fit_noffset_fdt = fdt_noffset;
631 fdt_blob = (char *)fdt_addr;
632 debug ("* fdt: raw FDT blob\n");
633 printf ("## Flattened Device Tree blob at %08lx\n", fdt_blob);
637 fdt_error ("Did not find a cmdline Flattened Device Tree");
641 printf (" Booting using the fdt blob at 0x%x\n", fdt_blob);
643 } else if (images->legacy_hdr_valid &&
644 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
646 ulong fdt_data, fdt_len;
649 * Now check if we have a legacy multi-component image,
650 * get second entry data start address and len.
652 printf ("## Flattened Device Tree from multi "
653 "component Image at %08lX\n",
654 (ulong)images->legacy_hdr_os);
656 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
659 fdt_blob = (char *)fdt_data;
660 printf (" Booting using the fdt at 0x%x\n", fdt_blob);
662 if (fdt_check_header (fdt_blob) != 0) {
663 fdt_error ("image is not a fdt");
667 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
668 fdt_error ("fdt size != image size");
672 debug ("## No Flattened Device Tree\n");
676 debug ("## No Flattened Device Tree\n");
680 *of_flat_tree = fdt_blob;
681 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
682 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
683 *of_flat_tree, *of_size);
688 do_reset (cmdtp, flag, argc, argv);
692 static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
693 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
694 char **of_flat_tree, ulong *of_size)
696 char *fdt_blob = *of_flat_tree;
704 if (fdt_check_header (fdt_blob) != 0) {
705 fdt_error ("image is not a fdt");
710 /* move the blob if it is in flash (set relocate) */
711 if (addr2info ((ulong)fdt_blob) != NULL)
716 * The blob must be within CFG_BOOTMAPSZ,
717 * so we flag it to be copied if it is not.
719 if (fdt_blob >= (char *)CFG_BOOTMAPSZ)
722 of_len = be32_to_cpu (fdt_totalsize (fdt_blob));
724 /* move flattend device tree if needed */
729 /* position on a 4K boundary before the alloc_current */
730 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
731 (CFG_BOOTMAPSZ + bootmap_base));
734 puts("device tree - allocation error\n");
738 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
739 (ulong)fdt_blob, (ulong)fdt_blob + of_len - 1,
742 printf (" Loading Device Tree to %08lx, end %08lx ... ",
743 of_start, of_start + of_len - 1);
745 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
747 fdt_error ("fdt move failed");
752 *of_flat_tree = (char *)of_start;
754 *of_flat_tree = fdt_blob;
755 lmb_reserve(lmb, (ulong)working_fdt, of_len);