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