]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - common/cmd_bootm.c
[new uImage] Cleanup image header pointer use in bootm code
[karo-tx-uboot.git] / common / cmd_bootm.c
1 /*
2  * (C) Copyright 2000-2006
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #define DEBUG
25
26 /*
27  * Boot support
28  */
29 #include <common.h>
30 #include <watchdog.h>
31 #include <command.h>
32 #include <image.h>
33 #include <malloc.h>
34 #include <zlib.h>
35 #include <bzlib.h>
36 #include <environment.h>
37 #include <asm/byteorder.h>
38
39 #if defined(CONFIG_OF_LIBFDT)
40 #include <fdt.h>
41 #include <libfdt.h>
42 #include <fdt_support.h>
43 #elif defined(CONFIG_OF_FLAT_TREE)
44 #include <ft_build.h>
45 #endif
46
47 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
48 #include <rtc.h>
49 #endif
50
51 #ifdef CFG_HUSH_PARSER
52 #include <hush.h>
53 #endif
54
55 #ifdef CONFIG_HAS_DATAFLASH
56 #include <dataflash.h>
57 #endif
58
59 DECLARE_GLOBAL_DATA_PTR;
60
61 extern int gunzip (void *dst, int dstlen, unsigned char *src, unsigned long *lenp);
62 #ifndef CFG_BOOTM_LEN
63 #define CFG_BOOTM_LEN   0x800000        /* use 8MByte as default max gunzip size */
64 #endif
65
66 #ifdef CONFIG_BZIP2
67 extern void bz_internal_error(int);
68 #endif
69
70 #if defined(CONFIG_CMD_IMI)
71 static int image_info (unsigned long addr);
72 #endif
73
74 #if defined(CONFIG_CMD_IMLS)
75 #include <flash.h>
76 extern flash_info_t flash_info[]; /* info for FLASH chips */
77 static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
78 #endif
79
80 #ifdef CONFIG_SILENT_CONSOLE
81 static void fixup_silent_linux (void);
82 #endif
83
84 static void print_type (image_header_t *hdr);
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                         image_header_t *hdr,    /* of image to boot */
98                         int verify);            /* getenv("verify")[0] != 'n' */
99
100 extern boot_os_fn do_bootm_linux;
101 static boot_os_fn do_bootm_netbsd;
102 #if defined(CONFIG_LYNXKDI)
103 static boot_os_fn do_bootm_lynxkdi;
104 extern void lynxkdi_boot (image_header_t *);
105 #endif
106 static boot_os_fn do_bootm_rtems;
107 #if defined(CONFIG_CMD_ELF)
108 static boot_os_fn do_bootm_vxworks;
109 static boot_os_fn do_bootm_qnxelf;
110 int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
111 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
112 #endif
113 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
114 extern uchar (*env_get_char)(int); /* Returns a character from the environment */
115 static boot_os_fn do_bootm_artos;
116 #endif
117
118 ulong load_addr = CFG_LOAD_ADDR;        /* Default Load Address */
119
120
121 /*******************************************************************/
122 /* bootm - boot application image from image in memory */
123 /*******************************************************************/
124 int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
125 {
126         ulong           iflag;
127         char            *name, *s;
128         int             (*appl)(int, char *[]);
129         uint            unc_len = CFG_BOOTM_LEN;
130         int             verify = getenv_verify();
131
132         image_header_t  *hdr;
133         ulong           img_addr;
134         ulong           os_data, os_len;
135
136         if (argc < 2) {
137                 img_addr = load_addr;
138         } else {
139                 img_addr = simple_strtoul(argv[1], NULL, 16);
140         }
141
142         show_boot_progress (1);
143         printf ("## Booting image at %08lx ...\n", img_addr);
144
145 #ifdef CONFIG_HAS_DATAFLASH
146         if (addr_dataflash (img_addr)){
147                 hdr = (image_header_t *)CFG_LOAD_ADDR;
148                 read_dataflash (img_addr, image_get_header_size (), (char *)hdr);
149         } else
150 #endif
151         hdr = (image_header_t *)img_addr;
152
153         if (!image_check_magic(hdr)) {
154                 puts ("Bad Magic Number\n");
155                 show_boot_progress (-1);
156                 return 1;
157         }
158         show_boot_progress (2);
159
160         if (!image_check_hcrc (hdr)) {
161                 puts ("Bad Header Checksum\n");
162                 show_boot_progress (-2);
163                 return 1;
164         }
165         show_boot_progress (3);
166
167 #ifdef CONFIG_HAS_DATAFLASH
168         if (addr_dataflash (img_addr))
169                 read_dataflash (img_addr + image_get_header_size (),
170                                 image_get_data_size (hdr),
171                                 (char *)image_get_data (hdr));
172 #endif
173
174         /* uImage is in a system RAM, pointed to by hdr */
175         print_image_hdr (hdr);
176
177         if (verify) {
178                 puts ("   Verifying Checksum ... ");
179                 if (!image_check_dcrc (hdr)) {
180                         printf ("Bad Data CRC\n");
181                         show_boot_progress (-3);
182                         return 1;
183                 }
184                 puts ("OK\n");
185         }
186         show_boot_progress (4);
187
188         if (!image_check_target_arch (hdr)) {
189                 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
190                 show_boot_progress (-4);
191                 return 1;
192         }
193         show_boot_progress (5);
194
195         switch (image_get_type (hdr)) {
196         case IH_TYPE_STANDALONE:
197                 name = "Standalone Application";
198                 /* A second argument overwrites the load address */
199                 if (argc > 2) {
200                         image_set_load (hdr, simple_strtoul (argv[2], NULL, 16));
201                 }
202                 break;
203         case IH_TYPE_KERNEL:
204                 name = "Kernel Image";
205                 os_data = image_get_data (hdr);
206                 os_len = image_get_data_size (hdr);
207                 break;
208         case IH_TYPE_MULTI:
209                 name = "Multi-File Image";
210                 image_multi_getimg (hdr, 0, &os_data, &os_len);
211                 break;
212         default:
213                 printf ("Wrong Image Type for %s command\n", cmdtp->name);
214                 show_boot_progress (-5);
215                 return 1;
216         }
217         show_boot_progress (6);
218
219         /*
220          * We have reached the point of no return: we are going to
221          * overwrite all exception vector code, so we cannot easily
222          * recover from any failures any more...
223          */
224         iflag = disable_interrupts();
225
226 #ifdef CONFIG_AMIGAONEG3SE
227         /*
228          * We've possible left the caches enabled during
229          * bios emulation, so turn them off again
230          */
231         icache_disable();
232         invalidate_l1_instruction_cache();
233         flush_data_cache();
234         dcache_disable();
235 #endif
236
237         switch (image_get_comp (hdr)) {
238         case IH_COMP_NONE:
239                 if (image_get_load (hdr) == img_addr) {
240                         printf ("   XIP %s ... ", name);
241                 } else {
242                         printf ("   Loading %s ... ", name);
243
244                         memmove_wd ((void *)image_get_load (hdr),
245                                    (void *)os_data, os_len, CHUNKSZ);
246
247                         puts("OK\n");
248                 }
249                 break;
250         case IH_COMP_GZIP:
251                 printf ("   Uncompressing %s ... ", name);
252                 if (gunzip ((void *)image_get_load (hdr), unc_len,
253                                         (uchar *)os_data, &os_len) != 0) {
254                         puts ("GUNZIP ERROR - must RESET board to recover\n");
255                         show_boot_progress (-6);
256                         do_reset (cmdtp, flag, argc, argv);
257                 }
258                 break;
259 #ifdef CONFIG_BZIP2
260         case IH_COMP_BZIP2:
261                 printf ("   Uncompressing %s ... ", name);
262                 /*
263                  * If we've got less than 4 MB of malloc() space,
264                  * use slower decompression algorithm which requires
265                  * at most 2300 KB of memory.
266                  */
267                 int i = BZ2_bzBuffToBuffDecompress ((char*)image_get_load (hdr),
268                                         &unc_len, (char *)os_data, os_len,
269                                         CFG_MALLOC_LEN < (4096 * 1024), 0);
270                 if (i != BZ_OK) {
271                         printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
272                         show_boot_progress (-6);
273                         do_reset (cmdtp, flag, argc, argv);
274                 }
275                 break;
276 #endif /* CONFIG_BZIP2 */
277         default:
278                 if (iflag)
279                         enable_interrupts();
280                 printf ("Unimplemented compression type %d\n", image_get_comp (hdr));
281                 show_boot_progress (-7);
282                 return 1;
283         }
284         puts ("OK\n");
285         show_boot_progress (7);
286
287         switch (image_get_type (hdr)) {
288         case IH_TYPE_STANDALONE:
289                 if (iflag)
290                         enable_interrupts();
291
292                 /* load (and uncompress), but don't start if "autostart"
293                  * is set to "no"
294                  */
295                 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
296                         char buf[32];
297                         sprintf(buf, "%lX", image_get_data_size(hdr));
298                         setenv("filesize", buf);
299                         return 0;
300                 }
301                 appl = (int (*)(int, char *[]))image_get_ep (hdr);
302                 (*appl)(argc-1, &argv[1]);
303                 return 0;
304         case IH_TYPE_KERNEL:
305         case IH_TYPE_MULTI:
306                 /* handled below */
307                 break;
308         default:
309                 if (iflag)
310                         enable_interrupts();
311                 printf ("Can't boot image type %d\n", image_get_type (hdr));
312                 show_boot_progress (-8);
313                 return 1;
314         }
315         show_boot_progress (8);
316
317         switch (image_get_os (hdr)) {
318         default:                        /* handled by (original) Linux case */
319         case IH_OS_LINUX:
320 #ifdef CONFIG_SILENT_CONSOLE
321             fixup_silent_linux();
322 #endif
323             do_bootm_linux (cmdtp, flag, argc, argv, hdr, verify);
324             break;
325
326         case IH_OS_NETBSD:
327             do_bootm_netbsd (cmdtp, flag, argc, argv, hdr, verify);
328             break;
329
330 #ifdef CONFIG_LYNXKDI
331         case IH_OS_LYNXOS:
332             do_bootm_lynxkdi (cmdtp, flag, argc, argv, hdr, verify);
333             break;
334 #endif
335
336         case IH_OS_RTEMS:
337             do_bootm_rtems (cmdtp, flag, argc, argv, hdr, verify);
338             break;
339
340 #if defined(CONFIG_CMD_ELF)
341         case IH_OS_VXWORKS:
342             do_bootm_vxworks (cmdtp, flag, argc, argv, hdr, verify);
343             break;
344
345         case IH_OS_QNX:
346             do_bootm_qnxelf (cmdtp, flag, argc, argv, hdr, verify);
347             break;
348 #endif
349
350 #ifdef CONFIG_ARTOS
351         case IH_OS_ARTOS:
352             do_bootm_artos (cmdtp, flag, argc, argv, hdr, verify);
353             break;
354 #endif
355         }
356
357         show_boot_progress (-9);
358 #ifdef DEBUG
359         puts ("\n## Control returned to monitor - resetting...\n");
360         do_reset (cmdtp, flag, argc, argv);
361 #endif
362         return 1;
363 }
364
365 U_BOOT_CMD(
366         bootm,  CFG_MAXARGS,    1,      do_bootm,
367         "bootm   - boot application image from memory\n",
368         "[addr [arg ...]]\n    - boot application image stored in memory\n"
369         "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
370         "\t'arg' can be the address of an initrd image\n"
371 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
372         "\tWhen booting a Linux kernel which requires a flat device-tree\n"
373         "\ta third argument is required which is the address of the\n"
374         "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
375         "\tuse a '-' for the second argument. If you do not pass a third\n"
376         "\ta bd_info struct will be passed instead\n"
377 #endif
378 );
379
380 /*******************************************************************/
381 /* bootd - boot default image */
382 /*******************************************************************/
383 #if defined(CONFIG_CMD_BOOTD)
384 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
385 {
386         int rcode = 0;
387
388 #ifndef CFG_HUSH_PARSER
389         if (run_command (getenv ("bootcmd"), flag) < 0)
390                 rcode = 1;
391 #else
392         if (parse_string_outer (getenv ("bootcmd"),
393                         FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
394                 rcode = 1;
395 #endif
396         return rcode;
397 }
398
399 U_BOOT_CMD(
400         boot,   1,      1,      do_bootd,
401         "boot    - boot default, i.e., run 'bootcmd'\n",
402         NULL
403 );
404
405 /* keep old command name "bootd" for backward compatibility */
406 U_BOOT_CMD(
407         bootd, 1,       1,      do_bootd,
408         "bootd   - boot default, i.e., run 'bootcmd'\n",
409         NULL
410 );
411
412 #endif
413
414
415 /*******************************************************************/
416 /* iminfo - print header info for a requested image */
417 /*******************************************************************/
418 #if defined(CONFIG_CMD_IMI)
419 int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
420 {
421         int     arg;
422         ulong   addr;
423         int     rcode = 0;
424
425         if (argc < 2) {
426                 return image_info (load_addr);
427         }
428
429         for (arg = 1; arg < argc; ++arg) {
430                 addr = simple_strtoul (argv[arg], NULL, 16);
431                 if (image_info (addr) != 0)
432                         rcode = 1;
433         }
434         return rcode;
435 }
436
437 static int image_info (ulong addr)
438 {
439         image_header_t *hdr = (image_header_t *)addr;
440
441         printf ("\n## Checking Image at %08lx ...\n", addr);
442
443         if (!image_check_magic (hdr)) {
444                 puts ("   Bad Magic Number\n");
445                 return 1;
446         }
447
448         if (!image_check_hcrc (hdr)) {
449                 puts ("   Bad Header Checksum\n");
450                 return 1;
451         }
452
453         print_image_hdr (hdr);
454
455         puts ("   Verifying Checksum ... ");
456         if (!image_check_dcrc (hdr)) {
457                 puts ("   Bad Data CRC\n");
458                 return 1;
459         }
460         puts ("OK\n");
461         return 0;
462 }
463
464 U_BOOT_CMD(
465         iminfo, CFG_MAXARGS,    1,      do_iminfo,
466         "iminfo  - print header information for application image\n",
467         "addr [addr ...]\n"
468         "    - print header information for application image starting at\n"
469         "      address 'addr' in memory; this includes verification of the\n"
470         "      image contents (magic number, header and payload checksums)\n"
471 );
472 #endif
473
474
475 /*******************************************************************/
476 /* imls - list all images found in flash */
477 /*******************************************************************/
478 #if defined(CONFIG_CMD_IMLS)
479 int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
480 {
481         flash_info_t *info;
482         int i, j;
483         image_header_t *hdr;
484
485         for (i = 0, info = &flash_info[0];
486                 i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
487
488                 if (info->flash_id == FLASH_UNKNOWN)
489                         goto next_bank;
490                 for (j = 0; j < info->sector_count; ++j) {
491
492                         hdr = (image_header_t *)info->start[j];
493
494                         if (!hdr || !image_check_magic (hdr))
495                                 goto next_sector;
496
497                         if (!image_check_hcrc (hdr))
498                                 goto next_sector;
499
500                         printf ("Image at %08lX:\n", (ulong)hdr);
501                         print_image_hdr (hdr);
502
503                         puts ("   Verifying Checksum ... ");
504                         if (!image_check_dcrc (hdr)) {
505                                 puts ("Bad Data CRC\n");
506                         } else {
507                                 puts ("OK\n");
508                         }
509 next_sector:            ;
510                 }
511 next_bank:      ;
512         }
513
514         return (0);
515 }
516
517 U_BOOT_CMD(
518         imls,   1,              1,      do_imls,
519         "imls    - list all images found in flash\n",
520         "\n"
521         "    - Prints information about all images found at sector\n"
522         "      boundaries in flash.\n"
523 );
524 #endif
525
526 /*******************************************************************/
527 /* */
528 /*******************************************************************/
529 void print_image_hdr (image_header_t *hdr)
530 {
531 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
532         time_t timestamp = (time_t)image_get_time (hdr);
533         struct rtc_time tm;
534 #endif
535
536         printf ("   Image Name:   %.*s\n", IH_NMLEN, image_get_name (hdr));
537
538 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
539         to_tm (timestamp, &tm);
540         printf ("   Created:      %4d-%02d-%02d  %2d:%02d:%02d UTC\n",
541                 tm.tm_year, tm.tm_mon, tm.tm_mday,
542                 tm.tm_hour, tm.tm_min, tm.tm_sec);
543 #endif
544         puts ("   Image Type:   ");
545         print_type (hdr);
546
547         printf ("\n   Data Size:    %d Bytes = ", image_get_data_size (hdr));
548         print_size (image_get_data_size (hdr), "\n");
549         printf ("   Load Address: %08x\n"
550                 "   Entry Point:  %08x\n",
551                  image_get_load (hdr), image_get_ep (hdr));
552
553         if (image_check_type (hdr, IH_TYPE_MULTI)) {
554                 int i;
555                 ulong data, len;
556                 ulong count = image_multi_count (hdr);
557
558                 puts ("   Contents:\n");
559                 for (i = 0; i < count; i++) {
560                         image_multi_getimg (hdr, i, &data, &len);
561                         printf ("   Image %d: %8ld Bytes = ", i, len);
562                         print_size (len, "\n");
563                 }
564         }
565 }
566
567 static void print_type (image_header_t *hdr)
568 {
569         char *os, *arch, *type, *comp;
570
571         switch (image_get_os (hdr)) {
572         case IH_OS_INVALID:     os = "Invalid OS";              break;
573         case IH_OS_NETBSD:      os = "NetBSD";                  break;
574         case IH_OS_LINUX:       os = "Linux";                   break;
575         case IH_OS_VXWORKS:     os = "VxWorks";                 break;
576         case IH_OS_QNX:         os = "QNX";                     break;
577         case IH_OS_U_BOOT:      os = "U-Boot";                  break;
578         case IH_OS_RTEMS:       os = "RTEMS";                   break;
579 #ifdef CONFIG_ARTOS
580         case IH_OS_ARTOS:       os = "ARTOS";                   break;
581 #endif
582 #ifdef CONFIG_LYNXKDI
583         case IH_OS_LYNXOS:      os = "LynxOS";                  break;
584 #endif
585         default:                os = "Unknown OS";              break;
586         }
587
588         switch (image_get_arch (hdr)) {
589         case IH_ARCH_INVALID:   arch = "Invalid CPU";           break;
590         case IH_ARCH_ALPHA:     arch = "Alpha";                 break;
591         case IH_ARCH_ARM:       arch = "ARM";                   break;
592         case IH_ARCH_AVR32:     arch = "AVR32";                 break;
593         case IH_ARCH_BLACKFIN:  arch = "Blackfin";              break;
594         case IH_ARCH_I386:      arch = "Intel x86";             break;
595         case IH_ARCH_IA64:      arch = "IA64";                  break;
596         case IH_ARCH_M68K:      arch = "M68K";                  break;
597         case IH_ARCH_MICROBLAZE:arch = "Microblaze";            break;
598         case IH_ARCH_MIPS64:    arch = "MIPS 64 Bit";           break;
599         case IH_ARCH_MIPS:      arch = "MIPS";                  break;
600         case IH_ARCH_NIOS2:     arch = "Nios-II";               break;
601         case IH_ARCH_NIOS:      arch = "Nios";                  break;
602         case IH_ARCH_PPC:       arch = "PowerPC";               break;
603         case IH_ARCH_S390:      arch = "IBM S390";              break;
604         case IH_ARCH_SH:        arch = "SuperH";                break;
605         case IH_ARCH_SPARC64:   arch = "SPARC 64 Bit";          break;
606         case IH_ARCH_SPARC:     arch = "SPARC";                 break;
607         default:                arch = "Unknown Architecture";  break;
608         }
609
610         switch (image_get_type (hdr)) {
611         case IH_TYPE_INVALID:   type = "Invalid Image";         break;
612         case IH_TYPE_STANDALONE:type = "Standalone Program";    break;
613         case IH_TYPE_KERNEL:    type = "Kernel Image";          break;
614         case IH_TYPE_RAMDISK:   type = "RAMDisk Image";         break;
615         case IH_TYPE_MULTI:     type = "Multi-File Image";      break;
616         case IH_TYPE_FIRMWARE:  type = "Firmware";              break;
617         case IH_TYPE_SCRIPT:    type = "Script";                break;
618         case IH_TYPE_FLATDT:    type = "Flat Device Tree";      break;
619         default:                type = "Unknown Image";         break;
620         }
621
622         switch (image_get_comp (hdr)) {
623         case IH_COMP_NONE:      comp = "uncompressed";          break;
624         case IH_COMP_GZIP:      comp = "gzip compressed";       break;
625         case IH_COMP_BZIP2:     comp = "bzip2 compressed";      break;
626         default:                comp = "unknown compression";   break;
627         }
628
629         printf ("%s %s %s (%s)", arch, os, type, comp);
630 }
631
632 #ifdef CONFIG_SILENT_CONSOLE
633 static void fixup_silent_linux ()
634 {
635         char buf[256], *start, *end;
636         char *cmdline = getenv ("bootargs");
637
638         /* Only fix cmdline when requested */
639         if (!(gd->flags & GD_FLG_SILENT))
640                 return;
641
642         debug ("before silent fix-up: %s\n", cmdline);
643         if (cmdline) {
644                 if ((start = strstr (cmdline, "console=")) != NULL) {
645                         end = strchr (start, ' ');
646                         strncpy (buf, cmdline, (start - cmdline + 8));
647                         if (end)
648                                 strcpy (buf + (start - cmdline + 8), end);
649                         else
650                                 buf[start - cmdline + 8] = '\0';
651                 } else {
652                         strcpy (buf, cmdline);
653                         strcat (buf, " console=");
654                 }
655         } else {
656                 strcpy (buf, "console=");
657         }
658
659         setenv ("bootargs", buf);
660         debug ("after silent fix-up: %s\n", buf);
661 }
662 #endif /* CONFIG_SILENT_CONSOLE */
663
664
665 /*******************************************************************/
666 /* OS booting routines */
667 /*******************************************************************/
668
669 static void do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
670                             int argc, char *argv[],
671                             image_header_t *hdr, int verify)
672 {
673         void (*loader)(bd_t *, image_header_t *, char *, char *);
674         image_header_t *img_addr;
675         ulong kernel_data, kernel_len;
676         char *consdev;
677         char *cmdline;
678
679         /*
680          * Booting a (NetBSD) kernel image
681          *
682          * This process is pretty similar to a standalone application:
683          * The (first part of an multi-) image must be a stage-2 loader,
684          * which in turn is responsible for loading & invoking the actual
685          * kernel.  The only differences are the parameters being passed:
686          * besides the board info strucure, the loader expects a command
687          * line, the name of the console device, and (optionally) the
688          * address of the original image header.
689          */
690
691         img_addr = 0;
692         if (image_check_type (hdr, IH_TYPE_MULTI)) {
693                 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
694                 if (kernel_len)
695                         img_addr = hdr;
696         }
697
698         consdev = "";
699 #if   defined (CONFIG_8xx_CONS_SMC1)
700         consdev = "smc1";
701 #elif defined (CONFIG_8xx_CONS_SMC2)
702         consdev = "smc2";
703 #elif defined (CONFIG_8xx_CONS_SCC2)
704         consdev = "scc2";
705 #elif defined (CONFIG_8xx_CONS_SCC3)
706         consdev = "scc3";
707 #endif
708
709         if (argc > 2) {
710                 ulong len;
711                 int   i;
712
713                 for (i = 2, len = 0; i < argc; i += 1)
714                         len += strlen (argv[i]) + 1;
715                 cmdline = malloc (len);
716
717                 for (i = 2, len = 0; i < argc; i += 1) {
718                         if (i > 2)
719                                 cmdline[len++] = ' ';
720                         strcpy (&cmdline[len], argv[i]);
721                         len += strlen (argv[i]);
722                 }
723         } else if ((cmdline = getenv ("bootargs")) == NULL) {
724                 cmdline = "";
725         }
726
727         loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
728
729         printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
730                 (ulong)loader);
731
732         show_boot_progress (15);
733
734         /*
735          * NetBSD Stage-2 Loader Parameters:
736          *   r3: ptr to board info data
737          *   r4: image address
738          *   r5: console device
739          *   r6: boot args string
740          */
741         (*loader) (gd->bd, img_addr, consdev, cmdline);
742 }
743
744 #ifdef CONFIG_LYNXKDI
745 static void do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
746                              int argc, char *argv[],
747                              image_header_t *hdr, int verify)
748 {
749         lynxkdi_boot (hdr);
750 }
751 #endif /* CONFIG_LYNXKDI */
752
753 static void do_bootm_rtems (cmd_tbl_t *cmdtp, int flag,
754                            int argc, char *argv[],
755                            image_header_t *hdr, int verify)
756 {
757         void (*entry_point)(bd_t *);
758
759         entry_point = (void (*)(bd_t *))image_get_ep (hdr);
760
761         printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
762                 (ulong)entry_point);
763
764         show_boot_progress (15);
765
766         /*
767          * RTEMS Parameters:
768          *   r3: ptr to board info data
769          */
770         (*entry_point)(gd->bd);
771 }
772
773 #if defined(CONFIG_CMD_ELF)
774 static void do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag,
775                              int argc, char *argv[],
776                              image_header_t *hdr, int verify)
777 {
778         char str[80];
779
780         sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
781         setenv("loadaddr", str);
782         do_bootvx(cmdtp, 0, 0, NULL);
783 }
784
785 static void do_bootm_qnxelf(cmd_tbl_t *cmdtp, int flag,
786                             int argc, char *argv[],
787                             image_header_t *hdr, int verify)
788 {
789         char *local_args[2];
790         char str[16];
791
792         sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
793         local_args[0] = argv[0];
794         local_args[1] = str;    /* and provide it via the arguments */
795         do_bootelf(cmdtp, 0, 2, local_args);
796 }
797 #endif
798
799 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
800 static void do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
801                            int argc, char *argv[],
802                            image_header_t *hdr, int verify)
803 {
804         ulong top;
805         char *s, *cmdline;
806         char **fwenv, **ss;
807         int i, j, nxt, len, envno, envsz;
808         bd_t *kbd;
809         void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
810
811         /*
812          * Booting an ARTOS kernel image + application
813          */
814
815         /* this used to be the top of memory, but was wrong... */
816 #ifdef CONFIG_PPC
817         /* get stack pointer */
818         asm volatile ("mr %0,1" : "=r"(top) );
819 #endif
820         debug ("## Current stack ends at 0x%08lX ", top);
821
822         top -= 2048;            /* just to be sure */
823         if (top > CFG_BOOTMAPSZ)
824                 top = CFG_BOOTMAPSZ;
825         top &= ~0xF;
826
827         debug ("=> set upper limit to 0x%08lX\n", top);
828
829         /* first check the artos specific boot args, then the linux args*/
830         if ((s = getenv( "abootargs")) == NULL && (s = getenv ("bootargs")) == NULL)
831                 s = "";
832
833         /* get length of cmdline, and place it */
834         len = strlen (s);
835         top = (top - (len + 1)) & ~0xF;
836         cmdline = (char *)top;
837         debug ("## cmdline at 0x%08lX ", top);
838         strcpy (cmdline, s);
839
840         /* copy bdinfo */
841         top = (top - sizeof (bd_t)) & ~0xF;
842         debug ("## bd at 0x%08lX ", top);
843         kbd = (bd_t *)top;
844         memcpy (kbd, gd->bd, sizeof (bd_t));
845
846         /* first find number of env entries, and their size */
847         envno = 0;
848         envsz = 0;
849         for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
850                 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
851                         ;
852                 envno++;
853                 envsz += (nxt - i) + 1; /* plus trailing zero */
854         }
855         envno++;        /* plus the terminating zero */
856         debug ("## %u envvars total size %u ", envno, envsz);
857
858         top = (top - sizeof (char **) * envno) & ~0xF;
859         fwenv = (char **)top;
860         debug ("## fwenv at 0x%08lX ", top);
861
862         top = (top - envsz) & ~0xF;
863         s = (char *)top;
864         ss = fwenv;
865
866         /* now copy them */
867         for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
868                 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
869                         ;
870                 *ss++ = s;
871                 for (j = i; j < nxt; ++j)
872                         *s++ = env_get_char (j);
873                 *s++ = '\0';
874         }
875         *ss++ = NULL;   /* terminate */
876
877         entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
878         (*entry) (kbd, cmdline, fwenv, top);
879 }
880 #endif