]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/image.c
i.mx28: Added register definitions for DIGCTL registers
[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 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
ceaed2b1 25
b97a2a0a 26#ifndef USE_HOSTCC
5ad03eb3
MB
27#include <common.h>
28#include <watchdog.h>
29
30#ifdef CONFIG_SHOW_BOOT_PROGRESS
31#include <status_led.h>
32#endif
33
34#ifdef CONFIG_HAS_DATAFLASH
35#include <dataflash.h>
36#endif
37
95d449ad
MB
38#ifdef CONFIG_LOGBUFFER
39#include <logbuff.h>
40#endif
41
2242f536
MB
42#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
43#include <rtc.h>
44#endif
45
5dfb5213
MB
46#include <image.h>
47
712fbcf3 48#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
fff888a1
MB
49#include <fdt.h>
50#include <libfdt.h>
51#include <fdt_support.h>
c8779648
MB
52#endif
53
54#if defined(CONFIG_FIT)
20a14a42 55#include <u-boot/md5.h>
5dfb5213 56#include <sha1.h>
c8779648 57
712fbcf3 58static int fit_check_ramdisk(const void *fit, int os_noffset,
c8779648 59 uint8_t arch, int verify);
fff888a1
MB
60#endif
61
b6b0fe64 62#ifdef CONFIG_CMD_BDI
54841ab5 63extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
b6b0fe64
MB
64#endif
65
66DECLARE_GLOBAL_DATA_PTR;
8a5ea3e6 67
712fbcf3 68static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
d985c849 69 int verify);
b97a2a0a 70#else
5ad03eb3 71#include "mkimage.h"
20a14a42 72#include <u-boot/md5.h>
5dfb5213 73#include <time.h>
b97a2a0a 74#include <image.h>
5dfb5213 75#endif /* !USE_HOSTCC*/
b97a2a0a 76
7edb186f 77static const table_entry_t uimage_arch[] = {
570abb0a
MB
78 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
79 { IH_ARCH_ALPHA, "alpha", "Alpha", },
80 { IH_ARCH_ARM, "arm", "ARM", },
81 { IH_ARCH_I386, "x86", "Intel x86", },
82 { IH_ARCH_IA64, "ia64", "IA64", },
83 { IH_ARCH_M68K, "m68k", "M68K", },
84 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
85 { IH_ARCH_MIPS, "mips", "MIPS", },
86 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
570abb0a 87 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
e419e12d 88 { IH_ARCH_PPC, "powerpc", "PowerPC", },
570abb0a
MB
89 { IH_ARCH_PPC, "ppc", "PowerPC", },
90 { IH_ARCH_S390, "s390", "IBM S390", },
91 { IH_ARCH_SH, "sh", "SuperH", },
92 { IH_ARCH_SPARC, "sparc", "SPARC", },
93 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
94 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
95 { IH_ARCH_AVR32, "avr32", "AVR32", },
64d61461 96 { IH_ARCH_NDS32, "nds32", "NDS32", },
3ddcaccd 97 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
570abb0a
MB
98 { -1, "", "", },
99};
100
7edb186f 101static const table_entry_t uimage_os[] = {
570abb0a 102 { IH_OS_INVALID, NULL, "Invalid OS", },
570abb0a
MB
103 { IH_OS_LINUX, "linux", "Linux", },
104#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
105 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
106#endif
107 { IH_OS_NETBSD, "netbsd", "NetBSD", },
3df61957 108 { IH_OS_OSE, "ose", "Enea OSE", },
570abb0a
MB
109 { IH_OS_RTEMS, "rtems", "RTEMS", },
110 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
111#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
112 { IH_OS_QNX, "qnx", "QNX", },
113 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
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
131 { -1, "", "", },
132};
133
7edb186f 134static const table_entry_t uimage_type[] = {
4962e38e 135 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
570abb0a
MB
136 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
137 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
3decb14a 138 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
570abb0a 139 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
b9b50e89 140 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
4962e38e
SB
141 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
142 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
143 { IH_TYPE_INVALID, NULL, "Invalid Image", },
570abb0a 144 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
4962e38e 145 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
570abb0a
MB
146 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
147 { IH_TYPE_SCRIPT, "script", "Script", },
148 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
7816f2cf 149 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
570abb0a
MB
150 { -1, "", "", },
151};
152
7edb186f 153static const table_entry_t uimage_comp[] = {
570abb0a
MB
154 { IH_COMP_NONE, "none", "uncompressed", },
155 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
156 { IH_COMP_GZIP, "gzip", "gzip compressed", },
fc9c1727 157 { IH_COMP_LZMA, "lzma", "lzma compressed", },
20dde48b 158 { IH_COMP_LZO, "lzo", "lzo compressed", },
570abb0a
MB
159 { -1, "", "", },
160};
161
712fbcf3
SW
162uint32_t crc32(uint32_t, const unsigned char *, uint);
163uint32_t crc32_wd(uint32_t, const unsigned char *, uint, uint);
570abb0a 164#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
712fbcf3 165static void genimg_print_time(time_t timestamp);
570abb0a 166#endif
b97a2a0a 167
9a4daad0
MB
168/*****************************************************************************/
169/* Legacy format routines */
170/*****************************************************************************/
712fbcf3 171int image_check_hcrc(const image_header_t *hdr)
b97a2a0a
MB
172{
173 ulong hcrc;
712fbcf3 174 ulong len = image_get_header_size();
b97a2a0a
MB
175 image_header_t header;
176
177 /* Copy header so we can blank CRC field for re-calculation */
712fbcf3
SW
178 memmove(&header, (char *)hdr, image_get_header_size());
179 image_set_hcrc(&header, 0);
b97a2a0a 180
712fbcf3 181 hcrc = crc32(0, (unsigned char *)&header, len);
b97a2a0a 182
712fbcf3 183 return (hcrc == image_get_hcrc(hdr));
b97a2a0a
MB
184}
185
712fbcf3 186int image_check_dcrc(const image_header_t *hdr)
b97a2a0a 187{
712fbcf3
SW
188 ulong data = image_get_data(hdr);
189 ulong len = image_get_data_size(hdr);
190 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
b97a2a0a 191
712fbcf3 192 return (dcrc == image_get_dcrc(hdr));
b97a2a0a
MB
193}
194
f13e7b2e
MB
195/**
196 * image_multi_count - get component (sub-image) count
197 * @hdr: pointer to the header of the multi component image
198 *
199 * image_multi_count() returns number of components in a multi
200 * component image.
201 *
202 * Note: no checking of the image type is done, caller must pass
203 * a valid multi component image.
204 *
205 * returns:
206 * number of components
207 */
712fbcf3 208ulong image_multi_count(const image_header_t *hdr)
f13e7b2e
MB
209{
210 ulong i, count = 0;
df6f1b89 211 uint32_t *size;
f13e7b2e
MB
212
213 /* get start of the image payload, which in case of multi
214 * component images that points to a table of component sizes */
712fbcf3 215 size = (uint32_t *)image_get_data(hdr);
f13e7b2e
MB
216
217 /* count non empty slots */
218 for (i = 0; size[i]; ++i)
219 count++;
220
221 return count;
222}
223
224/**
225 * image_multi_getimg - get component data address and size
226 * @hdr: pointer to the header of the multi component image
227 * @idx: index of the requested component
228 * @data: pointer to a ulong variable, will hold component data address
229 * @len: pointer to a ulong variable, will hold component size
230 *
231 * image_multi_getimg() returns size and data address for the requested
232 * component in a multi component image.
233 *
234 * Note: no checking of the image type is done, caller must pass
235 * a valid multi component image.
236 *
237 * returns:
238 * data address and size of the component, if idx is valid
239 * 0 in data and len, if idx is out of range
240 */
712fbcf3 241void image_multi_getimg(const image_header_t *hdr, ulong idx,
f13e7b2e
MB
242 ulong *data, ulong *len)
243{
244 int i;
df6f1b89 245 uint32_t *size;
02b9b224 246 ulong offset, count, img_data;
f13e7b2e
MB
247
248 /* get number of component */
712fbcf3 249 count = image_multi_count(hdr);
f13e7b2e
MB
250
251 /* get start of the image payload, which in case of multi
252 * component images that points to a table of component sizes */
712fbcf3 253 size = (uint32_t *)image_get_data(hdr);
f13e7b2e
MB
254
255 /* get address of the proper component data start, which means
256 * skipping sizes table (add 1 for last, null entry) */
712fbcf3 257 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
f13e7b2e
MB
258
259 if (idx < count) {
712fbcf3 260 *len = uimage_to_cpu(size[idx]);
f13e7b2e 261 offset = 0;
f13e7b2e
MB
262
263 /* go over all indices preceding requested component idx */
264 for (i = 0; i < idx; i++) {
02b9b224 265 /* add up i-th component size, rounding up to 4 bytes */
712fbcf3 266 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
f13e7b2e
MB
267 }
268
269 /* calculate idx-th component data address */
02b9b224 270 *data = img_data + offset;
f13e7b2e
MB
271 } else {
272 *len = 0;
273 *data = 0;
274 }
275}
42b73e8e 276
712fbcf3 277static void image_print_type(const image_header_t *hdr)
9a4daad0
MB
278{
279 const char *os, *arch, *type, *comp;
280
712fbcf3
SW
281 os = genimg_get_os_name(image_get_os(hdr));
282 arch = genimg_get_arch_name(image_get_arch(hdr));
283 type = genimg_get_type_name(image_get_type(hdr));
284 comp = genimg_get_comp_name(image_get_comp(hdr));
9a4daad0 285
712fbcf3 286 printf("%s %s %s (%s)\n", arch, os, type, comp);
9a4daad0
MB
287}
288
5dfb5213 289/**
edbed247 290 * image_print_contents - prints out the contents of the legacy format image
3a2003f6 291 * @ptr: pointer to the legacy format image header
5dfb5213
MB
292 * @p: pointer to prefix string
293 *
edbed247 294 * image_print_contents() formats a multi line legacy image contents description.
5dfb5213
MB
295 * The routine prints out all header fields followed by the size/offset data
296 * for MULTI/SCRIPT images.
297 *
298 * returns:
299 * no returned results
300 */
712fbcf3 301void image_print_contents(const void *ptr)
9a4daad0 302{
3a2003f6 303 const image_header_t *hdr = (const image_header_t *)ptr;
edbed247
BS
304 const char *p;
305
306#ifdef USE_HOSTCC
307 p = "";
308#else
309 p = " ";
310#endif
311
712fbcf3 312 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
570abb0a 313#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
712fbcf3
SW
314 printf("%sCreated: ", p);
315 genimg_print_time((time_t)image_get_time(hdr));
9a4daad0 316#endif
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
9a4daad0
MB
351/**
352 * image_get_ramdisk - get and verify ramdisk image
9a4daad0
MB
353 * @rd_addr: ramdisk image start address
354 * @arch: expected ramdisk architecture
355 * @verify: checksum verification flag
356 *
357 * image_get_ramdisk() returns a pointer to the verified ramdisk image
358 * header. Routine receives image start address and expected architecture
359 * flag. Verification done covers data and header integrity and os/type/arch
360 * fields checking.
361 *
362 * If dataflash support is enabled routine checks for dataflash addresses
363 * and handles required dataflash reads.
364 *
365 * returns:
366 * pointer to a ramdisk image header, if image was found and valid
367 * otherwise, return NULL
368 */
712fbcf3 369static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
d985c849 370 int verify)
9a4daad0 371{
3a2003f6 372 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
9a4daad0 373
712fbcf3
SW
374 if (!image_check_magic(rd_hdr)) {
375 puts("Bad Magic Number\n");
770605e4 376 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
9a4daad0
MB
377 return NULL;
378 }
379
712fbcf3
SW
380 if (!image_check_hcrc(rd_hdr)) {
381 puts("Bad Header Checksum\n");
770605e4 382 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
9a4daad0
MB
383 return NULL;
384 }
385
770605e4 386 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
712fbcf3 387 image_print_contents(rd_hdr);
9a4daad0
MB
388
389 if (verify) {
390 puts(" Verifying Checksum ... ");
712fbcf3
SW
391 if (!image_check_dcrc(rd_hdr)) {
392 puts("Bad Data CRC\n");
770605e4 393 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
9a4daad0
MB
394 return NULL;
395 }
396 puts("OK\n");
397 }
398
770605e4 399 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
9a4daad0 400
712fbcf3
SW
401 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
402 !image_check_arch(rd_hdr, arch) ||
403 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
404 printf("No Linux %s Ramdisk Image\n",
9a4daad0 405 genimg_get_arch_name(arch));
770605e4 406 bootstage_error(BOOTSTAGE_ID_RAMDISK);
9a4daad0
MB
407 return NULL;
408 }
409
410 return rd_hdr;
411}
570abb0a 412#endif /* !USE_HOSTCC */
9a4daad0
MB
413
414/*****************************************************************************/
415/* Shared dual-format routines */
416/*****************************************************************************/
570abb0a 417#ifndef USE_HOSTCC
712fbcf3 418int getenv_yesno(char *var)
9a4daad0 419{
712fbcf3 420 char *s = getenv(var);
9a4daad0
MB
421 return (s && (*s == 'n')) ? 0 : 1;
422}
423
424ulong getenv_bootm_low(void)
425{
712fbcf3 426 char *s = getenv("bootm_low");
9a4daad0 427 if (s) {
712fbcf3 428 ulong tmp = simple_strtoul(s, NULL, 16);
9a4daad0
MB
429 return tmp;
430 }
431
6d0f6bcf
JCPV
432#if defined(CONFIG_SYS_SDRAM_BASE)
433 return CONFIG_SYS_SDRAM_BASE;
afe45c87
MB
434#elif defined(CONFIG_ARM)
435 return gd->bd->bi_dram[0].start;
9a4daad0
MB
436#else
437 return 0;
438#endif
439}
440
391fd93a 441phys_size_t getenv_bootm_size(void)
9a4daad0 442{
c519facc 443 phys_size_t tmp;
712fbcf3 444 char *s = getenv("bootm_size");
9a4daad0 445 if (s) {
712fbcf3 446 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
9a4daad0
MB
447 return tmp;
448 }
c519facc
MM
449 s = getenv("bootm_low");
450 if (s)
712fbcf3 451 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
c519facc
MM
452 else
453 tmp = 0;
454
9a4daad0 455
afe45c87 456#if defined(CONFIG_ARM)
c519facc 457 return gd->bd->bi_dram[0].size - tmp;
afe45c87 458#else
c519facc 459 return gd->bd->bi_memsize - tmp;
afe45c87 460#endif
9a4daad0
MB
461}
462
c3624e6e
GL
463phys_size_t getenv_bootm_mapsize(void)
464{
465 phys_size_t tmp;
712fbcf3 466 char *s = getenv("bootm_mapsize");
c3624e6e 467 if (s) {
712fbcf3 468 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
c3624e6e
GL
469 return tmp;
470 }
471
472#if defined(CONFIG_SYS_BOOTMAPSZ)
473 return CONFIG_SYS_BOOTMAPSZ;
474#else
475 return getenv_bootm_size();
476#endif
477}
478
712fbcf3 479void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
9a4daad0 480{
54fa2c5b
LJ
481 if (to == from)
482 return;
483
9a4daad0
MB
484#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
485 while (len > 0) {
486 size_t tail = (len > chunksz) ? chunksz : len;
712fbcf3
SW
487 WATCHDOG_RESET();
488 memmove(to, from, tail);
9a4daad0
MB
489 to += tail;
490 from += tail;
491 len -= tail;
492 }
493#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
712fbcf3 494 memmove(to, from, len);
9a4daad0
MB
495#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
496}
570abb0a 497#endif /* !USE_HOSTCC */
9a4daad0 498
712fbcf3 499void genimg_print_size(uint32_t size)
42b73e8e 500{
570abb0a 501#ifndef USE_HOSTCC
712fbcf3
SW
502 printf("%d Bytes = ", size);
503 print_size(size, "\n");
570abb0a 504#else
712fbcf3 505 printf("%d Bytes = %.2f kB = %.2f MB\n",
570abb0a
MB
506 size, (double)size / 1.024e3,
507 (double)size / 1.048576e6);
42b73e8e 508#endif
570abb0a
MB
509}
510
511#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
712fbcf3 512static void genimg_print_time(time_t timestamp)
570abb0a
MB
513{
514#ifndef USE_HOSTCC
515 struct rtc_time tm;
516
712fbcf3
SW
517 to_tm(timestamp, &tm);
518 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
570abb0a
MB
519 tm.tm_year, tm.tm_mon, tm.tm_mday,
520 tm.tm_hour, tm.tm_min, tm.tm_sec);
521#else
712fbcf3 522 printf("%s", ctime(&timestamp));
42b73e8e 523#endif
570abb0a
MB
524}
525#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
42b73e8e 526
570abb0a
MB
527/**
528 * get_table_entry_name - translate entry id to long name
529 * @table: pointer to a translation table for entries of a specific type
530 * @msg: message to be returned when translation fails
531 * @id: entry id to be translated
532 *
533 * get_table_entry_name() will go over translation table trying to find
534 * entry that matches given id. If matching entry is found, its long
535 * name is returned to the caller.
536 *
537 * returns:
538 * long entry name if translation succeeds
539 * msg otherwise
540 */
7edb186f 541char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
570abb0a
MB
542{
543 for (; table->id >= 0; ++table) {
544 if (table->id == id)
2e5167cc 545#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
e3d1ac7b
SW
546 return table->lname;
547#else
548 return table->lname + gd->reloc_off;
549#endif
42b73e8e 550 }
570abb0a
MB
551 return (msg);
552}
42b73e8e 553
712fbcf3 554const char *genimg_get_os_name(uint8_t os)
570abb0a 555{
712fbcf3 556 return (get_table_entry_name(uimage_os, "Unknown OS", os));
42b73e8e
MB
557}
558
712fbcf3 559const char *genimg_get_arch_name(uint8_t arch)
42b73e8e 560{
712fbcf3
SW
561 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
562 arch));
570abb0a 563}
42b73e8e 564
712fbcf3 565const char *genimg_get_type_name(uint8_t type)
570abb0a 566{
712fbcf3 567 return (get_table_entry_name(uimage_type, "Unknown Image", type));
570abb0a 568}
42b73e8e 569
712fbcf3 570const char *genimg_get_comp_name(uint8_t comp)
570abb0a 571{
712fbcf3
SW
572 return (get_table_entry_name(uimage_comp, "Unknown Compression",
573 comp));
42b73e8e
MB
574}
575
570abb0a
MB
576/**
577 * get_table_entry_id - translate short entry name to id
578 * @table: pointer to a translation table for entries of a specific type
579 * @table_name: to be used in case of error
580 * @name: entry short name to be translated
581 *
582 * get_table_entry_id() will go over translation table trying to find
583 * entry that matches given short name. If matching entry is found,
584 * its id returned to the caller.
585 *
586 * returns:
587 * entry id if translation succeeds
588 * -1 otherwise
589 */
7edb186f 590int get_table_entry_id(const table_entry_t *table,
570abb0a 591 const char *table_name, const char *name)
42b73e8e 592{
7edb186f 593 const table_entry_t *t;
570abb0a
MB
594#ifdef USE_HOSTCC
595 int first = 1;
42b73e8e 596
570abb0a
MB
597 for (t = table; t->id >= 0; ++t) {
598 if (t->sname && strcasecmp(t->sname, name) == 0)
712fbcf3 599 return(t->id);
42b73e8e
MB
600 }
601
712fbcf3 602 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
570abb0a
MB
603 for (t = table; t->id >= 0; ++t) {
604 if (t->sname == NULL)
605 continue;
712fbcf3 606 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
570abb0a
MB
607 first = 0;
608 }
712fbcf3 609 fprintf(stderr, "\n");
570abb0a
MB
610#else
611 for (t = table; t->id >= 0; ++t) {
2e5167cc 612#ifdef CONFIG_NEEDS_MANUAL_RELOC
e3d1ac7b 613 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
2e5167cc
WD
614#else
615 if (t->sname && strcmp(t->sname, name) == 0)
521af04d 616#endif
570abb0a
MB
617 return (t->id);
618 }
712fbcf3 619 debug("Invalid %s Type: %s\n", table_name, name);
570abb0a
MB
620#endif /* USE_HOSTCC */
621 return (-1);
622}
623
712fbcf3 624int genimg_get_os_id(const char *name)
570abb0a 625{
712fbcf3 626 return (get_table_entry_id(uimage_os, "OS", name));
570abb0a
MB
627}
628
712fbcf3 629int genimg_get_arch_id(const char *name)
570abb0a 630{
712fbcf3 631 return (get_table_entry_id(uimage_arch, "CPU", name));
42b73e8e 632}
5ad03eb3 633
712fbcf3 634int genimg_get_type_id(const char *name)
570abb0a 635{
712fbcf3 636 return (get_table_entry_id(uimage_type, "Image", name));
570abb0a
MB
637}
638
712fbcf3 639int genimg_get_comp_id(const char *name)
570abb0a 640{
712fbcf3 641 return (get_table_entry_id(uimage_comp, "Compression", name));
570abb0a
MB
642}
643
644#ifndef USE_HOSTCC
fff888a1 645/**
9a4daad0 646 * genimg_get_format - get image format type
fff888a1
MB
647 * @img_addr: image start address
648 *
9a4daad0 649 * genimg_get_format() checks whether provided address points to a valid
fff888a1
MB
650 * legacy or FIT image.
651 *
4efbe9db
MB
652 * New uImage format and FDT blob are based on a libfdt. FDT blob
653 * may be passed directly or embedded in a FIT image. In both situations
9a4daad0 654 * genimg_get_format() must be able to dectect libfdt header.
4efbe9db 655 *
fff888a1
MB
656 * returns:
657 * image format type or IMAGE_FORMAT_INVALID if no image is present
658 */
712fbcf3 659int genimg_get_format(void *img_addr)
fff888a1 660{
3a2003f6
WD
661 ulong format = IMAGE_FORMAT_INVALID;
662 const image_header_t *hdr;
4efbe9db 663#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
3a2003f6 664 char *fit_hdr;
fff888a1
MB
665#endif
666
3a2003f6 667 hdr = (const image_header_t *)img_addr;
fff888a1
MB
668 if (image_check_magic(hdr))
669 format = IMAGE_FORMAT_LEGACY;
4efbe9db 670#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
fff888a1
MB
671 else {
672 fit_hdr = (char *)img_addr;
712fbcf3 673 if (fdt_check_header(fit_hdr) == 0)
fff888a1
MB
674 format = IMAGE_FORMAT_FIT;
675 }
676#endif
677
678 return format;
679}
680
681/**
9a4daad0 682 * genimg_get_image - get image from special storage (if necessary)
fff888a1
MB
683 * @img_addr: image start address
684 *
9a4daad0 685 * genimg_get_image() checks if provided image start adddress is located
fff888a1
MB
686 * in a dataflash storage. If so, image is moved to a system RAM memory.
687 *
688 * returns:
689 * image start address after possible relocation from special storage
690 */
712fbcf3 691ulong genimg_get_image(ulong img_addr)
fff888a1 692{
6f0f9dfc 693 ulong ram_addr = img_addr;
fff888a1
MB
694
695#ifdef CONFIG_HAS_DATAFLASH
6f0f9dfc
MB
696 ulong h_size, d_size;
697
712fbcf3 698 if (addr_dataflash(img_addr)) {
6f0f9dfc 699 /* ger RAM address */
6d0f6bcf 700 ram_addr = CONFIG_SYS_LOAD_ADDR;
6f0f9dfc
MB
701
702 /* get header size */
712fbcf3 703 h_size = image_get_header_size();
6f0f9dfc
MB
704#if defined(CONFIG_FIT)
705 if (sizeof(struct fdt_header) > h_size)
706 h_size = sizeof(struct fdt_header);
707#endif
708
709 /* read in header */
712fbcf3 710 debug(" Reading image header from dataflash address "
fff888a1 711 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
fff888a1 712
712fbcf3 713 read_dataflash(img_addr, h_size, (char *)ram_addr);
fff888a1 714
6f0f9dfc 715 /* get data size */
712fbcf3 716 switch (genimg_get_format((void *)ram_addr)) {
6f0f9dfc 717 case IMAGE_FORMAT_LEGACY:
712fbcf3
SW
718 d_size = image_get_data_size(
719 (const image_header_t *)ram_addr);
720 debug(" Legacy format image found at 0x%08lx, "
721 "size 0x%08lx\n",
6f0f9dfc
MB
722 ram_addr, d_size);
723 break;
fff888a1 724#if defined(CONFIG_FIT)
6f0f9dfc 725 case IMAGE_FORMAT_FIT:
712fbcf3
SW
726 d_size = fit_get_size((const void *)ram_addr) - h_size;
727 debug(" FIT/FDT format image found at 0x%08lx, "
728 "size 0x%08lx\n",
6f0f9dfc
MB
729 ram_addr, d_size);
730 break;
fff888a1 731#endif
6f0f9dfc 732 default:
712fbcf3
SW
733 printf(" No valid image found at 0x%08lx\n",
734 img_addr);
6f0f9dfc
MB
735 return ram_addr;
736 }
fff888a1 737
6f0f9dfc 738 /* read in image data */
712fbcf3 739 debug(" Reading image remaining data from dataflash address "
fff888a1
MB
740 "%08lx to RAM address %08lx\n", img_addr + h_size,
741 ram_addr + h_size);
742
712fbcf3 743 read_dataflash(img_addr + h_size, d_size,
fff888a1 744 (char *)(ram_addr + h_size));
6f0f9dfc 745
fff888a1 746 }
6f0f9dfc 747#endif /* CONFIG_HAS_DATAFLASH */
fff888a1
MB
748
749 return ram_addr;
750}
751
f773bea8
MB
752/**
753 * fit_has_config - check if there is a valid FIT configuration
754 * @images: pointer to the bootm command headers structure
755 *
756 * fit_has_config() checks if there is a FIT configuration in use
757 * (if FTI support is present).
758 *
759 * returns:
760 * 0, no FIT support or no configuration found
761 * 1, configuration found
762 */
712fbcf3 763int genimg_has_config(bootm_headers_t *images)
f773bea8
MB
764{
765#if defined(CONFIG_FIT)
766 if (images->fit_uname_cfg)
767 return 1;
768#endif
769 return 0;
770}
771
5ad03eb3 772/**
9a4daad0 773 * boot_get_ramdisk - main ramdisk handling routine
5ad03eb3
MB
774 * @argc: command argument count
775 * @argv: command argument list
8a5ea3e6 776 * @images: pointer to the bootm images structure
5ad03eb3
MB
777 * @arch: expected ramdisk architecture
778 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
779 * @rd_end: pointer to a ulong variable, will hold ramdisk end
780 *
9a4daad0 781 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
5ad03eb3
MB
782 * Curently supported are the following ramdisk sources:
783 * - multicomponent kernel/ramdisk image,
784 * - commandline provided address of decicated ramdisk image.
785 *
786 * returns:
d985c849 787 * 0, if ramdisk image was found and valid, or skiped
5ad03eb3
MB
788 * rd_start and rd_end are set to ramdisk start/end addresses if
789 * ramdisk image is found and valid
d985c849 790 *
ea86b9e6 791 * 1, if ramdisk image is found but corrupted, or invalid
5ad03eb3 792 * rd_start and rd_end are set to 0 if no ramdisk exists
5ad03eb3 793 */
712fbcf3 794int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
d985c849 795 uint8_t arch, ulong *rd_start, ulong *rd_end)
5ad03eb3 796{
d5934ad7 797 ulong rd_addr, rd_load;
5ad03eb3 798 ulong rd_data, rd_len;
3a2003f6 799 const image_header_t *rd_hdr;
d5934ad7
MB
800#if defined(CONFIG_FIT)
801 void *fit_hdr;
802 const char *fit_uname_config = NULL;
803 const char *fit_uname_ramdisk = NULL;
804 ulong default_addr;
c8779648 805 int rd_noffset;
f773bea8 806 int cfg_noffset;
c8779648
MB
807 const void *data;
808 size_t size;
d5934ad7 809#endif
5ad03eb3 810
c8779648
MB
811 *rd_start = 0;
812 *rd_end = 0;
813
d5934ad7
MB
814 /*
815 * Look for a '-' which indicates to ignore the
816 * ramdisk argument
817 */
818 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
712fbcf3 819 debug("## Skipping init Ramdisk\n");
d5934ad7 820 rd_len = rd_data = 0;
712fbcf3 821 } else if (argc >= 3 || genimg_has_config(images)) {
d5934ad7 822#if defined(CONFIG_FIT)
f773bea8
MB
823 if (argc >= 3) {
824 /*
825 * If the init ramdisk comes from the FIT image and
826 * the FIT image address is omitted in the command
827 * line argument, try to use os FIT image address or
828 * default load address.
829 */
830 if (images->fit_uname_os)
831 default_addr = (ulong)images->fit_hdr_os;
832 else
833 default_addr = load_addr;
834
712fbcf3 835 if (fit_parse_conf(argv[2], default_addr,
f773bea8 836 &rd_addr, &fit_uname_config)) {
712fbcf3
SW
837 debug("* ramdisk: config '%s' from image at "
838 "0x%08lx\n",
f773bea8 839 fit_uname_config, rd_addr);
712fbcf3 840 } else if (fit_parse_subimage(argv[2], default_addr,
f773bea8 841 &rd_addr, &fit_uname_ramdisk)) {
712fbcf3
SW
842 debug("* ramdisk: subimage '%s' from image at "
843 "0x%08lx\n",
f773bea8
MB
844 fit_uname_ramdisk, rd_addr);
845 } else
d5934ad7 846#endif
f773bea8
MB
847 {
848 rd_addr = simple_strtoul(argv[2], NULL, 16);
712fbcf3
SW
849 debug("* ramdisk: cmdline image address = "
850 "0x%08lx\n",
f773bea8
MB
851 rd_addr);
852 }
853#if defined(CONFIG_FIT)
854 } else {
855 /* use FIT configuration provided in first bootm
856 * command argument
857 */
858 rd_addr = (ulong)images->fit_hdr_os;
859 fit_uname_config = images->fit_uname_cfg;
712fbcf3
SW
860 debug("* ramdisk: using config '%s' from image "
861 "at 0x%08lx\n",
f773bea8
MB
862 fit_uname_config, rd_addr);
863
864 /*
865 * Check whether configuration has ramdisk defined,
866 * if not, don't try to use it, quit silently.
867 */
868 fit_hdr = (void *)rd_addr;
712fbcf3
SW
869 cfg_noffset = fit_conf_get_node(fit_hdr,
870 fit_uname_config);
f773bea8 871 if (cfg_noffset < 0) {
712fbcf3 872 debug("* ramdisk: no such config\n");
c78fce69 873 return 1;
f773bea8
MB
874 }
875
712fbcf3
SW
876 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
877 cfg_noffset);
f773bea8 878 if (rd_noffset < 0) {
712fbcf3 879 debug("* ramdisk: no ramdisk in config\n");
41266c9b 880 return 0;
f773bea8 881 }
d5934ad7 882 }
f773bea8 883#endif
d5934ad7
MB
884
885 /* copy from dataflash if needed */
712fbcf3 886 rd_addr = genimg_get_image(rd_addr);
d5934ad7
MB
887
888 /*
889 * Check if there is an initrd image at the
890 * address provided in the second bootm argument
891 * check image type, for FIT images get FIT node.
892 */
712fbcf3 893 switch (genimg_get_format((void *)rd_addr)) {
d5934ad7 894 case IMAGE_FORMAT_LEGACY:
712fbcf3 895 printf("## Loading init Ramdisk from Legacy "
c8779648 896 "Image at %08lx ...\n", rd_addr);
5ad03eb3 897
770605e4 898 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
712fbcf3 899 rd_hdr = image_get_ramdisk(rd_addr, arch,
d985c849 900 images->verify);
5ad03eb3 901
c8779648 902 if (rd_hdr == NULL)
274cea2b 903 return 1;
274cea2b 904
712fbcf3
SW
905 rd_data = image_get_data(rd_hdr);
906 rd_len = image_get_data_size(rd_hdr);
907 rd_load = image_get_load(rd_hdr);
d5934ad7
MB
908 break;
909#if defined(CONFIG_FIT)
910 case IMAGE_FORMAT_FIT:
911 fit_hdr = (void *)rd_addr;
712fbcf3 912 printf("## Loading init Ramdisk from FIT "
c8779648
MB
913 "Image at %08lx ...\n", rd_addr);
914
770605e4 915 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
712fbcf3
SW
916 if (!fit_check_format(fit_hdr)) {
917 puts("Bad FIT ramdisk image format!\n");
770605e4 918 bootstage_error(
aacc8c16 919 BOOTSTAGE_ID_FIT_RD_FORMAT);
c78fce69 920 return 1;
c8779648 921 }
770605e4 922 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
c8779648
MB
923
924 if (!fit_uname_ramdisk) {
925 /*
926 * no ramdisk image node unit name, try to get config
927 * node first. If config unit node name is NULL
928 * fit_conf_get_node() will try to find default config node
929 */
770605e4 930 bootstage_mark(
aacc8c16 931 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
712fbcf3
SW
932 cfg_noffset = fit_conf_get_node(fit_hdr,
933 fit_uname_config);
f773bea8 934 if (cfg_noffset < 0) {
712fbcf3
SW
935 puts("Could not find configuration "
936 "node\n");
770605e4 937 bootstage_error(
aacc8c16 938 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
c78fce69 939 return 1;
1372cce2 940 }
712fbcf3
SW
941 fit_uname_config = fdt_get_name(fit_hdr,
942 cfg_noffset, NULL);
943 printf(" Using '%s' configuration\n",
944 fit_uname_config);
c8779648 945
712fbcf3
SW
946 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
947 cfg_noffset);
948 fit_uname_ramdisk = fit_get_name(fit_hdr,
949 rd_noffset, NULL);
c8779648
MB
950 } else {
951 /* get ramdisk component image node offset */
770605e4 952 bootstage_mark(
aacc8c16 953 BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
712fbcf3
SW
954 rd_noffset = fit_image_get_node(fit_hdr,
955 fit_uname_ramdisk);
c8779648 956 }
1372cce2 957 if (rd_noffset < 0) {
712fbcf3 958 puts("Could not find subimage node\n");
770605e4 959 bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
c78fce69 960 return 1;
1372cce2 961 }
c8779648 962
712fbcf3
SW
963 printf(" Trying '%s' ramdisk subimage\n",
964 fit_uname_ramdisk);
c8779648 965
770605e4 966 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
712fbcf3
SW
967 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
968 images->verify))
c78fce69 969 return 1;
c8779648
MB
970
971 /* get ramdisk image data address and length */
712fbcf3
SW
972 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
973 &size)) {
974 puts("Could not find ramdisk subimage data!\n");
770605e4 975 bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
c78fce69 976 return 1;
c8779648 977 }
770605e4 978 bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
c8779648
MB
979
980 rd_data = (ulong)data;
981 rd_len = size;
982
712fbcf3
SW
983 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
984 puts("Can't get ramdisk subimage load "
985 "address!\n");
770605e4 986 bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
c78fce69 987 return 1;
c8779648 988 }
770605e4 989 bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
c8779648
MB
990
991 images->fit_hdr_rd = fit_hdr;
992 images->fit_uname_rd = fit_uname_ramdisk;
3dfe1101 993 images->fit_noffset_rd = rd_noffset;
c8779648 994 break;
d5934ad7
MB
995#endif
996 default:
712fbcf3 997 puts("Wrong Ramdisk Image Format\n");
c8779648 998 rd_data = rd_len = rd_load = 0;
ea86b9e6 999 return 1;
d5934ad7 1000 }
d5934ad7 1001 } else if (images->legacy_hdr_valid &&
712fbcf3
SW
1002 image_check_type(&images->legacy_hdr_os_copy,
1003 IH_TYPE_MULTI)) {
1004
5ad03eb3 1005 /*
d5934ad7
MB
1006 * Now check if we have a legacy mult-component image,
1007 * get second entry data start address and len.
5ad03eb3 1008 */
770605e4 1009 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
712fbcf3 1010 printf("## Loading init Ramdisk from multi component "
c8779648 1011 "Legacy Image at %08lx ...\n",
d5934ad7
MB
1012 (ulong)images->legacy_hdr_os);
1013
712fbcf3 1014 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
5ad03eb3
MB
1015 } else {
1016 /*
1017 * no initrd image
1018 */
770605e4 1019 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
5ad03eb3
MB
1020 rd_len = rd_data = 0;
1021 }
1022
1023 if (!rd_data) {
712fbcf3 1024 debug("## No init Ramdisk\n");
5ad03eb3
MB
1025 } else {
1026 *rd_start = rd_data;
1027 *rd_end = rd_data + rd_len;
1028 }
712fbcf3 1029 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
5ad03eb3 1030 *rd_start, *rd_end);
274cea2b
KG
1031
1032 return 0;
5ad03eb3 1033}
ceaed2b1 1034
fca43cc8 1035#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
ceaed2b1 1036/**
9a4daad0 1037 * boot_ramdisk_high - relocate init ramdisk
e822d7fc 1038 * @lmb: pointer to lmb handle, will be used for memory mgmt
ceaed2b1
MB
1039 * @rd_data: ramdisk data start address
1040 * @rd_len: ramdisk data length
ceaed2b1
MB
1041 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1042 * start address (after possible relocation)
1043 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1044 * end address (after possible relocation)
1045 *
9a4daad0 1046 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
ceaed2b1
MB
1047 * variable and if requested ramdisk data is moved to a specified location.
1048 *
9a4daad0
MB
1049 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1050 * start/end addresses if ramdisk image start and len were provided,
1051 * otherwise set initrd_start and initrd_end set to zeros.
1052 *
ceaed2b1 1053 * returns:
9a4daad0
MB
1054 * 0 - success
1055 * -1 - failure
ceaed2b1 1056 */
712fbcf3 1057int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
e822d7fc 1058 ulong *initrd_start, ulong *initrd_end)
ceaed2b1
MB
1059{
1060 char *s;
1061 ulong initrd_high;
1062 int initrd_copy_to_ram = 1;
1063
712fbcf3 1064 if ((s = getenv("initrd_high")) != NULL) {
ceaed2b1
MB
1065 /* a value of "no" or a similar string will act like 0,
1066 * turning the "load high" feature off. This is intentional.
1067 */
712fbcf3 1068 initrd_high = simple_strtoul(s, NULL, 16);
ceaed2b1
MB
1069 if (initrd_high == ~0)
1070 initrd_copy_to_ram = 0;
1071 } else {
1072 /* not set, no restrictions to load high */
1073 initrd_high = ~0;
1074 }
1075
95d449ad
MB
1076
1077#ifdef CONFIG_LOGBUFFER
1078 /* Prevent initrd from overwriting logbuffer */
1079 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1080#endif
1081
712fbcf3 1082 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
ceaed2b1
MB
1083 initrd_high, initrd_copy_to_ram);
1084
1085 if (rd_data) {
1086 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
712fbcf3 1087 debug(" in-place initrd\n");
ceaed2b1
MB
1088 *initrd_start = rd_data;
1089 *initrd_end = rd_data + rd_len;
e822d7fc 1090 lmb_reserve(lmb, rd_data, rd_len);
ceaed2b1 1091 } else {
e822d7fc 1092 if (initrd_high)
712fbcf3
SW
1093 *initrd_start = (ulong)lmb_alloc_base(lmb,
1094 rd_len, 0x1000, initrd_high);
e822d7fc 1095 else
712fbcf3
SW
1096 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1097 0x1000);
e822d7fc
KG
1098
1099 if (*initrd_start == 0) {
712fbcf3 1100 puts("ramdisk - allocation error\n");
e822d7fc 1101 goto error;
ceaed2b1 1102 }
770605e4 1103 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
ceaed2b1
MB
1104
1105 *initrd_end = *initrd_start + rd_len;
712fbcf3 1106 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
ceaed2b1
MB
1107 *initrd_start, *initrd_end);
1108
712fbcf3 1109 memmove_wd((void *)*initrd_start,
ceaed2b1
MB
1110 (void *)rd_data, rd_len, CHUNKSZ);
1111
3b200110
KG
1112#ifdef CONFIG_MP
1113 /*
1114 * Ensure the image is flushed to memory to handle
1115 * AMP boot scenarios in which we might not be
1116 * HW cache coherent
1117 */
1118 flush_cache((unsigned long)*initrd_start, rd_len);
1119#endif
712fbcf3 1120 puts("OK\n");
ceaed2b1
MB
1121 }
1122 } else {
1123 *initrd_start = 0;
1124 *initrd_end = 0;
1125 }
712fbcf3 1126 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
ceaed2b1 1127 *initrd_start, *initrd_end);
9a4daad0 1128
e822d7fc 1129 return 0;
b6b0fe64 1130
e822d7fc
KG
1131error:
1132 return -1;
b6b0fe64 1133}
fca43cc8 1134#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
b6b0fe64 1135
06a09918 1136#ifdef CONFIG_OF_LIBFDT
712fbcf3 1137static void fdt_error(const char *msg)
06a09918 1138{
712fbcf3
SW
1139 puts("ERROR: ");
1140 puts(msg);
1141 puts(" - must RESET the board to recover.\n");
06a09918
KG
1142}
1143
712fbcf3 1144static const image_header_t *image_get_fdt(ulong fdt_addr)
06a09918 1145{
3a2003f6 1146 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
06a09918 1147
712fbcf3 1148 image_print_contents(fdt_hdr);
06a09918 1149
712fbcf3
SW
1150 puts(" Verifying Checksum ... ");
1151 if (!image_check_hcrc(fdt_hdr)) {
1152 fdt_error("fdt header checksum invalid");
06a09918
KG
1153 return NULL;
1154 }
1155
712fbcf3
SW
1156 if (!image_check_dcrc(fdt_hdr)) {
1157 fdt_error("fdt checksum invalid");
06a09918
KG
1158 return NULL;
1159 }
712fbcf3 1160 puts("OK\n");
06a09918 1161
712fbcf3
SW
1162 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1163 fdt_error("uImage is not a fdt");
06a09918
KG
1164 return NULL;
1165 }
712fbcf3
SW
1166 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1167 fdt_error("uImage is compressed");
06a09918
KG
1168 return NULL;
1169 }
712fbcf3
SW
1170 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1171 fdt_error("uImage data is not a fdt");
06a09918
KG
1172 return NULL;
1173 }
1174 return fdt_hdr;
1175}
1176
1177/**
1178 * fit_check_fdt - verify FIT format FDT subimage
1179 * @fit_hdr: pointer to the FIT header
1180 * fdt_noffset: FDT subimage node offset within FIT image
1181 * @verify: data CRC verification flag
1182 *
1183 * fit_check_fdt() verifies integrity of the FDT subimage and from
1184 * specified FIT image.
1185 *
1186 * returns:
1187 * 1, on success
1188 * 0, on failure
1189 */
1190#if defined(CONFIG_FIT)
712fbcf3 1191static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
06a09918 1192{
712fbcf3 1193 fit_image_print(fit, fdt_noffset, " ");
06a09918
KG
1194
1195 if (verify) {
712fbcf3
SW
1196 puts(" Verifying Hash Integrity ... ");
1197 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1198 fdt_error("Bad Data Hash");
06a09918
KG
1199 return 0;
1200 }
712fbcf3 1201 puts("OK\n");
06a09918
KG
1202 }
1203
712fbcf3
SW
1204 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1205 fdt_error("Not a FDT image");
06a09918
KG
1206 return 0;
1207 }
1208
712fbcf3
SW
1209 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1210 fdt_error("FDT image is compressed");
06a09918
KG
1211 return 0;
1212 }
1213
1214 return 1;
1215}
1216#endif /* CONFIG_FIT */
1217
6d0f6bcf
JCPV
1218#ifndef CONFIG_SYS_FDT_PAD
1219#define CONFIG_SYS_FDT_PAD 0x3000
06a09918
KG
1220#endif
1221
55b0a393
GL
1222#if defined(CONFIG_OF_LIBFDT)
1223/**
1224 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1225 * @lmb: pointer to lmb handle, will be used for memory mgmt
1226 * @fdt_blob: pointer to fdt blob base address
1227 *
1228 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1229 * memreserve regions prevents u-boot from using them to store the initrd
1230 * or the fdt blob.
1231 */
1232void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1233{
1234 uint64_t addr, size;
1235 int i, total;
1236
712fbcf3 1237 if (fdt_check_header(fdt_blob) != 0)
55b0a393
GL
1238 return;
1239
1240 total = fdt_num_mem_rsv(fdt_blob);
1241 for (i = 0; i < total; i++) {
1242 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1243 continue;
1244 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1245 (unsigned long long)addr, (unsigned long long)size);
1246 lmb_reserve(lmb, addr, size);
1247 }
1248}
1249
06a09918
KG
1250/**
1251 * boot_relocate_fdt - relocate flat device tree
1252 * @lmb: pointer to lmb handle, will be used for memory mgmt
06a09918
KG
1253 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1254 * @of_size: pointer to a ulong variable, will hold fdt length
1255 *
43b08af5
TT
1256 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1257 * relocates the of_flat_tree into that region, even if the fdt is already in
1258 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1259 * bytes.
06a09918
KG
1260 *
1261 * of_flat_tree and of_size are set to final (after relocation) values
1262 *
1263 * returns:
1264 * 0 - success
1265 * 1 - failure
1266 */
712fbcf3 1267int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
06a09918 1268{
43b08af5
TT
1269 void *fdt_blob = *of_flat_tree;
1270 void *of_start = 0;
a28afca5 1271 char *fdt_high;
06a09918 1272 ulong of_len = 0;
43b08af5 1273 int err;
a28afca5 1274 int disable_relocation = 0;
06a09918
KG
1275
1276 /* nothing to do */
1277 if (*of_size == 0)
1278 return 0;
1279
712fbcf3
SW
1280 if (fdt_check_header(fdt_blob) != 0) {
1281 fdt_error("image is not a fdt");
06a09918
KG
1282 goto error;
1283 }
1284
43b08af5
TT
1285 /* position on a 4K boundary before the alloc_current */
1286 /* Pad the FDT by a specified amount */
1287 of_len = *of_size + CONFIG_SYS_FDT_PAD;
a28afca5
DL
1288
1289 /* If fdt_high is set use it to select the relocation address */
1290 fdt_high = getenv("fdt_high");
1291 if (fdt_high) {
1292 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1293
1294 if (((ulong) desired_addr) == ~0UL) {
1295 /* All ones means use fdt in place */
fa34f6b2
SG
1296 of_start = fdt_blob;
1297 lmb_reserve(lmb, (ulong)of_start, of_len);
a28afca5 1298 disable_relocation = 1;
fa34f6b2 1299 } else if (desired_addr) {
a28afca5
DL
1300 of_start =
1301 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
fa34f6b2
SG
1302 (ulong)desired_addr);
1303 if (of_start == 0) {
a28afca5
DL
1304 puts("Failed using fdt_high value for Device Tree");
1305 goto error;
1306 }
1307 } else {
1308 of_start =
1bb5e907 1309 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
a28afca5
DL
1310 }
1311 } else {
1312 of_start =
1313 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1314 getenv_bootm_mapsize()
1315 + getenv_bootm_low());
1316 }
06a09918 1317
43b08af5
TT
1318 if (of_start == 0) {
1319 puts("device tree - allocation error\n");
1320 goto error;
1321 }
06a09918 1322
a28afca5
DL
1323 if (disable_relocation) {
1324 /* We assume there is space after the existing fdt to use for padding */
1325 fdt_set_totalsize(of_start, of_len);
1326 printf(" Using Device Tree in place at %p, end %p\n",
1327 of_start, of_start + of_len - 1);
1328 } else {
712fbcf3 1329 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
a28afca5 1330 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
06a09918 1331
712fbcf3 1332 printf(" Loading Device Tree to %p, end %p ... ",
a28afca5 1333 of_start, of_start + of_len - 1);
06a09918 1334
712fbcf3 1335 err = fdt_open_into(fdt_blob, of_start, of_len);
a28afca5 1336 if (err != 0) {
712fbcf3 1337 fdt_error("fdt move failed");
a28afca5
DL
1338 goto error;
1339 }
712fbcf3 1340 puts("OK\n");
06a09918 1341 }
43b08af5
TT
1342
1343 *of_flat_tree = of_start;
1344 *of_size = of_len;
06a09918 1345
54f9c866 1346 set_working_fdt_addr(*of_flat_tree);
06a09918
KG
1347 return 0;
1348
1349error:
1350 return 1;
1351}
ed59e587 1352#endif /* CONFIG_OF_LIBFDT */
06a09918
KG
1353
1354/**
1355 * boot_get_fdt - main fdt handling routine
1356 * @argc: command argument count
1357 * @argv: command argument list
1358 * @images: pointer to the bootm images structure
1359 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1360 * @of_size: pointer to a ulong variable, will hold fdt length
1361 *
1362 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1363 * Curently supported are the following ramdisk sources:
1364 * - multicomponent kernel/ramdisk image,
1365 * - commandline provided address of decicated ramdisk image.
1366 *
1367 * returns:
1368 * 0, if fdt image was found and valid, or skipped
1369 * of_flat_tree and of_size are set to fdt start address and length if
1370 * fdt image is found and valid
1371 *
1372 * 1, if fdt image is found but corrupted
1373 * of_flat_tree and of_size are set to 0 if no fdt exists
1374 */
712fbcf3
SW
1375int boot_get_fdt(int flag, int argc, char * const argv[],
1376 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
06a09918 1377{
3a2003f6 1378 const image_header_t *fdt_hdr;
06a09918 1379 ulong fdt_addr;
06a09918 1380 char *fdt_blob = NULL;
e37ae40e 1381 ulong image_start, image_data, image_end;
06a09918
KG
1382 ulong load_start, load_end;
1383#if defined(CONFIG_FIT)
1384 void *fit_hdr;
1385 const char *fit_uname_config = NULL;
1386 const char *fit_uname_fdt = NULL;
1387 ulong default_addr;
1388 int cfg_noffset;
1389 int fdt_noffset;
1390 const void *data;
1391 size_t size;
1392#endif
1393
1394 *of_flat_tree = NULL;
1395 *of_size = 0;
1396
712fbcf3 1397 if (argc > 3 || genimg_has_config(images)) {
06a09918
KG
1398#if defined(CONFIG_FIT)
1399 if (argc > 3) {
1400 /*
1401 * If the FDT blob comes from the FIT image and the
1402 * FIT image address is omitted in the command line
1403 * argument, try to use ramdisk or os FIT image
1404 * address or default load address.
1405 */
1406 if (images->fit_uname_rd)
1407 default_addr = (ulong)images->fit_hdr_rd;
1408 else if (images->fit_uname_os)
1409 default_addr = (ulong)images->fit_hdr_os;
1410 else
1411 default_addr = load_addr;
1412
712fbcf3 1413 if (fit_parse_conf(argv[3], default_addr,
06a09918 1414 &fdt_addr, &fit_uname_config)) {
712fbcf3
SW
1415 debug("* fdt: config '%s' from image at "
1416 "0x%08lx\n",
06a09918 1417 fit_uname_config, fdt_addr);
712fbcf3 1418 } else if (fit_parse_subimage(argv[3], default_addr,
06a09918 1419 &fdt_addr, &fit_uname_fdt)) {
712fbcf3
SW
1420 debug("* fdt: subimage '%s' from image at "
1421 "0x%08lx\n",
06a09918
KG
1422 fit_uname_fdt, fdt_addr);
1423 } else
1424#endif
1425 {
1426 fdt_addr = simple_strtoul(argv[3], NULL, 16);
712fbcf3
SW
1427 debug("* fdt: cmdline image address = "
1428 "0x%08lx\n",
06a09918
KG
1429 fdt_addr);
1430 }
1431#if defined(CONFIG_FIT)
1432 } else {
1433 /* use FIT configuration provided in first bootm
1434 * command argument
1435 */
1436 fdt_addr = (ulong)images->fit_hdr_os;
1437 fit_uname_config = images->fit_uname_cfg;
712fbcf3
SW
1438 debug("* fdt: using config '%s' from image "
1439 "at 0x%08lx\n",
06a09918
KG
1440 fit_uname_config, fdt_addr);
1441
1442 /*
1443 * Check whether configuration has FDT blob defined,
1444 * if not quit silently.
1445 */
1446 fit_hdr = (void *)fdt_addr;
712fbcf3 1447 cfg_noffset = fit_conf_get_node(fit_hdr,
06a09918
KG
1448 fit_uname_config);
1449 if (cfg_noffset < 0) {
712fbcf3 1450 debug("* fdt: no such config\n");
06a09918
KG
1451 return 0;
1452 }
1453
712fbcf3 1454 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
06a09918
KG
1455 cfg_noffset);
1456 if (fdt_noffset < 0) {
712fbcf3 1457 debug("* fdt: no fdt in config\n");
06a09918
KG
1458 return 0;
1459 }
1460 }
1461#endif
1462
712fbcf3 1463 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
06a09918
KG
1464 fdt_addr);
1465
1466 /* copy from dataflash if needed */
712fbcf3 1467 fdt_addr = genimg_get_image(fdt_addr);
06a09918
KG
1468
1469 /*
1470 * Check if there is an FDT image at the
1471 * address provided in the second bootm argument
1472 * check image type, for FIT images get a FIT node.
1473 */
712fbcf3 1474 switch (genimg_get_format((void *)fdt_addr)) {
06a09918
KG
1475 case IMAGE_FORMAT_LEGACY:
1476 /* verify fdt_addr points to a valid image header */
712fbcf3
SW
1477 printf("## Flattened Device Tree from Legacy Image "
1478 "at %08lx\n",
06a09918 1479 fdt_addr);
712fbcf3 1480 fdt_hdr = image_get_fdt(fdt_addr);
06a09918
KG
1481 if (!fdt_hdr)
1482 goto error;
1483
1484 /*
1485 * move image data to the load address,
1486 * make sure we don't overwrite initial image
1487 */
1488 image_start = (ulong)fdt_hdr;
e37ae40e 1489 image_data = (ulong)image_get_data(fdt_hdr);
712fbcf3 1490 image_end = image_get_image_end(fdt_hdr);
06a09918 1491
712fbcf3
SW
1492 load_start = image_get_load(fdt_hdr);
1493 load_end = load_start + image_get_data_size(fdt_hdr);
06a09918 1494
e37ae40e
SW
1495 if (load_start == image_start ||
1496 load_start == image_data) {
1497 fdt_blob = (char *)image_data;
1498 break;
1499 }
1500
06a09918 1501 if ((load_start < image_end) && (load_end > image_start)) {
712fbcf3 1502 fdt_error("fdt overwritten");
06a09918
KG
1503 goto error;
1504 }
1505
712fbcf3 1506 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
e37ae40e 1507 image_data, load_start);
06a09918 1508
712fbcf3 1509 memmove((void *)load_start,
e37ae40e 1510 (void *)image_data,
712fbcf3 1511 image_get_data_size(fdt_hdr));
06a09918
KG
1512
1513 fdt_blob = (char *)load_start;
1514 break;
1515 case IMAGE_FORMAT_FIT:
1516 /*
1517 * This case will catch both: new uImage format
1518 * (libfdt based) and raw FDT blob (also libfdt
1519 * based).
1520 */
1521#if defined(CONFIG_FIT)
1522 /* check FDT blob vs FIT blob */
712fbcf3 1523 if (fit_check_format((const void *)fdt_addr)) {
06a09918
KG
1524 /*
1525 * FIT image
1526 */
1527 fit_hdr = (void *)fdt_addr;
712fbcf3
SW
1528 printf("## Flattened Device Tree from FIT "
1529 "Image at %08lx\n",
06a09918
KG
1530 fdt_addr);
1531
1532 if (!fit_uname_fdt) {
1533 /*
1534 * no FDT blob image node unit name,
1535 * try to get config node first. If
1536 * config unit node name is NULL
1537 * fit_conf_get_node() will try to
1538 * find default config node
1539 */
712fbcf3 1540 cfg_noffset = fit_conf_get_node(fit_hdr,
06a09918
KG
1541 fit_uname_config);
1542
1543 if (cfg_noffset < 0) {
712fbcf3
SW
1544 fdt_error("Could not find "
1545 "configuration "
1546 "node\n");
06a09918
KG
1547 goto error;
1548 }
1549
712fbcf3 1550 fit_uname_config = fdt_get_name(fit_hdr,
06a09918 1551 cfg_noffset, NULL);
712fbcf3 1552 printf(" Using '%s' configuration\n",
06a09918
KG
1553 fit_uname_config);
1554
712fbcf3
SW
1555 fdt_noffset = fit_conf_get_fdt_node(
1556 fit_hdr,
06a09918 1557 cfg_noffset);
712fbcf3 1558 fit_uname_fdt = fit_get_name(fit_hdr,
06a09918
KG
1559 fdt_noffset, NULL);
1560 } else {
1561 /* get FDT component image node offset */
712fbcf3
SW
1562 fdt_noffset = fit_image_get_node(
1563 fit_hdr,
1564 fit_uname_fdt);
06a09918
KG
1565 }
1566 if (fdt_noffset < 0) {
712fbcf3
SW
1567 fdt_error("Could not find subimage "
1568 "node\n");
06a09918
KG
1569 goto error;
1570 }
1571
712fbcf3 1572 printf(" Trying '%s' FDT blob subimage\n",
06a09918
KG
1573 fit_uname_fdt);
1574
712fbcf3 1575 if (!fit_check_fdt(fit_hdr, fdt_noffset,
06a09918
KG
1576 images->verify))
1577 goto error;
1578
1579 /* get ramdisk image data address and length */
712fbcf3 1580 if (fit_image_get_data(fit_hdr, fdt_noffset,
06a09918 1581 &data, &size)) {
712fbcf3
SW
1582 fdt_error("Could not find FDT "
1583 "subimage data");
06a09918
KG
1584 goto error;
1585 }
1586
1587 /* verift that image data is a proper FDT blob */
712fbcf3
SW
1588 if (fdt_check_header((char *)data) != 0) {
1589 fdt_error("Subimage data is not a FTD");
06a09918
KG
1590 goto error;
1591 }
1592
1593 /*
1594 * move image data to the load address,
1595 * make sure we don't overwrite initial image
1596 */
1597 image_start = (ulong)fit_hdr;
712fbcf3 1598 image_end = fit_get_end(fit_hdr);
06a09918 1599
712fbcf3 1600 if (fit_image_get_load(fit_hdr, fdt_noffset,
06a09918
KG
1601 &load_start) == 0) {
1602 load_end = load_start + size;
1603
1604 if ((load_start < image_end) &&
1605 (load_end > image_start)) {
712fbcf3 1606 fdt_error("FDT overwritten");
06a09918
KG
1607 goto error;
1608 }
1609
712fbcf3
SW
1610 printf(" Loading FDT from 0x%08lx "
1611 "to 0x%08lx\n",
1612 (ulong)data,
1613 load_start);
06a09918 1614
712fbcf3 1615 memmove((void *)load_start,
06a09918
KG
1616 (void *)data, size);
1617
1618 fdt_blob = (char *)load_start;
1619 } else {
1620 fdt_blob = (char *)data;
1621 }
1622
1623 images->fit_hdr_fdt = fit_hdr;
1624 images->fit_uname_fdt = fit_uname_fdt;
1625 images->fit_noffset_fdt = fdt_noffset;
1626 break;
1627 } else
1628#endif
1629 {
1630 /*
1631 * FDT blob
1632 */
1633 fdt_blob = (char *)fdt_addr;
712fbcf3
SW
1634 debug("* fdt: raw FDT blob\n");
1635 printf("## Flattened Device Tree blob at "
1636 "%08lx\n", (long)fdt_blob);
06a09918
KG
1637 }
1638 break;
1639 default:
712fbcf3
SW
1640 puts("ERROR: Did not find a cmdline Flattened Device "
1641 "Tree\n");
06a09918
KG
1642 goto error;
1643 }
1644
0ec2ce4a 1645 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
06a09918
KG
1646
1647 } else if (images->legacy_hdr_valid &&
712fbcf3
SW
1648 image_check_type(&images->legacy_hdr_os_copy,
1649 IH_TYPE_MULTI)) {
06a09918
KG
1650
1651 ulong fdt_data, fdt_len;
1652
1653 /*
1654 * Now check if we have a legacy multi-component image,
1655 * get second entry data start address and len.
1656 */
712fbcf3 1657 printf("## Flattened Device Tree from multi "
06a09918
KG
1658 "component Image at %08lX\n",
1659 (ulong)images->legacy_hdr_os);
1660
712fbcf3
SW
1661 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1662 &fdt_len);
06a09918
KG
1663 if (fdt_len) {
1664
1665 fdt_blob = (char *)fdt_data;
0ec2ce4a 1666 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
06a09918 1667
712fbcf3
SW
1668 if (fdt_check_header(fdt_blob) != 0) {
1669 fdt_error("image is not a fdt");
06a09918
KG
1670 goto error;
1671 }
1672
d1263fce 1673 if (fdt_totalsize(fdt_blob) != fdt_len) {
712fbcf3 1674 fdt_error("fdt size != image size");
06a09918
KG
1675 goto error;
1676 }
1677 } else {
712fbcf3 1678 debug("## No Flattened Device Tree\n");
06a09918
KG
1679 return 0;
1680 }
1681 } else {
712fbcf3 1682 debug("## No Flattened Device Tree\n");
06a09918
KG
1683 return 0;
1684 }
1685
1686 *of_flat_tree = fdt_blob;
d1263fce 1687 *of_size = fdt_totalsize(fdt_blob);
712fbcf3 1688 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
52514699 1689 (ulong)*of_flat_tree, *of_size);
06a09918
KG
1690
1691 return 0;
1692
1693error:
1694 *of_flat_tree = 0;
1695 *of_size = 0;
1696 return 1;
1697}
1698#endif /* CONFIG_OF_LIBFDT */
1699
fca43cc8 1700#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
b6b0fe64 1701/**
9a4daad0 1702 * boot_get_cmdline - allocate and initialize kernel cmdline
e822d7fc 1703 * @lmb: pointer to lmb handle, will be used for memory mgmt
b6b0fe64
MB
1704 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1705 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1706 *
9a4daad0 1707 * boot_get_cmdline() allocates space for kernel command line below
590d3cac 1708 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
b6b0fe64
MB
1709 * variable is present its contents is copied to allocated kernel
1710 * command line.
1711 *
1712 * returns:
e822d7fc
KG
1713 * 0 - success
1714 * -1 - failure
b6b0fe64 1715 */
712fbcf3 1716int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
b6b0fe64
MB
1717{
1718 char *cmdline;
1719 char *s;
1720
6d0f6bcf 1721 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
c3624e6e 1722 getenv_bootm_mapsize() + getenv_bootm_low());
e822d7fc
KG
1723
1724 if (cmdline == NULL)
1725 return -1;
b6b0fe64
MB
1726
1727 if ((s = getenv("bootargs")) == NULL)
1728 s = "";
1729
1730 strcpy(cmdline, s);
1731
1732 *cmd_start = (ulong) & cmdline[0];
1733 *cmd_end = *cmd_start + strlen(cmdline);
1734
712fbcf3 1735 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
b6b0fe64 1736
e822d7fc 1737 return 0;
b6b0fe64 1738}
fca43cc8 1739#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
b6b0fe64 1740
fca43cc8 1741#ifdef CONFIG_SYS_BOOT_GET_KBD
b6b0fe64 1742/**
9a4daad0 1743 * boot_get_kbd - allocate and initialize kernel copy of board info
e822d7fc 1744 * @lmb: pointer to lmb handle, will be used for memory mgmt
b6b0fe64
MB
1745 * @kbd: double pointer to board info data
1746 *
9a4daad0 1747 * boot_get_kbd() allocates space for kernel copy of board info data below
590d3cac
GL
1748 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1749 * with the current u-boot board info data.
b6b0fe64
MB
1750 *
1751 * returns:
e822d7fc
KG
1752 * 0 - success
1753 * -1 - failure
b6b0fe64 1754 */
712fbcf3 1755int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
b6b0fe64 1756{
391fd93a 1757 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
c3624e6e 1758 getenv_bootm_mapsize() + getenv_bootm_low());
e822d7fc
KG
1759 if (*kbd == NULL)
1760 return -1;
1761
b6b0fe64
MB
1762 **kbd = *(gd->bd);
1763
712fbcf3 1764 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
b6b0fe64
MB
1765
1766#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1767 do_bdinfo(NULL, 0, 0, NULL);
1768#endif
1769
e822d7fc 1770 return 0;
ceaed2b1 1771}
fca43cc8 1772#endif /* CONFIG_SYS_BOOT_GET_KBD */
5dfb5213 1773#endif /* !USE_HOSTCC */
5ad03eb3 1774
f50433d6
MB
1775#if defined(CONFIG_FIT)
1776/*****************************************************************************/
1777/* New uImage format routines */
1778/*****************************************************************************/
5dfb5213 1779#ifndef USE_HOSTCC
712fbcf3 1780static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
f50433d6
MB
1781 ulong *addr, const char **name)
1782{
1783 const char *sep;
1784
1785 *addr = addr_curr;
1786 *name = NULL;
1787
712fbcf3 1788 sep = strchr(spec, sepc);
f50433d6
MB
1789 if (sep) {
1790 if (sep - spec > 0)
712fbcf3 1791 *addr = simple_strtoul(spec, NULL, 16);
f50433d6
MB
1792
1793 *name = sep + 1;
1794 return 1;
1795 }
1796
1797 return 0;
1798}
1799
1800/**
1801 * fit_parse_conf - parse FIT configuration spec
1802 * @spec: input string, containing configuration spec
1803 * @add_curr: current image address (to be used as a possible default)
1804 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1805 * configuration
1806 * @conf_name double pointer to a char, will hold pointer to a configuration
1807 * unit name
1808 *
1809 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1810 * where <addr> is a FIT image address that contains configuration
1811 * with a <conf> unit name.
1812 *
1813 * Address part is optional, and if omitted default add_curr will
1814 * be used instead.
1815 *
1816 * returns:
1817 * 1 if spec is a valid configuration string,
1818 * addr and conf_name are set accordingly
1819 * 0 otherwise
1820 */
712fbcf3 1821inline int fit_parse_conf(const char *spec, ulong addr_curr,
f50433d6
MB
1822 ulong *addr, const char **conf_name)
1823{
712fbcf3 1824 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
f50433d6
MB
1825}
1826
1827/**
1828 * fit_parse_subimage - parse FIT subimage spec
1829 * @spec: input string, containing subimage spec
1830 * @add_curr: current image address (to be used as a possible default)
1831 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1832 * subimage
1833 * @image_name: double pointer to a char, will hold pointer to a subimage name
1834 *
1835 * fit_parse_subimage() expects subimage spec in the for of
1836 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1837 * subimage with a <subimg> unit name.
1838 *
1839 * Address part is optional, and if omitted default add_curr will
1840 * be used instead.
1841 *
1842 * returns:
1843 * 1 if spec is a valid subimage string,
1844 * addr and image_name are set accordingly
1845 * 0 otherwise
1846 */
712fbcf3 1847inline int fit_parse_subimage(const char *spec, ulong addr_curr,
f50433d6
MB
1848 ulong *addr, const char **image_name)
1849{
712fbcf3 1850 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
f50433d6 1851}
5dfb5213
MB
1852#endif /* !USE_HOSTCC */
1853
712fbcf3 1854static void fit_get_debug(const void *fit, int noffset,
5dfb5213
MB
1855 char *prop_name, int err)
1856{
712fbcf3 1857 debug("Can't get '%s' property from FIT 0x%08lx, "
5dfb5213
MB
1858 "node: offset %d, name %s (%s)\n",
1859 prop_name, (ulong)fit, noffset,
712fbcf3
SW
1860 fit_get_name(fit, noffset, NULL),
1861 fdt_strerror(err));
5dfb5213
MB
1862}
1863
1864/**
edbed247 1865 * fit_print_contents - prints out the contents of the FIT format image
5dfb5213
MB
1866 * @fit: pointer to the FIT format image header
1867 * @p: pointer to prefix string
1868 *
edbed247 1869 * fit_print_contents() formats a multi line FIT image contents description.
5dfb5213
MB
1870 * The routine prints out FIT image properties (root node level) follwed by
1871 * the details of each component image.
1872 *
1873 * returns:
1874 * no returned results
1875 */
712fbcf3 1876void fit_print_contents(const void *fit)
5dfb5213
MB
1877{
1878 char *desc;
1879 char *uname;
1880 int images_noffset;
1881 int confs_noffset;
1882 int noffset;
1883 int ndepth;
1884 int count = 0;
1885 int ret;
edbed247 1886 const char *p;
5dfb5213
MB
1887#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1888 time_t timestamp;
1889#endif
1890
edbed247
BS
1891#ifdef USE_HOSTCC
1892 p = "";
1893#else
1894 p = " ";
1895#endif
1896
5dfb5213 1897 /* Root node properties */
712fbcf3
SW
1898 ret = fit_get_desc(fit, 0, &desc);
1899 printf("%sFIT description: ", p);
5dfb5213 1900 if (ret)
712fbcf3 1901 printf("unavailable\n");
5dfb5213 1902 else
712fbcf3 1903 printf("%s\n", desc);
5dfb5213
MB
1904
1905#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
712fbcf3
SW
1906 ret = fit_get_timestamp(fit, 0, &timestamp);
1907 printf("%sCreated: ", p);
5dfb5213 1908 if (ret)
712fbcf3 1909 printf("unavailable\n");
5dfb5213 1910 else
712fbcf3 1911 genimg_print_time(timestamp);
5dfb5213
MB
1912#endif
1913
1914 /* Find images parent node offset */
712fbcf3 1915 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
5dfb5213 1916 if (images_noffset < 0) {
712fbcf3
SW
1917 printf("Can't find images parent node '%s' (%s)\n",
1918 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
5dfb5213
MB
1919 return;
1920 }
1921
1922 /* Process its subnodes, print out component images details */
712fbcf3
SW
1923 for (ndepth = 0, count = 0,
1924 noffset = fdt_next_node(fit, images_noffset, &ndepth);
5dfb5213 1925 (noffset >= 0) && (ndepth > 0);
712fbcf3 1926 noffset = fdt_next_node(fit, noffset, &ndepth)) {
5dfb5213
MB
1927 if (ndepth == 1) {
1928 /*
1929 * Direct child node of the images parent node,
1930 * i.e. component image node.
1931 */
712fbcf3 1932 printf("%s Image %u (%s)\n", p, count++,
5dfb5213
MB
1933 fit_get_name(fit, noffset, NULL));
1934
712fbcf3 1935 fit_image_print(fit, noffset, p);
5dfb5213
MB
1936 }
1937 }
1938
1939 /* Find configurations parent node offset */
712fbcf3 1940 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
5dfb5213 1941 if (confs_noffset < 0) {
712fbcf3
SW
1942 debug("Can't get configurations parent node '%s' (%s)\n",
1943 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
5dfb5213
MB
1944 return;
1945 }
1946
1947 /* get default configuration unit name from default property */
712fbcf3 1948 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
5dfb5213 1949 if (uname)
712fbcf3 1950 printf("%s Default Configuration: '%s'\n", p, uname);
5dfb5213
MB
1951
1952 /* Process its subnodes, print out configurations details */
712fbcf3
SW
1953 for (ndepth = 0, count = 0,
1954 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
5dfb5213 1955 (noffset >= 0) && (ndepth > 0);
712fbcf3 1956 noffset = fdt_next_node(fit, noffset, &ndepth)) {
5dfb5213
MB
1957 if (ndepth == 1) {
1958 /*
1959 * Direct child node of the configurations parent node,
1960 * i.e. configuration node.
1961 */
712fbcf3 1962 printf("%s Configuration %u (%s)\n", p, count++,
5dfb5213
MB
1963 fit_get_name(fit, noffset, NULL));
1964
712fbcf3 1965 fit_conf_print(fit, noffset, p);
5dfb5213
MB
1966 }
1967 }
1968}
1969
5dfb5213
MB
1970/**
1971 * fit_image_print - prints out the FIT component image details
1972 * @fit: pointer to the FIT format image header
1973 * @image_noffset: offset of the component image node
1974 * @p: pointer to prefix string
1975 *
1976 * fit_image_print() lists all mandatory properies for the processed component
fbc87dc0
BS
1977 * image. If present, hash nodes are printed out as well. Load
1978 * address for images of type firmware is also printed out. Since the load
1979 * address is not mandatory for firmware images, it will be output as
1980 * "unavailable" when not present.
5dfb5213
MB
1981 *
1982 * returns:
1983 * no returned results
1984 */
712fbcf3 1985void fit_image_print(const void *fit, int image_noffset, const char *p)
5dfb5213
MB
1986{
1987 char *desc;
1988 uint8_t type, arch, os, comp;
1989 size_t size;
1990 ulong load, entry;
1991 const void *data;
1992 int noffset;
1993 int ndepth;
1994 int ret;
1995
1996 /* Mandatory properties */
712fbcf3
SW
1997 ret = fit_get_desc(fit, image_noffset, &desc);
1998 printf("%s Description: ", p);
5dfb5213 1999 if (ret)
712fbcf3 2000 printf("unavailable\n");
5dfb5213 2001 else
712fbcf3 2002 printf("%s\n", desc);
5dfb5213 2003
712fbcf3
SW
2004 fit_image_get_type(fit, image_noffset, &type);
2005 printf("%s Type: %s\n", p, genimg_get_type_name(type));
5dfb5213 2006
712fbcf3
SW
2007 fit_image_get_comp(fit, image_noffset, &comp);
2008 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
5dfb5213 2009
712fbcf3 2010 ret = fit_image_get_data(fit, image_noffset, &data, &size);
5dfb5213
MB
2011
2012#ifndef USE_HOSTCC
712fbcf3 2013 printf("%s Data Start: ", p);
5dfb5213 2014 if (ret)
712fbcf3 2015 printf("unavailable\n");
5dfb5213 2016 else
712fbcf3 2017 printf("0x%08lx\n", (ulong)data);
5dfb5213
MB
2018#endif
2019
712fbcf3 2020 printf("%s Data Size: ", p);
5dfb5213 2021 if (ret)
712fbcf3 2022 printf("unavailable\n");
5dfb5213 2023 else
712fbcf3 2024 genimg_print_size(size);
5dfb5213
MB
2025
2026 /* Remaining, type dependent properties */
2027 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2028 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2029 (type == IH_TYPE_FLATDT)) {
712fbcf3
SW
2030 fit_image_get_arch(fit, image_noffset, &arch);
2031 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
5dfb5213
MB
2032 }
2033
2034 if (type == IH_TYPE_KERNEL) {
712fbcf3
SW
2035 fit_image_get_os(fit, image_noffset, &os);
2036 printf("%s OS: %s\n", p, genimg_get_os_name(os));
5dfb5213
MB
2037 }
2038
fbc87dc0
BS
2039 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2040 (type == IH_TYPE_FIRMWARE)) {
712fbcf3
SW
2041 ret = fit_image_get_load(fit, image_noffset, &load);
2042 printf("%s Load Address: ", p);
5dfb5213 2043 if (ret)
712fbcf3 2044 printf("unavailable\n");
5dfb5213 2045 else
712fbcf3 2046 printf("0x%08lx\n", load);
fbc87dc0 2047 }
5dfb5213 2048
fbc87dc0 2049 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
712fbcf3
SW
2050 fit_image_get_entry(fit, image_noffset, &entry);
2051 printf("%s Entry Point: ", p);
5dfb5213 2052 if (ret)
712fbcf3 2053 printf("unavailable\n");
5dfb5213 2054 else
712fbcf3 2055 printf("0x%08lx\n", entry);
5dfb5213
MB
2056 }
2057
2058 /* Process all hash subnodes of the component image node */
712fbcf3 2059 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
5dfb5213 2060 (noffset >= 0) && (ndepth > 0);
712fbcf3 2061 noffset = fdt_next_node(fit, noffset, &ndepth)) {
5dfb5213
MB
2062 if (ndepth == 1) {
2063 /* Direct child node of the component image node */
712fbcf3 2064 fit_image_print_hash(fit, noffset, p);
5dfb5213
MB
2065 }
2066 }
2067}
2068
2069/**
2070 * fit_image_print_hash - prints out the hash node details
2071 * @fit: pointer to the FIT format image header
2072 * @noffset: offset of the hash node
2073 * @p: pointer to prefix string
2074 *
2075 * fit_image_print_hash() lists properies for the processed hash node
2076 *
2077 * returns:
2078 * no returned results
2079 */
712fbcf3 2080void fit_image_print_hash(const void *fit, int noffset, const char *p)
5dfb5213
MB
2081{
2082 char *algo;
2083 uint8_t *value;
2084 int value_len;
2085 int i, ret;
2086
2087 /*
2088 * Check subnode name, must be equal to "hash".
2089 * Multiple hash nodes require unique unit node
2090 * names, e.g. hash@1, hash@2, etc.
2091 */
712fbcf3 2092 if (strncmp(fit_get_name(fit, noffset, NULL),
5dfb5213
MB
2093 FIT_HASH_NODENAME,
2094 strlen(FIT_HASH_NODENAME)) != 0)
2095 return;
2096
712fbcf3
SW
2097 debug("%s Hash node: '%s'\n", p,
2098 fit_get_name(fit, noffset, NULL));
5dfb5213 2099
712fbcf3
SW
2100 printf("%s Hash algo: ", p);
2101 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2102 printf("invalid/unsupported\n");
5dfb5213
MB
2103 return;
2104 }
712fbcf3 2105 printf("%s\n", algo);
5dfb5213 2106
712fbcf3 2107 ret = fit_image_hash_get_value(fit, noffset, &value,
5dfb5213 2108 &value_len);
712fbcf3 2109 printf("%s Hash value: ", p);
5dfb5213 2110 if (ret) {
712fbcf3 2111 printf("unavailable\n");
5dfb5213
MB
2112 } else {
2113 for (i = 0; i < value_len; i++)
712fbcf3
SW
2114 printf("%02x", value[i]);
2115 printf("\n");
5dfb5213
MB
2116 }
2117
712fbcf3 2118 debug("%s Hash len: %d\n", p, value_len);
5dfb5213
MB
2119}
2120
2121/**
2122 * fit_get_desc - get node description property
2123 * @fit: pointer to the FIT format image header
2124 * @noffset: node offset
2125 * @desc: double pointer to the char, will hold pointer to the descrption
2126 *
2127 * fit_get_desc() reads description property from a given node, if
2128 * description is found pointer to it is returened in third call argument.
2129 *
2130 * returns:
2131 * 0, on success
2132 * -1, on failure
2133 */
712fbcf3 2134int fit_get_desc(const void *fit, int noffset, char **desc)
5dfb5213
MB
2135{
2136 int len;
2137
712fbcf3 2138 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
5dfb5213 2139 if (*desc == NULL) {
712fbcf3 2140 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
5dfb5213
MB
2141 return -1;
2142 }
2143
2144 return 0;
2145}
2146
2147/**
2148 * fit_get_timestamp - get node timestamp property
2149 * @fit: pointer to the FIT format image header
2150 * @noffset: node offset
2151 * @timestamp: pointer to the time_t, will hold read timestamp
2152 *
2153 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2154 * is found and has a correct size its value is retured in third call
2155 * argument.
2156 *
2157 * returns:
2158 * 0, on success
2159 * -1, on property read failure
2160 * -2, on wrong timestamp size
2161 */
712fbcf3 2162int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
5dfb5213
MB
2163{
2164 int len;
2165 const void *data;
2166
712fbcf3 2167 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
5dfb5213 2168 if (data == NULL) {
712fbcf3 2169 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
5dfb5213
MB
2170 return -1;
2171 }
712fbcf3
SW
2172 if (len != sizeof(uint32_t)) {
2173 debug("FIT timestamp with incorrect size of (%u)\n", len);
5dfb5213
MB
2174 return -2;
2175 }
2176
712fbcf3 2177 *timestamp = uimage_to_cpu(*((uint32_t *)data));
5dfb5213
MB
2178 return 0;
2179}
2180
2181/**
2182 * fit_image_get_node - get node offset for component image of a given unit name
2183 * @fit: pointer to the FIT format image header
2184 * @image_uname: component image node unit name
2185 *
2186 * fit_image_get_node() finds a component image (withing the '/images'
2187 * node) of a provided unit name. If image is found its node offset is
2188 * returned to the caller.
2189 *
2190 * returns:
2191 * image node offset when found (>=0)
2192 * negative number on failure (FDT_ERR_* code)
2193 */
712fbcf3 2194int fit_image_get_node(const void *fit, const char *image_uname)
5dfb5213
MB
2195{
2196 int noffset, images_noffset;
2197
712fbcf3 2198 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
5dfb5213 2199 if (images_noffset < 0) {
712fbcf3
SW
2200 debug("Can't find images parent node '%s' (%s)\n",
2201 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
5dfb5213
MB
2202 return images_noffset;
2203 }
2204
712fbcf3 2205 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
5dfb5213 2206 if (noffset < 0) {
712fbcf3
SW
2207 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2208 image_uname, fdt_strerror(noffset));
5dfb5213
MB
2209 }
2210
2211 return noffset;
2212}
2213
2214/**
2215 * fit_image_get_os - get os id for a given component image node
2216 * @fit: pointer to the FIT format image header
2217 * @noffset: component image node offset
2218 * @os: pointer to the uint8_t, will hold os numeric id
2219 *
2220 * fit_image_get_os() finds os property in a given component image node.
2221 * If the property is found, its (string) value is translated to the numeric
2222 * id which is returned to the caller.
2223 *
2224 * returns:
2225 * 0, on success
2226 * -1, on failure
2227 */
712fbcf3 2228int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
5dfb5213
MB
2229{
2230 int len;
2231 const void *data;
2232
2233 /* Get OS name from property data */
712fbcf3 2234 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
5dfb5213 2235 if (data == NULL) {
712fbcf3 2236 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
5dfb5213
MB
2237 *os = -1;
2238 return -1;
2239 }
2240
2241 /* Translate OS name to id */
712fbcf3 2242 *os = genimg_get_os_id(data);
5dfb5213
MB
2243 return 0;
2244}
2245
2246/**
2247 * fit_image_get_arch - get arch id for a given component image node
2248 * @fit: pointer to the FIT format image header
2249 * @noffset: component image node offset
2250 * @arch: pointer to the uint8_t, will hold arch numeric id
2251 *
2252 * fit_image_get_arch() finds arch property in a given component image node.
2253 * If the property is found, its (string) value is translated to the numeric
2254 * id which is returned to the caller.
2255 *
2256 * returns:
2257 * 0, on success
2258 * -1, on failure
2259 */
712fbcf3 2260int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
5dfb5213
MB
2261{
2262 int len;
2263 const void *data;
2264
2265 /* Get architecture name from property data */
712fbcf3 2266 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
5dfb5213 2267 if (data == NULL) {
712fbcf3 2268 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
5dfb5213
MB
2269 *arch = -1;
2270 return -1;
2271 }
2272
2273 /* Translate architecture name to id */
712fbcf3 2274 *arch = genimg_get_arch_id(data);
5dfb5213
MB
2275 return 0;
2276}
2277
2278/**
2279 * fit_image_get_type - get type id for a given component image node
2280 * @fit: pointer to the FIT format image header
2281 * @noffset: component image node offset
2282 * @type: pointer to the uint8_t, will hold type numeric id
2283 *
2284 * fit_image_get_type() finds type property in a given component image node.
2285 * If the property is found, its (string) value is translated to the numeric
2286 * id which is returned to the caller.
2287 *
2288 * returns:
2289 * 0, on success
2290 * -1, on failure
2291 */
712fbcf3 2292int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
5dfb5213
MB
2293{
2294 int len;
2295 const void *data;
2296
2297 /* Get image type name from property data */
712fbcf3 2298 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
5dfb5213 2299 if (data == NULL) {
712fbcf3 2300 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
5dfb5213
MB
2301 *type = -1;
2302 return -1;
2303 }
2304
2305 /* Translate image type name to id */
712fbcf3 2306 *type = genimg_get_type_id(data);
5dfb5213
MB
2307 return 0;
2308}
2309
2310/**
2311 * fit_image_get_comp - get comp id for a given component image node
2312 * @fit: pointer to the FIT format image header
2313 * @noffset: component image node offset
2314 * @comp: pointer to the uint8_t, will hold comp numeric id
2315 *
2316 * fit_image_get_comp() finds comp property in a given component image node.
2317 * If the property is found, its (string) value is translated to the numeric
2318 * id which is returned to the caller.
2319 *
2320 * returns:
2321 * 0, on success
2322 * -1, on failure
2323 */
712fbcf3 2324int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
5dfb5213
MB
2325{
2326 int len;
2327 const void *data;
2328
2329 /* Get compression name from property data */
712fbcf3 2330 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
5dfb5213 2331 if (data == NULL) {
712fbcf3 2332 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
5dfb5213
MB
2333 *comp = -1;
2334 return -1;
2335 }
2336
2337 /* Translate compression name to id */
712fbcf3 2338 *comp = genimg_get_comp_id(data);
5dfb5213
MB
2339 return 0;
2340}
2341
2342/**
2343 * fit_image_get_load - get load address property for a given component image node
2344 * @fit: pointer to the FIT format image header
2345 * @noffset: component image node offset
2346 * @load: pointer to the uint32_t, will hold load address
2347 *
2348 * fit_image_get_load() finds load address property in a given component image node.
2349 * If the property is found, its value is returned to the caller.
2350 *
2351 * returns:
2352 * 0, on success
2353 * -1, on failure
2354 */
712fbcf3 2355int fit_image_get_load(const void *fit, int noffset, ulong *load)
5dfb5213
MB
2356{
2357 int len;
2358 const uint32_t *data;
2359
712fbcf3 2360 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
5dfb5213 2361 if (data == NULL) {
712fbcf3 2362 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
5dfb5213
MB
2363 return -1;
2364 }
2365
712fbcf3 2366 *load = uimage_to_cpu(*data);
5dfb5213
MB
2367 return 0;
2368}
2369
2370/**
2371 * fit_image_get_entry - get entry point address property for a given component image node
2372 * @fit: pointer to the FIT format image header
2373 * @noffset: component image node offset
2374 * @entry: pointer to the uint32_t, will hold entry point address
2375 *
2376 * fit_image_get_entry() finds entry point address property in a given component image node.
2377 * If the property is found, its value is returned to the caller.
2378 *
2379 * returns:
2380 * 0, on success
2381 * -1, on failure
2382 */
712fbcf3 2383int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
5dfb5213
MB
2384{
2385 int len;
2386 const uint32_t *data;
2387
712fbcf3 2388 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
5dfb5213 2389 if (data == NULL) {
712fbcf3 2390 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
5dfb5213
MB
2391 return -1;
2392 }
2393
712fbcf3 2394 *entry = uimage_to_cpu(*data);
5dfb5213
MB
2395 return 0;
2396}
2397
2398/**
2399 * fit_image_get_data - get data property and its size for a given component image node
2400 * @fit: pointer to the FIT format image header
2401 * @noffset: component image node offset
2402 * @data: double pointer to void, will hold data property's data address
2403 * @size: pointer to size_t, will hold data property's data size
2404 *
2405 * fit_image_get_data() finds data property in a given component image node.
2406 * If the property is found its data start address and size are returned to
2407 * the caller.
2408 *
2409 * returns:
2410 * 0, on success
2411 * -1, on failure
2412 */
712fbcf3 2413int fit_image_get_data(const void *fit, int noffset,
5dfb5213
MB
2414 const void **data, size_t *size)
2415{
2416 int len;
2417
712fbcf3 2418 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
5dfb5213 2419 if (*data == NULL) {
712fbcf3 2420 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
5dfb5213
MB
2421 *size = 0;
2422 return -1;
2423 }
2424
2425 *size = len;
2426 return 0;
2427}
2428
2429/**
2430 * fit_image_hash_get_algo - get hash algorithm name
2431 * @fit: pointer to the FIT format image header
2432 * @noffset: hash node offset
2433 * @algo: double pointer to char, will hold pointer to the algorithm name
2434 *
2435 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2436 * If the property is found its data start address is returned to the caller.
2437 *
2438 * returns:
2439 * 0, on success
2440 * -1, on failure
2441 */
712fbcf3 2442int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
5dfb5213
MB
2443{
2444 int len;
2445
712fbcf3 2446 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
5dfb5213 2447 if (*algo == NULL) {
712fbcf3 2448 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
5dfb5213
MB
2449 return -1;
2450 }
2451
2452 return 0;
2453}
2454
2455/**
2456 * fit_image_hash_get_value - get hash value and length
2457 * @fit: pointer to the FIT format image header
2458 * @noffset: hash node offset
2459 * @value: double pointer to uint8_t, will hold address of a hash value data
2460 * @value_len: pointer to an int, will hold hash data length
2461 *
2462 * fit_image_hash_get_value() finds hash value property in a given hash node.
2463 * If the property is found its data start address and size are returned to
2464 * the caller.
2465 *
2466 * returns:
2467 * 0, on success
2468 * -1, on failure
2469 */
712fbcf3 2470int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
5dfb5213
MB
2471 int *value_len)
2472{
2473 int len;
2474
712fbcf3 2475 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
5dfb5213 2476 if (*value == NULL) {
712fbcf3 2477 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
5dfb5213
MB
2478 *value_len = 0;
2479 return -1;
2480 }
2481
2482 *value_len = len;
2483 return 0;
2484}
2485
2486/**
2487 * fit_set_timestamp - set node timestamp property
2488 * @fit: pointer to the FIT format image header
2489 * @noffset: node offset
2490 * @timestamp: timestamp value to be set
2491 *
2492 * fit_set_timestamp() attempts to set timestamp property in the requested
2493 * node and returns operation status to the caller.
2494 *
2495 * returns:
2496 * 0, on success
2497 * -1, on property read failure
2498 */
712fbcf3 2499int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
5dfb5213
MB
2500{
2501 uint32_t t;
2502 int ret;
2503
712fbcf3
SW
2504 t = cpu_to_uimage(timestamp);
2505 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2506 sizeof(uint32_t));
5dfb5213 2507 if (ret) {
712fbcf3
SW
2508 printf("Can't set '%s' property for '%s' node (%s)\n",
2509 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2510 fdt_strerror(ret));
5dfb5213
MB
2511 return -1;
2512 }
2513
2514 return 0;
2515}
2516
2517/**
2518 * calculate_hash - calculate and return hash for provided input data
2519 * @data: pointer to the input data
2520 * @data_len: data length
2521 * @algo: requested hash algorithm
2522 * @value: pointer to the char, will hold hash value data (caller must
2523 * allocate enough free space)
2524 * value_len: length of the calculated hash
2525 *
2526 * calculate_hash() computes input data hash according to the requested algorithm.
2527 * Resulting hash value is placed in caller provided 'value' buffer, length
2528 * of the calculated hash is returned via value_len pointer argument.
2529 *
2530 * returns:
2531 * 0, on success
2532 * -1, when algo is unsupported
2533 */
712fbcf3 2534static int calculate_hash(const void *data, int data_len, const char *algo,
5dfb5213
MB
2535 uint8_t *value, int *value_len)
2536{
712fbcf3
SW
2537 if (strcmp(algo, "crc32") == 0) {
2538 *((uint32_t *)value) = crc32_wd(0, data, data_len,
7590378f 2539 CHUNKSZ_CRC32);
712fbcf3 2540 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
5dfb5213 2541 *value_len = 4;
712fbcf3
SW
2542 } else if (strcmp(algo, "sha1") == 0) {
2543 sha1_csum_wd((unsigned char *) data, data_len,
7590378f 2544 (unsigned char *) value, CHUNKSZ_SHA1);
5dfb5213 2545 *value_len = 20;
712fbcf3
SW
2546 } else if (strcmp(algo, "md5") == 0) {
2547 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
766529fc 2548 *value_len = 16;
5dfb5213 2549 } else {
712fbcf3 2550 debug("Unsupported hash alogrithm\n");
5dfb5213
MB
2551 return -1;
2552 }
2553 return 0;
2554}
2555
2556#ifdef USE_HOSTCC
2557/**
2558 * fit_set_hashes - process FIT component image nodes and calculate hashes
2559 * @fit: pointer to the FIT format image header
2560 *
2561 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2562 * Hashes are calculated for all component images which have hash subnodes
2563 * with algorithm property set to one of the supported hash algorithms.
2564 *
2565 * returns
2566 * 0, on success
2567 * libfdt error code, on failure
2568 */
712fbcf3 2569int fit_set_hashes(void *fit)
5dfb5213
MB
2570{
2571 int images_noffset;
2572 int noffset;
2573 int ndepth;
2574 int ret;
2575
2576 /* Find images parent node offset */
712fbcf3 2577 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
5dfb5213 2578 if (images_noffset < 0) {
712fbcf3
SW
2579 printf("Can't find images parent node '%s' (%s)\n",
2580 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
5dfb5213
MB
2581 return images_noffset;
2582 }
2583
2584 /* Process its subnodes, print out component images details */
712fbcf3 2585 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
5dfb5213 2586 (noffset >= 0) && (ndepth > 0);
712fbcf3 2587 noffset = fdt_next_node(fit, noffset, &ndepth)) {
5dfb5213
MB
2588 if (ndepth == 1) {
2589 /*
2590 * Direct child node of the images parent node,
2591 * i.e. component image node.
2592 */
712fbcf3 2593 ret = fit_image_set_hashes(fit, noffset);
5dfb5213
MB
2594 if (ret)
2595 return ret;
2596 }
2597 }
2598
2599 return 0;
2600}
2601
2602/**
2603 * fit_image_set_hashes - calculate/set hashes for given component image node
2604 * @fit: pointer to the FIT format image header
2605 * @image_noffset: requested component image node
2606 *
2607 * fit_image_set_hashes() adds hash values for an component image node. All
2608 * existing hash subnodes are checked, if algorithm property is set to one of
2609 * the supported hash algorithms, hash value is computed and corresponding
2610 * hash node property is set, for example:
2611 *
2612 * Input component image node structure:
2613 *
2614 * o image@1 (at image_noffset)
2615 * | - data = [binary data]
2616 * o hash@1
2617 * |- algo = "sha1"
2618 *
2619 * Output component image node structure:
2620 *
2621 * o image@1 (at image_noffset)
2622 * | - data = [binary data]
2623 * o hash@1
2624 * |- algo = "sha1"
2625 * |- value = sha1(data)
2626 *
2627 * returns:
2628 * 0 on sucess
2629 * <0 on failure
2630 */
712fbcf3 2631int fit_image_set_hashes(void *fit, int image_noffset)
5dfb5213
MB
2632{
2633 const void *data;
2634 size_t size;
2635 char *algo;
2636 uint8_t value[FIT_MAX_HASH_LEN];
2637 int value_len;
2638 int noffset;
2639 int ndepth;
2640
2641 /* Get image data and data length */
712fbcf3
SW
2642 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2643 printf("Can't get image data/size\n");
5dfb5213
MB
2644 return -1;
2645 }
2646
2647 /* Process all hash subnodes of the component image node */
712fbcf3 2648 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
5dfb5213 2649 (noffset >= 0) && (ndepth > 0);
712fbcf3 2650 noffset = fdt_next_node(fit, noffset, &ndepth)) {
5dfb5213
MB
2651 if (ndepth == 1) {
2652 /* Direct child node of the component image node */
2653
2654 /*
2655 * Check subnode name, must be equal to "hash".
2656 * Multiple hash nodes require unique unit node
2657 * names, e.g. hash@1, hash@2, etc.
2658 */
712fbcf3 2659 if (strncmp(fit_get_name(fit, noffset, NULL),
5dfb5213
MB
2660 FIT_HASH_NODENAME,
2661 strlen(FIT_HASH_NODENAME)) != 0) {
2662 /* Not a hash subnode, skip it */
2663 continue;
2664 }
2665
712fbcf3
SW
2666 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2667 printf("Can't get hash algo property for "
5dfb5213 2668 "'%s' hash node in '%s' image node\n",
712fbcf3
SW
2669 fit_get_name(fit, noffset, NULL),
2670 fit_get_name(fit, image_noffset, NULL));
5dfb5213
MB
2671 return -1;
2672 }
2673
712fbcf3
SW
2674 if (calculate_hash(data, size, algo, value,
2675 &value_len)) {
2676 printf("Unsupported hash algorithm (%s) for "
5dfb5213 2677 "'%s' hash node in '%s' image node\n",
712fbcf3
SW
2678 algo, fit_get_name(fit, noffset, NULL),
2679 fit_get_name(fit, image_noffset,
2680 NULL));
5dfb5213
MB
2681 return -1;
2682 }
2683
712fbcf3 2684 if (fit_image_hash_set_value(fit, noffset, value,
5dfb5213 2685 value_len)) {
712fbcf3 2686 printf("Can't set hash value for "
5dfb5213 2687 "'%s' hash node in '%s' image node\n",
712fbcf3
SW
2688 fit_get_name(fit, noffset, NULL),
2689 fit_get_name(fit, image_noffset, NULL));
5dfb5213
MB
2690 return -1;
2691 }
2692 }
2693 }
2694
2695 return 0;
2696}
2697
2698/**
2699 * fit_image_hash_set_value - set hash value in requested has node
2700 * @fit: pointer to the FIT format image header
2701 * @noffset: hash node offset
2702 * @value: hash value to be set
2703 * @value_len: hash value length
2704 *
2705 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2706 * given and returns operation status to the caller.
2707 *
2708 * returns
2709 * 0, on success
2710 * -1, on failure
2711 */
712fbcf3 2712int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
5dfb5213
MB
2713 int value_len)
2714{
2715 int ret;
2716
712fbcf3 2717 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
5dfb5213 2718 if (ret) {
712fbcf3
SW
2719 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2720 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2721 fdt_strerror(ret));
5dfb5213
MB
2722 return -1;
2723 }
2724
2725 return 0;
2726}
2727#endif /* USE_HOSTCC */
2728
2729/**
2730 * fit_image_check_hashes - verify data intergity
2731 * @fit: pointer to the FIT format image header
2732 * @image_noffset: component image node offset
2733 *
2734 * fit_image_check_hashes() goes over component image hash nodes,
2735 * re-calculates each data hash and compares with the value stored in hash
2736 * node.
2737 *
2738 * returns:
2739 * 1, if all hashes are valid
2740 * 0, otherwise (or on error)
2741 */
712fbcf3 2742int fit_image_check_hashes(const void *fit, int image_noffset)
5dfb5213
MB
2743{
2744 const void *data;
2745 size_t size;
2746 char *algo;
2747 uint8_t *fit_value;
2748 int fit_value_len;
2749 uint8_t value[FIT_MAX_HASH_LEN];
2750 int value_len;
2751 int noffset;
2752 int ndepth;
2753 char *err_msg = "";
2754
2755 /* Get image data and data length */
712fbcf3
SW
2756 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2757 printf("Can't get image data/size\n");
5dfb5213
MB
2758 return 0;
2759 }
2760
2761 /* Process all hash subnodes of the component image node */
712fbcf3 2762 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
5dfb5213 2763 (noffset >= 0) && (ndepth > 0);
712fbcf3 2764 noffset = fdt_next_node(fit, noffset, &ndepth)) {
5dfb5213
MB
2765 if (ndepth == 1) {
2766 /* Direct child node of the component image node */
2767
2768 /*
2769 * Check subnode name, must be equal to "hash".
2770 * Multiple hash nodes require unique unit node
2771 * names, e.g. hash@1, hash@2, etc.
2772 */
712fbcf3 2773 if (strncmp(fit_get_name(fit, noffset, NULL),
5dfb5213
MB
2774 FIT_HASH_NODENAME,
2775 strlen(FIT_HASH_NODENAME)) != 0)
2776 continue;
2777
712fbcf3 2778 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
919f550d
BS
2779 err_msg = " error!\nCan't get hash algo "
2780 "property";
5dfb5213
MB
2781 goto error;
2782 }
712fbcf3 2783 printf("%s", algo);
5dfb5213 2784
712fbcf3 2785 if (fit_image_hash_get_value(fit, noffset, &fit_value,
5dfb5213 2786 &fit_value_len)) {
919f550d
BS
2787 err_msg = " error!\nCan't get hash value "
2788 "property";
5dfb5213
MB
2789 goto error;
2790 }
2791
712fbcf3
SW
2792 if (calculate_hash(data, size, algo, value,
2793 &value_len)) {
2794 err_msg = " error!\n"
2795 "Unsupported hash algorithm";
5dfb5213
MB
2796 goto error;
2797 }
2798
2799 if (value_len != fit_value_len) {
919f550d 2800 err_msg = " error !\nBad hash value len";
5dfb5213 2801 goto error;
712fbcf3 2802 } else if (memcmp(value, fit_value, value_len) != 0) {
919f550d 2803 err_msg = " error!\nBad hash value";
5dfb5213
MB
2804 goto error;
2805 }
712fbcf3 2806 printf("+ ");
5dfb5213
MB
2807 }
2808 }
2809
2810 return 1;
2811
2812error:
712fbcf3
SW
2813 printf("%s for '%s' hash node in '%s' image node\n",
2814 err_msg, fit_get_name(fit, noffset, NULL),
2815 fit_get_name(fit, image_noffset, NULL));
5dfb5213
MB
2816 return 0;
2817}
2818
919f550d
BS
2819/**
2820 * fit_all_image_check_hashes - verify data intergity for all images
2821 * @fit: pointer to the FIT format image header
2822 *
2823 * fit_all_image_check_hashes() goes over all images in the FIT and
2824 * for every images checks if all it's hashes are valid.
2825 *
2826 * returns:
2827 * 1, if all hashes of all images are valid
2828 * 0, otherwise (or on error)
2829 */
712fbcf3 2830int fit_all_image_check_hashes(const void *fit)
919f550d
BS
2831{
2832 int images_noffset;
2833 int noffset;
2834 int ndepth;
2835 int count;
2836
2837 /* Find images parent node offset */
712fbcf3 2838 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
919f550d 2839 if (images_noffset < 0) {
712fbcf3
SW
2840 printf("Can't find images parent node '%s' (%s)\n",
2841 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
919f550d
BS
2842 return 0;
2843 }
2844
2845 /* Process all image subnodes, check hashes for each */
712fbcf3 2846 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
919f550d
BS
2847 (ulong)fit);
2848 for (ndepth = 0, count = 0,
712fbcf3 2849 noffset = fdt_next_node(fit, images_noffset, &ndepth);
919f550d 2850 (noffset >= 0) && (ndepth > 0);
712fbcf3 2851 noffset = fdt_next_node(fit, noffset, &ndepth)) {
919f550d
BS
2852 if (ndepth == 1) {
2853 /*
2854 * Direct child node of the images parent node,
2855 * i.e. component image node.
2856 */
712fbcf3
SW
2857 printf(" Hash(es) for Image %u (%s): ", count++,
2858 fit_get_name(fit, noffset, NULL));
919f550d 2859
712fbcf3 2860 if (!fit_image_check_hashes(fit, noffset))
919f550d 2861 return 0;
712fbcf3 2862 printf("\n");
919f550d
BS
2863 }
2864 }
2865 return 1;
2866}
2867
5dfb5213
MB
2868/**
2869 * fit_image_check_os - check whether image node is of a given os type
2870 * @fit: pointer to the FIT format image header
2871 * @noffset: component image node offset
2872 * @os: requested image os
2873 *
2874 * fit_image_check_os() reads image os property and compares its numeric
2875 * id with the requested os. Comparison result is returned to the caller.
2876 *
2877 * returns:
2878 * 1 if image is of given os type
2879 * 0 otherwise (or on error)
2880 */
712fbcf3 2881int fit_image_check_os(const void *fit, int noffset, uint8_t os)
5dfb5213
MB
2882{
2883 uint8_t image_os;
2884
712fbcf3 2885 if (fit_image_get_os(fit, noffset, &image_os))
5dfb5213
MB
2886 return 0;
2887 return (os == image_os);
2888}
2889
2890/**
2891 * fit_image_check_arch - check whether image node is of a given arch
2892 * @fit: pointer to the FIT format image header
2893 * @noffset: component image node offset
2894 * @arch: requested imagearch
2895 *
2896 * fit_image_check_arch() reads image arch property and compares its numeric
2897 * id with the requested arch. Comparison result is returned to the caller.
2898 *
2899 * returns:
2900 * 1 if image is of given arch
2901 * 0 otherwise (or on error)
2902 */
712fbcf3 2903int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
5dfb5213
MB
2904{
2905 uint8_t image_arch;
2906
712fbcf3 2907 if (fit_image_get_arch(fit, noffset, &image_arch))
5dfb5213
MB
2908 return 0;
2909 return (arch == image_arch);
2910}
2911
2912/**
2913 * fit_image_check_type - check whether image node is of a given type
2914 * @fit: pointer to the FIT format image header
2915 * @noffset: component image node offset
2916 * @type: requested image type
2917 *
2918 * fit_image_check_type() reads image type property and compares its numeric
2919 * id with the requested type. Comparison result is returned to the caller.
2920 *
2921 * returns:
2922 * 1 if image is of given type
2923 * 0 otherwise (or on error)
2924 */
712fbcf3 2925int fit_image_check_type(const void *fit, int noffset, uint8_t type)
5dfb5213
MB
2926{
2927 uint8_t image_type;
2928
712fbcf3 2929 if (fit_image_get_type(fit, noffset, &image_type))
5dfb5213
MB
2930 return 0;
2931 return (type == image_type);
2932}
2933
2934/**
2935 * fit_image_check_comp - check whether image node uses given compression
2936 * @fit: pointer to the FIT format image header
2937 * @noffset: component image node offset
2938 * @comp: requested image compression type
2939 *
2940 * fit_image_check_comp() reads image compression property and compares its
2941 * numeric id with the requested compression type. Comparison result is
2942 * returned to the caller.
2943 *
2944 * returns:
2945 * 1 if image uses requested compression
2946 * 0 otherwise (or on error)
2947 */
712fbcf3 2948int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
5dfb5213
MB
2949{
2950 uint8_t image_comp;
2951
712fbcf3 2952 if (fit_image_get_comp(fit, noffset, &image_comp))
5dfb5213
MB
2953 return 0;
2954 return (comp == image_comp);
2955}
2956
2957/**
2958 * fit_check_format - sanity check FIT image format
2959 * @fit: pointer to the FIT format image header
2960 *
2961 * fit_check_format() runs a basic sanity FIT image verification.
2962 * Routine checks for mandatory properties, nodes, etc.
2963 *
2964 * returns:
2965 * 1, on success
2966 * 0, on failure
2967 */
712fbcf3 2968int fit_check_format(const void *fit)
5dfb5213
MB
2969{
2970 /* mandatory / node 'description' property */
712fbcf3
SW
2971 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2972 debug("Wrong FIT format: no description\n");
5dfb5213
MB
2973 return 0;
2974 }
2975
2976#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2977 /* mandatory / node 'timestamp' property */
712fbcf3
SW
2978 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2979 debug("Wrong FIT format: no timestamp\n");
5dfb5213
MB
2980 return 0;
2981 }
2982#endif
2983
2984 /* mandatory subimages parent '/images' node */
712fbcf3
SW
2985 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
2986 debug("Wrong FIT format: no images parent node\n");
5dfb5213
MB
2987 return 0;
2988 }
2989
2990 return 1;
2991}
2992
2993/**
2994 * fit_conf_get_node - get node offset for configuration of a given unit name
2995 * @fit: pointer to the FIT format image header
2996 * @conf_uname: configuration node unit name
2997 *
2998 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2999 * parant node) of a provided unit name. If configuration is found its node offset
3000 * is returned to the caller.
3001 *
3002 * When NULL is provided in second argument fit_conf_get_node() will search
3003 * for a default configuration node instead. Default configuration node unit name
3004 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
3005 *
3006 * returns:
3007 * configuration node offset when found (>=0)
3008 * negative number on failure (FDT_ERR_* code)
3009 */
712fbcf3 3010int fit_conf_get_node(const void *fit, const char *conf_uname)
5dfb5213
MB
3011{
3012 int noffset, confs_noffset;
3013 int len;
3014
712fbcf3 3015 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
5dfb5213 3016 if (confs_noffset < 0) {
712fbcf3
SW
3017 debug("Can't find configurations parent node '%s' (%s)\n",
3018 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
5dfb5213
MB
3019 return confs_noffset;
3020 }
3021
3022 if (conf_uname == NULL) {
3023 /* get configuration unit name from the default property */
712fbcf3
SW
3024 debug("No configuration specified, trying default...\n");
3025 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3026 FIT_DEFAULT_PROP, &len);
5dfb5213 3027 if (conf_uname == NULL) {
712fbcf3
SW
3028 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3029 len);
5dfb5213
MB
3030 return len;
3031 }
712fbcf3 3032 debug("Found default configuration: '%s'\n", conf_uname);
5dfb5213
MB
3033 }
3034
712fbcf3 3035 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
5dfb5213 3036 if (noffset < 0) {
712fbcf3
SW
3037 debug("Can't get node offset for configuration unit name: "
3038 "'%s' (%s)\n",
3039 conf_uname, fdt_strerror(noffset));
5dfb5213
MB
3040 }
3041
3042 return noffset;
3043}
3044
712fbcf3 3045static int __fit_conf_get_prop_node(const void *fit, int noffset,
5dfb5213
MB
3046 const char *prop_name)
3047{
3048 char *uname;
3049 int len;
3050
3051 /* get kernel image unit name from configuration kernel property */
712fbcf3 3052 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
5dfb5213
MB
3053 if (uname == NULL)
3054 return len;
3055
712fbcf3 3056 return fit_image_get_node(fit, uname);
5dfb5213
MB
3057}
3058
3059/**
3060 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3061 * a given configuration
3062 * @fit: pointer to the FIT format image header
3063 * @noffset: configuration node offset
3064 *
3065 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3066 * configuration FIT_KERNEL_PROP property and translates it to the node
3067 * offset.
3068 *
3069 * returns:
3070 * image node offset when found (>=0)
3071 * negative number on failure (FDT_ERR_* code)
3072 */
712fbcf3 3073int fit_conf_get_kernel_node(const void *fit, int noffset)
5dfb5213 3074{
712fbcf3 3075 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
5dfb5213
MB
3076}
3077
3078/**
3079 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3080 * a given configuration
3081 * @fit: pointer to the FIT format image header
3082 * @noffset: configuration node offset
3083 *
3084 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3085 * configuration FIT_KERNEL_PROP property and translates it to the node
3086 * offset.
3087 *
3088 * returns:
3089 * image node offset when found (>=0)
3090 * negative number on failure (FDT_ERR_* code)
3091 */
712fbcf3 3092int fit_conf_get_ramdisk_node(const void *fit, int noffset)
5dfb5213 3093{
712fbcf3 3094 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
5dfb5213
MB
3095}
3096
3097/**
3098 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3099 * a given configuration
3100 * @fit: pointer to the FIT format image header
3101 * @noffset: configuration node offset
3102 *
3103 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3104 * configuration FIT_KERNEL_PROP property and translates it to the node
3105 * offset.
3106 *
3107 * returns:
3108 * image node offset when found (>=0)
3109 * negative number on failure (FDT_ERR_* code)
3110 */
712fbcf3 3111int fit_conf_get_fdt_node(const void *fit, int noffset)
5dfb5213 3112{
712fbcf3 3113 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
5dfb5213 3114}
d5934ad7 3115
5dfb5213
MB
3116/**
3117 * fit_conf_print - prints out the FIT configuration details
3118 * @fit: pointer to the FIT format image header
f773bea8 3119 * @noffset: offset of the configuration node
5dfb5213
MB
3120 * @p: pointer to prefix string
3121 *
3122 * fit_conf_print() lists all mandatory properies for the processed
3123 * configuration node.
3124 *
3125 * returns:
3126 * no returned results
3127 */
712fbcf3 3128void fit_conf_print(const void *fit, int noffset, const char *p)
5dfb5213
MB
3129{
3130 char *desc;
3131 char *uname;
3132 int ret;
3133
3134 /* Mandatory properties */
712fbcf3
SW
3135 ret = fit_get_desc(fit, noffset, &desc);
3136 printf("%s Description: ", p);
5dfb5213 3137 if (ret)
712fbcf3 3138 printf("unavailable\n");
5dfb5213 3139 else
712fbcf3 3140 printf("%s\n", desc);
5dfb5213 3141
712fbcf3
SW
3142 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3143 printf("%s Kernel: ", p);
5dfb5213 3144 if (uname == NULL)
712fbcf3 3145 printf("unavailable\n");
5dfb5213 3146 else
712fbcf3 3147 printf("%s\n", uname);
5dfb5213
MB
3148
3149 /* Optional properties */
712fbcf3 3150 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
5dfb5213 3151 if (uname)
712fbcf3 3152 printf("%s Init Ramdisk: %s\n", p, uname);
5dfb5213 3153
712fbcf3 3154 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
5dfb5213 3155 if (uname)
712fbcf3 3156 printf("%s FDT: %s\n", p, uname);
5dfb5213 3157}
c8779648
MB
3158
3159/**
3160 * fit_check_ramdisk - verify FIT format ramdisk subimage
3161 * @fit_hdr: pointer to the FIT ramdisk header
3162 * @rd_noffset: ramdisk subimage node offset within FIT image
3163 * @arch: requested ramdisk image architecture type
3164 * @verify: data CRC verification flag
3165 *
3166 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3167 * specified FIT image.
3168 *
3169 * returns:
3170 * 1, on success
3171 * 0, on failure
3172 */
3173#ifndef USE_HOSTCC
712fbcf3
SW
3174static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3175 int verify)
c8779648 3176{
712fbcf3 3177 fit_image_print(fit, rd_noffset, " ");
c8779648
MB
3178
3179 if (verify) {
712fbcf3
SW
3180 puts(" Verifying Hash Integrity ... ");
3181 if (!fit_image_check_hashes(fit, rd_noffset)) {
3182 puts("Bad Data Hash\n");
770605e4 3183 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
c8779648
MB
3184 return 0;
3185 }
712fbcf3 3186 puts("OK\n");
c8779648
MB
3187 }
3188
770605e4 3189 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
712fbcf3
SW
3190 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3191 !fit_image_check_arch(fit, rd_noffset, arch) ||
3192 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3193 printf("No Linux %s Ramdisk Image\n",
c8779648 3194 genimg_get_arch_name(arch));
770605e4 3195 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
c8779648
MB
3196 return 0;
3197 }
3198
770605e4 3199 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
c8779648
MB
3200 return 1;
3201}
3202#endif /* USE_HOSTCC */
f50433d6 3203#endif /* CONFIG_FIT */