blob: f1755410cdc401596857457343c80589f58ec663 [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_INVALID, NULL, "Invalid Image", },
134 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
135 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
136 { 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", },
141 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530142 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
Stefano Babic8edcde52010-01-20 18:19:10 +0100143 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000144 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100145 { -1, "", "", },
146};
147
Mike Frysinger7edb1862010-10-20 07:17:39 -0400148static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100149 { IH_COMP_NONE, "none", "uncompressed", },
150 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
151 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200152 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100153 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100154 { -1, "", "", },
155};
156
Mike Frysinger89cdab72008-03-31 11:02:01 -0400157uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200158uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100159#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
160static void genimg_print_time (time_t timestamp);
161#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100162
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100163/*****************************************************************************/
164/* Legacy format routines */
165/*****************************************************************************/
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200166int image_check_hcrc (const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100167{
168 ulong hcrc;
169 ulong len = image_get_header_size ();
170 image_header_t header;
171
172 /* Copy header so we can blank CRC field for re-calculation */
173 memmove (&header, (char *)hdr, image_get_header_size ());
174 image_set_hcrc (&header, 0);
175
176 hcrc = crc32 (0, (unsigned char *)&header, len);
177
178 return (hcrc == image_get_hcrc (hdr));
179}
180
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200181int image_check_dcrc (const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100182{
183 ulong data = image_get_data (hdr);
184 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200185 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100186
187 return (dcrc == image_get_dcrc (hdr));
188}
189
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100190/**
191 * image_multi_count - get component (sub-image) count
192 * @hdr: pointer to the header of the multi component image
193 *
194 * image_multi_count() returns number of components in a multi
195 * component image.
196 *
197 * Note: no checking of the image type is done, caller must pass
198 * a valid multi component image.
199 *
200 * returns:
201 * number of components
202 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200203ulong image_multi_count (const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100204{
205 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100206 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100207
208 /* get start of the image payload, which in case of multi
209 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100210 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100211
212 /* count non empty slots */
213 for (i = 0; size[i]; ++i)
214 count++;
215
216 return count;
217}
218
219/**
220 * image_multi_getimg - get component data address and size
221 * @hdr: pointer to the header of the multi component image
222 * @idx: index of the requested component
223 * @data: pointer to a ulong variable, will hold component data address
224 * @len: pointer to a ulong variable, will hold component size
225 *
226 * image_multi_getimg() returns size and data address for the requested
227 * component in a multi component image.
228 *
229 * Note: no checking of the image type is done, caller must pass
230 * a valid multi component image.
231 *
232 * returns:
233 * data address and size of the component, if idx is valid
234 * 0 in data and len, if idx is out of range
235 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200236void image_multi_getimg (const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100237 ulong *data, ulong *len)
238{
239 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100240 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700241 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100242
243 /* get number of component */
244 count = image_multi_count (hdr);
245
246 /* get start of the image payload, which in case of multi
247 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100248 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100249
250 /* get address of the proper component data start, which means
251 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100252 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100253
254 if (idx < count) {
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100255 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100256 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100257
258 /* go over all indices preceding requested component idx */
259 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700260 /* add up i-th component size, rounding up to 4 bytes */
261 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100262 }
263
264 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700265 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100266 } else {
267 *len = 0;
268 *data = 0;
269 }
270}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100271
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200272static void image_print_type (const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100273{
274 const char *os, *arch, *type, *comp;
275
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100276 os = genimg_get_os_name (image_get_os (hdr));
277 arch = genimg_get_arch_name (image_get_arch (hdr));
278 type = genimg_get_type_name (image_get_type (hdr));
279 comp = genimg_get_comp_name (image_get_comp (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100280
Marian Balakowicz570abb02008-02-29 15:59:59 +0100281 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100282}
283
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100284/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200285 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200286 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100287 * @p: pointer to prefix string
288 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200289 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100290 * The routine prints out all header fields followed by the size/offset data
291 * for MULTI/SCRIPT images.
292 *
293 * returns:
294 * no returned results
295 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200296void image_print_contents (const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100297{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200298 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200299 const char *p;
300
301#ifdef USE_HOSTCC
302 p = "";
303#else
304 p = " ";
305#endif
306
Marian Balakowicz570abb02008-02-29 15:59:59 +0100307 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
308#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
309 printf ("%sCreated: ", p);
310 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100311#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100312 printf ("%sImage Type: ", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100313 image_print_type (hdr);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100314 printf ("%sData Size: ", p);
315 genimg_print_size (image_get_data_size (hdr));
316 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
317 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100318
Marian Balakowicz570abb02008-02-29 15:59:59 +0100319 if (image_check_type (hdr, IH_TYPE_MULTI) ||
320 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100321 int i;
322 ulong data, len;
323 ulong count = image_multi_count (hdr);
324
Marian Balakowicz570abb02008-02-29 15:59:59 +0100325 printf ("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100326 for (i = 0; i < count; i++) {
327 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100328
329 printf ("%s Image %d: ", p, i);
330 genimg_print_size (len);
331
332 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
333 /*
334 * the user may need to know offsets
335 * if planning to do something with
336 * multiple files
337 */
338 printf ("%s Offset = 0x%08lx\n", p, data);
339 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100340 }
341 }
342}
343
Marian Balakowicz570abb02008-02-29 15:59:59 +0100344
345#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100346/**
347 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100348 * @rd_addr: ramdisk image start address
349 * @arch: expected ramdisk architecture
350 * @verify: checksum verification flag
351 *
352 * image_get_ramdisk() returns a pointer to the verified ramdisk image
353 * header. Routine receives image start address and expected architecture
354 * flag. Verification done covers data and header integrity and os/type/arch
355 * fields checking.
356 *
357 * If dataflash support is enabled routine checks for dataflash addresses
358 * and handles required dataflash reads.
359 *
360 * returns:
361 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600362 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100363 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200364static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100365 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100366{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200367 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100368
369 if (!image_check_magic (rd_hdr)) {
370 puts ("Bad Magic Number\n");
371 show_boot_progress (-10);
Kumar Gala274cea22008-02-27 21:51:46 -0600372 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100373 }
374
375 if (!image_check_hcrc (rd_hdr)) {
376 puts ("Bad Header Checksum\n");
377 show_boot_progress (-11);
Kumar Gala274cea22008-02-27 21:51:46 -0600378 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100379 }
380
381 show_boot_progress (10);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100382 image_print_contents (rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100383
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100384 if (verify) {
385 puts(" Verifying Checksum ... ");
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200386 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100387 puts ("Bad Data CRC\n");
388 show_boot_progress (-12);
Kumar Gala274cea22008-02-27 21:51:46 -0600389 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100390 }
391 puts("OK\n");
392 }
393
394 show_boot_progress (11);
395
396 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
397 !image_check_arch (rd_hdr, arch) ||
398 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
399 printf ("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100400 genimg_get_arch_name(arch));
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100401 show_boot_progress (-13);
Kumar Gala274cea22008-02-27 21:51:46 -0600402 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100403 }
404
405 return rd_hdr;
406}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100407#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100408
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100409/*****************************************************************************/
410/* Shared dual-format routines */
411/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100412#ifndef USE_HOSTCC
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200413int getenv_yesno (char *var)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100414{
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200415 char *s = getenv (var);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100416 return (s && (*s == 'n')) ? 0 : 1;
417}
418
419ulong getenv_bootm_low(void)
420{
421 char *s = getenv ("bootm_low");
422 if (s) {
423 ulong tmp = simple_strtoul (s, NULL, 16);
424 return tmp;
425 }
426
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200427#if defined(CONFIG_SYS_SDRAM_BASE)
428 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100429#elif defined(CONFIG_ARM)
430 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100431#else
432 return 0;
433#endif
434}
435
Becky Bruce391fd932008-06-09 20:37:18 -0500436phys_size_t getenv_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100437{
Matthew McClintockc519fac2010-07-08 10:11:08 -0500438 phys_size_t tmp;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100439 char *s = getenv ("bootm_size");
440 if (s) {
Becky Bruce391fd932008-06-09 20:37:18 -0500441 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100442 return tmp;
443 }
Matthew McClintockc519fac2010-07-08 10:11:08 -0500444 s = getenv("bootm_low");
445 if (s)
446 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
447 else
448 tmp = 0;
449
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100450
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100451#if defined(CONFIG_ARM)
Matthew McClintockc519fac2010-07-08 10:11:08 -0500452 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100453#else
Matthew McClintockc519fac2010-07-08 10:11:08 -0500454 return gd->bd->bi_memsize - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100455#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100456}
457
Grant Likelyc3624e62011-03-28 09:58:43 +0000458phys_size_t getenv_bootm_mapsize(void)
459{
460 phys_size_t tmp;
461 char *s = getenv ("bootm_mapsize");
462 if (s) {
463 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
464 return tmp;
465 }
466
467#if defined(CONFIG_SYS_BOOTMAPSZ)
468 return CONFIG_SYS_BOOTMAPSZ;
469#else
470 return getenv_bootm_size();
471#endif
472}
473
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100474void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
475{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400476 if (to == from)
477 return;
478
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100479#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
480 while (len > 0) {
481 size_t tail = (len > chunksz) ? chunksz : len;
482 WATCHDOG_RESET ();
483 memmove (to, from, tail);
484 to += tail;
485 from += tail;
486 len -= tail;
487 }
488#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
489 memmove (to, from, len);
490#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
491}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100492#endif /* !USE_HOSTCC */
493
Prafulla Wadaskarf666dea2009-09-07 14:59:07 +0530494void genimg_print_size (uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100495{
496#ifndef USE_HOSTCC
497 printf ("%d Bytes = ", size);
498 print_size (size, "\n");
499#else
500 printf ("%d Bytes = %.2f kB = %.2f MB\n",
501 size, (double)size / 1.024e3,
502 (double)size / 1.048576e6);
503#endif
504}
505
506#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
507static void genimg_print_time (time_t timestamp)
508{
509#ifndef USE_HOSTCC
510 struct rtc_time tm;
511
512 to_tm (timestamp, &tm);
513 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
514 tm.tm_year, tm.tm_mon, tm.tm_mday,
515 tm.tm_hour, tm.tm_min, tm.tm_sec);
516#else
517 printf ("%s", ctime(&timestamp));
518#endif
519}
520#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
521
522/**
523 * get_table_entry_name - translate entry id to long name
524 * @table: pointer to a translation table for entries of a specific type
525 * @msg: message to be returned when translation fails
526 * @id: entry id to be translated
527 *
528 * get_table_entry_name() will go over translation table trying to find
529 * entry that matches given id. If matching entry is found, its long
530 * name is returned to the caller.
531 *
532 * returns:
533 * long entry name if translation succeeds
534 * msg otherwise
535 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400536char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100537{
538 for (; table->id >= 0; ++table) {
539 if (table->id == id)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200540#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Woode3d1ac72009-04-02 16:15:10 -0500541 return table->lname;
542#else
543 return table->lname + gd->reloc_off;
544#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100545 }
546 return (msg);
547}
548
549const char *genimg_get_os_name (uint8_t os)
550{
551 return (get_table_entry_name (uimage_os, "Unknown OS", os));
552}
553
554const char *genimg_get_arch_name (uint8_t arch)
555{
556 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
557}
558
559const char *genimg_get_type_name (uint8_t type)
560{
561 return (get_table_entry_name (uimage_type, "Unknown Image", type));
562}
563
564const char *genimg_get_comp_name (uint8_t comp)
565{
566 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
567}
568
569/**
570 * get_table_entry_id - translate short entry name to id
571 * @table: pointer to a translation table for entries of a specific type
572 * @table_name: to be used in case of error
573 * @name: entry short name to be translated
574 *
575 * get_table_entry_id() will go over translation table trying to find
576 * entry that matches given short name. If matching entry is found,
577 * its id returned to the caller.
578 *
579 * returns:
580 * entry id if translation succeeds
581 * -1 otherwise
582 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400583int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100584 const char *table_name, const char *name)
585{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400586 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100587#ifdef USE_HOSTCC
588 int first = 1;
589
590 for (t = table; t->id >= 0; ++t) {
591 if (t->sname && strcasecmp(t->sname, name) == 0)
592 return (t->id);
593 }
594
595 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
596 for (t = table; t->id >= 0; ++t) {
597 if (t->sname == NULL)
598 continue;
599 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
600 first = 0;
601 }
602 fprintf (stderr, "\n");
603#else
604 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200605#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Woode3d1ac72009-04-02 16:15:10 -0500606 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200607#else
608 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500609#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100610 return (t->id);
611 }
612 debug ("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100613#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100614 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100615}
616
Marian Balakowicz570abb02008-02-29 15:59:59 +0100617int genimg_get_os_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100618{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100619 return (get_table_entry_id (uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100620}
621
Marian Balakowicz570abb02008-02-29 15:59:59 +0100622int genimg_get_arch_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100623{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100624 return (get_table_entry_id (uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100625}
626
Marian Balakowicz570abb02008-02-29 15:59:59 +0100627int genimg_get_type_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100628{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100629 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100630}
631
Marian Balakowicz570abb02008-02-29 15:59:59 +0100632int genimg_get_comp_id (const char *name)
633{
634 return (get_table_entry_id (uimage_comp, "Compression", name));
635}
636
637#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100638/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100639 * genimg_get_format - get image format type
640 * @img_addr: image start address
641 *
642 * genimg_get_format() checks whether provided address points to a valid
643 * legacy or FIT image.
644 *
645 * New uImage format and FDT blob are based on a libfdt. FDT blob
646 * may be passed directly or embedded in a FIT image. In both situations
647 * genimg_get_format() must be able to dectect libfdt header.
648 *
649 * returns:
650 * image format type or IMAGE_FORMAT_INVALID if no image is present
651 */
652int genimg_get_format (void *img_addr)
653{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200654 ulong format = IMAGE_FORMAT_INVALID;
655 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100656#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200657 char *fit_hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100658#endif
659
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200660 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100661 if (image_check_magic(hdr))
662 format = IMAGE_FORMAT_LEGACY;
663#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
664 else {
665 fit_hdr = (char *)img_addr;
666 if (fdt_check_header (fit_hdr) == 0)
667 format = IMAGE_FORMAT_FIT;
668 }
669#endif
670
671 return format;
672}
673
674/**
675 * genimg_get_image - get image from special storage (if necessary)
676 * @img_addr: image start address
677 *
678 * genimg_get_image() checks if provided image start adddress is located
679 * in a dataflash storage. If so, image is moved to a system RAM memory.
680 *
681 * returns:
682 * image start address after possible relocation from special storage
683 */
684ulong genimg_get_image (ulong img_addr)
685{
686 ulong ram_addr = img_addr;
687
688#ifdef CONFIG_HAS_DATAFLASH
689 ulong h_size, d_size;
690
691 if (addr_dataflash (img_addr)){
692 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200693 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100694
695 /* get header size */
696 h_size = image_get_header_size ();
697#if defined(CONFIG_FIT)
698 if (sizeof(struct fdt_header) > h_size)
699 h_size = sizeof(struct fdt_header);
700#endif
701
702 /* read in header */
703 debug (" Reading image header from dataflash address "
704 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
705
706 read_dataflash (img_addr, h_size, (char *)ram_addr);
707
708 /* get data size */
709 switch (genimg_get_format ((void *)ram_addr)) {
710 case IMAGE_FORMAT_LEGACY:
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200711 d_size = image_get_data_size ((const image_header_t *)ram_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100712 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
713 ram_addr, d_size);
714 break;
715#if defined(CONFIG_FIT)
716 case IMAGE_FORMAT_FIT:
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100717 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100718 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
719 ram_addr, d_size);
720 break;
721#endif
722 default:
723 printf (" No valid image found at 0x%08lx\n", img_addr);
724 return ram_addr;
725 }
726
727 /* read in image data */
728 debug (" Reading image remaining data from dataflash address "
729 "%08lx to RAM address %08lx\n", img_addr + h_size,
730 ram_addr + h_size);
731
732 read_dataflash (img_addr + h_size, d_size,
733 (char *)(ram_addr + h_size));
734
735 }
736#endif /* CONFIG_HAS_DATAFLASH */
737
738 return ram_addr;
739}
740
741/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100742 * fit_has_config - check if there is a valid FIT configuration
743 * @images: pointer to the bootm command headers structure
744 *
745 * fit_has_config() checks if there is a FIT configuration in use
746 * (if FTI support is present).
747 *
748 * returns:
749 * 0, no FIT support or no configuration found
750 * 1, configuration found
751 */
752int genimg_has_config (bootm_headers_t *images)
753{
754#if defined(CONFIG_FIT)
755 if (images->fit_uname_cfg)
756 return 1;
757#endif
758 return 0;
759}
760
761/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100762 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100763 * @argc: command argument count
764 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100765 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100766 * @arch: expected ramdisk architecture
767 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
768 * @rd_end: pointer to a ulong variable, will hold ramdisk end
769 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100770 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100771 * Curently supported are the following ramdisk sources:
772 * - multicomponent kernel/ramdisk image,
773 * - commandline provided address of decicated ramdisk image.
774 *
775 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100776 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100777 * rd_start and rd_end are set to ramdisk start/end addresses if
778 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100779 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500780 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100781 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100782 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200783int boot_get_ramdisk (int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100784 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100785{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100786 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100787 ulong rd_data, rd_len;
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200788 const image_header_t *rd_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100789#if defined(CONFIG_FIT)
790 void *fit_hdr;
791 const char *fit_uname_config = NULL;
792 const char *fit_uname_ramdisk = NULL;
793 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100794 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100795 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100796 const void *data;
797 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100798#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100799
Marian Balakowiczc8779642008-03-12 10:12:37 +0100800 *rd_start = 0;
801 *rd_end = 0;
802
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100803 /*
804 * Look for a '-' which indicates to ignore the
805 * ramdisk argument
806 */
807 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
808 debug ("## Skipping init Ramdisk\n");
809 rd_len = rd_data = 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100810 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100811#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100812 if (argc >= 3) {
813 /*
814 * If the init ramdisk comes from the FIT image and
815 * the FIT image address is omitted in the command
816 * line argument, try to use os FIT image address or
817 * default load address.
818 */
819 if (images->fit_uname_os)
820 default_addr = (ulong)images->fit_hdr_os;
821 else
822 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100823
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100824 if (fit_parse_conf (argv[2], default_addr,
825 &rd_addr, &fit_uname_config)) {
826 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
827 fit_uname_config, rd_addr);
828 } else if (fit_parse_subimage (argv[2], default_addr,
829 &rd_addr, &fit_uname_ramdisk)) {
830 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
831 fit_uname_ramdisk, rd_addr);
832 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100833#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100834 {
835 rd_addr = simple_strtoul(argv[2], NULL, 16);
836 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
837 rd_addr);
838 }
839#if defined(CONFIG_FIT)
840 } else {
841 /* use FIT configuration provided in first bootm
842 * command argument
843 */
844 rd_addr = (ulong)images->fit_hdr_os;
845 fit_uname_config = images->fit_uname_cfg;
846 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
847 fit_uname_config, rd_addr);
848
849 /*
850 * Check whether configuration has ramdisk defined,
851 * if not, don't try to use it, quit silently.
852 */
853 fit_hdr = (void *)rd_addr;
854 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
855 if (cfg_noffset < 0) {
856 debug ("* ramdisk: no such config\n");
Michal Simekc78fce62008-07-11 10:43:13 +0200857 return 1;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100858 }
859
860 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
861 if (rd_noffset < 0) {
862 debug ("* ramdisk: no ramdisk in config\n");
Peter Tyser41266c92008-08-05 10:51:57 -0500863 return 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100864 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100865 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100866#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100867
868 /* copy from dataflash if needed */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100869 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100870
871 /*
872 * Check if there is an initrd image at the
873 * address provided in the second bootm argument
874 * check image type, for FIT images get FIT node.
875 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100876 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100877 case IMAGE_FORMAT_LEGACY:
Marian Balakowiczc8779642008-03-12 10:12:37 +0100878 printf ("## Loading init Ramdisk from Legacy "
879 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100880
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100881 show_boot_progress (9);
Marian Balakowiczd985c842008-03-12 10:14:38 +0100882 rd_hdr = image_get_ramdisk (rd_addr, arch,
883 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100884
Marian Balakowiczc8779642008-03-12 10:12:37 +0100885 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600886 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600887
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100888 rd_data = image_get_data (rd_hdr);
889 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100890 rd_load = image_get_load (rd_hdr);
891 break;
892#if defined(CONFIG_FIT)
893 case IMAGE_FORMAT_FIT:
894 fit_hdr = (void *)rd_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100895 printf ("## Loading init Ramdisk from FIT "
896 "Image at %08lx ...\n", rd_addr);
897
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100898 show_boot_progress (120);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100899 if (!fit_check_format (fit_hdr)) {
900 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100901 show_boot_progress (-120);
Michal Simekc78fce62008-07-11 10:43:13 +0200902 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100903 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100904 show_boot_progress (121);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100905
906 if (!fit_uname_ramdisk) {
907 /*
908 * no ramdisk image node unit name, try to get config
909 * node first. If config unit node name is NULL
910 * fit_conf_get_node() will try to find default config node
911 */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100912 show_boot_progress (122);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100913 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
914 if (cfg_noffset < 0) {
915 puts ("Could not find configuration node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100916 show_boot_progress (-122);
Michal Simekc78fce62008-07-11 10:43:13 +0200917 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100918 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100919 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
920 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100921
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100922 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100923 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
924 } else {
925 /* get ramdisk component image node offset */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100926 show_boot_progress (123);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100927 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
928 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100929 if (rd_noffset < 0) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100930 puts ("Could not find subimage node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100931 show_boot_progress (-124);
Michal Simekc78fce62008-07-11 10:43:13 +0200932 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100933 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100934
935 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
936
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100937 show_boot_progress (125);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100938 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
Michal Simekc78fce62008-07-11 10:43:13 +0200939 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100940
941 /* get ramdisk image data address and length */
942 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
943 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100944 show_boot_progress (-127);
Michal Simekc78fce62008-07-11 10:43:13 +0200945 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100946 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100947 show_boot_progress (128);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100948
949 rd_data = (ulong)data;
950 rd_len = size;
951
952 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
953 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100954 show_boot_progress (-129);
Michal Simekc78fce62008-07-11 10:43:13 +0200955 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100956 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100957 show_boot_progress (129);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100958
959 images->fit_hdr_rd = fit_hdr;
960 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100961 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100962 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100963#endif
964 default:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100965 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +0100966 rd_data = rd_len = rd_load = 0;
Kumar Galaea86b9e2008-08-29 19:08:29 -0500967 return 1;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100968 }
969
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100970#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100971 /*
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100972 * We need to copy the ramdisk to SRAM to let Linux boot
973 */
974 if (rd_data) {
975 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
976 rd_data = rd_load;
977 }
978#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
979
980 } else if (images->legacy_hdr_valid &&
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200981 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100982 /*
983 * Now check if we have a legacy mult-component image,
984 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100985 */
986 show_boot_progress (13);
987 printf ("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100988 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100989 (ulong)images->legacy_hdr_os);
990
991 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100992 } else {
993 /*
994 * no initrd image
995 */
996 show_boot_progress (14);
997 rd_len = rd_data = 0;
998 }
999
1000 if (!rd_data) {
1001 debug ("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001002 } else {
1003 *rd_start = rd_data;
1004 *rd_end = rd_data + rd_len;
1005 }
1006 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1007 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -06001008
1009 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001010}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001011
John Rigbyfca43cc2010-10-13 13:57:35 -06001012#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001013/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001014 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001015 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001016 * @rd_data: ramdisk data start address
1017 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001018 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1019 * start address (after possible relocation)
1020 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1021 * end address (after possible relocation)
1022 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001023 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001024 * variable and if requested ramdisk data is moved to a specified location.
1025 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001026 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1027 * start/end addresses if ramdisk image start and len were provided,
1028 * otherwise set initrd_start and initrd_end set to zeros.
1029 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001030 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001031 * 0 - success
1032 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001033 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001034int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001035 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001036{
1037 char *s;
1038 ulong initrd_high;
1039 int initrd_copy_to_ram = 1;
1040
1041 if ((s = getenv ("initrd_high")) != NULL) {
1042 /* a value of "no" or a similar string will act like 0,
1043 * turning the "load high" feature off. This is intentional.
1044 */
1045 initrd_high = simple_strtoul (s, NULL, 16);
1046 if (initrd_high == ~0)
1047 initrd_copy_to_ram = 0;
1048 } else {
1049 /* not set, no restrictions to load high */
1050 initrd_high = ~0;
1051 }
1052
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001053
1054#ifdef CONFIG_LOGBUFFER
1055 /* Prevent initrd from overwriting logbuffer */
1056 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1057#endif
1058
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001059 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1060 initrd_high, initrd_copy_to_ram);
1061
1062 if (rd_data) {
1063 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1064 debug (" in-place initrd\n");
1065 *initrd_start = rd_data;
1066 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001067 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001068 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001069 if (initrd_high)
Becky Bruce391fd932008-06-09 20:37:18 -05001070 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001071 else
Becky Bruce391fd932008-06-09 20:37:18 -05001072 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001073
Kumar Galae822d7f2008-02-27 21:51:49 -06001074 if (*initrd_start == 0) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001075 puts ("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001076 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001077 }
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001078 show_boot_progress (12);
1079
1080 *initrd_end = *initrd_start + rd_len;
1081 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1082 *initrd_start, *initrd_end);
1083
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001084 memmove_wd ((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001085 (void *)rd_data, rd_len, CHUNKSZ);
1086
1087 puts ("OK\n");
1088 }
1089 } else {
1090 *initrd_start = 0;
1091 *initrd_end = 0;
1092 }
1093 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1094 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001095
Kumar Galae822d7f2008-02-27 21:51:49 -06001096 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001097
Kumar Galae822d7f2008-02-27 21:51:49 -06001098error:
1099 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001100}
John Rigbyfca43cc2010-10-13 13:57:35 -06001101#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001102
Kumar Gala06a09912008-08-15 08:24:38 -05001103#ifdef CONFIG_OF_LIBFDT
1104static void fdt_error (const char *msg)
1105{
1106 puts ("ERROR: ");
1107 puts (msg);
1108 puts (" - must RESET the board to recover.\n");
1109}
1110
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001111static const image_header_t *image_get_fdt (ulong fdt_addr)
Kumar Gala06a09912008-08-15 08:24:38 -05001112{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001113 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001114
1115 image_print_contents (fdt_hdr);
1116
1117 puts (" Verifying Checksum ... ");
1118 if (!image_check_hcrc (fdt_hdr)) {
1119 fdt_error ("fdt header checksum invalid");
1120 return NULL;
1121 }
1122
1123 if (!image_check_dcrc (fdt_hdr)) {
1124 fdt_error ("fdt checksum invalid");
1125 return NULL;
1126 }
1127 puts ("OK\n");
1128
1129 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1130 fdt_error ("uImage is not a fdt");
1131 return NULL;
1132 }
1133 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1134 fdt_error ("uImage is compressed");
1135 return NULL;
1136 }
1137 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1138 fdt_error ("uImage data is not a fdt");
1139 return NULL;
1140 }
1141 return fdt_hdr;
1142}
1143
1144/**
1145 * fit_check_fdt - verify FIT format FDT subimage
1146 * @fit_hdr: pointer to the FIT header
1147 * fdt_noffset: FDT subimage node offset within FIT image
1148 * @verify: data CRC verification flag
1149 *
1150 * fit_check_fdt() verifies integrity of the FDT subimage and from
1151 * specified FIT image.
1152 *
1153 * returns:
1154 * 1, on success
1155 * 0, on failure
1156 */
1157#if defined(CONFIG_FIT)
1158static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1159{
1160 fit_image_print (fit, fdt_noffset, " ");
1161
1162 if (verify) {
1163 puts (" Verifying Hash Integrity ... ");
1164 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1165 fdt_error ("Bad Data Hash");
1166 return 0;
1167 }
1168 puts ("OK\n");
1169 }
1170
1171 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1172 fdt_error ("Not a FDT image");
1173 return 0;
1174 }
1175
1176 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1177 fdt_error ("FDT image is compressed");
1178 return 0;
1179 }
1180
1181 return 1;
1182}
1183#endif /* CONFIG_FIT */
1184
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001185#ifndef CONFIG_SYS_FDT_PAD
1186#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Gala06a09912008-08-15 08:24:38 -05001187#endif
1188
Grant Likely55b0a392011-03-28 09:59:01 +00001189#if defined(CONFIG_OF_LIBFDT)
1190/**
1191 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1192 * @lmb: pointer to lmb handle, will be used for memory mgmt
1193 * @fdt_blob: pointer to fdt blob base address
1194 *
1195 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1196 * memreserve regions prevents u-boot from using them to store the initrd
1197 * or the fdt blob.
1198 */
1199void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1200{
1201 uint64_t addr, size;
1202 int i, total;
1203
1204 if (fdt_check_header (fdt_blob) != 0)
1205 return;
1206
1207 total = fdt_num_mem_rsv(fdt_blob);
1208 for (i = 0; i < total; i++) {
1209 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1210 continue;
1211 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1212 (unsigned long long)addr, (unsigned long long)size);
1213 lmb_reserve(lmb, addr, size);
1214 }
1215}
1216
Kumar Gala06a09912008-08-15 08:24:38 -05001217/**
1218 * boot_relocate_fdt - relocate flat device tree
1219 * @lmb: pointer to lmb handle, will be used for memory mgmt
Kumar Gala06a09912008-08-15 08:24:38 -05001220 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1221 * @of_size: pointer to a ulong variable, will hold fdt length
1222 *
Timur Tabi43b08af2010-05-24 15:10:25 -05001223 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1224 * relocates the of_flat_tree into that region, even if the fdt is already in
1225 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1226 * bytes.
Kumar Gala06a09912008-08-15 08:24:38 -05001227 *
1228 * of_flat_tree and of_size are set to final (after relocation) values
1229 *
1230 * returns:
1231 * 0 - success
1232 * 1 - failure
1233 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001234int boot_relocate_fdt (struct lmb *lmb, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001235{
Timur Tabi43b08af2010-05-24 15:10:25 -05001236 void *fdt_blob = *of_flat_tree;
1237 void *of_start = 0;
David A. Longa28afca2011-07-09 16:40:19 -04001238 char *fdt_high;
Kumar Gala06a09912008-08-15 08:24:38 -05001239 ulong of_len = 0;
Timur Tabi43b08af2010-05-24 15:10:25 -05001240 int err;
David A. Longa28afca2011-07-09 16:40:19 -04001241 int disable_relocation = 0;
Kumar Gala06a09912008-08-15 08:24:38 -05001242
1243 /* nothing to do */
1244 if (*of_size == 0)
1245 return 0;
1246
1247 if (fdt_check_header (fdt_blob) != 0) {
1248 fdt_error ("image is not a fdt");
1249 goto error;
1250 }
1251
Timur Tabi43b08af2010-05-24 15:10:25 -05001252 /* position on a 4K boundary before the alloc_current */
1253 /* Pad the FDT by a specified amount */
1254 of_len = *of_size + CONFIG_SYS_FDT_PAD;
David A. Longa28afca2011-07-09 16:40:19 -04001255
1256 /* If fdt_high is set use it to select the relocation address */
1257 fdt_high = getenv("fdt_high");
1258 if (fdt_high) {
1259 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1260
1261 if (((ulong) desired_addr) == ~0UL) {
1262 /* All ones means use fdt in place */
1263 desired_addr = fdt_blob;
1264 disable_relocation = 1;
1265 }
1266 if (desired_addr) {
1267 of_start =
1268 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1269 ((ulong)
1270 desired_addr)
1271 + of_len);
1272 if (desired_addr && of_start != desired_addr) {
1273 puts("Failed using fdt_high value for Device Tree");
1274 goto error;
1275 }
1276 } else {
1277 of_start =
Matthew McClintock1bb5e902011-07-18 13:08:05 +00001278 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
David A. Longa28afca2011-07-09 16:40:19 -04001279 }
1280 } else {
1281 of_start =
1282 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1283 getenv_bootm_mapsize()
1284 + getenv_bootm_low());
1285 }
Kumar Gala06a09912008-08-15 08:24:38 -05001286
Timur Tabi43b08af2010-05-24 15:10:25 -05001287 if (of_start == 0) {
1288 puts("device tree - allocation error\n");
1289 goto error;
Kumar Gala06a09912008-08-15 08:24:38 -05001290 }
1291
David A. Longa28afca2011-07-09 16:40:19 -04001292 if (disable_relocation) {
1293 /* We assume there is space after the existing fdt to use for padding */
1294 fdt_set_totalsize(of_start, of_len);
1295 printf(" Using Device Tree in place at %p, end %p\n",
1296 of_start, of_start + of_len - 1);
1297 } else {
1298 debug ("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1299 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
Timur Tabi43b08af2010-05-24 15:10:25 -05001300
David A. Longa28afca2011-07-09 16:40:19 -04001301 printf (" Loading Device Tree to %p, end %p ... ",
1302 of_start, of_start + of_len - 1);
Timur Tabi43b08af2010-05-24 15:10:25 -05001303
David A. Longa28afca2011-07-09 16:40:19 -04001304 err = fdt_open_into (fdt_blob, of_start, of_len);
1305 if (err != 0) {
1306 fdt_error ("fdt move failed");
1307 goto error;
1308 }
1309 puts ("OK\n");
Timur Tabi43b08af2010-05-24 15:10:25 -05001310 }
Timur Tabi43b08af2010-05-24 15:10:25 -05001311
1312 *of_flat_tree = of_start;
1313 *of_size = of_len;
1314
Kumar Gala54f9c862008-08-15 08:24:39 -05001315 set_working_fdt_addr(*of_flat_tree);
Kumar Gala06a09912008-08-15 08:24:38 -05001316 return 0;
1317
1318error:
1319 return 1;
1320}
Grant Likelyed59e582011-03-28 09:58:49 +00001321#endif /* CONFIG_OF_LIBFDT */
Kumar Gala06a09912008-08-15 08:24:38 -05001322
1323/**
1324 * boot_get_fdt - main fdt handling routine
1325 * @argc: command argument count
1326 * @argv: command argument list
1327 * @images: pointer to the bootm images structure
1328 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1329 * @of_size: pointer to a ulong variable, will hold fdt length
1330 *
1331 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1332 * Curently supported are the following ramdisk sources:
1333 * - multicomponent kernel/ramdisk image,
1334 * - commandline provided address of decicated ramdisk image.
1335 *
1336 * returns:
1337 * 0, if fdt image was found and valid, or skipped
1338 * of_flat_tree and of_size are set to fdt start address and length if
1339 * fdt image is found and valid
1340 *
1341 * 1, if fdt image is found but corrupted
1342 * of_flat_tree and of_size are set to 0 if no fdt exists
1343 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001344int boot_get_fdt (int flag, int argc, char * const argv[], bootm_headers_t *images,
Kumar Gala06a09912008-08-15 08:24:38 -05001345 char **of_flat_tree, ulong *of_size)
1346{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001347 const image_header_t *fdt_hdr;
Kumar Gala06a09912008-08-15 08:24:38 -05001348 ulong fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001349 char *fdt_blob = NULL;
1350 ulong image_start, image_end;
1351 ulong load_start, load_end;
1352#if defined(CONFIG_FIT)
1353 void *fit_hdr;
1354 const char *fit_uname_config = NULL;
1355 const char *fit_uname_fdt = NULL;
1356 ulong default_addr;
1357 int cfg_noffset;
1358 int fdt_noffset;
1359 const void *data;
1360 size_t size;
1361#endif
1362
1363 *of_flat_tree = NULL;
1364 *of_size = 0;
1365
1366 if (argc > 3 || genimg_has_config (images)) {
1367#if defined(CONFIG_FIT)
1368 if (argc > 3) {
1369 /*
1370 * If the FDT blob comes from the FIT image and the
1371 * FIT image address is omitted in the command line
1372 * argument, try to use ramdisk or os FIT image
1373 * address or default load address.
1374 */
1375 if (images->fit_uname_rd)
1376 default_addr = (ulong)images->fit_hdr_rd;
1377 else if (images->fit_uname_os)
1378 default_addr = (ulong)images->fit_hdr_os;
1379 else
1380 default_addr = load_addr;
1381
1382 if (fit_parse_conf (argv[3], default_addr,
1383 &fdt_addr, &fit_uname_config)) {
1384 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1385 fit_uname_config, fdt_addr);
1386 } else if (fit_parse_subimage (argv[3], default_addr,
1387 &fdt_addr, &fit_uname_fdt)) {
1388 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1389 fit_uname_fdt, fdt_addr);
1390 } else
1391#endif
1392 {
1393 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1394 debug ("* fdt: cmdline image address = 0x%08lx\n",
1395 fdt_addr);
1396 }
1397#if defined(CONFIG_FIT)
1398 } else {
1399 /* use FIT configuration provided in first bootm
1400 * command argument
1401 */
1402 fdt_addr = (ulong)images->fit_hdr_os;
1403 fit_uname_config = images->fit_uname_cfg;
1404 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1405 fit_uname_config, fdt_addr);
1406
1407 /*
1408 * Check whether configuration has FDT blob defined,
1409 * if not quit silently.
1410 */
1411 fit_hdr = (void *)fdt_addr;
1412 cfg_noffset = fit_conf_get_node (fit_hdr,
1413 fit_uname_config);
1414 if (cfg_noffset < 0) {
1415 debug ("* fdt: no such config\n");
1416 return 0;
1417 }
1418
1419 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1420 cfg_noffset);
1421 if (fdt_noffset < 0) {
1422 debug ("* fdt: no fdt in config\n");
1423 return 0;
1424 }
1425 }
1426#endif
1427
1428 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1429 fdt_addr);
1430
1431 /* copy from dataflash if needed */
1432 fdt_addr = genimg_get_image (fdt_addr);
1433
1434 /*
1435 * Check if there is an FDT image at the
1436 * address provided in the second bootm argument
1437 * check image type, for FIT images get a FIT node.
1438 */
1439 switch (genimg_get_format ((void *)fdt_addr)) {
1440 case IMAGE_FORMAT_LEGACY:
1441 /* verify fdt_addr points to a valid image header */
1442 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1443 fdt_addr);
1444 fdt_hdr = image_get_fdt (fdt_addr);
1445 if (!fdt_hdr)
1446 goto error;
1447
1448 /*
1449 * move image data to the load address,
1450 * make sure we don't overwrite initial image
1451 */
1452 image_start = (ulong)fdt_hdr;
1453 image_end = image_get_image_end (fdt_hdr);
1454
1455 load_start = image_get_load (fdt_hdr);
1456 load_end = load_start + image_get_data_size (fdt_hdr);
1457
1458 if ((load_start < image_end) && (load_end > image_start)) {
1459 fdt_error ("fdt overwritten");
1460 goto error;
1461 }
1462
1463 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1464 image_get_data (fdt_hdr), load_start);
1465
1466 memmove ((void *)load_start,
1467 (void *)image_get_data (fdt_hdr),
1468 image_get_data_size (fdt_hdr));
1469
1470 fdt_blob = (char *)load_start;
1471 break;
1472 case IMAGE_FORMAT_FIT:
1473 /*
1474 * This case will catch both: new uImage format
1475 * (libfdt based) and raw FDT blob (also libfdt
1476 * based).
1477 */
1478#if defined(CONFIG_FIT)
1479 /* check FDT blob vs FIT blob */
1480 if (fit_check_format ((const void *)fdt_addr)) {
1481 /*
1482 * FIT image
1483 */
1484 fit_hdr = (void *)fdt_addr;
1485 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1486 fdt_addr);
1487
1488 if (!fit_uname_fdt) {
1489 /*
1490 * no FDT blob image node unit name,
1491 * try to get config node first. If
1492 * config unit node name is NULL
1493 * fit_conf_get_node() will try to
1494 * find default config node
1495 */
1496 cfg_noffset = fit_conf_get_node (fit_hdr,
1497 fit_uname_config);
1498
1499 if (cfg_noffset < 0) {
1500 fdt_error ("Could not find configuration node\n");
1501 goto error;
1502 }
1503
1504 fit_uname_config = fdt_get_name (fit_hdr,
1505 cfg_noffset, NULL);
1506 printf (" Using '%s' configuration\n",
1507 fit_uname_config);
1508
1509 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1510 cfg_noffset);
1511 fit_uname_fdt = fit_get_name (fit_hdr,
1512 fdt_noffset, NULL);
1513 } else {
1514 /* get FDT component image node offset */
1515 fdt_noffset = fit_image_get_node (fit_hdr,
1516 fit_uname_fdt);
1517 }
1518 if (fdt_noffset < 0) {
1519 fdt_error ("Could not find subimage node\n");
1520 goto error;
1521 }
1522
1523 printf (" Trying '%s' FDT blob subimage\n",
1524 fit_uname_fdt);
1525
1526 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1527 images->verify))
1528 goto error;
1529
1530 /* get ramdisk image data address and length */
1531 if (fit_image_get_data (fit_hdr, fdt_noffset,
1532 &data, &size)) {
1533 fdt_error ("Could not find FDT subimage data");
1534 goto error;
1535 }
1536
1537 /* verift that image data is a proper FDT blob */
1538 if (fdt_check_header ((char *)data) != 0) {
1539 fdt_error ("Subimage data is not a FTD");
1540 goto error;
1541 }
1542
1543 /*
1544 * move image data to the load address,
1545 * make sure we don't overwrite initial image
1546 */
1547 image_start = (ulong)fit_hdr;
1548 image_end = fit_get_end (fit_hdr);
1549
1550 if (fit_image_get_load (fit_hdr, fdt_noffset,
1551 &load_start) == 0) {
1552 load_end = load_start + size;
1553
1554 if ((load_start < image_end) &&
1555 (load_end > image_start)) {
1556 fdt_error ("FDT overwritten");
1557 goto error;
1558 }
1559
1560 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1561 (ulong)data, load_start);
1562
1563 memmove ((void *)load_start,
1564 (void *)data, size);
1565
1566 fdt_blob = (char *)load_start;
1567 } else {
1568 fdt_blob = (char *)data;
1569 }
1570
1571 images->fit_hdr_fdt = fit_hdr;
1572 images->fit_uname_fdt = fit_uname_fdt;
1573 images->fit_noffset_fdt = fdt_noffset;
1574 break;
1575 } else
1576#endif
1577 {
1578 /*
1579 * FDT blob
1580 */
1581 fdt_blob = (char *)fdt_addr;
1582 debug ("* fdt: raw FDT blob\n");
1583 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1584 }
1585 break;
1586 default:
Anatolij Gustschin8e024942008-08-29 21:04:45 +02001587 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001588 goto error;
1589 }
1590
1591 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1592
1593 } else if (images->legacy_hdr_valid &&
1594 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1595
1596 ulong fdt_data, fdt_len;
1597
1598 /*
1599 * Now check if we have a legacy multi-component image,
1600 * get second entry data start address and len.
1601 */
1602 printf ("## Flattened Device Tree from multi "
1603 "component Image at %08lX\n",
1604 (ulong)images->legacy_hdr_os);
1605
1606 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1607 if (fdt_len) {
1608
1609 fdt_blob = (char *)fdt_data;
1610 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1611
1612 if (fdt_check_header (fdt_blob) != 0) {
1613 fdt_error ("image is not a fdt");
1614 goto error;
1615 }
1616
John Rigbyd1263fc2010-10-13 13:57:32 -06001617 if (fdt_totalsize(fdt_blob) != fdt_len) {
Kumar Gala06a09912008-08-15 08:24:38 -05001618 fdt_error ("fdt size != image size");
1619 goto error;
1620 }
1621 } else {
1622 debug ("## No Flattened Device Tree\n");
1623 return 0;
1624 }
1625 } else {
1626 debug ("## No Flattened Device Tree\n");
1627 return 0;
1628 }
1629
1630 *of_flat_tree = fdt_blob;
John Rigbyd1263fc2010-10-13 13:57:32 -06001631 *of_size = fdt_totalsize(fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001632 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner52514692008-08-21 07:12:26 -07001633 (ulong)*of_flat_tree, *of_size);
Kumar Gala06a09912008-08-15 08:24:38 -05001634
1635 return 0;
1636
1637error:
1638 *of_flat_tree = 0;
1639 *of_size = 0;
1640 return 1;
1641}
1642#endif /* CONFIG_OF_LIBFDT */
1643
John Rigbyfca43cc2010-10-13 13:57:35 -06001644#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001645/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001646 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001647 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001648 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1649 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1650 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001651 * boot_get_cmdline() allocates space for kernel command line below
Grant Likely590d3ca2011-03-28 09:58:34 +00001652 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001653 * variable is present its contents is copied to allocated kernel
1654 * command line.
1655 *
1656 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001657 * 0 - success
1658 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001659 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001660int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001661{
1662 char *cmdline;
1663 char *s;
1664
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001665 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001666 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001667
1668 if (cmdline == NULL)
1669 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001670
1671 if ((s = getenv("bootargs")) == NULL)
1672 s = "";
1673
1674 strcpy(cmdline, s);
1675
1676 *cmd_start = (ulong) & cmdline[0];
1677 *cmd_end = *cmd_start + strlen(cmdline);
1678
1679 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1680
Kumar Galae822d7f2008-02-27 21:51:49 -06001681 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001682}
John Rigbyfca43cc2010-10-13 13:57:35 -06001683#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001684
John Rigbyfca43cc2010-10-13 13:57:35 -06001685#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001686/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001687 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001688 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001689 * @kbd: double pointer to board info data
1690 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001691 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likely590d3ca2011-03-28 09:58:34 +00001692 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1693 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001694 *
1695 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001696 * 0 - success
1697 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001698 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001699int boot_get_kbd (struct lmb *lmb, bd_t **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001700{
Becky Bruce391fd932008-06-09 20:37:18 -05001701 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001702 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001703 if (*kbd == NULL)
1704 return -1;
1705
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001706 **kbd = *(gd->bd);
1707
1708 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1709
1710#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1711 do_bdinfo(NULL, 0, 0, NULL);
1712#endif
1713
Kumar Galae822d7f2008-02-27 21:51:49 -06001714 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001715}
John Rigbyfca43cc2010-10-13 13:57:35 -06001716#endif /* CONFIG_SYS_BOOT_GET_KBD */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001717#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001718
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001719#if defined(CONFIG_FIT)
1720/*****************************************************************************/
1721/* New uImage format routines */
1722/*****************************************************************************/
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001723#ifndef USE_HOSTCC
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001724static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1725 ulong *addr, const char **name)
1726{
1727 const char *sep;
1728
1729 *addr = addr_curr;
1730 *name = NULL;
1731
1732 sep = strchr (spec, sepc);
1733 if (sep) {
1734 if (sep - spec > 0)
1735 *addr = simple_strtoul (spec, NULL, 16);
1736
1737 *name = sep + 1;
1738 return 1;
1739 }
1740
1741 return 0;
1742}
1743
1744/**
1745 * fit_parse_conf - parse FIT configuration spec
1746 * @spec: input string, containing configuration spec
1747 * @add_curr: current image address (to be used as a possible default)
1748 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1749 * configuration
1750 * @conf_name double pointer to a char, will hold pointer to a configuration
1751 * unit name
1752 *
1753 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1754 * where <addr> is a FIT image address that contains configuration
1755 * with a <conf> unit name.
1756 *
1757 * Address part is optional, and if omitted default add_curr will
1758 * be used instead.
1759 *
1760 * returns:
1761 * 1 if spec is a valid configuration string,
1762 * addr and conf_name are set accordingly
1763 * 0 otherwise
1764 */
1765inline int fit_parse_conf (const char *spec, ulong addr_curr,
1766 ulong *addr, const char **conf_name)
1767{
1768 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1769}
1770
1771/**
1772 * fit_parse_subimage - parse FIT subimage spec
1773 * @spec: input string, containing subimage spec
1774 * @add_curr: current image address (to be used as a possible default)
1775 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1776 * subimage
1777 * @image_name: double pointer to a char, will hold pointer to a subimage name
1778 *
1779 * fit_parse_subimage() expects subimage spec in the for of
1780 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1781 * subimage with a <subimg> unit name.
1782 *
1783 * Address part is optional, and if omitted default add_curr will
1784 * be used instead.
1785 *
1786 * returns:
1787 * 1 if spec is a valid subimage string,
1788 * addr and image_name are set accordingly
1789 * 0 otherwise
1790 */
1791inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1792 ulong *addr, const char **image_name)
1793{
1794 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1795}
Marian Balakowicz570abb02008-02-29 15:59:59 +01001796#endif /* !USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001797
1798static void fit_get_debug (const void *fit, int noffset,
1799 char *prop_name, int err)
1800{
1801 debug ("Can't get '%s' property from FIT 0x%08lx, "
1802 "node: offset %d, name %s (%s)\n",
1803 prop_name, (ulong)fit, noffset,
1804 fit_get_name (fit, noffset, NULL),
1805 fdt_strerror (err));
1806}
1807
1808/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001809 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001810 * @fit: pointer to the FIT format image header
1811 * @p: pointer to prefix string
1812 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001813 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001814 * The routine prints out FIT image properties (root node level) follwed by
1815 * the details of each component image.
1816 *
1817 * returns:
1818 * no returned results
1819 */
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001820void fit_print_contents (const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001821{
1822 char *desc;
1823 char *uname;
1824 int images_noffset;
1825 int confs_noffset;
1826 int noffset;
1827 int ndepth;
1828 int count = 0;
1829 int ret;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001830 const char *p;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001831#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1832 time_t timestamp;
1833#endif
1834
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001835#ifdef USE_HOSTCC
1836 p = "";
1837#else
1838 p = " ";
1839#endif
1840
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001841 /* Root node properties */
1842 ret = fit_get_desc (fit, 0, &desc);
1843 printf ("%sFIT description: ", p);
1844 if (ret)
1845 printf ("unavailable\n");
1846 else
1847 printf ("%s\n", desc);
1848
1849#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1850 ret = fit_get_timestamp (fit, 0, &timestamp);
1851 printf ("%sCreated: ", p);
1852 if (ret)
1853 printf ("unavailable\n");
1854 else
1855 genimg_print_time (timestamp);
1856#endif
1857
1858 /* Find images parent node offset */
1859 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1860 if (images_noffset < 0) {
1861 printf ("Can't find images parent node '%s' (%s)\n",
1862 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1863 return;
1864 }
1865
1866 /* Process its subnodes, print out component images details */
1867 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1868 (noffset >= 0) && (ndepth > 0);
1869 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1870 if (ndepth == 1) {
1871 /*
1872 * Direct child node of the images parent node,
1873 * i.e. component image node.
1874 */
1875 printf ("%s Image %u (%s)\n", p, count++,
1876 fit_get_name(fit, noffset, NULL));
1877
1878 fit_image_print (fit, noffset, p);
1879 }
1880 }
1881
1882 /* Find configurations parent node offset */
1883 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1884 if (confs_noffset < 0) {
1885 debug ("Can't get configurations parent node '%s' (%s)\n",
1886 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1887 return;
1888 }
1889
1890 /* get default configuration unit name from default property */
1891 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1892 if (uname)
1893 printf ("%s Default Configuration: '%s'\n", p, uname);
1894
1895 /* Process its subnodes, print out configurations details */
1896 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1897 (noffset >= 0) && (ndepth > 0);
1898 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1899 if (ndepth == 1) {
1900 /*
1901 * Direct child node of the configurations parent node,
1902 * i.e. configuration node.
1903 */
1904 printf ("%s Configuration %u (%s)\n", p, count++,
1905 fit_get_name(fit, noffset, NULL));
1906
1907 fit_conf_print (fit, noffset, p);
1908 }
1909 }
1910}
1911
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001912/**
1913 * fit_image_print - prints out the FIT component image details
1914 * @fit: pointer to the FIT format image header
1915 * @image_noffset: offset of the component image node
1916 * @p: pointer to prefix string
1917 *
1918 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001919 * image. If present, hash nodes are printed out as well. Load
1920 * address for images of type firmware is also printed out. Since the load
1921 * address is not mandatory for firmware images, it will be output as
1922 * "unavailable" when not present.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001923 *
1924 * returns:
1925 * no returned results
1926 */
1927void fit_image_print (const void *fit, int image_noffset, const char *p)
1928{
1929 char *desc;
1930 uint8_t type, arch, os, comp;
1931 size_t size;
1932 ulong load, entry;
1933 const void *data;
1934 int noffset;
1935 int ndepth;
1936 int ret;
1937
1938 /* Mandatory properties */
1939 ret = fit_get_desc (fit, image_noffset, &desc);
1940 printf ("%s Description: ", p);
1941 if (ret)
1942 printf ("unavailable\n");
1943 else
1944 printf ("%s\n", desc);
1945
1946 fit_image_get_type (fit, image_noffset, &type);
1947 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1948
1949 fit_image_get_comp (fit, image_noffset, &comp);
1950 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1951
1952 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1953
1954#ifndef USE_HOSTCC
1955 printf ("%s Data Start: ", p);
1956 if (ret)
1957 printf ("unavailable\n");
1958 else
1959 printf ("0x%08lx\n", (ulong)data);
1960#endif
1961
1962 printf ("%s Data Size: ", p);
1963 if (ret)
1964 printf ("unavailable\n");
1965 else
1966 genimg_print_size (size);
1967
1968 /* Remaining, type dependent properties */
1969 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1970 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1971 (type == IH_TYPE_FLATDT)) {
1972 fit_image_get_arch (fit, image_noffset, &arch);
1973 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1974 }
1975
1976 if (type == IH_TYPE_KERNEL) {
1977 fit_image_get_os (fit, image_noffset, &os);
1978 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1979 }
1980
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001981 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1982 (type == IH_TYPE_FIRMWARE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001983 ret = fit_image_get_load (fit, image_noffset, &load);
1984 printf ("%s Load Address: ", p);
1985 if (ret)
1986 printf ("unavailable\n");
1987 else
1988 printf ("0x%08lx\n", load);
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001989 }
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001990
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001991 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001992 fit_image_get_entry (fit, image_noffset, &entry);
1993 printf ("%s Entry Point: ", p);
1994 if (ret)
1995 printf ("unavailable\n");
1996 else
1997 printf ("0x%08lx\n", entry);
1998 }
1999
2000 /* Process all hash subnodes of the component image node */
2001 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2002 (noffset >= 0) && (ndepth > 0);
2003 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2004 if (ndepth == 1) {
2005 /* Direct child node of the component image node */
2006 fit_image_print_hash (fit, noffset, p);
2007 }
2008 }
2009}
2010
2011/**
2012 * fit_image_print_hash - prints out the hash node details
2013 * @fit: pointer to the FIT format image header
2014 * @noffset: offset of the hash node
2015 * @p: pointer to prefix string
2016 *
2017 * fit_image_print_hash() lists properies for the processed hash node
2018 *
2019 * returns:
2020 * no returned results
2021 */
2022void fit_image_print_hash (const void *fit, int noffset, const char *p)
2023{
2024 char *algo;
2025 uint8_t *value;
2026 int value_len;
2027 int i, ret;
2028
2029 /*
2030 * Check subnode name, must be equal to "hash".
2031 * Multiple hash nodes require unique unit node
2032 * names, e.g. hash@1, hash@2, etc.
2033 */
2034 if (strncmp (fit_get_name(fit, noffset, NULL),
2035 FIT_HASH_NODENAME,
2036 strlen(FIT_HASH_NODENAME)) != 0)
2037 return;
2038
2039 debug ("%s Hash node: '%s'\n", p,
2040 fit_get_name (fit, noffset, NULL));
2041
2042 printf ("%s Hash algo: ", p);
2043 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2044 printf ("invalid/unsupported\n");
2045 return;
2046 }
2047 printf ("%s\n", algo);
2048
2049 ret = fit_image_hash_get_value (fit, noffset, &value,
2050 &value_len);
2051 printf ("%s Hash value: ", p);
2052 if (ret) {
2053 printf ("unavailable\n");
2054 } else {
2055 for (i = 0; i < value_len; i++)
2056 printf ("%02x", value[i]);
2057 printf ("\n");
2058 }
2059
2060 debug ("%s Hash len: %d\n", p, value_len);
2061}
2062
2063/**
2064 * fit_get_desc - get node description property
2065 * @fit: pointer to the FIT format image header
2066 * @noffset: node offset
2067 * @desc: double pointer to the char, will hold pointer to the descrption
2068 *
2069 * fit_get_desc() reads description property from a given node, if
2070 * description is found pointer to it is returened in third call argument.
2071 *
2072 * returns:
2073 * 0, on success
2074 * -1, on failure
2075 */
2076int fit_get_desc (const void *fit, int noffset, char **desc)
2077{
2078 int len;
2079
2080 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2081 if (*desc == NULL) {
2082 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2083 return -1;
2084 }
2085
2086 return 0;
2087}
2088
2089/**
2090 * fit_get_timestamp - get node timestamp property
2091 * @fit: pointer to the FIT format image header
2092 * @noffset: node offset
2093 * @timestamp: pointer to the time_t, will hold read timestamp
2094 *
2095 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2096 * is found and has a correct size its value is retured in third call
2097 * argument.
2098 *
2099 * returns:
2100 * 0, on success
2101 * -1, on property read failure
2102 * -2, on wrong timestamp size
2103 */
2104int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2105{
2106 int len;
2107 const void *data;
2108
2109 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2110 if (data == NULL) {
2111 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2112 return -1;
2113 }
2114 if (len != sizeof (uint32_t)) {
2115 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2116 return -2;
2117 }
2118
2119 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2120 return 0;
2121}
2122
2123/**
2124 * fit_image_get_node - get node offset for component image of a given unit name
2125 * @fit: pointer to the FIT format image header
2126 * @image_uname: component image node unit name
2127 *
2128 * fit_image_get_node() finds a component image (withing the '/images'
2129 * node) of a provided unit name. If image is found its node offset is
2130 * returned to the caller.
2131 *
2132 * returns:
2133 * image node offset when found (>=0)
2134 * negative number on failure (FDT_ERR_* code)
2135 */
2136int fit_image_get_node (const void *fit, const char *image_uname)
2137{
2138 int noffset, images_noffset;
2139
2140 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2141 if (images_noffset < 0) {
2142 debug ("Can't find images parent node '%s' (%s)\n",
2143 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2144 return images_noffset;
2145 }
2146
2147 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2148 if (noffset < 0) {
2149 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2150 image_uname, fdt_strerror (noffset));
2151 }
2152
2153 return noffset;
2154}
2155
2156/**
2157 * fit_image_get_os - get os id for a given component image node
2158 * @fit: pointer to the FIT format image header
2159 * @noffset: component image node offset
2160 * @os: pointer to the uint8_t, will hold os numeric id
2161 *
2162 * fit_image_get_os() finds os property in a given component image node.
2163 * If the property is found, its (string) value is translated to the numeric
2164 * id which is returned to the caller.
2165 *
2166 * returns:
2167 * 0, on success
2168 * -1, on failure
2169 */
2170int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2171{
2172 int len;
2173 const void *data;
2174
2175 /* Get OS name from property data */
2176 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2177 if (data == NULL) {
2178 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2179 *os = -1;
2180 return -1;
2181 }
2182
2183 /* Translate OS name to id */
2184 *os = genimg_get_os_id (data);
2185 return 0;
2186}
2187
2188/**
2189 * fit_image_get_arch - get arch id for a given component image node
2190 * @fit: pointer to the FIT format image header
2191 * @noffset: component image node offset
2192 * @arch: pointer to the uint8_t, will hold arch numeric id
2193 *
2194 * fit_image_get_arch() finds arch property in a given component image node.
2195 * If the property is found, its (string) value is translated to the numeric
2196 * id which is returned to the caller.
2197 *
2198 * returns:
2199 * 0, on success
2200 * -1, on failure
2201 */
2202int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2203{
2204 int len;
2205 const void *data;
2206
2207 /* Get architecture name from property data */
2208 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2209 if (data == NULL) {
2210 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2211 *arch = -1;
2212 return -1;
2213 }
2214
2215 /* Translate architecture name to id */
2216 *arch = genimg_get_arch_id (data);
2217 return 0;
2218}
2219
2220/**
2221 * fit_image_get_type - get type id for a given component image node
2222 * @fit: pointer to the FIT format image header
2223 * @noffset: component image node offset
2224 * @type: pointer to the uint8_t, will hold type numeric id
2225 *
2226 * fit_image_get_type() finds type property in a given component image node.
2227 * If the property is found, its (string) value is translated to the numeric
2228 * id which is returned to the caller.
2229 *
2230 * returns:
2231 * 0, on success
2232 * -1, on failure
2233 */
2234int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2235{
2236 int len;
2237 const void *data;
2238
2239 /* Get image type name from property data */
2240 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2241 if (data == NULL) {
2242 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2243 *type = -1;
2244 return -1;
2245 }
2246
2247 /* Translate image type name to id */
2248 *type = genimg_get_type_id (data);
2249 return 0;
2250}
2251
2252/**
2253 * fit_image_get_comp - get comp id for a given component image node
2254 * @fit: pointer to the FIT format image header
2255 * @noffset: component image node offset
2256 * @comp: pointer to the uint8_t, will hold comp numeric id
2257 *
2258 * fit_image_get_comp() finds comp property in a given component image node.
2259 * If the property is found, its (string) value is translated to the numeric
2260 * id which is returned to the caller.
2261 *
2262 * returns:
2263 * 0, on success
2264 * -1, on failure
2265 */
2266int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2267{
2268 int len;
2269 const void *data;
2270
2271 /* Get compression name from property data */
2272 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2273 if (data == NULL) {
2274 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2275 *comp = -1;
2276 return -1;
2277 }
2278
2279 /* Translate compression name to id */
2280 *comp = genimg_get_comp_id (data);
2281 return 0;
2282}
2283
2284/**
2285 * fit_image_get_load - get load address property for a given component image node
2286 * @fit: pointer to the FIT format image header
2287 * @noffset: component image node offset
2288 * @load: pointer to the uint32_t, will hold load address
2289 *
2290 * fit_image_get_load() finds load address property in a given component image node.
2291 * If the property is found, its value is returned to the caller.
2292 *
2293 * returns:
2294 * 0, on success
2295 * -1, on failure
2296 */
2297int fit_image_get_load (const void *fit, int noffset, ulong *load)
2298{
2299 int len;
2300 const uint32_t *data;
2301
2302 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2303 if (data == NULL) {
2304 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2305 return -1;
2306 }
2307
2308 *load = uimage_to_cpu (*data);
2309 return 0;
2310}
2311
2312/**
2313 * fit_image_get_entry - get entry point address property for a given component image node
2314 * @fit: pointer to the FIT format image header
2315 * @noffset: component image node offset
2316 * @entry: pointer to the uint32_t, will hold entry point address
2317 *
2318 * fit_image_get_entry() finds entry point address property in a given component image node.
2319 * If the property is found, its value is returned to the caller.
2320 *
2321 * returns:
2322 * 0, on success
2323 * -1, on failure
2324 */
2325int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2326{
2327 int len;
2328 const uint32_t *data;
2329
2330 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2331 if (data == NULL) {
2332 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2333 return -1;
2334 }
2335
2336 *entry = uimage_to_cpu (*data);
2337 return 0;
2338}
2339
2340/**
2341 * fit_image_get_data - get data property and its size for a given component image node
2342 * @fit: pointer to the FIT format image header
2343 * @noffset: component image node offset
2344 * @data: double pointer to void, will hold data property's data address
2345 * @size: pointer to size_t, will hold data property's data size
2346 *
2347 * fit_image_get_data() finds data property in a given component image node.
2348 * If the property is found its data start address and size are returned to
2349 * the caller.
2350 *
2351 * returns:
2352 * 0, on success
2353 * -1, on failure
2354 */
2355int fit_image_get_data (const void *fit, int noffset,
2356 const void **data, size_t *size)
2357{
2358 int len;
2359
2360 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2361 if (*data == NULL) {
2362 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2363 *size = 0;
2364 return -1;
2365 }
2366
2367 *size = len;
2368 return 0;
2369}
2370
2371/**
2372 * fit_image_hash_get_algo - get hash algorithm name
2373 * @fit: pointer to the FIT format image header
2374 * @noffset: hash node offset
2375 * @algo: double pointer to char, will hold pointer to the algorithm name
2376 *
2377 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2378 * If the property is found its data start address is returned to the caller.
2379 *
2380 * returns:
2381 * 0, on success
2382 * -1, on failure
2383 */
2384int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2385{
2386 int len;
2387
2388 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2389 if (*algo == NULL) {
2390 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2391 return -1;
2392 }
2393
2394 return 0;
2395}
2396
2397/**
2398 * fit_image_hash_get_value - get hash value and length
2399 * @fit: pointer to the FIT format image header
2400 * @noffset: hash node offset
2401 * @value: double pointer to uint8_t, will hold address of a hash value data
2402 * @value_len: pointer to an int, will hold hash data length
2403 *
2404 * fit_image_hash_get_value() finds hash value property in a given hash node.
2405 * If the property is found its data start address and size are returned to
2406 * the caller.
2407 *
2408 * returns:
2409 * 0, on success
2410 * -1, on failure
2411 */
2412int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2413 int *value_len)
2414{
2415 int len;
2416
2417 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2418 if (*value == NULL) {
2419 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2420 *value_len = 0;
2421 return -1;
2422 }
2423
2424 *value_len = len;
2425 return 0;
2426}
2427
2428/**
2429 * fit_set_timestamp - set node timestamp property
2430 * @fit: pointer to the FIT format image header
2431 * @noffset: node offset
2432 * @timestamp: timestamp value to be set
2433 *
2434 * fit_set_timestamp() attempts to set timestamp property in the requested
2435 * node and returns operation status to the caller.
2436 *
2437 * returns:
2438 * 0, on success
2439 * -1, on property read failure
2440 */
2441int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2442{
2443 uint32_t t;
2444 int ret;
2445
2446 t = cpu_to_uimage (timestamp);
2447 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2448 sizeof (uint32_t));
2449 if (ret) {
2450 printf ("Can't set '%s' property for '%s' node (%s)\n",
2451 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2452 fdt_strerror (ret));
2453 return -1;
2454 }
2455
2456 return 0;
2457}
2458
2459/**
2460 * calculate_hash - calculate and return hash for provided input data
2461 * @data: pointer to the input data
2462 * @data_len: data length
2463 * @algo: requested hash algorithm
2464 * @value: pointer to the char, will hold hash value data (caller must
2465 * allocate enough free space)
2466 * value_len: length of the calculated hash
2467 *
2468 * calculate_hash() computes input data hash according to the requested algorithm.
2469 * Resulting hash value is placed in caller provided 'value' buffer, length
2470 * of the calculated hash is returned via value_len pointer argument.
2471 *
2472 * returns:
2473 * 0, on success
2474 * -1, when algo is unsupported
2475 */
2476static int calculate_hash (const void *data, int data_len, const char *algo,
2477 uint8_t *value, int *value_len)
2478{
2479 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002480 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2481 CHUNKSZ_CRC32);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002482 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2483 *value_len = 4;
2484 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002485 sha1_csum_wd ((unsigned char *) data, data_len,
2486 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002487 *value_len = 20;
2488 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002489 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka766529f2008-03-14 16:22:34 +01002490 *value_len = 16;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002491 } else {
2492 debug ("Unsupported hash alogrithm\n");
2493 return -1;
2494 }
2495 return 0;
2496}
2497
2498#ifdef USE_HOSTCC
2499/**
2500 * fit_set_hashes - process FIT component image nodes and calculate hashes
2501 * @fit: pointer to the FIT format image header
2502 *
2503 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2504 * Hashes are calculated for all component images which have hash subnodes
2505 * with algorithm property set to one of the supported hash algorithms.
2506 *
2507 * returns
2508 * 0, on success
2509 * libfdt error code, on failure
2510 */
2511int fit_set_hashes (void *fit)
2512{
2513 int images_noffset;
2514 int noffset;
2515 int ndepth;
2516 int ret;
2517
2518 /* Find images parent node offset */
2519 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2520 if (images_noffset < 0) {
2521 printf ("Can't find images parent node '%s' (%s)\n",
2522 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2523 return images_noffset;
2524 }
2525
2526 /* Process its subnodes, print out component images details */
2527 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2528 (noffset >= 0) && (ndepth > 0);
2529 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2530 if (ndepth == 1) {
2531 /*
2532 * Direct child node of the images parent node,
2533 * i.e. component image node.
2534 */
2535 ret = fit_image_set_hashes (fit, noffset);
2536 if (ret)
2537 return ret;
2538 }
2539 }
2540
2541 return 0;
2542}
2543
2544/**
2545 * fit_image_set_hashes - calculate/set hashes for given component image node
2546 * @fit: pointer to the FIT format image header
2547 * @image_noffset: requested component image node
2548 *
2549 * fit_image_set_hashes() adds hash values for an component image node. All
2550 * existing hash subnodes are checked, if algorithm property is set to one of
2551 * the supported hash algorithms, hash value is computed and corresponding
2552 * hash node property is set, for example:
2553 *
2554 * Input component image node structure:
2555 *
2556 * o image@1 (at image_noffset)
2557 * | - data = [binary data]
2558 * o hash@1
2559 * |- algo = "sha1"
2560 *
2561 * Output component image node structure:
2562 *
2563 * o image@1 (at image_noffset)
2564 * | - data = [binary data]
2565 * o hash@1
2566 * |- algo = "sha1"
2567 * |- value = sha1(data)
2568 *
2569 * returns:
2570 * 0 on sucess
2571 * <0 on failure
2572 */
2573int fit_image_set_hashes (void *fit, int image_noffset)
2574{
2575 const void *data;
2576 size_t size;
2577 char *algo;
2578 uint8_t value[FIT_MAX_HASH_LEN];
2579 int value_len;
2580 int noffset;
2581 int ndepth;
2582
2583 /* Get image data and data length */
2584 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2585 printf ("Can't get image data/size\n");
2586 return -1;
2587 }
2588
2589 /* Process all hash subnodes of the component image node */
2590 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2591 (noffset >= 0) && (ndepth > 0);
2592 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2593 if (ndepth == 1) {
2594 /* Direct child node of the component image node */
2595
2596 /*
2597 * Check subnode name, must be equal to "hash".
2598 * Multiple hash nodes require unique unit node
2599 * names, e.g. hash@1, hash@2, etc.
2600 */
2601 if (strncmp (fit_get_name(fit, noffset, NULL),
2602 FIT_HASH_NODENAME,
2603 strlen(FIT_HASH_NODENAME)) != 0) {
2604 /* Not a hash subnode, skip it */
2605 continue;
2606 }
2607
2608 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2609 printf ("Can't get hash algo property for "
2610 "'%s' hash node in '%s' image node\n",
2611 fit_get_name (fit, noffset, NULL),
2612 fit_get_name (fit, image_noffset, NULL));
2613 return -1;
2614 }
2615
2616 if (calculate_hash (data, size, algo, value, &value_len)) {
2617 printf ("Unsupported hash algorithm (%s) for "
2618 "'%s' hash node in '%s' image node\n",
2619 algo, fit_get_name (fit, noffset, NULL),
2620 fit_get_name (fit, image_noffset, NULL));
2621 return -1;
2622 }
2623
2624 if (fit_image_hash_set_value (fit, noffset, value,
2625 value_len)) {
2626 printf ("Can't set hash value for "
2627 "'%s' hash node in '%s' image node\n",
2628 fit_get_name (fit, noffset, NULL),
2629 fit_get_name (fit, image_noffset, NULL));
2630 return -1;
2631 }
2632 }
2633 }
2634
2635 return 0;
2636}
2637
2638/**
2639 * fit_image_hash_set_value - set hash value in requested has node
2640 * @fit: pointer to the FIT format image header
2641 * @noffset: hash node offset
2642 * @value: hash value to be set
2643 * @value_len: hash value length
2644 *
2645 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2646 * given and returns operation status to the caller.
2647 *
2648 * returns
2649 * 0, on success
2650 * -1, on failure
2651 */
2652int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2653 int value_len)
2654{
2655 int ret;
2656
2657 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2658 if (ret) {
2659 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2660 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2661 fdt_strerror (ret));
2662 return -1;
2663 }
2664
2665 return 0;
2666}
2667#endif /* USE_HOSTCC */
2668
2669/**
2670 * fit_image_check_hashes - verify data intergity
2671 * @fit: pointer to the FIT format image header
2672 * @image_noffset: component image node offset
2673 *
2674 * fit_image_check_hashes() goes over component image hash nodes,
2675 * re-calculates each data hash and compares with the value stored in hash
2676 * node.
2677 *
2678 * returns:
2679 * 1, if all hashes are valid
2680 * 0, otherwise (or on error)
2681 */
2682int fit_image_check_hashes (const void *fit, int image_noffset)
2683{
2684 const void *data;
2685 size_t size;
2686 char *algo;
2687 uint8_t *fit_value;
2688 int fit_value_len;
2689 uint8_t value[FIT_MAX_HASH_LEN];
2690 int value_len;
2691 int noffset;
2692 int ndepth;
2693 char *err_msg = "";
2694
2695 /* Get image data and data length */
2696 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2697 printf ("Can't get image data/size\n");
2698 return 0;
2699 }
2700
2701 /* Process all hash subnodes of the component image node */
2702 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2703 (noffset >= 0) && (ndepth > 0);
2704 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2705 if (ndepth == 1) {
2706 /* Direct child node of the component image node */
2707
2708 /*
2709 * Check subnode name, must be equal to "hash".
2710 * Multiple hash nodes require unique unit node
2711 * names, e.g. hash@1, hash@2, etc.
2712 */
2713 if (strncmp (fit_get_name(fit, noffset, NULL),
2714 FIT_HASH_NODENAME,
2715 strlen(FIT_HASH_NODENAME)) != 0)
2716 continue;
2717
2718 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002719 err_msg = " error!\nCan't get hash algo "
2720 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002721 goto error;
2722 }
2723 printf ("%s", algo);
2724
2725 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2726 &fit_value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002727 err_msg = " error!\nCan't get hash value "
2728 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002729 goto error;
2730 }
2731
2732 if (calculate_hash (data, size, algo, value, &value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002733 err_msg = " error!\nUnsupported hash algorithm";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002734 goto error;
2735 }
2736
2737 if (value_len != fit_value_len) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002738 err_msg = " error !\nBad hash value len";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002739 goto error;
2740 } else if (memcmp (value, fit_value, value_len) != 0) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002741 err_msg = " error!\nBad hash value";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002742 goto error;
2743 }
2744 printf ("+ ");
2745 }
2746 }
2747
2748 return 1;
2749
2750error:
2751 printf ("%s for '%s' hash node in '%s' image node\n",
2752 err_msg, fit_get_name (fit, noffset, NULL),
2753 fit_get_name (fit, image_noffset, NULL));
2754 return 0;
2755}
2756
2757/**
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002758 * fit_all_image_check_hashes - verify data intergity for all images
2759 * @fit: pointer to the FIT format image header
2760 *
2761 * fit_all_image_check_hashes() goes over all images in the FIT and
2762 * for every images checks if all it's hashes are valid.
2763 *
2764 * returns:
2765 * 1, if all hashes of all images are valid
2766 * 0, otherwise (or on error)
2767 */
2768int fit_all_image_check_hashes (const void *fit)
2769{
2770 int images_noffset;
2771 int noffset;
2772 int ndepth;
2773 int count;
2774
2775 /* Find images parent node offset */
2776 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2777 if (images_noffset < 0) {
2778 printf ("Can't find images parent node '%s' (%s)\n",
2779 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2780 return 0;
2781 }
2782
2783 /* Process all image subnodes, check hashes for each */
2784 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2785 (ulong)fit);
2786 for (ndepth = 0, count = 0,
2787 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2788 (noffset >= 0) && (ndepth > 0);
2789 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2790 if (ndepth == 1) {
2791 /*
2792 * Direct child node of the images parent node,
2793 * i.e. component image node.
2794 */
2795 printf (" Hash(es) for Image %u (%s): ", count++,
2796 fit_get_name (fit, noffset, NULL));
2797
2798 if (!fit_image_check_hashes (fit, noffset))
2799 return 0;
2800 printf ("\n");
2801 }
2802 }
2803 return 1;
2804}
2805
2806/**
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002807 * fit_image_check_os - check whether image node is of a given os type
2808 * @fit: pointer to the FIT format image header
2809 * @noffset: component image node offset
2810 * @os: requested image os
2811 *
2812 * fit_image_check_os() reads image os property and compares its numeric
2813 * id with the requested os. Comparison result is returned to the caller.
2814 *
2815 * returns:
2816 * 1 if image is of given os type
2817 * 0 otherwise (or on error)
2818 */
2819int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2820{
2821 uint8_t image_os;
2822
2823 if (fit_image_get_os (fit, noffset, &image_os))
2824 return 0;
2825 return (os == image_os);
2826}
2827
2828/**
2829 * fit_image_check_arch - check whether image node is of a given arch
2830 * @fit: pointer to the FIT format image header
2831 * @noffset: component image node offset
2832 * @arch: requested imagearch
2833 *
2834 * fit_image_check_arch() reads image arch property and compares its numeric
2835 * id with the requested arch. Comparison result is returned to the caller.
2836 *
2837 * returns:
2838 * 1 if image is of given arch
2839 * 0 otherwise (or on error)
2840 */
2841int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2842{
2843 uint8_t image_arch;
2844
2845 if (fit_image_get_arch (fit, noffset, &image_arch))
2846 return 0;
2847 return (arch == image_arch);
2848}
2849
2850/**
2851 * fit_image_check_type - check whether image node is of a given type
2852 * @fit: pointer to the FIT format image header
2853 * @noffset: component image node offset
2854 * @type: requested image type
2855 *
2856 * fit_image_check_type() reads image type property and compares its numeric
2857 * id with the requested type. Comparison result is returned to the caller.
2858 *
2859 * returns:
2860 * 1 if image is of given type
2861 * 0 otherwise (or on error)
2862 */
2863int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2864{
2865 uint8_t image_type;
2866
2867 if (fit_image_get_type (fit, noffset, &image_type))
2868 return 0;
2869 return (type == image_type);
2870}
2871
2872/**
2873 * fit_image_check_comp - check whether image node uses given compression
2874 * @fit: pointer to the FIT format image header
2875 * @noffset: component image node offset
2876 * @comp: requested image compression type
2877 *
2878 * fit_image_check_comp() reads image compression property and compares its
2879 * numeric id with the requested compression type. Comparison result is
2880 * returned to the caller.
2881 *
2882 * returns:
2883 * 1 if image uses requested compression
2884 * 0 otherwise (or on error)
2885 */
2886int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2887{
2888 uint8_t image_comp;
2889
2890 if (fit_image_get_comp (fit, noffset, &image_comp))
2891 return 0;
2892 return (comp == image_comp);
2893}
2894
2895/**
2896 * fit_check_format - sanity check FIT image format
2897 * @fit: pointer to the FIT format image header
2898 *
2899 * fit_check_format() runs a basic sanity FIT image verification.
2900 * Routine checks for mandatory properties, nodes, etc.
2901 *
2902 * returns:
2903 * 1, on success
2904 * 0, on failure
2905 */
2906int fit_check_format (const void *fit)
2907{
2908 /* mandatory / node 'description' property */
2909 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2910 debug ("Wrong FIT format: no description\n");
2911 return 0;
2912 }
2913
2914#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2915 /* mandatory / node 'timestamp' property */
2916 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002917 debug ("Wrong FIT format: no timestamp\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002918 return 0;
2919 }
2920#endif
2921
2922 /* mandatory subimages parent '/images' node */
2923 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2924 debug ("Wrong FIT format: no images parent node\n");
2925 return 0;
2926 }
2927
2928 return 1;
2929}
2930
2931/**
2932 * fit_conf_get_node - get node offset for configuration of a given unit name
2933 * @fit: pointer to the FIT format image header
2934 * @conf_uname: configuration node unit name
2935 *
2936 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2937 * parant node) of a provided unit name. If configuration is found its node offset
2938 * is returned to the caller.
2939 *
2940 * When NULL is provided in second argument fit_conf_get_node() will search
2941 * for a default configuration node instead. Default configuration node unit name
2942 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2943 *
2944 * returns:
2945 * configuration node offset when found (>=0)
2946 * negative number on failure (FDT_ERR_* code)
2947 */
2948int fit_conf_get_node (const void *fit, const char *conf_uname)
2949{
2950 int noffset, confs_noffset;
2951 int len;
2952
2953 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2954 if (confs_noffset < 0) {
2955 debug ("Can't find configurations parent node '%s' (%s)\n",
2956 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2957 return confs_noffset;
2958 }
2959
2960 if (conf_uname == NULL) {
2961 /* get configuration unit name from the default property */
2962 debug ("No configuration specified, trying default...\n");
2963 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2964 if (conf_uname == NULL) {
2965 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2966 return len;
2967 }
2968 debug ("Found default configuration: '%s'\n", conf_uname);
2969 }
2970
2971 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2972 if (noffset < 0) {
2973 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2974 conf_uname, fdt_strerror (noffset));
2975 }
2976
2977 return noffset;
2978}
2979
2980static int __fit_conf_get_prop_node (const void *fit, int noffset,
2981 const char *prop_name)
2982{
2983 char *uname;
2984 int len;
2985
2986 /* get kernel image unit name from configuration kernel property */
2987 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2988 if (uname == NULL)
2989 return len;
2990
2991 return fit_image_get_node (fit, uname);
2992}
2993
2994/**
2995 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2996 * a given configuration
2997 * @fit: pointer to the FIT format image header
2998 * @noffset: configuration node offset
2999 *
3000 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3001 * configuration FIT_KERNEL_PROP property and translates it to the node
3002 * offset.
3003 *
3004 * returns:
3005 * image node offset when found (>=0)
3006 * negative number on failure (FDT_ERR_* code)
3007 */
3008int fit_conf_get_kernel_node (const void *fit, int noffset)
3009{
3010 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
3011}
3012
3013/**
3014 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3015 * a given configuration
3016 * @fit: pointer to the FIT format image header
3017 * @noffset: configuration node offset
3018 *
3019 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3020 * configuration FIT_KERNEL_PROP property and translates it to the node
3021 * offset.
3022 *
3023 * returns:
3024 * image node offset when found (>=0)
3025 * negative number on failure (FDT_ERR_* code)
3026 */
3027int fit_conf_get_ramdisk_node (const void *fit, int noffset)
3028{
3029 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
3030}
3031
3032/**
3033 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3034 * a given configuration
3035 * @fit: pointer to the FIT format image header
3036 * @noffset: configuration node offset
3037 *
3038 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3039 * configuration FIT_KERNEL_PROP property and translates it to the node
3040 * offset.
3041 *
3042 * returns:
3043 * image node offset when found (>=0)
3044 * negative number on failure (FDT_ERR_* code)
3045 */
3046int fit_conf_get_fdt_node (const void *fit, int noffset)
3047{
3048 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
3049}
3050
3051/**
3052 * fit_conf_print - prints out the FIT configuration details
3053 * @fit: pointer to the FIT format image header
Marian Balakowiczf773bea2008-03-12 10:35:46 +01003054 * @noffset: offset of the configuration node
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003055 * @p: pointer to prefix string
3056 *
3057 * fit_conf_print() lists all mandatory properies for the processed
3058 * configuration node.
3059 *
3060 * returns:
3061 * no returned results
3062 */
3063void fit_conf_print (const void *fit, int noffset, const char *p)
3064{
3065 char *desc;
3066 char *uname;
3067 int ret;
3068
3069 /* Mandatory properties */
3070 ret = fit_get_desc (fit, noffset, &desc);
3071 printf ("%s Description: ", p);
3072 if (ret)
3073 printf ("unavailable\n");
3074 else
3075 printf ("%s\n", desc);
3076
3077 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3078 printf ("%s Kernel: ", p);
3079 if (uname == NULL)
3080 printf ("unavailable\n");
3081 else
3082 printf ("%s\n", uname);
3083
3084 /* Optional properties */
3085 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3086 if (uname)
3087 printf ("%s Init Ramdisk: %s\n", p, uname);
3088
3089 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3090 if (uname)
3091 printf ("%s FDT: %s\n", p, uname);
3092}
Marian Balakowiczc8779642008-03-12 10:12:37 +01003093
3094/**
3095 * fit_check_ramdisk - verify FIT format ramdisk subimage
3096 * @fit_hdr: pointer to the FIT ramdisk header
3097 * @rd_noffset: ramdisk subimage node offset within FIT image
3098 * @arch: requested ramdisk image architecture type
3099 * @verify: data CRC verification flag
3100 *
3101 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3102 * specified FIT image.
3103 *
3104 * returns:
3105 * 1, on success
3106 * 0, on failure
3107 */
3108#ifndef USE_HOSTCC
3109static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3110{
3111 fit_image_print (fit, rd_noffset, " ");
3112
3113 if (verify) {
3114 puts (" Verifying Hash Integrity ... ");
3115 if (!fit_image_check_hashes (fit, rd_noffset)) {
3116 puts ("Bad Data Hash\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003117 show_boot_progress (-125);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003118 return 0;
3119 }
3120 puts ("OK\n");
3121 }
3122
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003123 show_boot_progress (126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003124 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3125 !fit_image_check_arch (fit, rd_noffset, arch) ||
3126 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3127 printf ("No Linux %s Ramdisk Image\n",
3128 genimg_get_arch_name(arch));
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003129 show_boot_progress (-126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003130 return 0;
3131 }
3132
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003133 show_boot_progress (127);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003134 return 1;
3135}
3136#endif /* USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003137#endif /* CONFIG_FIT */