blob: 92c18d059d72543f5b42d099ae11597d917efd53 [file] [log] [blame]
wdenk47d1a6e2002-11-03 00:01:44 +00001/*
Stefan Roese15940c92006-03-13 11:16:36 +01002 * (C) Copyright 2000-2006
wdenk47d1a6e2002-11-03 00:01:44 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010024#define DEBUG
25
wdenk47d1a6e2002-11-03 00:01:44 +000026/*
27 * Boot support
28 */
29#include <common.h>
30#include <watchdog.h>
31#include <command.h>
wdenk47d1a6e2002-11-03 00:01:44 +000032#include <image.h>
33#include <malloc.h>
34#include <zlib.h>
wdenkc29fdfc2003-08-29 20:57:53 +000035#include <bzlib.h>
wdenk7f70e852003-05-20 14:25:27 +000036#include <environment.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060037#include <lmb.h>
wdenk47d1a6e2002-11-03 00:01:44 +000038#include <asm/byteorder.h>
wdenk8bde7f72003-06-27 21:31:46 +000039
wdenk47d1a6e2002-11-03 00:01:44 +000040#ifdef CFG_HUSH_PARSER
41#include <hush.h>
42#endif
43
Marian Balakowicz1ee11802008-01-08 18:17:10 +010044DECLARE_GLOBAL_DATA_PTR;
45
46extern int gunzip (void *dst, int dstlen, unsigned char *src, unsigned long *lenp);
47#ifndef CFG_BOOTM_LEN
48#define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
49#endif
wdenk47d1a6e2002-11-03 00:01:44 +000050
Marian Balakowicz321359f2008-01-08 18:11:43 +010051#ifdef CONFIG_BZIP2
52extern void bz_internal_error(int);
53#endif
wdenk47d1a6e2002-11-03 00:01:44 +000054
Jon Loeligerbaa26db2007-07-08 17:51:39 -050055#if defined(CONFIG_CMD_IMI)
wdenk47d1a6e2002-11-03 00:01:44 +000056static int image_info (unsigned long addr);
57#endif
wdenk27b207f2003-07-24 23:38:38 +000058
Jon Loeligerbaa26db2007-07-08 17:51:39 -050059#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +000060#include <flash.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020061extern flash_info_t flash_info[]; /* info for FLASH chips */
wdenk27b207f2003-07-24 23:38:38 +000062static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
63#endif
64
Marian Balakowicz1ee11802008-01-08 18:17:10 +010065#ifdef CONFIG_SILENT_CONSOLE
66static void fixup_silent_linux (void);
67#endif
68
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010069static void *get_kernel (cmd_tbl_t *cmdtp, int flag,int argc, char *argv[],
70 bootm_headers_t *images, ulong *os_data, ulong *os_len);
Marian Balakowicz1ee11802008-01-08 18:17:10 +010071extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
wdenk47d1a6e2002-11-03 00:01:44 +000072
73/*
74 * Continue booting an OS image; caller already has:
75 * - copied image header to global variable `header'
76 * - checked header magic number, checksums (both header & image),
77 * - verified image architecture (PPC) and type (KERNEL or MULTI),
78 * - loaded (first part of) image to header load address,
79 * - disabled interrupts.
80 */
Marian Balakowicz1ee11802008-01-08 18:17:10 +010081typedef void boot_os_fn (cmd_tbl_t *cmdtp, int flag,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010082 int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010083 bootm_headers_t *images); /* pointers to os/initrd/fdt */
wdenk47d1a6e2002-11-03 00:01:44 +000084
Marian Balakowicz1ee11802008-01-08 18:17:10 +010085extern boot_os_fn do_bootm_linux;
86static boot_os_fn do_bootm_netbsd;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010087#if defined(CONFIG_LYNXKDI)
Marian Balakowicz1ee11802008-01-08 18:17:10 +010088static boot_os_fn do_bootm_lynxkdi;
89extern void lynxkdi_boot (image_header_t *);
wdenkf72da342003-10-10 10:05:42 +000090#endif
Marian Balakowicz1ee11802008-01-08 18:17:10 +010091static boot_os_fn do_bootm_rtems;
Jon Loeligerbaa26db2007-07-08 17:51:39 -050092#if defined(CONFIG_CMD_ELF)
Marian Balakowicz1ee11802008-01-08 18:17:10 +010093static boot_os_fn do_bootm_vxworks;
94static boot_os_fn do_bootm_qnxelf;
95int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
96int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
Jon Loeliger90253172007-07-10 11:02:44 -050097#endif
wdenk7f70e852003-05-20 14:25:27 +000098#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
Marian Balakowicz1ee11802008-01-08 18:17:10 +010099extern uchar (*env_get_char)(int); /* Returns a character from the environment */
100static boot_os_fn do_bootm_artos;
Stefan Roese15940c92006-03-13 11:16:36 +0100101#endif
102
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100103ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100104static bootm_headers_t images; /* pointers to os/initrd/fdt images */
wdenk47d1a6e2002-11-03 00:01:44 +0000105
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100106
107/*******************************************************************/
108/* bootm - boot application image from image in memory */
109/*******************************************************************/
wdenk47d1a6e2002-11-03 00:01:44 +0000110int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
111{
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100112 ulong iflag;
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100113 const char *type_name;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100114 uint unc_len = CFG_BOOTM_LEN;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100115 uint8_t comp, type, os;
wdenk47d1a6e2002-11-03 00:01:44 +0000116
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100117 void *os_hdr;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100118 ulong os_data, os_len;
Marian Balakowicz75824382008-01-31 13:20:06 +0100119 ulong image_start, image_end;
120 ulong load_start, load_end;
121
Kumar Gala4ed65522008-02-27 21:51:47 -0600122 struct lmb lmb;
123
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100124 memset ((void *)&images, 0, sizeof (images));
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100125 images.verify = getenv_verify();
Kumar Gala4ed65522008-02-27 21:51:47 -0600126 images.lmb = &lmb;
127
128 lmb_init(&lmb);
129
130#ifdef CFG_SDRAM_BASE
131 lmb_add(&lmb, CFG_SDRAM_BASE, gd->bd->bi_memsize);
132#else
133 lmb_add(&lmb, 0, gd->bd->bi_memsize);
134#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100135
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100136 /* get kernel image header, start address and length */
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100137 os_hdr = get_kernel (cmdtp, flag, argc, argv,
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100138 &images, &os_data, &os_len);
139 if (os_len == 0)
wdenk47d1a6e2002-11-03 00:01:44 +0000140 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000141
Heiko Schocherfad63402007-07-13 09:54:17 +0200142 show_boot_progress (6);
wdenk47d1a6e2002-11-03 00:01:44 +0000143
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100144 /* get image parameters */
145 switch (gen_image_get_format (os_hdr)) {
146 case IMAGE_FORMAT_LEGACY:
147 type = image_get_type (os_hdr);
148 comp = image_get_comp (os_hdr);
149 os = image_get_os (os_hdr);
150
151 image_end = image_get_image_end (os_hdr);
152 load_start = image_get_load (os_hdr);
153 break;
154#if defined(CONFIG_FIT)
155 case IMAGE_FORMAT_FIT:
156 fit_unsupported ("bootm");
157 return 1;
158#endif
159 default:
160 puts ("ERROR: unknown image format type!\n");
161 return 1;
162 }
163
164 image_start = (ulong)os_hdr;
165 load_end = 0;
166 type_name = image_get_type_name (type);
167
wdenk47d1a6e2002-11-03 00:01:44 +0000168 /*
169 * We have reached the point of no return: we are going to
170 * overwrite all exception vector code, so we cannot easily
171 * recover from any failures any more...
172 */
wdenk47d1a6e2002-11-03 00:01:44 +0000173 iflag = disable_interrupts();
174
wdenkc7de8292002-11-19 11:04:11 +0000175#ifdef CONFIG_AMIGAONEG3SE
176 /*
wdenk8bde7f72003-06-27 21:31:46 +0000177 * We've possible left the caches enabled during
wdenkc7de8292002-11-19 11:04:11 +0000178 * bios emulation, so turn them off again
179 */
180 icache_disable();
181 invalidate_l1_instruction_cache();
182 flush_data_cache();
183 dcache_disable();
184#endif
185
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100186 switch (comp) {
wdenk47d1a6e2002-11-03 00:01:44 +0000187 case IH_COMP_NONE:
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100188 if (load_start == (ulong)os_hdr) {
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100189 printf (" XIP %s ... ", type_name);
wdenk47d1a6e2002-11-03 00:01:44 +0000190 } else {
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100191 printf (" Loading %s ... ", type_name);
wdenk47d1a6e2002-11-03 00:01:44 +0000192
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100193 memmove_wd ((void *)load_start,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100194 (void *)os_data, os_len, CHUNKSZ);
Marian Balakowiczaf13cdb2008-01-08 18:11:45 +0100195
Marian Balakowicz75824382008-01-31 13:20:06 +0100196 load_end = load_start + os_len;
Marian Balakowiczaf13cdb2008-01-08 18:11:45 +0100197 puts("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000198 }
199 break;
200 case IH_COMP_GZIP:
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100201 printf (" Uncompressing %s ... ", type_name);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100202 if (gunzip ((void *)load_start, unc_len,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100203 (uchar *)os_data, &os_len) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000204 puts ("GUNZIP ERROR - must RESET board to recover\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200205 show_boot_progress (-6);
wdenk47d1a6e2002-11-03 00:01:44 +0000206 do_reset (cmdtp, flag, argc, argv);
207 }
Marian Balakowicz75824382008-01-31 13:20:06 +0100208
209 load_end = load_start + os_len;
wdenk47d1a6e2002-11-03 00:01:44 +0000210 break;
wdenkc29fdfc2003-08-29 20:57:53 +0000211#ifdef CONFIG_BZIP2
212 case IH_COMP_BZIP2:
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100213 printf (" Uncompressing %s ... ", type_name);
wdenk5653fc32004-02-08 22:55:38 +0000214 /*
215 * If we've got less than 4 MB of malloc() space,
216 * use slower decompression algorithm which requires
217 * at most 2300 KB of memory.
218 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100219 int i = BZ2_bzBuffToBuffDecompress ((char*)load_start,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100220 &unc_len, (char *)os_data, os_len,
221 CFG_MALLOC_LEN < (4096 * 1024), 0);
wdenkc29fdfc2003-08-29 20:57:53 +0000222 if (i != BZ_OK) {
223 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
Heiko Schocherfad63402007-07-13 09:54:17 +0200224 show_boot_progress (-6);
wdenkc29fdfc2003-08-29 20:57:53 +0000225 do_reset (cmdtp, flag, argc, argv);
226 }
Marian Balakowicz75824382008-01-31 13:20:06 +0100227
228 load_end = load_start + unc_len;
wdenkc29fdfc2003-08-29 20:57:53 +0000229 break;
230#endif /* CONFIG_BZIP2 */
wdenk47d1a6e2002-11-03 00:01:44 +0000231 default:
232 if (iflag)
233 enable_interrupts();
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100234 printf ("Unimplemented compression type %d\n", comp);
Heiko Schocherfad63402007-07-13 09:54:17 +0200235 show_boot_progress (-7);
wdenk47d1a6e2002-11-03 00:01:44 +0000236 return 1;
237 }
wdenk4b9206e2004-03-23 22:14:11 +0000238 puts ("OK\n");
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100239 debug (" kernel loaded at 0x%08lx, end = 0x%08lx\n", load_start, load_end);
Heiko Schocherfad63402007-07-13 09:54:17 +0200240 show_boot_progress (7);
wdenk47d1a6e2002-11-03 00:01:44 +0000241
Marian Balakowicz75824382008-01-31 13:20:06 +0100242 if ((load_start < image_end) && (load_end > image_start)) {
243 debug ("image_start = 0x%lX, image_end = 0x%lx\n", image_start, image_end);
244 debug ("load_start = 0x%lx, load_end = 0x%lx\n", load_start, load_end);
245
246 puts ("ERROR: image overwritten - must RESET the board to recover.\n");
247 do_reset (cmdtp, flag, argc, argv);
248 }
249
Heiko Schocherfad63402007-07-13 09:54:17 +0200250 show_boot_progress (8);
wdenk47d1a6e2002-11-03 00:01:44 +0000251
Kumar Gala4ed65522008-02-27 21:51:47 -0600252 lmb_reserve(&lmb, load_start, (load_end - load_start));
253
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100254 switch (os) {
wdenk47d1a6e2002-11-03 00:01:44 +0000255 default: /* handled by (original) Linux case */
256 case IH_OS_LINUX:
wdenkf72da342003-10-10 10:05:42 +0000257#ifdef CONFIG_SILENT_CONSOLE
258 fixup_silent_linux();
259#endif
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100260 do_bootm_linux (cmdtp, flag, argc, argv, &images);
wdenk47d1a6e2002-11-03 00:01:44 +0000261 break;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100262
wdenk47d1a6e2002-11-03 00:01:44 +0000263 case IH_OS_NETBSD:
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100264 do_bootm_netbsd (cmdtp, flag, argc, argv, &images);
wdenk47d1a6e2002-11-03 00:01:44 +0000265 break;
wdenk8bde7f72003-06-27 21:31:46 +0000266
wdenk1f4bb372003-07-27 00:21:01 +0000267#ifdef CONFIG_LYNXKDI
268 case IH_OS_LYNXOS:
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100269 do_bootm_lynxkdi (cmdtp, flag, argc, argv, &images);
wdenk1f4bb372003-07-27 00:21:01 +0000270 break;
271#endif
272
wdenkd791b1d2003-04-20 14:04:18 +0000273 case IH_OS_RTEMS:
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100274 do_bootm_rtems (cmdtp, flag, argc, argv, &images);
wdenkd791b1d2003-04-20 14:04:18 +0000275 break;
wdenk8bde7f72003-06-27 21:31:46 +0000276
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500277#if defined(CONFIG_CMD_ELF)
wdenk47d1a6e2002-11-03 00:01:44 +0000278 case IH_OS_VXWORKS:
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100279 do_bootm_vxworks (cmdtp, flag, argc, argv, &images);
wdenk47d1a6e2002-11-03 00:01:44 +0000280 break;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100281
wdenk47d1a6e2002-11-03 00:01:44 +0000282 case IH_OS_QNX:
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100283 do_bootm_qnxelf (cmdtp, flag, argc, argv, &images);
wdenk47d1a6e2002-11-03 00:01:44 +0000284 break;
Jon Loeliger90253172007-07-10 11:02:44 -0500285#endif
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100286
wdenk7f70e852003-05-20 14:25:27 +0000287#ifdef CONFIG_ARTOS
288 case IH_OS_ARTOS:
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100289 do_bootm_artos (cmdtp, flag, argc, argv, &images);
wdenk7f70e852003-05-20 14:25:27 +0000290 break;
291#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000292 }
293
Heiko Schocherfad63402007-07-13 09:54:17 +0200294 show_boot_progress (-9);
wdenk47d1a6e2002-11-03 00:01:44 +0000295#ifdef DEBUG
wdenk4b9206e2004-03-23 22:14:11 +0000296 puts ("\n## Control returned to monitor - resetting...\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000297 do_reset (cmdtp, flag, argc, argv);
298#endif
299 return 1;
300}
301
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100302/**
303 * get_kernel - find kernel image
304 * @os_data: pointer to a ulong variable, will hold os data start address
305 * @os_len: pointer to a ulong variable, will hold os data length
306 *
307 * get_kernel() tries to find a kernel image, verifies its integrity
308 * and locates kernel data.
309 *
310 * returns:
311 * pointer to image header if valid image was found, plus kernel start
312 * address and length, otherwise NULL
313 */
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100314static image_header_t *image_get_kernel (ulong img_addr, int verify)
315{
316 image_header_t *hdr = (image_header_t *)img_addr;
317
318 if (!image_check_magic(hdr)) {
319 puts ("Bad Magic Number\n");
320 show_boot_progress (-1);
321 return NULL;
322 }
323 show_boot_progress (2);
324
325 if (!image_check_hcrc (hdr)) {
326 puts ("Bad Header Checksum\n");
327 show_boot_progress (-2);
328 return NULL;
329 }
330
331 show_boot_progress (3);
332 image_print_contents (hdr);
333
334 if (verify) {
335 puts (" Verifying Checksum ... ");
336 if (!image_check_dcrc (hdr)) {
337 printf ("Bad Data CRC\n");
338 show_boot_progress (-3);
339 return NULL;
340 }
341 puts ("OK\n");
342 }
343 show_boot_progress (4);
344
345 if (!image_check_target_arch (hdr)) {
346 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
347 show_boot_progress (-4);
348 return NULL;
349 }
350 return hdr;
351}
352
353/**
354 * get_kernel - find kernel image
355 * @os_data: pointer to a ulong variable, will hold os data start address
356 * @os_len: pointer to a ulong variable, will hold os data length
357 *
358 * get_kernel() tries to find a kernel image, verifies its integrity
359 * and locates kernel data.
360 *
361 * returns:
362 * pointer to image header if valid image was found, plus kernel start
363 * address and length, otherwise NULL
364 */
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100365static void *get_kernel (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
366 bootm_headers_t *images, ulong *os_data, ulong *os_len)
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100367{
368 image_header_t *hdr;
369 ulong img_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100370#if defined(CONFIG_FIT)
371 void *fit_hdr;
372 const char *fit_uname_config = NULL;
373 const char *fit_uname_kernel = NULL;
374#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100375
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100376 /* find out kernel image address */
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100377 if (argc < 2) {
378 img_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100379 debug ("* kernel: default image load address = 0x%08lx\n",
380 load_addr);
381#if defined(CONFIG_FIT)
382 } else if (fit_parse_conf (argv[1], load_addr, &img_addr,
383 &fit_uname_config)) {
384 debug ("* kernel: config '%s' from image at 0x%08lx\n",
385 fit_uname_config, img_addr);
386 } else if (fit_parse_subimage (argv[1], load_addr, &img_addr,
387 &fit_uname_kernel)) {
388 debug ("* kernel: subimage '%s' from image at 0x%08lx\n",
389 fit_uname_kernel, img_addr);
390#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100391 } else {
392 img_addr = simple_strtoul(argv[1], NULL, 16);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100393 debug ("* kernel: cmdline image address = 0x%08lx\n", img_addr);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100394 }
395
396 show_boot_progress (1);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100397 printf ("## Booting kernel image at %08lx ...\n", img_addr);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100398
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100399 /* copy from dataflash if needed */
400 img_addr = gen_get_image (img_addr);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100401
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100402 /* check image type, for FIT images get FIT kernel node */
403 switch (gen_image_get_format ((void *)img_addr)) {
404 case IMAGE_FORMAT_LEGACY:
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100405
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100406 debug ("* kernel: legacy format image\n");
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100407 hdr = image_get_kernel (img_addr, images->verify);
408 if (!hdr)
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100409 return NULL;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100410 show_boot_progress (5);
411
412 switch (image_get_type (hdr)) {
413 case IH_TYPE_KERNEL:
414 *os_data = image_get_data (hdr);
415 *os_len = image_get_data_size (hdr);
416 break;
417 case IH_TYPE_MULTI:
418 image_multi_getimg (hdr, 0, os_data, os_len);
419 break;
420 default:
421 printf ("Wrong Image Type for %s command\n", cmdtp->name);
422 show_boot_progress (-5);
423 return NULL;
424 }
425 images->legacy_hdr_os = hdr;
426 images->legacy_hdr_valid = 1;
427
428 break;
429#if defined(CONFIG_FIT)
430 case IMAGE_FORMAT_FIT:
431 fit_hdr = (void *)img_addr;
432 debug ("* kernel: FIT format image\n");
433 fit_unsupported ("kernel");
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100434 return NULL;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100435#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100436 default:
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100437 printf ("Wrong Image Format for %s command\n", cmdtp->name);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100438 return NULL;
439 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100440
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100441 debug (" kernel data at 0x%08lx, end = 0x%08lx\n",
442 *os_data, *os_data + *os_len);
443
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100444 return (void *)img_addr;
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100445}
446
wdenk0d498392003-07-01 21:06:45 +0000447U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100448 bootm, CFG_MAXARGS, 1, do_bootm,
449 "bootm - boot application image from memory\n",
450 "[addr [arg ...]]\n - boot application image stored in memory\n"
451 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
452 "\t'arg' can be the address of an initrd image\n"
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100453#if defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500454 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
Detlev Zundel5441f612007-10-19 16:47:26 +0200455 "\ta third argument is required which is the address of the\n"
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500456 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
457 "\tuse a '-' for the second argument. If you do not pass a third\n"
458 "\ta bd_info struct will be passed instead\n"
459#endif
wdenk8bde7f72003-06-27 21:31:46 +0000460);
461
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100462/*******************************************************************/
463/* bootd - boot default image */
464/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500465#if defined(CONFIG_CMD_BOOTD)
wdenk47d1a6e2002-11-03 00:01:44 +0000466int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
467{
468 int rcode = 0;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100469
wdenk47d1a6e2002-11-03 00:01:44 +0000470#ifndef CFG_HUSH_PARSER
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100471 if (run_command (getenv ("bootcmd"), flag) < 0)
472 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000473#else
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100474 if (parse_string_outer (getenv ("bootcmd"),
475 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
476 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000477#endif
478 return rcode;
479}
wdenk8bde7f72003-06-27 21:31:46 +0000480
wdenk0d498392003-07-01 21:06:45 +0000481U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100482 boot, 1, 1, do_bootd,
483 "boot - boot default, i.e., run 'bootcmd'\n",
wdenk9d2b18a2003-06-28 23:11:04 +0000484 NULL
485);
486
487/* keep old command name "bootd" for backward compatibility */
wdenk0d498392003-07-01 21:06:45 +0000488U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100489 bootd, 1, 1, do_bootd,
490 "bootd - boot default, i.e., run 'bootcmd'\n",
wdenk8bde7f72003-06-27 21:31:46 +0000491 NULL
492);
493
wdenk47d1a6e2002-11-03 00:01:44 +0000494#endif
495
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100496
497/*******************************************************************/
498/* iminfo - print header info for a requested image */
499/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500500#if defined(CONFIG_CMD_IMI)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100501int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000502{
503 int arg;
504 ulong addr;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100505 int rcode = 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000506
507 if (argc < 2) {
508 return image_info (load_addr);
509 }
510
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100511 for (arg = 1; arg < argc; ++arg) {
512 addr = simple_strtoul (argv[arg], NULL, 16);
513 if (image_info (addr) != 0)
514 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000515 }
516 return rcode;
517}
518
519static int image_info (ulong addr)
520{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100521 void *hdr = (void *)addr;
wdenk47d1a6e2002-11-03 00:01:44 +0000522
523 printf ("\n## Checking Image at %08lx ...\n", addr);
524
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100525 switch (gen_image_get_format (hdr)) {
526 case IMAGE_FORMAT_LEGACY:
527 puts (" Legacy image found\n");
528 if (!image_check_magic (hdr)) {
529 puts (" Bad Magic Number\n");
530 return 1;
531 }
532
533 if (!image_check_hcrc (hdr)) {
534 puts (" Bad Header Checksum\n");
535 return 1;
536 }
537
538 image_print_contents (hdr);
539
540 puts (" Verifying Checksum ... ");
541 if (!image_check_dcrc (hdr)) {
542 puts (" Bad Data CRC\n");
543 return 1;
544 }
545 puts ("OK\n");
546 return 0;
547#if defined(CONFIG_FIT)
548 case IMAGE_FORMAT_FIT:
549 puts (" FIT image found\n");
550 fit_unsupported ("iminfo");
551 return 0;
552#endif
553 default:
554 puts ("Unknown image format!\n");
555 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000556 }
557
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100558 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000559}
wdenk0d498392003-07-01 21:06:45 +0000560
561U_BOOT_CMD(
562 iminfo, CFG_MAXARGS, 1, do_iminfo,
wdenk8bde7f72003-06-27 21:31:46 +0000563 "iminfo - print header information for application image\n",
564 "addr [addr ...]\n"
565 " - print header information for application image starting at\n"
566 " address 'addr' in memory; this includes verification of the\n"
567 " image contents (magic number, header and payload checksums)\n"
568);
Jon Loeliger90253172007-07-10 11:02:44 -0500569#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000570
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100571
572/*******************************************************************/
573/* imls - list all images found in flash */
574/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500575#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +0000576int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
577{
578 flash_info_t *info;
579 int i, j;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100580 void *hdr;
wdenk27b207f2003-07-24 23:38:38 +0000581
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100582 for (i = 0, info = &flash_info[0];
583 i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
584
wdenk27b207f2003-07-24 23:38:38 +0000585 if (info->flash_id == FLASH_UNKNOWN)
586 goto next_bank;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100587 for (j = 0; j < info->sector_count; ++j) {
wdenk27b207f2003-07-24 23:38:38 +0000588
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100589 hdr = (void *)info->start[j];
590 if (!hdr)
wdenk27b207f2003-07-24 23:38:38 +0000591 goto next_sector;
592
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100593 switch (gen_image_get_format (hdr)) {
594 case IMAGE_FORMAT_LEGACY:
595 if (!image_check_magic (hdr))
596 goto next_sector;
597
598 if (!image_check_hcrc (hdr))
599 goto next_sector;
600
601 printf ("Legacy Image at %08lX:\n", (ulong)hdr);
602 image_print_contents (hdr);
603
604 puts (" Verifying Checksum ... ");
605 if (!image_check_dcrc (hdr)) {
606 puts ("Bad Data CRC\n");
607 } else {
608 puts ("OK\n");
609 }
610 break;
611#if defined(CONFIG_FIT)
612 case IMAGE_FORMAT_FIT:
613 printf ("FIT Image at %08lX:\n", (ulong)hdr);
614 fit_unsupported ("imls");
615 break;
616#endif
617 default:
wdenk27b207f2003-07-24 23:38:38 +0000618 goto next_sector;
wdenk5bb226e2003-11-17 21:14:37 +0000619 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100620
wdenkbdccc4f2003-08-05 17:43:17 +0000621next_sector: ;
wdenk27b207f2003-07-24 23:38:38 +0000622 }
wdenkbdccc4f2003-08-05 17:43:17 +0000623next_bank: ;
wdenk27b207f2003-07-24 23:38:38 +0000624 }
625
626 return (0);
627}
628
629U_BOOT_CMD(
630 imls, 1, 1, do_imls,
631 "imls - list all images found in flash\n",
632 "\n"
633 " - Prints information about all images found at sector\n"
634 " boundaries in flash.\n"
635);
Jon Loeliger90253172007-07-10 11:02:44 -0500636#endif
wdenk27b207f2003-07-24 23:38:38 +0000637
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100638/*******************************************************************/
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100639/* helper routines */
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100640/*******************************************************************/
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100641#ifdef CONFIG_SILENT_CONSOLE
642static void fixup_silent_linux ()
643{
644 char buf[256], *start, *end;
645 char *cmdline = getenv ("bootargs");
646
647 /* Only fix cmdline when requested */
648 if (!(gd->flags & GD_FLG_SILENT))
649 return;
650
651 debug ("before silent fix-up: %s\n", cmdline);
652 if (cmdline) {
653 if ((start = strstr (cmdline, "console=")) != NULL) {
654 end = strchr (start, ' ');
655 strncpy (buf, cmdline, (start - cmdline + 8));
656 if (end)
657 strcpy (buf + (start - cmdline + 8), end);
658 else
659 buf[start - cmdline + 8] = '\0';
660 } else {
661 strcpy (buf, cmdline);
662 strcat (buf, " console=");
663 }
664 } else {
665 strcpy (buf, "console=");
666 }
667
668 setenv ("bootargs", buf);
669 debug ("after silent fix-up: %s\n", buf);
670}
671#endif /* CONFIG_SILENT_CONSOLE */
672
673
674/*******************************************************************/
675/* OS booting routines */
676/*******************************************************************/
677
678static void do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100679 int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100680 bootm_headers_t *images)
wdenkd791b1d2003-04-20 14:04:18 +0000681{
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100682 void (*loader)(bd_t *, image_header_t *, char *, char *);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100683 image_header_t *os_hdr, *hdr;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100684 ulong kernel_data, kernel_len;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100685 char *consdev;
686 char *cmdline;
687
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100688#if defined(CONFIG_FIT)
689 if (!images->legacy_hdr_valid) {
690 fit_unsupported_reset ("NetBSD");
691 do_reset (cmdtp, flag, argc, argv);
692 }
693#endif
694 hdr = images->legacy_hdr_os;
695
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100696 /*
697 * Booting a (NetBSD) kernel image
698 *
699 * This process is pretty similar to a standalone application:
700 * The (first part of an multi-) image must be a stage-2 loader,
701 * which in turn is responsible for loading & invoking the actual
702 * kernel. The only differences are the parameters being passed:
703 * besides the board info strucure, the loader expects a command
704 * line, the name of the console device, and (optionally) the
705 * address of the original image header.
706 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100707 os_hdr = NULL;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100708 if (image_check_type (hdr, IH_TYPE_MULTI)) {
709 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
710 if (kernel_len)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100711 os_hdr = hdr;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100712 }
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100713
714 consdev = "";
715#if defined (CONFIG_8xx_CONS_SMC1)
716 consdev = "smc1";
717#elif defined (CONFIG_8xx_CONS_SMC2)
718 consdev = "smc2";
719#elif defined (CONFIG_8xx_CONS_SCC2)
720 consdev = "scc2";
721#elif defined (CONFIG_8xx_CONS_SCC3)
722 consdev = "scc3";
723#endif
724
725 if (argc > 2) {
726 ulong len;
727 int i;
728
729 for (i = 2, len = 0; i < argc; i += 1)
730 len += strlen (argv[i]) + 1;
731 cmdline = malloc (len);
732
733 for (i = 2, len = 0; i < argc; i += 1) {
734 if (i > 2)
735 cmdline[len++] = ' ';
736 strcpy (&cmdline[len], argv[i]);
737 len += strlen (argv[i]);
738 }
739 } else if ((cmdline = getenv ("bootargs")) == NULL) {
740 cmdline = "";
741 }
742
743 loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
744
745 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
746 (ulong)loader);
747
748 show_boot_progress (15);
749
750 /*
751 * NetBSD Stage-2 Loader Parameters:
752 * r3: ptr to board info data
753 * r4: image address
754 * r5: console device
755 * r6: boot args string
756 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100757 (*loader) (gd->bd, os_hdr, consdev, cmdline);
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100758}
759
760#ifdef CONFIG_LYNXKDI
761static void do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
762 int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100763 bootm_headers_t *images)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100764{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100765 image_header_t *hdr = images->legacy_hdr_os;
766
767#if defined(CONFIG_FIT)
768 if (!images->legacy_hdr_valid) {
769 fit_unsupported_reset ("Lynx");
770 do_reset (cmdtp, flag, argc, argv);
771 }
772#endif
773
774 lynxkdi_boot ((image_header_t *)hdr);
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100775}
776#endif /* CONFIG_LYNXKDI */
777
778static void do_bootm_rtems (cmd_tbl_t *cmdtp, int flag,
779 int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100780 bootm_headers_t *images)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100781{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100782 image_header_t *hdr = images->legacy_hdr_os;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100783 void (*entry_point)(bd_t *);
wdenkd791b1d2003-04-20 14:04:18 +0000784
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100785#if defined(CONFIG_FIT)
786 if (!images->legacy_hdr_valid) {
787 fit_unsupported_reset ("RTEMS");
788 do_reset (cmdtp, flag, argc, argv);
789 }
790#endif
791
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100792 entry_point = (void (*)(bd_t *))image_get_ep (hdr);
wdenkd791b1d2003-04-20 14:04:18 +0000793
794 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
795 (ulong)entry_point);
796
Heiko Schocherfad63402007-07-13 09:54:17 +0200797 show_boot_progress (15);
wdenkd791b1d2003-04-20 14:04:18 +0000798
799 /*
800 * RTEMS Parameters:
801 * r3: ptr to board info data
802 */
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100803 (*entry_point)(gd->bd);
wdenkd791b1d2003-04-20 14:04:18 +0000804}
805
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500806#if defined(CONFIG_CMD_ELF)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100807static void do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag,
808 int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100809 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +0000810{
wdenk47d1a6e2002-11-03 00:01:44 +0000811 char str[80];
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100812 image_header_t *hdr = images->legacy_hdr_os;
813
814#if defined(CONFIG_FIT)
815 if (hdr == NULL) {
816 fit_unsupported_reset ("VxWorks");
817 do_reset (cmdtp, flag, argc, argv);
818 }
819#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000820
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100821 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +0000822 setenv("loadaddr", str);
823 do_bootvx(cmdtp, 0, 0, NULL);
824}
825
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100826static void do_bootm_qnxelf(cmd_tbl_t *cmdtp, int flag,
827 int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100828 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +0000829{
wdenk47d1a6e2002-11-03 00:01:44 +0000830 char *local_args[2];
831 char str[16];
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100832 image_header_t *hdr = images->legacy_hdr_os;
833
834#if defined(CONFIG_FIT)
835 if (!images->legacy_hdr_valid) {
836 fit_unsupported_reset ("QNX");
837 do_reset (cmdtp, flag, argc, argv);
838 }
839#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000840
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100841 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +0000842 local_args[0] = argv[0];
843 local_args[1] = str; /* and provide it via the arguments */
844 do_bootelf(cmdtp, 0, 2, local_args);
845}
Jon Loeliger90253172007-07-10 11:02:44 -0500846#endif
wdenk1f4bb372003-07-27 00:21:01 +0000847
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100848#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
849static void do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
850 int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100851 bootm_headers_t *images)
wdenk1f4bb372003-07-27 00:21:01 +0000852{
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100853 ulong top;
854 char *s, *cmdline;
855 char **fwenv, **ss;
856 int i, j, nxt, len, envno, envsz;
857 bd_t *kbd;
858 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100859 image_header_t *hdr = images->legacy_hdr_os;
860
861#if defined(CONFIG_FIT)
862 if (!images->legacy_hdr_valid) {
863 fit_unsupported_reset ("ARTOS");
864 do_reset (cmdtp, flag, argc, argv);
865 }
866#endif
wdenk1f4bb372003-07-27 00:21:01 +0000867
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100868 /*
869 * Booting an ARTOS kernel image + application
870 */
871
872 /* this used to be the top of memory, but was wrong... */
873#ifdef CONFIG_PPC
874 /* get stack pointer */
875 asm volatile ("mr %0,1" : "=r"(top) );
876#endif
877 debug ("## Current stack ends at 0x%08lX ", top);
878
879 top -= 2048; /* just to be sure */
880 if (top > CFG_BOOTMAPSZ)
881 top = CFG_BOOTMAPSZ;
882 top &= ~0xF;
883
884 debug ("=> set upper limit to 0x%08lX\n", top);
885
886 /* first check the artos specific boot args, then the linux args*/
887 if ((s = getenv( "abootargs")) == NULL && (s = getenv ("bootargs")) == NULL)
888 s = "";
889
890 /* get length of cmdline, and place it */
891 len = strlen (s);
892 top = (top - (len + 1)) & ~0xF;
893 cmdline = (char *)top;
894 debug ("## cmdline at 0x%08lX ", top);
895 strcpy (cmdline, s);
896
897 /* copy bdinfo */
898 top = (top - sizeof (bd_t)) & ~0xF;
899 debug ("## bd at 0x%08lX ", top);
900 kbd = (bd_t *)top;
901 memcpy (kbd, gd->bd, sizeof (bd_t));
902
903 /* first find number of env entries, and their size */
904 envno = 0;
905 envsz = 0;
906 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
907 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
908 ;
909 envno++;
910 envsz += (nxt - i) + 1; /* plus trailing zero */
911 }
912 envno++; /* plus the terminating zero */
913 debug ("## %u envvars total size %u ", envno, envsz);
914
915 top = (top - sizeof (char **) * envno) & ~0xF;
916 fwenv = (char **)top;
917 debug ("## fwenv at 0x%08lX ", top);
918
919 top = (top - envsz) & ~0xF;
920 s = (char *)top;
921 ss = fwenv;
922
923 /* now copy them */
924 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
925 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
926 ;
927 *ss++ = s;
928 for (j = i; j < nxt; ++j)
929 *s++ = env_get_char (j);
930 *s++ = '\0';
931 }
932 *ss++ = NULL; /* terminate */
933
934 entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
935 (*entry) (kbd, cmdline, fwenv, top);
936}
937#endif