]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - lib_ppc/bootm.c
[new uImage] Rename and move print_image_hdr() routine
[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                 image_header_t *hdr, 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                 image_header_t *hdr,
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         void    (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
73
74 #if defined(CONFIG_OF_LIBFDT)
75         char    *of_flat_tree;
76 #endif
77
78         /*
79          * Booting a (Linux) kernel image
80          *
81          * Allocate space for command line and board info - the
82          * address should be as high as possible within the reach of
83          * the kernel (see CFG_BOOTMAPSZ settings), but in unused
84          * memory, which means far enough below the current stack
85          * pointer.
86          */
87         sp = get_sp();
88         debug ("## Current stack ends at 0x%08lx ", sp);
89
90         alloc_current = sp_limit = get_boot_sp_limit(sp);
91         debug ("=> set upper limit to 0x%08lx\n", sp_limit);
92
93         /* allocate space and init command line */
94         alloc_current = get_boot_cmdline (alloc_current, &cmd_start, &cmd_end);
95
96         /* allocate space for kernel copy of board info */
97         alloc_current = get_boot_kbd (alloc_current, &kbd);
98         set_clocks_in_mhz(kbd);
99
100         /* find kernel */
101         kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
102
103         /* find ramdisk */
104         get_ramdisk (cmdtp, flag, argc, argv, hdr, verify,
105                         IH_ARCH_PPC, &rd_data_start, &rd_data_end);
106
107         rd_len = rd_data_end - rd_data_start;
108
109         alloc_current = ramdisk_high (alloc_current, rd_data_start, rd_len,
110                         kbd, sp_limit, get_sp (),
111                         &initrd_start, &initrd_end);
112
113 #if defined(CONFIG_OF_LIBFDT)
114         /* find flattened device tree */
115         alloc_current = get_fdt (alloc_current,
116                         cmdtp, flag, argc, argv, hdr, &of_flat_tree);
117
118         /*
119          * Add the chosen node if it doesn't exist, add the env and bd_t
120          * if the user wants it (the logic is in the subroutines).
121          */
122         if (of_flat_tree) {
123                 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
124                         fdt_error ("/chosen node create failed");
125                         do_reset (cmdtp, flag, argc, argv);
126                 }
127 #ifdef CONFIG_OF_HAS_UBOOT_ENV
128                 if (fdt_env(of_flat_tree) < 0) {
129                         fdt_error ("/u-boot-env node create failed");
130                         do_reset (cmdtp, flag, argc, argv);
131                 }
132 #endif
133 #ifdef CONFIG_OF_HAS_BD_T
134                 if (fdt_bd_t(of_flat_tree) < 0) {
135                         fdt_error ("/bd_t node create failed");
136                         do_reset (cmdtp, flag, argc, argv);
137                 }
138 #endif
139 #ifdef CONFIG_OF_BOARD_SETUP
140                 /* Call the board-specific fixup routine */
141                 ft_board_setup(of_flat_tree, gd->bd);
142 #endif
143         }
144 #endif  /* CONFIG_OF_LIBFDT */
145
146         debug ("## Transferring control to Linux (at address %08lx) ...\n",
147                 (ulong)kernel);
148
149         show_boot_progress (15);
150
151 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
152         unlock_ram_in_cache();
153 #endif
154
155 #if defined(CONFIG_OF_LIBFDT)
156         if (of_flat_tree) {     /* device tree; boot new style */
157                 /*
158                  * Linux Kernel Parameters (passing device tree):
159                  *   r3: pointer to the fdt, followed by the board info data
160                  *   r4: physical pointer to the kernel itself
161                  *   r5: NULL
162                  *   r6: NULL
163                  *   r7: NULL
164                  */
165                 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
166                 /* does not return */
167         }
168 #endif
169         /*
170          * Linux Kernel Parameters (passing board info data):
171          *   r3: ptr to board info data
172          *   r4: initrd_start or 0 if no initrd
173          *   r5: initrd_end - unused if r4 is 0
174          *   r6: Start of command line string
175          *   r7: End   of command line string
176          */
177         (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
178         /* does not return */
179 }
180
181 static ulong get_sp (void)
182 {
183         ulong sp;
184
185         asm( "mr %0,1": "=r"(sp) : );
186         return sp;
187 }
188
189 static void set_clocks_in_mhz (bd_t *kbd)
190 {
191         char    *s;
192
193         if ((s = getenv ("clocks_in_mhz")) != NULL) {
194                 /* convert all clock information to MHz */
195                 kbd->bi_intfreq /= 1000000L;
196                 kbd->bi_busfreq /= 1000000L;
197 #if defined(CONFIG_MPC8220)
198                 kbd->bi_inpfreq /= 1000000L;
199                 kbd->bi_pcifreq /= 1000000L;
200                 kbd->bi_pevfreq /= 1000000L;
201                 kbd->bi_flbfreq /= 1000000L;
202                 kbd->bi_vcofreq /= 1000000L;
203 #endif
204 #if defined(CONFIG_CPM2)
205                 kbd->bi_cpmfreq /= 1000000L;
206                 kbd->bi_brgfreq /= 1000000L;
207                 kbd->bi_sccfreq /= 1000000L;
208                 kbd->bi_vco     /= 1000000L;
209 #endif
210 #if defined(CONFIG_MPC5xxx)
211                 kbd->bi_ipbfreq /= 1000000L;
212                 kbd->bi_pcifreq /= 1000000L;
213 #endif /* CONFIG_MPC5xxx */
214         }
215 }
216
217 #if defined(CONFIG_OF_LIBFDT)
218 static void fdt_error (const char *msg)
219 {
220         puts ("ERROR: ");
221         puts (msg);
222         puts (" - must RESET the board to recover.\n");
223 }
224
225 static ulong get_fdt (ulong alloc_current,
226                 cmd_tbl_t *cmdtp, int flag,
227                 int argc, char *argv[],
228                 image_header_t *hdr, char **of_flat_tree)
229 {
230         image_header_t  *fdt_hdr;
231         ulong           fdt_relocate = 0;
232         char            *fdt = NULL;
233         ulong           new_alloc_current;
234
235         if(argc > 3) {
236                 fdt = (char *)simple_strtoul (argv[3], NULL, 16);
237
238                 debug ("## Checking for 'FDT'/'FDT image' at %08lx\n", fdt);
239
240                 /* copy from dataflash if needed */
241                 fdt = (char *)gen_get_image ((ulong)fdt);
242                 fdt_hdr = (image_header_t *)fdt;
243
244                 if (fdt_check_header (fdt) == 0) {
245                         printf ("## Flattened Device Tree blob at %08lx\n", fdt);
246 #ifndef CFG_NO_FLASH
247                         if (addr2info ((ulong)fdt) != NULL)
248                                 fdt_relocate = 1;
249 #endif
250                 } else if (image_check_magic (fdt_hdr)) {
251                         ulong image_start, image_end;
252                         ulong load_start, load_end;
253
254                         printf ("## Flattened Device Tree Image at %08lx\n",
255                                         fdt_hdr);
256
257                         image_print_contents (fdt_hdr);
258
259                         image_start = (ulong)fdt_hdr;
260                         image_end = image_get_image_end (fdt_hdr);
261
262                         load_start = image_get_load (fdt_hdr);
263                         load_end = load_start + image_get_data_size (fdt_hdr);
264
265                         if ((load_start < image_end) && (load_end > image_start)) {
266                                 fdt_error ("fdt overwritten");
267                                 do_reset (cmdtp, flag, argc, argv);
268                         }
269
270                         puts ("   Verifying Checksum ... ");
271                         if (!image_check_hcrc (fdt_hdr)) {
272                                 fdt_error ("fdt header checksum invalid");
273                                 do_reset (cmdtp, flag, argc, argv);
274                         }
275
276                         if (!image_check_dcrc (fdt_hdr)) {
277                                 fdt_error ("fdt checksum invalid");
278                                 do_reset (cmdtp, flag, argc, argv);
279                         }
280                         puts ("OK\n");
281
282                         if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
283                                 fdt_error ("uImage is not a fdt");
284                                 do_reset (cmdtp, flag, argc, argv);
285                         }
286                         if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
287                                 fdt_error ("uImage is compressed");
288                                 do_reset (cmdtp, flag, argc, argv);
289                         }
290                         if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
291                                 fdt_error ("uImage data is not a fdt");
292                                 do_reset (cmdtp, flag, argc, argv);
293                         }
294
295                         memmove ((void *)image_get_load (fdt_hdr),
296                                 (void *)image_get_data (fdt_hdr),
297                                 image_get_data_size (fdt_hdr));
298
299                         fdt = (char *)image_get_load (fdt_hdr);
300                 } else {
301                         fdt_error ("Did not find a Flattened Device Tree");
302                         do_reset (cmdtp, flag, argc, argv);
303                 }
304                 printf ("   Booting using the fdt at 0x%x\n",
305                                 fdt);
306         } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
307                 ulong fdt_data, fdt_len;
308
309                 printf ("## Flattened Device Tree from multi "
310                         "component Image at %08lX\n", (ulong)hdr);
311
312                 image_multi_getimg (hdr, 2, &fdt_data, &fdt_len);
313                 if (fdt_len) {
314
315                         fdt = (char *)fdt_data;
316                         printf ("   Booting using the fdt at 0x%x\n", fdt);
317
318 #ifndef CFG_NO_FLASH
319                         /* move the blob if it is in flash (set of_relocte) */
320                         if (addr2info ((ulong)fdt) != NULL)
321                                 fdt_relocate = 1;
322 #endif
323
324                         if (fdt_check_header (fdt) != 0) {
325                                 fdt_error ("image is not a fdt");
326                                 do_reset (cmdtp, flag, argc, argv);
327                         }
328
329                         if (be32_to_cpu (fdt_totalsize (fdt)) != fdt_len) {
330                                 fdt_error ("fdt size != image size");
331                                 do_reset (cmdtp, flag, argc, argv);
332                         }
333                 } else {
334                         debug ("   Did not find a Flattened Device Tree");
335                 }
336         }
337
338 #ifdef CFG_BOOTMAPSZ
339         /*
340          * The blob must be within CFG_BOOTMAPSZ,
341          * so we flag it to be copied if it is not.
342          */
343         if (fdt >= (char *)CFG_BOOTMAPSZ)
344                 fdt_relocate = 1;
345 #endif
346
347         /* move flattend device tree if needed */
348         if (fdt_relocate) {
349                 int err;
350                 ulong of_start, of_len;
351
352                 of_len = be32_to_cpu (fdt_totalsize (fdt));
353
354                 /* position on a 4K boundary before the alloc_current */
355                 of_start  = alloc_current - of_len;
356                 of_start &= ~(4096 - 1);        /* align on page */
357
358                 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
359                         (ulong)fdt, (ulong)fdt + of_len - 1,
360                         of_len, of_len);
361
362                 printf ("   Loading Device Tree to %08lx, end %08lx ... ",
363                         of_start, of_start + of_len - 1);
364
365                 err = fdt_open_into (fdt, (void *)of_start, of_len);
366                 if (err != 0) {
367                         fdt_error ("fdt move failed");
368                         do_reset (cmdtp, flag, argc, argv);
369                 }
370                 puts ("OK\n");
371
372                 *of_flat_tree = (char *)of_start;
373                 new_alloc_current = of_start;
374         } else {
375                 *of_flat_tree = fdt;
376                 new_alloc_current = alloc_current;
377         }
378
379         return new_alloc_current;
380 }
381 #endif