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