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