]> git.sur5r.net Git - u-boot/blob - lib_ppc/bootm.c
[new uImage] Factor out common image_get_ramdisk() routine
[u-boot] / 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 #endif
45
46 #ifdef CONFIG_LOGBUFFER
47 #include <logbuff.h>
48 #endif
49
50 #ifdef CFG_INIT_RAM_LOCK
51 #include <asm/cache.h>
52 #endif
53
54 DECLARE_GLOBAL_DATA_PTR;
55
56 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
57
58 #if defined(CONFIG_CMD_BDI)
59 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
60 #endif
61
62 void  __attribute__((noinline))
63 do_bootm_linux(cmd_tbl_t *cmdtp, int flag,
64                 int     argc, char *argv[],
65                 image_header_t *hdr,
66                 int     verify)
67 {
68         ulong   initrd_high;
69         int     initrd_copy_to_ram = 1;
70         ulong   initrd_start, initrd_end;
71         ulong   rd_data_start, rd_data_end, rd_len;
72
73         ulong   cmd_start, cmd_end;
74         char    *cmdline;
75
76         ulong   sp;
77         char    *s;
78         bd_t    *kbd;
79         void    (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
80
81 #if defined(CONFIG_OF_LIBFDT)
82         image_header_t *fdt_hdr;
83         char    *of_flat_tree = NULL;
84         ulong   of_data = 0;
85 #endif
86
87         if ((s = getenv ("initrd_high")) != NULL) {
88                 /* a value of "no" or a similar string will act like 0,
89                  * turning the "load high" feature off. This is intentional.
90                  */
91                 initrd_high = simple_strtoul(s, NULL, 16);
92                 if (initrd_high == ~0)
93                         initrd_copy_to_ram = 0;
94         } else {        /* not set, no restrictions to load high */
95                 initrd_high = ~0;
96         }
97
98 #ifdef CONFIG_LOGBUFFER
99         kbd=gd->bd;
100         /* Prevent initrd from overwriting logbuffer */
101         if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
102                 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
103         debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
104 #endif
105
106         /*
107          * Booting a (Linux) kernel image
108          *
109          * Allocate space for command line and board info - the
110          * address should be as high as possible within the reach of
111          * the kernel (see CFG_BOOTMAPSZ settings), but in unused
112          * memory, which means far enough below the current stack
113          * pointer.
114          */
115
116         asm( "mr %0,1": "=r"(sp) : );
117
118         debug ("## Current stack ends at 0x%08lX ", sp);
119
120         sp -= 2048;             /* just to be sure */
121         if (sp > CFG_BOOTMAPSZ)
122                 sp = CFG_BOOTMAPSZ;
123         sp &= ~0xF;
124
125         debug ("=> set upper limit to 0x%08lX\n", sp);
126
127         cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
128         kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
129
130         if ((s = getenv("bootargs")) == NULL)
131                 s = "";
132
133         strcpy (cmdline, s);
134
135         cmd_start    = (ulong)&cmdline[0];
136         cmd_end      = cmd_start + strlen(cmdline);
137
138         *kbd = *(gd->bd);
139
140 #ifdef  DEBUG
141         printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
142
143 #if defined(CONFIG_CMD_BDI)
144         do_bdinfo (NULL, 0, 0, NULL);
145 #endif
146 #endif
147
148         if ((s = getenv ("clocks_in_mhz")) != NULL) {
149                 /* convert all clock information to MHz */
150                 kbd->bi_intfreq /= 1000000L;
151                 kbd->bi_busfreq /= 1000000L;
152 #if defined(CONFIG_MPC8220)
153         kbd->bi_inpfreq /= 1000000L;
154         kbd->bi_pcifreq /= 1000000L;
155         kbd->bi_pevfreq /= 1000000L;
156         kbd->bi_flbfreq /= 1000000L;
157         kbd->bi_vcofreq /= 1000000L;
158 #endif
159 #if defined(CONFIG_CPM2)
160                 kbd->bi_cpmfreq /= 1000000L;
161                 kbd->bi_brgfreq /= 1000000L;
162                 kbd->bi_sccfreq /= 1000000L;
163                 kbd->bi_vco     /= 1000000L;
164 #endif
165 #if defined(CONFIG_MPC5xxx)
166                 kbd->bi_ipbfreq /= 1000000L;
167                 kbd->bi_pcifreq /= 1000000L;
168 #endif /* CONFIG_MPC5xxx */
169         }
170
171         kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
172
173         get_ramdisk (cmdtp, flag, argc, argv, hdr, verify,
174                         IH_ARCH_PPC, &rd_data_start, &rd_data_end);
175         rd_len = rd_data_end - rd_data_start;
176
177 #if defined(CONFIG_OF_LIBFDT)
178         if(argc > 3) {
179                 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
180                 fdt_hdr = (image_header_t *)of_flat_tree;
181
182                 if (fdt_check_header (of_flat_tree) == 0) {
183 #ifndef CFG_NO_FLASH
184                         if (addr2info((ulong)of_flat_tree) != NULL)
185                                 of_data = (ulong)of_flat_tree;
186 #endif
187                 } else if (image_check_magic (fdt_hdr)) {
188                         ulong image_start, image_end;
189                         ulong load_start, load_end;
190
191                         printf ("## Flat Device Tree at %08lX\n", fdt_hdr);
192                         print_image_hdr (fdt_hdr);
193
194                         image_start = (ulong)fdt_hdr;
195                         image_end = image_get_image_end (fdt_hdr);
196
197                         load_start = image_get_load (fdt_hdr);
198                         load_end = load_start + image_get_data_size (fdt_hdr);
199
200                         if ((load_start < image_end) && (load_end > image_start)) {
201                                 fdt_error ("fdt overwritten");
202                                 do_reset (cmdtp, flag, argc, argv);
203                         }
204
205                         puts ("   Verifying Checksum ... ");
206                         if (!image_check_hcrc (fdt_hdr)) {
207                                 fdt_error ("fdt header checksum invalid");
208                                 do_reset (cmdtp, flag, argc, argv);
209                         }
210
211                         if (!image_check_dcrc (fdt_hdr)) {
212                                 fdt_error ("fdt checksum invalid");
213                                 do_reset (cmdtp, flag, argc, argv);
214                         }
215                         puts ("OK\n");
216
217                         if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
218                                 fdt_error ("uImage is not a fdt");
219                                 do_reset (cmdtp, flag, argc, argv);
220                         }
221                         if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
222                                 fdt_error ("uImage is compressed");
223                                 do_reset (cmdtp, flag, argc, argv);
224                         }
225                         if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
226                                 fdt_error ("uImage data is not a fdt");
227                                 do_reset (cmdtp, flag, argc, argv);
228                         }
229
230                         memmove ((void *)image_get_load (fdt_hdr),
231                                 (void *)(of_flat_tree + image_get_header_size ()),
232                                 image_get_data_size (fdt_hdr));
233
234                         of_flat_tree = (char *)image_get_load (fdt_hdr);
235                 } else {
236                         fdt_error ("Did not find a Flattened Device Tree");
237                         do_reset (cmdtp, flag, argc, argv);
238                 }
239                 printf ("   Booting using the fdt at 0x%x\n",
240                                 of_flat_tree);
241         } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
242                 ulong fdt_data, fdt_len;
243
244                 image_multi_getimg (hdr, 2, &fdt_data, &fdt_len);
245                 if (fdt_len) {
246
247                         of_flat_tree = (char *)fdt_data;
248
249 #ifndef CFG_NO_FLASH
250                         /* move the blob if it is in flash (set of_data to !null) */
251                         if (addr2info ((ulong)of_flat_tree) != NULL)
252                                 of_data = (ulong)of_flat_tree;
253 #endif
254
255                         if (fdt_check_header (of_flat_tree) != 0) {
256                                 fdt_error ("image is not a fdt");
257                                 do_reset (cmdtp, flag, argc, argv);
258                         }
259
260                         if (be32_to_cpu (fdt_totalsize (of_flat_tree)) != fdt_len) {
261                                 fdt_error ("fdt size != image size");
262                                 do_reset (cmdtp, flag, argc, argv);
263                         }
264                 }
265         }
266 #endif
267
268         if (rd_data_start) {
269             if (!initrd_copy_to_ram) {  /* zero-copy ramdisk support */
270                 initrd_start = rd_data_start;
271                 initrd_end = rd_data_end;
272             } else {
273                 initrd_start  = (ulong)kbd - rd_len;
274                 initrd_start &= ~(4096 - 1);    /* align on page */
275
276                 if (initrd_high) {
277                         ulong nsp;
278
279                         /*
280                          * the inital ramdisk does not need to be within
281                          * CFG_BOOTMAPSZ as it is not accessed until after
282                          * the mm system is initialised.
283                          *
284                          * do the stack bottom calculation again and see if
285                          * the initrd will fit just below the monitor stack
286                          * bottom without overwriting the area allocated
287                          * above for command line args and board info.
288                          */
289                         asm( "mr %0,1": "=r"(nsp) : );
290                         nsp -= 2048;            /* just to be sure */
291                         nsp &= ~0xF;
292                         if (nsp > initrd_high)  /* limit as specified */
293                                 nsp = initrd_high;
294                         nsp -= rd_len;
295                         nsp &= ~(4096 - 1);     /* align on page */
296                         if (nsp >= sp)
297                                 initrd_start = nsp;
298                 }
299
300                 show_boot_progress (12);
301
302                 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
303                         rd_data_start, rd_data_end - 1, rd_len, rd_len);
304
305                 initrd_end    = initrd_start + rd_len;
306                 printf ("   Loading Ramdisk to %08lx, end %08lx ... ",
307                         initrd_start, initrd_end);
308
309                 memmove_wd((void *)initrd_start,
310                            (void *)rd_data_start, rd_len, CHUNKSZ);
311
312                 puts ("OK\n");
313             }
314         } else {
315                 initrd_start = 0;
316                 initrd_end = 0;
317         }
318
319 #if defined(CONFIG_OF_LIBFDT)
320
321 #ifdef CFG_BOOTMAPSZ
322         /*
323          * The blob must be within CFG_BOOTMAPSZ,
324          * so we flag it to be copied if it is not.
325          */
326         if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
327                 of_data = (ulong)of_flat_tree;
328 #endif
329
330         /* move of_flat_tree if needed */
331         if (of_data) {
332                 int err;
333                 ulong of_start, of_len;
334
335                 of_len = be32_to_cpu(fdt_totalsize(of_data));
336
337                 /* position on a 4K boundary before the kbd */
338                 of_start  = (ulong)kbd - of_len;
339                 of_start &= ~(4096 - 1);        /* align on page */
340                 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
341                         of_data, of_data + of_len - 1, of_len, of_len);
342
343                 of_flat_tree = (char *)of_start;
344                 printf ("   Loading Device Tree to %08lx, end %08lx ... ",
345                         of_start, of_start + of_len - 1);
346                 err = fdt_open_into((void *)of_data, (void *)of_start, of_len);
347                 if (err != 0) {
348                         fdt_error ("fdt move failed");
349                         do_reset (cmdtp, flag, argc, argv);
350                 }
351                 puts ("OK\n");
352         }
353         /*
354          * Add the chosen node if it doesn't exist, add the env and bd_t
355          * if the user wants it (the logic is in the subroutines).
356          */
357         if (of_flat_tree) {
358                 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
359                         fdt_error ("/chosen node create failed");
360                         do_reset (cmdtp, flag, argc, argv);
361                 }
362 #ifdef CONFIG_OF_HAS_UBOOT_ENV
363                 if (fdt_env(of_flat_tree) < 0) {
364                         fdt_error ("/u-boot-env node create failed");
365                         do_reset (cmdtp, flag, argc, argv);
366                 }
367 #endif
368 #ifdef CONFIG_OF_HAS_BD_T
369                 if (fdt_bd_t(of_flat_tree) < 0) {
370                         fdt_error ("/bd_t node create failed");
371                         do_reset (cmdtp, flag, argc, argv);
372                 }
373 #endif
374 #ifdef CONFIG_OF_BOARD_SETUP
375                 /* Call the board-specific fixup routine */
376                 ft_board_setup(of_flat_tree, gd->bd);
377 #endif
378         }
379 #endif  /* CONFIG_OF_LIBFDT */
380
381         debug ("## Transferring control to Linux (at address %08lx) ...\n",
382                 (ulong)kernel);
383
384         show_boot_progress (15);
385
386 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
387         unlock_ram_in_cache();
388 #endif
389
390 #if defined(CONFIG_OF_LIBFDT)
391         if (of_flat_tree) {     /* device tree; boot new style */
392                 /*
393                  * Linux Kernel Parameters (passing device tree):
394                  *   r3: pointer to the fdt, followed by the board info data
395                  *   r4: physical pointer to the kernel itself
396                  *   r5: NULL
397                  *   r6: NULL
398                  *   r7: NULL
399                  */
400                 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
401                 /* does not return */
402         }
403 #endif
404         /*
405          * Linux Kernel Parameters (passing board info data):
406          *   r3: ptr to board info data
407          *   r4: initrd_start or 0 if no initrd
408          *   r5: initrd_end - unused if r4 is 0
409          *   r6: Start of command line string
410          *   r7: End   of command line string
411          */
412         (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
413         /* does not return */
414 }
415
416 #if defined(CONFIG_OF_LIBFDT)
417 static void fdt_error (const char *msg)
418 {
419         puts ("ERROR: ");
420         puts (msg);
421         puts (" - must RESET the board to recover.\n");
422 }
423 #endif