blob: 3f099888f2b4ccc4822d2af4840006783862dafe [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>
wdenk47d1a6e2002-11-03 00:01:44 +000037#include <asm/byteorder.h>
wdenk8bde7f72003-06-27 21:31:46 +000038
wdenk47d1a6e2002-11-03 00:01:44 +000039#ifdef CFG_HUSH_PARSER
40#include <hush.h>
41#endif
42
Marian Balakowicz1ee11802008-01-08 18:17:10 +010043DECLARE_GLOBAL_DATA_PTR;
44
45extern int gunzip (void *dst, int dstlen, unsigned char *src, unsigned long *lenp);
46#ifndef CFG_BOOTM_LEN
47#define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
48#endif
wdenk47d1a6e2002-11-03 00:01:44 +000049
Marian Balakowicz321359f2008-01-08 18:11:43 +010050#ifdef CONFIG_BZIP2
51extern void bz_internal_error(int);
52#endif
wdenk47d1a6e2002-11-03 00:01:44 +000053
Jon Loeligerbaa26db2007-07-08 17:51:39 -050054#if defined(CONFIG_CMD_IMI)
wdenk47d1a6e2002-11-03 00:01:44 +000055static int image_info (unsigned long addr);
56#endif
wdenk27b207f2003-07-24 23:38:38 +000057
Jon Loeligerbaa26db2007-07-08 17:51:39 -050058#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +000059#include <flash.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020060extern flash_info_t flash_info[]; /* info for FLASH chips */
wdenk27b207f2003-07-24 23:38:38 +000061static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
62#endif
63
Marian Balakowicz1ee11802008-01-08 18:17:10 +010064#ifdef CONFIG_SILENT_CONSOLE
65static void fixup_silent_linux (void);
66#endif
67
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010068static void *get_kernel (cmd_tbl_t *cmdtp, int flag,
Marian Balakowicz5cf746c2008-01-31 13:59:09 +010069 int argc, char *argv[], int verify,
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010070 bootm_headers_t *images,
Marian Balakowicz5cf746c2008-01-31 13:59:09 +010071 ulong *os_data, ulong *os_len);
Marian Balakowicz1ee11802008-01-08 18:17:10 +010072extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
wdenk47d1a6e2002-11-03 00:01:44 +000073
74/*
75 * Continue booting an OS image; caller already has:
76 * - copied image header to global variable `header'
77 * - checked header magic number, checksums (both header & image),
78 * - verified image architecture (PPC) and type (KERNEL or MULTI),
79 * - loaded (first part of) image to header load address,
80 * - disabled interrupts.
81 */
Marian Balakowicz1ee11802008-01-08 18:17:10 +010082typedef void boot_os_fn (cmd_tbl_t *cmdtp, int flag,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010083 int argc, char *argv[],
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010084 bootm_headers_t *images,/* pointers to os/initrd/fdt */
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010085 int verify); /* getenv("verify")[0] != 'n' */
wdenk47d1a6e2002-11-03 00:01:44 +000086
Marian Balakowicz1ee11802008-01-08 18:17:10 +010087extern boot_os_fn do_bootm_linux;
88static boot_os_fn do_bootm_netbsd;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010089#if defined(CONFIG_LYNXKDI)
Marian Balakowicz1ee11802008-01-08 18:17:10 +010090static boot_os_fn do_bootm_lynxkdi;
91extern void lynxkdi_boot (image_header_t *);
wdenkf72da342003-10-10 10:05:42 +000092#endif
Marian Balakowicz1ee11802008-01-08 18:17:10 +010093static boot_os_fn do_bootm_rtems;
Jon Loeligerbaa26db2007-07-08 17:51:39 -050094#if defined(CONFIG_CMD_ELF)
Marian Balakowicz1ee11802008-01-08 18:17:10 +010095static boot_os_fn do_bootm_vxworks;
96static boot_os_fn do_bootm_qnxelf;
97int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
98int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
Jon Loeliger90253172007-07-10 11:02:44 -050099#endif
wdenk7f70e852003-05-20 14:25:27 +0000100#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100101extern uchar (*env_get_char)(int); /* Returns a character from the environment */
102static boot_os_fn do_bootm_artos;
Stefan Roese15940c92006-03-13 11:16:36 +0100103#endif
104
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100105ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100106static bootm_headers_t images; /* pointers to os/initrd/fdt images */
wdenk47d1a6e2002-11-03 00:01:44 +0000107
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100108
109/*******************************************************************/
110/* bootm - boot application image from image in memory */
111/*******************************************************************/
wdenk47d1a6e2002-11-03 00:01:44 +0000112int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
113{
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100114 ulong iflag;
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100115 const char *type_name;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100116 uint unc_len = CFG_BOOTM_LEN;
117 int verify = getenv_verify();
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100118 uint8_t comp, type, os;
wdenk47d1a6e2002-11-03 00:01:44 +0000119
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100120 void *os_hdr;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100121 ulong os_data, os_len;
Marian Balakowicz75824382008-01-31 13:20:06 +0100122 ulong image_start, image_end;
123 ulong load_start, load_end;
124
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100125 memset ((void *)&images, 0, sizeof (images));
126
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100127 /* get kernel image header, start address and length */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100128 os_hdr = get_kernel (cmdtp, flag, argc, argv, verify,
129 &images, &os_data, &os_len);
130 if (os_len == 0)
wdenk47d1a6e2002-11-03 00:01:44 +0000131 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000132
Heiko Schocherfad63402007-07-13 09:54:17 +0200133 show_boot_progress (6);
wdenk47d1a6e2002-11-03 00:01:44 +0000134
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100135 /* get image parameters */
136 switch (gen_image_get_format (os_hdr)) {
137 case IMAGE_FORMAT_LEGACY:
138 type = image_get_type (os_hdr);
139 comp = image_get_comp (os_hdr);
140 os = image_get_os (os_hdr);
141
142 image_end = image_get_image_end (os_hdr);
143 load_start = image_get_load (os_hdr);
144 break;
145#if defined(CONFIG_FIT)
146 case IMAGE_FORMAT_FIT:
147 fit_unsupported ("bootm");
148 return 1;
149#endif
150 default:
151 puts ("ERROR: unknown image format type!\n");
152 return 1;
153 }
154
155 image_start = (ulong)os_hdr;
156 load_end = 0;
157 type_name = image_get_type_name (type);
158
wdenk47d1a6e2002-11-03 00:01:44 +0000159 /*
160 * We have reached the point of no return: we are going to
161 * overwrite all exception vector code, so we cannot easily
162 * recover from any failures any more...
163 */
wdenk47d1a6e2002-11-03 00:01:44 +0000164 iflag = disable_interrupts();
165
wdenkc7de8292002-11-19 11:04:11 +0000166#ifdef CONFIG_AMIGAONEG3SE
167 /*
wdenk8bde7f72003-06-27 21:31:46 +0000168 * We've possible left the caches enabled during
wdenkc7de8292002-11-19 11:04:11 +0000169 * bios emulation, so turn them off again
170 */
171 icache_disable();
172 invalidate_l1_instruction_cache();
173 flush_data_cache();
174 dcache_disable();
175#endif
176
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100177 switch (comp) {
wdenk47d1a6e2002-11-03 00:01:44 +0000178 case IH_COMP_NONE:
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100179 if (load_start == (ulong)os_hdr) {
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100180 printf (" XIP %s ... ", type_name);
wdenk47d1a6e2002-11-03 00:01:44 +0000181 } else {
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100182 printf (" Loading %s ... ", type_name);
wdenk47d1a6e2002-11-03 00:01:44 +0000183
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100184 memmove_wd ((void *)load_start,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100185 (void *)os_data, os_len, CHUNKSZ);
Marian Balakowiczaf13cdb2008-01-08 18:11:45 +0100186
Marian Balakowicz75824382008-01-31 13:20:06 +0100187 load_end = load_start + os_len;
Marian Balakowiczaf13cdb2008-01-08 18:11:45 +0100188 puts("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000189 }
190 break;
191 case IH_COMP_GZIP:
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100192 printf (" Uncompressing %s ... ", type_name);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100193 if (gunzip ((void *)load_start, unc_len,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100194 (uchar *)os_data, &os_len) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000195 puts ("GUNZIP ERROR - must RESET board to recover\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200196 show_boot_progress (-6);
wdenk47d1a6e2002-11-03 00:01:44 +0000197 do_reset (cmdtp, flag, argc, argv);
198 }
Marian Balakowicz75824382008-01-31 13:20:06 +0100199
200 load_end = load_start + os_len;
wdenk47d1a6e2002-11-03 00:01:44 +0000201 break;
wdenkc29fdfc2003-08-29 20:57:53 +0000202#ifdef CONFIG_BZIP2
203 case IH_COMP_BZIP2:
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100204 printf (" Uncompressing %s ... ", type_name);
wdenk5653fc32004-02-08 22:55:38 +0000205 /*
206 * If we've got less than 4 MB of malloc() space,
207 * use slower decompression algorithm which requires
208 * at most 2300 KB of memory.
209 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100210 int i = BZ2_bzBuffToBuffDecompress ((char*)load_start,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100211 &unc_len, (char *)os_data, os_len,
212 CFG_MALLOC_LEN < (4096 * 1024), 0);
wdenkc29fdfc2003-08-29 20:57:53 +0000213 if (i != BZ_OK) {
214 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
Heiko Schocherfad63402007-07-13 09:54:17 +0200215 show_boot_progress (-6);
wdenkc29fdfc2003-08-29 20:57:53 +0000216 do_reset (cmdtp, flag, argc, argv);
217 }
Marian Balakowicz75824382008-01-31 13:20:06 +0100218
219 load_end = load_start + unc_len;
wdenkc29fdfc2003-08-29 20:57:53 +0000220 break;
221#endif /* CONFIG_BZIP2 */
wdenk47d1a6e2002-11-03 00:01:44 +0000222 default:
223 if (iflag)
224 enable_interrupts();
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100225 printf ("Unimplemented compression type %d\n", comp);
Heiko Schocherfad63402007-07-13 09:54:17 +0200226 show_boot_progress (-7);
wdenk47d1a6e2002-11-03 00:01:44 +0000227 return 1;
228 }
wdenk4b9206e2004-03-23 22:14:11 +0000229 puts ("OK\n");
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100230 debug (" kernel loaded at 0x%08lx, end = 0x%08lx\n", load_start, load_end);
Heiko Schocherfad63402007-07-13 09:54:17 +0200231 show_boot_progress (7);
wdenk47d1a6e2002-11-03 00:01:44 +0000232
Marian Balakowicz75824382008-01-31 13:20:06 +0100233 if ((load_start < image_end) && (load_end > image_start)) {
234 debug ("image_start = 0x%lX, image_end = 0x%lx\n", image_start, image_end);
235 debug ("load_start = 0x%lx, load_end = 0x%lx\n", load_start, load_end);
236
237 puts ("ERROR: image overwritten - must RESET the board to recover.\n");
238 do_reset (cmdtp, flag, argc, argv);
239 }
240
Heiko Schocherfad63402007-07-13 09:54:17 +0200241 show_boot_progress (8);
wdenk47d1a6e2002-11-03 00:01:44 +0000242
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100243 switch (os) {
wdenk47d1a6e2002-11-03 00:01:44 +0000244 default: /* handled by (original) Linux case */
245 case IH_OS_LINUX:
wdenkf72da342003-10-10 10:05:42 +0000246#ifdef CONFIG_SILENT_CONSOLE
247 fixup_silent_linux();
248#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100249 do_bootm_linux (cmdtp, flag, argc, argv, &images, verify);
wdenk47d1a6e2002-11-03 00:01:44 +0000250 break;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100251
wdenk47d1a6e2002-11-03 00:01:44 +0000252 case IH_OS_NETBSD:
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100253 do_bootm_netbsd (cmdtp, flag, argc, argv, &images, verify);
wdenk47d1a6e2002-11-03 00:01:44 +0000254 break;
wdenk8bde7f72003-06-27 21:31:46 +0000255
wdenk1f4bb372003-07-27 00:21:01 +0000256#ifdef CONFIG_LYNXKDI
257 case IH_OS_LYNXOS:
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100258 do_bootm_lynxkdi (cmdtp, flag, argc, argv, &images, verify);
wdenk1f4bb372003-07-27 00:21:01 +0000259 break;
260#endif
261
wdenkd791b1d2003-04-20 14:04:18 +0000262 case IH_OS_RTEMS:
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100263 do_bootm_rtems (cmdtp, flag, argc, argv, &images, verify);
wdenkd791b1d2003-04-20 14:04:18 +0000264 break;
wdenk8bde7f72003-06-27 21:31:46 +0000265
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500266#if defined(CONFIG_CMD_ELF)
wdenk47d1a6e2002-11-03 00:01:44 +0000267 case IH_OS_VXWORKS:
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100268 do_bootm_vxworks (cmdtp, flag, argc, argv, &images, verify);
wdenk47d1a6e2002-11-03 00:01:44 +0000269 break;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100270
wdenk47d1a6e2002-11-03 00:01:44 +0000271 case IH_OS_QNX:
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100272 do_bootm_qnxelf (cmdtp, flag, argc, argv, &images, verify);
wdenk47d1a6e2002-11-03 00:01:44 +0000273 break;
Jon Loeliger90253172007-07-10 11:02:44 -0500274#endif
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100275
wdenk7f70e852003-05-20 14:25:27 +0000276#ifdef CONFIG_ARTOS
277 case IH_OS_ARTOS:
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100278 do_bootm_artos (cmdtp, flag, argc, argv, &images, verify);
wdenk7f70e852003-05-20 14:25:27 +0000279 break;
280#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000281 }
282
Heiko Schocherfad63402007-07-13 09:54:17 +0200283 show_boot_progress (-9);
wdenk47d1a6e2002-11-03 00:01:44 +0000284#ifdef DEBUG
wdenk4b9206e2004-03-23 22:14:11 +0000285 puts ("\n## Control returned to monitor - resetting...\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000286 do_reset (cmdtp, flag, argc, argv);
287#endif
288 return 1;
289}
290
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100291/**
292 * get_kernel - find kernel image
293 * @os_data: pointer to a ulong variable, will hold os data start address
294 * @os_len: pointer to a ulong variable, will hold os data length
295 *
296 * get_kernel() tries to find a kernel image, verifies its integrity
297 * and locates kernel data.
298 *
299 * returns:
300 * pointer to image header if valid image was found, plus kernel start
301 * address and length, otherwise NULL
302 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100303static void *get_kernel (cmd_tbl_t *cmdtp, int flag,
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100304 int argc, char *argv[], int verify,
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100305 bootm_headers_t *images,
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100306 ulong *os_data, ulong *os_len)
307{
308 image_header_t *hdr;
309 ulong img_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100310#if defined(CONFIG_FIT)
311 void *fit_hdr;
312 const char *fit_uname_config = NULL;
313 const char *fit_uname_kernel = NULL;
314#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100315
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100316 /* find out kernel image address */
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100317 if (argc < 2) {
318 img_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100319 debug ("* kernel: default image load address = 0x%08lx\n",
320 load_addr);
321#if defined(CONFIG_FIT)
322 } else if (fit_parse_conf (argv[1], load_addr, &img_addr,
323 &fit_uname_config)) {
324 debug ("* kernel: config '%s' from image at 0x%08lx\n",
325 fit_uname_config, img_addr);
326 } else if (fit_parse_subimage (argv[1], load_addr, &img_addr,
327 &fit_uname_kernel)) {
328 debug ("* kernel: subimage '%s' from image at 0x%08lx\n",
329 fit_uname_kernel, img_addr);
330#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100331 } else {
332 img_addr = simple_strtoul(argv[1], NULL, 16);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100333 debug ("* kernel: cmdline image address = 0x%08lx\n", img_addr);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100334 }
335
336 show_boot_progress (1);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100337 printf ("## Booting kernel image at %08lx ...\n", img_addr);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100338
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100339 /* copy from dataflash if needed */
340 img_addr = gen_get_image (img_addr);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100341
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100342 /* check image type, for FIT images get FIT kernel node */
343 switch (gen_image_get_format ((void *)img_addr)) {
344 case IMAGE_FORMAT_LEGACY:
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100345
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100346 debug ("* kernel: legacy format image\n");
347 hdr = (image_header_t *)img_addr;
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100348
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100349 if (!image_check_magic(hdr)) {
350 puts ("Bad Magic Number\n");
351 show_boot_progress (-1);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100352 return NULL;
353 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100354 show_boot_progress (2);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100355
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100356 if (!image_check_hcrc (hdr)) {
357 puts ("Bad Header Checksum\n");
358 show_boot_progress (-2);
359 return NULL;
360 }
361
362 show_boot_progress (3);
363 image_print_contents (hdr);
364
365 if (verify) {
366 puts (" Verifying Checksum ... ");
367 if (!image_check_dcrc (hdr)) {
368 printf ("Bad Data CRC\n");
369 show_boot_progress (-3);
370 return NULL;
371 }
372 puts ("OK\n");
373 }
374 show_boot_progress (4);
375
376 if (!image_check_target_arch (hdr)) {
377 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
378 show_boot_progress (-4);
379 return NULL;
380 }
381 show_boot_progress (5);
382
383 switch (image_get_type (hdr)) {
384 case IH_TYPE_KERNEL:
385 *os_data = image_get_data (hdr);
386 *os_len = image_get_data_size (hdr);
387 break;
388 case IH_TYPE_MULTI:
389 image_multi_getimg (hdr, 0, os_data, os_len);
390 break;
391 default:
392 printf ("Wrong Image Type for %s command\n", cmdtp->name);
393 show_boot_progress (-5);
394 return NULL;
395 }
396 images->legacy_hdr_os = hdr;
397 images->legacy_hdr_valid = 1;
398
399 break;
400#if defined(CONFIG_FIT)
401 case IMAGE_FORMAT_FIT:
402 fit_hdr = (void *)img_addr;
403 debug ("* kernel: FIT format image\n");
404 fit_unsupported ("kernel");
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100405 return NULL;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100406#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100407 default:
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100408 printf ("Wrong Image Format for %s command\n", cmdtp->name);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100409 return NULL;
410 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100411
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100412 debug (" kernel data at 0x%08lx, end = 0x%08lx\n",
413 *os_data, *os_data + *os_len);
414
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100415 return (void *)img_addr;
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100416}
417
wdenk0d498392003-07-01 21:06:45 +0000418U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100419 bootm, CFG_MAXARGS, 1, do_bootm,
420 "bootm - boot application image from memory\n",
421 "[addr [arg ...]]\n - boot application image stored in memory\n"
422 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
423 "\t'arg' can be the address of an initrd image\n"
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100424#if defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500425 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
Detlev Zundel5441f612007-10-19 16:47:26 +0200426 "\ta third argument is required which is the address of the\n"
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500427 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
428 "\tuse a '-' for the second argument. If you do not pass a third\n"
429 "\ta bd_info struct will be passed instead\n"
430#endif
wdenk8bde7f72003-06-27 21:31:46 +0000431);
432
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100433/*******************************************************************/
434/* bootd - boot default image */
435/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500436#if defined(CONFIG_CMD_BOOTD)
wdenk47d1a6e2002-11-03 00:01:44 +0000437int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
438{
439 int rcode = 0;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100440
wdenk47d1a6e2002-11-03 00:01:44 +0000441#ifndef CFG_HUSH_PARSER
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100442 if (run_command (getenv ("bootcmd"), flag) < 0)
443 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000444#else
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100445 if (parse_string_outer (getenv ("bootcmd"),
446 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
447 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000448#endif
449 return rcode;
450}
wdenk8bde7f72003-06-27 21:31:46 +0000451
wdenk0d498392003-07-01 21:06:45 +0000452U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100453 boot, 1, 1, do_bootd,
454 "boot - boot default, i.e., run 'bootcmd'\n",
wdenk9d2b18a2003-06-28 23:11:04 +0000455 NULL
456);
457
458/* keep old command name "bootd" for backward compatibility */
wdenk0d498392003-07-01 21:06:45 +0000459U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100460 bootd, 1, 1, do_bootd,
461 "bootd - boot default, i.e., run 'bootcmd'\n",
wdenk8bde7f72003-06-27 21:31:46 +0000462 NULL
463);
464
wdenk47d1a6e2002-11-03 00:01:44 +0000465#endif
466
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100467
468/*******************************************************************/
469/* iminfo - print header info for a requested image */
470/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500471#if defined(CONFIG_CMD_IMI)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100472int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000473{
474 int arg;
475 ulong addr;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100476 int rcode = 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000477
478 if (argc < 2) {
479 return image_info (load_addr);
480 }
481
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100482 for (arg = 1; arg < argc; ++arg) {
483 addr = simple_strtoul (argv[arg], NULL, 16);
484 if (image_info (addr) != 0)
485 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000486 }
487 return rcode;
488}
489
490static int image_info (ulong addr)
491{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100492 void *hdr = (void *)addr;
wdenk47d1a6e2002-11-03 00:01:44 +0000493
494 printf ("\n## Checking Image at %08lx ...\n", addr);
495
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100496 switch (gen_image_get_format (hdr)) {
497 case IMAGE_FORMAT_LEGACY:
498 puts (" Legacy image found\n");
499 if (!image_check_magic (hdr)) {
500 puts (" Bad Magic Number\n");
501 return 1;
502 }
503
504 if (!image_check_hcrc (hdr)) {
505 puts (" Bad Header Checksum\n");
506 return 1;
507 }
508
509 image_print_contents (hdr);
510
511 puts (" Verifying Checksum ... ");
512 if (!image_check_dcrc (hdr)) {
513 puts (" Bad Data CRC\n");
514 return 1;
515 }
516 puts ("OK\n");
517 return 0;
518#if defined(CONFIG_FIT)
519 case IMAGE_FORMAT_FIT:
520 puts (" FIT image found\n");
521 fit_unsupported ("iminfo");
522 return 0;
523#endif
524 default:
525 puts ("Unknown image format!\n");
526 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000527 }
528
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100529 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000530}
wdenk0d498392003-07-01 21:06:45 +0000531
532U_BOOT_CMD(
533 iminfo, CFG_MAXARGS, 1, do_iminfo,
wdenk8bde7f72003-06-27 21:31:46 +0000534 "iminfo - print header information for application image\n",
535 "addr [addr ...]\n"
536 " - print header information for application image starting at\n"
537 " address 'addr' in memory; this includes verification of the\n"
538 " image contents (magic number, header and payload checksums)\n"
539);
Jon Loeliger90253172007-07-10 11:02:44 -0500540#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000541
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100542
543/*******************************************************************/
544/* imls - list all images found in flash */
545/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500546#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +0000547int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
548{
549 flash_info_t *info;
550 int i, j;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100551 void *hdr;
wdenk27b207f2003-07-24 23:38:38 +0000552
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100553 for (i = 0, info = &flash_info[0];
554 i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
555
wdenk27b207f2003-07-24 23:38:38 +0000556 if (info->flash_id == FLASH_UNKNOWN)
557 goto next_bank;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100558 for (j = 0; j < info->sector_count; ++j) {
wdenk27b207f2003-07-24 23:38:38 +0000559
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100560 hdr = (void *)info->start[j];
561 if (!hdr)
wdenk27b207f2003-07-24 23:38:38 +0000562 goto next_sector;
563
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100564 switch (gen_image_get_format (hdr)) {
565 case IMAGE_FORMAT_LEGACY:
566 if (!image_check_magic (hdr))
567 goto next_sector;
568
569 if (!image_check_hcrc (hdr))
570 goto next_sector;
571
572 printf ("Legacy Image at %08lX:\n", (ulong)hdr);
573 image_print_contents (hdr);
574
575 puts (" Verifying Checksum ... ");
576 if (!image_check_dcrc (hdr)) {
577 puts ("Bad Data CRC\n");
578 } else {
579 puts ("OK\n");
580 }
581 break;
582#if defined(CONFIG_FIT)
583 case IMAGE_FORMAT_FIT:
584 printf ("FIT Image at %08lX:\n", (ulong)hdr);
585 fit_unsupported ("imls");
586 break;
587#endif
588 default:
wdenk27b207f2003-07-24 23:38:38 +0000589 goto next_sector;
wdenk5bb226e2003-11-17 21:14:37 +0000590 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100591
wdenkbdccc4f2003-08-05 17:43:17 +0000592next_sector: ;
wdenk27b207f2003-07-24 23:38:38 +0000593 }
wdenkbdccc4f2003-08-05 17:43:17 +0000594next_bank: ;
wdenk27b207f2003-07-24 23:38:38 +0000595 }
596
597 return (0);
598}
599
600U_BOOT_CMD(
601 imls, 1, 1, do_imls,
602 "imls - list all images found in flash\n",
603 "\n"
604 " - Prints information about all images found at sector\n"
605 " boundaries in flash.\n"
606);
Jon Loeliger90253172007-07-10 11:02:44 -0500607#endif
wdenk27b207f2003-07-24 23:38:38 +0000608
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100609/*******************************************************************/
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100610/* helper routines */
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100611/*******************************************************************/
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100612#ifdef CONFIG_SILENT_CONSOLE
613static void fixup_silent_linux ()
614{
615 char buf[256], *start, *end;
616 char *cmdline = getenv ("bootargs");
617
618 /* Only fix cmdline when requested */
619 if (!(gd->flags & GD_FLG_SILENT))
620 return;
621
622 debug ("before silent fix-up: %s\n", cmdline);
623 if (cmdline) {
624 if ((start = strstr (cmdline, "console=")) != NULL) {
625 end = strchr (start, ' ');
626 strncpy (buf, cmdline, (start - cmdline + 8));
627 if (end)
628 strcpy (buf + (start - cmdline + 8), end);
629 else
630 buf[start - cmdline + 8] = '\0';
631 } else {
632 strcpy (buf, cmdline);
633 strcat (buf, " console=");
634 }
635 } else {
636 strcpy (buf, "console=");
637 }
638
639 setenv ("bootargs", buf);
640 debug ("after silent fix-up: %s\n", buf);
641}
642#endif /* CONFIG_SILENT_CONSOLE */
643
644
645/*******************************************************************/
646/* OS booting routines */
647/*******************************************************************/
648
649static void do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100650 int argc, char *argv[],
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100651 bootm_headers_t *images, int verify)
wdenkd791b1d2003-04-20 14:04:18 +0000652{
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100653 void (*loader)(bd_t *, image_header_t *, char *, char *);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100654 image_header_t *os_hdr, *hdr;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100655 ulong kernel_data, kernel_len;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100656 char *consdev;
657 char *cmdline;
658
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100659#if defined(CONFIG_FIT)
660 if (!images->legacy_hdr_valid) {
661 fit_unsupported_reset ("NetBSD");
662 do_reset (cmdtp, flag, argc, argv);
663 }
664#endif
665 hdr = images->legacy_hdr_os;
666
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100667 /*
668 * Booting a (NetBSD) kernel image
669 *
670 * This process is pretty similar to a standalone application:
671 * The (first part of an multi-) image must be a stage-2 loader,
672 * which in turn is responsible for loading & invoking the actual
673 * kernel. The only differences are the parameters being passed:
674 * besides the board info strucure, the loader expects a command
675 * line, the name of the console device, and (optionally) the
676 * address of the original image header.
677 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100678 os_hdr = NULL;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100679 if (image_check_type (hdr, IH_TYPE_MULTI)) {
680 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
681 if (kernel_len)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100682 os_hdr = hdr;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100683 }
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100684
685 consdev = "";
686#if defined (CONFIG_8xx_CONS_SMC1)
687 consdev = "smc1";
688#elif defined (CONFIG_8xx_CONS_SMC2)
689 consdev = "smc2";
690#elif defined (CONFIG_8xx_CONS_SCC2)
691 consdev = "scc2";
692#elif defined (CONFIG_8xx_CONS_SCC3)
693 consdev = "scc3";
694#endif
695
696 if (argc > 2) {
697 ulong len;
698 int i;
699
700 for (i = 2, len = 0; i < argc; i += 1)
701 len += strlen (argv[i]) + 1;
702 cmdline = malloc (len);
703
704 for (i = 2, len = 0; i < argc; i += 1) {
705 if (i > 2)
706 cmdline[len++] = ' ';
707 strcpy (&cmdline[len], argv[i]);
708 len += strlen (argv[i]);
709 }
710 } else if ((cmdline = getenv ("bootargs")) == NULL) {
711 cmdline = "";
712 }
713
714 loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
715
716 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
717 (ulong)loader);
718
719 show_boot_progress (15);
720
721 /*
722 * NetBSD Stage-2 Loader Parameters:
723 * r3: ptr to board info data
724 * r4: image address
725 * r5: console device
726 * r6: boot args string
727 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100728 (*loader) (gd->bd, os_hdr, consdev, cmdline);
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100729}
730
731#ifdef CONFIG_LYNXKDI
732static void do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
733 int argc, char *argv[],
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100734 bootm_headers_t *images, int verify)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100735{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100736 image_header_t *hdr = images->legacy_hdr_os;
737
738#if defined(CONFIG_FIT)
739 if (!images->legacy_hdr_valid) {
740 fit_unsupported_reset ("Lynx");
741 do_reset (cmdtp, flag, argc, argv);
742 }
743#endif
744
745 lynxkdi_boot ((image_header_t *)hdr);
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100746}
747#endif /* CONFIG_LYNXKDI */
748
749static void do_bootm_rtems (cmd_tbl_t *cmdtp, int flag,
750 int argc, char *argv[],
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100751 bootm_headers_t *images, int verify)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100752{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100753 image_header_t *hdr = images->legacy_hdr_os;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100754 void (*entry_point)(bd_t *);
wdenkd791b1d2003-04-20 14:04:18 +0000755
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100756#if defined(CONFIG_FIT)
757 if (!images->legacy_hdr_valid) {
758 fit_unsupported_reset ("RTEMS");
759 do_reset (cmdtp, flag, argc, argv);
760 }
761#endif
762
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100763 entry_point = (void (*)(bd_t *))image_get_ep (hdr);
wdenkd791b1d2003-04-20 14:04:18 +0000764
765 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
766 (ulong)entry_point);
767
Heiko Schocherfad63402007-07-13 09:54:17 +0200768 show_boot_progress (15);
wdenkd791b1d2003-04-20 14:04:18 +0000769
770 /*
771 * RTEMS Parameters:
772 * r3: ptr to board info data
773 */
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100774 (*entry_point)(gd->bd);
wdenkd791b1d2003-04-20 14:04:18 +0000775}
776
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500777#if defined(CONFIG_CMD_ELF)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100778static void do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag,
779 int argc, char *argv[],
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100780 bootm_headers_t *images, int verify)
wdenk47d1a6e2002-11-03 00:01:44 +0000781{
wdenk47d1a6e2002-11-03 00:01:44 +0000782 char str[80];
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100783 image_header_t *hdr = images->legacy_hdr_os;
784
785#if defined(CONFIG_FIT)
786 if (hdr == NULL) {
787 fit_unsupported_reset ("VxWorks");
788 do_reset (cmdtp, flag, argc, argv);
789 }
790#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000791
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100792 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +0000793 setenv("loadaddr", str);
794 do_bootvx(cmdtp, 0, 0, NULL);
795}
796
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100797static void do_bootm_qnxelf(cmd_tbl_t *cmdtp, int flag,
798 int argc, char *argv[],
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100799 bootm_headers_t *images, int verify)
wdenk47d1a6e2002-11-03 00:01:44 +0000800{
wdenk47d1a6e2002-11-03 00:01:44 +0000801 char *local_args[2];
802 char str[16];
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100803 image_header_t *hdr = images->legacy_hdr_os;
804
805#if defined(CONFIG_FIT)
806 if (!images->legacy_hdr_valid) {
807 fit_unsupported_reset ("QNX");
808 do_reset (cmdtp, flag, argc, argv);
809 }
810#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000811
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100812 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +0000813 local_args[0] = argv[0];
814 local_args[1] = str; /* and provide it via the arguments */
815 do_bootelf(cmdtp, 0, 2, local_args);
816}
Jon Loeliger90253172007-07-10 11:02:44 -0500817#endif
wdenk1f4bb372003-07-27 00:21:01 +0000818
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100819#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
820static void do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
821 int argc, char *argv[],
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100822 bootm_headers_t *images, int verify)
wdenk1f4bb372003-07-27 00:21:01 +0000823{
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100824 ulong top;
825 char *s, *cmdline;
826 char **fwenv, **ss;
827 int i, j, nxt, len, envno, envsz;
828 bd_t *kbd;
829 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100830 image_header_t *hdr = images->legacy_hdr_os;
831
832#if defined(CONFIG_FIT)
833 if (!images->legacy_hdr_valid) {
834 fit_unsupported_reset ("ARTOS");
835 do_reset (cmdtp, flag, argc, argv);
836 }
837#endif
wdenk1f4bb372003-07-27 00:21:01 +0000838
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100839 /*
840 * Booting an ARTOS kernel image + application
841 */
842
843 /* this used to be the top of memory, but was wrong... */
844#ifdef CONFIG_PPC
845 /* get stack pointer */
846 asm volatile ("mr %0,1" : "=r"(top) );
847#endif
848 debug ("## Current stack ends at 0x%08lX ", top);
849
850 top -= 2048; /* just to be sure */
851 if (top > CFG_BOOTMAPSZ)
852 top = CFG_BOOTMAPSZ;
853 top &= ~0xF;
854
855 debug ("=> set upper limit to 0x%08lX\n", top);
856
857 /* first check the artos specific boot args, then the linux args*/
858 if ((s = getenv( "abootargs")) == NULL && (s = getenv ("bootargs")) == NULL)
859 s = "";
860
861 /* get length of cmdline, and place it */
862 len = strlen (s);
863 top = (top - (len + 1)) & ~0xF;
864 cmdline = (char *)top;
865 debug ("## cmdline at 0x%08lX ", top);
866 strcpy (cmdline, s);
867
868 /* copy bdinfo */
869 top = (top - sizeof (bd_t)) & ~0xF;
870 debug ("## bd at 0x%08lX ", top);
871 kbd = (bd_t *)top;
872 memcpy (kbd, gd->bd, sizeof (bd_t));
873
874 /* first find number of env entries, and their size */
875 envno = 0;
876 envsz = 0;
877 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
878 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
879 ;
880 envno++;
881 envsz += (nxt - i) + 1; /* plus trailing zero */
882 }
883 envno++; /* plus the terminating zero */
884 debug ("## %u envvars total size %u ", envno, envsz);
885
886 top = (top - sizeof (char **) * envno) & ~0xF;
887 fwenv = (char **)top;
888 debug ("## fwenv at 0x%08lX ", top);
889
890 top = (top - envsz) & ~0xF;
891 s = (char *)top;
892 ss = fwenv;
893
894 /* now copy them */
895 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
896 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
897 ;
898 *ss++ = s;
899 for (j = i; j < nxt; ++j)
900 *s++ = env_get_char (j);
901 *s++ = '\0';
902 }
903 *ss++ = NULL; /* terminate */
904
905 entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
906 (*entry) (kbd, cmdline, fwenv, top);
907}
908#endif