blob: 6afbb40a9871078abe99d48deba8a2b54b16dbc5 [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
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -060046#include <environment.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010047#include <image.h>
48
Stephen Warren712fbcf2011-10-18 11:11:49 +000049#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczfff888a12008-02-21 17:20:19 +010050#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
Simon Glass0ccff502013-02-24 17:33:25 +000077#include <u-boot/crc.h>
78
Mike Frysinger7edb1862010-10-20 07:17:39 -040079static const table_entry_t uimage_arch[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +010080 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
81 { IH_ARCH_ALPHA, "alpha", "Alpha", },
82 { IH_ARCH_ARM, "arm", "ARM", },
83 { IH_ARCH_I386, "x86", "Intel x86", },
84 { IH_ARCH_IA64, "ia64", "IA64", },
85 { IH_ARCH_M68K, "m68k", "M68K", },
86 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
87 { IH_ARCH_MIPS, "mips", "MIPS", },
88 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010089 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -070090 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010091 { IH_ARCH_PPC, "ppc", "PowerPC", },
92 { IH_ARCH_S390, "s390", "IBM S390", },
93 { IH_ARCH_SH, "sh", "SuperH", },
94 { IH_ARCH_SPARC, "sparc", "SPARC", },
95 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
96 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
97 { IH_ARCH_AVR32, "avr32", "AVR32", },
Macpaul Lin64d61462011-10-19 20:41:09 +000098 { IH_ARCH_NDS32, "nds32", "NDS32", },
Stefan Kristiansson3ddcacc2011-11-26 19:04:50 +000099 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100100 { -1, "", "", },
101};
102
Mike Frysinger7edb1862010-10-20 07:17:39 -0400103static const table_entry_t uimage_os[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100104 { IH_OS_INVALID, NULL, "Invalid OS", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100105 { IH_OS_LINUX, "linux", "Linux", },
106#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
107 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
108#endif
109 { IH_OS_NETBSD, "netbsd", "NetBSD", },
Torkel Lundgren3df61952010-09-28 11:05:36 +0200110 { IH_OS_OSE, "ose", "Enea OSE", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100111 { IH_OS_RTEMS, "rtems", "RTEMS", },
112 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
113#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
114 { IH_OS_QNX, "qnx", "QNX", },
115 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
116#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500117#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
118 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
119#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100120#ifdef USE_HOSTCC
121 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
122 { IH_OS_DELL, "dell", "Dell", },
123 { IH_OS_ESIX, "esix", "Esix", },
124 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
125 { IH_OS_IRIX, "irix", "Irix", },
126 { IH_OS_NCR, "ncr", "NCR", },
127 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
128 { IH_OS_PSOS, "psos", "pSOS", },
129 { IH_OS_SCO, "sco", "SCO", },
130 { IH_OS_SOLARIS, "solaris", "Solaris", },
131 { IH_OS_SVR4, "svr4", "SVR4", },
132#endif
133 { -1, "", "", },
134};
135
Mike Frysinger7edb1862010-10-20 07:17:39 -0400136static const table_entry_t uimage_type[] = {
Stefano Babic4962e382011-10-17 00:07:43 +0000137 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100138 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
139 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby3decb142011-07-21 09:10:30 -0400140 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100141 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stephen Warrenb9b50e892011-11-10 13:17:53 -0700142 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
Stefano Babic4962e382011-10-17 00:07:43 +0000143 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
144 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
145 { IH_TYPE_INVALID, NULL, "Invalid Image", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100146 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic4962e382011-10-17 00:07:43 +0000147 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Shaohui Xie5d898a02012-08-10 02:49:35 +0000148 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100149 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
150 { IH_TYPE_SCRIPT, "script", "Script", },
151 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000152 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100153 { -1, "", "", },
154};
155
Mike Frysinger7edb1862010-10-20 07:17:39 -0400156static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100157 { IH_COMP_NONE, "none", "uncompressed", },
158 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
159 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200160 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100161 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100162 { -1, "", "", },
163};
164
Marian Balakowicz570abb02008-02-29 15:59:59 +0100165#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000166static void genimg_print_time(time_t timestamp);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100167#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100168
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100169/*****************************************************************************/
170/* Legacy format routines */
171/*****************************************************************************/
Stephen Warren712fbcf2011-10-18 11:11:49 +0000172int image_check_hcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100173{
174 ulong hcrc;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000175 ulong len = image_get_header_size();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100176 image_header_t header;
177
178 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000179 memmove(&header, (char *)hdr, image_get_header_size());
180 image_set_hcrc(&header, 0);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100181
Stephen Warren712fbcf2011-10-18 11:11:49 +0000182 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100183
Stephen Warren712fbcf2011-10-18 11:11:49 +0000184 return (hcrc == image_get_hcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100185}
186
Stephen Warren712fbcf2011-10-18 11:11:49 +0000187int image_check_dcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100188{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000189 ulong data = image_get_data(hdr);
190 ulong len = image_get_data_size(hdr);
191 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100192
Stephen Warren712fbcf2011-10-18 11:11:49 +0000193 return (dcrc == image_get_dcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100194}
195
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100196/**
197 * image_multi_count - get component (sub-image) count
198 * @hdr: pointer to the header of the multi component image
199 *
200 * image_multi_count() returns number of components in a multi
201 * component image.
202 *
203 * Note: no checking of the image type is done, caller must pass
204 * a valid multi component image.
205 *
206 * returns:
207 * number of components
208 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000209ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100210{
211 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100212 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100213
214 /* get start of the image payload, which in case of multi
215 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000216 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100217
218 /* count non empty slots */
219 for (i = 0; size[i]; ++i)
220 count++;
221
222 return count;
223}
224
225/**
226 * image_multi_getimg - get component data address and size
227 * @hdr: pointer to the header of the multi component image
228 * @idx: index of the requested component
229 * @data: pointer to a ulong variable, will hold component data address
230 * @len: pointer to a ulong variable, will hold component size
231 *
232 * image_multi_getimg() returns size and data address for the requested
233 * component in a multi component image.
234 *
235 * Note: no checking of the image type is done, caller must pass
236 * a valid multi component image.
237 *
238 * returns:
239 * data address and size of the component, if idx is valid
240 * 0 in data and len, if idx is out of range
241 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000242void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100243 ulong *data, ulong *len)
244{
245 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100246 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700247 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100248
249 /* get number of component */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000250 count = image_multi_count(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100251
252 /* get start of the image payload, which in case of multi
253 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000254 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100255
256 /* get address of the proper component data start, which means
257 * skipping sizes table (add 1 for last, null entry) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000258 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100259
260 if (idx < count) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000261 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100262 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100263
264 /* go over all indices preceding requested component idx */
265 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700266 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000267 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100268 }
269
270 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700271 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100272 } else {
273 *len = 0;
274 *data = 0;
275 }
276}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100277
Stephen Warren712fbcf2011-10-18 11:11:49 +0000278static void image_print_type(const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100279{
280 const char *os, *arch, *type, *comp;
281
Stephen Warren712fbcf2011-10-18 11:11:49 +0000282 os = genimg_get_os_name(image_get_os(hdr));
283 arch = genimg_get_arch_name(image_get_arch(hdr));
284 type = genimg_get_type_name(image_get_type(hdr));
285 comp = genimg_get_comp_name(image_get_comp(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100286
Stephen Warren712fbcf2011-10-18 11:11:49 +0000287 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100288}
289
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100290/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200291 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200292 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100293 * @p: pointer to prefix string
294 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200295 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100296 * The routine prints out all header fields followed by the size/offset data
297 * for MULTI/SCRIPT images.
298 *
299 * returns:
300 * no returned results
301 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000302void image_print_contents(const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100303{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200304 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200305 const char *p;
306
307#ifdef USE_HOSTCC
308 p = "";
309#else
310 p = " ";
311#endif
312
Stephen Warren712fbcf2011-10-18 11:11:49 +0000313 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100314#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000315 printf("%sCreated: ", p);
316 genimg_print_time((time_t)image_get_time(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100317#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +0000318 printf("%sImage Type: ", p);
319 image_print_type(hdr);
320 printf("%sData Size: ", p);
321 genimg_print_size(image_get_data_size(hdr));
322 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
323 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100324
Stephen Warren712fbcf2011-10-18 11:11:49 +0000325 if (image_check_type(hdr, IH_TYPE_MULTI) ||
326 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100327 int i;
328 ulong data, len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000329 ulong count = image_multi_count(hdr);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100330
Stephen Warren712fbcf2011-10-18 11:11:49 +0000331 printf("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100332 for (i = 0; i < count; i++) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000333 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100334
Stephen Warren712fbcf2011-10-18 11:11:49 +0000335 printf("%s Image %d: ", p, i);
336 genimg_print_size(len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100337
Stephen Warren712fbcf2011-10-18 11:11:49 +0000338 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100339 /*
340 * the user may need to know offsets
341 * if planning to do something with
342 * multiple files
343 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000344 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100345 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100346 }
347 }
348}
349
Marian Balakowicz570abb02008-02-29 15:59:59 +0100350
351#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100352/**
353 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100354 * @rd_addr: ramdisk image start address
355 * @arch: expected ramdisk architecture
356 * @verify: checksum verification flag
357 *
358 * image_get_ramdisk() returns a pointer to the verified ramdisk image
359 * header. Routine receives image start address and expected architecture
360 * flag. Verification done covers data and header integrity and os/type/arch
361 * fields checking.
362 *
363 * If dataflash support is enabled routine checks for dataflash addresses
364 * and handles required dataflash reads.
365 *
366 * returns:
367 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600368 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100369 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000370static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100371 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100372{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200373 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100374
Stephen Warren712fbcf2011-10-18 11:11:49 +0000375 if (!image_check_magic(rd_hdr)) {
376 puts("Bad Magic Number\n");
Simon Glass770605e2012-02-13 13:51:18 +0000377 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
Kumar Gala274cea22008-02-27 21:51:46 -0600378 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100379 }
380
Stephen Warren712fbcf2011-10-18 11:11:49 +0000381 if (!image_check_hcrc(rd_hdr)) {
382 puts("Bad Header Checksum\n");
Simon Glass770605e2012-02-13 13:51:18 +0000383 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600384 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100385 }
386
Simon Glass770605e2012-02-13 13:51:18 +0000387 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000388 image_print_contents(rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100389
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100390 if (verify) {
391 puts(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000392 if (!image_check_dcrc(rd_hdr)) {
393 puts("Bad Data CRC\n");
Simon Glass770605e2012-02-13 13:51:18 +0000394 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600395 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100396 }
397 puts("OK\n");
398 }
399
Simon Glass770605e2012-02-13 13:51:18 +0000400 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100401
Stephen Warren712fbcf2011-10-18 11:11:49 +0000402 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
403 !image_check_arch(rd_hdr, arch) ||
404 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
405 printf("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100406 genimg_get_arch_name(arch));
Simon Glass770605e2012-02-13 13:51:18 +0000407 bootstage_error(BOOTSTAGE_ID_RAMDISK);
Kumar Gala274cea22008-02-27 21:51:46 -0600408 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100409 }
410
411 return rd_hdr;
412}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100413#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100414
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100415/*****************************************************************************/
416/* Shared dual-format routines */
417/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100418#ifndef USE_HOSTCC
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -0600419ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
420ulong save_addr; /* Default Save Address */
421ulong save_size; /* Default Save Size (in bytes) */
422
423static int on_loadaddr(const char *name, const char *value, enum env_op op,
424 int flags)
425{
426 switch (op) {
427 case env_op_create:
428 case env_op_overwrite:
429 load_addr = simple_strtoul(value, NULL, 16);
430 break;
431 default:
432 break;
433 }
434
435 return 0;
436}
437U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
438
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100439ulong getenv_bootm_low(void)
440{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000441 char *s = getenv("bootm_low");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100442 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000443 ulong tmp = simple_strtoul(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100444 return tmp;
445 }
446
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200447#if defined(CONFIG_SYS_SDRAM_BASE)
448 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100449#elif defined(CONFIG_ARM)
450 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100451#else
452 return 0;
453#endif
454}
455
Becky Bruce391fd932008-06-09 20:37:18 -0500456phys_size_t getenv_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100457{
Matthew McClintockc519fac2010-07-08 10:11:08 -0500458 phys_size_t tmp;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000459 char *s = getenv("bootm_size");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100460 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000461 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100462 return tmp;
463 }
Matthew McClintockc519fac2010-07-08 10:11:08 -0500464 s = getenv("bootm_low");
465 if (s)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000466 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintockc519fac2010-07-08 10:11:08 -0500467 else
468 tmp = 0;
469
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100470
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100471#if defined(CONFIG_ARM)
Matthew McClintockc519fac2010-07-08 10:11:08 -0500472 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100473#else
Matthew McClintockc519fac2010-07-08 10:11:08 -0500474 return gd->bd->bi_memsize - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100475#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100476}
477
Grant Likelyc3624e62011-03-28 09:58:43 +0000478phys_size_t getenv_bootm_mapsize(void)
479{
480 phys_size_t tmp;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000481 char *s = getenv("bootm_mapsize");
Grant Likelyc3624e62011-03-28 09:58:43 +0000482 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000483 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likelyc3624e62011-03-28 09:58:43 +0000484 return tmp;
485 }
486
487#if defined(CONFIG_SYS_BOOTMAPSZ)
488 return CONFIG_SYS_BOOTMAPSZ;
489#else
490 return getenv_bootm_size();
491#endif
492}
493
Stephen Warren712fbcf2011-10-18 11:11:49 +0000494void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100495{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400496 if (to == from)
497 return;
498
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100499#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
500 while (len > 0) {
501 size_t tail = (len > chunksz) ? chunksz : len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000502 WATCHDOG_RESET();
503 memmove(to, from, tail);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100504 to += tail;
505 from += tail;
506 len -= tail;
507 }
508#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000509 memmove(to, from, len);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100510#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
511}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100512#endif /* !USE_HOSTCC */
513
Stephen Warren712fbcf2011-10-18 11:11:49 +0000514void genimg_print_size(uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100515{
516#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +0000517 printf("%d Bytes = ", size);
518 print_size(size, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100519#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000520 printf("%d Bytes = %.2f kB = %.2f MB\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100521 size, (double)size / 1.024e3,
522 (double)size / 1.048576e6);
523#endif
524}
525
526#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000527static void genimg_print_time(time_t timestamp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100528{
529#ifndef USE_HOSTCC
530 struct rtc_time tm;
531
Stephen Warren712fbcf2011-10-18 11:11:49 +0000532 to_tm(timestamp, &tm);
533 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100534 tm.tm_year, tm.tm_mon, tm.tm_mday,
535 tm.tm_hour, tm.tm_min, tm.tm_sec);
536#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000537 printf("%s", ctime(&timestamp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100538#endif
539}
540#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
541
542/**
543 * get_table_entry_name - translate entry id to long name
544 * @table: pointer to a translation table for entries of a specific type
545 * @msg: message to be returned when translation fails
546 * @id: entry id to be translated
547 *
548 * get_table_entry_name() will go over translation table trying to find
549 * entry that matches given id. If matching entry is found, its long
550 * name is returned to the caller.
551 *
552 * returns:
553 * long entry name if translation succeeds
554 * msg otherwise
555 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400556char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100557{
558 for (; table->id >= 0; ++table) {
559 if (table->id == id)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200560#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Woode3d1ac72009-04-02 16:15:10 -0500561 return table->lname;
562#else
563 return table->lname + gd->reloc_off;
564#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100565 }
566 return (msg);
567}
568
Stephen Warren712fbcf2011-10-18 11:11:49 +0000569const char *genimg_get_os_name(uint8_t os)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100570{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000571 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100572}
573
Stephen Warren712fbcf2011-10-18 11:11:49 +0000574const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100575{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000576 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
577 arch));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100578}
579
Stephen Warren712fbcf2011-10-18 11:11:49 +0000580const char *genimg_get_type_name(uint8_t type)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100581{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000582 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100583}
584
Stephen Warren712fbcf2011-10-18 11:11:49 +0000585const char *genimg_get_comp_name(uint8_t comp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100586{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000587 return (get_table_entry_name(uimage_comp, "Unknown Compression",
588 comp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100589}
590
591/**
592 * get_table_entry_id - translate short entry name to id
593 * @table: pointer to a translation table for entries of a specific type
594 * @table_name: to be used in case of error
595 * @name: entry short name to be translated
596 *
597 * get_table_entry_id() will go over translation table trying to find
598 * entry that matches given short name. If matching entry is found,
599 * its id returned to the caller.
600 *
601 * returns:
602 * entry id if translation succeeds
603 * -1 otherwise
604 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400605int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100606 const char *table_name, const char *name)
607{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400608 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100609#ifdef USE_HOSTCC
610 int first = 1;
611
612 for (t = table; t->id >= 0; ++t) {
613 if (t->sname && strcasecmp(t->sname, name) == 0)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000614 return(t->id);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100615 }
616
Stephen Warren712fbcf2011-10-18 11:11:49 +0000617 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100618 for (t = table; t->id >= 0; ++t) {
619 if (t->sname == NULL)
620 continue;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000621 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100622 first = 0;
623 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000624 fprintf(stderr, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100625#else
626 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200627#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Woode3d1ac72009-04-02 16:15:10 -0500628 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200629#else
630 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500631#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100632 return (t->id);
633 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000634 debug("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100635#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100636 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100637}
638
Stephen Warren712fbcf2011-10-18 11:11:49 +0000639int genimg_get_os_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100640{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000641 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100642}
643
Stephen Warren712fbcf2011-10-18 11:11:49 +0000644int genimg_get_arch_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100645{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000646 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100647}
648
Stephen Warren712fbcf2011-10-18 11:11:49 +0000649int genimg_get_type_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100650{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000651 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100652}
653
Stephen Warren712fbcf2011-10-18 11:11:49 +0000654int genimg_get_comp_id(const char *name)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100655{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000656 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100657}
658
659#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100660/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100661 * genimg_get_format - get image format type
662 * @img_addr: image start address
663 *
664 * genimg_get_format() checks whether provided address points to a valid
665 * legacy or FIT image.
666 *
667 * New uImage format and FDT blob are based on a libfdt. FDT blob
668 * may be passed directly or embedded in a FIT image. In both situations
669 * genimg_get_format() must be able to dectect libfdt header.
670 *
671 * returns:
672 * image format type or IMAGE_FORMAT_INVALID if no image is present
673 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000674int genimg_get_format(void *img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100675{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200676 ulong format = IMAGE_FORMAT_INVALID;
677 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100678#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200679 char *fit_hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100680#endif
681
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200682 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100683 if (image_check_magic(hdr))
684 format = IMAGE_FORMAT_LEGACY;
685#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
686 else {
687 fit_hdr = (char *)img_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000688 if (fdt_check_header(fit_hdr) == 0)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100689 format = IMAGE_FORMAT_FIT;
690 }
691#endif
692
693 return format;
694}
695
696/**
697 * genimg_get_image - get image from special storage (if necessary)
698 * @img_addr: image start address
699 *
700 * genimg_get_image() checks if provided image start adddress is located
701 * in a dataflash storage. If so, image is moved to a system RAM memory.
702 *
703 * returns:
704 * image start address after possible relocation from special storage
705 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000706ulong genimg_get_image(ulong img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100707{
708 ulong ram_addr = img_addr;
709
710#ifdef CONFIG_HAS_DATAFLASH
711 ulong h_size, d_size;
712
Stephen Warren712fbcf2011-10-18 11:11:49 +0000713 if (addr_dataflash(img_addr)) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100714 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200715 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100716
717 /* get header size */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000718 h_size = image_get_header_size();
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100719#if defined(CONFIG_FIT)
720 if (sizeof(struct fdt_header) > h_size)
721 h_size = sizeof(struct fdt_header);
722#endif
723
724 /* read in header */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000725 debug(" Reading image header from dataflash address "
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100726 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
727
Stephen Warren712fbcf2011-10-18 11:11:49 +0000728 read_dataflash(img_addr, h_size, (char *)ram_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100729
730 /* get data size */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000731 switch (genimg_get_format((void *)ram_addr)) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100732 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000733 d_size = image_get_data_size(
734 (const image_header_t *)ram_addr);
735 debug(" Legacy format image found at 0x%08lx, "
736 "size 0x%08lx\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100737 ram_addr, d_size);
738 break;
739#if defined(CONFIG_FIT)
740 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000741 d_size = fit_get_size((const void *)ram_addr) - h_size;
742 debug(" FIT/FDT format image found at 0x%08lx, "
743 "size 0x%08lx\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100744 ram_addr, d_size);
745 break;
746#endif
747 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000748 printf(" No valid image found at 0x%08lx\n",
749 img_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100750 return ram_addr;
751 }
752
753 /* read in image data */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000754 debug(" Reading image remaining data from dataflash address "
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100755 "%08lx to RAM address %08lx\n", img_addr + h_size,
756 ram_addr + h_size);
757
Stephen Warren712fbcf2011-10-18 11:11:49 +0000758 read_dataflash(img_addr + h_size, d_size,
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100759 (char *)(ram_addr + h_size));
760
761 }
762#endif /* CONFIG_HAS_DATAFLASH */
763
764 return ram_addr;
765}
766
767/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100768 * fit_has_config - check if there is a valid FIT configuration
769 * @images: pointer to the bootm command headers structure
770 *
771 * fit_has_config() checks if there is a FIT configuration in use
772 * (if FTI support is present).
773 *
774 * returns:
775 * 0, no FIT support or no configuration found
776 * 1, configuration found
777 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000778int genimg_has_config(bootm_headers_t *images)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100779{
780#if defined(CONFIG_FIT)
781 if (images->fit_uname_cfg)
782 return 1;
783#endif
784 return 0;
785}
786
787/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100788 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100789 * @argc: command argument count
790 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100791 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100792 * @arch: expected ramdisk architecture
793 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
794 * @rd_end: pointer to a ulong variable, will hold ramdisk end
795 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100796 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100797 * Curently supported are the following ramdisk sources:
798 * - multicomponent kernel/ramdisk image,
799 * - commandline provided address of decicated ramdisk image.
800 *
801 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100802 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100803 * rd_start and rd_end are set to ramdisk start/end addresses if
804 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100805 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500806 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100807 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100808 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000809int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100810 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100811{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100812 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100813 ulong rd_data, rd_len;
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200814 const image_header_t *rd_hdr;
Marek Vasut57d40ab2012-03-30 23:19:10 +0200815#ifdef CONFIG_SUPPORT_RAW_INITRD
Marek Vasut017e1f32012-03-18 11:47:58 +0000816 char *end;
Marek Vasut57d40ab2012-03-30 23:19:10 +0200817#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100818#if defined(CONFIG_FIT)
819 void *fit_hdr;
820 const char *fit_uname_config = NULL;
821 const char *fit_uname_ramdisk = NULL;
822 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100823 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100824 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100825 const void *data;
826 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100827#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100828
Marian Balakowiczc8779642008-03-12 10:12:37 +0100829 *rd_start = 0;
830 *rd_end = 0;
831
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100832 /*
833 * Look for a '-' which indicates to ignore the
834 * ramdisk argument
835 */
836 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000837 debug("## Skipping init Ramdisk\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100838 rd_len = rd_data = 0;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000839 } else if (argc >= 3 || genimg_has_config(images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100840#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100841 if (argc >= 3) {
842 /*
843 * If the init ramdisk comes from the FIT image and
844 * the FIT image address is omitted in the command
845 * line argument, try to use os FIT image address or
846 * default load address.
847 */
848 if (images->fit_uname_os)
849 default_addr = (ulong)images->fit_hdr_os;
850 else
851 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100852
Stephen Warren712fbcf2011-10-18 11:11:49 +0000853 if (fit_parse_conf(argv[2], default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100854 &rd_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000855 debug("* ramdisk: config '%s' from image at "
856 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100857 fit_uname_config, rd_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000858 } else if (fit_parse_subimage(argv[2], default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100859 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000860 debug("* ramdisk: subimage '%s' from image at "
861 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100862 fit_uname_ramdisk, rd_addr);
863 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100864#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100865 {
866 rd_addr = simple_strtoul(argv[2], NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000867 debug("* ramdisk: cmdline image address = "
868 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100869 rd_addr);
870 }
871#if defined(CONFIG_FIT)
872 } else {
873 /* use FIT configuration provided in first bootm
874 * command argument
875 */
876 rd_addr = (ulong)images->fit_hdr_os;
877 fit_uname_config = images->fit_uname_cfg;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000878 debug("* ramdisk: using config '%s' from image "
879 "at 0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100880 fit_uname_config, rd_addr);
881
882 /*
883 * Check whether configuration has ramdisk defined,
884 * if not, don't try to use it, quit silently.
885 */
886 fit_hdr = (void *)rd_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000887 cfg_noffset = fit_conf_get_node(fit_hdr,
888 fit_uname_config);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100889 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000890 debug("* ramdisk: no such config\n");
Michal Simekc78fce62008-07-11 10:43:13 +0200891 return 1;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100892 }
893
Stephen Warren712fbcf2011-10-18 11:11:49 +0000894 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
895 cfg_noffset);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100896 if (rd_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000897 debug("* ramdisk: no ramdisk in config\n");
Peter Tyser41266c92008-08-05 10:51:57 -0500898 return 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100899 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100900 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100901#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100902
903 /* copy from dataflash if needed */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000904 rd_addr = genimg_get_image(rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100905
906 /*
907 * Check if there is an initrd image at the
908 * address provided in the second bootm argument
909 * check image type, for FIT images get FIT node.
910 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000911 switch (genimg_get_format((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100912 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000913 printf("## Loading init Ramdisk from Legacy "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100914 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100915
Simon Glass770605e2012-02-13 13:51:18 +0000916 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000917 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100918 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100919
Marian Balakowiczc8779642008-03-12 10:12:37 +0100920 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600921 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600922
Stephen Warren712fbcf2011-10-18 11:11:49 +0000923 rd_data = image_get_data(rd_hdr);
924 rd_len = image_get_data_size(rd_hdr);
925 rd_load = image_get_load(rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100926 break;
927#if defined(CONFIG_FIT)
928 case IMAGE_FORMAT_FIT:
929 fit_hdr = (void *)rd_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000930 printf("## Loading init Ramdisk from FIT "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100931 "Image at %08lx ...\n", rd_addr);
932
Simon Glass770605e2012-02-13 13:51:18 +0000933 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000934 if (!fit_check_format(fit_hdr)) {
935 puts("Bad FIT ramdisk image format!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000936 bootstage_error(
Simon Glassaacc8c12011-12-10 11:08:02 +0000937 BOOTSTAGE_ID_FIT_RD_FORMAT);
Michal Simekc78fce62008-07-11 10:43:13 +0200938 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100939 }
Simon Glass770605e2012-02-13 13:51:18 +0000940 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100941
942 if (!fit_uname_ramdisk) {
943 /*
944 * no ramdisk image node unit name, try to get config
945 * node first. If config unit node name is NULL
946 * fit_conf_get_node() will try to find default config node
947 */
Simon Glass770605e2012-02-13 13:51:18 +0000948 bootstage_mark(
Simon Glassaacc8c12011-12-10 11:08:02 +0000949 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000950 cfg_noffset = fit_conf_get_node(fit_hdr,
951 fit_uname_config);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100952 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000953 puts("Could not find configuration "
954 "node\n");
Simon Glass770605e2012-02-13 13:51:18 +0000955 bootstage_error(
Simon Glassaacc8c12011-12-10 11:08:02 +0000956 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
Michal Simekc78fce62008-07-11 10:43:13 +0200957 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100958 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000959 fit_uname_config = fdt_get_name(fit_hdr,
960 cfg_noffset, NULL);
961 printf(" Using '%s' configuration\n",
962 fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100963
Stephen Warren712fbcf2011-10-18 11:11:49 +0000964 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
965 cfg_noffset);
966 fit_uname_ramdisk = fit_get_name(fit_hdr,
967 rd_noffset, NULL);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100968 } else {
969 /* get ramdisk component image node offset */
Simon Glass770605e2012-02-13 13:51:18 +0000970 bootstage_mark(
Simon Glassaacc8c12011-12-10 11:08:02 +0000971 BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000972 rd_noffset = fit_image_get_node(fit_hdr,
973 fit_uname_ramdisk);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100974 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100975 if (rd_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000976 puts("Could not find subimage node\n");
Simon Glass770605e2012-02-13 13:51:18 +0000977 bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
Michal Simekc78fce62008-07-11 10:43:13 +0200978 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100979 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100980
Stephen Warren712fbcf2011-10-18 11:11:49 +0000981 printf(" Trying '%s' ramdisk subimage\n",
982 fit_uname_ramdisk);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100983
Simon Glass770605e2012-02-13 13:51:18 +0000984 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000985 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
986 images->verify))
Michal Simekc78fce62008-07-11 10:43:13 +0200987 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100988
989 /* get ramdisk image data address and length */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000990 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
991 &size)) {
992 puts("Could not find ramdisk subimage data!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000993 bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
Michal Simekc78fce62008-07-11 10:43:13 +0200994 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100995 }
Simon Glass770605e2012-02-13 13:51:18 +0000996 bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100997
998 rd_data = (ulong)data;
999 rd_len = size;
1000
Stephen Warren712fbcf2011-10-18 11:11:49 +00001001 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
1002 puts("Can't get ramdisk subimage load "
1003 "address!\n");
Simon Glass770605e2012-02-13 13:51:18 +00001004 bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
Michal Simekc78fce62008-07-11 10:43:13 +02001005 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001006 }
Simon Glass770605e2012-02-13 13:51:18 +00001007 bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
Marian Balakowiczc8779642008-03-12 10:12:37 +01001008
1009 images->fit_hdr_rd = fit_hdr;
1010 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +01001011 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001012 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001013#endif
1014 default:
Marek Vasut017e1f32012-03-18 11:47:58 +00001015#ifdef CONFIG_SUPPORT_RAW_INITRD
1016 if (argc >= 3 && (end = strchr(argv[2], ':'))) {
1017 rd_len = simple_strtoul(++end, NULL, 16);
1018 rd_data = rd_addr;
1019 } else
1020#endif
1021 {
1022 puts("Wrong Ramdisk Image Format\n");
1023 rd_data = rd_len = rd_load = 0;
1024 return 1;
1025 }
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001026 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001027 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +00001028 image_check_type(&images->legacy_hdr_os_copy,
1029 IH_TYPE_MULTI)) {
1030
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001031 /*
1032 * Now check if we have a legacy mult-component image,
1033 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001034 */
Simon Glass770605e2012-02-13 13:51:18 +00001035 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001036 printf("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001037 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001038 (ulong)images->legacy_hdr_os);
1039
Stephen Warren712fbcf2011-10-18 11:11:49 +00001040 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001041 } else {
1042 /*
1043 * no initrd image
1044 */
Simon Glass770605e2012-02-13 13:51:18 +00001045 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001046 rd_len = rd_data = 0;
1047 }
1048
1049 if (!rd_data) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001050 debug("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001051 } else {
1052 *rd_start = rd_data;
1053 *rd_end = rd_data + rd_len;
1054 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001055 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001056 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -06001057
1058 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001059}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001060
John Rigbyfca43cc2010-10-13 13:57:35 -06001061#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001062/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001063 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001064 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001065 * @rd_data: ramdisk data start address
1066 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001067 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1068 * start address (after possible relocation)
1069 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1070 * end address (after possible relocation)
1071 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001072 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001073 * variable and if requested ramdisk data is moved to a specified location.
1074 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001075 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1076 * start/end addresses if ramdisk image start and len were provided,
1077 * otherwise set initrd_start and initrd_end set to zeros.
1078 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001079 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001080 * 0 - success
1081 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001082 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001083int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001084 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001085{
1086 char *s;
1087 ulong initrd_high;
1088 int initrd_copy_to_ram = 1;
1089
Stephen Warren712fbcf2011-10-18 11:11:49 +00001090 if ((s = getenv("initrd_high")) != NULL) {
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001091 /* a value of "no" or a similar string will act like 0,
1092 * turning the "load high" feature off. This is intentional.
1093 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001094 initrd_high = simple_strtoul(s, NULL, 16);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001095 if (initrd_high == ~0)
1096 initrd_copy_to_ram = 0;
1097 } else {
1098 /* not set, no restrictions to load high */
1099 initrd_high = ~0;
1100 }
1101
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001102
1103#ifdef CONFIG_LOGBUFFER
1104 /* Prevent initrd from overwriting logbuffer */
1105 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1106#endif
1107
Stephen Warren712fbcf2011-10-18 11:11:49 +00001108 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001109 initrd_high, initrd_copy_to_ram);
1110
1111 if (rd_data) {
1112 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001113 debug(" in-place initrd\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001114 *initrd_start = rd_data;
1115 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001116 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001117 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001118 if (initrd_high)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001119 *initrd_start = (ulong)lmb_alloc_base(lmb,
1120 rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001121 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001122 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1123 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001124
Kumar Galae822d7f2008-02-27 21:51:49 -06001125 if (*initrd_start == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001126 puts("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001127 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001128 }
Simon Glass770605e2012-02-13 13:51:18 +00001129 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001130
1131 *initrd_end = *initrd_start + rd_len;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001132 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001133 *initrd_start, *initrd_end);
1134
Stephen Warren712fbcf2011-10-18 11:11:49 +00001135 memmove_wd((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001136 (void *)rd_data, rd_len, CHUNKSZ);
1137
Kumar Gala3b200112011-12-07 04:42:58 +00001138#ifdef CONFIG_MP
1139 /*
1140 * Ensure the image is flushed to memory to handle
1141 * AMP boot scenarios in which we might not be
1142 * HW cache coherent
1143 */
1144 flush_cache((unsigned long)*initrd_start, rd_len);
1145#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +00001146 puts("OK\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001147 }
1148 } else {
1149 *initrd_start = 0;
1150 *initrd_end = 0;
1151 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001152 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001153 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001154
Kumar Galae822d7f2008-02-27 21:51:49 -06001155 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001156
Kumar Galae822d7f2008-02-27 21:51:49 -06001157error:
1158 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001159}
John Rigbyfca43cc2010-10-13 13:57:35 -06001160#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001161
Kumar Gala06a09912008-08-15 08:24:38 -05001162#ifdef CONFIG_OF_LIBFDT
Stephen Warren712fbcf2011-10-18 11:11:49 +00001163static void fdt_error(const char *msg)
Kumar Gala06a09912008-08-15 08:24:38 -05001164{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001165 puts("ERROR: ");
1166 puts(msg);
1167 puts(" - must RESET the board to recover.\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001168}
1169
Stephen Warren712fbcf2011-10-18 11:11:49 +00001170static const image_header_t *image_get_fdt(ulong fdt_addr)
Kumar Gala06a09912008-08-15 08:24:38 -05001171{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001172 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001173
Stephen Warren712fbcf2011-10-18 11:11:49 +00001174 image_print_contents(fdt_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001175
Stephen Warren712fbcf2011-10-18 11:11:49 +00001176 puts(" Verifying Checksum ... ");
1177 if (!image_check_hcrc(fdt_hdr)) {
1178 fdt_error("fdt header checksum invalid");
Kumar Gala06a09912008-08-15 08:24:38 -05001179 return NULL;
1180 }
1181
Stephen Warren712fbcf2011-10-18 11:11:49 +00001182 if (!image_check_dcrc(fdt_hdr)) {
1183 fdt_error("fdt checksum invalid");
Kumar Gala06a09912008-08-15 08:24:38 -05001184 return NULL;
1185 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001186 puts("OK\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001187
Stephen Warren712fbcf2011-10-18 11:11:49 +00001188 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1189 fdt_error("uImage is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001190 return NULL;
1191 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001192 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1193 fdt_error("uImage is compressed");
Kumar Gala06a09912008-08-15 08:24:38 -05001194 return NULL;
1195 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001196 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1197 fdt_error("uImage data is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001198 return NULL;
1199 }
1200 return fdt_hdr;
1201}
1202
1203/**
1204 * fit_check_fdt - verify FIT format FDT subimage
1205 * @fit_hdr: pointer to the FIT header
1206 * fdt_noffset: FDT subimage node offset within FIT image
1207 * @verify: data CRC verification flag
1208 *
1209 * fit_check_fdt() verifies integrity of the FDT subimage and from
1210 * specified FIT image.
1211 *
1212 * returns:
1213 * 1, on success
1214 * 0, on failure
1215 */
1216#if defined(CONFIG_FIT)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001217static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
Kumar Gala06a09912008-08-15 08:24:38 -05001218{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001219 fit_image_print(fit, fdt_noffset, " ");
Kumar Gala06a09912008-08-15 08:24:38 -05001220
1221 if (verify) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001222 puts(" Verifying Hash Integrity ... ");
1223 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1224 fdt_error("Bad Data Hash");
Kumar Gala06a09912008-08-15 08:24:38 -05001225 return 0;
1226 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001227 puts("OK\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001228 }
1229
Stephen Warren712fbcf2011-10-18 11:11:49 +00001230 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1231 fdt_error("Not a FDT image");
Kumar Gala06a09912008-08-15 08:24:38 -05001232 return 0;
1233 }
1234
Stephen Warren712fbcf2011-10-18 11:11:49 +00001235 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1236 fdt_error("FDT image is compressed");
Kumar Gala06a09912008-08-15 08:24:38 -05001237 return 0;
1238 }
1239
1240 return 1;
1241}
1242#endif /* CONFIG_FIT */
1243
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001244#ifndef CONFIG_SYS_FDT_PAD
1245#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Gala06a09912008-08-15 08:24:38 -05001246#endif
1247
Grant Likely55b0a392011-03-28 09:59:01 +00001248#if defined(CONFIG_OF_LIBFDT)
1249/**
1250 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1251 * @lmb: pointer to lmb handle, will be used for memory mgmt
1252 * @fdt_blob: pointer to fdt blob base address
1253 *
1254 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1255 * memreserve regions prevents u-boot from using them to store the initrd
1256 * or the fdt blob.
1257 */
1258void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1259{
1260 uint64_t addr, size;
1261 int i, total;
1262
Stephen Warren712fbcf2011-10-18 11:11:49 +00001263 if (fdt_check_header(fdt_blob) != 0)
Grant Likely55b0a392011-03-28 09:59:01 +00001264 return;
1265
1266 total = fdt_num_mem_rsv(fdt_blob);
1267 for (i = 0; i < total; i++) {
1268 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1269 continue;
1270 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1271 (unsigned long long)addr, (unsigned long long)size);
1272 lmb_reserve(lmb, addr, size);
1273 }
1274}
1275
Kumar Gala06a09912008-08-15 08:24:38 -05001276/**
1277 * boot_relocate_fdt - relocate flat device tree
1278 * @lmb: pointer to lmb handle, will be used for memory mgmt
Kumar Gala06a09912008-08-15 08:24:38 -05001279 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1280 * @of_size: pointer to a ulong variable, will hold fdt length
1281 *
Timur Tabi43b08af2010-05-24 15:10:25 -05001282 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1283 * relocates the of_flat_tree into that region, even if the fdt is already in
1284 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1285 * bytes.
Kumar Gala06a09912008-08-15 08:24:38 -05001286 *
1287 * of_flat_tree and of_size are set to final (after relocation) values
1288 *
1289 * returns:
1290 * 0 - success
1291 * 1 - failure
1292 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001293int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001294{
Timur Tabi43b08af2010-05-24 15:10:25 -05001295 void *fdt_blob = *of_flat_tree;
Kim Phillips199adb62012-10-29 13:34:32 +00001296 void *of_start = NULL;
David A. Longa28afca2011-07-09 16:40:19 -04001297 char *fdt_high;
Kumar Gala06a09912008-08-15 08:24:38 -05001298 ulong of_len = 0;
Timur Tabi43b08af2010-05-24 15:10:25 -05001299 int err;
David A. Longa28afca2011-07-09 16:40:19 -04001300 int disable_relocation = 0;
Kumar Gala06a09912008-08-15 08:24:38 -05001301
1302 /* nothing to do */
1303 if (*of_size == 0)
1304 return 0;
1305
Stephen Warren712fbcf2011-10-18 11:11:49 +00001306 if (fdt_check_header(fdt_blob) != 0) {
1307 fdt_error("image is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001308 goto error;
1309 }
1310
Timur Tabi43b08af2010-05-24 15:10:25 -05001311 /* position on a 4K boundary before the alloc_current */
1312 /* Pad the FDT by a specified amount */
1313 of_len = *of_size + CONFIG_SYS_FDT_PAD;
David A. Longa28afca2011-07-09 16:40:19 -04001314
1315 /* If fdt_high is set use it to select the relocation address */
1316 fdt_high = getenv("fdt_high");
1317 if (fdt_high) {
1318 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1319
1320 if (((ulong) desired_addr) == ~0UL) {
1321 /* All ones means use fdt in place */
Shawn Guofa34f6b2012-01-09 21:54:08 +00001322 of_start = fdt_blob;
1323 lmb_reserve(lmb, (ulong)of_start, of_len);
David A. Longa28afca2011-07-09 16:40:19 -04001324 disable_relocation = 1;
Shawn Guofa34f6b2012-01-09 21:54:08 +00001325 } else if (desired_addr) {
David A. Longa28afca2011-07-09 16:40:19 -04001326 of_start =
1327 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
Shawn Guofa34f6b2012-01-09 21:54:08 +00001328 (ulong)desired_addr);
Kim Phillips199adb62012-10-29 13:34:32 +00001329 if (of_start == NULL) {
David A. Longa28afca2011-07-09 16:40:19 -04001330 puts("Failed using fdt_high value for Device Tree");
1331 goto error;
1332 }
1333 } else {
1334 of_start =
Matthew McClintock1bb5e902011-07-18 13:08:05 +00001335 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
David A. Longa28afca2011-07-09 16:40:19 -04001336 }
1337 } else {
1338 of_start =
1339 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1340 getenv_bootm_mapsize()
1341 + getenv_bootm_low());
1342 }
Kumar Gala06a09912008-08-15 08:24:38 -05001343
Kim Phillips199adb62012-10-29 13:34:32 +00001344 if (of_start == NULL) {
Timur Tabi43b08af2010-05-24 15:10:25 -05001345 puts("device tree - allocation error\n");
1346 goto error;
Kumar Gala06a09912008-08-15 08:24:38 -05001347 }
1348
David A. Longa28afca2011-07-09 16:40:19 -04001349 if (disable_relocation) {
1350 /* We assume there is space after the existing fdt to use for padding */
1351 fdt_set_totalsize(of_start, of_len);
1352 printf(" Using Device Tree in place at %p, end %p\n",
1353 of_start, of_start + of_len - 1);
1354 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001355 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
David A. Longa28afca2011-07-09 16:40:19 -04001356 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
Timur Tabi43b08af2010-05-24 15:10:25 -05001357
Stephen Warren712fbcf2011-10-18 11:11:49 +00001358 printf(" Loading Device Tree to %p, end %p ... ",
David A. Longa28afca2011-07-09 16:40:19 -04001359 of_start, of_start + of_len - 1);
Timur Tabi43b08af2010-05-24 15:10:25 -05001360
Stephen Warren712fbcf2011-10-18 11:11:49 +00001361 err = fdt_open_into(fdt_blob, of_start, of_len);
David A. Longa28afca2011-07-09 16:40:19 -04001362 if (err != 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001363 fdt_error("fdt move failed");
David A. Longa28afca2011-07-09 16:40:19 -04001364 goto error;
1365 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001366 puts("OK\n");
Timur Tabi43b08af2010-05-24 15:10:25 -05001367 }
Timur Tabi43b08af2010-05-24 15:10:25 -05001368
1369 *of_flat_tree = of_start;
1370 *of_size = of_len;
1371
Kumar Gala54f9c862008-08-15 08:24:39 -05001372 set_working_fdt_addr(*of_flat_tree);
Kumar Gala06a09912008-08-15 08:24:38 -05001373 return 0;
1374
1375error:
1376 return 1;
1377}
Grant Likelyed59e582011-03-28 09:58:49 +00001378#endif /* CONFIG_OF_LIBFDT */
Kumar Gala06a09912008-08-15 08:24:38 -05001379
1380/**
1381 * boot_get_fdt - main fdt handling routine
1382 * @argc: command argument count
1383 * @argv: command argument list
1384 * @images: pointer to the bootm images structure
1385 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1386 * @of_size: pointer to a ulong variable, will hold fdt length
1387 *
1388 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1389 * Curently supported are the following ramdisk sources:
1390 * - multicomponent kernel/ramdisk image,
1391 * - commandline provided address of decicated ramdisk image.
1392 *
1393 * returns:
1394 * 0, if fdt image was found and valid, or skipped
1395 * of_flat_tree and of_size are set to fdt start address and length if
1396 * fdt image is found and valid
1397 *
1398 * 1, if fdt image is found but corrupted
1399 * of_flat_tree and of_size are set to 0 if no fdt exists
1400 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001401int boot_get_fdt(int flag, int argc, char * const argv[],
1402 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001403{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001404 const image_header_t *fdt_hdr;
Kumar Gala06a09912008-08-15 08:24:38 -05001405 ulong fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001406 char *fdt_blob = NULL;
Stephen Warrene37ae402011-11-01 06:28:21 +00001407 ulong image_start, image_data, image_end;
Kumar Gala06a09912008-08-15 08:24:38 -05001408 ulong load_start, load_end;
1409#if defined(CONFIG_FIT)
1410 void *fit_hdr;
1411 const char *fit_uname_config = NULL;
1412 const char *fit_uname_fdt = NULL;
1413 ulong default_addr;
1414 int cfg_noffset;
1415 int fdt_noffset;
1416 const void *data;
1417 size_t size;
1418#endif
1419
1420 *of_flat_tree = NULL;
1421 *of_size = 0;
1422
Stephen Warren712fbcf2011-10-18 11:11:49 +00001423 if (argc > 3 || genimg_has_config(images)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001424#if defined(CONFIG_FIT)
1425 if (argc > 3) {
1426 /*
1427 * If the FDT blob comes from the FIT image and the
1428 * FIT image address is omitted in the command line
1429 * argument, try to use ramdisk or os FIT image
1430 * address or default load address.
1431 */
1432 if (images->fit_uname_rd)
1433 default_addr = (ulong)images->fit_hdr_rd;
1434 else if (images->fit_uname_os)
1435 default_addr = (ulong)images->fit_hdr_os;
1436 else
1437 default_addr = load_addr;
1438
Stephen Warren712fbcf2011-10-18 11:11:49 +00001439 if (fit_parse_conf(argv[3], default_addr,
Kumar Gala06a09912008-08-15 08:24:38 -05001440 &fdt_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001441 debug("* fdt: config '%s' from image at "
1442 "0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001443 fit_uname_config, fdt_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001444 } else if (fit_parse_subimage(argv[3], default_addr,
Kumar Gala06a09912008-08-15 08:24:38 -05001445 &fdt_addr, &fit_uname_fdt)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001446 debug("* fdt: subimage '%s' from image at "
1447 "0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001448 fit_uname_fdt, fdt_addr);
1449 } else
1450#endif
1451 {
1452 fdt_addr = simple_strtoul(argv[3], NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001453 debug("* fdt: cmdline image address = "
1454 "0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001455 fdt_addr);
1456 }
1457#if defined(CONFIG_FIT)
1458 } else {
1459 /* use FIT configuration provided in first bootm
1460 * command argument
1461 */
1462 fdt_addr = (ulong)images->fit_hdr_os;
1463 fit_uname_config = images->fit_uname_cfg;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001464 debug("* fdt: using config '%s' from image "
1465 "at 0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001466 fit_uname_config, fdt_addr);
1467
1468 /*
1469 * Check whether configuration has FDT blob defined,
1470 * if not quit silently.
1471 */
1472 fit_hdr = (void *)fdt_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001473 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001474 fit_uname_config);
1475 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001476 debug("* fdt: no such config\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001477 return 0;
1478 }
1479
Stephen Warren712fbcf2011-10-18 11:11:49 +00001480 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001481 cfg_noffset);
1482 if (fdt_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001483 debug("* fdt: no fdt in config\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001484 return 0;
1485 }
1486 }
1487#endif
1488
Stephen Warren712fbcf2011-10-18 11:11:49 +00001489 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001490 fdt_addr);
1491
1492 /* copy from dataflash if needed */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001493 fdt_addr = genimg_get_image(fdt_addr);
Kumar Gala06a09912008-08-15 08:24:38 -05001494
1495 /*
1496 * Check if there is an FDT image at the
1497 * address provided in the second bootm argument
1498 * check image type, for FIT images get a FIT node.
1499 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001500 switch (genimg_get_format((void *)fdt_addr)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001501 case IMAGE_FORMAT_LEGACY:
1502 /* verify fdt_addr points to a valid image header */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001503 printf("## Flattened Device Tree from Legacy Image "
1504 "at %08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001505 fdt_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001506 fdt_hdr = image_get_fdt(fdt_addr);
Kumar Gala06a09912008-08-15 08:24:38 -05001507 if (!fdt_hdr)
1508 goto error;
1509
1510 /*
1511 * move image data to the load address,
1512 * make sure we don't overwrite initial image
1513 */
1514 image_start = (ulong)fdt_hdr;
Stephen Warrene37ae402011-11-01 06:28:21 +00001515 image_data = (ulong)image_get_data(fdt_hdr);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001516 image_end = image_get_image_end(fdt_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001517
Stephen Warren712fbcf2011-10-18 11:11:49 +00001518 load_start = image_get_load(fdt_hdr);
1519 load_end = load_start + image_get_data_size(fdt_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001520
Stephen Warrene37ae402011-11-01 06:28:21 +00001521 if (load_start == image_start ||
1522 load_start == image_data) {
1523 fdt_blob = (char *)image_data;
1524 break;
1525 }
1526
Kumar Gala06a09912008-08-15 08:24:38 -05001527 if ((load_start < image_end) && (load_end > image_start)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001528 fdt_error("fdt overwritten");
Kumar Gala06a09912008-08-15 08:24:38 -05001529 goto error;
1530 }
1531
Stephen Warren712fbcf2011-10-18 11:11:49 +00001532 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
Stephen Warrene37ae402011-11-01 06:28:21 +00001533 image_data, load_start);
Kumar Gala06a09912008-08-15 08:24:38 -05001534
Stephen Warren712fbcf2011-10-18 11:11:49 +00001535 memmove((void *)load_start,
Stephen Warrene37ae402011-11-01 06:28:21 +00001536 (void *)image_data,
Stephen Warren712fbcf2011-10-18 11:11:49 +00001537 image_get_data_size(fdt_hdr));
Kumar Gala06a09912008-08-15 08:24:38 -05001538
1539 fdt_blob = (char *)load_start;
1540 break;
1541 case IMAGE_FORMAT_FIT:
1542 /*
1543 * This case will catch both: new uImage format
1544 * (libfdt based) and raw FDT blob (also libfdt
1545 * based).
1546 */
1547#if defined(CONFIG_FIT)
1548 /* check FDT blob vs FIT blob */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001549 if (fit_check_format((const void *)fdt_addr)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001550 /*
1551 * FIT image
1552 */
1553 fit_hdr = (void *)fdt_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001554 printf("## Flattened Device Tree from FIT "
1555 "Image at %08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001556 fdt_addr);
1557
1558 if (!fit_uname_fdt) {
1559 /*
1560 * no FDT blob image node unit name,
1561 * try to get config node first. If
1562 * config unit node name is NULL
1563 * fit_conf_get_node() will try to
1564 * find default config node
1565 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001566 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001567 fit_uname_config);
1568
1569 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001570 fdt_error("Could not find "
1571 "configuration "
1572 "node\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001573 goto error;
1574 }
1575
Stephen Warren712fbcf2011-10-18 11:11:49 +00001576 fit_uname_config = fdt_get_name(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001577 cfg_noffset, NULL);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001578 printf(" Using '%s' configuration\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001579 fit_uname_config);
1580
Stephen Warren712fbcf2011-10-18 11:11:49 +00001581 fdt_noffset = fit_conf_get_fdt_node(
1582 fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001583 cfg_noffset);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001584 fit_uname_fdt = fit_get_name(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001585 fdt_noffset, NULL);
1586 } else {
1587 /* get FDT component image node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001588 fdt_noffset = fit_image_get_node(
1589 fit_hdr,
1590 fit_uname_fdt);
Kumar Gala06a09912008-08-15 08:24:38 -05001591 }
1592 if (fdt_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001593 fdt_error("Could not find subimage "
1594 "node\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001595 goto error;
1596 }
1597
Stephen Warren712fbcf2011-10-18 11:11:49 +00001598 printf(" Trying '%s' FDT blob subimage\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001599 fit_uname_fdt);
1600
Stephen Warren712fbcf2011-10-18 11:11:49 +00001601 if (!fit_check_fdt(fit_hdr, fdt_noffset,
Kumar Gala06a09912008-08-15 08:24:38 -05001602 images->verify))
1603 goto error;
1604
1605 /* get ramdisk image data address and length */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001606 if (fit_image_get_data(fit_hdr, fdt_noffset,
Kumar Gala06a09912008-08-15 08:24:38 -05001607 &data, &size)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001608 fdt_error("Could not find FDT "
1609 "subimage data");
Kumar Gala06a09912008-08-15 08:24:38 -05001610 goto error;
1611 }
1612
1613 /* verift that image data is a proper FDT blob */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001614 if (fdt_check_header((char *)data) != 0) {
1615 fdt_error("Subimage data is not a FTD");
Kumar Gala06a09912008-08-15 08:24:38 -05001616 goto error;
1617 }
1618
1619 /*
1620 * move image data to the load address,
1621 * make sure we don't overwrite initial image
1622 */
1623 image_start = (ulong)fit_hdr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001624 image_end = fit_get_end(fit_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001625
Stephen Warren712fbcf2011-10-18 11:11:49 +00001626 if (fit_image_get_load(fit_hdr, fdt_noffset,
Kumar Gala06a09912008-08-15 08:24:38 -05001627 &load_start) == 0) {
1628 load_end = load_start + size;
1629
1630 if ((load_start < image_end) &&
1631 (load_end > image_start)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001632 fdt_error("FDT overwritten");
Kumar Gala06a09912008-08-15 08:24:38 -05001633 goto error;
1634 }
1635
Stephen Warren712fbcf2011-10-18 11:11:49 +00001636 printf(" Loading FDT from 0x%08lx "
1637 "to 0x%08lx\n",
1638 (ulong)data,
1639 load_start);
Kumar Gala06a09912008-08-15 08:24:38 -05001640
Stephen Warren712fbcf2011-10-18 11:11:49 +00001641 memmove((void *)load_start,
Kumar Gala06a09912008-08-15 08:24:38 -05001642 (void *)data, size);
1643
1644 fdt_blob = (char *)load_start;
1645 } else {
1646 fdt_blob = (char *)data;
1647 }
1648
1649 images->fit_hdr_fdt = fit_hdr;
1650 images->fit_uname_fdt = fit_uname_fdt;
1651 images->fit_noffset_fdt = fdt_noffset;
1652 break;
1653 } else
1654#endif
1655 {
1656 /*
1657 * FDT blob
1658 */
1659 fdt_blob = (char *)fdt_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001660 debug("* fdt: raw FDT blob\n");
1661 printf("## Flattened Device Tree blob at "
1662 "%08lx\n", (long)fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001663 }
1664 break;
1665 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001666 puts("ERROR: Did not find a cmdline Flattened Device "
1667 "Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001668 goto error;
1669 }
1670
Simon Glass0ec2ce42011-09-23 06:22:04 +00001671 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001672
1673 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +00001674 image_check_type(&images->legacy_hdr_os_copy,
1675 IH_TYPE_MULTI)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001676
1677 ulong fdt_data, fdt_len;
1678
1679 /*
1680 * Now check if we have a legacy multi-component image,
1681 * get second entry data start address and len.
1682 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001683 printf("## Flattened Device Tree from multi "
Kumar Gala06a09912008-08-15 08:24:38 -05001684 "component Image at %08lX\n",
1685 (ulong)images->legacy_hdr_os);
1686
Stephen Warren712fbcf2011-10-18 11:11:49 +00001687 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1688 &fdt_len);
Kumar Gala06a09912008-08-15 08:24:38 -05001689 if (fdt_len) {
1690
1691 fdt_blob = (char *)fdt_data;
Simon Glass0ec2ce42011-09-23 06:22:04 +00001692 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001693
Stephen Warren712fbcf2011-10-18 11:11:49 +00001694 if (fdt_check_header(fdt_blob) != 0) {
1695 fdt_error("image is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001696 goto error;
1697 }
1698
John Rigbyd1263fc2010-10-13 13:57:32 -06001699 if (fdt_totalsize(fdt_blob) != fdt_len) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001700 fdt_error("fdt size != image size");
Kumar Gala06a09912008-08-15 08:24:38 -05001701 goto error;
1702 }
1703 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001704 debug("## No Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001705 return 0;
1706 }
1707 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001708 debug("## No Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001709 return 0;
1710 }
1711
1712 *of_flat_tree = fdt_blob;
John Rigbyd1263fc2010-10-13 13:57:32 -06001713 *of_size = fdt_totalsize(fdt_blob);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001714 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner52514692008-08-21 07:12:26 -07001715 (ulong)*of_flat_tree, *of_size);
Kumar Gala06a09912008-08-15 08:24:38 -05001716
1717 return 0;
1718
1719error:
Kim Phillips199adb62012-10-29 13:34:32 +00001720 *of_flat_tree = NULL;
Kumar Gala06a09912008-08-15 08:24:38 -05001721 *of_size = 0;
1722 return 1;
1723}
1724#endif /* CONFIG_OF_LIBFDT */
1725
John Rigbyfca43cc2010-10-13 13:57:35 -06001726#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001727/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001728 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001729 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001730 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1731 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1732 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001733 * boot_get_cmdline() allocates space for kernel command line below
Grant Likely590d3ca2011-03-28 09:58:34 +00001734 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001735 * variable is present its contents is copied to allocated kernel
1736 * command line.
1737 *
1738 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001739 * 0 - success
1740 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001741 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001742int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001743{
1744 char *cmdline;
1745 char *s;
1746
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001747 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001748 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001749
1750 if (cmdline == NULL)
1751 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001752
1753 if ((s = getenv("bootargs")) == NULL)
1754 s = "";
1755
1756 strcpy(cmdline, s);
1757
1758 *cmd_start = (ulong) & cmdline[0];
1759 *cmd_end = *cmd_start + strlen(cmdline);
1760
Stephen Warren712fbcf2011-10-18 11:11:49 +00001761 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001762
Kumar Galae822d7f2008-02-27 21:51:49 -06001763 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001764}
John Rigbyfca43cc2010-10-13 13:57:35 -06001765#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001766
John Rigbyfca43cc2010-10-13 13:57:35 -06001767#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001768/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001769 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001770 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001771 * @kbd: double pointer to board info data
1772 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001773 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likely590d3ca2011-03-28 09:58:34 +00001774 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1775 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001776 *
1777 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001778 * 0 - success
1779 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001780 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001781int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001782{
Becky Bruce391fd932008-06-09 20:37:18 -05001783 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001784 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001785 if (*kbd == NULL)
1786 return -1;
1787
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001788 **kbd = *(gd->bd);
1789
Stephen Warren712fbcf2011-10-18 11:11:49 +00001790 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001791
1792#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1793 do_bdinfo(NULL, 0, 0, NULL);
1794#endif
1795
Kumar Galae822d7f2008-02-27 21:51:49 -06001796 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001797}
John Rigbyfca43cc2010-10-13 13:57:35 -06001798#endif /* CONFIG_SYS_BOOT_GET_KBD */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001799#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001800
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001801#if defined(CONFIG_FIT)
1802/*****************************************************************************/
1803/* New uImage format routines */
1804/*****************************************************************************/
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001805#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +00001806static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001807 ulong *addr, const char **name)
1808{
1809 const char *sep;
1810
1811 *addr = addr_curr;
1812 *name = NULL;
1813
Stephen Warren712fbcf2011-10-18 11:11:49 +00001814 sep = strchr(spec, sepc);
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001815 if (sep) {
1816 if (sep - spec > 0)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001817 *addr = simple_strtoul(spec, NULL, 16);
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001818
1819 *name = sep + 1;
1820 return 1;
1821 }
1822
1823 return 0;
1824}
1825
1826/**
1827 * fit_parse_conf - parse FIT configuration spec
1828 * @spec: input string, containing configuration spec
1829 * @add_curr: current image address (to be used as a possible default)
1830 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1831 * configuration
1832 * @conf_name double pointer to a char, will hold pointer to a configuration
1833 * unit name
1834 *
1835 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1836 * where <addr> is a FIT image address that contains configuration
1837 * with a <conf> unit name.
1838 *
1839 * Address part is optional, and if omitted default add_curr will
1840 * be used instead.
1841 *
1842 * returns:
1843 * 1 if spec is a valid configuration string,
1844 * addr and conf_name are set accordingly
1845 * 0 otherwise
1846 */
Mike Frysinger314f6342012-04-22 06:59:06 +00001847int fit_parse_conf(const char *spec, ulong addr_curr,
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001848 ulong *addr, const char **conf_name)
1849{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001850 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001851}
1852
1853/**
1854 * fit_parse_subimage - parse FIT subimage spec
1855 * @spec: input string, containing subimage spec
1856 * @add_curr: current image address (to be used as a possible default)
1857 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1858 * subimage
1859 * @image_name: double pointer to a char, will hold pointer to a subimage name
1860 *
1861 * fit_parse_subimage() expects subimage spec in the for of
1862 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1863 * subimage with a <subimg> unit name.
1864 *
1865 * Address part is optional, and if omitted default add_curr will
1866 * be used instead.
1867 *
1868 * returns:
1869 * 1 if spec is a valid subimage string,
1870 * addr and image_name are set accordingly
1871 * 0 otherwise
1872 */
Mike Frysinger314f6342012-04-22 06:59:06 +00001873int fit_parse_subimage(const char *spec, ulong addr_curr,
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001874 ulong *addr, const char **image_name)
1875{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001876 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001877}
Marian Balakowicz570abb02008-02-29 15:59:59 +01001878#endif /* !USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001879
Stephen Warren712fbcf2011-10-18 11:11:49 +00001880static void fit_get_debug(const void *fit, int noffset,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001881 char *prop_name, int err)
1882{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001883 debug("Can't get '%s' property from FIT 0x%08lx, "
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001884 "node: offset %d, name %s (%s)\n",
1885 prop_name, (ulong)fit, noffset,
Stephen Warren712fbcf2011-10-18 11:11:49 +00001886 fit_get_name(fit, noffset, NULL),
1887 fdt_strerror(err));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001888}
1889
1890/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001891 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001892 * @fit: pointer to the FIT format image header
1893 * @p: pointer to prefix string
1894 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001895 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001896 * The routine prints out FIT image properties (root node level) follwed by
1897 * the details of each component image.
1898 *
1899 * returns:
1900 * no returned results
1901 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001902void fit_print_contents(const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001903{
1904 char *desc;
1905 char *uname;
1906 int images_noffset;
1907 int confs_noffset;
1908 int noffset;
1909 int ndepth;
1910 int count = 0;
1911 int ret;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001912 const char *p;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001913#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1914 time_t timestamp;
1915#endif
1916
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001917#ifdef USE_HOSTCC
1918 p = "";
1919#else
1920 p = " ";
1921#endif
1922
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001923 /* Root node properties */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001924 ret = fit_get_desc(fit, 0, &desc);
1925 printf("%sFIT description: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001926 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001927 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001928 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001929 printf("%s\n", desc);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001930
1931#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001932 ret = fit_get_timestamp(fit, 0, &timestamp);
1933 printf("%sCreated: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001934 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001935 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001936 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001937 genimg_print_time(timestamp);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001938#endif
1939
1940 /* Find images parent node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001941 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001942 if (images_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001943 printf("Can't find images parent node '%s' (%s)\n",
1944 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001945 return;
1946 }
1947
1948 /* Process its subnodes, print out component images details */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001949 for (ndepth = 0, count = 0,
1950 noffset = fdt_next_node(fit, images_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001951 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001952 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001953 if (ndepth == 1) {
1954 /*
1955 * Direct child node of the images parent node,
1956 * i.e. component image node.
1957 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001958 printf("%s Image %u (%s)\n", p, count++,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001959 fit_get_name(fit, noffset, NULL));
1960
Stephen Warren712fbcf2011-10-18 11:11:49 +00001961 fit_image_print(fit, noffset, p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001962 }
1963 }
1964
1965 /* Find configurations parent node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001966 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001967 if (confs_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001968 debug("Can't get configurations parent node '%s' (%s)\n",
1969 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001970 return;
1971 }
1972
1973 /* get default configuration unit name from default property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001974 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001975 if (uname)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001976 printf("%s Default Configuration: '%s'\n", p, uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001977
1978 /* Process its subnodes, print out configurations details */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001979 for (ndepth = 0, count = 0,
1980 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001981 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001982 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001983 if (ndepth == 1) {
1984 /*
1985 * Direct child node of the configurations parent node,
1986 * i.e. configuration node.
1987 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001988 printf("%s Configuration %u (%s)\n", p, count++,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001989 fit_get_name(fit, noffset, NULL));
1990
Stephen Warren712fbcf2011-10-18 11:11:49 +00001991 fit_conf_print(fit, noffset, p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001992 }
1993 }
1994}
1995
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001996/**
1997 * fit_image_print - prints out the FIT component image details
1998 * @fit: pointer to the FIT format image header
1999 * @image_noffset: offset of the component image node
2000 * @p: pointer to prefix string
2001 *
2002 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002003 * image. If present, hash nodes are printed out as well. Load
2004 * address for images of type firmware is also printed out. Since the load
2005 * address is not mandatory for firmware images, it will be output as
2006 * "unavailable" when not present.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002007 *
2008 * returns:
2009 * no returned results
2010 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002011void fit_image_print(const void *fit, int image_noffset, const char *p)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002012{
2013 char *desc;
2014 uint8_t type, arch, os, comp;
2015 size_t size;
2016 ulong load, entry;
2017 const void *data;
2018 int noffset;
2019 int ndepth;
2020 int ret;
2021
2022 /* Mandatory properties */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002023 ret = fit_get_desc(fit, image_noffset, &desc);
2024 printf("%s Description: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002025 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002026 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002027 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002028 printf("%s\n", desc);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002029
Stephen Warren712fbcf2011-10-18 11:11:49 +00002030 fit_image_get_type(fit, image_noffset, &type);
2031 printf("%s Type: %s\n", p, genimg_get_type_name(type));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002032
Stephen Warren712fbcf2011-10-18 11:11:49 +00002033 fit_image_get_comp(fit, image_noffset, &comp);
2034 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002035
Stephen Warren712fbcf2011-10-18 11:11:49 +00002036 ret = fit_image_get_data(fit, image_noffset, &data, &size);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002037
2038#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +00002039 printf("%s Data Start: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002040 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002041 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002042 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002043 printf("0x%08lx\n", (ulong)data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002044#endif
2045
Stephen Warren712fbcf2011-10-18 11:11:49 +00002046 printf("%s Data Size: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002047 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002048 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002049 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002050 genimg_print_size(size);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002051
2052 /* Remaining, type dependent properties */
2053 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2054 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2055 (type == IH_TYPE_FLATDT)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002056 fit_image_get_arch(fit, image_noffset, &arch);
2057 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002058 }
2059
Jagannadha Sutradharudu Tekicee84e82012-07-27 22:02:22 +00002060 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002061 fit_image_get_os(fit, image_noffset, &os);
2062 printf("%s OS: %s\n", p, genimg_get_os_name(os));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002063 }
2064
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002065 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Jagannadha Sutradharudu Tekicee84e82012-07-27 22:02:22 +00002066 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002067 ret = fit_image_get_load(fit, image_noffset, &load);
2068 printf("%s Load Address: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002069 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002070 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002071 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002072 printf("0x%08lx\n", load);
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002073 }
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002074
Jagannadha Sutradharudu Tekicee84e82012-07-27 22:02:22 +00002075 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2076 (type == IH_TYPE_RAMDISK)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002077 fit_image_get_entry(fit, image_noffset, &entry);
2078 printf("%s Entry Point: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002079 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002080 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002081 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002082 printf("0x%08lx\n", entry);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002083 }
2084
2085 /* Process all hash subnodes of the component image node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002086 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002087 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002088 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002089 if (ndepth == 1) {
2090 /* Direct child node of the component image node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002091 fit_image_print_hash(fit, noffset, p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002092 }
2093 }
2094}
2095
2096/**
2097 * fit_image_print_hash - prints out the hash node details
2098 * @fit: pointer to the FIT format image header
2099 * @noffset: offset of the hash node
2100 * @p: pointer to prefix string
2101 *
2102 * fit_image_print_hash() lists properies for the processed hash node
2103 *
2104 * returns:
2105 * no returned results
2106 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002107void fit_image_print_hash(const void *fit, int noffset, const char *p)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002108{
2109 char *algo;
2110 uint8_t *value;
2111 int value_len;
2112 int i, ret;
2113
2114 /*
2115 * Check subnode name, must be equal to "hash".
2116 * Multiple hash nodes require unique unit node
2117 * names, e.g. hash@1, hash@2, etc.
2118 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002119 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002120 FIT_HASH_NODENAME,
2121 strlen(FIT_HASH_NODENAME)) != 0)
2122 return;
2123
Stephen Warren712fbcf2011-10-18 11:11:49 +00002124 debug("%s Hash node: '%s'\n", p,
2125 fit_get_name(fit, noffset, NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002126
Stephen Warren712fbcf2011-10-18 11:11:49 +00002127 printf("%s Hash algo: ", p);
2128 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2129 printf("invalid/unsupported\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002130 return;
2131 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00002132 printf("%s\n", algo);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002133
Stephen Warren712fbcf2011-10-18 11:11:49 +00002134 ret = fit_image_hash_get_value(fit, noffset, &value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002135 &value_len);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002136 printf("%s Hash value: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002137 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002138 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002139 } else {
2140 for (i = 0; i < value_len; i++)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002141 printf("%02x", value[i]);
2142 printf("\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002143 }
2144
Stephen Warren712fbcf2011-10-18 11:11:49 +00002145 debug("%s Hash len: %d\n", p, value_len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002146}
2147
2148/**
2149 * fit_get_desc - get node description property
2150 * @fit: pointer to the FIT format image header
2151 * @noffset: node offset
2152 * @desc: double pointer to the char, will hold pointer to the descrption
2153 *
2154 * fit_get_desc() reads description property from a given node, if
2155 * description is found pointer to it is returened in third call argument.
2156 *
2157 * returns:
2158 * 0, on success
2159 * -1, on failure
2160 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002161int fit_get_desc(const void *fit, int noffset, char **desc)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002162{
2163 int len;
2164
Stephen Warren712fbcf2011-10-18 11:11:49 +00002165 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002166 if (*desc == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002167 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002168 return -1;
2169 }
2170
2171 return 0;
2172}
2173
2174/**
2175 * fit_get_timestamp - get node timestamp property
2176 * @fit: pointer to the FIT format image header
2177 * @noffset: node offset
2178 * @timestamp: pointer to the time_t, will hold read timestamp
2179 *
2180 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2181 * is found and has a correct size its value is retured in third call
2182 * argument.
2183 *
2184 * returns:
2185 * 0, on success
2186 * -1, on property read failure
2187 * -2, on wrong timestamp size
2188 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002189int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002190{
2191 int len;
2192 const void *data;
2193
Stephen Warren712fbcf2011-10-18 11:11:49 +00002194 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002195 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002196 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002197 return -1;
2198 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00002199 if (len != sizeof(uint32_t)) {
2200 debug("FIT timestamp with incorrect size of (%u)\n", len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002201 return -2;
2202 }
2203
Stephen Warren712fbcf2011-10-18 11:11:49 +00002204 *timestamp = uimage_to_cpu(*((uint32_t *)data));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002205 return 0;
2206}
2207
2208/**
2209 * fit_image_get_node - get node offset for component image of a given unit name
2210 * @fit: pointer to the FIT format image header
2211 * @image_uname: component image node unit name
2212 *
2213 * fit_image_get_node() finds a component image (withing the '/images'
2214 * node) of a provided unit name. If image is found its node offset is
2215 * returned to the caller.
2216 *
2217 * returns:
2218 * image node offset when found (>=0)
2219 * negative number on failure (FDT_ERR_* code)
2220 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002221int fit_image_get_node(const void *fit, const char *image_uname)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002222{
2223 int noffset, images_noffset;
2224
Stephen Warren712fbcf2011-10-18 11:11:49 +00002225 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002226 if (images_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002227 debug("Can't find images parent node '%s' (%s)\n",
2228 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002229 return images_noffset;
2230 }
2231
Stephen Warren712fbcf2011-10-18 11:11:49 +00002232 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002233 if (noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002234 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2235 image_uname, fdt_strerror(noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002236 }
2237
2238 return noffset;
2239}
2240
2241/**
2242 * fit_image_get_os - get os id for a given component image node
2243 * @fit: pointer to the FIT format image header
2244 * @noffset: component image node offset
2245 * @os: pointer to the uint8_t, will hold os numeric id
2246 *
2247 * fit_image_get_os() finds os property in a given component image node.
2248 * If the property is found, its (string) value is translated to the numeric
2249 * id which is returned to the caller.
2250 *
2251 * returns:
2252 * 0, on success
2253 * -1, on failure
2254 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002255int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002256{
2257 int len;
2258 const void *data;
2259
2260 /* Get OS name from property data */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002261 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002262 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002263 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002264 *os = -1;
2265 return -1;
2266 }
2267
2268 /* Translate OS name to id */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002269 *os = genimg_get_os_id(data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002270 return 0;
2271}
2272
2273/**
2274 * fit_image_get_arch - get arch id for a given component image node
2275 * @fit: pointer to the FIT format image header
2276 * @noffset: component image node offset
2277 * @arch: pointer to the uint8_t, will hold arch numeric id
2278 *
2279 * fit_image_get_arch() finds arch property in a given component image node.
2280 * If the property is found, its (string) value is translated to the numeric
2281 * id which is returned to the caller.
2282 *
2283 * returns:
2284 * 0, on success
2285 * -1, on failure
2286 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002287int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002288{
2289 int len;
2290 const void *data;
2291
2292 /* Get architecture name from property data */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002293 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002294 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002295 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002296 *arch = -1;
2297 return -1;
2298 }
2299
2300 /* Translate architecture name to id */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002301 *arch = genimg_get_arch_id(data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002302 return 0;
2303}
2304
2305/**
2306 * fit_image_get_type - get type id for a given component image node
2307 * @fit: pointer to the FIT format image header
2308 * @noffset: component image node offset
2309 * @type: pointer to the uint8_t, will hold type numeric id
2310 *
2311 * fit_image_get_type() finds type property in a given component image node.
2312 * If the property is found, its (string) value is translated to the numeric
2313 * id which is returned to the caller.
2314 *
2315 * returns:
2316 * 0, on success
2317 * -1, on failure
2318 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002319int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002320{
2321 int len;
2322 const void *data;
2323
2324 /* Get image type name from property data */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002325 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002326 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002327 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002328 *type = -1;
2329 return -1;
2330 }
2331
2332 /* Translate image type name to id */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002333 *type = genimg_get_type_id(data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002334 return 0;
2335}
2336
2337/**
2338 * fit_image_get_comp - get comp id for a given component image node
2339 * @fit: pointer to the FIT format image header
2340 * @noffset: component image node offset
2341 * @comp: pointer to the uint8_t, will hold comp numeric id
2342 *
2343 * fit_image_get_comp() finds comp property in a given component image node.
2344 * If the property is found, its (string) value is translated to the numeric
2345 * id which is returned to the caller.
2346 *
2347 * returns:
2348 * 0, on success
2349 * -1, on failure
2350 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002351int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002352{
2353 int len;
2354 const void *data;
2355
2356 /* Get compression name from property data */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002357 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002358 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002359 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002360 *comp = -1;
2361 return -1;
2362 }
2363
2364 /* Translate compression name to id */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002365 *comp = genimg_get_comp_id(data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002366 return 0;
2367}
2368
2369/**
2370 * fit_image_get_load - get load address property for a given component image node
2371 * @fit: pointer to the FIT format image header
2372 * @noffset: component image node offset
2373 * @load: pointer to the uint32_t, will hold load address
2374 *
2375 * fit_image_get_load() finds load address property in a given component image node.
2376 * If the property is found, its value is returned to the caller.
2377 *
2378 * returns:
2379 * 0, on success
2380 * -1, on failure
2381 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002382int fit_image_get_load(const void *fit, int noffset, ulong *load)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002383{
2384 int len;
2385 const uint32_t *data;
2386
Stephen Warren712fbcf2011-10-18 11:11:49 +00002387 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002388 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002389 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002390 return -1;
2391 }
2392
Stephen Warren712fbcf2011-10-18 11:11:49 +00002393 *load = uimage_to_cpu(*data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002394 return 0;
2395}
2396
2397/**
2398 * fit_image_get_entry - get entry point address property for a given component image node
2399 * @fit: pointer to the FIT format image header
2400 * @noffset: component image node offset
2401 * @entry: pointer to the uint32_t, will hold entry point address
2402 *
2403 * fit_image_get_entry() finds entry point address property in a given component image node.
2404 * If the property is found, its value is returned to the caller.
2405 *
2406 * returns:
2407 * 0, on success
2408 * -1, on failure
2409 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002410int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002411{
2412 int len;
2413 const uint32_t *data;
2414
Stephen Warren712fbcf2011-10-18 11:11:49 +00002415 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002416 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002417 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002418 return -1;
2419 }
2420
Stephen Warren712fbcf2011-10-18 11:11:49 +00002421 *entry = uimage_to_cpu(*data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002422 return 0;
2423}
2424
2425/**
2426 * fit_image_get_data - get data property and its size for a given component image node
2427 * @fit: pointer to the FIT format image header
2428 * @noffset: component image node offset
2429 * @data: double pointer to void, will hold data property's data address
2430 * @size: pointer to size_t, will hold data property's data size
2431 *
2432 * fit_image_get_data() finds data property in a given component image node.
2433 * If the property is found its data start address and size are returned to
2434 * the caller.
2435 *
2436 * returns:
2437 * 0, on success
2438 * -1, on failure
2439 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002440int fit_image_get_data(const void *fit, int noffset,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002441 const void **data, size_t *size)
2442{
2443 int len;
2444
Stephen Warren712fbcf2011-10-18 11:11:49 +00002445 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002446 if (*data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002447 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002448 *size = 0;
2449 return -1;
2450 }
2451
2452 *size = len;
2453 return 0;
2454}
2455
2456/**
2457 * fit_image_hash_get_algo - get hash algorithm name
2458 * @fit: pointer to the FIT format image header
2459 * @noffset: hash node offset
2460 * @algo: double pointer to char, will hold pointer to the algorithm name
2461 *
2462 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2463 * If the property is found its data start address is returned to the caller.
2464 *
2465 * returns:
2466 * 0, on success
2467 * -1, on failure
2468 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002469int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002470{
2471 int len;
2472
Stephen Warren712fbcf2011-10-18 11:11:49 +00002473 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002474 if (*algo == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002475 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002476 return -1;
2477 }
2478
2479 return 0;
2480}
2481
2482/**
2483 * fit_image_hash_get_value - get hash value and length
2484 * @fit: pointer to the FIT format image header
2485 * @noffset: hash node offset
2486 * @value: double pointer to uint8_t, will hold address of a hash value data
2487 * @value_len: pointer to an int, will hold hash data length
2488 *
2489 * fit_image_hash_get_value() finds hash value property in a given hash node.
2490 * If the property is found its data start address and size are returned to
2491 * the caller.
2492 *
2493 * returns:
2494 * 0, on success
2495 * -1, on failure
2496 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002497int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002498 int *value_len)
2499{
2500 int len;
2501
Stephen Warren712fbcf2011-10-18 11:11:49 +00002502 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002503 if (*value == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002504 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002505 *value_len = 0;
2506 return -1;
2507 }
2508
2509 *value_len = len;
2510 return 0;
2511}
2512
Joe Hershberger8ac88f22012-08-17 10:34:39 +00002513#ifndef USE_HOSTCC
2514/**
2515 * fit_image_hash_get_ignore - get hash ignore flag
2516 * @fit: pointer to the FIT format image header
2517 * @noffset: hash node offset
2518 * @ignore: pointer to an int, will hold hash ignore flag
2519 *
2520 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
2521 * If the property is found and non-zero, the hash algorithm is not verified by
2522 * u-boot automatically.
2523 *
2524 * returns:
2525 * 0, on ignore not found
2526 * value, on ignore found
2527 */
2528int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
2529{
2530 int len;
2531 int *value;
2532
2533 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
2534 if (value == NULL || len != sizeof(int))
2535 *ignore = 0;
2536 else
2537 *ignore = *value;
2538
2539 return 0;
2540}
2541#endif
2542
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002543/**
2544 * fit_set_timestamp - set node timestamp property
2545 * @fit: pointer to the FIT format image header
2546 * @noffset: node offset
2547 * @timestamp: timestamp value to be set
2548 *
2549 * fit_set_timestamp() attempts to set timestamp property in the requested
2550 * node and returns operation status to the caller.
2551 *
2552 * returns:
2553 * 0, on success
2554 * -1, on property read failure
2555 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002556int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002557{
2558 uint32_t t;
2559 int ret;
2560
Stephen Warren712fbcf2011-10-18 11:11:49 +00002561 t = cpu_to_uimage(timestamp);
2562 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2563 sizeof(uint32_t));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002564 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002565 printf("Can't set '%s' property for '%s' node (%s)\n",
2566 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2567 fdt_strerror(ret));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002568 return -1;
2569 }
2570
2571 return 0;
2572}
2573
2574/**
2575 * calculate_hash - calculate and return hash for provided input data
2576 * @data: pointer to the input data
2577 * @data_len: data length
2578 * @algo: requested hash algorithm
2579 * @value: pointer to the char, will hold hash value data (caller must
2580 * allocate enough free space)
2581 * value_len: length of the calculated hash
2582 *
2583 * calculate_hash() computes input data hash according to the requested algorithm.
2584 * Resulting hash value is placed in caller provided 'value' buffer, length
2585 * of the calculated hash is returned via value_len pointer argument.
2586 *
2587 * returns:
2588 * 0, on success
2589 * -1, when algo is unsupported
2590 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002591static int calculate_hash(const void *data, int data_len, const char *algo,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002592 uint8_t *value, int *value_len)
2593{
Stephen Warren712fbcf2011-10-18 11:11:49 +00002594 if (strcmp(algo, "crc32") == 0) {
2595 *((uint32_t *)value) = crc32_wd(0, data, data_len,
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002596 CHUNKSZ_CRC32);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002597 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002598 *value_len = 4;
Stephen Warren712fbcf2011-10-18 11:11:49 +00002599 } else if (strcmp(algo, "sha1") == 0) {
2600 sha1_csum_wd((unsigned char *) data, data_len,
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002601 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002602 *value_len = 20;
Stephen Warren712fbcf2011-10-18 11:11:49 +00002603 } else if (strcmp(algo, "md5") == 0) {
2604 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka766529f2008-03-14 16:22:34 +01002605 *value_len = 16;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002606 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002607 debug("Unsupported hash alogrithm\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002608 return -1;
2609 }
2610 return 0;
2611}
2612
2613#ifdef USE_HOSTCC
2614/**
2615 * fit_set_hashes - process FIT component image nodes and calculate hashes
2616 * @fit: pointer to the FIT format image header
2617 *
2618 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2619 * Hashes are calculated for all component images which have hash subnodes
2620 * with algorithm property set to one of the supported hash algorithms.
2621 *
2622 * returns
2623 * 0, on success
2624 * libfdt error code, on failure
2625 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002626int fit_set_hashes(void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002627{
2628 int images_noffset;
2629 int noffset;
2630 int ndepth;
2631 int ret;
2632
2633 /* Find images parent node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002634 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002635 if (images_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002636 printf("Can't find images parent node '%s' (%s)\n",
2637 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002638 return images_noffset;
2639 }
2640
2641 /* Process its subnodes, print out component images details */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002642 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002643 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002644 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002645 if (ndepth == 1) {
2646 /*
2647 * Direct child node of the images parent node,
2648 * i.e. component image node.
2649 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002650 ret = fit_image_set_hashes(fit, noffset);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002651 if (ret)
2652 return ret;
2653 }
2654 }
2655
2656 return 0;
2657}
2658
2659/**
2660 * fit_image_set_hashes - calculate/set hashes for given component image node
2661 * @fit: pointer to the FIT format image header
2662 * @image_noffset: requested component image node
2663 *
2664 * fit_image_set_hashes() adds hash values for an component image node. All
2665 * existing hash subnodes are checked, if algorithm property is set to one of
2666 * the supported hash algorithms, hash value is computed and corresponding
2667 * hash node property is set, for example:
2668 *
2669 * Input component image node structure:
2670 *
2671 * o image@1 (at image_noffset)
2672 * | - data = [binary data]
2673 * o hash@1
2674 * |- algo = "sha1"
2675 *
2676 * Output component image node structure:
2677 *
2678 * o image@1 (at image_noffset)
2679 * | - data = [binary data]
2680 * o hash@1
2681 * |- algo = "sha1"
2682 * |- value = sha1(data)
2683 *
2684 * returns:
2685 * 0 on sucess
2686 * <0 on failure
2687 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002688int fit_image_set_hashes(void *fit, int image_noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002689{
2690 const void *data;
2691 size_t size;
2692 char *algo;
2693 uint8_t value[FIT_MAX_HASH_LEN];
2694 int value_len;
2695 int noffset;
2696 int ndepth;
2697
2698 /* Get image data and data length */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002699 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2700 printf("Can't get image data/size\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002701 return -1;
2702 }
2703
2704 /* Process all hash subnodes of the component image node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002705 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002706 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002707 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002708 if (ndepth == 1) {
2709 /* Direct child node of the component image node */
2710
2711 /*
2712 * Check subnode name, must be equal to "hash".
2713 * Multiple hash nodes require unique unit node
2714 * names, e.g. hash@1, hash@2, etc.
2715 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002716 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002717 FIT_HASH_NODENAME,
2718 strlen(FIT_HASH_NODENAME)) != 0) {
2719 /* Not a hash subnode, skip it */
2720 continue;
2721 }
2722
Stephen Warren712fbcf2011-10-18 11:11:49 +00002723 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2724 printf("Can't get hash algo property for "
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002725 "'%s' hash node in '%s' image node\n",
Stephen Warren712fbcf2011-10-18 11:11:49 +00002726 fit_get_name(fit, noffset, NULL),
2727 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002728 return -1;
2729 }
2730
Stephen Warren712fbcf2011-10-18 11:11:49 +00002731 if (calculate_hash(data, size, algo, value,
2732 &value_len)) {
2733 printf("Unsupported hash algorithm (%s) for "
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002734 "'%s' hash node in '%s' image node\n",
Stephen Warren712fbcf2011-10-18 11:11:49 +00002735 algo, fit_get_name(fit, noffset, NULL),
2736 fit_get_name(fit, image_noffset,
2737 NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002738 return -1;
2739 }
2740
Stephen Warren712fbcf2011-10-18 11:11:49 +00002741 if (fit_image_hash_set_value(fit, noffset, value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002742 value_len)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002743 printf("Can't set hash value for "
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002744 "'%s' hash node in '%s' image node\n",
Stephen Warren712fbcf2011-10-18 11:11:49 +00002745 fit_get_name(fit, noffset, NULL),
2746 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002747 return -1;
2748 }
2749 }
2750 }
2751
2752 return 0;
2753}
2754
2755/**
2756 * fit_image_hash_set_value - set hash value in requested has node
2757 * @fit: pointer to the FIT format image header
2758 * @noffset: hash node offset
2759 * @value: hash value to be set
2760 * @value_len: hash value length
2761 *
2762 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2763 * given and returns operation status to the caller.
2764 *
2765 * returns
2766 * 0, on success
2767 * -1, on failure
2768 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002769int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002770 int value_len)
2771{
2772 int ret;
2773
Stephen Warren712fbcf2011-10-18 11:11:49 +00002774 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002775 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002776 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2777 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2778 fdt_strerror(ret));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002779 return -1;
2780 }
2781
2782 return 0;
2783}
2784#endif /* USE_HOSTCC */
2785
2786/**
2787 * fit_image_check_hashes - verify data intergity
2788 * @fit: pointer to the FIT format image header
2789 * @image_noffset: component image node offset
2790 *
2791 * fit_image_check_hashes() goes over component image hash nodes,
2792 * re-calculates each data hash and compares with the value stored in hash
2793 * node.
2794 *
2795 * returns:
2796 * 1, if all hashes are valid
2797 * 0, otherwise (or on error)
2798 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002799int fit_image_check_hashes(const void *fit, int image_noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002800{
2801 const void *data;
2802 size_t size;
2803 char *algo;
2804 uint8_t *fit_value;
2805 int fit_value_len;
Joe Hershberger8ac88f22012-08-17 10:34:39 +00002806#ifndef USE_HOSTCC
2807 int ignore;
2808#endif
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002809 uint8_t value[FIT_MAX_HASH_LEN];
2810 int value_len;
2811 int noffset;
2812 int ndepth;
2813 char *err_msg = "";
2814
2815 /* Get image data and data length */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002816 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2817 printf("Can't get image data/size\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002818 return 0;
2819 }
2820
2821 /* Process all hash subnodes of the component image node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002822 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002823 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002824 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002825 if (ndepth == 1) {
2826 /* Direct child node of the component image node */
2827
2828 /*
2829 * Check subnode name, must be equal to "hash".
2830 * Multiple hash nodes require unique unit node
2831 * names, e.g. hash@1, hash@2, etc.
2832 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002833 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002834 FIT_HASH_NODENAME,
2835 strlen(FIT_HASH_NODENAME)) != 0)
2836 continue;
2837
Stephen Warren712fbcf2011-10-18 11:11:49 +00002838 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002839 err_msg = " error!\nCan't get hash algo "
2840 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002841 goto error;
2842 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00002843 printf("%s", algo);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002844
Joe Hershberger8ac88f22012-08-17 10:34:39 +00002845#ifndef USE_HOSTCC
2846 fit_image_hash_get_ignore(fit, noffset, &ignore);
2847 if (ignore) {
2848 printf("-skipped ");
2849 continue;
2850 }
2851#endif
2852
Stephen Warren712fbcf2011-10-18 11:11:49 +00002853 if (fit_image_hash_get_value(fit, noffset, &fit_value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002854 &fit_value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002855 err_msg = " error!\nCan't get hash value "
2856 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002857 goto error;
2858 }
2859
Stephen Warren712fbcf2011-10-18 11:11:49 +00002860 if (calculate_hash(data, size, algo, value,
2861 &value_len)) {
2862 err_msg = " error!\n"
2863 "Unsupported hash algorithm";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002864 goto error;
2865 }
2866
2867 if (value_len != fit_value_len) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002868 err_msg = " error !\nBad hash value len";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002869 goto error;
Stephen Warren712fbcf2011-10-18 11:11:49 +00002870 } else if (memcmp(value, fit_value, value_len) != 0) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002871 err_msg = " error!\nBad hash value";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002872 goto error;
2873 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00002874 printf("+ ");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002875 }
2876 }
2877
Joe Hershberger367e1252012-08-17 10:34:35 +00002878 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
2879 err_msg = " error!\nCorrupted or truncated tree";
2880 goto error;
2881 }
2882
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002883 return 1;
2884
2885error:
Stephen Warren712fbcf2011-10-18 11:11:49 +00002886 printf("%s for '%s' hash node in '%s' image node\n",
2887 err_msg, fit_get_name(fit, noffset, NULL),
2888 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002889 return 0;
2890}
2891
2892/**
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002893 * fit_all_image_check_hashes - verify data intergity for all images
2894 * @fit: pointer to the FIT format image header
2895 *
2896 * fit_all_image_check_hashes() goes over all images in the FIT and
2897 * for every images checks if all it's hashes are valid.
2898 *
2899 * returns:
2900 * 1, if all hashes of all images are valid
2901 * 0, otherwise (or on error)
2902 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002903int fit_all_image_check_hashes(const void *fit)
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002904{
2905 int images_noffset;
2906 int noffset;
2907 int ndepth;
2908 int count;
2909
2910 /* Find images parent node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002911 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002912 if (images_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002913 printf("Can't find images parent node '%s' (%s)\n",
2914 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002915 return 0;
2916 }
2917
2918 /* Process all image subnodes, check hashes for each */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002919 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002920 (ulong)fit);
2921 for (ndepth = 0, count = 0,
Stephen Warren712fbcf2011-10-18 11:11:49 +00002922 noffset = fdt_next_node(fit, images_noffset, &ndepth);
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002923 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002924 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002925 if (ndepth == 1) {
2926 /*
2927 * Direct child node of the images parent node,
2928 * i.e. component image node.
2929 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002930 printf(" Hash(es) for Image %u (%s): ", count++,
2931 fit_get_name(fit, noffset, NULL));
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002932
Stephen Warren712fbcf2011-10-18 11:11:49 +00002933 if (!fit_image_check_hashes(fit, noffset))
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002934 return 0;
Stephen Warren712fbcf2011-10-18 11:11:49 +00002935 printf("\n");
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002936 }
2937 }
2938 return 1;
2939}
2940
2941/**
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002942 * fit_image_check_os - check whether image node is of a given os type
2943 * @fit: pointer to the FIT format image header
2944 * @noffset: component image node offset
2945 * @os: requested image os
2946 *
2947 * fit_image_check_os() reads image os property and compares its numeric
2948 * id with the requested os. Comparison result is returned to the caller.
2949 *
2950 * returns:
2951 * 1 if image is of given os type
2952 * 0 otherwise (or on error)
2953 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002954int fit_image_check_os(const void *fit, int noffset, uint8_t os)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002955{
2956 uint8_t image_os;
2957
Stephen Warren712fbcf2011-10-18 11:11:49 +00002958 if (fit_image_get_os(fit, noffset, &image_os))
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002959 return 0;
2960 return (os == image_os);
2961}
2962
2963/**
2964 * fit_image_check_arch - check whether image node is of a given arch
2965 * @fit: pointer to the FIT format image header
2966 * @noffset: component image node offset
2967 * @arch: requested imagearch
2968 *
2969 * fit_image_check_arch() reads image arch property and compares its numeric
2970 * id with the requested arch. Comparison result is returned to the caller.
2971 *
2972 * returns:
2973 * 1 if image is of given arch
2974 * 0 otherwise (or on error)
2975 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002976int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002977{
2978 uint8_t image_arch;
2979
Stephen Warren712fbcf2011-10-18 11:11:49 +00002980 if (fit_image_get_arch(fit, noffset, &image_arch))
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002981 return 0;
2982 return (arch == image_arch);
2983}
2984
2985/**
2986 * fit_image_check_type - check whether image node is of a given type
2987 * @fit: pointer to the FIT format image header
2988 * @noffset: component image node offset
2989 * @type: requested image type
2990 *
2991 * fit_image_check_type() reads image type property and compares its numeric
2992 * id with the requested type. Comparison result is returned to the caller.
2993 *
2994 * returns:
2995 * 1 if image is of given type
2996 * 0 otherwise (or on error)
2997 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002998int fit_image_check_type(const void *fit, int noffset, uint8_t type)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002999{
3000 uint8_t image_type;
3001
Stephen Warren712fbcf2011-10-18 11:11:49 +00003002 if (fit_image_get_type(fit, noffset, &image_type))
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003003 return 0;
3004 return (type == image_type);
3005}
3006
3007/**
3008 * fit_image_check_comp - check whether image node uses given compression
3009 * @fit: pointer to the FIT format image header
3010 * @noffset: component image node offset
3011 * @comp: requested image compression type
3012 *
3013 * fit_image_check_comp() reads image compression property and compares its
3014 * numeric id with the requested compression type. Comparison result is
3015 * returned to the caller.
3016 *
3017 * returns:
3018 * 1 if image uses requested compression
3019 * 0 otherwise (or on error)
3020 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003021int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003022{
3023 uint8_t image_comp;
3024
Stephen Warren712fbcf2011-10-18 11:11:49 +00003025 if (fit_image_get_comp(fit, noffset, &image_comp))
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003026 return 0;
3027 return (comp == image_comp);
3028}
3029
3030/**
3031 * fit_check_format - sanity check FIT image format
3032 * @fit: pointer to the FIT format image header
3033 *
3034 * fit_check_format() runs a basic sanity FIT image verification.
3035 * Routine checks for mandatory properties, nodes, etc.
3036 *
3037 * returns:
3038 * 1, on success
3039 * 0, on failure
3040 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003041int fit_check_format(const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003042{
3043 /* mandatory / node 'description' property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003044 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
3045 debug("Wrong FIT format: no description\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003046 return 0;
3047 }
3048
3049#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
3050 /* mandatory / node 'timestamp' property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003051 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
3052 debug("Wrong FIT format: no timestamp\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003053 return 0;
3054 }
3055#endif
3056
3057 /* mandatory subimages parent '/images' node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003058 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
3059 debug("Wrong FIT format: no images parent node\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003060 return 0;
3061 }
3062
3063 return 1;
3064}
3065
Gabe Blackd95f6ec2012-10-25 16:31:10 +00003066
3067/**
3068 * fit_conf_find_compat
3069 * @fit: pointer to the FIT format image header
3070 * @fdt: pointer to the device tree to compare against
3071 *
3072 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
3073 * most compatible with the passed in device tree.
3074 *
3075 * Example:
3076 *
3077 * / o image-tree
3078 * |-o images
3079 * | |-o fdt@1
3080 * | |-o fdt@2
3081 * |
3082 * |-o configurations
3083 * |-o config@1
3084 * | |-fdt = fdt@1
3085 * |
3086 * |-o config@2
3087 * |-fdt = fdt@2
3088 *
3089 * / o U-Boot fdt
3090 * |-compatible = "foo,bar", "bim,bam"
3091 *
3092 * / o kernel fdt1
3093 * |-compatible = "foo,bar",
3094 *
3095 * / o kernel fdt2
3096 * |-compatible = "bim,bam", "baz,biz"
3097 *
3098 * Configuration 1 would be picked because the first string in U-Boot's
3099 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
3100 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
3101 *
3102 * returns:
3103 * offset to the configuration to use if one was found
3104 * -1 otherwise
3105 */
3106int fit_conf_find_compat(const void *fit, const void *fdt)
3107{
3108 int ndepth = 0;
3109 int noffset, confs_noffset, images_noffset;
3110 const void *fdt_compat;
3111 int fdt_compat_len;
3112 int best_match_offset = 0;
3113 int best_match_pos = 0;
3114
3115 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3116 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
3117 if (confs_noffset < 0 || images_noffset < 0) {
3118 debug("Can't find configurations or images nodes.\n");
3119 return -1;
3120 }
3121
3122 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
3123 if (!fdt_compat) {
3124 debug("Fdt for comparison has no \"compatible\" property.\n");
3125 return -1;
3126 }
3127
3128 /*
3129 * Loop over the configurations in the FIT image.
3130 */
3131 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
3132 (noffset >= 0) && (ndepth > 0);
3133 noffset = fdt_next_node(fit, noffset, &ndepth)) {
3134 const void *kfdt;
3135 const char *kfdt_name;
3136 int kfdt_noffset;
3137 const char *cur_fdt_compat;
3138 int len;
3139 size_t size;
3140 int i;
3141
3142 if (ndepth > 1)
3143 continue;
3144
3145 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
3146 if (!kfdt_name) {
3147 debug("No fdt property found.\n");
3148 continue;
3149 }
3150 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
3151 kfdt_name);
3152 if (kfdt_noffset < 0) {
3153 debug("No image node named \"%s\" found.\n",
3154 kfdt_name);
3155 continue;
3156 }
3157 /*
3158 * Get a pointer to this configuration's fdt.
3159 */
3160 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
3161 debug("Failed to get fdt \"%s\".\n", kfdt_name);
3162 continue;
3163 }
3164
3165 len = fdt_compat_len;
3166 cur_fdt_compat = fdt_compat;
3167 /*
3168 * Look for a match for each U-Boot compatibility string in
3169 * turn in this configuration's fdt.
3170 */
3171 for (i = 0; len > 0 &&
3172 (!best_match_offset || best_match_pos > i); i++) {
3173 int cur_len = strlen(cur_fdt_compat) + 1;
3174
3175 if (!fdt_node_check_compatible(kfdt, 0,
3176 cur_fdt_compat)) {
3177 best_match_offset = noffset;
3178 best_match_pos = i;
3179 break;
3180 }
3181 len -= cur_len;
3182 cur_fdt_compat += cur_len;
3183 }
3184 }
3185 if (!best_match_offset) {
3186 debug("No match found.\n");
3187 return -1;
3188 }
3189
3190 return best_match_offset;
3191}
3192
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003193/**
3194 * fit_conf_get_node - get node offset for configuration of a given unit name
3195 * @fit: pointer to the FIT format image header
3196 * @conf_uname: configuration node unit name
3197 *
3198 * fit_conf_get_node() finds a configuration (withing the '/configurations'
3199 * parant node) of a provided unit name. If configuration is found its node offset
3200 * is returned to the caller.
3201 *
3202 * When NULL is provided in second argument fit_conf_get_node() will search
3203 * for a default configuration node instead. Default configuration node unit name
3204 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
3205 *
3206 * returns:
3207 * configuration node offset when found (>=0)
3208 * negative number on failure (FDT_ERR_* code)
3209 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003210int fit_conf_get_node(const void *fit, const char *conf_uname)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003211{
3212 int noffset, confs_noffset;
3213 int len;
3214
Stephen Warren712fbcf2011-10-18 11:11:49 +00003215 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003216 if (confs_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00003217 debug("Can't find configurations parent node '%s' (%s)\n",
3218 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003219 return confs_noffset;
3220 }
3221
3222 if (conf_uname == NULL) {
3223 /* get configuration unit name from the default property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003224 debug("No configuration specified, trying default...\n");
3225 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3226 FIT_DEFAULT_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003227 if (conf_uname == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00003228 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3229 len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003230 return len;
3231 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00003232 debug("Found default configuration: '%s'\n", conf_uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003233 }
3234
Stephen Warren712fbcf2011-10-18 11:11:49 +00003235 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003236 if (noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00003237 debug("Can't get node offset for configuration unit name: "
3238 "'%s' (%s)\n",
3239 conf_uname, fdt_strerror(noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003240 }
3241
3242 return noffset;
3243}
3244
Stephen Warren712fbcf2011-10-18 11:11:49 +00003245static int __fit_conf_get_prop_node(const void *fit, int noffset,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003246 const char *prop_name)
3247{
3248 char *uname;
3249 int len;
3250
3251 /* get kernel image unit name from configuration kernel property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003252 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003253 if (uname == NULL)
3254 return len;
3255
Stephen Warren712fbcf2011-10-18 11:11:49 +00003256 return fit_image_get_node(fit, uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003257}
3258
3259/**
3260 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3261 * a given configuration
3262 * @fit: pointer to the FIT format image header
3263 * @noffset: configuration node offset
3264 *
3265 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3266 * configuration FIT_KERNEL_PROP property and translates it to the node
3267 * offset.
3268 *
3269 * returns:
3270 * image node offset when found (>=0)
3271 * negative number on failure (FDT_ERR_* code)
3272 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003273int fit_conf_get_kernel_node(const void *fit, int noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003274{
Stephen Warren712fbcf2011-10-18 11:11:49 +00003275 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003276}
3277
3278/**
3279 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3280 * a given configuration
3281 * @fit: pointer to the FIT format image header
3282 * @noffset: configuration node offset
3283 *
3284 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3285 * configuration FIT_KERNEL_PROP property and translates it to the node
3286 * offset.
3287 *
3288 * returns:
3289 * image node offset when found (>=0)
3290 * negative number on failure (FDT_ERR_* code)
3291 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003292int fit_conf_get_ramdisk_node(const void *fit, int noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003293{
Stephen Warren712fbcf2011-10-18 11:11:49 +00003294 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003295}
3296
3297/**
3298 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3299 * a given configuration
3300 * @fit: pointer to the FIT format image header
3301 * @noffset: configuration node offset
3302 *
3303 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3304 * configuration FIT_KERNEL_PROP property and translates it to the node
3305 * offset.
3306 *
3307 * returns:
3308 * image node offset when found (>=0)
3309 * negative number on failure (FDT_ERR_* code)
3310 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003311int fit_conf_get_fdt_node(const void *fit, int noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003312{
Stephen Warren712fbcf2011-10-18 11:11:49 +00003313 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003314}
3315
3316/**
3317 * fit_conf_print - prints out the FIT configuration details
3318 * @fit: pointer to the FIT format image header
Marian Balakowiczf773bea2008-03-12 10:35:46 +01003319 * @noffset: offset of the configuration node
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003320 * @p: pointer to prefix string
3321 *
3322 * fit_conf_print() lists all mandatory properies for the processed
3323 * configuration node.
3324 *
3325 * returns:
3326 * no returned results
3327 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003328void fit_conf_print(const void *fit, int noffset, const char *p)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003329{
3330 char *desc;
3331 char *uname;
3332 int ret;
3333
3334 /* Mandatory properties */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003335 ret = fit_get_desc(fit, noffset, &desc);
3336 printf("%s Description: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003337 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00003338 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003339 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00003340 printf("%s\n", desc);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003341
Stephen Warren712fbcf2011-10-18 11:11:49 +00003342 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3343 printf("%s Kernel: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003344 if (uname == NULL)
Stephen Warren712fbcf2011-10-18 11:11:49 +00003345 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003346 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00003347 printf("%s\n", uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003348
3349 /* Optional properties */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003350 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003351 if (uname)
Stephen Warren712fbcf2011-10-18 11:11:49 +00003352 printf("%s Init Ramdisk: %s\n", p, uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003353
Stephen Warren712fbcf2011-10-18 11:11:49 +00003354 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003355 if (uname)
Stephen Warren712fbcf2011-10-18 11:11:49 +00003356 printf("%s FDT: %s\n", p, uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003357}
Marian Balakowiczc8779642008-03-12 10:12:37 +01003358
3359/**
3360 * fit_check_ramdisk - verify FIT format ramdisk subimage
3361 * @fit_hdr: pointer to the FIT ramdisk header
3362 * @rd_noffset: ramdisk subimage node offset within FIT image
3363 * @arch: requested ramdisk image architecture type
3364 * @verify: data CRC verification flag
3365 *
3366 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3367 * specified FIT image.
3368 *
3369 * returns:
3370 * 1, on success
3371 * 0, on failure
3372 */
3373#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +00003374static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3375 int verify)
Marian Balakowiczc8779642008-03-12 10:12:37 +01003376{
Stephen Warren712fbcf2011-10-18 11:11:49 +00003377 fit_image_print(fit, rd_noffset, " ");
Marian Balakowiczc8779642008-03-12 10:12:37 +01003378
3379 if (verify) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00003380 puts(" Verifying Hash Integrity ... ");
3381 if (!fit_image_check_hashes(fit, rd_noffset)) {
3382 puts("Bad Data Hash\n");
Simon Glass770605e2012-02-13 13:51:18 +00003383 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003384 return 0;
3385 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00003386 puts("OK\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +01003387 }
3388
Simon Glass770605e2012-02-13 13:51:18 +00003389 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
Stephen Warren712fbcf2011-10-18 11:11:49 +00003390 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3391 !fit_image_check_arch(fit, rd_noffset, arch) ||
3392 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3393 printf("No Linux %s Ramdisk Image\n",
Marian Balakowiczc8779642008-03-12 10:12:37 +01003394 genimg_get_arch_name(arch));
Simon Glass770605e2012-02-13 13:51:18 +00003395 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003396 return 0;
3397 }
3398
Simon Glass770605e2012-02-13 13:51:18 +00003399 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003400 return 1;
3401}
3402#endif /* USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003403#endif /* CONFIG_FIT */