crc32: Use the crc.h header for crc functions
[oweals/u-boot.git] / common / image.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2008 Semihalf
4  *
5  * (C) Copyright 2000-2006
6  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7  */
8
9 #ifndef USE_HOSTCC
10 #include <common.h>
11 #include <env.h>
12 #include <u-boot/crc.h>
13 #include <watchdog.h>
14
15 #ifdef CONFIG_SHOW_BOOT_PROGRESS
16 #include <status_led.h>
17 #endif
18
19 #include <rtc.h>
20
21 #include <gzip.h>
22 #include <image.h>
23 #include <mapmem.h>
24
25 #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
26 #include <linux/libfdt.h>
27 #include <fdt_support.h>
28 #include <fpga.h>
29 #include <xilinx.h>
30 #endif
31
32 #include <u-boot/md5.h>
33 #include <u-boot/sha1.h>
34 #include <linux/errno.h>
35 #include <asm/io.h>
36
37 #include <bzlib.h>
38 #include <linux/lzo.h>
39 #include <lzma/LzmaTypes.h>
40 #include <lzma/LzmaDec.h>
41 #include <lzma/LzmaTools.h>
42
43 #ifdef CONFIG_CMD_BDI
44 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
45 #endif
46
47 DECLARE_GLOBAL_DATA_PTR;
48
49 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
50 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
51                                                 int verify);
52 #endif
53 #else
54 #include "mkimage.h"
55 #include <u-boot/md5.h>
56 #include <time.h>
57 #include <image.h>
58
59 #ifndef __maybe_unused
60 # define __maybe_unused         /* unimplemented */
61 #endif
62 #endif /* !USE_HOSTCC*/
63
64 #include <u-boot/crc.h>
65 #include <imximage.h>
66
67 #ifndef CONFIG_SYS_BARGSIZE
68 #define CONFIG_SYS_BARGSIZE 512
69 #endif
70
71 static const table_entry_t uimage_arch[] = {
72         {       IH_ARCH_INVALID,        "invalid",      "Invalid ARCH", },
73         {       IH_ARCH_ALPHA,          "alpha",        "Alpha",        },
74         {       IH_ARCH_ARM,            "arm",          "ARM",          },
75         {       IH_ARCH_I386,           "x86",          "Intel x86",    },
76         {       IH_ARCH_IA64,           "ia64",         "IA64",         },
77         {       IH_ARCH_M68K,           "m68k",         "M68K",         },
78         {       IH_ARCH_MICROBLAZE,     "microblaze",   "MicroBlaze",   },
79         {       IH_ARCH_MIPS,           "mips",         "MIPS",         },
80         {       IH_ARCH_MIPS64,         "mips64",       "MIPS 64 Bit",  },
81         {       IH_ARCH_NIOS2,          "nios2",        "NIOS II",      },
82         {       IH_ARCH_PPC,            "powerpc",      "PowerPC",      },
83         {       IH_ARCH_PPC,            "ppc",          "PowerPC",      },
84         {       IH_ARCH_S390,           "s390",         "IBM S390",     },
85         {       IH_ARCH_SH,             "sh",           "SuperH",       },
86         {       IH_ARCH_SPARC,          "sparc",        "SPARC",        },
87         {       IH_ARCH_SPARC64,        "sparc64",      "SPARC 64 Bit", },
88         {       IH_ARCH_BLACKFIN,       "blackfin",     "Blackfin",     },
89         {       IH_ARCH_AVR32,          "avr32",        "AVR32",        },
90         {       IH_ARCH_NDS32,          "nds32",        "NDS32",        },
91         {       IH_ARCH_OPENRISC,       "or1k",         "OpenRISC 1000",},
92         {       IH_ARCH_SANDBOX,        "sandbox",      "Sandbox",      },
93         {       IH_ARCH_ARM64,          "arm64",        "AArch64",      },
94         {       IH_ARCH_ARC,            "arc",          "ARC",          },
95         {       IH_ARCH_X86_64,         "x86_64",       "AMD x86_64",   },
96         {       IH_ARCH_XTENSA,         "xtensa",       "Xtensa",       },
97         {       IH_ARCH_RISCV,          "riscv",        "RISC-V",       },
98         {       -1,                     "",             "",             },
99 };
100
101 static const table_entry_t uimage_os[] = {
102         {       IH_OS_INVALID,  "invalid",      "Invalid OS",           },
103         {       IH_OS_ARM_TRUSTED_FIRMWARE, "arm-trusted-firmware", "ARM Trusted Firmware"  },
104         {       IH_OS_LINUX,    "linux",        "Linux",                },
105 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
106         {       IH_OS_LYNXOS,   "lynxos",       "LynxOS",               },
107 #endif
108         {       IH_OS_NETBSD,   "netbsd",       "NetBSD",               },
109         {       IH_OS_OSE,      "ose",          "Enea OSE",             },
110         {       IH_OS_PLAN9,    "plan9",        "Plan 9",               },
111         {       IH_OS_RTEMS,    "rtems",        "RTEMS",                },
112         {       IH_OS_TEE,      "tee",          "Trusted Execution Environment" },
113         {       IH_OS_U_BOOT,   "u-boot",       "U-Boot",               },
114         {       IH_OS_VXWORKS,  "vxworks",      "VxWorks",              },
115 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
116         {       IH_OS_QNX,      "qnx",          "QNX",                  },
117 #endif
118 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
119         {       IH_OS_INTEGRITY,"integrity",    "INTEGRITY",            },
120 #endif
121 #ifdef USE_HOSTCC
122         {       IH_OS_4_4BSD,   "4_4bsd",       "4_4BSD",               },
123         {       IH_OS_DELL,     "dell",         "Dell",                 },
124         {       IH_OS_ESIX,     "esix",         "Esix",                 },
125         {       IH_OS_FREEBSD,  "freebsd",      "FreeBSD",              },
126         {       IH_OS_IRIX,     "irix",         "Irix",                 },
127         {       IH_OS_NCR,      "ncr",          "NCR",                  },
128         {       IH_OS_OPENBSD,  "openbsd",      "OpenBSD",              },
129         {       IH_OS_PSOS,     "psos",         "pSOS",                 },
130         {       IH_OS_SCO,      "sco",          "SCO",                  },
131         {       IH_OS_SOLARIS,  "solaris",      "Solaris",              },
132         {       IH_OS_SVR4,     "svr4",         "SVR4",                 },
133 #endif
134 #if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
135         {       IH_OS_OPENRTOS, "openrtos",     "OpenRTOS",             },
136 #endif
137         {       IH_OS_OPENSBI,  "opensbi",      "RISC-V OpenSBI",       },
138
139         {       -1,             "",             "",                     },
140 };
141
142 static const table_entry_t uimage_type[] = {
143         {       IH_TYPE_AISIMAGE,   "aisimage",   "Davinci AIS image",},
144         {       IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image",   },
145         {       IH_TYPE_FIRMWARE,   "firmware",   "Firmware",           },
146         {       IH_TYPE_FLATDT,     "flat_dt",    "Flat Device Tree",   },
147         {       IH_TYPE_GPIMAGE,    "gpimage",    "TI Keystone SPL Image",},
148         {       IH_TYPE_KERNEL,     "kernel",     "Kernel Image",       },
149         {       IH_TYPE_KERNEL_NOLOAD, "kernel_noload",  "Kernel Image (no loading done)", },
150         {       IH_TYPE_KWBIMAGE,   "kwbimage",   "Kirkwood Boot Image",},
151         {       IH_TYPE_IMXIMAGE,   "imximage",   "Freescale i.MX Boot Image",},
152         {       IH_TYPE_IMX8IMAGE,  "imx8image",  "NXP i.MX8 Boot Image",},
153         {       IH_TYPE_IMX8MIMAGE, "imx8mimage", "NXP i.MX8M Boot Image",},
154         {       IH_TYPE_INVALID,    "invalid",    "Invalid Image",      },
155         {       IH_TYPE_MULTI,      "multi",      "Multi-File Image",   },
156         {       IH_TYPE_OMAPIMAGE,  "omapimage",  "TI OMAP SPL With GP CH",},
157         {       IH_TYPE_PBLIMAGE,   "pblimage",   "Freescale PBL Boot Image",},
158         {       IH_TYPE_RAMDISK,    "ramdisk",    "RAMDisk Image",      },
159         {       IH_TYPE_SCRIPT,     "script",     "Script",             },
160         {       IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SoCFPGA CV/AV preloader",},
161         {       IH_TYPE_SOCFPGAIMAGE_V1, "socfpgaimage_v1", "Altera SoCFPGA A10 preloader",},
162         {       IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
163         {       IH_TYPE_UBLIMAGE,   "ublimage",   "Davinci UBL image",},
164         {       IH_TYPE_MXSIMAGE,   "mxsimage",   "Freescale MXS Boot Image",},
165         {       IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
166         {       IH_TYPE_X86_SETUP,  "x86_setup",  "x86 setup.bin",    },
167         {       IH_TYPE_LPC32XXIMAGE, "lpc32xximage",  "LPC32XX Boot Image", },
168         {       IH_TYPE_RKIMAGE,    "rkimage",    "Rockchip Boot Image" },
169         {       IH_TYPE_RKSD,       "rksd",       "Rockchip SD Boot Image" },
170         {       IH_TYPE_RKSPI,      "rkspi",      "Rockchip SPI Boot Image" },
171         {       IH_TYPE_VYBRIDIMAGE, "vybridimage",  "Vybrid Boot Image", },
172         {       IH_TYPE_ZYNQIMAGE,  "zynqimage",  "Xilinx Zynq Boot Image" },
173         {       IH_TYPE_ZYNQMPIMAGE, "zynqmpimage", "Xilinx ZynqMP Boot Image" },
174         {       IH_TYPE_ZYNQMPBIF,  "zynqmpbif",  "Xilinx ZynqMP Boot Image (bif)" },
175         {       IH_TYPE_FPGA,       "fpga",       "FPGA Image" },
176         {       IH_TYPE_TEE,        "tee",        "Trusted Execution Environment Image",},
177         {       IH_TYPE_FIRMWARE_IVT, "firmware_ivt", "Firmware with HABv4 IVT" },
178         {       IH_TYPE_PMMC,        "pmmc",        "TI Power Management Micro-Controller Firmware",},
179         {       IH_TYPE_STM32IMAGE, "stm32image", "STMicroelectronics STM32 Image" },
180         {       IH_TYPE_MTKIMAGE,   "mtk_image",   "MediaTek BootROM loadable Image" },
181         {       IH_TYPE_COPRO, "copro", "Coprocessor Image"},
182         {       -1,                 "",           "",                   },
183 };
184
185 static const table_entry_t uimage_comp[] = {
186         {       IH_COMP_NONE,   "none",         "uncompressed",         },
187         {       IH_COMP_BZIP2,  "bzip2",        "bzip2 compressed",     },
188         {       IH_COMP_GZIP,   "gzip",         "gzip compressed",      },
189         {       IH_COMP_LZMA,   "lzma",         "lzma compressed",      },
190         {       IH_COMP_LZO,    "lzo",          "lzo compressed",       },
191         {       IH_COMP_LZ4,    "lz4",          "lz4 compressed",       },
192         {       -1,             "",             "",                     },
193 };
194
195 struct table_info {
196         const char *desc;
197         int count;
198         const table_entry_t *table;
199 };
200
201 static const struct table_info table_info[IH_COUNT] = {
202         { "architecture", IH_ARCH_COUNT, uimage_arch },
203         { "compression", IH_COMP_COUNT, uimage_comp },
204         { "operating system", IH_OS_COUNT, uimage_os },
205         { "image type", IH_TYPE_COUNT, uimage_type },
206 };
207
208 /*****************************************************************************/
209 /* Legacy format routines */
210 /*****************************************************************************/
211 int image_check_hcrc(const image_header_t *hdr)
212 {
213         ulong hcrc;
214         ulong len = image_get_header_size();
215         image_header_t header;
216
217         /* Copy header so we can blank CRC field for re-calculation */
218         memmove(&header, (char *)hdr, image_get_header_size());
219         image_set_hcrc(&header, 0);
220
221         hcrc = crc32(0, (unsigned char *)&header, len);
222
223         return (hcrc == image_get_hcrc(hdr));
224 }
225
226 int image_check_dcrc(const image_header_t *hdr)
227 {
228         ulong data = image_get_data(hdr);
229         ulong len = image_get_data_size(hdr);
230         ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
231
232         return (dcrc == image_get_dcrc(hdr));
233 }
234
235 /**
236  * image_multi_count - get component (sub-image) count
237  * @hdr: pointer to the header of the multi component image
238  *
239  * image_multi_count() returns number of components in a multi
240  * component image.
241  *
242  * Note: no checking of the image type is done, caller must pass
243  * a valid multi component image.
244  *
245  * returns:
246  *     number of components
247  */
248 ulong image_multi_count(const image_header_t *hdr)
249 {
250         ulong i, count = 0;
251         uint32_t *size;
252
253         /* get start of the image payload, which in case of multi
254          * component images that points to a table of component sizes */
255         size = (uint32_t *)image_get_data(hdr);
256
257         /* count non empty slots */
258         for (i = 0; size[i]; ++i)
259                 count++;
260
261         return count;
262 }
263
264 /**
265  * image_multi_getimg - get component data address and size
266  * @hdr: pointer to the header of the multi component image
267  * @idx: index of the requested component
268  * @data: pointer to a ulong variable, will hold component data address
269  * @len: pointer to a ulong variable, will hold component size
270  *
271  * image_multi_getimg() returns size and data address for the requested
272  * component in a multi component image.
273  *
274  * Note: no checking of the image type is done, caller must pass
275  * a valid multi component image.
276  *
277  * returns:
278  *     data address and size of the component, if idx is valid
279  *     0 in data and len, if idx is out of range
280  */
281 void image_multi_getimg(const image_header_t *hdr, ulong idx,
282                         ulong *data, ulong *len)
283 {
284         int i;
285         uint32_t *size;
286         ulong offset, count, img_data;
287
288         /* get number of component */
289         count = image_multi_count(hdr);
290
291         /* get start of the image payload, which in case of multi
292          * component images that points to a table of component sizes */
293         size = (uint32_t *)image_get_data(hdr);
294
295         /* get address of the proper component data start, which means
296          * skipping sizes table (add 1 for last, null entry) */
297         img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
298
299         if (idx < count) {
300                 *len = uimage_to_cpu(size[idx]);
301                 offset = 0;
302
303                 /* go over all indices preceding requested component idx */
304                 for (i = 0; i < idx; i++) {
305                         /* add up i-th component size, rounding up to 4 bytes */
306                         offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
307                 }
308
309                 /* calculate idx-th component data address */
310                 *data = img_data + offset;
311         } else {
312                 *len = 0;
313                 *data = 0;
314         }
315 }
316
317 static void image_print_type(const image_header_t *hdr)
318 {
319         const char __maybe_unused *os, *arch, *type, *comp;
320
321         os = genimg_get_os_name(image_get_os(hdr));
322         arch = genimg_get_arch_name(image_get_arch(hdr));
323         type = genimg_get_type_name(image_get_type(hdr));
324         comp = genimg_get_comp_name(image_get_comp(hdr));
325
326         printf("%s %s %s (%s)\n", arch, os, type, comp);
327 }
328
329 /**
330  * image_print_contents - prints out the contents of the legacy format image
331  * @ptr: pointer to the legacy format image header
332  * @p: pointer to prefix string
333  *
334  * image_print_contents() formats a multi line legacy image contents description.
335  * The routine prints out all header fields followed by the size/offset data
336  * for MULTI/SCRIPT images.
337  *
338  * returns:
339  *     no returned results
340  */
341 void image_print_contents(const void *ptr)
342 {
343         const image_header_t *hdr = (const image_header_t *)ptr;
344         const char __maybe_unused *p;
345
346         p = IMAGE_INDENT_STRING;
347         printf("%sImage Name:   %.*s\n", p, IH_NMLEN, image_get_name(hdr));
348         if (IMAGE_ENABLE_TIMESTAMP) {
349                 printf("%sCreated:      ", p);
350                 genimg_print_time((time_t)image_get_time(hdr));
351         }
352         printf("%sImage Type:   ", p);
353         image_print_type(hdr);
354         printf("%sData Size:    ", p);
355         genimg_print_size(image_get_data_size(hdr));
356         printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
357         printf("%sEntry Point:  %08x\n", p, image_get_ep(hdr));
358
359         if (image_check_type(hdr, IH_TYPE_MULTI) ||
360                         image_check_type(hdr, IH_TYPE_SCRIPT)) {
361                 int i;
362                 ulong data, len;
363                 ulong count = image_multi_count(hdr);
364
365                 printf("%sContents:\n", p);
366                 for (i = 0; i < count; i++) {
367                         image_multi_getimg(hdr, i, &data, &len);
368
369                         printf("%s   Image %d: ", p, i);
370                         genimg_print_size(len);
371
372                         if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
373                                 /*
374                                  * the user may need to know offsets
375                                  * if planning to do something with
376                                  * multiple files
377                                  */
378                                 printf("%s    Offset = 0x%08lx\n", p, data);
379                         }
380                 }
381         } else if (image_check_type(hdr, IH_TYPE_FIRMWARE_IVT)) {
382                 printf("HAB Blocks:   0x%08x   0x0000   0x%08x\n",
383                         image_get_load(hdr) - image_get_header_size(),
384                         (int)(image_get_size(hdr) + image_get_header_size()
385                         + sizeof(flash_header_v2_t) - 0x2060));
386         }
387 }
388
389 /**
390  * print_decomp_msg() - Print a suitable decompression/loading message
391  *
392  * @type:       OS type (IH_OS_...)
393  * @comp_type:  Compression type being used (IH_COMP_...)
394  * @is_xip:     true if the load address matches the image start
395  */
396 static void print_decomp_msg(int comp_type, int type, bool is_xip)
397 {
398         const char *name = genimg_get_type_name(type);
399
400         if (comp_type == IH_COMP_NONE)
401                 printf("   %s %s\n", is_xip ? "XIP" : "Loading", name);
402         else
403                 printf("   Uncompressing %s\n", name);
404 }
405
406 int image_decomp(int comp, ulong load, ulong image_start, int type,
407                  void *load_buf, void *image_buf, ulong image_len,
408                  uint unc_len, ulong *load_end)
409 {
410         int ret = 0;
411
412         *load_end = load;
413         print_decomp_msg(comp, type, load == image_start);
414
415         /*
416          * Load the image to the right place, decompressing if needed. After
417          * this, image_len will be set to the number of uncompressed bytes
418          * loaded, ret will be non-zero on error.
419          */
420         switch (comp) {
421         case IH_COMP_NONE:
422                 if (load == image_start)
423                         break;
424                 if (image_len <= unc_len)
425                         memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
426                 else
427                         ret = -ENOSPC;
428                 break;
429 #ifdef CONFIG_GZIP
430         case IH_COMP_GZIP: {
431                 ret = gunzip(load_buf, unc_len, image_buf, &image_len);
432                 break;
433         }
434 #endif /* CONFIG_GZIP */
435 #ifdef CONFIG_BZIP2
436         case IH_COMP_BZIP2: {
437                 uint size = unc_len;
438
439                 /*
440                  * If we've got less than 4 MB of malloc() space,
441                  * use slower decompression algorithm which requires
442                  * at most 2300 KB of memory.
443                  */
444                 ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
445                         image_buf, image_len,
446                         CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
447                 image_len = size;
448                 break;
449         }
450 #endif /* CONFIG_BZIP2 */
451 #ifdef CONFIG_LZMA
452         case IH_COMP_LZMA: {
453                 SizeT lzma_len = unc_len;
454
455                 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
456                                                image_buf, image_len);
457                 image_len = lzma_len;
458                 break;
459         }
460 #endif /* CONFIG_LZMA */
461 #ifdef CONFIG_LZO
462         case IH_COMP_LZO: {
463                 size_t size = unc_len;
464
465                 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
466                 image_len = size;
467                 break;
468         }
469 #endif /* CONFIG_LZO */
470 #ifdef CONFIG_LZ4
471         case IH_COMP_LZ4: {
472                 size_t size = unc_len;
473
474                 ret = ulz4fn(image_buf, image_len, load_buf, &size);
475                 image_len = size;
476                 break;
477         }
478 #endif /* CONFIG_LZ4 */
479         default:
480                 printf("Unimplemented compression type %d\n", comp);
481                 return -ENOSYS;
482         }
483
484         *load_end = load + image_len;
485
486         return ret;
487 }
488
489
490 #ifndef USE_HOSTCC
491 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
492 /**
493  * image_get_ramdisk - get and verify ramdisk image
494  * @rd_addr: ramdisk image start address
495  * @arch: expected ramdisk architecture
496  * @verify: checksum verification flag
497  *
498  * image_get_ramdisk() returns a pointer to the verified ramdisk image
499  * header. Routine receives image start address and expected architecture
500  * flag. Verification done covers data and header integrity and os/type/arch
501  * fields checking.
502  *
503  * returns:
504  *     pointer to a ramdisk image header, if image was found and valid
505  *     otherwise, return NULL
506  */
507 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
508                                                 int verify)
509 {
510         const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
511
512         if (!image_check_magic(rd_hdr)) {
513                 puts("Bad Magic Number\n");
514                 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
515                 return NULL;
516         }
517
518         if (!image_check_hcrc(rd_hdr)) {
519                 puts("Bad Header Checksum\n");
520                 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
521                 return NULL;
522         }
523
524         bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
525         image_print_contents(rd_hdr);
526
527         if (verify) {
528                 puts("   Verifying Checksum ... ");
529                 if (!image_check_dcrc(rd_hdr)) {
530                         puts("Bad Data CRC\n");
531                         bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
532                         return NULL;
533                 }
534                 puts("OK\n");
535         }
536
537         bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
538
539         if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
540             !image_check_arch(rd_hdr, arch) ||
541             !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
542                 printf("No Linux %s Ramdisk Image\n",
543                                 genimg_get_arch_name(arch));
544                 bootstage_error(BOOTSTAGE_ID_RAMDISK);
545                 return NULL;
546         }
547
548         return rd_hdr;
549 }
550 #endif
551 #endif /* !USE_HOSTCC */
552
553 /*****************************************************************************/
554 /* Shared dual-format routines */
555 /*****************************************************************************/
556 #ifndef USE_HOSTCC
557 ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
558 ulong save_addr;                        /* Default Save Address */
559 ulong save_size;                        /* Default Save Size (in bytes) */
560
561 static int on_loadaddr(const char *name, const char *value, enum env_op op,
562         int flags)
563 {
564         switch (op) {
565         case env_op_create:
566         case env_op_overwrite:
567                 load_addr = simple_strtoul(value, NULL, 16);
568                 break;
569         default:
570                 break;
571         }
572
573         return 0;
574 }
575 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
576
577 ulong env_get_bootm_low(void)
578 {
579         char *s = env_get("bootm_low");
580         if (s) {
581                 ulong tmp = simple_strtoul(s, NULL, 16);
582                 return tmp;
583         }
584
585 #if defined(CONFIG_SYS_SDRAM_BASE)
586         return CONFIG_SYS_SDRAM_BASE;
587 #elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE)
588         return gd->bd->bi_dram[0].start;
589 #else
590         return 0;
591 #endif
592 }
593
594 phys_size_t env_get_bootm_size(void)
595 {
596         phys_size_t tmp, size;
597         phys_addr_t start;
598         char *s = env_get("bootm_size");
599         if (s) {
600                 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
601                 return tmp;
602         }
603
604 #if (defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE)) && \
605      defined(CONFIG_NR_DRAM_BANKS)
606         start = gd->bd->bi_dram[0].start;
607         size = gd->bd->bi_dram[0].size;
608 #else
609         start = gd->bd->bi_memstart;
610         size = gd->bd->bi_memsize;
611 #endif
612
613         s = env_get("bootm_low");
614         if (s)
615                 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
616         else
617                 tmp = start;
618
619         return size - (tmp - start);
620 }
621
622 phys_size_t env_get_bootm_mapsize(void)
623 {
624         phys_size_t tmp;
625         char *s = env_get("bootm_mapsize");
626         if (s) {
627                 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
628                 return tmp;
629         }
630
631 #if defined(CONFIG_SYS_BOOTMAPSZ)
632         return CONFIG_SYS_BOOTMAPSZ;
633 #else
634         return env_get_bootm_size();
635 #endif
636 }
637
638 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
639 {
640         if (to == from)
641                 return;
642
643 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
644         if (to > from) {
645                 from += len;
646                 to += len;
647         }
648         while (len > 0) {
649                 size_t tail = (len > chunksz) ? chunksz : len;
650                 WATCHDOG_RESET();
651                 if (to > from) {
652                         to -= tail;
653                         from -= tail;
654                 }
655                 memmove(to, from, tail);
656                 if (to < from) {
657                         to += tail;
658                         from += tail;
659                 }
660                 len -= tail;
661         }
662 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
663         memmove(to, from, len);
664 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
665 }
666 #else   /* USE_HOSTCC */
667 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
668 {
669         memmove(to, from, len);
670 }
671 #endif /* !USE_HOSTCC */
672
673 void genimg_print_size(uint32_t size)
674 {
675 #ifndef USE_HOSTCC
676         printf("%d Bytes = ", size);
677         print_size(size, "\n");
678 #else
679         printf("%d Bytes = %.2f KiB = %.2f MiB\n",
680                         size, (double)size / 1.024e3,
681                         (double)size / 1.048576e6);
682 #endif
683 }
684
685 #if IMAGE_ENABLE_TIMESTAMP
686 void genimg_print_time(time_t timestamp)
687 {
688 #ifndef USE_HOSTCC
689         struct rtc_time tm;
690
691         rtc_to_tm(timestamp, &tm);
692         printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
693                         tm.tm_year, tm.tm_mon, tm.tm_mday,
694                         tm.tm_hour, tm.tm_min, tm.tm_sec);
695 #else
696         printf("%s", ctime(&timestamp));
697 #endif
698 }
699 #endif
700
701 const table_entry_t *get_table_entry(const table_entry_t *table, int id)
702 {
703         for (; table->id >= 0; ++table) {
704                 if (table->id == id)
705                         return table;
706         }
707         return NULL;
708 }
709
710 static const char *unknown_msg(enum ih_category category)
711 {
712         static const char unknown_str[] = "Unknown ";
713         static char msg[30];
714
715         strcpy(msg, unknown_str);
716         strncat(msg, table_info[category].desc,
717                 sizeof(msg) - sizeof(unknown_str));
718
719         return msg;
720 }
721
722 /**
723  * get_cat_table_entry_name - translate entry id to long name
724  * @category: category to look up (enum ih_category)
725  * @id: entry id to be translated
726  *
727  * This will scan the translation table trying to find the entry that matches
728  * the given id.
729  *
730  * @retur long entry name if translation succeeds; error string on failure
731  */
732 const char *genimg_get_cat_name(enum ih_category category, uint id)
733 {
734         const table_entry_t *entry;
735
736         entry = get_table_entry(table_info[category].table, id);
737         if (!entry)
738                 return unknown_msg(category);
739 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
740         return entry->lname;
741 #else
742         return entry->lname + gd->reloc_off;
743 #endif
744 }
745
746 /**
747  * get_cat_table_entry_short_name - translate entry id to short name
748  * @category: category to look up (enum ih_category)
749  * @id: entry id to be translated
750  *
751  * This will scan the translation table trying to find the entry that matches
752  * the given id.
753  *
754  * @retur short entry name if translation succeeds; error string on failure
755  */
756 const char *genimg_get_cat_short_name(enum ih_category category, uint id)
757 {
758         const table_entry_t *entry;
759
760         entry = get_table_entry(table_info[category].table, id);
761         if (!entry)
762                 return unknown_msg(category);
763 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
764         return entry->sname;
765 #else
766         return entry->sname + gd->reloc_off;
767 #endif
768 }
769
770 int genimg_get_cat_count(enum ih_category category)
771 {
772         return table_info[category].count;
773 }
774
775 const char *genimg_get_cat_desc(enum ih_category category)
776 {
777         return table_info[category].desc;
778 }
779
780 /**
781  * get_table_entry_name - translate entry id to long name
782  * @table: pointer to a translation table for entries of a specific type
783  * @msg: message to be returned when translation fails
784  * @id: entry id to be translated
785  *
786  * get_table_entry_name() will go over translation table trying to find
787  * entry that matches given id. If matching entry is found, its long
788  * name is returned to the caller.
789  *
790  * returns:
791  *     long entry name if translation succeeds
792  *     msg otherwise
793  */
794 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
795 {
796         table = get_table_entry(table, id);
797         if (!table)
798                 return msg;
799 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
800         return table->lname;
801 #else
802         return table->lname + gd->reloc_off;
803 #endif
804 }
805
806 const char *genimg_get_os_name(uint8_t os)
807 {
808         return (get_table_entry_name(uimage_os, "Unknown OS", os));
809 }
810
811 const char *genimg_get_arch_name(uint8_t arch)
812 {
813         return (get_table_entry_name(uimage_arch, "Unknown Architecture",
814                                         arch));
815 }
816
817 const char *genimg_get_type_name(uint8_t type)
818 {
819         return (get_table_entry_name(uimage_type, "Unknown Image", type));
820 }
821
822 static const char *genimg_get_short_name(const table_entry_t *table, int val)
823 {
824         table = get_table_entry(table, val);
825         if (!table)
826                 return "unknown";
827 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
828         return table->sname;
829 #else
830         return table->sname + gd->reloc_off;
831 #endif
832 }
833
834 const char *genimg_get_type_short_name(uint8_t type)
835 {
836         return genimg_get_short_name(uimage_type, type);
837 }
838
839 const char *genimg_get_comp_name(uint8_t comp)
840 {
841         return (get_table_entry_name(uimage_comp, "Unknown Compression",
842                                         comp));
843 }
844
845 const char *genimg_get_comp_short_name(uint8_t comp)
846 {
847         return genimg_get_short_name(uimage_comp, comp);
848 }
849
850 const char *genimg_get_os_short_name(uint8_t os)
851 {
852         return genimg_get_short_name(uimage_os, os);
853 }
854
855 const char *genimg_get_arch_short_name(uint8_t arch)
856 {
857         return genimg_get_short_name(uimage_arch, arch);
858 }
859
860 /**
861  * get_table_entry_id - translate short entry name to id
862  * @table: pointer to a translation table for entries of a specific type
863  * @table_name: to be used in case of error
864  * @name: entry short name to be translated
865  *
866  * get_table_entry_id() will go over translation table trying to find
867  * entry that matches given short name. If matching entry is found,
868  * its id returned to the caller.
869  *
870  * returns:
871  *     entry id if translation succeeds
872  *     -1 otherwise
873  */
874 int get_table_entry_id(const table_entry_t *table,
875                 const char *table_name, const char *name)
876 {
877         const table_entry_t *t;
878
879         for (t = table; t->id >= 0; ++t) {
880 #ifdef CONFIG_NEEDS_MANUAL_RELOC
881                 if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
882 #else
883                 if (t->sname && strcasecmp(t->sname, name) == 0)
884 #endif
885                         return (t->id);
886         }
887         debug("Invalid %s Type: %s\n", table_name, name);
888
889         return -1;
890 }
891
892 int genimg_get_os_id(const char *name)
893 {
894         return (get_table_entry_id(uimage_os, "OS", name));
895 }
896
897 int genimg_get_arch_id(const char *name)
898 {
899         return (get_table_entry_id(uimage_arch, "CPU", name));
900 }
901
902 int genimg_get_type_id(const char *name)
903 {
904         return (get_table_entry_id(uimage_type, "Image", name));
905 }
906
907 int genimg_get_comp_id(const char *name)
908 {
909         return (get_table_entry_id(uimage_comp, "Compression", name));
910 }
911
912 #ifndef USE_HOSTCC
913 /**
914  * genimg_get_kernel_addr_fit - get the real kernel address and return 2
915  *                              FIT strings
916  * @img_addr: a string might contain real image address
917  * @fit_uname_config: double pointer to a char, will hold pointer to a
918  *                    configuration unit name
919  * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
920  *                    name
921  *
922  * genimg_get_kernel_addr_fit get the real kernel start address from a string
923  * which is normally the first argv of bootm/bootz
924  *
925  * returns:
926  *     kernel start address
927  */
928 ulong genimg_get_kernel_addr_fit(char * const img_addr,
929                              const char **fit_uname_config,
930                              const char **fit_uname_kernel)
931 {
932         ulong kernel_addr;
933
934         /* find out kernel image address */
935         if (!img_addr) {
936                 kernel_addr = load_addr;
937                 debug("*  kernel: default image load address = 0x%08lx\n",
938                       load_addr);
939 #if CONFIG_IS_ENABLED(FIT)
940         } else if (fit_parse_conf(img_addr, load_addr, &kernel_addr,
941                                   fit_uname_config)) {
942                 debug("*  kernel: config '%s' from image at 0x%08lx\n",
943                       *fit_uname_config, kernel_addr);
944         } else if (fit_parse_subimage(img_addr, load_addr, &kernel_addr,
945                                      fit_uname_kernel)) {
946                 debug("*  kernel: subimage '%s' from image at 0x%08lx\n",
947                       *fit_uname_kernel, kernel_addr);
948 #endif
949         } else {
950                 kernel_addr = simple_strtoul(img_addr, NULL, 16);
951                 debug("*  kernel: cmdline image address = 0x%08lx\n",
952                       kernel_addr);
953         }
954
955         return kernel_addr;
956 }
957
958 /**
959  * genimg_get_kernel_addr() is the simple version of
960  * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
961  */
962 ulong genimg_get_kernel_addr(char * const img_addr)
963 {
964         const char *fit_uname_config = NULL;
965         const char *fit_uname_kernel = NULL;
966
967         return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
968                                           &fit_uname_kernel);
969 }
970
971 /**
972  * genimg_get_format - get image format type
973  * @img_addr: image start address
974  *
975  * genimg_get_format() checks whether provided address points to a valid
976  * legacy or FIT image.
977  *
978  * New uImage format and FDT blob are based on a libfdt. FDT blob
979  * may be passed directly or embedded in a FIT image. In both situations
980  * genimg_get_format() must be able to dectect libfdt header.
981  *
982  * returns:
983  *     image format type or IMAGE_FORMAT_INVALID if no image is present
984  */
985 int genimg_get_format(const void *img_addr)
986 {
987 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
988         const image_header_t *hdr;
989
990         hdr = (const image_header_t *)img_addr;
991         if (image_check_magic(hdr))
992                 return IMAGE_FORMAT_LEGACY;
993 #endif
994 #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
995         if (fdt_check_header(img_addr) == 0)
996                 return IMAGE_FORMAT_FIT;
997 #endif
998 #ifdef CONFIG_ANDROID_BOOT_IMAGE
999         if (android_image_check_header(img_addr) == 0)
1000                 return IMAGE_FORMAT_ANDROID;
1001 #endif
1002
1003         return IMAGE_FORMAT_INVALID;
1004 }
1005
1006 /**
1007  * fit_has_config - check if there is a valid FIT configuration
1008  * @images: pointer to the bootm command headers structure
1009  *
1010  * fit_has_config() checks if there is a FIT configuration in use
1011  * (if FTI support is present).
1012  *
1013  * returns:
1014  *     0, no FIT support or no configuration found
1015  *     1, configuration found
1016  */
1017 int genimg_has_config(bootm_headers_t *images)
1018 {
1019 #if IMAGE_ENABLE_FIT
1020         if (images->fit_uname_cfg)
1021                 return 1;
1022 #endif
1023         return 0;
1024 }
1025
1026 /**
1027  * boot_get_ramdisk - main ramdisk handling routine
1028  * @argc: command argument count
1029  * @argv: command argument list
1030  * @images: pointer to the bootm images structure
1031  * @arch: expected ramdisk architecture
1032  * @rd_start: pointer to a ulong variable, will hold ramdisk start address
1033  * @rd_end: pointer to a ulong variable, will hold ramdisk end
1034  *
1035  * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
1036  * Curently supported are the following ramdisk sources:
1037  *      - multicomponent kernel/ramdisk image,
1038  *      - commandline provided address of decicated ramdisk image.
1039  *
1040  * returns:
1041  *     0, if ramdisk image was found and valid, or skiped
1042  *     rd_start and rd_end are set to ramdisk start/end addresses if
1043  *     ramdisk image is found and valid
1044  *
1045  *     1, if ramdisk image is found but corrupted, or invalid
1046  *     rd_start and rd_end are set to 0 if no ramdisk exists
1047  */
1048 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
1049                 uint8_t arch, ulong *rd_start, ulong *rd_end)
1050 {
1051         ulong rd_addr, rd_load;
1052         ulong rd_data, rd_len;
1053 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
1054         const image_header_t *rd_hdr;
1055 #endif
1056         void *buf;
1057 #ifdef CONFIG_SUPPORT_RAW_INITRD
1058         char *end;
1059 #endif
1060 #if IMAGE_ENABLE_FIT
1061         const char      *fit_uname_config = images->fit_uname_cfg;
1062         const char      *fit_uname_ramdisk = NULL;
1063         ulong           default_addr;
1064         int             rd_noffset;
1065 #endif
1066         const char *select = NULL;
1067
1068         *rd_start = 0;
1069         *rd_end = 0;
1070
1071 #ifdef CONFIG_ANDROID_BOOT_IMAGE
1072         /*
1073          * Look for an Android boot image.
1074          */
1075         buf = map_sysmem(images->os.start, 0);
1076         if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
1077                 select = (argc == 0) ? env_get("loadaddr") : argv[0];
1078 #endif
1079
1080         if (argc >= 2)
1081                 select = argv[1];
1082
1083         /*
1084          * Look for a '-' which indicates to ignore the
1085          * ramdisk argument
1086          */
1087         if (select && strcmp(select, "-") ==  0) {
1088                 debug("## Skipping init Ramdisk\n");
1089                 rd_len = rd_data = 0;
1090         } else if (select || genimg_has_config(images)) {
1091 #if IMAGE_ENABLE_FIT
1092                 if (select) {
1093                         /*
1094                          * If the init ramdisk comes from the FIT image and
1095                          * the FIT image address is omitted in the command
1096                          * line argument, try to use os FIT image address or
1097                          * default load address.
1098                          */
1099                         if (images->fit_uname_os)
1100                                 default_addr = (ulong)images->fit_hdr_os;
1101                         else
1102                                 default_addr = load_addr;
1103
1104                         if (fit_parse_conf(select, default_addr,
1105                                            &rd_addr, &fit_uname_config)) {
1106                                 debug("*  ramdisk: config '%s' from image at "
1107                                                 "0x%08lx\n",
1108                                                 fit_uname_config, rd_addr);
1109                         } else if (fit_parse_subimage(select, default_addr,
1110                                                 &rd_addr, &fit_uname_ramdisk)) {
1111                                 debug("*  ramdisk: subimage '%s' from image at "
1112                                                 "0x%08lx\n",
1113                                                 fit_uname_ramdisk, rd_addr);
1114                         } else
1115 #endif
1116                         {
1117                                 rd_addr = simple_strtoul(select, NULL, 16);
1118                                 debug("*  ramdisk: cmdline image address = "
1119                                                 "0x%08lx\n",
1120                                                 rd_addr);
1121                         }
1122 #if IMAGE_ENABLE_FIT
1123                 } else {
1124                         /* use FIT configuration provided in first bootm
1125                          * command argument. If the property is not defined,
1126                          * quit silently.
1127                          */
1128                         rd_addr = map_to_sysmem(images->fit_hdr_os);
1129                         rd_noffset = fit_get_node_from_config(images,
1130                                         FIT_RAMDISK_PROP, rd_addr);
1131                         if (rd_noffset == -ENOENT)
1132                                 return 0;
1133                         else if (rd_noffset < 0)
1134                                 return 1;
1135                 }
1136 #endif
1137
1138                 /*
1139                  * Check if there is an initrd image at the
1140                  * address provided in the second bootm argument
1141                  * check image type, for FIT images get FIT node.
1142                  */
1143                 buf = map_sysmem(rd_addr, 0);
1144                 switch (genimg_get_format(buf)) {
1145 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
1146                 case IMAGE_FORMAT_LEGACY:
1147                         printf("## Loading init Ramdisk from Legacy "
1148                                         "Image at %08lx ...\n", rd_addr);
1149
1150                         bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
1151                         rd_hdr = image_get_ramdisk(rd_addr, arch,
1152                                                         images->verify);
1153
1154                         if (rd_hdr == NULL)
1155                                 return 1;
1156
1157                         rd_data = image_get_data(rd_hdr);
1158                         rd_len = image_get_data_size(rd_hdr);
1159                         rd_load = image_get_load(rd_hdr);
1160                         break;
1161 #endif
1162 #if IMAGE_ENABLE_FIT
1163                 case IMAGE_FORMAT_FIT:
1164                         rd_noffset = fit_image_load(images,
1165                                         rd_addr, &fit_uname_ramdisk,
1166                                         &fit_uname_config, arch,
1167                                         IH_TYPE_RAMDISK,
1168                                         BOOTSTAGE_ID_FIT_RD_START,
1169                                         FIT_LOAD_OPTIONAL_NON_ZERO,
1170                                         &rd_data, &rd_len);
1171                         if (rd_noffset < 0)
1172                                 return 1;
1173
1174                         images->fit_hdr_rd = map_sysmem(rd_addr, 0);
1175                         images->fit_uname_rd = fit_uname_ramdisk;
1176                         images->fit_noffset_rd = rd_noffset;
1177                         break;
1178 #endif
1179 #ifdef CONFIG_ANDROID_BOOT_IMAGE
1180                 case IMAGE_FORMAT_ANDROID:
1181                         android_image_get_ramdisk((void *)images->os.start,
1182                                 &rd_data, &rd_len);
1183                         break;
1184 #endif
1185                 default:
1186 #ifdef CONFIG_SUPPORT_RAW_INITRD
1187                         end = NULL;
1188                         if (select)
1189                                 end = strchr(select, ':');
1190                         if (end) {
1191                                 rd_len = simple_strtoul(++end, NULL, 16);
1192                                 rd_data = rd_addr;
1193                         } else
1194 #endif
1195                         {
1196                                 puts("Wrong Ramdisk Image Format\n");
1197                                 rd_data = rd_len = rd_load = 0;
1198                                 return 1;
1199                         }
1200                 }
1201         } else if (images->legacy_hdr_valid &&
1202                         image_check_type(&images->legacy_hdr_os_copy,
1203                                                 IH_TYPE_MULTI)) {
1204
1205                 /*
1206                  * Now check if we have a legacy mult-component image,
1207                  * get second entry data start address and len.
1208                  */
1209                 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
1210                 printf("## Loading init Ramdisk from multi component "
1211                                 "Legacy Image at %08lx ...\n",
1212                                 (ulong)images->legacy_hdr_os);
1213
1214                 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1215         } else {
1216                 /*
1217                  * no initrd image
1218                  */
1219                 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1220                 rd_len = rd_data = 0;
1221         }
1222
1223         if (!rd_data) {
1224                 debug("## No init Ramdisk\n");
1225         } else {
1226                 *rd_start = rd_data;
1227                 *rd_end = rd_data + rd_len;
1228         }
1229         debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1230                         *rd_start, *rd_end);
1231
1232         return 0;
1233 }
1234
1235 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1236 /**
1237  * boot_ramdisk_high - relocate init ramdisk
1238  * @lmb: pointer to lmb handle, will be used for memory mgmt
1239  * @rd_data: ramdisk data start address
1240  * @rd_len: ramdisk data length
1241  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1242  *      start address (after possible relocation)
1243  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1244  *      end address (after possible relocation)
1245  *
1246  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
1247  * variable and if requested ramdisk data is moved to a specified location.
1248  *
1249  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1250  * start/end addresses if ramdisk image start and len were provided,
1251  * otherwise set initrd_start and initrd_end set to zeros.
1252  *
1253  * returns:
1254  *      0 - success
1255  *     -1 - failure
1256  */
1257 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1258                   ulong *initrd_start, ulong *initrd_end)
1259 {
1260         char    *s;
1261         ulong   initrd_high;
1262         int     initrd_copy_to_ram = 1;
1263
1264         s = env_get("initrd_high");
1265         if (s) {
1266                 /* a value of "no" or a similar string will act like 0,
1267                  * turning the "load high" feature off. This is intentional.
1268                  */
1269                 initrd_high = simple_strtoul(s, NULL, 16);
1270                 if (initrd_high == ~0)
1271                         initrd_copy_to_ram = 0;
1272         } else {
1273                 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
1274         }
1275
1276
1277         debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1278                         initrd_high, initrd_copy_to_ram);
1279
1280         if (rd_data) {
1281                 if (!initrd_copy_to_ram) {      /* zero-copy ramdisk support */
1282                         debug("   in-place initrd\n");
1283                         *initrd_start = rd_data;
1284                         *initrd_end = rd_data + rd_len;
1285                         lmb_reserve(lmb, rd_data, rd_len);
1286                 } else {
1287                         if (initrd_high)
1288                                 *initrd_start = (ulong)lmb_alloc_base(lmb,
1289                                                 rd_len, 0x1000, initrd_high);
1290                         else
1291                                 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1292                                                                  0x1000);
1293
1294                         if (*initrd_start == 0) {
1295                                 puts("ramdisk - allocation error\n");
1296                                 goto error;
1297                         }
1298                         bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1299
1300                         *initrd_end = *initrd_start + rd_len;
1301                         printf("   Loading Ramdisk to %08lx, end %08lx ... ",
1302                                         *initrd_start, *initrd_end);
1303
1304                         memmove_wd((void *)*initrd_start,
1305                                         (void *)rd_data, rd_len, CHUNKSZ);
1306
1307 #ifdef CONFIG_MP
1308                         /*
1309                          * Ensure the image is flushed to memory to handle
1310                          * AMP boot scenarios in which we might not be
1311                          * HW cache coherent
1312                          */
1313                         flush_cache((unsigned long)*initrd_start,
1314                                     ALIGN(rd_len, ARCH_DMA_MINALIGN));
1315 #endif
1316                         puts("OK\n");
1317                 }
1318         } else {
1319                 *initrd_start = 0;
1320                 *initrd_end = 0;
1321         }
1322         debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1323                         *initrd_start, *initrd_end);
1324
1325         return 0;
1326
1327 error:
1328         return -1;
1329 }
1330 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1331
1332 int boot_get_setup(bootm_headers_t *images, uint8_t arch,
1333                    ulong *setup_start, ulong *setup_len)
1334 {
1335 #if IMAGE_ENABLE_FIT
1336         return boot_get_setup_fit(images, arch, setup_start, setup_len);
1337 #else
1338         return -ENOENT;
1339 #endif
1340 }
1341
1342 #if IMAGE_ENABLE_FIT
1343 #if defined(CONFIG_FPGA)
1344 int boot_get_fpga(int argc, char * const argv[], bootm_headers_t *images,
1345                   uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1346 {
1347         ulong tmp_img_addr, img_data, img_len;
1348         void *buf;
1349         int conf_noffset;
1350         int fit_img_result;
1351         const char *uname, *name;
1352         int err;
1353         int devnum = 0; /* TODO support multi fpga platforms */
1354
1355         /* Check to see if the images struct has a FIT configuration */
1356         if (!genimg_has_config(images)) {
1357                 debug("## FIT configuration was not specified\n");
1358                 return 0;
1359         }
1360
1361         /*
1362          * Obtain the os FIT header from the images struct
1363          */
1364         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
1365         buf = map_sysmem(tmp_img_addr, 0);
1366         /*
1367          * Check image type. For FIT images get FIT node
1368          * and attempt to locate a generic binary.
1369          */
1370         switch (genimg_get_format(buf)) {
1371         case IMAGE_FORMAT_FIT:
1372                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1373
1374                 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
1375                                            NULL);
1376                 if (!uname) {
1377                         debug("## FPGA image is not specified\n");
1378                         return 0;
1379                 }
1380                 fit_img_result = fit_image_load(images,
1381                                                 tmp_img_addr,
1382                                                 (const char **)&uname,
1383                                                 &(images->fit_uname_cfg),
1384                                                 arch,
1385                                                 IH_TYPE_FPGA,
1386                                                 BOOTSTAGE_ID_FPGA_INIT,
1387                                                 FIT_LOAD_OPTIONAL_NON_ZERO,
1388                                                 &img_data, &img_len);
1389
1390                 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
1391                       uname, img_data, img_len);
1392
1393                 if (fit_img_result < 0) {
1394                         /* Something went wrong! */
1395                         return fit_img_result;
1396                 }
1397
1398                 if (!fpga_is_partial_data(devnum, img_len)) {
1399                         name = "full";
1400                         err = fpga_loadbitstream(devnum, (char *)img_data,
1401                                                  img_len, BIT_FULL);
1402                         if (err)
1403                                 err = fpga_load(devnum, (const void *)img_data,
1404                                                 img_len, BIT_FULL);
1405                 } else {
1406                         name = "partial";
1407                         err = fpga_loadbitstream(devnum, (char *)img_data,
1408                                                  img_len, BIT_PARTIAL);
1409                         if (err)
1410                                 err = fpga_load(devnum, (const void *)img_data,
1411                                                 img_len, BIT_PARTIAL);
1412                 }
1413
1414                 if (err)
1415                         return err;
1416
1417                 printf("   Programming %s bitstream... OK\n", name);
1418                 break;
1419         default:
1420                 printf("The given image format is not supported (corrupt?)\n");
1421                 return 1;
1422         }
1423
1424         return 0;
1425 }
1426 #endif
1427
1428 static void fit_loadable_process(uint8_t img_type,
1429                                  ulong img_data,
1430                                  ulong img_len)
1431 {
1432         int i;
1433         const unsigned int count =
1434                         ll_entry_count(struct fit_loadable_tbl, fit_loadable);
1435         struct fit_loadable_tbl *fit_loadable_handler =
1436                         ll_entry_start(struct fit_loadable_tbl, fit_loadable);
1437         /* For each loadable handler */
1438         for (i = 0; i < count; i++, fit_loadable_handler++)
1439                 /* matching this type */
1440                 if (fit_loadable_handler->type == img_type)
1441                         /* call that handler with this image data */
1442                         fit_loadable_handler->handler(img_data, img_len);
1443 }
1444
1445 int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
1446                 uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1447 {
1448         /*
1449          * These variables are used to hold the current image location
1450          * in system memory.
1451          */
1452         ulong tmp_img_addr;
1453         /*
1454          * These two variables are requirements for fit_image_load, but
1455          * their values are not used
1456          */
1457         ulong img_data, img_len;
1458         void *buf;
1459         int loadables_index;
1460         int conf_noffset;
1461         int fit_img_result;
1462         const char *uname;
1463         uint8_t img_type;
1464
1465         /* Check to see if the images struct has a FIT configuration */
1466         if (!genimg_has_config(images)) {
1467                 debug("## FIT configuration was not specified\n");
1468                 return 0;
1469         }
1470
1471         /*
1472          * Obtain the os FIT header from the images struct
1473          */
1474         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
1475         buf = map_sysmem(tmp_img_addr, 0);
1476         /*
1477          * Check image type. For FIT images get FIT node
1478          * and attempt to locate a generic binary.
1479          */
1480         switch (genimg_get_format(buf)) {
1481         case IMAGE_FORMAT_FIT:
1482                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1483
1484                 for (loadables_index = 0;
1485                      uname = fdt_stringlist_get(buf, conf_noffset,
1486                                         FIT_LOADABLE_PROP, loadables_index,
1487                                         NULL), uname;
1488                      loadables_index++)
1489                 {
1490                         fit_img_result = fit_image_load(images,
1491                                 tmp_img_addr,
1492                                 &uname,
1493                                 &(images->fit_uname_cfg), arch,
1494                                 IH_TYPE_LOADABLE,
1495                                 BOOTSTAGE_ID_FIT_LOADABLE_START,
1496                                 FIT_LOAD_OPTIONAL_NON_ZERO,
1497                                 &img_data, &img_len);
1498                         if (fit_img_result < 0) {
1499                                 /* Something went wrong! */
1500                                 return fit_img_result;
1501                         }
1502
1503                         fit_img_result = fit_image_get_node(buf, uname);
1504                         if (fit_img_result < 0) {
1505                                 /* Something went wrong! */
1506                                 return fit_img_result;
1507                         }
1508                         fit_img_result = fit_image_get_type(buf,
1509                                                             fit_img_result,
1510                                                             &img_type);
1511                         if (fit_img_result < 0) {
1512                                 /* Something went wrong! */
1513                                 return fit_img_result;
1514                         }
1515
1516                         fit_loadable_process(img_type, img_data, img_len);
1517                 }
1518                 break;
1519         default:
1520                 printf("The given image format is not supported (corrupt?)\n");
1521                 return 1;
1522         }
1523
1524         return 0;
1525 }
1526 #endif
1527
1528 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1529 /**
1530  * boot_get_cmdline - allocate and initialize kernel cmdline
1531  * @lmb: pointer to lmb handle, will be used for memory mgmt
1532  * @cmd_start: pointer to a ulong variable, will hold cmdline start
1533  * @cmd_end: pointer to a ulong variable, will hold cmdline end
1534  *
1535  * boot_get_cmdline() allocates space for kernel command line below
1536  * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
1537  * variable is present its contents is copied to allocated kernel
1538  * command line.
1539  *
1540  * returns:
1541  *      0 - success
1542  *     -1 - failure
1543  */
1544 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1545 {
1546         char *cmdline;
1547         char *s;
1548
1549         cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1550                                 env_get_bootm_mapsize() + env_get_bootm_low());
1551
1552         if (cmdline == NULL)
1553                 return -1;
1554
1555         s = env_get("bootargs");
1556         if (!s)
1557                 s = "";
1558
1559         strcpy(cmdline, s);
1560
1561         *cmd_start = (ulong) & cmdline[0];
1562         *cmd_end = *cmd_start + strlen(cmdline);
1563
1564         debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1565
1566         return 0;
1567 }
1568 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1569
1570 #ifdef CONFIG_SYS_BOOT_GET_KBD
1571 /**
1572  * boot_get_kbd - allocate and initialize kernel copy of board info
1573  * @lmb: pointer to lmb handle, will be used for memory mgmt
1574  * @kbd: double pointer to board info data
1575  *
1576  * boot_get_kbd() allocates space for kernel copy of board info data below
1577  * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
1578  * with the current u-boot board info data.
1579  *
1580  * returns:
1581  *      0 - success
1582  *     -1 - failure
1583  */
1584 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1585 {
1586         *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1587                                 env_get_bootm_mapsize() + env_get_bootm_low());
1588         if (*kbd == NULL)
1589                 return -1;
1590
1591         **kbd = *(gd->bd);
1592
1593         debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1594
1595 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1596         do_bdinfo(NULL, 0, 0, NULL);
1597 #endif
1598
1599         return 0;
1600 }
1601 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1602
1603 #ifdef CONFIG_LMB
1604 int image_setup_linux(bootm_headers_t *images)
1605 {
1606         ulong of_size = images->ft_len;
1607         char **of_flat_tree = &images->ft_addr;
1608         struct lmb *lmb = &images->lmb;
1609         int ret;
1610
1611         if (IMAGE_ENABLE_OF_LIBFDT)
1612                 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1613
1614         if (IMAGE_BOOT_GET_CMDLINE) {
1615                 ret = boot_get_cmdline(lmb, &images->cmdline_start,
1616                                 &images->cmdline_end);
1617                 if (ret) {
1618                         puts("ERROR with allocation of cmdline\n");
1619                         return ret;
1620                 }
1621         }
1622
1623         if (IMAGE_ENABLE_OF_LIBFDT) {
1624                 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1625                 if (ret)
1626                         return ret;
1627         }
1628
1629         if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
1630                 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
1631                 if (ret)
1632                         return ret;
1633         }
1634
1635         return 0;
1636 }
1637 #endif /* CONFIG_LMB */
1638 #endif /* !USE_HOSTCC */