]> Git Repo - J-u-boot.git/blame - lib_ppc/bootm.c
[new uImage] Move ramdisk loading to a common routine
[J-u-boot.git] / lib_ppc / bootm.c
CommitLineData
5d3cc55e
MB
1/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, [email protected].
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
7582438c
MB
26#define DEBUG
27
5d3cc55e
MB
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>
d3c5eb6d
MB
42
43static void fdt_error (const char *msg);
5d3cc55e
MB
44#endif
45
5d3cc55e
MB
46#ifdef CFG_INIT_RAM_LOCK
47#include <asm/cache.h>
48#endif
49
5d3cc55e 50DECLARE_GLOBAL_DATA_PTR;
5d3cc55e 51
5d3cc55e 52extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
ceaed2b1 53static ulong get_sp (void);
5d3cc55e
MB
54
55#if defined(CONFIG_CMD_BDI)
ceaed2b1 56extern int do_bdinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
5d3cc55e
MB
57#endif
58
59void __attribute__((noinline))
f13e7b2e 60do_bootm_linux(cmd_tbl_t *cmdtp, int flag,
5d3cc55e 61 int argc, char *argv[],
f13e7b2e 62 image_header_t *hdr,
5d3cc55e
MB
63 int verify)
64{
f13e7b2e 65 ulong initrd_start, initrd_end;
5ad03eb3 66 ulong rd_data_start, rd_data_end, rd_len;
f13e7b2e
MB
67
68 ulong cmd_start, cmd_end;
5d3cc55e 69 char *cmdline;
f13e7b2e 70
ceaed2b1 71 ulong sp_limit;
5d3cc55e
MB
72 char *s;
73 bd_t *kbd;
74 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
f13e7b2e 75
4a2ad5ff 76#if defined(CONFIG_OF_LIBFDT)
f13e7b2e 77 image_header_t *fdt_hdr;
5d3cc55e
MB
78 char *of_flat_tree = NULL;
79 ulong of_data = 0;
80#endif
81
5d3cc55e
MB
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
ceaed2b1
MB
92 sp_limit = get_sp();
93 debug ("## Current stack ends at 0x%08lX ", sp_limit);
5d3cc55e 94
ceaed2b1
MB
95 sp_limit -= 2048; /* just to be sure */
96 if (sp_limit > CFG_BOOTMAPSZ)
97 sp_limit = CFG_BOOTMAPSZ;
98 sp_limit &= ~0xF;
5d3cc55e 99
ceaed2b1 100 debug ("=> set upper limit to 0x%08lX\n", sp_limit);
5d3cc55e 101
ceaed2b1 102 cmdline = (char *)((sp_limit - CFG_BARGSIZE) & ~0xF);
5d3cc55e
MB
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
ceaed2b1 146 /* find kernel */
5d3cc55e
MB
147 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
148
ceaed2b1 149 /* find ramdisk */
5ad03eb3
MB
150 get_ramdisk (cmdtp, flag, argc, argv, hdr, verify,
151 IH_ARCH_PPC, &rd_data_start, &rd_data_end);
ceaed2b1 152
5ad03eb3 153 rd_len = rd_data_end - rd_data_start;
5d3cc55e 154
ceaed2b1
MB
155 ramdisk_high (rd_data_start, rd_len, kbd, sp_limit, get_sp (),
156 &initrd_start, &initrd_end);
157
4a2ad5ff 158#if defined(CONFIG_OF_LIBFDT)
ceaed2b1 159 /* find flattened device tree */
5d3cc55e
MB
160 if(argc > 3) {
161 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
f13e7b2e 162 fdt_hdr = (image_header_t *)of_flat_tree;
4a2ad5ff 163
958fc48a 164 if (fdt_check_header (of_flat_tree) == 0) {
5d3cc55e
MB
165#ifndef CFG_NO_FLASH
166 if (addr2info((ulong)of_flat_tree) != NULL)
167 of_data = (ulong)of_flat_tree;
168#endif
f13e7b2e 169 } else if (image_check_magic (fdt_hdr)) {
7582438c
MB
170 ulong image_start, image_end;
171 ulong load_start, load_end;
172
f13e7b2e
MB
173 printf ("## Flat Device Tree at %08lX\n", fdt_hdr);
174 print_image_hdr (fdt_hdr);
5d3cc55e 175
7582438c
MB
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)) {
d3c5eb6d 183 fdt_error ("fdt overwritten");
5d3cc55e
MB
184 do_reset (cmdtp, flag, argc, argv);
185 }
186
187 puts (" Verifying Checksum ... ");
f13e7b2e 188 if (!image_check_hcrc (fdt_hdr)) {
d3c5eb6d 189 fdt_error ("fdt header checksum invalid");
5d3cc55e
MB
190 do_reset (cmdtp, flag, argc, argv);
191 }
192
f13e7b2e 193 if (!image_check_dcrc (fdt_hdr)) {
d3c5eb6d 194 fdt_error ("fdt checksum invalid");
5d3cc55e
MB
195 do_reset (cmdtp, flag, argc, argv);
196 }
197 puts ("OK\n");
198
f13e7b2e 199 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
d3c5eb6d 200 fdt_error ("uImage is not a fdt");
5d3cc55e
MB
201 do_reset (cmdtp, flag, argc, argv);
202 }
f13e7b2e 203 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
d3c5eb6d 204 fdt_error ("uImage is compressed");
5d3cc55e
MB
205 do_reset (cmdtp, flag, argc, argv);
206 }
5d3cc55e 207 if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
d3c5eb6d 208 fdt_error ("uImage data is not a fdt");
5d3cc55e
MB
209 do_reset (cmdtp, flag, argc, argv);
210 }
211
f13e7b2e 212 memmove ((void *)image_get_load (fdt_hdr),
5d3cc55e 213 (void *)(of_flat_tree + image_get_header_size ()),
f13e7b2e 214 image_get_data_size (fdt_hdr));
5d3cc55e 215
f13e7b2e 216 of_flat_tree = (char *)image_get_load (fdt_hdr);
5d3cc55e 217 } else {
d3c5eb6d 218 fdt_error ("Did not find a Flattened Device Tree");
5d3cc55e
MB
219 do_reset (cmdtp, flag, argc, argv);
220 }
221 printf (" Booting using the fdt at 0x%x\n",
222 of_flat_tree);
f13e7b2e
MB
223 } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
224 ulong fdt_data, fdt_len;
5d3cc55e 225
f13e7b2e
MB
226 image_multi_getimg (hdr, 2, &fdt_data, &fdt_len);
227 if (fdt_len) {
228
229 of_flat_tree = (char *)fdt_data;
5d3cc55e
MB
230
231#ifndef CFG_NO_FLASH
f13e7b2e
MB
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;
5d3cc55e
MB
235#endif
236
f13e7b2e 237 if (fdt_check_header (of_flat_tree) != 0) {
d3c5eb6d 238 fdt_error ("image is not a fdt");
f13e7b2e
MB
239 do_reset (cmdtp, flag, argc, argv);
240 }
5d3cc55e 241
f13e7b2e 242 if (be32_to_cpu (fdt_totalsize (of_flat_tree)) != fdt_len) {
d3c5eb6d 243 fdt_error ("fdt size != image size");
f13e7b2e
MB
244 do_reset (cmdtp, flag, argc, argv);
245 }
5d3cc55e
MB
246 }
247 }
248#endif
5d3cc55e 249
5d3cc55e
MB
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) {
d3c5eb6d 280 fdt_error ("fdt move failed");
5d3cc55e
MB
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) {
d3c5eb6d 291 fdt_error ("/chosen node create failed");
5d3cc55e
MB
292 do_reset (cmdtp, flag, argc, argv);
293 }
294#ifdef CONFIG_OF_HAS_UBOOT_ENV
295 if (fdt_env(of_flat_tree) < 0) {
d3c5eb6d 296 fdt_error ("/u-boot-env node create failed");
5d3cc55e
MB
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) {
d3c5eb6d 302 fdt_error ("/bd_t node create failed");
5d3cc55e
MB
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 }
4a2ad5ff 311#endif /* CONFIG_OF_LIBFDT */
d45d5a18 312
5d3cc55e
MB
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
4a2ad5ff 322#if defined(CONFIG_OF_LIBFDT)
5d3cc55e
MB
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}
d3c5eb6d 347
ceaed2b1
MB
348static ulong get_sp (void)
349{
350 ulong sp;
351
352 asm( "mr %0,1": "=r"(sp) : );
353 return sp;
354}
355
d3c5eb6d
MB
356#if defined(CONFIG_OF_LIBFDT)
357static 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
This page took 0.068166 seconds and 4 git commands to generate.