d4acb6b5bee0662daf35a99ae6af09fb859afa4c
[oweals/u-boot.git] / common / image.c
1 /*
2  * (C) Copyright 2008 Semihalf
3  *
4  * (C) Copyright 2000-2006
5  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25
26 #define DEBUG
27
28 #ifndef USE_HOSTCC
29 #include <common.h>
30 #include <watchdog.h>
31
32 #ifdef CONFIG_SHOW_BOOT_PROGRESS
33 #include <status_led.h>
34 #endif
35
36 #ifdef CONFIG_HAS_DATAFLASH
37 #include <dataflash.h>
38 #endif
39
40 #ifdef CONFIG_LOGBUFFER
41 #include <logbuff.h>
42 #endif
43
44 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
45 #include <rtc.h>
46 #endif
47
48 #if defined(CONFIG_FIT)
49 #include <fdt.h>
50 #include <libfdt.h>
51 #include <fdt_support.h>
52 #endif
53
54 #ifdef CONFIG_CMD_BDI
55 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
56 #endif
57
58 DECLARE_GLOBAL_DATA_PTR;
59
60 static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
61                 int argc, char *argv[],
62                 ulong rd_addr, uint8_t arch, int verify);
63 #else
64 #include "mkimage.h"
65 #endif /* USE_HOSTCC*/
66
67 #include <image.h>
68
69 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
70
71 int image_check_hcrc (image_header_t *hdr)
72 {
73         ulong hcrc;
74         ulong len = image_get_header_size ();
75         image_header_t header;
76
77         /* Copy header so we can blank CRC field for re-calculation */
78         memmove (&header, (char *)hdr, image_get_header_size ());
79         image_set_hcrc (&header, 0);
80
81         hcrc = crc32 (0, (unsigned char *)&header, len);
82
83         return (hcrc == image_get_hcrc (hdr));
84 }
85
86 int image_check_dcrc (image_header_t *hdr)
87 {
88         ulong data = image_get_data (hdr);
89         ulong len = image_get_data_size (hdr);
90         ulong dcrc = crc32 (0, (unsigned char *)data, len);
91
92         return (dcrc == image_get_dcrc (hdr));
93 }
94
95 #ifndef USE_HOSTCC
96 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
97 {
98         ulong dcrc = 0;
99         ulong len = image_get_data_size (hdr);
100         ulong data = image_get_data (hdr);
101
102 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
103         ulong cdata = data;
104         ulong edata = cdata + len;
105
106         while (cdata < edata) {
107                 ulong chunk = edata - cdata;
108
109                 if (chunk > chunksz)
110                         chunk = chunksz;
111                 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
112                 cdata += chunk;
113
114                 WATCHDOG_RESET ();
115         }
116 #else
117         dcrc = crc32 (0, (unsigned char *)data, len);
118 #endif
119
120         return (dcrc == image_get_dcrc (hdr));
121 }
122
123 int getenv_verify (void)
124 {
125         char *s = getenv ("verify");
126         return (s && (*s == 'n')) ? 0 : 1;
127 }
128
129 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
130 {
131 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
132         while (len > 0) {
133                 size_t tail = (len > chunksz) ? chunksz : len;
134                 WATCHDOG_RESET ();
135                 memmove (to, from, tail);
136                 to += tail;
137                 from += tail;
138                 len -= tail;
139         }
140 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
141         memmove (to, from, len);
142 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
143 }
144 #endif /* USE_HOSTCC */
145
146 /**
147  * image_multi_count - get component (sub-image) count
148  * @hdr: pointer to the header of the multi component image
149  *
150  * image_multi_count() returns number of components in a multi
151  * component image.
152  *
153  * Note: no checking of the image type is done, caller must pass
154  * a valid multi component image.
155  *
156  * returns:
157  *     number of components
158  */
159 ulong image_multi_count (image_header_t *hdr)
160 {
161         ulong i, count = 0;
162         ulong *size;
163
164         /* get start of the image payload, which in case of multi
165          * component images that points to a table of component sizes */
166         size = (ulong *)image_get_data (hdr);
167
168         /* count non empty slots */
169         for (i = 0; size[i]; ++i)
170                 count++;
171
172         return count;
173 }
174
175 /**
176  * image_multi_getimg - get component data address and size
177  * @hdr: pointer to the header of the multi component image
178  * @idx: index of the requested component
179  * @data: pointer to a ulong variable, will hold component data address
180  * @len: pointer to a ulong variable, will hold component size
181  *
182  * image_multi_getimg() returns size and data address for the requested
183  * component in a multi component image.
184  *
185  * Note: no checking of the image type is done, caller must pass
186  * a valid multi component image.
187  *
188  * returns:
189  *     data address and size of the component, if idx is valid
190  *     0 in data and len, if idx is out of range
191  */
192 void image_multi_getimg (image_header_t *hdr, ulong idx,
193                         ulong *data, ulong *len)
194 {
195         int i;
196         ulong *size;
197         ulong offset, tail, count, img_data;
198
199         /* get number of component */
200         count = image_multi_count (hdr);
201
202         /* get start of the image payload, which in case of multi
203          * component images that points to a table of component sizes */
204         size = (ulong *)image_get_data (hdr);
205
206         /* get address of the proper component data start, which means
207          * skipping sizes table (add 1 for last, null entry) */
208         img_data = image_get_data (hdr) + (count + 1) * sizeof (ulong);
209
210         if (idx < count) {
211                 *len = size[idx];
212                 offset = 0;
213                 tail = 0;
214
215                 /* go over all indices preceding requested component idx */
216                 for (i = 0; i < idx; i++) {
217                         /* add up i-th component size */
218                         offset += size[i];
219
220                         /* add up alignment for i-th component */
221                         tail += (4 - size[i] % 4);
222                 }
223
224                 /* calculate idx-th component data address */
225                 *data = img_data + offset + tail;
226         } else {
227                 *len = 0;
228                 *data = 0;
229         }
230 }
231
232 #ifndef USE_HOSTCC
233 const char* image_get_os_name (uint8_t os)
234 {
235         const char *name;
236
237         switch (os) {
238         case IH_OS_INVALID:     name = "Invalid OS";            break;
239         case IH_OS_NETBSD:      name = "NetBSD";                break;
240         case IH_OS_LINUX:       name = "Linux";                 break;
241         case IH_OS_VXWORKS:     name = "VxWorks";               break;
242         case IH_OS_QNX:         name = "QNX";                   break;
243         case IH_OS_U_BOOT:      name = "U-Boot";                break;
244         case IH_OS_RTEMS:       name = "RTEMS";                 break;
245 #ifdef CONFIG_ARTOS
246         case IH_OS_ARTOS:       name = "ARTOS";                 break;
247 #endif
248 #ifdef CONFIG_LYNXKDI
249         case IH_OS_LYNXOS:      name = "LynxOS";                break;
250 #endif
251         default:                name = "Unknown OS";            break;
252         }
253
254         return name;
255 }
256
257 const char* image_get_arch_name (uint8_t arch)
258 {
259         const char *name;
260
261         switch (arch) {
262         case IH_ARCH_INVALID:   name = "Invalid Architecture";  break;
263         case IH_ARCH_ALPHA:     name = "Alpha";                 break;
264         case IH_ARCH_ARM:       name = "ARM";                   break;
265         case IH_ARCH_AVR32:     name = "AVR32";                 break;
266         case IH_ARCH_BLACKFIN:  name = "Blackfin";              break;
267         case IH_ARCH_I386:      name = "Intel x86";             break;
268         case IH_ARCH_IA64:      name = "IA64";                  break;
269         case IH_ARCH_M68K:      name = "M68K";                  break;
270         case IH_ARCH_MICROBLAZE:name = "Microblaze";            break;
271         case IH_ARCH_MIPS64:    name = "MIPS 64 Bit";           break;
272         case IH_ARCH_MIPS:      name = "MIPS";                  break;
273         case IH_ARCH_NIOS2:     name = "Nios-II";               break;
274         case IH_ARCH_NIOS:      name = "Nios";                  break;
275         case IH_ARCH_PPC:       name = "PowerPC";               break;
276         case IH_ARCH_S390:      name = "IBM S390";              break;
277         case IH_ARCH_SH:        name = "SuperH";                break;
278         case IH_ARCH_SPARC64:   name = "SPARC 64 Bit";          break;
279         case IH_ARCH_SPARC:     name = "SPARC";                 break;
280         default:                name = "Unknown Architecture";  break;
281         }
282
283         return name;
284 }
285
286 const char* image_get_type_name (uint8_t type)
287 {
288         const char *name;
289
290         switch (type) {
291         case IH_TYPE_INVALID:   name = "Invalid Image";         break;
292         case IH_TYPE_STANDALONE:name = "Standalone Program";    break;
293         case IH_TYPE_KERNEL:    name = "Kernel Image";          break;
294         case IH_TYPE_RAMDISK:   name = "RAMDisk Image";         break;
295         case IH_TYPE_MULTI:     name = "Multi-File Image";      break;
296         case IH_TYPE_FIRMWARE:  name = "Firmware";              break;
297         case IH_TYPE_SCRIPT:    name = "Script";                break;
298         case IH_TYPE_FLATDT:    name = "Flat Device Tree";      break;
299         default:                name = "Unknown Image";         break;
300         }
301
302         return name;
303 }
304
305 const char* image_get_comp_name (uint8_t comp)
306 {
307         const char *name;
308
309         switch (comp) {
310         case IH_COMP_NONE:      name = "uncompressed";          break;
311         case IH_COMP_GZIP:      name = "gzip compressed";       break;
312         case IH_COMP_BZIP2:     name = "bzip2 compressed";      break;
313         default:                name = "unknown compression";   break;
314         }
315
316         return name;
317 }
318
319 static void image_print_type (image_header_t *hdr)
320 {
321         const char *os, *arch, *type, *comp;
322
323         os = image_get_os_name (image_get_os (hdr));
324         arch = image_get_arch_name (image_get_arch (hdr));
325         type = image_get_type_name (image_get_type (hdr));
326         comp = image_get_comp_name (image_get_comp (hdr));
327
328         printf ("%s %s %s (%s)", arch, os, type, comp);
329 }
330
331 void image_print_contents (image_header_t *hdr)
332 {
333 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
334         time_t timestamp = (time_t)image_get_time (hdr);
335         struct rtc_time tm;
336 #endif
337
338         printf ("   Image Name:   %.*s\n", IH_NMLEN, image_get_name (hdr));
339
340 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
341         to_tm (timestamp, &tm);
342         printf ("   Created:      %4d-%02d-%02d  %2d:%02d:%02d UTC\n",
343                 tm.tm_year, tm.tm_mon, tm.tm_mday,
344                 tm.tm_hour, tm.tm_min, tm.tm_sec);
345 #endif
346         puts ("   Image Type:   ");
347         image_print_type (hdr);
348
349         printf ("\n   Data Size:    %d Bytes = ", image_get_data_size (hdr));
350         print_size (image_get_data_size (hdr), "\n");
351         printf ("   Load Address: %08x\n"
352                 "   Entry Point:  %08x\n",
353                  image_get_load (hdr), image_get_ep (hdr));
354
355         if (image_check_type (hdr, IH_TYPE_MULTI)) {
356                 int i;
357                 ulong data, len;
358                 ulong count = image_multi_count (hdr);
359
360                 puts ("   Contents:\n");
361                 for (i = 0; i < count; i++) {
362                         image_multi_getimg (hdr, i, &data, &len);
363                         printf ("   Image %d: %8ld Bytes = ", i, len);
364                         print_size (len, "\n");
365                 }
366         }
367 }
368
369 /**
370  * gen_image_get_format - get image format type
371  * @img_addr: image start address
372  *
373  * gen_image_get_format() checks whether provided address points to a valid
374  * legacy or FIT image.
375  *
376  * New uImage format and FDT blob are based on a libfdt. FDT blob
377  * may be passed directly or embedded in a FIT image. In both situations
378  * gen_image_get_format() must be able to dectect libfdt header.
379  *
380  * returns:
381  *     image format type or IMAGE_FORMAT_INVALID if no image is present
382  */
383 int gen_image_get_format (void *img_addr)
384 {
385         ulong           format = IMAGE_FORMAT_INVALID;
386         image_header_t  *hdr;
387 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
388         char            *fit_hdr;
389 #endif
390
391         hdr = (image_header_t *)img_addr;
392         if (image_check_magic(hdr))
393                 format = IMAGE_FORMAT_LEGACY;
394 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
395         else {
396                 fit_hdr = (char *)img_addr;
397                 if (fdt_check_header (fit_hdr) == 0)
398                         format = IMAGE_FORMAT_FIT;
399         }
400 #endif
401
402         return format;
403 }
404
405 /**
406  * gen_get_image - get image from special storage (if necessary)
407  * @img_addr: image start address
408  *
409  * gen_get_image() checks if provided image start adddress is located
410  * in a dataflash storage. If so, image is moved to a system RAM memory.
411  *
412  * returns:
413  *     image start address after possible relocation from special storage
414  */
415 ulong gen_get_image (ulong img_addr)
416 {
417         ulong ram_addr = img_addr;
418
419 #ifdef CONFIG_HAS_DATAFLASH
420         ulong h_size, d_size;
421
422         if (addr_dataflash (img_addr)){
423                 /* ger RAM address */
424                 ram_addr = CFG_LOAD_ADDR;
425
426                 /* get header size */
427                 h_size = image_get_header_size ();
428 #if defined(CONFIG_FIT)
429                 if (sizeof(struct fdt_header) > h_size)
430                         h_size = sizeof(struct fdt_header);
431 #endif
432
433                 /* read in header */
434                 debug ("   Reading image header from dataflash address "
435                         "%08lx to RAM address %08lx\n", img_addr, ram_addr);
436
437                 read_dataflash (img_addr, h_size, (char *)ram_addr);
438
439                 /* get data size */
440                 switch (gen_image_get_format ((void *)ram_addr)) {
441                 case IMAGE_FORMAT_LEGACY:
442                         d_size = image_get_data_size ((image_header_t *)ram_addr);
443                         debug ("   Legacy format image found at 0x%08lx, size 0x%08lx\n",
444                                         ram_addr, d_size);
445                         break;
446 #if defined(CONFIG_FIT)
447                 case IMAGE_FORMAT_FIT:
448                         d_size = fdt_totalsize((void *)ram_addr) - h_size;
449                         debug ("   FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
450                                         ram_addr, d_size);
451                         break;
452 #endif
453                 default:
454                         printf ("   No valid image found at 0x%08lx\n", img_addr);
455                         return ram_addr;
456                 }
457
458                 /* read in image data */
459                 debug ("   Reading image remaining data from dataflash address "
460                         "%08lx to RAM address %08lx\n", img_addr + h_size,
461                         ram_addr + h_size);
462
463                 read_dataflash (img_addr + h_size, d_size,
464                                 (char *)(ram_addr + h_size));
465
466         }
467 #endif /* CONFIG_HAS_DATAFLASH */
468
469         return ram_addr;
470 }
471
472 /**
473  * image_get_ramdisk - get and verify ramdisk image
474  * @cmdtp: command table pointer
475  * @flag: command flag
476  * @argc: command argument count
477  * @argv: command argument list
478  * @rd_addr: ramdisk image start address
479  * @arch: expected ramdisk architecture
480  * @verify: checksum verification flag
481  *
482  * image_get_ramdisk() returns a pointer to the verified ramdisk image
483  * header. Routine receives image start address and expected architecture
484  * flag. Verification done covers data and header integrity and os/type/arch
485  * fields checking.
486  *
487  * If dataflash support is enabled routine checks for dataflash addresses
488  * and handles required dataflash reads.
489  *
490  * returns:
491  *     pointer to a ramdisk image header, if image was found and valid
492  *     otherwise, return NULL
493  */
494 static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
495                 int argc, char *argv[],
496                 ulong rd_addr, uint8_t arch, int verify)
497 {
498         image_header_t *rd_hdr;
499
500         show_boot_progress (9);
501         rd_hdr = (image_header_t *)rd_addr;
502
503         if (!image_check_magic (rd_hdr)) {
504                 puts ("Bad Magic Number\n");
505                 show_boot_progress (-10);
506                 return NULL;
507         }
508
509         if (!image_check_hcrc (rd_hdr)) {
510                 puts ("Bad Header Checksum\n");
511                 show_boot_progress (-11);
512                 return NULL;
513         }
514
515         show_boot_progress (10);
516         image_print_contents (rd_hdr);
517
518         if (verify) {
519                 puts("   Verifying Checksum ... ");
520                 if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
521                         puts ("Bad Data CRC\n");
522                         show_boot_progress (-12);
523                         return NULL;
524                 }
525                 puts("OK\n");
526         }
527
528         show_boot_progress (11);
529
530         if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
531             !image_check_arch (rd_hdr, arch) ||
532             !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
533                 printf ("No Linux %s Ramdisk Image\n",
534                                 image_get_arch_name(arch));
535                 show_boot_progress (-13);
536                 return NULL;
537         }
538
539         return rd_hdr;
540 }
541
542 /**
543  * get_ramdisk - main ramdisk handling routine
544  * @cmdtp: command table pointer
545  * @flag: command flag
546  * @argc: command argument count
547  * @argv: command argument list
548  * @images: pointer to the bootm images structure
549  * @arch: expected ramdisk architecture
550  * @rd_start: pointer to a ulong variable, will hold ramdisk start address
551  * @rd_end: pointer to a ulong variable, will hold ramdisk end
552  *
553  * get_ramdisk() is responsible for finding a valid ramdisk image.
554  * Curently supported are the following ramdisk sources:
555  *      - multicomponent kernel/ramdisk image,
556  *      - commandline provided address of decicated ramdisk image.
557  *
558  * returns:
559  *     rd_start and rd_end are set to ramdisk start/end addresses if
560  *     ramdisk image is found and valid
561  *     rd_start and rd_end are set to 0 if no ramdisk exists
562  *     return 1 if ramdisk image is found but corrupted
563  */
564 int get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
565                 bootm_headers_t *images, uint8_t arch,
566                 ulong *rd_start, ulong *rd_end)
567 {
568         ulong rd_addr, rd_load;
569         ulong rd_data, rd_len;
570         image_header_t *rd_hdr;
571 #if defined(CONFIG_FIT)
572         void            *fit_hdr;
573         const char      *fit_uname_config = NULL;
574         const char      *fit_uname_ramdisk = NULL;
575         ulong           default_addr;
576 #endif
577
578         /*
579          * Look for a '-' which indicates to ignore the
580          * ramdisk argument
581          */
582         if ((argc >= 3) && (strcmp(argv[2], "-") ==  0)) {
583                 debug ("## Skipping init Ramdisk\n");
584                 rd_len = rd_data = 0;
585         } else if (argc >= 3) {
586 #if defined(CONFIG_FIT)
587                 /*
588                  * If the init ramdisk comes from the FIT image and the FIT image
589                  * address is omitted in the command line argument, try to use
590                  * os FIT image address or default load address.
591                  */
592                 if (images->fit_uname_os)
593                         default_addr = (ulong)images->fit_hdr_os;
594                 else
595                         default_addr = load_addr;
596
597                 if (fit_parse_conf (argv[2], default_addr,
598                                         &rd_addr, &fit_uname_config)) {
599                         debug ("*  ramdisk: config '%s' from image at 0x%08lx\n",
600                                         fit_uname_config, rd_addr);
601                 } else if (fit_parse_subimage (argv[2], default_addr,
602                                         &rd_addr, &fit_uname_ramdisk)) {
603                         debug ("*  ramdisk: subimage '%s' from image at 0x%08lx\n",
604                                         fit_uname_ramdisk, rd_addr);
605                 } else
606 #endif
607                 {
608                         rd_addr = simple_strtoul(argv[2], NULL, 16);
609                         debug ("*  ramdisk: cmdline image address = 0x%08lx\n",
610                                         rd_addr);
611                 }
612
613                 /* copy from dataflash if needed */
614                 printf ("## Loading init Ramdisk Image at %08lx ...\n",
615                                 rd_addr);
616                 rd_addr = gen_get_image (rd_addr);
617
618                 /*
619                  * Check if there is an initrd image at the
620                  * address provided in the second bootm argument
621                  * check image type, for FIT images get FIT node.
622                  */
623                 switch (gen_image_get_format ((void *)rd_addr)) {
624                 case IMAGE_FORMAT_LEGACY:
625
626                         debug ("*  ramdisk: legacy format image\n");
627
628                         rd_hdr = image_get_ramdisk (cmdtp, flag, argc, argv,
629                                                 rd_addr, arch, images->verify);
630
631                         if (rd_hdr == NULL) {
632                                 *rd_start = 0;
633                                 *rd_end = 0;
634                                 return 1;
635                         }
636
637                         rd_data = image_get_data (rd_hdr);
638                         rd_len = image_get_data_size (rd_hdr);
639                         rd_load = image_get_load (rd_hdr);
640                         break;
641 #if defined(CONFIG_FIT)
642                 case IMAGE_FORMAT_FIT:
643                         fit_hdr = (void *)rd_addr;
644                         debug ("*  ramdisk: FIT format image\n");
645                         fit_unsupported_reset ("ramdisk");
646                         return 1;
647 #endif
648                 default:
649                         printf ("Wrong Image Format for %s command\n",
650                                         cmdtp->name);
651                         rd_data = rd_len = 0;
652                 }
653
654 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
655                 /*
656                  * We need to copy the ramdisk to SRAM to let Linux boot
657                  */
658                 if (rd_data) {
659                         memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
660                         rd_data = rd_load;
661                 }
662 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
663
664         } else if (images->legacy_hdr_valid &&
665                         image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
666                 /*
667                  * Now check if we have a legacy mult-component image,
668                  * get second entry data start address and len.
669                  */
670                 show_boot_progress (13);
671                 printf ("## Loading init Ramdisk from multi component "
672                                 "Image at %08lx ...\n",
673                                 (ulong)images->legacy_hdr_os);
674
675                 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
676         } else {
677                 /*
678                  * no initrd image
679                  */
680                 show_boot_progress (14);
681                 rd_len = rd_data = 0;
682         }
683
684         if (!rd_data) {
685                 debug ("## No init Ramdisk\n");
686                 *rd_start = 0;
687                 *rd_end = 0;
688         } else {
689                 *rd_start = rd_data;
690                 *rd_end = rd_data + rd_len;
691         }
692         debug ("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
693                         *rd_start, *rd_end);
694
695         return 0;
696 }
697
698 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
699 /**
700  * ramdisk_high - relocate init ramdisk
701  * @rd_data: ramdisk data start address
702  * @rd_len: ramdisk data length
703  * @sp_limit: stack pointer limit (including BOOTMAPSZ)
704  * @sp: current stack pointer
705  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
706  *      start address (after possible relocation)
707  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
708  *      end address (after possible relocation)
709  *
710  * ramdisk_high() takes a relocation hint from "initrd_high" environement
711  * variable and if requested ramdisk data is moved to a specified location.
712  *
713  * returns:
714  *     - initrd_start and initrd_end are set to final (after relocation) ramdisk
715  *     start/end addresses if ramdisk image start and len were provided
716  *     otherwise set initrd_start and initrd_end set to zeros
717  *     - returns new allc_current, next free address below BOOTMAPSZ
718  */
719 ulong ramdisk_high (ulong alloc_current, ulong rd_data, ulong rd_len,
720                 ulong sp_limit, ulong sp,
721                 ulong *initrd_start, ulong *initrd_end)
722 {
723         char    *s;
724         ulong   initrd_high;
725         int     initrd_copy_to_ram = 1;
726         ulong   new_alloc_current = alloc_current;
727
728         if ((s = getenv ("initrd_high")) != NULL) {
729                 /* a value of "no" or a similar string will act like 0,
730                  * turning the "load high" feature off. This is intentional.
731                  */
732                 initrd_high = simple_strtoul (s, NULL, 16);
733                 if (initrd_high == ~0)
734                         initrd_copy_to_ram = 0;
735         } else {
736                 /* not set, no restrictions to load high */
737                 initrd_high = ~0;
738         }
739
740 #ifdef CONFIG_LOGBUFFER
741         /* Prevent initrd from overwriting logbuffer */
742         if (initrd_high < (gd->bd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD))
743             initrd_high = gd->bd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD;
744         debug ("## Logbuffer at 0x%08lx ", gd->bd->bi_memsize - LOGBUFF_LEN);
745 #endif
746         debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
747                         initrd_high, initrd_copy_to_ram);
748
749         if (rd_data) {
750                 if (!initrd_copy_to_ram) {      /* zero-copy ramdisk support */
751                         debug ("   in-place initrd\n");
752                         *initrd_start = rd_data;
753                         *initrd_end = rd_data + rd_len;
754                 } else {
755                         new_alloc_current = alloc_current - rd_len;
756                         *initrd_start  = new_alloc_current;
757                         *initrd_start &= ~(4096 - 1);   /* align on page */
758
759                         if (initrd_high) {
760                                 ulong nsp;
761
762                                 /*
763                                  * the inital ramdisk does not need to be within
764                                  * CFG_BOOTMAPSZ as it is not accessed until after
765                                  * the mm system is initialised.
766                                  *
767                                  * do the stack bottom calculation again and see if
768                                  * the initrd will fit just below the monitor stack
769                                  * bottom without overwriting the area allocated
770                                  * for command line args and board info.
771                                  */
772                                 nsp = sp;
773                                 nsp -= 2048;            /* just to be sure */
774                                 nsp &= ~0xF;
775
776                                 if (nsp > initrd_high)  /* limit as specified */
777                                         nsp = initrd_high;
778
779                                 nsp -= rd_len;
780                                 nsp &= ~(4096 - 1);     /* align on page */
781
782                                 if (nsp >= sp_limit) {
783                                         *initrd_start = nsp;
784                                         new_alloc_current = alloc_current;
785                                 }
786                         }
787
788                         show_boot_progress (12);
789
790                         *initrd_end = *initrd_start + rd_len;
791                         printf ("   Loading Ramdisk to %08lx, end %08lx ... ",
792                                         *initrd_start, *initrd_end);
793
794                         memmove_wd((void *)*initrd_start,
795                                         (void *)rd_data, rd_len, CHUNKSZ);
796
797                         puts ("OK\n");
798                 }
799         } else {
800                 *initrd_start = 0;
801                 *initrd_end = 0;
802         }
803         debug ("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
804                         *initrd_start, *initrd_end);
805
806         return new_alloc_current;
807 }
808
809 /**
810  * get_boot_sp_limit - calculate stack pointer limit
811  * @sp: current stack pointer
812  *
813  * get_boot_sp_limit() takes current stack pointer adrress and calculates
814  * stack pointer limit, below which kernel boot data (cmdline, board info,
815  * etc.) will be allocated.
816  *
817  * returns:
818  *     stack pointer limit
819  */
820 ulong get_boot_sp_limit(ulong sp)
821 {
822         ulong sp_limit = sp;
823
824         sp_limit -= 2048;       /* just to be sure */
825
826         /* make sure sp_limit is within kernel mapped space */
827         if (sp_limit > CFG_BOOTMAPSZ)
828                 sp_limit = CFG_BOOTMAPSZ;
829         sp_limit &= ~0xF;
830
831         return sp_limit;
832 }
833
834 /**
835  * get_boot_cmdline - allocate and initialize kernel cmdline
836  * @alloc_current: current boot allocation address (counting down
837  *      from sp_limit)
838  * @cmd_start: pointer to a ulong variable, will hold cmdline start
839  * @cmd_end: pointer to a ulong variable, will hold cmdline end
840  *
841  * get_boot_cmdline() allocates space for kernel command line below
842  * provided alloc_current address. If "bootargs" U-boot environemnt
843  * variable is present its contents is copied to allocated kernel
844  * command line.
845  *
846  * returns:
847  *     alloc_current after cmdline allocation
848  */
849 ulong get_boot_cmdline (ulong alloc_current, ulong *cmd_start, ulong *cmd_end)
850 {
851         char *cmdline;
852         char *s;
853
854         cmdline = (char *)((alloc_current - CFG_BARGSIZE) & ~0xF);
855
856         if ((s = getenv("bootargs")) == NULL)
857                 s = "";
858
859         strcpy(cmdline, s);
860
861         *cmd_start = (ulong) & cmdline[0];
862         *cmd_end = *cmd_start + strlen(cmdline);
863
864         debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
865
866         return (ulong)cmdline;
867 }
868
869 /**
870  * get_boot_kbd - allocate and initialize kernel copy of board info
871  * @alloc_current: current boot allocation address (counting down
872  *      from sp_limit)
873  * @kbd: double pointer to board info data
874  *
875  * get_boot_kbd() - allocates space for kernel copy of board info data.
876  * Space is allocated below provided alloc_current address and kernel
877  * board info is initialized with the current u-boot board info data.
878  *
879  * returns:
880  *     alloc_current after kbd allocation
881  */
882 ulong get_boot_kbd (ulong alloc_current, bd_t **kbd)
883 {
884         *kbd = (bd_t *) (((ulong)alloc_current - sizeof(bd_t)) & ~0xF);
885         **kbd = *(gd->bd);
886
887         debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
888
889 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
890         do_bdinfo(NULL, 0, 0, NULL);
891 #endif
892
893         return (ulong)*kbd;
894 }
895 #endif /* CONFIG_PPC || CONFIG_M68K */
896
897 #if defined(CONFIG_FIT)
898 /*****************************************************************************/
899 /* New uImage format routines */
900 /*****************************************************************************/
901 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
902                 ulong *addr, const char **name)
903 {
904         const char *sep;
905
906         *addr = addr_curr;
907         *name = NULL;
908
909         sep = strchr (spec, sepc);
910         if (sep) {
911                 if (sep - spec > 0)
912                         *addr = simple_strtoul (spec, NULL, 16);
913
914                 *name = sep + 1;
915                 return 1;
916         }
917
918         return 0;
919 }
920
921 /**
922  * fit_parse_conf - parse FIT configuration spec
923  * @spec: input string, containing configuration spec
924  * @add_curr: current image address (to be used as a possible default)
925  * @addr: pointer to a ulong variable, will hold FIT image address of a given
926  * configuration
927  * @conf_name double pointer to a char, will hold pointer to a configuration
928  * unit name
929  *
930  * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
931  * where <addr> is a FIT image address that contains configuration
932  * with a <conf> unit name.
933  *
934  * Address part is optional, and if omitted default add_curr will
935  * be used instead.
936  *
937  * returns:
938  *     1 if spec is a valid configuration string,
939  *     addr and conf_name are set accordingly
940  *     0 otherwise
941  */
942 inline int fit_parse_conf (const char *spec, ulong addr_curr,
943                 ulong *addr, const char **conf_name)
944 {
945         return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
946 }
947
948 /**
949  * fit_parse_subimage - parse FIT subimage spec
950  * @spec: input string, containing subimage spec
951  * @add_curr: current image address (to be used as a possible default)
952  * @addr: pointer to a ulong variable, will hold FIT image address of a given
953  * subimage
954  * @image_name: double pointer to a char, will hold pointer to a subimage name
955  *
956  * fit_parse_subimage() expects subimage spec in the for of
957  * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
958  * subimage with a <subimg> unit name.
959  *
960  * Address part is optional, and if omitted default add_curr will
961  * be used instead.
962  *
963  * returns:
964  *     1 if spec is a valid subimage string,
965  *     addr and image_name are set accordingly
966  *     0 otherwise
967  */
968 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
969                 ulong *addr, const char **image_name)
970 {
971         return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
972 }
973
974 #endif /* CONFIG_FIT */
975
976 #endif /* USE_HOSTCC */