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