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