]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/image.c
[new uImage] Add new uImage format support for ramdisk handling
[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
MB
25
26#define DEBUG
27
b97a2a0a 28#ifndef USE_HOSTCC
5ad03eb3
MB
29#include <common.h>
30#include <watchdog.h>
31
32#ifdef CONFIG_SHOW_BOOT_PROGRESS
33#include <status_led.h>
34#endif
35
36#ifdef CONFIG_HAS_DATAFLASH
37#include <dataflash.h>
38#endif
39
ceaed2b1
MB
40#ifdef CONFIG_LOGBUFFER
41#include <logbuff.h>
42#endif
43
2242f536
MB
44#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
45#include <rtc.h>
46#endif
47
5dfb5213
MB
48#include <image.h>
49
c8779648 50#if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
fff888a1
MB
51#include <fdt.h>
52#include <libfdt.h>
53#include <fdt_support.h>
c8779648
MB
54#endif
55
56#if defined(CONFIG_FIT)
5dfb5213 57#include <sha1.h>
c8779648
MB
58
59static int fit_check_ramdisk (const void *fit, int os_noffset,
60 uint8_t arch, int verify);
fff888a1
MB
61#endif
62
b6b0fe64
MB
63#ifdef CONFIG_CMD_BDI
64extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
65#endif
66
67DECLARE_GLOBAL_DATA_PTR;
8a5ea3e6
MB
68
69static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
70 int argc, char *argv[],
71 ulong rd_addr, uint8_t arch, int verify);
b97a2a0a 72#else
5ad03eb3 73#include "mkimage.h"
5dfb5213 74#include <time.h>
b97a2a0a 75#include <image.h>
5dfb5213 76#endif /* !USE_HOSTCC*/
b97a2a0a 77
570abb0a
MB
78typedef struct table_entry {
79 int id; /* as defined in image.h */
80 char *sname; /* short (input) name */
81 char *lname; /* long (output) name */
82} table_entry_t;
83
84static table_entry_t uimage_arch[] = {
85 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
86 { IH_ARCH_ALPHA, "alpha", "Alpha", },
87 { IH_ARCH_ARM, "arm", "ARM", },
88 { IH_ARCH_I386, "x86", "Intel x86", },
89 { IH_ARCH_IA64, "ia64", "IA64", },
90 { IH_ARCH_M68K, "m68k", "M68K", },
91 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
92 { IH_ARCH_MIPS, "mips", "MIPS", },
93 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
94 { IH_ARCH_NIOS, "nios", "NIOS", },
95 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
96 { IH_ARCH_PPC, "ppc", "PowerPC", },
97 { IH_ARCH_S390, "s390", "IBM S390", },
98 { IH_ARCH_SH, "sh", "SuperH", },
99 { IH_ARCH_SPARC, "sparc", "SPARC", },
100 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
101 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
102 { IH_ARCH_AVR32, "avr32", "AVR32", },
103 { -1, "", "", },
104};
105
106static table_entry_t uimage_os[] = {
107 { IH_OS_INVALID, NULL, "Invalid OS", },
108#if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
109 { IH_OS_ARTOS, "artos", "ARTOS", },
110#endif
111 { IH_OS_LINUX, "linux", "Linux", },
112#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
113 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
114#endif
115 { IH_OS_NETBSD, "netbsd", "NetBSD", },
116 { IH_OS_RTEMS, "rtems", "RTEMS", },
117 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
118#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
119 { IH_OS_QNX, "qnx", "QNX", },
120 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
121#endif
122#ifdef USE_HOSTCC
123 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
124 { IH_OS_DELL, "dell", "Dell", },
125 { IH_OS_ESIX, "esix", "Esix", },
126 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
127 { IH_OS_IRIX, "irix", "Irix", },
128 { IH_OS_NCR, "ncr", "NCR", },
129 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
130 { IH_OS_PSOS, "psos", "pSOS", },
131 { IH_OS_SCO, "sco", "SCO", },
132 { IH_OS_SOLARIS, "solaris", "Solaris", },
133 { IH_OS_SVR4, "svr4", "SVR4", },
134#endif
135 { -1, "", "", },
136};
137
138static table_entry_t uimage_type[] = {
139 { IH_TYPE_INVALID, NULL, "Invalid Image", },
140 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
141 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
142 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
143 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
144 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
145 { IH_TYPE_SCRIPT, "script", "Script", },
146 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
147 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
148 { -1, "", "", },
149};
150
151static table_entry_t uimage_comp[] = {
152 { IH_COMP_NONE, "none", "uncompressed", },
153 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
154 { IH_COMP_GZIP, "gzip", "gzip compressed", },
155 { -1, "", "", },
156};
157
b97a2a0a 158unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
570abb0a
MB
159static void genimg_print_size (uint32_t size);
160#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
161static void genimg_print_time (time_t timestamp);
162#endif
b97a2a0a 163
9a4daad0
MB
164/*****************************************************************************/
165/* Legacy format routines */
166/*****************************************************************************/
b97a2a0a
MB
167int image_check_hcrc (image_header_t *hdr)
168{
169 ulong hcrc;
170 ulong len = image_get_header_size ();
171 image_header_t header;
172
173 /* Copy header so we can blank CRC field for re-calculation */
174 memmove (&header, (char *)hdr, image_get_header_size ());
175 image_set_hcrc (&header, 0);
176
177 hcrc = crc32 (0, (unsigned char *)&header, len);
178
179 return (hcrc == image_get_hcrc (hdr));
180}
181
182int image_check_dcrc (image_header_t *hdr)
183{
184 ulong data = image_get_data (hdr);
185 ulong len = image_get_data_size (hdr);
186 ulong dcrc = crc32 (0, (unsigned char *)data, len);
187
188 return (dcrc == image_get_dcrc (hdr));
189}
190
af13cdbc 191#ifndef USE_HOSTCC
b97a2a0a
MB
192int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
193{
194 ulong dcrc = 0;
195 ulong len = image_get_data_size (hdr);
196 ulong data = image_get_data (hdr);
197
198#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
199 ulong cdata = data;
200 ulong edata = cdata + len;
201
202 while (cdata < edata) {
203 ulong chunk = edata - cdata;
204
205 if (chunk > chunksz)
206 chunk = chunksz;
207 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
208 cdata += chunk;
209
210 WATCHDOG_RESET ();
211 }
212#else
213 dcrc = crc32 (0, (unsigned char *)data, len);
214#endif
215
216 return (dcrc == image_get_dcrc (hdr));
217}
570abb0a 218#endif /* !USE_HOSTCC */
b97a2a0a 219
f13e7b2e
MB
220/**
221 * image_multi_count - get component (sub-image) count
222 * @hdr: pointer to the header of the multi component image
223 *
224 * image_multi_count() returns number of components in a multi
225 * 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 * number of components
232 */
233ulong image_multi_count (image_header_t *hdr)
234{
235 ulong i, count = 0;
df6f1b89 236 uint32_t *size;
f13e7b2e
MB
237
238 /* get start of the image payload, which in case of multi
239 * component images that points to a table of component sizes */
df6f1b89 240 size = (uint32_t *)image_get_data (hdr);
f13e7b2e
MB
241
242 /* count non empty slots */
243 for (i = 0; size[i]; ++i)
244 count++;
245
246 return count;
247}
248
249/**
250 * image_multi_getimg - get component data address and size
251 * @hdr: pointer to the header of the multi component image
252 * @idx: index of the requested component
253 * @data: pointer to a ulong variable, will hold component data address
254 * @len: pointer to a ulong variable, will hold component size
255 *
256 * image_multi_getimg() returns size and data address for the requested
257 * component in a multi component image.
258 *
259 * Note: no checking of the image type is done, caller must pass
260 * a valid multi component image.
261 *
262 * returns:
263 * data address and size of the component, if idx is valid
264 * 0 in data and len, if idx is out of range
265 */
266void image_multi_getimg (image_header_t *hdr, ulong idx,
267 ulong *data, ulong *len)
268{
269 int i;
df6f1b89 270 uint32_t *size;
f13e7b2e
MB
271 ulong offset, tail, count, img_data;
272
273 /* get number of component */
274 count = image_multi_count (hdr);
275
276 /* get start of the image payload, which in case of multi
277 * component images that points to a table of component sizes */
df6f1b89 278 size = (uint32_t *)image_get_data (hdr);
f13e7b2e
MB
279
280 /* get address of the proper component data start, which means
281 * skipping sizes table (add 1 for last, null entry) */
df6f1b89 282 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
f13e7b2e
MB
283
284 if (idx < count) {
df6f1b89 285 *len = uimage_to_cpu (size[idx]);
f13e7b2e
MB
286 offset = 0;
287 tail = 0;
288
289 /* go over all indices preceding requested component idx */
290 for (i = 0; i < idx; i++) {
291 /* add up i-th component size */
df6f1b89 292 offset += uimage_to_cpu (size[i]);
f13e7b2e
MB
293
294 /* add up alignment for i-th component */
df6f1b89 295 tail += (4 - uimage_to_cpu (size[i]) % 4);
f13e7b2e
MB
296 }
297
298 /* calculate idx-th component data address */
299 *data = img_data + offset + tail;
300 } else {
301 *len = 0;
302 *data = 0;
303 }
304}
42b73e8e 305
9a4daad0
MB
306static void image_print_type (image_header_t *hdr)
307{
308 const char *os, *arch, *type, *comp;
309
310 os = genimg_get_os_name (image_get_os (hdr));
311 arch = genimg_get_arch_name (image_get_arch (hdr));
312 type = genimg_get_type_name (image_get_type (hdr));
313 comp = genimg_get_comp_name (image_get_comp (hdr));
314
570abb0a 315 printf ("%s %s %s (%s)\n", arch, os, type, comp);
9a4daad0
MB
316}
317
5dfb5213
MB
318/**
319 * __image_print_contents - prints out the contents of the legacy format image
320 * @hdr: pointer to the legacy format image header
321 * @p: pointer to prefix string
322 *
323 * __image_print_contents() formats a multi line legacy image contents description.
324 * The routine prints out all header fields followed by the size/offset data
325 * for MULTI/SCRIPT images.
326 *
327 * returns:
328 * no returned results
329 */
570abb0a 330static void __image_print_contents (image_header_t *hdr, const char *p)
9a4daad0 331{
570abb0a
MB
332 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
333#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
334 printf ("%sCreated: ", p);
335 genimg_print_time ((time_t)image_get_time (hdr));
9a4daad0 336#endif
570abb0a 337 printf ("%sImage Type: ", p);
9a4daad0 338 image_print_type (hdr);
570abb0a
MB
339 printf ("%sData Size: ", p);
340 genimg_print_size (image_get_data_size (hdr));
341 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
342 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
9a4daad0 343
570abb0a
MB
344 if (image_check_type (hdr, IH_TYPE_MULTI) ||
345 image_check_type (hdr, IH_TYPE_SCRIPT)) {
9a4daad0
MB
346 int i;
347 ulong data, len;
348 ulong count = image_multi_count (hdr);
349
570abb0a 350 printf ("%sContents:\n", p);
9a4daad0
MB
351 for (i = 0; i < count; i++) {
352 image_multi_getimg (hdr, i, &data, &len);
570abb0a
MB
353
354 printf ("%s Image %d: ", p, i);
355 genimg_print_size (len);
356
357 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
358 /*
359 * the user may need to know offsets
360 * if planning to do something with
361 * multiple files
362 */
363 printf ("%s Offset = 0x%08lx\n", p, data);
364 }
9a4daad0
MB
365 }
366 }
367}
368
570abb0a
MB
369inline void image_print_contents (image_header_t *hdr)
370{
371 __image_print_contents (hdr, " ");
372}
373
374inline void image_print_contents_noindent (image_header_t *hdr)
375{
376 __image_print_contents (hdr, "");
377}
378
379#ifndef USE_HOSTCC
9a4daad0
MB
380/**
381 * image_get_ramdisk - get and verify ramdisk image
382 * @cmdtp: command table pointer
383 * @flag: command flag
384 * @argc: command argument count
385 * @argv: command argument list
386 * @rd_addr: ramdisk image start address
387 * @arch: expected ramdisk architecture
388 * @verify: checksum verification flag
389 *
390 * image_get_ramdisk() returns a pointer to the verified ramdisk image
391 * header. Routine receives image start address and expected architecture
392 * flag. Verification done covers data and header integrity and os/type/arch
393 * fields checking.
394 *
395 * If dataflash support is enabled routine checks for dataflash addresses
396 * and handles required dataflash reads.
397 *
398 * returns:
399 * pointer to a ramdisk image header, if image was found and valid
400 * otherwise, return NULL
401 */
402static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
403 int argc, char *argv[],
404 ulong rd_addr, uint8_t arch, int verify)
405{
406 image_header_t *rd_hdr;
407
408 show_boot_progress (9);
409 rd_hdr = (image_header_t *)rd_addr;
410
411 if (!image_check_magic (rd_hdr)) {
412 puts ("Bad Magic Number\n");
413 show_boot_progress (-10);
414 return NULL;
415 }
416
417 if (!image_check_hcrc (rd_hdr)) {
418 puts ("Bad Header Checksum\n");
419 show_boot_progress (-11);
420 return NULL;
421 }
422
423 show_boot_progress (10);
424 image_print_contents (rd_hdr);
425
426 if (verify) {
427 puts(" Verifying Checksum ... ");
428 if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
429 puts ("Bad Data CRC\n");
430 show_boot_progress (-12);
431 return NULL;
432 }
433 puts("OK\n");
434 }
435
436 show_boot_progress (11);
437
438 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
439 !image_check_arch (rd_hdr, arch) ||
440 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
441 printf ("No Linux %s Ramdisk Image\n",
442 genimg_get_arch_name(arch));
443 show_boot_progress (-13);
444 return NULL;
445 }
446
447 return rd_hdr;
448}
570abb0a 449#endif /* !USE_HOSTCC */
9a4daad0
MB
450
451/*****************************************************************************/
452/* Shared dual-format routines */
453/*****************************************************************************/
570abb0a 454#ifndef USE_HOSTCC
9a4daad0
MB
455int getenv_verify (void)
456{
457 char *s = getenv ("verify");
458 return (s && (*s == 'n')) ? 0 : 1;
459}
460
461int getenv_autostart (void)
462{
463 char *s = getenv ("autostart");
464 return (s && (*s == 'n')) ? 0 : 1;
465}
466
467ulong getenv_bootm_low(void)
468{
469 char *s = getenv ("bootm_low");
470 if (s) {
471 ulong tmp = simple_strtoul (s, NULL, 16);
472 return tmp;
473 }
474
475#ifdef CFG_SDRAM_BASE
476 return CFG_SDRAM_BASE;
477#else
478 return 0;
479#endif
480}
481
482ulong getenv_bootm_size(void)
483{
484 char *s = getenv ("bootm_size");
485 if (s) {
486 ulong tmp = simple_strtoul (s, NULL, 16);
487 return tmp;
488 }
489
490 return gd->bd->bi_memsize;
491}
492
493void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
494{
495#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
496 while (len > 0) {
497 size_t tail = (len > chunksz) ? chunksz : len;
498 WATCHDOG_RESET ();
499 memmove (to, from, tail);
500 to += tail;
501 from += tail;
502 len -= tail;
503 }
504#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
505 memmove (to, from, len);
506#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
507}
570abb0a 508#endif /* !USE_HOSTCC */
9a4daad0 509
570abb0a 510static void genimg_print_size (uint32_t size)
42b73e8e 511{
570abb0a
MB
512#ifndef USE_HOSTCC
513 printf ("%d Bytes = ", size);
514 print_size (size, "\n");
515#else
516 printf ("%d Bytes = %.2f kB = %.2f MB\n",
517 size, (double)size / 1.024e3,
518 (double)size / 1.048576e6);
42b73e8e 519#endif
570abb0a
MB
520}
521
522#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
523static void genimg_print_time (time_t timestamp)
524{
525#ifndef USE_HOSTCC
526 struct rtc_time tm;
527
528 to_tm (timestamp, &tm);
529 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
530 tm.tm_year, tm.tm_mon, tm.tm_mday,
531 tm.tm_hour, tm.tm_min, tm.tm_sec);
532#else
533 printf ("%s", ctime(&timestamp));
42b73e8e 534#endif
570abb0a
MB
535}
536#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
42b73e8e 537
570abb0a
MB
538/**
539 * get_table_entry_name - translate entry id to long name
540 * @table: pointer to a translation table for entries of a specific type
541 * @msg: message to be returned when translation fails
542 * @id: entry id to be translated
543 *
544 * get_table_entry_name() will go over translation table trying to find
545 * entry that matches given id. If matching entry is found, its long
546 * name is returned to the caller.
547 *
548 * returns:
549 * long entry name if translation succeeds
550 * msg otherwise
551 */
552static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
553{
554 for (; table->id >= 0; ++table) {
555 if (table->id == id)
556 return (table->lname);
42b73e8e 557 }
570abb0a
MB
558 return (msg);
559}
42b73e8e 560
570abb0a
MB
561const char *genimg_get_os_name (uint8_t os)
562{
563 return (get_table_entry_name (uimage_os, "Unknown OS", os));
42b73e8e
MB
564}
565
570abb0a 566const char *genimg_get_arch_name (uint8_t arch)
42b73e8e 567{
570abb0a
MB
568 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
569}
42b73e8e 570
570abb0a
MB
571const char *genimg_get_type_name (uint8_t type)
572{
573 return (get_table_entry_name (uimage_type, "Unknown Image", type));
574}
42b73e8e 575
570abb0a
MB
576const char *genimg_get_comp_name (uint8_t comp)
577{
578 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
42b73e8e
MB
579}
580
570abb0a
MB
581/**
582 * get_table_entry_id - translate short entry name to id
583 * @table: pointer to a translation table for entries of a specific type
584 * @table_name: to be used in case of error
585 * @name: entry short name to be translated
586 *
587 * get_table_entry_id() will go over translation table trying to find
588 * entry that matches given short name. If matching entry is found,
589 * its id returned to the caller.
590 *
591 * returns:
592 * entry id if translation succeeds
593 * -1 otherwise
594 */
595static int get_table_entry_id (table_entry_t *table,
596 const char *table_name, const char *name)
42b73e8e 597{
570abb0a
MB
598 table_entry_t *t;
599#ifdef USE_HOSTCC
600 int first = 1;
42b73e8e 601
570abb0a
MB
602 for (t = table; t->id >= 0; ++t) {
603 if (t->sname && strcasecmp(t->sname, name) == 0)
604 return (t->id);
42b73e8e
MB
605 }
606
570abb0a
MB
607 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
608 for (t = table; t->id >= 0; ++t) {
609 if (t->sname == NULL)
610 continue;
611 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
612 first = 0;
613 }
614 fprintf (stderr, "\n");
615#else
616 for (t = table; t->id >= 0; ++t) {
617 if (t->sname && strcmp(t->sname, name) == 0)
618 return (t->id);
619 }
620 debug ("Invalid %s Type: %s\n", table_name, name);
621#endif /* USE_HOSTCC */
622 return (-1);
623}
624
625int genimg_get_os_id (const char *name)
626{
627 return (get_table_entry_id (uimage_os, "OS", name));
628}
629
630int genimg_get_arch_id (const char *name)
631{
632 return (get_table_entry_id (uimage_arch, "CPU", name));
42b73e8e 633}
5ad03eb3 634
570abb0a
MB
635int genimg_get_type_id (const char *name)
636{
637 return (get_table_entry_id (uimage_type, "Image", name));
638}
639
640int genimg_get_comp_id (const char *name)
641{
642 return (get_table_entry_id (uimage_comp, "Compression", name));
643}
644
645#ifndef USE_HOSTCC
fff888a1 646/**
9a4daad0 647 * genimg_get_format - get image format type
fff888a1
MB
648 * @img_addr: image start address
649 *
9a4daad0 650 * genimg_get_format() checks whether provided address points to a valid
fff888a1
MB
651 * legacy or FIT image.
652 *
4efbe9db
MB
653 * New uImage format and FDT blob are based on a libfdt. FDT blob
654 * may be passed directly or embedded in a FIT image. In both situations
9a4daad0 655 * genimg_get_format() must be able to dectect libfdt header.
4efbe9db 656 *
fff888a1
MB
657 * returns:
658 * image format type or IMAGE_FORMAT_INVALID if no image is present
659 */
9a4daad0 660int genimg_get_format (void *img_addr)
fff888a1
MB
661{
662 ulong format = IMAGE_FORMAT_INVALID;
663 image_header_t *hdr;
4efbe9db 664#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
fff888a1
MB
665 char *fit_hdr;
666#endif
667
668 hdr = (image_header_t *)img_addr;
669 if (image_check_magic(hdr))
670 format = IMAGE_FORMAT_LEGACY;
4efbe9db 671#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
fff888a1
MB
672 else {
673 fit_hdr = (char *)img_addr;
674 if (fdt_check_header (fit_hdr) == 0)
675 format = IMAGE_FORMAT_FIT;
676 }
677#endif
678
679 return format;
680}
681
682/**
9a4daad0 683 * genimg_get_image - get image from special storage (if necessary)
fff888a1
MB
684 * @img_addr: image start address
685 *
9a4daad0 686 * genimg_get_image() checks if provided image start adddress is located
fff888a1
MB
687 * in a dataflash storage. If so, image is moved to a system RAM memory.
688 *
689 * returns:
690 * image start address after possible relocation from special storage
691 */
9a4daad0 692ulong genimg_get_image (ulong img_addr)
fff888a1 693{
6f0f9dfc 694 ulong ram_addr = img_addr;
fff888a1
MB
695
696#ifdef CONFIG_HAS_DATAFLASH
6f0f9dfc
MB
697 ulong h_size, d_size;
698
fff888a1 699 if (addr_dataflash (img_addr)){
6f0f9dfc 700 /* ger RAM address */
fff888a1 701 ram_addr = CFG_LOAD_ADDR;
6f0f9dfc
MB
702
703 /* get header size */
704 h_size = image_get_header_size ();
705#if defined(CONFIG_FIT)
706 if (sizeof(struct fdt_header) > h_size)
707 h_size = sizeof(struct fdt_header);
708#endif
709
710 /* read in header */
fff888a1
MB
711 debug (" Reading image header from dataflash address "
712 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
fff888a1 713
6f0f9dfc 714 read_dataflash (img_addr, h_size, (char *)ram_addr);
fff888a1 715
6f0f9dfc 716 /* get data size */
9a4daad0 717 switch (genimg_get_format ((void *)ram_addr)) {
6f0f9dfc
MB
718 case IMAGE_FORMAT_LEGACY:
719 d_size = image_get_data_size ((image_header_t *)ram_addr);
720 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
721 ram_addr, d_size);
722 break;
fff888a1 723#if defined(CONFIG_FIT)
6f0f9dfc 724 case IMAGE_FORMAT_FIT:
5dfb5213 725 d_size = fit_get_size ((const void *)ram_addr) - h_size;
6f0f9dfc
MB
726 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
727 ram_addr, d_size);
728 break;
fff888a1 729#endif
6f0f9dfc
MB
730 default:
731 printf (" No valid image found at 0x%08lx\n", img_addr);
732 return ram_addr;
733 }
fff888a1 734
6f0f9dfc 735 /* read in image data */
fff888a1
MB
736 debug (" Reading image remaining data from dataflash address "
737 "%08lx to RAM address %08lx\n", img_addr + h_size,
738 ram_addr + h_size);
739
740 read_dataflash (img_addr + h_size, d_size,
741 (char *)(ram_addr + h_size));
6f0f9dfc 742
fff888a1 743 }
6f0f9dfc 744#endif /* CONFIG_HAS_DATAFLASH */
fff888a1
MB
745
746 return ram_addr;
747}
748
5ad03eb3 749/**
9a4daad0 750 * boot_get_ramdisk - main ramdisk handling routine
5ad03eb3
MB
751 * @cmdtp: command table pointer
752 * @flag: command flag
753 * @argc: command argument count
754 * @argv: command argument list
8a5ea3e6 755 * @images: pointer to the bootm images structure
5ad03eb3
MB
756 * @arch: expected ramdisk architecture
757 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
758 * @rd_end: pointer to a ulong variable, will hold ramdisk end
759 *
9a4daad0 760 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
5ad03eb3
MB
761 * Curently supported are the following ramdisk sources:
762 * - multicomponent kernel/ramdisk image,
763 * - commandline provided address of decicated ramdisk image.
764 *
765 * returns:
766 * rd_start and rd_end are set to ramdisk start/end addresses if
767 * ramdisk image is found and valid
768 * rd_start and rd_end are set to 0 if no ramdisk exists
274cea2b 769 * return 1 if ramdisk image is found but corrupted
5ad03eb3 770 */
9a4daad0 771int boot_get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
8a5ea3e6 772 bootm_headers_t *images, uint8_t arch,
5ad03eb3
MB
773 ulong *rd_start, ulong *rd_end)
774{
d5934ad7 775 ulong rd_addr, rd_load;
5ad03eb3
MB
776 ulong rd_data, rd_len;
777 image_header_t *rd_hdr;
d5934ad7
MB
778#if defined(CONFIG_FIT)
779 void *fit_hdr;
780 const char *fit_uname_config = NULL;
781 const char *fit_uname_ramdisk = NULL;
782 ulong default_addr;
c8779648
MB
783 int rd_noffset;
784 int conf_noffset;
785 const void *data;
786 size_t size;
d5934ad7 787#endif
5ad03eb3 788
c8779648
MB
789 *rd_start = 0;
790 *rd_end = 0;
791
d5934ad7
MB
792 /*
793 * Look for a '-' which indicates to ignore the
794 * ramdisk argument
795 */
796 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
797 debug ("## Skipping init Ramdisk\n");
798 rd_len = rd_data = 0;
799 } else if (argc >= 3) {
800#if defined(CONFIG_FIT)
5ad03eb3 801 /*
d5934ad7
MB
802 * If the init ramdisk comes from the FIT image and the FIT image
803 * address is omitted in the command line argument, try to use
804 * os FIT image address or default load address.
5ad03eb3 805 */
d5934ad7
MB
806 if (images->fit_uname_os)
807 default_addr = (ulong)images->fit_hdr_os;
808 else
809 default_addr = load_addr;
810
811 if (fit_parse_conf (argv[2], default_addr,
812 &rd_addr, &fit_uname_config)) {
813 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
814 fit_uname_config, rd_addr);
815 } else if (fit_parse_subimage (argv[2], default_addr,
816 &rd_addr, &fit_uname_ramdisk)) {
817 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
818 fit_uname_ramdisk, rd_addr);
819 } else
820#endif
821 {
822 rd_addr = simple_strtoul(argv[2], NULL, 16);
823 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
5ad03eb3 824 rd_addr);
d5934ad7
MB
825 }
826
827 /* copy from dataflash if needed */
9a4daad0 828 rd_addr = genimg_get_image (rd_addr);
d5934ad7
MB
829
830 /*
831 * Check if there is an initrd image at the
832 * address provided in the second bootm argument
833 * check image type, for FIT images get FIT node.
834 */
9a4daad0 835 switch (genimg_get_format ((void *)rd_addr)) {
d5934ad7 836 case IMAGE_FORMAT_LEGACY:
c8779648
MB
837 printf ("## Loading init Ramdisk from Legacy "
838 "Image at %08lx ...\n", rd_addr);
5ad03eb3
MB
839
840 rd_hdr = image_get_ramdisk (cmdtp, flag, argc, argv,
8a5ea3e6 841 rd_addr, arch, images->verify);
5ad03eb3 842
c8779648 843 if (rd_hdr == NULL)
274cea2b 844 return 1;
274cea2b 845
5ad03eb3
MB
846 rd_data = image_get_data (rd_hdr);
847 rd_len = image_get_data_size (rd_hdr);
d5934ad7
MB
848 rd_load = image_get_load (rd_hdr);
849 break;
850#if defined(CONFIG_FIT)
851 case IMAGE_FORMAT_FIT:
852 fit_hdr = (void *)rd_addr;
c8779648
MB
853 printf ("## Loading init Ramdisk from FIT "
854 "Image at %08lx ...\n", rd_addr);
855
856 if (!fit_check_format (fit_hdr)) {
857 puts ("Bad FIT ramdisk image format!\n");
858 return 0;
859 }
860
861 if (!fit_uname_ramdisk) {
862 /*
863 * no ramdisk image node unit name, try to get config
864 * node first. If config unit node name is NULL
865 * fit_conf_get_node() will try to find default config node
866 */
867 conf_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
868 if (conf_noffset < 0)
869 return 0;
870
871 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, conf_noffset);
872 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
873 } else {
874 /* get ramdisk component image node offset */
875 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
876 }
877 if (rd_noffset < 0)
878 return 0;
879
880 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
881
882 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
883 return 0;
884
885 /* get ramdisk image data address and length */
886 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
887 puts ("Could not find ramdisk subimage data!\n");
888 return 0;
889 }
890
891 rd_data = (ulong)data;
892 rd_len = size;
893
894 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
895 puts ("Can't get ramdisk subimage load address!\n");
896 return 0;
897 }
898
899 images->fit_hdr_rd = fit_hdr;
900 images->fit_uname_rd = fit_uname_ramdisk;
901 break;
d5934ad7
MB
902#endif
903 default:
904 printf ("Wrong Image Format for %s command\n",
905 cmdtp->name);
c8779648 906 rd_data = rd_len = rd_load = 0;
d5934ad7 907 }
5ad03eb3
MB
908
909#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
d5934ad7
MB
910 /*
911 * We need to copy the ramdisk to SRAM to let Linux boot
912 */
913 if (rd_data) {
914 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
915 rd_data = rd_load;
5ad03eb3 916 }
d5934ad7 917#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
5ad03eb3 918
d5934ad7
MB
919 } else if (images->legacy_hdr_valid &&
920 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
5ad03eb3 921 /*
d5934ad7
MB
922 * Now check if we have a legacy mult-component image,
923 * get second entry data start address and len.
5ad03eb3
MB
924 */
925 show_boot_progress (13);
926 printf ("## Loading init Ramdisk from multi component "
c8779648 927 "Legacy Image at %08lx ...\n",
d5934ad7
MB
928 (ulong)images->legacy_hdr_os);
929
930 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
5ad03eb3
MB
931 } else {
932 /*
933 * no initrd image
934 */
935 show_boot_progress (14);
936 rd_len = rd_data = 0;
937 }
938
939 if (!rd_data) {
940 debug ("## No init Ramdisk\n");
5ad03eb3
MB
941 } else {
942 *rd_start = rd_data;
943 *rd_end = rd_data + rd_len;
944 }
945 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
946 *rd_start, *rd_end);
274cea2b
KG
947
948 return 0;
5ad03eb3 949}
ceaed2b1
MB
950
951#if defined(CONFIG_PPC) || defined(CONFIG_M68K)
952/**
9a4daad0 953 * boot_ramdisk_high - relocate init ramdisk
e822d7fc 954 * @lmb: pointer to lmb handle, will be used for memory mgmt
ceaed2b1
MB
955 * @rd_data: ramdisk data start address
956 * @rd_len: ramdisk data length
ceaed2b1
MB
957 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
958 * start address (after possible relocation)
959 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
960 * end address (after possible relocation)
961 *
9a4daad0 962 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
ceaed2b1
MB
963 * variable and if requested ramdisk data is moved to a specified location.
964 *
9a4daad0
MB
965 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
966 * start/end addresses if ramdisk image start and len were provided,
967 * otherwise set initrd_start and initrd_end set to zeros.
968 *
ceaed2b1 969 * returns:
9a4daad0
MB
970 * 0 - success
971 * -1 - failure
ceaed2b1 972 */
9a4daad0 973int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
e822d7fc 974 ulong *initrd_start, ulong *initrd_end)
ceaed2b1
MB
975{
976 char *s;
977 ulong initrd_high;
978 int initrd_copy_to_ram = 1;
979
980 if ((s = getenv ("initrd_high")) != NULL) {
981 /* a value of "no" or a similar string will act like 0,
982 * turning the "load high" feature off. This is intentional.
983 */
984 initrd_high = simple_strtoul (s, NULL, 16);
985 if (initrd_high == ~0)
986 initrd_copy_to_ram = 0;
987 } else {
988 /* not set, no restrictions to load high */
989 initrd_high = ~0;
990 }
991
ceaed2b1
MB
992 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
993 initrd_high, initrd_copy_to_ram);
994
995 if (rd_data) {
996 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
997 debug (" in-place initrd\n");
998 *initrd_start = rd_data;
999 *initrd_end = rd_data + rd_len;
e822d7fc 1000 lmb_reserve(lmb, rd_data, rd_len);
ceaed2b1 1001 } else {
e822d7fc 1002 if (initrd_high)
9a4daad0 1003 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
e822d7fc 1004 else
9a4daad0 1005 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
e822d7fc
KG
1006
1007 if (*initrd_start == 0) {
9a4daad0 1008 puts ("ramdisk - allocation error\n");
e822d7fc 1009 goto error;
ceaed2b1 1010 }
ceaed2b1
MB
1011 show_boot_progress (12);
1012
1013 *initrd_end = *initrd_start + rd_len;
1014 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1015 *initrd_start, *initrd_end);
1016
9a4daad0 1017 memmove_wd ((void *)*initrd_start,
ceaed2b1
MB
1018 (void *)rd_data, rd_len, CHUNKSZ);
1019
1020 puts ("OK\n");
1021 }
1022 } else {
1023 *initrd_start = 0;
1024 *initrd_end = 0;
1025 }
1026 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1027 *initrd_start, *initrd_end);
9a4daad0 1028
e822d7fc 1029 return 0;
b6b0fe64 1030
e822d7fc
KG
1031error:
1032 return -1;
b6b0fe64
MB
1033}
1034
1035/**
9a4daad0 1036 * boot_get_cmdline - allocate and initialize kernel cmdline
e822d7fc 1037 * @lmb: pointer to lmb handle, will be used for memory mgmt
b6b0fe64
MB
1038 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1039 * @cmd_end: pointer to a ulong variable, will hold cmdline end
e822d7fc
KG
1040 * @bootmap_base: ulong variable, holds offset in physical memory to
1041 * base of bootmap
b6b0fe64 1042 *
9a4daad0 1043 * boot_get_cmdline() allocates space for kernel command line below
e822d7fc 1044 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
b6b0fe64
MB
1045 * variable is present its contents is copied to allocated kernel
1046 * command line.
1047 *
1048 * returns:
e822d7fc
KG
1049 * 0 - success
1050 * -1 - failure
b6b0fe64 1051 */
9a4daad0 1052int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
e822d7fc 1053 ulong bootmap_base)
b6b0fe64
MB
1054{
1055 char *cmdline;
1056 char *s;
1057
e822d7fc
KG
1058 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1059 CFG_BOOTMAPSZ + bootmap_base);
1060
1061 if (cmdline == NULL)
1062 return -1;
b6b0fe64
MB
1063
1064 if ((s = getenv("bootargs")) == NULL)
1065 s = "";
1066
1067 strcpy(cmdline, s);
1068
1069 *cmd_start = (ulong) & cmdline[0];
1070 *cmd_end = *cmd_start + strlen(cmdline);
1071
1072 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1073
e822d7fc 1074 return 0;
b6b0fe64
MB
1075}
1076
1077/**
9a4daad0 1078 * boot_get_kbd - allocate and initialize kernel copy of board info
e822d7fc 1079 * @lmb: pointer to lmb handle, will be used for memory mgmt
b6b0fe64 1080 * @kbd: double pointer to board info data
e822d7fc
KG
1081 * @bootmap_base: ulong variable, holds offset in physical memory to
1082 * base of bootmap
b6b0fe64 1083 *
9a4daad0 1084 * boot_get_kbd() allocates space for kernel copy of board info data below
e822d7fc
KG
1085 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1086 * the current u-boot board info data.
b6b0fe64
MB
1087 *
1088 * returns:
e822d7fc
KG
1089 * 0 - success
1090 * -1 - failure
b6b0fe64 1091 */
9a4daad0 1092int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
b6b0fe64 1093{
e822d7fc
KG
1094 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1095 CFG_BOOTMAPSZ + bootmap_base);
1096 if (*kbd == NULL)
1097 return -1;
1098
b6b0fe64
MB
1099 **kbd = *(gd->bd);
1100
1101 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1102
1103#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1104 do_bdinfo(NULL, 0, 0, NULL);
1105#endif
1106
e822d7fc 1107 return 0;
ceaed2b1
MB
1108}
1109#endif /* CONFIG_PPC || CONFIG_M68K */
5dfb5213 1110#endif /* !USE_HOSTCC */
5ad03eb3 1111
f50433d6
MB
1112#if defined(CONFIG_FIT)
1113/*****************************************************************************/
1114/* New uImage format routines */
1115/*****************************************************************************/
5dfb5213 1116#ifndef USE_HOSTCC
f50433d6
MB
1117static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1118 ulong *addr, const char **name)
1119{
1120 const char *sep;
1121
1122 *addr = addr_curr;
1123 *name = NULL;
1124
1125 sep = strchr (spec, sepc);
1126 if (sep) {
1127 if (sep - spec > 0)
1128 *addr = simple_strtoul (spec, NULL, 16);
1129
1130 *name = sep + 1;
1131 return 1;
1132 }
1133
1134 return 0;
1135}
1136
1137/**
1138 * fit_parse_conf - parse FIT configuration spec
1139 * @spec: input string, containing configuration spec
1140 * @add_curr: current image address (to be used as a possible default)
1141 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1142 * configuration
1143 * @conf_name double pointer to a char, will hold pointer to a configuration
1144 * unit name
1145 *
1146 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1147 * where <addr> is a FIT image address that contains configuration
1148 * with a <conf> unit name.
1149 *
1150 * Address part is optional, and if omitted default add_curr will
1151 * be used instead.
1152 *
1153 * returns:
1154 * 1 if spec is a valid configuration string,
1155 * addr and conf_name are set accordingly
1156 * 0 otherwise
1157 */
1158inline int fit_parse_conf (const char *spec, ulong addr_curr,
1159 ulong *addr, const char **conf_name)
1160{
1161 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1162}
1163
1164/**
1165 * fit_parse_subimage - parse FIT subimage spec
1166 * @spec: input string, containing subimage spec
1167 * @add_curr: current image address (to be used as a possible default)
1168 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1169 * subimage
1170 * @image_name: double pointer to a char, will hold pointer to a subimage name
1171 *
1172 * fit_parse_subimage() expects subimage spec in the for of
1173 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1174 * subimage with a <subimg> unit name.
1175 *
1176 * Address part is optional, and if omitted default add_curr will
1177 * be used instead.
1178 *
1179 * returns:
1180 * 1 if spec is a valid subimage string,
1181 * addr and image_name are set accordingly
1182 * 0 otherwise
1183 */
1184inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1185 ulong *addr, const char **image_name)
1186{
1187 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1188}
5dfb5213
MB
1189#endif /* !USE_HOSTCC */
1190
1191static void fit_get_debug (const void *fit, int noffset,
1192 char *prop_name, int err)
1193{
1194 debug ("Can't get '%s' property from FIT 0x%08lx, "
1195 "node: offset %d, name %s (%s)\n",
1196 prop_name, (ulong)fit, noffset,
1197 fit_get_name (fit, noffset, NULL),
1198 fdt_strerror (err));
1199}
1200
1201/**
1202 * __fit_print_contents - prints out the contents of the FIT format image
1203 * @fit: pointer to the FIT format image header
1204 * @p: pointer to prefix string
1205 *
1206 * __fit_print_contents() formats a multi line FIT image contents description.
1207 * The routine prints out FIT image properties (root node level) follwed by
1208 * the details of each component image.
1209 *
1210 * returns:
1211 * no returned results
1212 */
1213static void __fit_print_contents (const void *fit, const char *p)
1214{
1215 char *desc;
1216 char *uname;
1217 int images_noffset;
1218 int confs_noffset;
1219 int noffset;
1220 int ndepth;
1221 int count = 0;
1222 int ret;
1223#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1224 time_t timestamp;
1225#endif
1226
1227 /* Root node properties */
1228 ret = fit_get_desc (fit, 0, &desc);
1229 printf ("%sFIT description: ", p);
1230 if (ret)
1231 printf ("unavailable\n");
1232 else
1233 printf ("%s\n", desc);
1234
1235#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1236 ret = fit_get_timestamp (fit, 0, &timestamp);
1237 printf ("%sCreated: ", p);
1238 if (ret)
1239 printf ("unavailable\n");
1240 else
1241 genimg_print_time (timestamp);
1242#endif
1243
1244 /* Find images parent node offset */
1245 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1246 if (images_noffset < 0) {
1247 printf ("Can't find images parent node '%s' (%s)\n",
1248 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1249 return;
1250 }
1251
1252 /* Process its subnodes, print out component images details */
1253 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1254 (noffset >= 0) && (ndepth > 0);
1255 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1256 if (ndepth == 1) {
1257 /*
1258 * Direct child node of the images parent node,
1259 * i.e. component image node.
1260 */
1261 printf ("%s Image %u (%s)\n", p, count++,
1262 fit_get_name(fit, noffset, NULL));
1263
1264 fit_image_print (fit, noffset, p);
1265 }
1266 }
1267
1268 /* Find configurations parent node offset */
1269 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1270 if (confs_noffset < 0) {
1271 debug ("Can't get configurations parent node '%s' (%s)\n",
1272 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1273 return;
1274 }
1275
1276 /* get default configuration unit name from default property */
1277 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1278 if (uname)
1279 printf ("%s Default Configuration: '%s'\n", p, uname);
1280
1281 /* Process its subnodes, print out configurations details */
1282 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1283 (noffset >= 0) && (ndepth > 0);
1284 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1285 if (ndepth == 1) {
1286 /*
1287 * Direct child node of the configurations parent node,
1288 * i.e. configuration node.
1289 */
1290 printf ("%s Configuration %u (%s)\n", p, count++,
1291 fit_get_name(fit, noffset, NULL));
1292
1293 fit_conf_print (fit, noffset, p);
1294 }
1295 }
1296}
1297
1298inline void fit_print_contents (const void *fit)
1299{
1300 __fit_print_contents (fit, " ");
1301}
1302
1303inline void fit_print_contents_noindent (const void *fit)
1304{
1305 __fit_print_contents (fit, "");
1306}
1307
1308/**
1309 * fit_image_print - prints out the FIT component image details
1310 * @fit: pointer to the FIT format image header
1311 * @image_noffset: offset of the component image node
1312 * @p: pointer to prefix string
1313 *
1314 * fit_image_print() lists all mandatory properies for the processed component
1315 * image. If present, hash nodes are printed out as well.
1316 *
1317 * returns:
1318 * no returned results
1319 */
1320void fit_image_print (const void *fit, int image_noffset, const char *p)
1321{
1322 char *desc;
1323 uint8_t type, arch, os, comp;
1324 size_t size;
1325 ulong load, entry;
1326 const void *data;
1327 int noffset;
1328 int ndepth;
1329 int ret;
1330
1331 /* Mandatory properties */
1332 ret = fit_get_desc (fit, image_noffset, &desc);
1333 printf ("%s Description: ", p);
1334 if (ret)
1335 printf ("unavailable\n");
1336 else
1337 printf ("%s\n", desc);
1338
1339 fit_image_get_type (fit, image_noffset, &type);
1340 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1341
1342 fit_image_get_comp (fit, image_noffset, &comp);
1343 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1344
1345 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1346
1347#ifndef USE_HOSTCC
1348 printf ("%s Data Start: ", p);
1349 if (ret)
1350 printf ("unavailable\n");
1351 else
1352 printf ("0x%08lx\n", (ulong)data);
1353#endif
1354
1355 printf ("%s Data Size: ", p);
1356 if (ret)
1357 printf ("unavailable\n");
1358 else
1359 genimg_print_size (size);
1360
1361 /* Remaining, type dependent properties */
1362 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1363 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1364 (type == IH_TYPE_FLATDT)) {
1365 fit_image_get_arch (fit, image_noffset, &arch);
1366 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1367 }
1368
1369 if (type == IH_TYPE_KERNEL) {
1370 fit_image_get_os (fit, image_noffset, &os);
1371 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1372 }
1373
1374 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1375 ret = fit_image_get_load (fit, image_noffset, &load);
1376 printf ("%s Load Address: ", p);
1377 if (ret)
1378 printf ("unavailable\n");
1379 else
1380 printf ("0x%08lx\n", load);
1381
1382 fit_image_get_entry (fit, image_noffset, &entry);
1383 printf ("%s Entry Point: ", p);
1384 if (ret)
1385 printf ("unavailable\n");
1386 else
1387 printf ("0x%08lx\n", entry);
1388 }
1389
1390 /* Process all hash subnodes of the component image node */
1391 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1392 (noffset >= 0) && (ndepth > 0);
1393 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1394 if (ndepth == 1) {
1395 /* Direct child node of the component image node */
1396 fit_image_print_hash (fit, noffset, p);
1397 }
1398 }
1399}
1400
1401/**
1402 * fit_image_print_hash - prints out the hash node details
1403 * @fit: pointer to the FIT format image header
1404 * @noffset: offset of the hash node
1405 * @p: pointer to prefix string
1406 *
1407 * fit_image_print_hash() lists properies for the processed hash node
1408 *
1409 * returns:
1410 * no returned results
1411 */
1412void fit_image_print_hash (const void *fit, int noffset, const char *p)
1413{
1414 char *algo;
1415 uint8_t *value;
1416 int value_len;
1417 int i, ret;
1418
1419 /*
1420 * Check subnode name, must be equal to "hash".
1421 * Multiple hash nodes require unique unit node
1422 * names, e.g. hash@1, hash@2, etc.
1423 */
1424 if (strncmp (fit_get_name(fit, noffset, NULL),
1425 FIT_HASH_NODENAME,
1426 strlen(FIT_HASH_NODENAME)) != 0)
1427 return;
1428
1429 debug ("%s Hash node: '%s'\n", p,
1430 fit_get_name (fit, noffset, NULL));
1431
1432 printf ("%s Hash algo: ", p);
1433 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1434 printf ("invalid/unsupported\n");
1435 return;
1436 }
1437 printf ("%s\n", algo);
1438
1439 ret = fit_image_hash_get_value (fit, noffset, &value,
1440 &value_len);
1441 printf ("%s Hash value: ", p);
1442 if (ret) {
1443 printf ("unavailable\n");
1444 } else {
1445 for (i = 0; i < value_len; i++)
1446 printf ("%02x", value[i]);
1447 printf ("\n");
1448 }
1449
1450 debug ("%s Hash len: %d\n", p, value_len);
1451}
1452
1453/**
1454 * fit_get_desc - get node description property
1455 * @fit: pointer to the FIT format image header
1456 * @noffset: node offset
1457 * @desc: double pointer to the char, will hold pointer to the descrption
1458 *
1459 * fit_get_desc() reads description property from a given node, if
1460 * description is found pointer to it is returened in third call argument.
1461 *
1462 * returns:
1463 * 0, on success
1464 * -1, on failure
1465 */
1466int fit_get_desc (const void *fit, int noffset, char **desc)
1467{
1468 int len;
1469
1470 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1471 if (*desc == NULL) {
1472 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1473 return -1;
1474 }
1475
1476 return 0;
1477}
1478
1479/**
1480 * fit_get_timestamp - get node timestamp property
1481 * @fit: pointer to the FIT format image header
1482 * @noffset: node offset
1483 * @timestamp: pointer to the time_t, will hold read timestamp
1484 *
1485 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1486 * is found and has a correct size its value is retured in third call
1487 * argument.
1488 *
1489 * returns:
1490 * 0, on success
1491 * -1, on property read failure
1492 * -2, on wrong timestamp size
1493 */
1494int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1495{
1496 int len;
1497 const void *data;
1498
1499 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1500 if (data == NULL) {
1501 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1502 return -1;
1503 }
1504 if (len != sizeof (uint32_t)) {
1505 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1506 return -2;
1507 }
1508
1509 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1510 return 0;
1511}
1512
1513/**
1514 * fit_image_get_node - get node offset for component image of a given unit name
1515 * @fit: pointer to the FIT format image header
1516 * @image_uname: component image node unit name
1517 *
1518 * fit_image_get_node() finds a component image (withing the '/images'
1519 * node) of a provided unit name. If image is found its node offset is
1520 * returned to the caller.
1521 *
1522 * returns:
1523 * image node offset when found (>=0)
1524 * negative number on failure (FDT_ERR_* code)
1525 */
1526int fit_image_get_node (const void *fit, const char *image_uname)
1527{
1528 int noffset, images_noffset;
1529
1530 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1531 if (images_noffset < 0) {
1532 debug ("Can't find images parent node '%s' (%s)\n",
1533 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1534 return images_noffset;
1535 }
1536
1537 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1538 if (noffset < 0) {
1539 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1540 image_uname, fdt_strerror (noffset));
1541 }
1542
1543 return noffset;
1544}
1545
1546/**
1547 * fit_image_get_os - get os id for a given component image node
1548 * @fit: pointer to the FIT format image header
1549 * @noffset: component image node offset
1550 * @os: pointer to the uint8_t, will hold os numeric id
1551 *
1552 * fit_image_get_os() finds os property in a given component image node.
1553 * If the property is found, its (string) value is translated to the numeric
1554 * id which is returned to the caller.
1555 *
1556 * returns:
1557 * 0, on success
1558 * -1, on failure
1559 */
1560int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1561{
1562 int len;
1563 const void *data;
1564
1565 /* Get OS name from property data */
1566 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1567 if (data == NULL) {
1568 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1569 *os = -1;
1570 return -1;
1571 }
1572
1573 /* Translate OS name to id */
1574 *os = genimg_get_os_id (data);
1575 return 0;
1576}
1577
1578/**
1579 * fit_image_get_arch - get arch id for a given component image node
1580 * @fit: pointer to the FIT format image header
1581 * @noffset: component image node offset
1582 * @arch: pointer to the uint8_t, will hold arch numeric id
1583 *
1584 * fit_image_get_arch() finds arch property in a given component image node.
1585 * If the property is found, its (string) value is translated to the numeric
1586 * id which is returned to the caller.
1587 *
1588 * returns:
1589 * 0, on success
1590 * -1, on failure
1591 */
1592int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1593{
1594 int len;
1595 const void *data;
1596
1597 /* Get architecture name from property data */
1598 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1599 if (data == NULL) {
1600 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1601 *arch = -1;
1602 return -1;
1603 }
1604
1605 /* Translate architecture name to id */
1606 *arch = genimg_get_arch_id (data);
1607 return 0;
1608}
1609
1610/**
1611 * fit_image_get_type - get type id for a given component image node
1612 * @fit: pointer to the FIT format image header
1613 * @noffset: component image node offset
1614 * @type: pointer to the uint8_t, will hold type numeric id
1615 *
1616 * fit_image_get_type() finds type property in a given component image node.
1617 * If the property is found, its (string) value is translated to the numeric
1618 * id which is returned to the caller.
1619 *
1620 * returns:
1621 * 0, on success
1622 * -1, on failure
1623 */
1624int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1625{
1626 int len;
1627 const void *data;
1628
1629 /* Get image type name from property data */
1630 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1631 if (data == NULL) {
1632 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1633 *type = -1;
1634 return -1;
1635 }
1636
1637 /* Translate image type name to id */
1638 *type = genimg_get_type_id (data);
1639 return 0;
1640}
1641
1642/**
1643 * fit_image_get_comp - get comp id for a given component image node
1644 * @fit: pointer to the FIT format image header
1645 * @noffset: component image node offset
1646 * @comp: pointer to the uint8_t, will hold comp numeric id
1647 *
1648 * fit_image_get_comp() finds comp property in a given component image node.
1649 * If the property is found, its (string) value is translated to the numeric
1650 * id which is returned to the caller.
1651 *
1652 * returns:
1653 * 0, on success
1654 * -1, on failure
1655 */
1656int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1657{
1658 int len;
1659 const void *data;
1660
1661 /* Get compression name from property data */
1662 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1663 if (data == NULL) {
1664 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1665 *comp = -1;
1666 return -1;
1667 }
1668
1669 /* Translate compression name to id */
1670 *comp = genimg_get_comp_id (data);
1671 return 0;
1672}
1673
1674/**
1675 * fit_image_get_load - get load address property for a given component image node
1676 * @fit: pointer to the FIT format image header
1677 * @noffset: component image node offset
1678 * @load: pointer to the uint32_t, will hold load address
1679 *
1680 * fit_image_get_load() finds load address property in a given component image node.
1681 * If the property is found, its value is returned to the caller.
1682 *
1683 * returns:
1684 * 0, on success
1685 * -1, on failure
1686 */
1687int fit_image_get_load (const void *fit, int noffset, ulong *load)
1688{
1689 int len;
1690 const uint32_t *data;
1691
1692 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1693 if (data == NULL) {
1694 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1695 return -1;
1696 }
1697
1698 *load = uimage_to_cpu (*data);
1699 return 0;
1700}
1701
1702/**
1703 * fit_image_get_entry - get entry point address property for a given component image node
1704 * @fit: pointer to the FIT format image header
1705 * @noffset: component image node offset
1706 * @entry: pointer to the uint32_t, will hold entry point address
1707 *
1708 * fit_image_get_entry() finds entry point address property in a given component image node.
1709 * If the property is found, its value is returned to the caller.
1710 *
1711 * returns:
1712 * 0, on success
1713 * -1, on failure
1714 */
1715int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1716{
1717 int len;
1718 const uint32_t *data;
1719
1720 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1721 if (data == NULL) {
1722 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1723 return -1;
1724 }
1725
1726 *entry = uimage_to_cpu (*data);
1727 return 0;
1728}
1729
1730/**
1731 * fit_image_get_data - get data property and its size for a given component image node
1732 * @fit: pointer to the FIT format image header
1733 * @noffset: component image node offset
1734 * @data: double pointer to void, will hold data property's data address
1735 * @size: pointer to size_t, will hold data property's data size
1736 *
1737 * fit_image_get_data() finds data property in a given component image node.
1738 * If the property is found its data start address and size are returned to
1739 * the caller.
1740 *
1741 * returns:
1742 * 0, on success
1743 * -1, on failure
1744 */
1745int fit_image_get_data (const void *fit, int noffset,
1746 const void **data, size_t *size)
1747{
1748 int len;
1749
1750 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1751 if (*data == NULL) {
1752 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1753 *size = 0;
1754 return -1;
1755 }
1756
1757 *size = len;
1758 return 0;
1759}
1760
1761/**
1762 * fit_image_hash_get_algo - get hash algorithm name
1763 * @fit: pointer to the FIT format image header
1764 * @noffset: hash node offset
1765 * @algo: double pointer to char, will hold pointer to the algorithm name
1766 *
1767 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1768 * If the property is found its data start address is returned to the caller.
1769 *
1770 * returns:
1771 * 0, on success
1772 * -1, on failure
1773 */
1774int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1775{
1776 int len;
1777
1778 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1779 if (*algo == NULL) {
1780 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1781 return -1;
1782 }
1783
1784 return 0;
1785}
1786
1787/**
1788 * fit_image_hash_get_value - get hash value and length
1789 * @fit: pointer to the FIT format image header
1790 * @noffset: hash node offset
1791 * @value: double pointer to uint8_t, will hold address of a hash value data
1792 * @value_len: pointer to an int, will hold hash data length
1793 *
1794 * fit_image_hash_get_value() finds hash value property in a given hash node.
1795 * If the property is found its data start address and size are returned to
1796 * the caller.
1797 *
1798 * returns:
1799 * 0, on success
1800 * -1, on failure
1801 */
1802int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1803 int *value_len)
1804{
1805 int len;
1806
1807 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1808 if (*value == NULL) {
1809 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1810 *value_len = 0;
1811 return -1;
1812 }
1813
1814 *value_len = len;
1815 return 0;
1816}
1817
1818/**
1819 * fit_set_timestamp - set node timestamp property
1820 * @fit: pointer to the FIT format image header
1821 * @noffset: node offset
1822 * @timestamp: timestamp value to be set
1823 *
1824 * fit_set_timestamp() attempts to set timestamp property in the requested
1825 * node and returns operation status to the caller.
1826 *
1827 * returns:
1828 * 0, on success
1829 * -1, on property read failure
1830 */
1831int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1832{
1833 uint32_t t;
1834 int ret;
1835
1836 t = cpu_to_uimage (timestamp);
1837 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1838 sizeof (uint32_t));
1839 if (ret) {
1840 printf ("Can't set '%s' property for '%s' node (%s)\n",
1841 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1842 fdt_strerror (ret));
1843 return -1;
1844 }
1845
1846 return 0;
1847}
1848
1849/**
1850 * calculate_hash - calculate and return hash for provided input data
1851 * @data: pointer to the input data
1852 * @data_len: data length
1853 * @algo: requested hash algorithm
1854 * @value: pointer to the char, will hold hash value data (caller must
1855 * allocate enough free space)
1856 * value_len: length of the calculated hash
1857 *
1858 * calculate_hash() computes input data hash according to the requested algorithm.
1859 * Resulting hash value is placed in caller provided 'value' buffer, length
1860 * of the calculated hash is returned via value_len pointer argument.
1861 *
1862 * returns:
1863 * 0, on success
1864 * -1, when algo is unsupported
1865 */
1866static int calculate_hash (const void *data, int data_len, const char *algo,
1867 uint8_t *value, int *value_len)
1868{
1869 if (strcmp (algo, "crc32") == 0 ) {
1870 *((uint32_t *)value) = crc32 (0, data, data_len);
1871 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1872 *value_len = 4;
1873 } else if (strcmp (algo, "sha1") == 0 ) {
1874 sha1_csum ((unsigned char *) data, data_len,
1875 (unsigned char *) value);
1876 *value_len = 20;
1877 } else if (strcmp (algo, "md5") == 0 ) {
1878 printf ("MD5 not supported\n");
1879 *value_len = 0;
1880 } else {
1881 debug ("Unsupported hash alogrithm\n");
1882 return -1;
1883 }
1884 return 0;
1885}
1886
1887#ifdef USE_HOSTCC
1888/**
1889 * fit_set_hashes - process FIT component image nodes and calculate hashes
1890 * @fit: pointer to the FIT format image header
1891 *
1892 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1893 * Hashes are calculated for all component images which have hash subnodes
1894 * with algorithm property set to one of the supported hash algorithms.
1895 *
1896 * returns
1897 * 0, on success
1898 * libfdt error code, on failure
1899 */
1900int fit_set_hashes (void *fit)
1901{
1902 int images_noffset;
1903 int noffset;
1904 int ndepth;
1905 int ret;
1906
1907 /* Find images parent node offset */
1908 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1909 if (images_noffset < 0) {
1910 printf ("Can't find images parent node '%s' (%s)\n",
1911 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1912 return images_noffset;
1913 }
1914
1915 /* Process its subnodes, print out component images details */
1916 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1917 (noffset >= 0) && (ndepth > 0);
1918 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1919 if (ndepth == 1) {
1920 /*
1921 * Direct child node of the images parent node,
1922 * i.e. component image node.
1923 */
1924 ret = fit_image_set_hashes (fit, noffset);
1925 if (ret)
1926 return ret;
1927 }
1928 }
1929
1930 return 0;
1931}
1932
1933/**
1934 * fit_image_set_hashes - calculate/set hashes for given component image node
1935 * @fit: pointer to the FIT format image header
1936 * @image_noffset: requested component image node
1937 *
1938 * fit_image_set_hashes() adds hash values for an component image node. All
1939 * existing hash subnodes are checked, if algorithm property is set to one of
1940 * the supported hash algorithms, hash value is computed and corresponding
1941 * hash node property is set, for example:
1942 *
1943 * Input component image node structure:
1944 *
1945 * o image@1 (at image_noffset)
1946 * | - data = [binary data]
1947 * o hash@1
1948 * |- algo = "sha1"
1949 *
1950 * Output component image node structure:
1951 *
1952 * o image@1 (at image_noffset)
1953 * | - data = [binary data]
1954 * o hash@1
1955 * |- algo = "sha1"
1956 * |- value = sha1(data)
1957 *
1958 * returns:
1959 * 0 on sucess
1960 * <0 on failure
1961 */
1962int fit_image_set_hashes (void *fit, int image_noffset)
1963{
1964 const void *data;
1965 size_t size;
1966 char *algo;
1967 uint8_t value[FIT_MAX_HASH_LEN];
1968 int value_len;
1969 int noffset;
1970 int ndepth;
1971
1972 /* Get image data and data length */
1973 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
1974 printf ("Can't get image data/size\n");
1975 return -1;
1976 }
1977
1978 /* Process all hash subnodes of the component image node */
1979 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1980 (noffset >= 0) && (ndepth > 0);
1981 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1982 if (ndepth == 1) {
1983 /* Direct child node of the component image node */
1984
1985 /*
1986 * Check subnode name, must be equal to "hash".
1987 * Multiple hash nodes require unique unit node
1988 * names, e.g. hash@1, hash@2, etc.
1989 */
1990 if (strncmp (fit_get_name(fit, noffset, NULL),
1991 FIT_HASH_NODENAME,
1992 strlen(FIT_HASH_NODENAME)) != 0) {
1993 /* Not a hash subnode, skip it */
1994 continue;
1995 }
1996
1997 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1998 printf ("Can't get hash algo property for "
1999 "'%s' hash node in '%s' image node\n",
2000 fit_get_name (fit, noffset, NULL),
2001 fit_get_name (fit, image_noffset, NULL));
2002 return -1;
2003 }
2004
2005 if (calculate_hash (data, size, algo, value, &value_len)) {
2006 printf ("Unsupported hash algorithm (%s) for "
2007 "'%s' hash node in '%s' image node\n",
2008 algo, fit_get_name (fit, noffset, NULL),
2009 fit_get_name (fit, image_noffset, NULL));
2010 return -1;
2011 }
2012
2013 if (fit_image_hash_set_value (fit, noffset, value,
2014 value_len)) {
2015 printf ("Can't set hash value for "
2016 "'%s' hash node in '%s' image node\n",
2017 fit_get_name (fit, noffset, NULL),
2018 fit_get_name (fit, image_noffset, NULL));
2019 return -1;
2020 }
2021 }
2022 }
2023
2024 return 0;
2025}
2026
2027/**
2028 * fit_image_hash_set_value - set hash value in requested has node
2029 * @fit: pointer to the FIT format image header
2030 * @noffset: hash node offset
2031 * @value: hash value to be set
2032 * @value_len: hash value length
2033 *
2034 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2035 * given and returns operation status to the caller.
2036 *
2037 * returns
2038 * 0, on success
2039 * -1, on failure
2040 */
2041int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2042 int value_len)
2043{
2044 int ret;
2045
2046 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2047 if (ret) {
2048 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2049 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2050 fdt_strerror (ret));
2051 return -1;
2052 }
2053
2054 return 0;
2055}
2056#endif /* USE_HOSTCC */
2057
2058/**
2059 * fit_image_check_hashes - verify data intergity
2060 * @fit: pointer to the FIT format image header
2061 * @image_noffset: component image node offset
2062 *
2063 * fit_image_check_hashes() goes over component image hash nodes,
2064 * re-calculates each data hash and compares with the value stored in hash
2065 * node.
2066 *
2067 * returns:
2068 * 1, if all hashes are valid
2069 * 0, otherwise (or on error)
2070 */
2071int fit_image_check_hashes (const void *fit, int image_noffset)
2072{
2073 const void *data;
2074 size_t size;
2075 char *algo;
2076 uint8_t *fit_value;
2077 int fit_value_len;
2078 uint8_t value[FIT_MAX_HASH_LEN];
2079 int value_len;
2080 int noffset;
2081 int ndepth;
2082 char *err_msg = "";
2083
2084 /* Get image data and data length */
2085 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2086 printf ("Can't get image data/size\n");
2087 return 0;
2088 }
2089
2090 /* Process all hash subnodes of the component image node */
2091 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2092 (noffset >= 0) && (ndepth > 0);
2093 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2094 if (ndepth == 1) {
2095 /* Direct child node of the component image node */
2096
2097 /*
2098 * Check subnode name, must be equal to "hash".
2099 * Multiple hash nodes require unique unit node
2100 * names, e.g. hash@1, hash@2, etc.
2101 */
2102 if (strncmp (fit_get_name(fit, noffset, NULL),
2103 FIT_HASH_NODENAME,
2104 strlen(FIT_HASH_NODENAME)) != 0)
2105 continue;
2106
2107 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2108 err_msg = "Can't get hash algo property";
2109 goto error;
2110 }
2111 printf ("%s", algo);
2112
2113 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2114 &fit_value_len)) {
2115 err_msg = "Can't get hash value property";
2116 goto error;
2117 }
2118
2119 if (calculate_hash (data, size, algo, value, &value_len)) {
2120 err_msg = "Unsupported hash algorithm";
2121 goto error;
2122 }
2123
2124 if (value_len != fit_value_len) {
2125 err_msg = "Bad hash value len";
2126 goto error;
2127 } else if (memcmp (value, fit_value, value_len) != 0) {
2128 err_msg = "Bad hash value";
2129 goto error;
2130 }
2131 printf ("+ ");
2132 }
2133 }
2134
2135 return 1;
2136
2137error:
2138 printf ("%s for '%s' hash node in '%s' image node\n",
2139 err_msg, fit_get_name (fit, noffset, NULL),
2140 fit_get_name (fit, image_noffset, NULL));
2141 return 0;
2142}
2143
2144/**
2145 * fit_image_check_os - check whether image node is of a given os type
2146 * @fit: pointer to the FIT format image header
2147 * @noffset: component image node offset
2148 * @os: requested image os
2149 *
2150 * fit_image_check_os() reads image os property and compares its numeric
2151 * id with the requested os. Comparison result is returned to the caller.
2152 *
2153 * returns:
2154 * 1 if image is of given os type
2155 * 0 otherwise (or on error)
2156 */
2157int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2158{
2159 uint8_t image_os;
2160
2161 if (fit_image_get_os (fit, noffset, &image_os))
2162 return 0;
2163 return (os == image_os);
2164}
2165
2166/**
2167 * fit_image_check_arch - check whether image node is of a given arch
2168 * @fit: pointer to the FIT format image header
2169 * @noffset: component image node offset
2170 * @arch: requested imagearch
2171 *
2172 * fit_image_check_arch() reads image arch property and compares its numeric
2173 * id with the requested arch. Comparison result is returned to the caller.
2174 *
2175 * returns:
2176 * 1 if image is of given arch
2177 * 0 otherwise (or on error)
2178 */
2179int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2180{
2181 uint8_t image_arch;
2182
2183 if (fit_image_get_arch (fit, noffset, &image_arch))
2184 return 0;
2185 return (arch == image_arch);
2186}
2187
2188/**
2189 * fit_image_check_type - check whether image node is of a given type
2190 * @fit: pointer to the FIT format image header
2191 * @noffset: component image node offset
2192 * @type: requested image type
2193 *
2194 * fit_image_check_type() reads image type property and compares its numeric
2195 * id with the requested type. Comparison result is returned to the caller.
2196 *
2197 * returns:
2198 * 1 if image is of given type
2199 * 0 otherwise (or on error)
2200 */
2201int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2202{
2203 uint8_t image_type;
2204
2205 if (fit_image_get_type (fit, noffset, &image_type))
2206 return 0;
2207 return (type == image_type);
2208}
2209
2210/**
2211 * fit_image_check_comp - check whether image node uses given compression
2212 * @fit: pointer to the FIT format image header
2213 * @noffset: component image node offset
2214 * @comp: requested image compression type
2215 *
2216 * fit_image_check_comp() reads image compression property and compares its
2217 * numeric id with the requested compression type. Comparison result is
2218 * returned to the caller.
2219 *
2220 * returns:
2221 * 1 if image uses requested compression
2222 * 0 otherwise (or on error)
2223 */
2224int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2225{
2226 uint8_t image_comp;
2227
2228 if (fit_image_get_comp (fit, noffset, &image_comp))
2229 return 0;
2230 return (comp == image_comp);
2231}
2232
2233/**
2234 * fit_check_format - sanity check FIT image format
2235 * @fit: pointer to the FIT format image header
2236 *
2237 * fit_check_format() runs a basic sanity FIT image verification.
2238 * Routine checks for mandatory properties, nodes, etc.
2239 *
2240 * returns:
2241 * 1, on success
2242 * 0, on failure
2243 */
2244int fit_check_format (const void *fit)
2245{
2246 /* mandatory / node 'description' property */
2247 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2248 debug ("Wrong FIT format: no description\n");
2249 return 0;
2250 }
2251
2252#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2253 /* mandatory / node 'timestamp' property */
2254 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2255 debug ("Wrong FIT format: no description\n");
2256 return 0;
2257 }
2258#endif
2259
2260 /* mandatory subimages parent '/images' node */
2261 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2262 debug ("Wrong FIT format: no images parent node\n");
2263 return 0;
2264 }
2265
2266 return 1;
2267}
2268
2269/**
2270 * fit_conf_get_node - get node offset for configuration of a given unit name
2271 * @fit: pointer to the FIT format image header
2272 * @conf_uname: configuration node unit name
2273 *
2274 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2275 * parant node) of a provided unit name. If configuration is found its node offset
2276 * is returned to the caller.
2277 *
2278 * When NULL is provided in second argument fit_conf_get_node() will search
2279 * for a default configuration node instead. Default configuration node unit name
2280 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2281 *
2282 * returns:
2283 * configuration node offset when found (>=0)
2284 * negative number on failure (FDT_ERR_* code)
2285 */
2286int fit_conf_get_node (const void *fit, const char *conf_uname)
2287{
2288 int noffset, confs_noffset;
2289 int len;
2290
2291 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2292 if (confs_noffset < 0) {
2293 debug ("Can't find configurations parent node '%s' (%s)\n",
2294 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2295 return confs_noffset;
2296 }
2297
2298 if (conf_uname == NULL) {
2299 /* get configuration unit name from the default property */
2300 debug ("No configuration specified, trying default...\n");
2301 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2302 if (conf_uname == NULL) {
2303 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2304 return len;
2305 }
2306 debug ("Found default configuration: '%s'\n", conf_uname);
2307 }
2308
2309 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2310 if (noffset < 0) {
2311 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2312 conf_uname, fdt_strerror (noffset));
2313 }
2314
2315 return noffset;
2316}
2317
2318static int __fit_conf_get_prop_node (const void *fit, int noffset,
2319 const char *prop_name)
2320{
2321 char *uname;
2322 int len;
2323
2324 /* get kernel image unit name from configuration kernel property */
2325 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2326 if (uname == NULL)
2327 return len;
2328
2329 return fit_image_get_node (fit, uname);
2330}
2331
2332/**
2333 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2334 * a given configuration
2335 * @fit: pointer to the FIT format image header
2336 * @noffset: configuration node offset
2337 *
2338 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2339 * configuration FIT_KERNEL_PROP property and translates it to the node
2340 * offset.
2341 *
2342 * returns:
2343 * image node offset when found (>=0)
2344 * negative number on failure (FDT_ERR_* code)
2345 */
2346int fit_conf_get_kernel_node (const void *fit, int noffset)
2347{
2348 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2349}
2350
2351/**
2352 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2353 * a given configuration
2354 * @fit: pointer to the FIT format image header
2355 * @noffset: configuration node offset
2356 *
2357 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2358 * configuration FIT_KERNEL_PROP property and translates it to the node
2359 * offset.
2360 *
2361 * returns:
2362 * image node offset when found (>=0)
2363 * negative number on failure (FDT_ERR_* code)
2364 */
2365int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2366{
2367 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2368}
2369
2370/**
2371 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2372 * a given configuration
2373 * @fit: pointer to the FIT format image header
2374 * @noffset: configuration node offset
2375 *
2376 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2377 * configuration FIT_KERNEL_PROP property and translates it to the node
2378 * offset.
2379 *
2380 * returns:
2381 * image node offset when found (>=0)
2382 * negative number on failure (FDT_ERR_* code)
2383 */
2384int fit_conf_get_fdt_node (const void *fit, int noffset)
2385{
2386 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2387}
d5934ad7 2388
5dfb5213
MB
2389/**
2390 * fit_conf_print - prints out the FIT configuration details
2391 * @fit: pointer to the FIT format image header
2392 * @conf_noffset: offset of the configuration node
2393 * @p: pointer to prefix string
2394 *
2395 * fit_conf_print() lists all mandatory properies for the processed
2396 * configuration node.
2397 *
2398 * returns:
2399 * no returned results
2400 */
2401void fit_conf_print (const void *fit, int noffset, const char *p)
2402{
2403 char *desc;
2404 char *uname;
2405 int ret;
2406
2407 /* Mandatory properties */
2408 ret = fit_get_desc (fit, noffset, &desc);
2409 printf ("%s Description: ", p);
2410 if (ret)
2411 printf ("unavailable\n");
2412 else
2413 printf ("%s\n", desc);
2414
2415 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2416 printf ("%s Kernel: ", p);
2417 if (uname == NULL)
2418 printf ("unavailable\n");
2419 else
2420 printf ("%s\n", uname);
2421
2422 /* Optional properties */
2423 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2424 if (uname)
2425 printf ("%s Init Ramdisk: %s\n", p, uname);
2426
2427 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2428 if (uname)
2429 printf ("%s FDT: %s\n", p, uname);
2430}
c8779648
MB
2431
2432/**
2433 * fit_check_ramdisk - verify FIT format ramdisk subimage
2434 * @fit_hdr: pointer to the FIT ramdisk header
2435 * @rd_noffset: ramdisk subimage node offset within FIT image
2436 * @arch: requested ramdisk image architecture type
2437 * @verify: data CRC verification flag
2438 *
2439 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2440 * specified FIT image.
2441 *
2442 * returns:
2443 * 1, on success
2444 * 0, on failure
2445 */
2446#ifndef USE_HOSTCC
2447static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2448{
2449 fit_image_print (fit, rd_noffset, " ");
2450
2451 if (verify) {
2452 puts (" Verifying Hash Integrity ... ");
2453 if (!fit_image_check_hashes (fit, rd_noffset)) {
2454 puts ("Bad Data Hash\n");
2455 return 0;
2456 }
2457 puts ("OK\n");
2458 }
2459
2460 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2461 !fit_image_check_arch (fit, rd_noffset, arch) ||
2462 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2463 printf ("No Linux %s Ramdisk Image\n",
2464 genimg_get_arch_name(arch));
2465 return 0;
2466 }
2467
2468 return 1;
2469}
2470#endif /* USE_HOSTCC */
f50433d6 2471#endif /* CONFIG_FIT */