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