blob: 81803ddef3dd0aca138359374bf123a4641595b9 [file] [log] [blame]
Marian Balakowicz5d3cc552008-01-08 18:11:43 +01001/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
Marian Balakowicz75824382008-01-31 13:20:06 +010026
Marian Balakowicz5d3cc552008-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 Balakowiczd3c5eb62008-01-31 13:20:08 +010041
42static void fdt_error (const char *msg);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +010043static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +010044 bootm_headers_t *images, char **of_flat_tree, ulong *of_size);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +010045static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
Marian Balakowiczd2ced9e2008-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 Balakowicz5d3cc552008-01-08 18:11:43 +010048#endif
49
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010050#ifdef CFG_INIT_RAM_LOCK
51#include <asm/cache.h>
52#endif
53
Andy Fleming41c5eaa2008-06-16 13:58:56 -050054#ifndef CFG_FDT_PAD
55#define CFG_FDT_PAD 0x3000
56#endif
57
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010058DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010059
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010060extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
Kumar Galae822d7f2008-02-27 21:51:49 -060061extern ulong get_effective_memsize(void);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +010062static ulong get_sp (void);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010063static void set_clocks_in_mhz (bd_t *kbd);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010064
Kumar Galad3f2fa02008-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 Balakowicz5d3cc552008-01-08 18:11:43 +010069void __attribute__((noinline))
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010070do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
71 bootm_headers_t *images)
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010072{
Kumar Galae822d7f2008-02-27 21:51:49 -060073 ulong sp;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010074
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010075 ulong initrd_start, initrd_end;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010076 ulong rd_data_start, rd_data_end, rd_len;
Kumar Galad3f2fa02008-02-27 21:51:50 -060077 ulong size;
Becky Bruce391fd932008-06-09 20:37:18 -050078 phys_size_t bootm_size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010079
Kumar Galae822d7f2008-02-27 21:51:49 -060080 ulong cmd_start, cmd_end, bootmap_base;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010081 bd_t *kbd;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010082 ulong ep = 0;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010083 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
Kumar Galad2bc0952008-02-27 21:51:45 -060084 int ret;
Kumar Gala27953492008-02-27 21:51:44 -060085 ulong of_size = 0;
Kumar Galae822d7f2008-02-27 21:51:49 -060086 struct lmb *lmb = images->lmb;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010087
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +010088#if defined(CONFIG_OF_LIBFDT)
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +010089 char *of_flat_tree = NULL;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +010090#endif
91
Kumar Galad3f2fa02008-02-27 21:51:50 -060092 bootmap_base = getenv_bootm_low();
Becky Bruce391fd932008-06-09 20:37:18 -050093 bootm_size = getenv_bootm_size();
Kumar Galad3f2fa02008-02-27 21:51:50 -060094
95#ifdef DEBUG
Becky Bruce391fd932008-06-09 20:37:18 -050096 if (((u64)bootmap_base + bootm_size) >
97 (CFG_SDRAM_BASE + (u64)gd->ram_size))
Kumar Galad3f2fa02008-02-27 21:51:50 -060098 puts("WARNING: bootm_low + bootm_size exceed total memory\n");
Becky Bruce391fd932008-06-09 20:37:18 -050099 if ((bootmap_base + bootm_size) > get_effective_memsize())
Kumar Galad3f2fa02008-02-27 21:51:50 -0600100 puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
101#endif
102
Becky Bruce391fd932008-06-09 20:37:18 -0500103 size = min(bootm_size, get_effective_memsize());
Kumar Galad3f2fa02008-02-27 21:51:50 -0600104 size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
105
Becky Bruce391fd932008-06-09 20:37:18 -0500106 if (size < bootm_size) {
Kumar Galad3f2fa02008-02-27 21:51:50 -0600107 ulong base = bootmap_base + size;
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700108 printf("WARNING: adjusting available memory to %lx\n", size);
Becky Bruce391fd932008-06-09 20:37:18 -0500109 lmb_reserve(lmb, base, bootm_size - size);
Kumar Galad3f2fa02008-02-27 21:51:50 -0600110 }
Kumar Galae822d7f2008-02-27 21:51:49 -0600111
Marian Balakowicz5d3cc552008-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 Balakowiczb6b0fe62008-01-31 13:58:13 +0100121 sp = get_sp();
Kumar Galad3f2fa02008-02-27 21:51:50 -0600122 debug ("## Current stack ends at 0x%08lx\n", sp);
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100123
Kumar Galae822d7f2008-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 Balakowicz5d3cc552008-01-08 18:11:43 +0100127
Kumar Gala27953492008-02-27 21:51:44 -0600128#if defined(CONFIG_OF_LIBFDT)
129 /* find flattened device tree */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100130 ret = boot_get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
Kumar Gala274cea22008-02-27 21:51:46 -0600131
132 if (ret)
133 goto error;
Kumar Gala27953492008-02-27 21:51:44 -0600134#endif
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100135
Kumar Gala27953492008-02-27 21:51:44 -0600136 if (!of_size) {
137 /* allocate space and init command line */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100138 ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
Kumar Galae822d7f2008-02-27 21:51:49 -0600139 if (ret) {
140 puts("ERROR with allocation of cmdline\n");
141 goto error;
142 }
Kumar Gala27953492008-02-27 21:51:44 -0600143
144 /* allocate space for kernel copy of board info */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100145 ret = boot_get_kbd (lmb, &kbd, bootmap_base);
Kumar Galae822d7f2008-02-27 21:51:49 -0600146 if (ret) {
147 puts("ERROR with allocation of kernel bd\n");
148 goto error;
149 }
Kumar Gala27953492008-02-27 21:51:44 -0600150 set_clocks_in_mhz(kbd);
151 }
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100152
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100153 /* find kernel entry point */
154 if (images->legacy_hdr_valid) {
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200155 ep = image_get_ep (&images->legacy_hdr_os_copy);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100156#if defined(CONFIG_FIT)
157 } else if (images->fit_uname_os) {
Marian Balakowiczcd7c5962008-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 Balakowiczd5934ad2008-02-04 08:28:09 +0100164#endif
165 } else {
166 puts ("Could not find kernel entry point!\n");
Kumar Gala274cea22008-02-27 21:51:46 -0600167 goto error;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100168 }
169 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100170
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100171 /* find ramdisk */
Marian Balakowiczd985c842008-03-12 10:14:38 +0100172 ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_PPC,
173 &rd_data_start, &rd_data_end);
Kumar Gala274cea22008-02-27 21:51:46 -0600174 if (ret)
175 goto error;
176
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100177 rd_len = rd_data_end - rd_data_start;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100178
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100179#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100180 ret = boot_relocate_fdt (lmb, bootmap_base,
Kumar Galae822d7f2008-02-27 21:51:49 -0600181 cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100182
Marian Balakowicz5d3cc552008-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 Balakowiczd2ced9e2008-02-04 08:28:17 +0100187 if (of_size) {
Kumar Galad2bc0952008-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 Balakowiczd3c5eb62008-01-31 13:20:08 +0100190 fdt_error ("/chosen node create failed");
Kumar Gala274cea22008-02-27 21:51:46 -0600191 goto error;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100192 }
Marian Balakowicz5d3cc552008-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
197 }
Andy Fleming41c5eaa2008-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);
Kumar Galada8693a2008-07-07 09:39:06 -0500208 if (addr == (uint64_t)(u32)of_flat_tree) {
Andy Fleming41c5eaa2008-06-16 13:58:56 -0500209 fdt_del_mem_rsv(of_flat_tree, j);
210 break;
211 }
212 }
213
214 /* Delete the old LMB reservation */
Andy Fleming98874ff2008-07-07 14:24:39 -0500215 lmb_free(lmb, (phys_addr_t)(u32)of_flat_tree,
216 (phys_size_t)fdt_totalsize(of_flat_tree));
Andy Fleming41c5eaa2008-06-16 13:58:56 -0500217
218 /* Calculate the actual size of the fdt */
219 actualsize = fdt_off_dt_strings(of_flat_tree) +
220 fdt_size_dt_strings(of_flat_tree);
221
222 /* Make it so the fdt ends on a page boundary */
223 actualsize = ALIGN(actualsize, 0x1000);
224 actualsize = actualsize - ((uint)of_flat_tree & 0xfff);
225
226 /* Change the fdt header to reflect the correct size */
227 fdt_set_totalsize(of_flat_tree, actualsize);
228 of_size = actualsize;
229
230 /* Add the new reservation */
231 ret = fdt_add_mem_rsv(of_flat_tree, (uint)of_flat_tree,
232 of_size);
233
234 /* Create a new LMB reservation */
235 lmb_reserve(lmb, (ulong)of_flat_tree, of_size);
236 }
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100237#endif /* CONFIG_OF_LIBFDT */
Marian Balakowiczd45d5a12008-01-08 18:11:43 +0100238
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100239 ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
Kumar Galae822d7f2008-02-27 21:51:49 -0600240 if (ret)
241 goto error;
Kumar Galad2bc0952008-02-27 21:51:45 -0600242
243#if defined(CONFIG_OF_LIBFDT)
244 /* fixup the initrd now that we know where it should be */
245 if ((of_flat_tree) && (initrd_start && initrd_end)) {
246 uint64_t addr, size;
247 int total = fdt_num_mem_rsv(of_flat_tree);
248 int j;
249
250 /* Look for the dummy entry and delete it */
251 for (j = 0; j < total; j++) {
252 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
253 if (addr == rd_data_start) {
254 fdt_del_mem_rsv(of_flat_tree, j);
255 break;
256 }
257 }
258
259 ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
260 initrd_end - initrd_start + 1);
261 if (ret < 0) {
262 printf("fdt_chosen: %s\n", fdt_strerror(ret));
Kumar Gala274cea22008-02-27 21:51:46 -0600263 goto error;
Kumar Galad2bc0952008-02-27 21:51:45 -0600264 }
265
266 do_fixup_by_path_u32(of_flat_tree, "/chosen",
267 "linux,initrd-start", initrd_start, 0);
268 do_fixup_by_path_u32(of_flat_tree, "/chosen",
269 "linux,initrd-end", initrd_end, 0);
270 }
271#endif
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100272 debug ("## Transferring control to Linux (at address %08lx) ...\n",
273 (ulong)kernel);
274
275 show_boot_progress (15);
276
277#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
278 unlock_ram_in_cache();
279#endif
Kumar Gala75fa0022008-02-27 21:51:51 -0600280 if (!images->autostart)
281 return ;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100282
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100283#if defined(CONFIG_OF_LIBFDT)
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100284 if (of_flat_tree) { /* device tree; boot new style */
285 /*
286 * Linux Kernel Parameters (passing device tree):
287 * r3: pointer to the fdt, followed by the board info data
288 * r4: physical pointer to the kernel itself
289 * r5: NULL
290 * r6: NULL
291 * r7: NULL
292 */
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100293 debug (" Booting using OF flat tree...\n");
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100294 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
295 /* does not return */
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100296 } else
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100297#endif
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100298 {
299 /*
300 * Linux Kernel Parameters (passing board info data):
301 * r3: ptr to board info data
302 * r4: initrd_start or 0 if no initrd
303 * r5: initrd_end - unused if r4 is 0
304 * r6: Start of command line string
305 * r7: End of command line string
306 */
307 debug (" Booting using board info...\n");
308 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
309 /* does not return */
310 }
Kumar Gala274cea22008-02-27 21:51:46 -0600311 return ;
312
313error:
Kumar Gala75fa0022008-02-27 21:51:51 -0600314 if (images->autostart)
315 do_reset (cmdtp, flag, argc, argv);
Kumar Gala274cea22008-02-27 21:51:46 -0600316 return ;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100317}
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100318
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100319static ulong get_sp (void)
320{
321 ulong sp;
322
323 asm( "mr %0,1": "=r"(sp) : );
324 return sp;
325}
326
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100327static void set_clocks_in_mhz (bd_t *kbd)
328{
329 char *s;
330
331 if ((s = getenv ("clocks_in_mhz")) != NULL) {
332 /* convert all clock information to MHz */
333 kbd->bi_intfreq /= 1000000L;
334 kbd->bi_busfreq /= 1000000L;
335#if defined(CONFIG_MPC8220)
336 kbd->bi_inpfreq /= 1000000L;
337 kbd->bi_pcifreq /= 1000000L;
338 kbd->bi_pevfreq /= 1000000L;
339 kbd->bi_flbfreq /= 1000000L;
340 kbd->bi_vcofreq /= 1000000L;
341#endif
342#if defined(CONFIG_CPM2)
343 kbd->bi_cpmfreq /= 1000000L;
344 kbd->bi_brgfreq /= 1000000L;
345 kbd->bi_sccfreq /= 1000000L;
Wolfgang Denk438a4c12008-03-26 11:48:46 +0100346 kbd->bi_vco /= 1000000L;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100347#endif
348#if defined(CONFIG_MPC5xxx)
349 kbd->bi_ipbfreq /= 1000000L;
350 kbd->bi_pcifreq /= 1000000L;
351#endif /* CONFIG_MPC5xxx */
352 }
353}
354
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100355#if defined(CONFIG_OF_LIBFDT)
356static void fdt_error (const char *msg)
357{
358 puts ("ERROR: ");
359 puts (msg);
360 puts (" - must RESET the board to recover.\n");
361}
Marian Balakowicz7b325452008-01-31 13:58:20 +0100362
Marian Balakowiczff0734c2008-02-27 11:02:26 +0100363static image_header_t *image_get_fdt (ulong fdt_addr)
364{
365 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
366
367 image_print_contents (fdt_hdr);
368
369 puts (" Verifying Checksum ... ");
370 if (!image_check_hcrc (fdt_hdr)) {
371 fdt_error ("fdt header checksum invalid");
372 return NULL;
373 }
374
375 if (!image_check_dcrc (fdt_hdr)) {
376 fdt_error ("fdt checksum invalid");
377 return NULL;
378 }
379 puts ("OK\n");
380
381 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
382 fdt_error ("uImage is not a fdt");
383 return NULL;
384 }
385 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
386 fdt_error ("uImage is compressed");
387 return NULL;
388 }
389 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
390 fdt_error ("uImage data is not a fdt");
391 return NULL;
392 }
393 return fdt_hdr;
394}
395
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100396/**
397 * fit_check_fdt - verify FIT format FDT subimage
398 * @fit_hdr: pointer to the FIT header
399 * fdt_noffset: FDT subimage node offset within FIT image
400 * @verify: data CRC verification flag
401 *
402 * fit_check_fdt() verifies integrity of the FDT subimage and from
403 * specified FIT image.
404 *
405 * returns:
406 * 1, on success
407 * 0, on failure
408 */
409#if defined(CONFIG_FIT)
410static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
411{
412 fit_image_print (fit, fdt_noffset, " ");
413
414 if (verify) {
415 puts (" Verifying Hash Integrity ... ");
416 if (!fit_image_check_hashes (fit, fdt_noffset)) {
417 fdt_error ("Bad Data Hash");
418 return 0;
419 }
420 puts ("OK\n");
421 }
422
423 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
424 fdt_error ("Not a FDT image");
425 return 0;
426 }
427
428 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
429 fdt_error ("FDT image is compressed");
430 return 0;
431 }
432
433 return 1;
434}
435#endif /* CONFIG_FIT */
436
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100437static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100438 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Marian Balakowicz7b325452008-01-31 13:58:20 +0100439{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100440 ulong fdt_addr;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100441 image_header_t *fdt_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100442 char *fdt_blob = NULL;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100443 ulong image_start, image_end;
444 ulong load_start, load_end;
445#if defined(CONFIG_FIT)
Wolfgang Denk438a4c12008-03-26 11:48:46 +0100446 void *fit_hdr;
447 const char *fit_uname_config = NULL;
448 const char *fit_uname_fdt = NULL;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100449 ulong default_addr;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100450 int cfg_noffset;
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100451 int fdt_noffset;
452 const void *data;
453 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100454#endif
Marian Balakowicz7b325452008-01-31 13:58:20 +0100455
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100456 *of_flat_tree = NULL;
457 *of_size = 0;
458
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100459 if (argc > 3 || genimg_has_config (images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100460#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100461 if (argc > 3) {
462 /*
463 * If the FDT blob comes from the FIT image and the
464 * FIT image address is omitted in the command line
465 * argument, try to use ramdisk or os FIT image
466 * address or default load address.
467 */
468 if (images->fit_uname_rd)
469 default_addr = (ulong)images->fit_hdr_rd;
470 else if (images->fit_uname_os)
471 default_addr = (ulong)images->fit_hdr_os;
472 else
473 default_addr = load_addr;
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100474
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100475 if (fit_parse_conf (argv[3], default_addr,
476 &fdt_addr, &fit_uname_config)) {
477 debug ("* fdt: config '%s' from image at 0x%08lx\n",
478 fit_uname_config, fdt_addr);
479 } else if (fit_parse_subimage (argv[3], default_addr,
480 &fdt_addr, &fit_uname_fdt)) {
481 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
482 fit_uname_fdt, fdt_addr);
483 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100484#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100485 {
486 fdt_addr = simple_strtoul(argv[3], NULL, 16);
487 debug ("* fdt: cmdline image address = 0x%08lx\n",
488 fdt_addr);
489 }
490#if defined(CONFIG_FIT)
491 } else {
492 /* use FIT configuration provided in first bootm
493 * command argument
494 */
495 fdt_addr = (ulong)images->fit_hdr_os;
496 fit_uname_config = images->fit_uname_cfg;
497 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
498 fit_uname_config, fdt_addr);
499
500 /*
501 * Check whether configuration has FDT blob defined,
502 * if not quit silently.
503 */
504 fit_hdr = (void *)fdt_addr;
505 cfg_noffset = fit_conf_get_node (fit_hdr,
506 fit_uname_config);
507 if (cfg_noffset < 0) {
508 debug ("* fdt: no such config\n");
509 return 0;
510 }
511
512 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
513 cfg_noffset);
514 if (fdt_noffset < 0) {
515 debug ("* fdt: no fdt in config\n");
516 return 0;
517 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100518 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100519#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100520
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100521 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100522 fdt_addr);
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100523
524 /* copy from dataflash if needed */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100525 fdt_addr = genimg_get_image (fdt_addr);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100526
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100527 /*
528 * Check if there is an FDT image at the
529 * address provided in the second bootm argument
530 * check image type, for FIT images get a FIT node.
531 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100532 switch (genimg_get_format ((void *)fdt_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100533 case IMAGE_FORMAT_LEGACY:
Marian Balakowiczff0734c2008-02-27 11:02:26 +0100534 /* verify fdt_addr points to a valid image header */
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100535 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
Marian Balakowiczff0734c2008-02-27 11:02:26 +0100536 fdt_addr);
537 fdt_hdr = image_get_fdt (fdt_addr);
538 if (!fdt_hdr)
Kumar Gala274cea22008-02-27 21:51:46 -0600539 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100540
Marian Balakowiczff0734c2008-02-27 11:02:26 +0100541 /*
542 * move image data to the load address,
543 * make sure we don't overwrite initial image
544 */
Marian Balakowicz7b325452008-01-31 13:58:20 +0100545 image_start = (ulong)fdt_hdr;
546 image_end = image_get_image_end (fdt_hdr);
547
548 load_start = image_get_load (fdt_hdr);
549 load_end = load_start + image_get_data_size (fdt_hdr);
550
551 if ((load_start < image_end) && (load_end > image_start)) {
552 fdt_error ("fdt overwritten");
Kumar Gala274cea22008-02-27 21:51:46 -0600553 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100554 }
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100555
556 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
557 image_get_data (fdt_hdr), load_start);
558
559 memmove ((void *)load_start,
Marian Balakowiczff0734c2008-02-27 11:02:26 +0100560 (void *)image_get_data (fdt_hdr),
561 image_get_data_size (fdt_hdr));
Marian Balakowicz7b325452008-01-31 13:58:20 +0100562
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100563 fdt_blob = (char *)load_start;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100564 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100565 case IMAGE_FORMAT_FIT:
Marian Balakowicz4efbe9d2008-02-27 11:02:26 +0100566 /*
567 * This case will catch both: new uImage format
568 * (libfdt based) and raw FDT blob (also libfdt
569 * based).
570 */
571#if defined(CONFIG_FIT)
572 /* check FDT blob vs FIT blob */
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100573 if (fit_check_format ((const void *)fdt_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100574 /*
575 * FIT image
576 */
577 fit_hdr = (void *)fdt_addr;
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100578 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
579 fdt_addr);
580
581 if (!fit_uname_fdt) {
582 /*
583 * no FDT blob image node unit name,
584 * try to get config node first. If
585 * config unit node name is NULL
586 * fit_conf_get_node() will try to
587 * find default config node
588 */
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100589 cfg_noffset = fit_conf_get_node (fit_hdr,
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100590 fit_uname_config);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100591
592 if (cfg_noffset < 0) {
593 fdt_error ("Could not find configuration node\n");
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100594 goto error;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100595 }
596
597 fit_uname_config = fdt_get_name (fit_hdr,
598 cfg_noffset, NULL);
599 printf (" Using '%s' configuration\n",
600 fit_uname_config);
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100601
602 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100603 cfg_noffset);
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100604 fit_uname_fdt = fit_get_name (fit_hdr,
605 fdt_noffset, NULL);
606 } else {
607 /* get FDT component image node offset */
608 fdt_noffset = fit_image_get_node (fit_hdr,
609 fit_uname_fdt);
610 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100611 if (fdt_noffset < 0) {
612 fdt_error ("Could not find subimage node\n");
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100613 goto error;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100614 }
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100615
616 printf (" Trying '%s' FDT blob subimage\n",
617 fit_uname_fdt);
618
619 if (!fit_check_fdt (fit_hdr, fdt_noffset,
620 images->verify))
621 goto error;
622
623 /* get ramdisk image data address and length */
624 if (fit_image_get_data (fit_hdr, fdt_noffset,
625 &data, &size)) {
626 fdt_error ("Could not find FDT subimage data");
627 goto error;
628 }
629
630 /* verift that image data is a proper FDT blob */
631 if (fdt_check_header ((char *)data) != 0) {
632 fdt_error ("Subimage data is not a FTD");
633 goto error;
634 }
635
636 /*
637 * move image data to the load address,
638 * make sure we don't overwrite initial image
639 */
640 image_start = (ulong)fit_hdr;
641 image_end = fit_get_end (fit_hdr);
642
643 if (fit_image_get_load (fit_hdr, fdt_noffset,
644 &load_start) == 0) {
645 load_end = load_start + size;
646
647 if ((load_start < image_end) &&
648 (load_end > image_start)) {
649 fdt_error ("FDT overwritten");
650 goto error;
651 }
652
653 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
654 (ulong)data, load_start);
655
656 memmove ((void *)load_start,
657 (void *)data, size);
658
659 fdt_blob = (char *)load_start;
660 } else {
661 fdt_blob = (char *)data;
662 }
663
664 images->fit_hdr_fdt = fit_hdr;
665 images->fit_uname_fdt = fit_uname_fdt;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100666 images->fit_noffset_fdt = fdt_noffset;
Marian Balakowiczbc8ed482008-03-12 10:32:53 +0100667 break;
Marian Balakowicz4efbe9d2008-02-27 11:02:26 +0100668 } else
669#endif
670 {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100671 /*
672 * FDT blob
673 */
Marian Balakowiczde2b3212008-04-11 11:07:43 +0200674 fdt_blob = (char *)fdt_addr;
Marian Balakowicz4efbe9d2008-02-27 11:02:26 +0100675 debug ("* fdt: raw FDT blob\n");
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700676 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100677 }
678 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100679 default:
680 fdt_error ("Did not find a cmdline Flattened Device Tree");
Kumar Gala274cea22008-02-27 21:51:46 -0600681 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100682 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100683
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700684 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100685
686 } else if (images->legacy_hdr_valid &&
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200687 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100688
Marian Balakowicz7b325452008-01-31 13:58:20 +0100689 ulong fdt_data, fdt_len;
690
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100691 /*
692 * Now check if we have a legacy multi-component image,
693 * get second entry data start address and len.
694 */
Marian Balakowicz7b325452008-01-31 13:58:20 +0100695 printf ("## Flattened Device Tree from multi "
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100696 "component Image at %08lX\n",
697 (ulong)images->legacy_hdr_os);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100698
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100699 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100700 if (fdt_len) {
701
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100702 fdt_blob = (char *)fdt_data;
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700703 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100704
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100705 if (fdt_check_header (fdt_blob) != 0) {
Marian Balakowicz7b325452008-01-31 13:58:20 +0100706 fdt_error ("image is not a fdt");
Kumar Gala274cea22008-02-27 21:51:46 -0600707 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100708 }
709
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100710 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
Marian Balakowicz7b325452008-01-31 13:58:20 +0100711 fdt_error ("fdt size != image size");
Kumar Gala274cea22008-02-27 21:51:46 -0600712 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100713 }
714 } else {
Nick Spence91a61672008-05-08 22:32:22 -0700715 debug ("## No Flattened Device Tree\n");
716 return 0;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100717 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100718 } else {
719 debug ("## No Flattened Device Tree\n");
Kumar Gala274cea22008-02-27 21:51:46 -0600720 return 0;
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100721 }
722
723 *of_flat_tree = fdt_blob;
724 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
725 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
726 *of_flat_tree, *of_size);
Kumar Gala274cea22008-02-27 21:51:46 -0600727
728 return 0;
729
730error:
731 do_reset (cmdtp, flag, argc, argv);
732 return 1;
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100733}
734
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100735static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100736 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
737 char **of_flat_tree, ulong *of_size)
738{
739 char *fdt_blob = *of_flat_tree;
740 ulong relocate = 0;
Kumar Galae822d7f2008-02-27 21:51:49 -0600741 ulong of_len = 0;
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100742
743 /* nothing to do */
744 if (*of_size == 0)
Kumar Galae822d7f2008-02-27 21:51:49 -0600745 return 0;
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100746
747 if (fdt_check_header (fdt_blob) != 0) {
748 fdt_error ("image is not a fdt");
Kumar Gala274cea22008-02-27 21:51:46 -0600749 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100750 }
751
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100752#ifndef CFG_NO_FLASH
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100753 /* move the blob if it is in flash (set relocate) */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100754 if (addr2info ((ulong)fdt_blob) != NULL)
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100755 relocate = 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100756#endif
757
Marian Balakowicz7b325452008-01-31 13:58:20 +0100758 /*
Andy Fleming41c5eaa2008-06-16 13:58:56 -0500759 * The blob needs to be inside the boot mapping.
Marian Balakowicz7b325452008-01-31 13:58:20 +0100760 */
Andy Fleming41c5eaa2008-06-16 13:58:56 -0500761 if (fdt_blob < (char *)bootmap_base)
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100762 relocate = 1;
Kumar Galae822d7f2008-02-27 21:51:49 -0600763
Andy Fleming41c5eaa2008-06-16 13:58:56 -0500764 if ((fdt_blob + *of_size + CFG_FDT_PAD) >=
765 ((char *)CFG_BOOTMAPSZ + bootmap_base))
766 relocate = 1;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100767
768 /* move flattend device tree if needed */
Marian Balakowiczd2ced9e2008-02-04 08:28:17 +0100769 if (relocate) {
Marian Balakowicz7b325452008-01-31 13:58:20 +0100770 int err;
Andy Fleming41c5eaa2008-06-16 13:58:56 -0500771 ulong of_start = 0;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100772
773 /* position on a 4K boundary before the alloc_current */
Andy Fleming41c5eaa2008-06-16 13:58:56 -0500774 /* Pad the FDT by a specified amount */
775 of_len = *of_size + CFG_FDT_PAD;
Becky Bruce391fd932008-06-09 20:37:18 -0500776 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
Andy Fleming41c5eaa2008-06-16 13:58:56 -0500777 (CFG_BOOTMAPSZ + bootmap_base));
Kumar Galae822d7f2008-02-27 21:51:49 -0600778
779 if (of_start == 0) {
780 puts("device tree - allocation error\n");
781 goto error;
782 }
Marian Balakowicz7b325452008-01-31 13:58:20 +0100783
784 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
Andy Fleming41c5eaa2008-06-16 13:58:56 -0500785 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
Marian Balakowicz7b325452008-01-31 13:58:20 +0100786 of_len, of_len);
787
788 printf (" Loading Device Tree to %08lx, end %08lx ... ",
789 of_start, of_start + of_len - 1);
790
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100791 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
Marian Balakowicz7b325452008-01-31 13:58:20 +0100792 if (err != 0) {
793 fdt_error ("fdt move failed");
Kumar Gala274cea22008-02-27 21:51:46 -0600794 goto error;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100795 }
796 puts ("OK\n");
797
798 *of_flat_tree = (char *)of_start;
Andy Fleming41c5eaa2008-06-16 13:58:56 -0500799 *of_size = of_len;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100800 } else {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100801 *of_flat_tree = fdt_blob;
Andy Fleming41c5eaa2008-06-16 13:58:56 -0500802 of_len = (CFG_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
803 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
804 fdt_set_totalsize(*of_flat_tree, of_len);
805
806 *of_size = of_len;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100807 }
808
Kumar Galae822d7f2008-02-27 21:51:49 -0600809 return 0;
Kumar Gala274cea22008-02-27 21:51:46 -0600810
811error:
Kumar Gala274cea22008-02-27 21:51:46 -0600812 return 1;
Marian Balakowicz7b325452008-01-31 13:58:20 +0100813}
Marian Balakowiczd3c5eb62008-01-31 13:20:08 +0100814#endif