]> git.ipfire.org Git - people/ms/u-boot.git/blob - lib_ppc/bootm.c
[new uImage] Use lmb for bootm allocations
[people/ms/u-boot.git] / lib_ppc / bootm.c
1 /*
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
26 #define DEBUG
27
28 #include <common.h>
29 #include <watchdog.h>
30 #include <command.h>
31 #include <image.h>
32 #include <malloc.h>
33 #include <zlib.h>
34 #include <bzlib.h>
35 #include <environment.h>
36 #include <asm/byteorder.h>
37
38 #if defined(CONFIG_OF_LIBFDT)
39 #include <fdt.h>
40 #include <libfdt.h>
41 #include <fdt_support.h>
42
43 static void fdt_error (const char *msg);
44 static int get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
45 bootm_headers_t *images, char **of_flat_tree, ulong *of_size);
46 static int fdt_relocate (struct lmb *lmb, ulong bootmap_base,
47 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
48 char **of_flat_tree, ulong *of_size);
49 #endif
50
51 #ifdef CFG_INIT_RAM_LOCK
52 #include <asm/cache.h>
53 #endif
54
55 DECLARE_GLOBAL_DATA_PTR;
56
57 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
58 extern ulong get_effective_memsize(void);
59 static ulong get_sp (void);
60 static void set_clocks_in_mhz (bd_t *kbd);
61
62 void __attribute__((noinline))
63 do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
64 bootm_headers_t *images)
65 {
66 ulong sp;
67
68 ulong initrd_start, initrd_end;
69 ulong rd_data_start, rd_data_end, rd_len;
70
71 ulong cmd_start, cmd_end, bootmap_base;
72 bd_t *kbd;
73 ulong ep = 0;
74 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
75 int ret;
76 ulong of_size = 0;
77 struct lmb *lmb = images->lmb;
78
79 #if defined(CONFIG_OF_LIBFDT)
80 char *of_flat_tree = NULL;
81 #endif
82
83 bootmap_base = 0;
84
85 /*
86 * Booting a (Linux) kernel image
87 *
88 * Allocate space for command line and board info - the
89 * address should be as high as possible within the reach of
90 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
91 * memory, which means far enough below the current stack
92 * pointer.
93 */
94 sp = get_sp();
95 debug ("## Current stack ends at 0x%08lx ", sp);
96
97 /* adjust sp by 1K to be safe */
98 sp -= 1024;
99 lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
100
101 #if defined(CONFIG_OF_LIBFDT)
102 /* find flattened device tree */
103 ret = get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
104
105 if (ret)
106 goto error;
107 #endif
108
109 if (!of_size) {
110 /* allocate space and init command line */
111 ret = get_boot_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
112 if (ret) {
113 puts("ERROR with allocation of cmdline\n");
114 goto error;
115 }
116
117 /* allocate space for kernel copy of board info */
118 ret = get_boot_kbd (lmb, &kbd, bootmap_base);
119 if (ret) {
120 puts("ERROR with allocation of kernel bd\n");
121 goto error;
122 }
123 set_clocks_in_mhz(kbd);
124 }
125
126 /* find kernel entry point */
127 if (images->legacy_hdr_valid) {
128 ep = image_get_ep (images->legacy_hdr_os);
129 #if defined(CONFIG_FIT)
130 } else if (images->fit_uname_os) {
131 fit_unsupported_reset ("PPC linux bootm");
132 goto error;
133 #endif
134 } else {
135 puts ("Could not find kernel entry point!\n");
136 goto error;
137 }
138 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
139
140 /* find ramdisk */
141 ret = get_ramdisk (cmdtp, flag, argc, argv, images,
142 IH_ARCH_PPC, &rd_data_start, &rd_data_end);
143
144 if (ret)
145 goto error;
146
147 rd_len = rd_data_end - rd_data_start;
148
149 #if defined(CONFIG_OF_LIBFDT)
150 ret = fdt_relocate (lmb, bootmap_base,
151 cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
152
153 /*
154 * Add the chosen node if it doesn't exist, add the env and bd_t
155 * if the user wants it (the logic is in the subroutines).
156 */
157 if (of_size) {
158 /* pass in dummy initrd info, we'll fix up later */
159 if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
160 fdt_error ("/chosen node create failed");
161 goto error;
162 }
163 #ifdef CONFIG_OF_HAS_UBOOT_ENV
164 if (fdt_env(of_flat_tree) < 0) {
165 fdt_error ("/u-boot-env node create failed");
166 goto error;
167 }
168 #endif
169 #ifdef CONFIG_OF_HAS_BD_T
170 if (fdt_bd_t(of_flat_tree) < 0) {
171 fdt_error ("/bd_t node create failed");
172 goto error;
173 }
174 #endif
175 #ifdef CONFIG_OF_BOARD_SETUP
176 /* Call the board-specific fixup routine */
177 ft_board_setup(of_flat_tree, gd->bd);
178 #endif
179 }
180 #endif /* CONFIG_OF_LIBFDT */
181
182 ret = ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
183 if (ret)
184 goto error;
185
186 #if defined(CONFIG_OF_LIBFDT)
187 /* fixup the initrd now that we know where it should be */
188 if ((of_flat_tree) && (initrd_start && initrd_end)) {
189 uint64_t addr, size;
190 int total = fdt_num_mem_rsv(of_flat_tree);
191 int j;
192
193 /* Look for the dummy entry and delete it */
194 for (j = 0; j < total; j++) {
195 fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
196 if (addr == rd_data_start) {
197 fdt_del_mem_rsv(of_flat_tree, j);
198 break;
199 }
200 }
201
202 ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
203 initrd_end - initrd_start + 1);
204 if (ret < 0) {
205 printf("fdt_chosen: %s\n", fdt_strerror(ret));
206 goto error;
207 }
208
209 do_fixup_by_path_u32(of_flat_tree, "/chosen",
210 "linux,initrd-start", initrd_start, 0);
211 do_fixup_by_path_u32(of_flat_tree, "/chosen",
212 "linux,initrd-end", initrd_end, 0);
213 }
214 #endif
215 debug ("## Transferring control to Linux (at address %08lx) ...\n",
216 (ulong)kernel);
217
218 show_boot_progress (15);
219
220 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
221 unlock_ram_in_cache();
222 #endif
223
224 #if defined(CONFIG_OF_LIBFDT)
225 if (of_flat_tree) { /* device tree; boot new style */
226 /*
227 * Linux Kernel Parameters (passing device tree):
228 * r3: pointer to the fdt, followed by the board info data
229 * r4: physical pointer to the kernel itself
230 * r5: NULL
231 * r6: NULL
232 * r7: NULL
233 */
234 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
235 /* does not return */
236 }
237 #endif
238 /*
239 * Linux Kernel Parameters (passing board info data):
240 * r3: ptr to board info data
241 * r4: initrd_start or 0 if no initrd
242 * r5: initrd_end - unused if r4 is 0
243 * r6: Start of command line string
244 * r7: End of command line string
245 */
246 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
247 /* does not return */
248 return ;
249
250 error:
251 do_reset (cmdtp, flag, argc, argv);
252 return ;
253 }
254
255 static ulong get_sp (void)
256 {
257 ulong sp;
258
259 asm( "mr %0,1": "=r"(sp) : );
260 return sp;
261 }
262
263 static void set_clocks_in_mhz (bd_t *kbd)
264 {
265 char *s;
266
267 if ((s = getenv ("clocks_in_mhz")) != NULL) {
268 /* convert all clock information to MHz */
269 kbd->bi_intfreq /= 1000000L;
270 kbd->bi_busfreq /= 1000000L;
271 #if defined(CONFIG_MPC8220)
272 kbd->bi_inpfreq /= 1000000L;
273 kbd->bi_pcifreq /= 1000000L;
274 kbd->bi_pevfreq /= 1000000L;
275 kbd->bi_flbfreq /= 1000000L;
276 kbd->bi_vcofreq /= 1000000L;
277 #endif
278 #if defined(CONFIG_CPM2)
279 kbd->bi_cpmfreq /= 1000000L;
280 kbd->bi_brgfreq /= 1000000L;
281 kbd->bi_sccfreq /= 1000000L;
282 kbd->bi_vco /= 1000000L;
283 #endif
284 #if defined(CONFIG_MPC5xxx)
285 kbd->bi_ipbfreq /= 1000000L;
286 kbd->bi_pcifreq /= 1000000L;
287 #endif /* CONFIG_MPC5xxx */
288 }
289 }
290
291 #if defined(CONFIG_OF_LIBFDT)
292 static void fdt_error (const char *msg)
293 {
294 puts ("ERROR: ");
295 puts (msg);
296 puts (" - must RESET the board to recover.\n");
297 }
298
299 static image_header_t *image_get_fdt (ulong fdt_addr)
300 {
301 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
302
303 image_print_contents (fdt_hdr);
304
305 puts (" Verifying Checksum ... ");
306 if (!image_check_hcrc (fdt_hdr)) {
307 fdt_error ("fdt header checksum invalid");
308 return NULL;
309 }
310
311 if (!image_check_dcrc (fdt_hdr)) {
312 fdt_error ("fdt checksum invalid");
313 return NULL;
314 }
315 puts ("OK\n");
316
317 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
318 fdt_error ("uImage is not a fdt");
319 return NULL;
320 }
321 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
322 fdt_error ("uImage is compressed");
323 return NULL;
324 }
325 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
326 fdt_error ("uImage data is not a fdt");
327 return NULL;
328 }
329 return fdt_hdr;
330 }
331
332 static int get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
333 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
334 {
335 ulong fdt_addr;
336 image_header_t *fdt_hdr;
337 char *fdt_blob = NULL;
338 ulong image_start, image_end;
339 ulong load_start, load_end;
340 #if defined(CONFIG_FIT)
341 void *fit_hdr;
342 const char *fit_uname_config = NULL;
343 const char *fit_uname_fdt = NULL;
344 ulong default_addr;
345 #endif
346
347 if (argc > 3) {
348 #if defined(CONFIG_FIT)
349 /*
350 * If the FDT blob comes from the FIT image and the FIT image
351 * address is omitted in the command line argument, try to use
352 * ramdisk or os FIT image address or default load address.
353 */
354 if (images->fit_uname_rd)
355 default_addr = (ulong)images->fit_hdr_rd;
356 else if (images->fit_uname_os)
357 default_addr = (ulong)images->fit_hdr_os;
358 else
359 default_addr = load_addr;
360
361 if (fit_parse_conf (argv[3], default_addr,
362 &fdt_addr, &fit_uname_config)) {
363 debug ("* fdt: config '%s' from image at 0x%08lx\n",
364 fit_uname_config, fdt_addr);
365 } else if (fit_parse_subimage (argv[3], default_addr,
366 &fdt_addr, &fit_uname_fdt)) {
367 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
368 fit_uname_fdt, fdt_addr);
369 } else
370 #endif
371 {
372 fdt_addr = simple_strtoul(argv[3], NULL, 16);
373 debug ("* fdt: cmdline image address = 0x%08lx\n",
374 fdt_addr);
375 }
376
377 debug ("## Checking for 'FDT'/'FDT image' at %08lx\n",
378 fdt_addr);
379
380 /* copy from dataflash if needed */
381 fdt_addr = gen_get_image (fdt_addr);
382
383 /*
384 * Check if there is an FDT image at the
385 * address provided in the second bootm argument
386 * check image type, for FIT images get a FIT node.
387 */
388 switch (gen_image_get_format ((void *)fdt_addr)) {
389 case IMAGE_FORMAT_LEGACY:
390 debug ("* fdt: legacy format image\n");
391
392 /* verify fdt_addr points to a valid image header */
393 printf ("## Flattened Device Tree Legacy Image at %08lx\n",
394 fdt_addr);
395 fdt_hdr = image_get_fdt (fdt_addr);
396 if (!fdt_hdr)
397 goto error;
398
399 /*
400 * move image data to the load address,
401 * make sure we don't overwrite initial image
402 */
403 image_start = (ulong)fdt_hdr;
404 image_end = image_get_image_end (fdt_hdr);
405
406 load_start = image_get_load (fdt_hdr);
407 load_end = load_start + image_get_data_size (fdt_hdr);
408
409 if ((load_start < image_end) && (load_end > image_start)) {
410 fdt_error ("fdt overwritten");
411 goto error;
412 }
413 memmove ((void *)image_get_load (fdt_hdr),
414 (void *)image_get_data (fdt_hdr),
415 image_get_data_size (fdt_hdr));
416
417 fdt_blob = (char *)image_get_load (fdt_hdr);
418 break;
419 case IMAGE_FORMAT_FIT:
420 /*
421 * This case will catch both: new uImage format
422 * (libfdt based) and raw FDT blob (also libfdt
423 * based).
424 */
425 #if defined(CONFIG_FIT)
426 /* check FDT blob vs FIT blob */
427 if (0) { /* FIXME: call FIT format verification */
428 /*
429 * FIT image
430 */
431 fit_hdr = (void *)fdt_addr;
432 debug ("* fdt: FIT format image\n");
433 fit_unsupported_reset ("PPC fdt");
434 goto error;
435 } else
436 #endif
437 {
438 /*
439 * FDT blob
440 */
441 debug ("* fdt: raw FDT blob\n");
442 printf ("## Flattened Device Tree blob at %08lx\n", fdt_blob);
443 fdt_blob = (char *)fdt_addr;
444 }
445 break;
446 default:
447 fdt_error ("Did not find a cmdline Flattened Device Tree");
448 goto error;
449 }
450
451 printf (" Booting using the fdt blob at 0x%x\n", fdt_blob);
452
453 } else if (images->legacy_hdr_valid &&
454 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
455
456 ulong fdt_data, fdt_len;
457
458 /*
459 * Now check if we have a legacy multi-component image,
460 * get second entry data start address and len.
461 */
462 printf ("## Flattened Device Tree from multi "
463 "component Image at %08lX\n",
464 (ulong)images->legacy_hdr_os);
465
466 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
467 if (fdt_len) {
468
469 fdt_blob = (char *)fdt_data;
470 printf (" Booting using the fdt at 0x%x\n", fdt_blob);
471
472 if (fdt_check_header (fdt_blob) != 0) {
473 fdt_error ("image is not a fdt");
474 goto error;
475 }
476
477 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
478 fdt_error ("fdt size != image size");
479 goto error;
480 }
481 } else {
482 fdt_error ("Did not find a Flattened Device Tree "
483 "in a legacy multi-component image");
484 goto error;
485 }
486 } else {
487 debug ("## No Flattened Device Tree\n");
488 *of_flat_tree = NULL;
489 *of_size = 0;
490 return 0;
491 }
492
493 *of_flat_tree = fdt_blob;
494 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
495 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
496 *of_flat_tree, *of_size);
497
498 return 0;
499
500 error:
501 do_reset (cmdtp, flag, argc, argv);
502 return 1;
503 }
504
505 static int fdt_relocate (struct lmb *lmb, ulong bootmap_base,
506 cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
507 char **of_flat_tree, ulong *of_size)
508 {
509 char *fdt_blob = *of_flat_tree;
510 ulong relocate = 0;
511 ulong of_len = 0;
512
513 /* nothing to do */
514 if (*of_size == 0)
515 return 0;
516
517 if (fdt_check_header (fdt_blob) != 0) {
518 fdt_error ("image is not a fdt");
519 goto error;
520 }
521
522 #ifndef CFG_NO_FLASH
523 /* move the blob if it is in flash (set relocate) */
524 if (addr2info ((ulong)fdt_blob) != NULL)
525 relocate = 1;
526 #endif
527
528 /*
529 * The blob must be within CFG_BOOTMAPSZ,
530 * so we flag it to be copied if it is not.
531 */
532 if (fdt_blob >= (char *)CFG_BOOTMAPSZ)
533 relocate = 1;
534
535 of_len = be32_to_cpu (fdt_totalsize (fdt));
536
537 /* move flattend device tree if needed */
538 if (relocate) {
539 int err;
540 ulong of_start;
541
542 /* position on a 4K boundary before the alloc_current */
543 of_start = lmb_alloc_base(lmb, of_len, 0x1000,
544 (CFG_BOOTMAPSZ + bootmap_base));
545
546 if (of_start == 0) {
547 puts("device tree - allocation error\n");
548 goto error;
549 }
550
551 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
552 (ulong)fdt_blob, (ulong)fdt_blob + of_len - 1,
553 of_len, of_len);
554
555 printf (" Loading Device Tree to %08lx, end %08lx ... ",
556 of_start, of_start + of_len - 1);
557
558 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
559 if (err != 0) {
560 fdt_error ("fdt move failed");
561 goto error;
562 }
563 puts ("OK\n");
564
565 *of_flat_tree = (char *)of_start;
566 } else {
567 *of_flat_tree = fdt_blob;
568 lmb_reserve(lmb, (ulong)fdt, of_len);
569 }
570
571 return 0;
572
573 error:
574 return 1;
575 }
576 #endif