blob: 5ce4b1bd24e32ddee909cc9b94c60536a9ff041c [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
Grant Likelyc3624e62011-03-28 09:58:43 +0000457phys_size_t getenv_bootm_mapsize(void)
458{
459 phys_size_t tmp;
460 char *s = getenv ("bootm_mapsize");
461 if (s) {
462 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
463 return tmp;
464 }
465
466#if defined(CONFIG_SYS_BOOTMAPSZ)
467 return CONFIG_SYS_BOOTMAPSZ;
468#else
469 return getenv_bootm_size();
470#endif
471}
472
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100473void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
474{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400475 if (to == from)
476 return;
477
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100478#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
479 while (len > 0) {
480 size_t tail = (len > chunksz) ? chunksz : len;
481 WATCHDOG_RESET ();
482 memmove (to, from, tail);
483 to += tail;
484 from += tail;
485 len -= tail;
486 }
487#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
488 memmove (to, from, len);
489#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
490}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100491#endif /* !USE_HOSTCC */
492
Prafulla Wadaskarf666dea2009-09-07 14:59:07 +0530493void genimg_print_size (uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100494{
495#ifndef USE_HOSTCC
496 printf ("%d Bytes = ", size);
497 print_size (size, "\n");
498#else
499 printf ("%d Bytes = %.2f kB = %.2f MB\n",
500 size, (double)size / 1.024e3,
501 (double)size / 1.048576e6);
502#endif
503}
504
505#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
506static void genimg_print_time (time_t timestamp)
507{
508#ifndef USE_HOSTCC
509 struct rtc_time tm;
510
511 to_tm (timestamp, &tm);
512 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
513 tm.tm_year, tm.tm_mon, tm.tm_mday,
514 tm.tm_hour, tm.tm_min, tm.tm_sec);
515#else
516 printf ("%s", ctime(&timestamp));
517#endif
518}
519#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
520
521/**
522 * get_table_entry_name - translate entry id to long name
523 * @table: pointer to a translation table for entries of a specific type
524 * @msg: message to be returned when translation fails
525 * @id: entry id to be translated
526 *
527 * get_table_entry_name() will go over translation table trying to find
528 * entry that matches given id. If matching entry is found, its long
529 * name is returned to the caller.
530 *
531 * returns:
532 * long entry name if translation succeeds
533 * msg otherwise
534 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400535char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100536{
537 for (; table->id >= 0; ++table) {
538 if (table->id == id)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200539#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Woode3d1ac72009-04-02 16:15:10 -0500540 return table->lname;
541#else
542 return table->lname + gd->reloc_off;
543#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100544 }
545 return (msg);
546}
547
548const char *genimg_get_os_name (uint8_t os)
549{
550 return (get_table_entry_name (uimage_os, "Unknown OS", os));
551}
552
553const char *genimg_get_arch_name (uint8_t arch)
554{
555 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
556}
557
558const char *genimg_get_type_name (uint8_t type)
559{
560 return (get_table_entry_name (uimage_type, "Unknown Image", type));
561}
562
563const char *genimg_get_comp_name (uint8_t comp)
564{
565 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
566}
567
568/**
569 * get_table_entry_id - translate short entry name to id
570 * @table: pointer to a translation table for entries of a specific type
571 * @table_name: to be used in case of error
572 * @name: entry short name to be translated
573 *
574 * get_table_entry_id() will go over translation table trying to find
575 * entry that matches given short name. If matching entry is found,
576 * its id returned to the caller.
577 *
578 * returns:
579 * entry id if translation succeeds
580 * -1 otherwise
581 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400582int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100583 const char *table_name, const char *name)
584{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400585 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100586#ifdef USE_HOSTCC
587 int first = 1;
588
589 for (t = table; t->id >= 0; ++t) {
590 if (t->sname && strcasecmp(t->sname, name) == 0)
591 return (t->id);
592 }
593
594 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
595 for (t = table; t->id >= 0; ++t) {
596 if (t->sname == NULL)
597 continue;
598 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
599 first = 0;
600 }
601 fprintf (stderr, "\n");
602#else
603 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200604#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Woode3d1ac72009-04-02 16:15:10 -0500605 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200606#else
607 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500608#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100609 return (t->id);
610 }
611 debug ("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100612#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100613 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100614}
615
Marian Balakowicz570abb02008-02-29 15:59:59 +0100616int genimg_get_os_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100617{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100618 return (get_table_entry_id (uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100619}
620
Marian Balakowicz570abb02008-02-29 15:59:59 +0100621int genimg_get_arch_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100622{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100623 return (get_table_entry_id (uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100624}
625
Marian Balakowicz570abb02008-02-29 15:59:59 +0100626int genimg_get_type_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100627{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100628 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100629}
630
Marian Balakowicz570abb02008-02-29 15:59:59 +0100631int genimg_get_comp_id (const char *name)
632{
633 return (get_table_entry_id (uimage_comp, "Compression", name));
634}
635
636#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100637/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100638 * genimg_get_format - get image format type
639 * @img_addr: image start address
640 *
641 * genimg_get_format() checks whether provided address points to a valid
642 * legacy or FIT image.
643 *
644 * New uImage format and FDT blob are based on a libfdt. FDT blob
645 * may be passed directly or embedded in a FIT image. In both situations
646 * genimg_get_format() must be able to dectect libfdt header.
647 *
648 * returns:
649 * image format type or IMAGE_FORMAT_INVALID if no image is present
650 */
651int genimg_get_format (void *img_addr)
652{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200653 ulong format = IMAGE_FORMAT_INVALID;
654 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100655#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200656 char *fit_hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100657#endif
658
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200659 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100660 if (image_check_magic(hdr))
661 format = IMAGE_FORMAT_LEGACY;
662#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
663 else {
664 fit_hdr = (char *)img_addr;
665 if (fdt_check_header (fit_hdr) == 0)
666 format = IMAGE_FORMAT_FIT;
667 }
668#endif
669
670 return format;
671}
672
673/**
674 * genimg_get_image - get image from special storage (if necessary)
675 * @img_addr: image start address
676 *
677 * genimg_get_image() checks if provided image start adddress is located
678 * in a dataflash storage. If so, image is moved to a system RAM memory.
679 *
680 * returns:
681 * image start address after possible relocation from special storage
682 */
683ulong genimg_get_image (ulong img_addr)
684{
685 ulong ram_addr = img_addr;
686
687#ifdef CONFIG_HAS_DATAFLASH
688 ulong h_size, d_size;
689
690 if (addr_dataflash (img_addr)){
691 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200692 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100693
694 /* get header size */
695 h_size = image_get_header_size ();
696#if defined(CONFIG_FIT)
697 if (sizeof(struct fdt_header) > h_size)
698 h_size = sizeof(struct fdt_header);
699#endif
700
701 /* read in header */
702 debug (" Reading image header from dataflash address "
703 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
704
705 read_dataflash (img_addr, h_size, (char *)ram_addr);
706
707 /* get data size */
708 switch (genimg_get_format ((void *)ram_addr)) {
709 case IMAGE_FORMAT_LEGACY:
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200710 d_size = image_get_data_size ((const image_header_t *)ram_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100711 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
712 ram_addr, d_size);
713 break;
714#if defined(CONFIG_FIT)
715 case IMAGE_FORMAT_FIT:
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100716 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100717 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
718 ram_addr, d_size);
719 break;
720#endif
721 default:
722 printf (" No valid image found at 0x%08lx\n", img_addr);
723 return ram_addr;
724 }
725
726 /* read in image data */
727 debug (" Reading image remaining data from dataflash address "
728 "%08lx to RAM address %08lx\n", img_addr + h_size,
729 ram_addr + h_size);
730
731 read_dataflash (img_addr + h_size, d_size,
732 (char *)(ram_addr + h_size));
733
734 }
735#endif /* CONFIG_HAS_DATAFLASH */
736
737 return ram_addr;
738}
739
740/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100741 * fit_has_config - check if there is a valid FIT configuration
742 * @images: pointer to the bootm command headers structure
743 *
744 * fit_has_config() checks if there is a FIT configuration in use
745 * (if FTI support is present).
746 *
747 * returns:
748 * 0, no FIT support or no configuration found
749 * 1, configuration found
750 */
751int genimg_has_config (bootm_headers_t *images)
752{
753#if defined(CONFIG_FIT)
754 if (images->fit_uname_cfg)
755 return 1;
756#endif
757 return 0;
758}
759
760/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100761 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100762 * @argc: command argument count
763 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100764 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100765 * @arch: expected ramdisk architecture
766 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
767 * @rd_end: pointer to a ulong variable, will hold ramdisk end
768 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100769 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100770 * Curently supported are the following ramdisk sources:
771 * - multicomponent kernel/ramdisk image,
772 * - commandline provided address of decicated ramdisk image.
773 *
774 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100775 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100776 * rd_start and rd_end are set to ramdisk start/end addresses if
777 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100778 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500779 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100780 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100781 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200782int boot_get_ramdisk (int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100783 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100784{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100785 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100786 ulong rd_data, rd_len;
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200787 const image_header_t *rd_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100788#if defined(CONFIG_FIT)
789 void *fit_hdr;
790 const char *fit_uname_config = NULL;
791 const char *fit_uname_ramdisk = NULL;
792 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100793 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100794 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100795 const void *data;
796 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100797#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100798
Marian Balakowiczc8779642008-03-12 10:12:37 +0100799 *rd_start = 0;
800 *rd_end = 0;
801
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100802 /*
803 * Look for a '-' which indicates to ignore the
804 * ramdisk argument
805 */
806 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
807 debug ("## Skipping init Ramdisk\n");
808 rd_len = rd_data = 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100809 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100810#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100811 if (argc >= 3) {
812 /*
813 * If the init ramdisk comes from the FIT image and
814 * the FIT image address is omitted in the command
815 * line argument, try to use os FIT image address or
816 * default load address.
817 */
818 if (images->fit_uname_os)
819 default_addr = (ulong)images->fit_hdr_os;
820 else
821 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100822
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100823 if (fit_parse_conf (argv[2], default_addr,
824 &rd_addr, &fit_uname_config)) {
825 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
826 fit_uname_config, rd_addr);
827 } else if (fit_parse_subimage (argv[2], default_addr,
828 &rd_addr, &fit_uname_ramdisk)) {
829 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
830 fit_uname_ramdisk, rd_addr);
831 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100832#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100833 {
834 rd_addr = simple_strtoul(argv[2], NULL, 16);
835 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
836 rd_addr);
837 }
838#if defined(CONFIG_FIT)
839 } else {
840 /* use FIT configuration provided in first bootm
841 * command argument
842 */
843 rd_addr = (ulong)images->fit_hdr_os;
844 fit_uname_config = images->fit_uname_cfg;
845 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
846 fit_uname_config, rd_addr);
847
848 /*
849 * Check whether configuration has ramdisk defined,
850 * if not, don't try to use it, quit silently.
851 */
852 fit_hdr = (void *)rd_addr;
853 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
854 if (cfg_noffset < 0) {
855 debug ("* ramdisk: no such config\n");
Michal Simekc78fce62008-07-11 10:43:13 +0200856 return 1;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100857 }
858
859 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
860 if (rd_noffset < 0) {
861 debug ("* ramdisk: no ramdisk in config\n");
Peter Tyser41266c92008-08-05 10:51:57 -0500862 return 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100863 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100864 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100865#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100866
867 /* copy from dataflash if needed */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100868 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100869
870 /*
871 * Check if there is an initrd image at the
872 * address provided in the second bootm argument
873 * check image type, for FIT images get FIT node.
874 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100875 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100876 case IMAGE_FORMAT_LEGACY:
Marian Balakowiczc8779642008-03-12 10:12:37 +0100877 printf ("## Loading init Ramdisk from Legacy "
878 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100879
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100880 show_boot_progress (9);
Marian Balakowiczd985c842008-03-12 10:14:38 +0100881 rd_hdr = image_get_ramdisk (rd_addr, arch,
882 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100883
Marian Balakowiczc8779642008-03-12 10:12:37 +0100884 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600885 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600886
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100887 rd_data = image_get_data (rd_hdr);
888 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100889 rd_load = image_get_load (rd_hdr);
890 break;
891#if defined(CONFIG_FIT)
892 case IMAGE_FORMAT_FIT:
893 fit_hdr = (void *)rd_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100894 printf ("## Loading init Ramdisk from FIT "
895 "Image at %08lx ...\n", rd_addr);
896
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100897 show_boot_progress (120);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100898 if (!fit_check_format (fit_hdr)) {
899 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100900 show_boot_progress (-120);
Michal Simekc78fce62008-07-11 10:43:13 +0200901 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100902 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100903 show_boot_progress (121);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100904
905 if (!fit_uname_ramdisk) {
906 /*
907 * no ramdisk image node unit name, try to get config
908 * node first. If config unit node name is NULL
909 * fit_conf_get_node() will try to find default config node
910 */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100911 show_boot_progress (122);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100912 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
913 if (cfg_noffset < 0) {
914 puts ("Could not find configuration node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100915 show_boot_progress (-122);
Michal Simekc78fce62008-07-11 10:43:13 +0200916 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100917 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100918 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
919 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100920
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100921 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100922 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
923 } else {
924 /* get ramdisk component image node offset */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100925 show_boot_progress (123);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100926 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
927 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100928 if (rd_noffset < 0) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100929 puts ("Could not find subimage node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100930 show_boot_progress (-124);
Michal Simekc78fce62008-07-11 10:43:13 +0200931 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100932 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100933
934 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
935
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100936 show_boot_progress (125);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100937 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
Michal Simekc78fce62008-07-11 10:43:13 +0200938 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100939
940 /* get ramdisk image data address and length */
941 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
942 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100943 show_boot_progress (-127);
Michal Simekc78fce62008-07-11 10:43:13 +0200944 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100945 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100946 show_boot_progress (128);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100947
948 rd_data = (ulong)data;
949 rd_len = size;
950
951 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
952 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100953 show_boot_progress (-129);
Michal Simekc78fce62008-07-11 10:43:13 +0200954 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100955 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100956 show_boot_progress (129);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100957
958 images->fit_hdr_rd = fit_hdr;
959 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100960 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100961 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100962#endif
963 default:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100964 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +0100965 rd_data = rd_len = rd_load = 0;
Kumar Galaea86b9e2008-08-29 19:08:29 -0500966 return 1;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100967 }
968
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100969#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100970 /*
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100971 * We need to copy the ramdisk to SRAM to let Linux boot
972 */
973 if (rd_data) {
974 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
975 rd_data = rd_load;
976 }
977#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
978
979 } else if (images->legacy_hdr_valid &&
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200980 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100981 /*
982 * Now check if we have a legacy mult-component image,
983 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100984 */
985 show_boot_progress (13);
986 printf ("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100987 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100988 (ulong)images->legacy_hdr_os);
989
990 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100991 } else {
992 /*
993 * no initrd image
994 */
995 show_boot_progress (14);
996 rd_len = rd_data = 0;
997 }
998
999 if (!rd_data) {
1000 debug ("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001001 } else {
1002 *rd_start = rd_data;
1003 *rd_end = rd_data + rd_len;
1004 }
1005 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1006 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -06001007
1008 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001009}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001010
John Rigbyfca43cc2010-10-13 13:57:35 -06001011#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001012/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001013 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001014 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001015 * @rd_data: ramdisk data start address
1016 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001017 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1018 * start address (after possible relocation)
1019 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1020 * end address (after possible relocation)
1021 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001022 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001023 * variable and if requested ramdisk data is moved to a specified location.
1024 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001025 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1026 * start/end addresses if ramdisk image start and len were provided,
1027 * otherwise set initrd_start and initrd_end set to zeros.
1028 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001029 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001030 * 0 - success
1031 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001032 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001033int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001034 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001035{
1036 char *s;
1037 ulong initrd_high;
1038 int initrd_copy_to_ram = 1;
1039
1040 if ((s = getenv ("initrd_high")) != NULL) {
1041 /* a value of "no" or a similar string will act like 0,
1042 * turning the "load high" feature off. This is intentional.
1043 */
1044 initrd_high = simple_strtoul (s, NULL, 16);
1045 if (initrd_high == ~0)
1046 initrd_copy_to_ram = 0;
1047 } else {
1048 /* not set, no restrictions to load high */
1049 initrd_high = ~0;
1050 }
1051
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001052
1053#ifdef CONFIG_LOGBUFFER
1054 /* Prevent initrd from overwriting logbuffer */
1055 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1056#endif
1057
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001058 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1059 initrd_high, initrd_copy_to_ram);
1060
1061 if (rd_data) {
1062 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1063 debug (" in-place initrd\n");
1064 *initrd_start = rd_data;
1065 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001066 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001067 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001068 if (initrd_high)
Becky Bruce391fd932008-06-09 20:37:18 -05001069 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001070 else
Becky Bruce391fd932008-06-09 20:37:18 -05001071 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001072
Kumar Galae822d7f2008-02-27 21:51:49 -06001073 if (*initrd_start == 0) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001074 puts ("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001075 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001076 }
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001077 show_boot_progress (12);
1078
1079 *initrd_end = *initrd_start + rd_len;
1080 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1081 *initrd_start, *initrd_end);
1082
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001083 memmove_wd ((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001084 (void *)rd_data, rd_len, CHUNKSZ);
1085
1086 puts ("OK\n");
1087 }
1088 } else {
1089 *initrd_start = 0;
1090 *initrd_end = 0;
1091 }
1092 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1093 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001094
Kumar Galae822d7f2008-02-27 21:51:49 -06001095 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001096
Kumar Galae822d7f2008-02-27 21:51:49 -06001097error:
1098 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001099}
John Rigbyfca43cc2010-10-13 13:57:35 -06001100#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001101
Kumar Gala06a09912008-08-15 08:24:38 -05001102#ifdef CONFIG_OF_LIBFDT
1103static void fdt_error (const char *msg)
1104{
1105 puts ("ERROR: ");
1106 puts (msg);
1107 puts (" - must RESET the board to recover.\n");
1108}
1109
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001110static const image_header_t *image_get_fdt (ulong fdt_addr)
Kumar Gala06a09912008-08-15 08:24:38 -05001111{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001112 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001113
1114 image_print_contents (fdt_hdr);
1115
1116 puts (" Verifying Checksum ... ");
1117 if (!image_check_hcrc (fdt_hdr)) {
1118 fdt_error ("fdt header checksum invalid");
1119 return NULL;
1120 }
1121
1122 if (!image_check_dcrc (fdt_hdr)) {
1123 fdt_error ("fdt checksum invalid");
1124 return NULL;
1125 }
1126 puts ("OK\n");
1127
1128 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1129 fdt_error ("uImage is not a fdt");
1130 return NULL;
1131 }
1132 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1133 fdt_error ("uImage is compressed");
1134 return NULL;
1135 }
1136 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1137 fdt_error ("uImage data is not a fdt");
1138 return NULL;
1139 }
1140 return fdt_hdr;
1141}
1142
1143/**
1144 * fit_check_fdt - verify FIT format FDT subimage
1145 * @fit_hdr: pointer to the FIT header
1146 * fdt_noffset: FDT subimage node offset within FIT image
1147 * @verify: data CRC verification flag
1148 *
1149 * fit_check_fdt() verifies integrity of the FDT subimage and from
1150 * specified FIT image.
1151 *
1152 * returns:
1153 * 1, on success
1154 * 0, on failure
1155 */
1156#if defined(CONFIG_FIT)
1157static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1158{
1159 fit_image_print (fit, fdt_noffset, " ");
1160
1161 if (verify) {
1162 puts (" Verifying Hash Integrity ... ");
1163 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1164 fdt_error ("Bad Data Hash");
1165 return 0;
1166 }
1167 puts ("OK\n");
1168 }
1169
1170 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1171 fdt_error ("Not a FDT image");
1172 return 0;
1173 }
1174
1175 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1176 fdt_error ("FDT image is compressed");
1177 return 0;
1178 }
1179
1180 return 1;
1181}
1182#endif /* CONFIG_FIT */
1183
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001184#ifndef CONFIG_SYS_FDT_PAD
1185#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Gala06a09912008-08-15 08:24:38 -05001186#endif
1187
1188/**
1189 * boot_relocate_fdt - relocate flat device tree
1190 * @lmb: pointer to lmb handle, will be used for memory mgmt
Kumar Gala06a09912008-08-15 08:24:38 -05001191 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1192 * @of_size: pointer to a ulong variable, will hold fdt length
1193 *
Timur Tabi43b08af2010-05-24 15:10:25 -05001194 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1195 * relocates the of_flat_tree into that region, even if the fdt is already in
1196 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1197 * bytes.
Kumar Gala06a09912008-08-15 08:24:38 -05001198 *
1199 * of_flat_tree and of_size are set to final (after relocation) values
1200 *
1201 * returns:
1202 * 0 - success
1203 * 1 - failure
1204 */
Grant Likelyed59e582011-03-28 09:58:49 +00001205#if defined(CONFIG_OF_LIBFDT)
Grant Likely590d3ca2011-03-28 09:58:34 +00001206int boot_relocate_fdt (struct lmb *lmb, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001207{
Timur Tabi43b08af2010-05-24 15:10:25 -05001208 void *fdt_blob = *of_flat_tree;
1209 void *of_start = 0;
Kumar Gala06a09912008-08-15 08:24:38 -05001210 ulong of_len = 0;
Timur Tabi43b08af2010-05-24 15:10:25 -05001211 int err;
Kumar Gala06a09912008-08-15 08:24:38 -05001212
1213 /* nothing to do */
1214 if (*of_size == 0)
1215 return 0;
1216
1217 if (fdt_check_header (fdt_blob) != 0) {
1218 fdt_error ("image is not a fdt");
1219 goto error;
1220 }
1221
Timur Tabi43b08af2010-05-24 15:10:25 -05001222 /* position on a 4K boundary before the alloc_current */
1223 /* Pad the FDT by a specified amount */
1224 of_len = *of_size + CONFIG_SYS_FDT_PAD;
1225 of_start = (void *)(unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
Grant Likelyc3624e62011-03-28 09:58:43 +00001226 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Gala06a09912008-08-15 08:24:38 -05001227
Timur Tabi43b08af2010-05-24 15:10:25 -05001228 if (of_start == 0) {
1229 puts("device tree - allocation error\n");
1230 goto error;
Kumar Gala06a09912008-08-15 08:24:38 -05001231 }
1232
Timur Tabi43b08af2010-05-24 15:10:25 -05001233 debug ("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1234 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1235
1236 printf (" Loading Device Tree to %p, end %p ... ",
1237 of_start, of_start + of_len - 1);
1238
1239 err = fdt_open_into (fdt_blob, of_start, of_len);
1240 if (err != 0) {
1241 fdt_error ("fdt move failed");
1242 goto error;
1243 }
1244 puts ("OK\n");
1245
1246 *of_flat_tree = of_start;
1247 *of_size = of_len;
1248
Kumar Gala54f9c862008-08-15 08:24:39 -05001249 set_working_fdt_addr(*of_flat_tree);
Kumar Gala06a09912008-08-15 08:24:38 -05001250 return 0;
1251
1252error:
1253 return 1;
1254}
Grant Likelyed59e582011-03-28 09:58:49 +00001255#endif /* CONFIG_OF_LIBFDT */
Kumar Gala06a09912008-08-15 08:24:38 -05001256
1257/**
1258 * boot_get_fdt - main fdt handling routine
1259 * @argc: command argument count
1260 * @argv: command argument list
1261 * @images: pointer to the bootm images structure
1262 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1263 * @of_size: pointer to a ulong variable, will hold fdt length
1264 *
1265 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1266 * Curently supported are the following ramdisk sources:
1267 * - multicomponent kernel/ramdisk image,
1268 * - commandline provided address of decicated ramdisk image.
1269 *
1270 * returns:
1271 * 0, if fdt image was found and valid, or skipped
1272 * of_flat_tree and of_size are set to fdt start address and length if
1273 * fdt image is found and valid
1274 *
1275 * 1, if fdt image is found but corrupted
1276 * of_flat_tree and of_size are set to 0 if no fdt exists
1277 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001278int boot_get_fdt (int flag, int argc, char * const argv[], bootm_headers_t *images,
Kumar Gala06a09912008-08-15 08:24:38 -05001279 char **of_flat_tree, ulong *of_size)
1280{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001281 const image_header_t *fdt_hdr;
Kumar Gala06a09912008-08-15 08:24:38 -05001282 ulong fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001283 char *fdt_blob = NULL;
1284 ulong image_start, image_end;
1285 ulong load_start, load_end;
1286#if defined(CONFIG_FIT)
1287 void *fit_hdr;
1288 const char *fit_uname_config = NULL;
1289 const char *fit_uname_fdt = NULL;
1290 ulong default_addr;
1291 int cfg_noffset;
1292 int fdt_noffset;
1293 const void *data;
1294 size_t size;
1295#endif
1296
1297 *of_flat_tree = NULL;
1298 *of_size = 0;
1299
1300 if (argc > 3 || genimg_has_config (images)) {
1301#if defined(CONFIG_FIT)
1302 if (argc > 3) {
1303 /*
1304 * If the FDT blob comes from the FIT image and the
1305 * FIT image address is omitted in the command line
1306 * argument, try to use ramdisk or os FIT image
1307 * address or default load address.
1308 */
1309 if (images->fit_uname_rd)
1310 default_addr = (ulong)images->fit_hdr_rd;
1311 else if (images->fit_uname_os)
1312 default_addr = (ulong)images->fit_hdr_os;
1313 else
1314 default_addr = load_addr;
1315
1316 if (fit_parse_conf (argv[3], default_addr,
1317 &fdt_addr, &fit_uname_config)) {
1318 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1319 fit_uname_config, fdt_addr);
1320 } else if (fit_parse_subimage (argv[3], default_addr,
1321 &fdt_addr, &fit_uname_fdt)) {
1322 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1323 fit_uname_fdt, fdt_addr);
1324 } else
1325#endif
1326 {
1327 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1328 debug ("* fdt: cmdline image address = 0x%08lx\n",
1329 fdt_addr);
1330 }
1331#if defined(CONFIG_FIT)
1332 } else {
1333 /* use FIT configuration provided in first bootm
1334 * command argument
1335 */
1336 fdt_addr = (ulong)images->fit_hdr_os;
1337 fit_uname_config = images->fit_uname_cfg;
1338 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1339 fit_uname_config, fdt_addr);
1340
1341 /*
1342 * Check whether configuration has FDT blob defined,
1343 * if not quit silently.
1344 */
1345 fit_hdr = (void *)fdt_addr;
1346 cfg_noffset = fit_conf_get_node (fit_hdr,
1347 fit_uname_config);
1348 if (cfg_noffset < 0) {
1349 debug ("* fdt: no such config\n");
1350 return 0;
1351 }
1352
1353 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1354 cfg_noffset);
1355 if (fdt_noffset < 0) {
1356 debug ("* fdt: no fdt in config\n");
1357 return 0;
1358 }
1359 }
1360#endif
1361
1362 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1363 fdt_addr);
1364
1365 /* copy from dataflash if needed */
1366 fdt_addr = genimg_get_image (fdt_addr);
1367
1368 /*
1369 * Check if there is an FDT image at the
1370 * address provided in the second bootm argument
1371 * check image type, for FIT images get a FIT node.
1372 */
1373 switch (genimg_get_format ((void *)fdt_addr)) {
1374 case IMAGE_FORMAT_LEGACY:
1375 /* verify fdt_addr points to a valid image header */
1376 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1377 fdt_addr);
1378 fdt_hdr = image_get_fdt (fdt_addr);
1379 if (!fdt_hdr)
1380 goto error;
1381
1382 /*
1383 * move image data to the load address,
1384 * make sure we don't overwrite initial image
1385 */
1386 image_start = (ulong)fdt_hdr;
1387 image_end = image_get_image_end (fdt_hdr);
1388
1389 load_start = image_get_load (fdt_hdr);
1390 load_end = load_start + image_get_data_size (fdt_hdr);
1391
1392 if ((load_start < image_end) && (load_end > image_start)) {
1393 fdt_error ("fdt overwritten");
1394 goto error;
1395 }
1396
1397 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1398 image_get_data (fdt_hdr), load_start);
1399
1400 memmove ((void *)load_start,
1401 (void *)image_get_data (fdt_hdr),
1402 image_get_data_size (fdt_hdr));
1403
1404 fdt_blob = (char *)load_start;
1405 break;
1406 case IMAGE_FORMAT_FIT:
1407 /*
1408 * This case will catch both: new uImage format
1409 * (libfdt based) and raw FDT blob (also libfdt
1410 * based).
1411 */
1412#if defined(CONFIG_FIT)
1413 /* check FDT blob vs FIT blob */
1414 if (fit_check_format ((const void *)fdt_addr)) {
1415 /*
1416 * FIT image
1417 */
1418 fit_hdr = (void *)fdt_addr;
1419 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1420 fdt_addr);
1421
1422 if (!fit_uname_fdt) {
1423 /*
1424 * no FDT blob image node unit name,
1425 * try to get config node first. If
1426 * config unit node name is NULL
1427 * fit_conf_get_node() will try to
1428 * find default config node
1429 */
1430 cfg_noffset = fit_conf_get_node (fit_hdr,
1431 fit_uname_config);
1432
1433 if (cfg_noffset < 0) {
1434 fdt_error ("Could not find configuration node\n");
1435 goto error;
1436 }
1437
1438 fit_uname_config = fdt_get_name (fit_hdr,
1439 cfg_noffset, NULL);
1440 printf (" Using '%s' configuration\n",
1441 fit_uname_config);
1442
1443 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1444 cfg_noffset);
1445 fit_uname_fdt = fit_get_name (fit_hdr,
1446 fdt_noffset, NULL);
1447 } else {
1448 /* get FDT component image node offset */
1449 fdt_noffset = fit_image_get_node (fit_hdr,
1450 fit_uname_fdt);
1451 }
1452 if (fdt_noffset < 0) {
1453 fdt_error ("Could not find subimage node\n");
1454 goto error;
1455 }
1456
1457 printf (" Trying '%s' FDT blob subimage\n",
1458 fit_uname_fdt);
1459
1460 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1461 images->verify))
1462 goto error;
1463
1464 /* get ramdisk image data address and length */
1465 if (fit_image_get_data (fit_hdr, fdt_noffset,
1466 &data, &size)) {
1467 fdt_error ("Could not find FDT subimage data");
1468 goto error;
1469 }
1470
1471 /* verift that image data is a proper FDT blob */
1472 if (fdt_check_header ((char *)data) != 0) {
1473 fdt_error ("Subimage data is not a FTD");
1474 goto error;
1475 }
1476
1477 /*
1478 * move image data to the load address,
1479 * make sure we don't overwrite initial image
1480 */
1481 image_start = (ulong)fit_hdr;
1482 image_end = fit_get_end (fit_hdr);
1483
1484 if (fit_image_get_load (fit_hdr, fdt_noffset,
1485 &load_start) == 0) {
1486 load_end = load_start + size;
1487
1488 if ((load_start < image_end) &&
1489 (load_end > image_start)) {
1490 fdt_error ("FDT overwritten");
1491 goto error;
1492 }
1493
1494 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1495 (ulong)data, load_start);
1496
1497 memmove ((void *)load_start,
1498 (void *)data, size);
1499
1500 fdt_blob = (char *)load_start;
1501 } else {
1502 fdt_blob = (char *)data;
1503 }
1504
1505 images->fit_hdr_fdt = fit_hdr;
1506 images->fit_uname_fdt = fit_uname_fdt;
1507 images->fit_noffset_fdt = fdt_noffset;
1508 break;
1509 } else
1510#endif
1511 {
1512 /*
1513 * FDT blob
1514 */
1515 fdt_blob = (char *)fdt_addr;
1516 debug ("* fdt: raw FDT blob\n");
1517 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1518 }
1519 break;
1520 default:
Anatolij Gustschin8e024942008-08-29 21:04:45 +02001521 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001522 goto error;
1523 }
1524
1525 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1526
1527 } else if (images->legacy_hdr_valid &&
1528 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1529
1530 ulong fdt_data, fdt_len;
1531
1532 /*
1533 * Now check if we have a legacy multi-component image,
1534 * get second entry data start address and len.
1535 */
1536 printf ("## Flattened Device Tree from multi "
1537 "component Image at %08lX\n",
1538 (ulong)images->legacy_hdr_os);
1539
1540 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1541 if (fdt_len) {
1542
1543 fdt_blob = (char *)fdt_data;
1544 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1545
1546 if (fdt_check_header (fdt_blob) != 0) {
1547 fdt_error ("image is not a fdt");
1548 goto error;
1549 }
1550
John Rigbyd1263fc2010-10-13 13:57:32 -06001551 if (fdt_totalsize(fdt_blob) != fdt_len) {
Kumar Gala06a09912008-08-15 08:24:38 -05001552 fdt_error ("fdt size != image size");
1553 goto error;
1554 }
1555 } else {
1556 debug ("## No Flattened Device Tree\n");
1557 return 0;
1558 }
1559 } else {
1560 debug ("## No Flattened Device Tree\n");
1561 return 0;
1562 }
1563
1564 *of_flat_tree = fdt_blob;
John Rigbyd1263fc2010-10-13 13:57:32 -06001565 *of_size = fdt_totalsize(fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001566 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner52514692008-08-21 07:12:26 -07001567 (ulong)*of_flat_tree, *of_size);
Kumar Gala06a09912008-08-15 08:24:38 -05001568
1569 return 0;
1570
1571error:
1572 *of_flat_tree = 0;
1573 *of_size = 0;
1574 return 1;
1575}
1576#endif /* CONFIG_OF_LIBFDT */
1577
John Rigbyfca43cc2010-10-13 13:57:35 -06001578#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001579/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001580 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001581 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001582 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1583 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1584 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001585 * boot_get_cmdline() allocates space for kernel command line below
Grant Likely590d3ca2011-03-28 09:58:34 +00001586 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001587 * variable is present its contents is copied to allocated kernel
1588 * command line.
1589 *
1590 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001591 * 0 - success
1592 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001593 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001594int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001595{
1596 char *cmdline;
1597 char *s;
1598
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001599 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001600 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001601
1602 if (cmdline == NULL)
1603 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001604
1605 if ((s = getenv("bootargs")) == NULL)
1606 s = "";
1607
1608 strcpy(cmdline, s);
1609
1610 *cmd_start = (ulong) & cmdline[0];
1611 *cmd_end = *cmd_start + strlen(cmdline);
1612
1613 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1614
Kumar Galae822d7f2008-02-27 21:51:49 -06001615 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001616}
John Rigbyfca43cc2010-10-13 13:57:35 -06001617#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001618
John Rigbyfca43cc2010-10-13 13:57:35 -06001619#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001620/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001621 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001622 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001623 * @kbd: double pointer to board info data
1624 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001625 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likely590d3ca2011-03-28 09:58:34 +00001626 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1627 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001628 *
1629 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001630 * 0 - success
1631 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001632 */
Grant Likely590d3ca2011-03-28 09:58:34 +00001633int boot_get_kbd (struct lmb *lmb, bd_t **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001634{
Becky Bruce391fd932008-06-09 20:37:18 -05001635 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001636 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001637 if (*kbd == NULL)
1638 return -1;
1639
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001640 **kbd = *(gd->bd);
1641
1642 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1643
1644#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1645 do_bdinfo(NULL, 0, 0, NULL);
1646#endif
1647
Kumar Galae822d7f2008-02-27 21:51:49 -06001648 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001649}
John Rigbyfca43cc2010-10-13 13:57:35 -06001650#endif /* CONFIG_SYS_BOOT_GET_KBD */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001651#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001652
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001653#if defined(CONFIG_FIT)
1654/*****************************************************************************/
1655/* New uImage format routines */
1656/*****************************************************************************/
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001657#ifndef USE_HOSTCC
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001658static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1659 ulong *addr, const char **name)
1660{
1661 const char *sep;
1662
1663 *addr = addr_curr;
1664 *name = NULL;
1665
1666 sep = strchr (spec, sepc);
1667 if (sep) {
1668 if (sep - spec > 0)
1669 *addr = simple_strtoul (spec, NULL, 16);
1670
1671 *name = sep + 1;
1672 return 1;
1673 }
1674
1675 return 0;
1676}
1677
1678/**
1679 * fit_parse_conf - parse FIT configuration spec
1680 * @spec: input string, containing configuration spec
1681 * @add_curr: current image address (to be used as a possible default)
1682 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1683 * configuration
1684 * @conf_name double pointer to a char, will hold pointer to a configuration
1685 * unit name
1686 *
1687 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1688 * where <addr> is a FIT image address that contains configuration
1689 * with a <conf> unit name.
1690 *
1691 * Address part is optional, and if omitted default add_curr will
1692 * be used instead.
1693 *
1694 * returns:
1695 * 1 if spec is a valid configuration string,
1696 * addr and conf_name are set accordingly
1697 * 0 otherwise
1698 */
1699inline int fit_parse_conf (const char *spec, ulong addr_curr,
1700 ulong *addr, const char **conf_name)
1701{
1702 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1703}
1704
1705/**
1706 * fit_parse_subimage - parse FIT subimage spec
1707 * @spec: input string, containing subimage spec
1708 * @add_curr: current image address (to be used as a possible default)
1709 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1710 * subimage
1711 * @image_name: double pointer to a char, will hold pointer to a subimage name
1712 *
1713 * fit_parse_subimage() expects subimage spec in the for of
1714 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1715 * subimage with a <subimg> unit name.
1716 *
1717 * Address part is optional, and if omitted default add_curr will
1718 * be used instead.
1719 *
1720 * returns:
1721 * 1 if spec is a valid subimage string,
1722 * addr and image_name are set accordingly
1723 * 0 otherwise
1724 */
1725inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1726 ulong *addr, const char **image_name)
1727{
1728 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1729}
Marian Balakowicz570abb02008-02-29 15:59:59 +01001730#endif /* !USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001731
1732static void fit_get_debug (const void *fit, int noffset,
1733 char *prop_name, int err)
1734{
1735 debug ("Can't get '%s' property from FIT 0x%08lx, "
1736 "node: offset %d, name %s (%s)\n",
1737 prop_name, (ulong)fit, noffset,
1738 fit_get_name (fit, noffset, NULL),
1739 fdt_strerror (err));
1740}
1741
1742/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001743 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001744 * @fit: pointer to the FIT format image header
1745 * @p: pointer to prefix string
1746 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001747 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001748 * The routine prints out FIT image properties (root node level) follwed by
1749 * the details of each component image.
1750 *
1751 * returns:
1752 * no returned results
1753 */
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001754void fit_print_contents (const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001755{
1756 char *desc;
1757 char *uname;
1758 int images_noffset;
1759 int confs_noffset;
1760 int noffset;
1761 int ndepth;
1762 int count = 0;
1763 int ret;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001764 const char *p;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001765#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1766 time_t timestamp;
1767#endif
1768
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001769#ifdef USE_HOSTCC
1770 p = "";
1771#else
1772 p = " ";
1773#endif
1774
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001775 /* Root node properties */
1776 ret = fit_get_desc (fit, 0, &desc);
1777 printf ("%sFIT description: ", p);
1778 if (ret)
1779 printf ("unavailable\n");
1780 else
1781 printf ("%s\n", desc);
1782
1783#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1784 ret = fit_get_timestamp (fit, 0, &timestamp);
1785 printf ("%sCreated: ", p);
1786 if (ret)
1787 printf ("unavailable\n");
1788 else
1789 genimg_print_time (timestamp);
1790#endif
1791
1792 /* Find images parent node offset */
1793 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1794 if (images_noffset < 0) {
1795 printf ("Can't find images parent node '%s' (%s)\n",
1796 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1797 return;
1798 }
1799
1800 /* Process its subnodes, print out component images details */
1801 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1802 (noffset >= 0) && (ndepth > 0);
1803 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1804 if (ndepth == 1) {
1805 /*
1806 * Direct child node of the images parent node,
1807 * i.e. component image node.
1808 */
1809 printf ("%s Image %u (%s)\n", p, count++,
1810 fit_get_name(fit, noffset, NULL));
1811
1812 fit_image_print (fit, noffset, p);
1813 }
1814 }
1815
1816 /* Find configurations parent node offset */
1817 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1818 if (confs_noffset < 0) {
1819 debug ("Can't get configurations parent node '%s' (%s)\n",
1820 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1821 return;
1822 }
1823
1824 /* get default configuration unit name from default property */
1825 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1826 if (uname)
1827 printf ("%s Default Configuration: '%s'\n", p, uname);
1828
1829 /* Process its subnodes, print out configurations details */
1830 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1831 (noffset >= 0) && (ndepth > 0);
1832 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1833 if (ndepth == 1) {
1834 /*
1835 * Direct child node of the configurations parent node,
1836 * i.e. configuration node.
1837 */
1838 printf ("%s Configuration %u (%s)\n", p, count++,
1839 fit_get_name(fit, noffset, NULL));
1840
1841 fit_conf_print (fit, noffset, p);
1842 }
1843 }
1844}
1845
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001846/**
1847 * fit_image_print - prints out the FIT component image details
1848 * @fit: pointer to the FIT format image header
1849 * @image_noffset: offset of the component image node
1850 * @p: pointer to prefix string
1851 *
1852 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001853 * image. If present, hash nodes are printed out as well. Load
1854 * address for images of type firmware is also printed out. Since the load
1855 * address is not mandatory for firmware images, it will be output as
1856 * "unavailable" when not present.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001857 *
1858 * returns:
1859 * no returned results
1860 */
1861void fit_image_print (const void *fit, int image_noffset, const char *p)
1862{
1863 char *desc;
1864 uint8_t type, arch, os, comp;
1865 size_t size;
1866 ulong load, entry;
1867 const void *data;
1868 int noffset;
1869 int ndepth;
1870 int ret;
1871
1872 /* Mandatory properties */
1873 ret = fit_get_desc (fit, image_noffset, &desc);
1874 printf ("%s Description: ", p);
1875 if (ret)
1876 printf ("unavailable\n");
1877 else
1878 printf ("%s\n", desc);
1879
1880 fit_image_get_type (fit, image_noffset, &type);
1881 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1882
1883 fit_image_get_comp (fit, image_noffset, &comp);
1884 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1885
1886 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1887
1888#ifndef USE_HOSTCC
1889 printf ("%s Data Start: ", p);
1890 if (ret)
1891 printf ("unavailable\n");
1892 else
1893 printf ("0x%08lx\n", (ulong)data);
1894#endif
1895
1896 printf ("%s Data Size: ", p);
1897 if (ret)
1898 printf ("unavailable\n");
1899 else
1900 genimg_print_size (size);
1901
1902 /* Remaining, type dependent properties */
1903 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1904 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1905 (type == IH_TYPE_FLATDT)) {
1906 fit_image_get_arch (fit, image_noffset, &arch);
1907 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1908 }
1909
1910 if (type == IH_TYPE_KERNEL) {
1911 fit_image_get_os (fit, image_noffset, &os);
1912 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1913 }
1914
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001915 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1916 (type == IH_TYPE_FIRMWARE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001917 ret = fit_image_get_load (fit, image_noffset, &load);
1918 printf ("%s Load Address: ", p);
1919 if (ret)
1920 printf ("unavailable\n");
1921 else
1922 printf ("0x%08lx\n", load);
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001923 }
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001924
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001925 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001926 fit_image_get_entry (fit, image_noffset, &entry);
1927 printf ("%s Entry Point: ", p);
1928 if (ret)
1929 printf ("unavailable\n");
1930 else
1931 printf ("0x%08lx\n", entry);
1932 }
1933
1934 /* Process all hash subnodes of the component image node */
1935 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1936 (noffset >= 0) && (ndepth > 0);
1937 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1938 if (ndepth == 1) {
1939 /* Direct child node of the component image node */
1940 fit_image_print_hash (fit, noffset, p);
1941 }
1942 }
1943}
1944
1945/**
1946 * fit_image_print_hash - prints out the hash node details
1947 * @fit: pointer to the FIT format image header
1948 * @noffset: offset of the hash node
1949 * @p: pointer to prefix string
1950 *
1951 * fit_image_print_hash() lists properies for the processed hash node
1952 *
1953 * returns:
1954 * no returned results
1955 */
1956void fit_image_print_hash (const void *fit, int noffset, const char *p)
1957{
1958 char *algo;
1959 uint8_t *value;
1960 int value_len;
1961 int i, ret;
1962
1963 /*
1964 * Check subnode name, must be equal to "hash".
1965 * Multiple hash nodes require unique unit node
1966 * names, e.g. hash@1, hash@2, etc.
1967 */
1968 if (strncmp (fit_get_name(fit, noffset, NULL),
1969 FIT_HASH_NODENAME,
1970 strlen(FIT_HASH_NODENAME)) != 0)
1971 return;
1972
1973 debug ("%s Hash node: '%s'\n", p,
1974 fit_get_name (fit, noffset, NULL));
1975
1976 printf ("%s Hash algo: ", p);
1977 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1978 printf ("invalid/unsupported\n");
1979 return;
1980 }
1981 printf ("%s\n", algo);
1982
1983 ret = fit_image_hash_get_value (fit, noffset, &value,
1984 &value_len);
1985 printf ("%s Hash value: ", p);
1986 if (ret) {
1987 printf ("unavailable\n");
1988 } else {
1989 for (i = 0; i < value_len; i++)
1990 printf ("%02x", value[i]);
1991 printf ("\n");
1992 }
1993
1994 debug ("%s Hash len: %d\n", p, value_len);
1995}
1996
1997/**
1998 * fit_get_desc - get node description property
1999 * @fit: pointer to the FIT format image header
2000 * @noffset: node offset
2001 * @desc: double pointer to the char, will hold pointer to the descrption
2002 *
2003 * fit_get_desc() reads description property from a given node, if
2004 * description is found pointer to it is returened in third call argument.
2005 *
2006 * returns:
2007 * 0, on success
2008 * -1, on failure
2009 */
2010int fit_get_desc (const void *fit, int noffset, char **desc)
2011{
2012 int len;
2013
2014 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2015 if (*desc == NULL) {
2016 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2017 return -1;
2018 }
2019
2020 return 0;
2021}
2022
2023/**
2024 * fit_get_timestamp - get node timestamp property
2025 * @fit: pointer to the FIT format image header
2026 * @noffset: node offset
2027 * @timestamp: pointer to the time_t, will hold read timestamp
2028 *
2029 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2030 * is found and has a correct size its value is retured in third call
2031 * argument.
2032 *
2033 * returns:
2034 * 0, on success
2035 * -1, on property read failure
2036 * -2, on wrong timestamp size
2037 */
2038int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2039{
2040 int len;
2041 const void *data;
2042
2043 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2044 if (data == NULL) {
2045 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2046 return -1;
2047 }
2048 if (len != sizeof (uint32_t)) {
2049 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2050 return -2;
2051 }
2052
2053 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2054 return 0;
2055}
2056
2057/**
2058 * fit_image_get_node - get node offset for component image of a given unit name
2059 * @fit: pointer to the FIT format image header
2060 * @image_uname: component image node unit name
2061 *
2062 * fit_image_get_node() finds a component image (withing the '/images'
2063 * node) of a provided unit name. If image is found its node offset is
2064 * returned to the caller.
2065 *
2066 * returns:
2067 * image node offset when found (>=0)
2068 * negative number on failure (FDT_ERR_* code)
2069 */
2070int fit_image_get_node (const void *fit, const char *image_uname)
2071{
2072 int noffset, images_noffset;
2073
2074 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2075 if (images_noffset < 0) {
2076 debug ("Can't find images parent node '%s' (%s)\n",
2077 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2078 return images_noffset;
2079 }
2080
2081 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2082 if (noffset < 0) {
2083 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2084 image_uname, fdt_strerror (noffset));
2085 }
2086
2087 return noffset;
2088}
2089
2090/**
2091 * fit_image_get_os - get os id for a given component image node
2092 * @fit: pointer to the FIT format image header
2093 * @noffset: component image node offset
2094 * @os: pointer to the uint8_t, will hold os numeric id
2095 *
2096 * fit_image_get_os() finds os property in a given component image node.
2097 * If the property is found, its (string) value is translated to the numeric
2098 * id which is returned to the caller.
2099 *
2100 * returns:
2101 * 0, on success
2102 * -1, on failure
2103 */
2104int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2105{
2106 int len;
2107 const void *data;
2108
2109 /* Get OS name from property data */
2110 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2111 if (data == NULL) {
2112 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2113 *os = -1;
2114 return -1;
2115 }
2116
2117 /* Translate OS name to id */
2118 *os = genimg_get_os_id (data);
2119 return 0;
2120}
2121
2122/**
2123 * fit_image_get_arch - get arch id for a given component image node
2124 * @fit: pointer to the FIT format image header
2125 * @noffset: component image node offset
2126 * @arch: pointer to the uint8_t, will hold arch numeric id
2127 *
2128 * fit_image_get_arch() finds arch property in a given component image node.
2129 * If the property is found, its (string) value is translated to the numeric
2130 * id which is returned to the caller.
2131 *
2132 * returns:
2133 * 0, on success
2134 * -1, on failure
2135 */
2136int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2137{
2138 int len;
2139 const void *data;
2140
2141 /* Get architecture name from property data */
2142 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2143 if (data == NULL) {
2144 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2145 *arch = -1;
2146 return -1;
2147 }
2148
2149 /* Translate architecture name to id */
2150 *arch = genimg_get_arch_id (data);
2151 return 0;
2152}
2153
2154/**
2155 * fit_image_get_type - get type id for a given component image node
2156 * @fit: pointer to the FIT format image header
2157 * @noffset: component image node offset
2158 * @type: pointer to the uint8_t, will hold type numeric id
2159 *
2160 * fit_image_get_type() finds type property in a given component image node.
2161 * If the property is found, its (string) value is translated to the numeric
2162 * id which is returned to the caller.
2163 *
2164 * returns:
2165 * 0, on success
2166 * -1, on failure
2167 */
2168int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2169{
2170 int len;
2171 const void *data;
2172
2173 /* Get image type name from property data */
2174 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2175 if (data == NULL) {
2176 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2177 *type = -1;
2178 return -1;
2179 }
2180
2181 /* Translate image type name to id */
2182 *type = genimg_get_type_id (data);
2183 return 0;
2184}
2185
2186/**
2187 * fit_image_get_comp - get comp id for a given component image node
2188 * @fit: pointer to the FIT format image header
2189 * @noffset: component image node offset
2190 * @comp: pointer to the uint8_t, will hold comp numeric id
2191 *
2192 * fit_image_get_comp() finds comp property in a given component image node.
2193 * If the property is found, its (string) value is translated to the numeric
2194 * id which is returned to the caller.
2195 *
2196 * returns:
2197 * 0, on success
2198 * -1, on failure
2199 */
2200int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2201{
2202 int len;
2203 const void *data;
2204
2205 /* Get compression name from property data */
2206 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2207 if (data == NULL) {
2208 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2209 *comp = -1;
2210 return -1;
2211 }
2212
2213 /* Translate compression name to id */
2214 *comp = genimg_get_comp_id (data);
2215 return 0;
2216}
2217
2218/**
2219 * fit_image_get_load - get load address property for a given component image node
2220 * @fit: pointer to the FIT format image header
2221 * @noffset: component image node offset
2222 * @load: pointer to the uint32_t, will hold load address
2223 *
2224 * fit_image_get_load() finds load address property in a given component image node.
2225 * If the property is found, its value is returned to the caller.
2226 *
2227 * returns:
2228 * 0, on success
2229 * -1, on failure
2230 */
2231int fit_image_get_load (const void *fit, int noffset, ulong *load)
2232{
2233 int len;
2234 const uint32_t *data;
2235
2236 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2237 if (data == NULL) {
2238 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2239 return -1;
2240 }
2241
2242 *load = uimage_to_cpu (*data);
2243 return 0;
2244}
2245
2246/**
2247 * fit_image_get_entry - get entry point address property for a given component image node
2248 * @fit: pointer to the FIT format image header
2249 * @noffset: component image node offset
2250 * @entry: pointer to the uint32_t, will hold entry point address
2251 *
2252 * fit_image_get_entry() finds entry point address property in a given component image node.
2253 * If the property is found, its value is returned to the caller.
2254 *
2255 * returns:
2256 * 0, on success
2257 * -1, on failure
2258 */
2259int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2260{
2261 int len;
2262 const uint32_t *data;
2263
2264 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2265 if (data == NULL) {
2266 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2267 return -1;
2268 }
2269
2270 *entry = uimage_to_cpu (*data);
2271 return 0;
2272}
2273
2274/**
2275 * fit_image_get_data - get data property and its size for a given component image node
2276 * @fit: pointer to the FIT format image header
2277 * @noffset: component image node offset
2278 * @data: double pointer to void, will hold data property's data address
2279 * @size: pointer to size_t, will hold data property's data size
2280 *
2281 * fit_image_get_data() finds data property in a given component image node.
2282 * If the property is found its data start address and size are returned to
2283 * the caller.
2284 *
2285 * returns:
2286 * 0, on success
2287 * -1, on failure
2288 */
2289int fit_image_get_data (const void *fit, int noffset,
2290 const void **data, size_t *size)
2291{
2292 int len;
2293
2294 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2295 if (*data == NULL) {
2296 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2297 *size = 0;
2298 return -1;
2299 }
2300
2301 *size = len;
2302 return 0;
2303}
2304
2305/**
2306 * fit_image_hash_get_algo - get hash algorithm name
2307 * @fit: pointer to the FIT format image header
2308 * @noffset: hash node offset
2309 * @algo: double pointer to char, will hold pointer to the algorithm name
2310 *
2311 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2312 * If the property is found its data start address is returned to the caller.
2313 *
2314 * returns:
2315 * 0, on success
2316 * -1, on failure
2317 */
2318int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2319{
2320 int len;
2321
2322 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2323 if (*algo == NULL) {
2324 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2325 return -1;
2326 }
2327
2328 return 0;
2329}
2330
2331/**
2332 * fit_image_hash_get_value - get hash value and length
2333 * @fit: pointer to the FIT format image header
2334 * @noffset: hash node offset
2335 * @value: double pointer to uint8_t, will hold address of a hash value data
2336 * @value_len: pointer to an int, will hold hash data length
2337 *
2338 * fit_image_hash_get_value() finds hash value property in a given hash node.
2339 * If the property is found its data start address and size are returned to
2340 * the caller.
2341 *
2342 * returns:
2343 * 0, on success
2344 * -1, on failure
2345 */
2346int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2347 int *value_len)
2348{
2349 int len;
2350
2351 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2352 if (*value == NULL) {
2353 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2354 *value_len = 0;
2355 return -1;
2356 }
2357
2358 *value_len = len;
2359 return 0;
2360}
2361
2362/**
2363 * fit_set_timestamp - set node timestamp property
2364 * @fit: pointer to the FIT format image header
2365 * @noffset: node offset
2366 * @timestamp: timestamp value to be set
2367 *
2368 * fit_set_timestamp() attempts to set timestamp property in the requested
2369 * node and returns operation status to the caller.
2370 *
2371 * returns:
2372 * 0, on success
2373 * -1, on property read failure
2374 */
2375int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2376{
2377 uint32_t t;
2378 int ret;
2379
2380 t = cpu_to_uimage (timestamp);
2381 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2382 sizeof (uint32_t));
2383 if (ret) {
2384 printf ("Can't set '%s' property for '%s' node (%s)\n",
2385 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2386 fdt_strerror (ret));
2387 return -1;
2388 }
2389
2390 return 0;
2391}
2392
2393/**
2394 * calculate_hash - calculate and return hash for provided input data
2395 * @data: pointer to the input data
2396 * @data_len: data length
2397 * @algo: requested hash algorithm
2398 * @value: pointer to the char, will hold hash value data (caller must
2399 * allocate enough free space)
2400 * value_len: length of the calculated hash
2401 *
2402 * calculate_hash() computes input data hash according to the requested algorithm.
2403 * Resulting hash value is placed in caller provided 'value' buffer, length
2404 * of the calculated hash is returned via value_len pointer argument.
2405 *
2406 * returns:
2407 * 0, on success
2408 * -1, when algo is unsupported
2409 */
2410static int calculate_hash (const void *data, int data_len, const char *algo,
2411 uint8_t *value, int *value_len)
2412{
2413 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002414 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2415 CHUNKSZ_CRC32);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002416 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2417 *value_len = 4;
2418 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002419 sha1_csum_wd ((unsigned char *) data, data_len,
2420 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002421 *value_len = 20;
2422 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002423 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka766529f2008-03-14 16:22:34 +01002424 *value_len = 16;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002425 } else {
2426 debug ("Unsupported hash alogrithm\n");
2427 return -1;
2428 }
2429 return 0;
2430}
2431
2432#ifdef USE_HOSTCC
2433/**
2434 * fit_set_hashes - process FIT component image nodes and calculate hashes
2435 * @fit: pointer to the FIT format image header
2436 *
2437 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2438 * Hashes are calculated for all component images which have hash subnodes
2439 * with algorithm property set to one of the supported hash algorithms.
2440 *
2441 * returns
2442 * 0, on success
2443 * libfdt error code, on failure
2444 */
2445int fit_set_hashes (void *fit)
2446{
2447 int images_noffset;
2448 int noffset;
2449 int ndepth;
2450 int ret;
2451
2452 /* Find images parent node offset */
2453 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2454 if (images_noffset < 0) {
2455 printf ("Can't find images parent node '%s' (%s)\n",
2456 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2457 return images_noffset;
2458 }
2459
2460 /* Process its subnodes, print out component images details */
2461 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2462 (noffset >= 0) && (ndepth > 0);
2463 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2464 if (ndepth == 1) {
2465 /*
2466 * Direct child node of the images parent node,
2467 * i.e. component image node.
2468 */
2469 ret = fit_image_set_hashes (fit, noffset);
2470 if (ret)
2471 return ret;
2472 }
2473 }
2474
2475 return 0;
2476}
2477
2478/**
2479 * fit_image_set_hashes - calculate/set hashes for given component image node
2480 * @fit: pointer to the FIT format image header
2481 * @image_noffset: requested component image node
2482 *
2483 * fit_image_set_hashes() adds hash values for an component image node. All
2484 * existing hash subnodes are checked, if algorithm property is set to one of
2485 * the supported hash algorithms, hash value is computed and corresponding
2486 * hash node property is set, for example:
2487 *
2488 * Input component image node structure:
2489 *
2490 * o image@1 (at image_noffset)
2491 * | - data = [binary data]
2492 * o hash@1
2493 * |- algo = "sha1"
2494 *
2495 * Output component image node structure:
2496 *
2497 * o image@1 (at image_noffset)
2498 * | - data = [binary data]
2499 * o hash@1
2500 * |- algo = "sha1"
2501 * |- value = sha1(data)
2502 *
2503 * returns:
2504 * 0 on sucess
2505 * <0 on failure
2506 */
2507int fit_image_set_hashes (void *fit, int image_noffset)
2508{
2509 const void *data;
2510 size_t size;
2511 char *algo;
2512 uint8_t value[FIT_MAX_HASH_LEN];
2513 int value_len;
2514 int noffset;
2515 int ndepth;
2516
2517 /* Get image data and data length */
2518 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2519 printf ("Can't get image data/size\n");
2520 return -1;
2521 }
2522
2523 /* Process all hash subnodes of the component image node */
2524 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2525 (noffset >= 0) && (ndepth > 0);
2526 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2527 if (ndepth == 1) {
2528 /* Direct child node of the component image node */
2529
2530 /*
2531 * Check subnode name, must be equal to "hash".
2532 * Multiple hash nodes require unique unit node
2533 * names, e.g. hash@1, hash@2, etc.
2534 */
2535 if (strncmp (fit_get_name(fit, noffset, NULL),
2536 FIT_HASH_NODENAME,
2537 strlen(FIT_HASH_NODENAME)) != 0) {
2538 /* Not a hash subnode, skip it */
2539 continue;
2540 }
2541
2542 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2543 printf ("Can't get hash algo property for "
2544 "'%s' hash node in '%s' image node\n",
2545 fit_get_name (fit, noffset, NULL),
2546 fit_get_name (fit, image_noffset, NULL));
2547 return -1;
2548 }
2549
2550 if (calculate_hash (data, size, algo, value, &value_len)) {
2551 printf ("Unsupported hash algorithm (%s) for "
2552 "'%s' hash node in '%s' image node\n",
2553 algo, fit_get_name (fit, noffset, NULL),
2554 fit_get_name (fit, image_noffset, NULL));
2555 return -1;
2556 }
2557
2558 if (fit_image_hash_set_value (fit, noffset, value,
2559 value_len)) {
2560 printf ("Can't set hash value for "
2561 "'%s' hash node in '%s' image node\n",
2562 fit_get_name (fit, noffset, NULL),
2563 fit_get_name (fit, image_noffset, NULL));
2564 return -1;
2565 }
2566 }
2567 }
2568
2569 return 0;
2570}
2571
2572/**
2573 * fit_image_hash_set_value - set hash value in requested has node
2574 * @fit: pointer to the FIT format image header
2575 * @noffset: hash node offset
2576 * @value: hash value to be set
2577 * @value_len: hash value length
2578 *
2579 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2580 * given and returns operation status to the caller.
2581 *
2582 * returns
2583 * 0, on success
2584 * -1, on failure
2585 */
2586int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2587 int value_len)
2588{
2589 int ret;
2590
2591 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2592 if (ret) {
2593 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2594 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2595 fdt_strerror (ret));
2596 return -1;
2597 }
2598
2599 return 0;
2600}
2601#endif /* USE_HOSTCC */
2602
2603/**
2604 * fit_image_check_hashes - verify data intergity
2605 * @fit: pointer to the FIT format image header
2606 * @image_noffset: component image node offset
2607 *
2608 * fit_image_check_hashes() goes over component image hash nodes,
2609 * re-calculates each data hash and compares with the value stored in hash
2610 * node.
2611 *
2612 * returns:
2613 * 1, if all hashes are valid
2614 * 0, otherwise (or on error)
2615 */
2616int fit_image_check_hashes (const void *fit, int image_noffset)
2617{
2618 const void *data;
2619 size_t size;
2620 char *algo;
2621 uint8_t *fit_value;
2622 int fit_value_len;
2623 uint8_t value[FIT_MAX_HASH_LEN];
2624 int value_len;
2625 int noffset;
2626 int ndepth;
2627 char *err_msg = "";
2628
2629 /* Get image data and data length */
2630 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2631 printf ("Can't get image data/size\n");
2632 return 0;
2633 }
2634
2635 /* Process all hash subnodes of the component image node */
2636 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2637 (noffset >= 0) && (ndepth > 0);
2638 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2639 if (ndepth == 1) {
2640 /* Direct child node of the component image node */
2641
2642 /*
2643 * Check subnode name, must be equal to "hash".
2644 * Multiple hash nodes require unique unit node
2645 * names, e.g. hash@1, hash@2, etc.
2646 */
2647 if (strncmp (fit_get_name(fit, noffset, NULL),
2648 FIT_HASH_NODENAME,
2649 strlen(FIT_HASH_NODENAME)) != 0)
2650 continue;
2651
2652 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002653 err_msg = " error!\nCan't get hash algo "
2654 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002655 goto error;
2656 }
2657 printf ("%s", algo);
2658
2659 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2660 &fit_value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002661 err_msg = " error!\nCan't get hash value "
2662 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002663 goto error;
2664 }
2665
2666 if (calculate_hash (data, size, algo, value, &value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002667 err_msg = " error!\nUnsupported hash algorithm";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002668 goto error;
2669 }
2670
2671 if (value_len != fit_value_len) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002672 err_msg = " error !\nBad hash value len";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002673 goto error;
2674 } else if (memcmp (value, fit_value, value_len) != 0) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002675 err_msg = " error!\nBad hash value";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002676 goto error;
2677 }
2678 printf ("+ ");
2679 }
2680 }
2681
2682 return 1;
2683
2684error:
2685 printf ("%s for '%s' hash node in '%s' image node\n",
2686 err_msg, fit_get_name (fit, noffset, NULL),
2687 fit_get_name (fit, image_noffset, NULL));
2688 return 0;
2689}
2690
2691/**
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002692 * fit_all_image_check_hashes - verify data intergity for all images
2693 * @fit: pointer to the FIT format image header
2694 *
2695 * fit_all_image_check_hashes() goes over all images in the FIT and
2696 * for every images checks if all it's hashes are valid.
2697 *
2698 * returns:
2699 * 1, if all hashes of all images are valid
2700 * 0, otherwise (or on error)
2701 */
2702int fit_all_image_check_hashes (const void *fit)
2703{
2704 int images_noffset;
2705 int noffset;
2706 int ndepth;
2707 int count;
2708
2709 /* Find images parent node offset */
2710 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2711 if (images_noffset < 0) {
2712 printf ("Can't find images parent node '%s' (%s)\n",
2713 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2714 return 0;
2715 }
2716
2717 /* Process all image subnodes, check hashes for each */
2718 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2719 (ulong)fit);
2720 for (ndepth = 0, count = 0,
2721 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2722 (noffset >= 0) && (ndepth > 0);
2723 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2724 if (ndepth == 1) {
2725 /*
2726 * Direct child node of the images parent node,
2727 * i.e. component image node.
2728 */
2729 printf (" Hash(es) for Image %u (%s): ", count++,
2730 fit_get_name (fit, noffset, NULL));
2731
2732 if (!fit_image_check_hashes (fit, noffset))
2733 return 0;
2734 printf ("\n");
2735 }
2736 }
2737 return 1;
2738}
2739
2740/**
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002741 * fit_image_check_os - check whether image node is of a given os type
2742 * @fit: pointer to the FIT format image header
2743 * @noffset: component image node offset
2744 * @os: requested image os
2745 *
2746 * fit_image_check_os() reads image os property and compares its numeric
2747 * id with the requested os. Comparison result is returned to the caller.
2748 *
2749 * returns:
2750 * 1 if image is of given os type
2751 * 0 otherwise (or on error)
2752 */
2753int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2754{
2755 uint8_t image_os;
2756
2757 if (fit_image_get_os (fit, noffset, &image_os))
2758 return 0;
2759 return (os == image_os);
2760}
2761
2762/**
2763 * fit_image_check_arch - check whether image node is of a given arch
2764 * @fit: pointer to the FIT format image header
2765 * @noffset: component image node offset
2766 * @arch: requested imagearch
2767 *
2768 * fit_image_check_arch() reads image arch property and compares its numeric
2769 * id with the requested arch. Comparison result is returned to the caller.
2770 *
2771 * returns:
2772 * 1 if image is of given arch
2773 * 0 otherwise (or on error)
2774 */
2775int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2776{
2777 uint8_t image_arch;
2778
2779 if (fit_image_get_arch (fit, noffset, &image_arch))
2780 return 0;
2781 return (arch == image_arch);
2782}
2783
2784/**
2785 * fit_image_check_type - check whether image node is of a given type
2786 * @fit: pointer to the FIT format image header
2787 * @noffset: component image node offset
2788 * @type: requested image type
2789 *
2790 * fit_image_check_type() reads image type property and compares its numeric
2791 * id with the requested type. Comparison result is returned to the caller.
2792 *
2793 * returns:
2794 * 1 if image is of given type
2795 * 0 otherwise (or on error)
2796 */
2797int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2798{
2799 uint8_t image_type;
2800
2801 if (fit_image_get_type (fit, noffset, &image_type))
2802 return 0;
2803 return (type == image_type);
2804}
2805
2806/**
2807 * fit_image_check_comp - check whether image node uses given compression
2808 * @fit: pointer to the FIT format image header
2809 * @noffset: component image node offset
2810 * @comp: requested image compression type
2811 *
2812 * fit_image_check_comp() reads image compression property and compares its
2813 * numeric id with the requested compression type. Comparison result is
2814 * returned to the caller.
2815 *
2816 * returns:
2817 * 1 if image uses requested compression
2818 * 0 otherwise (or on error)
2819 */
2820int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2821{
2822 uint8_t image_comp;
2823
2824 if (fit_image_get_comp (fit, noffset, &image_comp))
2825 return 0;
2826 return (comp == image_comp);
2827}
2828
2829/**
2830 * fit_check_format - sanity check FIT image format
2831 * @fit: pointer to the FIT format image header
2832 *
2833 * fit_check_format() runs a basic sanity FIT image verification.
2834 * Routine checks for mandatory properties, nodes, etc.
2835 *
2836 * returns:
2837 * 1, on success
2838 * 0, on failure
2839 */
2840int fit_check_format (const void *fit)
2841{
2842 /* mandatory / node 'description' property */
2843 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2844 debug ("Wrong FIT format: no description\n");
2845 return 0;
2846 }
2847
2848#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2849 /* mandatory / node 'timestamp' property */
2850 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002851 debug ("Wrong FIT format: no timestamp\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002852 return 0;
2853 }
2854#endif
2855
2856 /* mandatory subimages parent '/images' node */
2857 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2858 debug ("Wrong FIT format: no images parent node\n");
2859 return 0;
2860 }
2861
2862 return 1;
2863}
2864
2865/**
2866 * fit_conf_get_node - get node offset for configuration of a given unit name
2867 * @fit: pointer to the FIT format image header
2868 * @conf_uname: configuration node unit name
2869 *
2870 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2871 * parant node) of a provided unit name. If configuration is found its node offset
2872 * is returned to the caller.
2873 *
2874 * When NULL is provided in second argument fit_conf_get_node() will search
2875 * for a default configuration node instead. Default configuration node unit name
2876 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2877 *
2878 * returns:
2879 * configuration node offset when found (>=0)
2880 * negative number on failure (FDT_ERR_* code)
2881 */
2882int fit_conf_get_node (const void *fit, const char *conf_uname)
2883{
2884 int noffset, confs_noffset;
2885 int len;
2886
2887 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2888 if (confs_noffset < 0) {
2889 debug ("Can't find configurations parent node '%s' (%s)\n",
2890 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2891 return confs_noffset;
2892 }
2893
2894 if (conf_uname == NULL) {
2895 /* get configuration unit name from the default property */
2896 debug ("No configuration specified, trying default...\n");
2897 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2898 if (conf_uname == NULL) {
2899 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2900 return len;
2901 }
2902 debug ("Found default configuration: '%s'\n", conf_uname);
2903 }
2904
2905 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2906 if (noffset < 0) {
2907 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2908 conf_uname, fdt_strerror (noffset));
2909 }
2910
2911 return noffset;
2912}
2913
2914static int __fit_conf_get_prop_node (const void *fit, int noffset,
2915 const char *prop_name)
2916{
2917 char *uname;
2918 int len;
2919
2920 /* get kernel image unit name from configuration kernel property */
2921 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2922 if (uname == NULL)
2923 return len;
2924
2925 return fit_image_get_node (fit, uname);
2926}
2927
2928/**
2929 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2930 * a given configuration
2931 * @fit: pointer to the FIT format image header
2932 * @noffset: configuration node offset
2933 *
2934 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2935 * configuration FIT_KERNEL_PROP property and translates it to the node
2936 * offset.
2937 *
2938 * returns:
2939 * image node offset when found (>=0)
2940 * negative number on failure (FDT_ERR_* code)
2941 */
2942int fit_conf_get_kernel_node (const void *fit, int noffset)
2943{
2944 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2945}
2946
2947/**
2948 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2949 * a given configuration
2950 * @fit: pointer to the FIT format image header
2951 * @noffset: configuration node offset
2952 *
2953 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2954 * configuration FIT_KERNEL_PROP property and translates it to the node
2955 * offset.
2956 *
2957 * returns:
2958 * image node offset when found (>=0)
2959 * negative number on failure (FDT_ERR_* code)
2960 */
2961int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2962{
2963 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2964}
2965
2966/**
2967 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2968 * a given configuration
2969 * @fit: pointer to the FIT format image header
2970 * @noffset: configuration node offset
2971 *
2972 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2973 * configuration FIT_KERNEL_PROP property and translates it to the node
2974 * offset.
2975 *
2976 * returns:
2977 * image node offset when found (>=0)
2978 * negative number on failure (FDT_ERR_* code)
2979 */
2980int fit_conf_get_fdt_node (const void *fit, int noffset)
2981{
2982 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2983}
2984
2985/**
2986 * fit_conf_print - prints out the FIT configuration details
2987 * @fit: pointer to the FIT format image header
Marian Balakowiczf773bea2008-03-12 10:35:46 +01002988 * @noffset: offset of the configuration node
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002989 * @p: pointer to prefix string
2990 *
2991 * fit_conf_print() lists all mandatory properies for the processed
2992 * configuration node.
2993 *
2994 * returns:
2995 * no returned results
2996 */
2997void fit_conf_print (const void *fit, int noffset, const char *p)
2998{
2999 char *desc;
3000 char *uname;
3001 int ret;
3002
3003 /* Mandatory properties */
3004 ret = fit_get_desc (fit, noffset, &desc);
3005 printf ("%s Description: ", p);
3006 if (ret)
3007 printf ("unavailable\n");
3008 else
3009 printf ("%s\n", desc);
3010
3011 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3012 printf ("%s Kernel: ", p);
3013 if (uname == NULL)
3014 printf ("unavailable\n");
3015 else
3016 printf ("%s\n", uname);
3017
3018 /* Optional properties */
3019 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3020 if (uname)
3021 printf ("%s Init Ramdisk: %s\n", p, uname);
3022
3023 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3024 if (uname)
3025 printf ("%s FDT: %s\n", p, uname);
3026}
Marian Balakowiczc8779642008-03-12 10:12:37 +01003027
3028/**
3029 * fit_check_ramdisk - verify FIT format ramdisk subimage
3030 * @fit_hdr: pointer to the FIT ramdisk header
3031 * @rd_noffset: ramdisk subimage node offset within FIT image
3032 * @arch: requested ramdisk image architecture type
3033 * @verify: data CRC verification flag
3034 *
3035 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3036 * specified FIT image.
3037 *
3038 * returns:
3039 * 1, on success
3040 * 0, on failure
3041 */
3042#ifndef USE_HOSTCC
3043static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3044{
3045 fit_image_print (fit, rd_noffset, " ");
3046
3047 if (verify) {
3048 puts (" Verifying Hash Integrity ... ");
3049 if (!fit_image_check_hashes (fit, rd_noffset)) {
3050 puts ("Bad Data Hash\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003051 show_boot_progress (-125);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003052 return 0;
3053 }
3054 puts ("OK\n");
3055 }
3056
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003057 show_boot_progress (126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003058 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3059 !fit_image_check_arch (fit, rd_noffset, arch) ||
3060 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3061 printf ("No Linux %s Ramdisk Image\n",
3062 genimg_get_arch_name(arch));
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003063 show_boot_progress (-126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003064 return 0;
3065 }
3066
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003067 show_boot_progress (127);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003068 return 1;
3069}
3070#endif /* USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003071#endif /* CONFIG_FIT */