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