blob: c86b86fbe70f4316d95e0c5e74dff31b9ce7f527 [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",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100144 { -1, "", "", },
145};
146
Mike Frysinger7edb1862010-10-20 07:17:39 -0400147static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100148 { IH_COMP_NONE, "none", "uncompressed", },
149 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
150 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200151 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100152 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100153 { -1, "", "", },
154};
155
Mike Frysinger89cdab72008-03-31 11:02:01 -0400156uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200157uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100158#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
159static void genimg_print_time (time_t timestamp);
160#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100161
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100162/*****************************************************************************/
163/* Legacy format routines */
164/*****************************************************************************/
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200165int image_check_hcrc (const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100166{
167 ulong hcrc;
168 ulong len = image_get_header_size ();
169 image_header_t header;
170
171 /* Copy header so we can blank CRC field for re-calculation */
172 memmove (&header, (char *)hdr, image_get_header_size ());
173 image_set_hcrc (&header, 0);
174
175 hcrc = crc32 (0, (unsigned char *)&header, len);
176
177 return (hcrc == image_get_hcrc (hdr));
178}
179
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200180int image_check_dcrc (const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100181{
182 ulong data = image_get_data (hdr);
183 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200184 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100185
186 return (dcrc == image_get_dcrc (hdr));
187}
188
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100189/**
190 * image_multi_count - get component (sub-image) count
191 * @hdr: pointer to the header of the multi component image
192 *
193 * image_multi_count() returns number of components in a multi
194 * component image.
195 *
196 * Note: no checking of the image type is done, caller must pass
197 * a valid multi component image.
198 *
199 * returns:
200 * number of components
201 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200202ulong image_multi_count (const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100203{
204 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100205 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100206
207 /* get start of the image payload, which in case of multi
208 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100209 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100210
211 /* count non empty slots */
212 for (i = 0; size[i]; ++i)
213 count++;
214
215 return count;
216}
217
218/**
219 * image_multi_getimg - get component data address and size
220 * @hdr: pointer to the header of the multi component image
221 * @idx: index of the requested component
222 * @data: pointer to a ulong variable, will hold component data address
223 * @len: pointer to a ulong variable, will hold component size
224 *
225 * image_multi_getimg() returns size and data address for the requested
226 * component in a multi component image.
227 *
228 * Note: no checking of the image type is done, caller must pass
229 * a valid multi component image.
230 *
231 * returns:
232 * data address and size of the component, if idx is valid
233 * 0 in data and len, if idx is out of range
234 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200235void image_multi_getimg (const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100236 ulong *data, ulong *len)
237{
238 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100239 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700240 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100241
242 /* get number of component */
243 count = image_multi_count (hdr);
244
245 /* get start of the image payload, which in case of multi
246 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100247 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100248
249 /* get address of the proper component data start, which means
250 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100251 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100252
253 if (idx < count) {
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100254 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100255 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100256
257 /* go over all indices preceding requested component idx */
258 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700259 /* add up i-th component size, rounding up to 4 bytes */
260 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100261 }
262
263 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700264 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100265 } else {
266 *len = 0;
267 *data = 0;
268 }
269}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100270
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200271static void image_print_type (const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100272{
273 const char *os, *arch, *type, *comp;
274
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100275 os = genimg_get_os_name (image_get_os (hdr));
276 arch = genimg_get_arch_name (image_get_arch (hdr));
277 type = genimg_get_type_name (image_get_type (hdr));
278 comp = genimg_get_comp_name (image_get_comp (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100279
Marian Balakowicz570abb02008-02-29 15:59:59 +0100280 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100281}
282
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100283/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200284 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200285 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100286 * @p: pointer to prefix string
287 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200288 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100289 * The routine prints out all header fields followed by the size/offset data
290 * for MULTI/SCRIPT images.
291 *
292 * returns:
293 * no returned results
294 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200295void image_print_contents (const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100296{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200297 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200298 const char *p;
299
300#ifdef USE_HOSTCC
301 p = "";
302#else
303 p = " ";
304#endif
305
Marian Balakowicz570abb02008-02-29 15:59:59 +0100306 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
307#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
308 printf ("%sCreated: ", p);
309 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100310#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100311 printf ("%sImage Type: ", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100312 image_print_type (hdr);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100313 printf ("%sData Size: ", p);
314 genimg_print_size (image_get_data_size (hdr));
315 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
316 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100317
Marian Balakowicz570abb02008-02-29 15:59:59 +0100318 if (image_check_type (hdr, IH_TYPE_MULTI) ||
319 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100320 int i;
321 ulong data, len;
322 ulong count = image_multi_count (hdr);
323
Marian Balakowicz570abb02008-02-29 15:59:59 +0100324 printf ("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100325 for (i = 0; i < count; i++) {
326 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100327
328 printf ("%s Image %d: ", p, i);
329 genimg_print_size (len);
330
331 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
332 /*
333 * the user may need to know offsets
334 * if planning to do something with
335 * multiple files
336 */
337 printf ("%s Offset = 0x%08lx\n", p, data);
338 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100339 }
340 }
341}
342
Marian Balakowicz570abb02008-02-29 15:59:59 +0100343
344#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100345/**
346 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100347 * @rd_addr: ramdisk image start address
348 * @arch: expected ramdisk architecture
349 * @verify: checksum verification flag
350 *
351 * image_get_ramdisk() returns a pointer to the verified ramdisk image
352 * header. Routine receives image start address and expected architecture
353 * flag. Verification done covers data and header integrity and os/type/arch
354 * fields checking.
355 *
356 * If dataflash support is enabled routine checks for dataflash addresses
357 * and handles required dataflash reads.
358 *
359 * returns:
360 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600361 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100362 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200363static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100364 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100365{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200366 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100367
368 if (!image_check_magic (rd_hdr)) {
369 puts ("Bad Magic Number\n");
370 show_boot_progress (-10);
Kumar Gala274cea22008-02-27 21:51:46 -0600371 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100372 }
373
374 if (!image_check_hcrc (rd_hdr)) {
375 puts ("Bad Header Checksum\n");
376 show_boot_progress (-11);
Kumar Gala274cea22008-02-27 21:51:46 -0600377 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100378 }
379
380 show_boot_progress (10);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100381 image_print_contents (rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100382
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100383 if (verify) {
384 puts(" Verifying Checksum ... ");
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200385 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100386 puts ("Bad Data CRC\n");
387 show_boot_progress (-12);
Kumar Gala274cea22008-02-27 21:51:46 -0600388 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100389 }
390 puts("OK\n");
391 }
392
393 show_boot_progress (11);
394
395 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
396 !image_check_arch (rd_hdr, arch) ||
397 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
398 printf ("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100399 genimg_get_arch_name(arch));
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100400 show_boot_progress (-13);
Kumar Gala274cea22008-02-27 21:51:46 -0600401 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100402 }
403
404 return rd_hdr;
405}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100406#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100407
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100408/*****************************************************************************/
409/* Shared dual-format routines */
410/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100411#ifndef USE_HOSTCC
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200412int getenv_yesno (char *var)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100413{
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200414 char *s = getenv (var);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100415 return (s && (*s == 'n')) ? 0 : 1;
416}
417
418ulong getenv_bootm_low(void)
419{
420 char *s = getenv ("bootm_low");
421 if (s) {
422 ulong tmp = simple_strtoul (s, NULL, 16);
423 return tmp;
424 }
425
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200426#if defined(CONFIG_SYS_SDRAM_BASE)
427 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100428#elif defined(CONFIG_ARM)
429 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100430#else
431 return 0;
432#endif
433}
434
Becky Bruce391fd932008-06-09 20:37:18 -0500435phys_size_t getenv_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100436{
Matthew McClintockc519fac2010-07-08 10:11:08 -0500437 phys_size_t tmp;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100438 char *s = getenv ("bootm_size");
439 if (s) {
Becky Bruce391fd932008-06-09 20:37:18 -0500440 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100441 return tmp;
442 }
Matthew McClintockc519fac2010-07-08 10:11:08 -0500443 s = getenv("bootm_low");
444 if (s)
445 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
446 else
447 tmp = 0;
448
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100449
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100450#if defined(CONFIG_ARM)
Matthew McClintockc519fac2010-07-08 10:11:08 -0500451 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100452#else
Matthew McClintockc519fac2010-07-08 10:11:08 -0500453 return gd->bd->bi_memsize - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100454#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100455}
456
457void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
458{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400459 if (to == from)
460 return;
461
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100462#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
463 while (len > 0) {
464 size_t tail = (len > chunksz) ? chunksz : len;
465 WATCHDOG_RESET ();
466 memmove (to, from, tail);
467 to += tail;
468 from += tail;
469 len -= tail;
470 }
471#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
472 memmove (to, from, len);
473#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
474}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100475#endif /* !USE_HOSTCC */
476
Prafulla Wadaskarf666dea2009-09-07 14:59:07 +0530477void genimg_print_size (uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100478{
479#ifndef USE_HOSTCC
480 printf ("%d Bytes = ", size);
481 print_size (size, "\n");
482#else
483 printf ("%d Bytes = %.2f kB = %.2f MB\n",
484 size, (double)size / 1.024e3,
485 (double)size / 1.048576e6);
486#endif
487}
488
489#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
490static void genimg_print_time (time_t timestamp)
491{
492#ifndef USE_HOSTCC
493 struct rtc_time tm;
494
495 to_tm (timestamp, &tm);
496 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
497 tm.tm_year, tm.tm_mon, tm.tm_mday,
498 tm.tm_hour, tm.tm_min, tm.tm_sec);
499#else
500 printf ("%s", ctime(&timestamp));
501#endif
502}
503#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
504
505/**
506 * get_table_entry_name - translate entry id to long name
507 * @table: pointer to a translation table for entries of a specific type
508 * @msg: message to be returned when translation fails
509 * @id: entry id to be translated
510 *
511 * get_table_entry_name() will go over translation table trying to find
512 * entry that matches given id. If matching entry is found, its long
513 * name is returned to the caller.
514 *
515 * returns:
516 * long entry name if translation succeeds
517 * msg otherwise
518 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400519char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100520{
521 for (; table->id >= 0; ++table) {
522 if (table->id == id)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200523#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Woode3d1ac72009-04-02 16:15:10 -0500524 return table->lname;
525#else
526 return table->lname + gd->reloc_off;
527#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100528 }
529 return (msg);
530}
531
532const char *genimg_get_os_name (uint8_t os)
533{
534 return (get_table_entry_name (uimage_os, "Unknown OS", os));
535}
536
537const char *genimg_get_arch_name (uint8_t arch)
538{
539 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
540}
541
542const char *genimg_get_type_name (uint8_t type)
543{
544 return (get_table_entry_name (uimage_type, "Unknown Image", type));
545}
546
547const char *genimg_get_comp_name (uint8_t comp)
548{
549 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
550}
551
552/**
553 * get_table_entry_id - translate short entry name to id
554 * @table: pointer to a translation table for entries of a specific type
555 * @table_name: to be used in case of error
556 * @name: entry short name to be translated
557 *
558 * get_table_entry_id() will go over translation table trying to find
559 * entry that matches given short name. If matching entry is found,
560 * its id returned to the caller.
561 *
562 * returns:
563 * entry id if translation succeeds
564 * -1 otherwise
565 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400566int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100567 const char *table_name, const char *name)
568{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400569 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100570#ifdef USE_HOSTCC
571 int first = 1;
572
573 for (t = table; t->id >= 0; ++t) {
574 if (t->sname && strcasecmp(t->sname, name) == 0)
575 return (t->id);
576 }
577
578 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
579 for (t = table; t->id >= 0; ++t) {
580 if (t->sname == NULL)
581 continue;
582 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
583 first = 0;
584 }
585 fprintf (stderr, "\n");
586#else
587 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200588#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Woode3d1ac72009-04-02 16:15:10 -0500589 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200590#else
591 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500592#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100593 return (t->id);
594 }
595 debug ("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100596#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100597 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100598}
599
Marian Balakowicz570abb02008-02-29 15:59:59 +0100600int genimg_get_os_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100601{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100602 return (get_table_entry_id (uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100603}
604
Marian Balakowicz570abb02008-02-29 15:59:59 +0100605int genimg_get_arch_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100606{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100607 return (get_table_entry_id (uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100608}
609
Marian Balakowicz570abb02008-02-29 15:59:59 +0100610int genimg_get_type_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100611{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100612 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100613}
614
Marian Balakowicz570abb02008-02-29 15:59:59 +0100615int genimg_get_comp_id (const char *name)
616{
617 return (get_table_entry_id (uimage_comp, "Compression", name));
618}
619
620#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100621/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100622 * genimg_get_format - get image format type
623 * @img_addr: image start address
624 *
625 * genimg_get_format() checks whether provided address points to a valid
626 * legacy or FIT image.
627 *
628 * New uImage format and FDT blob are based on a libfdt. FDT blob
629 * may be passed directly or embedded in a FIT image. In both situations
630 * genimg_get_format() must be able to dectect libfdt header.
631 *
632 * returns:
633 * image format type or IMAGE_FORMAT_INVALID if no image is present
634 */
635int genimg_get_format (void *img_addr)
636{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200637 ulong format = IMAGE_FORMAT_INVALID;
638 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100639#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200640 char *fit_hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100641#endif
642
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200643 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100644 if (image_check_magic(hdr))
645 format = IMAGE_FORMAT_LEGACY;
646#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
647 else {
648 fit_hdr = (char *)img_addr;
649 if (fdt_check_header (fit_hdr) == 0)
650 format = IMAGE_FORMAT_FIT;
651 }
652#endif
653
654 return format;
655}
656
657/**
658 * genimg_get_image - get image from special storage (if necessary)
659 * @img_addr: image start address
660 *
661 * genimg_get_image() checks if provided image start adddress is located
662 * in a dataflash storage. If so, image is moved to a system RAM memory.
663 *
664 * returns:
665 * image start address after possible relocation from special storage
666 */
667ulong genimg_get_image (ulong img_addr)
668{
669 ulong ram_addr = img_addr;
670
671#ifdef CONFIG_HAS_DATAFLASH
672 ulong h_size, d_size;
673
674 if (addr_dataflash (img_addr)){
675 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200676 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100677
678 /* get header size */
679 h_size = image_get_header_size ();
680#if defined(CONFIG_FIT)
681 if (sizeof(struct fdt_header) > h_size)
682 h_size = sizeof(struct fdt_header);
683#endif
684
685 /* read in header */
686 debug (" Reading image header from dataflash address "
687 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
688
689 read_dataflash (img_addr, h_size, (char *)ram_addr);
690
691 /* get data size */
692 switch (genimg_get_format ((void *)ram_addr)) {
693 case IMAGE_FORMAT_LEGACY:
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200694 d_size = image_get_data_size ((const image_header_t *)ram_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100695 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
696 ram_addr, d_size);
697 break;
698#if defined(CONFIG_FIT)
699 case IMAGE_FORMAT_FIT:
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100700 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100701 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
702 ram_addr, d_size);
703 break;
704#endif
705 default:
706 printf (" No valid image found at 0x%08lx\n", img_addr);
707 return ram_addr;
708 }
709
710 /* read in image data */
711 debug (" Reading image remaining data from dataflash address "
712 "%08lx to RAM address %08lx\n", img_addr + h_size,
713 ram_addr + h_size);
714
715 read_dataflash (img_addr + h_size, d_size,
716 (char *)(ram_addr + h_size));
717
718 }
719#endif /* CONFIG_HAS_DATAFLASH */
720
721 return ram_addr;
722}
723
724/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100725 * fit_has_config - check if there is a valid FIT configuration
726 * @images: pointer to the bootm command headers structure
727 *
728 * fit_has_config() checks if there is a FIT configuration in use
729 * (if FTI support is present).
730 *
731 * returns:
732 * 0, no FIT support or no configuration found
733 * 1, configuration found
734 */
735int genimg_has_config (bootm_headers_t *images)
736{
737#if defined(CONFIG_FIT)
738 if (images->fit_uname_cfg)
739 return 1;
740#endif
741 return 0;
742}
743
744/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100745 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100746 * @argc: command argument count
747 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100748 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100749 * @arch: expected ramdisk architecture
750 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
751 * @rd_end: pointer to a ulong variable, will hold ramdisk end
752 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100753 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100754 * Curently supported are the following ramdisk sources:
755 * - multicomponent kernel/ramdisk image,
756 * - commandline provided address of decicated ramdisk image.
757 *
758 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100759 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100760 * rd_start and rd_end are set to ramdisk start/end addresses if
761 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100762 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500763 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100764 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100765 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200766int boot_get_ramdisk (int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100767 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100768{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100769 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100770 ulong rd_data, rd_len;
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200771 const image_header_t *rd_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100772#if defined(CONFIG_FIT)
773 void *fit_hdr;
774 const char *fit_uname_config = NULL;
775 const char *fit_uname_ramdisk = NULL;
776 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100777 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100778 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100779 const void *data;
780 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100781#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100782
Marian Balakowiczc8779642008-03-12 10:12:37 +0100783 *rd_start = 0;
784 *rd_end = 0;
785
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100786 /*
787 * Look for a '-' which indicates to ignore the
788 * ramdisk argument
789 */
790 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
791 debug ("## Skipping init Ramdisk\n");
792 rd_len = rd_data = 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100793 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100794#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100795 if (argc >= 3) {
796 /*
797 * If the init ramdisk comes from the FIT image and
798 * the FIT image address is omitted in the command
799 * line argument, try to use os FIT image address or
800 * default load address.
801 */
802 if (images->fit_uname_os)
803 default_addr = (ulong)images->fit_hdr_os;
804 else
805 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100806
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100807 if (fit_parse_conf (argv[2], default_addr,
808 &rd_addr, &fit_uname_config)) {
809 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
810 fit_uname_config, rd_addr);
811 } else if (fit_parse_subimage (argv[2], default_addr,
812 &rd_addr, &fit_uname_ramdisk)) {
813 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
814 fit_uname_ramdisk, rd_addr);
815 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100816#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100817 {
818 rd_addr = simple_strtoul(argv[2], NULL, 16);
819 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
820 rd_addr);
821 }
822#if defined(CONFIG_FIT)
823 } else {
824 /* use FIT configuration provided in first bootm
825 * command argument
826 */
827 rd_addr = (ulong)images->fit_hdr_os;
828 fit_uname_config = images->fit_uname_cfg;
829 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
830 fit_uname_config, rd_addr);
831
832 /*
833 * Check whether configuration has ramdisk defined,
834 * if not, don't try to use it, quit silently.
835 */
836 fit_hdr = (void *)rd_addr;
837 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
838 if (cfg_noffset < 0) {
839 debug ("* ramdisk: no such config\n");
Michal Simekc78fce62008-07-11 10:43:13 +0200840 return 1;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100841 }
842
843 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
844 if (rd_noffset < 0) {
845 debug ("* ramdisk: no ramdisk in config\n");
Peter Tyser41266c92008-08-05 10:51:57 -0500846 return 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100847 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100848 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100849#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100850
851 /* copy from dataflash if needed */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100852 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100853
854 /*
855 * Check if there is an initrd image at the
856 * address provided in the second bootm argument
857 * check image type, for FIT images get FIT node.
858 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100859 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100860 case IMAGE_FORMAT_LEGACY:
Marian Balakowiczc8779642008-03-12 10:12:37 +0100861 printf ("## Loading init Ramdisk from Legacy "
862 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100863
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100864 show_boot_progress (9);
Marian Balakowiczd985c842008-03-12 10:14:38 +0100865 rd_hdr = image_get_ramdisk (rd_addr, arch,
866 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100867
Marian Balakowiczc8779642008-03-12 10:12:37 +0100868 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600869 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600870
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100871 rd_data = image_get_data (rd_hdr);
872 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100873 rd_load = image_get_load (rd_hdr);
874 break;
875#if defined(CONFIG_FIT)
876 case IMAGE_FORMAT_FIT:
877 fit_hdr = (void *)rd_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100878 printf ("## Loading init Ramdisk from FIT "
879 "Image at %08lx ...\n", rd_addr);
880
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100881 show_boot_progress (120);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100882 if (!fit_check_format (fit_hdr)) {
883 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100884 show_boot_progress (-120);
Michal Simekc78fce62008-07-11 10:43:13 +0200885 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100886 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100887 show_boot_progress (121);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100888
889 if (!fit_uname_ramdisk) {
890 /*
891 * no ramdisk image node unit name, try to get config
892 * node first. If config unit node name is NULL
893 * fit_conf_get_node() will try to find default config node
894 */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100895 show_boot_progress (122);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100896 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
897 if (cfg_noffset < 0) {
898 puts ("Could not find configuration node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100899 show_boot_progress (-122);
Michal Simekc78fce62008-07-11 10:43:13 +0200900 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100901 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100902 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
903 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100904
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100905 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100906 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
907 } else {
908 /* get ramdisk component image node offset */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100909 show_boot_progress (123);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100910 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
911 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100912 if (rd_noffset < 0) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100913 puts ("Could not find subimage node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100914 show_boot_progress (-124);
Michal Simekc78fce62008-07-11 10:43:13 +0200915 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100916 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100917
918 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
919
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100920 show_boot_progress (125);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100921 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
Michal Simekc78fce62008-07-11 10:43:13 +0200922 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100923
924 /* get ramdisk image data address and length */
925 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
926 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100927 show_boot_progress (-127);
Michal Simekc78fce62008-07-11 10:43:13 +0200928 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100929 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100930 show_boot_progress (128);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100931
932 rd_data = (ulong)data;
933 rd_len = size;
934
935 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
936 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100937 show_boot_progress (-129);
Michal Simekc78fce62008-07-11 10:43:13 +0200938 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100939 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100940 show_boot_progress (129);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100941
942 images->fit_hdr_rd = fit_hdr;
943 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100944 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100945 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100946#endif
947 default:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100948 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +0100949 rd_data = rd_len = rd_load = 0;
Kumar Galaea86b9e2008-08-29 19:08:29 -0500950 return 1;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100951 }
952
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100953#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100954 /*
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100955 * We need to copy the ramdisk to SRAM to let Linux boot
956 */
957 if (rd_data) {
958 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
959 rd_data = rd_load;
960 }
961#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
962
963 } else if (images->legacy_hdr_valid &&
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200964 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100965 /*
966 * Now check if we have a legacy mult-component image,
967 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100968 */
969 show_boot_progress (13);
970 printf ("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100971 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100972 (ulong)images->legacy_hdr_os);
973
974 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100975 } else {
976 /*
977 * no initrd image
978 */
979 show_boot_progress (14);
980 rd_len = rd_data = 0;
981 }
982
983 if (!rd_data) {
984 debug ("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100985 } else {
986 *rd_start = rd_data;
987 *rd_end = rd_data + rd_len;
988 }
989 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
990 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -0600991
992 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100993}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100994
John Rigbyfca43cc2010-10-13 13:57:35 -0600995#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100996/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100997 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -0600998 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100999 * @rd_data: ramdisk data start address
1000 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001001 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1002 * start address (after possible relocation)
1003 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1004 * end address (after possible relocation)
1005 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001006 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001007 * variable and if requested ramdisk data is moved to a specified location.
1008 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001009 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1010 * start/end addresses if ramdisk image start and len were provided,
1011 * otherwise set initrd_start and initrd_end set to zeros.
1012 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001013 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001014 * 0 - success
1015 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001016 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001017int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001018 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001019{
1020 char *s;
1021 ulong initrd_high;
1022 int initrd_copy_to_ram = 1;
1023
1024 if ((s = getenv ("initrd_high")) != NULL) {
1025 /* a value of "no" or a similar string will act like 0,
1026 * turning the "load high" feature off. This is intentional.
1027 */
1028 initrd_high = simple_strtoul (s, NULL, 16);
1029 if (initrd_high == ~0)
1030 initrd_copy_to_ram = 0;
1031 } else {
1032 /* not set, no restrictions to load high */
1033 initrd_high = ~0;
1034 }
1035
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001036
1037#ifdef CONFIG_LOGBUFFER
1038 /* Prevent initrd from overwriting logbuffer */
1039 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1040#endif
1041
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001042 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1043 initrd_high, initrd_copy_to_ram);
1044
1045 if (rd_data) {
1046 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1047 debug (" in-place initrd\n");
1048 *initrd_start = rd_data;
1049 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001050 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001051 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001052 if (initrd_high)
Becky Bruce391fd932008-06-09 20:37:18 -05001053 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001054 else
Becky Bruce391fd932008-06-09 20:37:18 -05001055 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001056
Kumar Galae822d7f2008-02-27 21:51:49 -06001057 if (*initrd_start == 0) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001058 puts ("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001059 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001060 }
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001061 show_boot_progress (12);
1062
1063 *initrd_end = *initrd_start + rd_len;
1064 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1065 *initrd_start, *initrd_end);
1066
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001067 memmove_wd ((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001068 (void *)rd_data, rd_len, CHUNKSZ);
1069
1070 puts ("OK\n");
1071 }
1072 } else {
1073 *initrd_start = 0;
1074 *initrd_end = 0;
1075 }
1076 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1077 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001078
Kumar Galae822d7f2008-02-27 21:51:49 -06001079 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001080
Kumar Galae822d7f2008-02-27 21:51:49 -06001081error:
1082 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001083}
John Rigbyfca43cc2010-10-13 13:57:35 -06001084#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001085
Kumar Gala06a09912008-08-15 08:24:38 -05001086#ifdef CONFIG_OF_LIBFDT
1087static void fdt_error (const char *msg)
1088{
1089 puts ("ERROR: ");
1090 puts (msg);
1091 puts (" - must RESET the board to recover.\n");
1092}
1093
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001094static const image_header_t *image_get_fdt (ulong fdt_addr)
Kumar Gala06a09912008-08-15 08:24:38 -05001095{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001096 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001097
1098 image_print_contents (fdt_hdr);
1099
1100 puts (" Verifying Checksum ... ");
1101 if (!image_check_hcrc (fdt_hdr)) {
1102 fdt_error ("fdt header checksum invalid");
1103 return NULL;
1104 }
1105
1106 if (!image_check_dcrc (fdt_hdr)) {
1107 fdt_error ("fdt checksum invalid");
1108 return NULL;
1109 }
1110 puts ("OK\n");
1111
1112 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1113 fdt_error ("uImage is not a fdt");
1114 return NULL;
1115 }
1116 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1117 fdt_error ("uImage is compressed");
1118 return NULL;
1119 }
1120 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1121 fdt_error ("uImage data is not a fdt");
1122 return NULL;
1123 }
1124 return fdt_hdr;
1125}
1126
1127/**
1128 * fit_check_fdt - verify FIT format FDT subimage
1129 * @fit_hdr: pointer to the FIT header
1130 * fdt_noffset: FDT subimage node offset within FIT image
1131 * @verify: data CRC verification flag
1132 *
1133 * fit_check_fdt() verifies integrity of the FDT subimage and from
1134 * specified FIT image.
1135 *
1136 * returns:
1137 * 1, on success
1138 * 0, on failure
1139 */
1140#if defined(CONFIG_FIT)
1141static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1142{
1143 fit_image_print (fit, fdt_noffset, " ");
1144
1145 if (verify) {
1146 puts (" Verifying Hash Integrity ... ");
1147 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1148 fdt_error ("Bad Data Hash");
1149 return 0;
1150 }
1151 puts ("OK\n");
1152 }
1153
1154 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1155 fdt_error ("Not a FDT image");
1156 return 0;
1157 }
1158
1159 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1160 fdt_error ("FDT image is compressed");
1161 return 0;
1162 }
1163
1164 return 1;
1165}
1166#endif /* CONFIG_FIT */
1167
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001168#ifndef CONFIG_SYS_FDT_PAD
1169#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Gala06a09912008-08-15 08:24:38 -05001170#endif
1171
1172/**
1173 * boot_relocate_fdt - relocate flat device tree
1174 * @lmb: pointer to lmb handle, will be used for memory mgmt
Kumar Gala06a09912008-08-15 08:24:38 -05001175 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1176 * @of_size: pointer to a ulong variable, will hold fdt length
1177 *
Timur Tabi43b08af2010-05-24 15:10:25 -05001178 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1179 * relocates the of_flat_tree into that region, even if the fdt is already in
1180 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1181 * bytes.
Kumar Gala06a09912008-08-15 08:24:38 -05001182 *
1183 * of_flat_tree and of_size are set to final (after relocation) values
1184 *
1185 * returns:
1186 * 0 - success
1187 * 1 - failure
1188 */
Stephan Linz958e1202010-06-25 18:04:59 +02001189#if defined(CONFIG_SYS_BOOTMAPSZ)
Grant Likely590d3ca2011-03-28 09:58:34 +00001190int boot_relocate_fdt (struct lmb *lmb, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001191{
Timur Tabi43b08af2010-05-24 15:10:25 -05001192 void *fdt_blob = *of_flat_tree;
1193 void *of_start = 0;
Kumar Gala06a09912008-08-15 08:24:38 -05001194 ulong of_len = 0;
Timur Tabi43b08af2010-05-24 15:10:25 -05001195 int err;
Kumar Gala06a09912008-08-15 08:24:38 -05001196
1197 /* nothing to do */
1198 if (*of_size == 0)
1199 return 0;
1200
1201 if (fdt_check_header (fdt_blob) != 0) {
1202 fdt_error ("image is not a fdt");
1203 goto error;
1204 }
1205
Timur Tabi43b08af2010-05-24 15:10:25 -05001206 /* position on a 4K boundary before the alloc_current */
1207 /* Pad the FDT by a specified amount */
1208 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1209 of_start = (void *)(unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
Grant Likely590d3ca2011-03-28 09:58:34 +00001210 CONFIG_SYS_BOOTMAPSZ + getenv_bootm_low());
Kumar Gala06a09912008-08-15 08:24:38 -05001211
Timur Tabi43b08af2010-05-24 15:10:25 -05001212 if (of_start == 0) {
1213 puts("device tree - allocation error\n");
1214 goto error;
Kumar Gala06a09912008-08-15 08:24:38 -05001215 }
1216
Timur Tabi43b08af2010-05-24 15:10:25 -05001217 debug ("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1218 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1219
1220 printf (" Loading Device Tree to %p, end %p ... ",
1221 of_start, of_start + of_len - 1);
1222
1223 err = fdt_open_into (fdt_blob, of_start, of_len);
1224 if (err != 0) {
1225 fdt_error ("fdt move failed");
1226 goto error;
1227 }
1228 puts ("OK\n");
1229
1230 *of_flat_tree = of_start;
1231 *of_size = of_len;
1232
Kumar Gala54f9c862008-08-15 08:24:39 -05001233 set_working_fdt_addr(*of_flat_tree);
Kumar Gala06a09912008-08-15 08:24:38 -05001234 return 0;
1235
1236error:
1237 return 1;
1238}
Stephan Linz958e1202010-06-25 18:04:59 +02001239#endif /* CONFIG_SYS_BOOTMAPSZ */
Kumar Gala06a09912008-08-15 08:24:38 -05001240
1241/**
1242 * boot_get_fdt - main fdt handling routine
1243 * @argc: command argument count
1244 * @argv: command argument list
1245 * @images: pointer to the bootm images structure
1246 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1247 * @of_size: pointer to a ulong variable, will hold fdt length
1248 *
1249 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1250 * Curently supported are the following ramdisk sources:
1251 * - multicomponent kernel/ramdisk image,
1252 * - commandline provided address of decicated ramdisk image.
1253 *
1254 * returns:
1255 * 0, if fdt image was found and valid, or skipped
1256 * of_flat_tree and of_size are set to fdt start address and length if
1257 * fdt image is found and valid
1258 *
1259 * 1, if fdt image is found but corrupted
1260 * of_flat_tree and of_size are set to 0 if no fdt exists
1261 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001262int boot_get_fdt (int flag, int argc, char * const argv[], bootm_headers_t *images,
Kumar Gala06a09912008-08-15 08:24:38 -05001263 char **of_flat_tree, ulong *of_size)
1264{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001265 const image_header_t *fdt_hdr;
Kumar Gala06a09912008-08-15 08:24:38 -05001266 ulong fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001267 char *fdt_blob = NULL;
1268 ulong image_start, image_end;
1269 ulong load_start, load_end;
1270#if defined(CONFIG_FIT)
1271 void *fit_hdr;
1272 const char *fit_uname_config = NULL;
1273 const char *fit_uname_fdt = NULL;
1274 ulong default_addr;
1275 int cfg_noffset;
1276 int fdt_noffset;
1277 const void *data;
1278 size_t size;
1279#endif
1280
1281 *of_flat_tree = NULL;
1282 *of_size = 0;
1283
1284 if (argc > 3 || genimg_has_config (images)) {
1285#if defined(CONFIG_FIT)
1286 if (argc > 3) {
1287 /*
1288 * If the FDT blob comes from the FIT image and the
1289 * FIT image address is omitted in the command line
1290 * argument, try to use ramdisk or os FIT image
1291 * address or default load address.
1292 */
1293 if (images->fit_uname_rd)
1294 default_addr = (ulong)images->fit_hdr_rd;
1295 else if (images->fit_uname_os)
1296 default_addr = (ulong)images->fit_hdr_os;
1297 else
1298 default_addr = load_addr;
1299
1300 if (fit_parse_conf (argv[3], default_addr,
1301 &fdt_addr, &fit_uname_config)) {
1302 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1303 fit_uname_config, fdt_addr);
1304 } else if (fit_parse_subimage (argv[3], default_addr,
1305 &fdt_addr, &fit_uname_fdt)) {
1306 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1307 fit_uname_fdt, fdt_addr);
1308 } else
1309#endif
1310 {
1311 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1312 debug ("* fdt: cmdline image address = 0x%08lx\n",
1313 fdt_addr);
1314 }
1315#if defined(CONFIG_FIT)
1316 } else {
1317 /* use FIT configuration provided in first bootm
1318 * command argument
1319 */
1320 fdt_addr = (ulong)images->fit_hdr_os;
1321 fit_uname_config = images->fit_uname_cfg;
1322 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1323 fit_uname_config, fdt_addr);
1324
1325 /*
1326 * Check whether configuration has FDT blob defined,
1327 * if not quit silently.
1328 */
1329 fit_hdr = (void *)fdt_addr;
1330 cfg_noffset = fit_conf_get_node (fit_hdr,
1331 fit_uname_config);
1332 if (cfg_noffset < 0) {
1333 debug ("* fdt: no such config\n");
1334 return 0;
1335 }
1336
1337 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1338 cfg_noffset);
1339 if (fdt_noffset < 0) {
1340 debug ("* fdt: no fdt in config\n");
1341 return 0;
1342 }
1343 }
1344#endif
1345
1346 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1347 fdt_addr);
1348
1349 /* copy from dataflash if needed */
1350 fdt_addr = genimg_get_image (fdt_addr);
1351
1352 /*
1353 * Check if there is an FDT image at the
1354 * address provided in the second bootm argument
1355 * check image type, for FIT images get a FIT node.
1356 */
1357 switch (genimg_get_format ((void *)fdt_addr)) {
1358 case IMAGE_FORMAT_LEGACY:
1359 /* verify fdt_addr points to a valid image header */
1360 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1361 fdt_addr);
1362 fdt_hdr = image_get_fdt (fdt_addr);
1363 if (!fdt_hdr)
1364 goto error;
1365
1366 /*
1367 * move image data to the load address,
1368 * make sure we don't overwrite initial image
1369 */
1370 image_start = (ulong)fdt_hdr;
1371 image_end = image_get_image_end (fdt_hdr);
1372
1373 load_start = image_get_load (fdt_hdr);
1374 load_end = load_start + image_get_data_size (fdt_hdr);
1375
1376 if ((load_start < image_end) && (load_end > image_start)) {
1377 fdt_error ("fdt overwritten");
1378 goto error;
1379 }
1380
1381 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1382 image_get_data (fdt_hdr), load_start);
1383
1384 memmove ((void *)load_start,
1385 (void *)image_get_data (fdt_hdr),
1386 image_get_data_size (fdt_hdr));
1387
1388 fdt_blob = (char *)load_start;
1389 break;
1390 case IMAGE_FORMAT_FIT:
1391 /*
1392 * This case will catch both: new uImage format
1393 * (libfdt based) and raw FDT blob (also libfdt
1394 * based).
1395 */
1396#if defined(CONFIG_FIT)
1397 /* check FDT blob vs FIT blob */
1398 if (fit_check_format ((const void *)fdt_addr)) {
1399 /*
1400 * FIT image
1401 */
1402 fit_hdr = (void *)fdt_addr;
1403 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1404 fdt_addr);
1405
1406 if (!fit_uname_fdt) {
1407 /*
1408 * no FDT blob image node unit name,
1409 * try to get config node first. If
1410 * config unit node name is NULL
1411 * fit_conf_get_node() will try to
1412 * find default config node
1413 */
1414 cfg_noffset = fit_conf_get_node (fit_hdr,
1415 fit_uname_config);
1416
1417 if (cfg_noffset < 0) {
1418 fdt_error ("Could not find configuration node\n");
1419 goto error;
1420 }
1421
1422 fit_uname_config = fdt_get_name (fit_hdr,
1423 cfg_noffset, NULL);
1424 printf (" Using '%s' configuration\n",
1425 fit_uname_config);
1426
1427 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1428 cfg_noffset);
1429 fit_uname_fdt = fit_get_name (fit_hdr,
1430 fdt_noffset, NULL);
1431 } else {
1432 /* get FDT component image node offset */
1433 fdt_noffset = fit_image_get_node (fit_hdr,
1434 fit_uname_fdt);
1435 }
1436 if (fdt_noffset < 0) {
1437 fdt_error ("Could not find subimage node\n");
1438 goto error;
1439 }
1440
1441 printf (" Trying '%s' FDT blob subimage\n",
1442 fit_uname_fdt);
1443
1444 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1445 images->verify))
1446 goto error;
1447
1448 /* get ramdisk image data address and length */
1449 if (fit_image_get_data (fit_hdr, fdt_noffset,
1450 &data, &size)) {
1451 fdt_error ("Could not find FDT subimage data");
1452 goto error;
1453 }
1454
1455 /* verift that image data is a proper FDT blob */
1456 if (fdt_check_header ((char *)data) != 0) {
1457 fdt_error ("Subimage data is not a FTD");
1458 goto error;
1459 }
1460
1461 /*
1462 * move image data to the load address,
1463 * make sure we don't overwrite initial image
1464 */
1465 image_start = (ulong)fit_hdr;
1466 image_end = fit_get_end (fit_hdr);
1467
1468 if (fit_image_get_load (fit_hdr, fdt_noffset,
1469 &load_start) == 0) {
1470 load_end = load_start + size;
1471
1472 if ((load_start < image_end) &&
1473 (load_end > image_start)) {
1474 fdt_error ("FDT overwritten");
1475 goto error;
1476 }
1477
1478 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1479 (ulong)data, load_start);
1480
1481 memmove ((void *)load_start,
1482 (void *)data, size);
1483
1484 fdt_blob = (char *)load_start;
1485 } else {
1486 fdt_blob = (char *)data;
1487 }
1488
1489 images->fit_hdr_fdt = fit_hdr;
1490 images->fit_uname_fdt = fit_uname_fdt;
1491 images->fit_noffset_fdt = fdt_noffset;
1492 break;
1493 } else
1494#endif
1495 {
1496 /*
1497 * FDT blob
1498 */
1499 fdt_blob = (char *)fdt_addr;
1500 debug ("* fdt: raw FDT blob\n");
1501 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1502 }
1503 break;
1504 default:
Anatolij Gustschin8e024942008-08-29 21:04:45 +02001505 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001506 goto error;
1507 }
1508
1509 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1510
1511 } else if (images->legacy_hdr_valid &&
1512 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1513
1514 ulong fdt_data, fdt_len;
1515
1516 /*
1517 * Now check if we have a legacy multi-component image,
1518 * get second entry data start address and len.
1519 */
1520 printf ("## Flattened Device Tree from multi "
1521 "component Image at %08lX\n",
1522 (ulong)images->legacy_hdr_os);
1523
1524 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1525 if (fdt_len) {
1526
1527 fdt_blob = (char *)fdt_data;
1528 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1529
1530 if (fdt_check_header (fdt_blob) != 0) {
1531 fdt_error ("image is not a fdt");
1532 goto error;
1533 }
1534
John Rigbyd1263fc2010-10-13 13:57:32 -06001535 if (fdt_totalsize(fdt_blob) != fdt_len) {
Kumar Gala06a09912008-08-15 08:24:38 -05001536 fdt_error ("fdt size != image size");
1537 goto error;
1538 }
1539 } else {
1540 debug ("## No Flattened Device Tree\n");
1541 return 0;
1542 }
1543 } else {
1544 debug ("## No Flattened Device Tree\n");
1545 return 0;
1546 }
1547
1548 *of_flat_tree = fdt_blob;
John Rigbyd1263fc2010-10-13 13:57:32 -06001549 *of_size = fdt_totalsize(fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001550 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner52514692008-08-21 07:12:26 -07001551 (ulong)*of_flat_tree, *of_size);
Kumar Gala06a09912008-08-15 08:24:38 -05001552
1553 return 0;
1554
1555error:
1556 *of_flat_tree = 0;
1557 *of_size = 0;
1558 return 1;
1559}
1560#endif /* CONFIG_OF_LIBFDT */
1561
John Rigbyfca43cc2010-10-13 13:57:35 -06001562#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001563/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001564 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001565 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001566 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1567 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1568 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001569 * boot_get_cmdline() allocates space for kernel command line below
Grant Likely590d3ca2011-03-28 09:58:34 +00001570 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001571 * variable is present its contents is copied to allocated kernel
1572 * command line.
1573 *
1574 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001575 * 0 - success
1576 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001577 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001578int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001579{
1580 char *cmdline;
1581 char *s;
1582
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001583 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likely590d3ca2011-03-28 09:58:34 +00001584 CONFIG_SYS_BOOTMAPSZ + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001585
1586 if (cmdline == NULL)
1587 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001588
1589 if ((s = getenv("bootargs")) == NULL)
1590 s = "";
1591
1592 strcpy(cmdline, s);
1593
1594 *cmd_start = (ulong) & cmdline[0];
1595 *cmd_end = *cmd_start + strlen(cmdline);
1596
1597 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1598
Kumar Galae822d7f2008-02-27 21:51:49 -06001599 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001600}
John Rigbyfca43cc2010-10-13 13:57:35 -06001601#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001602
John Rigbyfca43cc2010-10-13 13:57:35 -06001603#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001604/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001605 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001606 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001607 * @kbd: double pointer to board info data
1608 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001609 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likely590d3ca2011-03-28 09:58:34 +00001610 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1611 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001612 *
1613 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001614 * 0 - success
1615 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001616 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001617int boot_get_kbd (struct lmb *lmb, bd_t **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001618{
Becky Bruce391fd932008-06-09 20:37:18 -05001619 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likely590d3ca2011-03-28 09:58:34 +00001620 CONFIG_SYS_BOOTMAPSZ + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001621 if (*kbd == NULL)
1622 return -1;
1623
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001624 **kbd = *(gd->bd);
1625
1626 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1627
1628#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1629 do_bdinfo(NULL, 0, 0, NULL);
1630#endif
1631
Kumar Galae822d7f2008-02-27 21:51:49 -06001632 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001633}
John Rigbyfca43cc2010-10-13 13:57:35 -06001634#endif /* CONFIG_SYS_BOOT_GET_KBD */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001635#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001636
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001637#if defined(CONFIG_FIT)
1638/*****************************************************************************/
1639/* New uImage format routines */
1640/*****************************************************************************/
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001641#ifndef USE_HOSTCC
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001642static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1643 ulong *addr, const char **name)
1644{
1645 const char *sep;
1646
1647 *addr = addr_curr;
1648 *name = NULL;
1649
1650 sep = strchr (spec, sepc);
1651 if (sep) {
1652 if (sep - spec > 0)
1653 *addr = simple_strtoul (spec, NULL, 16);
1654
1655 *name = sep + 1;
1656 return 1;
1657 }
1658
1659 return 0;
1660}
1661
1662/**
1663 * fit_parse_conf - parse FIT configuration spec
1664 * @spec: input string, containing configuration spec
1665 * @add_curr: current image address (to be used as a possible default)
1666 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1667 * configuration
1668 * @conf_name double pointer to a char, will hold pointer to a configuration
1669 * unit name
1670 *
1671 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1672 * where <addr> is a FIT image address that contains configuration
1673 * with a <conf> unit name.
1674 *
1675 * Address part is optional, and if omitted default add_curr will
1676 * be used instead.
1677 *
1678 * returns:
1679 * 1 if spec is a valid configuration string,
1680 * addr and conf_name are set accordingly
1681 * 0 otherwise
1682 */
1683inline int fit_parse_conf (const char *spec, ulong addr_curr,
1684 ulong *addr, const char **conf_name)
1685{
1686 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1687}
1688
1689/**
1690 * fit_parse_subimage - parse FIT subimage spec
1691 * @spec: input string, containing subimage spec
1692 * @add_curr: current image address (to be used as a possible default)
1693 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1694 * subimage
1695 * @image_name: double pointer to a char, will hold pointer to a subimage name
1696 *
1697 * fit_parse_subimage() expects subimage spec in the for of
1698 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1699 * subimage with a <subimg> unit name.
1700 *
1701 * Address part is optional, and if omitted default add_curr will
1702 * be used instead.
1703 *
1704 * returns:
1705 * 1 if spec is a valid subimage string,
1706 * addr and image_name are set accordingly
1707 * 0 otherwise
1708 */
1709inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1710 ulong *addr, const char **image_name)
1711{
1712 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1713}
Marian Balakowicz570abb02008-02-29 15:59:59 +01001714#endif /* !USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001715
1716static void fit_get_debug (const void *fit, int noffset,
1717 char *prop_name, int err)
1718{
1719 debug ("Can't get '%s' property from FIT 0x%08lx, "
1720 "node: offset %d, name %s (%s)\n",
1721 prop_name, (ulong)fit, noffset,
1722 fit_get_name (fit, noffset, NULL),
1723 fdt_strerror (err));
1724}
1725
1726/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001727 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001728 * @fit: pointer to the FIT format image header
1729 * @p: pointer to prefix string
1730 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001731 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001732 * The routine prints out FIT image properties (root node level) follwed by
1733 * the details of each component image.
1734 *
1735 * returns:
1736 * no returned results
1737 */
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001738void fit_print_contents (const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001739{
1740 char *desc;
1741 char *uname;
1742 int images_noffset;
1743 int confs_noffset;
1744 int noffset;
1745 int ndepth;
1746 int count = 0;
1747 int ret;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001748 const char *p;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001749#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1750 time_t timestamp;
1751#endif
1752
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001753#ifdef USE_HOSTCC
1754 p = "";
1755#else
1756 p = " ";
1757#endif
1758
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001759 /* Root node properties */
1760 ret = fit_get_desc (fit, 0, &desc);
1761 printf ("%sFIT description: ", p);
1762 if (ret)
1763 printf ("unavailable\n");
1764 else
1765 printf ("%s\n", desc);
1766
1767#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1768 ret = fit_get_timestamp (fit, 0, &timestamp);
1769 printf ("%sCreated: ", p);
1770 if (ret)
1771 printf ("unavailable\n");
1772 else
1773 genimg_print_time (timestamp);
1774#endif
1775
1776 /* Find images parent node offset */
1777 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1778 if (images_noffset < 0) {
1779 printf ("Can't find images parent node '%s' (%s)\n",
1780 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1781 return;
1782 }
1783
1784 /* Process its subnodes, print out component images details */
1785 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1786 (noffset >= 0) && (ndepth > 0);
1787 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1788 if (ndepth == 1) {
1789 /*
1790 * Direct child node of the images parent node,
1791 * i.e. component image node.
1792 */
1793 printf ("%s Image %u (%s)\n", p, count++,
1794 fit_get_name(fit, noffset, NULL));
1795
1796 fit_image_print (fit, noffset, p);
1797 }
1798 }
1799
1800 /* Find configurations parent node offset */
1801 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1802 if (confs_noffset < 0) {
1803 debug ("Can't get configurations parent node '%s' (%s)\n",
1804 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1805 return;
1806 }
1807
1808 /* get default configuration unit name from default property */
1809 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1810 if (uname)
1811 printf ("%s Default Configuration: '%s'\n", p, uname);
1812
1813 /* Process its subnodes, print out configurations details */
1814 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1815 (noffset >= 0) && (ndepth > 0);
1816 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1817 if (ndepth == 1) {
1818 /*
1819 * Direct child node of the configurations parent node,
1820 * i.e. configuration node.
1821 */
1822 printf ("%s Configuration %u (%s)\n", p, count++,
1823 fit_get_name(fit, noffset, NULL));
1824
1825 fit_conf_print (fit, noffset, p);
1826 }
1827 }
1828}
1829
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001830/**
1831 * fit_image_print - prints out the FIT component image details
1832 * @fit: pointer to the FIT format image header
1833 * @image_noffset: offset of the component image node
1834 * @p: pointer to prefix string
1835 *
1836 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001837 * image. If present, hash nodes are printed out as well. Load
1838 * address for images of type firmware is also printed out. Since the load
1839 * address is not mandatory for firmware images, it will be output as
1840 * "unavailable" when not present.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001841 *
1842 * returns:
1843 * no returned results
1844 */
1845void fit_image_print (const void *fit, int image_noffset, const char *p)
1846{
1847 char *desc;
1848 uint8_t type, arch, os, comp;
1849 size_t size;
1850 ulong load, entry;
1851 const void *data;
1852 int noffset;
1853 int ndepth;
1854 int ret;
1855
1856 /* Mandatory properties */
1857 ret = fit_get_desc (fit, image_noffset, &desc);
1858 printf ("%s Description: ", p);
1859 if (ret)
1860 printf ("unavailable\n");
1861 else
1862 printf ("%s\n", desc);
1863
1864 fit_image_get_type (fit, image_noffset, &type);
1865 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1866
1867 fit_image_get_comp (fit, image_noffset, &comp);
1868 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1869
1870 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1871
1872#ifndef USE_HOSTCC
1873 printf ("%s Data Start: ", p);
1874 if (ret)
1875 printf ("unavailable\n");
1876 else
1877 printf ("0x%08lx\n", (ulong)data);
1878#endif
1879
1880 printf ("%s Data Size: ", p);
1881 if (ret)
1882 printf ("unavailable\n");
1883 else
1884 genimg_print_size (size);
1885
1886 /* Remaining, type dependent properties */
1887 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1888 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1889 (type == IH_TYPE_FLATDT)) {
1890 fit_image_get_arch (fit, image_noffset, &arch);
1891 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1892 }
1893
1894 if (type == IH_TYPE_KERNEL) {
1895 fit_image_get_os (fit, image_noffset, &os);
1896 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1897 }
1898
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001899 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1900 (type == IH_TYPE_FIRMWARE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001901 ret = fit_image_get_load (fit, image_noffset, &load);
1902 printf ("%s Load Address: ", p);
1903 if (ret)
1904 printf ("unavailable\n");
1905 else
1906 printf ("0x%08lx\n", load);
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001907 }
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001908
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001909 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001910 fit_image_get_entry (fit, image_noffset, &entry);
1911 printf ("%s Entry Point: ", p);
1912 if (ret)
1913 printf ("unavailable\n");
1914 else
1915 printf ("0x%08lx\n", entry);
1916 }
1917
1918 /* Process all hash subnodes of the component image node */
1919 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1920 (noffset >= 0) && (ndepth > 0);
1921 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1922 if (ndepth == 1) {
1923 /* Direct child node of the component image node */
1924 fit_image_print_hash (fit, noffset, p);
1925 }
1926 }
1927}
1928
1929/**
1930 * fit_image_print_hash - prints out the hash node details
1931 * @fit: pointer to the FIT format image header
1932 * @noffset: offset of the hash node
1933 * @p: pointer to prefix string
1934 *
1935 * fit_image_print_hash() lists properies for the processed hash node
1936 *
1937 * returns:
1938 * no returned results
1939 */
1940void fit_image_print_hash (const void *fit, int noffset, const char *p)
1941{
1942 char *algo;
1943 uint8_t *value;
1944 int value_len;
1945 int i, ret;
1946
1947 /*
1948 * Check subnode name, must be equal to "hash".
1949 * Multiple hash nodes require unique unit node
1950 * names, e.g. hash@1, hash@2, etc.
1951 */
1952 if (strncmp (fit_get_name(fit, noffset, NULL),
1953 FIT_HASH_NODENAME,
1954 strlen(FIT_HASH_NODENAME)) != 0)
1955 return;
1956
1957 debug ("%s Hash node: '%s'\n", p,
1958 fit_get_name (fit, noffset, NULL));
1959
1960 printf ("%s Hash algo: ", p);
1961 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1962 printf ("invalid/unsupported\n");
1963 return;
1964 }
1965 printf ("%s\n", algo);
1966
1967 ret = fit_image_hash_get_value (fit, noffset, &value,
1968 &value_len);
1969 printf ("%s Hash value: ", p);
1970 if (ret) {
1971 printf ("unavailable\n");
1972 } else {
1973 for (i = 0; i < value_len; i++)
1974 printf ("%02x", value[i]);
1975 printf ("\n");
1976 }
1977
1978 debug ("%s Hash len: %d\n", p, value_len);
1979}
1980
1981/**
1982 * fit_get_desc - get node description property
1983 * @fit: pointer to the FIT format image header
1984 * @noffset: node offset
1985 * @desc: double pointer to the char, will hold pointer to the descrption
1986 *
1987 * fit_get_desc() reads description property from a given node, if
1988 * description is found pointer to it is returened in third call argument.
1989 *
1990 * returns:
1991 * 0, on success
1992 * -1, on failure
1993 */
1994int fit_get_desc (const void *fit, int noffset, char **desc)
1995{
1996 int len;
1997
1998 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1999 if (*desc == NULL) {
2000 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2001 return -1;
2002 }
2003
2004 return 0;
2005}
2006
2007/**
2008 * fit_get_timestamp - get node timestamp property
2009 * @fit: pointer to the FIT format image header
2010 * @noffset: node offset
2011 * @timestamp: pointer to the time_t, will hold read timestamp
2012 *
2013 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2014 * is found and has a correct size its value is retured in third call
2015 * argument.
2016 *
2017 * returns:
2018 * 0, on success
2019 * -1, on property read failure
2020 * -2, on wrong timestamp size
2021 */
2022int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2023{
2024 int len;
2025 const void *data;
2026
2027 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2028 if (data == NULL) {
2029 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2030 return -1;
2031 }
2032 if (len != sizeof (uint32_t)) {
2033 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2034 return -2;
2035 }
2036
2037 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2038 return 0;
2039}
2040
2041/**
2042 * fit_image_get_node - get node offset for component image of a given unit name
2043 * @fit: pointer to the FIT format image header
2044 * @image_uname: component image node unit name
2045 *
2046 * fit_image_get_node() finds a component image (withing the '/images'
2047 * node) of a provided unit name. If image is found its node offset is
2048 * returned to the caller.
2049 *
2050 * returns:
2051 * image node offset when found (>=0)
2052 * negative number on failure (FDT_ERR_* code)
2053 */
2054int fit_image_get_node (const void *fit, const char *image_uname)
2055{
2056 int noffset, images_noffset;
2057
2058 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2059 if (images_noffset < 0) {
2060 debug ("Can't find images parent node '%s' (%s)\n",
2061 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2062 return images_noffset;
2063 }
2064
2065 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2066 if (noffset < 0) {
2067 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2068 image_uname, fdt_strerror (noffset));
2069 }
2070
2071 return noffset;
2072}
2073
2074/**
2075 * fit_image_get_os - get os id for a given component image node
2076 * @fit: pointer to the FIT format image header
2077 * @noffset: component image node offset
2078 * @os: pointer to the uint8_t, will hold os numeric id
2079 *
2080 * fit_image_get_os() finds os property in a given component image node.
2081 * If the property is found, its (string) value is translated to the numeric
2082 * id which is returned to the caller.
2083 *
2084 * returns:
2085 * 0, on success
2086 * -1, on failure
2087 */
2088int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2089{
2090 int len;
2091 const void *data;
2092
2093 /* Get OS name from property data */
2094 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2095 if (data == NULL) {
2096 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2097 *os = -1;
2098 return -1;
2099 }
2100
2101 /* Translate OS name to id */
2102 *os = genimg_get_os_id (data);
2103 return 0;
2104}
2105
2106/**
2107 * fit_image_get_arch - get arch id for a given component image node
2108 * @fit: pointer to the FIT format image header
2109 * @noffset: component image node offset
2110 * @arch: pointer to the uint8_t, will hold arch numeric id
2111 *
2112 * fit_image_get_arch() finds arch property in a given component image node.
2113 * If the property is found, its (string) value is translated to the numeric
2114 * id which is returned to the caller.
2115 *
2116 * returns:
2117 * 0, on success
2118 * -1, on failure
2119 */
2120int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2121{
2122 int len;
2123 const void *data;
2124
2125 /* Get architecture name from property data */
2126 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2127 if (data == NULL) {
2128 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2129 *arch = -1;
2130 return -1;
2131 }
2132
2133 /* Translate architecture name to id */
2134 *arch = genimg_get_arch_id (data);
2135 return 0;
2136}
2137
2138/**
2139 * fit_image_get_type - get type id for a given component image node
2140 * @fit: pointer to the FIT format image header
2141 * @noffset: component image node offset
2142 * @type: pointer to the uint8_t, will hold type numeric id
2143 *
2144 * fit_image_get_type() finds type property in a given component image node.
2145 * If the property is found, its (string) value is translated to the numeric
2146 * id which is returned to the caller.
2147 *
2148 * returns:
2149 * 0, on success
2150 * -1, on failure
2151 */
2152int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2153{
2154 int len;
2155 const void *data;
2156
2157 /* Get image type name from property data */
2158 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2159 if (data == NULL) {
2160 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2161 *type = -1;
2162 return -1;
2163 }
2164
2165 /* Translate image type name to id */
2166 *type = genimg_get_type_id (data);
2167 return 0;
2168}
2169
2170/**
2171 * fit_image_get_comp - get comp id for a given component image node
2172 * @fit: pointer to the FIT format image header
2173 * @noffset: component image node offset
2174 * @comp: pointer to the uint8_t, will hold comp numeric id
2175 *
2176 * fit_image_get_comp() finds comp property in a given component image node.
2177 * If the property is found, its (string) value is translated to the numeric
2178 * id which is returned to the caller.
2179 *
2180 * returns:
2181 * 0, on success
2182 * -1, on failure
2183 */
2184int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2185{
2186 int len;
2187 const void *data;
2188
2189 /* Get compression name from property data */
2190 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2191 if (data == NULL) {
2192 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2193 *comp = -1;
2194 return -1;
2195 }
2196
2197 /* Translate compression name to id */
2198 *comp = genimg_get_comp_id (data);
2199 return 0;
2200}
2201
2202/**
2203 * fit_image_get_load - get load address property for a given component image node
2204 * @fit: pointer to the FIT format image header
2205 * @noffset: component image node offset
2206 * @load: pointer to the uint32_t, will hold load address
2207 *
2208 * fit_image_get_load() finds load address property in a given component image node.
2209 * If the property is found, its value is returned to the caller.
2210 *
2211 * returns:
2212 * 0, on success
2213 * -1, on failure
2214 */
2215int fit_image_get_load (const void *fit, int noffset, ulong *load)
2216{
2217 int len;
2218 const uint32_t *data;
2219
2220 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2221 if (data == NULL) {
2222 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2223 return -1;
2224 }
2225
2226 *load = uimage_to_cpu (*data);
2227 return 0;
2228}
2229
2230/**
2231 * fit_image_get_entry - get entry point address property for a given component image node
2232 * @fit: pointer to the FIT format image header
2233 * @noffset: component image node offset
2234 * @entry: pointer to the uint32_t, will hold entry point address
2235 *
2236 * fit_image_get_entry() finds entry point address property in a given component image node.
2237 * If the property is found, its value is returned to the caller.
2238 *
2239 * returns:
2240 * 0, on success
2241 * -1, on failure
2242 */
2243int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2244{
2245 int len;
2246 const uint32_t *data;
2247
2248 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2249 if (data == NULL) {
2250 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2251 return -1;
2252 }
2253
2254 *entry = uimage_to_cpu (*data);
2255 return 0;
2256}
2257
2258/**
2259 * fit_image_get_data - get data property and its size for a given component image node
2260 * @fit: pointer to the FIT format image header
2261 * @noffset: component image node offset
2262 * @data: double pointer to void, will hold data property's data address
2263 * @size: pointer to size_t, will hold data property's data size
2264 *
2265 * fit_image_get_data() finds data property in a given component image node.
2266 * If the property is found its data start address and size are returned to
2267 * the caller.
2268 *
2269 * returns:
2270 * 0, on success
2271 * -1, on failure
2272 */
2273int fit_image_get_data (const void *fit, int noffset,
2274 const void **data, size_t *size)
2275{
2276 int len;
2277
2278 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2279 if (*data == NULL) {
2280 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2281 *size = 0;
2282 return -1;
2283 }
2284
2285 *size = len;
2286 return 0;
2287}
2288
2289/**
2290 * fit_image_hash_get_algo - get hash algorithm name
2291 * @fit: pointer to the FIT format image header
2292 * @noffset: hash node offset
2293 * @algo: double pointer to char, will hold pointer to the algorithm name
2294 *
2295 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2296 * If the property is found its data start address is returned to the caller.
2297 *
2298 * returns:
2299 * 0, on success
2300 * -1, on failure
2301 */
2302int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2303{
2304 int len;
2305
2306 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2307 if (*algo == NULL) {
2308 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2309 return -1;
2310 }
2311
2312 return 0;
2313}
2314
2315/**
2316 * fit_image_hash_get_value - get hash value and length
2317 * @fit: pointer to the FIT format image header
2318 * @noffset: hash node offset
2319 * @value: double pointer to uint8_t, will hold address of a hash value data
2320 * @value_len: pointer to an int, will hold hash data length
2321 *
2322 * fit_image_hash_get_value() finds hash value property in a given hash node.
2323 * If the property is found its data start address and size are returned to
2324 * the caller.
2325 *
2326 * returns:
2327 * 0, on success
2328 * -1, on failure
2329 */
2330int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2331 int *value_len)
2332{
2333 int len;
2334
2335 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2336 if (*value == NULL) {
2337 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2338 *value_len = 0;
2339 return -1;
2340 }
2341
2342 *value_len = len;
2343 return 0;
2344}
2345
2346/**
2347 * fit_set_timestamp - set node timestamp property
2348 * @fit: pointer to the FIT format image header
2349 * @noffset: node offset
2350 * @timestamp: timestamp value to be set
2351 *
2352 * fit_set_timestamp() attempts to set timestamp property in the requested
2353 * node and returns operation status to the caller.
2354 *
2355 * returns:
2356 * 0, on success
2357 * -1, on property read failure
2358 */
2359int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2360{
2361 uint32_t t;
2362 int ret;
2363
2364 t = cpu_to_uimage (timestamp);
2365 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2366 sizeof (uint32_t));
2367 if (ret) {
2368 printf ("Can't set '%s' property for '%s' node (%s)\n",
2369 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2370 fdt_strerror (ret));
2371 return -1;
2372 }
2373
2374 return 0;
2375}
2376
2377/**
2378 * calculate_hash - calculate and return hash for provided input data
2379 * @data: pointer to the input data
2380 * @data_len: data length
2381 * @algo: requested hash algorithm
2382 * @value: pointer to the char, will hold hash value data (caller must
2383 * allocate enough free space)
2384 * value_len: length of the calculated hash
2385 *
2386 * calculate_hash() computes input data hash according to the requested algorithm.
2387 * Resulting hash value is placed in caller provided 'value' buffer, length
2388 * of the calculated hash is returned via value_len pointer argument.
2389 *
2390 * returns:
2391 * 0, on success
2392 * -1, when algo is unsupported
2393 */
2394static int calculate_hash (const void *data, int data_len, const char *algo,
2395 uint8_t *value, int *value_len)
2396{
2397 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002398 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2399 CHUNKSZ_CRC32);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002400 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2401 *value_len = 4;
2402 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002403 sha1_csum_wd ((unsigned char *) data, data_len,
2404 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002405 *value_len = 20;
2406 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002407 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka766529f2008-03-14 16:22:34 +01002408 *value_len = 16;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002409 } else {
2410 debug ("Unsupported hash alogrithm\n");
2411 return -1;
2412 }
2413 return 0;
2414}
2415
2416#ifdef USE_HOSTCC
2417/**
2418 * fit_set_hashes - process FIT component image nodes and calculate hashes
2419 * @fit: pointer to the FIT format image header
2420 *
2421 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2422 * Hashes are calculated for all component images which have hash subnodes
2423 * with algorithm property set to one of the supported hash algorithms.
2424 *
2425 * returns
2426 * 0, on success
2427 * libfdt error code, on failure
2428 */
2429int fit_set_hashes (void *fit)
2430{
2431 int images_noffset;
2432 int noffset;
2433 int ndepth;
2434 int ret;
2435
2436 /* Find images parent node offset */
2437 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2438 if (images_noffset < 0) {
2439 printf ("Can't find images parent node '%s' (%s)\n",
2440 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2441 return images_noffset;
2442 }
2443
2444 /* Process its subnodes, print out component images details */
2445 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2446 (noffset >= 0) && (ndepth > 0);
2447 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2448 if (ndepth == 1) {
2449 /*
2450 * Direct child node of the images parent node,
2451 * i.e. component image node.
2452 */
2453 ret = fit_image_set_hashes (fit, noffset);
2454 if (ret)
2455 return ret;
2456 }
2457 }
2458
2459 return 0;
2460}
2461
2462/**
2463 * fit_image_set_hashes - calculate/set hashes for given component image node
2464 * @fit: pointer to the FIT format image header
2465 * @image_noffset: requested component image node
2466 *
2467 * fit_image_set_hashes() adds hash values for an component image node. All
2468 * existing hash subnodes are checked, if algorithm property is set to one of
2469 * the supported hash algorithms, hash value is computed and corresponding
2470 * hash node property is set, for example:
2471 *
2472 * Input component image node structure:
2473 *
2474 * o image@1 (at image_noffset)
2475 * | - data = [binary data]
2476 * o hash@1
2477 * |- algo = "sha1"
2478 *
2479 * Output component image node structure:
2480 *
2481 * o image@1 (at image_noffset)
2482 * | - data = [binary data]
2483 * o hash@1
2484 * |- algo = "sha1"
2485 * |- value = sha1(data)
2486 *
2487 * returns:
2488 * 0 on sucess
2489 * <0 on failure
2490 */
2491int fit_image_set_hashes (void *fit, int image_noffset)
2492{
2493 const void *data;
2494 size_t size;
2495 char *algo;
2496 uint8_t value[FIT_MAX_HASH_LEN];
2497 int value_len;
2498 int noffset;
2499 int ndepth;
2500
2501 /* Get image data and data length */
2502 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2503 printf ("Can't get image data/size\n");
2504 return -1;
2505 }
2506
2507 /* Process all hash subnodes of the component image node */
2508 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2509 (noffset >= 0) && (ndepth > 0);
2510 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2511 if (ndepth == 1) {
2512 /* Direct child node of the component image node */
2513
2514 /*
2515 * Check subnode name, must be equal to "hash".
2516 * Multiple hash nodes require unique unit node
2517 * names, e.g. hash@1, hash@2, etc.
2518 */
2519 if (strncmp (fit_get_name(fit, noffset, NULL),
2520 FIT_HASH_NODENAME,
2521 strlen(FIT_HASH_NODENAME)) != 0) {
2522 /* Not a hash subnode, skip it */
2523 continue;
2524 }
2525
2526 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2527 printf ("Can't get hash algo property for "
2528 "'%s' hash node in '%s' image node\n",
2529 fit_get_name (fit, noffset, NULL),
2530 fit_get_name (fit, image_noffset, NULL));
2531 return -1;
2532 }
2533
2534 if (calculate_hash (data, size, algo, value, &value_len)) {
2535 printf ("Unsupported hash algorithm (%s) for "
2536 "'%s' hash node in '%s' image node\n",
2537 algo, fit_get_name (fit, noffset, NULL),
2538 fit_get_name (fit, image_noffset, NULL));
2539 return -1;
2540 }
2541
2542 if (fit_image_hash_set_value (fit, noffset, value,
2543 value_len)) {
2544 printf ("Can't set hash value for "
2545 "'%s' hash node in '%s' image node\n",
2546 fit_get_name (fit, noffset, NULL),
2547 fit_get_name (fit, image_noffset, NULL));
2548 return -1;
2549 }
2550 }
2551 }
2552
2553 return 0;
2554}
2555
2556/**
2557 * fit_image_hash_set_value - set hash value in requested has node
2558 * @fit: pointer to the FIT format image header
2559 * @noffset: hash node offset
2560 * @value: hash value to be set
2561 * @value_len: hash value length
2562 *
2563 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2564 * given and returns operation status to the caller.
2565 *
2566 * returns
2567 * 0, on success
2568 * -1, on failure
2569 */
2570int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2571 int value_len)
2572{
2573 int ret;
2574
2575 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2576 if (ret) {
2577 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2578 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2579 fdt_strerror (ret));
2580 return -1;
2581 }
2582
2583 return 0;
2584}
2585#endif /* USE_HOSTCC */
2586
2587/**
2588 * fit_image_check_hashes - verify data intergity
2589 * @fit: pointer to the FIT format image header
2590 * @image_noffset: component image node offset
2591 *
2592 * fit_image_check_hashes() goes over component image hash nodes,
2593 * re-calculates each data hash and compares with the value stored in hash
2594 * node.
2595 *
2596 * returns:
2597 * 1, if all hashes are valid
2598 * 0, otherwise (or on error)
2599 */
2600int fit_image_check_hashes (const void *fit, int image_noffset)
2601{
2602 const void *data;
2603 size_t size;
2604 char *algo;
2605 uint8_t *fit_value;
2606 int fit_value_len;
2607 uint8_t value[FIT_MAX_HASH_LEN];
2608 int value_len;
2609 int noffset;
2610 int ndepth;
2611 char *err_msg = "";
2612
2613 /* Get image data and data length */
2614 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2615 printf ("Can't get image data/size\n");
2616 return 0;
2617 }
2618
2619 /* Process all hash subnodes of the component image node */
2620 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2621 (noffset >= 0) && (ndepth > 0);
2622 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2623 if (ndepth == 1) {
2624 /* Direct child node of the component image node */
2625
2626 /*
2627 * Check subnode name, must be equal to "hash".
2628 * Multiple hash nodes require unique unit node
2629 * names, e.g. hash@1, hash@2, etc.
2630 */
2631 if (strncmp (fit_get_name(fit, noffset, NULL),
2632 FIT_HASH_NODENAME,
2633 strlen(FIT_HASH_NODENAME)) != 0)
2634 continue;
2635
2636 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002637 err_msg = " error!\nCan't get hash algo "
2638 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002639 goto error;
2640 }
2641 printf ("%s", algo);
2642
2643 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2644 &fit_value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002645 err_msg = " error!\nCan't get hash value "
2646 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002647 goto error;
2648 }
2649
2650 if (calculate_hash (data, size, algo, value, &value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002651 err_msg = " error!\nUnsupported hash algorithm";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002652 goto error;
2653 }
2654
2655 if (value_len != fit_value_len) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002656 err_msg = " error !\nBad hash value len";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002657 goto error;
2658 } else if (memcmp (value, fit_value, value_len) != 0) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002659 err_msg = " error!\nBad hash value";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002660 goto error;
2661 }
2662 printf ("+ ");
2663 }
2664 }
2665
2666 return 1;
2667
2668error:
2669 printf ("%s for '%s' hash node in '%s' image node\n",
2670 err_msg, fit_get_name (fit, noffset, NULL),
2671 fit_get_name (fit, image_noffset, NULL));
2672 return 0;
2673}
2674
2675/**
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002676 * fit_all_image_check_hashes - verify data intergity for all images
2677 * @fit: pointer to the FIT format image header
2678 *
2679 * fit_all_image_check_hashes() goes over all images in the FIT and
2680 * for every images checks if all it's hashes are valid.
2681 *
2682 * returns:
2683 * 1, if all hashes of all images are valid
2684 * 0, otherwise (or on error)
2685 */
2686int fit_all_image_check_hashes (const void *fit)
2687{
2688 int images_noffset;
2689 int noffset;
2690 int ndepth;
2691 int count;
2692
2693 /* Find images parent node offset */
2694 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2695 if (images_noffset < 0) {
2696 printf ("Can't find images parent node '%s' (%s)\n",
2697 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2698 return 0;
2699 }
2700
2701 /* Process all image subnodes, check hashes for each */
2702 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2703 (ulong)fit);
2704 for (ndepth = 0, count = 0,
2705 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2706 (noffset >= 0) && (ndepth > 0);
2707 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2708 if (ndepth == 1) {
2709 /*
2710 * Direct child node of the images parent node,
2711 * i.e. component image node.
2712 */
2713 printf (" Hash(es) for Image %u (%s): ", count++,
2714 fit_get_name (fit, noffset, NULL));
2715
2716 if (!fit_image_check_hashes (fit, noffset))
2717 return 0;
2718 printf ("\n");
2719 }
2720 }
2721 return 1;
2722}
2723
2724/**
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002725 * fit_image_check_os - check whether image node is of a given os type
2726 * @fit: pointer to the FIT format image header
2727 * @noffset: component image node offset
2728 * @os: requested image os
2729 *
2730 * fit_image_check_os() reads image os property and compares its numeric
2731 * id with the requested os. Comparison result is returned to the caller.
2732 *
2733 * returns:
2734 * 1 if image is of given os type
2735 * 0 otherwise (or on error)
2736 */
2737int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2738{
2739 uint8_t image_os;
2740
2741 if (fit_image_get_os (fit, noffset, &image_os))
2742 return 0;
2743 return (os == image_os);
2744}
2745
2746/**
2747 * fit_image_check_arch - check whether image node is of a given arch
2748 * @fit: pointer to the FIT format image header
2749 * @noffset: component image node offset
2750 * @arch: requested imagearch
2751 *
2752 * fit_image_check_arch() reads image arch property and compares its numeric
2753 * id with the requested arch. Comparison result is returned to the caller.
2754 *
2755 * returns:
2756 * 1 if image is of given arch
2757 * 0 otherwise (or on error)
2758 */
2759int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2760{
2761 uint8_t image_arch;
2762
2763 if (fit_image_get_arch (fit, noffset, &image_arch))
2764 return 0;
2765 return (arch == image_arch);
2766}
2767
2768/**
2769 * fit_image_check_type - check whether image node is of a given type
2770 * @fit: pointer to the FIT format image header
2771 * @noffset: component image node offset
2772 * @type: requested image type
2773 *
2774 * fit_image_check_type() reads image type property and compares its numeric
2775 * id with the requested type. Comparison result is returned to the caller.
2776 *
2777 * returns:
2778 * 1 if image is of given type
2779 * 0 otherwise (or on error)
2780 */
2781int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2782{
2783 uint8_t image_type;
2784
2785 if (fit_image_get_type (fit, noffset, &image_type))
2786 return 0;
2787 return (type == image_type);
2788}
2789
2790/**
2791 * fit_image_check_comp - check whether image node uses given compression
2792 * @fit: pointer to the FIT format image header
2793 * @noffset: component image node offset
2794 * @comp: requested image compression type
2795 *
2796 * fit_image_check_comp() reads image compression property and compares its
2797 * numeric id with the requested compression type. Comparison result is
2798 * returned to the caller.
2799 *
2800 * returns:
2801 * 1 if image uses requested compression
2802 * 0 otherwise (or on error)
2803 */
2804int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2805{
2806 uint8_t image_comp;
2807
2808 if (fit_image_get_comp (fit, noffset, &image_comp))
2809 return 0;
2810 return (comp == image_comp);
2811}
2812
2813/**
2814 * fit_check_format - sanity check FIT image format
2815 * @fit: pointer to the FIT format image header
2816 *
2817 * fit_check_format() runs a basic sanity FIT image verification.
2818 * Routine checks for mandatory properties, nodes, etc.
2819 *
2820 * returns:
2821 * 1, on success
2822 * 0, on failure
2823 */
2824int fit_check_format (const void *fit)
2825{
2826 /* mandatory / node 'description' property */
2827 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2828 debug ("Wrong FIT format: no description\n");
2829 return 0;
2830 }
2831
2832#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2833 /* mandatory / node 'timestamp' property */
2834 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002835 debug ("Wrong FIT format: no timestamp\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002836 return 0;
2837 }
2838#endif
2839
2840 /* mandatory subimages parent '/images' node */
2841 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2842 debug ("Wrong FIT format: no images parent node\n");
2843 return 0;
2844 }
2845
2846 return 1;
2847}
2848
2849/**
2850 * fit_conf_get_node - get node offset for configuration of a given unit name
2851 * @fit: pointer to the FIT format image header
2852 * @conf_uname: configuration node unit name
2853 *
2854 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2855 * parant node) of a provided unit name. If configuration is found its node offset
2856 * is returned to the caller.
2857 *
2858 * When NULL is provided in second argument fit_conf_get_node() will search
2859 * for a default configuration node instead. Default configuration node unit name
2860 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2861 *
2862 * returns:
2863 * configuration node offset when found (>=0)
2864 * negative number on failure (FDT_ERR_* code)
2865 */
2866int fit_conf_get_node (const void *fit, const char *conf_uname)
2867{
2868 int noffset, confs_noffset;
2869 int len;
2870
2871 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2872 if (confs_noffset < 0) {
2873 debug ("Can't find configurations parent node '%s' (%s)\n",
2874 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2875 return confs_noffset;
2876 }
2877
2878 if (conf_uname == NULL) {
2879 /* get configuration unit name from the default property */
2880 debug ("No configuration specified, trying default...\n");
2881 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2882 if (conf_uname == NULL) {
2883 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2884 return len;
2885 }
2886 debug ("Found default configuration: '%s'\n", conf_uname);
2887 }
2888
2889 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2890 if (noffset < 0) {
2891 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2892 conf_uname, fdt_strerror (noffset));
2893 }
2894
2895 return noffset;
2896}
2897
2898static int __fit_conf_get_prop_node (const void *fit, int noffset,
2899 const char *prop_name)
2900{
2901 char *uname;
2902 int len;
2903
2904 /* get kernel image unit name from configuration kernel property */
2905 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2906 if (uname == NULL)
2907 return len;
2908
2909 return fit_image_get_node (fit, uname);
2910}
2911
2912/**
2913 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2914 * a given configuration
2915 * @fit: pointer to the FIT format image header
2916 * @noffset: configuration node offset
2917 *
2918 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2919 * configuration FIT_KERNEL_PROP property and translates it to the node
2920 * offset.
2921 *
2922 * returns:
2923 * image node offset when found (>=0)
2924 * negative number on failure (FDT_ERR_* code)
2925 */
2926int fit_conf_get_kernel_node (const void *fit, int noffset)
2927{
2928 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2929}
2930
2931/**
2932 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2933 * a given configuration
2934 * @fit: pointer to the FIT format image header
2935 * @noffset: configuration node offset
2936 *
2937 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2938 * configuration FIT_KERNEL_PROP property and translates it to the node
2939 * offset.
2940 *
2941 * returns:
2942 * image node offset when found (>=0)
2943 * negative number on failure (FDT_ERR_* code)
2944 */
2945int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2946{
2947 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2948}
2949
2950/**
2951 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2952 * a given configuration
2953 * @fit: pointer to the FIT format image header
2954 * @noffset: configuration node offset
2955 *
2956 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2957 * configuration FIT_KERNEL_PROP property and translates it to the node
2958 * offset.
2959 *
2960 * returns:
2961 * image node offset when found (>=0)
2962 * negative number on failure (FDT_ERR_* code)
2963 */
2964int fit_conf_get_fdt_node (const void *fit, int noffset)
2965{
2966 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2967}
2968
2969/**
2970 * fit_conf_print - prints out the FIT configuration details
2971 * @fit: pointer to the FIT format image header
Marian Balakowiczf773bea2008-03-12 10:35:46 +01002972 * @noffset: offset of the configuration node
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002973 * @p: pointer to prefix string
2974 *
2975 * fit_conf_print() lists all mandatory properies for the processed
2976 * configuration node.
2977 *
2978 * returns:
2979 * no returned results
2980 */
2981void fit_conf_print (const void *fit, int noffset, const char *p)
2982{
2983 char *desc;
2984 char *uname;
2985 int ret;
2986
2987 /* Mandatory properties */
2988 ret = fit_get_desc (fit, noffset, &desc);
2989 printf ("%s Description: ", p);
2990 if (ret)
2991 printf ("unavailable\n");
2992 else
2993 printf ("%s\n", desc);
2994
2995 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2996 printf ("%s Kernel: ", p);
2997 if (uname == NULL)
2998 printf ("unavailable\n");
2999 else
3000 printf ("%s\n", uname);
3001
3002 /* Optional properties */
3003 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3004 if (uname)
3005 printf ("%s Init Ramdisk: %s\n", p, uname);
3006
3007 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3008 if (uname)
3009 printf ("%s FDT: %s\n", p, uname);
3010}
Marian Balakowiczc8779642008-03-12 10:12:37 +01003011
3012/**
3013 * fit_check_ramdisk - verify FIT format ramdisk subimage
3014 * @fit_hdr: pointer to the FIT ramdisk header
3015 * @rd_noffset: ramdisk subimage node offset within FIT image
3016 * @arch: requested ramdisk image architecture type
3017 * @verify: data CRC verification flag
3018 *
3019 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3020 * specified FIT image.
3021 *
3022 * returns:
3023 * 1, on success
3024 * 0, on failure
3025 */
3026#ifndef USE_HOSTCC
3027static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3028{
3029 fit_image_print (fit, rd_noffset, " ");
3030
3031 if (verify) {
3032 puts (" Verifying Hash Integrity ... ");
3033 if (!fit_image_check_hashes (fit, rd_noffset)) {
3034 puts ("Bad Data Hash\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003035 show_boot_progress (-125);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003036 return 0;
3037 }
3038 puts ("OK\n");
3039 }
3040
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003041 show_boot_progress (126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003042 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3043 !fit_image_check_arch (fit, rd_noffset, arch) ||
3044 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3045 printf ("No Linux %s Ramdisk Image\n",
3046 genimg_get_arch_name(arch));
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003047 show_boot_progress (-126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003048 return 0;
3049 }
3050
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003051 show_boot_progress (127);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003052 return 1;
3053}
3054#endif /* USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003055#endif /* CONFIG_FIT */