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