blob: 67e594df6939056801e2003d710f87c044277eda [file] [log] [blame]
Marian Balakowicz41d71ed2008-01-08 18:14:09 +01001/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
Marian Balakowicz9c701e82008-01-31 13:57:17 +010025
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010026#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +010027#include <common.h>
28#include <watchdog.h>
29
30#ifdef CONFIG_SHOW_BOOT_PROGRESS
31#include <status_led.h>
32#endif
33
34#ifdef CONFIG_HAS_DATAFLASH
35#include <dataflash.h>
36#endif
37
Marian Balakowicza1cc1472008-02-21 17:27:41 +010038#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
39#include <rtc.h>
40#endif
41
Marian Balakowicze227fbb2008-02-29 21:24:06 +010042#include <image.h>
43
Marian Balakowicza50d5152008-03-12 10:12:37 +010044#if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010045#include <fdt.h>
46#include <libfdt.h>
47#include <fdt_support.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010048#endif
49
50#if defined(CONFIG_FIT)
Andy Fleming72c23be2008-04-02 16:19:07 -050051#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010052#include <sha1.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010053
54static int fit_check_ramdisk (const void *fit, int os_noffset,
55 uint8_t arch, int verify);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010056#endif
57
Marian Balakowicz2efc2642008-01-31 13:58:13 +010058#ifdef CONFIG_CMD_BDI
59extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
60#endif
61
62DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz61fde552008-02-27 11:01:04 +010063
Marian Balakowicza0ffb422008-03-12 10:14:38 +010064static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
65 int verify);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010066#else
Marian Balakowicz95418502008-01-31 13:55:39 +010067#include "mkimage.h"
Andy Fleming72c23be2008-04-02 16:19:07 -050068#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010069#include <time.h>
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010070#include <image.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010071#endif /* !USE_HOSTCC*/
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010072
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010073typedef struct table_entry {
74 int id; /* as defined in image.h */
75 char *sname; /* short (input) name */
76 char *lname; /* long (output) name */
77} table_entry_t;
78
79static table_entry_t uimage_arch[] = {
80 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
81 { IH_ARCH_ALPHA, "alpha", "Alpha", },
82 { IH_ARCH_ARM, "arm", "ARM", },
83 { IH_ARCH_I386, "x86", "Intel x86", },
84 { IH_ARCH_IA64, "ia64", "IA64", },
85 { IH_ARCH_M68K, "m68k", "M68K", },
86 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
87 { IH_ARCH_MIPS, "mips", "MIPS", },
88 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
89 { IH_ARCH_NIOS, "nios", "NIOS", },
90 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Erickson80f51b82008-05-04 16:45:01 -070091 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010092 { IH_ARCH_PPC, "ppc", "PowerPC", },
93 { IH_ARCH_S390, "s390", "IBM S390", },
94 { IH_ARCH_SH, "sh", "SuperH", },
95 { IH_ARCH_SPARC, "sparc", "SPARC", },
96 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
97 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
98 { IH_ARCH_AVR32, "avr32", "AVR32", },
99 { -1, "", "", },
100};
101
102static table_entry_t uimage_os[] = {
103 { IH_OS_INVALID, NULL, "Invalid OS", },
104#if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
105 { IH_OS_ARTOS, "artos", "ARTOS", },
106#endif
107 { IH_OS_LINUX, "linux", "Linux", },
108#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
109 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
110#endif
111 { IH_OS_NETBSD, "netbsd", "NetBSD", },
112 { IH_OS_RTEMS, "rtems", "RTEMS", },
113 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
114#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
115 { IH_OS_QNX, "qnx", "QNX", },
116 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
117#endif
118#ifdef USE_HOSTCC
119 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
120 { IH_OS_DELL, "dell", "Dell", },
121 { IH_OS_ESIX, "esix", "Esix", },
122 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
123 { IH_OS_IRIX, "irix", "Irix", },
124 { IH_OS_NCR, "ncr", "NCR", },
125 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
126 { IH_OS_PSOS, "psos", "pSOS", },
127 { IH_OS_SCO, "sco", "SCO", },
128 { IH_OS_SOLARIS, "solaris", "Solaris", },
129 { IH_OS_SVR4, "svr4", "SVR4", },
130#endif
131 { -1, "", "", },
132};
133
134static table_entry_t uimage_type[] = {
135 { IH_TYPE_INVALID, NULL, "Invalid Image", },
136 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
137 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
138 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
139 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
140 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
141 { IH_TYPE_SCRIPT, "script", "Script", },
142 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
143 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
144 { -1, "", "", },
145};
146
147static table_entry_t uimage_comp[] = {
148 { IH_COMP_NONE, "none", "uncompressed", },
149 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
150 { IH_COMP_GZIP, "gzip", "gzip compressed", },
151 { -1, "", "", },
152};
153
Mike Frysingera2c67e92008-03-31 11:02:01 -0400154uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200155uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100156static void genimg_print_size (uint32_t size);
157#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
158static void genimg_print_time (time_t timestamp);
159#endif
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100160
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100161/*****************************************************************************/
162/* Legacy format routines */
163/*****************************************************************************/
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100164int image_check_hcrc (image_header_t *hdr)
165{
166 ulong hcrc;
167 ulong len = image_get_header_size ();
168 image_header_t header;
169
170 /* Copy header so we can blank CRC field for re-calculation */
171 memmove (&header, (char *)hdr, image_get_header_size ());
172 image_set_hcrc (&header, 0);
173
174 hcrc = crc32 (0, (unsigned char *)&header, len);
175
176 return (hcrc == image_get_hcrc (hdr));
177}
178
179int image_check_dcrc (image_header_t *hdr)
180{
181 ulong data = image_get_data (hdr);
182 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200183 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100184
185 return (dcrc == image_get_dcrc (hdr));
186}
187
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100188
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100189/**
190 * image_multi_count - get component (sub-image) count
191 * @hdr: pointer to the header of the multi component image
192 *
193 * image_multi_count() returns number of components in a multi
194 * component image.
195 *
196 * Note: no checking of the image type is done, caller must pass
197 * a valid multi component image.
198 *
199 * returns:
200 * number of components
201 */
202ulong image_multi_count (image_header_t *hdr)
203{
204 ulong i, count = 0;
Marian Balakowicza1474212008-02-29 16:00:06 +0100205 uint32_t *size;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100206
207 /* get start of the image payload, which in case of multi
208 * component images that points to a table of component sizes */
Marian Balakowicza1474212008-02-29 16:00:06 +0100209 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100210
211 /* count non empty slots */
212 for (i = 0; size[i]; ++i)
213 count++;
214
215 return count;
216}
217
218/**
219 * image_multi_getimg - get component data address and size
220 * @hdr: pointer to the header of the multi component image
221 * @idx: index of the requested component
222 * @data: pointer to a ulong variable, will hold component data address
223 * @len: pointer to a ulong variable, will hold component size
224 *
225 * image_multi_getimg() returns size and data address for the requested
226 * component in a multi component image.
227 *
228 * Note: no checking of the image type is done, caller must pass
229 * a valid multi component image.
230 *
231 * returns:
232 * data address and size of the component, if idx is valid
233 * 0 in data and len, if idx is out of range
234 */
235void image_multi_getimg (image_header_t *hdr, ulong idx,
236 ulong *data, ulong *len)
237{
238 int i;
Marian Balakowicza1474212008-02-29 16:00:06 +0100239 uint32_t *size;
Nick Spence143a1592008-05-10 14:02:04 -0700240 ulong offset, count, img_data;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100241
242 /* get number of component */
243 count = image_multi_count (hdr);
244
245 /* get start of the image payload, which in case of multi
246 * component images that points to a table of component sizes */
Marian Balakowicza1474212008-02-29 16:00:06 +0100247 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100248
249 /* get address of the proper component data start, which means
250 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowicza1474212008-02-29 16:00:06 +0100251 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100252
253 if (idx < count) {
Marian Balakowicza1474212008-02-29 16:00:06 +0100254 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100255 offset = 0;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100256
257 /* go over all indices preceding requested component idx */
258 for (i = 0; i < idx; i++) {
Nick Spence143a1592008-05-10 14:02:04 -0700259 /* add up i-th component size, rounding up to 4 bytes */
260 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100261 }
262
263 /* calculate idx-th component data address */
Nick Spence143a1592008-05-10 14:02:04 -0700264 *data = img_data + offset;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100265 } else {
266 *len = 0;
267 *data = 0;
268 }
269}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100270
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100271static void image_print_type (image_header_t *hdr)
272{
273 const char *os, *arch, *type, *comp;
274
275 os = genimg_get_os_name (image_get_os (hdr));
276 arch = genimg_get_arch_name (image_get_arch (hdr));
277 type = genimg_get_type_name (image_get_type (hdr));
278 comp = genimg_get_comp_name (image_get_comp (hdr));
279
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100280 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100281}
282
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100283/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200284 * image_print_contents - prints out the contents of the legacy format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100285 * @hdr: pointer to the legacy format image header
286 * @p: pointer to prefix string
287 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200288 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100289 * The routine prints out all header fields followed by the size/offset data
290 * for MULTI/SCRIPT images.
291 *
292 * returns:
293 * no returned results
294 */
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200295void image_print_contents (image_header_t *hdr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100296{
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200297 const char *p;
298
299#ifdef USE_HOSTCC
300 p = "";
301#else
302 p = " ";
303#endif
304
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100305 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
306#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
307 printf ("%sCreated: ", p);
308 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100309#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100310 printf ("%sImage Type: ", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100311 image_print_type (hdr);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100312 printf ("%sData Size: ", p);
313 genimg_print_size (image_get_data_size (hdr));
314 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
315 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100316
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100317 if (image_check_type (hdr, IH_TYPE_MULTI) ||
318 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100319 int i;
320 ulong data, len;
321 ulong count = image_multi_count (hdr);
322
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100323 printf ("%sContents:\n", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100324 for (i = 0; i < count; i++) {
325 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100326
327 printf ("%s Image %d: ", p, i);
328 genimg_print_size (len);
329
330 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
331 /*
332 * the user may need to know offsets
333 * if planning to do something with
334 * multiple files
335 */
336 printf ("%s Offset = 0x%08lx\n", p, data);
337 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100338 }
339 }
340}
341
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100342
343#ifndef USE_HOSTCC
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100344/**
345 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100346 * @rd_addr: ramdisk image start address
347 * @arch: expected ramdisk architecture
348 * @verify: checksum verification flag
349 *
350 * image_get_ramdisk() returns a pointer to the verified ramdisk image
351 * header. Routine receives image start address and expected architecture
352 * flag. Verification done covers data and header integrity and os/type/arch
353 * fields checking.
354 *
355 * If dataflash support is enabled routine checks for dataflash addresses
356 * and handles required dataflash reads.
357 *
358 * returns:
359 * pointer to a ramdisk image header, if image was found and valid
360 * otherwise, return NULL
361 */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100362static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
363 int verify)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100364{
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100365 image_header_t *rd_hdr = (image_header_t *)rd_addr;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100366
367 if (!image_check_magic (rd_hdr)) {
368 puts ("Bad Magic Number\n");
369 show_boot_progress (-10);
370 return NULL;
371 }
372
373 if (!image_check_hcrc (rd_hdr)) {
374 puts ("Bad Header Checksum\n");
375 show_boot_progress (-11);
376 return NULL;
377 }
378
379 show_boot_progress (10);
380 image_print_contents (rd_hdr);
381
382 if (verify) {
383 puts(" Verifying Checksum ... ");
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200384 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100385 puts ("Bad Data CRC\n");
386 show_boot_progress (-12);
387 return NULL;
388 }
389 puts("OK\n");
390 }
391
392 show_boot_progress (11);
393
394 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
395 !image_check_arch (rd_hdr, arch) ||
396 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
397 printf ("No Linux %s Ramdisk Image\n",
398 genimg_get_arch_name(arch));
399 show_boot_progress (-13);
400 return NULL;
401 }
402
403 return rd_hdr;
404}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100405#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100406
407/*****************************************************************************/
408/* Shared dual-format routines */
409/*****************************************************************************/
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100410#ifndef USE_HOSTCC
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200411int getenv_yesno (char *var)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100412{
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200413 char *s = getenv (var);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100414 return (s && (*s == 'n')) ? 0 : 1;
415}
416
417ulong getenv_bootm_low(void)
418{
419 char *s = getenv ("bootm_low");
420 if (s) {
421 ulong tmp = simple_strtoul (s, NULL, 16);
422 return tmp;
423 }
424
Marian Balakowicz2499b042008-03-12 12:14:15 +0100425#if defined(CFG_SDRAM_BASE)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100426 return CFG_SDRAM_BASE;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100427#elif defined(CONFIG_ARM)
428 return gd->bd->bi_dram[0].start;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100429#else
430 return 0;
431#endif
432}
433
434ulong getenv_bootm_size(void)
435{
436 char *s = getenv ("bootm_size");
437 if (s) {
438 ulong tmp = simple_strtoul (s, NULL, 16);
439 return tmp;
440 }
441
Marian Balakowicz2499b042008-03-12 12:14:15 +0100442#if defined(CONFIG_ARM)
443 return gd->bd->bi_dram[0].size;
444#else
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100445 return gd->bd->bi_memsize;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100446#endif
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100447}
448
449void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
450{
451#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
452 while (len > 0) {
453 size_t tail = (len > chunksz) ? chunksz : len;
454 WATCHDOG_RESET ();
455 memmove (to, from, tail);
456 to += tail;
457 from += tail;
458 len -= tail;
459 }
460#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
461 memmove (to, from, len);
462#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
463}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100464#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100465
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100466static void genimg_print_size (uint32_t size)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100467{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100468#ifndef USE_HOSTCC
469 printf ("%d Bytes = ", size);
470 print_size (size, "\n");
471#else
472 printf ("%d Bytes = %.2f kB = %.2f MB\n",
473 size, (double)size / 1.024e3,
474 (double)size / 1.048576e6);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100475#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100476}
477
478#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
479static void genimg_print_time (time_t timestamp)
480{
481#ifndef USE_HOSTCC
482 struct rtc_time tm;
483
484 to_tm (timestamp, &tm);
485 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
486 tm.tm_year, tm.tm_mon, tm.tm_mday,
487 tm.tm_hour, tm.tm_min, tm.tm_sec);
488#else
489 printf ("%s", ctime(&timestamp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100490#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100491}
492#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
493
494/**
495 * get_table_entry_name - translate entry id to long name
496 * @table: pointer to a translation table for entries of a specific type
497 * @msg: message to be returned when translation fails
498 * @id: entry id to be translated
499 *
500 * get_table_entry_name() will go over translation table trying to find
501 * entry that matches given id. If matching entry is found, its long
502 * name is returned to the caller.
503 *
504 * returns:
505 * long entry name if translation succeeds
506 * msg otherwise
507 */
508static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
509{
510 for (; table->id >= 0; ++table) {
511 if (table->id == id)
512 return (table->lname);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100513 }
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100514 return (msg);
515}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100516
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100517const char *genimg_get_os_name (uint8_t os)
518{
519 return (get_table_entry_name (uimage_os, "Unknown OS", os));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100520}
521
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100522const char *genimg_get_arch_name (uint8_t arch)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100523{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100524 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
525}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100526
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100527const char *genimg_get_type_name (uint8_t type)
528{
529 return (get_table_entry_name (uimage_type, "Unknown Image", type));
530}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100531
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100532const char *genimg_get_comp_name (uint8_t comp)
533{
534 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100535}
536
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100537/**
538 * get_table_entry_id - translate short entry name to id
539 * @table: pointer to a translation table for entries of a specific type
540 * @table_name: to be used in case of error
541 * @name: entry short name to be translated
542 *
543 * get_table_entry_id() will go over translation table trying to find
544 * entry that matches given short name. If matching entry is found,
545 * its id returned to the caller.
546 *
547 * returns:
548 * entry id if translation succeeds
549 * -1 otherwise
550 */
551static int get_table_entry_id (table_entry_t *table,
552 const char *table_name, const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100553{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100554 table_entry_t *t;
555#ifdef USE_HOSTCC
556 int first = 1;
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100557
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100558 for (t = table; t->id >= 0; ++t) {
559 if (t->sname && strcasecmp(t->sname, name) == 0)
560 return (t->id);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100561 }
562
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100563 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
564 for (t = table; t->id >= 0; ++t) {
565 if (t->sname == NULL)
566 continue;
567 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
568 first = 0;
569 }
570 fprintf (stderr, "\n");
571#else
572 for (t = table; t->id >= 0; ++t) {
573 if (t->sname && strcmp(t->sname, name) == 0)
574 return (t->id);
575 }
576 debug ("Invalid %s Type: %s\n", table_name, name);
577#endif /* USE_HOSTCC */
578 return (-1);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100579}
580
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100581int genimg_get_os_id (const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100582{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100583 return (get_table_entry_id (uimage_os, "OS", name));
584}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100585
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100586int genimg_get_arch_id (const char *name)
587{
588 return (get_table_entry_id (uimage_arch, "CPU", name));
589}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100590
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100591int genimg_get_type_id (const char *name)
592{
593 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100594}
Marian Balakowicz95418502008-01-31 13:55:39 +0100595
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100596int genimg_get_comp_id (const char *name)
597{
598 return (get_table_entry_id (uimage_comp, "Compression", name));
599}
600
601#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +0100602/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100603 * genimg_get_format - get image format type
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100604 * @img_addr: image start address
605 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100606 * genimg_get_format() checks whether provided address points to a valid
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100607 * legacy or FIT image.
608 *
Marian Balakowicz12560682008-02-27 11:02:26 +0100609 * New uImage format and FDT blob are based on a libfdt. FDT blob
610 * may be passed directly or embedded in a FIT image. In both situations
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100611 * genimg_get_format() must be able to dectect libfdt header.
Marian Balakowicz12560682008-02-27 11:02:26 +0100612 *
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100613 * returns:
614 * image format type or IMAGE_FORMAT_INVALID if no image is present
615 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100616int genimg_get_format (void *img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100617{
618 ulong format = IMAGE_FORMAT_INVALID;
619 image_header_t *hdr;
Marian Balakowicz12560682008-02-27 11:02:26 +0100620#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100621 char *fit_hdr;
622#endif
623
624 hdr = (image_header_t *)img_addr;
625 if (image_check_magic(hdr))
626 format = IMAGE_FORMAT_LEGACY;
Marian Balakowicz12560682008-02-27 11:02:26 +0100627#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100628 else {
629 fit_hdr = (char *)img_addr;
630 if (fdt_check_header (fit_hdr) == 0)
631 format = IMAGE_FORMAT_FIT;
632 }
633#endif
634
635 return format;
636}
637
638/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100639 * genimg_get_image - get image from special storage (if necessary)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100640 * @img_addr: image start address
641 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100642 * genimg_get_image() checks if provided image start adddress is located
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100643 * in a dataflash storage. If so, image is moved to a system RAM memory.
644 *
645 * returns:
646 * image start address after possible relocation from special storage
647 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100648ulong genimg_get_image (ulong img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100649{
Marian Balakowicz535b9102008-02-27 11:00:47 +0100650 ulong ram_addr = img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100651
652#ifdef CONFIG_HAS_DATAFLASH
Marian Balakowicz535b9102008-02-27 11:00:47 +0100653 ulong h_size, d_size;
654
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100655 if (addr_dataflash (img_addr)){
Marian Balakowicz535b9102008-02-27 11:00:47 +0100656 /* ger RAM address */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100657 ram_addr = CFG_LOAD_ADDR;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100658
659 /* get header size */
660 h_size = image_get_header_size ();
661#if defined(CONFIG_FIT)
662 if (sizeof(struct fdt_header) > h_size)
663 h_size = sizeof(struct fdt_header);
664#endif
665
666 /* read in header */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100667 debug (" Reading image header from dataflash address "
668 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100669
Marian Balakowicz535b9102008-02-27 11:00:47 +0100670 read_dataflash (img_addr, h_size, (char *)ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100671
Marian Balakowicz535b9102008-02-27 11:00:47 +0100672 /* get data size */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100673 switch (genimg_get_format ((void *)ram_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100674 case IMAGE_FORMAT_LEGACY:
675 d_size = image_get_data_size ((image_header_t *)ram_addr);
676 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
677 ram_addr, d_size);
678 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100679#if defined(CONFIG_FIT)
Marian Balakowicz535b9102008-02-27 11:00:47 +0100680 case IMAGE_FORMAT_FIT:
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100681 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100682 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
683 ram_addr, d_size);
684 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100685#endif
Marian Balakowicz535b9102008-02-27 11:00:47 +0100686 default:
687 printf (" No valid image found at 0x%08lx\n", img_addr);
688 return ram_addr;
689 }
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100690
Marian Balakowicz535b9102008-02-27 11:00:47 +0100691 /* read in image data */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100692 debug (" Reading image remaining data from dataflash address "
693 "%08lx to RAM address %08lx\n", img_addr + h_size,
694 ram_addr + h_size);
695
696 read_dataflash (img_addr + h_size, d_size,
697 (char *)(ram_addr + h_size));
Marian Balakowicz535b9102008-02-27 11:00:47 +0100698
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100699 }
Marian Balakowicz535b9102008-02-27 11:00:47 +0100700#endif /* CONFIG_HAS_DATAFLASH */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100701
702 return ram_addr;
703}
704
705/**
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100706 * fit_has_config - check if there is a valid FIT configuration
707 * @images: pointer to the bootm command headers structure
708 *
709 * fit_has_config() checks if there is a FIT configuration in use
710 * (if FTI support is present).
711 *
712 * returns:
713 * 0, no FIT support or no configuration found
714 * 1, configuration found
715 */
716int genimg_has_config (bootm_headers_t *images)
717{
718#if defined(CONFIG_FIT)
719 if (images->fit_uname_cfg)
720 return 1;
721#endif
722 return 0;
723}
724
725/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100726 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz95418502008-01-31 13:55:39 +0100727 * @argc: command argument count
728 * @argv: command argument list
Marian Balakowicz61fde552008-02-27 11:01:04 +0100729 * @images: pointer to the bootm images structure
Marian Balakowicz95418502008-01-31 13:55:39 +0100730 * @arch: expected ramdisk architecture
731 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
732 * @rd_end: pointer to a ulong variable, will hold ramdisk end
733 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100734 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz95418502008-01-31 13:55:39 +0100735 * Curently supported are the following ramdisk sources:
736 * - multicomponent kernel/ramdisk image,
737 * - commandline provided address of decicated ramdisk image.
738 *
739 * returns:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100740 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz95418502008-01-31 13:55:39 +0100741 * rd_start and rd_end are set to ramdisk start/end addresses if
742 * ramdisk image is found and valid
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100743 *
744 * 1, if ramdisk image is found but corrupted
Marian Balakowicz95418502008-01-31 13:55:39 +0100745 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz95418502008-01-31 13:55:39 +0100746 */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100747int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
748 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz95418502008-01-31 13:55:39 +0100749{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100750 ulong rd_addr, rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100751 ulong rd_data, rd_len;
752 image_header_t *rd_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100753#if defined(CONFIG_FIT)
754 void *fit_hdr;
755 const char *fit_uname_config = NULL;
756 const char *fit_uname_ramdisk = NULL;
757 ulong default_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100758 int rd_noffset;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100759 int cfg_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100760 const void *data;
761 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100762#endif
Marian Balakowicz95418502008-01-31 13:55:39 +0100763
Marian Balakowicza50d5152008-03-12 10:12:37 +0100764 *rd_start = 0;
765 *rd_end = 0;
766
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100767 /*
768 * Look for a '-' which indicates to ignore the
769 * ramdisk argument
770 */
771 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
772 debug ("## Skipping init Ramdisk\n");
773 rd_len = rd_data = 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100774 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100775#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100776 if (argc >= 3) {
777 /*
778 * If the init ramdisk comes from the FIT image and
779 * the FIT image address is omitted in the command
780 * line argument, try to use os FIT image address or
781 * default load address.
782 */
783 if (images->fit_uname_os)
784 default_addr = (ulong)images->fit_hdr_os;
785 else
786 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100787
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100788 if (fit_parse_conf (argv[2], default_addr,
789 &rd_addr, &fit_uname_config)) {
790 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
791 fit_uname_config, rd_addr);
792 } else if (fit_parse_subimage (argv[2], default_addr,
793 &rd_addr, &fit_uname_ramdisk)) {
794 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
795 fit_uname_ramdisk, rd_addr);
796 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100797#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100798 {
799 rd_addr = simple_strtoul(argv[2], NULL, 16);
800 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
801 rd_addr);
802 }
803#if defined(CONFIG_FIT)
804 } else {
805 /* use FIT configuration provided in first bootm
806 * command argument
807 */
808 rd_addr = (ulong)images->fit_hdr_os;
809 fit_uname_config = images->fit_uname_cfg;
810 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
811 fit_uname_config, rd_addr);
812
813 /*
814 * Check whether configuration has ramdisk defined,
815 * if not, don't try to use it, quit silently.
816 */
817 fit_hdr = (void *)rd_addr;
818 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
819 if (cfg_noffset < 0) {
820 debug ("* ramdisk: no such config\n");
821 return 0;
822 }
823
824 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
825 if (rd_noffset < 0) {
826 debug ("* ramdisk: no ramdisk in config\n");
827 return 0;
828 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100829 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100830#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100831
832 /* copy from dataflash if needed */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100833 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100834
835 /*
836 * Check if there is an initrd image at the
837 * address provided in the second bootm argument
838 * check image type, for FIT images get FIT node.
839 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100840 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100841 case IMAGE_FORMAT_LEGACY:
Marian Balakowicza50d5152008-03-12 10:12:37 +0100842 printf ("## Loading init Ramdisk from Legacy "
843 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz95418502008-01-31 13:55:39 +0100844
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100845 show_boot_progress (9);
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100846 rd_hdr = image_get_ramdisk (rd_addr, arch,
847 images->verify);
Marian Balakowicz95418502008-01-31 13:55:39 +0100848
Marian Balakowicza50d5152008-03-12 10:12:37 +0100849 if (rd_hdr == NULL)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600850 return 1;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600851
Marian Balakowicz95418502008-01-31 13:55:39 +0100852 rd_data = image_get_data (rd_hdr);
853 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100854 rd_load = image_get_load (rd_hdr);
855 break;
856#if defined(CONFIG_FIT)
857 case IMAGE_FORMAT_FIT:
858 fit_hdr = (void *)rd_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100859 printf ("## Loading init Ramdisk from FIT "
860 "Image at %08lx ...\n", rd_addr);
861
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100862 show_boot_progress (120);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100863 if (!fit_check_format (fit_hdr)) {
864 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100865 show_boot_progress (-120);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100866 return 0;
867 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100868 show_boot_progress (121);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100869
870 if (!fit_uname_ramdisk) {
871 /*
872 * no ramdisk image node unit name, try to get config
873 * node first. If config unit node name is NULL
874 * fit_conf_get_node() will try to find default config node
875 */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100876 show_boot_progress (122);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100877 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
878 if (cfg_noffset < 0) {
879 puts ("Could not find configuration node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100880 show_boot_progress (-122);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100881 return 0;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100882 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100883 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
884 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100885
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100886 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100887 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
888 } else {
889 /* get ramdisk component image node offset */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100890 show_boot_progress (123);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100891 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
892 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100893 if (rd_noffset < 0) {
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100894 puts ("Could not find subimage node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100895 show_boot_progress (-124);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100896 return 0;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100897 }
Marian Balakowicza50d5152008-03-12 10:12:37 +0100898
899 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
900
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100901 show_boot_progress (125);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100902 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
903 return 0;
904
905 /* get ramdisk image data address and length */
906 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
907 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100908 show_boot_progress (-127);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100909 return 0;
910 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100911 show_boot_progress (128);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100912
913 rd_data = (ulong)data;
914 rd_len = size;
915
916 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
917 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100918 show_boot_progress (-129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100919 return 0;
920 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100921 show_boot_progress (129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100922
923 images->fit_hdr_rd = fit_hdr;
924 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100925 images->fit_noffset_rd = rd_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100926 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100927#endif
928 default:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100929 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowicza50d5152008-03-12 10:12:37 +0100930 rd_data = rd_len = rd_load = 0;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100931 }
Marian Balakowicz95418502008-01-31 13:55:39 +0100932
933#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100934 /*
935 * We need to copy the ramdisk to SRAM to let Linux boot
936 */
937 if (rd_data) {
938 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
939 rd_data = rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100940 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100941#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
Marian Balakowicz95418502008-01-31 13:55:39 +0100942
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100943 } else if (images->legacy_hdr_valid &&
Marian Balakowiczab00e022008-04-11 11:07:49 +0200944 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowicz95418502008-01-31 13:55:39 +0100945 /*
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100946 * Now check if we have a legacy mult-component image,
947 * get second entry data start address and len.
Marian Balakowicz95418502008-01-31 13:55:39 +0100948 */
949 show_boot_progress (13);
950 printf ("## Loading init Ramdisk from multi component "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100951 "Legacy Image at %08lx ...\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100952 (ulong)images->legacy_hdr_os);
953
954 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz95418502008-01-31 13:55:39 +0100955 } else {
956 /*
957 * no initrd image
958 */
959 show_boot_progress (14);
960 rd_len = rd_data = 0;
961 }
962
963 if (!rd_data) {
964 debug ("## No init Ramdisk\n");
Marian Balakowicz95418502008-01-31 13:55:39 +0100965 } else {
966 *rd_start = rd_data;
967 *rd_end = rd_data + rd_len;
968 }
969 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
970 *rd_start, *rd_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600971
972 return 0;
Marian Balakowicz95418502008-01-31 13:55:39 +0100973}
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100974
Daniel Hellstrombb9c8292008-03-28 08:29:26 +0100975#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100976/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100977 * boot_ramdisk_high - relocate init ramdisk
Kumar Galab937bb72008-02-27 21:51:49 -0600978 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100979 * @rd_data: ramdisk data start address
980 * @rd_len: ramdisk data length
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100981 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
982 * start address (after possible relocation)
983 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
984 * end address (after possible relocation)
985 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100986 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100987 * variable and if requested ramdisk data is moved to a specified location.
988 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100989 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
990 * start/end addresses if ramdisk image start and len were provided,
991 * otherwise set initrd_start and initrd_end set to zeros.
992 *
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100993 * returns:
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100994 * 0 - success
995 * -1 - failure
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100996 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100997int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galab937bb72008-02-27 21:51:49 -0600998 ulong *initrd_start, ulong *initrd_end)
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100999{
1000 char *s;
1001 ulong initrd_high;
1002 int initrd_copy_to_ram = 1;
1003
1004 if ((s = getenv ("initrd_high")) != NULL) {
1005 /* a value of "no" or a similar string will act like 0,
1006 * turning the "load high" feature off. This is intentional.
1007 */
1008 initrd_high = simple_strtoul (s, NULL, 16);
1009 if (initrd_high == ~0)
1010 initrd_copy_to_ram = 0;
1011 } else {
1012 /* not set, no restrictions to load high */
1013 initrd_high = ~0;
1014 }
1015
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001016 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1017 initrd_high, initrd_copy_to_ram);
1018
1019 if (rd_data) {
1020 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1021 debug (" in-place initrd\n");
1022 *initrd_start = rd_data;
1023 *initrd_end = rd_data + rd_len;
Kumar Galab937bb72008-02-27 21:51:49 -06001024 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001025 } else {
Kumar Galab937bb72008-02-27 21:51:49 -06001026 if (initrd_high)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001027 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galab937bb72008-02-27 21:51:49 -06001028 else
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001029 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001030
Kumar Galab937bb72008-02-27 21:51:49 -06001031 if (*initrd_start == 0) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001032 puts ("ramdisk - allocation error\n");
Kumar Galab937bb72008-02-27 21:51:49 -06001033 goto error;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001034 }
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001035 show_boot_progress (12);
1036
1037 *initrd_end = *initrd_start + rd_len;
1038 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1039 *initrd_start, *initrd_end);
1040
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001041 memmove_wd ((void *)*initrd_start,
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001042 (void *)rd_data, rd_len, CHUNKSZ);
1043
1044 puts ("OK\n");
1045 }
1046 } else {
1047 *initrd_start = 0;
1048 *initrd_end = 0;
1049 }
1050 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1051 *initrd_start, *initrd_end);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001052
Kumar Galab937bb72008-02-27 21:51:49 -06001053 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001054
Kumar Galab937bb72008-02-27 21:51:49 -06001055error:
1056 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001057}
1058
1059/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001060 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galab937bb72008-02-27 21:51:49 -06001061 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001062 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1063 * @cmd_end: pointer to a ulong variable, will hold cmdline end
Kumar Galab937bb72008-02-27 21:51:49 -06001064 * @bootmap_base: ulong variable, holds offset in physical memory to
1065 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001066 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001067 * boot_get_cmdline() allocates space for kernel command line below
Kumar Galab937bb72008-02-27 21:51:49 -06001068 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001069 * variable is present its contents is copied to allocated kernel
1070 * command line.
1071 *
1072 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001073 * 0 - success
1074 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001075 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001076int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
Kumar Galab937bb72008-02-27 21:51:49 -06001077 ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001078{
1079 char *cmdline;
1080 char *s;
1081
Kumar Galab937bb72008-02-27 21:51:49 -06001082 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1083 CFG_BOOTMAPSZ + bootmap_base);
1084
1085 if (cmdline == NULL)
1086 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001087
1088 if ((s = getenv("bootargs")) == NULL)
1089 s = "";
1090
1091 strcpy(cmdline, s);
1092
1093 *cmd_start = (ulong) & cmdline[0];
1094 *cmd_end = *cmd_start + strlen(cmdline);
1095
1096 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1097
Kumar Galab937bb72008-02-27 21:51:49 -06001098 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001099}
1100
1101/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001102 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galab937bb72008-02-27 21:51:49 -06001103 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001104 * @kbd: double pointer to board info data
Kumar Galab937bb72008-02-27 21:51:49 -06001105 * @bootmap_base: ulong variable, holds offset in physical memory to
1106 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001107 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001108 * boot_get_kbd() allocates space for kernel copy of board info data below
Kumar Galab937bb72008-02-27 21:51:49 -06001109 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1110 * the current u-boot board info data.
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001111 *
1112 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001113 * 0 - success
1114 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001115 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001116int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001117{
Kumar Galab937bb72008-02-27 21:51:49 -06001118 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1119 CFG_BOOTMAPSZ + bootmap_base);
1120 if (*kbd == NULL)
1121 return -1;
1122
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001123 **kbd = *(gd->bd);
1124
1125 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1126
1127#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1128 do_bdinfo(NULL, 0, 0, NULL);
1129#endif
1130
Kumar Galab937bb72008-02-27 21:51:49 -06001131 return 0;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001132}
1133#endif /* CONFIG_PPC || CONFIG_M68K */
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001134#endif /* !USE_HOSTCC */
Marian Balakowicz95418502008-01-31 13:55:39 +01001135
Marian Balakowicz3413e222008-02-21 17:20:20 +01001136#if defined(CONFIG_FIT)
1137/*****************************************************************************/
1138/* New uImage format routines */
1139/*****************************************************************************/
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001140#ifndef USE_HOSTCC
Marian Balakowicz3413e222008-02-21 17:20:20 +01001141static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1142 ulong *addr, const char **name)
1143{
1144 const char *sep;
1145
1146 *addr = addr_curr;
1147 *name = NULL;
1148
1149 sep = strchr (spec, sepc);
1150 if (sep) {
1151 if (sep - spec > 0)
1152 *addr = simple_strtoul (spec, NULL, 16);
1153
1154 *name = sep + 1;
1155 return 1;
1156 }
1157
1158 return 0;
1159}
1160
1161/**
1162 * fit_parse_conf - parse FIT configuration spec
1163 * @spec: input string, containing configuration spec
1164 * @add_curr: current image address (to be used as a possible default)
1165 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1166 * configuration
1167 * @conf_name double pointer to a char, will hold pointer to a configuration
1168 * unit name
1169 *
1170 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1171 * where <addr> is a FIT image address that contains configuration
1172 * with a <conf> unit name.
1173 *
1174 * Address part is optional, and if omitted default add_curr will
1175 * be used instead.
1176 *
1177 * returns:
1178 * 1 if spec is a valid configuration string,
1179 * addr and conf_name are set accordingly
1180 * 0 otherwise
1181 */
1182inline int fit_parse_conf (const char *spec, ulong addr_curr,
1183 ulong *addr, const char **conf_name)
1184{
1185 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1186}
1187
1188/**
1189 * fit_parse_subimage - parse FIT subimage spec
1190 * @spec: input string, containing subimage spec
1191 * @add_curr: current image address (to be used as a possible default)
1192 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1193 * subimage
1194 * @image_name: double pointer to a char, will hold pointer to a subimage name
1195 *
1196 * fit_parse_subimage() expects subimage spec in the for of
1197 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1198 * subimage with a <subimg> unit name.
1199 *
1200 * Address part is optional, and if omitted default add_curr will
1201 * be used instead.
1202 *
1203 * returns:
1204 * 1 if spec is a valid subimage string,
1205 * addr and image_name are set accordingly
1206 * 0 otherwise
1207 */
1208inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1209 ulong *addr, const char **image_name)
1210{
1211 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1212}
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001213#endif /* !USE_HOSTCC */
1214
1215static void fit_get_debug (const void *fit, int noffset,
1216 char *prop_name, int err)
1217{
1218 debug ("Can't get '%s' property from FIT 0x%08lx, "
1219 "node: offset %d, name %s (%s)\n",
1220 prop_name, (ulong)fit, noffset,
1221 fit_get_name (fit, noffset, NULL),
1222 fdt_strerror (err));
1223}
1224
1225/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001226 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001227 * @fit: pointer to the FIT format image header
1228 * @p: pointer to prefix string
1229 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001230 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001231 * The routine prints out FIT image properties (root node level) follwed by
1232 * the details of each component image.
1233 *
1234 * returns:
1235 * no returned results
1236 */
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001237void fit_print_contents (const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001238{
1239 char *desc;
1240 char *uname;
1241 int images_noffset;
1242 int confs_noffset;
1243 int noffset;
1244 int ndepth;
1245 int count = 0;
1246 int ret;
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001247 const char *p;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001248#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1249 time_t timestamp;
1250#endif
1251
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001252#ifdef USE_HOSTCC
1253 p = "";
1254#else
1255 p = " ";
1256#endif
1257
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001258 /* Root node properties */
1259 ret = fit_get_desc (fit, 0, &desc);
1260 printf ("%sFIT description: ", p);
1261 if (ret)
1262 printf ("unavailable\n");
1263 else
1264 printf ("%s\n", desc);
1265
1266#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1267 ret = fit_get_timestamp (fit, 0, &timestamp);
1268 printf ("%sCreated: ", p);
1269 if (ret)
1270 printf ("unavailable\n");
1271 else
1272 genimg_print_time (timestamp);
1273#endif
1274
1275 /* Find images parent node offset */
1276 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1277 if (images_noffset < 0) {
1278 printf ("Can't find images parent node '%s' (%s)\n",
1279 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1280 return;
1281 }
1282
1283 /* Process its subnodes, print out component images details */
1284 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1285 (noffset >= 0) && (ndepth > 0);
1286 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1287 if (ndepth == 1) {
1288 /*
1289 * Direct child node of the images parent node,
1290 * i.e. component image node.
1291 */
1292 printf ("%s Image %u (%s)\n", p, count++,
1293 fit_get_name(fit, noffset, NULL));
1294
1295 fit_image_print (fit, noffset, p);
1296 }
1297 }
1298
1299 /* Find configurations parent node offset */
1300 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1301 if (confs_noffset < 0) {
1302 debug ("Can't get configurations parent node '%s' (%s)\n",
1303 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1304 return;
1305 }
1306
1307 /* get default configuration unit name from default property */
1308 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1309 if (uname)
1310 printf ("%s Default Configuration: '%s'\n", p, uname);
1311
1312 /* Process its subnodes, print out configurations details */
1313 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1314 (noffset >= 0) && (ndepth > 0);
1315 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1316 if (ndepth == 1) {
1317 /*
1318 * Direct child node of the configurations parent node,
1319 * i.e. configuration node.
1320 */
1321 printf ("%s Configuration %u (%s)\n", p, count++,
1322 fit_get_name(fit, noffset, NULL));
1323
1324 fit_conf_print (fit, noffset, p);
1325 }
1326 }
1327}
1328
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001329/**
1330 * fit_image_print - prints out the FIT component image details
1331 * @fit: pointer to the FIT format image header
1332 * @image_noffset: offset of the component image node
1333 * @p: pointer to prefix string
1334 *
1335 * fit_image_print() lists all mandatory properies for the processed component
1336 * image. If present, hash nodes are printed out as well.
1337 *
1338 * returns:
1339 * no returned results
1340 */
1341void fit_image_print (const void *fit, int image_noffset, const char *p)
1342{
1343 char *desc;
1344 uint8_t type, arch, os, comp;
1345 size_t size;
1346 ulong load, entry;
1347 const void *data;
1348 int noffset;
1349 int ndepth;
1350 int ret;
1351
1352 /* Mandatory properties */
1353 ret = fit_get_desc (fit, image_noffset, &desc);
1354 printf ("%s Description: ", p);
1355 if (ret)
1356 printf ("unavailable\n");
1357 else
1358 printf ("%s\n", desc);
1359
1360 fit_image_get_type (fit, image_noffset, &type);
1361 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1362
1363 fit_image_get_comp (fit, image_noffset, &comp);
1364 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1365
1366 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1367
1368#ifndef USE_HOSTCC
1369 printf ("%s Data Start: ", p);
1370 if (ret)
1371 printf ("unavailable\n");
1372 else
1373 printf ("0x%08lx\n", (ulong)data);
1374#endif
1375
1376 printf ("%s Data Size: ", p);
1377 if (ret)
1378 printf ("unavailable\n");
1379 else
1380 genimg_print_size (size);
1381
1382 /* Remaining, type dependent properties */
1383 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1384 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1385 (type == IH_TYPE_FLATDT)) {
1386 fit_image_get_arch (fit, image_noffset, &arch);
1387 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1388 }
1389
1390 if (type == IH_TYPE_KERNEL) {
1391 fit_image_get_os (fit, image_noffset, &os);
1392 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1393 }
1394
1395 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1396 ret = fit_image_get_load (fit, image_noffset, &load);
1397 printf ("%s Load Address: ", p);
1398 if (ret)
1399 printf ("unavailable\n");
1400 else
1401 printf ("0x%08lx\n", load);
1402
1403 fit_image_get_entry (fit, image_noffset, &entry);
1404 printf ("%s Entry Point: ", p);
1405 if (ret)
1406 printf ("unavailable\n");
1407 else
1408 printf ("0x%08lx\n", entry);
1409 }
1410
1411 /* Process all hash subnodes of the component image node */
1412 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1413 (noffset >= 0) && (ndepth > 0);
1414 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1415 if (ndepth == 1) {
1416 /* Direct child node of the component image node */
1417 fit_image_print_hash (fit, noffset, p);
1418 }
1419 }
1420}
1421
1422/**
1423 * fit_image_print_hash - prints out the hash node details
1424 * @fit: pointer to the FIT format image header
1425 * @noffset: offset of the hash node
1426 * @p: pointer to prefix string
1427 *
1428 * fit_image_print_hash() lists properies for the processed hash node
1429 *
1430 * returns:
1431 * no returned results
1432 */
1433void fit_image_print_hash (const void *fit, int noffset, const char *p)
1434{
1435 char *algo;
1436 uint8_t *value;
1437 int value_len;
1438 int i, ret;
1439
1440 /*
1441 * Check subnode name, must be equal to "hash".
1442 * Multiple hash nodes require unique unit node
1443 * names, e.g. hash@1, hash@2, etc.
1444 */
1445 if (strncmp (fit_get_name(fit, noffset, NULL),
1446 FIT_HASH_NODENAME,
1447 strlen(FIT_HASH_NODENAME)) != 0)
1448 return;
1449
1450 debug ("%s Hash node: '%s'\n", p,
1451 fit_get_name (fit, noffset, NULL));
1452
1453 printf ("%s Hash algo: ", p);
1454 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1455 printf ("invalid/unsupported\n");
1456 return;
1457 }
1458 printf ("%s\n", algo);
1459
1460 ret = fit_image_hash_get_value (fit, noffset, &value,
1461 &value_len);
1462 printf ("%s Hash value: ", p);
1463 if (ret) {
1464 printf ("unavailable\n");
1465 } else {
1466 for (i = 0; i < value_len; i++)
1467 printf ("%02x", value[i]);
1468 printf ("\n");
1469 }
1470
1471 debug ("%s Hash len: %d\n", p, value_len);
1472}
1473
1474/**
1475 * fit_get_desc - get node description property
1476 * @fit: pointer to the FIT format image header
1477 * @noffset: node offset
1478 * @desc: double pointer to the char, will hold pointer to the descrption
1479 *
1480 * fit_get_desc() reads description property from a given node, if
1481 * description is found pointer to it is returened in third call argument.
1482 *
1483 * returns:
1484 * 0, on success
1485 * -1, on failure
1486 */
1487int fit_get_desc (const void *fit, int noffset, char **desc)
1488{
1489 int len;
1490
1491 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1492 if (*desc == NULL) {
1493 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1494 return -1;
1495 }
1496
1497 return 0;
1498}
1499
1500/**
1501 * fit_get_timestamp - get node timestamp property
1502 * @fit: pointer to the FIT format image header
1503 * @noffset: node offset
1504 * @timestamp: pointer to the time_t, will hold read timestamp
1505 *
1506 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1507 * is found and has a correct size its value is retured in third call
1508 * argument.
1509 *
1510 * returns:
1511 * 0, on success
1512 * -1, on property read failure
1513 * -2, on wrong timestamp size
1514 */
1515int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1516{
1517 int len;
1518 const void *data;
1519
1520 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1521 if (data == NULL) {
1522 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1523 return -1;
1524 }
1525 if (len != sizeof (uint32_t)) {
1526 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1527 return -2;
1528 }
1529
1530 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1531 return 0;
1532}
1533
1534/**
1535 * fit_image_get_node - get node offset for component image of a given unit name
1536 * @fit: pointer to the FIT format image header
1537 * @image_uname: component image node unit name
1538 *
1539 * fit_image_get_node() finds a component image (withing the '/images'
1540 * node) of a provided unit name. If image is found its node offset is
1541 * returned to the caller.
1542 *
1543 * returns:
1544 * image node offset when found (>=0)
1545 * negative number on failure (FDT_ERR_* code)
1546 */
1547int fit_image_get_node (const void *fit, const char *image_uname)
1548{
1549 int noffset, images_noffset;
1550
1551 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1552 if (images_noffset < 0) {
1553 debug ("Can't find images parent node '%s' (%s)\n",
1554 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1555 return images_noffset;
1556 }
1557
1558 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1559 if (noffset < 0) {
1560 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1561 image_uname, fdt_strerror (noffset));
1562 }
1563
1564 return noffset;
1565}
1566
1567/**
1568 * fit_image_get_os - get os id for a given component image node
1569 * @fit: pointer to the FIT format image header
1570 * @noffset: component image node offset
1571 * @os: pointer to the uint8_t, will hold os numeric id
1572 *
1573 * fit_image_get_os() finds os property in a given component image node.
1574 * If the property is found, its (string) value is translated to the numeric
1575 * id which is returned to the caller.
1576 *
1577 * returns:
1578 * 0, on success
1579 * -1, on failure
1580 */
1581int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1582{
1583 int len;
1584 const void *data;
1585
1586 /* Get OS name from property data */
1587 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1588 if (data == NULL) {
1589 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1590 *os = -1;
1591 return -1;
1592 }
1593
1594 /* Translate OS name to id */
1595 *os = genimg_get_os_id (data);
1596 return 0;
1597}
1598
1599/**
1600 * fit_image_get_arch - get arch id for a given component image node
1601 * @fit: pointer to the FIT format image header
1602 * @noffset: component image node offset
1603 * @arch: pointer to the uint8_t, will hold arch numeric id
1604 *
1605 * fit_image_get_arch() finds arch property in a given component image node.
1606 * If the property is found, its (string) value is translated to the numeric
1607 * id which is returned to the caller.
1608 *
1609 * returns:
1610 * 0, on success
1611 * -1, on failure
1612 */
1613int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1614{
1615 int len;
1616 const void *data;
1617
1618 /* Get architecture name from property data */
1619 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1620 if (data == NULL) {
1621 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1622 *arch = -1;
1623 return -1;
1624 }
1625
1626 /* Translate architecture name to id */
1627 *arch = genimg_get_arch_id (data);
1628 return 0;
1629}
1630
1631/**
1632 * fit_image_get_type - get type id for a given component image node
1633 * @fit: pointer to the FIT format image header
1634 * @noffset: component image node offset
1635 * @type: pointer to the uint8_t, will hold type numeric id
1636 *
1637 * fit_image_get_type() finds type property in a given component image node.
1638 * If the property is found, its (string) value is translated to the numeric
1639 * id which is returned to the caller.
1640 *
1641 * returns:
1642 * 0, on success
1643 * -1, on failure
1644 */
1645int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1646{
1647 int len;
1648 const void *data;
1649
1650 /* Get image type name from property data */
1651 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1652 if (data == NULL) {
1653 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1654 *type = -1;
1655 return -1;
1656 }
1657
1658 /* Translate image type name to id */
1659 *type = genimg_get_type_id (data);
1660 return 0;
1661}
1662
1663/**
1664 * fit_image_get_comp - get comp id for a given component image node
1665 * @fit: pointer to the FIT format image header
1666 * @noffset: component image node offset
1667 * @comp: pointer to the uint8_t, will hold comp numeric id
1668 *
1669 * fit_image_get_comp() finds comp property in a given component image node.
1670 * If the property is found, its (string) value is translated to the numeric
1671 * id which is returned to the caller.
1672 *
1673 * returns:
1674 * 0, on success
1675 * -1, on failure
1676 */
1677int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1678{
1679 int len;
1680 const void *data;
1681
1682 /* Get compression name from property data */
1683 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1684 if (data == NULL) {
1685 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1686 *comp = -1;
1687 return -1;
1688 }
1689
1690 /* Translate compression name to id */
1691 *comp = genimg_get_comp_id (data);
1692 return 0;
1693}
1694
1695/**
1696 * fit_image_get_load - get load address property for a given component image node
1697 * @fit: pointer to the FIT format image header
1698 * @noffset: component image node offset
1699 * @load: pointer to the uint32_t, will hold load address
1700 *
1701 * fit_image_get_load() finds load address property in a given component image node.
1702 * If the property is found, its value is returned to the caller.
1703 *
1704 * returns:
1705 * 0, on success
1706 * -1, on failure
1707 */
1708int fit_image_get_load (const void *fit, int noffset, ulong *load)
1709{
1710 int len;
1711 const uint32_t *data;
1712
1713 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1714 if (data == NULL) {
1715 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1716 return -1;
1717 }
1718
1719 *load = uimage_to_cpu (*data);
1720 return 0;
1721}
1722
1723/**
1724 * fit_image_get_entry - get entry point address property for a given component image node
1725 * @fit: pointer to the FIT format image header
1726 * @noffset: component image node offset
1727 * @entry: pointer to the uint32_t, will hold entry point address
1728 *
1729 * fit_image_get_entry() finds entry point address property in a given component image node.
1730 * If the property is found, its value is returned to the caller.
1731 *
1732 * returns:
1733 * 0, on success
1734 * -1, on failure
1735 */
1736int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1737{
1738 int len;
1739 const uint32_t *data;
1740
1741 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1742 if (data == NULL) {
1743 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1744 return -1;
1745 }
1746
1747 *entry = uimage_to_cpu (*data);
1748 return 0;
1749}
1750
1751/**
1752 * fit_image_get_data - get data property and its size for a given component image node
1753 * @fit: pointer to the FIT format image header
1754 * @noffset: component image node offset
1755 * @data: double pointer to void, will hold data property's data address
1756 * @size: pointer to size_t, will hold data property's data size
1757 *
1758 * fit_image_get_data() finds data property in a given component image node.
1759 * If the property is found its data start address and size are returned to
1760 * the caller.
1761 *
1762 * returns:
1763 * 0, on success
1764 * -1, on failure
1765 */
1766int fit_image_get_data (const void *fit, int noffset,
1767 const void **data, size_t *size)
1768{
1769 int len;
1770
1771 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1772 if (*data == NULL) {
1773 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1774 *size = 0;
1775 return -1;
1776 }
1777
1778 *size = len;
1779 return 0;
1780}
1781
1782/**
1783 * fit_image_hash_get_algo - get hash algorithm name
1784 * @fit: pointer to the FIT format image header
1785 * @noffset: hash node offset
1786 * @algo: double pointer to char, will hold pointer to the algorithm name
1787 *
1788 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1789 * If the property is found its data start address is returned to the caller.
1790 *
1791 * returns:
1792 * 0, on success
1793 * -1, on failure
1794 */
1795int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1796{
1797 int len;
1798
1799 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1800 if (*algo == NULL) {
1801 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1802 return -1;
1803 }
1804
1805 return 0;
1806}
1807
1808/**
1809 * fit_image_hash_get_value - get hash value and length
1810 * @fit: pointer to the FIT format image header
1811 * @noffset: hash node offset
1812 * @value: double pointer to uint8_t, will hold address of a hash value data
1813 * @value_len: pointer to an int, will hold hash data length
1814 *
1815 * fit_image_hash_get_value() finds hash value property in a given hash node.
1816 * If the property is found its data start address and size are returned to
1817 * the caller.
1818 *
1819 * returns:
1820 * 0, on success
1821 * -1, on failure
1822 */
1823int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1824 int *value_len)
1825{
1826 int len;
1827
1828 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1829 if (*value == NULL) {
1830 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1831 *value_len = 0;
1832 return -1;
1833 }
1834
1835 *value_len = len;
1836 return 0;
1837}
1838
1839/**
1840 * fit_set_timestamp - set node timestamp property
1841 * @fit: pointer to the FIT format image header
1842 * @noffset: node offset
1843 * @timestamp: timestamp value to be set
1844 *
1845 * fit_set_timestamp() attempts to set timestamp property in the requested
1846 * node and returns operation status to the caller.
1847 *
1848 * returns:
1849 * 0, on success
1850 * -1, on property read failure
1851 */
1852int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1853{
1854 uint32_t t;
1855 int ret;
1856
1857 t = cpu_to_uimage (timestamp);
1858 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1859 sizeof (uint32_t));
1860 if (ret) {
1861 printf ("Can't set '%s' property for '%s' node (%s)\n",
1862 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1863 fdt_strerror (ret));
1864 return -1;
1865 }
1866
1867 return 0;
1868}
1869
1870/**
1871 * calculate_hash - calculate and return hash for provided input data
1872 * @data: pointer to the input data
1873 * @data_len: data length
1874 * @algo: requested hash algorithm
1875 * @value: pointer to the char, will hold hash value data (caller must
1876 * allocate enough free space)
1877 * value_len: length of the calculated hash
1878 *
1879 * calculate_hash() computes input data hash according to the requested algorithm.
1880 * Resulting hash value is placed in caller provided 'value' buffer, length
1881 * of the calculated hash is returned via value_len pointer argument.
1882 *
1883 * returns:
1884 * 0, on success
1885 * -1, when algo is unsupported
1886 */
1887static int calculate_hash (const void *data, int data_len, const char *algo,
1888 uint8_t *value, int *value_len)
1889{
1890 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02001891 *((uint32_t *)value) = crc32_wd (0, data, data_len,
1892 CHUNKSZ_CRC32);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001893 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1894 *value_len = 4;
1895 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02001896 sha1_csum_wd ((unsigned char *) data, data_len,
1897 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001898 *value_len = 20;
1899 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02001900 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka2e6f2852008-03-14 16:22:34 +01001901 *value_len = 16;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001902 } else {
1903 debug ("Unsupported hash alogrithm\n");
1904 return -1;
1905 }
1906 return 0;
1907}
1908
1909#ifdef USE_HOSTCC
1910/**
1911 * fit_set_hashes - process FIT component image nodes and calculate hashes
1912 * @fit: pointer to the FIT format image header
1913 *
1914 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1915 * Hashes are calculated for all component images which have hash subnodes
1916 * with algorithm property set to one of the supported hash algorithms.
1917 *
1918 * returns
1919 * 0, on success
1920 * libfdt error code, on failure
1921 */
1922int fit_set_hashes (void *fit)
1923{
1924 int images_noffset;
1925 int noffset;
1926 int ndepth;
1927 int ret;
1928
1929 /* Find images parent node offset */
1930 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1931 if (images_noffset < 0) {
1932 printf ("Can't find images parent node '%s' (%s)\n",
1933 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1934 return images_noffset;
1935 }
1936
1937 /* Process its subnodes, print out component images details */
1938 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1939 (noffset >= 0) && (ndepth > 0);
1940 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1941 if (ndepth == 1) {
1942 /*
1943 * Direct child node of the images parent node,
1944 * i.e. component image node.
1945 */
1946 ret = fit_image_set_hashes (fit, noffset);
1947 if (ret)
1948 return ret;
1949 }
1950 }
1951
1952 return 0;
1953}
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001954
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001955/**
1956 * fit_image_set_hashes - calculate/set hashes for given component image node
1957 * @fit: pointer to the FIT format image header
1958 * @image_noffset: requested component image node
1959 *
1960 * fit_image_set_hashes() adds hash values for an component image node. All
1961 * existing hash subnodes are checked, if algorithm property is set to one of
1962 * the supported hash algorithms, hash value is computed and corresponding
1963 * hash node property is set, for example:
1964 *
1965 * Input component image node structure:
1966 *
1967 * o image@1 (at image_noffset)
1968 * | - data = [binary data]
1969 * o hash@1
1970 * |- algo = "sha1"
1971 *
1972 * Output component image node structure:
1973 *
1974 * o image@1 (at image_noffset)
1975 * | - data = [binary data]
1976 * o hash@1
1977 * |- algo = "sha1"
1978 * |- value = sha1(data)
1979 *
1980 * returns:
1981 * 0 on sucess
1982 * <0 on failure
1983 */
1984int fit_image_set_hashes (void *fit, int image_noffset)
1985{
1986 const void *data;
1987 size_t size;
1988 char *algo;
1989 uint8_t value[FIT_MAX_HASH_LEN];
1990 int value_len;
1991 int noffset;
1992 int ndepth;
1993
1994 /* Get image data and data length */
1995 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
1996 printf ("Can't get image data/size\n");
1997 return -1;
1998 }
1999
2000 /* Process all hash subnodes of the component image node */
2001 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2002 (noffset >= 0) && (ndepth > 0);
2003 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2004 if (ndepth == 1) {
2005 /* Direct child node of the component image node */
2006
2007 /*
2008 * Check subnode name, must be equal to "hash".
2009 * Multiple hash nodes require unique unit node
2010 * names, e.g. hash@1, hash@2, etc.
2011 */
2012 if (strncmp (fit_get_name(fit, noffset, NULL),
2013 FIT_HASH_NODENAME,
2014 strlen(FIT_HASH_NODENAME)) != 0) {
2015 /* Not a hash subnode, skip it */
2016 continue;
2017 }
2018
2019 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2020 printf ("Can't get hash algo property 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 if (calculate_hash (data, size, algo, value, &value_len)) {
2028 printf ("Unsupported hash algorithm (%s) for "
2029 "'%s' hash node in '%s' image node\n",
2030 algo, fit_get_name (fit, noffset, NULL),
2031 fit_get_name (fit, image_noffset, NULL));
2032 return -1;
2033 }
2034
2035 if (fit_image_hash_set_value (fit, noffset, value,
2036 value_len)) {
2037 printf ("Can't set hash value for "
2038 "'%s' hash node in '%s' image node\n",
2039 fit_get_name (fit, noffset, NULL),
2040 fit_get_name (fit, image_noffset, NULL));
2041 return -1;
2042 }
2043 }
2044 }
2045
2046 return 0;
2047}
2048
2049/**
2050 * fit_image_hash_set_value - set hash value in requested has node
2051 * @fit: pointer to the FIT format image header
2052 * @noffset: hash node offset
2053 * @value: hash value to be set
2054 * @value_len: hash value length
2055 *
2056 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2057 * given and returns operation status to the caller.
2058 *
2059 * returns
2060 * 0, on success
2061 * -1, on failure
2062 */
2063int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2064 int value_len)
2065{
2066 int ret;
2067
2068 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2069 if (ret) {
2070 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2071 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2072 fdt_strerror (ret));
2073 return -1;
2074 }
2075
2076 return 0;
2077}
2078#endif /* USE_HOSTCC */
2079
2080/**
2081 * fit_image_check_hashes - verify data intergity
2082 * @fit: pointer to the FIT format image header
2083 * @image_noffset: component image node offset
2084 *
2085 * fit_image_check_hashes() goes over component image hash nodes,
2086 * re-calculates each data hash and compares with the value stored in hash
2087 * node.
2088 *
2089 * returns:
2090 * 1, if all hashes are valid
2091 * 0, otherwise (or on error)
2092 */
2093int fit_image_check_hashes (const void *fit, int image_noffset)
2094{
2095 const void *data;
2096 size_t size;
2097 char *algo;
2098 uint8_t *fit_value;
2099 int fit_value_len;
2100 uint8_t value[FIT_MAX_HASH_LEN];
2101 int value_len;
2102 int noffset;
2103 int ndepth;
2104 char *err_msg = "";
2105
2106 /* Get image data and data length */
2107 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2108 printf ("Can't get image data/size\n");
2109 return 0;
2110 }
2111
2112 /* Process all hash subnodes of the component image node */
2113 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2114 (noffset >= 0) && (ndepth > 0);
2115 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2116 if (ndepth == 1) {
2117 /* Direct child node of the component image node */
2118
2119 /*
2120 * Check subnode name, must be equal to "hash".
2121 * Multiple hash nodes require unique unit node
2122 * names, e.g. hash@1, hash@2, etc.
2123 */
2124 if (strncmp (fit_get_name(fit, noffset, NULL),
2125 FIT_HASH_NODENAME,
2126 strlen(FIT_HASH_NODENAME)) != 0)
2127 continue;
2128
2129 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2130 err_msg = "Can't get hash algo property";
2131 goto error;
2132 }
2133 printf ("%s", algo);
2134
2135 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2136 &fit_value_len)) {
2137 err_msg = "Can't get hash value property";
2138 goto error;
2139 }
2140
2141 if (calculate_hash (data, size, algo, value, &value_len)) {
2142 err_msg = "Unsupported hash algorithm";
2143 goto error;
2144 }
2145
2146 if (value_len != fit_value_len) {
2147 err_msg = "Bad hash value len";
2148 goto error;
2149 } else if (memcmp (value, fit_value, value_len) != 0) {
2150 err_msg = "Bad hash value";
2151 goto error;
2152 }
2153 printf ("+ ");
2154 }
2155 }
2156
2157 return 1;
2158
2159error:
2160 printf ("%s for '%s' hash node in '%s' image node\n",
2161 err_msg, fit_get_name (fit, noffset, NULL),
2162 fit_get_name (fit, image_noffset, NULL));
2163 return 0;
2164}
2165
2166/**
2167 * fit_image_check_os - check whether image node is of a given os type
2168 * @fit: pointer to the FIT format image header
2169 * @noffset: component image node offset
2170 * @os: requested image os
2171 *
2172 * fit_image_check_os() reads image os property and compares its numeric
2173 * id with the requested os. Comparison result is returned to the caller.
2174 *
2175 * returns:
2176 * 1 if image is of given os type
2177 * 0 otherwise (or on error)
2178 */
2179int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2180{
2181 uint8_t image_os;
2182
2183 if (fit_image_get_os (fit, noffset, &image_os))
2184 return 0;
2185 return (os == image_os);
2186}
2187
2188/**
2189 * fit_image_check_arch - check whether image node is of a given arch
2190 * @fit: pointer to the FIT format image header
2191 * @noffset: component image node offset
2192 * @arch: requested imagearch
2193 *
2194 * fit_image_check_arch() reads image arch property and compares its numeric
2195 * id with the requested arch. Comparison result is returned to the caller.
2196 *
2197 * returns:
2198 * 1 if image is of given arch
2199 * 0 otherwise (or on error)
2200 */
2201int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2202{
2203 uint8_t image_arch;
2204
2205 if (fit_image_get_arch (fit, noffset, &image_arch))
2206 return 0;
2207 return (arch == image_arch);
2208}
2209
2210/**
2211 * fit_image_check_type - check whether image node is of a given type
2212 * @fit: pointer to the FIT format image header
2213 * @noffset: component image node offset
2214 * @type: requested image type
2215 *
2216 * fit_image_check_type() reads image type property and compares its numeric
2217 * id with the requested type. Comparison result is returned to the caller.
2218 *
2219 * returns:
2220 * 1 if image is of given type
2221 * 0 otherwise (or on error)
2222 */
2223int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2224{
2225 uint8_t image_type;
2226
2227 if (fit_image_get_type (fit, noffset, &image_type))
2228 return 0;
2229 return (type == image_type);
2230}
2231
2232/**
2233 * fit_image_check_comp - check whether image node uses given compression
2234 * @fit: pointer to the FIT format image header
2235 * @noffset: component image node offset
2236 * @comp: requested image compression type
2237 *
2238 * fit_image_check_comp() reads image compression property and compares its
2239 * numeric id with the requested compression type. Comparison result is
2240 * returned to the caller.
2241 *
2242 * returns:
2243 * 1 if image uses requested compression
2244 * 0 otherwise (or on error)
2245 */
2246int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2247{
2248 uint8_t image_comp;
2249
2250 if (fit_image_get_comp (fit, noffset, &image_comp))
2251 return 0;
2252 return (comp == image_comp);
2253}
2254
2255/**
2256 * fit_check_format - sanity check FIT image format
2257 * @fit: pointer to the FIT format image header
2258 *
2259 * fit_check_format() runs a basic sanity FIT image verification.
2260 * Routine checks for mandatory properties, nodes, etc.
2261 *
2262 * returns:
2263 * 1, on success
2264 * 0, on failure
2265 */
2266int fit_check_format (const void *fit)
2267{
2268 /* mandatory / node 'description' property */
2269 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2270 debug ("Wrong FIT format: no description\n");
2271 return 0;
2272 }
2273
2274#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2275 /* mandatory / node 'timestamp' property */
2276 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2277 debug ("Wrong FIT format: no description\n");
2278 return 0;
2279 }
2280#endif
2281
2282 /* mandatory subimages parent '/images' node */
2283 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2284 debug ("Wrong FIT format: no images parent node\n");
2285 return 0;
2286 }
2287
2288 return 1;
2289}
2290
2291/**
2292 * fit_conf_get_node - get node offset for configuration of a given unit name
2293 * @fit: pointer to the FIT format image header
2294 * @conf_uname: configuration node unit name
2295 *
2296 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2297 * parant node) of a provided unit name. If configuration is found its node offset
2298 * is returned to the caller.
2299 *
2300 * When NULL is provided in second argument fit_conf_get_node() will search
2301 * for a default configuration node instead. Default configuration node unit name
2302 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2303 *
2304 * returns:
2305 * configuration node offset when found (>=0)
2306 * negative number on failure (FDT_ERR_* code)
2307 */
2308int fit_conf_get_node (const void *fit, const char *conf_uname)
2309{
2310 int noffset, confs_noffset;
2311 int len;
2312
2313 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2314 if (confs_noffset < 0) {
2315 debug ("Can't find configurations parent node '%s' (%s)\n",
2316 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2317 return confs_noffset;
2318 }
2319
2320 if (conf_uname == NULL) {
2321 /* get configuration unit name from the default property */
2322 debug ("No configuration specified, trying default...\n");
2323 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2324 if (conf_uname == NULL) {
2325 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2326 return len;
2327 }
2328 debug ("Found default configuration: '%s'\n", conf_uname);
2329 }
2330
2331 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2332 if (noffset < 0) {
2333 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2334 conf_uname, fdt_strerror (noffset));
2335 }
2336
2337 return noffset;
2338}
2339
2340static int __fit_conf_get_prop_node (const void *fit, int noffset,
2341 const char *prop_name)
2342{
2343 char *uname;
2344 int len;
2345
2346 /* get kernel image unit name from configuration kernel property */
2347 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2348 if (uname == NULL)
2349 return len;
2350
2351 return fit_image_get_node (fit, uname);
2352}
2353
2354/**
2355 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2356 * a given configuration
2357 * @fit: pointer to the FIT format image header
2358 * @noffset: configuration node offset
2359 *
2360 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2361 * configuration FIT_KERNEL_PROP property and translates it to the node
2362 * offset.
2363 *
2364 * returns:
2365 * image node offset when found (>=0)
2366 * negative number on failure (FDT_ERR_* code)
2367 */
2368int fit_conf_get_kernel_node (const void *fit, int noffset)
2369{
2370 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2371}
2372
2373/**
2374 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2375 * a given configuration
2376 * @fit: pointer to the FIT format image header
2377 * @noffset: configuration node offset
2378 *
2379 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2380 * configuration FIT_KERNEL_PROP property and translates it to the node
2381 * offset.
2382 *
2383 * returns:
2384 * image node offset when found (>=0)
2385 * negative number on failure (FDT_ERR_* code)
2386 */
2387int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2388{
2389 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2390}
2391
2392/**
2393 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2394 * a given configuration
2395 * @fit: pointer to the FIT format image header
2396 * @noffset: configuration node offset
2397 *
2398 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2399 * configuration FIT_KERNEL_PROP property and translates it to the node
2400 * offset.
2401 *
2402 * returns:
2403 * image node offset when found (>=0)
2404 * negative number on failure (FDT_ERR_* code)
2405 */
2406int fit_conf_get_fdt_node (const void *fit, int noffset)
2407{
2408 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2409}
2410
2411/**
2412 * fit_conf_print - prints out the FIT configuration details
2413 * @fit: pointer to the FIT format image header
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +01002414 * @noffset: offset of the configuration node
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002415 * @p: pointer to prefix string
2416 *
2417 * fit_conf_print() lists all mandatory properies for the processed
2418 * configuration node.
2419 *
2420 * returns:
2421 * no returned results
2422 */
2423void fit_conf_print (const void *fit, int noffset, const char *p)
2424{
2425 char *desc;
2426 char *uname;
2427 int ret;
2428
2429 /* Mandatory properties */
2430 ret = fit_get_desc (fit, noffset, &desc);
2431 printf ("%s Description: ", p);
2432 if (ret)
2433 printf ("unavailable\n");
2434 else
2435 printf ("%s\n", desc);
2436
2437 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2438 printf ("%s Kernel: ", p);
2439 if (uname == NULL)
2440 printf ("unavailable\n");
2441 else
2442 printf ("%s\n", uname);
2443
2444 /* Optional properties */
2445 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2446 if (uname)
2447 printf ("%s Init Ramdisk: %s\n", p, uname);
2448
2449 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2450 if (uname)
2451 printf ("%s FDT: %s\n", p, uname);
2452}
Marian Balakowicza50d5152008-03-12 10:12:37 +01002453
2454/**
2455 * fit_check_ramdisk - verify FIT format ramdisk subimage
2456 * @fit_hdr: pointer to the FIT ramdisk header
2457 * @rd_noffset: ramdisk subimage node offset within FIT image
2458 * @arch: requested ramdisk image architecture type
2459 * @verify: data CRC verification flag
2460 *
2461 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2462 * specified FIT image.
2463 *
2464 * returns:
2465 * 1, on success
2466 * 0, on failure
2467 */
2468#ifndef USE_HOSTCC
2469static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2470{
2471 fit_image_print (fit, rd_noffset, " ");
2472
2473 if (verify) {
2474 puts (" Verifying Hash Integrity ... ");
2475 if (!fit_image_check_hashes (fit, rd_noffset)) {
2476 puts ("Bad Data Hash\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002477 show_boot_progress (-125);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002478 return 0;
2479 }
2480 puts ("OK\n");
2481 }
2482
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002483 show_boot_progress (126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002484 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2485 !fit_image_check_arch (fit, rd_noffset, arch) ||
2486 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2487 printf ("No Linux %s Ramdisk Image\n",
2488 genimg_get_arch_name(arch));
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002489 show_boot_progress (-126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002490 return 0;
2491 }
2492
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002493 show_boot_progress (127);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002494 return 1;
2495}
2496#endif /* USE_HOSTCC */
Marian Balakowicz3413e222008-02-21 17:20:20 +01002497#endif /* CONFIG_FIT */