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