]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib_ppc/bootm.c
[new uImage] ppc: Determine if we are booting an OF style
[people/ms/u-boot.git] / lib_ppc / bootm.c
CommitLineData
5d3cc55e
MB
1/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
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.
14 *
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.
19 *
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,
23 * MA 02111-1307 USA
24 */
25
7582438c
MB
26#define DEBUG
27
5d3cc55e
MB
28#include <common.h>
29#include <watchdog.h>
30#include <command.h>
31#include <image.h>
32#include <malloc.h>
33#include <zlib.h>
34#include <bzlib.h>
35#include <environment.h>
36#include <asm/byteorder.h>
37
38#if defined(CONFIG_OF_LIBFDT)
39#include <fdt.h>
40#include <libfdt.h>
41#include <fdt_support.h>
d3c5eb6d
MB
42
43static void fdt_error (const char *msg);
d2ced9eb
MB
44static void get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
45 bootm_headers_t *images, char **of_flat_tree, ulong *of_size);
46static ulong fdt_relocate (ulong alloc_current,
47 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
48 char **of_flat_tree, ulong *of_size);
5d3cc55e
MB
49#endif
50
5d3cc55e
MB
51#ifdef CFG_INIT_RAM_LOCK
52#include <asm/cache.h>
53#endif
54
5d3cc55e 55DECLARE_GLOBAL_DATA_PTR;
5d3cc55e 56
5d3cc55e 57extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
ceaed2b1 58static ulong get_sp (void);
b6b0fe64 59static void set_clocks_in_mhz (bd_t *kbd);
5d3cc55e
MB
60
61void __attribute__((noinline))
8a5ea3e6
MB
62do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
63 bootm_headers_t *images)
5d3cc55e 64{
b6b0fe64
MB
65 ulong sp, sp_limit, alloc_current;
66
f13e7b2e 67 ulong initrd_start, initrd_end;
5ad03eb3 68 ulong rd_data_start, rd_data_end, rd_len;
f13e7b2e
MB
69
70 ulong cmd_start, cmd_end;
5d3cc55e 71 bd_t *kbd;
d5934ad7 72 ulong ep = 0;
5d3cc55e 73 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
27953493 74 ulong of_size = 0;
f13e7b2e 75
4a2ad5ff 76#if defined(CONFIG_OF_LIBFDT)
d2ced9eb 77 char *of_flat_tree = NULL;
5d3cc55e
MB
78#endif
79
5d3cc55e
MB
80 /*
81 * Booting a (Linux) kernel image
82 *
83 * Allocate space for command line and board info - the
84 * address should be as high as possible within the reach of
85 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
86 * memory, which means far enough below the current stack
87 * pointer.
88 */
b6b0fe64
MB
89 sp = get_sp();
90 debug ("## Current stack ends at 0x%08lx ", sp);
5d3cc55e 91
b6b0fe64
MB
92 alloc_current = sp_limit = get_boot_sp_limit(sp);
93 debug ("=> set upper limit to 0x%08lx\n", sp_limit);
5d3cc55e 94
27953493
KG
95#if defined(CONFIG_OF_LIBFDT)
96 /* find flattened device tree */
97 get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
98#endif
5d3cc55e 99
27953493
KG
100 if (!of_size) {
101 /* allocate space and init command line */
102 alloc_current = get_boot_cmdline (alloc_current, &cmd_start, &cmd_end);
103
104 /* allocate space for kernel copy of board info */
105 alloc_current = get_boot_kbd (alloc_current, &kbd);
106 set_clocks_in_mhz(kbd);
107 }
5d3cc55e 108
d5934ad7
MB
109 /* find kernel entry point */
110 if (images->legacy_hdr_valid) {
111 ep = image_get_ep (images->legacy_hdr_os);
112#if defined(CONFIG_FIT)
113 } else if (images->fit_uname_os) {
114 fit_unsupported_reset ("PPC linux bootm");
115 do_reset (cmdtp, flag, argc, argv);
116#endif
117 } else {
118 puts ("Could not find kernel entry point!\n");
119 do_reset (cmdtp, flag, argc, argv);
120 }
121 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
5d3cc55e 122
ceaed2b1 123 /* find ramdisk */
8a5ea3e6 124 get_ramdisk (cmdtp, flag, argc, argv, images,
5ad03eb3 125 IH_ARCH_PPC, &rd_data_start, &rd_data_end);
ceaed2b1 126
5ad03eb3 127 rd_len = rd_data_end - rd_data_start;
5d3cc55e 128
b6b0fe64 129 alloc_current = ramdisk_high (alloc_current, rd_data_start, rd_len,
a6612bdf 130 sp_limit, get_sp (), &initrd_start, &initrd_end);
ceaed2b1 131
4a2ad5ff 132#if defined(CONFIG_OF_LIBFDT)
d2ced9eb
MB
133 alloc_current = fdt_relocate (alloc_current,
134 cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
5d3cc55e 135
5d3cc55e
MB
136 /*
137 * Add the chosen node if it doesn't exist, add the env and bd_t
138 * if the user wants it (the logic is in the subroutines).
139 */
d2ced9eb 140 if (of_size) {
5d3cc55e 141 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
d3c5eb6d 142 fdt_error ("/chosen node create failed");
5d3cc55e
MB
143 do_reset (cmdtp, flag, argc, argv);
144 }
145#ifdef CONFIG_OF_HAS_UBOOT_ENV
146 if (fdt_env(of_flat_tree) < 0) {
d3c5eb6d 147 fdt_error ("/u-boot-env node create failed");
5d3cc55e
MB
148 do_reset (cmdtp, flag, argc, argv);
149 }
150#endif
151#ifdef CONFIG_OF_HAS_BD_T
152 if (fdt_bd_t(of_flat_tree) < 0) {
d3c5eb6d 153 fdt_error ("/bd_t node create failed");
5d3cc55e
MB
154 do_reset (cmdtp, flag, argc, argv);
155 }
156#endif
157#ifdef CONFIG_OF_BOARD_SETUP
158 /* Call the board-specific fixup routine */
159 ft_board_setup(of_flat_tree, gd->bd);
160#endif
161 }
4a2ad5ff 162#endif /* CONFIG_OF_LIBFDT */
d45d5a18 163
5d3cc55e
MB
164 debug ("## Transferring control to Linux (at address %08lx) ...\n",
165 (ulong)kernel);
166
167 show_boot_progress (15);
168
169#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
170 unlock_ram_in_cache();
171#endif
172
4a2ad5ff 173#if defined(CONFIG_OF_LIBFDT)
5d3cc55e
MB
174 if (of_flat_tree) { /* device tree; boot new style */
175 /*
176 * Linux Kernel Parameters (passing device tree):
177 * r3: pointer to the fdt, followed by the board info data
178 * r4: physical pointer to the kernel itself
179 * r5: NULL
180 * r6: NULL
181 * r7: NULL
182 */
183 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
184 /* does not return */
185 }
186#endif
187 /*
188 * Linux Kernel Parameters (passing board info data):
189 * r3: ptr to board info data
190 * r4: initrd_start or 0 if no initrd
191 * r5: initrd_end - unused if r4 is 0
192 * r6: Start of command line string
193 * r7: End of command line string
194 */
195 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
196 /* does not return */
197}
d3c5eb6d 198
ceaed2b1
MB
199static ulong get_sp (void)
200{
201 ulong sp;
202
203 asm( "mr %0,1": "=r"(sp) : );
204 return sp;
205}
206
b6b0fe64
MB
207static void set_clocks_in_mhz (bd_t *kbd)
208{
209 char *s;
210
211 if ((s = getenv ("clocks_in_mhz")) != NULL) {
212 /* convert all clock information to MHz */
213 kbd->bi_intfreq /= 1000000L;
214 kbd->bi_busfreq /= 1000000L;
215#if defined(CONFIG_MPC8220)
216 kbd->bi_inpfreq /= 1000000L;
217 kbd->bi_pcifreq /= 1000000L;
218 kbd->bi_pevfreq /= 1000000L;
219 kbd->bi_flbfreq /= 1000000L;
220 kbd->bi_vcofreq /= 1000000L;
221#endif
222#if defined(CONFIG_CPM2)
223 kbd->bi_cpmfreq /= 1000000L;
224 kbd->bi_brgfreq /= 1000000L;
225 kbd->bi_sccfreq /= 1000000L;
226 kbd->bi_vco /= 1000000L;
227#endif
228#if defined(CONFIG_MPC5xxx)
229 kbd->bi_ipbfreq /= 1000000L;
230 kbd->bi_pcifreq /= 1000000L;
231#endif /* CONFIG_MPC5xxx */
232 }
233}
234
d3c5eb6d
MB
235#if defined(CONFIG_OF_LIBFDT)
236static void fdt_error (const char *msg)
237{
238 puts ("ERROR: ");
239 puts (msg);
240 puts (" - must RESET the board to recover.\n");
241}
7b325454 242
ff0734cf
MB
243static image_header_t *image_get_fdt (ulong fdt_addr)
244{
245 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
246
247 image_print_contents (fdt_hdr);
248
249 puts (" Verifying Checksum ... ");
250 if (!image_check_hcrc (fdt_hdr)) {
251 fdt_error ("fdt header checksum invalid");
252 return NULL;
253 }
254
255 if (!image_check_dcrc (fdt_hdr)) {
256 fdt_error ("fdt checksum invalid");
257 return NULL;
258 }
259 puts ("OK\n");
260
261 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
262 fdt_error ("uImage is not a fdt");
263 return NULL;
264 }
265 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
266 fdt_error ("uImage is compressed");
267 return NULL;
268 }
269 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
270 fdt_error ("uImage data is not a fdt");
271 return NULL;
272 }
273 return fdt_hdr;
274}
275
d2ced9eb
MB
276static void get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
277 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
7b325454 278{
d5934ad7 279 ulong fdt_addr;
7b325454 280 image_header_t *fdt_hdr;
d5934ad7 281 char *fdt_blob = NULL;
d5934ad7
MB
282 ulong image_start, image_end;
283 ulong load_start, load_end;
284#if defined(CONFIG_FIT)
285 void *fit_hdr;
286 const char *fit_uname_config = NULL;
287 const char *fit_uname_fdt = NULL;
288 ulong default_addr;
289#endif
7b325454 290
d5934ad7
MB
291 if (argc > 3) {
292#if defined(CONFIG_FIT)
293 /*
294 * If the FDT blob comes from the FIT image and the FIT image
295 * address is omitted in the command line argument, try to use
296 * ramdisk or os FIT image address or default load address.
297 */
298 if (images->fit_uname_rd)
299 default_addr = (ulong)images->fit_hdr_rd;
300 else if (images->fit_uname_os)
301 default_addr = (ulong)images->fit_hdr_os;
302 else
303 default_addr = load_addr;
304
305 if (fit_parse_conf (argv[3], default_addr,
306 &fdt_addr, &fit_uname_config)) {
307 debug ("* fdt: config '%s' from image at 0x%08lx\n",
308 fit_uname_config, fdt_addr);
309 } else if (fit_parse_subimage (argv[3], default_addr,
310 &fdt_addr, &fit_uname_fdt)) {
311 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
312 fit_uname_fdt, fdt_addr);
313 } else
314#endif
315 {
316 fdt_addr = simple_strtoul(argv[3], NULL, 16);
317 debug ("* fdt: cmdline image address = 0x%08lx\n",
318 fdt_addr);
319 }
fff888a1 320
d5934ad7
MB
321 debug ("## Checking for 'FDT'/'FDT image' at %08lx\n",
322 fdt_addr);
fff888a1
MB
323
324 /* copy from dataflash if needed */
d5934ad7 325 fdt_addr = gen_get_image (fdt_addr);
7b325454 326
d5934ad7
MB
327 /*
328 * Check if there is an FDT image at the
329 * address provided in the second bootm argument
330 * check image type, for FIT images get a FIT node.
331 */
332 switch (gen_image_get_format ((void *)fdt_addr)) {
333 case IMAGE_FORMAT_LEGACY:
334 debug ("* fdt: legacy format image\n");
7b325454 335
ff0734cf 336 /* verify fdt_addr points to a valid image header */
d5934ad7 337 printf ("## Flattened Device Tree Legacy Image at %08lx\n",
ff0734cf
MB
338 fdt_addr);
339 fdt_hdr = image_get_fdt (fdt_addr);
340 if (!fdt_hdr)
341 do_reset (cmdtp, flag, argc, argv);
7b325454 342
ff0734cf
MB
343 /*
344 * move image data to the load address,
345 * make sure we don't overwrite initial image
346 */
7b325454
MB
347 image_start = (ulong)fdt_hdr;
348 image_end = image_get_image_end (fdt_hdr);
349
350 load_start = image_get_load (fdt_hdr);
351 load_end = load_start + image_get_data_size (fdt_hdr);
352
353 if ((load_start < image_end) && (load_end > image_start)) {
354 fdt_error ("fdt overwritten");
355 do_reset (cmdtp, flag, argc, argv);
356 }
7b325454 357 memmove ((void *)image_get_load (fdt_hdr),
ff0734cf
MB
358 (void *)image_get_data (fdt_hdr),
359 image_get_data_size (fdt_hdr));
7b325454 360
d5934ad7
MB
361 fdt_blob = (char *)image_get_load (fdt_hdr);
362 break;
d5934ad7 363 case IMAGE_FORMAT_FIT:
4efbe9db
MB
364 /*
365 * This case will catch both: new uImage format
366 * (libfdt based) and raw FDT blob (also libfdt
367 * based).
368 */
369#if defined(CONFIG_FIT)
370 /* check FDT blob vs FIT blob */
371 if (0) { /* FIXME: call FIT format verification */
d5934ad7
MB
372 /*
373 * FIT image
374 */
375 fit_hdr = (void *)fdt_addr;
376 debug ("* fdt: FIT format image\n");
377 fit_unsupported_reset ("PPC fdt");
378 do_reset (cmdtp, flag, argc, argv);
4efbe9db
MB
379 } else
380#endif
381 {
d5934ad7
MB
382 /*
383 * FDT blob
384 */
4efbe9db 385 debug ("* fdt: raw FDT blob\n");
d5934ad7
MB
386 printf ("## Flattened Device Tree blob at %08lx\n", fdt_blob);
387 fdt_blob = (char *)fdt_addr;
388 }
389 break;
d5934ad7
MB
390 default:
391 fdt_error ("Did not find a cmdline Flattened Device Tree");
7b325454
MB
392 do_reset (cmdtp, flag, argc, argv);
393 }
d5934ad7
MB
394
395 printf (" Booting using the fdt blob at 0x%x\n", fdt_blob);
396
397 } else if (images->legacy_hdr_valid &&
398 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
399
7b325454
MB
400 ulong fdt_data, fdt_len;
401
d5934ad7
MB
402 /*
403 * Now check if we have a legacy multi-component image,
404 * get second entry data start address and len.
405 */
7b325454 406 printf ("## Flattened Device Tree from multi "
d5934ad7
MB
407 "component Image at %08lX\n",
408 (ulong)images->legacy_hdr_os);
7b325454 409
d5934ad7 410 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
7b325454
MB
411 if (fdt_len) {
412
d5934ad7
MB
413 fdt_blob = (char *)fdt_data;
414 printf (" Booting using the fdt at 0x%x\n", fdt_blob);
7b325454 415
d5934ad7 416 if (fdt_check_header (fdt_blob) != 0) {
7b325454
MB
417 fdt_error ("image is not a fdt");
418 do_reset (cmdtp, flag, argc, argv);
419 }
420
d5934ad7 421 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
7b325454
MB
422 fdt_error ("fdt size != image size");
423 do_reset (cmdtp, flag, argc, argv);
424 }
425 } else {
d5934ad7
MB
426 fdt_error ("Did not find a Flattened Device Tree "
427 "in a legacy multi-component image");
428 do_reset (cmdtp, flag, argc, argv);
7b325454 429 }
d5934ad7
MB
430 } else {
431 debug ("## No Flattened Device Tree\n");
432 *of_flat_tree = NULL;
d2ced9eb
MB
433 *of_size = 0;
434 return;
435 }
436
437 *of_flat_tree = fdt_blob;
438 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
439 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
440 *of_flat_tree, *of_size);
441}
442
443static ulong fdt_relocate (ulong alloc_current,
444 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
445 char **of_flat_tree, ulong *of_size)
446{
447 char *fdt_blob = *of_flat_tree;
448 ulong relocate = 0;
449 ulong new_alloc_current;
450
451 /* nothing to do */
452 if (*of_size == 0)
d5934ad7 453 return alloc_current;
d2ced9eb
MB
454
455 if (fdt_check_header (fdt_blob) != 0) {
456 fdt_error ("image is not a fdt");
457 do_reset (cmdtp, flag, argc, argv);
7b325454
MB
458 }
459
d5934ad7 460#ifndef CFG_NO_FLASH
d2ced9eb 461 /* move the blob if it is in flash (set relocate) */
d5934ad7 462 if (addr2info ((ulong)fdt_blob) != NULL)
d2ced9eb 463 relocate = 1;
d5934ad7
MB
464#endif
465
7b325454
MB
466#ifdef CFG_BOOTMAPSZ
467 /*
468 * The blob must be within CFG_BOOTMAPSZ,
469 * so we flag it to be copied if it is not.
470 */
d5934ad7 471 if (fdt_blob >= (char *)CFG_BOOTMAPSZ)
d2ced9eb 472 relocate = 1;
7b325454
MB
473#endif
474
475 /* move flattend device tree if needed */
d2ced9eb 476 if (relocate) {
7b325454
MB
477 int err;
478 ulong of_start, of_len;
479
d2ced9eb 480 of_len = *of_size;
7b325454
MB
481
482 /* position on a 4K boundary before the alloc_current */
483 of_start = alloc_current - of_len;
484 of_start &= ~(4096 - 1); /* align on page */
485
486 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
d5934ad7 487 (ulong)fdt_blob, (ulong)fdt_blob + of_len - 1,
7b325454
MB
488 of_len, of_len);
489
490 printf (" Loading Device Tree to %08lx, end %08lx ... ",
491 of_start, of_start + of_len - 1);
492
d5934ad7 493 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
7b325454
MB
494 if (err != 0) {
495 fdt_error ("fdt move failed");
496 do_reset (cmdtp, flag, argc, argv);
497 }
498 puts ("OK\n");
499
500 *of_flat_tree = (char *)of_start;
501 new_alloc_current = of_start;
502 } else {
d5934ad7 503 *of_flat_tree = fdt_blob;
7b325454
MB
504 new_alloc_current = alloc_current;
505 }
506
507 return new_alloc_current;
508}
d3c5eb6d 509#endif