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,
33 #include <environment.h>
34 #include <asm/byteorder.h>
36 #if defined(CONFIG_OF_LIBFDT)
39 #include <fdt_support.h>
41 #if defined(CONFIG_OF_FLAT_TREE)
45 #ifdef CONFIG_LOGBUFFER
49 #ifdef CFG_INIT_RAM_LOCK
50 #include <asm/cache.h>
53 #define CHUNKSZ (64 * 1024)
55 DECLARE_GLOBAL_DATA_PTR;
56 extern image_header_t header;
59 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
61 #if defined(CONFIG_CMD_BDI)
62 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
65 void __attribute__((noinline))
66 do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
67 int argc, char *argv[],
74 ulong initrd_start, initrd_end;
75 ulong cmd_start, cmd_end;
78 int initrd_copy_to_ram = 1;
82 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
83 image_header_t *hdr = &header;
84 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
85 char *of_flat_tree = NULL;
89 if ((s = getenv ("initrd_high")) != NULL) {
90 /* a value of "no" or a similar string will act like 0,
91 * turning the "load high" feature off. This is intentional.
93 initrd_high = simple_strtoul(s, NULL, 16);
94 if (initrd_high == ~0)
95 initrd_copy_to_ram = 0;
96 } else { /* not set, no restrictions to load high */
100 #ifdef CONFIG_LOGBUFFER
102 /* Prevent initrd from overwriting logbuffer */
103 if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
104 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
105 debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
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 asm( "mr %0,1": "=r"(sp) : );
120 debug ("## Current stack ends at 0x%08lX ", sp);
122 sp -= 2048; /* just to be sure */
123 if (sp > CFG_BOOTMAPSZ)
127 debug ("=> set upper limit to 0x%08lX\n", sp);
129 cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
130 kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
132 if ((s = getenv("bootargs")) == NULL)
137 cmd_start = (ulong)&cmdline[0];
138 cmd_end = cmd_start + strlen(cmdline);
143 printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
145 #if defined(CONFIG_CMD_BDI)
146 do_bdinfo (NULL, 0, 0, NULL);
150 if ((s = getenv ("clocks_in_mhz")) != NULL) {
151 /* convert all clock information to MHz */
152 kbd->bi_intfreq /= 1000000L;
153 kbd->bi_busfreq /= 1000000L;
154 #if defined(CONFIG_MPC8220)
155 kbd->bi_inpfreq /= 1000000L;
156 kbd->bi_pcifreq /= 1000000L;
157 kbd->bi_pevfreq /= 1000000L;
158 kbd->bi_flbfreq /= 1000000L;
159 kbd->bi_vcofreq /= 1000000L;
161 #if defined(CONFIG_CPM2)
162 kbd->bi_cpmfreq /= 1000000L;
163 kbd->bi_brgfreq /= 1000000L;
164 kbd->bi_sccfreq /= 1000000L;
165 kbd->bi_vco /= 1000000L;
167 #if defined(CONFIG_MPC5xxx)
168 kbd->bi_ipbfreq /= 1000000L;
169 kbd->bi_pcifreq /= 1000000L;
170 #endif /* CONFIG_MPC5xxx */
173 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
176 * Check if there is an initrd image
179 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
180 /* Look for a '-' which indicates to ignore the ramdisk argument */
181 if (argc >= 3 && strcmp(argv[2], "-") == 0) {
182 debug ("Skipping initrd\n");
188 debug ("Not skipping initrd\n");
189 show_boot_progress (9);
191 addr = simple_strtoul(argv[2], NULL, 16);
193 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
194 hdr = (image_header_t *)addr;
196 if (!image_check_magic (hdr)) {
197 puts ("Bad Magic Number\n");
198 show_boot_progress (-10);
199 do_reset (cmdtp, flag, argc, argv);
202 if (!image_check_hcrc (hdr)) {
203 puts ("Bad Header Checksum\n");
204 show_boot_progress (-11);
205 do_reset (cmdtp, flag, argc, argv);
207 show_boot_progress (10);
209 print_image_hdr (hdr);
212 puts (" Verifying Checksum ... ");
214 if (!image_check_dcrc_wd (hdr, CHUNKSZ)) {
215 puts ("Bad Data CRC\n");
216 show_boot_progress (-12);
217 do_reset (cmdtp, flag, argc, argv);
222 show_boot_progress (11);
224 if (!image_check_os (hdr, IH_OS_LINUX) ||
225 !image_check_arch (hdr, IH_ARCH_PPC) ||
226 !image_check_type (hdr, IH_TYPE_RAMDISK)) {
227 puts ("No Linux PPC Ramdisk Image\n");
228 show_boot_progress (-13);
229 do_reset (cmdtp, flag, argc, argv);
232 data = image_get_data (hdr);
233 len = image_get_data_size (hdr);
236 * Now check if we have a multifile image
238 } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1])) {
239 u_long tail = image_to_cpu (len_ptr[0]) % 4;
242 show_boot_progress (13);
244 /* skip kernel length and terminator */
245 data = (ulong)(&len_ptr[2]);
246 /* skip any additional image length fields */
247 for (i=1; len_ptr[i]; ++i)
249 /* add kernel length, and align */
250 data += image_to_cpu (len_ptr[0]);
255 len = image_to_cpu (len_ptr[1]);
261 show_boot_progress (14);
266 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
268 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
269 hdr = (image_header_t *)of_flat_tree;
270 #if defined(CONFIG_OF_FLAT_TREE)
271 if (*((ulong *)(of_flat_tree + image_get_header_size ())) != OF_DT_HEADER) {
273 if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
276 if (addr2info((ulong)of_flat_tree) != NULL)
277 of_data = (ulong)of_flat_tree;
279 } else if (image_check_magic (hdr)) {
280 printf("## Flat Device Tree at %08lX\n", hdr);
281 print_image_hdr (hdr);
283 if ((image_get_load (hdr) < ((unsigned long)hdr + image_get_image_size (hdr))) &&
284 ((image_get_load (hdr) + image_get_data_size (hdr)) > (unsigned long)hdr)) {
285 puts ("ERROR: fdt overwritten - "
286 "must RESET the board to recover.\n");
287 do_reset (cmdtp, flag, argc, argv);
290 puts (" Verifying Checksum ... ");
291 if (!image_check_hcrc (hdr)) {
292 puts ("ERROR: fdt header checksum invalid - "
293 "must RESET the board to recover.\n");
294 do_reset (cmdtp, flag, argc, argv);
297 if (!image_check_dcrc (hdr)) {
298 puts ("ERROR: fdt checksum invalid - "
299 "must RESET the board to recover.\n");
300 do_reset (cmdtp, flag, argc, argv);
304 if (!image_check_type (hdr, IH_TYPE_FLATDT)) {
305 puts ("ERROR: uImage is not a fdt - "
306 "must RESET the board to recover.\n");
307 do_reset (cmdtp, flag, argc, argv);
309 if (image_get_comp (hdr) != IH_COMP_NONE) {
310 puts ("ERROR: uImage is compressed - "
311 "must RESET the board to recover.\n");
312 do_reset (cmdtp, flag, argc, argv);
314 #if defined(CONFIG_OF_FLAT_TREE)
315 if (*((ulong *)(of_flat_tree + image_get_header_size ())) != OF_DT_HEADER) {
317 if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
319 puts ("ERROR: uImage data is not a fdt - "
320 "must RESET the board to recover.\n");
321 do_reset (cmdtp, flag, argc, argv);
324 memmove ((void *)image_get_load (hdr),
325 (void *)(of_flat_tree + image_get_header_size ()),
326 image_get_data_size (hdr));
328 of_flat_tree = (char *)image_get_load (hdr);
330 puts ("Did not find a flat Flat Device Tree.\n"
331 "Must RESET the board to recover.\n");
332 do_reset (cmdtp, flag, argc, argv);
334 printf (" Booting using the fdt at 0x%x\n",
336 } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
337 u_long tail = image_to_cpu (len_ptr[0]) % 4;
340 /* skip kernel length, initrd length, and terminator */
341 of_flat_tree = (char *)(&len_ptr[3]);
342 /* skip any additional image length fields */
343 for (i=2; len_ptr[i]; ++i)
345 /* add kernel length, and align */
346 of_flat_tree += image_to_cpu (len_ptr[0]);
348 of_flat_tree += 4 - tail;
351 /* add initrd length, and align */
352 tail = image_to_cpu (len_ptr[1]) % 4;
353 of_flat_tree += image_to_cpu (len_ptr[1]);
355 of_flat_tree += 4 - tail;
359 /* move the blob if it is in flash (set of_data to !null) */
360 if (addr2info ((ulong)of_flat_tree) != NULL)
361 of_data = (ulong)of_flat_tree;
365 #if defined(CONFIG_OF_FLAT_TREE)
366 if (*((ulong *)(of_flat_tree)) != OF_DT_HEADER) {
368 if (fdt_check_header (of_flat_tree) != 0) {
370 puts ("ERROR: image is not a fdt - "
371 "must RESET the board to recover.\n");
372 do_reset (cmdtp, flag, argc, argv);
375 #if defined(CONFIG_OF_FLAT_TREE)
376 if (((struct boot_param_header *)of_flat_tree)->totalsize !=
377 image_to_cpu (len_ptr[2])) {
379 if (be32_to_cpu (fdt_totalsize (of_flat_tree)) !=
380 image_to_cpu (len_ptr[2])) {
382 puts ("ERROR: fdt size != image size - "
383 "must RESET the board to recover.\n");
384 do_reset (cmdtp, flag, argc, argv);
389 debug ("No initrd\n");
393 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
395 initrd_end = initrd_start + len;
397 initrd_start = (ulong)kbd - len;
398 initrd_start &= ~(4096 - 1); /* align on page */
404 * the inital ramdisk does not need to be within
405 * CFG_BOOTMAPSZ as it is not accessed until after
406 * the mm system is initialised.
408 * do the stack bottom calculation again and see if
409 * the initrd will fit just below the monitor stack
410 * bottom without overwriting the area allocated
411 * above for command line args and board info.
413 asm( "mr %0,1": "=r"(nsp) : );
414 nsp -= 2048; /* just to be sure */
416 if (nsp > initrd_high) /* limit as specified */
419 nsp &= ~(4096 - 1); /* align on page */
424 show_boot_progress (12);
426 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
427 data, data + len - 1, len, len);
429 initrd_end = initrd_start + len;
430 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
431 initrd_start, initrd_end);
432 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
435 void *to = (void *)initrd_start;
436 void *from = (void *)data;
439 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
441 memmove (to, from, tail);
447 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
448 memmove ((void *)initrd_start, (void *)data, len);
449 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
457 #if defined(CONFIG_OF_LIBFDT)
461 * The blob must be within CFG_BOOTMAPSZ,
462 * so we flag it to be copied if it is not.
464 if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
465 of_data = (ulong)of_flat_tree;
468 /* move of_flat_tree if needed */
471 ulong of_start, of_len;
473 of_len = be32_to_cpu(fdt_totalsize(of_data));
475 /* position on a 4K boundary before the kbd */
476 of_start = (ulong)kbd - of_len;
477 of_start &= ~(4096 - 1); /* align on page */
478 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
479 of_data, of_data + of_len - 1, of_len, of_len);
481 of_flat_tree = (char *)of_start;
482 printf (" Loading Device Tree to %08lx, end %08lx ... ",
483 of_start, of_start + of_len - 1);
484 err = fdt_open_into((void *)of_data, (void *)of_start, of_len);
486 puts ("ERROR: fdt move failed - "
487 "must RESET the board to recover.\n");
488 do_reset (cmdtp, flag, argc, argv);
493 * Add the chosen node if it doesn't exist, add the env and bd_t
494 * if the user wants it (the logic is in the subroutines).
497 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
498 puts ("ERROR: /chosen node create failed - "
499 "must RESET the board to recover.\n");
500 do_reset (cmdtp, flag, argc, argv);
502 #ifdef CONFIG_OF_HAS_UBOOT_ENV
503 if (fdt_env(of_flat_tree) < 0) {
504 puts ("ERROR: /u-boot-env node create failed - "
505 "must RESET the board to recover.\n");
506 do_reset (cmdtp, flag, argc, argv);
509 #ifdef CONFIG_OF_HAS_BD_T
510 if (fdt_bd_t(of_flat_tree) < 0) {
511 puts ("ERROR: /bd_t node create failed - "
512 "must RESET the board to recover.\n");
513 do_reset (cmdtp, flag, argc, argv);
516 #ifdef CONFIG_OF_BOARD_SETUP
517 /* Call the board-specific fixup routine */
518 ft_board_setup(of_flat_tree, gd->bd);
521 #endif /* CONFIG_OF_LIBFDT */
522 #if defined(CONFIG_OF_FLAT_TREE)
525 * The blob must be within CFG_BOOTMAPSZ,
526 * so we flag it to be copied if it is not.
528 if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
529 of_data = (ulong)of_flat_tree;
532 /* move of_flat_tree if needed */
534 ulong of_start, of_len;
535 of_len = ((struct boot_param_header *)of_data)->totalsize;
537 /* provide extra 8k pad */
538 of_start = (ulong)kbd - of_len - 8192;
539 of_start &= ~(4096 - 1); /* align on page */
540 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
541 of_data, of_data + of_len - 1, of_len, of_len);
543 of_flat_tree = (char *)of_start;
544 printf (" Loading Device Tree to %08lx, end %08lx ... ",
545 of_start, of_start + of_len - 1);
546 memmove ((void *)of_start, (void *)of_data, of_len);
550 * Create the /chosen node and modify the blob with board specific
553 ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
554 /* ft_dump_blob(of_flat_tree); */
556 debug ("## Transferring control to Linux (at address %08lx) ...\n",
559 show_boot_progress (15);
561 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
562 unlock_ram_in_cache();
565 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
566 if (of_flat_tree) { /* device tree; boot new style */
568 * Linux Kernel Parameters (passing device tree):
569 * r3: pointer to the fdt, followed by the board info data
570 * r4: physical pointer to the kernel itself
575 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
576 /* does not return */
580 * Linux Kernel Parameters (passing board info data):
581 * r3: ptr to board info data
582 * r4: initrd_start or 0 if no initrd
583 * r5: initrd_end - unused if r4 is 0
584 * r6: Start of command line string
585 * r7: End of command line string
587 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
588 /* does not return */