blob: 8cdace2859474dbba5ade46ea6cef5deadad7067 [file] [log] [blame]
Marian Balakowicz2437cf22008-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 Balakowiczf92332b2008-01-31 13:20:06 +010026#define DEBUG
27
Marian Balakowicz2437cf22008-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 Balakowicz28fb6152008-01-31 13:20:08 +010042
43static void fdt_error (const char *msg);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +010044static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
Marian Balakowicz6881dae2008-02-04 08:28:17 +010045 bootm_headers_t *images, char **of_flat_tree, ulong *of_size);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +010046static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
Marian Balakowicz6881dae2008-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 Balakowicz2437cf22008-01-08 18:11:43 +010049#endif
50
Marian Balakowicz2437cf22008-01-08 18:11:43 +010051#ifdef CFG_INIT_RAM_LOCK
52#include <asm/cache.h>
53#endif
54
Marian Balakowicz2437cf22008-01-08 18:11:43 +010055DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010056
Marian Balakowicz2437cf22008-01-08 18:11:43 +010057extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
Kumar Galab937bb72008-02-27 21:51:49 -060058extern ulong get_effective_memsize(void);
Marian Balakowicz9c701e82008-01-31 13:57:17 +010059static ulong get_sp (void);
Marian Balakowicz2efc2642008-01-31 13:58:13 +010060static void set_clocks_in_mhz (bd_t *kbd);
Marian Balakowicz2437cf22008-01-08 18:11:43 +010061
Kumar Galac5bacfd2008-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 Balakowicz2437cf22008-01-08 18:11:43 +010066void __attribute__((noinline))
Marian Balakowicz61fde552008-02-27 11:01:04 +010067do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
68 bootm_headers_t *images)
Marian Balakowicz2437cf22008-01-08 18:11:43 +010069{
Kumar Galab937bb72008-02-27 21:51:49 -060070 ulong sp;
Marian Balakowicz2efc2642008-01-31 13:58:13 +010071
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010072 ulong initrd_start, initrd_end;
Marian Balakowicz95418502008-01-31 13:55:39 +010073 ulong rd_data_start, rd_data_end, rd_len;
Kumar Galac5bacfd2008-02-27 21:51:50 -060074 ulong size;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010075
Kumar Galab937bb72008-02-27 21:51:49 -060076 ulong cmd_start, cmd_end, bootmap_base;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010077 bd_t *kbd;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010078 ulong ep = 0;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010079 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
Kumar Gala56bdf1d2008-02-27 21:51:45 -060080 int ret;
Kumar Galaa56d7d52008-02-27 21:51:44 -060081 ulong of_size = 0;
Kumar Galab937bb72008-02-27 21:51:49 -060082 struct lmb *lmb = images->lmb;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010083
Marian Balakowiczd2160852008-01-31 13:20:07 +010084#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz6881dae2008-02-04 08:28:17 +010085 char *of_flat_tree = NULL;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010086#endif
87
Kumar Galac5bacfd2008-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 Galab937bb72008-02-27 21:51:49 -0600106
Marian Balakowicz2437cf22008-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 Balakowicz2efc2642008-01-31 13:58:13 +0100116 sp = get_sp();
Kumar Galac5bacfd2008-02-27 21:51:50 -0600117 debug ("## Current stack ends at 0x%08lx\n", sp);
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100118
Kumar Galab937bb72008-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 Balakowicz2437cf22008-01-08 18:11:43 +0100122
Kumar Galaa56d7d52008-02-27 21:51:44 -0600123#if defined(CONFIG_OF_LIBFDT)
124 /* find flattened device tree */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100125 ret = boot_get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600126
127 if (ret)
128 goto error;
Kumar Galaa56d7d52008-02-27 21:51:44 -0600129#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100130
Kumar Galaa56d7d52008-02-27 21:51:44 -0600131 if (!of_size) {
132 /* allocate space and init command line */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100133 ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -0600134 if (ret) {
135 puts("ERROR with allocation of cmdline\n");
136 goto error;
137 }
Kumar Galaa56d7d52008-02-27 21:51:44 -0600138
139 /* allocate space for kernel copy of board info */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100140 ret = boot_get_kbd (lmb, &kbd, bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -0600141 if (ret) {
142 puts("ERROR with allocation of kernel bd\n");
143 goto error;
144 }
Kumar Galaa56d7d52008-02-27 21:51:44 -0600145 set_clocks_in_mhz(kbd);
146 }
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100147
Marian Balakowiczdbdd16a2008-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) {
Marian Balakowiczdf8ff332008-03-12 10:33:00 +0100153 ret = fit_image_get_entry (images->fit_hdr_os,
154 images->fit_noffset_os, &ep);
155 if (ret) {
156 puts ("Can't get entry point property!\n");
157 goto error;
158 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100159#endif
160 } else {
161 puts ("Could not find kernel entry point!\n");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600162 goto error;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100163 }
164 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100165
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100166 /* find ramdisk */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100167 ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_PPC,
168 &rd_data_start, &rd_data_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600169 if (ret)
170 goto error;
171
Marian Balakowicz95418502008-01-31 13:55:39 +0100172 rd_len = rd_data_end - rd_data_start;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100173
Marian Balakowiczd2160852008-01-31 13:20:07 +0100174#if defined(CONFIG_OF_LIBFDT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100175 ret = boot_relocate_fdt (lmb, bootmap_base,
Kumar Galab937bb72008-02-27 21:51:49 -0600176 cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100177
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100178 /*
179 * Add the chosen node if it doesn't exist, add the env and bd_t
180 * if the user wants it (the logic is in the subroutines).
181 */
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100182 if (of_size) {
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600183 /* pass in dummy initrd info, we'll fix up later */
184 if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100185 fdt_error ("/chosen node create failed");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600186 goto error;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100187 }
188#ifdef CONFIG_OF_HAS_UBOOT_ENV
189 if (fdt_env(of_flat_tree) < 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100190 fdt_error ("/u-boot-env node create failed");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600191 goto error;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100192 }
193#endif
194#ifdef CONFIG_OF_HAS_BD_T
195 if (fdt_bd_t(of_flat_tree) < 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100196 fdt_error ("/bd_t node create failed");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600197 goto error;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100198 }
199#endif
200#ifdef CONFIG_OF_BOARD_SETUP
201 /* Call the board-specific fixup routine */
202 ft_board_setup(of_flat_tree, gd->bd);
203#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100204 }
Marian Balakowiczd2160852008-01-31 13:20:07 +0100205#endif /* CONFIG_OF_LIBFDT */
Marian Balakowicz751b2b82008-01-08 18:11:43 +0100206
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100207 ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
Kumar Galab937bb72008-02-27 21:51:49 -0600208 if (ret)
209 goto error;
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600210
211#if defined(CONFIG_OF_LIBFDT)
212 /* fixup the initrd now that we know where it should be */
213 if ((of_flat_tree) && (initrd_start && initrd_end)) {
214 uint64_t addr, size;
215 int total = fdt_num_mem_rsv(of_flat_tree);
216 int j;
217
218 /* Look for the dummy entry and delete it */
219 for (j = 0; j < total; j++) {
220 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
221 if (addr == rd_data_start) {
222 fdt_del_mem_rsv(of_flat_tree, j);
223 break;
224 }
225 }
226
227 ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
228 initrd_end - initrd_start + 1);
229 if (ret < 0) {
230 printf("fdt_chosen: %s\n", fdt_strerror(ret));
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600231 goto error;
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600232 }
233
234 do_fixup_by_path_u32(of_flat_tree, "/chosen",
235 "linux,initrd-start", initrd_start, 0);
236 do_fixup_by_path_u32(of_flat_tree, "/chosen",
237 "linux,initrd-end", initrd_end, 0);
238 }
239#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100240 debug ("## Transferring control to Linux (at address %08lx) ...\n",
241 (ulong)kernel);
242
243 show_boot_progress (15);
244
245#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
246 unlock_ram_in_cache();
247#endif
Kumar Galaaa0826b2008-02-27 21:51:51 -0600248 if (!images->autostart)
249 return ;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100250
Marian Balakowiczd2160852008-01-31 13:20:07 +0100251#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100252 if (of_flat_tree) { /* device tree; boot new style */
253 /*
254 * Linux Kernel Parameters (passing device tree):
255 * r3: pointer to the fdt, followed by the board info data
256 * r4: physical pointer to the kernel itself
257 * r5: NULL
258 * r6: NULL
259 * r7: NULL
260 */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100261 debug (" Booting using OF flat tree...\n");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100262 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
263 /* does not return */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100264 } else
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100265#endif
Marian Balakowicz0d871362008-03-12 10:32:53 +0100266 {
267 /*
268 * Linux Kernel Parameters (passing board info data):
269 * r3: ptr to board info data
270 * r4: initrd_start or 0 if no initrd
271 * r5: initrd_end - unused if r4 is 0
272 * r6: Start of command line string
273 * r7: End of command line string
274 */
275 debug (" Booting using board info...\n");
276 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
277 /* does not return */
278 }
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600279 return ;
280
281error:
Kumar Galaaa0826b2008-02-27 21:51:51 -0600282 if (images->autostart)
283 do_reset (cmdtp, flag, argc, argv);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600284 return ;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100285}
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100286
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100287static ulong get_sp (void)
288{
289 ulong sp;
290
291 asm( "mr %0,1": "=r"(sp) : );
292 return sp;
293}
294
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100295static void set_clocks_in_mhz (bd_t *kbd)
296{
297 char *s;
298
299 if ((s = getenv ("clocks_in_mhz")) != NULL) {
300 /* convert all clock information to MHz */
301 kbd->bi_intfreq /= 1000000L;
302 kbd->bi_busfreq /= 1000000L;
303#if defined(CONFIG_MPC8220)
304 kbd->bi_inpfreq /= 1000000L;
305 kbd->bi_pcifreq /= 1000000L;
306 kbd->bi_pevfreq /= 1000000L;
307 kbd->bi_flbfreq /= 1000000L;
308 kbd->bi_vcofreq /= 1000000L;
309#endif
310#if defined(CONFIG_CPM2)
311 kbd->bi_cpmfreq /= 1000000L;
312 kbd->bi_brgfreq /= 1000000L;
313 kbd->bi_sccfreq /= 1000000L;
314 kbd->bi_vco /= 1000000L;
315#endif
316#if defined(CONFIG_MPC5xxx)
317 kbd->bi_ipbfreq /= 1000000L;
318 kbd->bi_pcifreq /= 1000000L;
319#endif /* CONFIG_MPC5xxx */
320 }
321}
322
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100323#if defined(CONFIG_OF_LIBFDT)
324static void fdt_error (const char *msg)
325{
326 puts ("ERROR: ");
327 puts (msg);
328 puts (" - must RESET the board to recover.\n");
329}
Marian Balakowicz25154b62008-01-31 13:58:20 +0100330
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100331static image_header_t *image_get_fdt (ulong fdt_addr)
332{
333 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
334
335 image_print_contents (fdt_hdr);
336
337 puts (" Verifying Checksum ... ");
338 if (!image_check_hcrc (fdt_hdr)) {
339 fdt_error ("fdt header checksum invalid");
340 return NULL;
341 }
342
343 if (!image_check_dcrc (fdt_hdr)) {
344 fdt_error ("fdt checksum invalid");
345 return NULL;
346 }
347 puts ("OK\n");
348
349 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
350 fdt_error ("uImage is not a fdt");
351 return NULL;
352 }
353 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
354 fdt_error ("uImage is compressed");
355 return NULL;
356 }
357 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
358 fdt_error ("uImage data is not a fdt");
359 return NULL;
360 }
361 return fdt_hdr;
362}
363
Marian Balakowicz0d871362008-03-12 10:32:53 +0100364/**
365 * fit_check_fdt - verify FIT format FDT subimage
366 * @fit_hdr: pointer to the FIT header
367 * fdt_noffset: FDT subimage node offset within FIT image
368 * @verify: data CRC verification flag
369 *
370 * fit_check_fdt() verifies integrity of the FDT subimage and from
371 * specified FIT image.
372 *
373 * returns:
374 * 1, on success
375 * 0, on failure
376 */
377#if defined(CONFIG_FIT)
378static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
379{
380 fit_image_print (fit, fdt_noffset, " ");
381
382 if (verify) {
383 puts (" Verifying Hash Integrity ... ");
384 if (!fit_image_check_hashes (fit, fdt_noffset)) {
385 fdt_error ("Bad Data Hash");
386 return 0;
387 }
388 puts ("OK\n");
389 }
390
391 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
392 fdt_error ("Not a FDT image");
393 return 0;
394 }
395
396 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
397 fdt_error ("FDT image is compressed");
398 return 0;
399 }
400
401 return 1;
402}
403#endif /* CONFIG_FIT */
404
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100405static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100406 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Marian Balakowicz25154b62008-01-31 13:58:20 +0100407{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100408 ulong fdt_addr;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100409 image_header_t *fdt_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100410 char *fdt_blob = NULL;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100411 ulong image_start, image_end;
412 ulong load_start, load_end;
413#if defined(CONFIG_FIT)
414 void *fit_hdr;
415 const char *fit_uname_config = NULL;
416 const char *fit_uname_fdt = NULL;
417 ulong default_addr;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100418 int cfg_noffset;
Marian Balakowicz0d871362008-03-12 10:32:53 +0100419 int fdt_noffset;
420 const void *data;
421 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100422#endif
Marian Balakowicz25154b62008-01-31 13:58:20 +0100423
Marian Balakowicz0d871362008-03-12 10:32:53 +0100424 *of_flat_tree = NULL;
425 *of_size = 0;
426
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100427 if (argc > 3 || genimg_has_config (images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100428#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100429 if (argc > 3) {
430 /*
431 * If the FDT blob comes from the FIT image and the
432 * FIT image address is omitted in the command line
433 * argument, try to use ramdisk or os FIT image
434 * address or default load address.
435 */
436 if (images->fit_uname_rd)
437 default_addr = (ulong)images->fit_hdr_rd;
438 else if (images->fit_uname_os)
439 default_addr = (ulong)images->fit_hdr_os;
440 else
441 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100442
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100443 if (fit_parse_conf (argv[3], default_addr,
444 &fdt_addr, &fit_uname_config)) {
445 debug ("* fdt: config '%s' from image at 0x%08lx\n",
446 fit_uname_config, fdt_addr);
447 } else if (fit_parse_subimage (argv[3], default_addr,
448 &fdt_addr, &fit_uname_fdt)) {
449 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
450 fit_uname_fdt, fdt_addr);
451 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100452#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100453 {
454 fdt_addr = simple_strtoul(argv[3], NULL, 16);
455 debug ("* fdt: cmdline image address = 0x%08lx\n",
456 fdt_addr);
457 }
458#if defined(CONFIG_FIT)
459 } else {
460 /* use FIT configuration provided in first bootm
461 * command argument
462 */
463 fdt_addr = (ulong)images->fit_hdr_os;
464 fit_uname_config = images->fit_uname_cfg;
465 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
466 fit_uname_config, fdt_addr);
467
468 /*
469 * Check whether configuration has FDT blob defined,
470 * if not quit silently.
471 */
472 fit_hdr = (void *)fdt_addr;
473 cfg_noffset = fit_conf_get_node (fit_hdr,
474 fit_uname_config);
475 if (cfg_noffset < 0) {
476 debug ("* fdt: no such config\n");
477 return 0;
478 }
479
480 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
481 cfg_noffset);
482 if (fdt_noffset < 0) {
483 debug ("* fdt: no fdt in config\n");
484 return 0;
485 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100486 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100487#endif
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100488
Marian Balakowicz0d871362008-03-12 10:32:53 +0100489 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100490 fdt_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100491
492 /* copy from dataflash if needed */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100493 fdt_addr = genimg_get_image (fdt_addr);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100494
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100495 /*
496 * Check if there is an FDT image at the
497 * address provided in the second bootm argument
498 * check image type, for FIT images get a FIT node.
499 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100500 switch (genimg_get_format ((void *)fdt_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100501 case IMAGE_FORMAT_LEGACY:
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100502 /* verify fdt_addr points to a valid image header */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100503 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100504 fdt_addr);
505 fdt_hdr = image_get_fdt (fdt_addr);
506 if (!fdt_hdr)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600507 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100508
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100509 /*
510 * move image data to the load address,
511 * make sure we don't overwrite initial image
512 */
Marian Balakowicz25154b62008-01-31 13:58:20 +0100513 image_start = (ulong)fdt_hdr;
514 image_end = image_get_image_end (fdt_hdr);
515
516 load_start = image_get_load (fdt_hdr);
517 load_end = load_start + image_get_data_size (fdt_hdr);
518
519 if ((load_start < image_end) && (load_end > image_start)) {
520 fdt_error ("fdt overwritten");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600521 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100522 }
Marian Balakowicz0d871362008-03-12 10:32:53 +0100523
524 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
525 image_get_data (fdt_hdr), load_start);
526
527 memmove ((void *)load_start,
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100528 (void *)image_get_data (fdt_hdr),
529 image_get_data_size (fdt_hdr));
Marian Balakowicz25154b62008-01-31 13:58:20 +0100530
Marian Balakowicz0d871362008-03-12 10:32:53 +0100531 fdt_blob = (char *)load_start;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100532 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100533 case IMAGE_FORMAT_FIT:
Marian Balakowicz12560682008-02-27 11:02:26 +0100534 /*
535 * This case will catch both: new uImage format
536 * (libfdt based) and raw FDT blob (also libfdt
537 * based).
538 */
539#if defined(CONFIG_FIT)
540 /* check FDT blob vs FIT blob */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100541 if (fit_check_format ((const void *)fdt_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100542 /*
543 * FIT image
544 */
545 fit_hdr = (void *)fdt_addr;
Marian Balakowicz0d871362008-03-12 10:32:53 +0100546 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
547 fdt_addr);
548
549 if (!fit_uname_fdt) {
550 /*
551 * no FDT blob image node unit name,
552 * try to get config node first. If
553 * config unit node name is NULL
554 * fit_conf_get_node() will try to
555 * find default config node
556 */
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100557 cfg_noffset = fit_conf_get_node (fit_hdr,
Marian Balakowicz0d871362008-03-12 10:32:53 +0100558 fit_uname_config);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100559
560 if (cfg_noffset < 0) {
561 fdt_error ("Could not find configuration node\n");
Marian Balakowicz0d871362008-03-12 10:32:53 +0100562 goto error;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100563 }
564
565 fit_uname_config = fdt_get_name (fit_hdr,
566 cfg_noffset, NULL);
567 printf (" Using '%s' configuration\n",
568 fit_uname_config);
Marian Balakowicz0d871362008-03-12 10:32:53 +0100569
570 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100571 cfg_noffset);
Marian Balakowicz0d871362008-03-12 10:32:53 +0100572 fit_uname_fdt = fit_get_name (fit_hdr,
573 fdt_noffset, NULL);
574 } else {
575 /* get FDT component image node offset */
576 fdt_noffset = fit_image_get_node (fit_hdr,
577 fit_uname_fdt);
578 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100579 if (fdt_noffset < 0) {
580 fdt_error ("Could not find subimage node\n");
Marian Balakowicz0d871362008-03-12 10:32:53 +0100581 goto error;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100582 }
Marian Balakowicz0d871362008-03-12 10:32:53 +0100583
584 printf (" Trying '%s' FDT blob subimage\n",
585 fit_uname_fdt);
586
587 if (!fit_check_fdt (fit_hdr, fdt_noffset,
588 images->verify))
589 goto error;
590
591 /* get ramdisk image data address and length */
592 if (fit_image_get_data (fit_hdr, fdt_noffset,
593 &data, &size)) {
594 fdt_error ("Could not find FDT subimage data");
595 goto error;
596 }
597
598 /* verift that image data is a proper FDT blob */
599 if (fdt_check_header ((char *)data) != 0) {
600 fdt_error ("Subimage data is not a FTD");
601 goto error;
602 }
603
604 /*
605 * move image data to the load address,
606 * make sure we don't overwrite initial image
607 */
608 image_start = (ulong)fit_hdr;
609 image_end = fit_get_end (fit_hdr);
610
611 if (fit_image_get_load (fit_hdr, fdt_noffset,
612 &load_start) == 0) {
613 load_end = load_start + size;
614
615 if ((load_start < image_end) &&
616 (load_end > image_start)) {
617 fdt_error ("FDT overwritten");
618 goto error;
619 }
620
621 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
622 (ulong)data, load_start);
623
624 memmove ((void *)load_start,
625 (void *)data, size);
626
627 fdt_blob = (char *)load_start;
628 } else {
629 fdt_blob = (char *)data;
630 }
631
632 images->fit_hdr_fdt = fit_hdr;
633 images->fit_uname_fdt = fit_uname_fdt;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100634 images->fit_noffset_fdt = fdt_noffset;
Marian Balakowicz0d871362008-03-12 10:32:53 +0100635 break;
Marian Balakowicz12560682008-02-27 11:02:26 +0100636 } else
637#endif
638 {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100639 /*
640 * FDT blob
641 */
Marian Balakowicz12560682008-02-27 11:02:26 +0100642 debug ("* fdt: raw FDT blob\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100643 printf ("## Flattened Device Tree blob at %08lx\n", fdt_blob);
644 fdt_blob = (char *)fdt_addr;
645 }
646 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100647 default:
648 fdt_error ("Did not find a cmdline Flattened Device Tree");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600649 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100650 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100651
652 printf (" Booting using the fdt blob at 0x%x\n", fdt_blob);
653
654 } else if (images->legacy_hdr_valid &&
655 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
656
Marian Balakowicz25154b62008-01-31 13:58:20 +0100657 ulong fdt_data, fdt_len;
658
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100659 /*
660 * Now check if we have a legacy multi-component image,
661 * get second entry data start address and len.
662 */
Marian Balakowicz25154b62008-01-31 13:58:20 +0100663 printf ("## Flattened Device Tree from multi "
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100664 "component Image at %08lX\n",
665 (ulong)images->legacy_hdr_os);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100666
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100667 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100668 if (fdt_len) {
669
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100670 fdt_blob = (char *)fdt_data;
671 printf (" Booting using the fdt at 0x%x\n", fdt_blob);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100672
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100673 if (fdt_check_header (fdt_blob) != 0) {
Marian Balakowicz25154b62008-01-31 13:58:20 +0100674 fdt_error ("image is not a fdt");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600675 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100676 }
677
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100678 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
Marian Balakowicz25154b62008-01-31 13:58:20 +0100679 fdt_error ("fdt size != image size");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600680 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100681 }
682 } else {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100683 fdt_error ("Did not find a Flattened Device Tree "
684 "in a legacy multi-component image");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600685 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100686 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100687 } else {
688 debug ("## No Flattened Device Tree\n");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600689 return 0;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100690 }
691
692 *of_flat_tree = fdt_blob;
693 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
694 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
695 *of_flat_tree, *of_size);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600696
697 return 0;
698
699error:
700 do_reset (cmdtp, flag, argc, argv);
701 return 1;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100702}
703
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100704static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100705 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
706 char **of_flat_tree, ulong *of_size)
707{
708 char *fdt_blob = *of_flat_tree;
709 ulong relocate = 0;
Kumar Galab937bb72008-02-27 21:51:49 -0600710 ulong of_len = 0;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100711
712 /* nothing to do */
713 if (*of_size == 0)
Kumar Galab937bb72008-02-27 21:51:49 -0600714 return 0;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100715
716 if (fdt_check_header (fdt_blob) != 0) {
717 fdt_error ("image is not a fdt");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600718 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100719 }
720
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100721#ifndef CFG_NO_FLASH
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100722 /* move the blob if it is in flash (set relocate) */
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100723 if (addr2info ((ulong)fdt_blob) != NULL)
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100724 relocate = 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100725#endif
726
Marian Balakowicz25154b62008-01-31 13:58:20 +0100727 /*
728 * The blob must be within CFG_BOOTMAPSZ,
729 * so we flag it to be copied if it is not.
730 */
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100731 if (fdt_blob >= (char *)CFG_BOOTMAPSZ)
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100732 relocate = 1;
Kumar Galab937bb72008-02-27 21:51:49 -0600733
Marian Balakowiczb5ec1b92008-02-29 22:22:46 +0100734 of_len = be32_to_cpu (fdt_totalsize (fdt_blob));
Marian Balakowicz25154b62008-01-31 13:58:20 +0100735
736 /* move flattend device tree if needed */
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100737 if (relocate) {
Marian Balakowicz25154b62008-01-31 13:58:20 +0100738 int err;
Kumar Galab937bb72008-02-27 21:51:49 -0600739 ulong of_start;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100740
741 /* position on a 4K boundary before the alloc_current */
Kumar Galab937bb72008-02-27 21:51:49 -0600742 of_start = lmb_alloc_base(lmb, of_len, 0x1000,
743 (CFG_BOOTMAPSZ + bootmap_base));
744
745 if (of_start == 0) {
746 puts("device tree - allocation error\n");
747 goto error;
748 }
Marian Balakowicz25154b62008-01-31 13:58:20 +0100749
750 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100751 (ulong)fdt_blob, (ulong)fdt_blob + of_len - 1,
Marian Balakowicz25154b62008-01-31 13:58:20 +0100752 of_len, of_len);
753
754 printf (" Loading Device Tree to %08lx, end %08lx ... ",
755 of_start, of_start + of_len - 1);
756
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100757 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100758 if (err != 0) {
759 fdt_error ("fdt move failed");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600760 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100761 }
762 puts ("OK\n");
763
764 *of_flat_tree = (char *)of_start;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100765 } else {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100766 *of_flat_tree = fdt_blob;
Kumar Galab937bb72008-02-27 21:51:49 -0600767 lmb_reserve(lmb, (ulong)fdt, of_len);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100768 }
769
Kumar Galab937bb72008-02-27 21:51:49 -0600770 return 0;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600771
772error:
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600773 return 1;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100774}
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100775#endif