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>
40 #elif defined(CONFIG_OF_FLAT_TREE)
44 #ifdef CONFIG_LOGBUFFER
48 #ifdef CFG_INIT_RAM_LOCK
49 #include <asm/cache.h>
52 DECLARE_GLOBAL_DATA_PTR;
53 extern image_header_t header;
56 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
58 #if defined(CONFIG_CMD_BDI)
59 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
62 void __attribute__((noinline))
63 do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
64 int argc, char *argv[],
71 ulong initrd_start, initrd_end;
72 ulong cmd_start, cmd_end;
75 int initrd_copy_to_ram = 1;
79 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
80 image_header_t *hdr = &header;
81 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
82 char *of_flat_tree = NULL;
86 if ((s = getenv ("initrd_high")) != NULL) {
87 /* a value of "no" or a similar string will act like 0,
88 * turning the "load high" feature off. This is intentional.
90 initrd_high = simple_strtoul(s, NULL, 16);
91 if (initrd_high == ~0)
92 initrd_copy_to_ram = 0;
93 } else { /* not set, no restrictions to load high */
97 #ifdef CONFIG_LOGBUFFER
99 /* Prevent initrd from overwriting logbuffer */
100 if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
101 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
102 debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
106 * Booting a (Linux) kernel image
108 * Allocate space for command line and board info - the
109 * address should be as high as possible within the reach of
110 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
111 * memory, which means far enough below the current stack
115 asm( "mr %0,1": "=r"(sp) : );
117 debug ("## Current stack ends at 0x%08lX ", sp);
119 sp -= 2048; /* just to be sure */
120 if (sp > CFG_BOOTMAPSZ)
124 debug ("=> set upper limit to 0x%08lX\n", sp);
126 cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
127 kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
129 if ((s = getenv("bootargs")) == NULL)
134 cmd_start = (ulong)&cmdline[0];
135 cmd_end = cmd_start + strlen(cmdline);
140 printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
142 #if defined(CONFIG_CMD_BDI)
143 do_bdinfo (NULL, 0, 0, NULL);
147 if ((s = getenv ("clocks_in_mhz")) != NULL) {
148 /* convert all clock information to MHz */
149 kbd->bi_intfreq /= 1000000L;
150 kbd->bi_busfreq /= 1000000L;
151 #if defined(CONFIG_MPC8220)
152 kbd->bi_inpfreq /= 1000000L;
153 kbd->bi_pcifreq /= 1000000L;
154 kbd->bi_pevfreq /= 1000000L;
155 kbd->bi_flbfreq /= 1000000L;
156 kbd->bi_vcofreq /= 1000000L;
158 #if defined(CONFIG_CPM2)
159 kbd->bi_cpmfreq /= 1000000L;
160 kbd->bi_brgfreq /= 1000000L;
161 kbd->bi_sccfreq /= 1000000L;
162 kbd->bi_vco /= 1000000L;
164 #if defined(CONFIG_MPC5xxx)
165 kbd->bi_ipbfreq /= 1000000L;
166 kbd->bi_pcifreq /= 1000000L;
167 #endif /* CONFIG_MPC5xxx */
170 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
173 * Check if there is an initrd image
176 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
177 /* Look for a '-' which indicates to ignore the ramdisk argument */
178 if (argc >= 3 && strcmp(argv[2], "-") == 0) {
179 debug ("Skipping initrd\n");
185 debug ("Not skipping initrd\n");
186 show_boot_progress (9);
188 addr = simple_strtoul(argv[2], NULL, 16);
190 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
191 hdr = (image_header_t *)addr;
193 if (!image_check_magic (hdr)) {
194 puts ("Bad Magic Number\n");
195 show_boot_progress (-10);
196 do_reset (cmdtp, flag, argc, argv);
199 if (!image_check_hcrc (hdr)) {
200 puts ("Bad Header Checksum\n");
201 show_boot_progress (-11);
202 do_reset (cmdtp, flag, argc, argv);
204 show_boot_progress (10);
206 print_image_hdr (hdr);
209 puts (" Verifying Checksum ... ");
211 if (!image_check_dcrc_wd (hdr, CHUNKSZ)) {
212 puts ("Bad Data CRC\n");
213 show_boot_progress (-12);
214 do_reset (cmdtp, flag, argc, argv);
219 show_boot_progress (11);
221 if (!image_check_os (hdr, IH_OS_LINUX) ||
222 !image_check_arch (hdr, IH_ARCH_PPC) ||
223 !image_check_type (hdr, IH_TYPE_RAMDISK)) {
224 puts ("No Linux PPC Ramdisk Image\n");
225 show_boot_progress (-13);
226 do_reset (cmdtp, flag, argc, argv);
229 data = image_get_data (hdr);
230 len = image_get_data_size (hdr);
233 * Now check if we have a multifile image
235 } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1])) {
236 u_long tail = image_to_cpu (len_ptr[0]) % 4;
239 show_boot_progress (13);
241 /* skip kernel length and terminator */
242 data = (ulong)(&len_ptr[2]);
243 /* skip any additional image length fields */
244 for (i=1; len_ptr[i]; ++i)
246 /* add kernel length, and align */
247 data += image_to_cpu (len_ptr[0]);
252 len = image_to_cpu (len_ptr[1]);
258 show_boot_progress (14);
263 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
265 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
266 hdr = (image_header_t *)of_flat_tree;
267 #if defined(CONFIG_OF_FLAT_TREE)
268 if (*((ulong *)(of_flat_tree)) == OF_DT_HEADER) {
269 #elif defined(CONFIG_OF_LIBFDT)
270 if (fdt_check_header (of_flat_tree) == 0) {
273 if (addr2info((ulong)of_flat_tree) != NULL)
274 of_data = (ulong)of_flat_tree;
276 } else if (image_check_magic (hdr)) {
277 printf("## Flat Device Tree at %08lX\n", hdr);
278 print_image_hdr (hdr);
280 if ((image_get_load (hdr) < ((unsigned long)hdr + image_get_image_size (hdr))) &&
281 ((image_get_load (hdr) + image_get_data_size (hdr)) > (unsigned long)hdr)) {
282 puts ("ERROR: fdt overwritten - "
283 "must RESET the board to recover.\n");
284 do_reset (cmdtp, flag, argc, argv);
287 puts (" Verifying Checksum ... ");
288 if (!image_check_hcrc (hdr)) {
289 puts ("ERROR: fdt header checksum invalid - "
290 "must RESET the board to recover.\n");
291 do_reset (cmdtp, flag, argc, argv);
294 if (!image_check_dcrc (hdr)) {
295 puts ("ERROR: fdt checksum invalid - "
296 "must RESET the board to recover.\n");
297 do_reset (cmdtp, flag, argc, argv);
301 if (!image_check_type (hdr, IH_TYPE_FLATDT)) {
302 puts ("ERROR: uImage is not a fdt - "
303 "must RESET the board to recover.\n");
304 do_reset (cmdtp, flag, argc, argv);
306 if (image_get_comp (hdr) != IH_COMP_NONE) {
307 puts ("ERROR: uImage is compressed - "
308 "must RESET the board to recover.\n");
309 do_reset (cmdtp, flag, argc, argv);
311 #if defined(CONFIG_OF_FLAT_TREE)
312 if (*((ulong *)(of_flat_tree + image_get_header_size ())) != OF_DT_HEADER) {
313 #elif defined(CONFIG_OF_LIBFDT)
314 if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
316 puts ("ERROR: uImage data is not a fdt - "
317 "must RESET the board to recover.\n");
318 do_reset (cmdtp, flag, argc, argv);
321 memmove ((void *)image_get_load (hdr),
322 (void *)(of_flat_tree + image_get_header_size ()),
323 image_get_data_size (hdr));
325 of_flat_tree = (char *)image_get_load (hdr);
327 puts ("Did not find a flat Flat Device Tree.\n"
328 "Must RESET the board to recover.\n");
329 do_reset (cmdtp, flag, argc, argv);
331 printf (" Booting using the fdt at 0x%x\n",
333 } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
334 u_long tail = image_to_cpu (len_ptr[0]) % 4;
337 /* skip kernel length, initrd length, and terminator */
338 of_flat_tree = (char *)(&len_ptr[3]);
339 /* skip any additional image length fields */
340 for (i=2; len_ptr[i]; ++i)
342 /* add kernel length, and align */
343 of_flat_tree += image_to_cpu (len_ptr[0]);
345 of_flat_tree += 4 - tail;
348 /* add initrd length, and align */
349 tail = image_to_cpu (len_ptr[1]) % 4;
350 of_flat_tree += image_to_cpu (len_ptr[1]);
352 of_flat_tree += 4 - tail;
356 /* move the blob if it is in flash (set of_data to !null) */
357 if (addr2info ((ulong)of_flat_tree) != NULL)
358 of_data = (ulong)of_flat_tree;
362 #if defined(CONFIG_OF_FLAT_TREE)
363 if (*((ulong *)(of_flat_tree)) != OF_DT_HEADER) {
364 #elif defined(CONFIG_OF_LIBFDT)
365 if (fdt_check_header (of_flat_tree) != 0) {
367 puts ("ERROR: image is not a fdt - "
368 "must RESET the board to recover.\n");
369 do_reset (cmdtp, flag, argc, argv);
372 #if defined(CONFIG_OF_FLAT_TREE)
373 if (((struct boot_param_header *)of_flat_tree)->totalsize !=
374 image_to_cpu (len_ptr[2])) {
375 #elif defined(CONFIG_OF_LIBFDT)
376 if (be32_to_cpu (fdt_totalsize (of_flat_tree)) !=
377 image_to_cpu (len_ptr[2])) {
379 puts ("ERROR: fdt size != image size - "
380 "must RESET the board to recover.\n");
381 do_reset (cmdtp, flag, argc, argv);
386 debug ("No initrd\n");
390 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
392 initrd_end = initrd_start + len;
394 initrd_start = (ulong)kbd - len;
395 initrd_start &= ~(4096 - 1); /* align on page */
401 * the inital ramdisk does not need to be within
402 * CFG_BOOTMAPSZ as it is not accessed until after
403 * the mm system is initialised.
405 * do the stack bottom calculation again and see if
406 * the initrd will fit just below the monitor stack
407 * bottom without overwriting the area allocated
408 * above for command line args and board info.
410 asm( "mr %0,1": "=r"(nsp) : );
411 nsp -= 2048; /* just to be sure */
413 if (nsp > initrd_high) /* limit as specified */
416 nsp &= ~(4096 - 1); /* align on page */
421 show_boot_progress (12);
423 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
424 data, data + len - 1, len, len);
426 initrd_end = initrd_start + len;
427 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
428 initrd_start, initrd_end);
430 memmove_wd((void *)initrd_start,
431 (void *)data, len, CHUNKSZ);
440 #if defined(CONFIG_OF_LIBFDT)
444 * The blob must be within CFG_BOOTMAPSZ,
445 * so we flag it to be copied if it is not.
447 if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
448 of_data = (ulong)of_flat_tree;
451 /* move of_flat_tree if needed */
454 ulong of_start, of_len;
456 of_len = be32_to_cpu(fdt_totalsize(of_data));
458 /* position on a 4K boundary before the kbd */
459 of_start = (ulong)kbd - of_len;
460 of_start &= ~(4096 - 1); /* align on page */
461 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
462 of_data, of_data + of_len - 1, of_len, of_len);
464 of_flat_tree = (char *)of_start;
465 printf (" Loading Device Tree to %08lx, end %08lx ... ",
466 of_start, of_start + of_len - 1);
467 err = fdt_open_into((void *)of_data, (void *)of_start, of_len);
469 puts ("ERROR: fdt move failed - "
470 "must RESET the board to recover.\n");
471 do_reset (cmdtp, flag, argc, argv);
476 * Add the chosen node if it doesn't exist, add the env and bd_t
477 * if the user wants it (the logic is in the subroutines).
480 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
481 puts ("ERROR: /chosen node create failed - "
482 "must RESET the board to recover.\n");
483 do_reset (cmdtp, flag, argc, argv);
485 #ifdef CONFIG_OF_HAS_UBOOT_ENV
486 if (fdt_env(of_flat_tree) < 0) {
487 puts ("ERROR: /u-boot-env node create failed - "
488 "must RESET the board to recover.\n");
489 do_reset (cmdtp, flag, argc, argv);
492 #ifdef CONFIG_OF_HAS_BD_T
493 if (fdt_bd_t(of_flat_tree) < 0) {
494 puts ("ERROR: /bd_t node create failed - "
495 "must RESET the board to recover.\n");
496 do_reset (cmdtp, flag, argc, argv);
499 #ifdef CONFIG_OF_BOARD_SETUP
500 /* Call the board-specific fixup routine */
501 ft_board_setup(of_flat_tree, gd->bd);
505 #elif defined(CONFIG_OF_FLAT_TREE)
509 * The blob must be within CFG_BOOTMAPSZ,
510 * so we flag it to be copied if it is not.
512 if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
513 of_data = (ulong)of_flat_tree;
516 /* move of_flat_tree if needed */
518 ulong of_start, of_len;
519 of_len = ((struct boot_param_header *)of_data)->totalsize;
521 /* provide extra 8k pad */
522 of_start = (ulong)kbd - of_len - 8192;
523 of_start &= ~(4096 - 1); /* align on page */
524 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
525 of_data, of_data + of_len - 1, of_len, of_len);
527 of_flat_tree = (char *)of_start;
528 printf (" Loading Device Tree to %08lx, end %08lx ... ",
529 of_start, of_start + of_len - 1);
530 memmove ((void *)of_start, (void *)of_data, of_len);
534 * Create the /chosen node and modify the blob with board specific
537 ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
538 /* ft_dump_blob(of_flat_tree); */
540 #endif /* #if defined(CONFIG_OF_LIBFDT) #elif defined(CONFIG_OF_FLAT_TREE) */
542 debug ("## Transferring control to Linux (at address %08lx) ...\n",
545 show_boot_progress (15);
547 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
548 unlock_ram_in_cache();
551 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
552 if (of_flat_tree) { /* device tree; boot new style */
554 * Linux Kernel Parameters (passing device tree):
555 * r3: pointer to the fdt, followed by the board info data
556 * r4: physical pointer to the kernel itself
561 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
562 /* does not return */
566 * Linux Kernel Parameters (passing board info data):
567 * r3: ptr to board info data
568 * r4: initrd_start or 0 if no initrd
569 * r5: initrd_end - unused if r4 is 0
570 * r6: Start of command line string
571 * r7: End of command line string
573 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
574 /* does not return */