blob: f7faf504d428e3a63fe0d2b02194b8d5269df2d3 [file] [log] [blame]
Marian Balakowicz41d71ed2008-01-08 18:14:09 +01001/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
Marian Balakowicz9c701e82008-01-31 13:57:17 +010025
Marian Balakowicz9c701e82008-01-31 13:57:17 +010026
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010027#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +010028#include <common.h>
29#include <watchdog.h>
30
31#ifdef CONFIG_SHOW_BOOT_PROGRESS
32#include <status_led.h>
33#endif
34
35#ifdef CONFIG_HAS_DATAFLASH
36#include <dataflash.h>
37#endif
38
Marian Balakowicz2b77b9d2008-05-07 13:10:04 +020039#ifdef CONFIG_LOGBUFFER
40#include <logbuff.h>
41#endif
42
Marian Balakowicza1cc1472008-02-21 17:27:41 +010043#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
44#include <rtc.h>
45#endif
46
Marian Balakowicze227fbb2008-02-29 21:24:06 +010047#include <image.h>
48
Marian Balakowicza50d5152008-03-12 10:12:37 +010049#if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010050#include <fdt.h>
51#include <libfdt.h>
52#include <fdt_support.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010053#endif
54
55#if defined(CONFIG_FIT)
Andy Fleming72c23be2008-04-02 16:19:07 -050056#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010057#include <sha1.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010058
59static int fit_check_ramdisk (const void *fit, int os_noffset,
60 uint8_t arch, int verify);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010061#endif
62
Marian Balakowicz2efc2642008-01-31 13:58:13 +010063#ifdef CONFIG_CMD_BDI
64extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
65#endif
66
67DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz61fde552008-02-27 11:01:04 +010068
Marian Balakowicza0ffb422008-03-12 10:14:38 +010069static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
70 int verify);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010071#else
Marian Balakowicz95418502008-01-31 13:55:39 +010072#include "mkimage.h"
Andy Fleming72c23be2008-04-02 16:19:07 -050073#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010074#include <time.h>
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010075#include <image.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010076#endif /* !USE_HOSTCC*/
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010077
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010078typedef struct table_entry {
79 int id; /* as defined in image.h */
80 char *sname; /* short (input) name */
81 char *lname; /* long (output) name */
82} table_entry_t;
83
84static table_entry_t uimage_arch[] = {
85 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
86 { IH_ARCH_ALPHA, "alpha", "Alpha", },
87 { IH_ARCH_ARM, "arm", "ARM", },
88 { IH_ARCH_I386, "x86", "Intel x86", },
89 { IH_ARCH_IA64, "ia64", "IA64", },
90 { IH_ARCH_M68K, "m68k", "M68K", },
91 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
92 { IH_ARCH_MIPS, "mips", "MIPS", },
93 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
94 { IH_ARCH_NIOS, "nios", "NIOS", },
95 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Erickson80f51b82008-05-04 16:45:01 -070096 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010097 { IH_ARCH_PPC, "ppc", "PowerPC", },
98 { IH_ARCH_S390, "s390", "IBM S390", },
99 { IH_ARCH_SH, "sh", "SuperH", },
100 { IH_ARCH_SPARC, "sparc", "SPARC", },
101 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
102 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
103 { IH_ARCH_AVR32, "avr32", "AVR32", },
104 { -1, "", "", },
105};
106
107static table_entry_t uimage_os[] = {
108 { IH_OS_INVALID, NULL, "Invalid OS", },
109#if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
110 { IH_OS_ARTOS, "artos", "ARTOS", },
111#endif
112 { IH_OS_LINUX, "linux", "Linux", },
113#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
114 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
115#endif
116 { IH_OS_NETBSD, "netbsd", "NetBSD", },
117 { IH_OS_RTEMS, "rtems", "RTEMS", },
118 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
119#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
120 { IH_OS_QNX, "qnx", "QNX", },
121 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
122#endif
123#ifdef USE_HOSTCC
124 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
125 { IH_OS_DELL, "dell", "Dell", },
126 { IH_OS_ESIX, "esix", "Esix", },
127 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
128 { IH_OS_IRIX, "irix", "Irix", },
129 { IH_OS_NCR, "ncr", "NCR", },
130 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
131 { IH_OS_PSOS, "psos", "pSOS", },
132 { IH_OS_SCO, "sco", "SCO", },
133 { IH_OS_SOLARIS, "solaris", "Solaris", },
134 { IH_OS_SVR4, "svr4", "SVR4", },
135#endif
136 { -1, "", "", },
137};
138
139static table_entry_t uimage_type[] = {
140 { IH_TYPE_INVALID, NULL, "Invalid Image", },
141 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
142 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
143 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
144 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
145 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
146 { IH_TYPE_SCRIPT, "script", "Script", },
147 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
148 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
149 { -1, "", "", },
150};
151
152static table_entry_t uimage_comp[] = {
153 { IH_COMP_NONE, "none", "uncompressed", },
154 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
155 { IH_COMP_GZIP, "gzip", "gzip compressed", },
156 { -1, "", "", },
157};
158
Mike Frysingera2c67e92008-03-31 11:02:01 -0400159uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200160uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100161static void genimg_print_size (uint32_t size);
162#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
163static void genimg_print_time (time_t timestamp);
164#endif
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100165
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100166/*****************************************************************************/
167/* Legacy format routines */
168/*****************************************************************************/
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100169int image_check_hcrc (image_header_t *hdr)
170{
171 ulong hcrc;
172 ulong len = image_get_header_size ();
173 image_header_t header;
174
175 /* Copy header so we can blank CRC field for re-calculation */
176 memmove (&header, (char *)hdr, image_get_header_size ());
177 image_set_hcrc (&header, 0);
178
179 hcrc = crc32 (0, (unsigned char *)&header, len);
180
181 return (hcrc == image_get_hcrc (hdr));
182}
183
184int image_check_dcrc (image_header_t *hdr)
185{
186 ulong data = image_get_data (hdr);
187 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200188 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100189
190 return (dcrc == image_get_dcrc (hdr));
191}
192
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100193
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100194/**
195 * image_multi_count - get component (sub-image) count
196 * @hdr: pointer to the header of the multi component image
197 *
198 * image_multi_count() returns number of components in a multi
199 * component image.
200 *
201 * Note: no checking of the image type is done, caller must pass
202 * a valid multi component image.
203 *
204 * returns:
205 * number of components
206 */
207ulong image_multi_count (image_header_t *hdr)
208{
209 ulong i, count = 0;
Marian Balakowicza1474212008-02-29 16:00:06 +0100210 uint32_t *size;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100211
212 /* get start of the image payload, which in case of multi
213 * component images that points to a table of component sizes */
Marian Balakowicza1474212008-02-29 16:00:06 +0100214 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100215
216 /* count non empty slots */
217 for (i = 0; size[i]; ++i)
218 count++;
219
220 return count;
221}
222
223/**
224 * image_multi_getimg - get component data address and size
225 * @hdr: pointer to the header of the multi component image
226 * @idx: index of the requested component
227 * @data: pointer to a ulong variable, will hold component data address
228 * @len: pointer to a ulong variable, will hold component size
229 *
230 * image_multi_getimg() returns size and data address for the requested
231 * component in a multi component image.
232 *
233 * Note: no checking of the image type is done, caller must pass
234 * a valid multi component image.
235 *
236 * returns:
237 * data address and size of the component, if idx is valid
238 * 0 in data and len, if idx is out of range
239 */
240void image_multi_getimg (image_header_t *hdr, ulong idx,
241 ulong *data, ulong *len)
242{
243 int i;
Marian Balakowicza1474212008-02-29 16:00:06 +0100244 uint32_t *size;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100245 ulong offset, tail, count, img_data;
246
247 /* get number of component */
248 count = image_multi_count (hdr);
249
250 /* get start of the image payload, which in case of multi
251 * component images that points to a table of component sizes */
Marian Balakowicza1474212008-02-29 16:00:06 +0100252 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100253
254 /* get address of the proper component data start, which means
255 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowicza1474212008-02-29 16:00:06 +0100256 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100257
258 if (idx < count) {
Marian Balakowicza1474212008-02-29 16:00:06 +0100259 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100260 offset = 0;
261 tail = 0;
262
263 /* go over all indices preceding requested component idx */
264 for (i = 0; i < idx; i++) {
265 /* add up i-th component size */
Marian Balakowicza1474212008-02-29 16:00:06 +0100266 offset += uimage_to_cpu (size[i]);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100267
268 /* add up alignment for i-th component */
Marian Balakowicza1474212008-02-29 16:00:06 +0100269 tail += (4 - uimage_to_cpu (size[i]) % 4);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100270 }
271
272 /* calculate idx-th component data address */
273 *data = img_data + offset + tail;
274 } else {
275 *len = 0;
276 *data = 0;
277 }
278}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100279
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100280static void image_print_type (image_header_t *hdr)
281{
282 const char *os, *arch, *type, *comp;
283
284 os = genimg_get_os_name (image_get_os (hdr));
285 arch = genimg_get_arch_name (image_get_arch (hdr));
286 type = genimg_get_type_name (image_get_type (hdr));
287 comp = genimg_get_comp_name (image_get_comp (hdr));
288
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100289 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100290}
291
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100292/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200293 * image_print_contents - prints out the contents of the legacy format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100294 * @hdr: pointer to the legacy format image header
295 * @p: pointer to prefix string
296 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200297 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100298 * The routine prints out all header fields followed by the size/offset data
299 * for MULTI/SCRIPT images.
300 *
301 * returns:
302 * no returned results
303 */
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200304void image_print_contents (image_header_t *hdr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100305{
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200306 const char *p;
307
308#ifdef USE_HOSTCC
309 p = "";
310#else
311 p = " ";
312#endif
313
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100314 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
315#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
316 printf ("%sCreated: ", p);
317 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100318#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100319 printf ("%sImage Type: ", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100320 image_print_type (hdr);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100321 printf ("%sData Size: ", p);
322 genimg_print_size (image_get_data_size (hdr));
323 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
324 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100325
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100326 if (image_check_type (hdr, IH_TYPE_MULTI) ||
327 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100328 int i;
329 ulong data, len;
330 ulong count = image_multi_count (hdr);
331
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100332 printf ("%sContents:\n", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100333 for (i = 0; i < count; i++) {
334 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100335
336 printf ("%s Image %d: ", p, i);
337 genimg_print_size (len);
338
339 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
340 /*
341 * the user may need to know offsets
342 * if planning to do something with
343 * multiple files
344 */
345 printf ("%s Offset = 0x%08lx\n", p, data);
346 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100347 }
348 }
349}
350
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100351
352#ifndef USE_HOSTCC
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100353/**
354 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100355 * @rd_addr: ramdisk image start address
356 * @arch: expected ramdisk architecture
357 * @verify: checksum verification flag
358 *
359 * image_get_ramdisk() returns a pointer to the verified ramdisk image
360 * header. Routine receives image start address and expected architecture
361 * flag. Verification done covers data and header integrity and os/type/arch
362 * fields checking.
363 *
364 * If dataflash support is enabled routine checks for dataflash addresses
365 * and handles required dataflash reads.
366 *
367 * returns:
368 * pointer to a ramdisk image header, if image was found and valid
369 * otherwise, return NULL
370 */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100371static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
372 int verify)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100373{
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100374 image_header_t *rd_hdr = (image_header_t *)rd_addr;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100375
376 if (!image_check_magic (rd_hdr)) {
377 puts ("Bad Magic Number\n");
378 show_boot_progress (-10);
379 return NULL;
380 }
381
382 if (!image_check_hcrc (rd_hdr)) {
383 puts ("Bad Header Checksum\n");
384 show_boot_progress (-11);
385 return NULL;
386 }
387
388 show_boot_progress (10);
389 image_print_contents (rd_hdr);
390
391 if (verify) {
392 puts(" Verifying Checksum ... ");
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200393 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100394 puts ("Bad Data CRC\n");
395 show_boot_progress (-12);
396 return NULL;
397 }
398 puts("OK\n");
399 }
400
401 show_boot_progress (11);
402
403 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
404 !image_check_arch (rd_hdr, arch) ||
405 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
406 printf ("No Linux %s Ramdisk Image\n",
407 genimg_get_arch_name(arch));
408 show_boot_progress (-13);
409 return NULL;
410 }
411
412 return rd_hdr;
413}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100414#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100415
416/*****************************************************************************/
417/* Shared dual-format routines */
418/*****************************************************************************/
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100419#ifndef USE_HOSTCC
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200420int getenv_yesno (char *var)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100421{
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200422 char *s = getenv (var);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100423 return (s && (*s == 'n')) ? 0 : 1;
424}
425
426ulong getenv_bootm_low(void)
427{
428 char *s = getenv ("bootm_low");
429 if (s) {
430 ulong tmp = simple_strtoul (s, NULL, 16);
431 return tmp;
432 }
433
Marian Balakowicz2499b042008-03-12 12:14:15 +0100434#if defined(CFG_SDRAM_BASE)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100435 return CFG_SDRAM_BASE;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100436#elif defined(CONFIG_ARM)
437 return gd->bd->bi_dram[0].start;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100438#else
439 return 0;
440#endif
441}
442
443ulong getenv_bootm_size(void)
444{
445 char *s = getenv ("bootm_size");
446 if (s) {
447 ulong tmp = simple_strtoul (s, NULL, 16);
448 return tmp;
449 }
450
Marian Balakowicz2499b042008-03-12 12:14:15 +0100451#if defined(CONFIG_ARM)
452 return gd->bd->bi_dram[0].size;
453#else
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100454 return gd->bd->bi_memsize;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100455#endif
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100456}
457
458void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
459{
460#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
461 while (len > 0) {
462 size_t tail = (len > chunksz) ? chunksz : len;
463 WATCHDOG_RESET ();
464 memmove (to, from, tail);
465 to += tail;
466 from += tail;
467 len -= tail;
468 }
469#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
470 memmove (to, from, len);
471#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
472}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100473#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100474
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100475static void genimg_print_size (uint32_t size)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100476{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100477#ifndef USE_HOSTCC
478 printf ("%d Bytes = ", size);
479 print_size (size, "\n");
480#else
481 printf ("%d Bytes = %.2f kB = %.2f MB\n",
482 size, (double)size / 1.024e3,
483 (double)size / 1.048576e6);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100484#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100485}
486
487#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
488static void genimg_print_time (time_t timestamp)
489{
490#ifndef USE_HOSTCC
491 struct rtc_time tm;
492
493 to_tm (timestamp, &tm);
494 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
495 tm.tm_year, tm.tm_mon, tm.tm_mday,
496 tm.tm_hour, tm.tm_min, tm.tm_sec);
497#else
498 printf ("%s", ctime(&timestamp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100499#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100500}
501#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
502
503/**
504 * get_table_entry_name - translate entry id to long name
505 * @table: pointer to a translation table for entries of a specific type
506 * @msg: message to be returned when translation fails
507 * @id: entry id to be translated
508 *
509 * get_table_entry_name() will go over translation table trying to find
510 * entry that matches given id. If matching entry is found, its long
511 * name is returned to the caller.
512 *
513 * returns:
514 * long entry name if translation succeeds
515 * msg otherwise
516 */
517static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
518{
519 for (; table->id >= 0; ++table) {
520 if (table->id == id)
521 return (table->lname);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100522 }
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100523 return (msg);
524}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100525
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100526const char *genimg_get_os_name (uint8_t os)
527{
528 return (get_table_entry_name (uimage_os, "Unknown OS", os));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100529}
530
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100531const char *genimg_get_arch_name (uint8_t arch)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100532{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100533 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
534}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100535
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100536const char *genimg_get_type_name (uint8_t type)
537{
538 return (get_table_entry_name (uimage_type, "Unknown Image", type));
539}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100540
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100541const char *genimg_get_comp_name (uint8_t comp)
542{
543 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100544}
545
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100546/**
547 * get_table_entry_id - translate short entry name to id
548 * @table: pointer to a translation table for entries of a specific type
549 * @table_name: to be used in case of error
550 * @name: entry short name to be translated
551 *
552 * get_table_entry_id() will go over translation table trying to find
553 * entry that matches given short name. If matching entry is found,
554 * its id returned to the caller.
555 *
556 * returns:
557 * entry id if translation succeeds
558 * -1 otherwise
559 */
560static int get_table_entry_id (table_entry_t *table,
561 const char *table_name, const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100562{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100563 table_entry_t *t;
564#ifdef USE_HOSTCC
565 int first = 1;
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100566
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100567 for (t = table; t->id >= 0; ++t) {
568 if (t->sname && strcasecmp(t->sname, name) == 0)
569 return (t->id);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100570 }
571
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100572 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
573 for (t = table; t->id >= 0; ++t) {
574 if (t->sname == NULL)
575 continue;
576 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
577 first = 0;
578 }
579 fprintf (stderr, "\n");
580#else
581 for (t = table; t->id >= 0; ++t) {
582 if (t->sname && strcmp(t->sname, name) == 0)
583 return (t->id);
584 }
585 debug ("Invalid %s Type: %s\n", table_name, name);
586#endif /* USE_HOSTCC */
587 return (-1);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100588}
589
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100590int genimg_get_os_id (const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100591{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100592 return (get_table_entry_id (uimage_os, "OS", name));
593}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100594
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100595int genimg_get_arch_id (const char *name)
596{
597 return (get_table_entry_id (uimage_arch, "CPU", name));
598}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100599
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100600int genimg_get_type_id (const char *name)
601{
602 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100603}
Marian Balakowicz95418502008-01-31 13:55:39 +0100604
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100605int genimg_get_comp_id (const char *name)
606{
607 return (get_table_entry_id (uimage_comp, "Compression", name));
608}
609
610#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +0100611/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100612 * genimg_get_format - get image format type
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100613 * @img_addr: image start address
614 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100615 * genimg_get_format() checks whether provided address points to a valid
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100616 * legacy or FIT image.
617 *
Marian Balakowicz12560682008-02-27 11:02:26 +0100618 * New uImage format and FDT blob are based on a libfdt. FDT blob
619 * may be passed directly or embedded in a FIT image. In both situations
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100620 * genimg_get_format() must be able to dectect libfdt header.
Marian Balakowicz12560682008-02-27 11:02:26 +0100621 *
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100622 * returns:
623 * image format type or IMAGE_FORMAT_INVALID if no image is present
624 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100625int genimg_get_format (void *img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100626{
627 ulong format = IMAGE_FORMAT_INVALID;
628 image_header_t *hdr;
Marian Balakowicz12560682008-02-27 11:02:26 +0100629#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100630 char *fit_hdr;
631#endif
632
633 hdr = (image_header_t *)img_addr;
634 if (image_check_magic(hdr))
635 format = IMAGE_FORMAT_LEGACY;
Marian Balakowicz12560682008-02-27 11:02:26 +0100636#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100637 else {
638 fit_hdr = (char *)img_addr;
639 if (fdt_check_header (fit_hdr) == 0)
640 format = IMAGE_FORMAT_FIT;
641 }
642#endif
643
644 return format;
645}
646
647/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100648 * genimg_get_image - get image from special storage (if necessary)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100649 * @img_addr: image start address
650 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100651 * genimg_get_image() checks if provided image start adddress is located
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100652 * in a dataflash storage. If so, image is moved to a system RAM memory.
653 *
654 * returns:
655 * image start address after possible relocation from special storage
656 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100657ulong genimg_get_image (ulong img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100658{
Marian Balakowicz535b9102008-02-27 11:00:47 +0100659 ulong ram_addr = img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100660
661#ifdef CONFIG_HAS_DATAFLASH
Marian Balakowicz535b9102008-02-27 11:00:47 +0100662 ulong h_size, d_size;
663
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100664 if (addr_dataflash (img_addr)){
Marian Balakowicz535b9102008-02-27 11:00:47 +0100665 /* ger RAM address */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100666 ram_addr = CFG_LOAD_ADDR;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100667
668 /* get header size */
669 h_size = image_get_header_size ();
670#if defined(CONFIG_FIT)
671 if (sizeof(struct fdt_header) > h_size)
672 h_size = sizeof(struct fdt_header);
673#endif
674
675 /* read in header */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100676 debug (" Reading image header from dataflash address "
677 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100678
Marian Balakowicz535b9102008-02-27 11:00:47 +0100679 read_dataflash (img_addr, h_size, (char *)ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100680
Marian Balakowicz535b9102008-02-27 11:00:47 +0100681 /* get data size */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100682 switch (genimg_get_format ((void *)ram_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100683 case IMAGE_FORMAT_LEGACY:
684 d_size = image_get_data_size ((image_header_t *)ram_addr);
685 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
686 ram_addr, d_size);
687 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100688#if defined(CONFIG_FIT)
Marian Balakowicz535b9102008-02-27 11:00:47 +0100689 case IMAGE_FORMAT_FIT:
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100690 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100691 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
692 ram_addr, d_size);
693 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100694#endif
Marian Balakowicz535b9102008-02-27 11:00:47 +0100695 default:
696 printf (" No valid image found at 0x%08lx\n", img_addr);
697 return ram_addr;
698 }
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100699
Marian Balakowicz535b9102008-02-27 11:00:47 +0100700 /* read in image data */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100701 debug (" Reading image remaining data from dataflash address "
702 "%08lx to RAM address %08lx\n", img_addr + h_size,
703 ram_addr + h_size);
704
705 read_dataflash (img_addr + h_size, d_size,
706 (char *)(ram_addr + h_size));
Marian Balakowicz535b9102008-02-27 11:00:47 +0100707
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100708 }
Marian Balakowicz535b9102008-02-27 11:00:47 +0100709#endif /* CONFIG_HAS_DATAFLASH */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100710
711 return ram_addr;
712}
713
714/**
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100715 * fit_has_config - check if there is a valid FIT configuration
716 * @images: pointer to the bootm command headers structure
717 *
718 * fit_has_config() checks if there is a FIT configuration in use
719 * (if FTI support is present).
720 *
721 * returns:
722 * 0, no FIT support or no configuration found
723 * 1, configuration found
724 */
725int genimg_has_config (bootm_headers_t *images)
726{
727#if defined(CONFIG_FIT)
728 if (images->fit_uname_cfg)
729 return 1;
730#endif
731 return 0;
732}
733
734/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100735 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz95418502008-01-31 13:55:39 +0100736 * @argc: command argument count
737 * @argv: command argument list
Marian Balakowicz61fde552008-02-27 11:01:04 +0100738 * @images: pointer to the bootm images structure
Marian Balakowicz95418502008-01-31 13:55:39 +0100739 * @arch: expected ramdisk architecture
740 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
741 * @rd_end: pointer to a ulong variable, will hold ramdisk end
742 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100743 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz95418502008-01-31 13:55:39 +0100744 * Curently supported are the following ramdisk sources:
745 * - multicomponent kernel/ramdisk image,
746 * - commandline provided address of decicated ramdisk image.
747 *
748 * returns:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100749 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz95418502008-01-31 13:55:39 +0100750 * rd_start and rd_end are set to ramdisk start/end addresses if
751 * ramdisk image is found and valid
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100752 *
753 * 1, if ramdisk image is found but corrupted
Marian Balakowicz95418502008-01-31 13:55:39 +0100754 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz95418502008-01-31 13:55:39 +0100755 */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100756int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
757 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz95418502008-01-31 13:55:39 +0100758{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100759 ulong rd_addr, rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100760 ulong rd_data, rd_len;
761 image_header_t *rd_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100762#if defined(CONFIG_FIT)
763 void *fit_hdr;
764 const char *fit_uname_config = NULL;
765 const char *fit_uname_ramdisk = NULL;
766 ulong default_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100767 int rd_noffset;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100768 int cfg_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100769 const void *data;
770 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100771#endif
Marian Balakowicz95418502008-01-31 13:55:39 +0100772
Marian Balakowicza50d5152008-03-12 10:12:37 +0100773 *rd_start = 0;
774 *rd_end = 0;
775
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100776 /*
777 * Look for a '-' which indicates to ignore the
778 * ramdisk argument
779 */
780 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
781 debug ("## Skipping init Ramdisk\n");
782 rd_len = rd_data = 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100783 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100784#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100785 if (argc >= 3) {
786 /*
787 * If the init ramdisk comes from the FIT image and
788 * the FIT image address is omitted in the command
789 * line argument, try to use os FIT image address or
790 * default load address.
791 */
792 if (images->fit_uname_os)
793 default_addr = (ulong)images->fit_hdr_os;
794 else
795 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100796
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100797 if (fit_parse_conf (argv[2], default_addr,
798 &rd_addr, &fit_uname_config)) {
799 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
800 fit_uname_config, rd_addr);
801 } else if (fit_parse_subimage (argv[2], default_addr,
802 &rd_addr, &fit_uname_ramdisk)) {
803 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
804 fit_uname_ramdisk, rd_addr);
805 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100806#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100807 {
808 rd_addr = simple_strtoul(argv[2], NULL, 16);
809 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
810 rd_addr);
811 }
812#if defined(CONFIG_FIT)
813 } else {
814 /* use FIT configuration provided in first bootm
815 * command argument
816 */
817 rd_addr = (ulong)images->fit_hdr_os;
818 fit_uname_config = images->fit_uname_cfg;
819 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
820 fit_uname_config, rd_addr);
821
822 /*
823 * Check whether configuration has ramdisk defined,
824 * if not, don't try to use it, quit silently.
825 */
826 fit_hdr = (void *)rd_addr;
827 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
828 if (cfg_noffset < 0) {
829 debug ("* ramdisk: no such config\n");
830 return 0;
831 }
832
833 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
834 if (rd_noffset < 0) {
835 debug ("* ramdisk: no ramdisk in config\n");
836 return 0;
837 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100838 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100839#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100840
841 /* copy from dataflash if needed */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100842 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100843
844 /*
845 * Check if there is an initrd image at the
846 * address provided in the second bootm argument
847 * check image type, for FIT images get FIT node.
848 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100849 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100850 case IMAGE_FORMAT_LEGACY:
Marian Balakowicza50d5152008-03-12 10:12:37 +0100851 printf ("## Loading init Ramdisk from Legacy "
852 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz95418502008-01-31 13:55:39 +0100853
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100854 show_boot_progress (9);
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100855 rd_hdr = image_get_ramdisk (rd_addr, arch,
856 images->verify);
Marian Balakowicz95418502008-01-31 13:55:39 +0100857
Marian Balakowicza50d5152008-03-12 10:12:37 +0100858 if (rd_hdr == NULL)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600859 return 1;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600860
Marian Balakowicz95418502008-01-31 13:55:39 +0100861 rd_data = image_get_data (rd_hdr);
862 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100863 rd_load = image_get_load (rd_hdr);
864 break;
865#if defined(CONFIG_FIT)
866 case IMAGE_FORMAT_FIT:
867 fit_hdr = (void *)rd_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100868 printf ("## Loading init Ramdisk from FIT "
869 "Image at %08lx ...\n", rd_addr);
870
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100871 show_boot_progress (120);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100872 if (!fit_check_format (fit_hdr)) {
873 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100874 show_boot_progress (-120);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100875 return 0;
876 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100877 show_boot_progress (121);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100878
879 if (!fit_uname_ramdisk) {
880 /*
881 * no ramdisk image node unit name, try to get config
882 * node first. If config unit node name is NULL
883 * fit_conf_get_node() will try to find default config node
884 */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100885 show_boot_progress (122);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100886 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
887 if (cfg_noffset < 0) {
888 puts ("Could not find configuration node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100889 show_boot_progress (-122);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100890 return 0;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100891 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100892 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
893 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100894
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100895 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100896 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
897 } else {
898 /* get ramdisk component image node offset */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100899 show_boot_progress (123);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100900 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
901 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100902 if (rd_noffset < 0) {
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100903 puts ("Could not find subimage node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100904 show_boot_progress (-124);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100905 return 0;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100906 }
Marian Balakowicza50d5152008-03-12 10:12:37 +0100907
908 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
909
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100910 show_boot_progress (125);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100911 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
912 return 0;
913
914 /* get ramdisk image data address and length */
915 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
916 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100917 show_boot_progress (-127);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100918 return 0;
919 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100920 show_boot_progress (128);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100921
922 rd_data = (ulong)data;
923 rd_len = size;
924
925 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
926 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100927 show_boot_progress (-129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100928 return 0;
929 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100930 show_boot_progress (129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100931
932 images->fit_hdr_rd = fit_hdr;
933 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100934 images->fit_noffset_rd = rd_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100935 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100936#endif
937 default:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100938 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowicza50d5152008-03-12 10:12:37 +0100939 rd_data = rd_len = rd_load = 0;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100940 }
Marian Balakowicz95418502008-01-31 13:55:39 +0100941
942#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100943 /*
944 * We need to copy the ramdisk to SRAM to let Linux boot
945 */
946 if (rd_data) {
947 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
948 rd_data = rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100949 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100950#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
Marian Balakowicz95418502008-01-31 13:55:39 +0100951
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100952 } else if (images->legacy_hdr_valid &&
Marian Balakowiczab00e022008-04-11 11:07:49 +0200953 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowicz95418502008-01-31 13:55:39 +0100954 /*
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100955 * Now check if we have a legacy mult-component image,
956 * get second entry data start address and len.
Marian Balakowicz95418502008-01-31 13:55:39 +0100957 */
958 show_boot_progress (13);
959 printf ("## Loading init Ramdisk from multi component "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100960 "Legacy Image at %08lx ...\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100961 (ulong)images->legacy_hdr_os);
962
963 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz95418502008-01-31 13:55:39 +0100964 } else {
965 /*
966 * no initrd image
967 */
968 show_boot_progress (14);
969 rd_len = rd_data = 0;
970 }
971
972 if (!rd_data) {
973 debug ("## No init Ramdisk\n");
Marian Balakowicz95418502008-01-31 13:55:39 +0100974 } else {
975 *rd_start = rd_data;
976 *rd_end = rd_data + rd_len;
977 }
978 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
979 *rd_start, *rd_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600980
981 return 0;
Marian Balakowicz95418502008-01-31 13:55:39 +0100982}
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100983
Daniel Hellstrombb9c8292008-03-28 08:29:26 +0100984#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100985/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100986 * boot_ramdisk_high - relocate init ramdisk
Kumar Galab937bb72008-02-27 21:51:49 -0600987 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100988 * @rd_data: ramdisk data start address
989 * @rd_len: ramdisk data length
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100990 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
991 * start address (after possible relocation)
992 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
993 * end address (after possible relocation)
994 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100995 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100996 * variable and if requested ramdisk data is moved to a specified location.
997 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100998 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
999 * start/end addresses if ramdisk image start and len were provided,
1000 * otherwise set initrd_start and initrd_end set to zeros.
1001 *
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001002 * returns:
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001003 * 0 - success
1004 * -1 - failure
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001005 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001006int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galab937bb72008-02-27 21:51:49 -06001007 ulong *initrd_start, ulong *initrd_end)
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001008{
1009 char *s;
1010 ulong initrd_high;
1011 int initrd_copy_to_ram = 1;
1012
1013 if ((s = getenv ("initrd_high")) != NULL) {
1014 /* a value of "no" or a similar string will act like 0,
1015 * turning the "load high" feature off. This is intentional.
1016 */
1017 initrd_high = simple_strtoul (s, NULL, 16);
1018 if (initrd_high == ~0)
1019 initrd_copy_to_ram = 0;
1020 } else {
1021 /* not set, no restrictions to load high */
1022 initrd_high = ~0;
1023 }
1024
Marian Balakowicz2b77b9d2008-05-07 13:10:04 +02001025 /* Prevent initrd from overwriting logbuffer */
1026 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1027
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001028 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1029 initrd_high, initrd_copy_to_ram);
1030
1031 if (rd_data) {
1032 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1033 debug (" in-place initrd\n");
1034 *initrd_start = rd_data;
1035 *initrd_end = rd_data + rd_len;
Kumar Galab937bb72008-02-27 21:51:49 -06001036 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001037 } else {
Kumar Galab937bb72008-02-27 21:51:49 -06001038 if (initrd_high)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001039 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galab937bb72008-02-27 21:51:49 -06001040 else
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001041 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001042
Kumar Galab937bb72008-02-27 21:51:49 -06001043 if (*initrd_start == 0) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001044 puts ("ramdisk - allocation error\n");
Kumar Galab937bb72008-02-27 21:51:49 -06001045 goto error;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001046 }
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001047 show_boot_progress (12);
1048
1049 *initrd_end = *initrd_start + rd_len;
1050 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1051 *initrd_start, *initrd_end);
1052
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001053 memmove_wd ((void *)*initrd_start,
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001054 (void *)rd_data, rd_len, CHUNKSZ);
1055
1056 puts ("OK\n");
1057 }
1058 } else {
1059 *initrd_start = 0;
1060 *initrd_end = 0;
1061 }
1062 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1063 *initrd_start, *initrd_end);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001064
Kumar Galab937bb72008-02-27 21:51:49 -06001065 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001066
Kumar Galab937bb72008-02-27 21:51:49 -06001067error:
1068 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001069}
1070
1071/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001072 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galab937bb72008-02-27 21:51:49 -06001073 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001074 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1075 * @cmd_end: pointer to a ulong variable, will hold cmdline end
Kumar Galab937bb72008-02-27 21:51:49 -06001076 * @bootmap_base: ulong variable, holds offset in physical memory to
1077 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001078 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001079 * boot_get_cmdline() allocates space for kernel command line below
Kumar Galab937bb72008-02-27 21:51:49 -06001080 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001081 * variable is present its contents is copied to allocated kernel
1082 * command line.
1083 *
1084 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001085 * 0 - success
1086 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001087 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001088int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
Kumar Galab937bb72008-02-27 21:51:49 -06001089 ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001090{
1091 char *cmdline;
1092 char *s;
1093
Kumar Galab937bb72008-02-27 21:51:49 -06001094 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1095 CFG_BOOTMAPSZ + bootmap_base);
1096
1097 if (cmdline == NULL)
1098 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001099
1100 if ((s = getenv("bootargs")) == NULL)
1101 s = "";
1102
1103 strcpy(cmdline, s);
1104
1105 *cmd_start = (ulong) & cmdline[0];
1106 *cmd_end = *cmd_start + strlen(cmdline);
1107
1108 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1109
Kumar Galab937bb72008-02-27 21:51:49 -06001110 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001111}
1112
1113/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001114 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galab937bb72008-02-27 21:51:49 -06001115 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001116 * @kbd: double pointer to board info data
Kumar Galab937bb72008-02-27 21:51:49 -06001117 * @bootmap_base: ulong variable, holds offset in physical memory to
1118 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001119 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001120 * boot_get_kbd() allocates space for kernel copy of board info data below
Kumar Galab937bb72008-02-27 21:51:49 -06001121 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1122 * the current u-boot board info data.
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001123 *
1124 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001125 * 0 - success
1126 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001127 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001128int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001129{
Kumar Galab937bb72008-02-27 21:51:49 -06001130 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1131 CFG_BOOTMAPSZ + bootmap_base);
1132 if (*kbd == NULL)
1133 return -1;
1134
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001135 **kbd = *(gd->bd);
1136
1137 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1138
1139#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1140 do_bdinfo(NULL, 0, 0, NULL);
1141#endif
1142
Kumar Galab937bb72008-02-27 21:51:49 -06001143 return 0;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001144}
1145#endif /* CONFIG_PPC || CONFIG_M68K */
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001146#endif /* !USE_HOSTCC */
Marian Balakowicz95418502008-01-31 13:55:39 +01001147
Marian Balakowicz3413e222008-02-21 17:20:20 +01001148#if defined(CONFIG_FIT)
1149/*****************************************************************************/
1150/* New uImage format routines */
1151/*****************************************************************************/
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001152#ifndef USE_HOSTCC
Marian Balakowicz3413e222008-02-21 17:20:20 +01001153static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1154 ulong *addr, const char **name)
1155{
1156 const char *sep;
1157
1158 *addr = addr_curr;
1159 *name = NULL;
1160
1161 sep = strchr (spec, sepc);
1162 if (sep) {
1163 if (sep - spec > 0)
1164 *addr = simple_strtoul (spec, NULL, 16);
1165
1166 *name = sep + 1;
1167 return 1;
1168 }
1169
1170 return 0;
1171}
1172
1173/**
1174 * fit_parse_conf - parse FIT configuration spec
1175 * @spec: input string, containing configuration spec
1176 * @add_curr: current image address (to be used as a possible default)
1177 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1178 * configuration
1179 * @conf_name double pointer to a char, will hold pointer to a configuration
1180 * unit name
1181 *
1182 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1183 * where <addr> is a FIT image address that contains configuration
1184 * with a <conf> unit name.
1185 *
1186 * Address part is optional, and if omitted default add_curr will
1187 * be used instead.
1188 *
1189 * returns:
1190 * 1 if spec is a valid configuration string,
1191 * addr and conf_name are set accordingly
1192 * 0 otherwise
1193 */
1194inline int fit_parse_conf (const char *spec, ulong addr_curr,
1195 ulong *addr, const char **conf_name)
1196{
1197 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1198}
1199
1200/**
1201 * fit_parse_subimage - parse FIT subimage spec
1202 * @spec: input string, containing subimage spec
1203 * @add_curr: current image address (to be used as a possible default)
1204 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1205 * subimage
1206 * @image_name: double pointer to a char, will hold pointer to a subimage name
1207 *
1208 * fit_parse_subimage() expects subimage spec in the for of
1209 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1210 * subimage with a <subimg> unit name.
1211 *
1212 * Address part is optional, and if omitted default add_curr will
1213 * be used instead.
1214 *
1215 * returns:
1216 * 1 if spec is a valid subimage string,
1217 * addr and image_name are set accordingly
1218 * 0 otherwise
1219 */
1220inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1221 ulong *addr, const char **image_name)
1222{
1223 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1224}
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001225#endif /* !USE_HOSTCC */
1226
1227static void fit_get_debug (const void *fit, int noffset,
1228 char *prop_name, int err)
1229{
1230 debug ("Can't get '%s' property from FIT 0x%08lx, "
1231 "node: offset %d, name %s (%s)\n",
1232 prop_name, (ulong)fit, noffset,
1233 fit_get_name (fit, noffset, NULL),
1234 fdt_strerror (err));
1235}
1236
1237/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001238 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001239 * @fit: pointer to the FIT format image header
1240 * @p: pointer to prefix string
1241 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001242 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001243 * The routine prints out FIT image properties (root node level) follwed by
1244 * the details of each component image.
1245 *
1246 * returns:
1247 * no returned results
1248 */
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001249void fit_print_contents (const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001250{
1251 char *desc;
1252 char *uname;
1253 int images_noffset;
1254 int confs_noffset;
1255 int noffset;
1256 int ndepth;
1257 int count = 0;
1258 int ret;
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001259 const char *p;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001260#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1261 time_t timestamp;
1262#endif
1263
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001264#ifdef USE_HOSTCC
1265 p = "";
1266#else
1267 p = " ";
1268#endif
1269
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001270 /* Root node properties */
1271 ret = fit_get_desc (fit, 0, &desc);
1272 printf ("%sFIT description: ", p);
1273 if (ret)
1274 printf ("unavailable\n");
1275 else
1276 printf ("%s\n", desc);
1277
1278#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1279 ret = fit_get_timestamp (fit, 0, &timestamp);
1280 printf ("%sCreated: ", p);
1281 if (ret)
1282 printf ("unavailable\n");
1283 else
1284 genimg_print_time (timestamp);
1285#endif
1286
1287 /* Find images parent node offset */
1288 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1289 if (images_noffset < 0) {
1290 printf ("Can't find images parent node '%s' (%s)\n",
1291 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1292 return;
1293 }
1294
1295 /* Process its subnodes, print out component images details */
1296 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1297 (noffset >= 0) && (ndepth > 0);
1298 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1299 if (ndepth == 1) {
1300 /*
1301 * Direct child node of the images parent node,
1302 * i.e. component image node.
1303 */
1304 printf ("%s Image %u (%s)\n", p, count++,
1305 fit_get_name(fit, noffset, NULL));
1306
1307 fit_image_print (fit, noffset, p);
1308 }
1309 }
1310
1311 /* Find configurations parent node offset */
1312 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1313 if (confs_noffset < 0) {
1314 debug ("Can't get configurations parent node '%s' (%s)\n",
1315 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1316 return;
1317 }
1318
1319 /* get default configuration unit name from default property */
1320 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1321 if (uname)
1322 printf ("%s Default Configuration: '%s'\n", p, uname);
1323
1324 /* Process its subnodes, print out configurations details */
1325 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1326 (noffset >= 0) && (ndepth > 0);
1327 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1328 if (ndepth == 1) {
1329 /*
1330 * Direct child node of the configurations parent node,
1331 * i.e. configuration node.
1332 */
1333 printf ("%s Configuration %u (%s)\n", p, count++,
1334 fit_get_name(fit, noffset, NULL));
1335
1336 fit_conf_print (fit, noffset, p);
1337 }
1338 }
1339}
1340
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001341/**
1342 * fit_image_print - prints out the FIT component image details
1343 * @fit: pointer to the FIT format image header
1344 * @image_noffset: offset of the component image node
1345 * @p: pointer to prefix string
1346 *
1347 * fit_image_print() lists all mandatory properies for the processed component
1348 * image. If present, hash nodes are printed out as well.
1349 *
1350 * returns:
1351 * no returned results
1352 */
1353void fit_image_print (const void *fit, int image_noffset, const char *p)
1354{
1355 char *desc;
1356 uint8_t type, arch, os, comp;
1357 size_t size;
1358 ulong load, entry;
1359 const void *data;
1360 int noffset;
1361 int ndepth;
1362 int ret;
1363
1364 /* Mandatory properties */
1365 ret = fit_get_desc (fit, image_noffset, &desc);
1366 printf ("%s Description: ", p);
1367 if (ret)
1368 printf ("unavailable\n");
1369 else
1370 printf ("%s\n", desc);
1371
1372 fit_image_get_type (fit, image_noffset, &type);
1373 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1374
1375 fit_image_get_comp (fit, image_noffset, &comp);
1376 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1377
1378 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1379
1380#ifndef USE_HOSTCC
1381 printf ("%s Data Start: ", p);
1382 if (ret)
1383 printf ("unavailable\n");
1384 else
1385 printf ("0x%08lx\n", (ulong)data);
1386#endif
1387
1388 printf ("%s Data Size: ", p);
1389 if (ret)
1390 printf ("unavailable\n");
1391 else
1392 genimg_print_size (size);
1393
1394 /* Remaining, type dependent properties */
1395 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1396 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1397 (type == IH_TYPE_FLATDT)) {
1398 fit_image_get_arch (fit, image_noffset, &arch);
1399 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1400 }
1401
1402 if (type == IH_TYPE_KERNEL) {
1403 fit_image_get_os (fit, image_noffset, &os);
1404 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1405 }
1406
1407 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1408 ret = fit_image_get_load (fit, image_noffset, &load);
1409 printf ("%s Load Address: ", p);
1410 if (ret)
1411 printf ("unavailable\n");
1412 else
1413 printf ("0x%08lx\n", load);
1414
1415 fit_image_get_entry (fit, image_noffset, &entry);
1416 printf ("%s Entry Point: ", p);
1417 if (ret)
1418 printf ("unavailable\n");
1419 else
1420 printf ("0x%08lx\n", entry);
1421 }
1422
1423 /* Process all hash subnodes of the component image node */
1424 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1425 (noffset >= 0) && (ndepth > 0);
1426 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1427 if (ndepth == 1) {
1428 /* Direct child node of the component image node */
1429 fit_image_print_hash (fit, noffset, p);
1430 }
1431 }
1432}
1433
1434/**
1435 * fit_image_print_hash - prints out the hash node details
1436 * @fit: pointer to the FIT format image header
1437 * @noffset: offset of the hash node
1438 * @p: pointer to prefix string
1439 *
1440 * fit_image_print_hash() lists properies for the processed hash node
1441 *
1442 * returns:
1443 * no returned results
1444 */
1445void fit_image_print_hash (const void *fit, int noffset, const char *p)
1446{
1447 char *algo;
1448 uint8_t *value;
1449 int value_len;
1450 int i, ret;
1451
1452 /*
1453 * Check subnode name, must be equal to "hash".
1454 * Multiple hash nodes require unique unit node
1455 * names, e.g. hash@1, hash@2, etc.
1456 */
1457 if (strncmp (fit_get_name(fit, noffset, NULL),
1458 FIT_HASH_NODENAME,
1459 strlen(FIT_HASH_NODENAME)) != 0)
1460 return;
1461
1462 debug ("%s Hash node: '%s'\n", p,
1463 fit_get_name (fit, noffset, NULL));
1464
1465 printf ("%s Hash algo: ", p);
1466 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1467 printf ("invalid/unsupported\n");
1468 return;
1469 }
1470 printf ("%s\n", algo);
1471
1472 ret = fit_image_hash_get_value (fit, noffset, &value,
1473 &value_len);
1474 printf ("%s Hash value: ", p);
1475 if (ret) {
1476 printf ("unavailable\n");
1477 } else {
1478 for (i = 0; i < value_len; i++)
1479 printf ("%02x", value[i]);
1480 printf ("\n");
1481 }
1482
1483 debug ("%s Hash len: %d\n", p, value_len);
1484}
1485
1486/**
1487 * fit_get_desc - get node description property
1488 * @fit: pointer to the FIT format image header
1489 * @noffset: node offset
1490 * @desc: double pointer to the char, will hold pointer to the descrption
1491 *
1492 * fit_get_desc() reads description property from a given node, if
1493 * description is found pointer to it is returened in third call argument.
1494 *
1495 * returns:
1496 * 0, on success
1497 * -1, on failure
1498 */
1499int fit_get_desc (const void *fit, int noffset, char **desc)
1500{
1501 int len;
1502
1503 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1504 if (*desc == NULL) {
1505 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1506 return -1;
1507 }
1508
1509 return 0;
1510}
1511
1512/**
1513 * fit_get_timestamp - get node timestamp property
1514 * @fit: pointer to the FIT format image header
1515 * @noffset: node offset
1516 * @timestamp: pointer to the time_t, will hold read timestamp
1517 *
1518 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1519 * is found and has a correct size its value is retured in third call
1520 * argument.
1521 *
1522 * returns:
1523 * 0, on success
1524 * -1, on property read failure
1525 * -2, on wrong timestamp size
1526 */
1527int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1528{
1529 int len;
1530 const void *data;
1531
1532 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1533 if (data == NULL) {
1534 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1535 return -1;
1536 }
1537 if (len != sizeof (uint32_t)) {
1538 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1539 return -2;
1540 }
1541
1542 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1543 return 0;
1544}
1545
1546/**
1547 * fit_image_get_node - get node offset for component image of a given unit name
1548 * @fit: pointer to the FIT format image header
1549 * @image_uname: component image node unit name
1550 *
1551 * fit_image_get_node() finds a component image (withing the '/images'
1552 * node) of a provided unit name. If image is found its node offset is
1553 * returned to the caller.
1554 *
1555 * returns:
1556 * image node offset when found (>=0)
1557 * negative number on failure (FDT_ERR_* code)
1558 */
1559int fit_image_get_node (const void *fit, const char *image_uname)
1560{
1561 int noffset, images_noffset;
1562
1563 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1564 if (images_noffset < 0) {
1565 debug ("Can't find images parent node '%s' (%s)\n",
1566 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1567 return images_noffset;
1568 }
1569
1570 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1571 if (noffset < 0) {
1572 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1573 image_uname, fdt_strerror (noffset));
1574 }
1575
1576 return noffset;
1577}
1578
1579/**
1580 * fit_image_get_os - get os id for a given component image node
1581 * @fit: pointer to the FIT format image header
1582 * @noffset: component image node offset
1583 * @os: pointer to the uint8_t, will hold os numeric id
1584 *
1585 * fit_image_get_os() finds os property in a given component image node.
1586 * If the property is found, its (string) value is translated to the numeric
1587 * id which is returned to the caller.
1588 *
1589 * returns:
1590 * 0, on success
1591 * -1, on failure
1592 */
1593int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1594{
1595 int len;
1596 const void *data;
1597
1598 /* Get OS name from property data */
1599 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1600 if (data == NULL) {
1601 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1602 *os = -1;
1603 return -1;
1604 }
1605
1606 /* Translate OS name to id */
1607 *os = genimg_get_os_id (data);
1608 return 0;
1609}
1610
1611/**
1612 * fit_image_get_arch - get arch id for a given component image node
1613 * @fit: pointer to the FIT format image header
1614 * @noffset: component image node offset
1615 * @arch: pointer to the uint8_t, will hold arch numeric id
1616 *
1617 * fit_image_get_arch() finds arch property in a given component image node.
1618 * If the property is found, its (string) value is translated to the numeric
1619 * id which is returned to the caller.
1620 *
1621 * returns:
1622 * 0, on success
1623 * -1, on failure
1624 */
1625int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1626{
1627 int len;
1628 const void *data;
1629
1630 /* Get architecture name from property data */
1631 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1632 if (data == NULL) {
1633 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1634 *arch = -1;
1635 return -1;
1636 }
1637
1638 /* Translate architecture name to id */
1639 *arch = genimg_get_arch_id (data);
1640 return 0;
1641}
1642
1643/**
1644 * fit_image_get_type - get type id for a given component image node
1645 * @fit: pointer to the FIT format image header
1646 * @noffset: component image node offset
1647 * @type: pointer to the uint8_t, will hold type numeric id
1648 *
1649 * fit_image_get_type() finds type property in a given component image node.
1650 * If the property is found, its (string) value is translated to the numeric
1651 * id which is returned to the caller.
1652 *
1653 * returns:
1654 * 0, on success
1655 * -1, on failure
1656 */
1657int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1658{
1659 int len;
1660 const void *data;
1661
1662 /* Get image type name from property data */
1663 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1664 if (data == NULL) {
1665 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1666 *type = -1;
1667 return -1;
1668 }
1669
1670 /* Translate image type name to id */
1671 *type = genimg_get_type_id (data);
1672 return 0;
1673}
1674
1675/**
1676 * fit_image_get_comp - get comp id for a given component image node
1677 * @fit: pointer to the FIT format image header
1678 * @noffset: component image node offset
1679 * @comp: pointer to the uint8_t, will hold comp numeric id
1680 *
1681 * fit_image_get_comp() finds comp property in a given component image node.
1682 * If the property is found, its (string) value is translated to the numeric
1683 * id which is returned to the caller.
1684 *
1685 * returns:
1686 * 0, on success
1687 * -1, on failure
1688 */
1689int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1690{
1691 int len;
1692 const void *data;
1693
1694 /* Get compression name from property data */
1695 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1696 if (data == NULL) {
1697 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1698 *comp = -1;
1699 return -1;
1700 }
1701
1702 /* Translate compression name to id */
1703 *comp = genimg_get_comp_id (data);
1704 return 0;
1705}
1706
1707/**
1708 * fit_image_get_load - get load address property for a given component image node
1709 * @fit: pointer to the FIT format image header
1710 * @noffset: component image node offset
1711 * @load: pointer to the uint32_t, will hold load address
1712 *
1713 * fit_image_get_load() finds load address property in a given component image node.
1714 * If the property is found, its value is returned to the caller.
1715 *
1716 * returns:
1717 * 0, on success
1718 * -1, on failure
1719 */
1720int fit_image_get_load (const void *fit, int noffset, ulong *load)
1721{
1722 int len;
1723 const uint32_t *data;
1724
1725 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1726 if (data == NULL) {
1727 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1728 return -1;
1729 }
1730
1731 *load = uimage_to_cpu (*data);
1732 return 0;
1733}
1734
1735/**
1736 * fit_image_get_entry - get entry point address property for a given component image node
1737 * @fit: pointer to the FIT format image header
1738 * @noffset: component image node offset
1739 * @entry: pointer to the uint32_t, will hold entry point address
1740 *
1741 * fit_image_get_entry() finds entry point address property in a given component image node.
1742 * If the property is found, its value is returned to the caller.
1743 *
1744 * returns:
1745 * 0, on success
1746 * -1, on failure
1747 */
1748int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1749{
1750 int len;
1751 const uint32_t *data;
1752
1753 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1754 if (data == NULL) {
1755 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1756 return -1;
1757 }
1758
1759 *entry = uimage_to_cpu (*data);
1760 return 0;
1761}
1762
1763/**
1764 * fit_image_get_data - get data property and its size for a given component image node
1765 * @fit: pointer to the FIT format image header
1766 * @noffset: component image node offset
1767 * @data: double pointer to void, will hold data property's data address
1768 * @size: pointer to size_t, will hold data property's data size
1769 *
1770 * fit_image_get_data() finds data property in a given component image node.
1771 * If the property is found its data start address and size are returned to
1772 * the caller.
1773 *
1774 * returns:
1775 * 0, on success
1776 * -1, on failure
1777 */
1778int fit_image_get_data (const void *fit, int noffset,
1779 const void **data, size_t *size)
1780{
1781 int len;
1782
1783 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1784 if (*data == NULL) {
1785 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1786 *size = 0;
1787 return -1;
1788 }
1789
1790 *size = len;
1791 return 0;
1792}
1793
1794/**
1795 * fit_image_hash_get_algo - get hash algorithm name
1796 * @fit: pointer to the FIT format image header
1797 * @noffset: hash node offset
1798 * @algo: double pointer to char, will hold pointer to the algorithm name
1799 *
1800 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1801 * If the property is found its data start address is returned to the caller.
1802 *
1803 * returns:
1804 * 0, on success
1805 * -1, on failure
1806 */
1807int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1808{
1809 int len;
1810
1811 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1812 if (*algo == NULL) {
1813 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1814 return -1;
1815 }
1816
1817 return 0;
1818}
1819
1820/**
1821 * fit_image_hash_get_value - get hash value and length
1822 * @fit: pointer to the FIT format image header
1823 * @noffset: hash node offset
1824 * @value: double pointer to uint8_t, will hold address of a hash value data
1825 * @value_len: pointer to an int, will hold hash data length
1826 *
1827 * fit_image_hash_get_value() finds hash value property in a given hash node.
1828 * If the property is found its data start address and size are returned to
1829 * the caller.
1830 *
1831 * returns:
1832 * 0, on success
1833 * -1, on failure
1834 */
1835int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1836 int *value_len)
1837{
1838 int len;
1839
1840 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1841 if (*value == NULL) {
1842 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1843 *value_len = 0;
1844 return -1;
1845 }
1846
1847 *value_len = len;
1848 return 0;
1849}
1850
1851/**
1852 * fit_set_timestamp - set node timestamp property
1853 * @fit: pointer to the FIT format image header
1854 * @noffset: node offset
1855 * @timestamp: timestamp value to be set
1856 *
1857 * fit_set_timestamp() attempts to set timestamp property in the requested
1858 * node and returns operation status to the caller.
1859 *
1860 * returns:
1861 * 0, on success
1862 * -1, on property read failure
1863 */
1864int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1865{
1866 uint32_t t;
1867 int ret;
1868
1869 t = cpu_to_uimage (timestamp);
1870 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1871 sizeof (uint32_t));
1872 if (ret) {
1873 printf ("Can't set '%s' property for '%s' node (%s)\n",
1874 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1875 fdt_strerror (ret));
1876 return -1;
1877 }
1878
1879 return 0;
1880}
1881
1882/**
1883 * calculate_hash - calculate and return hash for provided input data
1884 * @data: pointer to the input data
1885 * @data_len: data length
1886 * @algo: requested hash algorithm
1887 * @value: pointer to the char, will hold hash value data (caller must
1888 * allocate enough free space)
1889 * value_len: length of the calculated hash
1890 *
1891 * calculate_hash() computes input data hash according to the requested algorithm.
1892 * Resulting hash value is placed in caller provided 'value' buffer, length
1893 * of the calculated hash is returned via value_len pointer argument.
1894 *
1895 * returns:
1896 * 0, on success
1897 * -1, when algo is unsupported
1898 */
1899static int calculate_hash (const void *data, int data_len, const char *algo,
1900 uint8_t *value, int *value_len)
1901{
1902 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02001903 *((uint32_t *)value) = crc32_wd (0, data, data_len,
1904 CHUNKSZ_CRC32);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001905 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1906 *value_len = 4;
1907 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02001908 sha1_csum_wd ((unsigned char *) data, data_len,
1909 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001910 *value_len = 20;
1911 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02001912 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka2e6f2852008-03-14 16:22:34 +01001913 *value_len = 16;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001914 } else {
1915 debug ("Unsupported hash alogrithm\n");
1916 return -1;
1917 }
1918 return 0;
1919}
1920
1921#ifdef USE_HOSTCC
1922/**
1923 * fit_set_hashes - process FIT component image nodes and calculate hashes
1924 * @fit: pointer to the FIT format image header
1925 *
1926 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1927 * Hashes are calculated for all component images which have hash subnodes
1928 * with algorithm property set to one of the supported hash algorithms.
1929 *
1930 * returns
1931 * 0, on success
1932 * libfdt error code, on failure
1933 */
1934int fit_set_hashes (void *fit)
1935{
1936 int images_noffset;
1937 int noffset;
1938 int ndepth;
1939 int ret;
1940
1941 /* Find images parent node offset */
1942 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1943 if (images_noffset < 0) {
1944 printf ("Can't find images parent node '%s' (%s)\n",
1945 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1946 return images_noffset;
1947 }
1948
1949 /* Process its subnodes, print out component images details */
1950 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1951 (noffset >= 0) && (ndepth > 0);
1952 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1953 if (ndepth == 1) {
1954 /*
1955 * Direct child node of the images parent node,
1956 * i.e. component image node.
1957 */
1958 ret = fit_image_set_hashes (fit, noffset);
1959 if (ret)
1960 return ret;
1961 }
1962 }
1963
1964 return 0;
1965}
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001966
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001967/**
1968 * fit_image_set_hashes - calculate/set hashes for given component image node
1969 * @fit: pointer to the FIT format image header
1970 * @image_noffset: requested component image node
1971 *
1972 * fit_image_set_hashes() adds hash values for an component image node. All
1973 * existing hash subnodes are checked, if algorithm property is set to one of
1974 * the supported hash algorithms, hash value is computed and corresponding
1975 * hash node property is set, for example:
1976 *
1977 * Input component image node structure:
1978 *
1979 * o image@1 (at image_noffset)
1980 * | - data = [binary data]
1981 * o hash@1
1982 * |- algo = "sha1"
1983 *
1984 * Output component image node structure:
1985 *
1986 * o image@1 (at image_noffset)
1987 * | - data = [binary data]
1988 * o hash@1
1989 * |- algo = "sha1"
1990 * |- value = sha1(data)
1991 *
1992 * returns:
1993 * 0 on sucess
1994 * <0 on failure
1995 */
1996int fit_image_set_hashes (void *fit, int image_noffset)
1997{
1998 const void *data;
1999 size_t size;
2000 char *algo;
2001 uint8_t value[FIT_MAX_HASH_LEN];
2002 int value_len;
2003 int noffset;
2004 int ndepth;
2005
2006 /* Get image data and data length */
2007 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2008 printf ("Can't get image data/size\n");
2009 return -1;
2010 }
2011
2012 /* Process all hash subnodes of the component image node */
2013 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2014 (noffset >= 0) && (ndepth > 0);
2015 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2016 if (ndepth == 1) {
2017 /* Direct child node of the component image node */
2018
2019 /*
2020 * Check subnode name, must be equal to "hash".
2021 * Multiple hash nodes require unique unit node
2022 * names, e.g. hash@1, hash@2, etc.
2023 */
2024 if (strncmp (fit_get_name(fit, noffset, NULL),
2025 FIT_HASH_NODENAME,
2026 strlen(FIT_HASH_NODENAME)) != 0) {
2027 /* Not a hash subnode, skip it */
2028 continue;
2029 }
2030
2031 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2032 printf ("Can't get hash algo property for "
2033 "'%s' hash node in '%s' image node\n",
2034 fit_get_name (fit, noffset, NULL),
2035 fit_get_name (fit, image_noffset, NULL));
2036 return -1;
2037 }
2038
2039 if (calculate_hash (data, size, algo, value, &value_len)) {
2040 printf ("Unsupported hash algorithm (%s) for "
2041 "'%s' hash node in '%s' image node\n",
2042 algo, fit_get_name (fit, noffset, NULL),
2043 fit_get_name (fit, image_noffset, NULL));
2044 return -1;
2045 }
2046
2047 if (fit_image_hash_set_value (fit, noffset, value,
2048 value_len)) {
2049 printf ("Can't set hash value for "
2050 "'%s' hash node in '%s' image node\n",
2051 fit_get_name (fit, noffset, NULL),
2052 fit_get_name (fit, image_noffset, NULL));
2053 return -1;
2054 }
2055 }
2056 }
2057
2058 return 0;
2059}
2060
2061/**
2062 * fit_image_hash_set_value - set hash value in requested has node
2063 * @fit: pointer to the FIT format image header
2064 * @noffset: hash node offset
2065 * @value: hash value to be set
2066 * @value_len: hash value length
2067 *
2068 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2069 * given and returns operation status to the caller.
2070 *
2071 * returns
2072 * 0, on success
2073 * -1, on failure
2074 */
2075int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2076 int value_len)
2077{
2078 int ret;
2079
2080 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2081 if (ret) {
2082 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2083 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2084 fdt_strerror (ret));
2085 return -1;
2086 }
2087
2088 return 0;
2089}
2090#endif /* USE_HOSTCC */
2091
2092/**
2093 * fit_image_check_hashes - verify data intergity
2094 * @fit: pointer to the FIT format image header
2095 * @image_noffset: component image node offset
2096 *
2097 * fit_image_check_hashes() goes over component image hash nodes,
2098 * re-calculates each data hash and compares with the value stored in hash
2099 * node.
2100 *
2101 * returns:
2102 * 1, if all hashes are valid
2103 * 0, otherwise (or on error)
2104 */
2105int fit_image_check_hashes (const void *fit, int image_noffset)
2106{
2107 const void *data;
2108 size_t size;
2109 char *algo;
2110 uint8_t *fit_value;
2111 int fit_value_len;
2112 uint8_t value[FIT_MAX_HASH_LEN];
2113 int value_len;
2114 int noffset;
2115 int ndepth;
2116 char *err_msg = "";
2117
2118 /* Get image data and data length */
2119 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2120 printf ("Can't get image data/size\n");
2121 return 0;
2122 }
2123
2124 /* Process all hash subnodes of the component image node */
2125 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2126 (noffset >= 0) && (ndepth > 0);
2127 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2128 if (ndepth == 1) {
2129 /* Direct child node of the component image node */
2130
2131 /*
2132 * Check subnode name, must be equal to "hash".
2133 * Multiple hash nodes require unique unit node
2134 * names, e.g. hash@1, hash@2, etc.
2135 */
2136 if (strncmp (fit_get_name(fit, noffset, NULL),
2137 FIT_HASH_NODENAME,
2138 strlen(FIT_HASH_NODENAME)) != 0)
2139 continue;
2140
2141 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2142 err_msg = "Can't get hash algo property";
2143 goto error;
2144 }
2145 printf ("%s", algo);
2146
2147 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2148 &fit_value_len)) {
2149 err_msg = "Can't get hash value property";
2150 goto error;
2151 }
2152
2153 if (calculate_hash (data, size, algo, value, &value_len)) {
2154 err_msg = "Unsupported hash algorithm";
2155 goto error;
2156 }
2157
2158 if (value_len != fit_value_len) {
2159 err_msg = "Bad hash value len";
2160 goto error;
2161 } else if (memcmp (value, fit_value, value_len) != 0) {
2162 err_msg = "Bad hash value";
2163 goto error;
2164 }
2165 printf ("+ ");
2166 }
2167 }
2168
2169 return 1;
2170
2171error:
2172 printf ("%s for '%s' hash node in '%s' image node\n",
2173 err_msg, fit_get_name (fit, noffset, NULL),
2174 fit_get_name (fit, image_noffset, NULL));
2175 return 0;
2176}
2177
2178/**
2179 * fit_image_check_os - check whether image node is of a given os type
2180 * @fit: pointer to the FIT format image header
2181 * @noffset: component image node offset
2182 * @os: requested image os
2183 *
2184 * fit_image_check_os() reads image os property and compares its numeric
2185 * id with the requested os. Comparison result is returned to the caller.
2186 *
2187 * returns:
2188 * 1 if image is of given os type
2189 * 0 otherwise (or on error)
2190 */
2191int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2192{
2193 uint8_t image_os;
2194
2195 if (fit_image_get_os (fit, noffset, &image_os))
2196 return 0;
2197 return (os == image_os);
2198}
2199
2200/**
2201 * fit_image_check_arch - check whether image node is of a given arch
2202 * @fit: pointer to the FIT format image header
2203 * @noffset: component image node offset
2204 * @arch: requested imagearch
2205 *
2206 * fit_image_check_arch() reads image arch property and compares its numeric
2207 * id with the requested arch. Comparison result is returned to the caller.
2208 *
2209 * returns:
2210 * 1 if image is of given arch
2211 * 0 otherwise (or on error)
2212 */
2213int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2214{
2215 uint8_t image_arch;
2216
2217 if (fit_image_get_arch (fit, noffset, &image_arch))
2218 return 0;
2219 return (arch == image_arch);
2220}
2221
2222/**
2223 * fit_image_check_type - check whether image node is of a given type
2224 * @fit: pointer to the FIT format image header
2225 * @noffset: component image node offset
2226 * @type: requested image type
2227 *
2228 * fit_image_check_type() reads image type property and compares its numeric
2229 * id with the requested type. Comparison result is returned to the caller.
2230 *
2231 * returns:
2232 * 1 if image is of given type
2233 * 0 otherwise (or on error)
2234 */
2235int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2236{
2237 uint8_t image_type;
2238
2239 if (fit_image_get_type (fit, noffset, &image_type))
2240 return 0;
2241 return (type == image_type);
2242}
2243
2244/**
2245 * fit_image_check_comp - check whether image node uses given compression
2246 * @fit: pointer to the FIT format image header
2247 * @noffset: component image node offset
2248 * @comp: requested image compression type
2249 *
2250 * fit_image_check_comp() reads image compression property and compares its
2251 * numeric id with the requested compression type. Comparison result is
2252 * returned to the caller.
2253 *
2254 * returns:
2255 * 1 if image uses requested compression
2256 * 0 otherwise (or on error)
2257 */
2258int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2259{
2260 uint8_t image_comp;
2261
2262 if (fit_image_get_comp (fit, noffset, &image_comp))
2263 return 0;
2264 return (comp == image_comp);
2265}
2266
2267/**
2268 * fit_check_format - sanity check FIT image format
2269 * @fit: pointer to the FIT format image header
2270 *
2271 * fit_check_format() runs a basic sanity FIT image verification.
2272 * Routine checks for mandatory properties, nodes, etc.
2273 *
2274 * returns:
2275 * 1, on success
2276 * 0, on failure
2277 */
2278int fit_check_format (const void *fit)
2279{
2280 /* mandatory / node 'description' property */
2281 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2282 debug ("Wrong FIT format: no description\n");
2283 return 0;
2284 }
2285
2286#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2287 /* mandatory / node 'timestamp' property */
2288 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2289 debug ("Wrong FIT format: no description\n");
2290 return 0;
2291 }
2292#endif
2293
2294 /* mandatory subimages parent '/images' node */
2295 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2296 debug ("Wrong FIT format: no images parent node\n");
2297 return 0;
2298 }
2299
2300 return 1;
2301}
2302
2303/**
2304 * fit_conf_get_node - get node offset for configuration of a given unit name
2305 * @fit: pointer to the FIT format image header
2306 * @conf_uname: configuration node unit name
2307 *
2308 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2309 * parant node) of a provided unit name. If configuration is found its node offset
2310 * is returned to the caller.
2311 *
2312 * When NULL is provided in second argument fit_conf_get_node() will search
2313 * for a default configuration node instead. Default configuration node unit name
2314 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2315 *
2316 * returns:
2317 * configuration node offset when found (>=0)
2318 * negative number on failure (FDT_ERR_* code)
2319 */
2320int fit_conf_get_node (const void *fit, const char *conf_uname)
2321{
2322 int noffset, confs_noffset;
2323 int len;
2324
2325 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2326 if (confs_noffset < 0) {
2327 debug ("Can't find configurations parent node '%s' (%s)\n",
2328 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2329 return confs_noffset;
2330 }
2331
2332 if (conf_uname == NULL) {
2333 /* get configuration unit name from the default property */
2334 debug ("No configuration specified, trying default...\n");
2335 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2336 if (conf_uname == NULL) {
2337 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2338 return len;
2339 }
2340 debug ("Found default configuration: '%s'\n", conf_uname);
2341 }
2342
2343 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2344 if (noffset < 0) {
2345 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2346 conf_uname, fdt_strerror (noffset));
2347 }
2348
2349 return noffset;
2350}
2351
2352static int __fit_conf_get_prop_node (const void *fit, int noffset,
2353 const char *prop_name)
2354{
2355 char *uname;
2356 int len;
2357
2358 /* get kernel image unit name from configuration kernel property */
2359 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2360 if (uname == NULL)
2361 return len;
2362
2363 return fit_image_get_node (fit, uname);
2364}
2365
2366/**
2367 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2368 * a given configuration
2369 * @fit: pointer to the FIT format image header
2370 * @noffset: configuration node offset
2371 *
2372 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2373 * configuration FIT_KERNEL_PROP property and translates it to the node
2374 * offset.
2375 *
2376 * returns:
2377 * image node offset when found (>=0)
2378 * negative number on failure (FDT_ERR_* code)
2379 */
2380int fit_conf_get_kernel_node (const void *fit, int noffset)
2381{
2382 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2383}
2384
2385/**
2386 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2387 * a given configuration
2388 * @fit: pointer to the FIT format image header
2389 * @noffset: configuration node offset
2390 *
2391 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2392 * configuration FIT_KERNEL_PROP property and translates it to the node
2393 * offset.
2394 *
2395 * returns:
2396 * image node offset when found (>=0)
2397 * negative number on failure (FDT_ERR_* code)
2398 */
2399int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2400{
2401 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2402}
2403
2404/**
2405 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2406 * a given configuration
2407 * @fit: pointer to the FIT format image header
2408 * @noffset: configuration node offset
2409 *
2410 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2411 * configuration FIT_KERNEL_PROP property and translates it to the node
2412 * offset.
2413 *
2414 * returns:
2415 * image node offset when found (>=0)
2416 * negative number on failure (FDT_ERR_* code)
2417 */
2418int fit_conf_get_fdt_node (const void *fit, int noffset)
2419{
2420 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2421}
2422
2423/**
2424 * fit_conf_print - prints out the FIT configuration details
2425 * @fit: pointer to the FIT format image header
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +01002426 * @noffset: offset of the configuration node
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002427 * @p: pointer to prefix string
2428 *
2429 * fit_conf_print() lists all mandatory properies for the processed
2430 * configuration node.
2431 *
2432 * returns:
2433 * no returned results
2434 */
2435void fit_conf_print (const void *fit, int noffset, const char *p)
2436{
2437 char *desc;
2438 char *uname;
2439 int ret;
2440
2441 /* Mandatory properties */
2442 ret = fit_get_desc (fit, noffset, &desc);
2443 printf ("%s Description: ", p);
2444 if (ret)
2445 printf ("unavailable\n");
2446 else
2447 printf ("%s\n", desc);
2448
2449 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2450 printf ("%s Kernel: ", p);
2451 if (uname == NULL)
2452 printf ("unavailable\n");
2453 else
2454 printf ("%s\n", uname);
2455
2456 /* Optional properties */
2457 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2458 if (uname)
2459 printf ("%s Init Ramdisk: %s\n", p, uname);
2460
2461 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2462 if (uname)
2463 printf ("%s FDT: %s\n", p, uname);
2464}
Marian Balakowicza50d5152008-03-12 10:12:37 +01002465
2466/**
2467 * fit_check_ramdisk - verify FIT format ramdisk subimage
2468 * @fit_hdr: pointer to the FIT ramdisk header
2469 * @rd_noffset: ramdisk subimage node offset within FIT image
2470 * @arch: requested ramdisk image architecture type
2471 * @verify: data CRC verification flag
2472 *
2473 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2474 * specified FIT image.
2475 *
2476 * returns:
2477 * 1, on success
2478 * 0, on failure
2479 */
2480#ifndef USE_HOSTCC
2481static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2482{
2483 fit_image_print (fit, rd_noffset, " ");
2484
2485 if (verify) {
2486 puts (" Verifying Hash Integrity ... ");
2487 if (!fit_image_check_hashes (fit, rd_noffset)) {
2488 puts ("Bad Data Hash\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002489 show_boot_progress (-125);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002490 return 0;
2491 }
2492 puts ("OK\n");
2493 }
2494
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002495 show_boot_progress (126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002496 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2497 !fit_image_check_arch (fit, rd_noffset, arch) ||
2498 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2499 printf ("No Linux %s Ramdisk Image\n",
2500 genimg_get_arch_name(arch));
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002501 show_boot_progress (-126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002502 return 0;
2503 }
2504
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01002505 show_boot_progress (127);
Marian Balakowicza50d5152008-03-12 10:12:37 +01002506 return 1;
2507}
2508#endif /* USE_HOSTCC */
Marian Balakowicz3413e222008-02-21 17:20:20 +01002509#endif /* CONFIG_FIT */