blob: 77ca6e4705777aa77dca3040c77cede2d1b9b746 [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
Stephen Warren712fbcf2011-10-18 11:11:49 +000048#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
Stephen Warren712fbcf2011-10-18 11:11:49 +000058static int fit_check_ramdisk(const void *fit, int os_noffset,
Marian Balakowiczc8779642008-03-12 10:12:37 +010059 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
Stephen Warren712fbcf2011-10-18 11:11:49 +000068static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +010069 int verify);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010070#else
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010071#include "mkimage.h"
Andy Fleming20a14a42008-04-02 16:19:07 -050072#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010073#include <time.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010074#include <image.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010075#endif /* !USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010076
Mike Frysinger7edb1862010-10-20 07:17:39 -040077static const table_entry_t uimage_arch[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +010078 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
79 { IH_ARCH_ALPHA, "alpha", "Alpha", },
80 { IH_ARCH_ARM, "arm", "ARM", },
81 { IH_ARCH_I386, "x86", "Intel x86", },
82 { IH_ARCH_IA64, "ia64", "IA64", },
83 { IH_ARCH_M68K, "m68k", "M68K", },
84 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
85 { IH_ARCH_MIPS, "mips", "MIPS", },
86 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010087 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -070088 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010089 { IH_ARCH_PPC, "ppc", "PowerPC", },
90 { IH_ARCH_S390, "s390", "IBM S390", },
91 { IH_ARCH_SH, "sh", "SuperH", },
92 { IH_ARCH_SPARC, "sparc", "SPARC", },
93 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
94 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
95 { IH_ARCH_AVR32, "avr32", "AVR32", },
Macpaul Lin64d61462011-10-19 20:41:09 +000096 { IH_ARCH_NDS32, "nds32", "NDS32", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010097 { -1, "", "", },
98};
99
Mike Frysinger7edb1862010-10-20 07:17:39 -0400100static const table_entry_t uimage_os[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100101 { IH_OS_INVALID, NULL, "Invalid OS", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100102 { IH_OS_LINUX, "linux", "Linux", },
103#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
104 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
105#endif
106 { IH_OS_NETBSD, "netbsd", "NetBSD", },
Torkel Lundgren3df61952010-09-28 11:05:36 +0200107 { IH_OS_OSE, "ose", "Enea OSE", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100108 { IH_OS_RTEMS, "rtems", "RTEMS", },
109 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
110#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
111 { IH_OS_QNX, "qnx", "QNX", },
112 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
113#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500114#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
115 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
116#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100117#ifdef USE_HOSTCC
118 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
119 { IH_OS_DELL, "dell", "Dell", },
120 { IH_OS_ESIX, "esix", "Esix", },
121 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
122 { IH_OS_IRIX, "irix", "Irix", },
123 { IH_OS_NCR, "ncr", "NCR", },
124 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
125 { IH_OS_PSOS, "psos", "pSOS", },
126 { IH_OS_SCO, "sco", "SCO", },
127 { IH_OS_SOLARIS, "solaris", "Solaris", },
128 { IH_OS_SVR4, "svr4", "SVR4", },
129#endif
130 { -1, "", "", },
131};
132
Mike Frysinger7edb1862010-10-20 07:17:39 -0400133static const table_entry_t uimage_type[] = {
Stefano Babic4962e382011-10-17 00:07:43 +0000134 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100135 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
136 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby3decb142011-07-21 09:10:30 -0400137 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100138 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stephen Warrenb9b50e82011-11-10 13:17:53 -0700139 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
Stefano Babic4962e382011-10-17 00:07:43 +0000140 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
141 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
142 { IH_TYPE_INVALID, NULL, "Invalid Image", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100143 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic4962e382011-10-17 00:07:43 +0000144 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100145 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
146 { IH_TYPE_SCRIPT, "script", "Script", },
147 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000148 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100149 { -1, "", "", },
150};
151
Mike Frysinger7edb1862010-10-20 07:17:39 -0400152static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100153 { IH_COMP_NONE, "none", "uncompressed", },
154 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
155 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200156 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100157 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100158 { -1, "", "", },
159};
160
Stephen Warren712fbcf2011-10-18 11:11:49 +0000161uint32_t crc32(uint32_t, const unsigned char *, uint);
162uint32_t crc32_wd(uint32_t, const unsigned char *, uint, uint);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100163#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000164static void genimg_print_time(time_t timestamp);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100165#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100166
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100167/*****************************************************************************/
168/* Legacy format routines */
169/*****************************************************************************/
Stephen Warren712fbcf2011-10-18 11:11:49 +0000170int image_check_hcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100171{
172 ulong hcrc;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000173 ulong len = image_get_header_size();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100174 image_header_t header;
175
176 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000177 memmove(&header, (char *)hdr, image_get_header_size());
178 image_set_hcrc(&header, 0);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100179
Stephen Warren712fbcf2011-10-18 11:11:49 +0000180 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100181
Stephen Warren712fbcf2011-10-18 11:11:49 +0000182 return (hcrc == image_get_hcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100183}
184
Stephen Warren712fbcf2011-10-18 11:11:49 +0000185int image_check_dcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100186{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000187 ulong data = image_get_data(hdr);
188 ulong len = image_get_data_size(hdr);
189 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100190
Stephen Warren712fbcf2011-10-18 11:11:49 +0000191 return (dcrc == image_get_dcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100192}
193
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100194/**
195 * image_multi_count - get component (sub-image) count
196 * @hdr: pointer to the header of the multi component image
197 *
198 * image_multi_count() returns number of components in a multi
199 * component image.
200 *
201 * Note: no checking of the image type is done, caller must pass
202 * a valid multi component image.
203 *
204 * returns:
205 * number of components
206 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000207ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100208{
209 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100210 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100211
212 /* get start of the image payload, which in case of multi
213 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000214 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100215
216 /* count non empty slots */
217 for (i = 0; size[i]; ++i)
218 count++;
219
220 return count;
221}
222
223/**
224 * image_multi_getimg - get component data address and size
225 * @hdr: pointer to the header of the multi component image
226 * @idx: index of the requested component
227 * @data: pointer to a ulong variable, will hold component data address
228 * @len: pointer to a ulong variable, will hold component size
229 *
230 * image_multi_getimg() returns size and data address for the requested
231 * component in a multi component image.
232 *
233 * Note: no checking of the image type is done, caller must pass
234 * a valid multi component image.
235 *
236 * returns:
237 * data address and size of the component, if idx is valid
238 * 0 in data and len, if idx is out of range
239 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000240void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100241 ulong *data, ulong *len)
242{
243 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100244 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700245 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100246
247 /* get number of component */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000248 count = image_multi_count(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100249
250 /* get start of the image payload, which in case of multi
251 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000252 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100253
254 /* get address of the proper component data start, which means
255 * skipping sizes table (add 1 for last, null entry) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000256 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100257
258 if (idx < count) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000259 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100260 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100261
262 /* go over all indices preceding requested component idx */
263 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700264 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000265 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100266 }
267
268 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700269 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100270 } else {
271 *len = 0;
272 *data = 0;
273 }
274}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100275
Stephen Warren712fbcf2011-10-18 11:11:49 +0000276static void image_print_type(const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100277{
278 const char *os, *arch, *type, *comp;
279
Stephen Warren712fbcf2011-10-18 11:11:49 +0000280 os = genimg_get_os_name(image_get_os(hdr));
281 arch = genimg_get_arch_name(image_get_arch(hdr));
282 type = genimg_get_type_name(image_get_type(hdr));
283 comp = genimg_get_comp_name(image_get_comp(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100284
Stephen Warren712fbcf2011-10-18 11:11:49 +0000285 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100286}
287
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100288/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200289 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200290 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100291 * @p: pointer to prefix string
292 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200293 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100294 * The routine prints out all header fields followed by the size/offset data
295 * for MULTI/SCRIPT images.
296 *
297 * returns:
298 * no returned results
299 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000300void image_print_contents(const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100301{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200302 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200303 const char *p;
304
305#ifdef USE_HOSTCC
306 p = "";
307#else
308 p = " ";
309#endif
310
Stephen Warren712fbcf2011-10-18 11:11:49 +0000311 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100312#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000313 printf("%sCreated: ", p);
314 genimg_print_time((time_t)image_get_time(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100315#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +0000316 printf("%sImage Type: ", p);
317 image_print_type(hdr);
318 printf("%sData Size: ", p);
319 genimg_print_size(image_get_data_size(hdr));
320 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
321 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100322
Stephen Warren712fbcf2011-10-18 11:11:49 +0000323 if (image_check_type(hdr, IH_TYPE_MULTI) ||
324 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100325 int i;
326 ulong data, len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000327 ulong count = image_multi_count(hdr);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100328
Stephen Warren712fbcf2011-10-18 11:11:49 +0000329 printf("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100330 for (i = 0; i < count; i++) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000331 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100332
Stephen Warren712fbcf2011-10-18 11:11:49 +0000333 printf("%s Image %d: ", p, i);
334 genimg_print_size(len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100335
Stephen Warren712fbcf2011-10-18 11:11:49 +0000336 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100337 /*
338 * the user may need to know offsets
339 * if planning to do something with
340 * multiple files
341 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000342 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100343 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100344 }
345 }
346}
347
Marian Balakowicz570abb02008-02-29 15:59:59 +0100348
349#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100350/**
351 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100352 * @rd_addr: ramdisk image start address
353 * @arch: expected ramdisk architecture
354 * @verify: checksum verification flag
355 *
356 * image_get_ramdisk() returns a pointer to the verified ramdisk image
357 * header. Routine receives image start address and expected architecture
358 * flag. Verification done covers data and header integrity and os/type/arch
359 * fields checking.
360 *
361 * If dataflash support is enabled routine checks for dataflash addresses
362 * and handles required dataflash reads.
363 *
364 * returns:
365 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600366 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100367 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000368static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100369 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100370{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200371 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100372
Stephen Warren712fbcf2011-10-18 11:11:49 +0000373 if (!image_check_magic(rd_hdr)) {
374 puts("Bad Magic Number\n");
375 show_boot_progress(-10);
Kumar Gala274cea22008-02-27 21:51:46 -0600376 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100377 }
378
Stephen Warren712fbcf2011-10-18 11:11:49 +0000379 if (!image_check_hcrc(rd_hdr)) {
380 puts("Bad Header Checksum\n");
381 show_boot_progress(-11);
Kumar Gala274cea22008-02-27 21:51:46 -0600382 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100383 }
384
Stephen Warren712fbcf2011-10-18 11:11:49 +0000385 show_boot_progress(10);
386 image_print_contents(rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100387
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100388 if (verify) {
389 puts(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000390 if (!image_check_dcrc(rd_hdr)) {
391 puts("Bad Data CRC\n");
392 show_boot_progress(-12);
Kumar Gala274cea22008-02-27 21:51:46 -0600393 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100394 }
395 puts("OK\n");
396 }
397
Stephen Warren712fbcf2011-10-18 11:11:49 +0000398 show_boot_progress(11);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100399
Stephen Warren712fbcf2011-10-18 11:11:49 +0000400 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
401 !image_check_arch(rd_hdr, arch) ||
402 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
403 printf("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100404 genimg_get_arch_name(arch));
Stephen Warren712fbcf2011-10-18 11:11:49 +0000405 show_boot_progress(-13);
Kumar Gala274cea22008-02-27 21:51:46 -0600406 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100407 }
408
409 return rd_hdr;
410}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100411#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100412
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100413/*****************************************************************************/
414/* Shared dual-format routines */
415/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100416#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +0000417int getenv_yesno(char *var)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100418{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000419 char *s = getenv(var);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100420 return (s && (*s == 'n')) ? 0 : 1;
421}
422
423ulong getenv_bootm_low(void)
424{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000425 char *s = getenv("bootm_low");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100426 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000427 ulong tmp = simple_strtoul(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100428 return tmp;
429 }
430
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200431#if defined(CONFIG_SYS_SDRAM_BASE)
432 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100433#elif defined(CONFIG_ARM)
434 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100435#else
436 return 0;
437#endif
438}
439
Becky Bruce391fd932008-06-09 20:37:18 -0500440phys_size_t getenv_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100441{
Matthew McClintockc519fac2010-07-08 10:11:08 -0500442 phys_size_t tmp;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000443 char *s = getenv("bootm_size");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100444 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000445 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100446 return tmp;
447 }
Matthew McClintockc519fac2010-07-08 10:11:08 -0500448 s = getenv("bootm_low");
449 if (s)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000450 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintockc519fac2010-07-08 10:11:08 -0500451 else
452 tmp = 0;
453
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100454
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100455#if defined(CONFIG_ARM)
Matthew McClintockc519fac2010-07-08 10:11:08 -0500456 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100457#else
Matthew McClintockc519fac2010-07-08 10:11:08 -0500458 return gd->bd->bi_memsize - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100459#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100460}
461
Grant Likelyc3624e62011-03-28 09:58:43 +0000462phys_size_t getenv_bootm_mapsize(void)
463{
464 phys_size_t tmp;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000465 char *s = getenv("bootm_mapsize");
Grant Likelyc3624e62011-03-28 09:58:43 +0000466 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000467 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likelyc3624e62011-03-28 09:58:43 +0000468 return tmp;
469 }
470
471#if defined(CONFIG_SYS_BOOTMAPSZ)
472 return CONFIG_SYS_BOOTMAPSZ;
473#else
474 return getenv_bootm_size();
475#endif
476}
477
Stephen Warren712fbcf2011-10-18 11:11:49 +0000478void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100479{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400480 if (to == from)
481 return;
482
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100483#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
484 while (len > 0) {
485 size_t tail = (len > chunksz) ? chunksz : len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000486 WATCHDOG_RESET();
487 memmove(to, from, tail);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100488 to += tail;
489 from += tail;
490 len -= tail;
491 }
492#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000493 memmove(to, from, len);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100494#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
495}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100496#endif /* !USE_HOSTCC */
497
Stephen Warren712fbcf2011-10-18 11:11:49 +0000498void genimg_print_size(uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100499{
500#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +0000501 printf("%d Bytes = ", size);
502 print_size(size, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100503#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000504 printf("%d Bytes = %.2f kB = %.2f MB\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100505 size, (double)size / 1.024e3,
506 (double)size / 1.048576e6);
507#endif
508}
509
510#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000511static void genimg_print_time(time_t timestamp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100512{
513#ifndef USE_HOSTCC
514 struct rtc_time tm;
515
Stephen Warren712fbcf2011-10-18 11:11:49 +0000516 to_tm(timestamp, &tm);
517 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100518 tm.tm_year, tm.tm_mon, tm.tm_mday,
519 tm.tm_hour, tm.tm_min, tm.tm_sec);
520#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000521 printf("%s", ctime(&timestamp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100522#endif
523}
524#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
525
526/**
527 * get_table_entry_name - translate entry id to long name
528 * @table: pointer to a translation table for entries of a specific type
529 * @msg: message to be returned when translation fails
530 * @id: entry id to be translated
531 *
532 * get_table_entry_name() will go over translation table trying to find
533 * entry that matches given id. If matching entry is found, its long
534 * name is returned to the caller.
535 *
536 * returns:
537 * long entry name if translation succeeds
538 * msg otherwise
539 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400540char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100541{
542 for (; table->id >= 0; ++table) {
543 if (table->id == id)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200544#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Woode3d1ac72009-04-02 16:15:10 -0500545 return table->lname;
546#else
547 return table->lname + gd->reloc_off;
548#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100549 }
550 return (msg);
551}
552
Stephen Warren712fbcf2011-10-18 11:11:49 +0000553const char *genimg_get_os_name(uint8_t os)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100554{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000555 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100556}
557
Stephen Warren712fbcf2011-10-18 11:11:49 +0000558const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100559{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000560 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
561 arch));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100562}
563
Stephen Warren712fbcf2011-10-18 11:11:49 +0000564const char *genimg_get_type_name(uint8_t type)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100565{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000566 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100567}
568
Stephen Warren712fbcf2011-10-18 11:11:49 +0000569const char *genimg_get_comp_name(uint8_t comp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100570{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000571 return (get_table_entry_name(uimage_comp, "Unknown Compression",
572 comp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100573}
574
575/**
576 * get_table_entry_id - translate short entry name to id
577 * @table: pointer to a translation table for entries of a specific type
578 * @table_name: to be used in case of error
579 * @name: entry short name to be translated
580 *
581 * get_table_entry_id() will go over translation table trying to find
582 * entry that matches given short name. If matching entry is found,
583 * its id returned to the caller.
584 *
585 * returns:
586 * entry id if translation succeeds
587 * -1 otherwise
588 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400589int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100590 const char *table_name, const char *name)
591{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400592 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100593#ifdef USE_HOSTCC
594 int first = 1;
595
596 for (t = table; t->id >= 0; ++t) {
597 if (t->sname && strcasecmp(t->sname, name) == 0)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000598 return(t->id);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100599 }
600
Stephen Warren712fbcf2011-10-18 11:11:49 +0000601 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100602 for (t = table; t->id >= 0; ++t) {
603 if (t->sname == NULL)
604 continue;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000605 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100606 first = 0;
607 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000608 fprintf(stderr, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100609#else
610 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200611#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Woode3d1ac72009-04-02 16:15:10 -0500612 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200613#else
614 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500615#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100616 return (t->id);
617 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000618 debug("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100619#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100620 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100621}
622
Stephen Warren712fbcf2011-10-18 11:11:49 +0000623int genimg_get_os_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100624{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000625 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100626}
627
Stephen Warren712fbcf2011-10-18 11:11:49 +0000628int genimg_get_arch_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100629{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000630 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100631}
632
Stephen Warren712fbcf2011-10-18 11:11:49 +0000633int genimg_get_type_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100634{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000635 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100636}
637
Stephen Warren712fbcf2011-10-18 11:11:49 +0000638int genimg_get_comp_id(const char *name)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100639{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000640 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100641}
642
643#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100644/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100645 * genimg_get_format - get image format type
646 * @img_addr: image start address
647 *
648 * genimg_get_format() checks whether provided address points to a valid
649 * legacy or FIT image.
650 *
651 * New uImage format and FDT blob are based on a libfdt. FDT blob
652 * may be passed directly or embedded in a FIT image. In both situations
653 * genimg_get_format() must be able to dectect libfdt header.
654 *
655 * returns:
656 * image format type or IMAGE_FORMAT_INVALID if no image is present
657 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000658int genimg_get_format(void *img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100659{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200660 ulong format = IMAGE_FORMAT_INVALID;
661 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100662#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200663 char *fit_hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100664#endif
665
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200666 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100667 if (image_check_magic(hdr))
668 format = IMAGE_FORMAT_LEGACY;
669#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
670 else {
671 fit_hdr = (char *)img_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000672 if (fdt_check_header(fit_hdr) == 0)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100673 format = IMAGE_FORMAT_FIT;
674 }
675#endif
676
677 return format;
678}
679
680/**
681 * genimg_get_image - get image from special storage (if necessary)
682 * @img_addr: image start address
683 *
684 * genimg_get_image() checks if provided image start adddress is located
685 * in a dataflash storage. If so, image is moved to a system RAM memory.
686 *
687 * returns:
688 * image start address after possible relocation from special storage
689 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000690ulong genimg_get_image(ulong img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100691{
692 ulong ram_addr = img_addr;
693
694#ifdef CONFIG_HAS_DATAFLASH
695 ulong h_size, d_size;
696
Stephen Warren712fbcf2011-10-18 11:11:49 +0000697 if (addr_dataflash(img_addr)) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100698 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200699 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100700
701 /* get header size */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000702 h_size = image_get_header_size();
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100703#if defined(CONFIG_FIT)
704 if (sizeof(struct fdt_header) > h_size)
705 h_size = sizeof(struct fdt_header);
706#endif
707
708 /* read in header */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000709 debug(" Reading image header from dataflash address "
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100710 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
711
Stephen Warren712fbcf2011-10-18 11:11:49 +0000712 read_dataflash(img_addr, h_size, (char *)ram_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100713
714 /* get data size */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000715 switch (genimg_get_format((void *)ram_addr)) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100716 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000717 d_size = image_get_data_size(
718 (const image_header_t *)ram_addr);
719 debug(" Legacy format image found at 0x%08lx, "
720 "size 0x%08lx\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100721 ram_addr, d_size);
722 break;
723#if defined(CONFIG_FIT)
724 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000725 d_size = fit_get_size((const void *)ram_addr) - h_size;
726 debug(" FIT/FDT format image found at 0x%08lx, "
727 "size 0x%08lx\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100728 ram_addr, d_size);
729 break;
730#endif
731 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000732 printf(" No valid image found at 0x%08lx\n",
733 img_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100734 return ram_addr;
735 }
736
737 /* read in image data */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000738 debug(" Reading image remaining data from dataflash address "
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100739 "%08lx to RAM address %08lx\n", img_addr + h_size,
740 ram_addr + h_size);
741
Stephen Warren712fbcf2011-10-18 11:11:49 +0000742 read_dataflash(img_addr + h_size, d_size,
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100743 (char *)(ram_addr + h_size));
744
745 }
746#endif /* CONFIG_HAS_DATAFLASH */
747
748 return ram_addr;
749}
750
751/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100752 * fit_has_config - check if there is a valid FIT configuration
753 * @images: pointer to the bootm command headers structure
754 *
755 * fit_has_config() checks if there is a FIT configuration in use
756 * (if FTI support is present).
757 *
758 * returns:
759 * 0, no FIT support or no configuration found
760 * 1, configuration found
761 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000762int genimg_has_config(bootm_headers_t *images)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100763{
764#if defined(CONFIG_FIT)
765 if (images->fit_uname_cfg)
766 return 1;
767#endif
768 return 0;
769}
770
771/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100772 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100773 * @argc: command argument count
774 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100775 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100776 * @arch: expected ramdisk architecture
777 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
778 * @rd_end: pointer to a ulong variable, will hold ramdisk end
779 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100780 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100781 * Curently supported are the following ramdisk sources:
782 * - multicomponent kernel/ramdisk image,
783 * - commandline provided address of decicated ramdisk image.
784 *
785 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100786 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100787 * rd_start and rd_end are set to ramdisk start/end addresses if
788 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100789 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500790 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100791 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100792 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000793int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100794 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100795{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100796 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100797 ulong rd_data, rd_len;
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200798 const image_header_t *rd_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100799#if defined(CONFIG_FIT)
800 void *fit_hdr;
801 const char *fit_uname_config = NULL;
802 const char *fit_uname_ramdisk = NULL;
803 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100804 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100805 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100806 const void *data;
807 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100808#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100809
Marian Balakowiczc8779642008-03-12 10:12:37 +0100810 *rd_start = 0;
811 *rd_end = 0;
812
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100813 /*
814 * Look for a '-' which indicates to ignore the
815 * ramdisk argument
816 */
817 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000818 debug("## Skipping init Ramdisk\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100819 rd_len = rd_data = 0;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000820 } else if (argc >= 3 || genimg_has_config(images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100821#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100822 if (argc >= 3) {
823 /*
824 * If the init ramdisk comes from the FIT image and
825 * the FIT image address is omitted in the command
826 * line argument, try to use os FIT image address or
827 * default load address.
828 */
829 if (images->fit_uname_os)
830 default_addr = (ulong)images->fit_hdr_os;
831 else
832 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100833
Stephen Warren712fbcf2011-10-18 11:11:49 +0000834 if (fit_parse_conf(argv[2], default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100835 &rd_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000836 debug("* ramdisk: config '%s' from image at "
837 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100838 fit_uname_config, rd_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000839 } else if (fit_parse_subimage(argv[2], default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100840 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000841 debug("* ramdisk: subimage '%s' from image at "
842 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100843 fit_uname_ramdisk, rd_addr);
844 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100845#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100846 {
847 rd_addr = simple_strtoul(argv[2], NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000848 debug("* ramdisk: cmdline image address = "
849 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100850 rd_addr);
851 }
852#if defined(CONFIG_FIT)
853 } else {
854 /* use FIT configuration provided in first bootm
855 * command argument
856 */
857 rd_addr = (ulong)images->fit_hdr_os;
858 fit_uname_config = images->fit_uname_cfg;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000859 debug("* ramdisk: using config '%s' from image "
860 "at 0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100861 fit_uname_config, rd_addr);
862
863 /*
864 * Check whether configuration has ramdisk defined,
865 * if not, don't try to use it, quit silently.
866 */
867 fit_hdr = (void *)rd_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000868 cfg_noffset = fit_conf_get_node(fit_hdr,
869 fit_uname_config);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100870 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000871 debug("* ramdisk: no such config\n");
Michal Simekc78fce62008-07-11 10:43:13 +0200872 return 1;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100873 }
874
Stephen Warren712fbcf2011-10-18 11:11:49 +0000875 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
876 cfg_noffset);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100877 if (rd_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000878 debug("* ramdisk: no ramdisk in config\n");
Peter Tyser41266c92008-08-05 10:51:57 -0500879 return 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100880 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100881 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100882#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100883
884 /* copy from dataflash if needed */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000885 rd_addr = genimg_get_image(rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100886
887 /*
888 * Check if there is an initrd image at the
889 * address provided in the second bootm argument
890 * check image type, for FIT images get FIT node.
891 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000892 switch (genimg_get_format((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100893 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000894 printf("## Loading init Ramdisk from Legacy "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100895 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100896
Stephen Warren712fbcf2011-10-18 11:11:49 +0000897 show_boot_progress(9);
898 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100899 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100900
Marian Balakowiczc8779642008-03-12 10:12:37 +0100901 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600902 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600903
Stephen Warren712fbcf2011-10-18 11:11:49 +0000904 rd_data = image_get_data(rd_hdr);
905 rd_len = image_get_data_size(rd_hdr);
906 rd_load = image_get_load(rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100907 break;
908#if defined(CONFIG_FIT)
909 case IMAGE_FORMAT_FIT:
910 fit_hdr = (void *)rd_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000911 printf("## Loading init Ramdisk from FIT "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100912 "Image at %08lx ...\n", rd_addr);
913
Stephen Warren712fbcf2011-10-18 11:11:49 +0000914 show_boot_progress(120);
915 if (!fit_check_format(fit_hdr)) {
916 puts("Bad FIT ramdisk image format!\n");
917 show_boot_progress(-120);
Michal Simekc78fce62008-07-11 10:43:13 +0200918 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100919 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000920 show_boot_progress(121);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100921
922 if (!fit_uname_ramdisk) {
923 /*
924 * no ramdisk image node unit name, try to get config
925 * node first. If config unit node name is NULL
926 * fit_conf_get_node() will try to find default config node
927 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000928 show_boot_progress(122);
929 cfg_noffset = fit_conf_get_node(fit_hdr,
930 fit_uname_config);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100931 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000932 puts("Could not find configuration "
933 "node\n");
934 show_boot_progress(-122);
Michal Simekc78fce62008-07-11 10:43:13 +0200935 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100936 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000937 fit_uname_config = fdt_get_name(fit_hdr,
938 cfg_noffset, NULL);
939 printf(" Using '%s' configuration\n",
940 fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100941
Stephen Warren712fbcf2011-10-18 11:11:49 +0000942 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
943 cfg_noffset);
944 fit_uname_ramdisk = fit_get_name(fit_hdr,
945 rd_noffset, NULL);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100946 } else {
947 /* get ramdisk component image node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000948 show_boot_progress(123);
949 rd_noffset = fit_image_get_node(fit_hdr,
950 fit_uname_ramdisk);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100951 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100952 if (rd_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000953 puts("Could not find subimage node\n");
954 show_boot_progress(-124);
Michal Simekc78fce62008-07-11 10:43:13 +0200955 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100956 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100957
Stephen Warren712fbcf2011-10-18 11:11:49 +0000958 printf(" Trying '%s' ramdisk subimage\n",
959 fit_uname_ramdisk);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100960
Stephen Warren712fbcf2011-10-18 11:11:49 +0000961 show_boot_progress(125);
962 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
963 images->verify))
Michal Simekc78fce62008-07-11 10:43:13 +0200964 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100965
966 /* get ramdisk image data address and length */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000967 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
968 &size)) {
969 puts("Could not find ramdisk subimage data!\n");
970 show_boot_progress(-127);
Michal Simekc78fce62008-07-11 10:43:13 +0200971 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100972 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000973 show_boot_progress(128);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100974
975 rd_data = (ulong)data;
976 rd_len = size;
977
Stephen Warren712fbcf2011-10-18 11:11:49 +0000978 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
979 puts("Can't get ramdisk subimage load "
980 "address!\n");
981 show_boot_progress(-129);
Michal Simekc78fce62008-07-11 10:43:13 +0200982 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100983 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000984 show_boot_progress(129);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100985
986 images->fit_hdr_rd = fit_hdr;
987 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100988 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100989 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100990#endif
991 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000992 puts("Wrong Ramdisk Image Format\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +0100993 rd_data = rd_len = rd_load = 0;
Kumar Galaea86b9e2008-08-29 19:08:29 -0500994 return 1;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100995 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100996 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +0000997 image_check_type(&images->legacy_hdr_os_copy,
998 IH_TYPE_MULTI)) {
999
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001000 /*
1001 * Now check if we have a legacy mult-component image,
1002 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001003 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001004 show_boot_progress(13);
1005 printf("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001006 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001007 (ulong)images->legacy_hdr_os);
1008
Stephen Warren712fbcf2011-10-18 11:11:49 +00001009 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001010 } else {
1011 /*
1012 * no initrd image
1013 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001014 show_boot_progress(14);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001015 rd_len = rd_data = 0;
1016 }
1017
1018 if (!rd_data) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001019 debug("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001020 } else {
1021 *rd_start = rd_data;
1022 *rd_end = rd_data + rd_len;
1023 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001024 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001025 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -06001026
1027 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001028}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001029
John Rigbyfca43cc2010-10-13 13:57:35 -06001030#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001031/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001032 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001033 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001034 * @rd_data: ramdisk data start address
1035 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001036 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1037 * start address (after possible relocation)
1038 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1039 * end address (after possible relocation)
1040 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001041 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001042 * variable and if requested ramdisk data is moved to a specified location.
1043 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001044 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1045 * start/end addresses if ramdisk image start and len were provided,
1046 * otherwise set initrd_start and initrd_end set to zeros.
1047 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001048 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001049 * 0 - success
1050 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001051 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001052int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001053 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001054{
1055 char *s;
1056 ulong initrd_high;
1057 int initrd_copy_to_ram = 1;
1058
Stephen Warren712fbcf2011-10-18 11:11:49 +00001059 if ((s = getenv("initrd_high")) != NULL) {
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001060 /* a value of "no" or a similar string will act like 0,
1061 * turning the "load high" feature off. This is intentional.
1062 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001063 initrd_high = simple_strtoul(s, NULL, 16);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001064 if (initrd_high == ~0)
1065 initrd_copy_to_ram = 0;
1066 } else {
1067 /* not set, no restrictions to load high */
1068 initrd_high = ~0;
1069 }
1070
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001071
1072#ifdef CONFIG_LOGBUFFER
1073 /* Prevent initrd from overwriting logbuffer */
1074 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1075#endif
1076
Stephen Warren712fbcf2011-10-18 11:11:49 +00001077 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001078 initrd_high, initrd_copy_to_ram);
1079
1080 if (rd_data) {
1081 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001082 debug(" in-place initrd\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001083 *initrd_start = rd_data;
1084 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001085 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001086 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001087 if (initrd_high)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001088 *initrd_start = (ulong)lmb_alloc_base(lmb,
1089 rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001090 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001091 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1092 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001093
Kumar Galae822d7f2008-02-27 21:51:49 -06001094 if (*initrd_start == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001095 puts("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001096 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001097 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001098 show_boot_progress(12);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001099
1100 *initrd_end = *initrd_start + rd_len;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001101 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001102 *initrd_start, *initrd_end);
1103
Stephen Warren712fbcf2011-10-18 11:11:49 +00001104 memmove_wd((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001105 (void *)rd_data, rd_len, CHUNKSZ);
1106
Kumar Gala3b200112011-12-07 04:42:58 +00001107#ifdef CONFIG_MP
1108 /*
1109 * Ensure the image is flushed to memory to handle
1110 * AMP boot scenarios in which we might not be
1111 * HW cache coherent
1112 */
1113 flush_cache((unsigned long)*initrd_start, rd_len);
1114#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +00001115 puts("OK\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001116 }
1117 } else {
1118 *initrd_start = 0;
1119 *initrd_end = 0;
1120 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001121 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001122 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001123
Kumar Galae822d7f2008-02-27 21:51:49 -06001124 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001125
Kumar Galae822d7f2008-02-27 21:51:49 -06001126error:
1127 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001128}
John Rigbyfca43cc2010-10-13 13:57:35 -06001129#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001130
Kumar Gala06a09912008-08-15 08:24:38 -05001131#ifdef CONFIG_OF_LIBFDT
Stephen Warren712fbcf2011-10-18 11:11:49 +00001132static void fdt_error(const char *msg)
Kumar Gala06a09912008-08-15 08:24:38 -05001133{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001134 puts("ERROR: ");
1135 puts(msg);
1136 puts(" - must RESET the board to recover.\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001137}
1138
Stephen Warren712fbcf2011-10-18 11:11:49 +00001139static const image_header_t *image_get_fdt(ulong fdt_addr)
Kumar Gala06a09912008-08-15 08:24:38 -05001140{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001141 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001142
Stephen Warren712fbcf2011-10-18 11:11:49 +00001143 image_print_contents(fdt_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001144
Stephen Warren712fbcf2011-10-18 11:11:49 +00001145 puts(" Verifying Checksum ... ");
1146 if (!image_check_hcrc(fdt_hdr)) {
1147 fdt_error("fdt header checksum invalid");
Kumar Gala06a09912008-08-15 08:24:38 -05001148 return NULL;
1149 }
1150
Stephen Warren712fbcf2011-10-18 11:11:49 +00001151 if (!image_check_dcrc(fdt_hdr)) {
1152 fdt_error("fdt checksum invalid");
Kumar Gala06a09912008-08-15 08:24:38 -05001153 return NULL;
1154 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001155 puts("OK\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001156
Stephen Warren712fbcf2011-10-18 11:11:49 +00001157 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1158 fdt_error("uImage is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001159 return NULL;
1160 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001161 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1162 fdt_error("uImage is compressed");
Kumar Gala06a09912008-08-15 08:24:38 -05001163 return NULL;
1164 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001165 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1166 fdt_error("uImage data is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001167 return NULL;
1168 }
1169 return fdt_hdr;
1170}
1171
1172/**
1173 * fit_check_fdt - verify FIT format FDT subimage
1174 * @fit_hdr: pointer to the FIT header
1175 * fdt_noffset: FDT subimage node offset within FIT image
1176 * @verify: data CRC verification flag
1177 *
1178 * fit_check_fdt() verifies integrity of the FDT subimage and from
1179 * specified FIT image.
1180 *
1181 * returns:
1182 * 1, on success
1183 * 0, on failure
1184 */
1185#if defined(CONFIG_FIT)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001186static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
Kumar Gala06a09912008-08-15 08:24:38 -05001187{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001188 fit_image_print(fit, fdt_noffset, " ");
Kumar Gala06a09912008-08-15 08:24:38 -05001189
1190 if (verify) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001191 puts(" Verifying Hash Integrity ... ");
1192 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1193 fdt_error("Bad Data Hash");
Kumar Gala06a09912008-08-15 08:24:38 -05001194 return 0;
1195 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001196 puts("OK\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001197 }
1198
Stephen Warren712fbcf2011-10-18 11:11:49 +00001199 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1200 fdt_error("Not a FDT image");
Kumar Gala06a09912008-08-15 08:24:38 -05001201 return 0;
1202 }
1203
Stephen Warren712fbcf2011-10-18 11:11:49 +00001204 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1205 fdt_error("FDT image is compressed");
Kumar Gala06a09912008-08-15 08:24:38 -05001206 return 0;
1207 }
1208
1209 return 1;
1210}
1211#endif /* CONFIG_FIT */
1212
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001213#ifndef CONFIG_SYS_FDT_PAD
1214#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Gala06a09912008-08-15 08:24:38 -05001215#endif
1216
Grant Likely55b0a392011-03-28 09:59:01 +00001217#if defined(CONFIG_OF_LIBFDT)
1218/**
1219 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1220 * @lmb: pointer to lmb handle, will be used for memory mgmt
1221 * @fdt_blob: pointer to fdt blob base address
1222 *
1223 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1224 * memreserve regions prevents u-boot from using them to store the initrd
1225 * or the fdt blob.
1226 */
1227void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1228{
1229 uint64_t addr, size;
1230 int i, total;
1231
Stephen Warren712fbcf2011-10-18 11:11:49 +00001232 if (fdt_check_header(fdt_blob) != 0)
Grant Likely55b0a392011-03-28 09:59:01 +00001233 return;
1234
1235 total = fdt_num_mem_rsv(fdt_blob);
1236 for (i = 0; i < total; i++) {
1237 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1238 continue;
1239 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1240 (unsigned long long)addr, (unsigned long long)size);
1241 lmb_reserve(lmb, addr, size);
1242 }
1243}
1244
Kumar Gala06a09912008-08-15 08:24:38 -05001245/**
1246 * boot_relocate_fdt - relocate flat device tree
1247 * @lmb: pointer to lmb handle, will be used for memory mgmt
Kumar Gala06a09912008-08-15 08:24:38 -05001248 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1249 * @of_size: pointer to a ulong variable, will hold fdt length
1250 *
Timur Tabi43b08af2010-05-24 15:10:25 -05001251 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1252 * relocates the of_flat_tree into that region, even if the fdt is already in
1253 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1254 * bytes.
Kumar Gala06a09912008-08-15 08:24:38 -05001255 *
1256 * of_flat_tree and of_size are set to final (after relocation) values
1257 *
1258 * returns:
1259 * 0 - success
1260 * 1 - failure
1261 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001262int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001263{
Timur Tabi43b08af2010-05-24 15:10:25 -05001264 void *fdt_blob = *of_flat_tree;
1265 void *of_start = 0;
David A. Longa28afca2011-07-09 16:40:19 -04001266 char *fdt_high;
Kumar Gala06a09912008-08-15 08:24:38 -05001267 ulong of_len = 0;
Timur Tabi43b08af2010-05-24 15:10:25 -05001268 int err;
David A. Longa28afca2011-07-09 16:40:19 -04001269 int disable_relocation = 0;
Kumar Gala06a09912008-08-15 08:24:38 -05001270
1271 /* nothing to do */
1272 if (*of_size == 0)
1273 return 0;
1274
Stephen Warren712fbcf2011-10-18 11:11:49 +00001275 if (fdt_check_header(fdt_blob) != 0) {
1276 fdt_error("image is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001277 goto error;
1278 }
1279
Timur Tabi43b08af2010-05-24 15:10:25 -05001280 /* position on a 4K boundary before the alloc_current */
1281 /* Pad the FDT by a specified amount */
1282 of_len = *of_size + CONFIG_SYS_FDT_PAD;
David A. Longa28afca2011-07-09 16:40:19 -04001283
1284 /* If fdt_high is set use it to select the relocation address */
1285 fdt_high = getenv("fdt_high");
1286 if (fdt_high) {
1287 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1288
1289 if (((ulong) desired_addr) == ~0UL) {
1290 /* All ones means use fdt in place */
1291 desired_addr = fdt_blob;
1292 disable_relocation = 1;
1293 }
1294 if (desired_addr) {
1295 of_start =
1296 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1297 ((ulong)
1298 desired_addr)
1299 + of_len);
1300 if (desired_addr && of_start != desired_addr) {
1301 puts("Failed using fdt_high value for Device Tree");
1302 goto error;
1303 }
1304 } else {
1305 of_start =
Matthew McClintock1bb5e902011-07-18 13:08:05 +00001306 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
David A. Longa28afca2011-07-09 16:40:19 -04001307 }
1308 } else {
1309 of_start =
1310 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1311 getenv_bootm_mapsize()
1312 + getenv_bootm_low());
1313 }
Kumar Gala06a09912008-08-15 08:24:38 -05001314
Timur Tabi43b08af2010-05-24 15:10:25 -05001315 if (of_start == 0) {
1316 puts("device tree - allocation error\n");
1317 goto error;
Kumar Gala06a09912008-08-15 08:24:38 -05001318 }
1319
David A. Longa28afca2011-07-09 16:40:19 -04001320 if (disable_relocation) {
1321 /* We assume there is space after the existing fdt to use for padding */
1322 fdt_set_totalsize(of_start, of_len);
1323 printf(" Using Device Tree in place at %p, end %p\n",
1324 of_start, of_start + of_len - 1);
1325 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001326 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
David A. Longa28afca2011-07-09 16:40:19 -04001327 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
Timur Tabi43b08af2010-05-24 15:10:25 -05001328
Stephen Warren712fbcf2011-10-18 11:11:49 +00001329 printf(" Loading Device Tree to %p, end %p ... ",
David A. Longa28afca2011-07-09 16:40:19 -04001330 of_start, of_start + of_len - 1);
Timur Tabi43b08af2010-05-24 15:10:25 -05001331
Stephen Warren712fbcf2011-10-18 11:11:49 +00001332 err = fdt_open_into(fdt_blob, of_start, of_len);
David A. Longa28afca2011-07-09 16:40:19 -04001333 if (err != 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001334 fdt_error("fdt move failed");
David A. Longa28afca2011-07-09 16:40:19 -04001335 goto error;
1336 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001337 puts("OK\n");
Timur Tabi43b08af2010-05-24 15:10:25 -05001338 }
Timur Tabi43b08af2010-05-24 15:10:25 -05001339
1340 *of_flat_tree = of_start;
1341 *of_size = of_len;
1342
Kumar Gala54f9c862008-08-15 08:24:39 -05001343 set_working_fdt_addr(*of_flat_tree);
Kumar Gala06a09912008-08-15 08:24:38 -05001344 return 0;
1345
1346error:
1347 return 1;
1348}
Grant Likelyed59e582011-03-28 09:58:49 +00001349#endif /* CONFIG_OF_LIBFDT */
Kumar Gala06a09912008-08-15 08:24:38 -05001350
1351/**
1352 * boot_get_fdt - main fdt handling routine
1353 * @argc: command argument count
1354 * @argv: command argument list
1355 * @images: pointer to the bootm images structure
1356 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1357 * @of_size: pointer to a ulong variable, will hold fdt length
1358 *
1359 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1360 * Curently supported are the following ramdisk sources:
1361 * - multicomponent kernel/ramdisk image,
1362 * - commandline provided address of decicated ramdisk image.
1363 *
1364 * returns:
1365 * 0, if fdt image was found and valid, or skipped
1366 * of_flat_tree and of_size are set to fdt start address and length if
1367 * fdt image is found and valid
1368 *
1369 * 1, if fdt image is found but corrupted
1370 * of_flat_tree and of_size are set to 0 if no fdt exists
1371 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001372int boot_get_fdt(int flag, int argc, char * const argv[],
1373 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001374{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001375 const image_header_t *fdt_hdr;
Kumar Gala06a09912008-08-15 08:24:38 -05001376 ulong fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001377 char *fdt_blob = NULL;
1378 ulong image_start, image_end;
1379 ulong load_start, load_end;
1380#if defined(CONFIG_FIT)
1381 void *fit_hdr;
1382 const char *fit_uname_config = NULL;
1383 const char *fit_uname_fdt = NULL;
1384 ulong default_addr;
1385 int cfg_noffset;
1386 int fdt_noffset;
1387 const void *data;
1388 size_t size;
1389#endif
1390
1391 *of_flat_tree = NULL;
1392 *of_size = 0;
1393
Stephen Warren712fbcf2011-10-18 11:11:49 +00001394 if (argc > 3 || genimg_has_config(images)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001395#if defined(CONFIG_FIT)
1396 if (argc > 3) {
1397 /*
1398 * If the FDT blob comes from the FIT image and the
1399 * FIT image address is omitted in the command line
1400 * argument, try to use ramdisk or os FIT image
1401 * address or default load address.
1402 */
1403 if (images->fit_uname_rd)
1404 default_addr = (ulong)images->fit_hdr_rd;
1405 else if (images->fit_uname_os)
1406 default_addr = (ulong)images->fit_hdr_os;
1407 else
1408 default_addr = load_addr;
1409
Stephen Warren712fbcf2011-10-18 11:11:49 +00001410 if (fit_parse_conf(argv[3], default_addr,
Kumar Gala06a09912008-08-15 08:24:38 -05001411 &fdt_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001412 debug("* fdt: config '%s' from image at "
1413 "0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001414 fit_uname_config, fdt_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001415 } else if (fit_parse_subimage(argv[3], default_addr,
Kumar Gala06a09912008-08-15 08:24:38 -05001416 &fdt_addr, &fit_uname_fdt)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001417 debug("* fdt: subimage '%s' from image at "
1418 "0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001419 fit_uname_fdt, fdt_addr);
1420 } else
1421#endif
1422 {
1423 fdt_addr = simple_strtoul(argv[3], NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001424 debug("* fdt: cmdline image address = "
1425 "0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001426 fdt_addr);
1427 }
1428#if defined(CONFIG_FIT)
1429 } else {
1430 /* use FIT configuration provided in first bootm
1431 * command argument
1432 */
1433 fdt_addr = (ulong)images->fit_hdr_os;
1434 fit_uname_config = images->fit_uname_cfg;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001435 debug("* fdt: using config '%s' from image "
1436 "at 0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001437 fit_uname_config, fdt_addr);
1438
1439 /*
1440 * Check whether configuration has FDT blob defined,
1441 * if not quit silently.
1442 */
1443 fit_hdr = (void *)fdt_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001444 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001445 fit_uname_config);
1446 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001447 debug("* fdt: no such config\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001448 return 0;
1449 }
1450
Stephen Warren712fbcf2011-10-18 11:11:49 +00001451 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001452 cfg_noffset);
1453 if (fdt_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001454 debug("* fdt: no fdt in config\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001455 return 0;
1456 }
1457 }
1458#endif
1459
Stephen Warren712fbcf2011-10-18 11:11:49 +00001460 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001461 fdt_addr);
1462
1463 /* copy from dataflash if needed */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001464 fdt_addr = genimg_get_image(fdt_addr);
Kumar Gala06a09912008-08-15 08:24:38 -05001465
1466 /*
1467 * Check if there is an FDT image at the
1468 * address provided in the second bootm argument
1469 * check image type, for FIT images get a FIT node.
1470 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001471 switch (genimg_get_format((void *)fdt_addr)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001472 case IMAGE_FORMAT_LEGACY:
1473 /* verify fdt_addr points to a valid image header */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001474 printf("## Flattened Device Tree from Legacy Image "
1475 "at %08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001476 fdt_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001477 fdt_hdr = image_get_fdt(fdt_addr);
Kumar Gala06a09912008-08-15 08:24:38 -05001478 if (!fdt_hdr)
1479 goto error;
1480
1481 /*
1482 * move image data to the load address,
1483 * make sure we don't overwrite initial image
1484 */
1485 image_start = (ulong)fdt_hdr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001486 image_end = image_get_image_end(fdt_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001487
Stephen Warren712fbcf2011-10-18 11:11:49 +00001488 load_start = image_get_load(fdt_hdr);
1489 load_end = load_start + image_get_data_size(fdt_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001490
1491 if ((load_start < image_end) && (load_end > image_start)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001492 fdt_error("fdt overwritten");
Kumar Gala06a09912008-08-15 08:24:38 -05001493 goto error;
1494 }
1495
Stephen Warren712fbcf2011-10-18 11:11:49 +00001496 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
1497 image_get_data(fdt_hdr), load_start);
Kumar Gala06a09912008-08-15 08:24:38 -05001498
Stephen Warren712fbcf2011-10-18 11:11:49 +00001499 memmove((void *)load_start,
1500 (void *)image_get_data(fdt_hdr),
1501 image_get_data_size(fdt_hdr));
Kumar Gala06a09912008-08-15 08:24:38 -05001502
1503 fdt_blob = (char *)load_start;
1504 break;
1505 case IMAGE_FORMAT_FIT:
1506 /*
1507 * This case will catch both: new uImage format
1508 * (libfdt based) and raw FDT blob (also libfdt
1509 * based).
1510 */
1511#if defined(CONFIG_FIT)
1512 /* check FDT blob vs FIT blob */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001513 if (fit_check_format((const void *)fdt_addr)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001514 /*
1515 * FIT image
1516 */
1517 fit_hdr = (void *)fdt_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001518 printf("## Flattened Device Tree from FIT "
1519 "Image at %08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001520 fdt_addr);
1521
1522 if (!fit_uname_fdt) {
1523 /*
1524 * no FDT blob image node unit name,
1525 * try to get config node first. If
1526 * config unit node name is NULL
1527 * fit_conf_get_node() will try to
1528 * find default config node
1529 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001530 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001531 fit_uname_config);
1532
1533 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001534 fdt_error("Could not find "
1535 "configuration "
1536 "node\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001537 goto error;
1538 }
1539
Stephen Warren712fbcf2011-10-18 11:11:49 +00001540 fit_uname_config = fdt_get_name(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001541 cfg_noffset, NULL);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001542 printf(" Using '%s' configuration\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001543 fit_uname_config);
1544
Stephen Warren712fbcf2011-10-18 11:11:49 +00001545 fdt_noffset = fit_conf_get_fdt_node(
1546 fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001547 cfg_noffset);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001548 fit_uname_fdt = fit_get_name(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001549 fdt_noffset, NULL);
1550 } else {
1551 /* get FDT component image node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001552 fdt_noffset = fit_image_get_node(
1553 fit_hdr,
1554 fit_uname_fdt);
Kumar Gala06a09912008-08-15 08:24:38 -05001555 }
1556 if (fdt_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001557 fdt_error("Could not find subimage "
1558 "node\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001559 goto error;
1560 }
1561
Stephen Warren712fbcf2011-10-18 11:11:49 +00001562 printf(" Trying '%s' FDT blob subimage\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001563 fit_uname_fdt);
1564
Stephen Warren712fbcf2011-10-18 11:11:49 +00001565 if (!fit_check_fdt(fit_hdr, fdt_noffset,
Kumar Gala06a09912008-08-15 08:24:38 -05001566 images->verify))
1567 goto error;
1568
1569 /* get ramdisk image data address and length */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001570 if (fit_image_get_data(fit_hdr, fdt_noffset,
Kumar Gala06a09912008-08-15 08:24:38 -05001571 &data, &size)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001572 fdt_error("Could not find FDT "
1573 "subimage data");
Kumar Gala06a09912008-08-15 08:24:38 -05001574 goto error;
1575 }
1576
1577 /* verift that image data is a proper FDT blob */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001578 if (fdt_check_header((char *)data) != 0) {
1579 fdt_error("Subimage data is not a FTD");
Kumar Gala06a09912008-08-15 08:24:38 -05001580 goto error;
1581 }
1582
1583 /*
1584 * move image data to the load address,
1585 * make sure we don't overwrite initial image
1586 */
1587 image_start = (ulong)fit_hdr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001588 image_end = fit_get_end(fit_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001589
Stephen Warren712fbcf2011-10-18 11:11:49 +00001590 if (fit_image_get_load(fit_hdr, fdt_noffset,
Kumar Gala06a09912008-08-15 08:24:38 -05001591 &load_start) == 0) {
1592 load_end = load_start + size;
1593
1594 if ((load_start < image_end) &&
1595 (load_end > image_start)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001596 fdt_error("FDT overwritten");
Kumar Gala06a09912008-08-15 08:24:38 -05001597 goto error;
1598 }
1599
Stephen Warren712fbcf2011-10-18 11:11:49 +00001600 printf(" Loading FDT from 0x%08lx "
1601 "to 0x%08lx\n",
1602 (ulong)data,
1603 load_start);
Kumar Gala06a09912008-08-15 08:24:38 -05001604
Stephen Warren712fbcf2011-10-18 11:11:49 +00001605 memmove((void *)load_start,
Kumar Gala06a09912008-08-15 08:24:38 -05001606 (void *)data, size);
1607
1608 fdt_blob = (char *)load_start;
1609 } else {
1610 fdt_blob = (char *)data;
1611 }
1612
1613 images->fit_hdr_fdt = fit_hdr;
1614 images->fit_uname_fdt = fit_uname_fdt;
1615 images->fit_noffset_fdt = fdt_noffset;
1616 break;
1617 } else
1618#endif
1619 {
1620 /*
1621 * FDT blob
1622 */
1623 fdt_blob = (char *)fdt_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001624 debug("* fdt: raw FDT blob\n");
1625 printf("## Flattened Device Tree blob at "
1626 "%08lx\n", (long)fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001627 }
1628 break;
1629 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001630 puts("ERROR: Did not find a cmdline Flattened Device "
1631 "Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001632 goto error;
1633 }
1634
Simon Glass0ec2ce42011-09-23 06:22:04 +00001635 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001636
1637 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +00001638 image_check_type(&images->legacy_hdr_os_copy,
1639 IH_TYPE_MULTI)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001640
1641 ulong fdt_data, fdt_len;
1642
1643 /*
1644 * Now check if we have a legacy multi-component image,
1645 * get second entry data start address and len.
1646 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001647 printf("## Flattened Device Tree from multi "
Kumar Gala06a09912008-08-15 08:24:38 -05001648 "component Image at %08lX\n",
1649 (ulong)images->legacy_hdr_os);
1650
Stephen Warren712fbcf2011-10-18 11:11:49 +00001651 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1652 &fdt_len);
Kumar Gala06a09912008-08-15 08:24:38 -05001653 if (fdt_len) {
1654
1655 fdt_blob = (char *)fdt_data;
Simon Glass0ec2ce42011-09-23 06:22:04 +00001656 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001657
Stephen Warren712fbcf2011-10-18 11:11:49 +00001658 if (fdt_check_header(fdt_blob) != 0) {
1659 fdt_error("image is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001660 goto error;
1661 }
1662
John Rigbyd1263fc2010-10-13 13:57:32 -06001663 if (fdt_totalsize(fdt_blob) != fdt_len) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001664 fdt_error("fdt size != image size");
Kumar Gala06a09912008-08-15 08:24:38 -05001665 goto error;
1666 }
1667 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001668 debug("## No Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001669 return 0;
1670 }
1671 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001672 debug("## No Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001673 return 0;
1674 }
1675
1676 *of_flat_tree = fdt_blob;
John Rigbyd1263fc2010-10-13 13:57:32 -06001677 *of_size = fdt_totalsize(fdt_blob);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001678 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner52514692008-08-21 07:12:26 -07001679 (ulong)*of_flat_tree, *of_size);
Kumar Gala06a09912008-08-15 08:24:38 -05001680
1681 return 0;
1682
1683error:
1684 *of_flat_tree = 0;
1685 *of_size = 0;
1686 return 1;
1687}
1688#endif /* CONFIG_OF_LIBFDT */
1689
John Rigbyfca43cc2010-10-13 13:57:35 -06001690#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001691/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001692 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001693 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001694 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1695 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1696 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001697 * boot_get_cmdline() allocates space for kernel command line below
Grant Likely590d3ca2011-03-28 09:58:34 +00001698 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001699 * variable is present its contents is copied to allocated kernel
1700 * command line.
1701 *
1702 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001703 * 0 - success
1704 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001705 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001706int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001707{
1708 char *cmdline;
1709 char *s;
1710
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001711 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001712 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001713
1714 if (cmdline == NULL)
1715 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001716
1717 if ((s = getenv("bootargs")) == NULL)
1718 s = "";
1719
1720 strcpy(cmdline, s);
1721
1722 *cmd_start = (ulong) & cmdline[0];
1723 *cmd_end = *cmd_start + strlen(cmdline);
1724
Stephen Warren712fbcf2011-10-18 11:11:49 +00001725 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001726
Kumar Galae822d7f2008-02-27 21:51:49 -06001727 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001728}
John Rigbyfca43cc2010-10-13 13:57:35 -06001729#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001730
John Rigbyfca43cc2010-10-13 13:57:35 -06001731#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001732/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001733 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001734 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001735 * @kbd: double pointer to board info data
1736 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001737 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likely590d3ca2011-03-28 09:58:34 +00001738 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1739 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001740 *
1741 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001742 * 0 - success
1743 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001744 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001745int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001746{
Becky Bruce391fd932008-06-09 20:37:18 -05001747 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001748 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001749 if (*kbd == NULL)
1750 return -1;
1751
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001752 **kbd = *(gd->bd);
1753
Stephen Warren712fbcf2011-10-18 11:11:49 +00001754 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001755
1756#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1757 do_bdinfo(NULL, 0, 0, NULL);
1758#endif
1759
Kumar Galae822d7f2008-02-27 21:51:49 -06001760 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001761}
John Rigbyfca43cc2010-10-13 13:57:35 -06001762#endif /* CONFIG_SYS_BOOT_GET_KBD */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001763#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001764
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001765#if defined(CONFIG_FIT)
1766/*****************************************************************************/
1767/* New uImage format routines */
1768/*****************************************************************************/
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001769#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +00001770static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001771 ulong *addr, const char **name)
1772{
1773 const char *sep;
1774
1775 *addr = addr_curr;
1776 *name = NULL;
1777
Stephen Warren712fbcf2011-10-18 11:11:49 +00001778 sep = strchr(spec, sepc);
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001779 if (sep) {
1780 if (sep - spec > 0)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001781 *addr = simple_strtoul(spec, NULL, 16);
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001782
1783 *name = sep + 1;
1784 return 1;
1785 }
1786
1787 return 0;
1788}
1789
1790/**
1791 * fit_parse_conf - parse FIT configuration spec
1792 * @spec: input string, containing configuration spec
1793 * @add_curr: current image address (to be used as a possible default)
1794 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1795 * configuration
1796 * @conf_name double pointer to a char, will hold pointer to a configuration
1797 * unit name
1798 *
1799 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1800 * where <addr> is a FIT image address that contains configuration
1801 * with a <conf> unit name.
1802 *
1803 * Address part is optional, and if omitted default add_curr will
1804 * be used instead.
1805 *
1806 * returns:
1807 * 1 if spec is a valid configuration string,
1808 * addr and conf_name are set accordingly
1809 * 0 otherwise
1810 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001811inline int fit_parse_conf(const char *spec, ulong addr_curr,
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001812 ulong *addr, const char **conf_name)
1813{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001814 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001815}
1816
1817/**
1818 * fit_parse_subimage - parse FIT subimage spec
1819 * @spec: input string, containing subimage spec
1820 * @add_curr: current image address (to be used as a possible default)
1821 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1822 * subimage
1823 * @image_name: double pointer to a char, will hold pointer to a subimage name
1824 *
1825 * fit_parse_subimage() expects subimage spec in the for of
1826 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1827 * subimage with a <subimg> unit name.
1828 *
1829 * Address part is optional, and if omitted default add_curr will
1830 * be used instead.
1831 *
1832 * returns:
1833 * 1 if spec is a valid subimage string,
1834 * addr and image_name are set accordingly
1835 * 0 otherwise
1836 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001837inline int fit_parse_subimage(const char *spec, ulong addr_curr,
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001838 ulong *addr, const char **image_name)
1839{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001840 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001841}
Marian Balakowicz570abb02008-02-29 15:59:59 +01001842#endif /* !USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001843
Stephen Warren712fbcf2011-10-18 11:11:49 +00001844static void fit_get_debug(const void *fit, int noffset,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001845 char *prop_name, int err)
1846{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001847 debug("Can't get '%s' property from FIT 0x%08lx, "
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001848 "node: offset %d, name %s (%s)\n",
1849 prop_name, (ulong)fit, noffset,
Stephen Warren712fbcf2011-10-18 11:11:49 +00001850 fit_get_name(fit, noffset, NULL),
1851 fdt_strerror(err));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001852}
1853
1854/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001855 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001856 * @fit: pointer to the FIT format image header
1857 * @p: pointer to prefix string
1858 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001859 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001860 * The routine prints out FIT image properties (root node level) follwed by
1861 * the details of each component image.
1862 *
1863 * returns:
1864 * no returned results
1865 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001866void fit_print_contents(const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001867{
1868 char *desc;
1869 char *uname;
1870 int images_noffset;
1871 int confs_noffset;
1872 int noffset;
1873 int ndepth;
1874 int count = 0;
1875 int ret;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001876 const char *p;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001877#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1878 time_t timestamp;
1879#endif
1880
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001881#ifdef USE_HOSTCC
1882 p = "";
1883#else
1884 p = " ";
1885#endif
1886
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001887 /* Root node properties */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001888 ret = fit_get_desc(fit, 0, &desc);
1889 printf("%sFIT description: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001890 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001891 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001892 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001893 printf("%s\n", desc);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001894
1895#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001896 ret = fit_get_timestamp(fit, 0, &timestamp);
1897 printf("%sCreated: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001898 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001899 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001900 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001901 genimg_print_time(timestamp);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001902#endif
1903
1904 /* Find images parent node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001905 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001906 if (images_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001907 printf("Can't find images parent node '%s' (%s)\n",
1908 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001909 return;
1910 }
1911
1912 /* Process its subnodes, print out component images details */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001913 for (ndepth = 0, count = 0,
1914 noffset = fdt_next_node(fit, images_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001915 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001916 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001917 if (ndepth == 1) {
1918 /*
1919 * Direct child node of the images parent node,
1920 * i.e. component image node.
1921 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001922 printf("%s Image %u (%s)\n", p, count++,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001923 fit_get_name(fit, noffset, NULL));
1924
Stephen Warren712fbcf2011-10-18 11:11:49 +00001925 fit_image_print(fit, noffset, p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001926 }
1927 }
1928
1929 /* Find configurations parent node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001930 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001931 if (confs_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001932 debug("Can't get configurations parent node '%s' (%s)\n",
1933 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001934 return;
1935 }
1936
1937 /* get default configuration unit name from default property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001938 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001939 if (uname)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001940 printf("%s Default Configuration: '%s'\n", p, uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001941
1942 /* Process its subnodes, print out configurations details */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001943 for (ndepth = 0, count = 0,
1944 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001945 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001946 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001947 if (ndepth == 1) {
1948 /*
1949 * Direct child node of the configurations parent node,
1950 * i.e. configuration node.
1951 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001952 printf("%s Configuration %u (%s)\n", p, count++,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001953 fit_get_name(fit, noffset, NULL));
1954
Stephen Warren712fbcf2011-10-18 11:11:49 +00001955 fit_conf_print(fit, noffset, p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001956 }
1957 }
1958}
1959
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001960/**
1961 * fit_image_print - prints out the FIT component image details
1962 * @fit: pointer to the FIT format image header
1963 * @image_noffset: offset of the component image node
1964 * @p: pointer to prefix string
1965 *
1966 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001967 * image. If present, hash nodes are printed out as well. Load
1968 * address for images of type firmware is also printed out. Since the load
1969 * address is not mandatory for firmware images, it will be output as
1970 * "unavailable" when not present.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001971 *
1972 * returns:
1973 * no returned results
1974 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001975void fit_image_print(const void *fit, int image_noffset, const char *p)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001976{
1977 char *desc;
1978 uint8_t type, arch, os, comp;
1979 size_t size;
1980 ulong load, entry;
1981 const void *data;
1982 int noffset;
1983 int ndepth;
1984 int ret;
1985
1986 /* Mandatory properties */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001987 ret = fit_get_desc(fit, image_noffset, &desc);
1988 printf("%s Description: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001989 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001990 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001991 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001992 printf("%s\n", desc);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001993
Stephen Warren712fbcf2011-10-18 11:11:49 +00001994 fit_image_get_type(fit, image_noffset, &type);
1995 printf("%s Type: %s\n", p, genimg_get_type_name(type));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001996
Stephen Warren712fbcf2011-10-18 11:11:49 +00001997 fit_image_get_comp(fit, image_noffset, &comp);
1998 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001999
Stephen Warren712fbcf2011-10-18 11:11:49 +00002000 ret = fit_image_get_data(fit, image_noffset, &data, &size);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002001
2002#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +00002003 printf("%s Data Start: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002004 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002005 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002006 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002007 printf("0x%08lx\n", (ulong)data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002008#endif
2009
Stephen Warren712fbcf2011-10-18 11:11:49 +00002010 printf("%s Data Size: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002011 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002012 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002013 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002014 genimg_print_size(size);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002015
2016 /* Remaining, type dependent properties */
2017 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2018 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2019 (type == IH_TYPE_FLATDT)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002020 fit_image_get_arch(fit, image_noffset, &arch);
2021 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002022 }
2023
2024 if (type == IH_TYPE_KERNEL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002025 fit_image_get_os(fit, image_noffset, &os);
2026 printf("%s OS: %s\n", p, genimg_get_os_name(os));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002027 }
2028
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002029 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2030 (type == IH_TYPE_FIRMWARE)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002031 ret = fit_image_get_load(fit, image_noffset, &load);
2032 printf("%s Load Address: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002033 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002034 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002035 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002036 printf("0x%08lx\n", load);
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002037 }
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002038
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002039 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002040 fit_image_get_entry(fit, image_noffset, &entry);
2041 printf("%s Entry Point: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002042 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002043 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002044 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002045 printf("0x%08lx\n", entry);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002046 }
2047
2048 /* Process all hash subnodes of the component image node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002049 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002050 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002051 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002052 if (ndepth == 1) {
2053 /* Direct child node of the component image node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002054 fit_image_print_hash(fit, noffset, p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002055 }
2056 }
2057}
2058
2059/**
2060 * fit_image_print_hash - prints out the hash node details
2061 * @fit: pointer to the FIT format image header
2062 * @noffset: offset of the hash node
2063 * @p: pointer to prefix string
2064 *
2065 * fit_image_print_hash() lists properies for the processed hash node
2066 *
2067 * returns:
2068 * no returned results
2069 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002070void fit_image_print_hash(const void *fit, int noffset, const char *p)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002071{
2072 char *algo;
2073 uint8_t *value;
2074 int value_len;
2075 int i, ret;
2076
2077 /*
2078 * Check subnode name, must be equal to "hash".
2079 * Multiple hash nodes require unique unit node
2080 * names, e.g. hash@1, hash@2, etc.
2081 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002082 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002083 FIT_HASH_NODENAME,
2084 strlen(FIT_HASH_NODENAME)) != 0)
2085 return;
2086
Stephen Warren712fbcf2011-10-18 11:11:49 +00002087 debug("%s Hash node: '%s'\n", p,
2088 fit_get_name(fit, noffset, NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002089
Stephen Warren712fbcf2011-10-18 11:11:49 +00002090 printf("%s Hash algo: ", p);
2091 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2092 printf("invalid/unsupported\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002093 return;
2094 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00002095 printf("%s\n", algo);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002096
Stephen Warren712fbcf2011-10-18 11:11:49 +00002097 ret = fit_image_hash_get_value(fit, noffset, &value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002098 &value_len);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002099 printf("%s Hash value: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002100 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002101 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002102 } else {
2103 for (i = 0; i < value_len; i++)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002104 printf("%02x", value[i]);
2105 printf("\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002106 }
2107
Stephen Warren712fbcf2011-10-18 11:11:49 +00002108 debug("%s Hash len: %d\n", p, value_len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002109}
2110
2111/**
2112 * fit_get_desc - get node description property
2113 * @fit: pointer to the FIT format image header
2114 * @noffset: node offset
2115 * @desc: double pointer to the char, will hold pointer to the descrption
2116 *
2117 * fit_get_desc() reads description property from a given node, if
2118 * description is found pointer to it is returened in third call argument.
2119 *
2120 * returns:
2121 * 0, on success
2122 * -1, on failure
2123 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002124int fit_get_desc(const void *fit, int noffset, char **desc)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002125{
2126 int len;
2127
Stephen Warren712fbcf2011-10-18 11:11:49 +00002128 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002129 if (*desc == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002130 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002131 return -1;
2132 }
2133
2134 return 0;
2135}
2136
2137/**
2138 * fit_get_timestamp - get node timestamp property
2139 * @fit: pointer to the FIT format image header
2140 * @noffset: node offset
2141 * @timestamp: pointer to the time_t, will hold read timestamp
2142 *
2143 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2144 * is found and has a correct size its value is retured in third call
2145 * argument.
2146 *
2147 * returns:
2148 * 0, on success
2149 * -1, on property read failure
2150 * -2, on wrong timestamp size
2151 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002152int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002153{
2154 int len;
2155 const void *data;
2156
Stephen Warren712fbcf2011-10-18 11:11:49 +00002157 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002158 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002159 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002160 return -1;
2161 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00002162 if (len != sizeof(uint32_t)) {
2163 debug("FIT timestamp with incorrect size of (%u)\n", len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002164 return -2;
2165 }
2166
Stephen Warren712fbcf2011-10-18 11:11:49 +00002167 *timestamp = uimage_to_cpu(*((uint32_t *)data));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002168 return 0;
2169}
2170
2171/**
2172 * fit_image_get_node - get node offset for component image of a given unit name
2173 * @fit: pointer to the FIT format image header
2174 * @image_uname: component image node unit name
2175 *
2176 * fit_image_get_node() finds a component image (withing the '/images'
2177 * node) of a provided unit name. If image is found its node offset is
2178 * returned to the caller.
2179 *
2180 * returns:
2181 * image node offset when found (>=0)
2182 * negative number on failure (FDT_ERR_* code)
2183 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002184int fit_image_get_node(const void *fit, const char *image_uname)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002185{
2186 int noffset, images_noffset;
2187
Stephen Warren712fbcf2011-10-18 11:11:49 +00002188 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002189 if (images_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002190 debug("Can't find images parent node '%s' (%s)\n",
2191 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002192 return images_noffset;
2193 }
2194
Stephen Warren712fbcf2011-10-18 11:11:49 +00002195 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002196 if (noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002197 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2198 image_uname, fdt_strerror(noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002199 }
2200
2201 return noffset;
2202}
2203
2204/**
2205 * fit_image_get_os - get os id for a given component image node
2206 * @fit: pointer to the FIT format image header
2207 * @noffset: component image node offset
2208 * @os: pointer to the uint8_t, will hold os numeric id
2209 *
2210 * fit_image_get_os() finds os property in a given component image node.
2211 * If the property is found, its (string) value is translated to the numeric
2212 * id which is returned to the caller.
2213 *
2214 * returns:
2215 * 0, on success
2216 * -1, on failure
2217 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002218int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002219{
2220 int len;
2221 const void *data;
2222
2223 /* Get OS name from property data */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002224 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002225 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002226 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002227 *os = -1;
2228 return -1;
2229 }
2230
2231 /* Translate OS name to id */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002232 *os = genimg_get_os_id(data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002233 return 0;
2234}
2235
2236/**
2237 * fit_image_get_arch - get arch id for a given component image node
2238 * @fit: pointer to the FIT format image header
2239 * @noffset: component image node offset
2240 * @arch: pointer to the uint8_t, will hold arch numeric id
2241 *
2242 * fit_image_get_arch() finds arch property in a given component image node.
2243 * If the property is found, its (string) value is translated to the numeric
2244 * id which is returned to the caller.
2245 *
2246 * returns:
2247 * 0, on success
2248 * -1, on failure
2249 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002250int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002251{
2252 int len;
2253 const void *data;
2254
2255 /* Get architecture name from property data */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002256 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002257 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002258 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002259 *arch = -1;
2260 return -1;
2261 }
2262
2263 /* Translate architecture name to id */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002264 *arch = genimg_get_arch_id(data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002265 return 0;
2266}
2267
2268/**
2269 * fit_image_get_type - get type id for a given component image node
2270 * @fit: pointer to the FIT format image header
2271 * @noffset: component image node offset
2272 * @type: pointer to the uint8_t, will hold type numeric id
2273 *
2274 * fit_image_get_type() finds type property in a given component image node.
2275 * If the property is found, its (string) value is translated to the numeric
2276 * id which is returned to the caller.
2277 *
2278 * returns:
2279 * 0, on success
2280 * -1, on failure
2281 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002282int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002283{
2284 int len;
2285 const void *data;
2286
2287 /* Get image type name from property data */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002288 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002289 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002290 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002291 *type = -1;
2292 return -1;
2293 }
2294
2295 /* Translate image type name to id */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002296 *type = genimg_get_type_id(data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002297 return 0;
2298}
2299
2300/**
2301 * fit_image_get_comp - get comp id for a given component image node
2302 * @fit: pointer to the FIT format image header
2303 * @noffset: component image node offset
2304 * @comp: pointer to the uint8_t, will hold comp numeric id
2305 *
2306 * fit_image_get_comp() finds comp property in a given component image node.
2307 * If the property is found, its (string) value is translated to the numeric
2308 * id which is returned to the caller.
2309 *
2310 * returns:
2311 * 0, on success
2312 * -1, on failure
2313 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002314int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002315{
2316 int len;
2317 const void *data;
2318
2319 /* Get compression name from property data */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002320 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002321 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002322 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002323 *comp = -1;
2324 return -1;
2325 }
2326
2327 /* Translate compression name to id */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002328 *comp = genimg_get_comp_id(data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002329 return 0;
2330}
2331
2332/**
2333 * fit_image_get_load - get load address property for a given component image node
2334 * @fit: pointer to the FIT format image header
2335 * @noffset: component image node offset
2336 * @load: pointer to the uint32_t, will hold load address
2337 *
2338 * fit_image_get_load() finds load address property in a given component image node.
2339 * If the property is found, its value is returned to the caller.
2340 *
2341 * returns:
2342 * 0, on success
2343 * -1, on failure
2344 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002345int fit_image_get_load(const void *fit, int noffset, ulong *load)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002346{
2347 int len;
2348 const uint32_t *data;
2349
Stephen Warren712fbcf2011-10-18 11:11:49 +00002350 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002351 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002352 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002353 return -1;
2354 }
2355
Stephen Warren712fbcf2011-10-18 11:11:49 +00002356 *load = uimage_to_cpu(*data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002357 return 0;
2358}
2359
2360/**
2361 * fit_image_get_entry - get entry point address property for a given component image node
2362 * @fit: pointer to the FIT format image header
2363 * @noffset: component image node offset
2364 * @entry: pointer to the uint32_t, will hold entry point address
2365 *
2366 * fit_image_get_entry() finds entry point address property in a given component image node.
2367 * If the property is found, its value is returned to the caller.
2368 *
2369 * returns:
2370 * 0, on success
2371 * -1, on failure
2372 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002373int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002374{
2375 int len;
2376 const uint32_t *data;
2377
Stephen Warren712fbcf2011-10-18 11:11:49 +00002378 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002379 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002380 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002381 return -1;
2382 }
2383
Stephen Warren712fbcf2011-10-18 11:11:49 +00002384 *entry = uimage_to_cpu(*data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002385 return 0;
2386}
2387
2388/**
2389 * fit_image_get_data - get data property and its size for a given component image node
2390 * @fit: pointer to the FIT format image header
2391 * @noffset: component image node offset
2392 * @data: double pointer to void, will hold data property's data address
2393 * @size: pointer to size_t, will hold data property's data size
2394 *
2395 * fit_image_get_data() finds data property in a given component image node.
2396 * If the property is found its data start address and size are returned to
2397 * the caller.
2398 *
2399 * returns:
2400 * 0, on success
2401 * -1, on failure
2402 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002403int fit_image_get_data(const void *fit, int noffset,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002404 const void **data, size_t *size)
2405{
2406 int len;
2407
Stephen Warren712fbcf2011-10-18 11:11:49 +00002408 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002409 if (*data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002410 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002411 *size = 0;
2412 return -1;
2413 }
2414
2415 *size = len;
2416 return 0;
2417}
2418
2419/**
2420 * fit_image_hash_get_algo - get hash algorithm name
2421 * @fit: pointer to the FIT format image header
2422 * @noffset: hash node offset
2423 * @algo: double pointer to char, will hold pointer to the algorithm name
2424 *
2425 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2426 * If the property is found its data start address is returned to the caller.
2427 *
2428 * returns:
2429 * 0, on success
2430 * -1, on failure
2431 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002432int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002433{
2434 int len;
2435
Stephen Warren712fbcf2011-10-18 11:11:49 +00002436 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002437 if (*algo == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002438 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002439 return -1;
2440 }
2441
2442 return 0;
2443}
2444
2445/**
2446 * fit_image_hash_get_value - get hash value and length
2447 * @fit: pointer to the FIT format image header
2448 * @noffset: hash node offset
2449 * @value: double pointer to uint8_t, will hold address of a hash value data
2450 * @value_len: pointer to an int, will hold hash data length
2451 *
2452 * fit_image_hash_get_value() finds hash value property in a given hash node.
2453 * If the property is found its data start address and size are returned to
2454 * the caller.
2455 *
2456 * returns:
2457 * 0, on success
2458 * -1, on failure
2459 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002460int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002461 int *value_len)
2462{
2463 int len;
2464
Stephen Warren712fbcf2011-10-18 11:11:49 +00002465 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002466 if (*value == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002467 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002468 *value_len = 0;
2469 return -1;
2470 }
2471
2472 *value_len = len;
2473 return 0;
2474}
2475
2476/**
2477 * fit_set_timestamp - set node timestamp property
2478 * @fit: pointer to the FIT format image header
2479 * @noffset: node offset
2480 * @timestamp: timestamp value to be set
2481 *
2482 * fit_set_timestamp() attempts to set timestamp property in the requested
2483 * node and returns operation status to the caller.
2484 *
2485 * returns:
2486 * 0, on success
2487 * -1, on property read failure
2488 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002489int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002490{
2491 uint32_t t;
2492 int ret;
2493
Stephen Warren712fbcf2011-10-18 11:11:49 +00002494 t = cpu_to_uimage(timestamp);
2495 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2496 sizeof(uint32_t));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002497 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002498 printf("Can't set '%s' property for '%s' node (%s)\n",
2499 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2500 fdt_strerror(ret));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002501 return -1;
2502 }
2503
2504 return 0;
2505}
2506
2507/**
2508 * calculate_hash - calculate and return hash for provided input data
2509 * @data: pointer to the input data
2510 * @data_len: data length
2511 * @algo: requested hash algorithm
2512 * @value: pointer to the char, will hold hash value data (caller must
2513 * allocate enough free space)
2514 * value_len: length of the calculated hash
2515 *
2516 * calculate_hash() computes input data hash according to the requested algorithm.
2517 * Resulting hash value is placed in caller provided 'value' buffer, length
2518 * of the calculated hash is returned via value_len pointer argument.
2519 *
2520 * returns:
2521 * 0, on success
2522 * -1, when algo is unsupported
2523 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002524static int calculate_hash(const void *data, int data_len, const char *algo,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002525 uint8_t *value, int *value_len)
2526{
Stephen Warren712fbcf2011-10-18 11:11:49 +00002527 if (strcmp(algo, "crc32") == 0) {
2528 *((uint32_t *)value) = crc32_wd(0, data, data_len,
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002529 CHUNKSZ_CRC32);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002530 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002531 *value_len = 4;
Stephen Warren712fbcf2011-10-18 11:11:49 +00002532 } else if (strcmp(algo, "sha1") == 0) {
2533 sha1_csum_wd((unsigned char *) data, data_len,
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002534 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002535 *value_len = 20;
Stephen Warren712fbcf2011-10-18 11:11:49 +00002536 } else if (strcmp(algo, "md5") == 0) {
2537 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka766529f2008-03-14 16:22:34 +01002538 *value_len = 16;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002539 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002540 debug("Unsupported hash alogrithm\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002541 return -1;
2542 }
2543 return 0;
2544}
2545
2546#ifdef USE_HOSTCC
2547/**
2548 * fit_set_hashes - process FIT component image nodes and calculate hashes
2549 * @fit: pointer to the FIT format image header
2550 *
2551 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2552 * Hashes are calculated for all component images which have hash subnodes
2553 * with algorithm property set to one of the supported hash algorithms.
2554 *
2555 * returns
2556 * 0, on success
2557 * libfdt error code, on failure
2558 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002559int fit_set_hashes(void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002560{
2561 int images_noffset;
2562 int noffset;
2563 int ndepth;
2564 int ret;
2565
2566 /* Find images parent node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002567 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002568 if (images_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002569 printf("Can't find images parent node '%s' (%s)\n",
2570 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002571 return images_noffset;
2572 }
2573
2574 /* Process its subnodes, print out component images details */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002575 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002576 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002577 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002578 if (ndepth == 1) {
2579 /*
2580 * Direct child node of the images parent node,
2581 * i.e. component image node.
2582 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002583 ret = fit_image_set_hashes(fit, noffset);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002584 if (ret)
2585 return ret;
2586 }
2587 }
2588
2589 return 0;
2590}
2591
2592/**
2593 * fit_image_set_hashes - calculate/set hashes for given component image node
2594 * @fit: pointer to the FIT format image header
2595 * @image_noffset: requested component image node
2596 *
2597 * fit_image_set_hashes() adds hash values for an component image node. All
2598 * existing hash subnodes are checked, if algorithm property is set to one of
2599 * the supported hash algorithms, hash value is computed and corresponding
2600 * hash node property is set, for example:
2601 *
2602 * Input component image node structure:
2603 *
2604 * o image@1 (at image_noffset)
2605 * | - data = [binary data]
2606 * o hash@1
2607 * |- algo = "sha1"
2608 *
2609 * Output component image node structure:
2610 *
2611 * o image@1 (at image_noffset)
2612 * | - data = [binary data]
2613 * o hash@1
2614 * |- algo = "sha1"
2615 * |- value = sha1(data)
2616 *
2617 * returns:
2618 * 0 on sucess
2619 * <0 on failure
2620 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002621int fit_image_set_hashes(void *fit, int image_noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002622{
2623 const void *data;
2624 size_t size;
2625 char *algo;
2626 uint8_t value[FIT_MAX_HASH_LEN];
2627 int value_len;
2628 int noffset;
2629 int ndepth;
2630
2631 /* Get image data and data length */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002632 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2633 printf("Can't get image data/size\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002634 return -1;
2635 }
2636
2637 /* Process all hash subnodes of the component image node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002638 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002639 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002640 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002641 if (ndepth == 1) {
2642 /* Direct child node of the component image node */
2643
2644 /*
2645 * Check subnode name, must be equal to "hash".
2646 * Multiple hash nodes require unique unit node
2647 * names, e.g. hash@1, hash@2, etc.
2648 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002649 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002650 FIT_HASH_NODENAME,
2651 strlen(FIT_HASH_NODENAME)) != 0) {
2652 /* Not a hash subnode, skip it */
2653 continue;
2654 }
2655
Stephen Warren712fbcf2011-10-18 11:11:49 +00002656 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2657 printf("Can't get hash algo property for "
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002658 "'%s' hash node in '%s' image node\n",
Stephen Warren712fbcf2011-10-18 11:11:49 +00002659 fit_get_name(fit, noffset, NULL),
2660 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002661 return -1;
2662 }
2663
Stephen Warren712fbcf2011-10-18 11:11:49 +00002664 if (calculate_hash(data, size, algo, value,
2665 &value_len)) {
2666 printf("Unsupported hash algorithm (%s) for "
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002667 "'%s' hash node in '%s' image node\n",
Stephen Warren712fbcf2011-10-18 11:11:49 +00002668 algo, fit_get_name(fit, noffset, NULL),
2669 fit_get_name(fit, image_noffset,
2670 NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002671 return -1;
2672 }
2673
Stephen Warren712fbcf2011-10-18 11:11:49 +00002674 if (fit_image_hash_set_value(fit, noffset, value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002675 value_len)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002676 printf("Can't set hash value for "
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002677 "'%s' hash node in '%s' image node\n",
Stephen Warren712fbcf2011-10-18 11:11:49 +00002678 fit_get_name(fit, noffset, NULL),
2679 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002680 return -1;
2681 }
2682 }
2683 }
2684
2685 return 0;
2686}
2687
2688/**
2689 * fit_image_hash_set_value - set hash value in requested has node
2690 * @fit: pointer to the FIT format image header
2691 * @noffset: hash node offset
2692 * @value: hash value to be set
2693 * @value_len: hash value length
2694 *
2695 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2696 * given and returns operation status to the caller.
2697 *
2698 * returns
2699 * 0, on success
2700 * -1, on failure
2701 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002702int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002703 int value_len)
2704{
2705 int ret;
2706
Stephen Warren712fbcf2011-10-18 11:11:49 +00002707 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002708 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002709 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2710 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2711 fdt_strerror(ret));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002712 return -1;
2713 }
2714
2715 return 0;
2716}
2717#endif /* USE_HOSTCC */
2718
2719/**
2720 * fit_image_check_hashes - verify data intergity
2721 * @fit: pointer to the FIT format image header
2722 * @image_noffset: component image node offset
2723 *
2724 * fit_image_check_hashes() goes over component image hash nodes,
2725 * re-calculates each data hash and compares with the value stored in hash
2726 * node.
2727 *
2728 * returns:
2729 * 1, if all hashes are valid
2730 * 0, otherwise (or on error)
2731 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002732int fit_image_check_hashes(const void *fit, int image_noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002733{
2734 const void *data;
2735 size_t size;
2736 char *algo;
2737 uint8_t *fit_value;
2738 int fit_value_len;
2739 uint8_t value[FIT_MAX_HASH_LEN];
2740 int value_len;
2741 int noffset;
2742 int ndepth;
2743 char *err_msg = "";
2744
2745 /* Get image data and data length */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002746 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2747 printf("Can't get image data/size\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002748 return 0;
2749 }
2750
2751 /* Process all hash subnodes of the component image node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002752 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002753 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002754 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002755 if (ndepth == 1) {
2756 /* Direct child node of the component image node */
2757
2758 /*
2759 * Check subnode name, must be equal to "hash".
2760 * Multiple hash nodes require unique unit node
2761 * names, e.g. hash@1, hash@2, etc.
2762 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002763 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002764 FIT_HASH_NODENAME,
2765 strlen(FIT_HASH_NODENAME)) != 0)
2766 continue;
2767
Stephen Warren712fbcf2011-10-18 11:11:49 +00002768 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002769 err_msg = " error!\nCan't get hash algo "
2770 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002771 goto error;
2772 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00002773 printf("%s", algo);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002774
Stephen Warren712fbcf2011-10-18 11:11:49 +00002775 if (fit_image_hash_get_value(fit, noffset, &fit_value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002776 &fit_value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002777 err_msg = " error!\nCan't get hash value "
2778 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002779 goto error;
2780 }
2781
Stephen Warren712fbcf2011-10-18 11:11:49 +00002782 if (calculate_hash(data, size, algo, value,
2783 &value_len)) {
2784 err_msg = " error!\n"
2785 "Unsupported hash algorithm";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002786 goto error;
2787 }
2788
2789 if (value_len != fit_value_len) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002790 err_msg = " error !\nBad hash value len";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002791 goto error;
Stephen Warren712fbcf2011-10-18 11:11:49 +00002792 } else if (memcmp(value, fit_value, value_len) != 0) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002793 err_msg = " error!\nBad hash value";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002794 goto error;
2795 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00002796 printf("+ ");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002797 }
2798 }
2799
2800 return 1;
2801
2802error:
Stephen Warren712fbcf2011-10-18 11:11:49 +00002803 printf("%s for '%s' hash node in '%s' image node\n",
2804 err_msg, fit_get_name(fit, noffset, NULL),
2805 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002806 return 0;
2807}
2808
2809/**
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002810 * fit_all_image_check_hashes - verify data intergity for all images
2811 * @fit: pointer to the FIT format image header
2812 *
2813 * fit_all_image_check_hashes() goes over all images in the FIT and
2814 * for every images checks if all it's hashes are valid.
2815 *
2816 * returns:
2817 * 1, if all hashes of all images are valid
2818 * 0, otherwise (or on error)
2819 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002820int fit_all_image_check_hashes(const void *fit)
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002821{
2822 int images_noffset;
2823 int noffset;
2824 int ndepth;
2825 int count;
2826
2827 /* Find images parent node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002828 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002829 if (images_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002830 printf("Can't find images parent node '%s' (%s)\n",
2831 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002832 return 0;
2833 }
2834
2835 /* Process all image subnodes, check hashes for each */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002836 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002837 (ulong)fit);
2838 for (ndepth = 0, count = 0,
Stephen Warren712fbcf2011-10-18 11:11:49 +00002839 noffset = fdt_next_node(fit, images_noffset, &ndepth);
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002840 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002841 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002842 if (ndepth == 1) {
2843 /*
2844 * Direct child node of the images parent node,
2845 * i.e. component image node.
2846 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002847 printf(" Hash(es) for Image %u (%s): ", count++,
2848 fit_get_name(fit, noffset, NULL));
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002849
Stephen Warren712fbcf2011-10-18 11:11:49 +00002850 if (!fit_image_check_hashes(fit, noffset))
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002851 return 0;
Stephen Warren712fbcf2011-10-18 11:11:49 +00002852 printf("\n");
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002853 }
2854 }
2855 return 1;
2856}
2857
2858/**
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002859 * fit_image_check_os - check whether image node is of a given os type
2860 * @fit: pointer to the FIT format image header
2861 * @noffset: component image node offset
2862 * @os: requested image os
2863 *
2864 * fit_image_check_os() reads image os property and compares its numeric
2865 * id with the requested os. Comparison result is returned to the caller.
2866 *
2867 * returns:
2868 * 1 if image is of given os type
2869 * 0 otherwise (or on error)
2870 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002871int fit_image_check_os(const void *fit, int noffset, uint8_t os)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002872{
2873 uint8_t image_os;
2874
Stephen Warren712fbcf2011-10-18 11:11:49 +00002875 if (fit_image_get_os(fit, noffset, &image_os))
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002876 return 0;
2877 return (os == image_os);
2878}
2879
2880/**
2881 * fit_image_check_arch - check whether image node is of a given arch
2882 * @fit: pointer to the FIT format image header
2883 * @noffset: component image node offset
2884 * @arch: requested imagearch
2885 *
2886 * fit_image_check_arch() reads image arch property and compares its numeric
2887 * id with the requested arch. Comparison result is returned to the caller.
2888 *
2889 * returns:
2890 * 1 if image is of given arch
2891 * 0 otherwise (or on error)
2892 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002893int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002894{
2895 uint8_t image_arch;
2896
Stephen Warren712fbcf2011-10-18 11:11:49 +00002897 if (fit_image_get_arch(fit, noffset, &image_arch))
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002898 return 0;
2899 return (arch == image_arch);
2900}
2901
2902/**
2903 * fit_image_check_type - check whether image node is of a given type
2904 * @fit: pointer to the FIT format image header
2905 * @noffset: component image node offset
2906 * @type: requested image type
2907 *
2908 * fit_image_check_type() reads image type property and compares its numeric
2909 * id with the requested type. Comparison result is returned to the caller.
2910 *
2911 * returns:
2912 * 1 if image is of given type
2913 * 0 otherwise (or on error)
2914 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002915int fit_image_check_type(const void *fit, int noffset, uint8_t type)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002916{
2917 uint8_t image_type;
2918
Stephen Warren712fbcf2011-10-18 11:11:49 +00002919 if (fit_image_get_type(fit, noffset, &image_type))
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002920 return 0;
2921 return (type == image_type);
2922}
2923
2924/**
2925 * fit_image_check_comp - check whether image node uses given compression
2926 * @fit: pointer to the FIT format image header
2927 * @noffset: component image node offset
2928 * @comp: requested image compression type
2929 *
2930 * fit_image_check_comp() reads image compression property and compares its
2931 * numeric id with the requested compression type. Comparison result is
2932 * returned to the caller.
2933 *
2934 * returns:
2935 * 1 if image uses requested compression
2936 * 0 otherwise (or on error)
2937 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002938int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002939{
2940 uint8_t image_comp;
2941
Stephen Warren712fbcf2011-10-18 11:11:49 +00002942 if (fit_image_get_comp(fit, noffset, &image_comp))
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002943 return 0;
2944 return (comp == image_comp);
2945}
2946
2947/**
2948 * fit_check_format - sanity check FIT image format
2949 * @fit: pointer to the FIT format image header
2950 *
2951 * fit_check_format() runs a basic sanity FIT image verification.
2952 * Routine checks for mandatory properties, nodes, etc.
2953 *
2954 * returns:
2955 * 1, on success
2956 * 0, on failure
2957 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002958int fit_check_format(const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002959{
2960 /* mandatory / node 'description' property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002961 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2962 debug("Wrong FIT format: no description\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002963 return 0;
2964 }
2965
2966#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2967 /* mandatory / node 'timestamp' property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002968 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2969 debug("Wrong FIT format: no timestamp\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002970 return 0;
2971 }
2972#endif
2973
2974 /* mandatory subimages parent '/images' node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002975 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
2976 debug("Wrong FIT format: no images parent node\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002977 return 0;
2978 }
2979
2980 return 1;
2981}
2982
2983/**
2984 * fit_conf_get_node - get node offset for configuration of a given unit name
2985 * @fit: pointer to the FIT format image header
2986 * @conf_uname: configuration node unit name
2987 *
2988 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2989 * parant node) of a provided unit name. If configuration is found its node offset
2990 * is returned to the caller.
2991 *
2992 * When NULL is provided in second argument fit_conf_get_node() will search
2993 * for a default configuration node instead. Default configuration node unit name
2994 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2995 *
2996 * returns:
2997 * configuration node offset when found (>=0)
2998 * negative number on failure (FDT_ERR_* code)
2999 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003000int fit_conf_get_node(const void *fit, const char *conf_uname)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003001{
3002 int noffset, confs_noffset;
3003 int len;
3004
Stephen Warren712fbcf2011-10-18 11:11:49 +00003005 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003006 if (confs_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00003007 debug("Can't find configurations parent node '%s' (%s)\n",
3008 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003009 return confs_noffset;
3010 }
3011
3012 if (conf_uname == NULL) {
3013 /* get configuration unit name from the default property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003014 debug("No configuration specified, trying default...\n");
3015 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3016 FIT_DEFAULT_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003017 if (conf_uname == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00003018 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3019 len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003020 return len;
3021 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00003022 debug("Found default configuration: '%s'\n", conf_uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003023 }
3024
Stephen Warren712fbcf2011-10-18 11:11:49 +00003025 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003026 if (noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00003027 debug("Can't get node offset for configuration unit name: "
3028 "'%s' (%s)\n",
3029 conf_uname, fdt_strerror(noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003030 }
3031
3032 return noffset;
3033}
3034
Stephen Warren712fbcf2011-10-18 11:11:49 +00003035static int __fit_conf_get_prop_node(const void *fit, int noffset,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003036 const char *prop_name)
3037{
3038 char *uname;
3039 int len;
3040
3041 /* get kernel image unit name from configuration kernel property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003042 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003043 if (uname == NULL)
3044 return len;
3045
Stephen Warren712fbcf2011-10-18 11:11:49 +00003046 return fit_image_get_node(fit, uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003047}
3048
3049/**
3050 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3051 * a given configuration
3052 * @fit: pointer to the FIT format image header
3053 * @noffset: configuration node offset
3054 *
3055 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3056 * configuration FIT_KERNEL_PROP property and translates it to the node
3057 * offset.
3058 *
3059 * returns:
3060 * image node offset when found (>=0)
3061 * negative number on failure (FDT_ERR_* code)
3062 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003063int fit_conf_get_kernel_node(const void *fit, int noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003064{
Stephen Warren712fbcf2011-10-18 11:11:49 +00003065 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003066}
3067
3068/**
3069 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3070 * a given configuration
3071 * @fit: pointer to the FIT format image header
3072 * @noffset: configuration node offset
3073 *
3074 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3075 * configuration FIT_KERNEL_PROP property and translates it to the node
3076 * offset.
3077 *
3078 * returns:
3079 * image node offset when found (>=0)
3080 * negative number on failure (FDT_ERR_* code)
3081 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003082int fit_conf_get_ramdisk_node(const void *fit, int noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003083{
Stephen Warren712fbcf2011-10-18 11:11:49 +00003084 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003085}
3086
3087/**
3088 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3089 * a given configuration
3090 * @fit: pointer to the FIT format image header
3091 * @noffset: configuration node offset
3092 *
3093 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3094 * configuration FIT_KERNEL_PROP property and translates it to the node
3095 * offset.
3096 *
3097 * returns:
3098 * image node offset when found (>=0)
3099 * negative number on failure (FDT_ERR_* code)
3100 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003101int fit_conf_get_fdt_node(const void *fit, int noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003102{
Stephen Warren712fbcf2011-10-18 11:11:49 +00003103 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003104}
3105
3106/**
3107 * fit_conf_print - prints out the FIT configuration details
3108 * @fit: pointer to the FIT format image header
Marian Balakowiczf773bea2008-03-12 10:35:46 +01003109 * @noffset: offset of the configuration node
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003110 * @p: pointer to prefix string
3111 *
3112 * fit_conf_print() lists all mandatory properies for the processed
3113 * configuration node.
3114 *
3115 * returns:
3116 * no returned results
3117 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003118void fit_conf_print(const void *fit, int noffset, const char *p)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003119{
3120 char *desc;
3121 char *uname;
3122 int ret;
3123
3124 /* Mandatory properties */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003125 ret = fit_get_desc(fit, noffset, &desc);
3126 printf("%s Description: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003127 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00003128 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003129 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00003130 printf("%s\n", desc);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003131
Stephen Warren712fbcf2011-10-18 11:11:49 +00003132 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3133 printf("%s Kernel: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003134 if (uname == NULL)
Stephen Warren712fbcf2011-10-18 11:11:49 +00003135 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003136 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00003137 printf("%s\n", uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003138
3139 /* Optional properties */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003140 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003141 if (uname)
Stephen Warren712fbcf2011-10-18 11:11:49 +00003142 printf("%s Init Ramdisk: %s\n", p, uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003143
Stephen Warren712fbcf2011-10-18 11:11:49 +00003144 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003145 if (uname)
Stephen Warren712fbcf2011-10-18 11:11:49 +00003146 printf("%s FDT: %s\n", p, uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003147}
Marian Balakowiczc8779642008-03-12 10:12:37 +01003148
3149/**
3150 * fit_check_ramdisk - verify FIT format ramdisk subimage
3151 * @fit_hdr: pointer to the FIT ramdisk header
3152 * @rd_noffset: ramdisk subimage node offset within FIT image
3153 * @arch: requested ramdisk image architecture type
3154 * @verify: data CRC verification flag
3155 *
3156 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3157 * specified FIT image.
3158 *
3159 * returns:
3160 * 1, on success
3161 * 0, on failure
3162 */
3163#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +00003164static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3165 int verify)
Marian Balakowiczc8779642008-03-12 10:12:37 +01003166{
Stephen Warren712fbcf2011-10-18 11:11:49 +00003167 fit_image_print(fit, rd_noffset, " ");
Marian Balakowiczc8779642008-03-12 10:12:37 +01003168
3169 if (verify) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00003170 puts(" Verifying Hash Integrity ... ");
3171 if (!fit_image_check_hashes(fit, rd_noffset)) {
3172 puts("Bad Data Hash\n");
3173 show_boot_progress(-125);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003174 return 0;
3175 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00003176 puts("OK\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +01003177 }
3178
Stephen Warren712fbcf2011-10-18 11:11:49 +00003179 show_boot_progress(126);
3180 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3181 !fit_image_check_arch(fit, rd_noffset, arch) ||
3182 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3183 printf("No Linux %s Ramdisk Image\n",
Marian Balakowiczc8779642008-03-12 10:12:37 +01003184 genimg_get_arch_name(arch));
Stephen Warren712fbcf2011-10-18 11:11:49 +00003185 show_boot_progress(-126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003186 return 0;
3187 }
3188
Stephen Warren712fbcf2011-10-18 11:11:49 +00003189 show_boot_progress(127);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003190 return 1;
3191}
3192#endif /* USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003193#endif /* CONFIG_FIT */