]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_bootm.c
bootm: refactor entry point code
[karo-tx-uboot.git] / common / cmd_bootm.c
1 /*
2  * (C) Copyright 2000-2006
3  * 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
24
25 /*
26  * Boot support
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 <lmb.h>
37 #include <asm/byteorder.h>
38
39 #if defined(CONFIG_CMD_USB)
40 #include <usb.h>
41 #endif
42
43 #ifdef CFG_HUSH_PARSER
44 #include <hush.h>
45 #endif
46
47 DECLARE_GLOBAL_DATA_PTR;
48
49 extern int gunzip (void *dst, int dstlen, unsigned char *src, unsigned long *lenp);
50 #ifndef CFG_BOOTM_LEN
51 #define CFG_BOOTM_LEN   0x800000        /* use 8MByte as default max gunzip size */
52 #endif
53
54 #ifdef CONFIG_BZIP2
55 extern void bz_internal_error(int);
56 #endif
57
58 #if defined(CONFIG_CMD_IMI)
59 static int image_info (unsigned long addr);
60 #endif
61
62 #if defined(CONFIG_CMD_IMLS)
63 #include <flash.h>
64 extern flash_info_t flash_info[]; /* info for FLASH chips */
65 static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
66 #endif
67
68 #ifdef CONFIG_SILENT_CONSOLE
69 static void fixup_silent_linux (void);
70 #endif
71
72 static image_header_t *image_get_kernel (ulong img_addr, int verify);
73 #if defined(CONFIG_FIT)
74 static int fit_check_kernel (const void *fit, int os_noffset, int verify);
75 #endif
76
77 static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag,int argc, char *argv[],
78                 bootm_headers_t *images, ulong *os_data, ulong *os_len);
79 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
80
81 /*
82  *  Continue booting an OS image; caller already has:
83  *  - copied image header to global variable `header'
84  *  - checked header magic number, checksums (both header & image),
85  *  - verified image architecture (PPC) and type (KERNEL or MULTI),
86  *  - loaded (first part of) image to header load address,
87  *  - disabled interrupts.
88  */
89 typedef void boot_os_fn (cmd_tbl_t *cmdtp, int flag,
90                         int argc, char *argv[],
91                         bootm_headers_t *images); /* pointers to os/initrd/fdt */
92
93 extern boot_os_fn do_bootm_linux;
94 static boot_os_fn do_bootm_netbsd;
95 #if defined(CONFIG_LYNXKDI)
96 static boot_os_fn do_bootm_lynxkdi;
97 extern void lynxkdi_boot (image_header_t *);
98 #endif
99 static boot_os_fn do_bootm_rtems;
100 #if defined(CONFIG_CMD_ELF)
101 static boot_os_fn do_bootm_vxworks;
102 static boot_os_fn do_bootm_qnxelf;
103 int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
104 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
105 #endif
106 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
107 static boot_os_fn do_bootm_artos;
108 #endif
109
110 ulong load_addr = CFG_LOAD_ADDR;        /* Default Load Address */
111 static bootm_headers_t images;          /* pointers to os/initrd/fdt images */
112
113 void __board_lmb_reserve(struct lmb *lmb)
114 {
115         /* please define platform specific board_lmb_reserve() */
116 }
117 void board_lmb_reserve(struct lmb *lmb) __attribute__((weak, alias("__board_lmb_reserve")));
118
119
120 /*******************************************************************/
121 /* bootm - boot application image from image in memory */
122 /*******************************************************************/
123 int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
124 {
125         ulong           iflag;
126         const char      *type_name;
127         uint            unc_len = CFG_BOOTM_LEN;
128         uint8_t         comp, type, os;
129
130         void            *os_hdr;
131         ulong           os_data, os_len;
132         ulong           image_start, image_end;
133         ulong           load_start, load_end;
134         ulong           mem_start;
135         phys_size_t     mem_size;
136
137         struct lmb lmb;
138
139         memset ((void *)&images, 0, sizeof (images));
140         images.verify = getenv_yesno ("verify");
141         images.lmb = &lmb;
142
143         lmb_init(&lmb);
144
145         mem_start = getenv_bootm_low();
146         mem_size = getenv_bootm_size();
147
148         lmb_add(&lmb, (phys_addr_t)mem_start, mem_size);
149
150         board_lmb_reserve(&lmb);
151
152         /* get kernel image header, start address and length */
153         os_hdr = boot_get_kernel (cmdtp, flag, argc, argv,
154                         &images, &os_data, &os_len);
155         if (os_len == 0) {
156                 puts ("ERROR: can't get kernel image!\n");
157                 return 1;
158         }
159
160         /* get image parameters */
161         switch (genimg_get_format (os_hdr)) {
162         case IMAGE_FORMAT_LEGACY:
163                 type = image_get_type (os_hdr);
164                 comp = image_get_comp (os_hdr);
165                 os = image_get_os (os_hdr);
166
167                 image_end = image_get_image_end (os_hdr);
168                 load_start = image_get_load (os_hdr);
169                 break;
170 #if defined(CONFIG_FIT)
171         case IMAGE_FORMAT_FIT:
172                 if (fit_image_get_type (images.fit_hdr_os,
173                                         images.fit_noffset_os, &type)) {
174                         puts ("Can't get image type!\n");
175                         show_boot_progress (-109);
176                         return 1;
177                 }
178
179                 if (fit_image_get_comp (images.fit_hdr_os,
180                                         images.fit_noffset_os, &comp)) {
181                         puts ("Can't get image compression!\n");
182                         show_boot_progress (-110);
183                         return 1;
184                 }
185
186                 if (fit_image_get_os (images.fit_hdr_os,
187                                         images.fit_noffset_os, &os)) {
188                         puts ("Can't get image OS!\n");
189                         show_boot_progress (-111);
190                         return 1;
191                 }
192
193                 image_end = fit_get_end (images.fit_hdr_os);
194
195                 if (fit_image_get_load (images.fit_hdr_os, images.fit_noffset_os,
196                                         &load_start)) {
197                         puts ("Can't get image load address!\n");
198                         show_boot_progress (-112);
199                         return 1;
200                 }
201                 break;
202 #endif
203         default:
204                 puts ("ERROR: unknown image format type!\n");
205                 return 1;
206         }
207
208         /* find kernel entry point */
209         if (images.legacy_hdr_valid) {
210                 images.ep = image_get_ep (&images.legacy_hdr_os_copy);
211 #if defined(CONFIG_FIT)
212         } else if (images.fit_uname_os) {
213                 ret = fit_image_get_entry (images.fit_hdr_os,
214                                 images.fit_noffset_os, &images.ep);
215                 if (ret) {
216                         puts ("Can't get entry point property!\n");
217                         return 1;
218                 }
219 #endif
220         } else {
221                 puts ("Could not find kernel entry point!\n");
222                 return 1;
223         }
224
225         image_start = (ulong)os_hdr;
226         load_end = 0;
227         type_name = genimg_get_type_name (type);
228
229         /*
230          * We have reached the point of no return: we are going to
231          * overwrite all exception vector code, so we cannot easily
232          * recover from any failures any more...
233          */
234         iflag = disable_interrupts();
235
236 #if defined(CONFIG_CMD_USB)
237         /*
238          * turn off USB to prevent the host controller from writing to the
239          * SDRAM while Linux is booting. This could happen (at least for OHCI
240          * controller), because the HCCA (Host Controller Communication Area)
241          * lies within the SDRAM and the host controller writes continously to
242          * this area (as busmaster!). The HccaFrameNumber is for example
243          * updated every 1 ms within the HCCA structure in SDRAM! For more
244          * details see the OpenHCI specification.
245          */
246         usb_stop();
247 #endif
248
249
250 #ifdef CONFIG_AMIGAONEG3SE
251         /*
252          * We've possible left the caches enabled during
253          * bios emulation, so turn them off again
254          */
255         icache_disable();
256         dcache_disable();
257 #endif
258
259         switch (comp) {
260         case IH_COMP_NONE:
261                 if (load_start == (ulong)os_hdr) {
262                         printf ("   XIP %s ... ", type_name);
263                 } else {
264                         printf ("   Loading %s ... ", type_name);
265
266                         memmove_wd ((void *)load_start,
267                                    (void *)os_data, os_len, CHUNKSZ);
268                 }
269                 load_end = load_start + os_len;
270                 puts("OK\n");
271                 break;
272         case IH_COMP_GZIP:
273                 printf ("   Uncompressing %s ... ", type_name);
274                 if (gunzip ((void *)load_start, unc_len,
275                                         (uchar *)os_data, &os_len) != 0) {
276                         puts ("GUNZIP: uncompress or overwrite error "
277                                 "- must RESET board to recover\n");
278                         show_boot_progress (-6);
279                         do_reset (cmdtp, flag, argc, argv);
280                 }
281
282                 load_end = load_start + os_len;
283                 break;
284 #ifdef CONFIG_BZIP2
285         case IH_COMP_BZIP2:
286                 printf ("   Uncompressing %s ... ", type_name);
287                 /*
288                  * If we've got less than 4 MB of malloc() space,
289                  * use slower decompression algorithm which requires
290                  * at most 2300 KB of memory.
291                  */
292                 int i = BZ2_bzBuffToBuffDecompress ((char*)load_start,
293                                         &unc_len, (char *)os_data, os_len,
294                                         CFG_MALLOC_LEN < (4096 * 1024), 0);
295                 if (i != BZ_OK) {
296                         printf ("BUNZIP2: uncompress or overwrite error %d "
297                                 "- must RESET board to recover\n", i);
298                         show_boot_progress (-6);
299                         do_reset (cmdtp, flag, argc, argv);
300                 }
301
302                 load_end = load_start + unc_len;
303                 break;
304 #endif /* CONFIG_BZIP2 */
305         default:
306                 if (iflag)
307                         enable_interrupts();
308                 printf ("Unimplemented compression type %d\n", comp);
309                 show_boot_progress (-7);
310                 return 1;
311         }
312         puts ("OK\n");
313         debug ("   kernel loaded at 0x%08lx, end = 0x%08lx\n", load_start, load_end);
314         show_boot_progress (7);
315
316         if ((load_start < image_end) && (load_end > image_start)) {
317                 debug ("image_start = 0x%lX, image_end = 0x%lx\n", image_start, image_end);
318                 debug ("load_start = 0x%lx, load_end = 0x%lx\n", load_start, load_end);
319
320                 if (images.legacy_hdr_valid) {
321                         if (image_get_type (&images.legacy_hdr_os_copy) == IH_TYPE_MULTI)
322                                 puts ("WARNING: legacy format multi component "
323                                         "image overwritten\n");
324                 } else {
325                         puts ("ERROR: new format image overwritten - "
326                                 "must RESET the board to recover\n");
327                         show_boot_progress (-113);
328                         do_reset (cmdtp, flag, argc, argv);
329                 }
330         }
331
332         show_boot_progress (8);
333
334         lmb_reserve(&lmb, load_start, (load_end - load_start));
335
336         switch (os) {
337         default:                        /* handled by (original) Linux case */
338         case IH_OS_LINUX:
339 #ifdef CONFIG_SILENT_CONSOLE
340             fixup_silent_linux();
341 #endif
342             do_bootm_linux (cmdtp, flag, argc, argv, &images);
343             break;
344
345         case IH_OS_NETBSD:
346             do_bootm_netbsd (cmdtp, flag, argc, argv, &images);
347             break;
348
349 #ifdef CONFIG_LYNXKDI
350         case IH_OS_LYNXOS:
351             do_bootm_lynxkdi (cmdtp, flag, argc, argv, &images);
352             break;
353 #endif
354
355         case IH_OS_RTEMS:
356             do_bootm_rtems (cmdtp, flag, argc, argv, &images);
357             break;
358
359 #if defined(CONFIG_CMD_ELF)
360         case IH_OS_VXWORKS:
361             do_bootm_vxworks (cmdtp, flag, argc, argv, &images);
362             break;
363
364         case IH_OS_QNX:
365             do_bootm_qnxelf (cmdtp, flag, argc, argv, &images);
366             break;
367 #endif
368
369 #ifdef CONFIG_ARTOS
370         case IH_OS_ARTOS:
371             do_bootm_artos (cmdtp, flag, argc, argv, &images);
372             break;
373 #endif
374         }
375
376         show_boot_progress (-9);
377 #ifdef DEBUG
378         puts ("\n## Control returned to monitor - resetting...\n");
379         do_reset (cmdtp, flag, argc, argv);
380 #endif
381         if (iflag)
382                 enable_interrupts();
383
384         return 1;
385 }
386
387 /**
388  * image_get_kernel - verify legacy format kernel image
389  * @img_addr: in RAM address of the legacy format image to be verified
390  * @verify: data CRC verification flag
391  *
392  * image_get_kernel() verifies legacy image integrity and returns pointer to
393  * legacy image header if image verification was completed successfully.
394  *
395  * returns:
396  *     pointer to a legacy image header if valid image was found
397  *     otherwise return NULL
398  */
399 static image_header_t *image_get_kernel (ulong img_addr, int verify)
400 {
401         image_header_t *hdr = (image_header_t *)img_addr;
402
403         if (!image_check_magic(hdr)) {
404                 puts ("Bad Magic Number\n");
405                 show_boot_progress (-1);
406                 return NULL;
407         }
408         show_boot_progress (2);
409
410         if (!image_check_hcrc (hdr)) {
411                 puts ("Bad Header Checksum\n");
412                 show_boot_progress (-2);
413                 return NULL;
414         }
415
416         show_boot_progress (3);
417         image_print_contents (hdr);
418
419         if (verify) {
420                 puts ("   Verifying Checksum ... ");
421                 if (!image_check_dcrc (hdr)) {
422                         printf ("Bad Data CRC\n");
423                         show_boot_progress (-3);
424                         return NULL;
425                 }
426                 puts ("OK\n");
427         }
428         show_boot_progress (4);
429
430         if (!image_check_target_arch (hdr)) {
431                 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
432                 show_boot_progress (-4);
433                 return NULL;
434         }
435         return hdr;
436 }
437
438 /**
439  * fit_check_kernel - verify FIT format kernel subimage
440  * @fit_hdr: pointer to the FIT image header
441  * os_noffset: kernel subimage node offset within FIT image
442  * @verify: data CRC verification flag
443  *
444  * fit_check_kernel() verifies integrity of the kernel subimage and from
445  * specified FIT image.
446  *
447  * returns:
448  *     1, on success
449  *     0, on failure
450  */
451 #if defined (CONFIG_FIT)
452 static int fit_check_kernel (const void *fit, int os_noffset, int verify)
453 {
454         fit_image_print (fit, os_noffset, "   ");
455
456         if (verify) {
457                 puts ("   Verifying Hash Integrity ... ");
458                 if (!fit_image_check_hashes (fit, os_noffset)) {
459                         puts ("Bad Data Hash\n");
460                         show_boot_progress (-104);
461                         return 0;
462                 }
463                 puts ("OK\n");
464         }
465         show_boot_progress (105);
466
467         if (!fit_image_check_target_arch (fit, os_noffset)) {
468                 puts ("Unsupported Architecture\n");
469                 show_boot_progress (-105);
470                 return 0;
471         }
472
473         show_boot_progress (106);
474         if (!fit_image_check_type (fit, os_noffset, IH_TYPE_KERNEL)) {
475                 puts ("Not a kernel image\n");
476                 show_boot_progress (-106);
477                 return 0;
478         }
479
480         show_boot_progress (107);
481         return 1;
482 }
483 #endif /* CONFIG_FIT */
484
485 /**
486  * boot_get_kernel - find kernel image
487  * @os_data: pointer to a ulong variable, will hold os data start address
488  * @os_len: pointer to a ulong variable, will hold os data length
489  *
490  * boot_get_kernel() tries to find a kernel image, verifies its integrity
491  * and locates kernel data.
492  *
493  * returns:
494  *     pointer to image header if valid image was found, plus kernel start
495  *     address and length, otherwise NULL
496  */
497 static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
498                 bootm_headers_t *images, ulong *os_data, ulong *os_len)
499 {
500         image_header_t  *hdr;
501         ulong           img_addr;
502 #if defined(CONFIG_FIT)
503         void            *fit_hdr;
504         const char      *fit_uname_config = NULL;
505         const char      *fit_uname_kernel = NULL;
506         const void      *data;
507         size_t          len;
508         int             cfg_noffset;
509         int             os_noffset;
510 #endif
511
512         /* find out kernel image address */
513         if (argc < 2) {
514                 img_addr = load_addr;
515                 debug ("*  kernel: default image load address = 0x%08lx\n",
516                                 load_addr);
517 #if defined(CONFIG_FIT)
518         } else if (fit_parse_conf (argv[1], load_addr, &img_addr,
519                                                         &fit_uname_config)) {
520                 debug ("*  kernel: config '%s' from image at 0x%08lx\n",
521                                 fit_uname_config, img_addr);
522         } else if (fit_parse_subimage (argv[1], load_addr, &img_addr,
523                                                         &fit_uname_kernel)) {
524                 debug ("*  kernel: subimage '%s' from image at 0x%08lx\n",
525                                 fit_uname_kernel, img_addr);
526 #endif
527         } else {
528                 img_addr = simple_strtoul(argv[1], NULL, 16);
529                 debug ("*  kernel: cmdline image address = 0x%08lx\n", img_addr);
530         }
531
532         show_boot_progress (1);
533
534         /* copy from dataflash if needed */
535         img_addr = genimg_get_image (img_addr);
536
537         /* check image type, for FIT images get FIT kernel node */
538         *os_data = *os_len = 0;
539         switch (genimg_get_format ((void *)img_addr)) {
540         case IMAGE_FORMAT_LEGACY:
541                 printf ("## Booting kernel from Legacy Image at %08lx ...\n",
542                                 img_addr);
543                 hdr = image_get_kernel (img_addr, images->verify);
544                 if (!hdr)
545                         return NULL;
546                 show_boot_progress (5);
547
548                 /* get os_data and os_len */
549                 switch (image_get_type (hdr)) {
550                 case IH_TYPE_KERNEL:
551                         *os_data = image_get_data (hdr);
552                         *os_len = image_get_data_size (hdr);
553                         break;
554                 case IH_TYPE_MULTI:
555                         image_multi_getimg (hdr, 0, os_data, os_len);
556                         break;
557                 default:
558                         printf ("Wrong Image Type for %s command\n", cmdtp->name);
559                         show_boot_progress (-5);
560                         return NULL;
561                 }
562
563                 /*
564                  * copy image header to allow for image overwrites during kernel
565                  * decompression.
566                  */
567                 memmove (&images->legacy_hdr_os_copy, hdr, sizeof(image_header_t));
568
569                 /* save pointer to image header */
570                 images->legacy_hdr_os = hdr;
571
572                 images->legacy_hdr_valid = 1;
573                 show_boot_progress (6);
574                 break;
575 #if defined(CONFIG_FIT)
576         case IMAGE_FORMAT_FIT:
577                 fit_hdr = (void *)img_addr;
578                 printf ("## Booting kernel from FIT Image at %08lx ...\n",
579                                 img_addr);
580
581                 if (!fit_check_format (fit_hdr)) {
582                         puts ("Bad FIT kernel image format!\n");
583                         show_boot_progress (-100);
584                         return NULL;
585                 }
586                 show_boot_progress (100);
587
588                 if (!fit_uname_kernel) {
589                         /*
590                          * no kernel image node unit name, try to get config
591                          * node first. If config unit node name is NULL
592                          * fit_conf_get_node() will try to find default config node
593                          */
594                         show_boot_progress (101);
595                         cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
596                         if (cfg_noffset < 0) {
597                                 show_boot_progress (-101);
598                                 return NULL;
599                         }
600                         /* save configuration uname provided in the first
601                          * bootm argument
602                          */
603                         images->fit_uname_cfg = fdt_get_name (fit_hdr, cfg_noffset, NULL);
604                         printf ("   Using '%s' configuration\n", images->fit_uname_cfg);
605                         show_boot_progress (103);
606
607                         os_noffset = fit_conf_get_kernel_node (fit_hdr, cfg_noffset);
608                         fit_uname_kernel = fit_get_name (fit_hdr, os_noffset, NULL);
609                 } else {
610                         /* get kernel component image node offset */
611                         show_boot_progress (102);
612                         os_noffset = fit_image_get_node (fit_hdr, fit_uname_kernel);
613                 }
614                 if (os_noffset < 0) {
615                         show_boot_progress (-103);
616                         return NULL;
617                 }
618
619                 printf ("   Trying '%s' kernel subimage\n", fit_uname_kernel);
620
621                 show_boot_progress (104);
622                 if (!fit_check_kernel (fit_hdr, os_noffset, images->verify))
623                         return NULL;
624
625                 /* get kernel image data address and length */
626                 if (fit_image_get_data (fit_hdr, os_noffset, &data, &len)) {
627                         puts ("Could not find kernel subimage data!\n");
628                         show_boot_progress (-107);
629                         return NULL;
630                 }
631                 show_boot_progress (108);
632
633                 *os_len = len;
634                 *os_data = (ulong)data;
635                 images->fit_hdr_os = fit_hdr;
636                 images->fit_uname_os = fit_uname_kernel;
637                 images->fit_noffset_os = os_noffset;
638                 break;
639 #endif
640         default:
641                 printf ("Wrong Image Format for %s command\n", cmdtp->name);
642                 show_boot_progress (-108);
643                 return NULL;
644         }
645
646         debug ("   kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
647                         *os_data, *os_len, *os_len);
648
649         return (void *)img_addr;
650 }
651
652 U_BOOT_CMD(
653         bootm,  CFG_MAXARGS,    1,      do_bootm,
654         "bootm   - boot application image from memory\n",
655         "[addr [arg ...]]\n    - boot application image stored in memory\n"
656         "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
657         "\t'arg' can be the address of an initrd image\n"
658 #if defined(CONFIG_OF_LIBFDT)
659         "\tWhen booting a Linux kernel which requires a flat device-tree\n"
660         "\ta third argument is required which is the address of the\n"
661         "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
662         "\tuse a '-' for the second argument. If you do not pass a third\n"
663         "\ta bd_info struct will be passed instead\n"
664 #endif
665 #if defined(CONFIG_FIT)
666         "\t\nFor the new multi component uImage format (FIT) addresses\n"
667         "\tmust be extened to include component or configuration unit name:\n"
668         "\taddr:<subimg_uname> - direct component image specification\n"
669         "\taddr#<conf_uname>   - configuration specification\n"
670         "\tUse iminfo command to get the list of existing component\n"
671         "\timages and configurations.\n"
672 #endif
673 );
674
675 /*******************************************************************/
676 /* bootd - boot default image */
677 /*******************************************************************/
678 #if defined(CONFIG_CMD_BOOTD)
679 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
680 {
681         int rcode = 0;
682
683 #ifndef CFG_HUSH_PARSER
684         if (run_command (getenv ("bootcmd"), flag) < 0)
685                 rcode = 1;
686 #else
687         if (parse_string_outer (getenv ("bootcmd"),
688                         FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
689                 rcode = 1;
690 #endif
691         return rcode;
692 }
693
694 U_BOOT_CMD(
695         boot,   1,      1,      do_bootd,
696         "boot    - boot default, i.e., run 'bootcmd'\n",
697         NULL
698 );
699
700 /* keep old command name "bootd" for backward compatibility */
701 U_BOOT_CMD(
702         bootd, 1,       1,      do_bootd,
703         "bootd   - boot default, i.e., run 'bootcmd'\n",
704         NULL
705 );
706
707 #endif
708
709
710 /*******************************************************************/
711 /* iminfo - print header info for a requested image */
712 /*******************************************************************/
713 #if defined(CONFIG_CMD_IMI)
714 int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
715 {
716         int     arg;
717         ulong   addr;
718         int     rcode = 0;
719
720         if (argc < 2) {
721                 return image_info (load_addr);
722         }
723
724         for (arg = 1; arg < argc; ++arg) {
725                 addr = simple_strtoul (argv[arg], NULL, 16);
726                 if (image_info (addr) != 0)
727                         rcode = 1;
728         }
729         return rcode;
730 }
731
732 static int image_info (ulong addr)
733 {
734         void *hdr = (void *)addr;
735
736         printf ("\n## Checking Image at %08lx ...\n", addr);
737
738         switch (genimg_get_format (hdr)) {
739         case IMAGE_FORMAT_LEGACY:
740                 puts ("   Legacy image found\n");
741                 if (!image_check_magic (hdr)) {
742                         puts ("   Bad Magic Number\n");
743                         return 1;
744                 }
745
746                 if (!image_check_hcrc (hdr)) {
747                         puts ("   Bad Header Checksum\n");
748                         return 1;
749                 }
750
751                 image_print_contents (hdr);
752
753                 puts ("   Verifying Checksum ... ");
754                 if (!image_check_dcrc (hdr)) {
755                         puts ("   Bad Data CRC\n");
756                         return 1;
757                 }
758                 puts ("OK\n");
759                 return 0;
760 #if defined(CONFIG_FIT)
761         case IMAGE_FORMAT_FIT:
762                 puts ("   FIT image found\n");
763
764                 if (!fit_check_format (hdr)) {
765                         puts ("Bad FIT image format!\n");
766                         return 1;
767                 }
768
769                 fit_print_contents (hdr);
770                 return 0;
771 #endif
772         default:
773                 puts ("Unknown image format!\n");
774                 break;
775         }
776
777         return 1;
778 }
779
780 U_BOOT_CMD(
781         iminfo, CFG_MAXARGS,    1,      do_iminfo,
782         "iminfo  - print header information for application image\n",
783         "addr [addr ...]\n"
784         "    - print header information for application image starting at\n"
785         "      address 'addr' in memory; this includes verification of the\n"
786         "      image contents (magic number, header and payload checksums)\n"
787 );
788 #endif
789
790
791 /*******************************************************************/
792 /* imls - list all images found in flash */
793 /*******************************************************************/
794 #if defined(CONFIG_CMD_IMLS)
795 int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
796 {
797         flash_info_t *info;
798         int i, j;
799         void *hdr;
800
801         for (i = 0, info = &flash_info[0];
802                 i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
803
804                 if (info->flash_id == FLASH_UNKNOWN)
805                         goto next_bank;
806                 for (j = 0; j < info->sector_count; ++j) {
807
808                         hdr = (void *)info->start[j];
809                         if (!hdr)
810                                 goto next_sector;
811
812                         switch (genimg_get_format (hdr)) {
813                         case IMAGE_FORMAT_LEGACY:
814                                 if (!image_check_hcrc (hdr))
815                                         goto next_sector;
816
817                                 printf ("Legacy Image at %08lX:\n", (ulong)hdr);
818                                 image_print_contents (hdr);
819
820                                 puts ("   Verifying Checksum ... ");
821                                 if (!image_check_dcrc (hdr)) {
822                                         puts ("Bad Data CRC\n");
823                                 } else {
824                                         puts ("OK\n");
825                                 }
826                                 break;
827 #if defined(CONFIG_FIT)
828                         case IMAGE_FORMAT_FIT:
829                                 if (!fit_check_format (hdr))
830                                         goto next_sector;
831
832                                 printf ("FIT Image at %08lX:\n", (ulong)hdr);
833                                 fit_print_contents (hdr);
834                                 break;
835 #endif
836                         default:
837                                 goto next_sector;
838                         }
839
840 next_sector:            ;
841                 }
842 next_bank:      ;
843         }
844
845         return (0);
846 }
847
848 U_BOOT_CMD(
849         imls,   1,              1,      do_imls,
850         "imls    - list all images found in flash\n",
851         "\n"
852         "    - Prints information about all images found at sector\n"
853         "      boundaries in flash.\n"
854 );
855 #endif
856
857 /*******************************************************************/
858 /* helper routines */
859 /*******************************************************************/
860 #ifdef CONFIG_SILENT_CONSOLE
861 static void fixup_silent_linux ()
862 {
863         char buf[256], *start, *end;
864         char *cmdline = getenv ("bootargs");
865
866         /* Only fix cmdline when requested */
867         if (!(gd->flags & GD_FLG_SILENT))
868                 return;
869
870         debug ("before silent fix-up: %s\n", cmdline);
871         if (cmdline) {
872                 if ((start = strstr (cmdline, "console=")) != NULL) {
873                         end = strchr (start, ' ');
874                         strncpy (buf, cmdline, (start - cmdline + 8));
875                         if (end)
876                                 strcpy (buf + (start - cmdline + 8), end);
877                         else
878                                 buf[start - cmdline + 8] = '\0';
879                 } else {
880                         strcpy (buf, cmdline);
881                         strcat (buf, " console=");
882                 }
883         } else {
884                 strcpy (buf, "console=");
885         }
886
887         setenv ("bootargs", buf);
888         debug ("after silent fix-up: %s\n", buf);
889 }
890 #endif /* CONFIG_SILENT_CONSOLE */
891
892
893 /*******************************************************************/
894 /* OS booting routines */
895 /*******************************************************************/
896
897 static void do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
898                             int argc, char *argv[],
899                             bootm_headers_t *images)
900 {
901         void (*loader)(bd_t *, image_header_t *, char *, char *);
902         image_header_t *os_hdr, *hdr;
903         ulong kernel_data, kernel_len;
904         char *consdev;
905         char *cmdline;
906
907 #if defined(CONFIG_FIT)
908         if (!images->legacy_hdr_valid) {
909                 fit_unsupported_reset ("NetBSD");
910                 do_reset (cmdtp, flag, argc, argv);
911         }
912 #endif
913         hdr = images->legacy_hdr_os;
914
915         /*
916          * Booting a (NetBSD) kernel image
917          *
918          * This process is pretty similar to a standalone application:
919          * The (first part of an multi-) image must be a stage-2 loader,
920          * which in turn is responsible for loading & invoking the actual
921          * kernel.  The only differences are the parameters being passed:
922          * besides the board info strucure, the loader expects a command
923          * line, the name of the console device, and (optionally) the
924          * address of the original image header.
925          */
926         os_hdr = NULL;
927         if (image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
928                 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
929                 if (kernel_len)
930                         os_hdr = hdr;
931         }
932
933         consdev = "";
934 #if   defined (CONFIG_8xx_CONS_SMC1)
935         consdev = "smc1";
936 #elif defined (CONFIG_8xx_CONS_SMC2)
937         consdev = "smc2";
938 #elif defined (CONFIG_8xx_CONS_SCC2)
939         consdev = "scc2";
940 #elif defined (CONFIG_8xx_CONS_SCC3)
941         consdev = "scc3";
942 #endif
943
944         if (argc > 2) {
945                 ulong len;
946                 int   i;
947
948                 for (i = 2, len = 0; i < argc; i += 1)
949                         len += strlen (argv[i]) + 1;
950                 cmdline = malloc (len);
951
952                 for (i = 2, len = 0; i < argc; i += 1) {
953                         if (i > 2)
954                                 cmdline[len++] = ' ';
955                         strcpy (&cmdline[len], argv[i]);
956                         len += strlen (argv[i]);
957                 }
958         } else if ((cmdline = getenv ("bootargs")) == NULL) {
959                 cmdline = "";
960         }
961
962         loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
963
964         printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
965                 (ulong)loader);
966
967         show_boot_progress (15);
968
969         /*
970          * NetBSD Stage-2 Loader Parameters:
971          *   r3: ptr to board info data
972          *   r4: image address
973          *   r5: console device
974          *   r6: boot args string
975          */
976         (*loader) (gd->bd, os_hdr, consdev, cmdline);
977 }
978
979 #ifdef CONFIG_LYNXKDI
980 static void do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
981                              int argc, char *argv[],
982                              bootm_headers_t *images)
983 {
984         image_header_t *hdr = &images->legacy_hdr_os_copy;
985
986 #if defined(CONFIG_FIT)
987         if (!images->legacy_hdr_valid) {
988                 fit_unsupported_reset ("Lynx");
989                 do_reset (cmdtp, flag, argc, argv);
990         }
991 #endif
992
993         lynxkdi_boot ((image_header_t *)hdr);
994 }
995 #endif /* CONFIG_LYNXKDI */
996
997 static void do_bootm_rtems (cmd_tbl_t *cmdtp, int flag,
998                            int argc, char *argv[],
999                            bootm_headers_t *images)
1000 {
1001         void (*entry_point)(bd_t *);
1002
1003 #if defined(CONFIG_FIT)
1004         if (!images->legacy_hdr_valid) {
1005                 fit_unsupported_reset ("RTEMS");
1006                 do_reset (cmdtp, flag, argc, argv);
1007         }
1008 #endif
1009
1010         entry_point = (void (*)(bd_t *))images->ep;
1011
1012         printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1013                 (ulong)entry_point);
1014
1015         show_boot_progress (15);
1016
1017         /*
1018          * RTEMS Parameters:
1019          *   r3: ptr to board info data
1020          */
1021         (*entry_point)(gd->bd);
1022 }
1023
1024 #if defined(CONFIG_CMD_ELF)
1025 static void do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag,
1026                              int argc, char *argv[],
1027                              bootm_headers_t *images)
1028 {
1029         char str[80];
1030
1031 #if defined(CONFIG_FIT)
1032         if (!images->legacy_hdr_valid) {
1033                 fit_unsupported_reset ("VxWorks");
1034                 do_reset (cmdtp, flag, argc, argv);
1035         }
1036 #endif
1037
1038         sprintf(str, "%lx", images->ep); /* write entry-point into string */
1039         setenv("loadaddr", str);
1040         do_bootvx(cmdtp, 0, 0, NULL);
1041 }
1042
1043 static void do_bootm_qnxelf(cmd_tbl_t *cmdtp, int flag,
1044                             int argc, char *argv[],
1045                             bootm_headers_t *images)
1046 {
1047         char *local_args[2];
1048         char str[16];
1049
1050 #if defined(CONFIG_FIT)
1051         if (!images->legacy_hdr_valid) {
1052                 fit_unsupported_reset ("QNX");
1053                 do_reset (cmdtp, flag, argc, argv);
1054         }
1055 #endif
1056
1057         sprintf(str, "%lx", images->ep); /* write entry-point into string */
1058         local_args[0] = argv[0];
1059         local_args[1] = str;    /* and provide it via the arguments */
1060         do_bootelf(cmdtp, 0, 2, local_args);
1061 }
1062 #endif
1063
1064 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
1065 static void do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
1066                            int argc, char *argv[],
1067                            bootm_headers_t *images)
1068 {
1069         ulong top;
1070         char *s, *cmdline;
1071         char **fwenv, **ss;
1072         int i, j, nxt, len, envno, envsz;
1073         bd_t *kbd;
1074         void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
1075
1076 #if defined(CONFIG_FIT)
1077         if (!images->legacy_hdr_valid) {
1078                 fit_unsupported_reset ("ARTOS");
1079                 do_reset (cmdtp, flag, argc, argv);
1080         }
1081 #endif
1082
1083         /*
1084          * Booting an ARTOS kernel image + application
1085          */
1086
1087         /* this used to be the top of memory, but was wrong... */
1088 #ifdef CONFIG_PPC
1089         /* get stack pointer */
1090         asm volatile ("mr %0,1" : "=r"(top) );
1091 #endif
1092         debug ("## Current stack ends at 0x%08lX ", top);
1093
1094         top -= 2048;            /* just to be sure */
1095         if (top > CFG_BOOTMAPSZ)
1096                 top = CFG_BOOTMAPSZ;
1097         top &= ~0xF;
1098
1099         debug ("=> set upper limit to 0x%08lX\n", top);
1100
1101         /* first check the artos specific boot args, then the linux args*/
1102         if ((s = getenv( "abootargs")) == NULL && (s = getenv ("bootargs")) == NULL)
1103                 s = "";
1104
1105         /* get length of cmdline, and place it */
1106         len = strlen (s);
1107         top = (top - (len + 1)) & ~0xF;
1108         cmdline = (char *)top;
1109         debug ("## cmdline at 0x%08lX ", top);
1110         strcpy (cmdline, s);
1111
1112         /* copy bdinfo */
1113         top = (top - sizeof (bd_t)) & ~0xF;
1114         debug ("## bd at 0x%08lX ", top);
1115         kbd = (bd_t *)top;
1116         memcpy (kbd, gd->bd, sizeof (bd_t));
1117
1118         /* first find number of env entries, and their size */
1119         envno = 0;
1120         envsz = 0;
1121         for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
1122                 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
1123                         ;
1124                 envno++;
1125                 envsz += (nxt - i) + 1; /* plus trailing zero */
1126         }
1127         envno++;        /* plus the terminating zero */
1128         debug ("## %u envvars total size %u ", envno, envsz);
1129
1130         top = (top - sizeof (char **) * envno) & ~0xF;
1131         fwenv = (char **)top;
1132         debug ("## fwenv at 0x%08lX ", top);
1133
1134         top = (top - envsz) & ~0xF;
1135         s = (char *)top;
1136         ss = fwenv;
1137
1138         /* now copy them */
1139         for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
1140                 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
1141                         ;
1142                 *ss++ = s;
1143                 for (j = i; j < nxt; ++j)
1144                         *s++ = env_get_char (j);
1145                 *s++ = '\0';
1146         }
1147         *ss++ = NULL;   /* terminate */
1148
1149         entry = (void (*)(bd_t *, char *, char **, ulong))images->ep;
1150         (*entry) (kbd, cmdline, fwenv, top);
1151 }
1152 #endif