blob: ed6b26bd2a7fc17542791cfc9d55e7b0b9c41db4 [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", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100136 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
137 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
138 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
139 { IH_TYPE_SCRIPT, "script", "Script", },
140 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Stefano Babic8edcde52010-01-20 18:19:10 +0100141 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
Wolfgang Denke42624b2011-10-07 08:33:16 +0200142 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
143 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000144 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Wolfgang Denke42624b2011-10-07 08:33:16 +0200145 { IH_TYPE_INVALID, NULL, "Invalid Image", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100146 { -1, "", "", },
147};
148
Mike Frysinger7edb1862010-10-20 07:17:39 -0400149static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100150 { IH_COMP_NONE, "none", "uncompressed", },
151 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
152 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200153 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100154 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100155 { -1, "", "", },
156};
157
Mike Frysinger89cdab72008-03-31 11:02:01 -0400158uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200159uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100160#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
161static void genimg_print_time (time_t timestamp);
162#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100163
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100164/*****************************************************************************/
165/* Legacy format routines */
166/*****************************************************************************/
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200167int image_check_hcrc (const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100168{
169 ulong hcrc;
170 ulong len = image_get_header_size ();
171 image_header_t header;
172
173 /* Copy header so we can blank CRC field for re-calculation */
174 memmove (&header, (char *)hdr, image_get_header_size ());
175 image_set_hcrc (&header, 0);
176
177 hcrc = crc32 (0, (unsigned char *)&header, len);
178
179 return (hcrc == image_get_hcrc (hdr));
180}
181
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200182int image_check_dcrc (const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100183{
184 ulong data = image_get_data (hdr);
185 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200186 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100187
188 return (dcrc == image_get_dcrc (hdr));
189}
190
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100191/**
192 * image_multi_count - get component (sub-image) count
193 * @hdr: pointer to the header of the multi component image
194 *
195 * image_multi_count() returns number of components in a multi
196 * component image.
197 *
198 * Note: no checking of the image type is done, caller must pass
199 * a valid multi component image.
200 *
201 * returns:
202 * number of components
203 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200204ulong image_multi_count (const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100205{
206 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100207 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100208
209 /* get start of the image payload, which in case of multi
210 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100211 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100212
213 /* count non empty slots */
214 for (i = 0; size[i]; ++i)
215 count++;
216
217 return count;
218}
219
220/**
221 * image_multi_getimg - get component data address and size
222 * @hdr: pointer to the header of the multi component image
223 * @idx: index of the requested component
224 * @data: pointer to a ulong variable, will hold component data address
225 * @len: pointer to a ulong variable, will hold component size
226 *
227 * image_multi_getimg() returns size and data address for the requested
228 * component in a multi component image.
229 *
230 * Note: no checking of the image type is done, caller must pass
231 * a valid multi component image.
232 *
233 * returns:
234 * data address and size of the component, if idx is valid
235 * 0 in data and len, if idx is out of range
236 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200237void image_multi_getimg (const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100238 ulong *data, ulong *len)
239{
240 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100241 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700242 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100243
244 /* get number of component */
245 count = image_multi_count (hdr);
246
247 /* get start of the image payload, which in case of multi
248 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100249 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100250
251 /* get address of the proper component data start, which means
252 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100253 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100254
255 if (idx < count) {
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100256 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100257 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100258
259 /* go over all indices preceding requested component idx */
260 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700261 /* add up i-th component size, rounding up to 4 bytes */
262 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100263 }
264
265 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700266 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100267 } else {
268 *len = 0;
269 *data = 0;
270 }
271}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100272
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200273static void image_print_type (const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100274{
275 const char *os, *arch, *type, *comp;
276
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100277 os = genimg_get_os_name (image_get_os (hdr));
278 arch = genimg_get_arch_name (image_get_arch (hdr));
279 type = genimg_get_type_name (image_get_type (hdr));
280 comp = genimg_get_comp_name (image_get_comp (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100281
Marian Balakowicz570abb02008-02-29 15:59:59 +0100282 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100283}
284
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100285/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200286 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200287 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100288 * @p: pointer to prefix string
289 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200290 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100291 * The routine prints out all header fields followed by the size/offset data
292 * for MULTI/SCRIPT images.
293 *
294 * returns:
295 * no returned results
296 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200297void image_print_contents (const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100298{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200299 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200300 const char *p;
301
302#ifdef USE_HOSTCC
303 p = "";
304#else
305 p = " ";
306#endif
307
Marian Balakowicz570abb02008-02-29 15:59:59 +0100308 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
309#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
310 printf ("%sCreated: ", p);
311 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100312#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100313 printf ("%sImage Type: ", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100314 image_print_type (hdr);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100315 printf ("%sData Size: ", p);
316 genimg_print_size (image_get_data_size (hdr));
317 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
318 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100319
Marian Balakowicz570abb02008-02-29 15:59:59 +0100320 if (image_check_type (hdr, IH_TYPE_MULTI) ||
321 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100322 int i;
323 ulong data, len;
324 ulong count = image_multi_count (hdr);
325
Marian Balakowicz570abb02008-02-29 15:59:59 +0100326 printf ("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100327 for (i = 0; i < count; i++) {
328 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100329
330 printf ("%s Image %d: ", p, i);
331 genimg_print_size (len);
332
333 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
334 /*
335 * the user may need to know offsets
336 * if planning to do something with
337 * multiple files
338 */
339 printf ("%s Offset = 0x%08lx\n", p, data);
340 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100341 }
342 }
343}
344
Marian Balakowicz570abb02008-02-29 15:59:59 +0100345
346#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100347/**
348 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100349 * @rd_addr: ramdisk image start address
350 * @arch: expected ramdisk architecture
351 * @verify: checksum verification flag
352 *
353 * image_get_ramdisk() returns a pointer to the verified ramdisk image
354 * header. Routine receives image start address and expected architecture
355 * flag. Verification done covers data and header integrity and os/type/arch
356 * fields checking.
357 *
358 * If dataflash support is enabled routine checks for dataflash addresses
359 * and handles required dataflash reads.
360 *
361 * returns:
362 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600363 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100364 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200365static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100366 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100367{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200368 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100369
370 if (!image_check_magic (rd_hdr)) {
371 puts ("Bad Magic Number\n");
372 show_boot_progress (-10);
Kumar Gala274cea22008-02-27 21:51:46 -0600373 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100374 }
375
376 if (!image_check_hcrc (rd_hdr)) {
377 puts ("Bad Header Checksum\n");
378 show_boot_progress (-11);
Kumar Gala274cea22008-02-27 21:51:46 -0600379 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100380 }
381
382 show_boot_progress (10);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100383 image_print_contents (rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100384
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100385 if (verify) {
386 puts(" Verifying Checksum ... ");
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200387 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100388 puts ("Bad Data CRC\n");
389 show_boot_progress (-12);
Kumar Gala274cea22008-02-27 21:51:46 -0600390 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100391 }
392 puts("OK\n");
393 }
394
395 show_boot_progress (11);
396
397 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
398 !image_check_arch (rd_hdr, arch) ||
399 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
400 printf ("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100401 genimg_get_arch_name(arch));
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100402 show_boot_progress (-13);
Kumar Gala274cea22008-02-27 21:51:46 -0600403 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100404 }
405
406 return rd_hdr;
407}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100408#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100409
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100410/*****************************************************************************/
411/* Shared dual-format routines */
412/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100413#ifndef USE_HOSTCC
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200414int getenv_yesno (char *var)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100415{
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200416 char *s = getenv (var);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100417 return (s && (*s == 'n')) ? 0 : 1;
418}
419
420ulong getenv_bootm_low(void)
421{
422 char *s = getenv ("bootm_low");
423 if (s) {
424 ulong tmp = simple_strtoul (s, NULL, 16);
425 return tmp;
426 }
427
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200428#if defined(CONFIG_SYS_SDRAM_BASE)
429 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100430#elif defined(CONFIG_ARM)
431 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100432#else
433 return 0;
434#endif
435}
436
Becky Bruce391fd932008-06-09 20:37:18 -0500437phys_size_t getenv_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100438{
Matthew McClintockc519fac2010-07-08 10:11:08 -0500439 phys_size_t tmp;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100440 char *s = getenv ("bootm_size");
441 if (s) {
Becky Bruce391fd932008-06-09 20:37:18 -0500442 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100443 return tmp;
444 }
Matthew McClintockc519fac2010-07-08 10:11:08 -0500445 s = getenv("bootm_low");
446 if (s)
447 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
448 else
449 tmp = 0;
450
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100451
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100452#if defined(CONFIG_ARM)
Matthew McClintockc519fac2010-07-08 10:11:08 -0500453 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100454#else
Matthew McClintockc519fac2010-07-08 10:11:08 -0500455 return gd->bd->bi_memsize - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100456#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100457}
458
Grant Likelyc3624e62011-03-28 09:58:43 +0000459phys_size_t getenv_bootm_mapsize(void)
460{
461 phys_size_t tmp;
462 char *s = getenv ("bootm_mapsize");
463 if (s) {
464 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
465 return tmp;
466 }
467
468#if defined(CONFIG_SYS_BOOTMAPSZ)
469 return CONFIG_SYS_BOOTMAPSZ;
470#else
471 return getenv_bootm_size();
472#endif
473}
474
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100475void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
476{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400477 if (to == from)
478 return;
479
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100480#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
481 while (len > 0) {
482 size_t tail = (len > chunksz) ? chunksz : len;
483 WATCHDOG_RESET ();
484 memmove (to, from, tail);
485 to += tail;
486 from += tail;
487 len -= tail;
488 }
489#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
490 memmove (to, from, len);
491#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
492}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100493#endif /* !USE_HOSTCC */
494
Prafulla Wadaskarf666dea2009-09-07 14:59:07 +0530495void genimg_print_size (uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100496{
497#ifndef USE_HOSTCC
498 printf ("%d Bytes = ", size);
499 print_size (size, "\n");
500#else
501 printf ("%d Bytes = %.2f kB = %.2f MB\n",
502 size, (double)size / 1.024e3,
503 (double)size / 1.048576e6);
504#endif
505}
506
507#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
508static void genimg_print_time (time_t timestamp)
509{
510#ifndef USE_HOSTCC
511 struct rtc_time tm;
512
513 to_tm (timestamp, &tm);
514 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
515 tm.tm_year, tm.tm_mon, tm.tm_mday,
516 tm.tm_hour, tm.tm_min, tm.tm_sec);
517#else
518 printf ("%s", ctime(&timestamp));
519#endif
520}
521#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
522
523/**
524 * get_table_entry_name - translate entry id to long name
525 * @table: pointer to a translation table for entries of a specific type
526 * @msg: message to be returned when translation fails
527 * @id: entry id to be translated
528 *
529 * get_table_entry_name() will go over translation table trying to find
530 * entry that matches given id. If matching entry is found, its long
531 * name is returned to the caller.
532 *
533 * returns:
534 * long entry name if translation succeeds
535 * msg otherwise
536 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400537char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100538{
539 for (; table->id >= 0; ++table) {
540 if (table->id == id)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200541#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Woode3d1ac72009-04-02 16:15:10 -0500542 return table->lname;
543#else
544 return table->lname + gd->reloc_off;
545#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100546 }
547 return (msg);
548}
549
550const char *genimg_get_os_name (uint8_t os)
551{
552 return (get_table_entry_name (uimage_os, "Unknown OS", os));
553}
554
555const char *genimg_get_arch_name (uint8_t arch)
556{
557 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
558}
559
560const char *genimg_get_type_name (uint8_t type)
561{
562 return (get_table_entry_name (uimage_type, "Unknown Image", type));
563}
564
565const char *genimg_get_comp_name (uint8_t comp)
566{
567 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
568}
569
570/**
571 * get_table_entry_id - translate short entry name to id
572 * @table: pointer to a translation table for entries of a specific type
573 * @table_name: to be used in case of error
574 * @name: entry short name to be translated
575 *
576 * get_table_entry_id() will go over translation table trying to find
577 * entry that matches given short name. If matching entry is found,
578 * its id returned to the caller.
579 *
580 * returns:
581 * entry id if translation succeeds
582 * -1 otherwise
583 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400584int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100585 const char *table_name, const char *name)
586{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400587 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100588#ifdef USE_HOSTCC
589 int first = 1;
590
591 for (t = table; t->id >= 0; ++t) {
592 if (t->sname && strcasecmp(t->sname, name) == 0)
593 return (t->id);
594 }
595
596 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
597 for (t = table; t->id >= 0; ++t) {
598 if (t->sname == NULL)
599 continue;
600 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
601 first = 0;
602 }
603 fprintf (stderr, "\n");
604#else
605 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200606#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Woode3d1ac72009-04-02 16:15:10 -0500607 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200608#else
609 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500610#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100611 return (t->id);
612 }
613 debug ("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100614#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100615 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100616}
617
Marian Balakowicz570abb02008-02-29 15:59:59 +0100618int genimg_get_os_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100619{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100620 return (get_table_entry_id (uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100621}
622
Marian Balakowicz570abb02008-02-29 15:59:59 +0100623int genimg_get_arch_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100624{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100625 return (get_table_entry_id (uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100626}
627
Marian Balakowicz570abb02008-02-29 15:59:59 +0100628int genimg_get_type_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100629{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100630 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100631}
632
Marian Balakowicz570abb02008-02-29 15:59:59 +0100633int genimg_get_comp_id (const char *name)
634{
635 return (get_table_entry_id (uimage_comp, "Compression", name));
636}
637
638#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100639/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100640 * genimg_get_format - get image format type
641 * @img_addr: image start address
642 *
643 * genimg_get_format() checks whether provided address points to a valid
644 * legacy or FIT image.
645 *
646 * New uImage format and FDT blob are based on a libfdt. FDT blob
647 * may be passed directly or embedded in a FIT image. In both situations
648 * genimg_get_format() must be able to dectect libfdt header.
649 *
650 * returns:
651 * image format type or IMAGE_FORMAT_INVALID if no image is present
652 */
653int genimg_get_format (void *img_addr)
654{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200655 ulong format = IMAGE_FORMAT_INVALID;
656 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100657#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200658 char *fit_hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100659#endif
660
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200661 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100662 if (image_check_magic(hdr))
663 format = IMAGE_FORMAT_LEGACY;
664#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
665 else {
666 fit_hdr = (char *)img_addr;
667 if (fdt_check_header (fit_hdr) == 0)
668 format = IMAGE_FORMAT_FIT;
669 }
670#endif
671
672 return format;
673}
674
675/**
676 * genimg_get_image - get image from special storage (if necessary)
677 * @img_addr: image start address
678 *
679 * genimg_get_image() checks if provided image start adddress is located
680 * in a dataflash storage. If so, image is moved to a system RAM memory.
681 *
682 * returns:
683 * image start address after possible relocation from special storage
684 */
685ulong genimg_get_image (ulong img_addr)
686{
687 ulong ram_addr = img_addr;
688
689#ifdef CONFIG_HAS_DATAFLASH
690 ulong h_size, d_size;
691
692 if (addr_dataflash (img_addr)){
693 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200694 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100695
696 /* get header size */
697 h_size = image_get_header_size ();
698#if defined(CONFIG_FIT)
699 if (sizeof(struct fdt_header) > h_size)
700 h_size = sizeof(struct fdt_header);
701#endif
702
703 /* read in header */
704 debug (" Reading image header from dataflash address "
705 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
706
707 read_dataflash (img_addr, h_size, (char *)ram_addr);
708
709 /* get data size */
710 switch (genimg_get_format ((void *)ram_addr)) {
711 case IMAGE_FORMAT_LEGACY:
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200712 d_size = image_get_data_size ((const image_header_t *)ram_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100713 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
714 ram_addr, d_size);
715 break;
716#if defined(CONFIG_FIT)
717 case IMAGE_FORMAT_FIT:
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100718 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100719 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
720 ram_addr, d_size);
721 break;
722#endif
723 default:
724 printf (" No valid image found at 0x%08lx\n", img_addr);
725 return ram_addr;
726 }
727
728 /* read in image data */
729 debug (" Reading image remaining data from dataflash address "
730 "%08lx to RAM address %08lx\n", img_addr + h_size,
731 ram_addr + h_size);
732
733 read_dataflash (img_addr + h_size, d_size,
734 (char *)(ram_addr + h_size));
735
736 }
737#endif /* CONFIG_HAS_DATAFLASH */
738
739 return ram_addr;
740}
741
742/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100743 * fit_has_config - check if there is a valid FIT configuration
744 * @images: pointer to the bootm command headers structure
745 *
746 * fit_has_config() checks if there is a FIT configuration in use
747 * (if FTI support is present).
748 *
749 * returns:
750 * 0, no FIT support or no configuration found
751 * 1, configuration found
752 */
753int genimg_has_config (bootm_headers_t *images)
754{
755#if defined(CONFIG_FIT)
756 if (images->fit_uname_cfg)
757 return 1;
758#endif
759 return 0;
760}
761
762/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100763 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100764 * @argc: command argument count
765 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100766 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100767 * @arch: expected ramdisk architecture
768 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
769 * @rd_end: pointer to a ulong variable, will hold ramdisk end
770 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100771 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100772 * Curently supported are the following ramdisk sources:
773 * - multicomponent kernel/ramdisk image,
774 * - commandline provided address of decicated ramdisk image.
775 *
776 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100777 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100778 * rd_start and rd_end are set to ramdisk start/end addresses if
779 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100780 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500781 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100782 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100783 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200784int boot_get_ramdisk (int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100785 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100786{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100787 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100788 ulong rd_data, rd_len;
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200789 const image_header_t *rd_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100790#if defined(CONFIG_FIT)
791 void *fit_hdr;
792 const char *fit_uname_config = NULL;
793 const char *fit_uname_ramdisk = NULL;
794 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100795 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100796 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100797 const void *data;
798 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100799#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100800
Marian Balakowiczc8779642008-03-12 10:12:37 +0100801 *rd_start = 0;
802 *rd_end = 0;
803
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100804 /*
805 * Look for a '-' which indicates to ignore the
806 * ramdisk argument
807 */
808 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
809 debug ("## Skipping init Ramdisk\n");
810 rd_len = rd_data = 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100811 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100812#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100813 if (argc >= 3) {
814 /*
815 * If the init ramdisk comes from the FIT image and
816 * the FIT image address is omitted in the command
817 * line argument, try to use os FIT image address or
818 * default load address.
819 */
820 if (images->fit_uname_os)
821 default_addr = (ulong)images->fit_hdr_os;
822 else
823 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100824
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100825 if (fit_parse_conf (argv[2], default_addr,
826 &rd_addr, &fit_uname_config)) {
827 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
828 fit_uname_config, rd_addr);
829 } else if (fit_parse_subimage (argv[2], default_addr,
830 &rd_addr, &fit_uname_ramdisk)) {
831 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
832 fit_uname_ramdisk, rd_addr);
833 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100834#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100835 {
836 rd_addr = simple_strtoul(argv[2], NULL, 16);
837 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
838 rd_addr);
839 }
840#if defined(CONFIG_FIT)
841 } else {
842 /* use FIT configuration provided in first bootm
843 * command argument
844 */
845 rd_addr = (ulong)images->fit_hdr_os;
846 fit_uname_config = images->fit_uname_cfg;
847 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
848 fit_uname_config, rd_addr);
849
850 /*
851 * Check whether configuration has ramdisk defined,
852 * if not, don't try to use it, quit silently.
853 */
854 fit_hdr = (void *)rd_addr;
855 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
856 if (cfg_noffset < 0) {
857 debug ("* ramdisk: no such config\n");
Michal Simekc78fce62008-07-11 10:43:13 +0200858 return 1;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100859 }
860
861 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
862 if (rd_noffset < 0) {
863 debug ("* ramdisk: no ramdisk in config\n");
Peter Tyser41266c92008-08-05 10:51:57 -0500864 return 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100865 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100866 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100867#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100868
869 /* copy from dataflash if needed */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100870 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100871
872 /*
873 * Check if there is an initrd image at the
874 * address provided in the second bootm argument
875 * check image type, for FIT images get FIT node.
876 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100877 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100878 case IMAGE_FORMAT_LEGACY:
Marian Balakowiczc8779642008-03-12 10:12:37 +0100879 printf ("## Loading init Ramdisk from Legacy "
880 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100881
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100882 show_boot_progress (9);
Marian Balakowiczd985c842008-03-12 10:14:38 +0100883 rd_hdr = image_get_ramdisk (rd_addr, arch,
884 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100885
Marian Balakowiczc8779642008-03-12 10:12:37 +0100886 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600887 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600888
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100889 rd_data = image_get_data (rd_hdr);
890 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100891 rd_load = image_get_load (rd_hdr);
892 break;
893#if defined(CONFIG_FIT)
894 case IMAGE_FORMAT_FIT:
895 fit_hdr = (void *)rd_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100896 printf ("## Loading init Ramdisk from FIT "
897 "Image at %08lx ...\n", rd_addr);
898
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100899 show_boot_progress (120);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100900 if (!fit_check_format (fit_hdr)) {
901 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100902 show_boot_progress (-120);
Michal Simekc78fce62008-07-11 10:43:13 +0200903 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100904 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100905 show_boot_progress (121);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100906
907 if (!fit_uname_ramdisk) {
908 /*
909 * no ramdisk image node unit name, try to get config
910 * node first. If config unit node name is NULL
911 * fit_conf_get_node() will try to find default config node
912 */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100913 show_boot_progress (122);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100914 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
915 if (cfg_noffset < 0) {
916 puts ("Could not find configuration node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100917 show_boot_progress (-122);
Michal Simekc78fce62008-07-11 10:43:13 +0200918 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100919 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100920 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
921 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100922
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100923 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100924 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
925 } else {
926 /* get ramdisk component image node offset */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100927 show_boot_progress (123);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100928 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
929 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100930 if (rd_noffset < 0) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100931 puts ("Could not find subimage node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100932 show_boot_progress (-124);
Michal Simekc78fce62008-07-11 10:43:13 +0200933 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100934 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100935
936 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
937
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100938 show_boot_progress (125);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100939 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
Michal Simekc78fce62008-07-11 10:43:13 +0200940 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100941
942 /* get ramdisk image data address and length */
943 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
944 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100945 show_boot_progress (-127);
Michal Simekc78fce62008-07-11 10:43:13 +0200946 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100947 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100948 show_boot_progress (128);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100949
950 rd_data = (ulong)data;
951 rd_len = size;
952
953 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
954 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100955 show_boot_progress (-129);
Michal Simekc78fce62008-07-11 10:43:13 +0200956 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100957 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100958 show_boot_progress (129);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100959
960 images->fit_hdr_rd = fit_hdr;
961 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100962 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100963 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100964#endif
965 default:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100966 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +0100967 rd_data = rd_len = rd_load = 0;
Kumar Galaea86b9e2008-08-29 19:08:29 -0500968 return 1;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100969 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100970 } else if (images->legacy_hdr_valid &&
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200971 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100972 /*
973 * Now check if we have a legacy mult-component image,
974 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100975 */
976 show_boot_progress (13);
977 printf ("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100978 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100979 (ulong)images->legacy_hdr_os);
980
981 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100982 } else {
983 /*
984 * no initrd image
985 */
986 show_boot_progress (14);
987 rd_len = rd_data = 0;
988 }
989
990 if (!rd_data) {
991 debug ("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100992 } else {
993 *rd_start = rd_data;
994 *rd_end = rd_data + rd_len;
995 }
996 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
997 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -0600998
999 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001000}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001001
John Rigbyfca43cc2010-10-13 13:57:35 -06001002#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001003/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001004 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001005 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001006 * @rd_data: ramdisk data start address
1007 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001008 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1009 * start address (after possible relocation)
1010 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1011 * end address (after possible relocation)
1012 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001013 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001014 * variable and if requested ramdisk data is moved to a specified location.
1015 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001016 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1017 * start/end addresses if ramdisk image start and len were provided,
1018 * otherwise set initrd_start and initrd_end set to zeros.
1019 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001020 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001021 * 0 - success
1022 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001023 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001024int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001025 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001026{
1027 char *s;
1028 ulong initrd_high;
1029 int initrd_copy_to_ram = 1;
1030
1031 if ((s = getenv ("initrd_high")) != NULL) {
1032 /* a value of "no" or a similar string will act like 0,
1033 * turning the "load high" feature off. This is intentional.
1034 */
1035 initrd_high = simple_strtoul (s, NULL, 16);
1036 if (initrd_high == ~0)
1037 initrd_copy_to_ram = 0;
1038 } else {
1039 /* not set, no restrictions to load high */
1040 initrd_high = ~0;
1041 }
1042
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001043
1044#ifdef CONFIG_LOGBUFFER
1045 /* Prevent initrd from overwriting logbuffer */
1046 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1047#endif
1048
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001049 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1050 initrd_high, initrd_copy_to_ram);
1051
1052 if (rd_data) {
1053 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1054 debug (" in-place initrd\n");
1055 *initrd_start = rd_data;
1056 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001057 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001058 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001059 if (initrd_high)
Becky Bruce391fd932008-06-09 20:37:18 -05001060 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001061 else
Becky Bruce391fd932008-06-09 20:37:18 -05001062 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001063
Kumar Galae822d7f2008-02-27 21:51:49 -06001064 if (*initrd_start == 0) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001065 puts ("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001066 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001067 }
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001068 show_boot_progress (12);
1069
1070 *initrd_end = *initrd_start + rd_len;
1071 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1072 *initrd_start, *initrd_end);
1073
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001074 memmove_wd ((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001075 (void *)rd_data, rd_len, CHUNKSZ);
1076
1077 puts ("OK\n");
1078 }
1079 } else {
1080 *initrd_start = 0;
1081 *initrd_end = 0;
1082 }
1083 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1084 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001085
Kumar Galae822d7f2008-02-27 21:51:49 -06001086 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001087
Kumar Galae822d7f2008-02-27 21:51:49 -06001088error:
1089 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001090}
John Rigbyfca43cc2010-10-13 13:57:35 -06001091#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001092
Kumar Gala06a09912008-08-15 08:24:38 -05001093#ifdef CONFIG_OF_LIBFDT
1094static void fdt_error (const char *msg)
1095{
1096 puts ("ERROR: ");
1097 puts (msg);
1098 puts (" - must RESET the board to recover.\n");
1099}
1100
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001101static const image_header_t *image_get_fdt (ulong fdt_addr)
Kumar Gala06a09912008-08-15 08:24:38 -05001102{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001103 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001104
1105 image_print_contents (fdt_hdr);
1106
1107 puts (" Verifying Checksum ... ");
1108 if (!image_check_hcrc (fdt_hdr)) {
1109 fdt_error ("fdt header checksum invalid");
1110 return NULL;
1111 }
1112
1113 if (!image_check_dcrc (fdt_hdr)) {
1114 fdt_error ("fdt checksum invalid");
1115 return NULL;
1116 }
1117 puts ("OK\n");
1118
1119 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1120 fdt_error ("uImage is not a fdt");
1121 return NULL;
1122 }
1123 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1124 fdt_error ("uImage is compressed");
1125 return NULL;
1126 }
1127 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1128 fdt_error ("uImage data is not a fdt");
1129 return NULL;
1130 }
1131 return fdt_hdr;
1132}
1133
1134/**
1135 * fit_check_fdt - verify FIT format FDT subimage
1136 * @fit_hdr: pointer to the FIT header
1137 * fdt_noffset: FDT subimage node offset within FIT image
1138 * @verify: data CRC verification flag
1139 *
1140 * fit_check_fdt() verifies integrity of the FDT subimage and from
1141 * specified FIT image.
1142 *
1143 * returns:
1144 * 1, on success
1145 * 0, on failure
1146 */
1147#if defined(CONFIG_FIT)
1148static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1149{
1150 fit_image_print (fit, fdt_noffset, " ");
1151
1152 if (verify) {
1153 puts (" Verifying Hash Integrity ... ");
1154 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1155 fdt_error ("Bad Data Hash");
1156 return 0;
1157 }
1158 puts ("OK\n");
1159 }
1160
1161 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1162 fdt_error ("Not a FDT image");
1163 return 0;
1164 }
1165
1166 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1167 fdt_error ("FDT image is compressed");
1168 return 0;
1169 }
1170
1171 return 1;
1172}
1173#endif /* CONFIG_FIT */
1174
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001175#ifndef CONFIG_SYS_FDT_PAD
1176#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Gala06a09912008-08-15 08:24:38 -05001177#endif
1178
Grant Likely55b0a392011-03-28 09:59:01 +00001179#if defined(CONFIG_OF_LIBFDT)
1180/**
1181 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1182 * @lmb: pointer to lmb handle, will be used for memory mgmt
1183 * @fdt_blob: pointer to fdt blob base address
1184 *
1185 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1186 * memreserve regions prevents u-boot from using them to store the initrd
1187 * or the fdt blob.
1188 */
1189void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1190{
1191 uint64_t addr, size;
1192 int i, total;
1193
1194 if (fdt_check_header (fdt_blob) != 0)
1195 return;
1196
1197 total = fdt_num_mem_rsv(fdt_blob);
1198 for (i = 0; i < total; i++) {
1199 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1200 continue;
1201 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1202 (unsigned long long)addr, (unsigned long long)size);
1203 lmb_reserve(lmb, addr, size);
1204 }
1205}
1206
Kumar Gala06a09912008-08-15 08:24:38 -05001207/**
1208 * boot_relocate_fdt - relocate flat device tree
1209 * @lmb: pointer to lmb handle, will be used for memory mgmt
Kumar Gala06a09912008-08-15 08:24:38 -05001210 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1211 * @of_size: pointer to a ulong variable, will hold fdt length
1212 *
Timur Tabi43b08af2010-05-24 15:10:25 -05001213 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1214 * relocates the of_flat_tree into that region, even if the fdt is already in
1215 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1216 * bytes.
Kumar Gala06a09912008-08-15 08:24:38 -05001217 *
1218 * of_flat_tree and of_size are set to final (after relocation) values
1219 *
1220 * returns:
1221 * 0 - success
1222 * 1 - failure
1223 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001224int boot_relocate_fdt (struct lmb *lmb, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001225{
Timur Tabi43b08af2010-05-24 15:10:25 -05001226 void *fdt_blob = *of_flat_tree;
1227 void *of_start = 0;
David A. Longa28afca2011-07-09 16:40:19 -04001228 char *fdt_high;
Kumar Gala06a09912008-08-15 08:24:38 -05001229 ulong of_len = 0;
Timur Tabi43b08af2010-05-24 15:10:25 -05001230 int err;
David A. Longa28afca2011-07-09 16:40:19 -04001231 int disable_relocation = 0;
Kumar Gala06a09912008-08-15 08:24:38 -05001232
1233 /* nothing to do */
1234 if (*of_size == 0)
1235 return 0;
1236
1237 if (fdt_check_header (fdt_blob) != 0) {
1238 fdt_error ("image is not a fdt");
1239 goto error;
1240 }
1241
Timur Tabi43b08af2010-05-24 15:10:25 -05001242 /* position on a 4K boundary before the alloc_current */
1243 /* Pad the FDT by a specified amount */
1244 of_len = *of_size + CONFIG_SYS_FDT_PAD;
David A. Longa28afca2011-07-09 16:40:19 -04001245
1246 /* If fdt_high is set use it to select the relocation address */
1247 fdt_high = getenv("fdt_high");
1248 if (fdt_high) {
1249 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1250
1251 if (((ulong) desired_addr) == ~0UL) {
1252 /* All ones means use fdt in place */
1253 desired_addr = fdt_blob;
1254 disable_relocation = 1;
1255 }
1256 if (desired_addr) {
1257 of_start =
1258 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1259 ((ulong)
1260 desired_addr)
1261 + of_len);
1262 if (desired_addr && of_start != desired_addr) {
1263 puts("Failed using fdt_high value for Device Tree");
1264 goto error;
1265 }
1266 } else {
1267 of_start =
Matthew McClintock1bb5e902011-07-18 13:08:05 +00001268 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
David A. Longa28afca2011-07-09 16:40:19 -04001269 }
1270 } else {
1271 of_start =
1272 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1273 getenv_bootm_mapsize()
1274 + getenv_bootm_low());
1275 }
Kumar Gala06a09912008-08-15 08:24:38 -05001276
Timur Tabi43b08af2010-05-24 15:10:25 -05001277 if (of_start == 0) {
1278 puts("device tree - allocation error\n");
1279 goto error;
Kumar Gala06a09912008-08-15 08:24:38 -05001280 }
1281
David A. Longa28afca2011-07-09 16:40:19 -04001282 if (disable_relocation) {
1283 /* We assume there is space after the existing fdt to use for padding */
1284 fdt_set_totalsize(of_start, of_len);
1285 printf(" Using Device Tree in place at %p, end %p\n",
1286 of_start, of_start + of_len - 1);
1287 } else {
1288 debug ("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1289 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
Timur Tabi43b08af2010-05-24 15:10:25 -05001290
David A. Longa28afca2011-07-09 16:40:19 -04001291 printf (" Loading Device Tree to %p, end %p ... ",
1292 of_start, of_start + of_len - 1);
Timur Tabi43b08af2010-05-24 15:10:25 -05001293
David A. Longa28afca2011-07-09 16:40:19 -04001294 err = fdt_open_into (fdt_blob, of_start, of_len);
1295 if (err != 0) {
1296 fdt_error ("fdt move failed");
1297 goto error;
1298 }
1299 puts ("OK\n");
Timur Tabi43b08af2010-05-24 15:10:25 -05001300 }
Timur Tabi43b08af2010-05-24 15:10:25 -05001301
1302 *of_flat_tree = of_start;
1303 *of_size = of_len;
1304
Kumar Gala54f9c862008-08-15 08:24:39 -05001305 set_working_fdt_addr(*of_flat_tree);
Kumar Gala06a09912008-08-15 08:24:38 -05001306 return 0;
1307
1308error:
1309 return 1;
1310}
Grant Likelyed59e582011-03-28 09:58:49 +00001311#endif /* CONFIG_OF_LIBFDT */
Kumar Gala06a09912008-08-15 08:24:38 -05001312
1313/**
1314 * boot_get_fdt - main fdt handling routine
1315 * @argc: command argument count
1316 * @argv: command argument list
1317 * @images: pointer to the bootm images structure
1318 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1319 * @of_size: pointer to a ulong variable, will hold fdt length
1320 *
1321 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1322 * Curently supported are the following ramdisk sources:
1323 * - multicomponent kernel/ramdisk image,
1324 * - commandline provided address of decicated ramdisk image.
1325 *
1326 * returns:
1327 * 0, if fdt image was found and valid, or skipped
1328 * of_flat_tree and of_size are set to fdt start address and length if
1329 * fdt image is found and valid
1330 *
1331 * 1, if fdt image is found but corrupted
1332 * of_flat_tree and of_size are set to 0 if no fdt exists
1333 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001334int boot_get_fdt (int flag, int argc, char * const argv[], bootm_headers_t *images,
Kumar Gala06a09912008-08-15 08:24:38 -05001335 char **of_flat_tree, ulong *of_size)
1336{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001337 const image_header_t *fdt_hdr;
Kumar Gala06a09912008-08-15 08:24:38 -05001338 ulong fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001339 char *fdt_blob = NULL;
1340 ulong image_start, image_end;
1341 ulong load_start, load_end;
1342#if defined(CONFIG_FIT)
1343 void *fit_hdr;
1344 const char *fit_uname_config = NULL;
1345 const char *fit_uname_fdt = NULL;
1346 ulong default_addr;
1347 int cfg_noffset;
1348 int fdt_noffset;
1349 const void *data;
1350 size_t size;
1351#endif
1352
1353 *of_flat_tree = NULL;
1354 *of_size = 0;
1355
1356 if (argc > 3 || genimg_has_config (images)) {
1357#if defined(CONFIG_FIT)
1358 if (argc > 3) {
1359 /*
1360 * If the FDT blob comes from the FIT image and the
1361 * FIT image address is omitted in the command line
1362 * argument, try to use ramdisk or os FIT image
1363 * address or default load address.
1364 */
1365 if (images->fit_uname_rd)
1366 default_addr = (ulong)images->fit_hdr_rd;
1367 else if (images->fit_uname_os)
1368 default_addr = (ulong)images->fit_hdr_os;
1369 else
1370 default_addr = load_addr;
1371
1372 if (fit_parse_conf (argv[3], default_addr,
1373 &fdt_addr, &fit_uname_config)) {
1374 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1375 fit_uname_config, fdt_addr);
1376 } else if (fit_parse_subimage (argv[3], default_addr,
1377 &fdt_addr, &fit_uname_fdt)) {
1378 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1379 fit_uname_fdt, fdt_addr);
1380 } else
1381#endif
1382 {
1383 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1384 debug ("* fdt: cmdline image address = 0x%08lx\n",
1385 fdt_addr);
1386 }
1387#if defined(CONFIG_FIT)
1388 } else {
1389 /* use FIT configuration provided in first bootm
1390 * command argument
1391 */
1392 fdt_addr = (ulong)images->fit_hdr_os;
1393 fit_uname_config = images->fit_uname_cfg;
1394 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1395 fit_uname_config, fdt_addr);
1396
1397 /*
1398 * Check whether configuration has FDT blob defined,
1399 * if not quit silently.
1400 */
1401 fit_hdr = (void *)fdt_addr;
1402 cfg_noffset = fit_conf_get_node (fit_hdr,
1403 fit_uname_config);
1404 if (cfg_noffset < 0) {
1405 debug ("* fdt: no such config\n");
1406 return 0;
1407 }
1408
1409 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1410 cfg_noffset);
1411 if (fdt_noffset < 0) {
1412 debug ("* fdt: no fdt in config\n");
1413 return 0;
1414 }
1415 }
1416#endif
1417
1418 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1419 fdt_addr);
1420
1421 /* copy from dataflash if needed */
1422 fdt_addr = genimg_get_image (fdt_addr);
1423
1424 /*
1425 * Check if there is an FDT image at the
1426 * address provided in the second bootm argument
1427 * check image type, for FIT images get a FIT node.
1428 */
1429 switch (genimg_get_format ((void *)fdt_addr)) {
1430 case IMAGE_FORMAT_LEGACY:
1431 /* verify fdt_addr points to a valid image header */
1432 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1433 fdt_addr);
1434 fdt_hdr = image_get_fdt (fdt_addr);
1435 if (!fdt_hdr)
1436 goto error;
1437
1438 /*
1439 * move image data to the load address,
1440 * make sure we don't overwrite initial image
1441 */
1442 image_start = (ulong)fdt_hdr;
1443 image_end = image_get_image_end (fdt_hdr);
1444
1445 load_start = image_get_load (fdt_hdr);
1446 load_end = load_start + image_get_data_size (fdt_hdr);
1447
1448 if ((load_start < image_end) && (load_end > image_start)) {
1449 fdt_error ("fdt overwritten");
1450 goto error;
1451 }
1452
1453 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1454 image_get_data (fdt_hdr), load_start);
1455
1456 memmove ((void *)load_start,
1457 (void *)image_get_data (fdt_hdr),
1458 image_get_data_size (fdt_hdr));
1459
1460 fdt_blob = (char *)load_start;
1461 break;
1462 case IMAGE_FORMAT_FIT:
1463 /*
1464 * This case will catch both: new uImage format
1465 * (libfdt based) and raw FDT blob (also libfdt
1466 * based).
1467 */
1468#if defined(CONFIG_FIT)
1469 /* check FDT blob vs FIT blob */
1470 if (fit_check_format ((const void *)fdt_addr)) {
1471 /*
1472 * FIT image
1473 */
1474 fit_hdr = (void *)fdt_addr;
1475 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1476 fdt_addr);
1477
1478 if (!fit_uname_fdt) {
1479 /*
1480 * no FDT blob image node unit name,
1481 * try to get config node first. If
1482 * config unit node name is NULL
1483 * fit_conf_get_node() will try to
1484 * find default config node
1485 */
1486 cfg_noffset = fit_conf_get_node (fit_hdr,
1487 fit_uname_config);
1488
1489 if (cfg_noffset < 0) {
1490 fdt_error ("Could not find configuration node\n");
1491 goto error;
1492 }
1493
1494 fit_uname_config = fdt_get_name (fit_hdr,
1495 cfg_noffset, NULL);
1496 printf (" Using '%s' configuration\n",
1497 fit_uname_config);
1498
1499 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1500 cfg_noffset);
1501 fit_uname_fdt = fit_get_name (fit_hdr,
1502 fdt_noffset, NULL);
1503 } else {
1504 /* get FDT component image node offset */
1505 fdt_noffset = fit_image_get_node (fit_hdr,
1506 fit_uname_fdt);
1507 }
1508 if (fdt_noffset < 0) {
1509 fdt_error ("Could not find subimage node\n");
1510 goto error;
1511 }
1512
1513 printf (" Trying '%s' FDT blob subimage\n",
1514 fit_uname_fdt);
1515
1516 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1517 images->verify))
1518 goto error;
1519
1520 /* get ramdisk image data address and length */
1521 if (fit_image_get_data (fit_hdr, fdt_noffset,
1522 &data, &size)) {
1523 fdt_error ("Could not find FDT subimage data");
1524 goto error;
1525 }
1526
1527 /* verift that image data is a proper FDT blob */
1528 if (fdt_check_header ((char *)data) != 0) {
1529 fdt_error ("Subimage data is not a FTD");
1530 goto error;
1531 }
1532
1533 /*
1534 * move image data to the load address,
1535 * make sure we don't overwrite initial image
1536 */
1537 image_start = (ulong)fit_hdr;
1538 image_end = fit_get_end (fit_hdr);
1539
1540 if (fit_image_get_load (fit_hdr, fdt_noffset,
1541 &load_start) == 0) {
1542 load_end = load_start + size;
1543
1544 if ((load_start < image_end) &&
1545 (load_end > image_start)) {
1546 fdt_error ("FDT overwritten");
1547 goto error;
1548 }
1549
1550 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1551 (ulong)data, load_start);
1552
1553 memmove ((void *)load_start,
1554 (void *)data, size);
1555
1556 fdt_blob = (char *)load_start;
1557 } else {
1558 fdt_blob = (char *)data;
1559 }
1560
1561 images->fit_hdr_fdt = fit_hdr;
1562 images->fit_uname_fdt = fit_uname_fdt;
1563 images->fit_noffset_fdt = fdt_noffset;
1564 break;
1565 } else
1566#endif
1567 {
1568 /*
1569 * FDT blob
1570 */
1571 fdt_blob = (char *)fdt_addr;
1572 debug ("* fdt: raw FDT blob\n");
1573 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1574 }
1575 break;
1576 default:
Anatolij Gustschin8e024942008-08-29 21:04:45 +02001577 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001578 goto error;
1579 }
1580
1581 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1582
1583 } else if (images->legacy_hdr_valid &&
1584 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1585
1586 ulong fdt_data, fdt_len;
1587
1588 /*
1589 * Now check if we have a legacy multi-component image,
1590 * get second entry data start address and len.
1591 */
1592 printf ("## Flattened Device Tree from multi "
1593 "component Image at %08lX\n",
1594 (ulong)images->legacy_hdr_os);
1595
1596 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1597 if (fdt_len) {
1598
1599 fdt_blob = (char *)fdt_data;
1600 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1601
1602 if (fdt_check_header (fdt_blob) != 0) {
1603 fdt_error ("image is not a fdt");
1604 goto error;
1605 }
1606
John Rigbyd1263fc2010-10-13 13:57:32 -06001607 if (fdt_totalsize(fdt_blob) != fdt_len) {
Kumar Gala06a09912008-08-15 08:24:38 -05001608 fdt_error ("fdt size != image size");
1609 goto error;
1610 }
1611 } else {
1612 debug ("## No Flattened Device Tree\n");
1613 return 0;
1614 }
1615 } else {
1616 debug ("## No Flattened Device Tree\n");
1617 return 0;
1618 }
1619
1620 *of_flat_tree = fdt_blob;
John Rigbyd1263fc2010-10-13 13:57:32 -06001621 *of_size = fdt_totalsize(fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001622 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner52514692008-08-21 07:12:26 -07001623 (ulong)*of_flat_tree, *of_size);
Kumar Gala06a09912008-08-15 08:24:38 -05001624
1625 return 0;
1626
1627error:
1628 *of_flat_tree = 0;
1629 *of_size = 0;
1630 return 1;
1631}
1632#endif /* CONFIG_OF_LIBFDT */
1633
John Rigbyfca43cc2010-10-13 13:57:35 -06001634#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001635/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001636 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001637 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001638 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1639 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1640 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001641 * boot_get_cmdline() allocates space for kernel command line below
Grant Likely590d3ca2011-03-28 09:58:34 +00001642 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001643 * variable is present its contents is copied to allocated kernel
1644 * command line.
1645 *
1646 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001647 * 0 - success
1648 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001649 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001650int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001651{
1652 char *cmdline;
1653 char *s;
1654
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001655 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001656 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001657
1658 if (cmdline == NULL)
1659 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001660
1661 if ((s = getenv("bootargs")) == NULL)
1662 s = "";
1663
1664 strcpy(cmdline, s);
1665
1666 *cmd_start = (ulong) & cmdline[0];
1667 *cmd_end = *cmd_start + strlen(cmdline);
1668
1669 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1670
Kumar Galae822d7f2008-02-27 21:51:49 -06001671 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001672}
John Rigbyfca43cc2010-10-13 13:57:35 -06001673#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001674
John Rigbyfca43cc2010-10-13 13:57:35 -06001675#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001676/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001677 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001678 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001679 * @kbd: double pointer to board info data
1680 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001681 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likely590d3ca2011-03-28 09:58:34 +00001682 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1683 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001684 *
1685 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001686 * 0 - success
1687 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001688 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001689int boot_get_kbd (struct lmb *lmb, bd_t **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001690{
Becky Bruce391fd932008-06-09 20:37:18 -05001691 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001692 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001693 if (*kbd == NULL)
1694 return -1;
1695
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001696 **kbd = *(gd->bd);
1697
1698 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1699
1700#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1701 do_bdinfo(NULL, 0, 0, NULL);
1702#endif
1703
Kumar Galae822d7f2008-02-27 21:51:49 -06001704 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001705}
John Rigbyfca43cc2010-10-13 13:57:35 -06001706#endif /* CONFIG_SYS_BOOT_GET_KBD */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001707#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001708
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001709#if defined(CONFIG_FIT)
1710/*****************************************************************************/
1711/* New uImage format routines */
1712/*****************************************************************************/
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001713#ifndef USE_HOSTCC
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001714static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1715 ulong *addr, const char **name)
1716{
1717 const char *sep;
1718
1719 *addr = addr_curr;
1720 *name = NULL;
1721
1722 sep = strchr (spec, sepc);
1723 if (sep) {
1724 if (sep - spec > 0)
1725 *addr = simple_strtoul (spec, NULL, 16);
1726
1727 *name = sep + 1;
1728 return 1;
1729 }
1730
1731 return 0;
1732}
1733
1734/**
1735 * fit_parse_conf - parse FIT configuration spec
1736 * @spec: input string, containing configuration spec
1737 * @add_curr: current image address (to be used as a possible default)
1738 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1739 * configuration
1740 * @conf_name double pointer to a char, will hold pointer to a configuration
1741 * unit name
1742 *
1743 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1744 * where <addr> is a FIT image address that contains configuration
1745 * with a <conf> unit name.
1746 *
1747 * Address part is optional, and if omitted default add_curr will
1748 * be used instead.
1749 *
1750 * returns:
1751 * 1 if spec is a valid configuration string,
1752 * addr and conf_name are set accordingly
1753 * 0 otherwise
1754 */
1755inline int fit_parse_conf (const char *spec, ulong addr_curr,
1756 ulong *addr, const char **conf_name)
1757{
1758 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1759}
1760
1761/**
1762 * fit_parse_subimage - parse FIT subimage spec
1763 * @spec: input string, containing subimage spec
1764 * @add_curr: current image address (to be used as a possible default)
1765 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1766 * subimage
1767 * @image_name: double pointer to a char, will hold pointer to a subimage name
1768 *
1769 * fit_parse_subimage() expects subimage spec in the for of
1770 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1771 * subimage with a <subimg> unit name.
1772 *
1773 * Address part is optional, and if omitted default add_curr will
1774 * be used instead.
1775 *
1776 * returns:
1777 * 1 if spec is a valid subimage string,
1778 * addr and image_name are set accordingly
1779 * 0 otherwise
1780 */
1781inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1782 ulong *addr, const char **image_name)
1783{
1784 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1785}
Marian Balakowicz570abb02008-02-29 15:59:59 +01001786#endif /* !USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001787
1788static void fit_get_debug (const void *fit, int noffset,
1789 char *prop_name, int err)
1790{
1791 debug ("Can't get '%s' property from FIT 0x%08lx, "
1792 "node: offset %d, name %s (%s)\n",
1793 prop_name, (ulong)fit, noffset,
1794 fit_get_name (fit, noffset, NULL),
1795 fdt_strerror (err));
1796}
1797
1798/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001799 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001800 * @fit: pointer to the FIT format image header
1801 * @p: pointer to prefix string
1802 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001803 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001804 * The routine prints out FIT image properties (root node level) follwed by
1805 * the details of each component image.
1806 *
1807 * returns:
1808 * no returned results
1809 */
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001810void fit_print_contents (const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001811{
1812 char *desc;
1813 char *uname;
1814 int images_noffset;
1815 int confs_noffset;
1816 int noffset;
1817 int ndepth;
1818 int count = 0;
1819 int ret;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001820 const char *p;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001821#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1822 time_t timestamp;
1823#endif
1824
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001825#ifdef USE_HOSTCC
1826 p = "";
1827#else
1828 p = " ";
1829#endif
1830
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001831 /* Root node properties */
1832 ret = fit_get_desc (fit, 0, &desc);
1833 printf ("%sFIT description: ", p);
1834 if (ret)
1835 printf ("unavailable\n");
1836 else
1837 printf ("%s\n", desc);
1838
1839#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1840 ret = fit_get_timestamp (fit, 0, &timestamp);
1841 printf ("%sCreated: ", p);
1842 if (ret)
1843 printf ("unavailable\n");
1844 else
1845 genimg_print_time (timestamp);
1846#endif
1847
1848 /* Find images parent node offset */
1849 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1850 if (images_noffset < 0) {
1851 printf ("Can't find images parent node '%s' (%s)\n",
1852 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1853 return;
1854 }
1855
1856 /* Process its subnodes, print out component images details */
1857 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1858 (noffset >= 0) && (ndepth > 0);
1859 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1860 if (ndepth == 1) {
1861 /*
1862 * Direct child node of the images parent node,
1863 * i.e. component image node.
1864 */
1865 printf ("%s Image %u (%s)\n", p, count++,
1866 fit_get_name(fit, noffset, NULL));
1867
1868 fit_image_print (fit, noffset, p);
1869 }
1870 }
1871
1872 /* Find configurations parent node offset */
1873 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1874 if (confs_noffset < 0) {
1875 debug ("Can't get configurations parent node '%s' (%s)\n",
1876 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1877 return;
1878 }
1879
1880 /* get default configuration unit name from default property */
1881 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1882 if (uname)
1883 printf ("%s Default Configuration: '%s'\n", p, uname);
1884
1885 /* Process its subnodes, print out configurations details */
1886 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1887 (noffset >= 0) && (ndepth > 0);
1888 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1889 if (ndepth == 1) {
1890 /*
1891 * Direct child node of the configurations parent node,
1892 * i.e. configuration node.
1893 */
1894 printf ("%s Configuration %u (%s)\n", p, count++,
1895 fit_get_name(fit, noffset, NULL));
1896
1897 fit_conf_print (fit, noffset, p);
1898 }
1899 }
1900}
1901
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001902/**
1903 * fit_image_print - prints out the FIT component image details
1904 * @fit: pointer to the FIT format image header
1905 * @image_noffset: offset of the component image node
1906 * @p: pointer to prefix string
1907 *
1908 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001909 * image. If present, hash nodes are printed out as well. Load
1910 * address for images of type firmware is also printed out. Since the load
1911 * address is not mandatory for firmware images, it will be output as
1912 * "unavailable" when not present.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001913 *
1914 * returns:
1915 * no returned results
1916 */
1917void fit_image_print (const void *fit, int image_noffset, const char *p)
1918{
1919 char *desc;
1920 uint8_t type, arch, os, comp;
1921 size_t size;
1922 ulong load, entry;
1923 const void *data;
1924 int noffset;
1925 int ndepth;
1926 int ret;
1927
1928 /* Mandatory properties */
1929 ret = fit_get_desc (fit, image_noffset, &desc);
1930 printf ("%s Description: ", p);
1931 if (ret)
1932 printf ("unavailable\n");
1933 else
1934 printf ("%s\n", desc);
1935
1936 fit_image_get_type (fit, image_noffset, &type);
1937 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1938
1939 fit_image_get_comp (fit, image_noffset, &comp);
1940 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1941
1942 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1943
1944#ifndef USE_HOSTCC
1945 printf ("%s Data Start: ", p);
1946 if (ret)
1947 printf ("unavailable\n");
1948 else
1949 printf ("0x%08lx\n", (ulong)data);
1950#endif
1951
1952 printf ("%s Data Size: ", p);
1953 if (ret)
1954 printf ("unavailable\n");
1955 else
1956 genimg_print_size (size);
1957
1958 /* Remaining, type dependent properties */
1959 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1960 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1961 (type == IH_TYPE_FLATDT)) {
1962 fit_image_get_arch (fit, image_noffset, &arch);
1963 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1964 }
1965
1966 if (type == IH_TYPE_KERNEL) {
1967 fit_image_get_os (fit, image_noffset, &os);
1968 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1969 }
1970
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001971 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1972 (type == IH_TYPE_FIRMWARE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001973 ret = fit_image_get_load (fit, image_noffset, &load);
1974 printf ("%s Load Address: ", p);
1975 if (ret)
1976 printf ("unavailable\n");
1977 else
1978 printf ("0x%08lx\n", load);
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001979 }
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001980
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001981 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001982 fit_image_get_entry (fit, image_noffset, &entry);
1983 printf ("%s Entry Point: ", p);
1984 if (ret)
1985 printf ("unavailable\n");
1986 else
1987 printf ("0x%08lx\n", entry);
1988 }
1989
1990 /* Process all hash subnodes of the component image node */
1991 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1992 (noffset >= 0) && (ndepth > 0);
1993 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1994 if (ndepth == 1) {
1995 /* Direct child node of the component image node */
1996 fit_image_print_hash (fit, noffset, p);
1997 }
1998 }
1999}
2000
2001/**
2002 * fit_image_print_hash - prints out the hash node details
2003 * @fit: pointer to the FIT format image header
2004 * @noffset: offset of the hash node
2005 * @p: pointer to prefix string
2006 *
2007 * fit_image_print_hash() lists properies for the processed hash node
2008 *
2009 * returns:
2010 * no returned results
2011 */
2012void fit_image_print_hash (const void *fit, int noffset, const char *p)
2013{
2014 char *algo;
2015 uint8_t *value;
2016 int value_len;
2017 int i, ret;
2018
2019 /*
2020 * Check subnode name, must be equal to "hash".
2021 * Multiple hash nodes require unique unit node
2022 * names, e.g. hash@1, hash@2, etc.
2023 */
2024 if (strncmp (fit_get_name(fit, noffset, NULL),
2025 FIT_HASH_NODENAME,
2026 strlen(FIT_HASH_NODENAME)) != 0)
2027 return;
2028
2029 debug ("%s Hash node: '%s'\n", p,
2030 fit_get_name (fit, noffset, NULL));
2031
2032 printf ("%s Hash algo: ", p);
2033 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2034 printf ("invalid/unsupported\n");
2035 return;
2036 }
2037 printf ("%s\n", algo);
2038
2039 ret = fit_image_hash_get_value (fit, noffset, &value,
2040 &value_len);
2041 printf ("%s Hash value: ", p);
2042 if (ret) {
2043 printf ("unavailable\n");
2044 } else {
2045 for (i = 0; i < value_len; i++)
2046 printf ("%02x", value[i]);
2047 printf ("\n");
2048 }
2049
2050 debug ("%s Hash len: %d\n", p, value_len);
2051}
2052
2053/**
2054 * fit_get_desc - get node description property
2055 * @fit: pointer to the FIT format image header
2056 * @noffset: node offset
2057 * @desc: double pointer to the char, will hold pointer to the descrption
2058 *
2059 * fit_get_desc() reads description property from a given node, if
2060 * description is found pointer to it is returened in third call argument.
2061 *
2062 * returns:
2063 * 0, on success
2064 * -1, on failure
2065 */
2066int fit_get_desc (const void *fit, int noffset, char **desc)
2067{
2068 int len;
2069
2070 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2071 if (*desc == NULL) {
2072 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2073 return -1;
2074 }
2075
2076 return 0;
2077}
2078
2079/**
2080 * fit_get_timestamp - get node timestamp property
2081 * @fit: pointer to the FIT format image header
2082 * @noffset: node offset
2083 * @timestamp: pointer to the time_t, will hold read timestamp
2084 *
2085 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2086 * is found and has a correct size its value is retured in third call
2087 * argument.
2088 *
2089 * returns:
2090 * 0, on success
2091 * -1, on property read failure
2092 * -2, on wrong timestamp size
2093 */
2094int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2095{
2096 int len;
2097 const void *data;
2098
2099 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2100 if (data == NULL) {
2101 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2102 return -1;
2103 }
2104 if (len != sizeof (uint32_t)) {
2105 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2106 return -2;
2107 }
2108
2109 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2110 return 0;
2111}
2112
2113/**
2114 * fit_image_get_node - get node offset for component image of a given unit name
2115 * @fit: pointer to the FIT format image header
2116 * @image_uname: component image node unit name
2117 *
2118 * fit_image_get_node() finds a component image (withing the '/images'
2119 * node) of a provided unit name. If image is found its node offset is
2120 * returned to the caller.
2121 *
2122 * returns:
2123 * image node offset when found (>=0)
2124 * negative number on failure (FDT_ERR_* code)
2125 */
2126int fit_image_get_node (const void *fit, const char *image_uname)
2127{
2128 int noffset, images_noffset;
2129
2130 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2131 if (images_noffset < 0) {
2132 debug ("Can't find images parent node '%s' (%s)\n",
2133 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2134 return images_noffset;
2135 }
2136
2137 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2138 if (noffset < 0) {
2139 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2140 image_uname, fdt_strerror (noffset));
2141 }
2142
2143 return noffset;
2144}
2145
2146/**
2147 * fit_image_get_os - get os id for a given component image node
2148 * @fit: pointer to the FIT format image header
2149 * @noffset: component image node offset
2150 * @os: pointer to the uint8_t, will hold os numeric id
2151 *
2152 * fit_image_get_os() finds os property in a given component image node.
2153 * If the property is found, its (string) value is translated to the numeric
2154 * id which is returned to the caller.
2155 *
2156 * returns:
2157 * 0, on success
2158 * -1, on failure
2159 */
2160int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2161{
2162 int len;
2163 const void *data;
2164
2165 /* Get OS name from property data */
2166 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2167 if (data == NULL) {
2168 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2169 *os = -1;
2170 return -1;
2171 }
2172
2173 /* Translate OS name to id */
2174 *os = genimg_get_os_id (data);
2175 return 0;
2176}
2177
2178/**
2179 * fit_image_get_arch - get arch id for a given component image node
2180 * @fit: pointer to the FIT format image header
2181 * @noffset: component image node offset
2182 * @arch: pointer to the uint8_t, will hold arch numeric id
2183 *
2184 * fit_image_get_arch() finds arch property in a given component image node.
2185 * If the property is found, its (string) value is translated to the numeric
2186 * id which is returned to the caller.
2187 *
2188 * returns:
2189 * 0, on success
2190 * -1, on failure
2191 */
2192int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2193{
2194 int len;
2195 const void *data;
2196
2197 /* Get architecture name from property data */
2198 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2199 if (data == NULL) {
2200 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2201 *arch = -1;
2202 return -1;
2203 }
2204
2205 /* Translate architecture name to id */
2206 *arch = genimg_get_arch_id (data);
2207 return 0;
2208}
2209
2210/**
2211 * fit_image_get_type - get type id for a given component image node
2212 * @fit: pointer to the FIT format image header
2213 * @noffset: component image node offset
2214 * @type: pointer to the uint8_t, will hold type numeric id
2215 *
2216 * fit_image_get_type() finds type property in a given component image node.
2217 * If the property is found, its (string) value is translated to the numeric
2218 * id which is returned to the caller.
2219 *
2220 * returns:
2221 * 0, on success
2222 * -1, on failure
2223 */
2224int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2225{
2226 int len;
2227 const void *data;
2228
2229 /* Get image type name from property data */
2230 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2231 if (data == NULL) {
2232 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2233 *type = -1;
2234 return -1;
2235 }
2236
2237 /* Translate image type name to id */
2238 *type = genimg_get_type_id (data);
2239 return 0;
2240}
2241
2242/**
2243 * fit_image_get_comp - get comp id for a given component image node
2244 * @fit: pointer to the FIT format image header
2245 * @noffset: component image node offset
2246 * @comp: pointer to the uint8_t, will hold comp numeric id
2247 *
2248 * fit_image_get_comp() finds comp property in a given component image node.
2249 * If the property is found, its (string) value is translated to the numeric
2250 * id which is returned to the caller.
2251 *
2252 * returns:
2253 * 0, on success
2254 * -1, on failure
2255 */
2256int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2257{
2258 int len;
2259 const void *data;
2260
2261 /* Get compression name from property data */
2262 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2263 if (data == NULL) {
2264 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2265 *comp = -1;
2266 return -1;
2267 }
2268
2269 /* Translate compression name to id */
2270 *comp = genimg_get_comp_id (data);
2271 return 0;
2272}
2273
2274/**
2275 * fit_image_get_load - get load address property for a given component image node
2276 * @fit: pointer to the FIT format image header
2277 * @noffset: component image node offset
2278 * @load: pointer to the uint32_t, will hold load address
2279 *
2280 * fit_image_get_load() finds load address property in a given component image node.
2281 * If the property is found, its value is returned to the caller.
2282 *
2283 * returns:
2284 * 0, on success
2285 * -1, on failure
2286 */
2287int fit_image_get_load (const void *fit, int noffset, ulong *load)
2288{
2289 int len;
2290 const uint32_t *data;
2291
2292 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2293 if (data == NULL) {
2294 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2295 return -1;
2296 }
2297
2298 *load = uimage_to_cpu (*data);
2299 return 0;
2300}
2301
2302/**
2303 * fit_image_get_entry - get entry point address property for a given component image node
2304 * @fit: pointer to the FIT format image header
2305 * @noffset: component image node offset
2306 * @entry: pointer to the uint32_t, will hold entry point address
2307 *
2308 * fit_image_get_entry() finds entry point address property in a given component image node.
2309 * If the property is found, its value is returned to the caller.
2310 *
2311 * returns:
2312 * 0, on success
2313 * -1, on failure
2314 */
2315int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2316{
2317 int len;
2318 const uint32_t *data;
2319
2320 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2321 if (data == NULL) {
2322 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2323 return -1;
2324 }
2325
2326 *entry = uimage_to_cpu (*data);
2327 return 0;
2328}
2329
2330/**
2331 * fit_image_get_data - get data property and its size for a given component image node
2332 * @fit: pointer to the FIT format image header
2333 * @noffset: component image node offset
2334 * @data: double pointer to void, will hold data property's data address
2335 * @size: pointer to size_t, will hold data property's data size
2336 *
2337 * fit_image_get_data() finds data property in a given component image node.
2338 * If the property is found its data start address and size are returned to
2339 * the caller.
2340 *
2341 * returns:
2342 * 0, on success
2343 * -1, on failure
2344 */
2345int fit_image_get_data (const void *fit, int noffset,
2346 const void **data, size_t *size)
2347{
2348 int len;
2349
2350 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2351 if (*data == NULL) {
2352 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2353 *size = 0;
2354 return -1;
2355 }
2356
2357 *size = len;
2358 return 0;
2359}
2360
2361/**
2362 * fit_image_hash_get_algo - get hash algorithm name
2363 * @fit: pointer to the FIT format image header
2364 * @noffset: hash node offset
2365 * @algo: double pointer to char, will hold pointer to the algorithm name
2366 *
2367 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2368 * If the property is found its data start address is returned to the caller.
2369 *
2370 * returns:
2371 * 0, on success
2372 * -1, on failure
2373 */
2374int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2375{
2376 int len;
2377
2378 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2379 if (*algo == NULL) {
2380 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2381 return -1;
2382 }
2383
2384 return 0;
2385}
2386
2387/**
2388 * fit_image_hash_get_value - get hash value and length
2389 * @fit: pointer to the FIT format image header
2390 * @noffset: hash node offset
2391 * @value: double pointer to uint8_t, will hold address of a hash value data
2392 * @value_len: pointer to an int, will hold hash data length
2393 *
2394 * fit_image_hash_get_value() finds hash value property in a given hash node.
2395 * If the property is found its data start address and size are returned to
2396 * the caller.
2397 *
2398 * returns:
2399 * 0, on success
2400 * -1, on failure
2401 */
2402int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2403 int *value_len)
2404{
2405 int len;
2406
2407 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2408 if (*value == NULL) {
2409 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2410 *value_len = 0;
2411 return -1;
2412 }
2413
2414 *value_len = len;
2415 return 0;
2416}
2417
2418/**
2419 * fit_set_timestamp - set node timestamp property
2420 * @fit: pointer to the FIT format image header
2421 * @noffset: node offset
2422 * @timestamp: timestamp value to be set
2423 *
2424 * fit_set_timestamp() attempts to set timestamp property in the requested
2425 * node and returns operation status to the caller.
2426 *
2427 * returns:
2428 * 0, on success
2429 * -1, on property read failure
2430 */
2431int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2432{
2433 uint32_t t;
2434 int ret;
2435
2436 t = cpu_to_uimage (timestamp);
2437 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2438 sizeof (uint32_t));
2439 if (ret) {
2440 printf ("Can't set '%s' property for '%s' node (%s)\n",
2441 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2442 fdt_strerror (ret));
2443 return -1;
2444 }
2445
2446 return 0;
2447}
2448
2449/**
2450 * calculate_hash - calculate and return hash for provided input data
2451 * @data: pointer to the input data
2452 * @data_len: data length
2453 * @algo: requested hash algorithm
2454 * @value: pointer to the char, will hold hash value data (caller must
2455 * allocate enough free space)
2456 * value_len: length of the calculated hash
2457 *
2458 * calculate_hash() computes input data hash according to the requested algorithm.
2459 * Resulting hash value is placed in caller provided 'value' buffer, length
2460 * of the calculated hash is returned via value_len pointer argument.
2461 *
2462 * returns:
2463 * 0, on success
2464 * -1, when algo is unsupported
2465 */
2466static int calculate_hash (const void *data, int data_len, const char *algo,
2467 uint8_t *value, int *value_len)
2468{
2469 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002470 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2471 CHUNKSZ_CRC32);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002472 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2473 *value_len = 4;
2474 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002475 sha1_csum_wd ((unsigned char *) data, data_len,
2476 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002477 *value_len = 20;
2478 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002479 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka766529f2008-03-14 16:22:34 +01002480 *value_len = 16;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002481 } else {
2482 debug ("Unsupported hash alogrithm\n");
2483 return -1;
2484 }
2485 return 0;
2486}
2487
2488#ifdef USE_HOSTCC
2489/**
2490 * fit_set_hashes - process FIT component image nodes and calculate hashes
2491 * @fit: pointer to the FIT format image header
2492 *
2493 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2494 * Hashes are calculated for all component images which have hash subnodes
2495 * with algorithm property set to one of the supported hash algorithms.
2496 *
2497 * returns
2498 * 0, on success
2499 * libfdt error code, on failure
2500 */
2501int fit_set_hashes (void *fit)
2502{
2503 int images_noffset;
2504 int noffset;
2505 int ndepth;
2506 int ret;
2507
2508 /* Find images parent node offset */
2509 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2510 if (images_noffset < 0) {
2511 printf ("Can't find images parent node '%s' (%s)\n",
2512 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2513 return images_noffset;
2514 }
2515
2516 /* Process its subnodes, print out component images details */
2517 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2518 (noffset >= 0) && (ndepth > 0);
2519 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2520 if (ndepth == 1) {
2521 /*
2522 * Direct child node of the images parent node,
2523 * i.e. component image node.
2524 */
2525 ret = fit_image_set_hashes (fit, noffset);
2526 if (ret)
2527 return ret;
2528 }
2529 }
2530
2531 return 0;
2532}
2533
2534/**
2535 * fit_image_set_hashes - calculate/set hashes for given component image node
2536 * @fit: pointer to the FIT format image header
2537 * @image_noffset: requested component image node
2538 *
2539 * fit_image_set_hashes() adds hash values for an component image node. All
2540 * existing hash subnodes are checked, if algorithm property is set to one of
2541 * the supported hash algorithms, hash value is computed and corresponding
2542 * hash node property is set, for example:
2543 *
2544 * Input component image node structure:
2545 *
2546 * o image@1 (at image_noffset)
2547 * | - data = [binary data]
2548 * o hash@1
2549 * |- algo = "sha1"
2550 *
2551 * Output component image node structure:
2552 *
2553 * o image@1 (at image_noffset)
2554 * | - data = [binary data]
2555 * o hash@1
2556 * |- algo = "sha1"
2557 * |- value = sha1(data)
2558 *
2559 * returns:
2560 * 0 on sucess
2561 * <0 on failure
2562 */
2563int fit_image_set_hashes (void *fit, int image_noffset)
2564{
2565 const void *data;
2566 size_t size;
2567 char *algo;
2568 uint8_t value[FIT_MAX_HASH_LEN];
2569 int value_len;
2570 int noffset;
2571 int ndepth;
2572
2573 /* Get image data and data length */
2574 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2575 printf ("Can't get image data/size\n");
2576 return -1;
2577 }
2578
2579 /* Process all hash subnodes of the component image node */
2580 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2581 (noffset >= 0) && (ndepth > 0);
2582 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2583 if (ndepth == 1) {
2584 /* Direct child node of the component image node */
2585
2586 /*
2587 * Check subnode name, must be equal to "hash".
2588 * Multiple hash nodes require unique unit node
2589 * names, e.g. hash@1, hash@2, etc.
2590 */
2591 if (strncmp (fit_get_name(fit, noffset, NULL),
2592 FIT_HASH_NODENAME,
2593 strlen(FIT_HASH_NODENAME)) != 0) {
2594 /* Not a hash subnode, skip it */
2595 continue;
2596 }
2597
2598 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2599 printf ("Can't get hash algo property for "
2600 "'%s' hash node in '%s' image node\n",
2601 fit_get_name (fit, noffset, NULL),
2602 fit_get_name (fit, image_noffset, NULL));
2603 return -1;
2604 }
2605
2606 if (calculate_hash (data, size, algo, value, &value_len)) {
2607 printf ("Unsupported hash algorithm (%s) for "
2608 "'%s' hash node in '%s' image node\n",
2609 algo, fit_get_name (fit, noffset, NULL),
2610 fit_get_name (fit, image_noffset, NULL));
2611 return -1;
2612 }
2613
2614 if (fit_image_hash_set_value (fit, noffset, value,
2615 value_len)) {
2616 printf ("Can't set hash value for "
2617 "'%s' hash node in '%s' image node\n",
2618 fit_get_name (fit, noffset, NULL),
2619 fit_get_name (fit, image_noffset, NULL));
2620 return -1;
2621 }
2622 }
2623 }
2624
2625 return 0;
2626}
2627
2628/**
2629 * fit_image_hash_set_value - set hash value in requested has node
2630 * @fit: pointer to the FIT format image header
2631 * @noffset: hash node offset
2632 * @value: hash value to be set
2633 * @value_len: hash value length
2634 *
2635 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2636 * given and returns operation status to the caller.
2637 *
2638 * returns
2639 * 0, on success
2640 * -1, on failure
2641 */
2642int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2643 int value_len)
2644{
2645 int ret;
2646
2647 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2648 if (ret) {
2649 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2650 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2651 fdt_strerror (ret));
2652 return -1;
2653 }
2654
2655 return 0;
2656}
2657#endif /* USE_HOSTCC */
2658
2659/**
2660 * fit_image_check_hashes - verify data intergity
2661 * @fit: pointer to the FIT format image header
2662 * @image_noffset: component image node offset
2663 *
2664 * fit_image_check_hashes() goes over component image hash nodes,
2665 * re-calculates each data hash and compares with the value stored in hash
2666 * node.
2667 *
2668 * returns:
2669 * 1, if all hashes are valid
2670 * 0, otherwise (or on error)
2671 */
2672int fit_image_check_hashes (const void *fit, int image_noffset)
2673{
2674 const void *data;
2675 size_t size;
2676 char *algo;
2677 uint8_t *fit_value;
2678 int fit_value_len;
2679 uint8_t value[FIT_MAX_HASH_LEN];
2680 int value_len;
2681 int noffset;
2682 int ndepth;
2683 char *err_msg = "";
2684
2685 /* Get image data and data length */
2686 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2687 printf ("Can't get image data/size\n");
2688 return 0;
2689 }
2690
2691 /* Process all hash subnodes of the component image node */
2692 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2693 (noffset >= 0) && (ndepth > 0);
2694 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2695 if (ndepth == 1) {
2696 /* Direct child node of the component image node */
2697
2698 /*
2699 * Check subnode name, must be equal to "hash".
2700 * Multiple hash nodes require unique unit node
2701 * names, e.g. hash@1, hash@2, etc.
2702 */
2703 if (strncmp (fit_get_name(fit, noffset, NULL),
2704 FIT_HASH_NODENAME,
2705 strlen(FIT_HASH_NODENAME)) != 0)
2706 continue;
2707
2708 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002709 err_msg = " error!\nCan't get hash algo "
2710 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002711 goto error;
2712 }
2713 printf ("%s", algo);
2714
2715 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2716 &fit_value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002717 err_msg = " error!\nCan't get hash value "
2718 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002719 goto error;
2720 }
2721
2722 if (calculate_hash (data, size, algo, value, &value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002723 err_msg = " error!\nUnsupported hash algorithm";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002724 goto error;
2725 }
2726
2727 if (value_len != fit_value_len) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002728 err_msg = " error !\nBad hash value len";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002729 goto error;
2730 } else if (memcmp (value, fit_value, value_len) != 0) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002731 err_msg = " error!\nBad hash value";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002732 goto error;
2733 }
2734 printf ("+ ");
2735 }
2736 }
2737
2738 return 1;
2739
2740error:
2741 printf ("%s for '%s' hash node in '%s' image node\n",
2742 err_msg, fit_get_name (fit, noffset, NULL),
2743 fit_get_name (fit, image_noffset, NULL));
2744 return 0;
2745}
2746
2747/**
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002748 * fit_all_image_check_hashes - verify data intergity for all images
2749 * @fit: pointer to the FIT format image header
2750 *
2751 * fit_all_image_check_hashes() goes over all images in the FIT and
2752 * for every images checks if all it's hashes are valid.
2753 *
2754 * returns:
2755 * 1, if all hashes of all images are valid
2756 * 0, otherwise (or on error)
2757 */
2758int fit_all_image_check_hashes (const void *fit)
2759{
2760 int images_noffset;
2761 int noffset;
2762 int ndepth;
2763 int count;
2764
2765 /* Find images parent node offset */
2766 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2767 if (images_noffset < 0) {
2768 printf ("Can't find images parent node '%s' (%s)\n",
2769 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2770 return 0;
2771 }
2772
2773 /* Process all image subnodes, check hashes for each */
2774 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2775 (ulong)fit);
2776 for (ndepth = 0, count = 0,
2777 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2778 (noffset >= 0) && (ndepth > 0);
2779 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2780 if (ndepth == 1) {
2781 /*
2782 * Direct child node of the images parent node,
2783 * i.e. component image node.
2784 */
2785 printf (" Hash(es) for Image %u (%s): ", count++,
2786 fit_get_name (fit, noffset, NULL));
2787
2788 if (!fit_image_check_hashes (fit, noffset))
2789 return 0;
2790 printf ("\n");
2791 }
2792 }
2793 return 1;
2794}
2795
2796/**
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002797 * fit_image_check_os - check whether image node is of a given os type
2798 * @fit: pointer to the FIT format image header
2799 * @noffset: component image node offset
2800 * @os: requested image os
2801 *
2802 * fit_image_check_os() reads image os property and compares its numeric
2803 * id with the requested os. Comparison result is returned to the caller.
2804 *
2805 * returns:
2806 * 1 if image is of given os type
2807 * 0 otherwise (or on error)
2808 */
2809int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2810{
2811 uint8_t image_os;
2812
2813 if (fit_image_get_os (fit, noffset, &image_os))
2814 return 0;
2815 return (os == image_os);
2816}
2817
2818/**
2819 * fit_image_check_arch - check whether image node is of a given arch
2820 * @fit: pointer to the FIT format image header
2821 * @noffset: component image node offset
2822 * @arch: requested imagearch
2823 *
2824 * fit_image_check_arch() reads image arch property and compares its numeric
2825 * id with the requested arch. Comparison result is returned to the caller.
2826 *
2827 * returns:
2828 * 1 if image is of given arch
2829 * 0 otherwise (or on error)
2830 */
2831int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2832{
2833 uint8_t image_arch;
2834
2835 if (fit_image_get_arch (fit, noffset, &image_arch))
2836 return 0;
2837 return (arch == image_arch);
2838}
2839
2840/**
2841 * fit_image_check_type - check whether image node is of a given type
2842 * @fit: pointer to the FIT format image header
2843 * @noffset: component image node offset
2844 * @type: requested image type
2845 *
2846 * fit_image_check_type() reads image type property and compares its numeric
2847 * id with the requested type. Comparison result is returned to the caller.
2848 *
2849 * returns:
2850 * 1 if image is of given type
2851 * 0 otherwise (or on error)
2852 */
2853int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2854{
2855 uint8_t image_type;
2856
2857 if (fit_image_get_type (fit, noffset, &image_type))
2858 return 0;
2859 return (type == image_type);
2860}
2861
2862/**
2863 * fit_image_check_comp - check whether image node uses given compression
2864 * @fit: pointer to the FIT format image header
2865 * @noffset: component image node offset
2866 * @comp: requested image compression type
2867 *
2868 * fit_image_check_comp() reads image compression property and compares its
2869 * numeric id with the requested compression type. Comparison result is
2870 * returned to the caller.
2871 *
2872 * returns:
2873 * 1 if image uses requested compression
2874 * 0 otherwise (or on error)
2875 */
2876int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2877{
2878 uint8_t image_comp;
2879
2880 if (fit_image_get_comp (fit, noffset, &image_comp))
2881 return 0;
2882 return (comp == image_comp);
2883}
2884
2885/**
2886 * fit_check_format - sanity check FIT image format
2887 * @fit: pointer to the FIT format image header
2888 *
2889 * fit_check_format() runs a basic sanity FIT image verification.
2890 * Routine checks for mandatory properties, nodes, etc.
2891 *
2892 * returns:
2893 * 1, on success
2894 * 0, on failure
2895 */
2896int fit_check_format (const void *fit)
2897{
2898 /* mandatory / node 'description' property */
2899 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2900 debug ("Wrong FIT format: no description\n");
2901 return 0;
2902 }
2903
2904#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2905 /* mandatory / node 'timestamp' property */
2906 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002907 debug ("Wrong FIT format: no timestamp\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002908 return 0;
2909 }
2910#endif
2911
2912 /* mandatory subimages parent '/images' node */
2913 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2914 debug ("Wrong FIT format: no images parent node\n");
2915 return 0;
2916 }
2917
2918 return 1;
2919}
2920
2921/**
2922 * fit_conf_get_node - get node offset for configuration of a given unit name
2923 * @fit: pointer to the FIT format image header
2924 * @conf_uname: configuration node unit name
2925 *
2926 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2927 * parant node) of a provided unit name. If configuration is found its node offset
2928 * is returned to the caller.
2929 *
2930 * When NULL is provided in second argument fit_conf_get_node() will search
2931 * for a default configuration node instead. Default configuration node unit name
2932 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2933 *
2934 * returns:
2935 * configuration node offset when found (>=0)
2936 * negative number on failure (FDT_ERR_* code)
2937 */
2938int fit_conf_get_node (const void *fit, const char *conf_uname)
2939{
2940 int noffset, confs_noffset;
2941 int len;
2942
2943 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2944 if (confs_noffset < 0) {
2945 debug ("Can't find configurations parent node '%s' (%s)\n",
2946 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2947 return confs_noffset;
2948 }
2949
2950 if (conf_uname == NULL) {
2951 /* get configuration unit name from the default property */
2952 debug ("No configuration specified, trying default...\n");
2953 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2954 if (conf_uname == NULL) {
2955 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2956 return len;
2957 }
2958 debug ("Found default configuration: '%s'\n", conf_uname);
2959 }
2960
2961 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2962 if (noffset < 0) {
2963 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2964 conf_uname, fdt_strerror (noffset));
2965 }
2966
2967 return noffset;
2968}
2969
2970static int __fit_conf_get_prop_node (const void *fit, int noffset,
2971 const char *prop_name)
2972{
2973 char *uname;
2974 int len;
2975
2976 /* get kernel image unit name from configuration kernel property */
2977 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2978 if (uname == NULL)
2979 return len;
2980
2981 return fit_image_get_node (fit, uname);
2982}
2983
2984/**
2985 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2986 * a given configuration
2987 * @fit: pointer to the FIT format image header
2988 * @noffset: configuration node offset
2989 *
2990 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2991 * configuration FIT_KERNEL_PROP property and translates it to the node
2992 * offset.
2993 *
2994 * returns:
2995 * image node offset when found (>=0)
2996 * negative number on failure (FDT_ERR_* code)
2997 */
2998int fit_conf_get_kernel_node (const void *fit, int noffset)
2999{
3000 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
3001}
3002
3003/**
3004 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3005 * a given configuration
3006 * @fit: pointer to the FIT format image header
3007 * @noffset: configuration node offset
3008 *
3009 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3010 * configuration FIT_KERNEL_PROP property and translates it to the node
3011 * offset.
3012 *
3013 * returns:
3014 * image node offset when found (>=0)
3015 * negative number on failure (FDT_ERR_* code)
3016 */
3017int fit_conf_get_ramdisk_node (const void *fit, int noffset)
3018{
3019 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
3020}
3021
3022/**
3023 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3024 * a given configuration
3025 * @fit: pointer to the FIT format image header
3026 * @noffset: configuration node offset
3027 *
3028 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3029 * configuration FIT_KERNEL_PROP property and translates it to the node
3030 * offset.
3031 *
3032 * returns:
3033 * image node offset when found (>=0)
3034 * negative number on failure (FDT_ERR_* code)
3035 */
3036int fit_conf_get_fdt_node (const void *fit, int noffset)
3037{
3038 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
3039}
3040
3041/**
3042 * fit_conf_print - prints out the FIT configuration details
3043 * @fit: pointer to the FIT format image header
Marian Balakowiczf773bea2008-03-12 10:35:46 +01003044 * @noffset: offset of the configuration node
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003045 * @p: pointer to prefix string
3046 *
3047 * fit_conf_print() lists all mandatory properies for the processed
3048 * configuration node.
3049 *
3050 * returns:
3051 * no returned results
3052 */
3053void fit_conf_print (const void *fit, int noffset, const char *p)
3054{
3055 char *desc;
3056 char *uname;
3057 int ret;
3058
3059 /* Mandatory properties */
3060 ret = fit_get_desc (fit, noffset, &desc);
3061 printf ("%s Description: ", p);
3062 if (ret)
3063 printf ("unavailable\n");
3064 else
3065 printf ("%s\n", desc);
3066
3067 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3068 printf ("%s Kernel: ", p);
3069 if (uname == NULL)
3070 printf ("unavailable\n");
3071 else
3072 printf ("%s\n", uname);
3073
3074 /* Optional properties */
3075 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3076 if (uname)
3077 printf ("%s Init Ramdisk: %s\n", p, uname);
3078
3079 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3080 if (uname)
3081 printf ("%s FDT: %s\n", p, uname);
3082}
Marian Balakowiczc8779642008-03-12 10:12:37 +01003083
3084/**
3085 * fit_check_ramdisk - verify FIT format ramdisk subimage
3086 * @fit_hdr: pointer to the FIT ramdisk header
3087 * @rd_noffset: ramdisk subimage node offset within FIT image
3088 * @arch: requested ramdisk image architecture type
3089 * @verify: data CRC verification flag
3090 *
3091 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3092 * specified FIT image.
3093 *
3094 * returns:
3095 * 1, on success
3096 * 0, on failure
3097 */
3098#ifndef USE_HOSTCC
3099static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3100{
3101 fit_image_print (fit, rd_noffset, " ");
3102
3103 if (verify) {
3104 puts (" Verifying Hash Integrity ... ");
3105 if (!fit_image_check_hashes (fit, rd_noffset)) {
3106 puts ("Bad Data Hash\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003107 show_boot_progress (-125);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003108 return 0;
3109 }
3110 puts ("OK\n");
3111 }
3112
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003113 show_boot_progress (126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003114 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3115 !fit_image_check_arch (fit, rd_noffset, arch) ||
3116 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3117 printf ("No Linux %s Ramdisk Image\n",
3118 genimg_get_arch_name(arch));
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003119 show_boot_progress (-126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003120 return 0;
3121 }
3122
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003123 show_boot_progress (127);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003124 return 1;
3125}
3126#endif /* USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003127#endif /* CONFIG_FIT */