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