blob: ea5973086140930d9f995924aab60dcd3c319447 [file] [log] [blame]
Marian Balakowiczb97a2a02008-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 Balakowiczceaed2b2008-01-31 13:57:17 +010025
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010026#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010027#include <common.h>
28#include <watchdog.h>
29
30#ifdef CONFIG_SHOW_BOOT_PROGRESS
31#include <status_led.h>
32#endif
33
34#ifdef CONFIG_HAS_DATAFLASH
35#include <dataflash.h>
36#endif
37
Marian Balakowicz95d449a2008-05-13 15:53:29 +020038#ifdef CONFIG_LOGBUFFER
39#include <logbuff.h>
40#endif
41
Marian Balakowicz2242f532008-02-21 17:27:41 +010042#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
43#include <rtc.h>
44#endif
45
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010046#include <image.h>
47
Marian Balakowiczc8779642008-03-12 10:12:37 +010048#if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
Marian Balakowiczfff888a12008-02-21 17:20:19 +010049#include <fdt.h>
50#include <libfdt.h>
51#include <fdt_support.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010052#endif
53
54#if defined(CONFIG_FIT)
Andy Fleming20a14a42008-04-02 16:19:07 -050055#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010056#include <sha1.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010057
58static int fit_check_ramdisk (const void *fit, int os_noffset,
59 uint8_t arch, int verify);
Marian Balakowiczfff888a12008-02-21 17:20:19 +010060#endif
61
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010062#ifdef CONFIG_CMD_BDI
Wolfgang Denk54841ab2010-06-28 22:00:46 +020063extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010064#endif
65
66DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010067
Wolfgang Denk3a2003f2009-08-19 11:42:56 +020068static const image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +010069 int verify);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010070#else
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010071#include "mkimage.h"
Andy Fleming20a14a42008-04-02 16:19:07 -050072#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010073#include <time.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010074#include <image.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010075#endif /* !USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010076
Marian Balakowicz570abb02008-02-29 15:59:59 +010077static table_entry_t uimage_arch[] = {
78 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
79 { IH_ARCH_ALPHA, "alpha", "Alpha", },
80 { IH_ARCH_ARM, "arm", "ARM", },
81 { IH_ARCH_I386, "x86", "Intel x86", },
82 { IH_ARCH_IA64, "ia64", "IA64", },
83 { IH_ARCH_M68K, "m68k", "M68K", },
84 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
85 { IH_ARCH_MIPS, "mips", "MIPS", },
86 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010087 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -070088 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010089 { IH_ARCH_PPC, "ppc", "PowerPC", },
90 { IH_ARCH_S390, "s390", "IBM S390", },
91 { IH_ARCH_SH, "sh", "SuperH", },
92 { IH_ARCH_SPARC, "sparc", "SPARC", },
93 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
94 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
95 { IH_ARCH_AVR32, "avr32", "AVR32", },
96 { -1, "", "", },
97};
98
99static table_entry_t uimage_os[] = {
100 { IH_OS_INVALID, NULL, "Invalid OS", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100101 { IH_OS_LINUX, "linux", "Linux", },
102#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
103 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
104#endif
105 { IH_OS_NETBSD, "netbsd", "NetBSD", },
106 { IH_OS_RTEMS, "rtems", "RTEMS", },
107 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
108#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
109 { IH_OS_QNX, "qnx", "QNX", },
110 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
111#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500112#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
113 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
114#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100115#ifdef USE_HOSTCC
116 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
117 { IH_OS_DELL, "dell", "Dell", },
118 { IH_OS_ESIX, "esix", "Esix", },
119 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
120 { IH_OS_IRIX, "irix", "Irix", },
121 { IH_OS_NCR, "ncr", "NCR", },
122 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
123 { IH_OS_PSOS, "psos", "pSOS", },
124 { IH_OS_SCO, "sco", "SCO", },
125 { IH_OS_SOLARIS, "solaris", "Solaris", },
126 { IH_OS_SVR4, "svr4", "SVR4", },
127#endif
128 { -1, "", "", },
129};
130
131static table_entry_t uimage_type[] = {
132 { IH_TYPE_INVALID, NULL, "Invalid Image", },
133 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
134 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
135 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
136 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
137 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
138 { IH_TYPE_SCRIPT, "script", "Script", },
139 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
140 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530141 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
Stefano Babic8edcde52010-01-20 18:19:10 +0100142 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100143 { -1, "", "", },
144};
145
146static table_entry_t uimage_comp[] = {
147 { IH_COMP_NONE, "none", "uncompressed", },
148 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
149 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200150 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100151 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100152 { -1, "", "", },
153};
154
Mike Frysinger89cdab72008-03-31 11:02:01 -0400155uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200156uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100157#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
158static void genimg_print_time (time_t timestamp);
159#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100160
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100161/*****************************************************************************/
162/* Legacy format routines */
163/*****************************************************************************/
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200164int image_check_hcrc (const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100165{
166 ulong hcrc;
167 ulong len = image_get_header_size ();
168 image_header_t header;
169
170 /* Copy header so we can blank CRC field for re-calculation */
171 memmove (&header, (char *)hdr, image_get_header_size ());
172 image_set_hcrc (&header, 0);
173
174 hcrc = crc32 (0, (unsigned char *)&header, len);
175
176 return (hcrc == image_get_hcrc (hdr));
177}
178
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200179int image_check_dcrc (const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100180{
181 ulong data = image_get_data (hdr);
182 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200183 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100184
185 return (dcrc == image_get_dcrc (hdr));
186}
187
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100188/**
189 * image_multi_count - get component (sub-image) count
190 * @hdr: pointer to the header of the multi component image
191 *
192 * image_multi_count() returns number of components in a multi
193 * component image.
194 *
195 * Note: no checking of the image type is done, caller must pass
196 * a valid multi component image.
197 *
198 * returns:
199 * number of components
200 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200201ulong image_multi_count (const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100202{
203 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100204 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100205
206 /* get start of the image payload, which in case of multi
207 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100208 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100209
210 /* count non empty slots */
211 for (i = 0; size[i]; ++i)
212 count++;
213
214 return count;
215}
216
217/**
218 * image_multi_getimg - get component data address and size
219 * @hdr: pointer to the header of the multi component image
220 * @idx: index of the requested component
221 * @data: pointer to a ulong variable, will hold component data address
222 * @len: pointer to a ulong variable, will hold component size
223 *
224 * image_multi_getimg() returns size and data address for the requested
225 * component in a multi component image.
226 *
227 * Note: no checking of the image type is done, caller must pass
228 * a valid multi component image.
229 *
230 * returns:
231 * data address and size of the component, if idx is valid
232 * 0 in data and len, if idx is out of range
233 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200234void image_multi_getimg (const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100235 ulong *data, ulong *len)
236{
237 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100238 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700239 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100240
241 /* get number of component */
242 count = image_multi_count (hdr);
243
244 /* get start of the image payload, which in case of multi
245 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100246 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100247
248 /* get address of the proper component data start, which means
249 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100250 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100251
252 if (idx < count) {
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100253 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100254 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100255
256 /* go over all indices preceding requested component idx */
257 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700258 /* add up i-th component size, rounding up to 4 bytes */
259 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100260 }
261
262 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700263 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100264 } else {
265 *len = 0;
266 *data = 0;
267 }
268}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100269
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200270static void image_print_type (const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100271{
272 const char *os, *arch, *type, *comp;
273
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100274 os = genimg_get_os_name (image_get_os (hdr));
275 arch = genimg_get_arch_name (image_get_arch (hdr));
276 type = genimg_get_type_name (image_get_type (hdr));
277 comp = genimg_get_comp_name (image_get_comp (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100278
Marian Balakowicz570abb02008-02-29 15:59:59 +0100279 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100280}
281
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100282/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200283 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200284 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100285 * @p: pointer to prefix string
286 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200287 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100288 * The routine prints out all header fields followed by the size/offset data
289 * for MULTI/SCRIPT images.
290 *
291 * returns:
292 * no returned results
293 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200294void image_print_contents (const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100295{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200296 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200297 const char *p;
298
299#ifdef USE_HOSTCC
300 p = "";
301#else
302 p = " ";
303#endif
304
Marian Balakowicz570abb02008-02-29 15:59:59 +0100305 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
306#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
307 printf ("%sCreated: ", p);
308 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100309#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100310 printf ("%sImage Type: ", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100311 image_print_type (hdr);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100312 printf ("%sData Size: ", p);
313 genimg_print_size (image_get_data_size (hdr));
314 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
315 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100316
Marian Balakowicz570abb02008-02-29 15:59:59 +0100317 if (image_check_type (hdr, IH_TYPE_MULTI) ||
318 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100319 int i;
320 ulong data, len;
321 ulong count = image_multi_count (hdr);
322
Marian Balakowicz570abb02008-02-29 15:59:59 +0100323 printf ("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100324 for (i = 0; i < count; i++) {
325 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100326
327 printf ("%s Image %d: ", p, i);
328 genimg_print_size (len);
329
330 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
331 /*
332 * the user may need to know offsets
333 * if planning to do something with
334 * multiple files
335 */
336 printf ("%s Offset = 0x%08lx\n", p, data);
337 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100338 }
339 }
340}
341
Marian Balakowicz570abb02008-02-29 15:59:59 +0100342
343#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100344/**
345 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100346 * @rd_addr: ramdisk image start address
347 * @arch: expected ramdisk architecture
348 * @verify: checksum verification flag
349 *
350 * image_get_ramdisk() returns a pointer to the verified ramdisk image
351 * header. Routine receives image start address and expected architecture
352 * flag. Verification done covers data and header integrity and os/type/arch
353 * fields checking.
354 *
355 * If dataflash support is enabled routine checks for dataflash addresses
356 * and handles required dataflash reads.
357 *
358 * returns:
359 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600360 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100361 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200362static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100363 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100364{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200365 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100366
367 if (!image_check_magic (rd_hdr)) {
368 puts ("Bad Magic Number\n");
369 show_boot_progress (-10);
Kumar Gala274cea22008-02-27 21:51:46 -0600370 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100371 }
372
373 if (!image_check_hcrc (rd_hdr)) {
374 puts ("Bad Header Checksum\n");
375 show_boot_progress (-11);
Kumar Gala274cea22008-02-27 21:51:46 -0600376 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100377 }
378
379 show_boot_progress (10);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100380 image_print_contents (rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100381
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100382 if (verify) {
383 puts(" Verifying Checksum ... ");
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200384 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100385 puts ("Bad Data CRC\n");
386 show_boot_progress (-12);
Kumar Gala274cea22008-02-27 21:51:46 -0600387 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100388 }
389 puts("OK\n");
390 }
391
392 show_boot_progress (11);
393
394 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
395 !image_check_arch (rd_hdr, arch) ||
396 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
397 printf ("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100398 genimg_get_arch_name(arch));
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100399 show_boot_progress (-13);
Kumar Gala274cea22008-02-27 21:51:46 -0600400 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100401 }
402
403 return rd_hdr;
404}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100405#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100406
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100407/*****************************************************************************/
408/* Shared dual-format routines */
409/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100410#ifndef USE_HOSTCC
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200411int getenv_yesno (char *var)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100412{
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200413 char *s = getenv (var);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100414 return (s && (*s == 'n')) ? 0 : 1;
415}
416
417ulong getenv_bootm_low(void)
418{
419 char *s = getenv ("bootm_low");
420 if (s) {
421 ulong tmp = simple_strtoul (s, NULL, 16);
422 return tmp;
423 }
424
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200425#if defined(CONFIG_SYS_SDRAM_BASE)
426 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100427#elif defined(CONFIG_ARM)
428 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100429#else
430 return 0;
431#endif
432}
433
Becky Bruce391fd932008-06-09 20:37:18 -0500434phys_size_t getenv_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100435{
Matthew McClintockc519fac2010-07-08 10:11:08 -0500436 phys_size_t tmp;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100437 char *s = getenv ("bootm_size");
438 if (s) {
Becky Bruce391fd932008-06-09 20:37:18 -0500439 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100440 return tmp;
441 }
Matthew McClintockc519fac2010-07-08 10:11:08 -0500442 s = getenv("bootm_low");
443 if (s)
444 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
445 else
446 tmp = 0;
447
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100448
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100449#if defined(CONFIG_ARM)
Matthew McClintockc519fac2010-07-08 10:11:08 -0500450 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100451#else
Matthew McClintockc519fac2010-07-08 10:11:08 -0500452 return gd->bd->bi_memsize - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100453#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100454}
455
456void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
457{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400458 if (to == from)
459 return;
460
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100461#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
462 while (len > 0) {
463 size_t tail = (len > chunksz) ? chunksz : len;
464 WATCHDOG_RESET ();
465 memmove (to, from, tail);
466 to += tail;
467 from += tail;
468 len -= tail;
469 }
470#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
471 memmove (to, from, len);
472#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
473}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100474#endif /* !USE_HOSTCC */
475
Prafulla Wadaskarf666dea2009-09-07 14:59:07 +0530476void genimg_print_size (uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100477{
478#ifndef USE_HOSTCC
479 printf ("%d Bytes = ", size);
480 print_size (size, "\n");
481#else
482 printf ("%d Bytes = %.2f kB = %.2f MB\n",
483 size, (double)size / 1.024e3,
484 (double)size / 1.048576e6);
485#endif
486}
487
488#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
489static void genimg_print_time (time_t timestamp)
490{
491#ifndef USE_HOSTCC
492 struct rtc_time tm;
493
494 to_tm (timestamp, &tm);
495 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
496 tm.tm_year, tm.tm_mon, tm.tm_mday,
497 tm.tm_hour, tm.tm_min, tm.tm_sec);
498#else
499 printf ("%s", ctime(&timestamp));
500#endif
501}
502#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
503
504/**
505 * get_table_entry_name - translate entry id to long name
506 * @table: pointer to a translation table for entries of a specific type
507 * @msg: message to be returned when translation fails
508 * @id: entry id to be translated
509 *
510 * get_table_entry_name() will go over translation table trying to find
511 * entry that matches given id. If matching entry is found, its long
512 * name is returned to the caller.
513 *
514 * returns:
515 * long entry name if translation succeeds
516 * msg otherwise
517 */
Prafulla Wadaskarb029ddd2009-09-07 14:59:08 +0530518char *get_table_entry_name (table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100519{
520 for (; table->id >= 0; ++table) {
521 if (table->id == id)
Peter Tyser521af042009-09-21 11:20:36 -0500522#if defined(USE_HOSTCC) || defined(CONFIG_RELOC_FIXUP_WORKS)
Scott Woode3d1ac72009-04-02 16:15:10 -0500523 return table->lname;
524#else
525 return table->lname + gd->reloc_off;
526#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100527 }
528 return (msg);
529}
530
531const char *genimg_get_os_name (uint8_t os)
532{
533 return (get_table_entry_name (uimage_os, "Unknown OS", os));
534}
535
536const char *genimg_get_arch_name (uint8_t arch)
537{
538 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
539}
540
541const char *genimg_get_type_name (uint8_t type)
542{
543 return (get_table_entry_name (uimage_type, "Unknown Image", type));
544}
545
546const char *genimg_get_comp_name (uint8_t comp)
547{
548 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
549}
550
551/**
552 * get_table_entry_id - translate short entry name to id
553 * @table: pointer to a translation table for entries of a specific type
554 * @table_name: to be used in case of error
555 * @name: entry short name to be translated
556 *
557 * get_table_entry_id() will go over translation table trying to find
558 * entry that matches given short name. If matching entry is found,
559 * its id returned to the caller.
560 *
561 * returns:
562 * entry id if translation succeeds
563 * -1 otherwise
564 */
Prafulla Wadaskarb029ddd2009-09-07 14:59:08 +0530565int get_table_entry_id (table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100566 const char *table_name, const char *name)
567{
568 table_entry_t *t;
569#ifdef USE_HOSTCC
570 int first = 1;
571
572 for (t = table; t->id >= 0; ++t) {
573 if (t->sname && strcasecmp(t->sname, name) == 0)
574 return (t->id);
575 }
576
577 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
578 for (t = table; t->id >= 0; ++t) {
579 if (t->sname == NULL)
580 continue;
581 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
582 first = 0;
583 }
584 fprintf (stderr, "\n");
585#else
586 for (t = table; t->id >= 0; ++t) {
Peter Tyser521af042009-09-21 11:20:36 -0500587#ifdef CONFIG_RELOC_FIXUP_WORKS
588 if (t->sname && strcmp(t->sname, name) == 0)
589#else
Scott Woode3d1ac72009-04-02 16:15:10 -0500590 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500591#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100592 return (t->id);
593 }
594 debug ("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100595#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100596 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100597}
598
Marian Balakowicz570abb02008-02-29 15:59:59 +0100599int genimg_get_os_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100600{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100601 return (get_table_entry_id (uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100602}
603
Marian Balakowicz570abb02008-02-29 15:59:59 +0100604int genimg_get_arch_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100605{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100606 return (get_table_entry_id (uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100607}
608
Marian Balakowicz570abb02008-02-29 15:59:59 +0100609int genimg_get_type_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100610{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100611 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100612}
613
Marian Balakowicz570abb02008-02-29 15:59:59 +0100614int genimg_get_comp_id (const char *name)
615{
616 return (get_table_entry_id (uimage_comp, "Compression", name));
617}
618
619#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100620/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100621 * genimg_get_format - get image format type
622 * @img_addr: image start address
623 *
624 * genimg_get_format() checks whether provided address points to a valid
625 * legacy or FIT image.
626 *
627 * New uImage format and FDT blob are based on a libfdt. FDT blob
628 * may be passed directly or embedded in a FIT image. In both situations
629 * genimg_get_format() must be able to dectect libfdt header.
630 *
631 * returns:
632 * image format type or IMAGE_FORMAT_INVALID if no image is present
633 */
634int genimg_get_format (void *img_addr)
635{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200636 ulong format = IMAGE_FORMAT_INVALID;
637 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100638#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200639 char *fit_hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100640#endif
641
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200642 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100643 if (image_check_magic(hdr))
644 format = IMAGE_FORMAT_LEGACY;
645#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
646 else {
647 fit_hdr = (char *)img_addr;
648 if (fdt_check_header (fit_hdr) == 0)
649 format = IMAGE_FORMAT_FIT;
650 }
651#endif
652
653 return format;
654}
655
656/**
657 * genimg_get_image - get image from special storage (if necessary)
658 * @img_addr: image start address
659 *
660 * genimg_get_image() checks if provided image start adddress is located
661 * in a dataflash storage. If so, image is moved to a system RAM memory.
662 *
663 * returns:
664 * image start address after possible relocation from special storage
665 */
666ulong genimg_get_image (ulong img_addr)
667{
668 ulong ram_addr = img_addr;
669
670#ifdef CONFIG_HAS_DATAFLASH
671 ulong h_size, d_size;
672
673 if (addr_dataflash (img_addr)){
674 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200675 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100676
677 /* get header size */
678 h_size = image_get_header_size ();
679#if defined(CONFIG_FIT)
680 if (sizeof(struct fdt_header) > h_size)
681 h_size = sizeof(struct fdt_header);
682#endif
683
684 /* read in header */
685 debug (" Reading image header from dataflash address "
686 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
687
688 read_dataflash (img_addr, h_size, (char *)ram_addr);
689
690 /* get data size */
691 switch (genimg_get_format ((void *)ram_addr)) {
692 case IMAGE_FORMAT_LEGACY:
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200693 d_size = image_get_data_size ((const image_header_t *)ram_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100694 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
695 ram_addr, d_size);
696 break;
697#if defined(CONFIG_FIT)
698 case IMAGE_FORMAT_FIT:
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100699 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100700 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
701 ram_addr, d_size);
702 break;
703#endif
704 default:
705 printf (" No valid image found at 0x%08lx\n", img_addr);
706 return ram_addr;
707 }
708
709 /* read in image data */
710 debug (" Reading image remaining data from dataflash address "
711 "%08lx to RAM address %08lx\n", img_addr + h_size,
712 ram_addr + h_size);
713
714 read_dataflash (img_addr + h_size, d_size,
715 (char *)(ram_addr + h_size));
716
717 }
718#endif /* CONFIG_HAS_DATAFLASH */
719
720 return ram_addr;
721}
722
723/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100724 * fit_has_config - check if there is a valid FIT configuration
725 * @images: pointer to the bootm command headers structure
726 *
727 * fit_has_config() checks if there is a FIT configuration in use
728 * (if FTI support is present).
729 *
730 * returns:
731 * 0, no FIT support or no configuration found
732 * 1, configuration found
733 */
734int genimg_has_config (bootm_headers_t *images)
735{
736#if defined(CONFIG_FIT)
737 if (images->fit_uname_cfg)
738 return 1;
739#endif
740 return 0;
741}
742
743/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100744 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100745 * @argc: command argument count
746 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100747 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100748 * @arch: expected ramdisk architecture
749 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
750 * @rd_end: pointer to a ulong variable, will hold ramdisk end
751 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100752 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100753 * Curently supported are the following ramdisk sources:
754 * - multicomponent kernel/ramdisk image,
755 * - commandline provided address of decicated ramdisk image.
756 *
757 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100758 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100759 * rd_start and rd_end are set to ramdisk start/end addresses if
760 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100761 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500762 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100763 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100764 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200765int boot_get_ramdisk (int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100766 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100767{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100768 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100769 ulong rd_data, rd_len;
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200770 const image_header_t *rd_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100771#if defined(CONFIG_FIT)
772 void *fit_hdr;
773 const char *fit_uname_config = NULL;
774 const char *fit_uname_ramdisk = NULL;
775 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100776 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100777 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100778 const void *data;
779 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100780#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100781
Marian Balakowiczc8779642008-03-12 10:12:37 +0100782 *rd_start = 0;
783 *rd_end = 0;
784
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100785 /*
786 * Look for a '-' which indicates to ignore the
787 * ramdisk argument
788 */
789 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
790 debug ("## Skipping init Ramdisk\n");
791 rd_len = rd_data = 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100792 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100793#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100794 if (argc >= 3) {
795 /*
796 * If the init ramdisk comes from the FIT image and
797 * the FIT image address is omitted in the command
798 * line argument, try to use os FIT image address or
799 * default load address.
800 */
801 if (images->fit_uname_os)
802 default_addr = (ulong)images->fit_hdr_os;
803 else
804 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100805
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100806 if (fit_parse_conf (argv[2], default_addr,
807 &rd_addr, &fit_uname_config)) {
808 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
809 fit_uname_config, rd_addr);
810 } else if (fit_parse_subimage (argv[2], default_addr,
811 &rd_addr, &fit_uname_ramdisk)) {
812 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
813 fit_uname_ramdisk, rd_addr);
814 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100815#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100816 {
817 rd_addr = simple_strtoul(argv[2], NULL, 16);
818 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
819 rd_addr);
820 }
821#if defined(CONFIG_FIT)
822 } else {
823 /* use FIT configuration provided in first bootm
824 * command argument
825 */
826 rd_addr = (ulong)images->fit_hdr_os;
827 fit_uname_config = images->fit_uname_cfg;
828 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
829 fit_uname_config, rd_addr);
830
831 /*
832 * Check whether configuration has ramdisk defined,
833 * if not, don't try to use it, quit silently.
834 */
835 fit_hdr = (void *)rd_addr;
836 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
837 if (cfg_noffset < 0) {
838 debug ("* ramdisk: no such config\n");
Michal Simekc78fce62008-07-11 10:43:13 +0200839 return 1;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100840 }
841
842 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
843 if (rd_noffset < 0) {
844 debug ("* ramdisk: no ramdisk in config\n");
Peter Tyser41266c92008-08-05 10:51:57 -0500845 return 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100846 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100847 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100848#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100849
850 /* copy from dataflash if needed */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100851 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100852
853 /*
854 * Check if there is an initrd image at the
855 * address provided in the second bootm argument
856 * check image type, for FIT images get FIT node.
857 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100858 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100859 case IMAGE_FORMAT_LEGACY:
Marian Balakowiczc8779642008-03-12 10:12:37 +0100860 printf ("## Loading init Ramdisk from Legacy "
861 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100862
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100863 show_boot_progress (9);
Marian Balakowiczd985c842008-03-12 10:14:38 +0100864 rd_hdr = image_get_ramdisk (rd_addr, arch,
865 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100866
Marian Balakowiczc8779642008-03-12 10:12:37 +0100867 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600868 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600869
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100870 rd_data = image_get_data (rd_hdr);
871 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100872 rd_load = image_get_load (rd_hdr);
873 break;
874#if defined(CONFIG_FIT)
875 case IMAGE_FORMAT_FIT:
876 fit_hdr = (void *)rd_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100877 printf ("## Loading init Ramdisk from FIT "
878 "Image at %08lx ...\n", rd_addr);
879
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100880 show_boot_progress (120);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100881 if (!fit_check_format (fit_hdr)) {
882 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100883 show_boot_progress (-120);
Michal Simekc78fce62008-07-11 10:43:13 +0200884 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100885 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100886 show_boot_progress (121);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100887
888 if (!fit_uname_ramdisk) {
889 /*
890 * no ramdisk image node unit name, try to get config
891 * node first. If config unit node name is NULL
892 * fit_conf_get_node() will try to find default config node
893 */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100894 show_boot_progress (122);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100895 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
896 if (cfg_noffset < 0) {
897 puts ("Could not find configuration node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100898 show_boot_progress (-122);
Michal Simekc78fce62008-07-11 10:43:13 +0200899 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100900 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100901 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
902 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100903
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100904 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100905 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
906 } else {
907 /* get ramdisk component image node offset */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100908 show_boot_progress (123);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100909 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
910 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100911 if (rd_noffset < 0) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100912 puts ("Could not find subimage node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100913 show_boot_progress (-124);
Michal Simekc78fce62008-07-11 10:43:13 +0200914 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100915 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100916
917 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
918
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100919 show_boot_progress (125);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100920 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
Michal Simekc78fce62008-07-11 10:43:13 +0200921 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100922
923 /* get ramdisk image data address and length */
924 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
925 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100926 show_boot_progress (-127);
Michal Simekc78fce62008-07-11 10:43:13 +0200927 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100928 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100929 show_boot_progress (128);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100930
931 rd_data = (ulong)data;
932 rd_len = size;
933
934 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
935 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100936 show_boot_progress (-129);
Michal Simekc78fce62008-07-11 10:43:13 +0200937 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100938 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100939 show_boot_progress (129);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100940
941 images->fit_hdr_rd = fit_hdr;
942 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100943 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100944 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100945#endif
946 default:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100947 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +0100948 rd_data = rd_len = rd_load = 0;
Kumar Galaea86b9e2008-08-29 19:08:29 -0500949 return 1;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100950 }
951
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100952#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100953 /*
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100954 * We need to copy the ramdisk to SRAM to let Linux boot
955 */
956 if (rd_data) {
957 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
958 rd_data = rd_load;
959 }
960#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
961
962 } else if (images->legacy_hdr_valid &&
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200963 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100964 /*
965 * Now check if we have a legacy mult-component image,
966 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100967 */
968 show_boot_progress (13);
969 printf ("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100970 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100971 (ulong)images->legacy_hdr_os);
972
973 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100974 } else {
975 /*
976 * no initrd image
977 */
978 show_boot_progress (14);
979 rd_len = rd_data = 0;
980 }
981
982 if (!rd_data) {
983 debug ("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100984 } else {
985 *rd_start = rd_data;
986 *rd_end = rd_data + rd_len;
987 }
988 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
989 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -0600990
991 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100992}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100993
Daniel Hellstrombf3d8b32008-03-28 08:29:26 +0100994#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100995/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100996 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -0600997 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100998 * @rd_data: ramdisk data start address
999 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001000 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1001 * start address (after possible relocation)
1002 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1003 * end address (after possible relocation)
1004 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001005 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001006 * variable and if requested ramdisk data is moved to a specified location.
1007 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001008 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1009 * start/end addresses if ramdisk image start and len were provided,
1010 * otherwise set initrd_start and initrd_end set to zeros.
1011 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001012 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001013 * 0 - success
1014 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001015 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001016int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001017 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001018{
1019 char *s;
1020 ulong initrd_high;
1021 int initrd_copy_to_ram = 1;
1022
1023 if ((s = getenv ("initrd_high")) != NULL) {
1024 /* a value of "no" or a similar string will act like 0,
1025 * turning the "load high" feature off. This is intentional.
1026 */
1027 initrd_high = simple_strtoul (s, NULL, 16);
1028 if (initrd_high == ~0)
1029 initrd_copy_to_ram = 0;
1030 } else {
1031 /* not set, no restrictions to load high */
1032 initrd_high = ~0;
1033 }
1034
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001035
1036#ifdef CONFIG_LOGBUFFER
1037 /* Prevent initrd from overwriting logbuffer */
1038 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1039#endif
1040
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001041 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1042 initrd_high, initrd_copy_to_ram);
1043
1044 if (rd_data) {
1045 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1046 debug (" in-place initrd\n");
1047 *initrd_start = rd_data;
1048 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001049 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001050 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001051 if (initrd_high)
Becky Bruce391fd932008-06-09 20:37:18 -05001052 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001053 else
Becky Bruce391fd932008-06-09 20:37:18 -05001054 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001055
Kumar Galae822d7f2008-02-27 21:51:49 -06001056 if (*initrd_start == 0) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001057 puts ("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001058 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001059 }
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001060 show_boot_progress (12);
1061
1062 *initrd_end = *initrd_start + rd_len;
1063 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1064 *initrd_start, *initrd_end);
1065
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001066 memmove_wd ((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001067 (void *)rd_data, rd_len, CHUNKSZ);
1068
1069 puts ("OK\n");
1070 }
1071 } else {
1072 *initrd_start = 0;
1073 *initrd_end = 0;
1074 }
1075 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1076 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001077
Kumar Galae822d7f2008-02-27 21:51:49 -06001078 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001079
Kumar Galae822d7f2008-02-27 21:51:49 -06001080error:
1081 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001082}
Jean-Christophe PLAGNIOL-VILLARDcd673452008-11-24 13:33:51 +01001083#endif /* defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC) */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001084
Kumar Gala06a09912008-08-15 08:24:38 -05001085#ifdef CONFIG_OF_LIBFDT
1086static void fdt_error (const char *msg)
1087{
1088 puts ("ERROR: ");
1089 puts (msg);
1090 puts (" - must RESET the board to recover.\n");
1091}
1092
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001093static const image_header_t *image_get_fdt (ulong fdt_addr)
Kumar Gala06a09912008-08-15 08:24:38 -05001094{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001095 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001096
1097 image_print_contents (fdt_hdr);
1098
1099 puts (" Verifying Checksum ... ");
1100 if (!image_check_hcrc (fdt_hdr)) {
1101 fdt_error ("fdt header checksum invalid");
1102 return NULL;
1103 }
1104
1105 if (!image_check_dcrc (fdt_hdr)) {
1106 fdt_error ("fdt checksum invalid");
1107 return NULL;
1108 }
1109 puts ("OK\n");
1110
1111 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1112 fdt_error ("uImage is not a fdt");
1113 return NULL;
1114 }
1115 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1116 fdt_error ("uImage is compressed");
1117 return NULL;
1118 }
1119 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1120 fdt_error ("uImage data is not a fdt");
1121 return NULL;
1122 }
1123 return fdt_hdr;
1124}
1125
1126/**
1127 * fit_check_fdt - verify FIT format FDT subimage
1128 * @fit_hdr: pointer to the FIT header
1129 * fdt_noffset: FDT subimage node offset within FIT image
1130 * @verify: data CRC verification flag
1131 *
1132 * fit_check_fdt() verifies integrity of the FDT subimage and from
1133 * specified FIT image.
1134 *
1135 * returns:
1136 * 1, on success
1137 * 0, on failure
1138 */
1139#if defined(CONFIG_FIT)
1140static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1141{
1142 fit_image_print (fit, fdt_noffset, " ");
1143
1144 if (verify) {
1145 puts (" Verifying Hash Integrity ... ");
1146 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1147 fdt_error ("Bad Data Hash");
1148 return 0;
1149 }
1150 puts ("OK\n");
1151 }
1152
1153 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1154 fdt_error ("Not a FDT image");
1155 return 0;
1156 }
1157
1158 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1159 fdt_error ("FDT image is compressed");
1160 return 0;
1161 }
1162
1163 return 1;
1164}
1165#endif /* CONFIG_FIT */
1166
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001167#ifndef CONFIG_SYS_FDT_PAD
1168#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Gala06a09912008-08-15 08:24:38 -05001169#endif
1170
1171/**
1172 * boot_relocate_fdt - relocate flat device tree
1173 * @lmb: pointer to lmb handle, will be used for memory mgmt
1174 * @bootmap_base: base address of the bootmap region
1175 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1176 * @of_size: pointer to a ulong variable, will hold fdt length
1177 *
1178 * boot_relocate_fdt() determines if the of_flat_tree address is within
1179 * the bootmap and if not relocates it into that region
1180 *
1181 * of_flat_tree and of_size are set to final (after relocation) values
1182 *
1183 * returns:
1184 * 0 - success
1185 * 1 - failure
1186 */
1187int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
1188 char **of_flat_tree, ulong *of_size)
1189{
1190 char *fdt_blob = *of_flat_tree;
1191 ulong relocate = 0;
1192 ulong of_len = 0;
1193
1194 /* nothing to do */
1195 if (*of_size == 0)
1196 return 0;
1197
1198 if (fdt_check_header (fdt_blob) != 0) {
1199 fdt_error ("image is not a fdt");
1200 goto error;
1201 }
1202
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001203#ifndef CONFIG_SYS_NO_FLASH
Kumar Gala06a09912008-08-15 08:24:38 -05001204 /* move the blob if it is in flash (set relocate) */
1205 if (addr2info ((ulong)fdt_blob) != NULL)
1206 relocate = 1;
1207#endif
1208
1209 /*
1210 * The blob needs to be inside the boot mapping.
1211 */
1212 if (fdt_blob < (char *)bootmap_base)
1213 relocate = 1;
1214
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001215 if ((fdt_blob + *of_size + CONFIG_SYS_FDT_PAD) >=
1216 ((char *)CONFIG_SYS_BOOTMAPSZ + bootmap_base))
Kumar Gala06a09912008-08-15 08:24:38 -05001217 relocate = 1;
1218
1219 /* move flattend device tree if needed */
1220 if (relocate) {
1221 int err;
1222 ulong of_start = 0;
1223
1224 /* position on a 4K boundary before the alloc_current */
1225 /* Pad the FDT by a specified amount */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001226 of_len = *of_size + CONFIG_SYS_FDT_PAD;
Kumar Gala06a09912008-08-15 08:24:38 -05001227 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001228 (CONFIG_SYS_BOOTMAPSZ + bootmap_base));
Kumar Gala06a09912008-08-15 08:24:38 -05001229
1230 if (of_start == 0) {
1231 puts("device tree - allocation error\n");
1232 goto error;
1233 }
1234
1235 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
1236 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
1237 of_len, of_len);
1238
1239 printf (" Loading Device Tree to %08lx, end %08lx ... ",
1240 of_start, of_start + of_len - 1);
1241
1242 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
1243 if (err != 0) {
1244 fdt_error ("fdt move failed");
1245 goto error;
1246 }
1247 puts ("OK\n");
1248
1249 *of_flat_tree = (char *)of_start;
1250 *of_size = of_len;
1251 } else {
1252 *of_flat_tree = fdt_blob;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001253 of_len = (CONFIG_SYS_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
Kumar Gala06a09912008-08-15 08:24:38 -05001254 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
1255 fdt_set_totalsize(*of_flat_tree, of_len);
1256
1257 *of_size = of_len;
1258 }
1259
Kumar Gala54f9c862008-08-15 08:24:39 -05001260 set_working_fdt_addr(*of_flat_tree);
Kumar Gala06a09912008-08-15 08:24:38 -05001261 return 0;
1262
1263error:
1264 return 1;
1265}
1266
1267/**
1268 * boot_get_fdt - main fdt handling routine
1269 * @argc: command argument count
1270 * @argv: command argument list
1271 * @images: pointer to the bootm images structure
1272 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1273 * @of_size: pointer to a ulong variable, will hold fdt length
1274 *
1275 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1276 * Curently supported are the following ramdisk sources:
1277 * - multicomponent kernel/ramdisk image,
1278 * - commandline provided address of decicated ramdisk image.
1279 *
1280 * returns:
1281 * 0, if fdt image was found and valid, or skipped
1282 * of_flat_tree and of_size are set to fdt start address and length if
1283 * fdt image is found and valid
1284 *
1285 * 1, if fdt image is found but corrupted
1286 * of_flat_tree and of_size are set to 0 if no fdt exists
1287 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001288int boot_get_fdt (int flag, int argc, char * const argv[], bootm_headers_t *images,
Kumar Gala06a09912008-08-15 08:24:38 -05001289 char **of_flat_tree, ulong *of_size)
1290{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001291 const image_header_t *fdt_hdr;
Kumar Gala06a09912008-08-15 08:24:38 -05001292 ulong fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001293 char *fdt_blob = NULL;
1294 ulong image_start, image_end;
1295 ulong load_start, load_end;
1296#if defined(CONFIG_FIT)
1297 void *fit_hdr;
1298 const char *fit_uname_config = NULL;
1299 const char *fit_uname_fdt = NULL;
1300 ulong default_addr;
1301 int cfg_noffset;
1302 int fdt_noffset;
1303 const void *data;
1304 size_t size;
1305#endif
1306
1307 *of_flat_tree = NULL;
1308 *of_size = 0;
1309
1310 if (argc > 3 || genimg_has_config (images)) {
1311#if defined(CONFIG_FIT)
1312 if (argc > 3) {
1313 /*
1314 * If the FDT blob comes from the FIT image and the
1315 * FIT image address is omitted in the command line
1316 * argument, try to use ramdisk or os FIT image
1317 * address or default load address.
1318 */
1319 if (images->fit_uname_rd)
1320 default_addr = (ulong)images->fit_hdr_rd;
1321 else if (images->fit_uname_os)
1322 default_addr = (ulong)images->fit_hdr_os;
1323 else
1324 default_addr = load_addr;
1325
1326 if (fit_parse_conf (argv[3], default_addr,
1327 &fdt_addr, &fit_uname_config)) {
1328 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1329 fit_uname_config, fdt_addr);
1330 } else if (fit_parse_subimage (argv[3], default_addr,
1331 &fdt_addr, &fit_uname_fdt)) {
1332 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1333 fit_uname_fdt, fdt_addr);
1334 } else
1335#endif
1336 {
1337 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1338 debug ("* fdt: cmdline image address = 0x%08lx\n",
1339 fdt_addr);
1340 }
1341#if defined(CONFIG_FIT)
1342 } else {
1343 /* use FIT configuration provided in first bootm
1344 * command argument
1345 */
1346 fdt_addr = (ulong)images->fit_hdr_os;
1347 fit_uname_config = images->fit_uname_cfg;
1348 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1349 fit_uname_config, fdt_addr);
1350
1351 /*
1352 * Check whether configuration has FDT blob defined,
1353 * if not quit silently.
1354 */
1355 fit_hdr = (void *)fdt_addr;
1356 cfg_noffset = fit_conf_get_node (fit_hdr,
1357 fit_uname_config);
1358 if (cfg_noffset < 0) {
1359 debug ("* fdt: no such config\n");
1360 return 0;
1361 }
1362
1363 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1364 cfg_noffset);
1365 if (fdt_noffset < 0) {
1366 debug ("* fdt: no fdt in config\n");
1367 return 0;
1368 }
1369 }
1370#endif
1371
1372 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1373 fdt_addr);
1374
1375 /* copy from dataflash if needed */
1376 fdt_addr = genimg_get_image (fdt_addr);
1377
1378 /*
1379 * Check if there is an FDT image at the
1380 * address provided in the second bootm argument
1381 * check image type, for FIT images get a FIT node.
1382 */
1383 switch (genimg_get_format ((void *)fdt_addr)) {
1384 case IMAGE_FORMAT_LEGACY:
1385 /* verify fdt_addr points to a valid image header */
1386 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1387 fdt_addr);
1388 fdt_hdr = image_get_fdt (fdt_addr);
1389 if (!fdt_hdr)
1390 goto error;
1391
1392 /*
1393 * move image data to the load address,
1394 * make sure we don't overwrite initial image
1395 */
1396 image_start = (ulong)fdt_hdr;
1397 image_end = image_get_image_end (fdt_hdr);
1398
1399 load_start = image_get_load (fdt_hdr);
1400 load_end = load_start + image_get_data_size (fdt_hdr);
1401
1402 if ((load_start < image_end) && (load_end > image_start)) {
1403 fdt_error ("fdt overwritten");
1404 goto error;
1405 }
1406
1407 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1408 image_get_data (fdt_hdr), load_start);
1409
1410 memmove ((void *)load_start,
1411 (void *)image_get_data (fdt_hdr),
1412 image_get_data_size (fdt_hdr));
1413
1414 fdt_blob = (char *)load_start;
1415 break;
1416 case IMAGE_FORMAT_FIT:
1417 /*
1418 * This case will catch both: new uImage format
1419 * (libfdt based) and raw FDT blob (also libfdt
1420 * based).
1421 */
1422#if defined(CONFIG_FIT)
1423 /* check FDT blob vs FIT blob */
1424 if (fit_check_format ((const void *)fdt_addr)) {
1425 /*
1426 * FIT image
1427 */
1428 fit_hdr = (void *)fdt_addr;
1429 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1430 fdt_addr);
1431
1432 if (!fit_uname_fdt) {
1433 /*
1434 * no FDT blob image node unit name,
1435 * try to get config node first. If
1436 * config unit node name is NULL
1437 * fit_conf_get_node() will try to
1438 * find default config node
1439 */
1440 cfg_noffset = fit_conf_get_node (fit_hdr,
1441 fit_uname_config);
1442
1443 if (cfg_noffset < 0) {
1444 fdt_error ("Could not find configuration node\n");
1445 goto error;
1446 }
1447
1448 fit_uname_config = fdt_get_name (fit_hdr,
1449 cfg_noffset, NULL);
1450 printf (" Using '%s' configuration\n",
1451 fit_uname_config);
1452
1453 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1454 cfg_noffset);
1455 fit_uname_fdt = fit_get_name (fit_hdr,
1456 fdt_noffset, NULL);
1457 } else {
1458 /* get FDT component image node offset */
1459 fdt_noffset = fit_image_get_node (fit_hdr,
1460 fit_uname_fdt);
1461 }
1462 if (fdt_noffset < 0) {
1463 fdt_error ("Could not find subimage node\n");
1464 goto error;
1465 }
1466
1467 printf (" Trying '%s' FDT blob subimage\n",
1468 fit_uname_fdt);
1469
1470 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1471 images->verify))
1472 goto error;
1473
1474 /* get ramdisk image data address and length */
1475 if (fit_image_get_data (fit_hdr, fdt_noffset,
1476 &data, &size)) {
1477 fdt_error ("Could not find FDT subimage data");
1478 goto error;
1479 }
1480
1481 /* verift that image data is a proper FDT blob */
1482 if (fdt_check_header ((char *)data) != 0) {
1483 fdt_error ("Subimage data is not a FTD");
1484 goto error;
1485 }
1486
1487 /*
1488 * move image data to the load address,
1489 * make sure we don't overwrite initial image
1490 */
1491 image_start = (ulong)fit_hdr;
1492 image_end = fit_get_end (fit_hdr);
1493
1494 if (fit_image_get_load (fit_hdr, fdt_noffset,
1495 &load_start) == 0) {
1496 load_end = load_start + size;
1497
1498 if ((load_start < image_end) &&
1499 (load_end > image_start)) {
1500 fdt_error ("FDT overwritten");
1501 goto error;
1502 }
1503
1504 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1505 (ulong)data, load_start);
1506
1507 memmove ((void *)load_start,
1508 (void *)data, size);
1509
1510 fdt_blob = (char *)load_start;
1511 } else {
1512 fdt_blob = (char *)data;
1513 }
1514
1515 images->fit_hdr_fdt = fit_hdr;
1516 images->fit_uname_fdt = fit_uname_fdt;
1517 images->fit_noffset_fdt = fdt_noffset;
1518 break;
1519 } else
1520#endif
1521 {
1522 /*
1523 * FDT blob
1524 */
1525 fdt_blob = (char *)fdt_addr;
1526 debug ("* fdt: raw FDT blob\n");
1527 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1528 }
1529 break;
1530 default:
Anatolij Gustschin8e024942008-08-29 21:04:45 +02001531 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001532 goto error;
1533 }
1534
1535 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1536
1537 } else if (images->legacy_hdr_valid &&
1538 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1539
1540 ulong fdt_data, fdt_len;
1541
1542 /*
1543 * Now check if we have a legacy multi-component image,
1544 * get second entry data start address and len.
1545 */
1546 printf ("## Flattened Device Tree from multi "
1547 "component Image at %08lX\n",
1548 (ulong)images->legacy_hdr_os);
1549
1550 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1551 if (fdt_len) {
1552
1553 fdt_blob = (char *)fdt_data;
1554 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1555
1556 if (fdt_check_header (fdt_blob) != 0) {
1557 fdt_error ("image is not a fdt");
1558 goto error;
1559 }
1560
1561 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
1562 fdt_error ("fdt size != image size");
1563 goto error;
1564 }
1565 } else {
1566 debug ("## No Flattened Device Tree\n");
1567 return 0;
1568 }
1569 } else {
1570 debug ("## No Flattened Device Tree\n");
1571 return 0;
1572 }
1573
1574 *of_flat_tree = fdt_blob;
1575 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
1576 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner52514692008-08-21 07:12:26 -07001577 (ulong)*of_flat_tree, *of_size);
Kumar Gala06a09912008-08-15 08:24:38 -05001578
1579 return 0;
1580
1581error:
1582 *of_flat_tree = 0;
1583 *of_size = 0;
1584 return 1;
1585}
1586#endif /* CONFIG_OF_LIBFDT */
1587
Jean-Christophe PLAGNIOL-VILLARDcd673452008-11-24 13:33:51 +01001588#if defined(CONFIG_PPC) || defined(CONFIG_M68K)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001589/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001590 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001591 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001592 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1593 * @cmd_end: pointer to a ulong variable, will hold cmdline end
Kumar Galae822d7f2008-02-27 21:51:49 -06001594 * @bootmap_base: ulong variable, holds offset in physical memory to
1595 * base of bootmap
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001596 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001597 * boot_get_cmdline() allocates space for kernel command line below
Kumar Galae822d7f2008-02-27 21:51:49 -06001598 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001599 * variable is present its contents is copied to allocated kernel
1600 * command line.
1601 *
1602 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001603 * 0 - success
1604 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001605 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001606int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
Kumar Galae822d7f2008-02-27 21:51:49 -06001607 ulong bootmap_base)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001608{
1609 char *cmdline;
1610 char *s;
1611
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001612 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1613 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
Kumar Galae822d7f2008-02-27 21:51:49 -06001614
1615 if (cmdline == NULL)
1616 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001617
1618 if ((s = getenv("bootargs")) == NULL)
1619 s = "";
1620
1621 strcpy(cmdline, s);
1622
1623 *cmd_start = (ulong) & cmdline[0];
1624 *cmd_end = *cmd_start + strlen(cmdline);
1625
1626 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1627
Kumar Galae822d7f2008-02-27 21:51:49 -06001628 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001629}
1630
1631/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001632 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001633 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001634 * @kbd: double pointer to board info data
Kumar Galae822d7f2008-02-27 21:51:49 -06001635 * @bootmap_base: ulong variable, holds offset in physical memory to
1636 * base of bootmap
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001637 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001638 * boot_get_kbd() allocates space for kernel copy of board info data below
Kumar Galae822d7f2008-02-27 21:51:49 -06001639 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1640 * the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001641 *
1642 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001643 * 0 - success
1644 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001645 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001646int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001647{
Becky Bruce391fd932008-06-09 20:37:18 -05001648 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001649 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
Kumar Galae822d7f2008-02-27 21:51:49 -06001650 if (*kbd == NULL)
1651 return -1;
1652
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001653 **kbd = *(gd->bd);
1654
1655 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1656
1657#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1658 do_bdinfo(NULL, 0, 0, NULL);
1659#endif
1660
Kumar Galae822d7f2008-02-27 21:51:49 -06001661 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001662}
1663#endif /* CONFIG_PPC || CONFIG_M68K */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001664#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001665
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001666#if defined(CONFIG_FIT)
1667/*****************************************************************************/
1668/* New uImage format routines */
1669/*****************************************************************************/
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001670#ifndef USE_HOSTCC
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001671static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1672 ulong *addr, const char **name)
1673{
1674 const char *sep;
1675
1676 *addr = addr_curr;
1677 *name = NULL;
1678
1679 sep = strchr (spec, sepc);
1680 if (sep) {
1681 if (sep - spec > 0)
1682 *addr = simple_strtoul (spec, NULL, 16);
1683
1684 *name = sep + 1;
1685 return 1;
1686 }
1687
1688 return 0;
1689}
1690
1691/**
1692 * fit_parse_conf - parse FIT configuration spec
1693 * @spec: input string, containing configuration spec
1694 * @add_curr: current image address (to be used as a possible default)
1695 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1696 * configuration
1697 * @conf_name double pointer to a char, will hold pointer to a configuration
1698 * unit name
1699 *
1700 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1701 * where <addr> is a FIT image address that contains configuration
1702 * with a <conf> unit name.
1703 *
1704 * Address part is optional, and if omitted default add_curr will
1705 * be used instead.
1706 *
1707 * returns:
1708 * 1 if spec is a valid configuration string,
1709 * addr and conf_name are set accordingly
1710 * 0 otherwise
1711 */
1712inline int fit_parse_conf (const char *spec, ulong addr_curr,
1713 ulong *addr, const char **conf_name)
1714{
1715 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1716}
1717
1718/**
1719 * fit_parse_subimage - parse FIT subimage spec
1720 * @spec: input string, containing subimage spec
1721 * @add_curr: current image address (to be used as a possible default)
1722 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1723 * subimage
1724 * @image_name: double pointer to a char, will hold pointer to a subimage name
1725 *
1726 * fit_parse_subimage() expects subimage spec in the for of
1727 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1728 * subimage with a <subimg> unit name.
1729 *
1730 * Address part is optional, and if omitted default add_curr will
1731 * be used instead.
1732 *
1733 * returns:
1734 * 1 if spec is a valid subimage string,
1735 * addr and image_name are set accordingly
1736 * 0 otherwise
1737 */
1738inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1739 ulong *addr, const char **image_name)
1740{
1741 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1742}
Marian Balakowicz570abb02008-02-29 15:59:59 +01001743#endif /* !USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001744
1745static void fit_get_debug (const void *fit, int noffset,
1746 char *prop_name, int err)
1747{
1748 debug ("Can't get '%s' property from FIT 0x%08lx, "
1749 "node: offset %d, name %s (%s)\n",
1750 prop_name, (ulong)fit, noffset,
1751 fit_get_name (fit, noffset, NULL),
1752 fdt_strerror (err));
1753}
1754
1755/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001756 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001757 * @fit: pointer to the FIT format image header
1758 * @p: pointer to prefix string
1759 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001760 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001761 * The routine prints out FIT image properties (root node level) follwed by
1762 * the details of each component image.
1763 *
1764 * returns:
1765 * no returned results
1766 */
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001767void fit_print_contents (const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001768{
1769 char *desc;
1770 char *uname;
1771 int images_noffset;
1772 int confs_noffset;
1773 int noffset;
1774 int ndepth;
1775 int count = 0;
1776 int ret;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001777 const char *p;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001778#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1779 time_t timestamp;
1780#endif
1781
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001782#ifdef USE_HOSTCC
1783 p = "";
1784#else
1785 p = " ";
1786#endif
1787
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001788 /* Root node properties */
1789 ret = fit_get_desc (fit, 0, &desc);
1790 printf ("%sFIT description: ", p);
1791 if (ret)
1792 printf ("unavailable\n");
1793 else
1794 printf ("%s\n", desc);
1795
1796#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1797 ret = fit_get_timestamp (fit, 0, &timestamp);
1798 printf ("%sCreated: ", p);
1799 if (ret)
1800 printf ("unavailable\n");
1801 else
1802 genimg_print_time (timestamp);
1803#endif
1804
1805 /* Find images parent node offset */
1806 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1807 if (images_noffset < 0) {
1808 printf ("Can't find images parent node '%s' (%s)\n",
1809 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1810 return;
1811 }
1812
1813 /* Process its subnodes, print out component images details */
1814 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1815 (noffset >= 0) && (ndepth > 0);
1816 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1817 if (ndepth == 1) {
1818 /*
1819 * Direct child node of the images parent node,
1820 * i.e. component image node.
1821 */
1822 printf ("%s Image %u (%s)\n", p, count++,
1823 fit_get_name(fit, noffset, NULL));
1824
1825 fit_image_print (fit, noffset, p);
1826 }
1827 }
1828
1829 /* Find configurations parent node offset */
1830 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1831 if (confs_noffset < 0) {
1832 debug ("Can't get configurations parent node '%s' (%s)\n",
1833 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1834 return;
1835 }
1836
1837 /* get default configuration unit name from default property */
1838 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1839 if (uname)
1840 printf ("%s Default Configuration: '%s'\n", p, uname);
1841
1842 /* Process its subnodes, print out configurations details */
1843 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1844 (noffset >= 0) && (ndepth > 0);
1845 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1846 if (ndepth == 1) {
1847 /*
1848 * Direct child node of the configurations parent node,
1849 * i.e. configuration node.
1850 */
1851 printf ("%s Configuration %u (%s)\n", p, count++,
1852 fit_get_name(fit, noffset, NULL));
1853
1854 fit_conf_print (fit, noffset, p);
1855 }
1856 }
1857}
1858
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001859/**
1860 * fit_image_print - prints out the FIT component image details
1861 * @fit: pointer to the FIT format image header
1862 * @image_noffset: offset of the component image node
1863 * @p: pointer to prefix string
1864 *
1865 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001866 * image. If present, hash nodes are printed out as well. Load
1867 * address for images of type firmware is also printed out. Since the load
1868 * address is not mandatory for firmware images, it will be output as
1869 * "unavailable" when not present.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001870 *
1871 * returns:
1872 * no returned results
1873 */
1874void fit_image_print (const void *fit, int image_noffset, const char *p)
1875{
1876 char *desc;
1877 uint8_t type, arch, os, comp;
1878 size_t size;
1879 ulong load, entry;
1880 const void *data;
1881 int noffset;
1882 int ndepth;
1883 int ret;
1884
1885 /* Mandatory properties */
1886 ret = fit_get_desc (fit, image_noffset, &desc);
1887 printf ("%s Description: ", p);
1888 if (ret)
1889 printf ("unavailable\n");
1890 else
1891 printf ("%s\n", desc);
1892
1893 fit_image_get_type (fit, image_noffset, &type);
1894 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1895
1896 fit_image_get_comp (fit, image_noffset, &comp);
1897 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1898
1899 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1900
1901#ifndef USE_HOSTCC
1902 printf ("%s Data Start: ", p);
1903 if (ret)
1904 printf ("unavailable\n");
1905 else
1906 printf ("0x%08lx\n", (ulong)data);
1907#endif
1908
1909 printf ("%s Data Size: ", p);
1910 if (ret)
1911 printf ("unavailable\n");
1912 else
1913 genimg_print_size (size);
1914
1915 /* Remaining, type dependent properties */
1916 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1917 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1918 (type == IH_TYPE_FLATDT)) {
1919 fit_image_get_arch (fit, image_noffset, &arch);
1920 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1921 }
1922
1923 if (type == IH_TYPE_KERNEL) {
1924 fit_image_get_os (fit, image_noffset, &os);
1925 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1926 }
1927
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001928 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1929 (type == IH_TYPE_FIRMWARE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001930 ret = fit_image_get_load (fit, image_noffset, &load);
1931 printf ("%s Load Address: ", p);
1932 if (ret)
1933 printf ("unavailable\n");
1934 else
1935 printf ("0x%08lx\n", load);
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001936 }
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001937
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001938 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001939 fit_image_get_entry (fit, image_noffset, &entry);
1940 printf ("%s Entry Point: ", p);
1941 if (ret)
1942 printf ("unavailable\n");
1943 else
1944 printf ("0x%08lx\n", entry);
1945 }
1946
1947 /* Process all hash subnodes of the component image node */
1948 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1949 (noffset >= 0) && (ndepth > 0);
1950 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1951 if (ndepth == 1) {
1952 /* Direct child node of the component image node */
1953 fit_image_print_hash (fit, noffset, p);
1954 }
1955 }
1956}
1957
1958/**
1959 * fit_image_print_hash - prints out the hash node details
1960 * @fit: pointer to the FIT format image header
1961 * @noffset: offset of the hash node
1962 * @p: pointer to prefix string
1963 *
1964 * fit_image_print_hash() lists properies for the processed hash node
1965 *
1966 * returns:
1967 * no returned results
1968 */
1969void fit_image_print_hash (const void *fit, int noffset, const char *p)
1970{
1971 char *algo;
1972 uint8_t *value;
1973 int value_len;
1974 int i, ret;
1975
1976 /*
1977 * Check subnode name, must be equal to "hash".
1978 * Multiple hash nodes require unique unit node
1979 * names, e.g. hash@1, hash@2, etc.
1980 */
1981 if (strncmp (fit_get_name(fit, noffset, NULL),
1982 FIT_HASH_NODENAME,
1983 strlen(FIT_HASH_NODENAME)) != 0)
1984 return;
1985
1986 debug ("%s Hash node: '%s'\n", p,
1987 fit_get_name (fit, noffset, NULL));
1988
1989 printf ("%s Hash algo: ", p);
1990 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1991 printf ("invalid/unsupported\n");
1992 return;
1993 }
1994 printf ("%s\n", algo);
1995
1996 ret = fit_image_hash_get_value (fit, noffset, &value,
1997 &value_len);
1998 printf ("%s Hash value: ", p);
1999 if (ret) {
2000 printf ("unavailable\n");
2001 } else {
2002 for (i = 0; i < value_len; i++)
2003 printf ("%02x", value[i]);
2004 printf ("\n");
2005 }
2006
2007 debug ("%s Hash len: %d\n", p, value_len);
2008}
2009
2010/**
2011 * fit_get_desc - get node description property
2012 * @fit: pointer to the FIT format image header
2013 * @noffset: node offset
2014 * @desc: double pointer to the char, will hold pointer to the descrption
2015 *
2016 * fit_get_desc() reads description property from a given node, if
2017 * description is found pointer to it is returened in third call argument.
2018 *
2019 * returns:
2020 * 0, on success
2021 * -1, on failure
2022 */
2023int fit_get_desc (const void *fit, int noffset, char **desc)
2024{
2025 int len;
2026
2027 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2028 if (*desc == NULL) {
2029 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2030 return -1;
2031 }
2032
2033 return 0;
2034}
2035
2036/**
2037 * fit_get_timestamp - get node timestamp property
2038 * @fit: pointer to the FIT format image header
2039 * @noffset: node offset
2040 * @timestamp: pointer to the time_t, will hold read timestamp
2041 *
2042 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2043 * is found and has a correct size its value is retured in third call
2044 * argument.
2045 *
2046 * returns:
2047 * 0, on success
2048 * -1, on property read failure
2049 * -2, on wrong timestamp size
2050 */
2051int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2052{
2053 int len;
2054 const void *data;
2055
2056 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2057 if (data == NULL) {
2058 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2059 return -1;
2060 }
2061 if (len != sizeof (uint32_t)) {
2062 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2063 return -2;
2064 }
2065
2066 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2067 return 0;
2068}
2069
2070/**
2071 * fit_image_get_node - get node offset for component image of a given unit name
2072 * @fit: pointer to the FIT format image header
2073 * @image_uname: component image node unit name
2074 *
2075 * fit_image_get_node() finds a component image (withing the '/images'
2076 * node) of a provided unit name. If image is found its node offset is
2077 * returned to the caller.
2078 *
2079 * returns:
2080 * image node offset when found (>=0)
2081 * negative number on failure (FDT_ERR_* code)
2082 */
2083int fit_image_get_node (const void *fit, const char *image_uname)
2084{
2085 int noffset, images_noffset;
2086
2087 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2088 if (images_noffset < 0) {
2089 debug ("Can't find images parent node '%s' (%s)\n",
2090 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2091 return images_noffset;
2092 }
2093
2094 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2095 if (noffset < 0) {
2096 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2097 image_uname, fdt_strerror (noffset));
2098 }
2099
2100 return noffset;
2101}
2102
2103/**
2104 * fit_image_get_os - get os id for a given component image node
2105 * @fit: pointer to the FIT format image header
2106 * @noffset: component image node offset
2107 * @os: pointer to the uint8_t, will hold os numeric id
2108 *
2109 * fit_image_get_os() finds os property in a given component image node.
2110 * If the property is found, its (string) value is translated to the numeric
2111 * id which is returned to the caller.
2112 *
2113 * returns:
2114 * 0, on success
2115 * -1, on failure
2116 */
2117int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2118{
2119 int len;
2120 const void *data;
2121
2122 /* Get OS name from property data */
2123 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2124 if (data == NULL) {
2125 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2126 *os = -1;
2127 return -1;
2128 }
2129
2130 /* Translate OS name to id */
2131 *os = genimg_get_os_id (data);
2132 return 0;
2133}
2134
2135/**
2136 * fit_image_get_arch - get arch id for a given component image node
2137 * @fit: pointer to the FIT format image header
2138 * @noffset: component image node offset
2139 * @arch: pointer to the uint8_t, will hold arch numeric id
2140 *
2141 * fit_image_get_arch() finds arch property in a given component image node.
2142 * If the property is found, its (string) value is translated to the numeric
2143 * id which is returned to the caller.
2144 *
2145 * returns:
2146 * 0, on success
2147 * -1, on failure
2148 */
2149int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2150{
2151 int len;
2152 const void *data;
2153
2154 /* Get architecture name from property data */
2155 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2156 if (data == NULL) {
2157 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2158 *arch = -1;
2159 return -1;
2160 }
2161
2162 /* Translate architecture name to id */
2163 *arch = genimg_get_arch_id (data);
2164 return 0;
2165}
2166
2167/**
2168 * fit_image_get_type - get type id for a given component image node
2169 * @fit: pointer to the FIT format image header
2170 * @noffset: component image node offset
2171 * @type: pointer to the uint8_t, will hold type numeric id
2172 *
2173 * fit_image_get_type() finds type property in a given component image node.
2174 * If the property is found, its (string) value is translated to the numeric
2175 * id which is returned to the caller.
2176 *
2177 * returns:
2178 * 0, on success
2179 * -1, on failure
2180 */
2181int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2182{
2183 int len;
2184 const void *data;
2185
2186 /* Get image type name from property data */
2187 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2188 if (data == NULL) {
2189 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2190 *type = -1;
2191 return -1;
2192 }
2193
2194 /* Translate image type name to id */
2195 *type = genimg_get_type_id (data);
2196 return 0;
2197}
2198
2199/**
2200 * fit_image_get_comp - get comp id for a given component image node
2201 * @fit: pointer to the FIT format image header
2202 * @noffset: component image node offset
2203 * @comp: pointer to the uint8_t, will hold comp numeric id
2204 *
2205 * fit_image_get_comp() finds comp property in a given component image node.
2206 * If the property is found, its (string) value is translated to the numeric
2207 * id which is returned to the caller.
2208 *
2209 * returns:
2210 * 0, on success
2211 * -1, on failure
2212 */
2213int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2214{
2215 int len;
2216 const void *data;
2217
2218 /* Get compression name from property data */
2219 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2220 if (data == NULL) {
2221 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2222 *comp = -1;
2223 return -1;
2224 }
2225
2226 /* Translate compression name to id */
2227 *comp = genimg_get_comp_id (data);
2228 return 0;
2229}
2230
2231/**
2232 * fit_image_get_load - get load address property for a given component image node
2233 * @fit: pointer to the FIT format image header
2234 * @noffset: component image node offset
2235 * @load: pointer to the uint32_t, will hold load address
2236 *
2237 * fit_image_get_load() finds load address property in a given component image node.
2238 * If the property is found, its value is returned to the caller.
2239 *
2240 * returns:
2241 * 0, on success
2242 * -1, on failure
2243 */
2244int fit_image_get_load (const void *fit, int noffset, ulong *load)
2245{
2246 int len;
2247 const uint32_t *data;
2248
2249 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2250 if (data == NULL) {
2251 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2252 return -1;
2253 }
2254
2255 *load = uimage_to_cpu (*data);
2256 return 0;
2257}
2258
2259/**
2260 * fit_image_get_entry - get entry point address property for a given component image node
2261 * @fit: pointer to the FIT format image header
2262 * @noffset: component image node offset
2263 * @entry: pointer to the uint32_t, will hold entry point address
2264 *
2265 * fit_image_get_entry() finds entry point address property in a given component image node.
2266 * If the property is found, its value is returned to the caller.
2267 *
2268 * returns:
2269 * 0, on success
2270 * -1, on failure
2271 */
2272int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2273{
2274 int len;
2275 const uint32_t *data;
2276
2277 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2278 if (data == NULL) {
2279 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2280 return -1;
2281 }
2282
2283 *entry = uimage_to_cpu (*data);
2284 return 0;
2285}
2286
2287/**
2288 * fit_image_get_data - get data property and its size for a given component image node
2289 * @fit: pointer to the FIT format image header
2290 * @noffset: component image node offset
2291 * @data: double pointer to void, will hold data property's data address
2292 * @size: pointer to size_t, will hold data property's data size
2293 *
2294 * fit_image_get_data() finds data property in a given component image node.
2295 * If the property is found its data start address and size are returned to
2296 * the caller.
2297 *
2298 * returns:
2299 * 0, on success
2300 * -1, on failure
2301 */
2302int fit_image_get_data (const void *fit, int noffset,
2303 const void **data, size_t *size)
2304{
2305 int len;
2306
2307 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2308 if (*data == NULL) {
2309 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2310 *size = 0;
2311 return -1;
2312 }
2313
2314 *size = len;
2315 return 0;
2316}
2317
2318/**
2319 * fit_image_hash_get_algo - get hash algorithm name
2320 * @fit: pointer to the FIT format image header
2321 * @noffset: hash node offset
2322 * @algo: double pointer to char, will hold pointer to the algorithm name
2323 *
2324 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2325 * If the property is found its data start address is returned to the caller.
2326 *
2327 * returns:
2328 * 0, on success
2329 * -1, on failure
2330 */
2331int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2332{
2333 int len;
2334
2335 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2336 if (*algo == NULL) {
2337 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2338 return -1;
2339 }
2340
2341 return 0;
2342}
2343
2344/**
2345 * fit_image_hash_get_value - get hash value and length
2346 * @fit: pointer to the FIT format image header
2347 * @noffset: hash node offset
2348 * @value: double pointer to uint8_t, will hold address of a hash value data
2349 * @value_len: pointer to an int, will hold hash data length
2350 *
2351 * fit_image_hash_get_value() finds hash value property in a given hash node.
2352 * If the property is found its data start address and size are returned to
2353 * the caller.
2354 *
2355 * returns:
2356 * 0, on success
2357 * -1, on failure
2358 */
2359int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2360 int *value_len)
2361{
2362 int len;
2363
2364 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2365 if (*value == NULL) {
2366 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2367 *value_len = 0;
2368 return -1;
2369 }
2370
2371 *value_len = len;
2372 return 0;
2373}
2374
2375/**
2376 * fit_set_timestamp - set node timestamp property
2377 * @fit: pointer to the FIT format image header
2378 * @noffset: node offset
2379 * @timestamp: timestamp value to be set
2380 *
2381 * fit_set_timestamp() attempts to set timestamp property in the requested
2382 * node and returns operation status to the caller.
2383 *
2384 * returns:
2385 * 0, on success
2386 * -1, on property read failure
2387 */
2388int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2389{
2390 uint32_t t;
2391 int ret;
2392
2393 t = cpu_to_uimage (timestamp);
2394 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2395 sizeof (uint32_t));
2396 if (ret) {
2397 printf ("Can't set '%s' property for '%s' node (%s)\n",
2398 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2399 fdt_strerror (ret));
2400 return -1;
2401 }
2402
2403 return 0;
2404}
2405
2406/**
2407 * calculate_hash - calculate and return hash for provided input data
2408 * @data: pointer to the input data
2409 * @data_len: data length
2410 * @algo: requested hash algorithm
2411 * @value: pointer to the char, will hold hash value data (caller must
2412 * allocate enough free space)
2413 * value_len: length of the calculated hash
2414 *
2415 * calculate_hash() computes input data hash according to the requested algorithm.
2416 * Resulting hash value is placed in caller provided 'value' buffer, length
2417 * of the calculated hash is returned via value_len pointer argument.
2418 *
2419 * returns:
2420 * 0, on success
2421 * -1, when algo is unsupported
2422 */
2423static int calculate_hash (const void *data, int data_len, const char *algo,
2424 uint8_t *value, int *value_len)
2425{
2426 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002427 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2428 CHUNKSZ_CRC32);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002429 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2430 *value_len = 4;
2431 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002432 sha1_csum_wd ((unsigned char *) data, data_len,
2433 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002434 *value_len = 20;
2435 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002436 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka766529f2008-03-14 16:22:34 +01002437 *value_len = 16;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002438 } else {
2439 debug ("Unsupported hash alogrithm\n");
2440 return -1;
2441 }
2442 return 0;
2443}
2444
2445#ifdef USE_HOSTCC
2446/**
2447 * fit_set_hashes - process FIT component image nodes and calculate hashes
2448 * @fit: pointer to the FIT format image header
2449 *
2450 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2451 * Hashes are calculated for all component images which have hash subnodes
2452 * with algorithm property set to one of the supported hash algorithms.
2453 *
2454 * returns
2455 * 0, on success
2456 * libfdt error code, on failure
2457 */
2458int fit_set_hashes (void *fit)
2459{
2460 int images_noffset;
2461 int noffset;
2462 int ndepth;
2463 int ret;
2464
2465 /* Find images parent node offset */
2466 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2467 if (images_noffset < 0) {
2468 printf ("Can't find images parent node '%s' (%s)\n",
2469 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2470 return images_noffset;
2471 }
2472
2473 /* Process its subnodes, print out component images details */
2474 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2475 (noffset >= 0) && (ndepth > 0);
2476 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2477 if (ndepth == 1) {
2478 /*
2479 * Direct child node of the images parent node,
2480 * i.e. component image node.
2481 */
2482 ret = fit_image_set_hashes (fit, noffset);
2483 if (ret)
2484 return ret;
2485 }
2486 }
2487
2488 return 0;
2489}
2490
2491/**
2492 * fit_image_set_hashes - calculate/set hashes for given component image node
2493 * @fit: pointer to the FIT format image header
2494 * @image_noffset: requested component image node
2495 *
2496 * fit_image_set_hashes() adds hash values for an component image node. All
2497 * existing hash subnodes are checked, if algorithm property is set to one of
2498 * the supported hash algorithms, hash value is computed and corresponding
2499 * hash node property is set, for example:
2500 *
2501 * Input component image node structure:
2502 *
2503 * o image@1 (at image_noffset)
2504 * | - data = [binary data]
2505 * o hash@1
2506 * |- algo = "sha1"
2507 *
2508 * Output component image node structure:
2509 *
2510 * o image@1 (at image_noffset)
2511 * | - data = [binary data]
2512 * o hash@1
2513 * |- algo = "sha1"
2514 * |- value = sha1(data)
2515 *
2516 * returns:
2517 * 0 on sucess
2518 * <0 on failure
2519 */
2520int fit_image_set_hashes (void *fit, int image_noffset)
2521{
2522 const void *data;
2523 size_t size;
2524 char *algo;
2525 uint8_t value[FIT_MAX_HASH_LEN];
2526 int value_len;
2527 int noffset;
2528 int ndepth;
2529
2530 /* Get image data and data length */
2531 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2532 printf ("Can't get image data/size\n");
2533 return -1;
2534 }
2535
2536 /* Process all hash subnodes of the component image node */
2537 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2538 (noffset >= 0) && (ndepth > 0);
2539 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2540 if (ndepth == 1) {
2541 /* Direct child node of the component image node */
2542
2543 /*
2544 * Check subnode name, must be equal to "hash".
2545 * Multiple hash nodes require unique unit node
2546 * names, e.g. hash@1, hash@2, etc.
2547 */
2548 if (strncmp (fit_get_name(fit, noffset, NULL),
2549 FIT_HASH_NODENAME,
2550 strlen(FIT_HASH_NODENAME)) != 0) {
2551 /* Not a hash subnode, skip it */
2552 continue;
2553 }
2554
2555 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2556 printf ("Can't get hash algo property for "
2557 "'%s' hash node in '%s' image node\n",
2558 fit_get_name (fit, noffset, NULL),
2559 fit_get_name (fit, image_noffset, NULL));
2560 return -1;
2561 }
2562
2563 if (calculate_hash (data, size, algo, value, &value_len)) {
2564 printf ("Unsupported hash algorithm (%s) for "
2565 "'%s' hash node in '%s' image node\n",
2566 algo, fit_get_name (fit, noffset, NULL),
2567 fit_get_name (fit, image_noffset, NULL));
2568 return -1;
2569 }
2570
2571 if (fit_image_hash_set_value (fit, noffset, value,
2572 value_len)) {
2573 printf ("Can't set hash value for "
2574 "'%s' hash node in '%s' image node\n",
2575 fit_get_name (fit, noffset, NULL),
2576 fit_get_name (fit, image_noffset, NULL));
2577 return -1;
2578 }
2579 }
2580 }
2581
2582 return 0;
2583}
2584
2585/**
2586 * fit_image_hash_set_value - set hash value in requested has node
2587 * @fit: pointer to the FIT format image header
2588 * @noffset: hash node offset
2589 * @value: hash value to be set
2590 * @value_len: hash value length
2591 *
2592 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2593 * given and returns operation status to the caller.
2594 *
2595 * returns
2596 * 0, on success
2597 * -1, on failure
2598 */
2599int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2600 int value_len)
2601{
2602 int ret;
2603
2604 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2605 if (ret) {
2606 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2607 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2608 fdt_strerror (ret));
2609 return -1;
2610 }
2611
2612 return 0;
2613}
2614#endif /* USE_HOSTCC */
2615
2616/**
2617 * fit_image_check_hashes - verify data intergity
2618 * @fit: pointer to the FIT format image header
2619 * @image_noffset: component image node offset
2620 *
2621 * fit_image_check_hashes() goes over component image hash nodes,
2622 * re-calculates each data hash and compares with the value stored in hash
2623 * node.
2624 *
2625 * returns:
2626 * 1, if all hashes are valid
2627 * 0, otherwise (or on error)
2628 */
2629int fit_image_check_hashes (const void *fit, int image_noffset)
2630{
2631 const void *data;
2632 size_t size;
2633 char *algo;
2634 uint8_t *fit_value;
2635 int fit_value_len;
2636 uint8_t value[FIT_MAX_HASH_LEN];
2637 int value_len;
2638 int noffset;
2639 int ndepth;
2640 char *err_msg = "";
2641
2642 /* Get image data and data length */
2643 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2644 printf ("Can't get image data/size\n");
2645 return 0;
2646 }
2647
2648 /* Process all hash subnodes of the component image node */
2649 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2650 (noffset >= 0) && (ndepth > 0);
2651 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2652 if (ndepth == 1) {
2653 /* Direct child node of the component image node */
2654
2655 /*
2656 * Check subnode name, must be equal to "hash".
2657 * Multiple hash nodes require unique unit node
2658 * names, e.g. hash@1, hash@2, etc.
2659 */
2660 if (strncmp (fit_get_name(fit, noffset, NULL),
2661 FIT_HASH_NODENAME,
2662 strlen(FIT_HASH_NODENAME)) != 0)
2663 continue;
2664
2665 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002666 err_msg = " error!\nCan't get hash algo "
2667 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002668 goto error;
2669 }
2670 printf ("%s", algo);
2671
2672 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2673 &fit_value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002674 err_msg = " error!\nCan't get hash value "
2675 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002676 goto error;
2677 }
2678
2679 if (calculate_hash (data, size, algo, value, &value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002680 err_msg = " error!\nUnsupported hash algorithm";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002681 goto error;
2682 }
2683
2684 if (value_len != fit_value_len) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002685 err_msg = " error !\nBad hash value len";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002686 goto error;
2687 } else if (memcmp (value, fit_value, value_len) != 0) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002688 err_msg = " error!\nBad hash value";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002689 goto error;
2690 }
2691 printf ("+ ");
2692 }
2693 }
2694
2695 return 1;
2696
2697error:
2698 printf ("%s for '%s' hash node in '%s' image node\n",
2699 err_msg, fit_get_name (fit, noffset, NULL),
2700 fit_get_name (fit, image_noffset, NULL));
2701 return 0;
2702}
2703
2704/**
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002705 * fit_all_image_check_hashes - verify data intergity for all images
2706 * @fit: pointer to the FIT format image header
2707 *
2708 * fit_all_image_check_hashes() goes over all images in the FIT and
2709 * for every images checks if all it's hashes are valid.
2710 *
2711 * returns:
2712 * 1, if all hashes of all images are valid
2713 * 0, otherwise (or on error)
2714 */
2715int fit_all_image_check_hashes (const void *fit)
2716{
2717 int images_noffset;
2718 int noffset;
2719 int ndepth;
2720 int count;
2721
2722 /* Find images parent node offset */
2723 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2724 if (images_noffset < 0) {
2725 printf ("Can't find images parent node '%s' (%s)\n",
2726 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2727 return 0;
2728 }
2729
2730 /* Process all image subnodes, check hashes for each */
2731 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2732 (ulong)fit);
2733 for (ndepth = 0, count = 0,
2734 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2735 (noffset >= 0) && (ndepth > 0);
2736 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2737 if (ndepth == 1) {
2738 /*
2739 * Direct child node of the images parent node,
2740 * i.e. component image node.
2741 */
2742 printf (" Hash(es) for Image %u (%s): ", count++,
2743 fit_get_name (fit, noffset, NULL));
2744
2745 if (!fit_image_check_hashes (fit, noffset))
2746 return 0;
2747 printf ("\n");
2748 }
2749 }
2750 return 1;
2751}
2752
2753/**
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002754 * fit_image_check_os - check whether image node is of a given os type
2755 * @fit: pointer to the FIT format image header
2756 * @noffset: component image node offset
2757 * @os: requested image os
2758 *
2759 * fit_image_check_os() reads image os property and compares its numeric
2760 * id with the requested os. Comparison result is returned to the caller.
2761 *
2762 * returns:
2763 * 1 if image is of given os type
2764 * 0 otherwise (or on error)
2765 */
2766int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2767{
2768 uint8_t image_os;
2769
2770 if (fit_image_get_os (fit, noffset, &image_os))
2771 return 0;
2772 return (os == image_os);
2773}
2774
2775/**
2776 * fit_image_check_arch - check whether image node is of a given arch
2777 * @fit: pointer to the FIT format image header
2778 * @noffset: component image node offset
2779 * @arch: requested imagearch
2780 *
2781 * fit_image_check_arch() reads image arch property and compares its numeric
2782 * id with the requested arch. Comparison result is returned to the caller.
2783 *
2784 * returns:
2785 * 1 if image is of given arch
2786 * 0 otherwise (or on error)
2787 */
2788int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2789{
2790 uint8_t image_arch;
2791
2792 if (fit_image_get_arch (fit, noffset, &image_arch))
2793 return 0;
2794 return (arch == image_arch);
2795}
2796
2797/**
2798 * fit_image_check_type - check whether image node is of a given type
2799 * @fit: pointer to the FIT format image header
2800 * @noffset: component image node offset
2801 * @type: requested image type
2802 *
2803 * fit_image_check_type() reads image type property and compares its numeric
2804 * id with the requested type. Comparison result is returned to the caller.
2805 *
2806 * returns:
2807 * 1 if image is of given type
2808 * 0 otherwise (or on error)
2809 */
2810int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2811{
2812 uint8_t image_type;
2813
2814 if (fit_image_get_type (fit, noffset, &image_type))
2815 return 0;
2816 return (type == image_type);
2817}
2818
2819/**
2820 * fit_image_check_comp - check whether image node uses given compression
2821 * @fit: pointer to the FIT format image header
2822 * @noffset: component image node offset
2823 * @comp: requested image compression type
2824 *
2825 * fit_image_check_comp() reads image compression property and compares its
2826 * numeric id with the requested compression type. Comparison result is
2827 * returned to the caller.
2828 *
2829 * returns:
2830 * 1 if image uses requested compression
2831 * 0 otherwise (or on error)
2832 */
2833int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2834{
2835 uint8_t image_comp;
2836
2837 if (fit_image_get_comp (fit, noffset, &image_comp))
2838 return 0;
2839 return (comp == image_comp);
2840}
2841
2842/**
2843 * fit_check_format - sanity check FIT image format
2844 * @fit: pointer to the FIT format image header
2845 *
2846 * fit_check_format() runs a basic sanity FIT image verification.
2847 * Routine checks for mandatory properties, nodes, etc.
2848 *
2849 * returns:
2850 * 1, on success
2851 * 0, on failure
2852 */
2853int fit_check_format (const void *fit)
2854{
2855 /* mandatory / node 'description' property */
2856 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2857 debug ("Wrong FIT format: no description\n");
2858 return 0;
2859 }
2860
2861#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2862 /* mandatory / node 'timestamp' property */
2863 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002864 debug ("Wrong FIT format: no timestamp\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002865 return 0;
2866 }
2867#endif
2868
2869 /* mandatory subimages parent '/images' node */
2870 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2871 debug ("Wrong FIT format: no images parent node\n");
2872 return 0;
2873 }
2874
2875 return 1;
2876}
2877
2878/**
2879 * fit_conf_get_node - get node offset for configuration of a given unit name
2880 * @fit: pointer to the FIT format image header
2881 * @conf_uname: configuration node unit name
2882 *
2883 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2884 * parant node) of a provided unit name. If configuration is found its node offset
2885 * is returned to the caller.
2886 *
2887 * When NULL is provided in second argument fit_conf_get_node() will search
2888 * for a default configuration node instead. Default configuration node unit name
2889 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2890 *
2891 * returns:
2892 * configuration node offset when found (>=0)
2893 * negative number on failure (FDT_ERR_* code)
2894 */
2895int fit_conf_get_node (const void *fit, const char *conf_uname)
2896{
2897 int noffset, confs_noffset;
2898 int len;
2899
2900 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2901 if (confs_noffset < 0) {
2902 debug ("Can't find configurations parent node '%s' (%s)\n",
2903 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2904 return confs_noffset;
2905 }
2906
2907 if (conf_uname == NULL) {
2908 /* get configuration unit name from the default property */
2909 debug ("No configuration specified, trying default...\n");
2910 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2911 if (conf_uname == NULL) {
2912 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2913 return len;
2914 }
2915 debug ("Found default configuration: '%s'\n", conf_uname);
2916 }
2917
2918 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2919 if (noffset < 0) {
2920 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2921 conf_uname, fdt_strerror (noffset));
2922 }
2923
2924 return noffset;
2925}
2926
2927static int __fit_conf_get_prop_node (const void *fit, int noffset,
2928 const char *prop_name)
2929{
2930 char *uname;
2931 int len;
2932
2933 /* get kernel image unit name from configuration kernel property */
2934 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2935 if (uname == NULL)
2936 return len;
2937
2938 return fit_image_get_node (fit, uname);
2939}
2940
2941/**
2942 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2943 * a given configuration
2944 * @fit: pointer to the FIT format image header
2945 * @noffset: configuration node offset
2946 *
2947 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2948 * configuration FIT_KERNEL_PROP property and translates it to the node
2949 * offset.
2950 *
2951 * returns:
2952 * image node offset when found (>=0)
2953 * negative number on failure (FDT_ERR_* code)
2954 */
2955int fit_conf_get_kernel_node (const void *fit, int noffset)
2956{
2957 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2958}
2959
2960/**
2961 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2962 * a given configuration
2963 * @fit: pointer to the FIT format image header
2964 * @noffset: configuration node offset
2965 *
2966 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2967 * configuration FIT_KERNEL_PROP property and translates it to the node
2968 * offset.
2969 *
2970 * returns:
2971 * image node offset when found (>=0)
2972 * negative number on failure (FDT_ERR_* code)
2973 */
2974int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2975{
2976 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2977}
2978
2979/**
2980 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2981 * a given configuration
2982 * @fit: pointer to the FIT format image header
2983 * @noffset: configuration node offset
2984 *
2985 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2986 * configuration FIT_KERNEL_PROP property and translates it to the node
2987 * offset.
2988 *
2989 * returns:
2990 * image node offset when found (>=0)
2991 * negative number on failure (FDT_ERR_* code)
2992 */
2993int fit_conf_get_fdt_node (const void *fit, int noffset)
2994{
2995 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2996}
2997
2998/**
2999 * fit_conf_print - prints out the FIT configuration details
3000 * @fit: pointer to the FIT format image header
Marian Balakowiczf773bea2008-03-12 10:35:46 +01003001 * @noffset: offset of the configuration node
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003002 * @p: pointer to prefix string
3003 *
3004 * fit_conf_print() lists all mandatory properies for the processed
3005 * configuration node.
3006 *
3007 * returns:
3008 * no returned results
3009 */
3010void fit_conf_print (const void *fit, int noffset, const char *p)
3011{
3012 char *desc;
3013 char *uname;
3014 int ret;
3015
3016 /* Mandatory properties */
3017 ret = fit_get_desc (fit, noffset, &desc);
3018 printf ("%s Description: ", p);
3019 if (ret)
3020 printf ("unavailable\n");
3021 else
3022 printf ("%s\n", desc);
3023
3024 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3025 printf ("%s Kernel: ", p);
3026 if (uname == NULL)
3027 printf ("unavailable\n");
3028 else
3029 printf ("%s\n", uname);
3030
3031 /* Optional properties */
3032 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3033 if (uname)
3034 printf ("%s Init Ramdisk: %s\n", p, uname);
3035
3036 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3037 if (uname)
3038 printf ("%s FDT: %s\n", p, uname);
3039}
Marian Balakowiczc8779642008-03-12 10:12:37 +01003040
3041/**
3042 * fit_check_ramdisk - verify FIT format ramdisk subimage
3043 * @fit_hdr: pointer to the FIT ramdisk header
3044 * @rd_noffset: ramdisk subimage node offset within FIT image
3045 * @arch: requested ramdisk image architecture type
3046 * @verify: data CRC verification flag
3047 *
3048 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3049 * specified FIT image.
3050 *
3051 * returns:
3052 * 1, on success
3053 * 0, on failure
3054 */
3055#ifndef USE_HOSTCC
3056static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3057{
3058 fit_image_print (fit, rd_noffset, " ");
3059
3060 if (verify) {
3061 puts (" Verifying Hash Integrity ... ");
3062 if (!fit_image_check_hashes (fit, rd_noffset)) {
3063 puts ("Bad Data Hash\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003064 show_boot_progress (-125);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003065 return 0;
3066 }
3067 puts ("OK\n");
3068 }
3069
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003070 show_boot_progress (126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003071 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3072 !fit_image_check_arch (fit, rd_noffset, arch) ||
3073 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3074 printf ("No Linux %s Ramdisk Image\n",
3075 genimg_get_arch_name(arch));
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003076 show_boot_progress (-126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003077 return 0;
3078 }
3079
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003080 show_boot_progress (127);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003081 return 1;
3082}
3083#endif /* USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003084#endif /* CONFIG_FIT */