blob: 89463e342f0f4accb4175ed1f67961986b397205 [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
Marian Balakowicz2437cf22008-01-08 18:11:43 +010027#include <common.h>
28#include <watchdog.h>
29#include <command.h>
30#include <image.h>
31#include <malloc.h>
32#include <zlib.h>
33#include <bzlib.h>
34#include <environment.h>
35#include <asm/byteorder.h>
36
37#if defined(CONFIG_OF_LIBFDT)
38#include <fdt.h>
39#include <libfdt.h>
40#include <fdt_support.h>
Marian Balakowicz28fb6152008-01-31 13:20:08 +010041
42static void fdt_error (const char *msg);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +010043static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
Marian Balakowicz6881dae2008-02-04 08:28:17 +010044 bootm_headers_t *images, char **of_flat_tree, ulong *of_size);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +010045static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
Marian Balakowicz6881dae2008-02-04 08:28:17 +010046 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
47 char **of_flat_tree, ulong *of_size);
Marian Balakowicz2437cf22008-01-08 18:11:43 +010048#endif
49
Marian Balakowicz2437cf22008-01-08 18:11:43 +010050#ifdef CFG_INIT_RAM_LOCK
51#include <asm/cache.h>
52#endif
53
Marian Balakowicz2437cf22008-01-08 18:11:43 +010054DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010055
Marian Balakowicz2437cf22008-01-08 18:11:43 +010056extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
Kumar Galab937bb72008-02-27 21:51:49 -060057extern ulong get_effective_memsize(void);
Marian Balakowicz9c701e82008-01-31 13:57:17 +010058static ulong get_sp (void);
Marian Balakowicz2efc2642008-01-31 13:58:13 +010059static void set_clocks_in_mhz (bd_t *kbd);
Marian Balakowicz2437cf22008-01-08 18:11:43 +010060
Kumar Galac5bacfd2008-02-27 21:51:50 -060061#ifndef CFG_LINUX_LOWMEM_MAX_SIZE
62#define CFG_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
63#endif
64
Marian Balakowicz2437cf22008-01-08 18:11:43 +010065void __attribute__((noinline))
Marian Balakowicz61fde552008-02-27 11:01:04 +010066do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
67 bootm_headers_t *images)
Marian Balakowicz2437cf22008-01-08 18:11:43 +010068{
Kumar Galab937bb72008-02-27 21:51:49 -060069 ulong sp;
Marian Balakowicz2efc2642008-01-31 13:58:13 +010070
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010071 ulong initrd_start, initrd_end;
Marian Balakowicz95418502008-01-31 13:55:39 +010072 ulong rd_data_start, rd_data_end, rd_len;
Kumar Galac5bacfd2008-02-27 21:51:50 -060073 ulong size;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010074
Kumar Galab937bb72008-02-27 21:51:49 -060075 ulong cmd_start, cmd_end, bootmap_base;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010076 bd_t *kbd;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010077 ulong ep = 0;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010078 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
Kumar Gala56bdf1d2008-02-27 21:51:45 -060079 int ret;
Kumar Galaa56d7d52008-02-27 21:51:44 -060080 ulong of_size = 0;
Kumar Galab937bb72008-02-27 21:51:49 -060081 struct lmb *lmb = images->lmb;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010082
Marian Balakowiczd2160852008-01-31 13:20:07 +010083#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz6881dae2008-02-04 08:28:17 +010084 char *of_flat_tree = NULL;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010085#endif
86
Kumar Galac5bacfd2008-02-27 21:51:50 -060087 bootmap_base = getenv_bootm_low();
88 size = getenv_bootm_size();
89
90#ifdef DEBUG
91 if (((u64)bootmap_base + size) > (CFG_SDRAM_BASE + (u64)gd->ram_size))
92 puts("WARNING: bootm_low + bootm_size exceed total memory\n");
93 if ((bootmap_base + size) > get_effective_memsize())
94 puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
95#endif
96
97 size = min(size, get_effective_memsize());
98 size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
99
100 if (size < getenv_bootm_size()) {
101 ulong base = bootmap_base + size;
102 printf("WARNING: adjusting available memory to %x\n", size);
103 lmb_reserve(lmb, base, getenv_bootm_size() - size);
104 }
Kumar Galab937bb72008-02-27 21:51:49 -0600105
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100106 /*
107 * Booting a (Linux) kernel image
108 *
109 * Allocate space for command line and board info - the
110 * address should be as high as possible within the reach of
111 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
112 * memory, which means far enough below the current stack
113 * pointer.
114 */
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100115 sp = get_sp();
Kumar Galac5bacfd2008-02-27 21:51:50 -0600116 debug ("## Current stack ends at 0x%08lx\n", sp);
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100117
Kumar Galab937bb72008-02-27 21:51:49 -0600118 /* adjust sp by 1K to be safe */
119 sp -= 1024;
120 lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100121
Kumar Galaa56d7d52008-02-27 21:51:44 -0600122#if defined(CONFIG_OF_LIBFDT)
123 /* find flattened device tree */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100124 ret = boot_get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600125
126 if (ret)
127 goto error;
Kumar Galaa56d7d52008-02-27 21:51:44 -0600128#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100129
Kumar Galaa56d7d52008-02-27 21:51:44 -0600130 if (!of_size) {
131 /* allocate space and init command line */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100132 ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -0600133 if (ret) {
134 puts("ERROR with allocation of cmdline\n");
135 goto error;
136 }
Kumar Galaa56d7d52008-02-27 21:51:44 -0600137
138 /* allocate space for kernel copy of board info */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100139 ret = boot_get_kbd (lmb, &kbd, bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -0600140 if (ret) {
141 puts("ERROR with allocation of kernel bd\n");
142 goto error;
143 }
Kumar Galaa56d7d52008-02-27 21:51:44 -0600144 set_clocks_in_mhz(kbd);
145 }
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100146
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100147 /* find kernel entry point */
148 if (images->legacy_hdr_valid) {
149 ep = image_get_ep (images->legacy_hdr_os);
150#if defined(CONFIG_FIT)
151 } else if (images->fit_uname_os) {
Marian Balakowiczdf8ff332008-03-12 10:33:00 +0100152 ret = fit_image_get_entry (images->fit_hdr_os,
153 images->fit_noffset_os, &ep);
154 if (ret) {
155 puts ("Can't get entry point property!\n");
156 goto error;
157 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100158#endif
159 } else {
160 puts ("Could not find kernel entry point!\n");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600161 goto error;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100162 }
163 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100164
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100165 /* find ramdisk */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100166 ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_PPC,
167 &rd_data_start, &rd_data_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600168 if (ret)
169 goto error;
170
Marian Balakowicz95418502008-01-31 13:55:39 +0100171 rd_len = rd_data_end - rd_data_start;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100172
Marian Balakowiczd2160852008-01-31 13:20:07 +0100173#if defined(CONFIG_OF_LIBFDT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100174 ret = boot_relocate_fdt (lmb, bootmap_base,
Kumar Galab937bb72008-02-27 21:51:49 -0600175 cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100176
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100177 /*
178 * Add the chosen node if it doesn't exist, add the env and bd_t
179 * if the user wants it (the logic is in the subroutines).
180 */
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100181 if (of_size) {
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600182 /* pass in dummy initrd info, we'll fix up later */
183 if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100184 fdt_error ("/chosen node create failed");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600185 goto error;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100186 }
187#ifdef CONFIG_OF_HAS_UBOOT_ENV
188 if (fdt_env(of_flat_tree) < 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100189 fdt_error ("/u-boot-env node create failed");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600190 goto error;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100191 }
192#endif
193#ifdef CONFIG_OF_HAS_BD_T
194 if (fdt_bd_t(of_flat_tree) < 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100195 fdt_error ("/bd_t node create failed");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600196 goto error;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100197 }
198#endif
199#ifdef CONFIG_OF_BOARD_SETUP
200 /* Call the board-specific fixup routine */
201 ft_board_setup(of_flat_tree, gd->bd);
202#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100203 }
Marian Balakowiczd2160852008-01-31 13:20:07 +0100204#endif /* CONFIG_OF_LIBFDT */
Marian Balakowicz751b2b82008-01-08 18:11:43 +0100205
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100206 ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
Kumar Galab937bb72008-02-27 21:51:49 -0600207 if (ret)
208 goto error;
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600209
210#if defined(CONFIG_OF_LIBFDT)
211 /* fixup the initrd now that we know where it should be */
212 if ((of_flat_tree) && (initrd_start && initrd_end)) {
213 uint64_t addr, size;
214 int total = fdt_num_mem_rsv(of_flat_tree);
215 int j;
216
217 /* Look for the dummy entry and delete it */
218 for (j = 0; j < total; j++) {
219 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
220 if (addr == rd_data_start) {
221 fdt_del_mem_rsv(of_flat_tree, j);
222 break;
223 }
224 }
225
226 ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
227 initrd_end - initrd_start + 1);
228 if (ret < 0) {
229 printf("fdt_chosen: %s\n", fdt_strerror(ret));
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600230 goto error;
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600231 }
232
233 do_fixup_by_path_u32(of_flat_tree, "/chosen",
234 "linux,initrd-start", initrd_start, 0);
235 do_fixup_by_path_u32(of_flat_tree, "/chosen",
236 "linux,initrd-end", initrd_end, 0);
237 }
238#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100239 debug ("## Transferring control to Linux (at address %08lx) ...\n",
240 (ulong)kernel);
241
242 show_boot_progress (15);
243
244#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
245 unlock_ram_in_cache();
246#endif
Kumar Galaaa0826b2008-02-27 21:51:51 -0600247 if (!images->autostart)
248 return ;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100249
Marian Balakowiczd2160852008-01-31 13:20:07 +0100250#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100251 if (of_flat_tree) { /* device tree; boot new style */
252 /*
253 * Linux Kernel Parameters (passing device tree):
254 * r3: pointer to the fdt, followed by the board info data
255 * r4: physical pointer to the kernel itself
256 * r5: NULL
257 * r6: NULL
258 * r7: NULL
259 */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100260 debug (" Booting using OF flat tree...\n");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100261 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
262 /* does not return */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100263 } else
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100264#endif
Marian Balakowicz0d871362008-03-12 10:32:53 +0100265 {
266 /*
267 * Linux Kernel Parameters (passing board info data):
268 * r3: ptr to board info data
269 * r4: initrd_start or 0 if no initrd
270 * r5: initrd_end - unused if r4 is 0
271 * r6: Start of command line string
272 * r7: End of command line string
273 */
274 debug (" Booting using board info...\n");
275 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
276 /* does not return */
277 }
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600278 return ;
279
280error:
Kumar Galaaa0826b2008-02-27 21:51:51 -0600281 if (images->autostart)
282 do_reset (cmdtp, flag, argc, argv);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600283 return ;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100284}
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100285
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100286static ulong get_sp (void)
287{
288 ulong sp;
289
290 asm( "mr %0,1": "=r"(sp) : );
291 return sp;
292}
293
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100294static void set_clocks_in_mhz (bd_t *kbd)
295{
296 char *s;
297
298 if ((s = getenv ("clocks_in_mhz")) != NULL) {
299 /* convert all clock information to MHz */
300 kbd->bi_intfreq /= 1000000L;
301 kbd->bi_busfreq /= 1000000L;
302#if defined(CONFIG_MPC8220)
303 kbd->bi_inpfreq /= 1000000L;
304 kbd->bi_pcifreq /= 1000000L;
305 kbd->bi_pevfreq /= 1000000L;
306 kbd->bi_flbfreq /= 1000000L;
307 kbd->bi_vcofreq /= 1000000L;
308#endif
309#if defined(CONFIG_CPM2)
310 kbd->bi_cpmfreq /= 1000000L;
311 kbd->bi_brgfreq /= 1000000L;
312 kbd->bi_sccfreq /= 1000000L;
313 kbd->bi_vco /= 1000000L;
314#endif
315#if defined(CONFIG_MPC5xxx)
316 kbd->bi_ipbfreq /= 1000000L;
317 kbd->bi_pcifreq /= 1000000L;
318#endif /* CONFIG_MPC5xxx */
319 }
320}
321
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100322#if defined(CONFIG_OF_LIBFDT)
323static void fdt_error (const char *msg)
324{
325 puts ("ERROR: ");
326 puts (msg);
327 puts (" - must RESET the board to recover.\n");
328}
Marian Balakowicz25154b62008-01-31 13:58:20 +0100329
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100330static image_header_t *image_get_fdt (ulong fdt_addr)
331{
332 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
333
334 image_print_contents (fdt_hdr);
335
336 puts (" Verifying Checksum ... ");
337 if (!image_check_hcrc (fdt_hdr)) {
338 fdt_error ("fdt header checksum invalid");
339 return NULL;
340 }
341
342 if (!image_check_dcrc (fdt_hdr)) {
343 fdt_error ("fdt checksum invalid");
344 return NULL;
345 }
346 puts ("OK\n");
347
348 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
349 fdt_error ("uImage is not a fdt");
350 return NULL;
351 }
352 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
353 fdt_error ("uImage is compressed");
354 return NULL;
355 }
356 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
357 fdt_error ("uImage data is not a fdt");
358 return NULL;
359 }
360 return fdt_hdr;
361}
362
Marian Balakowicz0d871362008-03-12 10:32:53 +0100363/**
364 * fit_check_fdt - verify FIT format FDT subimage
365 * @fit_hdr: pointer to the FIT header
366 * fdt_noffset: FDT subimage node offset within FIT image
367 * @verify: data CRC verification flag
368 *
369 * fit_check_fdt() verifies integrity of the FDT subimage and from
370 * specified FIT image.
371 *
372 * returns:
373 * 1, on success
374 * 0, on failure
375 */
376#if defined(CONFIG_FIT)
377static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
378{
379 fit_image_print (fit, fdt_noffset, " ");
380
381 if (verify) {
382 puts (" Verifying Hash Integrity ... ");
383 if (!fit_image_check_hashes (fit, fdt_noffset)) {
384 fdt_error ("Bad Data Hash");
385 return 0;
386 }
387 puts ("OK\n");
388 }
389
390 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
391 fdt_error ("Not a FDT image");
392 return 0;
393 }
394
395 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
396 fdt_error ("FDT image is compressed");
397 return 0;
398 }
399
400 return 1;
401}
402#endif /* CONFIG_FIT */
403
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100404static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100405 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Marian Balakowicz25154b62008-01-31 13:58:20 +0100406{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100407 ulong fdt_addr;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100408 image_header_t *fdt_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100409 char *fdt_blob = NULL;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100410 ulong image_start, image_end;
411 ulong load_start, load_end;
412#if defined(CONFIG_FIT)
413 void *fit_hdr;
414 const char *fit_uname_config = NULL;
415 const char *fit_uname_fdt = NULL;
416 ulong default_addr;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100417 int cfg_noffset;
Marian Balakowicz0d871362008-03-12 10:32:53 +0100418 int fdt_noffset;
419 const void *data;
420 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100421#endif
Marian Balakowicz25154b62008-01-31 13:58:20 +0100422
Marian Balakowicz0d871362008-03-12 10:32:53 +0100423 *of_flat_tree = NULL;
424 *of_size = 0;
425
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100426 if (argc > 3 || genimg_has_config (images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100427#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100428 if (argc > 3) {
429 /*
430 * If the FDT blob comes from the FIT image and the
431 * FIT image address is omitted in the command line
432 * argument, try to use ramdisk or os FIT image
433 * address or default load address.
434 */
435 if (images->fit_uname_rd)
436 default_addr = (ulong)images->fit_hdr_rd;
437 else if (images->fit_uname_os)
438 default_addr = (ulong)images->fit_hdr_os;
439 else
440 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100441
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100442 if (fit_parse_conf (argv[3], default_addr,
443 &fdt_addr, &fit_uname_config)) {
444 debug ("* fdt: config '%s' from image at 0x%08lx\n",
445 fit_uname_config, fdt_addr);
446 } else if (fit_parse_subimage (argv[3], default_addr,
447 &fdt_addr, &fit_uname_fdt)) {
448 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
449 fit_uname_fdt, fdt_addr);
450 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100451#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100452 {
453 fdt_addr = simple_strtoul(argv[3], NULL, 16);
454 debug ("* fdt: cmdline image address = 0x%08lx\n",
455 fdt_addr);
456 }
457#if defined(CONFIG_FIT)
458 } else {
459 /* use FIT configuration provided in first bootm
460 * command argument
461 */
462 fdt_addr = (ulong)images->fit_hdr_os;
463 fit_uname_config = images->fit_uname_cfg;
464 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
465 fit_uname_config, fdt_addr);
466
467 /*
468 * Check whether configuration has FDT blob defined,
469 * if not quit silently.
470 */
471 fit_hdr = (void *)fdt_addr;
472 cfg_noffset = fit_conf_get_node (fit_hdr,
473 fit_uname_config);
474 if (cfg_noffset < 0) {
475 debug ("* fdt: no such config\n");
476 return 0;
477 }
478
479 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
480 cfg_noffset);
481 if (fdt_noffset < 0) {
482 debug ("* fdt: no fdt in config\n");
483 return 0;
484 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100485 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100486#endif
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100487
Marian Balakowicz0d871362008-03-12 10:32:53 +0100488 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100489 fdt_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100490
491 /* copy from dataflash if needed */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100492 fdt_addr = genimg_get_image (fdt_addr);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100493
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100494 /*
495 * Check if there is an FDT image at the
496 * address provided in the second bootm argument
497 * check image type, for FIT images get a FIT node.
498 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100499 switch (genimg_get_format ((void *)fdt_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100500 case IMAGE_FORMAT_LEGACY:
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100501 /* verify fdt_addr points to a valid image header */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100502 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100503 fdt_addr);
504 fdt_hdr = image_get_fdt (fdt_addr);
505 if (!fdt_hdr)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600506 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100507
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100508 /*
509 * move image data to the load address,
510 * make sure we don't overwrite initial image
511 */
Marian Balakowicz25154b62008-01-31 13:58:20 +0100512 image_start = (ulong)fdt_hdr;
513 image_end = image_get_image_end (fdt_hdr);
514
515 load_start = image_get_load (fdt_hdr);
516 load_end = load_start + image_get_data_size (fdt_hdr);
517
518 if ((load_start < image_end) && (load_end > image_start)) {
519 fdt_error ("fdt overwritten");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600520 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100521 }
Marian Balakowicz0d871362008-03-12 10:32:53 +0100522
523 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
524 image_get_data (fdt_hdr), load_start);
525
526 memmove ((void *)load_start,
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100527 (void *)image_get_data (fdt_hdr),
528 image_get_data_size (fdt_hdr));
Marian Balakowicz25154b62008-01-31 13:58:20 +0100529
Marian Balakowicz0d871362008-03-12 10:32:53 +0100530 fdt_blob = (char *)load_start;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100531 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100532 case IMAGE_FORMAT_FIT:
Marian Balakowicz12560682008-02-27 11:02:26 +0100533 /*
534 * This case will catch both: new uImage format
535 * (libfdt based) and raw FDT blob (also libfdt
536 * based).
537 */
538#if defined(CONFIG_FIT)
539 /* check FDT blob vs FIT blob */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100540 if (fit_check_format ((const void *)fdt_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100541 /*
542 * FIT image
543 */
544 fit_hdr = (void *)fdt_addr;
Marian Balakowicz0d871362008-03-12 10:32:53 +0100545 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
546 fdt_addr);
547
548 if (!fit_uname_fdt) {
549 /*
550 * no FDT blob image node unit name,
551 * try to get config node first. If
552 * config unit node name is NULL
553 * fit_conf_get_node() will try to
554 * find default config node
555 */
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100556 cfg_noffset = fit_conf_get_node (fit_hdr,
Marian Balakowicz0d871362008-03-12 10:32:53 +0100557 fit_uname_config);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100558
559 if (cfg_noffset < 0) {
560 fdt_error ("Could not find configuration node\n");
Marian Balakowicz0d871362008-03-12 10:32:53 +0100561 goto error;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100562 }
563
564 fit_uname_config = fdt_get_name (fit_hdr,
565 cfg_noffset, NULL);
566 printf (" Using '%s' configuration\n",
567 fit_uname_config);
Marian Balakowicz0d871362008-03-12 10:32:53 +0100568
569 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100570 cfg_noffset);
Marian Balakowicz0d871362008-03-12 10:32:53 +0100571 fit_uname_fdt = fit_get_name (fit_hdr,
572 fdt_noffset, NULL);
573 } else {
574 /* get FDT component image node offset */
575 fdt_noffset = fit_image_get_node (fit_hdr,
576 fit_uname_fdt);
577 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100578 if (fdt_noffset < 0) {
579 fdt_error ("Could not find subimage node\n");
Marian Balakowicz0d871362008-03-12 10:32:53 +0100580 goto error;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100581 }
Marian Balakowicz0d871362008-03-12 10:32:53 +0100582
583 printf (" Trying '%s' FDT blob subimage\n",
584 fit_uname_fdt);
585
586 if (!fit_check_fdt (fit_hdr, fdt_noffset,
587 images->verify))
588 goto error;
589
590 /* get ramdisk image data address and length */
591 if (fit_image_get_data (fit_hdr, fdt_noffset,
592 &data, &size)) {
593 fdt_error ("Could not find FDT subimage data");
594 goto error;
595 }
596
597 /* verift that image data is a proper FDT blob */
598 if (fdt_check_header ((char *)data) != 0) {
599 fdt_error ("Subimage data is not a FTD");
600 goto error;
601 }
602
603 /*
604 * move image data to the load address,
605 * make sure we don't overwrite initial image
606 */
607 image_start = (ulong)fit_hdr;
608 image_end = fit_get_end (fit_hdr);
609
610 if (fit_image_get_load (fit_hdr, fdt_noffset,
611 &load_start) == 0) {
612 load_end = load_start + size;
613
614 if ((load_start < image_end) &&
615 (load_end > image_start)) {
616 fdt_error ("FDT overwritten");
617 goto error;
618 }
619
620 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
621 (ulong)data, load_start);
622
623 memmove ((void *)load_start,
624 (void *)data, size);
625
626 fdt_blob = (char *)load_start;
627 } else {
628 fdt_blob = (char *)data;
629 }
630
631 images->fit_hdr_fdt = fit_hdr;
632 images->fit_uname_fdt = fit_uname_fdt;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100633 images->fit_noffset_fdt = fdt_noffset;
Marian Balakowicz0d871362008-03-12 10:32:53 +0100634 break;
Marian Balakowicz12560682008-02-27 11:02:26 +0100635 } else
636#endif
637 {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100638 /*
639 * FDT blob
640 */
Marian Balakowicz12560682008-02-27 11:02:26 +0100641 debug ("* fdt: raw FDT blob\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100642 printf ("## Flattened Device Tree blob at %08lx\n", fdt_blob);
643 fdt_blob = (char *)fdt_addr;
644 }
645 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100646 default:
647 fdt_error ("Did not find a cmdline Flattened Device Tree");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600648 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100649 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100650
651 printf (" Booting using the fdt blob at 0x%x\n", fdt_blob);
652
653 } else if (images->legacy_hdr_valid &&
654 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
655
Marian Balakowicz25154b62008-01-31 13:58:20 +0100656 ulong fdt_data, fdt_len;
657
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100658 /*
659 * Now check if we have a legacy multi-component image,
660 * get second entry data start address and len.
661 */
Marian Balakowicz25154b62008-01-31 13:58:20 +0100662 printf ("## Flattened Device Tree from multi "
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100663 "component Image at %08lX\n",
664 (ulong)images->legacy_hdr_os);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100665
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100666 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100667 if (fdt_len) {
668
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100669 fdt_blob = (char *)fdt_data;
670 printf (" Booting using the fdt at 0x%x\n", fdt_blob);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100671
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100672 if (fdt_check_header (fdt_blob) != 0) {
Marian Balakowicz25154b62008-01-31 13:58:20 +0100673 fdt_error ("image is not a fdt");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600674 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100675 }
676
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100677 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
Marian Balakowicz25154b62008-01-31 13:58:20 +0100678 fdt_error ("fdt size != image size");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600679 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100680 }
681 } else {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100682 fdt_error ("Did not find a Flattened Device Tree "
683 "in a legacy multi-component image");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600684 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100685 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100686 } else {
687 debug ("## No Flattened Device Tree\n");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600688 return 0;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100689 }
690
691 *of_flat_tree = fdt_blob;
692 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
693 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
694 *of_flat_tree, *of_size);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600695
696 return 0;
697
698error:
699 do_reset (cmdtp, flag, argc, argv);
700 return 1;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100701}
702
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100703static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100704 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
705 char **of_flat_tree, ulong *of_size)
706{
707 char *fdt_blob = *of_flat_tree;
708 ulong relocate = 0;
Kumar Galab937bb72008-02-27 21:51:49 -0600709 ulong of_len = 0;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100710
711 /* nothing to do */
712 if (*of_size == 0)
Kumar Galab937bb72008-02-27 21:51:49 -0600713 return 0;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100714
715 if (fdt_check_header (fdt_blob) != 0) {
716 fdt_error ("image is not a fdt");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600717 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100718 }
719
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100720#ifndef CFG_NO_FLASH
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100721 /* move the blob if it is in flash (set relocate) */
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100722 if (addr2info ((ulong)fdt_blob) != NULL)
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100723 relocate = 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100724#endif
725
Marian Balakowicz25154b62008-01-31 13:58:20 +0100726 /*
727 * The blob must be within CFG_BOOTMAPSZ,
728 * so we flag it to be copied if it is not.
729 */
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100730 if (fdt_blob >= (char *)CFG_BOOTMAPSZ)
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100731 relocate = 1;
Kumar Galab937bb72008-02-27 21:51:49 -0600732
Marian Balakowiczb5ec1b92008-02-29 22:22:46 +0100733 of_len = be32_to_cpu (fdt_totalsize (fdt_blob));
Marian Balakowicz25154b62008-01-31 13:58:20 +0100734
735 /* move flattend device tree if needed */
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100736 if (relocate) {
Marian Balakowicz25154b62008-01-31 13:58:20 +0100737 int err;
Kumar Galab937bb72008-02-27 21:51:49 -0600738 ulong of_start;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100739
740 /* position on a 4K boundary before the alloc_current */
Kumar Galab937bb72008-02-27 21:51:49 -0600741 of_start = lmb_alloc_base(lmb, of_len, 0x1000,
742 (CFG_BOOTMAPSZ + bootmap_base));
743
744 if (of_start == 0) {
745 puts("device tree - allocation error\n");
746 goto error;
747 }
Marian Balakowicz25154b62008-01-31 13:58:20 +0100748
749 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100750 (ulong)fdt_blob, (ulong)fdt_blob + of_len - 1,
Marian Balakowicz25154b62008-01-31 13:58:20 +0100751 of_len, of_len);
752
753 printf (" Loading Device Tree to %08lx, end %08lx ... ",
754 of_start, of_start + of_len - 1);
755
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100756 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100757 if (err != 0) {
758 fdt_error ("fdt move failed");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600759 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100760 }
761 puts ("OK\n");
762
763 *of_flat_tree = (char *)of_start;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100764 } else {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100765 *of_flat_tree = fdt_blob;
Kumar Galab937bb72008-02-27 21:51:49 -0600766 lmb_reserve(lmb, (ulong)fdt, of_len);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100767 }
768
Kumar Galab937bb72008-02-27 21:51:49 -0600769 return 0;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600770
771error:
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600772 return 1;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100773}
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100774#endif