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,
35 #include <environment.h>
36 #include <asm/byteorder.h>
38 #if defined(CONFIG_OF_LIBFDT)
41 #include <fdt_support.h>
44 #ifdef CONFIG_LOGBUFFER
48 #ifdef CFG_INIT_RAM_LOCK
49 #include <asm/cache.h>
52 DECLARE_GLOBAL_DATA_PTR;
54 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
56 #if defined(CONFIG_CMD_BDI)
57 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
60 void __attribute__((noinline))
61 do_bootm_linux(cmd_tbl_t *cmdtp, int flag,
62 int argc, char *argv[],
67 int initrd_copy_to_ram = 1;
68 ulong initrd_start, initrd_end;
69 ulong rd_data, rd_len;
70 image_header_t *rd_hdr;
72 ulong cmd_start, cmd_end;
78 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
80 #if defined(CONFIG_OF_LIBFDT)
81 image_header_t *fdt_hdr;
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_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");
180 rd_len = rd_data = 0;
185 debug ("Not skipping initrd\n");
186 show_boot_progress (9);
188 rd_hdr = (image_header_t *)simple_strtoul (argv[2], NULL, 16);
189 printf ("## Loading RAMDisk Image at %08lx ...\n", (ulong)rd_hdr);
191 if (!image_check_magic (rd_hdr)) {
192 puts ("Bad Magic Number\n");
193 show_boot_progress (-10);
194 do_reset (cmdtp, flag, argc, argv);
197 if (!image_check_hcrc (rd_hdr)) {
198 puts ("Bad Header Checksum\n");
199 show_boot_progress (-11);
200 do_reset (cmdtp, flag, argc, argv);
202 show_boot_progress (10);
204 print_image_hdr (rd_hdr);
207 puts (" Verifying Checksum ... ");
209 if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
210 puts ("Bad Data CRC\n");
211 show_boot_progress (-12);
212 do_reset (cmdtp, flag, argc, argv);
217 show_boot_progress (11);
219 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
220 !image_check_arch (rd_hdr, IH_ARCH_PPC) ||
221 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
222 puts ("No Linux PPC Ramdisk Image\n");
223 show_boot_progress (-13);
224 do_reset (cmdtp, flag, argc, argv);
227 rd_data = image_get_data (rd_hdr);
228 rd_len = image_get_data_size (rd_hdr);
231 * Now check if we have a multifile image
233 } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
235 * Get second entry data start address and len
237 image_multi_getimg (hdr, 1, &rd_data, &rd_len);
238 show_boot_progress (13);
243 show_boot_progress (14);
245 rd_len = rd_data = 0;
248 #if defined(CONFIG_OF_LIBFDT)
250 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
251 fdt_hdr = (image_header_t *)of_flat_tree;
253 if (fdt_check_header (of_flat_tree) == 0) {
255 if (addr2info((ulong)of_flat_tree) != NULL)
256 of_data = (ulong)of_flat_tree;
258 } else if (image_check_magic (fdt_hdr)) {
259 ulong image_start, image_end;
260 ulong load_start, load_end;
262 printf ("## Flat Device Tree at %08lX\n", fdt_hdr);
263 print_image_hdr (fdt_hdr);
265 image_start = (ulong)fdt_hdr;
266 image_end = image_get_image_end (fdt_hdr);
268 load_start = image_get_load (fdt_hdr);
269 load_end = load_start + image_get_data_size (fdt_hdr);
271 if ((load_start < image_end) && (load_end > image_start)) {
272 puts ("ERROR: fdt overwritten - "
273 "must RESET the board to recover.\n");
274 do_reset (cmdtp, flag, argc, argv);
277 puts (" Verifying Checksum ... ");
278 if (!image_check_hcrc (fdt_hdr)) {
279 puts ("ERROR: fdt header checksum invalid - "
280 "must RESET the board to recover.\n");
281 do_reset (cmdtp, flag, argc, argv);
284 if (!image_check_dcrc (fdt_hdr)) {
285 puts ("ERROR: fdt checksum invalid - "
286 "must RESET the board to recover.\n");
287 do_reset (cmdtp, flag, argc, argv);
291 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
292 puts ("ERROR: uImage is not a fdt - "
293 "must RESET the board to recover.\n");
294 do_reset (cmdtp, flag, argc, argv);
296 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
297 puts ("ERROR: uImage is compressed - "
298 "must RESET the board to recover.\n");
299 do_reset (cmdtp, flag, argc, argv);
301 if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
302 puts ("ERROR: uImage data is not a fdt - "
303 "must RESET the board to recover.\n");
304 do_reset (cmdtp, flag, argc, argv);
307 memmove ((void *)image_get_load (fdt_hdr),
308 (void *)(of_flat_tree + image_get_header_size ()),
309 image_get_data_size (fdt_hdr));
311 of_flat_tree = (char *)image_get_load (fdt_hdr);
313 puts ("Did not find a flat Flat Device Tree.\n"
314 "Must RESET the board to recover.\n");
315 do_reset (cmdtp, flag, argc, argv);
317 printf (" Booting using the fdt at 0x%x\n",
319 } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
320 ulong fdt_data, fdt_len;
322 image_multi_getimg (hdr, 2, &fdt_data, &fdt_len);
325 of_flat_tree = (char *)fdt_data;
328 /* move the blob if it is in flash (set of_data to !null) */
329 if (addr2info ((ulong)of_flat_tree) != NULL)
330 of_data = (ulong)of_flat_tree;
333 if (fdt_check_header (of_flat_tree) != 0) {
334 puts ("ERROR: image is not a fdt - "
335 "must RESET the board to recover.\n");
336 do_reset (cmdtp, flag, argc, argv);
339 if (be32_to_cpu (fdt_totalsize (of_flat_tree)) != fdt_len) {
340 puts ("ERROR: fdt size != image size - "
341 "must RESET the board to recover.\n");
342 do_reset (cmdtp, flag, argc, argv);
348 debug ("No initrd\n");
352 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
353 initrd_start = rd_data;
354 initrd_end = initrd_start + rd_len;
356 initrd_start = (ulong)kbd - rd_len;
357 initrd_start &= ~(4096 - 1); /* align on page */
363 * the inital ramdisk does not need to be within
364 * CFG_BOOTMAPSZ as it is not accessed until after
365 * the mm system is initialised.
367 * do the stack bottom calculation again and see if
368 * the initrd will fit just below the monitor stack
369 * bottom without overwriting the area allocated
370 * above for command line args and board info.
372 asm( "mr %0,1": "=r"(nsp) : );
373 nsp -= 2048; /* just to be sure */
375 if (nsp > initrd_high) /* limit as specified */
378 nsp &= ~(4096 - 1); /* align on page */
383 show_boot_progress (12);
385 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
386 rd_data, rd_data + rd_len - 1, rd_len, rd_len);
388 initrd_end = initrd_start + rd_len;
389 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
390 initrd_start, initrd_end);
392 memmove_wd((void *)initrd_start,
393 (void *)rd_data, rd_len, CHUNKSZ);
402 #if defined(CONFIG_OF_LIBFDT)
406 * The blob must be within CFG_BOOTMAPSZ,
407 * so we flag it to be copied if it is not.
409 if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
410 of_data = (ulong)of_flat_tree;
413 /* move of_flat_tree if needed */
416 ulong of_start, of_len;
418 of_len = be32_to_cpu(fdt_totalsize(of_data));
420 /* position on a 4K boundary before the kbd */
421 of_start = (ulong)kbd - of_len;
422 of_start &= ~(4096 - 1); /* align on page */
423 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
424 of_data, of_data + of_len - 1, of_len, of_len);
426 of_flat_tree = (char *)of_start;
427 printf (" Loading Device Tree to %08lx, end %08lx ... ",
428 of_start, of_start + of_len - 1);
429 err = fdt_open_into((void *)of_data, (void *)of_start, of_len);
431 puts ("ERROR: fdt move failed - "
432 "must RESET the board to recover.\n");
433 do_reset (cmdtp, flag, argc, argv);
438 * Add the chosen node if it doesn't exist, add the env and bd_t
439 * if the user wants it (the logic is in the subroutines).
442 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
443 puts ("ERROR: /chosen node create failed - "
444 "must RESET the board to recover.\n");
445 do_reset (cmdtp, flag, argc, argv);
447 #ifdef CONFIG_OF_HAS_UBOOT_ENV
448 if (fdt_env(of_flat_tree) < 0) {
449 puts ("ERROR: /u-boot-env node create failed - "
450 "must RESET the board to recover.\n");
451 do_reset (cmdtp, flag, argc, argv);
454 #ifdef CONFIG_OF_HAS_BD_T
455 if (fdt_bd_t(of_flat_tree) < 0) {
456 puts ("ERROR: /bd_t node create failed - "
457 "must RESET the board to recover.\n");
458 do_reset (cmdtp, flag, argc, argv);
461 #ifdef CONFIG_OF_BOARD_SETUP
462 /* Call the board-specific fixup routine */
463 ft_board_setup(of_flat_tree, gd->bd);
466 #endif /* CONFIG_OF_LIBFDT */
468 debug ("## Transferring control to Linux (at address %08lx) ...\n",
471 show_boot_progress (15);
473 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
474 unlock_ram_in_cache();
477 #if defined(CONFIG_OF_LIBFDT)
478 if (of_flat_tree) { /* device tree; boot new style */
480 * Linux Kernel Parameters (passing device tree):
481 * r3: pointer to the fdt, followed by the board info data
482 * r4: physical pointer to the kernel itself
487 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
488 /* does not return */
492 * Linux Kernel Parameters (passing board info data):
493 * r3: ptr to board info data
494 * r4: initrd_start or 0 if no initrd
495 * r5: initrd_end - unused if r4 is 0
496 * r6: Start of command line string
497 * r7: End of command line string
499 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
500 /* does not return */