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