blob: 59cc2a44718ebf83c9d9a60c58f3660c65d6d5b4 [file] [log] [blame]
Marian Balakowicz5d3cc552008-01-08 18:11:43 +01001/*
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
Marian Balakowicz75824382008-01-31 13:20:06 +010026#define DEBUG
27
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010028#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>
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +010042
43static void fdt_error (const char *msg);
Kumar Gala274cea22008-02-27 21:51:46 -060044static int get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +010045 bootm_headers_t *images, char **of_flat_tree, ulong *of_size);
Kumar Galae822d7f2008-02-27 21:51:49 -060046static int fdt_relocate (struct lmb *lmb, ulong bootmap_base,
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +010047 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
48 char **of_flat_tree, ulong *of_size);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010049#endif
50
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010051#ifdef CFG_INIT_RAM_LOCK
52#include <asm/cache.h>
53#endif
54
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010055DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010056
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010057extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
Kumar Galae822d7f2008-02-27 21:51:49 -060058extern ulong get_effective_memsize(void);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +010059static ulong get_sp (void);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010060static void set_clocks_in_mhz (bd_t *kbd);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010061
Kumar Galad3f2fa02008-02-27 21:51:50 -060062#ifndef CFG_LINUX_LOWMEM_MAX_SIZE
63#define CFG_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
64#endif
65
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010066void __attribute__((noinline))
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010067do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
68 bootm_headers_t *images)
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010069{
Kumar Galae822d7f2008-02-27 21:51:49 -060070 ulong sp;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010071
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010072 ulong initrd_start, initrd_end;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010073 ulong rd_data_start, rd_data_end, rd_len;
Kumar Galad3f2fa02008-02-27 21:51:50 -060074 ulong size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010075
Kumar Galae822d7f2008-02-27 21:51:49 -060076 ulong cmd_start, cmd_end, bootmap_base;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010077 bd_t *kbd;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010078 ulong ep = 0;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010079 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
Kumar Galad2bc0952008-02-27 21:51:45 -060080 int ret;
Kumar Gala27953492008-02-27 21:51:44 -060081 ulong of_size = 0;
Kumar Galae822d7f2008-02-27 21:51:49 -060082 struct lmb *lmb = images->lmb;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010083
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +010084#if defined(CONFIG_OF_LIBFDT)
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +010085 char *of_flat_tree = NULL;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010086#endif
87
Kumar Galad3f2fa02008-02-27 21:51:50 -060088 bootmap_base = getenv_bootm_low();
89 size = getenv_bootm_size();
90
91#ifdef DEBUG
92 if (((u64)bootmap_base + size) > (CFG_SDRAM_BASE + (u64)gd->ram_size))
93 puts("WARNING: bootm_low + bootm_size exceed total memory\n");
94 if ((bootmap_base + size) > get_effective_memsize())
95 puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
96#endif
97
98 size = min(size, get_effective_memsize());
99 size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
100
101 if (size < getenv_bootm_size()) {
102 ulong base = bootmap_base + size;
103 printf("WARNING: adjusting available memory to %x\n", size);
104 lmb_reserve(lmb, base, getenv_bootm_size() - size);
105 }
Kumar Galae822d7f2008-02-27 21:51:49 -0600106
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100107 /*
108 * Booting a (Linux) kernel image
109 *
110 * Allocate space for command line and board info - the
111 * address should be as high as possible within the reach of
112 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
113 * memory, which means far enough below the current stack
114 * pointer.
115 */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100116 sp = get_sp();
Kumar Galad3f2fa02008-02-27 21:51:50 -0600117 debug ("## Current stack ends at 0x%08lx\n", sp);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100118
Kumar Galae822d7f2008-02-27 21:51:49 -0600119 /* adjust sp by 1K to be safe */
120 sp -= 1024;
121 lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100122
Kumar Gala27953492008-02-27 21:51:44 -0600123#if defined(CONFIG_OF_LIBFDT)
124 /* find flattened device tree */
Kumar Gala274cea22008-02-27 21:51:46 -0600125 ret = get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
126
127 if (ret)
128 goto error;
Kumar Gala27953492008-02-27 21:51:44 -0600129#endif
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100130
Kumar Gala27953492008-02-27 21:51:44 -0600131 if (!of_size) {
132 /* allocate space and init command line */
Kumar Galae822d7f2008-02-27 21:51:49 -0600133 ret = get_boot_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
134 if (ret) {
135 puts("ERROR with allocation of cmdline\n");
136 goto error;
137 }
Kumar Gala27953492008-02-27 21:51:44 -0600138
139 /* allocate space for kernel copy of board info */
Kumar Galae822d7f2008-02-27 21:51:49 -0600140 ret = get_boot_kbd (lmb, &kbd, bootmap_base);
141 if (ret) {
142 puts("ERROR with allocation of kernel bd\n");
143 goto error;
144 }
Kumar Gala27953492008-02-27 21:51:44 -0600145 set_clocks_in_mhz(kbd);
146 }
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100147
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100148 /* find kernel entry point */
149 if (images->legacy_hdr_valid) {
150 ep = image_get_ep (images->legacy_hdr_os);
151#if defined(CONFIG_FIT)
152 } else if (images->fit_uname_os) {
153 fit_unsupported_reset ("PPC linux bootm");
Kumar Gala274cea22008-02-27 21:51:46 -0600154 goto error;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100155#endif
156 } else {
157 puts ("Could not find kernel entry point!\n");
Kumar Gala274cea22008-02-27 21:51:46 -0600158 goto error;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100159 }
160 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100161
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100162 /* find ramdisk */
Kumar Gala274cea22008-02-27 21:51:46 -0600163 ret = get_ramdisk (cmdtp, flag, argc, argv, images,
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100164 IH_ARCH_PPC, &rd_data_start, &rd_data_end);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100165
Kumar Gala274cea22008-02-27 21:51:46 -0600166 if (ret)
167 goto error;
168
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100169 rd_len = rd_data_end - rd_data_start;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100170
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100171#if defined(CONFIG_OF_LIBFDT)
Kumar Galae822d7f2008-02-27 21:51:49 -0600172 ret = fdt_relocate (lmb, bootmap_base,
173 cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100174
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100175 /*
176 * Add the chosen node if it doesn't exist, add the env and bd_t
177 * if the user wants it (the logic is in the subroutines).
178 */
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100179 if (of_size) {
Kumar Galad2bc0952008-02-27 21:51:45 -0600180 /* pass in dummy initrd info, we'll fix up later */
181 if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100182 fdt_error ("/chosen node create failed");
Kumar Gala274cea22008-02-27 21:51:46 -0600183 goto error;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100184 }
185#ifdef CONFIG_OF_HAS_UBOOT_ENV
186 if (fdt_env(of_flat_tree) < 0) {
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100187 fdt_error ("/u-boot-env node create failed");
Kumar Gala274cea22008-02-27 21:51:46 -0600188 goto error;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100189 }
190#endif
191#ifdef CONFIG_OF_HAS_BD_T
192 if (fdt_bd_t(of_flat_tree) < 0) {
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100193 fdt_error ("/bd_t node create failed");
Kumar Gala274cea22008-02-27 21:51:46 -0600194 goto error;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100195 }
196#endif
197#ifdef CONFIG_OF_BOARD_SETUP
198 /* Call the board-specific fixup routine */
199 ft_board_setup(of_flat_tree, gd->bd);
200#endif
201 }
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100202#endif /* CONFIG_OF_LIBFDT */
Marian Balakowiczd45d5a12008-01-08 18:11:43 +0100203
Kumar Galae822d7f2008-02-27 21:51:49 -0600204 ret = ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
205 if (ret)
206 goto error;
Kumar Galad2bc0952008-02-27 21:51:45 -0600207
208#if defined(CONFIG_OF_LIBFDT)
209 /* fixup the initrd now that we know where it should be */
210 if ((of_flat_tree) && (initrd_start && initrd_end)) {
211 uint64_t addr, size;
212 int total = fdt_num_mem_rsv(of_flat_tree);
213 int j;
214
215 /* Look for the dummy entry and delete it */
216 for (j = 0; j < total; j++) {
217 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
218 if (addr == rd_data_start) {
219 fdt_del_mem_rsv(of_flat_tree, j);
220 break;
221 }
222 }
223
224 ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
225 initrd_end - initrd_start + 1);
226 if (ret < 0) {
227 printf("fdt_chosen: %s\n", fdt_strerror(ret));
Kumar Gala274cea22008-02-27 21:51:46 -0600228 goto error;
Kumar Galad2bc0952008-02-27 21:51:45 -0600229 }
230
231 do_fixup_by_path_u32(of_flat_tree, "/chosen",
232 "linux,initrd-start", initrd_start, 0);
233 do_fixup_by_path_u32(of_flat_tree, "/chosen",
234 "linux,initrd-end", initrd_end, 0);
235 }
236#endif
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100237 debug ("## Transferring control to Linux (at address %08lx) ...\n",
238 (ulong)kernel);
239
240 show_boot_progress (15);
241
242#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
243 unlock_ram_in_cache();
244#endif
245
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100246#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100247 if (of_flat_tree) { /* device tree; boot new style */
248 /*
249 * Linux Kernel Parameters (passing device tree):
250 * r3: pointer to the fdt, followed by the board info data
251 * r4: physical pointer to the kernel itself
252 * r5: NULL
253 * r6: NULL
254 * r7: NULL
255 */
256 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
257 /* does not return */
258 }
259#endif
260 /*
261 * Linux Kernel Parameters (passing board info data):
262 * r3: ptr to board info data
263 * r4: initrd_start or 0 if no initrd
264 * r5: initrd_end - unused if r4 is 0
265 * r6: Start of command line string
266 * r7: End of command line string
267 */
268 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
269 /* does not return */
Kumar Gala274cea22008-02-27 21:51:46 -0600270 return ;
271
272error:
273 do_reset (cmdtp, flag, argc, argv);
274 return ;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100275}
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100276
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100277static ulong get_sp (void)
278{
279 ulong sp;
280
281 asm( "mr %0,1": "=r"(sp) : );
282 return sp;
283}
284
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100285static void set_clocks_in_mhz (bd_t *kbd)
286{
287 char *s;
288
289 if ((s = getenv ("clocks_in_mhz")) != NULL) {
290 /* convert all clock information to MHz */
291 kbd->bi_intfreq /= 1000000L;
292 kbd->bi_busfreq /= 1000000L;
293#if defined(CONFIG_MPC8220)
294 kbd->bi_inpfreq /= 1000000L;
295 kbd->bi_pcifreq /= 1000000L;
296 kbd->bi_pevfreq /= 1000000L;
297 kbd->bi_flbfreq /= 1000000L;
298 kbd->bi_vcofreq /= 1000000L;
299#endif
300#if defined(CONFIG_CPM2)
301 kbd->bi_cpmfreq /= 1000000L;
302 kbd->bi_brgfreq /= 1000000L;
303 kbd->bi_sccfreq /= 1000000L;
304 kbd->bi_vco /= 1000000L;
305#endif
306#if defined(CONFIG_MPC5xxx)
307 kbd->bi_ipbfreq /= 1000000L;
308 kbd->bi_pcifreq /= 1000000L;
309#endif /* CONFIG_MPC5xxx */
310 }
311}
312
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100313#if defined(CONFIG_OF_LIBFDT)
314static void fdt_error (const char *msg)
315{
316 puts ("ERROR: ");
317 puts (msg);
318 puts (" - must RESET the board to recover.\n");
319}
Marian Balakowicz7b325452008-01-31 13:58:20 +0100320
Marian Balakowiczff0734c2008-02-27 11:02:26 +0100321static image_header_t *image_get_fdt (ulong fdt_addr)
322{
323 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
324
325 image_print_contents (fdt_hdr);
326
327 puts (" Verifying Checksum ... ");
328 if (!image_check_hcrc (fdt_hdr)) {
329 fdt_error ("fdt header checksum invalid");
330 return NULL;
331 }
332
333 if (!image_check_dcrc (fdt_hdr)) {
334 fdt_error ("fdt checksum invalid");
335 return NULL;
336 }
337 puts ("OK\n");
338
339 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
340 fdt_error ("uImage is not a fdt");
341 return NULL;
342 }
343 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
344 fdt_error ("uImage is compressed");
345 return NULL;
346 }
347 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
348 fdt_error ("uImage data is not a fdt");
349 return NULL;
350 }
351 return fdt_hdr;
352}
353
Kumar Gala274cea22008-02-27 21:51:46 -0600354static int get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100355 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Marian Balakowicz7b325452008-01-31 13:58:20 +0100356{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100357 ulong fdt_addr;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100358 image_header_t *fdt_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100359 char *fdt_blob = NULL;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100360 ulong image_start, image_end;
361 ulong load_start, load_end;
362#if defined(CONFIG_FIT)
363 void *fit_hdr;
364 const char *fit_uname_config = NULL;
365 const char *fit_uname_fdt = NULL;
366 ulong default_addr;
367#endif
Marian Balakowicz7b325452008-01-31 13:58:20 +0100368
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100369 if (argc > 3) {
370#if defined(CONFIG_FIT)
371 /*
372 * If the FDT blob comes from the FIT image and the FIT image
373 * address is omitted in the command line argument, try to use
374 * ramdisk or os FIT image address or default load address.
375 */
376 if (images->fit_uname_rd)
377 default_addr = (ulong)images->fit_hdr_rd;
378 else if (images->fit_uname_os)
379 default_addr = (ulong)images->fit_hdr_os;
380 else
381 default_addr = load_addr;
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100382
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100383 if (fit_parse_conf (argv[3], default_addr,
384 &fdt_addr, &fit_uname_config)) {
385 debug ("* fdt: config '%s' from image at 0x%08lx\n",
386 fit_uname_config, fdt_addr);
387 } else if (fit_parse_subimage (argv[3], default_addr,
388 &fdt_addr, &fit_uname_fdt)) {
389 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
390 fit_uname_fdt, fdt_addr);
391 } else
392#endif
393 {
394 fdt_addr = simple_strtoul(argv[3], NULL, 16);
395 debug ("* fdt: cmdline image address = 0x%08lx\n",
396 fdt_addr);
397 }
398
399 debug ("## Checking for 'FDT'/'FDT image' at %08lx\n",
400 fdt_addr);
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100401
402 /* copy from dataflash if needed */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100403 fdt_addr = gen_get_image (fdt_addr);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100404
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100405 /*
406 * Check if there is an FDT image at the
407 * address provided in the second bootm argument
408 * check image type, for FIT images get a FIT node.
409 */
410 switch (gen_image_get_format ((void *)fdt_addr)) {
411 case IMAGE_FORMAT_LEGACY:
412 debug ("* fdt: legacy format image\n");
Marian Balakowicz7b325452008-01-31 13:58:20 +0100413
Marian Balakowiczff0734c2008-02-27 11:02:26 +0100414 /* verify fdt_addr points to a valid image header */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100415 printf ("## Flattened Device Tree Legacy Image at %08lx\n",
Marian Balakowiczff0734c2008-02-27 11:02:26 +0100416 fdt_addr);
417 fdt_hdr = image_get_fdt (fdt_addr);
418 if (!fdt_hdr)
Kumar Gala274cea22008-02-27 21:51:46 -0600419 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100420
Marian Balakowiczff0734c2008-02-27 11:02:26 +0100421 /*
422 * move image data to the load address,
423 * make sure we don't overwrite initial image
424 */
Marian Balakowicz7b325452008-01-31 13:58:20 +0100425 image_start = (ulong)fdt_hdr;
426 image_end = image_get_image_end (fdt_hdr);
427
428 load_start = image_get_load (fdt_hdr);
429 load_end = load_start + image_get_data_size (fdt_hdr);
430
431 if ((load_start < image_end) && (load_end > image_start)) {
432 fdt_error ("fdt overwritten");
Kumar Gala274cea22008-02-27 21:51:46 -0600433 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100434 }
Marian Balakowicz7b325452008-01-31 13:58:20 +0100435 memmove ((void *)image_get_load (fdt_hdr),
Marian Balakowiczff0734c2008-02-27 11:02:26 +0100436 (void *)image_get_data (fdt_hdr),
437 image_get_data_size (fdt_hdr));
Marian Balakowicz7b325452008-01-31 13:58:20 +0100438
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100439 fdt_blob = (char *)image_get_load (fdt_hdr);
440 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100441 case IMAGE_FORMAT_FIT:
Marian Balakowicz4efbe9d2008-02-27 11:02:26 +0100442 /*
443 * This case will catch both: new uImage format
444 * (libfdt based) and raw FDT blob (also libfdt
445 * based).
446 */
447#if defined(CONFIG_FIT)
448 /* check FDT blob vs FIT blob */
449 if (0) { /* FIXME: call FIT format verification */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100450 /*
451 * FIT image
452 */
453 fit_hdr = (void *)fdt_addr;
454 debug ("* fdt: FIT format image\n");
455 fit_unsupported_reset ("PPC fdt");
Kumar Gala274cea22008-02-27 21:51:46 -0600456 goto error;
Marian Balakowicz4efbe9d2008-02-27 11:02:26 +0100457 } else
458#endif
459 {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100460 /*
461 * FDT blob
462 */
Marian Balakowicz4efbe9d2008-02-27 11:02:26 +0100463 debug ("* fdt: raw FDT blob\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100464 printf ("## Flattened Device Tree blob at %08lx\n", fdt_blob);
465 fdt_blob = (char *)fdt_addr;
466 }
467 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100468 default:
469 fdt_error ("Did not find a cmdline Flattened Device Tree");
Kumar Gala274cea22008-02-27 21:51:46 -0600470 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100471 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100472
473 printf (" Booting using the fdt blob at 0x%x\n", fdt_blob);
474
475 } else if (images->legacy_hdr_valid &&
476 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
477
Marian Balakowicz7b325452008-01-31 13:58:20 +0100478 ulong fdt_data, fdt_len;
479
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100480 /*
481 * Now check if we have a legacy multi-component image,
482 * get second entry data start address and len.
483 */
Marian Balakowicz7b325452008-01-31 13:58:20 +0100484 printf ("## Flattened Device Tree from multi "
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100485 "component Image at %08lX\n",
486 (ulong)images->legacy_hdr_os);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100487
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100488 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100489 if (fdt_len) {
490
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100491 fdt_blob = (char *)fdt_data;
492 printf (" Booting using the fdt at 0x%x\n", fdt_blob);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100493
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100494 if (fdt_check_header (fdt_blob) != 0) {
Marian Balakowicz7b325452008-01-31 13:58:20 +0100495 fdt_error ("image is not a fdt");
Kumar Gala274cea22008-02-27 21:51:46 -0600496 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100497 }
498
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100499 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
Marian Balakowicz7b325452008-01-31 13:58:20 +0100500 fdt_error ("fdt size != image size");
Kumar Gala274cea22008-02-27 21:51:46 -0600501 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100502 }
503 } else {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100504 fdt_error ("Did not find a Flattened Device Tree "
505 "in a legacy multi-component image");
Kumar Gala274cea22008-02-27 21:51:46 -0600506 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100507 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100508 } else {
509 debug ("## No Flattened Device Tree\n");
510 *of_flat_tree = NULL;
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100511 *of_size = 0;
Kumar Gala274cea22008-02-27 21:51:46 -0600512 return 0;
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100513 }
514
515 *of_flat_tree = fdt_blob;
516 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
517 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
518 *of_flat_tree, *of_size);
Kumar Gala274cea22008-02-27 21:51:46 -0600519
520 return 0;
521
522error:
523 do_reset (cmdtp, flag, argc, argv);
524 return 1;
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100525}
526
Kumar Galae822d7f2008-02-27 21:51:49 -0600527static int fdt_relocate (struct lmb *lmb, ulong bootmap_base,
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100528 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
529 char **of_flat_tree, ulong *of_size)
530{
531 char *fdt_blob = *of_flat_tree;
532 ulong relocate = 0;
Kumar Galae822d7f2008-02-27 21:51:49 -0600533 ulong of_len = 0;
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100534
535 /* nothing to do */
536 if (*of_size == 0)
Kumar Galae822d7f2008-02-27 21:51:49 -0600537 return 0;
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100538
539 if (fdt_check_header (fdt_blob) != 0) {
540 fdt_error ("image is not a fdt");
Kumar Gala274cea22008-02-27 21:51:46 -0600541 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100542 }
543
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100544#ifndef CFG_NO_FLASH
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100545 /* move the blob if it is in flash (set relocate) */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100546 if (addr2info ((ulong)fdt_blob) != NULL)
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100547 relocate = 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100548#endif
549
Marian Balakowicz7b325452008-01-31 13:58:20 +0100550 /*
551 * The blob must be within CFG_BOOTMAPSZ,
552 * so we flag it to be copied if it is not.
553 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100554 if (fdt_blob >= (char *)CFG_BOOTMAPSZ)
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100555 relocate = 1;
Kumar Galae822d7f2008-02-27 21:51:49 -0600556
557 of_len = be32_to_cpu (fdt_totalsize (fdt));
Marian Balakowicz7b325452008-01-31 13:58:20 +0100558
559 /* move flattend device tree if needed */
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100560 if (relocate) {
Marian Balakowicz7b325452008-01-31 13:58:20 +0100561 int err;
Kumar Galae822d7f2008-02-27 21:51:49 -0600562 ulong of_start;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100563
564 /* position on a 4K boundary before the alloc_current */
Kumar Galae822d7f2008-02-27 21:51:49 -0600565 of_start = lmb_alloc_base(lmb, of_len, 0x1000,
566 (CFG_BOOTMAPSZ + bootmap_base));
567
568 if (of_start == 0) {
569 puts("device tree - allocation error\n");
570 goto error;
571 }
Marian Balakowicz7b325452008-01-31 13:58:20 +0100572
573 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100574 (ulong)fdt_blob, (ulong)fdt_blob + of_len - 1,
Marian Balakowicz7b325452008-01-31 13:58:20 +0100575 of_len, of_len);
576
577 printf (" Loading Device Tree to %08lx, end %08lx ... ",
578 of_start, of_start + of_len - 1);
579
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100580 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100581 if (err != 0) {
582 fdt_error ("fdt move failed");
Kumar Gala274cea22008-02-27 21:51:46 -0600583 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100584 }
585 puts ("OK\n");
586
587 *of_flat_tree = (char *)of_start;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100588 } else {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100589 *of_flat_tree = fdt_blob;
Kumar Galae822d7f2008-02-27 21:51:49 -0600590 lmb_reserve(lmb, (ulong)fdt, of_len);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100591 }
592
Kumar Galae822d7f2008-02-27 21:51:49 -0600593 return 0;
Kumar Gala274cea22008-02-27 21:51:46 -0600594
595error:
Kumar Gala274cea22008-02-27 21:51:46 -0600596 return 1;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100597}
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100598#endif