blob: 4a024d4dfb34552c0c46689b04cbece9fd3aefd2 [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 Balakowicz9c701e82008-01-31 13:57:17 +010026
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010027#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +010028#include <common.h>
29#include <watchdog.h>
30
31#ifdef CONFIG_SHOW_BOOT_PROGRESS
32#include <status_led.h>
33#endif
34
35#ifdef CONFIG_HAS_DATAFLASH
36#include <dataflash.h>
37#endif
38
Marian Balakowicz9c701e82008-01-31 13:57:17 +010039#ifdef CONFIG_LOGBUFFER
40#include <logbuff.h>
41#endif
42
Marian Balakowicza1cc1472008-02-21 17:27:41 +010043#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
44#include <rtc.h>
45#endif
46
Marian Balakowicze227fbb2008-02-29 21:24:06 +010047#include <image.h>
48
Marian Balakowicza50d5152008-03-12 10:12:37 +010049#if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010050#include <fdt.h>
51#include <libfdt.h>
52#include <fdt_support.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010053#endif
54
55#if defined(CONFIG_FIT)
Andy Fleming72c23be2008-04-02 16:19:07 -050056#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010057#include <sha1.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010058
59static int fit_check_ramdisk (const void *fit, int os_noffset,
60 uint8_t arch, int verify);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010061#endif
62
Marian Balakowicz2efc2642008-01-31 13:58:13 +010063#ifdef CONFIG_CMD_BDI
64extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
65#endif
66
67DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz61fde552008-02-27 11:01:04 +010068
Marian Balakowicza0ffb422008-03-12 10:14:38 +010069static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
70 int verify);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010071#else
Marian Balakowicz95418502008-01-31 13:55:39 +010072#include "mkimage.h"
Andy Fleming72c23be2008-04-02 16:19:07 -050073#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010074#include <time.h>
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010075#include <image.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010076#endif /* !USE_HOSTCC*/
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010077
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010078typedef struct table_entry {
79 int id; /* as defined in image.h */
80 char *sname; /* short (input) name */
81 char *lname; /* long (output) name */
82} table_entry_t;
83
84static table_entry_t uimage_arch[] = {
85 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
86 { IH_ARCH_ALPHA, "alpha", "Alpha", },
87 { IH_ARCH_ARM, "arm", "ARM", },
88 { IH_ARCH_I386, "x86", "Intel x86", },
89 { IH_ARCH_IA64, "ia64", "IA64", },
90 { IH_ARCH_M68K, "m68k", "M68K", },
91 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
92 { IH_ARCH_MIPS, "mips", "MIPS", },
93 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
94 { IH_ARCH_NIOS, "nios", "NIOS", },
95 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
96 { IH_ARCH_PPC, "ppc", "PowerPC", },
97 { IH_ARCH_S390, "s390", "IBM S390", },
98 { IH_ARCH_SH, "sh", "SuperH", },
99 { IH_ARCH_SPARC, "sparc", "SPARC", },
100 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
101 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
102 { IH_ARCH_AVR32, "avr32", "AVR32", },
103 { -1, "", "", },
104};
105
106static table_entry_t uimage_os[] = {
107 { IH_OS_INVALID, NULL, "Invalid OS", },
108#if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
109 { IH_OS_ARTOS, "artos", "ARTOS", },
110#endif
111 { IH_OS_LINUX, "linux", "Linux", },
112#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
113 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
114#endif
115 { IH_OS_NETBSD, "netbsd", "NetBSD", },
116 { IH_OS_RTEMS, "rtems", "RTEMS", },
117 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
118#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
119 { IH_OS_QNX, "qnx", "QNX", },
120 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
121#endif
122#ifdef USE_HOSTCC
123 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
124 { IH_OS_DELL, "dell", "Dell", },
125 { IH_OS_ESIX, "esix", "Esix", },
126 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
127 { IH_OS_IRIX, "irix", "Irix", },
128 { IH_OS_NCR, "ncr", "NCR", },
129 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
130 { IH_OS_PSOS, "psos", "pSOS", },
131 { IH_OS_SCO, "sco", "SCO", },
132 { IH_OS_SOLARIS, "solaris", "Solaris", },
133 { IH_OS_SVR4, "svr4", "SVR4", },
134#endif
135 { -1, "", "", },
136};
137
138static table_entry_t uimage_type[] = {
139 { IH_TYPE_INVALID, NULL, "Invalid Image", },
140 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
141 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
142 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
143 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
144 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
145 { IH_TYPE_SCRIPT, "script", "Script", },
146 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
147 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
148 { -1, "", "", },
149};
150
151static table_entry_t uimage_comp[] = {
152 { IH_COMP_NONE, "none", "uncompressed", },
153 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
154 { IH_COMP_GZIP, "gzip", "gzip compressed", },
155 { -1, "", "", },
156};
157
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;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100244 ulong offset, tail, count, img_data;
245
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;
260 tail = 0;
261
262 /* go over all indices preceding requested component idx */
263 for (i = 0; i < idx; i++) {
264 /* add up i-th component size */
Marian Balakowicza1474212008-02-29 16:00:06 +0100265 offset += uimage_to_cpu (size[i]);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100266
267 /* add up alignment for i-th component */
Marian Balakowicza1474212008-02-29 16:00:06 +0100268 tail += (4 - uimage_to_cpu (size[i]) % 4);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100269 }
270
271 /* calculate idx-th component data address */
272 *data = img_data + offset + tail;
273 } else {
274 *len = 0;
275 *data = 0;
276 }
277}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100278
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100279static void image_print_type (image_header_t *hdr)
280{
281 const char *os, *arch, *type, *comp;
282
283 os = genimg_get_os_name (image_get_os (hdr));
284 arch = genimg_get_arch_name (image_get_arch (hdr));
285 type = genimg_get_type_name (image_get_type (hdr));
286 comp = genimg_get_comp_name (image_get_comp (hdr));
287
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100288 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100289}
290
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100291/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200292 * image_print_contents - prints out the contents of the legacy format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100293 * @hdr: pointer to the legacy format image header
294 * @p: pointer to prefix string
295 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200296 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100297 * The routine prints out all header fields followed by the size/offset data
298 * for MULTI/SCRIPT images.
299 *
300 * returns:
301 * no returned results
302 */
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200303void image_print_contents (image_header_t *hdr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100304{
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200305 const char *p;
306
307#ifdef USE_HOSTCC
308 p = "";
309#else
310 p = " ";
311#endif
312
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100313 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
314#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
315 printf ("%sCreated: ", p);
316 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100317#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100318 printf ("%sImage Type: ", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100319 image_print_type (hdr);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100320 printf ("%sData Size: ", p);
321 genimg_print_size (image_get_data_size (hdr));
322 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
323 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100324
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100325 if (image_check_type (hdr, IH_TYPE_MULTI) ||
326 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100327 int i;
328 ulong data, len;
329 ulong count = image_multi_count (hdr);
330
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100331 printf ("%sContents:\n", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100332 for (i = 0; i < count; i++) {
333 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100334
335 printf ("%s Image %d: ", p, i);
336 genimg_print_size (len);
337
338 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
339 /*
340 * the user may need to know offsets
341 * if planning to do something with
342 * multiple files
343 */
344 printf ("%s Offset = 0x%08lx\n", p, data);
345 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100346 }
347 }
348}
349
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100350
351#ifndef USE_HOSTCC
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100352/**
353 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100354 * @rd_addr: ramdisk image start address
355 * @arch: expected ramdisk architecture
356 * @verify: checksum verification flag
357 *
358 * image_get_ramdisk() returns a pointer to the verified ramdisk image
359 * header. Routine receives image start address and expected architecture
360 * flag. Verification done covers data and header integrity and os/type/arch
361 * fields checking.
362 *
363 * If dataflash support is enabled routine checks for dataflash addresses
364 * and handles required dataflash reads.
365 *
366 * returns:
367 * pointer to a ramdisk image header, if image was found and valid
368 * otherwise, return NULL
369 */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100370static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
371 int verify)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100372{
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100373 image_header_t *rd_hdr = (image_header_t *)rd_addr;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100374
375 if (!image_check_magic (rd_hdr)) {
376 puts ("Bad Magic Number\n");
377 show_boot_progress (-10);
378 return NULL;
379 }
380
381 if (!image_check_hcrc (rd_hdr)) {
382 puts ("Bad Header Checksum\n");
383 show_boot_progress (-11);
384 return NULL;
385 }
386
387 show_boot_progress (10);
388 image_print_contents (rd_hdr);
389
390 if (verify) {
391 puts(" Verifying Checksum ... ");
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200392 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100393 puts ("Bad Data CRC\n");
394 show_boot_progress (-12);
395 return NULL;
396 }
397 puts("OK\n");
398 }
399
400 show_boot_progress (11);
401
402 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
403 !image_check_arch (rd_hdr, arch) ||
404 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
405 printf ("No Linux %s Ramdisk Image\n",
406 genimg_get_arch_name(arch));
407 show_boot_progress (-13);
408 return NULL;
409 }
410
411 return rd_hdr;
412}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100413#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100414
415/*****************************************************************************/
416/* Shared dual-format routines */
417/*****************************************************************************/
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100418#ifndef USE_HOSTCC
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200419int getenv_yesno (char *var)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100420{
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200421 char *s = getenv (var);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100422 return (s && (*s == 'n')) ? 0 : 1;
423}
424
425ulong getenv_bootm_low(void)
426{
427 char *s = getenv ("bootm_low");
428 if (s) {
429 ulong tmp = simple_strtoul (s, NULL, 16);
430 return tmp;
431 }
432
Marian Balakowicz2499b042008-03-12 12:14:15 +0100433#if defined(CFG_SDRAM_BASE)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100434 return CFG_SDRAM_BASE;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100435#elif defined(CONFIG_ARM)
436 return gd->bd->bi_dram[0].start;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100437#else
438 return 0;
439#endif
440}
441
442ulong getenv_bootm_size(void)
443{
444 char *s = getenv ("bootm_size");
445 if (s) {
446 ulong tmp = simple_strtoul (s, NULL, 16);
447 return tmp;
448 }
449
Marian Balakowicz2499b042008-03-12 12:14:15 +0100450#if defined(CONFIG_ARM)
451 return gd->bd->bi_dram[0].size;
452#else
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100453 return gd->bd->bi_memsize;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100454#endif
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100455}
456
457void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
458{
459#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
460 while (len > 0) {
461 size_t tail = (len > chunksz) ? chunksz : len;
462 WATCHDOG_RESET ();
463 memmove (to, from, tail);
464 to += tail;
465 from += tail;
466 len -= tail;
467 }
468#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
469 memmove (to, from, len);
470#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
471}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100472#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100473
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100474static void genimg_print_size (uint32_t size)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100475{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100476#ifndef USE_HOSTCC
477 printf ("%d Bytes = ", size);
478 print_size (size, "\n");
479#else
480 printf ("%d Bytes = %.2f kB = %.2f MB\n",
481 size, (double)size / 1.024e3,
482 (double)size / 1.048576e6);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100483#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100484}
485
486#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
487static void genimg_print_time (time_t timestamp)
488{
489#ifndef USE_HOSTCC
490 struct rtc_time tm;
491
492 to_tm (timestamp, &tm);
493 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
494 tm.tm_year, tm.tm_mon, tm.tm_mday,
495 tm.tm_hour, tm.tm_min, tm.tm_sec);
496#else
497 printf ("%s", ctime(&timestamp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100498#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100499}
500#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
501
502/**
503 * get_table_entry_name - translate entry id to long name
504 * @table: pointer to a translation table for entries of a specific type
505 * @msg: message to be returned when translation fails
506 * @id: entry id to be translated
507 *
508 * get_table_entry_name() will go over translation table trying to find
509 * entry that matches given id. If matching entry is found, its long
510 * name is returned to the caller.
511 *
512 * returns:
513 * long entry name if translation succeeds
514 * msg otherwise
515 */
516static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
517{
518 for (; table->id >= 0; ++table) {
519 if (table->id == id)
520 return (table->lname);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100521 }
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100522 return (msg);
523}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100524
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100525const char *genimg_get_os_name (uint8_t os)
526{
527 return (get_table_entry_name (uimage_os, "Unknown OS", os));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100528}
529
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100530const char *genimg_get_arch_name (uint8_t arch)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100531{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100532 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
533}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100534
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100535const char *genimg_get_type_name (uint8_t type)
536{
537 return (get_table_entry_name (uimage_type, "Unknown Image", type));
538}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100539
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100540const char *genimg_get_comp_name (uint8_t comp)
541{
542 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100543}
544
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100545/**
546 * get_table_entry_id - translate short entry name to id
547 * @table: pointer to a translation table for entries of a specific type
548 * @table_name: to be used in case of error
549 * @name: entry short name to be translated
550 *
551 * get_table_entry_id() will go over translation table trying to find
552 * entry that matches given short name. If matching entry is found,
553 * its id returned to the caller.
554 *
555 * returns:
556 * entry id if translation succeeds
557 * -1 otherwise
558 */
559static int get_table_entry_id (table_entry_t *table,
560 const char *table_name, const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100561{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100562 table_entry_t *t;
563#ifdef USE_HOSTCC
564 int first = 1;
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100565
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100566 for (t = table; t->id >= 0; ++t) {
567 if (t->sname && strcasecmp(t->sname, name) == 0)
568 return (t->id);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100569 }
570
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100571 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
572 for (t = table; t->id >= 0; ++t) {
573 if (t->sname == NULL)
574 continue;
575 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
576 first = 0;
577 }
578 fprintf (stderr, "\n");
579#else
580 for (t = table; t->id >= 0; ++t) {
581 if (t->sname && strcmp(t->sname, name) == 0)
582 return (t->id);
583 }
584 debug ("Invalid %s Type: %s\n", table_name, name);
585#endif /* USE_HOSTCC */
586 return (-1);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100587}
588
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100589int genimg_get_os_id (const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100590{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100591 return (get_table_entry_id (uimage_os, "OS", name));
592}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100593
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100594int genimg_get_arch_id (const char *name)
595{
596 return (get_table_entry_id (uimage_arch, "CPU", name));
597}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100598
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100599int genimg_get_type_id (const char *name)
600{
601 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100602}
Marian Balakowicz95418502008-01-31 13:55:39 +0100603
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100604int genimg_get_comp_id (const char *name)
605{
606 return (get_table_entry_id (uimage_comp, "Compression", name));
607}
608
609#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +0100610/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100611 * genimg_get_format - get image format type
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100612 * @img_addr: image start address
613 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100614 * genimg_get_format() checks whether provided address points to a valid
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100615 * legacy or FIT image.
616 *
Marian Balakowicz12560682008-02-27 11:02:26 +0100617 * New uImage format and FDT blob are based on a libfdt. FDT blob
618 * may be passed directly or embedded in a FIT image. In both situations
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100619 * genimg_get_format() must be able to dectect libfdt header.
Marian Balakowicz12560682008-02-27 11:02:26 +0100620 *
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100621 * returns:
622 * image format type or IMAGE_FORMAT_INVALID if no image is present
623 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100624int genimg_get_format (void *img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100625{
626 ulong format = IMAGE_FORMAT_INVALID;
627 image_header_t *hdr;
Marian Balakowicz12560682008-02-27 11:02:26 +0100628#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100629 char *fit_hdr;
630#endif
631
632 hdr = (image_header_t *)img_addr;
633 if (image_check_magic(hdr))
634 format = IMAGE_FORMAT_LEGACY;
Marian Balakowicz12560682008-02-27 11:02:26 +0100635#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100636 else {
637 fit_hdr = (char *)img_addr;
638 if (fdt_check_header (fit_hdr) == 0)
639 format = IMAGE_FORMAT_FIT;
640 }
641#endif
642
643 return format;
644}
645
646/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100647 * genimg_get_image - get image from special storage (if necessary)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100648 * @img_addr: image start address
649 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100650 * genimg_get_image() checks if provided image start adddress is located
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100651 * in a dataflash storage. If so, image is moved to a system RAM memory.
652 *
653 * returns:
654 * image start address after possible relocation from special storage
655 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100656ulong genimg_get_image (ulong img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100657{
Marian Balakowicz535b9102008-02-27 11:00:47 +0100658 ulong ram_addr = img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100659
660#ifdef CONFIG_HAS_DATAFLASH
Marian Balakowicz535b9102008-02-27 11:00:47 +0100661 ulong h_size, d_size;
662
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100663 if (addr_dataflash (img_addr)){
Marian Balakowicz535b9102008-02-27 11:00:47 +0100664 /* ger RAM address */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100665 ram_addr = CFG_LOAD_ADDR;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100666
667 /* get header size */
668 h_size = image_get_header_size ();
669#if defined(CONFIG_FIT)
670 if (sizeof(struct fdt_header) > h_size)
671 h_size = sizeof(struct fdt_header);
672#endif
673
674 /* read in header */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100675 debug (" Reading image header from dataflash address "
676 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100677
Marian Balakowicz535b9102008-02-27 11:00:47 +0100678 read_dataflash (img_addr, h_size, (char *)ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100679
Marian Balakowicz535b9102008-02-27 11:00:47 +0100680 /* get data size */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100681 switch (genimg_get_format ((void *)ram_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100682 case IMAGE_FORMAT_LEGACY:
683 d_size = image_get_data_size ((image_header_t *)ram_addr);
684 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
685 ram_addr, d_size);
686 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100687#if defined(CONFIG_FIT)
Marian Balakowicz535b9102008-02-27 11:00:47 +0100688 case IMAGE_FORMAT_FIT:
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100689 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100690 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
691 ram_addr, d_size);
692 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100693#endif
Marian Balakowicz535b9102008-02-27 11:00:47 +0100694 default:
695 printf (" No valid image found at 0x%08lx\n", img_addr);
696 return ram_addr;
697 }
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100698
Marian Balakowicz535b9102008-02-27 11:00:47 +0100699 /* read in image data */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100700 debug (" Reading image remaining data from dataflash address "
701 "%08lx to RAM address %08lx\n", img_addr + h_size,
702 ram_addr + h_size);
703
704 read_dataflash (img_addr + h_size, d_size,
705 (char *)(ram_addr + h_size));
Marian Balakowicz535b9102008-02-27 11:00:47 +0100706
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100707 }
Marian Balakowicz535b9102008-02-27 11:00:47 +0100708#endif /* CONFIG_HAS_DATAFLASH */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100709
710 return ram_addr;
711}
712
713/**
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100714 * fit_has_config - check if there is a valid FIT configuration
715 * @images: pointer to the bootm command headers structure
716 *
717 * fit_has_config() checks if there is a FIT configuration in use
718 * (if FTI support is present).
719 *
720 * returns:
721 * 0, no FIT support or no configuration found
722 * 1, configuration found
723 */
724int genimg_has_config (bootm_headers_t *images)
725{
726#if defined(CONFIG_FIT)
727 if (images->fit_uname_cfg)
728 return 1;
729#endif
730 return 0;
731}
732
733/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100734 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz95418502008-01-31 13:55:39 +0100735 * @argc: command argument count
736 * @argv: command argument list
Marian Balakowicz61fde552008-02-27 11:01:04 +0100737 * @images: pointer to the bootm images structure
Marian Balakowicz95418502008-01-31 13:55:39 +0100738 * @arch: expected ramdisk architecture
739 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
740 * @rd_end: pointer to a ulong variable, will hold ramdisk end
741 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100742 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz95418502008-01-31 13:55:39 +0100743 * Curently supported are the following ramdisk sources:
744 * - multicomponent kernel/ramdisk image,
745 * - commandline provided address of decicated ramdisk image.
746 *
747 * returns:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100748 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz95418502008-01-31 13:55:39 +0100749 * rd_start and rd_end are set to ramdisk start/end addresses if
750 * ramdisk image is found and valid
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100751 *
752 * 1, if ramdisk image is found but corrupted
Marian Balakowicz95418502008-01-31 13:55:39 +0100753 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz95418502008-01-31 13:55:39 +0100754 */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100755int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
756 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz95418502008-01-31 13:55:39 +0100757{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100758 ulong rd_addr, rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100759 ulong rd_data, rd_len;
760 image_header_t *rd_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100761#if defined(CONFIG_FIT)
762 void *fit_hdr;
763 const char *fit_uname_config = NULL;
764 const char *fit_uname_ramdisk = NULL;
765 ulong default_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100766 int rd_noffset;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100767 int cfg_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100768 const void *data;
769 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100770#endif
Marian Balakowicz95418502008-01-31 13:55:39 +0100771
Marian Balakowicza50d5152008-03-12 10:12:37 +0100772 *rd_start = 0;
773 *rd_end = 0;
774
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100775 /*
776 * Look for a '-' which indicates to ignore the
777 * ramdisk argument
778 */
779 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
780 debug ("## Skipping init Ramdisk\n");
781 rd_len = rd_data = 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100782 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100783#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100784 if (argc >= 3) {
785 /*
786 * If the init ramdisk comes from the FIT image and
787 * the FIT image address is omitted in the command
788 * line argument, try to use os FIT image address or
789 * default load address.
790 */
791 if (images->fit_uname_os)
792 default_addr = (ulong)images->fit_hdr_os;
793 else
794 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100795
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100796 if (fit_parse_conf (argv[2], default_addr,
797 &rd_addr, &fit_uname_config)) {
798 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
799 fit_uname_config, rd_addr);
800 } else if (fit_parse_subimage (argv[2], default_addr,
801 &rd_addr, &fit_uname_ramdisk)) {
802 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
803 fit_uname_ramdisk, rd_addr);
804 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100805#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100806 {
807 rd_addr = simple_strtoul(argv[2], NULL, 16);
808 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
809 rd_addr);
810 }
811#if defined(CONFIG_FIT)
812 } else {
813 /* use FIT configuration provided in first bootm
814 * command argument
815 */
816 rd_addr = (ulong)images->fit_hdr_os;
817 fit_uname_config = images->fit_uname_cfg;
818 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
819 fit_uname_config, rd_addr);
820
821 /*
822 * Check whether configuration has ramdisk defined,
823 * if not, don't try to use it, quit silently.
824 */
825 fit_hdr = (void *)rd_addr;
826 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
827 if (cfg_noffset < 0) {
828 debug ("* ramdisk: no such config\n");
829 return 0;
830 }
831
832 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
833 if (rd_noffset < 0) {
834 debug ("* ramdisk: no ramdisk in config\n");
835 return 0;
836 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100837 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100838#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100839
840 /* copy from dataflash if needed */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100841 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100842
843 /*
844 * Check if there is an initrd image at the
845 * address provided in the second bootm argument
846 * check image type, for FIT images get FIT node.
847 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100848 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100849 case IMAGE_FORMAT_LEGACY:
Marian Balakowicza50d5152008-03-12 10:12:37 +0100850 printf ("## Loading init Ramdisk from Legacy "
851 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz95418502008-01-31 13:55:39 +0100852
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100853 show_boot_progress (9);
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100854 rd_hdr = image_get_ramdisk (rd_addr, arch,
855 images->verify);
Marian Balakowicz95418502008-01-31 13:55:39 +0100856
Marian Balakowicza50d5152008-03-12 10:12:37 +0100857 if (rd_hdr == NULL)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600858 return 1;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600859
Marian Balakowicz95418502008-01-31 13:55:39 +0100860 rd_data = image_get_data (rd_hdr);
861 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100862 rd_load = image_get_load (rd_hdr);
863 break;
864#if defined(CONFIG_FIT)
865 case IMAGE_FORMAT_FIT:
866 fit_hdr = (void *)rd_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100867 printf ("## Loading init Ramdisk from FIT "
868 "Image at %08lx ...\n", rd_addr);
869
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100870 show_boot_progress (120);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100871 if (!fit_check_format (fit_hdr)) {
872 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100873 show_boot_progress (-120);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100874 return 0;
875 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100876 show_boot_progress (121);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100877
878 if (!fit_uname_ramdisk) {
879 /*
880 * no ramdisk image node unit name, try to get config
881 * node first. If config unit node name is NULL
882 * fit_conf_get_node() will try to find default config node
883 */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100884 show_boot_progress (122);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100885 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
886 if (cfg_noffset < 0) {
887 puts ("Could not find configuration node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100888 show_boot_progress (-122);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100889 return 0;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100890 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100891 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
892 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100893
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100894 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100895 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
896 } else {
897 /* get ramdisk component image node offset */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100898 show_boot_progress (123);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100899 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
900 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100901 if (rd_noffset < 0) {
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100902 puts ("Could not find subimage node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100903 show_boot_progress (-124);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100904 return 0;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100905 }
Marian Balakowicza50d5152008-03-12 10:12:37 +0100906
907 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
908
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100909 show_boot_progress (125);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100910 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
911 return 0;
912
913 /* get ramdisk image data address and length */
914 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
915 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100916 show_boot_progress (-127);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100917 return 0;
918 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100919 show_boot_progress (128);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100920
921 rd_data = (ulong)data;
922 rd_len = size;
923
924 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
925 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100926 show_boot_progress (-129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100927 return 0;
928 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100929 show_boot_progress (129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100930
931 images->fit_hdr_rd = fit_hdr;
932 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100933 images->fit_noffset_rd = rd_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100934 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100935#endif
936 default:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100937 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowicza50d5152008-03-12 10:12:37 +0100938 rd_data = rd_len = rd_load = 0;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100939 }
Marian Balakowicz95418502008-01-31 13:55:39 +0100940
941#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100942 /*
943 * We need to copy the ramdisk to SRAM to let Linux boot
944 */
945 if (rd_data) {
946 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
947 rd_data = rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100948 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100949#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
Marian Balakowicz95418502008-01-31 13:55:39 +0100950
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100951 } else if (images->legacy_hdr_valid &&
Marian Balakowiczab00e022008-04-11 11:07:49 +0200952 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowicz95418502008-01-31 13:55:39 +0100953 /*
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100954 * Now check if we have a legacy mult-component image,
955 * get second entry data start address and len.
Marian Balakowicz95418502008-01-31 13:55:39 +0100956 */
957 show_boot_progress (13);
958 printf ("## Loading init Ramdisk from multi component "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100959 "Legacy Image at %08lx ...\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100960 (ulong)images->legacy_hdr_os);
961
962 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz95418502008-01-31 13:55:39 +0100963 } else {
964 /*
965 * no initrd image
966 */
967 show_boot_progress (14);
968 rd_len = rd_data = 0;
969 }
970
971 if (!rd_data) {
972 debug ("## No init Ramdisk\n");
Marian Balakowicz95418502008-01-31 13:55:39 +0100973 } else {
974 *rd_start = rd_data;
975 *rd_end = rd_data + rd_len;
976 }
977 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
978 *rd_start, *rd_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600979
980 return 0;
Marian Balakowicz95418502008-01-31 13:55:39 +0100981}
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100982
Daniel Hellstrombb9c8292008-03-28 08:29:26 +0100983#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100984/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100985 * boot_ramdisk_high - relocate init ramdisk
Kumar Galab937bb72008-02-27 21:51:49 -0600986 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100987 * @rd_data: ramdisk data start address
988 * @rd_len: ramdisk data length
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100989 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
990 * start address (after possible relocation)
991 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
992 * end address (after possible relocation)
993 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100994 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100995 * variable and if requested ramdisk data is moved to a specified location.
996 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100997 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
998 * start/end addresses if ramdisk image start and len were provided,
999 * otherwise set initrd_start and initrd_end set to zeros.
1000 *
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001001 * returns:
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001002 * 0 - success
1003 * -1 - failure
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001004 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001005int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galab937bb72008-02-27 21:51:49 -06001006 ulong *initrd_start, ulong *initrd_end)
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001007{
1008 char *s;
1009 ulong initrd_high;
1010 int initrd_copy_to_ram = 1;
1011
1012 if ((s = getenv ("initrd_high")) != NULL) {
1013 /* a value of "no" or a similar string will act like 0,
1014 * turning the "load high" feature off. This is intentional.
1015 */
1016 initrd_high = simple_strtoul (s, NULL, 16);
1017 if (initrd_high == ~0)
1018 initrd_copy_to_ram = 0;
1019 } else {
1020 /* not set, no restrictions to load high */
1021 initrd_high = ~0;
1022 }
1023
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001024 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1025 initrd_high, initrd_copy_to_ram);
1026
1027 if (rd_data) {
1028 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1029 debug (" in-place initrd\n");
1030 *initrd_start = rd_data;
1031 *initrd_end = rd_data + rd_len;
Kumar Galab937bb72008-02-27 21:51:49 -06001032 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001033 } else {
Kumar Galab937bb72008-02-27 21:51:49 -06001034 if (initrd_high)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001035 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galab937bb72008-02-27 21:51:49 -06001036 else
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001037 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001038
Kumar Galab937bb72008-02-27 21:51:49 -06001039 if (*initrd_start == 0) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001040 puts ("ramdisk - allocation error\n");
Kumar Galab937bb72008-02-27 21:51:49 -06001041 goto error;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001042 }
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001043 show_boot_progress (12);
1044
1045 *initrd_end = *initrd_start + rd_len;
1046 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1047 *initrd_start, *initrd_end);
1048
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001049 memmove_wd ((void *)*initrd_start,
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001050 (void *)rd_data, rd_len, CHUNKSZ);
1051
1052 puts ("OK\n");
1053 }
1054 } else {
1055 *initrd_start = 0;
1056 *initrd_end = 0;
1057 }
1058 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1059 *initrd_start, *initrd_end);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001060
Kumar Galab937bb72008-02-27 21:51:49 -06001061 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001062
Kumar Galab937bb72008-02-27 21:51:49 -06001063error:
1064 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001065}
1066
1067/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001068 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galab937bb72008-02-27 21:51:49 -06001069 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001070 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1071 * @cmd_end: pointer to a ulong variable, will hold cmdline end
Kumar Galab937bb72008-02-27 21:51:49 -06001072 * @bootmap_base: ulong variable, holds offset in physical memory to
1073 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001074 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001075 * boot_get_cmdline() allocates space for kernel command line below
Kumar Galab937bb72008-02-27 21:51:49 -06001076 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001077 * variable is present its contents is copied to allocated kernel
1078 * command line.
1079 *
1080 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001081 * 0 - success
1082 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001083 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001084int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
Kumar Galab937bb72008-02-27 21:51:49 -06001085 ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001086{
1087 char *cmdline;
1088 char *s;
1089
Kumar Galab937bb72008-02-27 21:51:49 -06001090 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1091 CFG_BOOTMAPSZ + bootmap_base);
1092
1093 if (cmdline == NULL)
1094 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001095
1096 if ((s = getenv("bootargs")) == NULL)
1097 s = "";
1098
1099 strcpy(cmdline, s);
1100
1101 *cmd_start = (ulong) & cmdline[0];
1102 *cmd_end = *cmd_start + strlen(cmdline);
1103
1104 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1105
Kumar Galab937bb72008-02-27 21:51:49 -06001106 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001107}
1108
1109/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001110 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galab937bb72008-02-27 21:51:49 -06001111 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001112 * @kbd: double pointer to board info data
Kumar Galab937bb72008-02-27 21:51:49 -06001113 * @bootmap_base: ulong variable, holds offset in physical memory to
1114 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001115 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001116 * boot_get_kbd() allocates space for kernel copy of board info data below
Kumar Galab937bb72008-02-27 21:51:49 -06001117 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1118 * the current u-boot board info data.
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001119 *
1120 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001121 * 0 - success
1122 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001123 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001124int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001125{
Kumar Galab937bb72008-02-27 21:51:49 -06001126 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1127 CFG_BOOTMAPSZ + bootmap_base);
1128 if (*kbd == NULL)
1129 return -1;
1130
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001131 **kbd = *(gd->bd);
1132
1133 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1134
1135#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1136 do_bdinfo(NULL, 0, 0, NULL);
1137#endif
1138
Kumar Galab937bb72008-02-27 21:51:49 -06001139 return 0;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001140}
1141#endif /* CONFIG_PPC || CONFIG_M68K */
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001142#endif /* !USE_HOSTCC */
Marian Balakowicz95418502008-01-31 13:55:39 +01001143
Marian Balakowicz3413e222008-02-21 17:20:20 +01001144#if defined(CONFIG_FIT)
1145/*****************************************************************************/
1146/* New uImage format routines */
1147/*****************************************************************************/
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001148#ifndef USE_HOSTCC
Marian Balakowicz3413e222008-02-21 17:20:20 +01001149static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1150 ulong *addr, const char **name)
1151{
1152 const char *sep;
1153
1154 *addr = addr_curr;
1155 *name = NULL;
1156
1157 sep = strchr (spec, sepc);
1158 if (sep) {
1159 if (sep - spec > 0)
1160 *addr = simple_strtoul (spec, NULL, 16);
1161
1162 *name = sep + 1;
1163 return 1;
1164 }
1165
1166 return 0;
1167}
1168
1169/**
1170 * fit_parse_conf - parse FIT configuration spec
1171 * @spec: input string, containing configuration spec
1172 * @add_curr: current image address (to be used as a possible default)
1173 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1174 * configuration
1175 * @conf_name double pointer to a char, will hold pointer to a configuration
1176 * unit name
1177 *
1178 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1179 * where <addr> is a FIT image address that contains configuration
1180 * with a <conf> unit name.
1181 *
1182 * Address part is optional, and if omitted default add_curr will
1183 * be used instead.
1184 *
1185 * returns:
1186 * 1 if spec is a valid configuration string,
1187 * addr and conf_name are set accordingly
1188 * 0 otherwise
1189 */
1190inline int fit_parse_conf (const char *spec, ulong addr_curr,
1191 ulong *addr, const char **conf_name)
1192{
1193 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1194}
1195
1196/**
1197 * fit_parse_subimage - parse FIT subimage spec
1198 * @spec: input string, containing subimage spec
1199 * @add_curr: current image address (to be used as a possible default)
1200 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1201 * subimage
1202 * @image_name: double pointer to a char, will hold pointer to a subimage name
1203 *
1204 * fit_parse_subimage() expects subimage spec in the for of
1205 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1206 * subimage with a <subimg> unit name.
1207 *
1208 * Address part is optional, and if omitted default add_curr will
1209 * be used instead.
1210 *
1211 * returns:
1212 * 1 if spec is a valid subimage string,
1213 * addr and image_name are set accordingly
1214 * 0 otherwise
1215 */
1216inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1217 ulong *addr, const char **image_name)
1218{
1219 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1220}
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001221#endif /* !USE_HOSTCC */
1222
1223static void fit_get_debug (const void *fit, int noffset,
1224 char *prop_name, int err)
1225{
1226 debug ("Can't get '%s' property from FIT 0x%08lx, "
1227 "node: offset %d, name %s (%s)\n",
1228 prop_name, (ulong)fit, noffset,
1229 fit_get_name (fit, noffset, NULL),
1230 fdt_strerror (err));
1231}
1232
1233/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001234 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001235 * @fit: pointer to the FIT format image header
1236 * @p: pointer to prefix string
1237 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001238 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001239 * The routine prints out FIT image properties (root node level) follwed by
1240 * the details of each component image.
1241 *
1242 * returns:
1243 * no returned results
1244 */
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001245void fit_print_contents (const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001246{
1247 char *desc;
1248 char *uname;
1249 int images_noffset;
1250 int confs_noffset;
1251 int noffset;
1252 int ndepth;
1253 int count = 0;
1254 int ret;
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001255 const char *p;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001256#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1257 time_t timestamp;
1258#endif
1259
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001260#ifdef USE_HOSTCC
1261 p = "";
1262#else
1263 p = " ";
1264#endif
1265
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001266 /* Root node properties */
1267 ret = fit_get_desc (fit, 0, &desc);
1268 printf ("%sFIT description: ", p);
1269 if (ret)
1270 printf ("unavailable\n");
1271 else
1272 printf ("%s\n", desc);
1273
1274#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1275 ret = fit_get_timestamp (fit, 0, &timestamp);
1276 printf ("%sCreated: ", p);
1277 if (ret)
1278 printf ("unavailable\n");
1279 else
1280 genimg_print_time (timestamp);
1281#endif
1282
1283 /* Find images parent node offset */
1284 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1285 if (images_noffset < 0) {
1286 printf ("Can't find images parent node '%s' (%s)\n",
1287 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1288 return;
1289 }
1290
1291 /* Process its subnodes, print out component images details */
1292 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1293 (noffset >= 0) && (ndepth > 0);
1294 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1295 if (ndepth == 1) {
1296 /*
1297 * Direct child node of the images parent node,
1298 * i.e. component image node.
1299 */
1300 printf ("%s Image %u (%s)\n", p, count++,
1301 fit_get_name(fit, noffset, NULL));
1302
1303 fit_image_print (fit, noffset, p);
1304 }
1305 }
1306
1307 /* Find configurations parent node offset */
1308 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1309 if (confs_noffset < 0) {
1310 debug ("Can't get configurations parent node '%s' (%s)\n",
1311 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1312 return;
1313 }
1314
1315 /* get default configuration unit name from default property */
1316 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1317 if (uname)
1318 printf ("%s Default Configuration: '%s'\n", p, uname);
1319
1320 /* Process its subnodes, print out configurations details */
1321 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1322 (noffset >= 0) && (ndepth > 0);
1323 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1324 if (ndepth == 1) {
1325 /*
1326 * Direct child node of the configurations parent node,
1327 * i.e. configuration node.
1328 */
1329 printf ("%s Configuration %u (%s)\n", p, count++,
1330 fit_get_name(fit, noffset, NULL));
1331
1332 fit_conf_print (fit, noffset, p);
1333 }
1334 }
1335}
1336
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001337/**
1338 * fit_image_print - prints out the FIT component image details
1339 * @fit: pointer to the FIT format image header
1340 * @image_noffset: offset of the component image node
1341 * @p: pointer to prefix string
1342 *
1343 * fit_image_print() lists all mandatory properies for the processed component
1344 * image. If present, hash nodes are printed out as well.
1345 *
1346 * returns:
1347 * no returned results
1348 */
1349void fit_image_print (const void *fit, int image_noffset, const char *p)
1350{
1351 char *desc;
1352 uint8_t type, arch, os, comp;
1353 size_t size;
1354 ulong load, entry;
1355 const void *data;
1356 int noffset;
1357 int ndepth;
1358 int ret;
1359
1360 /* Mandatory properties */
1361 ret = fit_get_desc (fit, image_noffset, &desc);
1362 printf ("%s Description: ", p);
1363 if (ret)
1364 printf ("unavailable\n");
1365 else
1366 printf ("%s\n", desc);
1367
1368 fit_image_get_type (fit, image_noffset, &type);
1369 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1370
1371 fit_image_get_comp (fit, image_noffset, &comp);
1372 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1373
1374 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1375
1376#ifndef USE_HOSTCC
1377 printf ("%s Data Start: ", p);
1378 if (ret)
1379 printf ("unavailable\n");
1380 else
1381 printf ("0x%08lx\n", (ulong)data);
1382#endif
1383
1384 printf ("%s Data Size: ", p);
1385 if (ret)
1386 printf ("unavailable\n");
1387 else
1388 genimg_print_size (size);
1389
1390 /* Remaining, type dependent properties */
1391 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1392 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1393 (type == IH_TYPE_FLATDT)) {
1394 fit_image_get_arch (fit, image_noffset, &arch);
1395 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1396 }
1397
1398 if (type == IH_TYPE_KERNEL) {
1399 fit_image_get_os (fit, image_noffset, &os);
1400 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1401 }
1402
1403 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1404 ret = fit_image_get_load (fit, image_noffset, &load);
1405 printf ("%s Load Address: ", p);
1406 if (ret)
1407 printf ("unavailable\n");
1408 else
1409 printf ("0x%08lx\n", load);
1410
1411 fit_image_get_entry (fit, image_noffset, &entry);
1412 printf ("%s Entry Point: ", p);
1413 if (ret)
1414 printf ("unavailable\n");
1415 else
1416 printf ("0x%08lx\n", entry);
1417 }
1418
1419 /* Process all hash subnodes of the component image node */
1420 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1421 (noffset >= 0) && (ndepth > 0);
1422 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1423 if (ndepth == 1) {
1424 /* Direct child node of the component image node */
1425 fit_image_print_hash (fit, noffset, p);
1426 }
1427 }
1428}
1429
1430/**
1431 * fit_image_print_hash - prints out the hash node details
1432 * @fit: pointer to the FIT format image header
1433 * @noffset: offset of the hash node
1434 * @p: pointer to prefix string
1435 *
1436 * fit_image_print_hash() lists properies for the processed hash node
1437 *
1438 * returns:
1439 * no returned results
1440 */
1441void fit_image_print_hash (const void *fit, int noffset, const char *p)
1442{
1443 char *algo;
1444 uint8_t *value;
1445 int value_len;
1446 int i, ret;
1447
1448 /*
1449 * Check subnode name, must be equal to "hash".
1450 * Multiple hash nodes require unique unit node
1451 * names, e.g. hash@1, hash@2, etc.
1452 */
1453 if (strncmp (fit_get_name(fit, noffset, NULL),
1454 FIT_HASH_NODENAME,
1455 strlen(FIT_HASH_NODENAME)) != 0)
1456 return;
1457
1458 debug ("%s Hash node: '%s'\n", p,
1459 fit_get_name (fit, noffset, NULL));
1460
1461 printf ("%s Hash algo: ", p);
1462 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1463 printf ("invalid/unsupported\n");
1464 return;
1465 }
1466 printf ("%s\n", algo);
1467
1468 ret = fit_image_hash_get_value (fit, noffset, &value,
1469 &value_len);
1470 printf ("%s Hash value: ", p);
1471 if (ret) {
1472 printf ("unavailable\n");
1473 } else {
1474 for (i = 0; i < value_len; i++)
1475 printf ("%02x", value[i]);
1476 printf ("\n");
1477 }
1478
1479 debug ("%s Hash len: %d\n", p, value_len);
1480}
1481
1482/**
1483 * fit_get_desc - get node description property
1484 * @fit: pointer to the FIT format image header
1485 * @noffset: node offset
1486 * @desc: double pointer to the char, will hold pointer to the descrption
1487 *
1488 * fit_get_desc() reads description property from a given node, if
1489 * description is found pointer to it is returened in third call argument.
1490 *
1491 * returns:
1492 * 0, on success
1493 * -1, on failure
1494 */
1495int fit_get_desc (const void *fit, int noffset, char **desc)
1496{
1497 int len;
1498
1499 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1500 if (*desc == NULL) {
1501 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1502 return -1;
1503 }
1504
1505 return 0;
1506}
1507
1508/**
1509 * fit_get_timestamp - get node timestamp property
1510 * @fit: pointer to the FIT format image header
1511 * @noffset: node offset
1512 * @timestamp: pointer to the time_t, will hold read timestamp
1513 *
1514 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1515 * is found and has a correct size its value is retured in third call
1516 * argument.
1517 *
1518 * returns:
1519 * 0, on success
1520 * -1, on property read failure
1521 * -2, on wrong timestamp size
1522 */
1523int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1524{
1525 int len;
1526 const void *data;
1527
1528 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1529 if (data == NULL) {
1530 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1531 return -1;
1532 }
1533 if (len != sizeof (uint32_t)) {
1534 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1535 return -2;
1536 }
1537
1538 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1539 return 0;
1540}
1541
1542/**
1543 * fit_image_get_node - get node offset for component image of a given unit name
1544 * @fit: pointer to the FIT format image header
1545 * @image_uname: component image node unit name
1546 *
1547 * fit_image_get_node() finds a component image (withing the '/images'
1548 * node) of a provided unit name. If image is found its node offset is
1549 * returned to the caller.
1550 *
1551 * returns:
1552 * image node offset when found (>=0)
1553 * negative number on failure (FDT_ERR_* code)
1554 */
1555int fit_image_get_node (const void *fit, const char *image_uname)
1556{
1557 int noffset, images_noffset;
1558
1559 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1560 if (images_noffset < 0) {
1561 debug ("Can't find images parent node '%s' (%s)\n",
1562 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1563 return images_noffset;
1564 }
1565
1566 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1567 if (noffset < 0) {
1568 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1569 image_uname, fdt_strerror (noffset));
1570 }
1571
1572 return noffset;
1573}
1574
1575/**
1576 * fit_image_get_os - get os id for a given component image node
1577 * @fit: pointer to the FIT format image header
1578 * @noffset: component image node offset
1579 * @os: pointer to the uint8_t, will hold os numeric id
1580 *
1581 * fit_image_get_os() finds os property in a given component image node.
1582 * If the property is found, its (string) value is translated to the numeric
1583 * id which is returned to the caller.
1584 *
1585 * returns:
1586 * 0, on success
1587 * -1, on failure
1588 */
1589int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1590{
1591 int len;
1592 const void *data;
1593
1594 /* Get OS name from property data */
1595 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1596 if (data == NULL) {
1597 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1598 *os = -1;
1599 return -1;
1600 }
1601
1602 /* Translate OS name to id */
1603 *os = genimg_get_os_id (data);
1604 return 0;
1605}
1606
1607/**
1608 * fit_image_get_arch - get arch id for a given component image node
1609 * @fit: pointer to the FIT format image header
1610 * @noffset: component image node offset
1611 * @arch: pointer to the uint8_t, will hold arch numeric id
1612 *
1613 * fit_image_get_arch() finds arch property in a given component image node.
1614 * If the property is found, its (string) value is translated to the numeric
1615 * id which is returned to the caller.
1616 *
1617 * returns:
1618 * 0, on success
1619 * -1, on failure
1620 */
1621int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1622{
1623 int len;
1624 const void *data;
1625
1626 /* Get architecture name from property data */
1627 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1628 if (data == NULL) {
1629 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1630 *arch = -1;
1631 return -1;
1632 }
1633
1634 /* Translate architecture name to id */
1635 *arch = genimg_get_arch_id (data);
1636 return 0;
1637}
1638
1639/**
1640 * fit_image_get_type - get type id for a given component image node
1641 * @fit: pointer to the FIT format image header
1642 * @noffset: component image node offset
1643 * @type: pointer to the uint8_t, will hold type numeric id
1644 *
1645 * fit_image_get_type() finds type property in a given component image node.
1646 * If the property is found, its (string) value is translated to the numeric
1647 * id which is returned to the caller.
1648 *
1649 * returns:
1650 * 0, on success
1651 * -1, on failure
1652 */
1653int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1654{
1655 int len;
1656 const void *data;
1657
1658 /* Get image type name from property data */
1659 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1660 if (data == NULL) {
1661 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1662 *type = -1;
1663 return -1;
1664 }
1665
1666 /* Translate image type name to id */
1667 *type = genimg_get_type_id (data);
1668 return 0;
1669}
1670
1671/**
1672 * fit_image_get_comp - get comp id for a given component image node
1673 * @fit: pointer to the FIT format image header
1674 * @noffset: component image node offset
1675 * @comp: pointer to the uint8_t, will hold comp numeric id
1676 *
1677 * fit_image_get_comp() finds comp property in a given component image node.
1678 * If the property is found, its (string) value is translated to the numeric
1679 * id which is returned to the caller.
1680 *
1681 * returns:
1682 * 0, on success
1683 * -1, on failure
1684 */
1685int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1686{
1687 int len;
1688 const void *data;
1689
1690 /* Get compression name from property data */
1691 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1692 if (data == NULL) {
1693 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1694 *comp = -1;
1695 return -1;
1696 }
1697
1698 /* Translate compression name to id */
1699 *comp = genimg_get_comp_id (data);
1700 return 0;
1701}
1702
1703/**
1704 * fit_image_get_load - get load address property for a given component image node
1705 * @fit: pointer to the FIT format image header
1706 * @noffset: component image node offset
1707 * @load: pointer to the uint32_t, will hold load address
1708 *
1709 * fit_image_get_load() finds load address property in a given component image node.
1710 * If the property is found, its value is returned to the caller.
1711 *
1712 * returns:
1713 * 0, on success
1714 * -1, on failure
1715 */
1716int fit_image_get_load (const void *fit, int noffset, ulong *load)
1717{
1718 int len;
1719 const uint32_t *data;
1720
1721 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1722 if (data == NULL) {
1723 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1724 return -1;
1725 }
1726
1727 *load = uimage_to_cpu (*data);
1728 return 0;
1729}
1730
1731/**
1732 * fit_image_get_entry - get entry point address property for a given component image node
1733 * @fit: pointer to the FIT format image header
1734 * @noffset: component image node offset
1735 * @entry: pointer to the uint32_t, will hold entry point address
1736 *
1737 * fit_image_get_entry() finds entry point address property in a given component image node.
1738 * If the property is found, its value is returned to the caller.
1739 *
1740 * returns:
1741 * 0, on success
1742 * -1, on failure
1743 */
1744int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1745{
1746 int len;
1747 const uint32_t *data;
1748
1749 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1750 if (data == NULL) {
1751 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1752 return -1;
1753 }
1754
1755 *entry = uimage_to_cpu (*data);
1756 return 0;
1757}
1758
1759/**
1760 * fit_image_get_data - get data property and its size for a given component image node
1761 * @fit: pointer to the FIT format image header
1762 * @noffset: component image node offset
1763 * @data: double pointer to void, will hold data property's data address
1764 * @size: pointer to size_t, will hold data property's data size
1765 *
1766 * fit_image_get_data() finds data property in a given component image node.
1767 * If the property is found its data start address and size are returned to
1768 * the caller.
1769 *
1770 * returns:
1771 * 0, on success
1772 * -1, on failure
1773 */
1774int fit_image_get_data (const void *fit, int noffset,
1775 const void **data, size_t *size)
1776{
1777 int len;
1778
1779 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1780 if (*data == NULL) {
1781 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1782 *size = 0;
1783 return -1;
1784 }
1785
1786 *size = len;
1787 return 0;
1788}
1789
1790/**
1791 * fit_image_hash_get_algo - get hash algorithm name
1792 * @fit: pointer to the FIT format image header
1793 * @noffset: hash node offset
1794 * @algo: double pointer to char, will hold pointer to the algorithm name
1795 *
1796 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1797 * If the property is found its data start address is returned to the caller.
1798 *
1799 * returns:
1800 * 0, on success
1801 * -1, on failure
1802 */
1803int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1804{
1805 int len;
1806
1807 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1808 if (*algo == NULL) {
1809 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1810 return -1;
1811 }
1812
1813 return 0;
1814}
1815
1816/**
1817 * fit_image_hash_get_value - get hash value and length
1818 * @fit: pointer to the FIT format image header
1819 * @noffset: hash node offset
1820 * @value: double pointer to uint8_t, will hold address of a hash value data
1821 * @value_len: pointer to an int, will hold hash data length
1822 *
1823 * fit_image_hash_get_value() finds hash value property in a given hash node.
1824 * If the property is found its data start address and size are returned to
1825 * the caller.
1826 *
1827 * returns:
1828 * 0, on success
1829 * -1, on failure
1830 */
1831int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1832 int *value_len)
1833{
1834 int len;
1835
1836 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1837 if (*value == NULL) {
1838 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1839 *value_len = 0;
1840 return -1;
1841 }
1842
1843 *value_len = len;
1844 return 0;
1845}
1846
1847/**
1848 * fit_set_timestamp - set node timestamp property
1849 * @fit: pointer to the FIT format image header
1850 * @noffset: node offset
1851 * @timestamp: timestamp value to be set
1852 *
1853 * fit_set_timestamp() attempts to set timestamp property in the requested
1854 * node and returns operation status to the caller.
1855 *
1856 * returns:
1857 * 0, on success
1858 * -1, on property read failure
1859 */
1860int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1861{
1862 uint32_t t;
1863 int ret;
1864
1865 t = cpu_to_uimage (timestamp);
1866 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1867 sizeof (uint32_t));
1868 if (ret) {
1869 printf ("Can't set '%s' property for '%s' node (%s)\n",
1870 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1871 fdt_strerror (ret));
1872 return -1;
1873 }
1874
1875 return 0;
1876}
1877
1878/**
1879 * calculate_hash - calculate and return hash for provided input data
1880 * @data: pointer to the input data
1881 * @data_len: data length
1882 * @algo: requested hash algorithm
1883 * @value: pointer to the char, will hold hash value data (caller must
1884 * allocate enough free space)
1885 * value_len: length of the calculated hash
1886 *
1887 * calculate_hash() computes input data hash according to the requested algorithm.
1888 * Resulting hash value is placed in caller provided 'value' buffer, length
1889 * of the calculated hash is returned via value_len pointer argument.
1890 *
1891 * returns:
1892 * 0, on success
1893 * -1, when algo is unsupported
1894 */
1895static int calculate_hash (const void *data, int data_len, const char *algo,
1896 uint8_t *value, int *value_len)
1897{
1898 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02001899 *((uint32_t *)value) = crc32_wd (0, data, data_len,
1900 CHUNKSZ_CRC32);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001901 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1902 *value_len = 4;
1903 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02001904 sha1_csum_wd ((unsigned char *) data, data_len,
1905 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001906 *value_len = 20;
1907 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02001908 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka2e6f2852008-03-14 16:22:34 +01001909 *value_len = 16;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001910 } else {
1911 debug ("Unsupported hash alogrithm\n");
1912 return -1;
1913 }
1914 return 0;
1915}
1916
1917#ifdef USE_HOSTCC
1918/**
1919 * fit_set_hashes - process FIT component image nodes and calculate hashes
1920 * @fit: pointer to the FIT format image header
1921 *
1922 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1923 * Hashes are calculated for all component images which have hash subnodes
1924 * with algorithm property set to one of the supported hash algorithms.
1925 *
1926 * returns
1927 * 0, on success
1928 * libfdt error code, on failure
1929 */
1930int fit_set_hashes (void *fit)
1931{
1932 int images_noffset;
1933 int noffset;
1934 int ndepth;
1935 int ret;
1936
1937 /* Find images parent node offset */
1938 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1939 if (images_noffset < 0) {
1940 printf ("Can't find images parent node '%s' (%s)\n",
1941 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1942 return images_noffset;
1943 }
1944
1945 /* Process its subnodes, print out component images details */
1946 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1947 (noffset >= 0) && (ndepth > 0);
1948 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1949 if (ndepth == 1) {
1950 /*
1951 * Direct child node of the images parent node,
1952 * i.e. component image node.
1953 */
1954 ret = fit_image_set_hashes (fit, noffset);
1955 if (ret)
1956 return ret;
1957 }
1958 }
1959
1960 return 0;
1961}
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001962
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001963/**
1964 * fit_image_set_hashes - calculate/set hashes for given component image node
1965 * @fit: pointer to the FIT format image header
1966 * @image_noffset: requested component image node
1967 *
1968 * fit_image_set_hashes() adds hash values for an component image node. All
1969 * existing hash subnodes are checked, if algorithm property is set to one of
1970 * the supported hash algorithms, hash value is computed and corresponding
1971 * hash node property is set, for example:
1972 *
1973 * Input component image node structure:
1974 *
1975 * o image@1 (at image_noffset)
1976 * | - data = [binary data]
1977 * o hash@1
1978 * |- algo = "sha1"
1979 *
1980 * Output component image node structure:
1981 *
1982 * o image@1 (at image_noffset)
1983 * | - data = [binary data]
1984 * o hash@1
1985 * |- algo = "sha1"
1986 * |- value = sha1(data)
1987 *
1988 * returns:
1989 * 0 on sucess
1990 * <0 on failure
1991 */
1992int fit_image_set_hashes (void *fit, int image_noffset)
1993{
1994 const void *data;
1995 size_t size;
1996 char *algo;
1997 uint8_t value[FIT_MAX_HASH_LEN];
1998 int value_len;
1999 int noffset;
2000 int ndepth;
2001
2002 /* Get image data and data length */
2003 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2004 printf ("Can't get image data/size\n");
2005 return -1;
2006 }
2007
2008 /* Process all hash subnodes of the component image node */
2009 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2010 (noffset >= 0) && (ndepth > 0);
2011 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2012 if (ndepth == 1) {
2013 /* Direct child node of the component image node */
2014
2015 /*
2016 * Check subnode name, must be equal to "hash".
2017 * Multiple hash nodes require unique unit node
2018 * names, e.g. hash@1, hash@2, etc.
2019 */
2020 if (strncmp (fit_get_name(fit, noffset, NULL),
2021 FIT_HASH_NODENAME,
2022 strlen(FIT_HASH_NODENAME)) != 0) {
2023 /* Not a hash subnode, skip it */
2024 continue;
2025 }
2026
2027 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2028 printf ("Can't get hash algo property for "
2029 "'%s' hash node in '%s' image node\n",
2030 fit_get_name (fit, noffset, NULL),
2031 fit_get_name (fit, image_noffset, NULL));
2032 return -1;
2033 }
2034
2035 if (calculate_hash (data, size, algo, value, &value_len)) {
2036 printf ("Unsupported hash algorithm (%s) for "
2037 "'%s' hash node in '%s' image node\n",
2038 algo, fit_get_name (fit, noffset, NULL),
2039 fit_get_name (fit, image_noffset, NULL));
2040 return -1;
2041 }
2042
2043 if (fit_image_hash_set_value (fit, noffset, value,
2044 value_len)) {
2045 printf ("Can't set hash value for "
2046 "'%s' hash node in '%s' image node\n",
2047 fit_get_name (fit, noffset, NULL),
2048 fit_get_name (fit, image_noffset, NULL));
2049 return -1;
2050 }
2051 }
2052 }
2053
2054 return 0;
2055}
2056
2057/**
2058 * fit_image_hash_set_value - set hash value in requested has node
2059 * @fit: pointer to the FIT format image header
2060 * @noffset: hash node offset
2061 * @value: hash value to be set
2062 * @value_len: hash value length
2063 *
2064 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2065 * given and returns operation status to the caller.
2066 *
2067 * returns
2068 * 0, on success
2069 * -1, on failure
2070 */
2071int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2072 int value_len)
2073{
2074 int ret;
2075
2076 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2077 if (ret) {
2078 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2079 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2080 fdt_strerror (ret));
2081 return -1;
2082 }
2083
2084 return 0;
2085}
2086#endif /* USE_HOSTCC */
2087
2088/**
2089 * fit_image_check_hashes - verify data intergity
2090 * @fit: pointer to the FIT format image header
2091 * @image_noffset: component image node offset
2092 *
2093 * fit_image_check_hashes() goes over component image hash nodes,
2094 * re-calculates each data hash and compares with the value stored in hash
2095 * node.
2096 *
2097 * returns:
2098 * 1, if all hashes are valid
2099 * 0, otherwise (or on error)
2100 */
2101int fit_image_check_hashes (const void *fit, int image_noffset)
2102{
2103 const void *data;
2104 size_t size;
2105 char *algo;
2106 uint8_t *fit_value;
2107 int fit_value_len;
2108 uint8_t value[FIT_MAX_HASH_LEN];
2109 int value_len;
2110 int noffset;
2111 int ndepth;
2112 char *err_msg = "";
2113
2114 /* Get image data and data length */
2115 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2116 printf ("Can't get image data/size\n");
2117 return 0;
2118 }
2119
2120 /* Process all hash subnodes of the component image node */
2121 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2122 (noffset >= 0) && (ndepth > 0);
2123 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2124 if (ndepth == 1) {
2125 /* Direct child node of the component image node */
2126
2127 /*
2128 * Check subnode name, must be equal to "hash".
2129 * Multiple hash nodes require unique unit node
2130 * names, e.g. hash@1, hash@2, etc.
2131 */
2132 if (strncmp (fit_get_name(fit, noffset, NULL),
2133 FIT_HASH_NODENAME,
2134 strlen(FIT_HASH_NODENAME)) != 0)
2135 continue;
2136
2137 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2138 err_msg = "Can't get hash algo property";
2139 goto error;
2140 }
2141 printf ("%s", algo);
2142
2143 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2144 &fit_value_len)) {
2145 err_msg = "Can't get hash value property";
2146 goto error;
2147 }
2148
2149 if (calculate_hash (data, size, algo, value, &value_len)) {
2150 err_msg = "Unsupported hash algorithm";
2151 goto error;
2152 }
2153
2154 if (value_len != fit_value_len) {
2155 err_msg = "Bad hash value len";
2156 goto error;
2157 } else if (memcmp (value, fit_value, value_len) != 0) {
2158 err_msg = "Bad hash value";
2159 goto error;
2160 }
2161 printf ("+ ");
2162 }
2163 }
2164
2165 return 1;
2166
2167error:
2168 printf ("%s for '%s' hash node in '%s' image node\n",
2169 err_msg, fit_get_name (fit, noffset, NULL),
2170 fit_get_name (fit, image_noffset, NULL));
2171 return 0;
2172}
2173
2174/**
2175 * fit_image_check_os - check whether image node is of a given os type
2176 * @fit: pointer to the FIT format image header
2177 * @noffset: component image node offset
2178 * @os: requested image os
2179 *
2180 * fit_image_check_os() reads image os property and compares its numeric
2181 * id with the requested os. Comparison result is returned to the caller.
2182 *
2183 * returns:
2184 * 1 if image is of given os type
2185 * 0 otherwise (or on error)
2186 */
2187int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2188{
2189 uint8_t image_os;
2190
2191 if (fit_image_get_os (fit, noffset, &image_os))
2192 return 0;
2193 return (os == image_os);
2194}
2195
2196/**
2197 * fit_image_check_arch - check whether image node is of a given arch
2198 * @fit: pointer to the FIT format image header
2199 * @noffset: component image node offset
2200 * @arch: requested imagearch
2201 *
2202 * fit_image_check_arch() reads image arch property and compares its numeric
2203 * id with the requested arch. Comparison result is returned to the caller.
2204 *
2205 * returns:
2206 * 1 if image is of given arch
2207 * 0 otherwise (or on error)
2208 */
2209int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2210{
2211 uint8_t image_arch;
2212
2213 if (fit_image_get_arch (fit, noffset, &image_arch))
2214 return 0;
2215 return (arch == image_arch);
2216}
2217
2218/**
2219 * fit_image_check_type - check whether image node is of a given type
2220 * @fit: pointer to the FIT format image header
2221 * @noffset: component image node offset
2222 * @type: requested image type
2223 *
2224 * fit_image_check_type() reads image type property and compares its numeric
2225 * id with the requested type. Comparison result is returned to the caller.
2226 *
2227 * returns:
2228 * 1 if image is of given type
2229 * 0 otherwise (or on error)
2230 */
2231int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2232{
2233 uint8_t image_type;
2234
2235 if (fit_image_get_type (fit, noffset, &image_type))
2236 return 0;
2237 return (type == image_type);
2238}
2239
2240/**
2241 * fit_image_check_comp - check whether image node uses given compression
2242 * @fit: pointer to the FIT format image header
2243 * @noffset: component image node offset
2244 * @comp: requested image compression type
2245 *
2246 * fit_image_check_comp() reads image compression property and compares its
2247 * numeric id with the requested compression type. Comparison result is
2248 * returned to the caller.
2249 *
2250 * returns:
2251 * 1 if image uses requested compression
2252 * 0 otherwise (or on error)
2253 */
2254int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2255{
2256 uint8_t image_comp;
2257
2258 if (fit_image_get_comp (fit, noffset, &image_comp))
2259 return 0;
2260 return (comp == image_comp);
2261}
2262
2263/**
2264 * fit_check_format - sanity check FIT image format
2265 * @fit: pointer to the FIT format image header
2266 *
2267 * fit_check_format() runs a basic sanity FIT image verification.
2268 * Routine checks for mandatory properties, nodes, etc.
2269 *
2270 * returns:
2271 * 1, on success
2272 * 0, on failure
2273 */
2274int fit_check_format (const void *fit)
2275{
2276 /* mandatory / node 'description' property */
2277 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2278 debug ("Wrong FIT format: no description\n");
2279 return 0;
2280 }
2281
2282#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2283 /* mandatory / node 'timestamp' property */
2284 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2285 debug ("Wrong FIT format: no description\n");
2286 return 0;
2287 }
2288#endif
2289
2290 /* mandatory subimages parent '/images' node */
2291 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2292 debug ("Wrong FIT format: no images parent node\n");
2293 return 0;
2294 }
2295
2296 return 1;
2297}
2298
2299/**
2300 * fit_conf_get_node - get node offset for configuration of a given unit name
2301 * @fit: pointer to the FIT format image header
2302 * @conf_uname: configuration node unit name
2303 *
2304 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2305 * parant node) of a provided unit name. If configuration is found its node offset
2306 * is returned to the caller.
2307 *
2308 * When NULL is provided in second argument fit_conf_get_node() will search
2309 * for a default configuration node instead. Default configuration node unit name
2310 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2311 *
2312 * returns:
2313 * configuration node offset when found (>=0)
2314 * negative number on failure (FDT_ERR_* code)
2315 */
2316int fit_conf_get_node (const void *fit, const char *conf_uname)
2317{
2318 int noffset, confs_noffset;
2319 int len;
2320
2321 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2322 if (confs_noffset < 0) {
2323 debug ("Can't find configurations parent node '%s' (%s)\n",
2324 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2325 return confs_noffset;
2326 }
2327
2328 if (conf_uname == NULL) {
2329 /* get configuration unit name from the default property */
2330 debug ("No configuration specified, trying default...\n");
2331 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2332 if (conf_uname == NULL) {
2333 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2334 return len;
2335 }
2336 debug ("Found default configuration: '%s'\n", conf_uname);
2337 }
2338
2339 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2340 if (noffset < 0) {
2341 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2342 conf_uname, fdt_strerror (noffset));
2343 }
2344
2345 return noffset;
2346}
2347
2348static int __fit_conf_get_prop_node (const void *fit, int noffset,
2349 const char *prop_name)
2350{
2351 char *uname;
2352 int len;
2353
2354 /* get kernel image unit name from configuration kernel property */
2355 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2356 if (uname == NULL)
2357 return len;
2358
2359 return fit_image_get_node (fit, uname);
2360}
2361
2362/**
2363 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2364 * a given configuration
2365 * @fit: pointer to the FIT format image header
2366 * @noffset: configuration node offset
2367 *
2368 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2369 * configuration FIT_KERNEL_PROP property and translates it to the node
2370 * offset.
2371 *
2372 * returns:
2373 * image node offset when found (>=0)
2374 * negative number on failure (FDT_ERR_* code)
2375 */
2376int fit_conf_get_kernel_node (const void *fit, int noffset)
2377{
2378 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2379}
2380
2381/**
2382 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2383 * a given configuration
2384 * @fit: pointer to the FIT format image header
2385 * @noffset: configuration node offset
2386 *
2387 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2388 * configuration FIT_KERNEL_PROP property and translates it to the node
2389 * offset.
2390 *
2391 * returns:
2392 * image node offset when found (>=0)
2393 * negative number on failure (FDT_ERR_* code)
2394 */
2395int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2396{
2397 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2398}
2399
2400/**
2401 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2402 * a given configuration
2403 * @fit: pointer to the FIT format image header
2404 * @noffset: configuration node offset
2405 *
2406 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2407 * configuration FIT_KERNEL_PROP property and translates it to the node
2408 * offset.
2409 *
2410 * returns:
2411 * image node offset when found (>=0)
2412 * negative number on failure (FDT_ERR_* code)
2413 */
2414int fit_conf_get_fdt_node (const void *fit, int noffset)
2415{
2416 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2417}
2418
2419/**
2420 * fit_conf_print - prints out the FIT configuration details
2421 * @fit: pointer to the FIT format image header
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +01002422 * @noffset: offset of the configuration node
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002423 * @p: pointer to prefix string
2424 *
2425 * fit_conf_print() lists all mandatory properies for the processed
2426 * configuration node.
2427 *
2428 * returns:
2429 * no returned results
2430 */
2431void fit_conf_print (const void *fit, int noffset, const char *p)
2432{
2433 char *desc;
2434 char *uname;
2435 int ret;
2436
2437 /* Mandatory properties */
2438 ret = fit_get_desc (fit, noffset, &desc);
2439 printf ("%s Description: ", p);
2440 if (ret)
2441 printf ("unavailable\n");
2442 else
2443 printf ("%s\n", desc);
2444
2445 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2446 printf ("%s Kernel: ", p);
2447 if (uname == NULL)
2448 printf ("unavailable\n");
2449 else
2450 printf ("%s\n", uname);
2451
2452 /* Optional properties */
2453 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2454 if (uname)
2455 printf ("%s Init Ramdisk: %s\n", p, uname);
2456
2457 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2458 if (uname)
2459 printf ("%s FDT: %s\n", p, uname);
2460}
Marian Balakowicza50d5152008-03-12 10:12:37 +01002461
2462/**
2463 * fit_check_ramdisk - verify FIT format ramdisk subimage
2464 * @fit_hdr: pointer to the FIT ramdisk header
2465 * @rd_noffset: ramdisk subimage node offset within FIT image
2466 * @arch: requested ramdisk image architecture type
2467 * @verify: data CRC verification flag
2468 *
2469 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2470 * specified FIT image.
2471 *
2472 * returns:
2473 * 1, on success
2474 * 0, on failure
2475 */
2476#ifndef USE_HOSTCC
2477static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2478{
2479 fit_image_print (fit, rd_noffset, " ");
2480
2481 if (verify) {
2482 puts (" Verifying Hash Integrity ... ");
2483 if (!fit_image_check_hashes (fit, rd_noffset)) {
2484 puts ("Bad Data Hash\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002485 show_boot_progress (-125);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002486 return 0;
2487 }
2488 puts ("OK\n");
2489 }
2490
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002491 show_boot_progress (126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002492 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2493 !fit_image_check_arch (fit, rd_noffset, arch) ||
2494 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2495 printf ("No Linux %s Ramdisk Image\n",
2496 genimg_get_arch_name(arch));
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002497 show_boot_progress (-126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002498 return 0;
2499 }
2500
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002501 show_boot_progress (127);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002502 return 1;
2503}
2504#endif /* USE_HOSTCC */
Marian Balakowicz3413e222008-02-21 17:20:20 +01002505#endif /* CONFIG_FIT */