blob: 8d546756c1fb810a4db649651961185837c160db [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
Andy Fleming7d32ae32008-06-16 13:58:56 -050054#ifndef CFG_FDT_PAD
55#define CFG_FDT_PAD 0x3000
56#endif
57
Marian Balakowicz2437cf22008-01-08 18:11:43 +010058DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010059
Marian Balakowicz2437cf22008-01-08 18:11:43 +010060extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
Kumar Galab937bb72008-02-27 21:51:49 -060061extern ulong get_effective_memsize(void);
Marian Balakowicz9c701e82008-01-31 13:57:17 +010062static ulong get_sp (void);
Marian Balakowicz2efc2642008-01-31 13:58:13 +010063static void set_clocks_in_mhz (bd_t *kbd);
Marian Balakowicz2437cf22008-01-08 18:11:43 +010064
Kumar Galac5bacfd2008-02-27 21:51:50 -060065#ifndef CFG_LINUX_LOWMEM_MAX_SIZE
66#define CFG_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
67#endif
68
Marian Balakowicz2437cf22008-01-08 18:11:43 +010069void __attribute__((noinline))
Marian Balakowicz61fde552008-02-27 11:01:04 +010070do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
71 bootm_headers_t *images)
Marian Balakowicz2437cf22008-01-08 18:11:43 +010072{
Kumar Galab937bb72008-02-27 21:51:49 -060073 ulong sp;
Marian Balakowicz2efc2642008-01-31 13:58:13 +010074
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010075 ulong initrd_start, initrd_end;
Marian Balakowicz95418502008-01-31 13:55:39 +010076 ulong rd_data_start, rd_data_end, rd_len;
Kumar Galac5bacfd2008-02-27 21:51:50 -060077 ulong size;
Becky Bruced26d67c2008-06-09 20:37:18 -050078 phys_size_t bootm_size;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010079
Kumar Galab937bb72008-02-27 21:51:49 -060080 ulong cmd_start, cmd_end, bootmap_base;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010081 bd_t *kbd;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +010082 ulong ep = 0;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010083 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
Kumar Gala56bdf1d2008-02-27 21:51:45 -060084 int ret;
Kumar Galaa56d7d52008-02-27 21:51:44 -060085 ulong of_size = 0;
Kumar Galab937bb72008-02-27 21:51:49 -060086 struct lmb *lmb = images->lmb;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010087
Marian Balakowiczd2160852008-01-31 13:20:07 +010088#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz6881dae2008-02-04 08:28:17 +010089 char *of_flat_tree = NULL;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010090#endif
91
Kumar Galac5bacfd2008-02-27 21:51:50 -060092 bootmap_base = getenv_bootm_low();
Becky Bruced26d67c2008-06-09 20:37:18 -050093 bootm_size = getenv_bootm_size();
Kumar Galac5bacfd2008-02-27 21:51:50 -060094
95#ifdef DEBUG
Becky Bruced26d67c2008-06-09 20:37:18 -050096 if (((u64)bootmap_base + bootm_size) >
97 (CFG_SDRAM_BASE + (u64)gd->ram_size))
Kumar Galac5bacfd2008-02-27 21:51:50 -060098 puts("WARNING: bootm_low + bootm_size exceed total memory\n");
Becky Bruced26d67c2008-06-09 20:37:18 -050099 if ((bootmap_base + bootm_size) > get_effective_memsize())
Kumar Galac5bacfd2008-02-27 21:51:50 -0600100 puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
101#endif
102
Becky Bruced26d67c2008-06-09 20:37:18 -0500103 size = min(bootm_size, get_effective_memsize());
Kumar Galac5bacfd2008-02-27 21:51:50 -0600104 size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
105
Becky Bruced26d67c2008-06-09 20:37:18 -0500106 if (size < bootm_size) {
Kumar Galac5bacfd2008-02-27 21:51:50 -0600107 ulong base = bootmap_base + size;
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700108 printf("WARNING: adjusting available memory to %lx\n", size);
Becky Bruced26d67c2008-06-09 20:37:18 -0500109 lmb_reserve(lmb, base, bootm_size - size);
Kumar Galac5bacfd2008-02-27 21:51:50 -0600110 }
Kumar Galab937bb72008-02-27 21:51:49 -0600111
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100112 /*
113 * Booting a (Linux) kernel image
114 *
115 * Allocate space for command line and board info - the
116 * address should be as high as possible within the reach of
117 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
118 * memory, which means far enough below the current stack
119 * pointer.
120 */
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100121 sp = get_sp();
Kumar Galac5bacfd2008-02-27 21:51:50 -0600122 debug ("## Current stack ends at 0x%08lx\n", sp);
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100123
Kumar Galab937bb72008-02-27 21:51:49 -0600124 /* adjust sp by 1K to be safe */
125 sp -= 1024;
126 lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100127
Kumar Galaa56d7d52008-02-27 21:51:44 -0600128#if defined(CONFIG_OF_LIBFDT)
129 /* find flattened device tree */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100130 ret = boot_get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600131
132 if (ret)
133 goto error;
Kumar Galaa56d7d52008-02-27 21:51:44 -0600134#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100135
Kumar Galaa56d7d52008-02-27 21:51:44 -0600136 if (!of_size) {
137 /* allocate space and init command line */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100138 ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -0600139 if (ret) {
140 puts("ERROR with allocation of cmdline\n");
141 goto error;
142 }
Kumar Galaa56d7d52008-02-27 21:51:44 -0600143
144 /* allocate space for kernel copy of board info */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100145 ret = boot_get_kbd (lmb, &kbd, bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -0600146 if (ret) {
147 puts("ERROR with allocation of kernel bd\n");
148 goto error;
149 }
Kumar Galaa56d7d52008-02-27 21:51:44 -0600150 set_clocks_in_mhz(kbd);
151 }
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100152
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100153 /* find kernel entry point */
154 if (images->legacy_hdr_valid) {
Marian Balakowiczab00e022008-04-11 11:07:49 +0200155 ep = image_get_ep (&images->legacy_hdr_os_copy);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100156#if defined(CONFIG_FIT)
157 } else if (images->fit_uname_os) {
Marian Balakowiczdf8ff332008-03-12 10:33:00 +0100158 ret = fit_image_get_entry (images->fit_hdr_os,
159 images->fit_noffset_os, &ep);
160 if (ret) {
161 puts ("Can't get entry point property!\n");
162 goto error;
163 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100164#endif
165 } else {
166 puts ("Could not find kernel entry point!\n");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600167 goto error;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100168 }
169 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100170
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100171 /* find ramdisk */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100172 ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_PPC,
173 &rd_data_start, &rd_data_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600174 if (ret)
175 goto error;
176
Marian Balakowicz95418502008-01-31 13:55:39 +0100177 rd_len = rd_data_end - rd_data_start;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100178
Marian Balakowiczd2160852008-01-31 13:20:07 +0100179#if defined(CONFIG_OF_LIBFDT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100180 ret = boot_relocate_fdt (lmb, bootmap_base,
Kumar Galab937bb72008-02-27 21:51:49 -0600181 cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100182
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100183 /*
184 * Add the chosen node if it doesn't exist, add the env and bd_t
185 * if the user wants it (the logic is in the subroutines).
186 */
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100187 if (of_size) {
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600188 /* pass in dummy initrd info, we'll fix up later */
189 if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100190 fdt_error ("/chosen node create failed");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600191 goto error;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100192 }
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100193#ifdef CONFIG_OF_BOARD_SETUP
194 /* Call the board-specific fixup routine */
195 ft_board_setup(of_flat_tree, gd->bd);
196#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100197 }
Andy Fleming7d32ae32008-06-16 13:58:56 -0500198
199 /* Fixup the fdt memreserve now that we know how big it is */
200 if (of_flat_tree) {
201 int j;
202 uint64_t addr, size;
203 int total = fdt_num_mem_rsv(of_flat_tree);
204 uint actualsize;
205
206 for (j = 0; j < total; j++) {
207 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
208 if (addr == (uint64_t)of_flat_tree) {
209 fdt_del_mem_rsv(of_flat_tree, j);
210 break;
211 }
212 }
213
214 /* Delete the old LMB reservation */
Wolfgang Denka7a141b2008-07-03 22:04:17 +0200215 lmb_free(lmb, (uint64_t)of_flat_tree, fdt_totalsize(of_flat_tree));
Andy Fleming7d32ae32008-06-16 13:58:56 -0500216
217 /* Calculate the actual size of the fdt */
218 actualsize = fdt_off_dt_strings(of_flat_tree) +
219 fdt_size_dt_strings(of_flat_tree);
220
221 /* Make it so the fdt ends on a page boundary */
222 actualsize = ALIGN(actualsize, 0x1000);
223 actualsize = actualsize - ((uint)of_flat_tree & 0xfff);
224
225 /* Change the fdt header to reflect the correct size */
226 fdt_set_totalsize(of_flat_tree, actualsize);
227 of_size = actualsize;
228
229 /* Add the new reservation */
230 ret = fdt_add_mem_rsv(of_flat_tree, (uint)of_flat_tree,
231 of_size);
232
233 /* Create a new LMB reservation */
234 lmb_reserve(lmb, (ulong)of_flat_tree, of_size);
235 }
Marian Balakowiczd2160852008-01-31 13:20:07 +0100236#endif /* CONFIG_OF_LIBFDT */
Marian Balakowicz751b2b82008-01-08 18:11:43 +0100237
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100238 ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
Kumar Galab937bb72008-02-27 21:51:49 -0600239 if (ret)
240 goto error;
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600241
242#if defined(CONFIG_OF_LIBFDT)
243 /* fixup the initrd now that we know where it should be */
244 if ((of_flat_tree) && (initrd_start && initrd_end)) {
245 uint64_t addr, size;
246 int total = fdt_num_mem_rsv(of_flat_tree);
247 int j;
248
249 /* Look for the dummy entry and delete it */
250 for (j = 0; j < total; j++) {
251 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
252 if (addr == rd_data_start) {
253 fdt_del_mem_rsv(of_flat_tree, j);
254 break;
255 }
256 }
257
258 ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
259 initrd_end - initrd_start + 1);
260 if (ret < 0) {
261 printf("fdt_chosen: %s\n", fdt_strerror(ret));
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600262 goto error;
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600263 }
264
265 do_fixup_by_path_u32(of_flat_tree, "/chosen",
266 "linux,initrd-start", initrd_start, 0);
267 do_fixup_by_path_u32(of_flat_tree, "/chosen",
268 "linux,initrd-end", initrd_end, 0);
269 }
270#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100271 debug ("## Transferring control to Linux (at address %08lx) ...\n",
272 (ulong)kernel);
273
274 show_boot_progress (15);
275
276#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
277 unlock_ram_in_cache();
278#endif
Kumar Galaaa0826b2008-02-27 21:51:51 -0600279 if (!images->autostart)
280 return ;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100281
Marian Balakowiczd2160852008-01-31 13:20:07 +0100282#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100283 if (of_flat_tree) { /* device tree; boot new style */
284 /*
285 * Linux Kernel Parameters (passing device tree):
286 * r3: pointer to the fdt, followed by the board info data
287 * r4: physical pointer to the kernel itself
288 * r5: NULL
289 * r6: NULL
290 * r7: NULL
291 */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100292 debug (" Booting using OF flat tree...\n");
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100293 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
294 /* does not return */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100295 } else
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100296#endif
Marian Balakowicz0d871362008-03-12 10:32:53 +0100297 {
298 /*
299 * Linux Kernel Parameters (passing board info data):
300 * r3: ptr to board info data
301 * r4: initrd_start or 0 if no initrd
302 * r5: initrd_end - unused if r4 is 0
303 * r6: Start of command line string
304 * r7: End of command line string
305 */
306 debug (" Booting using board info...\n");
307 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
308 /* does not return */
309 }
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600310 return ;
311
312error:
Kumar Galaaa0826b2008-02-27 21:51:51 -0600313 if (images->autostart)
314 do_reset (cmdtp, flag, argc, argv);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600315 return ;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100316}
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100317
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100318static ulong get_sp (void)
319{
320 ulong sp;
321
322 asm( "mr %0,1": "=r"(sp) : );
323 return sp;
324}
325
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100326static void set_clocks_in_mhz (bd_t *kbd)
327{
328 char *s;
329
330 if ((s = getenv ("clocks_in_mhz")) != NULL) {
331 /* convert all clock information to MHz */
332 kbd->bi_intfreq /= 1000000L;
333 kbd->bi_busfreq /= 1000000L;
334#if defined(CONFIG_MPC8220)
335 kbd->bi_inpfreq /= 1000000L;
336 kbd->bi_pcifreq /= 1000000L;
337 kbd->bi_pevfreq /= 1000000L;
338 kbd->bi_flbfreq /= 1000000L;
339 kbd->bi_vcofreq /= 1000000L;
340#endif
341#if defined(CONFIG_CPM2)
342 kbd->bi_cpmfreq /= 1000000L;
343 kbd->bi_brgfreq /= 1000000L;
344 kbd->bi_sccfreq /= 1000000L;
Wolfgang Denk8f702ad2008-03-26 11:48:46 +0100345 kbd->bi_vco /= 1000000L;
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100346#endif
347#if defined(CONFIG_MPC5xxx)
348 kbd->bi_ipbfreq /= 1000000L;
349 kbd->bi_pcifreq /= 1000000L;
350#endif /* CONFIG_MPC5xxx */
351 }
352}
353
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100354#if defined(CONFIG_OF_LIBFDT)
355static void fdt_error (const char *msg)
356{
357 puts ("ERROR: ");
358 puts (msg);
359 puts (" - must RESET the board to recover.\n");
360}
Marian Balakowicz25154b62008-01-31 13:58:20 +0100361
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100362static image_header_t *image_get_fdt (ulong fdt_addr)
363{
364 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
365
366 image_print_contents (fdt_hdr);
367
368 puts (" Verifying Checksum ... ");
369 if (!image_check_hcrc (fdt_hdr)) {
370 fdt_error ("fdt header checksum invalid");
371 return NULL;
372 }
373
374 if (!image_check_dcrc (fdt_hdr)) {
375 fdt_error ("fdt checksum invalid");
376 return NULL;
377 }
378 puts ("OK\n");
379
380 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
381 fdt_error ("uImage is not a fdt");
382 return NULL;
383 }
384 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
385 fdt_error ("uImage is compressed");
386 return NULL;
387 }
388 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
389 fdt_error ("uImage data is not a fdt");
390 return NULL;
391 }
392 return fdt_hdr;
393}
394
Marian Balakowicz0d871362008-03-12 10:32:53 +0100395/**
396 * fit_check_fdt - verify FIT format FDT subimage
397 * @fit_hdr: pointer to the FIT header
398 * fdt_noffset: FDT subimage node offset within FIT image
399 * @verify: data CRC verification flag
400 *
401 * fit_check_fdt() verifies integrity of the FDT subimage and from
402 * specified FIT image.
403 *
404 * returns:
405 * 1, on success
406 * 0, on failure
407 */
408#if defined(CONFIG_FIT)
409static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
410{
411 fit_image_print (fit, fdt_noffset, " ");
412
413 if (verify) {
414 puts (" Verifying Hash Integrity ... ");
415 if (!fit_image_check_hashes (fit, fdt_noffset)) {
416 fdt_error ("Bad Data Hash");
417 return 0;
418 }
419 puts ("OK\n");
420 }
421
422 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
423 fdt_error ("Not a FDT image");
424 return 0;
425 }
426
427 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
428 fdt_error ("FDT image is compressed");
429 return 0;
430 }
431
432 return 1;
433}
434#endif /* CONFIG_FIT */
435
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100436static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100437 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Marian Balakowicz25154b62008-01-31 13:58:20 +0100438{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100439 ulong fdt_addr;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100440 image_header_t *fdt_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100441 char *fdt_blob = NULL;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100442 ulong image_start, image_end;
443 ulong load_start, load_end;
444#if defined(CONFIG_FIT)
Wolfgang Denk8f702ad2008-03-26 11:48:46 +0100445 void *fit_hdr;
446 const char *fit_uname_config = NULL;
447 const char *fit_uname_fdt = NULL;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100448 ulong default_addr;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100449 int cfg_noffset;
Marian Balakowicz0d871362008-03-12 10:32:53 +0100450 int fdt_noffset;
451 const void *data;
452 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100453#endif
Marian Balakowicz25154b62008-01-31 13:58:20 +0100454
Marian Balakowicz0d871362008-03-12 10:32:53 +0100455 *of_flat_tree = NULL;
456 *of_size = 0;
457
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100458 if (argc > 3 || genimg_has_config (images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100459#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100460 if (argc > 3) {
461 /*
462 * If the FDT blob comes from the FIT image and the
463 * FIT image address is omitted in the command line
464 * argument, try to use ramdisk or os FIT image
465 * address or default load address.
466 */
467 if (images->fit_uname_rd)
468 default_addr = (ulong)images->fit_hdr_rd;
469 else if (images->fit_uname_os)
470 default_addr = (ulong)images->fit_hdr_os;
471 else
472 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100473
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100474 if (fit_parse_conf (argv[3], default_addr,
475 &fdt_addr, &fit_uname_config)) {
476 debug ("* fdt: config '%s' from image at 0x%08lx\n",
477 fit_uname_config, fdt_addr);
478 } else if (fit_parse_subimage (argv[3], default_addr,
479 &fdt_addr, &fit_uname_fdt)) {
480 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
481 fit_uname_fdt, fdt_addr);
482 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100483#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100484 {
485 fdt_addr = simple_strtoul(argv[3], NULL, 16);
486 debug ("* fdt: cmdline image address = 0x%08lx\n",
487 fdt_addr);
488 }
489#if defined(CONFIG_FIT)
490 } else {
491 /* use FIT configuration provided in first bootm
492 * command argument
493 */
494 fdt_addr = (ulong)images->fit_hdr_os;
495 fit_uname_config = images->fit_uname_cfg;
496 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
497 fit_uname_config, fdt_addr);
498
499 /*
500 * Check whether configuration has FDT blob defined,
501 * if not quit silently.
502 */
503 fit_hdr = (void *)fdt_addr;
504 cfg_noffset = fit_conf_get_node (fit_hdr,
505 fit_uname_config);
506 if (cfg_noffset < 0) {
507 debug ("* fdt: no such config\n");
508 return 0;
509 }
510
511 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
512 cfg_noffset);
513 if (fdt_noffset < 0) {
514 debug ("* fdt: no fdt in config\n");
515 return 0;
516 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100517 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100518#endif
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100519
Marian Balakowicz0d871362008-03-12 10:32:53 +0100520 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100521 fdt_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100522
523 /* copy from dataflash if needed */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100524 fdt_addr = genimg_get_image (fdt_addr);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100525
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100526 /*
527 * Check if there is an FDT image at the
528 * address provided in the second bootm argument
529 * check image type, for FIT images get a FIT node.
530 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100531 switch (genimg_get_format ((void *)fdt_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100532 case IMAGE_FORMAT_LEGACY:
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100533 /* verify fdt_addr points to a valid image header */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100534 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100535 fdt_addr);
536 fdt_hdr = image_get_fdt (fdt_addr);
537 if (!fdt_hdr)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600538 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100539
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100540 /*
541 * move image data to the load address,
542 * make sure we don't overwrite initial image
543 */
Marian Balakowicz25154b62008-01-31 13:58:20 +0100544 image_start = (ulong)fdt_hdr;
545 image_end = image_get_image_end (fdt_hdr);
546
547 load_start = image_get_load (fdt_hdr);
548 load_end = load_start + image_get_data_size (fdt_hdr);
549
550 if ((load_start < image_end) && (load_end > image_start)) {
551 fdt_error ("fdt overwritten");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600552 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100553 }
Marian Balakowicz0d871362008-03-12 10:32:53 +0100554
555 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
556 image_get_data (fdt_hdr), load_start);
557
558 memmove ((void *)load_start,
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100559 (void *)image_get_data (fdt_hdr),
560 image_get_data_size (fdt_hdr));
Marian Balakowicz25154b62008-01-31 13:58:20 +0100561
Marian Balakowicz0d871362008-03-12 10:32:53 +0100562 fdt_blob = (char *)load_start;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100563 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100564 case IMAGE_FORMAT_FIT:
Marian Balakowicz12560682008-02-27 11:02:26 +0100565 /*
566 * This case will catch both: new uImage format
567 * (libfdt based) and raw FDT blob (also libfdt
568 * based).
569 */
570#if defined(CONFIG_FIT)
571 /* check FDT blob vs FIT blob */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100572 if (fit_check_format ((const void *)fdt_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100573 /*
574 * FIT image
575 */
576 fit_hdr = (void *)fdt_addr;
Marian Balakowicz0d871362008-03-12 10:32:53 +0100577 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
578 fdt_addr);
579
580 if (!fit_uname_fdt) {
581 /*
582 * no FDT blob image node unit name,
583 * try to get config node first. If
584 * config unit node name is NULL
585 * fit_conf_get_node() will try to
586 * find default config node
587 */
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100588 cfg_noffset = fit_conf_get_node (fit_hdr,
Marian Balakowicz0d871362008-03-12 10:32:53 +0100589 fit_uname_config);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100590
591 if (cfg_noffset < 0) {
592 fdt_error ("Could not find configuration node\n");
Marian Balakowicz0d871362008-03-12 10:32:53 +0100593 goto error;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100594 }
595
596 fit_uname_config = fdt_get_name (fit_hdr,
597 cfg_noffset, NULL);
598 printf (" Using '%s' configuration\n",
599 fit_uname_config);
Marian Balakowicz0d871362008-03-12 10:32:53 +0100600
601 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100602 cfg_noffset);
Marian Balakowicz0d871362008-03-12 10:32:53 +0100603 fit_uname_fdt = fit_get_name (fit_hdr,
604 fdt_noffset, NULL);
605 } else {
606 /* get FDT component image node offset */
607 fdt_noffset = fit_image_get_node (fit_hdr,
608 fit_uname_fdt);
609 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100610 if (fdt_noffset < 0) {
611 fdt_error ("Could not find subimage node\n");
Marian Balakowicz0d871362008-03-12 10:32:53 +0100612 goto error;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100613 }
Marian Balakowicz0d871362008-03-12 10:32:53 +0100614
615 printf (" Trying '%s' FDT blob subimage\n",
616 fit_uname_fdt);
617
618 if (!fit_check_fdt (fit_hdr, fdt_noffset,
619 images->verify))
620 goto error;
621
622 /* get ramdisk image data address and length */
623 if (fit_image_get_data (fit_hdr, fdt_noffset,
624 &data, &size)) {
625 fdt_error ("Could not find FDT subimage data");
626 goto error;
627 }
628
629 /* verift that image data is a proper FDT blob */
630 if (fdt_check_header ((char *)data) != 0) {
631 fdt_error ("Subimage data is not a FTD");
632 goto error;
633 }
634
635 /*
636 * move image data to the load address,
637 * make sure we don't overwrite initial image
638 */
639 image_start = (ulong)fit_hdr;
640 image_end = fit_get_end (fit_hdr);
641
642 if (fit_image_get_load (fit_hdr, fdt_noffset,
643 &load_start) == 0) {
644 load_end = load_start + size;
645
646 if ((load_start < image_end) &&
647 (load_end > image_start)) {
648 fdt_error ("FDT overwritten");
649 goto error;
650 }
651
652 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
653 (ulong)data, load_start);
654
655 memmove ((void *)load_start,
656 (void *)data, size);
657
658 fdt_blob = (char *)load_start;
659 } else {
660 fdt_blob = (char *)data;
661 }
662
663 images->fit_hdr_fdt = fit_hdr;
664 images->fit_uname_fdt = fit_uname_fdt;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100665 images->fit_noffset_fdt = fdt_noffset;
Marian Balakowicz0d871362008-03-12 10:32:53 +0100666 break;
Marian Balakowicz12560682008-02-27 11:02:26 +0100667 } else
668#endif
669 {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100670 /*
671 * FDT blob
672 */
Marian Balakowicze5d63b72008-04-11 11:07:43 +0200673 fdt_blob = (char *)fdt_addr;
Marian Balakowicz12560682008-02-27 11:02:26 +0100674 debug ("* fdt: raw FDT blob\n");
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700675 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100676 }
677 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100678 default:
679 fdt_error ("Did not find a cmdline Flattened Device Tree");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600680 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100681 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100682
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700683 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100684
685 } else if (images->legacy_hdr_valid &&
Marian Balakowiczab00e022008-04-11 11:07:49 +0200686 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100687
Marian Balakowicz25154b62008-01-31 13:58:20 +0100688 ulong fdt_data, fdt_len;
689
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100690 /*
691 * Now check if we have a legacy multi-component image,
692 * get second entry data start address and len.
693 */
Marian Balakowicz25154b62008-01-31 13:58:20 +0100694 printf ("## Flattened Device Tree from multi "
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100695 "component Image at %08lX\n",
696 (ulong)images->legacy_hdr_os);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100697
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100698 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100699 if (fdt_len) {
700
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100701 fdt_blob = (char *)fdt_data;
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700702 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100703
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100704 if (fdt_check_header (fdt_blob) != 0) {
Marian Balakowicz25154b62008-01-31 13:58:20 +0100705 fdt_error ("image is not a fdt");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600706 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100707 }
708
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100709 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
Marian Balakowicz25154b62008-01-31 13:58:20 +0100710 fdt_error ("fdt size != image size");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600711 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100712 }
713 } else {
Nick Spence5bcd4102008-05-08 22:32:22 -0700714 debug ("## No Flattened Device Tree\n");
715 return 0;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100716 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100717 } else {
718 debug ("## No Flattened Device Tree\n");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600719 return 0;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100720 }
721
722 *of_flat_tree = fdt_blob;
723 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
724 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
725 *of_flat_tree, *of_size);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600726
727 return 0;
728
729error:
730 do_reset (cmdtp, flag, argc, argv);
731 return 1;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100732}
733
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100734static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100735 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
736 char **of_flat_tree, ulong *of_size)
737{
738 char *fdt_blob = *of_flat_tree;
739 ulong relocate = 0;
Kumar Galab937bb72008-02-27 21:51:49 -0600740 ulong of_len = 0;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100741
742 /* nothing to do */
743 if (*of_size == 0)
Kumar Galab937bb72008-02-27 21:51:49 -0600744 return 0;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100745
746 if (fdt_check_header (fdt_blob) != 0) {
747 fdt_error ("image is not a fdt");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600748 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100749 }
750
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100751#ifndef CFG_NO_FLASH
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100752 /* move the blob if it is in flash (set relocate) */
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100753 if (addr2info ((ulong)fdt_blob) != NULL)
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100754 relocate = 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100755#endif
756
Marian Balakowicz25154b62008-01-31 13:58:20 +0100757 /*
Andy Fleming7d32ae32008-06-16 13:58:56 -0500758 * The blob needs to be inside the boot mapping.
Marian Balakowicz25154b62008-01-31 13:58:20 +0100759 */
Andy Fleming7d32ae32008-06-16 13:58:56 -0500760 if (fdt_blob < (char *)bootmap_base)
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100761 relocate = 1;
Kumar Galab937bb72008-02-27 21:51:49 -0600762
Andy Fleming7d32ae32008-06-16 13:58:56 -0500763 if ((fdt_blob + *of_size + CFG_FDT_PAD) >=
764 ((char *)CFG_BOOTMAPSZ + bootmap_base))
765 relocate = 1;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100766
767 /* move flattend device tree if needed */
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100768 if (relocate) {
Marian Balakowicz25154b62008-01-31 13:58:20 +0100769 int err;
Andy Fleming7d32ae32008-06-16 13:58:56 -0500770 ulong of_start = 0;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100771
772 /* position on a 4K boundary before the alloc_current */
Andy Fleming7d32ae32008-06-16 13:58:56 -0500773 /* Pad the FDT by a specified amount */
774 of_len = *of_size + CFG_FDT_PAD;
Becky Bruced26d67c2008-06-09 20:37:18 -0500775 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
Andy Fleming7d32ae32008-06-16 13:58:56 -0500776 (CFG_BOOTMAPSZ + bootmap_base));
Kumar Galab937bb72008-02-27 21:51:49 -0600777
778 if (of_start == 0) {
779 puts("device tree - allocation error\n");
780 goto error;
781 }
Marian Balakowicz25154b62008-01-31 13:58:20 +0100782
783 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
Andy Fleming7d32ae32008-06-16 13:58:56 -0500784 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
Marian Balakowicz25154b62008-01-31 13:58:20 +0100785 of_len, of_len);
786
787 printf (" Loading Device Tree to %08lx, end %08lx ... ",
788 of_start, of_start + of_len - 1);
789
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100790 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100791 if (err != 0) {
792 fdt_error ("fdt move failed");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600793 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100794 }
795 puts ("OK\n");
796
797 *of_flat_tree = (char *)of_start;
Andy Fleming7d32ae32008-06-16 13:58:56 -0500798 *of_size = of_len;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100799 } else {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100800 *of_flat_tree = fdt_blob;
Andy Fleming7d32ae32008-06-16 13:58:56 -0500801 of_len = (CFG_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
802 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
803 fdt_set_totalsize(*of_flat_tree, of_len);
804
805 *of_size = of_len;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100806 }
807
Kumar Galab937bb72008-02-27 21:51:49 -0600808 return 0;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600809
810error:
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600811 return 1;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100812}
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100813#endif