blob: 9188024802d7b9f0cd87e08c048047c55e3c08b6 [file] [log] [blame]
Marian Balakowicz41d71ed2008-01-08 18:14:09 +01001/*
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 */
Marian Balakowicz9c701e82008-01-31 13:57:17 +010025
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010026#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +010027#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
Marian Balakowicz902ce702008-05-13 15:53:29 +020038#ifdef CONFIG_LOGBUFFER
39#include <logbuff.h>
40#endif
41
Marian Balakowicza1cc1472008-02-21 17:27:41 +010042#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
43#include <rtc.h>
44#endif
45
Marian Balakowicze227fbb2008-02-29 21:24:06 +010046#include <image.h>
47
Marian Balakowicza50d5152008-03-12 10:12:37 +010048#if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010049#include <fdt.h>
50#include <libfdt.h>
51#include <fdt_support.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010052#endif
53
54#if defined(CONFIG_FIT)
Andy Fleming72c23be2008-04-02 16:19:07 -050055#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010056#include <sha1.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010057
58static int fit_check_ramdisk (const void *fit, int os_noffset,
59 uint8_t arch, int verify);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010060#endif
61
Marian Balakowicz2efc2642008-01-31 13:58:13 +010062#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;
Marian Balakowicz61fde552008-02-27 11:01:04 +010067
Marian Balakowicza0ffb422008-03-12 10:14:38 +010068static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
69 int verify);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010070#else
Marian Balakowicz95418502008-01-31 13:55:39 +010071#include "mkimage.h"
Andy Fleming72c23be2008-04-02 16:19:07 -050072#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010073#include <time.h>
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010074#include <image.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010075#endif /* !USE_HOSTCC*/
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010076
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010077typedef 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", },
Grant Erickson80f51b82008-05-04 16:45:01 -070095 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010096 { 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
Mike Frysingera2c67e92008-03-31 11:02:01 -0400158uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200159uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100160static 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
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100164
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100165/*****************************************************************************/
166/* Legacy format routines */
167/*****************************************************************************/
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100168int 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);
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200187 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100188
189 return (dcrc == image_get_dcrc (hdr));
190}
191
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100192
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100193/**
194 * image_multi_count - get component (sub-image) count
195 * @hdr: pointer to the header of the multi component image
196 *
197 * image_multi_count() returns number of components in a multi
198 * component image.
199 *
200 * Note: no checking of the image type is done, caller must pass
201 * a valid multi component image.
202 *
203 * returns:
204 * number of components
205 */
206ulong image_multi_count (image_header_t *hdr)
207{
208 ulong i, count = 0;
Marian Balakowicza1474212008-02-29 16:00:06 +0100209 uint32_t *size;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100210
211 /* get start of the image payload, which in case of multi
212 * component images that points to a table of component sizes */
Marian Balakowicza1474212008-02-29 16:00:06 +0100213 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100214
215 /* count non empty slots */
216 for (i = 0; size[i]; ++i)
217 count++;
218
219 return count;
220}
221
222/**
223 * image_multi_getimg - get component data address and size
224 * @hdr: pointer to the header of the multi component image
225 * @idx: index of the requested component
226 * @data: pointer to a ulong variable, will hold component data address
227 * @len: pointer to a ulong variable, will hold component size
228 *
229 * image_multi_getimg() returns size and data address for the requested
230 * component in a multi component image.
231 *
232 * Note: no checking of the image type is done, caller must pass
233 * a valid multi component image.
234 *
235 * returns:
236 * data address and size of the component, if idx is valid
237 * 0 in data and len, if idx is out of range
238 */
239void image_multi_getimg (image_header_t *hdr, ulong idx,
240 ulong *data, ulong *len)
241{
242 int i;
Marian Balakowicza1474212008-02-29 16:00:06 +0100243 uint32_t *size;
Nick Spence143a1592008-05-10 14:02:04 -0700244 ulong offset, count, img_data;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100245
246 /* get number of component */
247 count = image_multi_count (hdr);
248
249 /* get start of the image payload, which in case of multi
250 * component images that points to a table of component sizes */
Marian Balakowicza1474212008-02-29 16:00:06 +0100251 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100252
253 /* get address of the proper component data start, which means
254 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowicza1474212008-02-29 16:00:06 +0100255 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100256
257 if (idx < count) {
Marian Balakowicza1474212008-02-29 16:00:06 +0100258 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100259 offset = 0;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100260
261 /* go over all indices preceding requested component idx */
262 for (i = 0; i < idx; i++) {
Nick Spence143a1592008-05-10 14:02:04 -0700263 /* add up i-th component size, rounding up to 4 bytes */
264 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100265 }
266
267 /* calculate idx-th component data address */
Nick Spence143a1592008-05-10 14:02:04 -0700268 *data = img_data + offset;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100269 } else {
270 *len = 0;
271 *data = 0;
272 }
273}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100274
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100275static void image_print_type (image_header_t *hdr)
276{
277 const char *os, *arch, *type, *comp;
278
279 os = genimg_get_os_name (image_get_os (hdr));
280 arch = genimg_get_arch_name (image_get_arch (hdr));
281 type = genimg_get_type_name (image_get_type (hdr));
282 comp = genimg_get_comp_name (image_get_comp (hdr));
283
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100284 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100285}
286
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100287/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200288 * image_print_contents - prints out the contents of the legacy format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100289 * @hdr: pointer to the legacy format image header
290 * @p: pointer to prefix string
291 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200292 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100293 * The routine prints out all header fields followed by the size/offset data
294 * for MULTI/SCRIPT images.
295 *
296 * returns:
297 * no returned results
298 */
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200299void image_print_contents (image_header_t *hdr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100300{
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200301 const char *p;
302
303#ifdef USE_HOSTCC
304 p = "";
305#else
306 p = " ";
307#endif
308
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100309 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
310#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
311 printf ("%sCreated: ", p);
312 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100313#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100314 printf ("%sImage Type: ", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100315 image_print_type (hdr);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100316 printf ("%sData Size: ", p);
317 genimg_print_size (image_get_data_size (hdr));
318 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
319 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100320
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100321 if (image_check_type (hdr, IH_TYPE_MULTI) ||
322 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100323 int i;
324 ulong data, len;
325 ulong count = image_multi_count (hdr);
326
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100327 printf ("%sContents:\n", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100328 for (i = 0; i < count; i++) {
329 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100330
331 printf ("%s Image %d: ", p, i);
332 genimg_print_size (len);
333
334 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
335 /*
336 * the user may need to know offsets
337 * if planning to do something with
338 * multiple files
339 */
340 printf ("%s Offset = 0x%08lx\n", p, data);
341 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100342 }
343 }
344}
345
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100346
347#ifndef USE_HOSTCC
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100348/**
349 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100350 * @rd_addr: ramdisk image start address
351 * @arch: expected ramdisk architecture
352 * @verify: checksum verification flag
353 *
354 * image_get_ramdisk() returns a pointer to the verified ramdisk image
355 * header. Routine receives image start address and expected architecture
356 * flag. Verification done covers data and header integrity and os/type/arch
357 * fields checking.
358 *
359 * If dataflash support is enabled routine checks for dataflash addresses
360 * and handles required dataflash reads.
361 *
362 * returns:
363 * pointer to a ramdisk image header, if image was found and valid
364 * otherwise, return NULL
365 */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100366static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
367 int verify)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100368{
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100369 image_header_t *rd_hdr = (image_header_t *)rd_addr;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100370
371 if (!image_check_magic (rd_hdr)) {
372 puts ("Bad Magic Number\n");
373 show_boot_progress (-10);
374 return NULL;
375 }
376
377 if (!image_check_hcrc (rd_hdr)) {
378 puts ("Bad Header Checksum\n");
379 show_boot_progress (-11);
380 return NULL;
381 }
382
383 show_boot_progress (10);
384 image_print_contents (rd_hdr);
385
386 if (verify) {
387 puts(" Verifying Checksum ... ");
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200388 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100389 puts ("Bad Data CRC\n");
390 show_boot_progress (-12);
391 return NULL;
392 }
393 puts("OK\n");
394 }
395
396 show_boot_progress (11);
397
398 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
399 !image_check_arch (rd_hdr, arch) ||
400 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
401 printf ("No Linux %s Ramdisk Image\n",
402 genimg_get_arch_name(arch));
403 show_boot_progress (-13);
404 return NULL;
405 }
406
407 return rd_hdr;
408}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100409#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100410
411/*****************************************************************************/
412/* Shared dual-format routines */
413/*****************************************************************************/
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100414#ifndef USE_HOSTCC
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200415int getenv_yesno (char *var)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100416{
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200417 char *s = getenv (var);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100418 return (s && (*s == 'n')) ? 0 : 1;
419}
420
421ulong getenv_bootm_low(void)
422{
423 char *s = getenv ("bootm_low");
424 if (s) {
425 ulong tmp = simple_strtoul (s, NULL, 16);
426 return tmp;
427 }
428
Marian Balakowicz2499b042008-03-12 12:14:15 +0100429#if defined(CFG_SDRAM_BASE)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100430 return CFG_SDRAM_BASE;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100431#elif defined(CONFIG_ARM)
432 return gd->bd->bi_dram[0].start;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100433#else
434 return 0;
435#endif
436}
437
438ulong getenv_bootm_size(void)
439{
440 char *s = getenv ("bootm_size");
441 if (s) {
442 ulong tmp = simple_strtoul (s, NULL, 16);
443 return tmp;
444 }
445
Marian Balakowicz2499b042008-03-12 12:14:15 +0100446#if defined(CONFIG_ARM)
447 return gd->bd->bi_dram[0].size;
448#else
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100449 return gd->bd->bi_memsize;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100450#endif
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100451}
452
453void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
454{
455#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
456 while (len > 0) {
457 size_t tail = (len > chunksz) ? chunksz : len;
458 WATCHDOG_RESET ();
459 memmove (to, from, tail);
460 to += tail;
461 from += tail;
462 len -= tail;
463 }
464#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
465 memmove (to, from, len);
466#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
467}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100468#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100469
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100470static void genimg_print_size (uint32_t size)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100471{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100472#ifndef USE_HOSTCC
473 printf ("%d Bytes = ", size);
474 print_size (size, "\n");
475#else
476 printf ("%d Bytes = %.2f kB = %.2f MB\n",
477 size, (double)size / 1.024e3,
478 (double)size / 1.048576e6);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100479#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100480}
481
482#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
483static void genimg_print_time (time_t timestamp)
484{
485#ifndef USE_HOSTCC
486 struct rtc_time tm;
487
488 to_tm (timestamp, &tm);
489 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
490 tm.tm_year, tm.tm_mon, tm.tm_mday,
491 tm.tm_hour, tm.tm_min, tm.tm_sec);
492#else
493 printf ("%s", ctime(&timestamp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100494#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100495}
496#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
497
498/**
499 * get_table_entry_name - translate entry id to long name
500 * @table: pointer to a translation table for entries of a specific type
501 * @msg: message to be returned when translation fails
502 * @id: entry id to be translated
503 *
504 * get_table_entry_name() will go over translation table trying to find
505 * entry that matches given id. If matching entry is found, its long
506 * name is returned to the caller.
507 *
508 * returns:
509 * long entry name if translation succeeds
510 * msg otherwise
511 */
512static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
513{
514 for (; table->id >= 0; ++table) {
515 if (table->id == id)
516 return (table->lname);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100517 }
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100518 return (msg);
519}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100520
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100521const char *genimg_get_os_name (uint8_t os)
522{
523 return (get_table_entry_name (uimage_os, "Unknown OS", os));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100524}
525
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100526const char *genimg_get_arch_name (uint8_t arch)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100527{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100528 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
529}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100530
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100531const char *genimg_get_type_name (uint8_t type)
532{
533 return (get_table_entry_name (uimage_type, "Unknown Image", type));
534}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100535
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100536const char *genimg_get_comp_name (uint8_t comp)
537{
538 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100539}
540
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100541/**
542 * get_table_entry_id - translate short entry name to id
543 * @table: pointer to a translation table for entries of a specific type
544 * @table_name: to be used in case of error
545 * @name: entry short name to be translated
546 *
547 * get_table_entry_id() will go over translation table trying to find
548 * entry that matches given short name. If matching entry is found,
549 * its id returned to the caller.
550 *
551 * returns:
552 * entry id if translation succeeds
553 * -1 otherwise
554 */
555static int get_table_entry_id (table_entry_t *table,
556 const char *table_name, const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100557{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100558 table_entry_t *t;
559#ifdef USE_HOSTCC
560 int first = 1;
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100561
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100562 for (t = table; t->id >= 0; ++t) {
563 if (t->sname && strcasecmp(t->sname, name) == 0)
564 return (t->id);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100565 }
566
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100567 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
568 for (t = table; t->id >= 0; ++t) {
569 if (t->sname == NULL)
570 continue;
571 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
572 first = 0;
573 }
574 fprintf (stderr, "\n");
575#else
576 for (t = table; t->id >= 0; ++t) {
577 if (t->sname && strcmp(t->sname, name) == 0)
578 return (t->id);
579 }
580 debug ("Invalid %s Type: %s\n", table_name, name);
581#endif /* USE_HOSTCC */
582 return (-1);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100583}
584
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100585int genimg_get_os_id (const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100586{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100587 return (get_table_entry_id (uimage_os, "OS", name));
588}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100589
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100590int genimg_get_arch_id (const char *name)
591{
592 return (get_table_entry_id (uimage_arch, "CPU", name));
593}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100594
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100595int genimg_get_type_id (const char *name)
596{
597 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100598}
Marian Balakowicz95418502008-01-31 13:55:39 +0100599
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100600int genimg_get_comp_id (const char *name)
601{
602 return (get_table_entry_id (uimage_comp, "Compression", name));
603}
604
605#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +0100606/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100607 * genimg_get_format - get image format type
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100608 * @img_addr: image start address
609 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100610 * genimg_get_format() checks whether provided address points to a valid
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100611 * legacy or FIT image.
612 *
Marian Balakowicz12560682008-02-27 11:02:26 +0100613 * New uImage format and FDT blob are based on a libfdt. FDT blob
614 * may be passed directly or embedded in a FIT image. In both situations
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100615 * genimg_get_format() must be able to dectect libfdt header.
Marian Balakowicz12560682008-02-27 11:02:26 +0100616 *
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100617 * returns:
618 * image format type or IMAGE_FORMAT_INVALID if no image is present
619 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100620int genimg_get_format (void *img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100621{
622 ulong format = IMAGE_FORMAT_INVALID;
623 image_header_t *hdr;
Marian Balakowicz12560682008-02-27 11:02:26 +0100624#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100625 char *fit_hdr;
626#endif
627
628 hdr = (image_header_t *)img_addr;
629 if (image_check_magic(hdr))
630 format = IMAGE_FORMAT_LEGACY;
Marian Balakowicz12560682008-02-27 11:02:26 +0100631#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100632 else {
633 fit_hdr = (char *)img_addr;
634 if (fdt_check_header (fit_hdr) == 0)
635 format = IMAGE_FORMAT_FIT;
636 }
637#endif
638
639 return format;
640}
641
642/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100643 * genimg_get_image - get image from special storage (if necessary)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100644 * @img_addr: image start address
645 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100646 * genimg_get_image() checks if provided image start adddress is located
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100647 * in a dataflash storage. If so, image is moved to a system RAM memory.
648 *
649 * returns:
650 * image start address after possible relocation from special storage
651 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100652ulong genimg_get_image (ulong img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100653{
Marian Balakowicz535b9102008-02-27 11:00:47 +0100654 ulong ram_addr = img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100655
656#ifdef CONFIG_HAS_DATAFLASH
Marian Balakowicz535b9102008-02-27 11:00:47 +0100657 ulong h_size, d_size;
658
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100659 if (addr_dataflash (img_addr)){
Marian Balakowicz535b9102008-02-27 11:00:47 +0100660 /* ger RAM address */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100661 ram_addr = CFG_LOAD_ADDR;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100662
663 /* get header size */
664 h_size = image_get_header_size ();
665#if defined(CONFIG_FIT)
666 if (sizeof(struct fdt_header) > h_size)
667 h_size = sizeof(struct fdt_header);
668#endif
669
670 /* read in header */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100671 debug (" Reading image header from dataflash address "
672 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100673
Marian Balakowicz535b9102008-02-27 11:00:47 +0100674 read_dataflash (img_addr, h_size, (char *)ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100675
Marian Balakowicz535b9102008-02-27 11:00:47 +0100676 /* get data size */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100677 switch (genimg_get_format ((void *)ram_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100678 case IMAGE_FORMAT_LEGACY:
679 d_size = image_get_data_size ((image_header_t *)ram_addr);
680 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
681 ram_addr, d_size);
682 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100683#if defined(CONFIG_FIT)
Marian Balakowicz535b9102008-02-27 11:00:47 +0100684 case IMAGE_FORMAT_FIT:
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100685 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100686 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
687 ram_addr, d_size);
688 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100689#endif
Marian Balakowicz535b9102008-02-27 11:00:47 +0100690 default:
691 printf (" No valid image found at 0x%08lx\n", img_addr);
692 return ram_addr;
693 }
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100694
Marian Balakowicz535b9102008-02-27 11:00:47 +0100695 /* read in image data */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100696 debug (" Reading image remaining data from dataflash address "
697 "%08lx to RAM address %08lx\n", img_addr + h_size,
698 ram_addr + h_size);
699
700 read_dataflash (img_addr + h_size, d_size,
701 (char *)(ram_addr + h_size));
Marian Balakowicz535b9102008-02-27 11:00:47 +0100702
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100703 }
Marian Balakowicz535b9102008-02-27 11:00:47 +0100704#endif /* CONFIG_HAS_DATAFLASH */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100705
706 return ram_addr;
707}
708
709/**
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100710 * fit_has_config - check if there is a valid FIT configuration
711 * @images: pointer to the bootm command headers structure
712 *
713 * fit_has_config() checks if there is a FIT configuration in use
714 * (if FTI support is present).
715 *
716 * returns:
717 * 0, no FIT support or no configuration found
718 * 1, configuration found
719 */
720int genimg_has_config (bootm_headers_t *images)
721{
722#if defined(CONFIG_FIT)
723 if (images->fit_uname_cfg)
724 return 1;
725#endif
726 return 0;
727}
728
729/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100730 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz95418502008-01-31 13:55:39 +0100731 * @argc: command argument count
732 * @argv: command argument list
Marian Balakowicz61fde552008-02-27 11:01:04 +0100733 * @images: pointer to the bootm images structure
Marian Balakowicz95418502008-01-31 13:55:39 +0100734 * @arch: expected ramdisk architecture
735 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
736 * @rd_end: pointer to a ulong variable, will hold ramdisk end
737 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100738 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz95418502008-01-31 13:55:39 +0100739 * Curently supported are the following ramdisk sources:
740 * - multicomponent kernel/ramdisk image,
741 * - commandline provided address of decicated ramdisk image.
742 *
743 * returns:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100744 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz95418502008-01-31 13:55:39 +0100745 * rd_start and rd_end are set to ramdisk start/end addresses if
746 * ramdisk image is found and valid
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100747 *
748 * 1, if ramdisk image is found but corrupted
Marian Balakowicz95418502008-01-31 13:55:39 +0100749 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz95418502008-01-31 13:55:39 +0100750 */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100751int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
752 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz95418502008-01-31 13:55:39 +0100753{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100754 ulong rd_addr, rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100755 ulong rd_data, rd_len;
756 image_header_t *rd_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100757#if defined(CONFIG_FIT)
758 void *fit_hdr;
759 const char *fit_uname_config = NULL;
760 const char *fit_uname_ramdisk = NULL;
761 ulong default_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100762 int rd_noffset;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100763 int cfg_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100764 const void *data;
765 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100766#endif
Marian Balakowicz95418502008-01-31 13:55:39 +0100767
Marian Balakowicza50d5152008-03-12 10:12:37 +0100768 *rd_start = 0;
769 *rd_end = 0;
770
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100771 /*
772 * Look for a '-' which indicates to ignore the
773 * ramdisk argument
774 */
775 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
776 debug ("## Skipping init Ramdisk\n");
777 rd_len = rd_data = 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100778 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100779#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100780 if (argc >= 3) {
781 /*
782 * If the init ramdisk comes from the FIT image and
783 * the FIT image address is omitted in the command
784 * line argument, try to use os FIT image address or
785 * default load address.
786 */
787 if (images->fit_uname_os)
788 default_addr = (ulong)images->fit_hdr_os;
789 else
790 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100791
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100792 if (fit_parse_conf (argv[2], default_addr,
793 &rd_addr, &fit_uname_config)) {
794 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
795 fit_uname_config, rd_addr);
796 } else if (fit_parse_subimage (argv[2], default_addr,
797 &rd_addr, &fit_uname_ramdisk)) {
798 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
799 fit_uname_ramdisk, rd_addr);
800 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100801#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100802 {
803 rd_addr = simple_strtoul(argv[2], NULL, 16);
804 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
805 rd_addr);
806 }
807#if defined(CONFIG_FIT)
808 } else {
809 /* use FIT configuration provided in first bootm
810 * command argument
811 */
812 rd_addr = (ulong)images->fit_hdr_os;
813 fit_uname_config = images->fit_uname_cfg;
814 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
815 fit_uname_config, rd_addr);
816
817 /*
818 * Check whether configuration has ramdisk defined,
819 * if not, don't try to use it, quit silently.
820 */
821 fit_hdr = (void *)rd_addr;
822 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
823 if (cfg_noffset < 0) {
824 debug ("* ramdisk: no such config\n");
825 return 0;
826 }
827
828 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
829 if (rd_noffset < 0) {
830 debug ("* ramdisk: no ramdisk in config\n");
831 return 0;
832 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100833 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100834#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100835
836 /* copy from dataflash if needed */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100837 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100838
839 /*
840 * Check if there is an initrd image at the
841 * address provided in the second bootm argument
842 * check image type, for FIT images get FIT node.
843 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100844 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100845 case IMAGE_FORMAT_LEGACY:
Marian Balakowicza50d5152008-03-12 10:12:37 +0100846 printf ("## Loading init Ramdisk from Legacy "
847 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz95418502008-01-31 13:55:39 +0100848
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100849 show_boot_progress (9);
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100850 rd_hdr = image_get_ramdisk (rd_addr, arch,
851 images->verify);
Marian Balakowicz95418502008-01-31 13:55:39 +0100852
Marian Balakowicza50d5152008-03-12 10:12:37 +0100853 if (rd_hdr == NULL)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600854 return 1;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600855
Marian Balakowicz95418502008-01-31 13:55:39 +0100856 rd_data = image_get_data (rd_hdr);
857 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100858 rd_load = image_get_load (rd_hdr);
859 break;
860#if defined(CONFIG_FIT)
861 case IMAGE_FORMAT_FIT:
862 fit_hdr = (void *)rd_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100863 printf ("## Loading init Ramdisk from FIT "
864 "Image at %08lx ...\n", rd_addr);
865
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100866 show_boot_progress (120);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100867 if (!fit_check_format (fit_hdr)) {
868 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100869 show_boot_progress (-120);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100870 return 0;
871 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100872 show_boot_progress (121);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100873
874 if (!fit_uname_ramdisk) {
875 /*
876 * no ramdisk image node unit name, try to get config
877 * node first. If config unit node name is NULL
878 * fit_conf_get_node() will try to find default config node
879 */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100880 show_boot_progress (122);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100881 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
882 if (cfg_noffset < 0) {
883 puts ("Could not find configuration node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100884 show_boot_progress (-122);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100885 return 0;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100886 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100887 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
888 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100889
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100890 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100891 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
892 } else {
893 /* get ramdisk component image node offset */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100894 show_boot_progress (123);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100895 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
896 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100897 if (rd_noffset < 0) {
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100898 puts ("Could not find subimage node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100899 show_boot_progress (-124);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100900 return 0;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100901 }
Marian Balakowicza50d5152008-03-12 10:12:37 +0100902
903 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
904
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100905 show_boot_progress (125);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100906 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
907 return 0;
908
909 /* get ramdisk image data address and length */
910 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
911 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100912 show_boot_progress (-127);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100913 return 0;
914 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100915 show_boot_progress (128);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100916
917 rd_data = (ulong)data;
918 rd_len = size;
919
920 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
921 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100922 show_boot_progress (-129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100923 return 0;
924 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100925 show_boot_progress (129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100926
927 images->fit_hdr_rd = fit_hdr;
928 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100929 images->fit_noffset_rd = rd_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100930 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100931#endif
932 default:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100933 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowicza50d5152008-03-12 10:12:37 +0100934 rd_data = rd_len = rd_load = 0;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100935 }
Marian Balakowicz95418502008-01-31 13:55:39 +0100936
937#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100938 /*
939 * We need to copy the ramdisk to SRAM to let Linux boot
940 */
941 if (rd_data) {
942 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
943 rd_data = rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100944 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100945#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
Marian Balakowicz95418502008-01-31 13:55:39 +0100946
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100947 } else if (images->legacy_hdr_valid &&
Marian Balakowiczab00e022008-04-11 11:07:49 +0200948 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowicz95418502008-01-31 13:55:39 +0100949 /*
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100950 * Now check if we have a legacy mult-component image,
951 * get second entry data start address and len.
Marian Balakowicz95418502008-01-31 13:55:39 +0100952 */
953 show_boot_progress (13);
954 printf ("## Loading init Ramdisk from multi component "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100955 "Legacy Image at %08lx ...\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100956 (ulong)images->legacy_hdr_os);
957
958 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz95418502008-01-31 13:55:39 +0100959 } else {
960 /*
961 * no initrd image
962 */
963 show_boot_progress (14);
964 rd_len = rd_data = 0;
965 }
966
967 if (!rd_data) {
968 debug ("## No init Ramdisk\n");
Marian Balakowicz95418502008-01-31 13:55:39 +0100969 } else {
970 *rd_start = rd_data;
971 *rd_end = rd_data + rd_len;
972 }
973 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
974 *rd_start, *rd_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600975
976 return 0;
Marian Balakowicz95418502008-01-31 13:55:39 +0100977}
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100978
Daniel Hellstrombb9c8292008-03-28 08:29:26 +0100979#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100980/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100981 * boot_ramdisk_high - relocate init ramdisk
Kumar Galab937bb72008-02-27 21:51:49 -0600982 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100983 * @rd_data: ramdisk data start address
984 * @rd_len: ramdisk data length
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100985 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
986 * start address (after possible relocation)
987 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
988 * end address (after possible relocation)
989 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100990 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100991 * variable and if requested ramdisk data is moved to a specified location.
992 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100993 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
994 * start/end addresses if ramdisk image start and len were provided,
995 * otherwise set initrd_start and initrd_end set to zeros.
996 *
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100997 * returns:
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100998 * 0 - success
999 * -1 - failure
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001000 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001001int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galab937bb72008-02-27 21:51:49 -06001002 ulong *initrd_start, ulong *initrd_end)
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001003{
1004 char *s;
1005 ulong initrd_high;
1006 int initrd_copy_to_ram = 1;
1007
1008 if ((s = getenv ("initrd_high")) != NULL) {
1009 /* a value of "no" or a similar string will act like 0,
1010 * turning the "load high" feature off. This is intentional.
1011 */
1012 initrd_high = simple_strtoul (s, NULL, 16);
1013 if (initrd_high == ~0)
1014 initrd_copy_to_ram = 0;
1015 } else {
1016 /* not set, no restrictions to load high */
1017 initrd_high = ~0;
1018 }
1019
Marian Balakowicz902ce702008-05-13 15:53:29 +02001020
1021#ifdef CONFIG_LOGBUFFER
1022 /* Prevent initrd from overwriting logbuffer */
1023 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1024#endif
1025
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001026 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1027 initrd_high, initrd_copy_to_ram);
1028
1029 if (rd_data) {
1030 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1031 debug (" in-place initrd\n");
1032 *initrd_start = rd_data;
1033 *initrd_end = rd_data + rd_len;
Kumar Galab937bb72008-02-27 21:51:49 -06001034 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001035 } else {
Kumar Galab937bb72008-02-27 21:51:49 -06001036 if (initrd_high)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001037 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galab937bb72008-02-27 21:51:49 -06001038 else
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001039 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001040
Kumar Galab937bb72008-02-27 21:51:49 -06001041 if (*initrd_start == 0) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001042 puts ("ramdisk - allocation error\n");
Kumar Galab937bb72008-02-27 21:51:49 -06001043 goto error;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001044 }
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001045 show_boot_progress (12);
1046
1047 *initrd_end = *initrd_start + rd_len;
1048 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1049 *initrd_start, *initrd_end);
1050
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001051 memmove_wd ((void *)*initrd_start,
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001052 (void *)rd_data, rd_len, CHUNKSZ);
1053
1054 puts ("OK\n");
1055 }
1056 } else {
1057 *initrd_start = 0;
1058 *initrd_end = 0;
1059 }
1060 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1061 *initrd_start, *initrd_end);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001062
Kumar Galab937bb72008-02-27 21:51:49 -06001063 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001064
Kumar Galab937bb72008-02-27 21:51:49 -06001065error:
1066 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001067}
1068
1069/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001070 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galab937bb72008-02-27 21:51:49 -06001071 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001072 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1073 * @cmd_end: pointer to a ulong variable, will hold cmdline end
Kumar Galab937bb72008-02-27 21:51:49 -06001074 * @bootmap_base: ulong variable, holds offset in physical memory to
1075 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001076 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001077 * boot_get_cmdline() allocates space for kernel command line below
Kumar Galab937bb72008-02-27 21:51:49 -06001078 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001079 * variable is present its contents is copied to allocated kernel
1080 * command line.
1081 *
1082 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001083 * 0 - success
1084 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001085 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001086int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
Kumar Galab937bb72008-02-27 21:51:49 -06001087 ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001088{
1089 char *cmdline;
1090 char *s;
1091
Kumar Galab937bb72008-02-27 21:51:49 -06001092 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1093 CFG_BOOTMAPSZ + bootmap_base);
1094
1095 if (cmdline == NULL)
1096 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001097
1098 if ((s = getenv("bootargs")) == NULL)
1099 s = "";
1100
1101 strcpy(cmdline, s);
1102
1103 *cmd_start = (ulong) & cmdline[0];
1104 *cmd_end = *cmd_start + strlen(cmdline);
1105
1106 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1107
Kumar Galab937bb72008-02-27 21:51:49 -06001108 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001109}
1110
1111/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001112 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galab937bb72008-02-27 21:51:49 -06001113 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001114 * @kbd: double pointer to board info data
Kumar Galab937bb72008-02-27 21:51:49 -06001115 * @bootmap_base: ulong variable, holds offset in physical memory to
1116 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001117 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001118 * boot_get_kbd() allocates space for kernel copy of board info data below
Kumar Galab937bb72008-02-27 21:51:49 -06001119 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1120 * the current u-boot board info data.
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001121 *
1122 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001123 * 0 - success
1124 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001125 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001126int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001127{
Kumar Galab937bb72008-02-27 21:51:49 -06001128 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1129 CFG_BOOTMAPSZ + bootmap_base);
1130 if (*kbd == NULL)
1131 return -1;
1132
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001133 **kbd = *(gd->bd);
1134
1135 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1136
1137#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1138 do_bdinfo(NULL, 0, 0, NULL);
1139#endif
1140
Kumar Galab937bb72008-02-27 21:51:49 -06001141 return 0;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001142}
1143#endif /* CONFIG_PPC || CONFIG_M68K */
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001144#endif /* !USE_HOSTCC */
Marian Balakowicz95418502008-01-31 13:55:39 +01001145
Marian Balakowicz3413e222008-02-21 17:20:20 +01001146#if defined(CONFIG_FIT)
1147/*****************************************************************************/
1148/* New uImage format routines */
1149/*****************************************************************************/
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001150#ifndef USE_HOSTCC
Marian Balakowicz3413e222008-02-21 17:20:20 +01001151static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1152 ulong *addr, const char **name)
1153{
1154 const char *sep;
1155
1156 *addr = addr_curr;
1157 *name = NULL;
1158
1159 sep = strchr (spec, sepc);
1160 if (sep) {
1161 if (sep - spec > 0)
1162 *addr = simple_strtoul (spec, NULL, 16);
1163
1164 *name = sep + 1;
1165 return 1;
1166 }
1167
1168 return 0;
1169}
1170
1171/**
1172 * fit_parse_conf - parse FIT configuration spec
1173 * @spec: input string, containing configuration spec
1174 * @add_curr: current image address (to be used as a possible default)
1175 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1176 * configuration
1177 * @conf_name double pointer to a char, will hold pointer to a configuration
1178 * unit name
1179 *
1180 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1181 * where <addr> is a FIT image address that contains configuration
1182 * with a <conf> unit name.
1183 *
1184 * Address part is optional, and if omitted default add_curr will
1185 * be used instead.
1186 *
1187 * returns:
1188 * 1 if spec is a valid configuration string,
1189 * addr and conf_name are set accordingly
1190 * 0 otherwise
1191 */
1192inline int fit_parse_conf (const char *spec, ulong addr_curr,
1193 ulong *addr, const char **conf_name)
1194{
1195 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1196}
1197
1198/**
1199 * fit_parse_subimage - parse FIT subimage spec
1200 * @spec: input string, containing subimage spec
1201 * @add_curr: current image address (to be used as a possible default)
1202 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1203 * subimage
1204 * @image_name: double pointer to a char, will hold pointer to a subimage name
1205 *
1206 * fit_parse_subimage() expects subimage spec in the for of
1207 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1208 * subimage with a <subimg> unit name.
1209 *
1210 * Address part is optional, and if omitted default add_curr will
1211 * be used instead.
1212 *
1213 * returns:
1214 * 1 if spec is a valid subimage string,
1215 * addr and image_name are set accordingly
1216 * 0 otherwise
1217 */
1218inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1219 ulong *addr, const char **image_name)
1220{
1221 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1222}
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001223#endif /* !USE_HOSTCC */
1224
1225static void fit_get_debug (const void *fit, int noffset,
1226 char *prop_name, int err)
1227{
1228 debug ("Can't get '%s' property from FIT 0x%08lx, "
1229 "node: offset %d, name %s (%s)\n",
1230 prop_name, (ulong)fit, noffset,
1231 fit_get_name (fit, noffset, NULL),
1232 fdt_strerror (err));
1233}
1234
1235/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001236 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001237 * @fit: pointer to the FIT format image header
1238 * @p: pointer to prefix string
1239 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001240 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001241 * The routine prints out FIT image properties (root node level) follwed by
1242 * the details of each component image.
1243 *
1244 * returns:
1245 * no returned results
1246 */
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001247void fit_print_contents (const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001248{
1249 char *desc;
1250 char *uname;
1251 int images_noffset;
1252 int confs_noffset;
1253 int noffset;
1254 int ndepth;
1255 int count = 0;
1256 int ret;
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001257 const char *p;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001258#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1259 time_t timestamp;
1260#endif
1261
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001262#ifdef USE_HOSTCC
1263 p = "";
1264#else
1265 p = " ";
1266#endif
1267
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001268 /* Root node properties */
1269 ret = fit_get_desc (fit, 0, &desc);
1270 printf ("%sFIT description: ", p);
1271 if (ret)
1272 printf ("unavailable\n");
1273 else
1274 printf ("%s\n", desc);
1275
1276#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1277 ret = fit_get_timestamp (fit, 0, &timestamp);
1278 printf ("%sCreated: ", p);
1279 if (ret)
1280 printf ("unavailable\n");
1281 else
1282 genimg_print_time (timestamp);
1283#endif
1284
1285 /* Find images parent node offset */
1286 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1287 if (images_noffset < 0) {
1288 printf ("Can't find images parent node '%s' (%s)\n",
1289 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1290 return;
1291 }
1292
1293 /* Process its subnodes, print out component images details */
1294 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1295 (noffset >= 0) && (ndepth > 0);
1296 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1297 if (ndepth == 1) {
1298 /*
1299 * Direct child node of the images parent node,
1300 * i.e. component image node.
1301 */
1302 printf ("%s Image %u (%s)\n", p, count++,
1303 fit_get_name(fit, noffset, NULL));
1304
1305 fit_image_print (fit, noffset, p);
1306 }
1307 }
1308
1309 /* Find configurations parent node offset */
1310 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1311 if (confs_noffset < 0) {
1312 debug ("Can't get configurations parent node '%s' (%s)\n",
1313 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1314 return;
1315 }
1316
1317 /* get default configuration unit name from default property */
1318 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1319 if (uname)
1320 printf ("%s Default Configuration: '%s'\n", p, uname);
1321
1322 /* Process its subnodes, print out configurations details */
1323 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1324 (noffset >= 0) && (ndepth > 0);
1325 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1326 if (ndepth == 1) {
1327 /*
1328 * Direct child node of the configurations parent node,
1329 * i.e. configuration node.
1330 */
1331 printf ("%s Configuration %u (%s)\n", p, count++,
1332 fit_get_name(fit, noffset, NULL));
1333
1334 fit_conf_print (fit, noffset, p);
1335 }
1336 }
1337}
1338
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001339/**
1340 * fit_image_print - prints out the FIT component image details
1341 * @fit: pointer to the FIT format image header
1342 * @image_noffset: offset of the component image node
1343 * @p: pointer to prefix string
1344 *
1345 * fit_image_print() lists all mandatory properies for the processed component
1346 * image. If present, hash nodes are printed out as well.
1347 *
1348 * returns:
1349 * no returned results
1350 */
1351void fit_image_print (const void *fit, int image_noffset, const char *p)
1352{
1353 char *desc;
1354 uint8_t type, arch, os, comp;
1355 size_t size;
1356 ulong load, entry;
1357 const void *data;
1358 int noffset;
1359 int ndepth;
1360 int ret;
1361
1362 /* Mandatory properties */
1363 ret = fit_get_desc (fit, image_noffset, &desc);
1364 printf ("%s Description: ", p);
1365 if (ret)
1366 printf ("unavailable\n");
1367 else
1368 printf ("%s\n", desc);
1369
1370 fit_image_get_type (fit, image_noffset, &type);
1371 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1372
1373 fit_image_get_comp (fit, image_noffset, &comp);
1374 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1375
1376 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1377
1378#ifndef USE_HOSTCC
1379 printf ("%s Data Start: ", p);
1380 if (ret)
1381 printf ("unavailable\n");
1382 else
1383 printf ("0x%08lx\n", (ulong)data);
1384#endif
1385
1386 printf ("%s Data Size: ", p);
1387 if (ret)
1388 printf ("unavailable\n");
1389 else
1390 genimg_print_size (size);
1391
1392 /* Remaining, type dependent properties */
1393 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1394 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1395 (type == IH_TYPE_FLATDT)) {
1396 fit_image_get_arch (fit, image_noffset, &arch);
1397 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1398 }
1399
1400 if (type == IH_TYPE_KERNEL) {
1401 fit_image_get_os (fit, image_noffset, &os);
1402 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1403 }
1404
1405 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1406 ret = fit_image_get_load (fit, image_noffset, &load);
1407 printf ("%s Load Address: ", p);
1408 if (ret)
1409 printf ("unavailable\n");
1410 else
1411 printf ("0x%08lx\n", load);
1412
1413 fit_image_get_entry (fit, image_noffset, &entry);
1414 printf ("%s Entry Point: ", p);
1415 if (ret)
1416 printf ("unavailable\n");
1417 else
1418 printf ("0x%08lx\n", entry);
1419 }
1420
1421 /* Process all hash subnodes of the component image node */
1422 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1423 (noffset >= 0) && (ndepth > 0);
1424 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1425 if (ndepth == 1) {
1426 /* Direct child node of the component image node */
1427 fit_image_print_hash (fit, noffset, p);
1428 }
1429 }
1430}
1431
1432/**
1433 * fit_image_print_hash - prints out the hash node details
1434 * @fit: pointer to the FIT format image header
1435 * @noffset: offset of the hash node
1436 * @p: pointer to prefix string
1437 *
1438 * fit_image_print_hash() lists properies for the processed hash node
1439 *
1440 * returns:
1441 * no returned results
1442 */
1443void fit_image_print_hash (const void *fit, int noffset, const char *p)
1444{
1445 char *algo;
1446 uint8_t *value;
1447 int value_len;
1448 int i, ret;
1449
1450 /*
1451 * Check subnode name, must be equal to "hash".
1452 * Multiple hash nodes require unique unit node
1453 * names, e.g. hash@1, hash@2, etc.
1454 */
1455 if (strncmp (fit_get_name(fit, noffset, NULL),
1456 FIT_HASH_NODENAME,
1457 strlen(FIT_HASH_NODENAME)) != 0)
1458 return;
1459
1460 debug ("%s Hash node: '%s'\n", p,
1461 fit_get_name (fit, noffset, NULL));
1462
1463 printf ("%s Hash algo: ", p);
1464 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1465 printf ("invalid/unsupported\n");
1466 return;
1467 }
1468 printf ("%s\n", algo);
1469
1470 ret = fit_image_hash_get_value (fit, noffset, &value,
1471 &value_len);
1472 printf ("%s Hash value: ", p);
1473 if (ret) {
1474 printf ("unavailable\n");
1475 } else {
1476 for (i = 0; i < value_len; i++)
1477 printf ("%02x", value[i]);
1478 printf ("\n");
1479 }
1480
1481 debug ("%s Hash len: %d\n", p, value_len);
1482}
1483
1484/**
1485 * fit_get_desc - get node description property
1486 * @fit: pointer to the FIT format image header
1487 * @noffset: node offset
1488 * @desc: double pointer to the char, will hold pointer to the descrption
1489 *
1490 * fit_get_desc() reads description property from a given node, if
1491 * description is found pointer to it is returened in third call argument.
1492 *
1493 * returns:
1494 * 0, on success
1495 * -1, on failure
1496 */
1497int fit_get_desc (const void *fit, int noffset, char **desc)
1498{
1499 int len;
1500
1501 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1502 if (*desc == NULL) {
1503 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1504 return -1;
1505 }
1506
1507 return 0;
1508}
1509
1510/**
1511 * fit_get_timestamp - get node timestamp property
1512 * @fit: pointer to the FIT format image header
1513 * @noffset: node offset
1514 * @timestamp: pointer to the time_t, will hold read timestamp
1515 *
1516 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1517 * is found and has a correct size its value is retured in third call
1518 * argument.
1519 *
1520 * returns:
1521 * 0, on success
1522 * -1, on property read failure
1523 * -2, on wrong timestamp size
1524 */
1525int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1526{
1527 int len;
1528 const void *data;
1529
1530 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1531 if (data == NULL) {
1532 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1533 return -1;
1534 }
1535 if (len != sizeof (uint32_t)) {
1536 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1537 return -2;
1538 }
1539
1540 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1541 return 0;
1542}
1543
1544/**
1545 * fit_image_get_node - get node offset for component image of a given unit name
1546 * @fit: pointer to the FIT format image header
1547 * @image_uname: component image node unit name
1548 *
1549 * fit_image_get_node() finds a component image (withing the '/images'
1550 * node) of a provided unit name. If image is found its node offset is
1551 * returned to the caller.
1552 *
1553 * returns:
1554 * image node offset when found (>=0)
1555 * negative number on failure (FDT_ERR_* code)
1556 */
1557int fit_image_get_node (const void *fit, const char *image_uname)
1558{
1559 int noffset, images_noffset;
1560
1561 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1562 if (images_noffset < 0) {
1563 debug ("Can't find images parent node '%s' (%s)\n",
1564 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1565 return images_noffset;
1566 }
1567
1568 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1569 if (noffset < 0) {
1570 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1571 image_uname, fdt_strerror (noffset));
1572 }
1573
1574 return noffset;
1575}
1576
1577/**
1578 * fit_image_get_os - get os id for a given component image node
1579 * @fit: pointer to the FIT format image header
1580 * @noffset: component image node offset
1581 * @os: pointer to the uint8_t, will hold os numeric id
1582 *
1583 * fit_image_get_os() finds os property in a given component image node.
1584 * If the property is found, its (string) value is translated to the numeric
1585 * id which is returned to the caller.
1586 *
1587 * returns:
1588 * 0, on success
1589 * -1, on failure
1590 */
1591int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1592{
1593 int len;
1594 const void *data;
1595
1596 /* Get OS name from property data */
1597 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1598 if (data == NULL) {
1599 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1600 *os = -1;
1601 return -1;
1602 }
1603
1604 /* Translate OS name to id */
1605 *os = genimg_get_os_id (data);
1606 return 0;
1607}
1608
1609/**
1610 * fit_image_get_arch - get arch id for a given component image node
1611 * @fit: pointer to the FIT format image header
1612 * @noffset: component image node offset
1613 * @arch: pointer to the uint8_t, will hold arch numeric id
1614 *
1615 * fit_image_get_arch() finds arch property in a given component image node.
1616 * If the property is found, its (string) value is translated to the numeric
1617 * id which is returned to the caller.
1618 *
1619 * returns:
1620 * 0, on success
1621 * -1, on failure
1622 */
1623int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1624{
1625 int len;
1626 const void *data;
1627
1628 /* Get architecture name from property data */
1629 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1630 if (data == NULL) {
1631 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1632 *arch = -1;
1633 return -1;
1634 }
1635
1636 /* Translate architecture name to id */
1637 *arch = genimg_get_arch_id (data);
1638 return 0;
1639}
1640
1641/**
1642 * fit_image_get_type - get type id for a given component image node
1643 * @fit: pointer to the FIT format image header
1644 * @noffset: component image node offset
1645 * @type: pointer to the uint8_t, will hold type numeric id
1646 *
1647 * fit_image_get_type() finds type property in a given component image node.
1648 * If the property is found, its (string) value is translated to the numeric
1649 * id which is returned to the caller.
1650 *
1651 * returns:
1652 * 0, on success
1653 * -1, on failure
1654 */
1655int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1656{
1657 int len;
1658 const void *data;
1659
1660 /* Get image type name from property data */
1661 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1662 if (data == NULL) {
1663 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1664 *type = -1;
1665 return -1;
1666 }
1667
1668 /* Translate image type name to id */
1669 *type = genimg_get_type_id (data);
1670 return 0;
1671}
1672
1673/**
1674 * fit_image_get_comp - get comp id for a given component image node
1675 * @fit: pointer to the FIT format image header
1676 * @noffset: component image node offset
1677 * @comp: pointer to the uint8_t, will hold comp numeric id
1678 *
1679 * fit_image_get_comp() finds comp property in a given component image node.
1680 * If the property is found, its (string) value is translated to the numeric
1681 * id which is returned to the caller.
1682 *
1683 * returns:
1684 * 0, on success
1685 * -1, on failure
1686 */
1687int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1688{
1689 int len;
1690 const void *data;
1691
1692 /* Get compression name from property data */
1693 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1694 if (data == NULL) {
1695 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1696 *comp = -1;
1697 return -1;
1698 }
1699
1700 /* Translate compression name to id */
1701 *comp = genimg_get_comp_id (data);
1702 return 0;
1703}
1704
1705/**
1706 * fit_image_get_load - get load address property for a given component image node
1707 * @fit: pointer to the FIT format image header
1708 * @noffset: component image node offset
1709 * @load: pointer to the uint32_t, will hold load address
1710 *
1711 * fit_image_get_load() finds load address property in a given component image node.
1712 * If the property is found, its value is returned to the caller.
1713 *
1714 * returns:
1715 * 0, on success
1716 * -1, on failure
1717 */
1718int fit_image_get_load (const void *fit, int noffset, ulong *load)
1719{
1720 int len;
1721 const uint32_t *data;
1722
1723 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1724 if (data == NULL) {
1725 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1726 return -1;
1727 }
1728
1729 *load = uimage_to_cpu (*data);
1730 return 0;
1731}
1732
1733/**
1734 * fit_image_get_entry - get entry point address property for a given component image node
1735 * @fit: pointer to the FIT format image header
1736 * @noffset: component image node offset
1737 * @entry: pointer to the uint32_t, will hold entry point address
1738 *
1739 * fit_image_get_entry() finds entry point address property in a given component image node.
1740 * If the property is found, its value is returned to the caller.
1741 *
1742 * returns:
1743 * 0, on success
1744 * -1, on failure
1745 */
1746int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1747{
1748 int len;
1749 const uint32_t *data;
1750
1751 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1752 if (data == NULL) {
1753 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1754 return -1;
1755 }
1756
1757 *entry = uimage_to_cpu (*data);
1758 return 0;
1759}
1760
1761/**
1762 * fit_image_get_data - get data property and its size for a given component image node
1763 * @fit: pointer to the FIT format image header
1764 * @noffset: component image node offset
1765 * @data: double pointer to void, will hold data property's data address
1766 * @size: pointer to size_t, will hold data property's data size
1767 *
1768 * fit_image_get_data() finds data property in a given component image node.
1769 * If the property is found its data start address and size are returned to
1770 * the caller.
1771 *
1772 * returns:
1773 * 0, on success
1774 * -1, on failure
1775 */
1776int fit_image_get_data (const void *fit, int noffset,
1777 const void **data, size_t *size)
1778{
1779 int len;
1780
1781 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1782 if (*data == NULL) {
1783 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1784 *size = 0;
1785 return -1;
1786 }
1787
1788 *size = len;
1789 return 0;
1790}
1791
1792/**
1793 * fit_image_hash_get_algo - get hash algorithm name
1794 * @fit: pointer to the FIT format image header
1795 * @noffset: hash node offset
1796 * @algo: double pointer to char, will hold pointer to the algorithm name
1797 *
1798 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1799 * If the property is found its data start address is returned to the caller.
1800 *
1801 * returns:
1802 * 0, on success
1803 * -1, on failure
1804 */
1805int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1806{
1807 int len;
1808
1809 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1810 if (*algo == NULL) {
1811 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1812 return -1;
1813 }
1814
1815 return 0;
1816}
1817
1818/**
1819 * fit_image_hash_get_value - get hash value and length
1820 * @fit: pointer to the FIT format image header
1821 * @noffset: hash node offset
1822 * @value: double pointer to uint8_t, will hold address of a hash value data
1823 * @value_len: pointer to an int, will hold hash data length
1824 *
1825 * fit_image_hash_get_value() finds hash value property in a given hash node.
1826 * If the property is found its data start address and size are returned to
1827 * the caller.
1828 *
1829 * returns:
1830 * 0, on success
1831 * -1, on failure
1832 */
1833int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1834 int *value_len)
1835{
1836 int len;
1837
1838 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1839 if (*value == NULL) {
1840 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1841 *value_len = 0;
1842 return -1;
1843 }
1844
1845 *value_len = len;
1846 return 0;
1847}
1848
1849/**
1850 * fit_set_timestamp - set node timestamp property
1851 * @fit: pointer to the FIT format image header
1852 * @noffset: node offset
1853 * @timestamp: timestamp value to be set
1854 *
1855 * fit_set_timestamp() attempts to set timestamp property in the requested
1856 * node and returns operation status to the caller.
1857 *
1858 * returns:
1859 * 0, on success
1860 * -1, on property read failure
1861 */
1862int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1863{
1864 uint32_t t;
1865 int ret;
1866
1867 t = cpu_to_uimage (timestamp);
1868 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1869 sizeof (uint32_t));
1870 if (ret) {
1871 printf ("Can't set '%s' property for '%s' node (%s)\n",
1872 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1873 fdt_strerror (ret));
1874 return -1;
1875 }
1876
1877 return 0;
1878}
1879
1880/**
1881 * calculate_hash - calculate and return hash for provided input data
1882 * @data: pointer to the input data
1883 * @data_len: data length
1884 * @algo: requested hash algorithm
1885 * @value: pointer to the char, will hold hash value data (caller must
1886 * allocate enough free space)
1887 * value_len: length of the calculated hash
1888 *
1889 * calculate_hash() computes input data hash according to the requested algorithm.
1890 * Resulting hash value is placed in caller provided 'value' buffer, length
1891 * of the calculated hash is returned via value_len pointer argument.
1892 *
1893 * returns:
1894 * 0, on success
1895 * -1, when algo is unsupported
1896 */
1897static int calculate_hash (const void *data, int data_len, const char *algo,
1898 uint8_t *value, int *value_len)
1899{
1900 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02001901 *((uint32_t *)value) = crc32_wd (0, data, data_len,
1902 CHUNKSZ_CRC32);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001903 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1904 *value_len = 4;
1905 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02001906 sha1_csum_wd ((unsigned char *) data, data_len,
1907 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001908 *value_len = 20;
1909 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02001910 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka2e6f2852008-03-14 16:22:34 +01001911 *value_len = 16;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001912 } else {
1913 debug ("Unsupported hash alogrithm\n");
1914 return -1;
1915 }
1916 return 0;
1917}
1918
1919#ifdef USE_HOSTCC
1920/**
1921 * fit_set_hashes - process FIT component image nodes and calculate hashes
1922 * @fit: pointer to the FIT format image header
1923 *
1924 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1925 * Hashes are calculated for all component images which have hash subnodes
1926 * with algorithm property set to one of the supported hash algorithms.
1927 *
1928 * returns
1929 * 0, on success
1930 * libfdt error code, on failure
1931 */
1932int fit_set_hashes (void *fit)
1933{
1934 int images_noffset;
1935 int noffset;
1936 int ndepth;
1937 int ret;
1938
1939 /* Find images parent node offset */
1940 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1941 if (images_noffset < 0) {
1942 printf ("Can't find images parent node '%s' (%s)\n",
1943 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1944 return images_noffset;
1945 }
1946
1947 /* Process its subnodes, print out component images details */
1948 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1949 (noffset >= 0) && (ndepth > 0);
1950 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1951 if (ndepth == 1) {
1952 /*
1953 * Direct child node of the images parent node,
1954 * i.e. component image node.
1955 */
1956 ret = fit_image_set_hashes (fit, noffset);
1957 if (ret)
1958 return ret;
1959 }
1960 }
1961
1962 return 0;
1963}
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001964
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001965/**
1966 * fit_image_set_hashes - calculate/set hashes for given component image node
1967 * @fit: pointer to the FIT format image header
1968 * @image_noffset: requested component image node
1969 *
1970 * fit_image_set_hashes() adds hash values for an component image node. All
1971 * existing hash subnodes are checked, if algorithm property is set to one of
1972 * the supported hash algorithms, hash value is computed and corresponding
1973 * hash node property is set, for example:
1974 *
1975 * Input component image node structure:
1976 *
1977 * o image@1 (at image_noffset)
1978 * | - data = [binary data]
1979 * o hash@1
1980 * |- algo = "sha1"
1981 *
1982 * Output component image node structure:
1983 *
1984 * o image@1 (at image_noffset)
1985 * | - data = [binary data]
1986 * o hash@1
1987 * |- algo = "sha1"
1988 * |- value = sha1(data)
1989 *
1990 * returns:
1991 * 0 on sucess
1992 * <0 on failure
1993 */
1994int fit_image_set_hashes (void *fit, int image_noffset)
1995{
1996 const void *data;
1997 size_t size;
1998 char *algo;
1999 uint8_t value[FIT_MAX_HASH_LEN];
2000 int value_len;
2001 int noffset;
2002 int ndepth;
2003
2004 /* Get image data and data length */
2005 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2006 printf ("Can't get image data/size\n");
2007 return -1;
2008 }
2009
2010 /* Process all hash subnodes of the component image node */
2011 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2012 (noffset >= 0) && (ndepth > 0);
2013 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2014 if (ndepth == 1) {
2015 /* Direct child node of the component image node */
2016
2017 /*
2018 * Check subnode name, must be equal to "hash".
2019 * Multiple hash nodes require unique unit node
2020 * names, e.g. hash@1, hash@2, etc.
2021 */
2022 if (strncmp (fit_get_name(fit, noffset, NULL),
2023 FIT_HASH_NODENAME,
2024 strlen(FIT_HASH_NODENAME)) != 0) {
2025 /* Not a hash subnode, skip it */
2026 continue;
2027 }
2028
2029 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2030 printf ("Can't get hash algo property for "
2031 "'%s' hash node in '%s' image node\n",
2032 fit_get_name (fit, noffset, NULL),
2033 fit_get_name (fit, image_noffset, NULL));
2034 return -1;
2035 }
2036
2037 if (calculate_hash (data, size, algo, value, &value_len)) {
2038 printf ("Unsupported hash algorithm (%s) for "
2039 "'%s' hash node in '%s' image node\n",
2040 algo, fit_get_name (fit, noffset, NULL),
2041 fit_get_name (fit, image_noffset, NULL));
2042 return -1;
2043 }
2044
2045 if (fit_image_hash_set_value (fit, noffset, value,
2046 value_len)) {
2047 printf ("Can't set hash value for "
2048 "'%s' hash node in '%s' image node\n",
2049 fit_get_name (fit, noffset, NULL),
2050 fit_get_name (fit, image_noffset, NULL));
2051 return -1;
2052 }
2053 }
2054 }
2055
2056 return 0;
2057}
2058
2059/**
2060 * fit_image_hash_set_value - set hash value in requested has node
2061 * @fit: pointer to the FIT format image header
2062 * @noffset: hash node offset
2063 * @value: hash value to be set
2064 * @value_len: hash value length
2065 *
2066 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2067 * given and returns operation status to the caller.
2068 *
2069 * returns
2070 * 0, on success
2071 * -1, on failure
2072 */
2073int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2074 int value_len)
2075{
2076 int ret;
2077
2078 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2079 if (ret) {
2080 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2081 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2082 fdt_strerror (ret));
2083 return -1;
2084 }
2085
2086 return 0;
2087}
2088#endif /* USE_HOSTCC */
2089
2090/**
2091 * fit_image_check_hashes - verify data intergity
2092 * @fit: pointer to the FIT format image header
2093 * @image_noffset: component image node offset
2094 *
2095 * fit_image_check_hashes() goes over component image hash nodes,
2096 * re-calculates each data hash and compares with the value stored in hash
2097 * node.
2098 *
2099 * returns:
2100 * 1, if all hashes are valid
2101 * 0, otherwise (or on error)
2102 */
2103int fit_image_check_hashes (const void *fit, int image_noffset)
2104{
2105 const void *data;
2106 size_t size;
2107 char *algo;
2108 uint8_t *fit_value;
2109 int fit_value_len;
2110 uint8_t value[FIT_MAX_HASH_LEN];
2111 int value_len;
2112 int noffset;
2113 int ndepth;
2114 char *err_msg = "";
2115
2116 /* Get image data and data length */
2117 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2118 printf ("Can't get image data/size\n");
2119 return 0;
2120 }
2121
2122 /* Process all hash subnodes of the component image node */
2123 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2124 (noffset >= 0) && (ndepth > 0);
2125 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2126 if (ndepth == 1) {
2127 /* Direct child node of the component image node */
2128
2129 /*
2130 * Check subnode name, must be equal to "hash".
2131 * Multiple hash nodes require unique unit node
2132 * names, e.g. hash@1, hash@2, etc.
2133 */
2134 if (strncmp (fit_get_name(fit, noffset, NULL),
2135 FIT_HASH_NODENAME,
2136 strlen(FIT_HASH_NODENAME)) != 0)
2137 continue;
2138
2139 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2140 err_msg = "Can't get hash algo property";
2141 goto error;
2142 }
2143 printf ("%s", algo);
2144
2145 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2146 &fit_value_len)) {
2147 err_msg = "Can't get hash value property";
2148 goto error;
2149 }
2150
2151 if (calculate_hash (data, size, algo, value, &value_len)) {
2152 err_msg = "Unsupported hash algorithm";
2153 goto error;
2154 }
2155
2156 if (value_len != fit_value_len) {
2157 err_msg = "Bad hash value len";
2158 goto error;
2159 } else if (memcmp (value, fit_value, value_len) != 0) {
2160 err_msg = "Bad hash value";
2161 goto error;
2162 }
2163 printf ("+ ");
2164 }
2165 }
2166
2167 return 1;
2168
2169error:
2170 printf ("%s for '%s' hash node in '%s' image node\n",
2171 err_msg, fit_get_name (fit, noffset, NULL),
2172 fit_get_name (fit, image_noffset, NULL));
2173 return 0;
2174}
2175
2176/**
2177 * fit_image_check_os - check whether image node is of a given os type
2178 * @fit: pointer to the FIT format image header
2179 * @noffset: component image node offset
2180 * @os: requested image os
2181 *
2182 * fit_image_check_os() reads image os property and compares its numeric
2183 * id with the requested os. Comparison result is returned to the caller.
2184 *
2185 * returns:
2186 * 1 if image is of given os type
2187 * 0 otherwise (or on error)
2188 */
2189int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2190{
2191 uint8_t image_os;
2192
2193 if (fit_image_get_os (fit, noffset, &image_os))
2194 return 0;
2195 return (os == image_os);
2196}
2197
2198/**
2199 * fit_image_check_arch - check whether image node is of a given arch
2200 * @fit: pointer to the FIT format image header
2201 * @noffset: component image node offset
2202 * @arch: requested imagearch
2203 *
2204 * fit_image_check_arch() reads image arch property and compares its numeric
2205 * id with the requested arch. Comparison result is returned to the caller.
2206 *
2207 * returns:
2208 * 1 if image is of given arch
2209 * 0 otherwise (or on error)
2210 */
2211int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2212{
2213 uint8_t image_arch;
2214
2215 if (fit_image_get_arch (fit, noffset, &image_arch))
2216 return 0;
2217 return (arch == image_arch);
2218}
2219
2220/**
2221 * fit_image_check_type - check whether image node is of a given type
2222 * @fit: pointer to the FIT format image header
2223 * @noffset: component image node offset
2224 * @type: requested image type
2225 *
2226 * fit_image_check_type() reads image type property and compares its numeric
2227 * id with the requested type. Comparison result is returned to the caller.
2228 *
2229 * returns:
2230 * 1 if image is of given type
2231 * 0 otherwise (or on error)
2232 */
2233int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2234{
2235 uint8_t image_type;
2236
2237 if (fit_image_get_type (fit, noffset, &image_type))
2238 return 0;
2239 return (type == image_type);
2240}
2241
2242/**
2243 * fit_image_check_comp - check whether image node uses given compression
2244 * @fit: pointer to the FIT format image header
2245 * @noffset: component image node offset
2246 * @comp: requested image compression type
2247 *
2248 * fit_image_check_comp() reads image compression property and compares its
2249 * numeric id with the requested compression type. Comparison result is
2250 * returned to the caller.
2251 *
2252 * returns:
2253 * 1 if image uses requested compression
2254 * 0 otherwise (or on error)
2255 */
2256int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2257{
2258 uint8_t image_comp;
2259
2260 if (fit_image_get_comp (fit, noffset, &image_comp))
2261 return 0;
2262 return (comp == image_comp);
2263}
2264
2265/**
2266 * fit_check_format - sanity check FIT image format
2267 * @fit: pointer to the FIT format image header
2268 *
2269 * fit_check_format() runs a basic sanity FIT image verification.
2270 * Routine checks for mandatory properties, nodes, etc.
2271 *
2272 * returns:
2273 * 1, on success
2274 * 0, on failure
2275 */
2276int fit_check_format (const void *fit)
2277{
2278 /* mandatory / node 'description' property */
2279 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2280 debug ("Wrong FIT format: no description\n");
2281 return 0;
2282 }
2283
2284#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2285 /* mandatory / node 'timestamp' property */
2286 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2287 debug ("Wrong FIT format: no description\n");
2288 return 0;
2289 }
2290#endif
2291
2292 /* mandatory subimages parent '/images' node */
2293 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2294 debug ("Wrong FIT format: no images parent node\n");
2295 return 0;
2296 }
2297
2298 return 1;
2299}
2300
2301/**
2302 * fit_conf_get_node - get node offset for configuration of a given unit name
2303 * @fit: pointer to the FIT format image header
2304 * @conf_uname: configuration node unit name
2305 *
2306 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2307 * parant node) of a provided unit name. If configuration is found its node offset
2308 * is returned to the caller.
2309 *
2310 * When NULL is provided in second argument fit_conf_get_node() will search
2311 * for a default configuration node instead. Default configuration node unit name
2312 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2313 *
2314 * returns:
2315 * configuration node offset when found (>=0)
2316 * negative number on failure (FDT_ERR_* code)
2317 */
2318int fit_conf_get_node (const void *fit, const char *conf_uname)
2319{
2320 int noffset, confs_noffset;
2321 int len;
2322
2323 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2324 if (confs_noffset < 0) {
2325 debug ("Can't find configurations parent node '%s' (%s)\n",
2326 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2327 return confs_noffset;
2328 }
2329
2330 if (conf_uname == NULL) {
2331 /* get configuration unit name from the default property */
2332 debug ("No configuration specified, trying default...\n");
2333 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2334 if (conf_uname == NULL) {
2335 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2336 return len;
2337 }
2338 debug ("Found default configuration: '%s'\n", conf_uname);
2339 }
2340
2341 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2342 if (noffset < 0) {
2343 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2344 conf_uname, fdt_strerror (noffset));
2345 }
2346
2347 return noffset;
2348}
2349
2350static int __fit_conf_get_prop_node (const void *fit, int noffset,
2351 const char *prop_name)
2352{
2353 char *uname;
2354 int len;
2355
2356 /* get kernel image unit name from configuration kernel property */
2357 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2358 if (uname == NULL)
2359 return len;
2360
2361 return fit_image_get_node (fit, uname);
2362}
2363
2364/**
2365 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2366 * a given configuration
2367 * @fit: pointer to the FIT format image header
2368 * @noffset: configuration node offset
2369 *
2370 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2371 * configuration FIT_KERNEL_PROP property and translates it to the node
2372 * offset.
2373 *
2374 * returns:
2375 * image node offset when found (>=0)
2376 * negative number on failure (FDT_ERR_* code)
2377 */
2378int fit_conf_get_kernel_node (const void *fit, int noffset)
2379{
2380 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2381}
2382
2383/**
2384 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2385 * a given configuration
2386 * @fit: pointer to the FIT format image header
2387 * @noffset: configuration node offset
2388 *
2389 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2390 * configuration FIT_KERNEL_PROP property and translates it to the node
2391 * offset.
2392 *
2393 * returns:
2394 * image node offset when found (>=0)
2395 * negative number on failure (FDT_ERR_* code)
2396 */
2397int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2398{
2399 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2400}
2401
2402/**
2403 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2404 * a given configuration
2405 * @fit: pointer to the FIT format image header
2406 * @noffset: configuration node offset
2407 *
2408 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2409 * configuration FIT_KERNEL_PROP property and translates it to the node
2410 * offset.
2411 *
2412 * returns:
2413 * image node offset when found (>=0)
2414 * negative number on failure (FDT_ERR_* code)
2415 */
2416int fit_conf_get_fdt_node (const void *fit, int noffset)
2417{
2418 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2419}
2420
2421/**
2422 * fit_conf_print - prints out the FIT configuration details
2423 * @fit: pointer to the FIT format image header
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +01002424 * @noffset: offset of the configuration node
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002425 * @p: pointer to prefix string
2426 *
2427 * fit_conf_print() lists all mandatory properies for the processed
2428 * configuration node.
2429 *
2430 * returns:
2431 * no returned results
2432 */
2433void fit_conf_print (const void *fit, int noffset, const char *p)
2434{
2435 char *desc;
2436 char *uname;
2437 int ret;
2438
2439 /* Mandatory properties */
2440 ret = fit_get_desc (fit, noffset, &desc);
2441 printf ("%s Description: ", p);
2442 if (ret)
2443 printf ("unavailable\n");
2444 else
2445 printf ("%s\n", desc);
2446
2447 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2448 printf ("%s Kernel: ", p);
2449 if (uname == NULL)
2450 printf ("unavailable\n");
2451 else
2452 printf ("%s\n", uname);
2453
2454 /* Optional properties */
2455 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2456 if (uname)
2457 printf ("%s Init Ramdisk: %s\n", p, uname);
2458
2459 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2460 if (uname)
2461 printf ("%s FDT: %s\n", p, uname);
2462}
Marian Balakowicza50d5152008-03-12 10:12:37 +01002463
2464/**
2465 * fit_check_ramdisk - verify FIT format ramdisk subimage
2466 * @fit_hdr: pointer to the FIT ramdisk header
2467 * @rd_noffset: ramdisk subimage node offset within FIT image
2468 * @arch: requested ramdisk image architecture type
2469 * @verify: data CRC verification flag
2470 *
2471 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2472 * specified FIT image.
2473 *
2474 * returns:
2475 * 1, on success
2476 * 0, on failure
2477 */
2478#ifndef USE_HOSTCC
2479static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2480{
2481 fit_image_print (fit, rd_noffset, " ");
2482
2483 if (verify) {
2484 puts (" Verifying Hash Integrity ... ");
2485 if (!fit_image_check_hashes (fit, rd_noffset)) {
2486 puts ("Bad Data Hash\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002487 show_boot_progress (-125);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002488 return 0;
2489 }
2490 puts ("OK\n");
2491 }
2492
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002493 show_boot_progress (126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002494 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2495 !fit_image_check_arch (fit, rd_noffset, arch) ||
2496 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2497 printf ("No Linux %s Ramdisk Image\n",
2498 genimg_get_arch_name(arch));
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002499 show_boot_progress (-126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002500 return 0;
2501 }
2502
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002503 show_boot_progress (127);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002504 return 1;
2505}
2506#endif /* USE_HOSTCC */
Marian Balakowicz3413e222008-02-21 17:20:20 +01002507#endif /* CONFIG_FIT */