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