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