blob: 5eea2a1f551d7cf5f753817ea8a1fb501359f26f [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
Mike Frysinger7edb1862010-10-20 07:17:39 -040077static const table_entry_t uimage_arch[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +010078 { 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
Mike Frysinger7edb1862010-10-20 07:17:39 -040099static const table_entry_t uimage_os[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100100 { 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", },
Torkel Lundgren3df61952010-09-28 11:05:36 +0200106 { IH_OS_OSE, "ose", "Enea OSE", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100107 { IH_OS_RTEMS, "rtems", "RTEMS", },
108 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
109#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
110 { IH_OS_QNX, "qnx", "QNX", },
111 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
112#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500113#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
114 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
115#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100116#ifdef USE_HOSTCC
117 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
118 { IH_OS_DELL, "dell", "Dell", },
119 { IH_OS_ESIX, "esix", "Esix", },
120 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
121 { IH_OS_IRIX, "irix", "Irix", },
122 { IH_OS_NCR, "ncr", "NCR", },
123 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
124 { IH_OS_PSOS, "psos", "pSOS", },
125 { IH_OS_SCO, "sco", "SCO", },
126 { IH_OS_SOLARIS, "solaris", "Solaris", },
127 { IH_OS_SVR4, "svr4", "SVR4", },
128#endif
129 { -1, "", "", },
130};
131
Mike Frysinger7edb1862010-10-20 07:17:39 -0400132static const table_entry_t uimage_type[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100133 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
134 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby3decb142011-07-21 09:10:30 -0400135 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
136 { IH_TYPE_INVALID, NULL, "Invalid Image", },
137 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100138 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
John Rigby3decb142011-07-21 09:10:30 -0400139 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100140 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
John Rigby3decb142011-07-21 09:10:30 -0400141 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100142 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
143 { IH_TYPE_SCRIPT, "script", "Script", },
144 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
145 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530146 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
Stefano Babic8edcde52010-01-20 18:19:10 +0100147 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000148 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100149 { -1, "", "", },
150};
151
Mike Frysinger7edb1862010-10-20 07:17:39 -0400152static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100153 { IH_COMP_NONE, "none", "uncompressed", },
154 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
155 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200156 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100157 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100158 { -1, "", "", },
159};
160
Mike Frysinger89cdab72008-03-31 11:02:01 -0400161uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200162uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100163#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
164static void genimg_print_time (time_t timestamp);
165#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100166
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100167/*****************************************************************************/
168/* Legacy format routines */
169/*****************************************************************************/
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200170int image_check_hcrc (const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100171{
172 ulong hcrc;
173 ulong len = image_get_header_size ();
174 image_header_t header;
175
176 /* Copy header so we can blank CRC field for re-calculation */
177 memmove (&header, (char *)hdr, image_get_header_size ());
178 image_set_hcrc (&header, 0);
179
180 hcrc = crc32 (0, (unsigned char *)&header, len);
181
182 return (hcrc == image_get_hcrc (hdr));
183}
184
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200185int image_check_dcrc (const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100186{
187 ulong data = image_get_data (hdr);
188 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200189 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100190
191 return (dcrc == image_get_dcrc (hdr));
192}
193
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100194/**
195 * image_multi_count - get component (sub-image) count
196 * @hdr: pointer to the header of the multi component image
197 *
198 * image_multi_count() returns number of components in a multi
199 * component image.
200 *
201 * Note: no checking of the image type is done, caller must pass
202 * a valid multi component image.
203 *
204 * returns:
205 * number of components
206 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200207ulong image_multi_count (const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100208{
209 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100210 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100211
212 /* get start of the image payload, which in case of multi
213 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100214 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100215
216 /* count non empty slots */
217 for (i = 0; size[i]; ++i)
218 count++;
219
220 return count;
221}
222
223/**
224 * image_multi_getimg - get component data address and size
225 * @hdr: pointer to the header of the multi component image
226 * @idx: index of the requested component
227 * @data: pointer to a ulong variable, will hold component data address
228 * @len: pointer to a ulong variable, will hold component size
229 *
230 * image_multi_getimg() returns size and data address for the requested
231 * component in a multi component image.
232 *
233 * Note: no checking of the image type is done, caller must pass
234 * a valid multi component image.
235 *
236 * returns:
237 * data address and size of the component, if idx is valid
238 * 0 in data and len, if idx is out of range
239 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200240void image_multi_getimg (const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100241 ulong *data, ulong *len)
242{
243 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100244 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700245 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100246
247 /* get number of component */
248 count = image_multi_count (hdr);
249
250 /* get start of the image payload, which in case of multi
251 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100252 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100253
254 /* get address of the proper component data start, which means
255 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100256 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100257
258 if (idx < count) {
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100259 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100260 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100261
262 /* go over all indices preceding requested component idx */
263 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700264 /* add up i-th component size, rounding up to 4 bytes */
265 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100266 }
267
268 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700269 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100270 } else {
271 *len = 0;
272 *data = 0;
273 }
274}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100275
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200276static void image_print_type (const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100277{
278 const char *os, *arch, *type, *comp;
279
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100280 os = genimg_get_os_name (image_get_os (hdr));
281 arch = genimg_get_arch_name (image_get_arch (hdr));
282 type = genimg_get_type_name (image_get_type (hdr));
283 comp = genimg_get_comp_name (image_get_comp (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100284
Marian Balakowicz570abb02008-02-29 15:59:59 +0100285 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100286}
287
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100288/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200289 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200290 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100291 * @p: pointer to prefix string
292 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200293 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100294 * The routine prints out all header fields followed by the size/offset data
295 * for MULTI/SCRIPT images.
296 *
297 * returns:
298 * no returned results
299 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200300void image_print_contents (const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100301{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200302 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200303 const char *p;
304
305#ifdef USE_HOSTCC
306 p = "";
307#else
308 p = " ";
309#endif
310
Marian Balakowicz570abb02008-02-29 15:59:59 +0100311 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
312#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
313 printf ("%sCreated: ", p);
314 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100315#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100316 printf ("%sImage Type: ", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100317 image_print_type (hdr);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100318 printf ("%sData Size: ", p);
319 genimg_print_size (image_get_data_size (hdr));
320 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
321 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100322
Marian Balakowicz570abb02008-02-29 15:59:59 +0100323 if (image_check_type (hdr, IH_TYPE_MULTI) ||
324 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100325 int i;
326 ulong data, len;
327 ulong count = image_multi_count (hdr);
328
Marian Balakowicz570abb02008-02-29 15:59:59 +0100329 printf ("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100330 for (i = 0; i < count; i++) {
331 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100332
333 printf ("%s Image %d: ", p, i);
334 genimg_print_size (len);
335
336 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
337 /*
338 * the user may need to know offsets
339 * if planning to do something with
340 * multiple files
341 */
342 printf ("%s Offset = 0x%08lx\n", p, data);
343 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100344 }
345 }
346}
347
Marian Balakowicz570abb02008-02-29 15:59:59 +0100348
349#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100350/**
351 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100352 * @rd_addr: ramdisk image start address
353 * @arch: expected ramdisk architecture
354 * @verify: checksum verification flag
355 *
356 * image_get_ramdisk() returns a pointer to the verified ramdisk image
357 * header. Routine receives image start address and expected architecture
358 * flag. Verification done covers data and header integrity and os/type/arch
359 * fields checking.
360 *
361 * If dataflash support is enabled routine checks for dataflash addresses
362 * and handles required dataflash reads.
363 *
364 * returns:
365 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600366 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100367 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200368static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100369 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100370{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200371 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100372
373 if (!image_check_magic (rd_hdr)) {
374 puts ("Bad Magic Number\n");
375 show_boot_progress (-10);
Kumar Gala274cea22008-02-27 21:51:46 -0600376 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100377 }
378
379 if (!image_check_hcrc (rd_hdr)) {
380 puts ("Bad Header Checksum\n");
381 show_boot_progress (-11);
Kumar Gala274cea22008-02-27 21:51:46 -0600382 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100383 }
384
385 show_boot_progress (10);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100386 image_print_contents (rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100387
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100388 if (verify) {
389 puts(" Verifying Checksum ... ");
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200390 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100391 puts ("Bad Data CRC\n");
392 show_boot_progress (-12);
Kumar Gala274cea22008-02-27 21:51:46 -0600393 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100394 }
395 puts("OK\n");
396 }
397
398 show_boot_progress (11);
399
400 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
401 !image_check_arch (rd_hdr, arch) ||
402 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
403 printf ("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100404 genimg_get_arch_name(arch));
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100405 show_boot_progress (-13);
Kumar Gala274cea22008-02-27 21:51:46 -0600406 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100407 }
408
409 return rd_hdr;
410}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100411#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100412
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100413/*****************************************************************************/
414/* Shared dual-format routines */
415/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100416#ifndef USE_HOSTCC
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200417int getenv_yesno (char *var)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100418{
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200419 char *s = getenv (var);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100420 return (s && (*s == 'n')) ? 0 : 1;
421}
422
423ulong getenv_bootm_low(void)
424{
425 char *s = getenv ("bootm_low");
426 if (s) {
427 ulong tmp = simple_strtoul (s, NULL, 16);
428 return tmp;
429 }
430
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200431#if defined(CONFIG_SYS_SDRAM_BASE)
432 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100433#elif defined(CONFIG_ARM)
434 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100435#else
436 return 0;
437#endif
438}
439
Becky Bruce391fd932008-06-09 20:37:18 -0500440phys_size_t getenv_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100441{
Matthew McClintockc519fac2010-07-08 10:11:08 -0500442 phys_size_t tmp;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100443 char *s = getenv ("bootm_size");
444 if (s) {
Becky Bruce391fd932008-06-09 20:37:18 -0500445 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100446 return tmp;
447 }
Matthew McClintockc519fac2010-07-08 10:11:08 -0500448 s = getenv("bootm_low");
449 if (s)
450 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
451 else
452 tmp = 0;
453
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100454
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100455#if defined(CONFIG_ARM)
Matthew McClintockc519fac2010-07-08 10:11:08 -0500456 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100457#else
Matthew McClintockc519fac2010-07-08 10:11:08 -0500458 return gd->bd->bi_memsize - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100459#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100460}
461
Grant Likelyc3624e62011-03-28 09:58:43 +0000462phys_size_t getenv_bootm_mapsize(void)
463{
464 phys_size_t tmp;
465 char *s = getenv ("bootm_mapsize");
466 if (s) {
467 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
468 return tmp;
469 }
470
471#if defined(CONFIG_SYS_BOOTMAPSZ)
472 return CONFIG_SYS_BOOTMAPSZ;
473#else
474 return getenv_bootm_size();
475#endif
476}
477
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100478void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
479{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400480 if (to == from)
481 return;
482
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100483#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
484 while (len > 0) {
485 size_t tail = (len > chunksz) ? chunksz : len;
486 WATCHDOG_RESET ();
487 memmove (to, from, tail);
488 to += tail;
489 from += tail;
490 len -= tail;
491 }
492#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
493 memmove (to, from, len);
494#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
495}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100496#endif /* !USE_HOSTCC */
497
Prafulla Wadaskarf666dea2009-09-07 14:59:07 +0530498void genimg_print_size (uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100499{
500#ifndef USE_HOSTCC
501 printf ("%d Bytes = ", size);
502 print_size (size, "\n");
503#else
504 printf ("%d Bytes = %.2f kB = %.2f MB\n",
505 size, (double)size / 1.024e3,
506 (double)size / 1.048576e6);
507#endif
508}
509
510#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
511static void genimg_print_time (time_t timestamp)
512{
513#ifndef USE_HOSTCC
514 struct rtc_time tm;
515
516 to_tm (timestamp, &tm);
517 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
518 tm.tm_year, tm.tm_mon, tm.tm_mday,
519 tm.tm_hour, tm.tm_min, tm.tm_sec);
520#else
521 printf ("%s", ctime(&timestamp));
522#endif
523}
524#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
525
526/**
527 * get_table_entry_name - translate entry id to long name
528 * @table: pointer to a translation table for entries of a specific type
529 * @msg: message to be returned when translation fails
530 * @id: entry id to be translated
531 *
532 * get_table_entry_name() will go over translation table trying to find
533 * entry that matches given id. If matching entry is found, its long
534 * name is returned to the caller.
535 *
536 * returns:
537 * long entry name if translation succeeds
538 * msg otherwise
539 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400540char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100541{
542 for (; table->id >= 0; ++table) {
543 if (table->id == id)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200544#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Woode3d1ac72009-04-02 16:15:10 -0500545 return table->lname;
546#else
547 return table->lname + gd->reloc_off;
548#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100549 }
550 return (msg);
551}
552
553const char *genimg_get_os_name (uint8_t os)
554{
555 return (get_table_entry_name (uimage_os, "Unknown OS", os));
556}
557
558const char *genimg_get_arch_name (uint8_t arch)
559{
560 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
561}
562
563const char *genimg_get_type_name (uint8_t type)
564{
565 return (get_table_entry_name (uimage_type, "Unknown Image", type));
566}
567
568const char *genimg_get_comp_name (uint8_t comp)
569{
570 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
571}
572
573/**
574 * get_table_entry_id - translate short entry name to id
575 * @table: pointer to a translation table for entries of a specific type
576 * @table_name: to be used in case of error
577 * @name: entry short name to be translated
578 *
579 * get_table_entry_id() will go over translation table trying to find
580 * entry that matches given short name. If matching entry is found,
581 * its id returned to the caller.
582 *
583 * returns:
584 * entry id if translation succeeds
585 * -1 otherwise
586 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400587int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100588 const char *table_name, const char *name)
589{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400590 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100591#ifdef USE_HOSTCC
592 int first = 1;
593
594 for (t = table; t->id >= 0; ++t) {
595 if (t->sname && strcasecmp(t->sname, name) == 0)
596 return (t->id);
597 }
598
599 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
600 for (t = table; t->id >= 0; ++t) {
601 if (t->sname == NULL)
602 continue;
603 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
604 first = 0;
605 }
606 fprintf (stderr, "\n");
607#else
608 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200609#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Woode3d1ac72009-04-02 16:15:10 -0500610 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200611#else
612 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500613#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100614 return (t->id);
615 }
616 debug ("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100617#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100618 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100619}
620
Marian Balakowicz570abb02008-02-29 15:59:59 +0100621int genimg_get_os_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100622{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100623 return (get_table_entry_id (uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100624}
625
Marian Balakowicz570abb02008-02-29 15:59:59 +0100626int genimg_get_arch_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100627{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100628 return (get_table_entry_id (uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100629}
630
Marian Balakowicz570abb02008-02-29 15:59:59 +0100631int genimg_get_type_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100632{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100633 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100634}
635
Marian Balakowicz570abb02008-02-29 15:59:59 +0100636int genimg_get_comp_id (const char *name)
637{
638 return (get_table_entry_id (uimage_comp, "Compression", name));
639}
640
641#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100642/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100643 * genimg_get_format - get image format type
644 * @img_addr: image start address
645 *
646 * genimg_get_format() checks whether provided address points to a valid
647 * legacy or FIT image.
648 *
649 * New uImage format and FDT blob are based on a libfdt. FDT blob
650 * may be passed directly or embedded in a FIT image. In both situations
651 * genimg_get_format() must be able to dectect libfdt header.
652 *
653 * returns:
654 * image format type or IMAGE_FORMAT_INVALID if no image is present
655 */
656int genimg_get_format (void *img_addr)
657{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200658 ulong format = IMAGE_FORMAT_INVALID;
659 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100660#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200661 char *fit_hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100662#endif
663
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200664 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100665 if (image_check_magic(hdr))
666 format = IMAGE_FORMAT_LEGACY;
667#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
668 else {
669 fit_hdr = (char *)img_addr;
670 if (fdt_check_header (fit_hdr) == 0)
671 format = IMAGE_FORMAT_FIT;
672 }
673#endif
674
675 return format;
676}
677
678/**
679 * genimg_get_image - get image from special storage (if necessary)
680 * @img_addr: image start address
681 *
682 * genimg_get_image() checks if provided image start adddress is located
683 * in a dataflash storage. If so, image is moved to a system RAM memory.
684 *
685 * returns:
686 * image start address after possible relocation from special storage
687 */
688ulong genimg_get_image (ulong img_addr)
689{
690 ulong ram_addr = img_addr;
691
692#ifdef CONFIG_HAS_DATAFLASH
693 ulong h_size, d_size;
694
695 if (addr_dataflash (img_addr)){
696 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200697 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100698
699 /* get header size */
700 h_size = image_get_header_size ();
701#if defined(CONFIG_FIT)
702 if (sizeof(struct fdt_header) > h_size)
703 h_size = sizeof(struct fdt_header);
704#endif
705
706 /* read in header */
707 debug (" Reading image header from dataflash address "
708 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
709
710 read_dataflash (img_addr, h_size, (char *)ram_addr);
711
712 /* get data size */
713 switch (genimg_get_format ((void *)ram_addr)) {
714 case IMAGE_FORMAT_LEGACY:
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200715 d_size = image_get_data_size ((const image_header_t *)ram_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100716 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
717 ram_addr, d_size);
718 break;
719#if defined(CONFIG_FIT)
720 case IMAGE_FORMAT_FIT:
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100721 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100722 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
723 ram_addr, d_size);
724 break;
725#endif
726 default:
727 printf (" No valid image found at 0x%08lx\n", img_addr);
728 return ram_addr;
729 }
730
731 /* read in image data */
732 debug (" Reading image remaining data from dataflash address "
733 "%08lx to RAM address %08lx\n", img_addr + h_size,
734 ram_addr + h_size);
735
736 read_dataflash (img_addr + h_size, d_size,
737 (char *)(ram_addr + h_size));
738
739 }
740#endif /* CONFIG_HAS_DATAFLASH */
741
742 return ram_addr;
743}
744
745/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100746 * fit_has_config - check if there is a valid FIT configuration
747 * @images: pointer to the bootm command headers structure
748 *
749 * fit_has_config() checks if there is a FIT configuration in use
750 * (if FTI support is present).
751 *
752 * returns:
753 * 0, no FIT support or no configuration found
754 * 1, configuration found
755 */
756int genimg_has_config (bootm_headers_t *images)
757{
758#if defined(CONFIG_FIT)
759 if (images->fit_uname_cfg)
760 return 1;
761#endif
762 return 0;
763}
764
765/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100766 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100767 * @argc: command argument count
768 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100769 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100770 * @arch: expected ramdisk architecture
771 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
772 * @rd_end: pointer to a ulong variable, will hold ramdisk end
773 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100774 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100775 * Curently supported are the following ramdisk sources:
776 * - multicomponent kernel/ramdisk image,
777 * - commandline provided address of decicated ramdisk image.
778 *
779 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100780 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100781 * rd_start and rd_end are set to ramdisk start/end addresses if
782 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100783 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500784 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100785 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100786 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200787int boot_get_ramdisk (int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100788 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100789{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100790 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100791 ulong rd_data, rd_len;
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200792 const image_header_t *rd_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100793#if defined(CONFIG_FIT)
794 void *fit_hdr;
795 const char *fit_uname_config = NULL;
796 const char *fit_uname_ramdisk = NULL;
797 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100798 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100799 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100800 const void *data;
801 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100802#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100803
Marian Balakowiczc8779642008-03-12 10:12:37 +0100804 *rd_start = 0;
805 *rd_end = 0;
806
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100807 /*
808 * Look for a '-' which indicates to ignore the
809 * ramdisk argument
810 */
811 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
812 debug ("## Skipping init Ramdisk\n");
813 rd_len = rd_data = 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100814 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100815#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100816 if (argc >= 3) {
817 /*
818 * If the init ramdisk comes from the FIT image and
819 * the FIT image address is omitted in the command
820 * line argument, try to use os FIT image address or
821 * default load address.
822 */
823 if (images->fit_uname_os)
824 default_addr = (ulong)images->fit_hdr_os;
825 else
826 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100827
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100828 if (fit_parse_conf (argv[2], default_addr,
829 &rd_addr, &fit_uname_config)) {
830 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
831 fit_uname_config, rd_addr);
832 } else if (fit_parse_subimage (argv[2], default_addr,
833 &rd_addr, &fit_uname_ramdisk)) {
834 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
835 fit_uname_ramdisk, rd_addr);
836 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100837#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100838 {
839 rd_addr = simple_strtoul(argv[2], NULL, 16);
840 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
841 rd_addr);
842 }
843#if defined(CONFIG_FIT)
844 } else {
845 /* use FIT configuration provided in first bootm
846 * command argument
847 */
848 rd_addr = (ulong)images->fit_hdr_os;
849 fit_uname_config = images->fit_uname_cfg;
850 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
851 fit_uname_config, rd_addr);
852
853 /*
854 * Check whether configuration has ramdisk defined,
855 * if not, don't try to use it, quit silently.
856 */
857 fit_hdr = (void *)rd_addr;
858 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
859 if (cfg_noffset < 0) {
860 debug ("* ramdisk: no such config\n");
Michal Simekc78fce62008-07-11 10:43:13 +0200861 return 1;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100862 }
863
864 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
865 if (rd_noffset < 0) {
866 debug ("* ramdisk: no ramdisk in config\n");
Peter Tyser41266c92008-08-05 10:51:57 -0500867 return 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100868 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100869 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100870#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100871
872 /* copy from dataflash if needed */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100873 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100874
875 /*
876 * Check if there is an initrd image at the
877 * address provided in the second bootm argument
878 * check image type, for FIT images get FIT node.
879 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100880 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100881 case IMAGE_FORMAT_LEGACY:
Marian Balakowiczc8779642008-03-12 10:12:37 +0100882 printf ("## Loading init Ramdisk from Legacy "
883 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100884
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100885 show_boot_progress (9);
Marian Balakowiczd985c842008-03-12 10:14:38 +0100886 rd_hdr = image_get_ramdisk (rd_addr, arch,
887 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100888
Marian Balakowiczc8779642008-03-12 10:12:37 +0100889 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600890 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600891
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100892 rd_data = image_get_data (rd_hdr);
893 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100894 rd_load = image_get_load (rd_hdr);
895 break;
896#if defined(CONFIG_FIT)
897 case IMAGE_FORMAT_FIT:
898 fit_hdr = (void *)rd_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100899 printf ("## Loading init Ramdisk from FIT "
900 "Image at %08lx ...\n", rd_addr);
901
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100902 show_boot_progress (120);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100903 if (!fit_check_format (fit_hdr)) {
904 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100905 show_boot_progress (-120);
Michal Simekc78fce62008-07-11 10:43:13 +0200906 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100907 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100908 show_boot_progress (121);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100909
910 if (!fit_uname_ramdisk) {
911 /*
912 * no ramdisk image node unit name, try to get config
913 * node first. If config unit node name is NULL
914 * fit_conf_get_node() will try to find default config node
915 */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100916 show_boot_progress (122);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100917 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
918 if (cfg_noffset < 0) {
919 puts ("Could not find configuration node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100920 show_boot_progress (-122);
Michal Simekc78fce62008-07-11 10:43:13 +0200921 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100922 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100923 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
924 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100925
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100926 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100927 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
928 } else {
929 /* get ramdisk component image node offset */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100930 show_boot_progress (123);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100931 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
932 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100933 if (rd_noffset < 0) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100934 puts ("Could not find subimage node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100935 show_boot_progress (-124);
Michal Simekc78fce62008-07-11 10:43:13 +0200936 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100937 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100938
939 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
940
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100941 show_boot_progress (125);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100942 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
Michal Simekc78fce62008-07-11 10:43:13 +0200943 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100944
945 /* get ramdisk image data address and length */
946 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
947 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100948 show_boot_progress (-127);
Michal Simekc78fce62008-07-11 10:43:13 +0200949 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100950 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100951 show_boot_progress (128);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100952
953 rd_data = (ulong)data;
954 rd_len = size;
955
956 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
957 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100958 show_boot_progress (-129);
Michal Simekc78fce62008-07-11 10:43:13 +0200959 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100960 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100961 show_boot_progress (129);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100962
963 images->fit_hdr_rd = fit_hdr;
964 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100965 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100966 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100967#endif
968 default:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100969 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +0100970 rd_data = rd_len = rd_load = 0;
Kumar Galaea86b9e2008-08-29 19:08:29 -0500971 return 1;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100972 }
973
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100974#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100975 /*
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100976 * We need to copy the ramdisk to SRAM to let Linux boot
977 */
978 if (rd_data) {
979 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
980 rd_data = rd_load;
981 }
982#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
983
984 } else if (images->legacy_hdr_valid &&
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200985 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100986 /*
987 * Now check if we have a legacy mult-component image,
988 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100989 */
990 show_boot_progress (13);
991 printf ("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100992 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100993 (ulong)images->legacy_hdr_os);
994
995 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100996 } else {
997 /*
998 * no initrd image
999 */
1000 show_boot_progress (14);
1001 rd_len = rd_data = 0;
1002 }
1003
1004 if (!rd_data) {
1005 debug ("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001006 } else {
1007 *rd_start = rd_data;
1008 *rd_end = rd_data + rd_len;
1009 }
1010 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1011 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -06001012
1013 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001014}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001015
John Rigbyfca43cc2010-10-13 13:57:35 -06001016#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001017/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001018 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001019 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001020 * @rd_data: ramdisk data start address
1021 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001022 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1023 * start address (after possible relocation)
1024 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1025 * end address (after possible relocation)
1026 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001027 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001028 * variable and if requested ramdisk data is moved to a specified location.
1029 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001030 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1031 * start/end addresses if ramdisk image start and len were provided,
1032 * otherwise set initrd_start and initrd_end set to zeros.
1033 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001034 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001035 * 0 - success
1036 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001037 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001038int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001039 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001040{
1041 char *s;
1042 ulong initrd_high;
1043 int initrd_copy_to_ram = 1;
1044
1045 if ((s = getenv ("initrd_high")) != NULL) {
1046 /* a value of "no" or a similar string will act like 0,
1047 * turning the "load high" feature off. This is intentional.
1048 */
1049 initrd_high = simple_strtoul (s, NULL, 16);
1050 if (initrd_high == ~0)
1051 initrd_copy_to_ram = 0;
1052 } else {
1053 /* not set, no restrictions to load high */
1054 initrd_high = ~0;
1055 }
1056
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001057
1058#ifdef CONFIG_LOGBUFFER
1059 /* Prevent initrd from overwriting logbuffer */
1060 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1061#endif
1062
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001063 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1064 initrd_high, initrd_copy_to_ram);
1065
1066 if (rd_data) {
1067 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1068 debug (" in-place initrd\n");
1069 *initrd_start = rd_data;
1070 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001071 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001072 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001073 if (initrd_high)
Becky Bruce391fd932008-06-09 20:37:18 -05001074 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001075 else
Becky Bruce391fd932008-06-09 20:37:18 -05001076 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001077
Kumar Galae822d7f2008-02-27 21:51:49 -06001078 if (*initrd_start == 0) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001079 puts ("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001080 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001081 }
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001082 show_boot_progress (12);
1083
1084 *initrd_end = *initrd_start + rd_len;
1085 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1086 *initrd_start, *initrd_end);
1087
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001088 memmove_wd ((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001089 (void *)rd_data, rd_len, CHUNKSZ);
1090
1091 puts ("OK\n");
1092 }
1093 } else {
1094 *initrd_start = 0;
1095 *initrd_end = 0;
1096 }
1097 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1098 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001099
Kumar Galae822d7f2008-02-27 21:51:49 -06001100 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001101
Kumar Galae822d7f2008-02-27 21:51:49 -06001102error:
1103 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001104}
John Rigbyfca43cc2010-10-13 13:57:35 -06001105#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001106
Kumar Gala06a09912008-08-15 08:24:38 -05001107#ifdef CONFIG_OF_LIBFDT
1108static void fdt_error (const char *msg)
1109{
1110 puts ("ERROR: ");
1111 puts (msg);
1112 puts (" - must RESET the board to recover.\n");
1113}
1114
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001115static const image_header_t *image_get_fdt (ulong fdt_addr)
Kumar Gala06a09912008-08-15 08:24:38 -05001116{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001117 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001118
1119 image_print_contents (fdt_hdr);
1120
1121 puts (" Verifying Checksum ... ");
1122 if (!image_check_hcrc (fdt_hdr)) {
1123 fdt_error ("fdt header checksum invalid");
1124 return NULL;
1125 }
1126
1127 if (!image_check_dcrc (fdt_hdr)) {
1128 fdt_error ("fdt checksum invalid");
1129 return NULL;
1130 }
1131 puts ("OK\n");
1132
1133 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1134 fdt_error ("uImage is not a fdt");
1135 return NULL;
1136 }
1137 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1138 fdt_error ("uImage is compressed");
1139 return NULL;
1140 }
1141 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1142 fdt_error ("uImage data is not a fdt");
1143 return NULL;
1144 }
1145 return fdt_hdr;
1146}
1147
1148/**
1149 * fit_check_fdt - verify FIT format FDT subimage
1150 * @fit_hdr: pointer to the FIT header
1151 * fdt_noffset: FDT subimage node offset within FIT image
1152 * @verify: data CRC verification flag
1153 *
1154 * fit_check_fdt() verifies integrity of the FDT subimage and from
1155 * specified FIT image.
1156 *
1157 * returns:
1158 * 1, on success
1159 * 0, on failure
1160 */
1161#if defined(CONFIG_FIT)
1162static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1163{
1164 fit_image_print (fit, fdt_noffset, " ");
1165
1166 if (verify) {
1167 puts (" Verifying Hash Integrity ... ");
1168 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1169 fdt_error ("Bad Data Hash");
1170 return 0;
1171 }
1172 puts ("OK\n");
1173 }
1174
1175 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1176 fdt_error ("Not a FDT image");
1177 return 0;
1178 }
1179
1180 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1181 fdt_error ("FDT image is compressed");
1182 return 0;
1183 }
1184
1185 return 1;
1186}
1187#endif /* CONFIG_FIT */
1188
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001189#ifndef CONFIG_SYS_FDT_PAD
1190#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Gala06a09912008-08-15 08:24:38 -05001191#endif
1192
Grant Likely55b0a392011-03-28 09:59:01 +00001193#if defined(CONFIG_OF_LIBFDT)
1194/**
1195 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1196 * @lmb: pointer to lmb handle, will be used for memory mgmt
1197 * @fdt_blob: pointer to fdt blob base address
1198 *
1199 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1200 * memreserve regions prevents u-boot from using them to store the initrd
1201 * or the fdt blob.
1202 */
1203void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1204{
1205 uint64_t addr, size;
1206 int i, total;
1207
1208 if (fdt_check_header (fdt_blob) != 0)
1209 return;
1210
1211 total = fdt_num_mem_rsv(fdt_blob);
1212 for (i = 0; i < total; i++) {
1213 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1214 continue;
1215 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1216 (unsigned long long)addr, (unsigned long long)size);
1217 lmb_reserve(lmb, addr, size);
1218 }
1219}
1220
Kumar Gala06a09912008-08-15 08:24:38 -05001221/**
1222 * boot_relocate_fdt - relocate flat device tree
1223 * @lmb: pointer to lmb handle, will be used for memory mgmt
Kumar Gala06a09912008-08-15 08:24:38 -05001224 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1225 * @of_size: pointer to a ulong variable, will hold fdt length
1226 *
Timur Tabi43b08af2010-05-24 15:10:25 -05001227 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1228 * relocates the of_flat_tree into that region, even if the fdt is already in
1229 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1230 * bytes.
Kumar Gala06a09912008-08-15 08:24:38 -05001231 *
1232 * of_flat_tree and of_size are set to final (after relocation) values
1233 *
1234 * returns:
1235 * 0 - success
1236 * 1 - failure
1237 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001238int boot_relocate_fdt (struct lmb *lmb, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001239{
Timur Tabi43b08af2010-05-24 15:10:25 -05001240 void *fdt_blob = *of_flat_tree;
1241 void *of_start = 0;
David A. Longa28afca2011-07-09 16:40:19 -04001242 char *fdt_high;
Kumar Gala06a09912008-08-15 08:24:38 -05001243 ulong of_len = 0;
Timur Tabi43b08af2010-05-24 15:10:25 -05001244 int err;
David A. Longa28afca2011-07-09 16:40:19 -04001245 int disable_relocation = 0;
Kumar Gala06a09912008-08-15 08:24:38 -05001246
1247 /* nothing to do */
1248 if (*of_size == 0)
1249 return 0;
1250
1251 if (fdt_check_header (fdt_blob) != 0) {
1252 fdt_error ("image is not a fdt");
1253 goto error;
1254 }
1255
Timur Tabi43b08af2010-05-24 15:10:25 -05001256 /* position on a 4K boundary before the alloc_current */
1257 /* Pad the FDT by a specified amount */
1258 of_len = *of_size + CONFIG_SYS_FDT_PAD;
David A. Longa28afca2011-07-09 16:40:19 -04001259
1260 /* If fdt_high is set use it to select the relocation address */
1261 fdt_high = getenv("fdt_high");
1262 if (fdt_high) {
1263 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1264
1265 if (((ulong) desired_addr) == ~0UL) {
1266 /* All ones means use fdt in place */
1267 desired_addr = fdt_blob;
1268 disable_relocation = 1;
1269 }
1270 if (desired_addr) {
1271 of_start =
1272 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1273 ((ulong)
1274 desired_addr)
1275 + of_len);
1276 if (desired_addr && of_start != desired_addr) {
1277 puts("Failed using fdt_high value for Device Tree");
1278 goto error;
1279 }
1280 } else {
1281 of_start =
Matthew McClintock1bb5e902011-07-18 13:08:05 +00001282 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
David A. Longa28afca2011-07-09 16:40:19 -04001283 }
1284 } else {
1285 of_start =
1286 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1287 getenv_bootm_mapsize()
1288 + getenv_bootm_low());
1289 }
Kumar Gala06a09912008-08-15 08:24:38 -05001290
Timur Tabi43b08af2010-05-24 15:10:25 -05001291 if (of_start == 0) {
1292 puts("device tree - allocation error\n");
1293 goto error;
Kumar Gala06a09912008-08-15 08:24:38 -05001294 }
1295
David A. Longa28afca2011-07-09 16:40:19 -04001296 if (disable_relocation) {
1297 /* We assume there is space after the existing fdt to use for padding */
1298 fdt_set_totalsize(of_start, of_len);
1299 printf(" Using Device Tree in place at %p, end %p\n",
1300 of_start, of_start + of_len - 1);
1301 } else {
1302 debug ("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1303 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
Timur Tabi43b08af2010-05-24 15:10:25 -05001304
David A. Longa28afca2011-07-09 16:40:19 -04001305 printf (" Loading Device Tree to %p, end %p ... ",
1306 of_start, of_start + of_len - 1);
Timur Tabi43b08af2010-05-24 15:10:25 -05001307
David A. Longa28afca2011-07-09 16:40:19 -04001308 err = fdt_open_into (fdt_blob, of_start, of_len);
1309 if (err != 0) {
1310 fdt_error ("fdt move failed");
1311 goto error;
1312 }
1313 puts ("OK\n");
Timur Tabi43b08af2010-05-24 15:10:25 -05001314 }
Timur Tabi43b08af2010-05-24 15:10:25 -05001315
1316 *of_flat_tree = of_start;
1317 *of_size = of_len;
1318
Kumar Gala54f9c862008-08-15 08:24:39 -05001319 set_working_fdt_addr(*of_flat_tree);
Kumar Gala06a09912008-08-15 08:24:38 -05001320 return 0;
1321
1322error:
1323 return 1;
1324}
Grant Likelyed59e582011-03-28 09:58:49 +00001325#endif /* CONFIG_OF_LIBFDT */
Kumar Gala06a09912008-08-15 08:24:38 -05001326
1327/**
1328 * boot_get_fdt - main fdt handling routine
1329 * @argc: command argument count
1330 * @argv: command argument list
1331 * @images: pointer to the bootm images structure
1332 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1333 * @of_size: pointer to a ulong variable, will hold fdt length
1334 *
1335 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1336 * Curently supported are the following ramdisk sources:
1337 * - multicomponent kernel/ramdisk image,
1338 * - commandline provided address of decicated ramdisk image.
1339 *
1340 * returns:
1341 * 0, if fdt image was found and valid, or skipped
1342 * of_flat_tree and of_size are set to fdt start address and length if
1343 * fdt image is found and valid
1344 *
1345 * 1, if fdt image is found but corrupted
1346 * of_flat_tree and of_size are set to 0 if no fdt exists
1347 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001348int boot_get_fdt (int flag, int argc, char * const argv[], bootm_headers_t *images,
Kumar Gala06a09912008-08-15 08:24:38 -05001349 char **of_flat_tree, ulong *of_size)
1350{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001351 const image_header_t *fdt_hdr;
Kumar Gala06a09912008-08-15 08:24:38 -05001352 ulong fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001353 char *fdt_blob = NULL;
1354 ulong image_start, image_end;
1355 ulong load_start, load_end;
1356#if defined(CONFIG_FIT)
1357 void *fit_hdr;
1358 const char *fit_uname_config = NULL;
1359 const char *fit_uname_fdt = NULL;
1360 ulong default_addr;
1361 int cfg_noffset;
1362 int fdt_noffset;
1363 const void *data;
1364 size_t size;
1365#endif
1366
1367 *of_flat_tree = NULL;
1368 *of_size = 0;
1369
1370 if (argc > 3 || genimg_has_config (images)) {
1371#if defined(CONFIG_FIT)
1372 if (argc > 3) {
1373 /*
1374 * If the FDT blob comes from the FIT image and the
1375 * FIT image address is omitted in the command line
1376 * argument, try to use ramdisk or os FIT image
1377 * address or default load address.
1378 */
1379 if (images->fit_uname_rd)
1380 default_addr = (ulong)images->fit_hdr_rd;
1381 else if (images->fit_uname_os)
1382 default_addr = (ulong)images->fit_hdr_os;
1383 else
1384 default_addr = load_addr;
1385
1386 if (fit_parse_conf (argv[3], default_addr,
1387 &fdt_addr, &fit_uname_config)) {
1388 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1389 fit_uname_config, fdt_addr);
1390 } else if (fit_parse_subimage (argv[3], default_addr,
1391 &fdt_addr, &fit_uname_fdt)) {
1392 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1393 fit_uname_fdt, fdt_addr);
1394 } else
1395#endif
1396 {
1397 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1398 debug ("* fdt: cmdline image address = 0x%08lx\n",
1399 fdt_addr);
1400 }
1401#if defined(CONFIG_FIT)
1402 } else {
1403 /* use FIT configuration provided in first bootm
1404 * command argument
1405 */
1406 fdt_addr = (ulong)images->fit_hdr_os;
1407 fit_uname_config = images->fit_uname_cfg;
1408 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1409 fit_uname_config, fdt_addr);
1410
1411 /*
1412 * Check whether configuration has FDT blob defined,
1413 * if not quit silently.
1414 */
1415 fit_hdr = (void *)fdt_addr;
1416 cfg_noffset = fit_conf_get_node (fit_hdr,
1417 fit_uname_config);
1418 if (cfg_noffset < 0) {
1419 debug ("* fdt: no such config\n");
1420 return 0;
1421 }
1422
1423 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1424 cfg_noffset);
1425 if (fdt_noffset < 0) {
1426 debug ("* fdt: no fdt in config\n");
1427 return 0;
1428 }
1429 }
1430#endif
1431
1432 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1433 fdt_addr);
1434
1435 /* copy from dataflash if needed */
1436 fdt_addr = genimg_get_image (fdt_addr);
1437
1438 /*
1439 * Check if there is an FDT image at the
1440 * address provided in the second bootm argument
1441 * check image type, for FIT images get a FIT node.
1442 */
1443 switch (genimg_get_format ((void *)fdt_addr)) {
1444 case IMAGE_FORMAT_LEGACY:
1445 /* verify fdt_addr points to a valid image header */
1446 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1447 fdt_addr);
1448 fdt_hdr = image_get_fdt (fdt_addr);
1449 if (!fdt_hdr)
1450 goto error;
1451
1452 /*
1453 * move image data to the load address,
1454 * make sure we don't overwrite initial image
1455 */
1456 image_start = (ulong)fdt_hdr;
1457 image_end = image_get_image_end (fdt_hdr);
1458
1459 load_start = image_get_load (fdt_hdr);
1460 load_end = load_start + image_get_data_size (fdt_hdr);
1461
1462 if ((load_start < image_end) && (load_end > image_start)) {
1463 fdt_error ("fdt overwritten");
1464 goto error;
1465 }
1466
1467 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1468 image_get_data (fdt_hdr), load_start);
1469
1470 memmove ((void *)load_start,
1471 (void *)image_get_data (fdt_hdr),
1472 image_get_data_size (fdt_hdr));
1473
1474 fdt_blob = (char *)load_start;
1475 break;
1476 case IMAGE_FORMAT_FIT:
1477 /*
1478 * This case will catch both: new uImage format
1479 * (libfdt based) and raw FDT blob (also libfdt
1480 * based).
1481 */
1482#if defined(CONFIG_FIT)
1483 /* check FDT blob vs FIT blob */
1484 if (fit_check_format ((const void *)fdt_addr)) {
1485 /*
1486 * FIT image
1487 */
1488 fit_hdr = (void *)fdt_addr;
1489 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1490 fdt_addr);
1491
1492 if (!fit_uname_fdt) {
1493 /*
1494 * no FDT blob image node unit name,
1495 * try to get config node first. If
1496 * config unit node name is NULL
1497 * fit_conf_get_node() will try to
1498 * find default config node
1499 */
1500 cfg_noffset = fit_conf_get_node (fit_hdr,
1501 fit_uname_config);
1502
1503 if (cfg_noffset < 0) {
1504 fdt_error ("Could not find configuration node\n");
1505 goto error;
1506 }
1507
1508 fit_uname_config = fdt_get_name (fit_hdr,
1509 cfg_noffset, NULL);
1510 printf (" Using '%s' configuration\n",
1511 fit_uname_config);
1512
1513 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1514 cfg_noffset);
1515 fit_uname_fdt = fit_get_name (fit_hdr,
1516 fdt_noffset, NULL);
1517 } else {
1518 /* get FDT component image node offset */
1519 fdt_noffset = fit_image_get_node (fit_hdr,
1520 fit_uname_fdt);
1521 }
1522 if (fdt_noffset < 0) {
1523 fdt_error ("Could not find subimage node\n");
1524 goto error;
1525 }
1526
1527 printf (" Trying '%s' FDT blob subimage\n",
1528 fit_uname_fdt);
1529
1530 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1531 images->verify))
1532 goto error;
1533
1534 /* get ramdisk image data address and length */
1535 if (fit_image_get_data (fit_hdr, fdt_noffset,
1536 &data, &size)) {
1537 fdt_error ("Could not find FDT subimage data");
1538 goto error;
1539 }
1540
1541 /* verift that image data is a proper FDT blob */
1542 if (fdt_check_header ((char *)data) != 0) {
1543 fdt_error ("Subimage data is not a FTD");
1544 goto error;
1545 }
1546
1547 /*
1548 * move image data to the load address,
1549 * make sure we don't overwrite initial image
1550 */
1551 image_start = (ulong)fit_hdr;
1552 image_end = fit_get_end (fit_hdr);
1553
1554 if (fit_image_get_load (fit_hdr, fdt_noffset,
1555 &load_start) == 0) {
1556 load_end = load_start + size;
1557
1558 if ((load_start < image_end) &&
1559 (load_end > image_start)) {
1560 fdt_error ("FDT overwritten");
1561 goto error;
1562 }
1563
1564 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1565 (ulong)data, load_start);
1566
1567 memmove ((void *)load_start,
1568 (void *)data, size);
1569
1570 fdt_blob = (char *)load_start;
1571 } else {
1572 fdt_blob = (char *)data;
1573 }
1574
1575 images->fit_hdr_fdt = fit_hdr;
1576 images->fit_uname_fdt = fit_uname_fdt;
1577 images->fit_noffset_fdt = fdt_noffset;
1578 break;
1579 } else
1580#endif
1581 {
1582 /*
1583 * FDT blob
1584 */
1585 fdt_blob = (char *)fdt_addr;
1586 debug ("* fdt: raw FDT blob\n");
1587 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1588 }
1589 break;
1590 default:
Anatolij Gustschin8e024942008-08-29 21:04:45 +02001591 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001592 goto error;
1593 }
1594
1595 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1596
1597 } else if (images->legacy_hdr_valid &&
1598 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1599
1600 ulong fdt_data, fdt_len;
1601
1602 /*
1603 * Now check if we have a legacy multi-component image,
1604 * get second entry data start address and len.
1605 */
1606 printf ("## Flattened Device Tree from multi "
1607 "component Image at %08lX\n",
1608 (ulong)images->legacy_hdr_os);
1609
1610 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1611 if (fdt_len) {
1612
1613 fdt_blob = (char *)fdt_data;
1614 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1615
1616 if (fdt_check_header (fdt_blob) != 0) {
1617 fdt_error ("image is not a fdt");
1618 goto error;
1619 }
1620
John Rigbyd1263fc2010-10-13 13:57:32 -06001621 if (fdt_totalsize(fdt_blob) != fdt_len) {
Kumar Gala06a09912008-08-15 08:24:38 -05001622 fdt_error ("fdt size != image size");
1623 goto error;
1624 }
1625 } else {
1626 debug ("## No Flattened Device Tree\n");
1627 return 0;
1628 }
1629 } else {
1630 debug ("## No Flattened Device Tree\n");
1631 return 0;
1632 }
1633
1634 *of_flat_tree = fdt_blob;
John Rigbyd1263fc2010-10-13 13:57:32 -06001635 *of_size = fdt_totalsize(fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001636 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner52514692008-08-21 07:12:26 -07001637 (ulong)*of_flat_tree, *of_size);
Kumar Gala06a09912008-08-15 08:24:38 -05001638
1639 return 0;
1640
1641error:
1642 *of_flat_tree = 0;
1643 *of_size = 0;
1644 return 1;
1645}
1646#endif /* CONFIG_OF_LIBFDT */
1647
John Rigbyfca43cc2010-10-13 13:57:35 -06001648#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001649/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001650 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001651 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001652 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1653 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1654 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001655 * boot_get_cmdline() allocates space for kernel command line below
Grant Likely590d3ca2011-03-28 09:58:34 +00001656 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001657 * variable is present its contents is copied to allocated kernel
1658 * command line.
1659 *
1660 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001661 * 0 - success
1662 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001663 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001664int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001665{
1666 char *cmdline;
1667 char *s;
1668
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001669 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001670 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001671
1672 if (cmdline == NULL)
1673 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001674
1675 if ((s = getenv("bootargs")) == NULL)
1676 s = "";
1677
1678 strcpy(cmdline, s);
1679
1680 *cmd_start = (ulong) & cmdline[0];
1681 *cmd_end = *cmd_start + strlen(cmdline);
1682
1683 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1684
Kumar Galae822d7f2008-02-27 21:51:49 -06001685 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001686}
John Rigbyfca43cc2010-10-13 13:57:35 -06001687#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001688
John Rigbyfca43cc2010-10-13 13:57:35 -06001689#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001690/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001691 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001692 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001693 * @kbd: double pointer to board info data
1694 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001695 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likely590d3ca2011-03-28 09:58:34 +00001696 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1697 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001698 *
1699 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001700 * 0 - success
1701 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001702 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001703int boot_get_kbd (struct lmb *lmb, bd_t **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001704{
Becky Bruce391fd932008-06-09 20:37:18 -05001705 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001706 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001707 if (*kbd == NULL)
1708 return -1;
1709
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001710 **kbd = *(gd->bd);
1711
1712 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1713
1714#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1715 do_bdinfo(NULL, 0, 0, NULL);
1716#endif
1717
Kumar Galae822d7f2008-02-27 21:51:49 -06001718 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001719}
John Rigbyfca43cc2010-10-13 13:57:35 -06001720#endif /* CONFIG_SYS_BOOT_GET_KBD */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001721#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001722
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001723#if defined(CONFIG_FIT)
1724/*****************************************************************************/
1725/* New uImage format routines */
1726/*****************************************************************************/
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001727#ifndef USE_HOSTCC
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001728static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1729 ulong *addr, const char **name)
1730{
1731 const char *sep;
1732
1733 *addr = addr_curr;
1734 *name = NULL;
1735
1736 sep = strchr (spec, sepc);
1737 if (sep) {
1738 if (sep - spec > 0)
1739 *addr = simple_strtoul (spec, NULL, 16);
1740
1741 *name = sep + 1;
1742 return 1;
1743 }
1744
1745 return 0;
1746}
1747
1748/**
1749 * fit_parse_conf - parse FIT configuration spec
1750 * @spec: input string, containing configuration spec
1751 * @add_curr: current image address (to be used as a possible default)
1752 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1753 * configuration
1754 * @conf_name double pointer to a char, will hold pointer to a configuration
1755 * unit name
1756 *
1757 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1758 * where <addr> is a FIT image address that contains configuration
1759 * with a <conf> unit name.
1760 *
1761 * Address part is optional, and if omitted default add_curr will
1762 * be used instead.
1763 *
1764 * returns:
1765 * 1 if spec is a valid configuration string,
1766 * addr and conf_name are set accordingly
1767 * 0 otherwise
1768 */
1769inline int fit_parse_conf (const char *spec, ulong addr_curr,
1770 ulong *addr, const char **conf_name)
1771{
1772 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1773}
1774
1775/**
1776 * fit_parse_subimage - parse FIT subimage spec
1777 * @spec: input string, containing subimage spec
1778 * @add_curr: current image address (to be used as a possible default)
1779 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1780 * subimage
1781 * @image_name: double pointer to a char, will hold pointer to a subimage name
1782 *
1783 * fit_parse_subimage() expects subimage spec in the for of
1784 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1785 * subimage with a <subimg> unit name.
1786 *
1787 * Address part is optional, and if omitted default add_curr will
1788 * be used instead.
1789 *
1790 * returns:
1791 * 1 if spec is a valid subimage string,
1792 * addr and image_name are set accordingly
1793 * 0 otherwise
1794 */
1795inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1796 ulong *addr, const char **image_name)
1797{
1798 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1799}
Marian Balakowicz570abb02008-02-29 15:59:59 +01001800#endif /* !USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001801
1802static void fit_get_debug (const void *fit, int noffset,
1803 char *prop_name, int err)
1804{
1805 debug ("Can't get '%s' property from FIT 0x%08lx, "
1806 "node: offset %d, name %s (%s)\n",
1807 prop_name, (ulong)fit, noffset,
1808 fit_get_name (fit, noffset, NULL),
1809 fdt_strerror (err));
1810}
1811
1812/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001813 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001814 * @fit: pointer to the FIT format image header
1815 * @p: pointer to prefix string
1816 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001817 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001818 * The routine prints out FIT image properties (root node level) follwed by
1819 * the details of each component image.
1820 *
1821 * returns:
1822 * no returned results
1823 */
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001824void fit_print_contents (const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001825{
1826 char *desc;
1827 char *uname;
1828 int images_noffset;
1829 int confs_noffset;
1830 int noffset;
1831 int ndepth;
1832 int count = 0;
1833 int ret;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001834 const char *p;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001835#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1836 time_t timestamp;
1837#endif
1838
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001839#ifdef USE_HOSTCC
1840 p = "";
1841#else
1842 p = " ";
1843#endif
1844
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001845 /* Root node properties */
1846 ret = fit_get_desc (fit, 0, &desc);
1847 printf ("%sFIT description: ", p);
1848 if (ret)
1849 printf ("unavailable\n");
1850 else
1851 printf ("%s\n", desc);
1852
1853#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1854 ret = fit_get_timestamp (fit, 0, &timestamp);
1855 printf ("%sCreated: ", p);
1856 if (ret)
1857 printf ("unavailable\n");
1858 else
1859 genimg_print_time (timestamp);
1860#endif
1861
1862 /* Find images parent node offset */
1863 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1864 if (images_noffset < 0) {
1865 printf ("Can't find images parent node '%s' (%s)\n",
1866 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1867 return;
1868 }
1869
1870 /* Process its subnodes, print out component images details */
1871 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1872 (noffset >= 0) && (ndepth > 0);
1873 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1874 if (ndepth == 1) {
1875 /*
1876 * Direct child node of the images parent node,
1877 * i.e. component image node.
1878 */
1879 printf ("%s Image %u (%s)\n", p, count++,
1880 fit_get_name(fit, noffset, NULL));
1881
1882 fit_image_print (fit, noffset, p);
1883 }
1884 }
1885
1886 /* Find configurations parent node offset */
1887 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1888 if (confs_noffset < 0) {
1889 debug ("Can't get configurations parent node '%s' (%s)\n",
1890 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1891 return;
1892 }
1893
1894 /* get default configuration unit name from default property */
1895 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1896 if (uname)
1897 printf ("%s Default Configuration: '%s'\n", p, uname);
1898
1899 /* Process its subnodes, print out configurations details */
1900 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1901 (noffset >= 0) && (ndepth > 0);
1902 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1903 if (ndepth == 1) {
1904 /*
1905 * Direct child node of the configurations parent node,
1906 * i.e. configuration node.
1907 */
1908 printf ("%s Configuration %u (%s)\n", p, count++,
1909 fit_get_name(fit, noffset, NULL));
1910
1911 fit_conf_print (fit, noffset, p);
1912 }
1913 }
1914}
1915
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001916/**
1917 * fit_image_print - prints out the FIT component image details
1918 * @fit: pointer to the FIT format image header
1919 * @image_noffset: offset of the component image node
1920 * @p: pointer to prefix string
1921 *
1922 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001923 * image. If present, hash nodes are printed out as well. Load
1924 * address for images of type firmware is also printed out. Since the load
1925 * address is not mandatory for firmware images, it will be output as
1926 * "unavailable" when not present.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001927 *
1928 * returns:
1929 * no returned results
1930 */
1931void fit_image_print (const void *fit, int image_noffset, const char *p)
1932{
1933 char *desc;
1934 uint8_t type, arch, os, comp;
1935 size_t size;
1936 ulong load, entry;
1937 const void *data;
1938 int noffset;
1939 int ndepth;
1940 int ret;
1941
1942 /* Mandatory properties */
1943 ret = fit_get_desc (fit, image_noffset, &desc);
1944 printf ("%s Description: ", p);
1945 if (ret)
1946 printf ("unavailable\n");
1947 else
1948 printf ("%s\n", desc);
1949
1950 fit_image_get_type (fit, image_noffset, &type);
1951 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1952
1953 fit_image_get_comp (fit, image_noffset, &comp);
1954 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1955
1956 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1957
1958#ifndef USE_HOSTCC
1959 printf ("%s Data Start: ", p);
1960 if (ret)
1961 printf ("unavailable\n");
1962 else
1963 printf ("0x%08lx\n", (ulong)data);
1964#endif
1965
1966 printf ("%s Data Size: ", p);
1967 if (ret)
1968 printf ("unavailable\n");
1969 else
1970 genimg_print_size (size);
1971
1972 /* Remaining, type dependent properties */
1973 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1974 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1975 (type == IH_TYPE_FLATDT)) {
1976 fit_image_get_arch (fit, image_noffset, &arch);
1977 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1978 }
1979
1980 if (type == IH_TYPE_KERNEL) {
1981 fit_image_get_os (fit, image_noffset, &os);
1982 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1983 }
1984
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001985 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1986 (type == IH_TYPE_FIRMWARE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001987 ret = fit_image_get_load (fit, image_noffset, &load);
1988 printf ("%s Load Address: ", p);
1989 if (ret)
1990 printf ("unavailable\n");
1991 else
1992 printf ("0x%08lx\n", load);
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001993 }
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001994
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001995 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001996 fit_image_get_entry (fit, image_noffset, &entry);
1997 printf ("%s Entry Point: ", p);
1998 if (ret)
1999 printf ("unavailable\n");
2000 else
2001 printf ("0x%08lx\n", entry);
2002 }
2003
2004 /* Process all hash subnodes of the component image node */
2005 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2006 (noffset >= 0) && (ndepth > 0);
2007 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2008 if (ndepth == 1) {
2009 /* Direct child node of the component image node */
2010 fit_image_print_hash (fit, noffset, p);
2011 }
2012 }
2013}
2014
2015/**
2016 * fit_image_print_hash - prints out the hash node details
2017 * @fit: pointer to the FIT format image header
2018 * @noffset: offset of the hash node
2019 * @p: pointer to prefix string
2020 *
2021 * fit_image_print_hash() lists properies for the processed hash node
2022 *
2023 * returns:
2024 * no returned results
2025 */
2026void fit_image_print_hash (const void *fit, int noffset, const char *p)
2027{
2028 char *algo;
2029 uint8_t *value;
2030 int value_len;
2031 int i, ret;
2032
2033 /*
2034 * Check subnode name, must be equal to "hash".
2035 * Multiple hash nodes require unique unit node
2036 * names, e.g. hash@1, hash@2, etc.
2037 */
2038 if (strncmp (fit_get_name(fit, noffset, NULL),
2039 FIT_HASH_NODENAME,
2040 strlen(FIT_HASH_NODENAME)) != 0)
2041 return;
2042
2043 debug ("%s Hash node: '%s'\n", p,
2044 fit_get_name (fit, noffset, NULL));
2045
2046 printf ("%s Hash algo: ", p);
2047 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2048 printf ("invalid/unsupported\n");
2049 return;
2050 }
2051 printf ("%s\n", algo);
2052
2053 ret = fit_image_hash_get_value (fit, noffset, &value,
2054 &value_len);
2055 printf ("%s Hash value: ", p);
2056 if (ret) {
2057 printf ("unavailable\n");
2058 } else {
2059 for (i = 0; i < value_len; i++)
2060 printf ("%02x", value[i]);
2061 printf ("\n");
2062 }
2063
2064 debug ("%s Hash len: %d\n", p, value_len);
2065}
2066
2067/**
2068 * fit_get_desc - get node description property
2069 * @fit: pointer to the FIT format image header
2070 * @noffset: node offset
2071 * @desc: double pointer to the char, will hold pointer to the descrption
2072 *
2073 * fit_get_desc() reads description property from a given node, if
2074 * description is found pointer to it is returened in third call argument.
2075 *
2076 * returns:
2077 * 0, on success
2078 * -1, on failure
2079 */
2080int fit_get_desc (const void *fit, int noffset, char **desc)
2081{
2082 int len;
2083
2084 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2085 if (*desc == NULL) {
2086 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2087 return -1;
2088 }
2089
2090 return 0;
2091}
2092
2093/**
2094 * fit_get_timestamp - get node timestamp property
2095 * @fit: pointer to the FIT format image header
2096 * @noffset: node offset
2097 * @timestamp: pointer to the time_t, will hold read timestamp
2098 *
2099 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2100 * is found and has a correct size its value is retured in third call
2101 * argument.
2102 *
2103 * returns:
2104 * 0, on success
2105 * -1, on property read failure
2106 * -2, on wrong timestamp size
2107 */
2108int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2109{
2110 int len;
2111 const void *data;
2112
2113 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2114 if (data == NULL) {
2115 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2116 return -1;
2117 }
2118 if (len != sizeof (uint32_t)) {
2119 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2120 return -2;
2121 }
2122
2123 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2124 return 0;
2125}
2126
2127/**
2128 * fit_image_get_node - get node offset for component image of a given unit name
2129 * @fit: pointer to the FIT format image header
2130 * @image_uname: component image node unit name
2131 *
2132 * fit_image_get_node() finds a component image (withing the '/images'
2133 * node) of a provided unit name. If image is found its node offset is
2134 * returned to the caller.
2135 *
2136 * returns:
2137 * image node offset when found (>=0)
2138 * negative number on failure (FDT_ERR_* code)
2139 */
2140int fit_image_get_node (const void *fit, const char *image_uname)
2141{
2142 int noffset, images_noffset;
2143
2144 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2145 if (images_noffset < 0) {
2146 debug ("Can't find images parent node '%s' (%s)\n",
2147 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2148 return images_noffset;
2149 }
2150
2151 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2152 if (noffset < 0) {
2153 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2154 image_uname, fdt_strerror (noffset));
2155 }
2156
2157 return noffset;
2158}
2159
2160/**
2161 * fit_image_get_os - get os id for a given component image node
2162 * @fit: pointer to the FIT format image header
2163 * @noffset: component image node offset
2164 * @os: pointer to the uint8_t, will hold os numeric id
2165 *
2166 * fit_image_get_os() finds os property in a given component image node.
2167 * If the property is found, its (string) value is translated to the numeric
2168 * id which is returned to the caller.
2169 *
2170 * returns:
2171 * 0, on success
2172 * -1, on failure
2173 */
2174int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2175{
2176 int len;
2177 const void *data;
2178
2179 /* Get OS name from property data */
2180 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2181 if (data == NULL) {
2182 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2183 *os = -1;
2184 return -1;
2185 }
2186
2187 /* Translate OS name to id */
2188 *os = genimg_get_os_id (data);
2189 return 0;
2190}
2191
2192/**
2193 * fit_image_get_arch - get arch id for a given component image node
2194 * @fit: pointer to the FIT format image header
2195 * @noffset: component image node offset
2196 * @arch: pointer to the uint8_t, will hold arch numeric id
2197 *
2198 * fit_image_get_arch() finds arch property in a given component image node.
2199 * If the property is found, its (string) value is translated to the numeric
2200 * id which is returned to the caller.
2201 *
2202 * returns:
2203 * 0, on success
2204 * -1, on failure
2205 */
2206int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2207{
2208 int len;
2209 const void *data;
2210
2211 /* Get architecture name from property data */
2212 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2213 if (data == NULL) {
2214 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2215 *arch = -1;
2216 return -1;
2217 }
2218
2219 /* Translate architecture name to id */
2220 *arch = genimg_get_arch_id (data);
2221 return 0;
2222}
2223
2224/**
2225 * fit_image_get_type - get type id for a given component image node
2226 * @fit: pointer to the FIT format image header
2227 * @noffset: component image node offset
2228 * @type: pointer to the uint8_t, will hold type numeric id
2229 *
2230 * fit_image_get_type() finds type property in a given component image node.
2231 * If the property is found, its (string) value is translated to the numeric
2232 * id which is returned to the caller.
2233 *
2234 * returns:
2235 * 0, on success
2236 * -1, on failure
2237 */
2238int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2239{
2240 int len;
2241 const void *data;
2242
2243 /* Get image type name from property data */
2244 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2245 if (data == NULL) {
2246 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2247 *type = -1;
2248 return -1;
2249 }
2250
2251 /* Translate image type name to id */
2252 *type = genimg_get_type_id (data);
2253 return 0;
2254}
2255
2256/**
2257 * fit_image_get_comp - get comp id for a given component image node
2258 * @fit: pointer to the FIT format image header
2259 * @noffset: component image node offset
2260 * @comp: pointer to the uint8_t, will hold comp numeric id
2261 *
2262 * fit_image_get_comp() finds comp property in a given component image node.
2263 * If the property is found, its (string) value is translated to the numeric
2264 * id which is returned to the caller.
2265 *
2266 * returns:
2267 * 0, on success
2268 * -1, on failure
2269 */
2270int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2271{
2272 int len;
2273 const void *data;
2274
2275 /* Get compression name from property data */
2276 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2277 if (data == NULL) {
2278 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2279 *comp = -1;
2280 return -1;
2281 }
2282
2283 /* Translate compression name to id */
2284 *comp = genimg_get_comp_id (data);
2285 return 0;
2286}
2287
2288/**
2289 * fit_image_get_load - get load address property for a given component image node
2290 * @fit: pointer to the FIT format image header
2291 * @noffset: component image node offset
2292 * @load: pointer to the uint32_t, will hold load address
2293 *
2294 * fit_image_get_load() finds load address property in a given component image node.
2295 * If the property is found, its value is returned to the caller.
2296 *
2297 * returns:
2298 * 0, on success
2299 * -1, on failure
2300 */
2301int fit_image_get_load (const void *fit, int noffset, ulong *load)
2302{
2303 int len;
2304 const uint32_t *data;
2305
2306 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2307 if (data == NULL) {
2308 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2309 return -1;
2310 }
2311
2312 *load = uimage_to_cpu (*data);
2313 return 0;
2314}
2315
2316/**
2317 * fit_image_get_entry - get entry point address property for a given component image node
2318 * @fit: pointer to the FIT format image header
2319 * @noffset: component image node offset
2320 * @entry: pointer to the uint32_t, will hold entry point address
2321 *
2322 * fit_image_get_entry() finds entry point address property in a given component image node.
2323 * If the property is found, its value is returned to the caller.
2324 *
2325 * returns:
2326 * 0, on success
2327 * -1, on failure
2328 */
2329int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2330{
2331 int len;
2332 const uint32_t *data;
2333
2334 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2335 if (data == NULL) {
2336 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2337 return -1;
2338 }
2339
2340 *entry = uimage_to_cpu (*data);
2341 return 0;
2342}
2343
2344/**
2345 * fit_image_get_data - get data property and its size for a given component image node
2346 * @fit: pointer to the FIT format image header
2347 * @noffset: component image node offset
2348 * @data: double pointer to void, will hold data property's data address
2349 * @size: pointer to size_t, will hold data property's data size
2350 *
2351 * fit_image_get_data() finds data property in a given component image 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_get_data (const void *fit, int noffset,
2360 const void **data, size_t *size)
2361{
2362 int len;
2363
2364 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2365 if (*data == NULL) {
2366 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2367 *size = 0;
2368 return -1;
2369 }
2370
2371 *size = len;
2372 return 0;
2373}
2374
2375/**
2376 * fit_image_hash_get_algo - get hash algorithm name
2377 * @fit: pointer to the FIT format image header
2378 * @noffset: hash node offset
2379 * @algo: double pointer to char, will hold pointer to the algorithm name
2380 *
2381 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2382 * If the property is found its data start address is returned to the caller.
2383 *
2384 * returns:
2385 * 0, on success
2386 * -1, on failure
2387 */
2388int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2389{
2390 int len;
2391
2392 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2393 if (*algo == NULL) {
2394 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2395 return -1;
2396 }
2397
2398 return 0;
2399}
2400
2401/**
2402 * fit_image_hash_get_value - get hash value and length
2403 * @fit: pointer to the FIT format image header
2404 * @noffset: hash node offset
2405 * @value: double pointer to uint8_t, will hold address of a hash value data
2406 * @value_len: pointer to an int, will hold hash data length
2407 *
2408 * fit_image_hash_get_value() finds hash value property in a given hash node.
2409 * If the property is found its data start address and size are returned to
2410 * the caller.
2411 *
2412 * returns:
2413 * 0, on success
2414 * -1, on failure
2415 */
2416int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2417 int *value_len)
2418{
2419 int len;
2420
2421 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2422 if (*value == NULL) {
2423 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2424 *value_len = 0;
2425 return -1;
2426 }
2427
2428 *value_len = len;
2429 return 0;
2430}
2431
2432/**
2433 * fit_set_timestamp - set node timestamp property
2434 * @fit: pointer to the FIT format image header
2435 * @noffset: node offset
2436 * @timestamp: timestamp value to be set
2437 *
2438 * fit_set_timestamp() attempts to set timestamp property in the requested
2439 * node and returns operation status to the caller.
2440 *
2441 * returns:
2442 * 0, on success
2443 * -1, on property read failure
2444 */
2445int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2446{
2447 uint32_t t;
2448 int ret;
2449
2450 t = cpu_to_uimage (timestamp);
2451 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2452 sizeof (uint32_t));
2453 if (ret) {
2454 printf ("Can't set '%s' property for '%s' node (%s)\n",
2455 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2456 fdt_strerror (ret));
2457 return -1;
2458 }
2459
2460 return 0;
2461}
2462
2463/**
2464 * calculate_hash - calculate and return hash for provided input data
2465 * @data: pointer to the input data
2466 * @data_len: data length
2467 * @algo: requested hash algorithm
2468 * @value: pointer to the char, will hold hash value data (caller must
2469 * allocate enough free space)
2470 * value_len: length of the calculated hash
2471 *
2472 * calculate_hash() computes input data hash according to the requested algorithm.
2473 * Resulting hash value is placed in caller provided 'value' buffer, length
2474 * of the calculated hash is returned via value_len pointer argument.
2475 *
2476 * returns:
2477 * 0, on success
2478 * -1, when algo is unsupported
2479 */
2480static int calculate_hash (const void *data, int data_len, const char *algo,
2481 uint8_t *value, int *value_len)
2482{
2483 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002484 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2485 CHUNKSZ_CRC32);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002486 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2487 *value_len = 4;
2488 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002489 sha1_csum_wd ((unsigned char *) data, data_len,
2490 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002491 *value_len = 20;
2492 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002493 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka766529f2008-03-14 16:22:34 +01002494 *value_len = 16;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002495 } else {
2496 debug ("Unsupported hash alogrithm\n");
2497 return -1;
2498 }
2499 return 0;
2500}
2501
2502#ifdef USE_HOSTCC
2503/**
2504 * fit_set_hashes - process FIT component image nodes and calculate hashes
2505 * @fit: pointer to the FIT format image header
2506 *
2507 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2508 * Hashes are calculated for all component images which have hash subnodes
2509 * with algorithm property set to one of the supported hash algorithms.
2510 *
2511 * returns
2512 * 0, on success
2513 * libfdt error code, on failure
2514 */
2515int fit_set_hashes (void *fit)
2516{
2517 int images_noffset;
2518 int noffset;
2519 int ndepth;
2520 int ret;
2521
2522 /* Find images parent node offset */
2523 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2524 if (images_noffset < 0) {
2525 printf ("Can't find images parent node '%s' (%s)\n",
2526 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2527 return images_noffset;
2528 }
2529
2530 /* Process its subnodes, print out component images details */
2531 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2532 (noffset >= 0) && (ndepth > 0);
2533 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2534 if (ndepth == 1) {
2535 /*
2536 * Direct child node of the images parent node,
2537 * i.e. component image node.
2538 */
2539 ret = fit_image_set_hashes (fit, noffset);
2540 if (ret)
2541 return ret;
2542 }
2543 }
2544
2545 return 0;
2546}
2547
2548/**
2549 * fit_image_set_hashes - calculate/set hashes for given component image node
2550 * @fit: pointer to the FIT format image header
2551 * @image_noffset: requested component image node
2552 *
2553 * fit_image_set_hashes() adds hash values for an component image node. All
2554 * existing hash subnodes are checked, if algorithm property is set to one of
2555 * the supported hash algorithms, hash value is computed and corresponding
2556 * hash node property is set, for example:
2557 *
2558 * Input component image node structure:
2559 *
2560 * o image@1 (at image_noffset)
2561 * | - data = [binary data]
2562 * o hash@1
2563 * |- algo = "sha1"
2564 *
2565 * Output component image node structure:
2566 *
2567 * o image@1 (at image_noffset)
2568 * | - data = [binary data]
2569 * o hash@1
2570 * |- algo = "sha1"
2571 * |- value = sha1(data)
2572 *
2573 * returns:
2574 * 0 on sucess
2575 * <0 on failure
2576 */
2577int fit_image_set_hashes (void *fit, int image_noffset)
2578{
2579 const void *data;
2580 size_t size;
2581 char *algo;
2582 uint8_t value[FIT_MAX_HASH_LEN];
2583 int value_len;
2584 int noffset;
2585 int ndepth;
2586
2587 /* Get image data and data length */
2588 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2589 printf ("Can't get image data/size\n");
2590 return -1;
2591 }
2592
2593 /* Process all hash subnodes of the component image node */
2594 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2595 (noffset >= 0) && (ndepth > 0);
2596 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2597 if (ndepth == 1) {
2598 /* Direct child node of the component image node */
2599
2600 /*
2601 * Check subnode name, must be equal to "hash".
2602 * Multiple hash nodes require unique unit node
2603 * names, e.g. hash@1, hash@2, etc.
2604 */
2605 if (strncmp (fit_get_name(fit, noffset, NULL),
2606 FIT_HASH_NODENAME,
2607 strlen(FIT_HASH_NODENAME)) != 0) {
2608 /* Not a hash subnode, skip it */
2609 continue;
2610 }
2611
2612 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2613 printf ("Can't get hash algo property for "
2614 "'%s' hash node in '%s' image node\n",
2615 fit_get_name (fit, noffset, NULL),
2616 fit_get_name (fit, image_noffset, NULL));
2617 return -1;
2618 }
2619
2620 if (calculate_hash (data, size, algo, value, &value_len)) {
2621 printf ("Unsupported hash algorithm (%s) for "
2622 "'%s' hash node in '%s' image node\n",
2623 algo, fit_get_name (fit, noffset, NULL),
2624 fit_get_name (fit, image_noffset, NULL));
2625 return -1;
2626 }
2627
2628 if (fit_image_hash_set_value (fit, noffset, value,
2629 value_len)) {
2630 printf ("Can't set hash value for "
2631 "'%s' hash node in '%s' image node\n",
2632 fit_get_name (fit, noffset, NULL),
2633 fit_get_name (fit, image_noffset, NULL));
2634 return -1;
2635 }
2636 }
2637 }
2638
2639 return 0;
2640}
2641
2642/**
2643 * fit_image_hash_set_value - set hash value in requested has node
2644 * @fit: pointer to the FIT format image header
2645 * @noffset: hash node offset
2646 * @value: hash value to be set
2647 * @value_len: hash value length
2648 *
2649 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2650 * given and returns operation status to the caller.
2651 *
2652 * returns
2653 * 0, on success
2654 * -1, on failure
2655 */
2656int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2657 int value_len)
2658{
2659 int ret;
2660
2661 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2662 if (ret) {
2663 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2664 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2665 fdt_strerror (ret));
2666 return -1;
2667 }
2668
2669 return 0;
2670}
2671#endif /* USE_HOSTCC */
2672
2673/**
2674 * fit_image_check_hashes - verify data intergity
2675 * @fit: pointer to the FIT format image header
2676 * @image_noffset: component image node offset
2677 *
2678 * fit_image_check_hashes() goes over component image hash nodes,
2679 * re-calculates each data hash and compares with the value stored in hash
2680 * node.
2681 *
2682 * returns:
2683 * 1, if all hashes are valid
2684 * 0, otherwise (or on error)
2685 */
2686int fit_image_check_hashes (const void *fit, int image_noffset)
2687{
2688 const void *data;
2689 size_t size;
2690 char *algo;
2691 uint8_t *fit_value;
2692 int fit_value_len;
2693 uint8_t value[FIT_MAX_HASH_LEN];
2694 int value_len;
2695 int noffset;
2696 int ndepth;
2697 char *err_msg = "";
2698
2699 /* Get image data and data length */
2700 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2701 printf ("Can't get image data/size\n");
2702 return 0;
2703 }
2704
2705 /* Process all hash subnodes of the component image node */
2706 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2707 (noffset >= 0) && (ndepth > 0);
2708 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2709 if (ndepth == 1) {
2710 /* Direct child node of the component image node */
2711
2712 /*
2713 * Check subnode name, must be equal to "hash".
2714 * Multiple hash nodes require unique unit node
2715 * names, e.g. hash@1, hash@2, etc.
2716 */
2717 if (strncmp (fit_get_name(fit, noffset, NULL),
2718 FIT_HASH_NODENAME,
2719 strlen(FIT_HASH_NODENAME)) != 0)
2720 continue;
2721
2722 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002723 err_msg = " error!\nCan't get hash algo "
2724 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002725 goto error;
2726 }
2727 printf ("%s", algo);
2728
2729 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2730 &fit_value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002731 err_msg = " error!\nCan't get hash value "
2732 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002733 goto error;
2734 }
2735
2736 if (calculate_hash (data, size, algo, value, &value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002737 err_msg = " error!\nUnsupported hash algorithm";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002738 goto error;
2739 }
2740
2741 if (value_len != fit_value_len) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002742 err_msg = " error !\nBad hash value len";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002743 goto error;
2744 } else if (memcmp (value, fit_value, value_len) != 0) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002745 err_msg = " error!\nBad hash value";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002746 goto error;
2747 }
2748 printf ("+ ");
2749 }
2750 }
2751
2752 return 1;
2753
2754error:
2755 printf ("%s for '%s' hash node in '%s' image node\n",
2756 err_msg, fit_get_name (fit, noffset, NULL),
2757 fit_get_name (fit, image_noffset, NULL));
2758 return 0;
2759}
2760
2761/**
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002762 * fit_all_image_check_hashes - verify data intergity for all images
2763 * @fit: pointer to the FIT format image header
2764 *
2765 * fit_all_image_check_hashes() goes over all images in the FIT and
2766 * for every images checks if all it's hashes are valid.
2767 *
2768 * returns:
2769 * 1, if all hashes of all images are valid
2770 * 0, otherwise (or on error)
2771 */
2772int fit_all_image_check_hashes (const void *fit)
2773{
2774 int images_noffset;
2775 int noffset;
2776 int ndepth;
2777 int count;
2778
2779 /* Find images parent node offset */
2780 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2781 if (images_noffset < 0) {
2782 printf ("Can't find images parent node '%s' (%s)\n",
2783 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2784 return 0;
2785 }
2786
2787 /* Process all image subnodes, check hashes for each */
2788 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2789 (ulong)fit);
2790 for (ndepth = 0, count = 0,
2791 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2792 (noffset >= 0) && (ndepth > 0);
2793 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2794 if (ndepth == 1) {
2795 /*
2796 * Direct child node of the images parent node,
2797 * i.e. component image node.
2798 */
2799 printf (" Hash(es) for Image %u (%s): ", count++,
2800 fit_get_name (fit, noffset, NULL));
2801
2802 if (!fit_image_check_hashes (fit, noffset))
2803 return 0;
2804 printf ("\n");
2805 }
2806 }
2807 return 1;
2808}
2809
2810/**
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002811 * fit_image_check_os - check whether image node is of a given os type
2812 * @fit: pointer to the FIT format image header
2813 * @noffset: component image node offset
2814 * @os: requested image os
2815 *
2816 * fit_image_check_os() reads image os property and compares its numeric
2817 * id with the requested os. Comparison result is returned to the caller.
2818 *
2819 * returns:
2820 * 1 if image is of given os type
2821 * 0 otherwise (or on error)
2822 */
2823int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2824{
2825 uint8_t image_os;
2826
2827 if (fit_image_get_os (fit, noffset, &image_os))
2828 return 0;
2829 return (os == image_os);
2830}
2831
2832/**
2833 * fit_image_check_arch - check whether image node is of a given arch
2834 * @fit: pointer to the FIT format image header
2835 * @noffset: component image node offset
2836 * @arch: requested imagearch
2837 *
2838 * fit_image_check_arch() reads image arch property and compares its numeric
2839 * id with the requested arch. Comparison result is returned to the caller.
2840 *
2841 * returns:
2842 * 1 if image is of given arch
2843 * 0 otherwise (or on error)
2844 */
2845int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2846{
2847 uint8_t image_arch;
2848
2849 if (fit_image_get_arch (fit, noffset, &image_arch))
2850 return 0;
2851 return (arch == image_arch);
2852}
2853
2854/**
2855 * fit_image_check_type - check whether image node is of a given type
2856 * @fit: pointer to the FIT format image header
2857 * @noffset: component image node offset
2858 * @type: requested image type
2859 *
2860 * fit_image_check_type() reads image type property and compares its numeric
2861 * id with the requested type. Comparison result is returned to the caller.
2862 *
2863 * returns:
2864 * 1 if image is of given type
2865 * 0 otherwise (or on error)
2866 */
2867int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2868{
2869 uint8_t image_type;
2870
2871 if (fit_image_get_type (fit, noffset, &image_type))
2872 return 0;
2873 return (type == image_type);
2874}
2875
2876/**
2877 * fit_image_check_comp - check whether image node uses given compression
2878 * @fit: pointer to the FIT format image header
2879 * @noffset: component image node offset
2880 * @comp: requested image compression type
2881 *
2882 * fit_image_check_comp() reads image compression property and compares its
2883 * numeric id with the requested compression type. Comparison result is
2884 * returned to the caller.
2885 *
2886 * returns:
2887 * 1 if image uses requested compression
2888 * 0 otherwise (or on error)
2889 */
2890int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2891{
2892 uint8_t image_comp;
2893
2894 if (fit_image_get_comp (fit, noffset, &image_comp))
2895 return 0;
2896 return (comp == image_comp);
2897}
2898
2899/**
2900 * fit_check_format - sanity check FIT image format
2901 * @fit: pointer to the FIT format image header
2902 *
2903 * fit_check_format() runs a basic sanity FIT image verification.
2904 * Routine checks for mandatory properties, nodes, etc.
2905 *
2906 * returns:
2907 * 1, on success
2908 * 0, on failure
2909 */
2910int fit_check_format (const void *fit)
2911{
2912 /* mandatory / node 'description' property */
2913 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2914 debug ("Wrong FIT format: no description\n");
2915 return 0;
2916 }
2917
2918#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2919 /* mandatory / node 'timestamp' property */
2920 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002921 debug ("Wrong FIT format: no timestamp\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002922 return 0;
2923 }
2924#endif
2925
2926 /* mandatory subimages parent '/images' node */
2927 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2928 debug ("Wrong FIT format: no images parent node\n");
2929 return 0;
2930 }
2931
2932 return 1;
2933}
2934
2935/**
2936 * fit_conf_get_node - get node offset for configuration of a given unit name
2937 * @fit: pointer to the FIT format image header
2938 * @conf_uname: configuration node unit name
2939 *
2940 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2941 * parant node) of a provided unit name. If configuration is found its node offset
2942 * is returned to the caller.
2943 *
2944 * When NULL is provided in second argument fit_conf_get_node() will search
2945 * for a default configuration node instead. Default configuration node unit name
2946 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2947 *
2948 * returns:
2949 * configuration node offset when found (>=0)
2950 * negative number on failure (FDT_ERR_* code)
2951 */
2952int fit_conf_get_node (const void *fit, const char *conf_uname)
2953{
2954 int noffset, confs_noffset;
2955 int len;
2956
2957 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2958 if (confs_noffset < 0) {
2959 debug ("Can't find configurations parent node '%s' (%s)\n",
2960 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2961 return confs_noffset;
2962 }
2963
2964 if (conf_uname == NULL) {
2965 /* get configuration unit name from the default property */
2966 debug ("No configuration specified, trying default...\n");
2967 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2968 if (conf_uname == NULL) {
2969 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2970 return len;
2971 }
2972 debug ("Found default configuration: '%s'\n", conf_uname);
2973 }
2974
2975 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2976 if (noffset < 0) {
2977 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2978 conf_uname, fdt_strerror (noffset));
2979 }
2980
2981 return noffset;
2982}
2983
2984static int __fit_conf_get_prop_node (const void *fit, int noffset,
2985 const char *prop_name)
2986{
2987 char *uname;
2988 int len;
2989
2990 /* get kernel image unit name from configuration kernel property */
2991 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2992 if (uname == NULL)
2993 return len;
2994
2995 return fit_image_get_node (fit, uname);
2996}
2997
2998/**
2999 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3000 * a given configuration
3001 * @fit: pointer to the FIT format image header
3002 * @noffset: configuration node offset
3003 *
3004 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3005 * configuration FIT_KERNEL_PROP property and translates it to the node
3006 * offset.
3007 *
3008 * returns:
3009 * image node offset when found (>=0)
3010 * negative number on failure (FDT_ERR_* code)
3011 */
3012int fit_conf_get_kernel_node (const void *fit, int noffset)
3013{
3014 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
3015}
3016
3017/**
3018 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3019 * a given configuration
3020 * @fit: pointer to the FIT format image header
3021 * @noffset: configuration node offset
3022 *
3023 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3024 * configuration FIT_KERNEL_PROP property and translates it to the node
3025 * offset.
3026 *
3027 * returns:
3028 * image node offset when found (>=0)
3029 * negative number on failure (FDT_ERR_* code)
3030 */
3031int fit_conf_get_ramdisk_node (const void *fit, int noffset)
3032{
3033 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
3034}
3035
3036/**
3037 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3038 * a given configuration
3039 * @fit: pointer to the FIT format image header
3040 * @noffset: configuration node offset
3041 *
3042 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3043 * configuration FIT_KERNEL_PROP property and translates it to the node
3044 * offset.
3045 *
3046 * returns:
3047 * image node offset when found (>=0)
3048 * negative number on failure (FDT_ERR_* code)
3049 */
3050int fit_conf_get_fdt_node (const void *fit, int noffset)
3051{
3052 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
3053}
3054
3055/**
3056 * fit_conf_print - prints out the FIT configuration details
3057 * @fit: pointer to the FIT format image header
Marian Balakowiczf773bea2008-03-12 10:35:46 +01003058 * @noffset: offset of the configuration node
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003059 * @p: pointer to prefix string
3060 *
3061 * fit_conf_print() lists all mandatory properies for the processed
3062 * configuration node.
3063 *
3064 * returns:
3065 * no returned results
3066 */
3067void fit_conf_print (const void *fit, int noffset, const char *p)
3068{
3069 char *desc;
3070 char *uname;
3071 int ret;
3072
3073 /* Mandatory properties */
3074 ret = fit_get_desc (fit, noffset, &desc);
3075 printf ("%s Description: ", p);
3076 if (ret)
3077 printf ("unavailable\n");
3078 else
3079 printf ("%s\n", desc);
3080
3081 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3082 printf ("%s Kernel: ", p);
3083 if (uname == NULL)
3084 printf ("unavailable\n");
3085 else
3086 printf ("%s\n", uname);
3087
3088 /* Optional properties */
3089 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3090 if (uname)
3091 printf ("%s Init Ramdisk: %s\n", p, uname);
3092
3093 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3094 if (uname)
3095 printf ("%s FDT: %s\n", p, uname);
3096}
Marian Balakowiczc8779642008-03-12 10:12:37 +01003097
3098/**
3099 * fit_check_ramdisk - verify FIT format ramdisk subimage
3100 * @fit_hdr: pointer to the FIT ramdisk header
3101 * @rd_noffset: ramdisk subimage node offset within FIT image
3102 * @arch: requested ramdisk image architecture type
3103 * @verify: data CRC verification flag
3104 *
3105 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3106 * specified FIT image.
3107 *
3108 * returns:
3109 * 1, on success
3110 * 0, on failure
3111 */
3112#ifndef USE_HOSTCC
3113static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3114{
3115 fit_image_print (fit, rd_noffset, " ");
3116
3117 if (verify) {
3118 puts (" Verifying Hash Integrity ... ");
3119 if (!fit_image_check_hashes (fit, rd_noffset)) {
3120 puts ("Bad Data Hash\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003121 show_boot_progress (-125);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003122 return 0;
3123 }
3124 puts ("OK\n");
3125 }
3126
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003127 show_boot_progress (126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003128 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3129 !fit_image_check_arch (fit, rd_noffset, arch) ||
3130 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3131 printf ("No Linux %s Ramdisk Image\n",
3132 genimg_get_arch_name(arch));
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003133 show_boot_progress (-126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003134 return 0;
3135 }
3136
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003137 show_boot_progress (127);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003138 return 1;
3139}
3140#endif /* USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003141#endif /* CONFIG_FIT */