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