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