]> git.sur5r.net Git - u-boot/blob - lib_ppc/ppc_linux.c
[new uImage] Move PPC do_bootm_linux() to lib_ppc/ppc_linux.c
[u-boot] / lib_ppc / ppc_linux.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 #include <common.h>
27 #include <watchdog.h>
28 #include <command.h>
29 #include <image.h>
30 #include <malloc.h>
31 #include <zlib.h>
32 #include <bzlib.h>
33 #include <environment.h>
34 #include <asm/byteorder.h>
35
36 #if defined(CONFIG_OF_LIBFDT)
37 #include <fdt.h>
38 #include <libfdt.h>
39 #include <fdt_support.h>
40 #endif
41 #if defined(CONFIG_OF_FLAT_TREE)
42 #include <ft_build.h>
43 #endif
44
45 #ifdef CONFIG_LOGBUFFER
46 #include <logbuff.h>
47 #endif
48
49 #ifdef CFG_INIT_RAM_LOCK
50 #include <asm/cache.h>
51 #endif
52
53 #define CHUNKSZ (64 * 1024)
54
55 DECLARE_GLOBAL_DATA_PTR;
56 extern image_header_t header;
57
58 /*cmd_boot.c*/
59 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
60
61 #if defined(CONFIG_CMD_BDI)
62 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
63 #endif
64
65 void  __attribute__((noinline))
66 do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
67                 int     argc, char *argv[],
68                 ulong   addr,
69                 ulong   *len_ptr,
70                 int     verify)
71 {
72         ulong   sp;
73         ulong   len;
74         ulong   initrd_start, initrd_end;
75         ulong   cmd_start, cmd_end;
76         ulong   initrd_high;
77         ulong   data;
78         int     initrd_copy_to_ram = 1;
79         char    *cmdline;
80         char    *s;
81         bd_t    *kbd;
82         void    (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
83         image_header_t *hdr = &header;
84 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
85         char    *of_flat_tree = NULL;
86         ulong   of_data = 0;
87 #endif
88
89         if ((s = getenv ("initrd_high")) != NULL) {
90                 /* a value of "no" or a similar string will act like 0,
91                  * turning the "load high" feature off. This is intentional.
92                  */
93                 initrd_high = simple_strtoul(s, NULL, 16);
94                 if (initrd_high == ~0)
95                         initrd_copy_to_ram = 0;
96         } else {        /* not set, no restrictions to load high */
97                 initrd_high = ~0;
98         }
99
100 #ifdef CONFIG_LOGBUFFER
101         kbd=gd->bd;
102         /* Prevent initrd from overwriting logbuffer */
103         if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
104                 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
105         debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
106 #endif
107
108         /*
109          * Booting a (Linux) kernel image
110          *
111          * Allocate space for command line and board info - the
112          * address should be as high as possible within the reach of
113          * the kernel (see CFG_BOOTMAPSZ settings), but in unused
114          * memory, which means far enough below the current stack
115          * pointer.
116          */
117
118         asm( "mr %0,1": "=r"(sp) : );
119
120         debug ("## Current stack ends at 0x%08lX ", sp);
121
122         sp -= 2048;             /* just to be sure */
123         if (sp > CFG_BOOTMAPSZ)
124                 sp = CFG_BOOTMAPSZ;
125         sp &= ~0xF;
126
127         debug ("=> set upper limit to 0x%08lX\n", sp);
128
129         cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
130         kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
131
132         if ((s = getenv("bootargs")) == NULL)
133                 s = "";
134
135         strcpy (cmdline, s);
136
137         cmd_start    = (ulong)&cmdline[0];
138         cmd_end      = cmd_start + strlen(cmdline);
139
140         *kbd = *(gd->bd);
141
142 #ifdef  DEBUG
143         printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
144
145 #if defined(CONFIG_CMD_BDI)
146         do_bdinfo (NULL, 0, 0, NULL);
147 #endif
148 #endif
149
150         if ((s = getenv ("clocks_in_mhz")) != NULL) {
151                 /* convert all clock information to MHz */
152                 kbd->bi_intfreq /= 1000000L;
153                 kbd->bi_busfreq /= 1000000L;
154 #if defined(CONFIG_MPC8220)
155         kbd->bi_inpfreq /= 1000000L;
156         kbd->bi_pcifreq /= 1000000L;
157         kbd->bi_pevfreq /= 1000000L;
158         kbd->bi_flbfreq /= 1000000L;
159         kbd->bi_vcofreq /= 1000000L;
160 #endif
161 #if defined(CONFIG_CPM2)
162                 kbd->bi_cpmfreq /= 1000000L;
163                 kbd->bi_brgfreq /= 1000000L;
164                 kbd->bi_sccfreq /= 1000000L;
165                 kbd->bi_vco     /= 1000000L;
166 #endif
167 #if defined(CONFIG_MPC5xxx)
168                 kbd->bi_ipbfreq /= 1000000L;
169                 kbd->bi_pcifreq /= 1000000L;
170 #endif /* CONFIG_MPC5xxx */
171         }
172
173         kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
174
175         /*
176          * Check if there is an initrd image
177          */
178
179 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
180         /* Look for a '-' which indicates to ignore the ramdisk argument */
181         if (argc >= 3 && strcmp(argv[2], "-") ==  0) {
182                         debug ("Skipping initrd\n");
183                         len = data = 0;
184                 }
185         else
186 #endif
187         if (argc >= 3) {
188                 debug ("Not skipping initrd\n");
189                 show_boot_progress (9);
190
191                 addr = simple_strtoul(argv[2], NULL, 16);
192
193                 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
194                 hdr = (image_header_t *)addr;
195
196                 if (!image_check_magic (hdr)) {
197                         puts ("Bad Magic Number\n");
198                         show_boot_progress (-10);
199                         do_reset (cmdtp, flag, argc, argv);
200                 }
201
202                 if (!image_check_hcrc (hdr)) {
203                         puts ("Bad Header Checksum\n");
204                         show_boot_progress (-11);
205                         do_reset (cmdtp, flag, argc, argv);
206                 }
207                 show_boot_progress (10);
208
209                 print_image_hdr (hdr);
210
211                 if (verify) {
212                         puts ("   Verifying Checksum ... ");
213
214                         if (!image_check_dcrc_wd (hdr, CHUNKSZ)) {
215                                 puts ("Bad Data CRC\n");
216                                 show_boot_progress (-12);
217                                 do_reset (cmdtp, flag, argc, argv);
218                         }
219                         puts ("OK\n");
220                 }
221
222                 show_boot_progress (11);
223
224                 if (!image_check_os (hdr, IH_OS_LINUX) ||
225                     !image_check_arch (hdr, IH_ARCH_PPC) ||
226                     !image_check_type (hdr, IH_TYPE_RAMDISK)) {
227                         puts ("No Linux PPC Ramdisk Image\n");
228                         show_boot_progress (-13);
229                         do_reset (cmdtp, flag, argc, argv);
230                 }
231
232                 data = image_get_data (hdr);
233                 len = image_get_data_size (hdr);
234
235                 /*
236                  * Now check if we have a multifile image
237                  */
238         } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1])) {
239                 u_long tail    = image_to_cpu (len_ptr[0]) % 4;
240                 int i;
241
242                 show_boot_progress (13);
243
244                 /* skip kernel length and terminator */
245                 data = (ulong)(&len_ptr[2]);
246                 /* skip any additional image length fields */
247                 for (i=1; len_ptr[i]; ++i)
248                         data += 4;
249                 /* add kernel length, and align */
250                 data += image_to_cpu (len_ptr[0]);
251                 if (tail) {
252                         data += 4 - tail;
253                 }
254
255                 len   = image_to_cpu (len_ptr[1]);
256
257         } else {
258                 /*
259                  * no initrd image
260                  */
261                 show_boot_progress (14);
262
263                 len = data = 0;
264         }
265
266 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
267         if(argc > 3) {
268                 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
269                 hdr = (image_header_t *)of_flat_tree;
270 #if defined(CONFIG_OF_FLAT_TREE)
271                 if (*((ulong *)(of_flat_tree + image_get_header_size ())) != OF_DT_HEADER) {
272 #else
273                 if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
274 #endif
275 #ifndef CFG_NO_FLASH
276                         if (addr2info((ulong)of_flat_tree) != NULL)
277                                 of_data = (ulong)of_flat_tree;
278 #endif
279                 } else if (image_check_magic (hdr)) {
280                         printf("## Flat Device Tree at %08lX\n", hdr);
281                         print_image_hdr (hdr);
282
283                         if ((image_get_load (hdr) <  ((unsigned long)hdr + image_get_image_size (hdr))) &&
284                            ((image_get_load (hdr) + image_get_data_size (hdr)) > (unsigned long)hdr)) {
285                                 puts ("ERROR: fdt overwritten - "
286                                         "must RESET the board to recover.\n");
287                                 do_reset (cmdtp, flag, argc, argv);
288                         }
289
290                         puts ("   Verifying Checksum ... ");
291                         if (!image_check_hcrc (hdr)) {
292                                 puts ("ERROR: fdt header checksum invalid - "
293                                         "must RESET the board to recover.\n");
294                                 do_reset (cmdtp, flag, argc, argv);
295                         }
296
297                         if (!image_check_dcrc (hdr)) {
298                                 puts ("ERROR: fdt checksum invalid - "
299                                         "must RESET the board to recover.\n");
300                                 do_reset (cmdtp, flag, argc, argv);
301                         }
302                         puts ("OK\n");
303
304                         if (!image_check_type (hdr, IH_TYPE_FLATDT)) {
305                                 puts ("ERROR: uImage is not a fdt - "
306                                         "must RESET the board to recover.\n");
307                                 do_reset (cmdtp, flag, argc, argv);
308                         }
309                         if (image_get_comp (hdr) != IH_COMP_NONE) {
310                                 puts ("ERROR: uImage is compressed - "
311                                         "must RESET the board to recover.\n");
312                                 do_reset (cmdtp, flag, argc, argv);
313                         }
314 #if defined(CONFIG_OF_FLAT_TREE)
315                         if (*((ulong *)(of_flat_tree + image_get_header_size ())) != OF_DT_HEADER) {
316 #else
317                         if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
318 #endif
319                                 puts ("ERROR: uImage data is not a fdt - "
320                                         "must RESET the board to recover.\n");
321                                 do_reset (cmdtp, flag, argc, argv);
322                         }
323
324                         memmove ((void *)image_get_load (hdr),
325                                 (void *)(of_flat_tree + image_get_header_size ()),
326                                 image_get_data_size (hdr));
327
328                         of_flat_tree = (char *)image_get_load (hdr);
329                 } else {
330                         puts ("Did not find a flat Flat Device Tree.\n"
331                                 "Must RESET the board to recover.\n");
332                         do_reset (cmdtp, flag, argc, argv);
333                 }
334                 printf ("   Booting using the fdt at 0x%x\n",
335                                 of_flat_tree);
336         } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
337                 u_long tail    = image_to_cpu (len_ptr[0]) % 4;
338                 int i;
339
340                 /* skip kernel length, initrd length, and terminator */
341                 of_flat_tree = (char *)(&len_ptr[3]);
342                 /* skip any additional image length fields */
343                 for (i=2; len_ptr[i]; ++i)
344                         of_flat_tree += 4;
345                 /* add kernel length, and align */
346                 of_flat_tree += image_to_cpu (len_ptr[0]);
347                 if (tail) {
348                         of_flat_tree += 4 - tail;
349                 }
350
351                 /* add initrd length, and align */
352                 tail = image_to_cpu (len_ptr[1]) % 4;
353                 of_flat_tree += image_to_cpu (len_ptr[1]);
354                 if (tail) {
355                         of_flat_tree += 4 - tail;
356                 }
357
358 #ifndef CFG_NO_FLASH
359                 /* move the blob if it is in flash (set of_data to !null) */
360                 if (addr2info ((ulong)of_flat_tree) != NULL)
361                         of_data = (ulong)of_flat_tree;
362 #endif
363
364
365 #if defined(CONFIG_OF_FLAT_TREE)
366                 if (*((ulong *)(of_flat_tree)) != OF_DT_HEADER) {
367 #else
368                 if (fdt_check_header (of_flat_tree) != 0) {
369 #endif
370                         puts ("ERROR: image is not a fdt - "
371                                 "must RESET the board to recover.\n");
372                         do_reset (cmdtp, flag, argc, argv);
373                 }
374
375 #if defined(CONFIG_OF_FLAT_TREE)
376                 if (((struct boot_param_header *)of_flat_tree)->totalsize !=
377                         image_to_cpu (len_ptr[2])) {
378 #else
379                 if (be32_to_cpu (fdt_totalsize (of_flat_tree)) !=
380                         image_to_cpu (len_ptr[2])) {
381 #endif
382                         puts ("ERROR: fdt size != image size - "
383                                 "must RESET the board to recover.\n");
384                         do_reset (cmdtp, flag, argc, argv);
385                 }
386         }
387 #endif
388         if (!data) {
389                 debug ("No initrd\n");
390         }
391
392         if (data) {
393             if (!initrd_copy_to_ram) {  /* zero-copy ramdisk support */
394                 initrd_start = data;
395                 initrd_end = initrd_start + len;
396             } else {
397                 initrd_start  = (ulong)kbd - len;
398                 initrd_start &= ~(4096 - 1);    /* align on page */
399
400                 if (initrd_high) {
401                         ulong nsp;
402
403                         /*
404                          * the inital ramdisk does not need to be within
405                          * CFG_BOOTMAPSZ as it is not accessed until after
406                          * the mm system is initialised.
407                          *
408                          * do the stack bottom calculation again and see if
409                          * the initrd will fit just below the monitor stack
410                          * bottom without overwriting the area allocated
411                          * above for command line args and board info.
412                          */
413                         asm( "mr %0,1": "=r"(nsp) : );
414                         nsp -= 2048;            /* just to be sure */
415                         nsp &= ~0xF;
416                         if (nsp > initrd_high)  /* limit as specified */
417                                 nsp = initrd_high;
418                         nsp -= len;
419                         nsp &= ~(4096 - 1);     /* align on page */
420                         if (nsp >= sp)
421                                 initrd_start = nsp;
422                 }
423
424                 show_boot_progress (12);
425
426                 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
427                         data, data + len - 1, len, len);
428
429                 initrd_end    = initrd_start + len;
430                 printf ("   Loading Ramdisk to %08lx, end %08lx ... ",
431                         initrd_start, initrd_end);
432 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
433                 {
434                         size_t l = len;
435                         void *to = (void *)initrd_start;
436                         void *from = (void *)data;
437
438                         while (l > 0) {
439                                 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
440                                 WATCHDOG_RESET();
441                                 memmove (to, from, tail);
442                                 to += tail;
443                                 from += tail;
444                                 l -= tail;
445                         }
446                 }
447 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
448                 memmove ((void *)initrd_start, (void *)data, len);
449 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
450                 puts ("OK\n");
451             }
452         } else {
453                 initrd_start = 0;
454                 initrd_end = 0;
455         }
456
457 #if defined(CONFIG_OF_LIBFDT)
458
459 #ifdef CFG_BOOTMAPSZ
460         /*
461          * The blob must be within CFG_BOOTMAPSZ,
462          * so we flag it to be copied if it is not.
463          */
464         if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
465                 of_data = (ulong)of_flat_tree;
466 #endif
467
468         /* move of_flat_tree if needed */
469         if (of_data) {
470                 int err;
471                 ulong of_start, of_len;
472
473                 of_len = be32_to_cpu(fdt_totalsize(of_data));
474
475                 /* position on a 4K boundary before the kbd */
476                 of_start  = (ulong)kbd - of_len;
477                 of_start &= ~(4096 - 1);        /* align on page */
478                 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
479                         of_data, of_data + of_len - 1, of_len, of_len);
480
481                 of_flat_tree = (char *)of_start;
482                 printf ("   Loading Device Tree to %08lx, end %08lx ... ",
483                         of_start, of_start + of_len - 1);
484                 err = fdt_open_into((void *)of_data, (void *)of_start, of_len);
485                 if (err != 0) {
486                         puts ("ERROR: fdt move failed - "
487                                 "must RESET the board to recover.\n");
488                         do_reset (cmdtp, flag, argc, argv);
489                 }
490                 puts ("OK\n");
491         }
492         /*
493          * Add the chosen node if it doesn't exist, add the env and bd_t
494          * if the user wants it (the logic is in the subroutines).
495          */
496         if (of_flat_tree) {
497                 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
498                         puts ("ERROR: /chosen node create failed - "
499                                 "must RESET the board to recover.\n");
500                         do_reset (cmdtp, flag, argc, argv);
501                 }
502 #ifdef CONFIG_OF_HAS_UBOOT_ENV
503                 if (fdt_env(of_flat_tree) < 0) {
504                         puts ("ERROR: /u-boot-env node create failed - "
505                                 "must RESET the board to recover.\n");
506                         do_reset (cmdtp, flag, argc, argv);
507                 }
508 #endif
509 #ifdef CONFIG_OF_HAS_BD_T
510                 if (fdt_bd_t(of_flat_tree) < 0) {
511                         puts ("ERROR: /bd_t node create failed - "
512                                 "must RESET the board to recover.\n");
513                         do_reset (cmdtp, flag, argc, argv);
514                 }
515 #endif
516 #ifdef CONFIG_OF_BOARD_SETUP
517                 /* Call the board-specific fixup routine */
518                 ft_board_setup(of_flat_tree, gd->bd);
519 #endif
520         }
521 #endif /* CONFIG_OF_LIBFDT */
522 #if defined(CONFIG_OF_FLAT_TREE)
523 #ifdef CFG_BOOTMAPSZ
524         /*
525          * The blob must be within CFG_BOOTMAPSZ,
526          * so we flag it to be copied if it is not.
527          */
528         if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
529                 of_data = (ulong)of_flat_tree;
530 #endif
531
532         /* move of_flat_tree if needed */
533         if (of_data) {
534                 ulong of_start, of_len;
535                 of_len = ((struct boot_param_header *)of_data)->totalsize;
536
537                 /* provide extra 8k pad */
538                 of_start  = (ulong)kbd - of_len - 8192;
539                 of_start &= ~(4096 - 1);        /* align on page */
540                 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
541                         of_data, of_data + of_len - 1, of_len, of_len);
542
543                 of_flat_tree = (char *)of_start;
544                 printf ("   Loading Device Tree to %08lx, end %08lx ... ",
545                         of_start, of_start + of_len - 1);
546                 memmove ((void *)of_start, (void *)of_data, of_len);
547                 puts ("OK\n");
548         }
549         /*
550          * Create the /chosen node and modify the blob with board specific
551          * values as needed.
552          */
553         ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
554         /* ft_dump_blob(of_flat_tree); */
555 #endif
556         debug ("## Transferring control to Linux (at address %08lx) ...\n",
557                 (ulong)kernel);
558
559         show_boot_progress (15);
560
561 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
562         unlock_ram_in_cache();
563 #endif
564
565 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
566         if (of_flat_tree) {     /* device tree; boot new style */
567                 /*
568                  * Linux Kernel Parameters (passing device tree):
569                  *   r3: pointer to the fdt, followed by the board info data
570                  *   r4: physical pointer to the kernel itself
571                  *   r5: NULL
572                  *   r6: NULL
573                  *   r7: NULL
574                  */
575                 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
576                 /* does not return */
577         }
578 #endif
579         /*
580          * Linux Kernel Parameters (passing board info data):
581          *   r3: ptr to board info data
582          *   r4: initrd_start or 0 if no initrd
583          *   r5: initrd_end - unused if r4 is 0
584          *   r6: Start of command line string
585          *   r7: End   of command line string
586          */
587         (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
588         /* does not return */
589 }