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