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