blob: e83c8608221d54eab8666804ca46c3f9a9329485 [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;
Kumar Galadcdcfea2008-08-15 08:24:31 -050083 void (*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
84 ulong r7, ulong r8, ulong r9);
Kumar Gala56bdf1d2008-02-27 21:51:45 -060085 int ret;
Kumar Galaa56d7d52008-02-27 21:51:44 -060086 ulong of_size = 0;
Kumar Galab937bb72008-02-27 21:51:49 -060087 struct lmb *lmb = images->lmb;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +010088
Marian Balakowiczd2160852008-01-31 13:20:07 +010089#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz6881dae2008-02-04 08:28:17 +010090 char *of_flat_tree = NULL;
Marian Balakowicz2437cf22008-01-08 18:11:43 +010091#endif
92
Kumar Galac5bacfd2008-02-27 21:51:50 -060093 bootmap_base = getenv_bootm_low();
Becky Bruced26d67c2008-06-09 20:37:18 -050094 bootm_size = getenv_bootm_size();
Kumar Galac5bacfd2008-02-27 21:51:50 -060095
96#ifdef DEBUG
Becky Bruced26d67c2008-06-09 20:37:18 -050097 if (((u64)bootmap_base + bootm_size) >
98 (CFG_SDRAM_BASE + (u64)gd->ram_size))
Kumar Galac5bacfd2008-02-27 21:51:50 -060099 puts("WARNING: bootm_low + bootm_size exceed total memory\n");
Becky Bruced26d67c2008-06-09 20:37:18 -0500100 if ((bootmap_base + bootm_size) > get_effective_memsize())
Kumar Galac5bacfd2008-02-27 21:51:50 -0600101 puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
102#endif
103
Becky Bruced26d67c2008-06-09 20:37:18 -0500104 size = min(bootm_size, get_effective_memsize());
Kumar Galac5bacfd2008-02-27 21:51:50 -0600105 size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
106
Becky Bruced26d67c2008-06-09 20:37:18 -0500107 if (size < bootm_size) {
Kumar Galac5bacfd2008-02-27 21:51:50 -0600108 ulong base = bootmap_base + size;
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700109 printf("WARNING: adjusting available memory to %lx\n", size);
Becky Bruced26d67c2008-06-09 20:37:18 -0500110 lmb_reserve(lmb, base, bootm_size - size);
Kumar Galac5bacfd2008-02-27 21:51:50 -0600111 }
Kumar Galab937bb72008-02-27 21:51:49 -0600112
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100113 /*
114 * Booting a (Linux) kernel image
115 *
116 * Allocate space for command line and board info - the
117 * address should be as high as possible within the reach of
118 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
119 * memory, which means far enough below the current stack
120 * pointer.
121 */
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100122 sp = get_sp();
Kumar Galac5bacfd2008-02-27 21:51:50 -0600123 debug ("## Current stack ends at 0x%08lx\n", sp);
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100124
Kumar Galab937bb72008-02-27 21:51:49 -0600125 /* adjust sp by 1K to be safe */
126 sp -= 1024;
127 lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100128
Kumar Galaa56d7d52008-02-27 21:51:44 -0600129#if defined(CONFIG_OF_LIBFDT)
130 /* find flattened device tree */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100131 ret = boot_get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600132
133 if (ret)
134 goto error;
Kumar Galaa56d7d52008-02-27 21:51:44 -0600135#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100136
Kumar Galaa56d7d52008-02-27 21:51:44 -0600137 if (!of_size) {
138 /* allocate space and init command line */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100139 ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -0600140 if (ret) {
141 puts("ERROR with allocation of cmdline\n");
142 goto error;
143 }
Kumar Galaa56d7d52008-02-27 21:51:44 -0600144
145 /* allocate space for kernel copy of board info */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100146 ret = boot_get_kbd (lmb, &kbd, bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -0600147 if (ret) {
148 puts("ERROR with allocation of kernel bd\n");
149 goto error;
150 }
Kumar Galaa56d7d52008-02-27 21:51:44 -0600151 set_clocks_in_mhz(kbd);
152 }
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100153
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100154 /* find kernel entry point */
155 if (images->legacy_hdr_valid) {
Marian Balakowiczab00e022008-04-11 11:07:49 +0200156 ep = image_get_ep (&images->legacy_hdr_os_copy);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100157#if defined(CONFIG_FIT)
158 } else if (images->fit_uname_os) {
Marian Balakowiczdf8ff332008-03-12 10:33:00 +0100159 ret = fit_image_get_entry (images->fit_hdr_os,
160 images->fit_noffset_os, &ep);
161 if (ret) {
162 puts ("Can't get entry point property!\n");
163 goto error;
164 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100165#endif
166 } else {
167 puts ("Could not find kernel entry point!\n");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600168 goto error;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100169 }
Kumar Galadcdcfea2008-08-15 08:24:31 -0500170 kernel = (void (*)(bd_t *, ulong, ulong, ulong,
171 ulong, ulong, ulong))ep;
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100172 /* find ramdisk */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100173 ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_PPC,
174 &rd_data_start, &rd_data_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600175 if (ret)
176 goto error;
177
Marian Balakowicz95418502008-01-31 13:55:39 +0100178 rd_len = rd_data_end - rd_data_start;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100179
Marian Balakowiczd2160852008-01-31 13:20:07 +0100180#if defined(CONFIG_OF_LIBFDT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100181 ret = boot_relocate_fdt (lmb, bootmap_base,
Kumar Galab937bb72008-02-27 21:51:49 -0600182 cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100183
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100184 /*
185 * Add the chosen node if it doesn't exist, add the env and bd_t
186 * if the user wants it (the logic is in the subroutines).
187 */
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100188 if (of_size) {
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600189 /* pass in dummy initrd info, we'll fix up later */
190 if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100191 fdt_error ("/chosen node create failed");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600192 goto error;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100193 }
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100194#ifdef CONFIG_OF_BOARD_SETUP
195 /* Call the board-specific fixup routine */
196 ft_board_setup(of_flat_tree, gd->bd);
197#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100198 }
Andy Fleming7d32ae32008-06-16 13:58:56 -0500199
200 /* Fixup the fdt memreserve now that we know how big it is */
201 if (of_flat_tree) {
202 int j;
203 uint64_t addr, size;
204 int total = fdt_num_mem_rsv(of_flat_tree);
205 uint actualsize;
206
207 for (j = 0; j < total; j++) {
208 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
Kumar Gala3fddff12008-07-07 09:39:06 -0500209 if (addr == (uint64_t)(u32)of_flat_tree) {
Andy Fleming7d32ae32008-06-16 13:58:56 -0500210 fdt_del_mem_rsv(of_flat_tree, j);
211 break;
212 }
213 }
214
215 /* Delete the old LMB reservation */
Andy Fleming09d2a712008-07-07 14:24:39 -0500216 lmb_free(lmb, (phys_addr_t)(u32)of_flat_tree,
217 (phys_size_t)fdt_totalsize(of_flat_tree));
Andy Fleming7d32ae32008-06-16 13:58:56 -0500218
219 /* Calculate the actual size of the fdt */
220 actualsize = fdt_off_dt_strings(of_flat_tree) +
221 fdt_size_dt_strings(of_flat_tree);
222
223 /* Make it so the fdt ends on a page boundary */
224 actualsize = ALIGN(actualsize, 0x1000);
225 actualsize = actualsize - ((uint)of_flat_tree & 0xfff);
226
227 /* Change the fdt header to reflect the correct size */
228 fdt_set_totalsize(of_flat_tree, actualsize);
229 of_size = actualsize;
230
231 /* Add the new reservation */
232 ret = fdt_add_mem_rsv(of_flat_tree, (uint)of_flat_tree,
233 of_size);
234
235 /* Create a new LMB reservation */
236 lmb_reserve(lmb, (ulong)of_flat_tree, of_size);
237 }
Marian Balakowiczd2160852008-01-31 13:20:07 +0100238#endif /* CONFIG_OF_LIBFDT */
Marian Balakowicz751b2b82008-01-08 18:11:43 +0100239
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100240 ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
Kumar Galab937bb72008-02-27 21:51:49 -0600241 if (ret)
242 goto error;
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600243
244#if defined(CONFIG_OF_LIBFDT)
245 /* fixup the initrd now that we know where it should be */
246 if ((of_flat_tree) && (initrd_start && initrd_end)) {
247 uint64_t addr, size;
248 int total = fdt_num_mem_rsv(of_flat_tree);
249 int j;
250
251 /* Look for the dummy entry and delete it */
252 for (j = 0; j < total; j++) {
253 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
254 if (addr == rd_data_start) {
255 fdt_del_mem_rsv(of_flat_tree, j);
256 break;
257 }
258 }
259
260 ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
261 initrd_end - initrd_start + 1);
262 if (ret < 0) {
263 printf("fdt_chosen: %s\n", fdt_strerror(ret));
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600264 goto error;
Kumar Gala56bdf1d2008-02-27 21:51:45 -0600265 }
266
267 do_fixup_by_path_u32(of_flat_tree, "/chosen",
268 "linux,initrd-start", initrd_start, 0);
269 do_fixup_by_path_u32(of_flat_tree, "/chosen",
270 "linux,initrd-end", initrd_end, 0);
271 }
272#endif
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100273 debug ("## Transferring control to Linux (at address %08lx) ...\n",
274 (ulong)kernel);
275
276 show_boot_progress (15);
277
278#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
279 unlock_ram_in_cache();
280#endif
281
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):
Kumar Galadcdcfea2008-08-15 08:24:31 -0500286 * r3: pointer to the fdt
287 * r4: 0
288 * r5: 0
289 * r6: epapr magic
290 * r7: size of IMA in bytes
291 * r8: 0
292 * r9: 0
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100293 */
Kumar Galadcdcfea2008-08-15 08:24:31 -0500294#if defined(CONFIG_85xx) || defined(CONFIG_440)
295 #define EPAPR_MAGIC (0x45504150)
296#else
297 #define EPAPR_MAGIC (0x65504150)
298#endif
299
Marian Balakowicz0d871362008-03-12 10:32:53 +0100300 debug (" Booting using OF flat tree...\n");
Kumar Galadcdcfea2008-08-15 08:24:31 -0500301 (*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
302 CFG_BOOTMAPSZ, 0, 0);
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100303 /* does not return */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100304 } else
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100305#endif
Marian Balakowicz0d871362008-03-12 10:32:53 +0100306 {
307 /*
308 * Linux Kernel Parameters (passing board info data):
309 * r3: ptr to board info data
310 * r4: initrd_start or 0 if no initrd
311 * r5: initrd_end - unused if r4 is 0
312 * r6: Start of command line string
313 * r7: End of command line string
Kumar Galadcdcfea2008-08-15 08:24:31 -0500314 * r8: 0
315 * r9: 0
Marian Balakowicz0d871362008-03-12 10:32:53 +0100316 */
317 debug (" Booting using board info...\n");
Kumar Galadcdcfea2008-08-15 08:24:31 -0500318 (*kernel) (kbd, initrd_start, initrd_end,
319 cmd_start, cmd_end, 0, 0);
Marian Balakowicz0d871362008-03-12 10:32:53 +0100320 /* does not return */
321 }
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600322 return ;
323
324error:
Kumar Gala026e4ac2008-08-11 09:20:53 -0500325 do_reset (cmdtp, flag, argc, argv);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600326 return ;
Marian Balakowicz2437cf22008-01-08 18:11:43 +0100327}
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100328
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100329static ulong get_sp (void)
330{
331 ulong sp;
332
333 asm( "mr %0,1": "=r"(sp) : );
334 return sp;
335}
336
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100337static void set_clocks_in_mhz (bd_t *kbd)
338{
339 char *s;
340
341 if ((s = getenv ("clocks_in_mhz")) != NULL) {
342 /* convert all clock information to MHz */
343 kbd->bi_intfreq /= 1000000L;
344 kbd->bi_busfreq /= 1000000L;
345#if defined(CONFIG_MPC8220)
346 kbd->bi_inpfreq /= 1000000L;
347 kbd->bi_pcifreq /= 1000000L;
348 kbd->bi_pevfreq /= 1000000L;
349 kbd->bi_flbfreq /= 1000000L;
350 kbd->bi_vcofreq /= 1000000L;
351#endif
352#if defined(CONFIG_CPM2)
353 kbd->bi_cpmfreq /= 1000000L;
354 kbd->bi_brgfreq /= 1000000L;
355 kbd->bi_sccfreq /= 1000000L;
Wolfgang Denk8f702ad2008-03-26 11:48:46 +0100356 kbd->bi_vco /= 1000000L;
Marian Balakowicz2efc2642008-01-31 13:58:13 +0100357#endif
358#if defined(CONFIG_MPC5xxx)
359 kbd->bi_ipbfreq /= 1000000L;
360 kbd->bi_pcifreq /= 1000000L;
361#endif /* CONFIG_MPC5xxx */
362 }
363}
364
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100365#if defined(CONFIG_OF_LIBFDT)
366static void fdt_error (const char *msg)
367{
368 puts ("ERROR: ");
369 puts (msg);
370 puts (" - must RESET the board to recover.\n");
371}
Marian Balakowicz25154b62008-01-31 13:58:20 +0100372
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100373static image_header_t *image_get_fdt (ulong fdt_addr)
374{
375 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
376
377 image_print_contents (fdt_hdr);
378
379 puts (" Verifying Checksum ... ");
380 if (!image_check_hcrc (fdt_hdr)) {
381 fdt_error ("fdt header checksum invalid");
382 return NULL;
383 }
384
385 if (!image_check_dcrc (fdt_hdr)) {
386 fdt_error ("fdt checksum invalid");
387 return NULL;
388 }
389 puts ("OK\n");
390
391 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
392 fdt_error ("uImage is not a fdt");
393 return NULL;
394 }
395 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
396 fdt_error ("uImage is compressed");
397 return NULL;
398 }
399 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
400 fdt_error ("uImage data is not a fdt");
401 return NULL;
402 }
403 return fdt_hdr;
404}
405
Marian Balakowicz0d871362008-03-12 10:32:53 +0100406/**
407 * fit_check_fdt - verify FIT format FDT subimage
408 * @fit_hdr: pointer to the FIT header
409 * fdt_noffset: FDT subimage node offset within FIT image
410 * @verify: data CRC verification flag
411 *
412 * fit_check_fdt() verifies integrity of the FDT subimage and from
413 * specified FIT image.
414 *
415 * returns:
416 * 1, on success
417 * 0, on failure
418 */
419#if defined(CONFIG_FIT)
420static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
421{
422 fit_image_print (fit, fdt_noffset, " ");
423
424 if (verify) {
425 puts (" Verifying Hash Integrity ... ");
426 if (!fit_image_check_hashes (fit, fdt_noffset)) {
427 fdt_error ("Bad Data Hash");
428 return 0;
429 }
430 puts ("OK\n");
431 }
432
433 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
434 fdt_error ("Not a FDT image");
435 return 0;
436 }
437
438 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
439 fdt_error ("FDT image is compressed");
440 return 0;
441 }
442
443 return 1;
444}
445#endif /* CONFIG_FIT */
446
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100447static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100448 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Marian Balakowicz25154b62008-01-31 13:58:20 +0100449{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100450 ulong fdt_addr;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100451 image_header_t *fdt_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100452 char *fdt_blob = NULL;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100453 ulong image_start, image_end;
454 ulong load_start, load_end;
455#if defined(CONFIG_FIT)
Wolfgang Denk8f702ad2008-03-26 11:48:46 +0100456 void *fit_hdr;
457 const char *fit_uname_config = NULL;
458 const char *fit_uname_fdt = NULL;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100459 ulong default_addr;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100460 int cfg_noffset;
Marian Balakowicz0d871362008-03-12 10:32:53 +0100461 int fdt_noffset;
462 const void *data;
463 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100464#endif
Marian Balakowicz25154b62008-01-31 13:58:20 +0100465
Marian Balakowicz0d871362008-03-12 10:32:53 +0100466 *of_flat_tree = NULL;
467 *of_size = 0;
468
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100469 if (argc > 3 || genimg_has_config (images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100470#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100471 if (argc > 3) {
472 /*
473 * If the FDT blob comes from the FIT image and the
474 * FIT image address is omitted in the command line
475 * argument, try to use ramdisk or os FIT image
476 * address or default load address.
477 */
478 if (images->fit_uname_rd)
479 default_addr = (ulong)images->fit_hdr_rd;
480 else if (images->fit_uname_os)
481 default_addr = (ulong)images->fit_hdr_os;
482 else
483 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100484
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100485 if (fit_parse_conf (argv[3], default_addr,
486 &fdt_addr, &fit_uname_config)) {
487 debug ("* fdt: config '%s' from image at 0x%08lx\n",
488 fit_uname_config, fdt_addr);
489 } else if (fit_parse_subimage (argv[3], default_addr,
490 &fdt_addr, &fit_uname_fdt)) {
491 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
492 fit_uname_fdt, fdt_addr);
493 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100494#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100495 {
496 fdt_addr = simple_strtoul(argv[3], NULL, 16);
497 debug ("* fdt: cmdline image address = 0x%08lx\n",
498 fdt_addr);
499 }
500#if defined(CONFIG_FIT)
501 } else {
502 /* use FIT configuration provided in first bootm
503 * command argument
504 */
505 fdt_addr = (ulong)images->fit_hdr_os;
506 fit_uname_config = images->fit_uname_cfg;
507 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
508 fit_uname_config, fdt_addr);
509
510 /*
511 * Check whether configuration has FDT blob defined,
512 * if not quit silently.
513 */
514 fit_hdr = (void *)fdt_addr;
515 cfg_noffset = fit_conf_get_node (fit_hdr,
516 fit_uname_config);
517 if (cfg_noffset < 0) {
518 debug ("* fdt: no such config\n");
519 return 0;
520 }
521
522 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
523 cfg_noffset);
524 if (fdt_noffset < 0) {
525 debug ("* fdt: no fdt in config\n");
526 return 0;
527 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100528 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100529#endif
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100530
Marian Balakowicz0d871362008-03-12 10:32:53 +0100531 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100532 fdt_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100533
534 /* copy from dataflash if needed */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100535 fdt_addr = genimg_get_image (fdt_addr);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100536
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100537 /*
538 * Check if there is an FDT image at the
539 * address provided in the second bootm argument
540 * check image type, for FIT images get a FIT node.
541 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100542 switch (genimg_get_format ((void *)fdt_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100543 case IMAGE_FORMAT_LEGACY:
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100544 /* verify fdt_addr points to a valid image header */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100545 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100546 fdt_addr);
547 fdt_hdr = image_get_fdt (fdt_addr);
548 if (!fdt_hdr)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600549 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100550
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100551 /*
552 * move image data to the load address,
553 * make sure we don't overwrite initial image
554 */
Marian Balakowicz25154b62008-01-31 13:58:20 +0100555 image_start = (ulong)fdt_hdr;
556 image_end = image_get_image_end (fdt_hdr);
557
558 load_start = image_get_load (fdt_hdr);
559 load_end = load_start + image_get_data_size (fdt_hdr);
560
561 if ((load_start < image_end) && (load_end > image_start)) {
562 fdt_error ("fdt overwritten");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600563 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100564 }
Marian Balakowicz0d871362008-03-12 10:32:53 +0100565
566 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
567 image_get_data (fdt_hdr), load_start);
568
569 memmove ((void *)load_start,
Marian Balakowicz351d3e32008-02-27 11:02:26 +0100570 (void *)image_get_data (fdt_hdr),
571 image_get_data_size (fdt_hdr));
Marian Balakowicz25154b62008-01-31 13:58:20 +0100572
Marian Balakowicz0d871362008-03-12 10:32:53 +0100573 fdt_blob = (char *)load_start;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100574 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100575 case IMAGE_FORMAT_FIT:
Marian Balakowicz12560682008-02-27 11:02:26 +0100576 /*
577 * This case will catch both: new uImage format
578 * (libfdt based) and raw FDT blob (also libfdt
579 * based).
580 */
581#if defined(CONFIG_FIT)
582 /* check FDT blob vs FIT blob */
Marian Balakowicz0d871362008-03-12 10:32:53 +0100583 if (fit_check_format ((const void *)fdt_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100584 /*
585 * FIT image
586 */
587 fit_hdr = (void *)fdt_addr;
Marian Balakowicz0d871362008-03-12 10:32:53 +0100588 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
589 fdt_addr);
590
591 if (!fit_uname_fdt) {
592 /*
593 * no FDT blob image node unit name,
594 * try to get config node first. If
595 * config unit node name is NULL
596 * fit_conf_get_node() will try to
597 * find default config node
598 */
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100599 cfg_noffset = fit_conf_get_node (fit_hdr,
Marian Balakowicz0d871362008-03-12 10:32:53 +0100600 fit_uname_config);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100601
602 if (cfg_noffset < 0) {
603 fdt_error ("Could not find configuration node\n");
Marian Balakowicz0d871362008-03-12 10:32:53 +0100604 goto error;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100605 }
606
607 fit_uname_config = fdt_get_name (fit_hdr,
608 cfg_noffset, NULL);
609 printf (" Using '%s' configuration\n",
610 fit_uname_config);
Marian Balakowicz0d871362008-03-12 10:32:53 +0100611
612 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100613 cfg_noffset);
Marian Balakowicz0d871362008-03-12 10:32:53 +0100614 fit_uname_fdt = fit_get_name (fit_hdr,
615 fdt_noffset, NULL);
616 } else {
617 /* get FDT component image node offset */
618 fdt_noffset = fit_image_get_node (fit_hdr,
619 fit_uname_fdt);
620 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100621 if (fdt_noffset < 0) {
622 fdt_error ("Could not find subimage node\n");
Marian Balakowicz0d871362008-03-12 10:32:53 +0100623 goto error;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100624 }
Marian Balakowicz0d871362008-03-12 10:32:53 +0100625
626 printf (" Trying '%s' FDT blob subimage\n",
627 fit_uname_fdt);
628
629 if (!fit_check_fdt (fit_hdr, fdt_noffset,
630 images->verify))
631 goto error;
632
633 /* get ramdisk image data address and length */
634 if (fit_image_get_data (fit_hdr, fdt_noffset,
635 &data, &size)) {
636 fdt_error ("Could not find FDT subimage data");
637 goto error;
638 }
639
640 /* verift that image data is a proper FDT blob */
641 if (fdt_check_header ((char *)data) != 0) {
642 fdt_error ("Subimage data is not a FTD");
643 goto error;
644 }
645
646 /*
647 * move image data to the load address,
648 * make sure we don't overwrite initial image
649 */
650 image_start = (ulong)fit_hdr;
651 image_end = fit_get_end (fit_hdr);
652
653 if (fit_image_get_load (fit_hdr, fdt_noffset,
654 &load_start) == 0) {
655 load_end = load_start + size;
656
657 if ((load_start < image_end) &&
658 (load_end > image_start)) {
659 fdt_error ("FDT overwritten");
660 goto error;
661 }
662
663 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
664 (ulong)data, load_start);
665
666 memmove ((void *)load_start,
667 (void *)data, size);
668
669 fdt_blob = (char *)load_start;
670 } else {
671 fdt_blob = (char *)data;
672 }
673
674 images->fit_hdr_fdt = fit_hdr;
675 images->fit_uname_fdt = fit_uname_fdt;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100676 images->fit_noffset_fdt = fdt_noffset;
Marian Balakowicz0d871362008-03-12 10:32:53 +0100677 break;
Marian Balakowicz12560682008-02-27 11:02:26 +0100678 } else
679#endif
680 {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100681 /*
682 * FDT blob
683 */
Marian Balakowicze5d63b72008-04-11 11:07:43 +0200684 fdt_blob = (char *)fdt_addr;
Marian Balakowicz12560682008-02-27 11:02:26 +0100685 debug ("* fdt: raw FDT blob\n");
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700686 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100687 }
688 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100689 default:
690 fdt_error ("Did not find a cmdline Flattened Device Tree");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600691 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100692 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100693
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700694 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100695
696 } else if (images->legacy_hdr_valid &&
Marian Balakowiczab00e022008-04-11 11:07:49 +0200697 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100698
Marian Balakowicz25154b62008-01-31 13:58:20 +0100699 ulong fdt_data, fdt_len;
700
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100701 /*
702 * Now check if we have a legacy multi-component image,
703 * get second entry data start address and len.
704 */
Marian Balakowicz25154b62008-01-31 13:58:20 +0100705 printf ("## Flattened Device Tree from multi "
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100706 "component Image at %08lX\n",
707 (ulong)images->legacy_hdr_os);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100708
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100709 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100710 if (fdt_len) {
711
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100712 fdt_blob = (char *)fdt_data;
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700713 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100714
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100715 if (fdt_check_header (fdt_blob) != 0) {
Marian Balakowicz25154b62008-01-31 13:58:20 +0100716 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 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
Marian Balakowicz25154b62008-01-31 13:58:20 +0100721 fdt_error ("fdt size != image size");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600722 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100723 }
724 } else {
Nick Spence5bcd4102008-05-08 22:32:22 -0700725 debug ("## No Flattened Device Tree\n");
726 return 0;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100727 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100728 } else {
729 debug ("## No Flattened Device Tree\n");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600730 return 0;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100731 }
732
733 *of_flat_tree = fdt_blob;
734 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
735 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
736 *of_flat_tree, *of_size);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600737
738 return 0;
739
740error:
741 do_reset (cmdtp, flag, argc, argv);
742 return 1;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100743}
744
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100745static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100746 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
747 char **of_flat_tree, ulong *of_size)
748{
749 char *fdt_blob = *of_flat_tree;
750 ulong relocate = 0;
Kumar Galab937bb72008-02-27 21:51:49 -0600751 ulong of_len = 0;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100752
753 /* nothing to do */
754 if (*of_size == 0)
Kumar Galab937bb72008-02-27 21:51:49 -0600755 return 0;
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100756
757 if (fdt_check_header (fdt_blob) != 0) {
758 fdt_error ("image is not a fdt");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600759 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100760 }
761
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100762#ifndef CFG_NO_FLASH
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100763 /* move the blob if it is in flash (set relocate) */
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100764 if (addr2info ((ulong)fdt_blob) != NULL)
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100765 relocate = 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100766#endif
767
Marian Balakowicz25154b62008-01-31 13:58:20 +0100768 /*
Andy Fleming7d32ae32008-06-16 13:58:56 -0500769 * The blob needs to be inside the boot mapping.
Marian Balakowicz25154b62008-01-31 13:58:20 +0100770 */
Andy Fleming7d32ae32008-06-16 13:58:56 -0500771 if (fdt_blob < (char *)bootmap_base)
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100772 relocate = 1;
Kumar Galab937bb72008-02-27 21:51:49 -0600773
Andy Fleming7d32ae32008-06-16 13:58:56 -0500774 if ((fdt_blob + *of_size + CFG_FDT_PAD) >=
775 ((char *)CFG_BOOTMAPSZ + bootmap_base))
776 relocate = 1;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100777
778 /* move flattend device tree if needed */
Marian Balakowicz6881dae2008-02-04 08:28:17 +0100779 if (relocate) {
Marian Balakowicz25154b62008-01-31 13:58:20 +0100780 int err;
Andy Fleming7d32ae32008-06-16 13:58:56 -0500781 ulong of_start = 0;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100782
783 /* position on a 4K boundary before the alloc_current */
Andy Fleming7d32ae32008-06-16 13:58:56 -0500784 /* Pad the FDT by a specified amount */
785 of_len = *of_size + CFG_FDT_PAD;
Becky Bruced26d67c2008-06-09 20:37:18 -0500786 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
Andy Fleming7d32ae32008-06-16 13:58:56 -0500787 (CFG_BOOTMAPSZ + bootmap_base));
Kumar Galab937bb72008-02-27 21:51:49 -0600788
789 if (of_start == 0) {
790 puts("device tree - allocation error\n");
791 goto error;
792 }
Marian Balakowicz25154b62008-01-31 13:58:20 +0100793
794 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
Andy Fleming7d32ae32008-06-16 13:58:56 -0500795 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
Marian Balakowicz25154b62008-01-31 13:58:20 +0100796 of_len, of_len);
797
798 printf (" Loading Device Tree to %08lx, end %08lx ... ",
799 of_start, of_start + of_len - 1);
800
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100801 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
Marian Balakowicz25154b62008-01-31 13:58:20 +0100802 if (err != 0) {
803 fdt_error ("fdt move failed");
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600804 goto error;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100805 }
806 puts ("OK\n");
807
808 *of_flat_tree = (char *)of_start;
Andy Fleming7d32ae32008-06-16 13:58:56 -0500809 *of_size = of_len;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100810 } else {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100811 *of_flat_tree = fdt_blob;
Andy Fleming7d32ae32008-06-16 13:58:56 -0500812 of_len = (CFG_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
813 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
814 fdt_set_totalsize(*of_flat_tree, of_len);
815
816 *of_size = of_len;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100817 }
818
Kumar Galab937bb72008-02-27 21:51:49 -0600819 return 0;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600820
821error:
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600822 return 1;
Marian Balakowicz25154b62008-01-31 13:58:20 +0100823}
Marian Balakowicz28fb6152008-01-31 13:20:08 +0100824#endif