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