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