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