blob: 103e0e6e757bf47553c4fed357bf037072b63fa6 [file] [log] [blame]
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
Marian Balakowiczceaed2b2008-01-31 13:57:17 +010025
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010026#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010027#include <common.h>
28#include <watchdog.h>
29
30#ifdef CONFIG_SHOW_BOOT_PROGRESS
31#include <status_led.h>
32#endif
33
34#ifdef CONFIG_HAS_DATAFLASH
35#include <dataflash.h>
36#endif
37
Marian Balakowicz95d449a2008-05-13 15:53:29 +020038#ifdef CONFIG_LOGBUFFER
39#include <logbuff.h>
40#endif
41
Marian Balakowicz2242f532008-02-21 17:27:41 +010042#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
43#include <rtc.h>
44#endif
45
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010046#include <image.h>
47
Stephen Warren712fbcf2011-10-18 11:11:49 +000048#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczfff888a12008-02-21 17:20:19 +010049#include <fdt.h>
50#include <libfdt.h>
51#include <fdt_support.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010052#endif
53
54#if defined(CONFIG_FIT)
Andy Fleming20a14a42008-04-02 16:19:07 -050055#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010056#include <sha1.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010057
Stephen Warren712fbcf2011-10-18 11:11:49 +000058static int fit_check_ramdisk(const void *fit, int os_noffset,
Marian Balakowiczc8779642008-03-12 10:12:37 +010059 uint8_t arch, int verify);
Marian Balakowiczfff888a12008-02-21 17:20:19 +010060#endif
61
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010062#ifdef CONFIG_CMD_BDI
Wolfgang Denk54841ab2010-06-28 22:00:46 +020063extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010064#endif
65
66DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010067
Stephen Warren712fbcf2011-10-18 11:11:49 +000068static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +010069 int verify);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010070#else
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010071#include "mkimage.h"
Andy Fleming20a14a42008-04-02 16:19:07 -050072#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010073#include <time.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010074#include <image.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010075#endif /* !USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010076
Mike Frysinger7edb1862010-10-20 07:17:39 -040077static const table_entry_t uimage_arch[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +010078 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
79 { IH_ARCH_ALPHA, "alpha", "Alpha", },
80 { IH_ARCH_ARM, "arm", "ARM", },
81 { IH_ARCH_I386, "x86", "Intel x86", },
82 { IH_ARCH_IA64, "ia64", "IA64", },
83 { IH_ARCH_M68K, "m68k", "M68K", },
84 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
85 { IH_ARCH_MIPS, "mips", "MIPS", },
86 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010087 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -070088 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010089 { IH_ARCH_PPC, "ppc", "PowerPC", },
90 { IH_ARCH_S390, "s390", "IBM S390", },
91 { IH_ARCH_SH, "sh", "SuperH", },
92 { IH_ARCH_SPARC, "sparc", "SPARC", },
93 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
94 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
95 { IH_ARCH_AVR32, "avr32", "AVR32", },
Macpaul Lin64d61462011-10-19 20:41:09 +000096 { IH_ARCH_NDS32, "nds32", "NDS32", },
Stefan Kristiansson3ddcacc2011-11-26 19:04:50 +000097 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
Marian Balakowicz570abb02008-02-29 15:59:59 +010098 { -1, "", "", },
99};
100
Mike Frysinger7edb1862010-10-20 07:17:39 -0400101static const table_entry_t uimage_os[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100102 { IH_OS_INVALID, NULL, "Invalid OS", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100103 { IH_OS_LINUX, "linux", "Linux", },
104#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
105 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
106#endif
107 { IH_OS_NETBSD, "netbsd", "NetBSD", },
Torkel Lundgren3df61952010-09-28 11:05:36 +0200108 { IH_OS_OSE, "ose", "Enea OSE", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100109 { IH_OS_RTEMS, "rtems", "RTEMS", },
110 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
111#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
112 { IH_OS_QNX, "qnx", "QNX", },
113 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
114#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500115#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
116 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
117#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100118#ifdef USE_HOSTCC
119 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
120 { IH_OS_DELL, "dell", "Dell", },
121 { IH_OS_ESIX, "esix", "Esix", },
122 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
123 { IH_OS_IRIX, "irix", "Irix", },
124 { IH_OS_NCR, "ncr", "NCR", },
125 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
126 { IH_OS_PSOS, "psos", "pSOS", },
127 { IH_OS_SCO, "sco", "SCO", },
128 { IH_OS_SOLARIS, "solaris", "Solaris", },
129 { IH_OS_SVR4, "svr4", "SVR4", },
130#endif
131 { -1, "", "", },
132};
133
Mike Frysinger7edb1862010-10-20 07:17:39 -0400134static const table_entry_t uimage_type[] = {
Stefano Babic4962e382011-10-17 00:07:43 +0000135 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100136 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
137 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby3decb142011-07-21 09:10:30 -0400138 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100139 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stephen Warrenb9b50e892011-11-10 13:17:53 -0700140 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
Stefano Babic4962e382011-10-17 00:07:43 +0000141 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
142 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
143 { IH_TYPE_INVALID, NULL, "Invalid Image", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100144 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic4962e382011-10-17 00:07:43 +0000145 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100146 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
147 { IH_TYPE_SCRIPT, "script", "Script", },
148 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000149 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100150 { -1, "", "", },
151};
152
Mike Frysinger7edb1862010-10-20 07:17:39 -0400153static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100154 { IH_COMP_NONE, "none", "uncompressed", },
155 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
156 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200157 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100158 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100159 { -1, "", "", },
160};
161
Stephen Warren712fbcf2011-10-18 11:11:49 +0000162uint32_t crc32(uint32_t, const unsigned char *, uint);
163uint32_t crc32_wd(uint32_t, const unsigned char *, uint, uint);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100164#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000165static void genimg_print_time(time_t timestamp);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100166#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100167
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100168/*****************************************************************************/
169/* Legacy format routines */
170/*****************************************************************************/
Stephen Warren712fbcf2011-10-18 11:11:49 +0000171int image_check_hcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100172{
173 ulong hcrc;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000174 ulong len = image_get_header_size();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100175 image_header_t header;
176
177 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000178 memmove(&header, (char *)hdr, image_get_header_size());
179 image_set_hcrc(&header, 0);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100180
Stephen Warren712fbcf2011-10-18 11:11:49 +0000181 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100182
Stephen Warren712fbcf2011-10-18 11:11:49 +0000183 return (hcrc == image_get_hcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100184}
185
Stephen Warren712fbcf2011-10-18 11:11:49 +0000186int image_check_dcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100187{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000188 ulong data = image_get_data(hdr);
189 ulong len = image_get_data_size(hdr);
190 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100191
Stephen Warren712fbcf2011-10-18 11:11:49 +0000192 return (dcrc == image_get_dcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100193}
194
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100195/**
196 * image_multi_count - get component (sub-image) count
197 * @hdr: pointer to the header of the multi component image
198 *
199 * image_multi_count() returns number of components in a multi
200 * component image.
201 *
202 * Note: no checking of the image type is done, caller must pass
203 * a valid multi component image.
204 *
205 * returns:
206 * number of components
207 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000208ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100209{
210 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100211 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100212
213 /* get start of the image payload, which in case of multi
214 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000215 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100216
217 /* count non empty slots */
218 for (i = 0; size[i]; ++i)
219 count++;
220
221 return count;
222}
223
224/**
225 * image_multi_getimg - get component data address and size
226 * @hdr: pointer to the header of the multi component image
227 * @idx: index of the requested component
228 * @data: pointer to a ulong variable, will hold component data address
229 * @len: pointer to a ulong variable, will hold component size
230 *
231 * image_multi_getimg() returns size and data address for the requested
232 * component in a multi component image.
233 *
234 * Note: no checking of the image type is done, caller must pass
235 * a valid multi component image.
236 *
237 * returns:
238 * data address and size of the component, if idx is valid
239 * 0 in data and len, if idx is out of range
240 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000241void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100242 ulong *data, ulong *len)
243{
244 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100245 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700246 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100247
248 /* get number of component */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000249 count = image_multi_count(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100250
251 /* get start of the image payload, which in case of multi
252 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000253 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100254
255 /* get address of the proper component data start, which means
256 * skipping sizes table (add 1 for last, null entry) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000257 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100258
259 if (idx < count) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000260 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100261 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100262
263 /* go over all indices preceding requested component idx */
264 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700265 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000266 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100267 }
268
269 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700270 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100271 } else {
272 *len = 0;
273 *data = 0;
274 }
275}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100276
Stephen Warren712fbcf2011-10-18 11:11:49 +0000277static void image_print_type(const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100278{
279 const char *os, *arch, *type, *comp;
280
Stephen Warren712fbcf2011-10-18 11:11:49 +0000281 os = genimg_get_os_name(image_get_os(hdr));
282 arch = genimg_get_arch_name(image_get_arch(hdr));
283 type = genimg_get_type_name(image_get_type(hdr));
284 comp = genimg_get_comp_name(image_get_comp(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100285
Stephen Warren712fbcf2011-10-18 11:11:49 +0000286 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100287}
288
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100289/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200290 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200291 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100292 * @p: pointer to prefix string
293 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200294 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100295 * The routine prints out all header fields followed by the size/offset data
296 * for MULTI/SCRIPT images.
297 *
298 * returns:
299 * no returned results
300 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000301void image_print_contents(const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100302{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200303 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200304 const char *p;
305
306#ifdef USE_HOSTCC
307 p = "";
308#else
309 p = " ";
310#endif
311
Stephen Warren712fbcf2011-10-18 11:11:49 +0000312 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100313#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000314 printf("%sCreated: ", p);
315 genimg_print_time((time_t)image_get_time(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100316#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +0000317 printf("%sImage Type: ", p);
318 image_print_type(hdr);
319 printf("%sData Size: ", p);
320 genimg_print_size(image_get_data_size(hdr));
321 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
322 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100323
Stephen Warren712fbcf2011-10-18 11:11:49 +0000324 if (image_check_type(hdr, IH_TYPE_MULTI) ||
325 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100326 int i;
327 ulong data, len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000328 ulong count = image_multi_count(hdr);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100329
Stephen Warren712fbcf2011-10-18 11:11:49 +0000330 printf("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100331 for (i = 0; i < count; i++) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000332 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100333
Stephen Warren712fbcf2011-10-18 11:11:49 +0000334 printf("%s Image %d: ", p, i);
335 genimg_print_size(len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100336
Stephen Warren712fbcf2011-10-18 11:11:49 +0000337 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100338 /*
339 * the user may need to know offsets
340 * if planning to do something with
341 * multiple files
342 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000343 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100344 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100345 }
346 }
347}
348
Marian Balakowicz570abb02008-02-29 15:59:59 +0100349
350#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100351/**
352 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100353 * @rd_addr: ramdisk image start address
354 * @arch: expected ramdisk architecture
355 * @verify: checksum verification flag
356 *
357 * image_get_ramdisk() returns a pointer to the verified ramdisk image
358 * header. Routine receives image start address and expected architecture
359 * flag. Verification done covers data and header integrity and os/type/arch
360 * fields checking.
361 *
362 * If dataflash support is enabled routine checks for dataflash addresses
363 * and handles required dataflash reads.
364 *
365 * returns:
366 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600367 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100368 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000369static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100370 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100371{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200372 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100373
Stephen Warren712fbcf2011-10-18 11:11:49 +0000374 if (!image_check_magic(rd_hdr)) {
375 puts("Bad Magic Number\n");
Simon Glass770605e2012-02-13 13:51:18 +0000376 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
Kumar Gala274cea22008-02-27 21:51:46 -0600377 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100378 }
379
Stephen Warren712fbcf2011-10-18 11:11:49 +0000380 if (!image_check_hcrc(rd_hdr)) {
381 puts("Bad Header Checksum\n");
Simon Glass770605e2012-02-13 13:51:18 +0000382 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600383 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100384 }
385
Simon Glass770605e2012-02-13 13:51:18 +0000386 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000387 image_print_contents(rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100388
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100389 if (verify) {
390 puts(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000391 if (!image_check_dcrc(rd_hdr)) {
392 puts("Bad Data CRC\n");
Simon Glass770605e2012-02-13 13:51:18 +0000393 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600394 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100395 }
396 puts("OK\n");
397 }
398
Simon Glass770605e2012-02-13 13:51:18 +0000399 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100400
Stephen Warren712fbcf2011-10-18 11:11:49 +0000401 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
402 !image_check_arch(rd_hdr, arch) ||
403 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
404 printf("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100405 genimg_get_arch_name(arch));
Simon Glass770605e2012-02-13 13:51:18 +0000406 bootstage_error(BOOTSTAGE_ID_RAMDISK);
Kumar Gala274cea22008-02-27 21:51:46 -0600407 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100408 }
409
410 return rd_hdr;
411}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100412#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100413
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100414/*****************************************************************************/
415/* Shared dual-format routines */
416/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100417#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +0000418int getenv_yesno(char *var)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100419{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000420 char *s = getenv(var);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100421 return (s && (*s == 'n')) ? 0 : 1;
422}
423
424ulong getenv_bootm_low(void)
425{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000426 char *s = getenv("bootm_low");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100427 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000428 ulong tmp = simple_strtoul(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100429 return tmp;
430 }
431
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200432#if defined(CONFIG_SYS_SDRAM_BASE)
433 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100434#elif defined(CONFIG_ARM)
435 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100436#else
437 return 0;
438#endif
439}
440
Becky Bruce391fd932008-06-09 20:37:18 -0500441phys_size_t getenv_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100442{
Matthew McClintockc519fac2010-07-08 10:11:08 -0500443 phys_size_t tmp;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000444 char *s = getenv("bootm_size");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100445 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000446 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100447 return tmp;
448 }
Matthew McClintockc519fac2010-07-08 10:11:08 -0500449 s = getenv("bootm_low");
450 if (s)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000451 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintockc519fac2010-07-08 10:11:08 -0500452 else
453 tmp = 0;
454
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100455
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100456#if defined(CONFIG_ARM)
Matthew McClintockc519fac2010-07-08 10:11:08 -0500457 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100458#else
Matthew McClintockc519fac2010-07-08 10:11:08 -0500459 return gd->bd->bi_memsize - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100460#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100461}
462
Grant Likelyc3624e62011-03-28 09:58:43 +0000463phys_size_t getenv_bootm_mapsize(void)
464{
465 phys_size_t tmp;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000466 char *s = getenv("bootm_mapsize");
Grant Likelyc3624e62011-03-28 09:58:43 +0000467 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000468 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likelyc3624e62011-03-28 09:58:43 +0000469 return tmp;
470 }
471
472#if defined(CONFIG_SYS_BOOTMAPSZ)
473 return CONFIG_SYS_BOOTMAPSZ;
474#else
475 return getenv_bootm_size();
476#endif
477}
478
Stephen Warren712fbcf2011-10-18 11:11:49 +0000479void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100480{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400481 if (to == from)
482 return;
483
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100484#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
485 while (len > 0) {
486 size_t tail = (len > chunksz) ? chunksz : len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000487 WATCHDOG_RESET();
488 memmove(to, from, tail);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100489 to += tail;
490 from += tail;
491 len -= tail;
492 }
493#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000494 memmove(to, from, len);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100495#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
496}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100497#endif /* !USE_HOSTCC */
498
Stephen Warren712fbcf2011-10-18 11:11:49 +0000499void genimg_print_size(uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100500{
501#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +0000502 printf("%d Bytes = ", size);
503 print_size(size, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100504#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000505 printf("%d Bytes = %.2f kB = %.2f MB\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100506 size, (double)size / 1.024e3,
507 (double)size / 1.048576e6);
508#endif
509}
510
511#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000512static void genimg_print_time(time_t timestamp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100513{
514#ifndef USE_HOSTCC
515 struct rtc_time tm;
516
Stephen Warren712fbcf2011-10-18 11:11:49 +0000517 to_tm(timestamp, &tm);
518 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100519 tm.tm_year, tm.tm_mon, tm.tm_mday,
520 tm.tm_hour, tm.tm_min, tm.tm_sec);
521#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000522 printf("%s", ctime(&timestamp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100523#endif
524}
525#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
526
527/**
528 * get_table_entry_name - translate entry id to long name
529 * @table: pointer to a translation table for entries of a specific type
530 * @msg: message to be returned when translation fails
531 * @id: entry id to be translated
532 *
533 * get_table_entry_name() will go over translation table trying to find
534 * entry that matches given id. If matching entry is found, its long
535 * name is returned to the caller.
536 *
537 * returns:
538 * long entry name if translation succeeds
539 * msg otherwise
540 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400541char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100542{
543 for (; table->id >= 0; ++table) {
544 if (table->id == id)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200545#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Woode3d1ac72009-04-02 16:15:10 -0500546 return table->lname;
547#else
548 return table->lname + gd->reloc_off;
549#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100550 }
551 return (msg);
552}
553
Stephen Warren712fbcf2011-10-18 11:11:49 +0000554const char *genimg_get_os_name(uint8_t os)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100555{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000556 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100557}
558
Stephen Warren712fbcf2011-10-18 11:11:49 +0000559const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100560{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000561 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
562 arch));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100563}
564
Stephen Warren712fbcf2011-10-18 11:11:49 +0000565const char *genimg_get_type_name(uint8_t type)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100566{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000567 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100568}
569
Stephen Warren712fbcf2011-10-18 11:11:49 +0000570const char *genimg_get_comp_name(uint8_t comp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100571{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000572 return (get_table_entry_name(uimage_comp, "Unknown Compression",
573 comp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100574}
575
576/**
577 * get_table_entry_id - translate short entry name to id
578 * @table: pointer to a translation table for entries of a specific type
579 * @table_name: to be used in case of error
580 * @name: entry short name to be translated
581 *
582 * get_table_entry_id() will go over translation table trying to find
583 * entry that matches given short name. If matching entry is found,
584 * its id returned to the caller.
585 *
586 * returns:
587 * entry id if translation succeeds
588 * -1 otherwise
589 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400590int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100591 const char *table_name, const char *name)
592{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400593 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100594#ifdef USE_HOSTCC
595 int first = 1;
596
597 for (t = table; t->id >= 0; ++t) {
598 if (t->sname && strcasecmp(t->sname, name) == 0)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000599 return(t->id);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100600 }
601
Stephen Warren712fbcf2011-10-18 11:11:49 +0000602 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100603 for (t = table; t->id >= 0; ++t) {
604 if (t->sname == NULL)
605 continue;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000606 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100607 first = 0;
608 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000609 fprintf(stderr, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100610#else
611 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200612#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Woode3d1ac72009-04-02 16:15:10 -0500613 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200614#else
615 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500616#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100617 return (t->id);
618 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000619 debug("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100620#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100621 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100622}
623
Stephen Warren712fbcf2011-10-18 11:11:49 +0000624int genimg_get_os_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100625{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000626 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100627}
628
Stephen Warren712fbcf2011-10-18 11:11:49 +0000629int genimg_get_arch_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100630{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000631 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100632}
633
Stephen Warren712fbcf2011-10-18 11:11:49 +0000634int genimg_get_type_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100635{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000636 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100637}
638
Stephen Warren712fbcf2011-10-18 11:11:49 +0000639int genimg_get_comp_id(const char *name)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100640{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000641 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100642}
643
644#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100645/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100646 * genimg_get_format - get image format type
647 * @img_addr: image start address
648 *
649 * genimg_get_format() checks whether provided address points to a valid
650 * legacy or FIT image.
651 *
652 * New uImage format and FDT blob are based on a libfdt. FDT blob
653 * may be passed directly or embedded in a FIT image. In both situations
654 * genimg_get_format() must be able to dectect libfdt header.
655 *
656 * returns:
657 * image format type or IMAGE_FORMAT_INVALID if no image is present
658 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000659int genimg_get_format(void *img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100660{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200661 ulong format = IMAGE_FORMAT_INVALID;
662 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100663#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200664 char *fit_hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100665#endif
666
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200667 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100668 if (image_check_magic(hdr))
669 format = IMAGE_FORMAT_LEGACY;
670#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
671 else {
672 fit_hdr = (char *)img_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000673 if (fdt_check_header(fit_hdr) == 0)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100674 format = IMAGE_FORMAT_FIT;
675 }
676#endif
677
678 return format;
679}
680
681/**
682 * genimg_get_image - get image from special storage (if necessary)
683 * @img_addr: image start address
684 *
685 * genimg_get_image() checks if provided image start adddress is located
686 * in a dataflash storage. If so, image is moved to a system RAM memory.
687 *
688 * returns:
689 * image start address after possible relocation from special storage
690 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000691ulong genimg_get_image(ulong img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100692{
693 ulong ram_addr = img_addr;
694
695#ifdef CONFIG_HAS_DATAFLASH
696 ulong h_size, d_size;
697
Stephen Warren712fbcf2011-10-18 11:11:49 +0000698 if (addr_dataflash(img_addr)) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100699 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200700 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100701
702 /* get header size */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000703 h_size = image_get_header_size();
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100704#if defined(CONFIG_FIT)
705 if (sizeof(struct fdt_header) > h_size)
706 h_size = sizeof(struct fdt_header);
707#endif
708
709 /* read in header */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000710 debug(" Reading image header from dataflash address "
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100711 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
712
Stephen Warren712fbcf2011-10-18 11:11:49 +0000713 read_dataflash(img_addr, h_size, (char *)ram_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100714
715 /* get data size */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000716 switch (genimg_get_format((void *)ram_addr)) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100717 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000718 d_size = image_get_data_size(
719 (const image_header_t *)ram_addr);
720 debug(" Legacy format image found at 0x%08lx, "
721 "size 0x%08lx\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100722 ram_addr, d_size);
723 break;
724#if defined(CONFIG_FIT)
725 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000726 d_size = fit_get_size((const void *)ram_addr) - h_size;
727 debug(" FIT/FDT format image found at 0x%08lx, "
728 "size 0x%08lx\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100729 ram_addr, d_size);
730 break;
731#endif
732 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000733 printf(" No valid image found at 0x%08lx\n",
734 img_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100735 return ram_addr;
736 }
737
738 /* read in image data */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000739 debug(" Reading image remaining data from dataflash address "
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100740 "%08lx to RAM address %08lx\n", img_addr + h_size,
741 ram_addr + h_size);
742
Stephen Warren712fbcf2011-10-18 11:11:49 +0000743 read_dataflash(img_addr + h_size, d_size,
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100744 (char *)(ram_addr + h_size));
745
746 }
747#endif /* CONFIG_HAS_DATAFLASH */
748
749 return ram_addr;
750}
751
752/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100753 * fit_has_config - check if there is a valid FIT configuration
754 * @images: pointer to the bootm command headers structure
755 *
756 * fit_has_config() checks if there is a FIT configuration in use
757 * (if FTI support is present).
758 *
759 * returns:
760 * 0, no FIT support or no configuration found
761 * 1, configuration found
762 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000763int genimg_has_config(bootm_headers_t *images)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100764{
765#if defined(CONFIG_FIT)
766 if (images->fit_uname_cfg)
767 return 1;
768#endif
769 return 0;
770}
771
772/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100773 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100774 * @argc: command argument count
775 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100776 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100777 * @arch: expected ramdisk architecture
778 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
779 * @rd_end: pointer to a ulong variable, will hold ramdisk end
780 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100781 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100782 * Curently supported are the following ramdisk sources:
783 * - multicomponent kernel/ramdisk image,
784 * - commandline provided address of decicated ramdisk image.
785 *
786 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100787 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100788 * rd_start and rd_end are set to ramdisk start/end addresses if
789 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100790 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500791 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100792 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100793 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000794int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100795 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100796{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100797 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100798 ulong rd_data, rd_len;
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200799 const image_header_t *rd_hdr;
Marek Vasut017e1f32012-03-18 11:47:58 +0000800 char *end;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100801#if defined(CONFIG_FIT)
802 void *fit_hdr;
803 const char *fit_uname_config = NULL;
804 const char *fit_uname_ramdisk = NULL;
805 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100806 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100807 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100808 const void *data;
809 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100810#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100811
Marian Balakowiczc8779642008-03-12 10:12:37 +0100812 *rd_start = 0;
813 *rd_end = 0;
814
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100815 /*
816 * Look for a '-' which indicates to ignore the
817 * ramdisk argument
818 */
819 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000820 debug("## Skipping init Ramdisk\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100821 rd_len = rd_data = 0;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000822 } else if (argc >= 3 || genimg_has_config(images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100823#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100824 if (argc >= 3) {
825 /*
826 * If the init ramdisk comes from the FIT image and
827 * the FIT image address is omitted in the command
828 * line argument, try to use os FIT image address or
829 * default load address.
830 */
831 if (images->fit_uname_os)
832 default_addr = (ulong)images->fit_hdr_os;
833 else
834 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100835
Stephen Warren712fbcf2011-10-18 11:11:49 +0000836 if (fit_parse_conf(argv[2], default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100837 &rd_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000838 debug("* ramdisk: config '%s' from image at "
839 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100840 fit_uname_config, rd_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000841 } else if (fit_parse_subimage(argv[2], default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100842 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000843 debug("* ramdisk: subimage '%s' from image at "
844 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100845 fit_uname_ramdisk, rd_addr);
846 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100847#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100848 {
849 rd_addr = simple_strtoul(argv[2], NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000850 debug("* ramdisk: cmdline image address = "
851 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100852 rd_addr);
853 }
854#if defined(CONFIG_FIT)
855 } else {
856 /* use FIT configuration provided in first bootm
857 * command argument
858 */
859 rd_addr = (ulong)images->fit_hdr_os;
860 fit_uname_config = images->fit_uname_cfg;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000861 debug("* ramdisk: using config '%s' from image "
862 "at 0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100863 fit_uname_config, rd_addr);
864
865 /*
866 * Check whether configuration has ramdisk defined,
867 * if not, don't try to use it, quit silently.
868 */
869 fit_hdr = (void *)rd_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000870 cfg_noffset = fit_conf_get_node(fit_hdr,
871 fit_uname_config);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100872 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000873 debug("* ramdisk: no such config\n");
Michal Simekc78fce62008-07-11 10:43:13 +0200874 return 1;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100875 }
876
Stephen Warren712fbcf2011-10-18 11:11:49 +0000877 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
878 cfg_noffset);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100879 if (rd_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000880 debug("* ramdisk: no ramdisk in config\n");
Peter Tyser41266c92008-08-05 10:51:57 -0500881 return 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100882 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100883 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100884#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100885
886 /* copy from dataflash if needed */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000887 rd_addr = genimg_get_image(rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100888
889 /*
890 * Check if there is an initrd image at the
891 * address provided in the second bootm argument
892 * check image type, for FIT images get FIT node.
893 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000894 switch (genimg_get_format((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100895 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000896 printf("## Loading init Ramdisk from Legacy "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100897 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100898
Simon Glass770605e2012-02-13 13:51:18 +0000899 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000900 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100901 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100902
Marian Balakowiczc8779642008-03-12 10:12:37 +0100903 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600904 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600905
Stephen Warren712fbcf2011-10-18 11:11:49 +0000906 rd_data = image_get_data(rd_hdr);
907 rd_len = image_get_data_size(rd_hdr);
908 rd_load = image_get_load(rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100909 break;
910#if defined(CONFIG_FIT)
911 case IMAGE_FORMAT_FIT:
912 fit_hdr = (void *)rd_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000913 printf("## Loading init Ramdisk from FIT "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100914 "Image at %08lx ...\n", rd_addr);
915
Simon Glass770605e2012-02-13 13:51:18 +0000916 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000917 if (!fit_check_format(fit_hdr)) {
918 puts("Bad FIT ramdisk image format!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000919 bootstage_error(
Simon Glassaacc8c12011-12-10 11:08:02 +0000920 BOOTSTAGE_ID_FIT_RD_FORMAT);
Michal Simekc78fce62008-07-11 10:43:13 +0200921 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100922 }
Simon Glass770605e2012-02-13 13:51:18 +0000923 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100924
925 if (!fit_uname_ramdisk) {
926 /*
927 * no ramdisk image node unit name, try to get config
928 * node first. If config unit node name is NULL
929 * fit_conf_get_node() will try to find default config node
930 */
Simon Glass770605e2012-02-13 13:51:18 +0000931 bootstage_mark(
Simon Glassaacc8c12011-12-10 11:08:02 +0000932 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000933 cfg_noffset = fit_conf_get_node(fit_hdr,
934 fit_uname_config);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100935 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000936 puts("Could not find configuration "
937 "node\n");
Simon Glass770605e2012-02-13 13:51:18 +0000938 bootstage_error(
Simon Glassaacc8c12011-12-10 11:08:02 +0000939 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
Michal Simekc78fce62008-07-11 10:43:13 +0200940 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100941 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000942 fit_uname_config = fdt_get_name(fit_hdr,
943 cfg_noffset, NULL);
944 printf(" Using '%s' configuration\n",
945 fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100946
Stephen Warren712fbcf2011-10-18 11:11:49 +0000947 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
948 cfg_noffset);
949 fit_uname_ramdisk = fit_get_name(fit_hdr,
950 rd_noffset, NULL);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100951 } else {
952 /* get ramdisk component image node offset */
Simon Glass770605e2012-02-13 13:51:18 +0000953 bootstage_mark(
Simon Glassaacc8c12011-12-10 11:08:02 +0000954 BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000955 rd_noffset = fit_image_get_node(fit_hdr,
956 fit_uname_ramdisk);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100957 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100958 if (rd_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000959 puts("Could not find subimage node\n");
Simon Glass770605e2012-02-13 13:51:18 +0000960 bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
Michal Simekc78fce62008-07-11 10:43:13 +0200961 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100962 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100963
Stephen Warren712fbcf2011-10-18 11:11:49 +0000964 printf(" Trying '%s' ramdisk subimage\n",
965 fit_uname_ramdisk);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100966
Simon Glass770605e2012-02-13 13:51:18 +0000967 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000968 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
969 images->verify))
Michal Simekc78fce62008-07-11 10:43:13 +0200970 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100971
972 /* get ramdisk image data address and length */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000973 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
974 &size)) {
975 puts("Could not find ramdisk subimage data!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000976 bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
Michal Simekc78fce62008-07-11 10:43:13 +0200977 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100978 }
Simon Glass770605e2012-02-13 13:51:18 +0000979 bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100980
981 rd_data = (ulong)data;
982 rd_len = size;
983
Stephen Warren712fbcf2011-10-18 11:11:49 +0000984 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
985 puts("Can't get ramdisk subimage load "
986 "address!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000987 bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
Michal Simekc78fce62008-07-11 10:43:13 +0200988 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100989 }
Simon Glass770605e2012-02-13 13:51:18 +0000990 bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100991
992 images->fit_hdr_rd = fit_hdr;
993 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100994 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100995 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100996#endif
997 default:
Marek Vasut017e1f32012-03-18 11:47:58 +0000998#ifdef CONFIG_SUPPORT_RAW_INITRD
999 if (argc >= 3 && (end = strchr(argv[2], ':'))) {
1000 rd_len = simple_strtoul(++end, NULL, 16);
1001 rd_data = rd_addr;
1002 } else
1003#endif
1004 {
1005 puts("Wrong Ramdisk Image Format\n");
1006 rd_data = rd_len = rd_load = 0;
1007 return 1;
1008 }
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001009 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001010 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +00001011 image_check_type(&images->legacy_hdr_os_copy,
1012 IH_TYPE_MULTI)) {
1013
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001014 /*
1015 * Now check if we have a legacy mult-component image,
1016 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001017 */
Simon Glass770605e2012-02-13 13:51:18 +00001018 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001019 printf("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001020 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001021 (ulong)images->legacy_hdr_os);
1022
Stephen Warren712fbcf2011-10-18 11:11:49 +00001023 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001024 } else {
1025 /*
1026 * no initrd image
1027 */
Simon Glass770605e2012-02-13 13:51:18 +00001028 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001029 rd_len = rd_data = 0;
1030 }
1031
1032 if (!rd_data) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001033 debug("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001034 } else {
1035 *rd_start = rd_data;
1036 *rd_end = rd_data + rd_len;
1037 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001038 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001039 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -06001040
1041 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001042}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001043
John Rigbyfca43cc2010-10-13 13:57:35 -06001044#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001045/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001046 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001047 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001048 * @rd_data: ramdisk data start address
1049 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001050 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1051 * start address (after possible relocation)
1052 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1053 * end address (after possible relocation)
1054 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001055 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001056 * variable and if requested ramdisk data is moved to a specified location.
1057 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001058 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1059 * start/end addresses if ramdisk image start and len were provided,
1060 * otherwise set initrd_start and initrd_end set to zeros.
1061 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001062 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001063 * 0 - success
1064 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001065 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001066int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001067 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001068{
1069 char *s;
1070 ulong initrd_high;
1071 int initrd_copy_to_ram = 1;
1072
Stephen Warren712fbcf2011-10-18 11:11:49 +00001073 if ((s = getenv("initrd_high")) != NULL) {
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001074 /* a value of "no" or a similar string will act like 0,
1075 * turning the "load high" feature off. This is intentional.
1076 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001077 initrd_high = simple_strtoul(s, NULL, 16);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001078 if (initrd_high == ~0)
1079 initrd_copy_to_ram = 0;
1080 } else {
1081 /* not set, no restrictions to load high */
1082 initrd_high = ~0;
1083 }
1084
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001085
1086#ifdef CONFIG_LOGBUFFER
1087 /* Prevent initrd from overwriting logbuffer */
1088 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1089#endif
1090
Stephen Warren712fbcf2011-10-18 11:11:49 +00001091 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001092 initrd_high, initrd_copy_to_ram);
1093
1094 if (rd_data) {
1095 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001096 debug(" in-place initrd\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001097 *initrd_start = rd_data;
1098 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001099 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001100 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001101 if (initrd_high)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001102 *initrd_start = (ulong)lmb_alloc_base(lmb,
1103 rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001104 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001105 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1106 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001107
Kumar Galae822d7f2008-02-27 21:51:49 -06001108 if (*initrd_start == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001109 puts("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001110 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001111 }
Simon Glass770605e2012-02-13 13:51:18 +00001112 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001113
1114 *initrd_end = *initrd_start + rd_len;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001115 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001116 *initrd_start, *initrd_end);
1117
Stephen Warren712fbcf2011-10-18 11:11:49 +00001118 memmove_wd((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001119 (void *)rd_data, rd_len, CHUNKSZ);
1120
Kumar Gala3b200112011-12-07 04:42:58 +00001121#ifdef CONFIG_MP
1122 /*
1123 * Ensure the image is flushed to memory to handle
1124 * AMP boot scenarios in which we might not be
1125 * HW cache coherent
1126 */
1127 flush_cache((unsigned long)*initrd_start, rd_len);
1128#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +00001129 puts("OK\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001130 }
1131 } else {
1132 *initrd_start = 0;
1133 *initrd_end = 0;
1134 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001135 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001136 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001137
Kumar Galae822d7f2008-02-27 21:51:49 -06001138 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001139
Kumar Galae822d7f2008-02-27 21:51:49 -06001140error:
1141 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001142}
John Rigbyfca43cc2010-10-13 13:57:35 -06001143#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001144
Kumar Gala06a09912008-08-15 08:24:38 -05001145#ifdef CONFIG_OF_LIBFDT
Stephen Warren712fbcf2011-10-18 11:11:49 +00001146static void fdt_error(const char *msg)
Kumar Gala06a09912008-08-15 08:24:38 -05001147{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001148 puts("ERROR: ");
1149 puts(msg);
1150 puts(" - must RESET the board to recover.\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001151}
1152
Stephen Warren712fbcf2011-10-18 11:11:49 +00001153static const image_header_t *image_get_fdt(ulong fdt_addr)
Kumar Gala06a09912008-08-15 08:24:38 -05001154{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001155 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001156
Stephen Warren712fbcf2011-10-18 11:11:49 +00001157 image_print_contents(fdt_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001158
Stephen Warren712fbcf2011-10-18 11:11:49 +00001159 puts(" Verifying Checksum ... ");
1160 if (!image_check_hcrc(fdt_hdr)) {
1161 fdt_error("fdt header checksum invalid");
Kumar Gala06a09912008-08-15 08:24:38 -05001162 return NULL;
1163 }
1164
Stephen Warren712fbcf2011-10-18 11:11:49 +00001165 if (!image_check_dcrc(fdt_hdr)) {
1166 fdt_error("fdt checksum invalid");
Kumar Gala06a09912008-08-15 08:24:38 -05001167 return NULL;
1168 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001169 puts("OK\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001170
Stephen Warren712fbcf2011-10-18 11:11:49 +00001171 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1172 fdt_error("uImage is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001173 return NULL;
1174 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001175 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1176 fdt_error("uImage is compressed");
Kumar Gala06a09912008-08-15 08:24:38 -05001177 return NULL;
1178 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001179 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1180 fdt_error("uImage data is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001181 return NULL;
1182 }
1183 return fdt_hdr;
1184}
1185
1186/**
1187 * fit_check_fdt - verify FIT format FDT subimage
1188 * @fit_hdr: pointer to the FIT header
1189 * fdt_noffset: FDT subimage node offset within FIT image
1190 * @verify: data CRC verification flag
1191 *
1192 * fit_check_fdt() verifies integrity of the FDT subimage and from
1193 * specified FIT image.
1194 *
1195 * returns:
1196 * 1, on success
1197 * 0, on failure
1198 */
1199#if defined(CONFIG_FIT)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001200static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
Kumar Gala06a09912008-08-15 08:24:38 -05001201{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001202 fit_image_print(fit, fdt_noffset, " ");
Kumar Gala06a09912008-08-15 08:24:38 -05001203
1204 if (verify) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001205 puts(" Verifying Hash Integrity ... ");
1206 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1207 fdt_error("Bad Data Hash");
Kumar Gala06a09912008-08-15 08:24:38 -05001208 return 0;
1209 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001210 puts("OK\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001211 }
1212
Stephen Warren712fbcf2011-10-18 11:11:49 +00001213 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1214 fdt_error("Not a FDT image");
Kumar Gala06a09912008-08-15 08:24:38 -05001215 return 0;
1216 }
1217
Stephen Warren712fbcf2011-10-18 11:11:49 +00001218 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1219 fdt_error("FDT image is compressed");
Kumar Gala06a09912008-08-15 08:24:38 -05001220 return 0;
1221 }
1222
1223 return 1;
1224}
1225#endif /* CONFIG_FIT */
1226
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001227#ifndef CONFIG_SYS_FDT_PAD
1228#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Gala06a09912008-08-15 08:24:38 -05001229#endif
1230
Grant Likely55b0a392011-03-28 09:59:01 +00001231#if defined(CONFIG_OF_LIBFDT)
1232/**
1233 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1234 * @lmb: pointer to lmb handle, will be used for memory mgmt
1235 * @fdt_blob: pointer to fdt blob base address
1236 *
1237 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1238 * memreserve regions prevents u-boot from using them to store the initrd
1239 * or the fdt blob.
1240 */
1241void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1242{
1243 uint64_t addr, size;
1244 int i, total;
1245
Stephen Warren712fbcf2011-10-18 11:11:49 +00001246 if (fdt_check_header(fdt_blob) != 0)
Grant Likely55b0a392011-03-28 09:59:01 +00001247 return;
1248
1249 total = fdt_num_mem_rsv(fdt_blob);
1250 for (i = 0; i < total; i++) {
1251 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1252 continue;
1253 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1254 (unsigned long long)addr, (unsigned long long)size);
1255 lmb_reserve(lmb, addr, size);
1256 }
1257}
1258
Kumar Gala06a09912008-08-15 08:24:38 -05001259/**
1260 * boot_relocate_fdt - relocate flat device tree
1261 * @lmb: pointer to lmb handle, will be used for memory mgmt
Kumar Gala06a09912008-08-15 08:24:38 -05001262 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1263 * @of_size: pointer to a ulong variable, will hold fdt length
1264 *
Timur Tabi43b08af2010-05-24 15:10:25 -05001265 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1266 * relocates the of_flat_tree into that region, even if the fdt is already in
1267 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1268 * bytes.
Kumar Gala06a09912008-08-15 08:24:38 -05001269 *
1270 * of_flat_tree and of_size are set to final (after relocation) values
1271 *
1272 * returns:
1273 * 0 - success
1274 * 1 - failure
1275 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001276int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001277{
Timur Tabi43b08af2010-05-24 15:10:25 -05001278 void *fdt_blob = *of_flat_tree;
1279 void *of_start = 0;
David A. Longa28afca2011-07-09 16:40:19 -04001280 char *fdt_high;
Kumar Gala06a09912008-08-15 08:24:38 -05001281 ulong of_len = 0;
Timur Tabi43b08af2010-05-24 15:10:25 -05001282 int err;
David A. Longa28afca2011-07-09 16:40:19 -04001283 int disable_relocation = 0;
Kumar Gala06a09912008-08-15 08:24:38 -05001284
1285 /* nothing to do */
1286 if (*of_size == 0)
1287 return 0;
1288
Stephen Warren712fbcf2011-10-18 11:11:49 +00001289 if (fdt_check_header(fdt_blob) != 0) {
1290 fdt_error("image is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001291 goto error;
1292 }
1293
Timur Tabi43b08af2010-05-24 15:10:25 -05001294 /* position on a 4K boundary before the alloc_current */
1295 /* Pad the FDT by a specified amount */
1296 of_len = *of_size + CONFIG_SYS_FDT_PAD;
David A. Longa28afca2011-07-09 16:40:19 -04001297
1298 /* If fdt_high is set use it to select the relocation address */
1299 fdt_high = getenv("fdt_high");
1300 if (fdt_high) {
1301 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1302
1303 if (((ulong) desired_addr) == ~0UL) {
1304 /* All ones means use fdt in place */
Shawn Guofa34f6b2012-01-09 21:54:08 +00001305 of_start = fdt_blob;
1306 lmb_reserve(lmb, (ulong)of_start, of_len);
David A. Longa28afca2011-07-09 16:40:19 -04001307 disable_relocation = 1;
Shawn Guofa34f6b2012-01-09 21:54:08 +00001308 } else if (desired_addr) {
David A. Longa28afca2011-07-09 16:40:19 -04001309 of_start =
1310 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
Shawn Guofa34f6b2012-01-09 21:54:08 +00001311 (ulong)desired_addr);
1312 if (of_start == 0) {
David A. Longa28afca2011-07-09 16:40:19 -04001313 puts("Failed using fdt_high value for Device Tree");
1314 goto error;
1315 }
1316 } else {
1317 of_start =
Matthew McClintock1bb5e902011-07-18 13:08:05 +00001318 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
David A. Longa28afca2011-07-09 16:40:19 -04001319 }
1320 } else {
1321 of_start =
1322 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1323 getenv_bootm_mapsize()
1324 + getenv_bootm_low());
1325 }
Kumar Gala06a09912008-08-15 08:24:38 -05001326
Timur Tabi43b08af2010-05-24 15:10:25 -05001327 if (of_start == 0) {
1328 puts("device tree - allocation error\n");
1329 goto error;
Kumar Gala06a09912008-08-15 08:24:38 -05001330 }
1331
David A. Longa28afca2011-07-09 16:40:19 -04001332 if (disable_relocation) {
1333 /* We assume there is space after the existing fdt to use for padding */
1334 fdt_set_totalsize(of_start, of_len);
1335 printf(" Using Device Tree in place at %p, end %p\n",
1336 of_start, of_start + of_len - 1);
1337 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001338 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
David A. Longa28afca2011-07-09 16:40:19 -04001339 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
Timur Tabi43b08af2010-05-24 15:10:25 -05001340
Stephen Warren712fbcf2011-10-18 11:11:49 +00001341 printf(" Loading Device Tree to %p, end %p ... ",
David A. Longa28afca2011-07-09 16:40:19 -04001342 of_start, of_start + of_len - 1);
Timur Tabi43b08af2010-05-24 15:10:25 -05001343
Stephen Warren712fbcf2011-10-18 11:11:49 +00001344 err = fdt_open_into(fdt_blob, of_start, of_len);
David A. Longa28afca2011-07-09 16:40:19 -04001345 if (err != 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001346 fdt_error("fdt move failed");
David A. Longa28afca2011-07-09 16:40:19 -04001347 goto error;
1348 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001349 puts("OK\n");
Timur Tabi43b08af2010-05-24 15:10:25 -05001350 }
Timur Tabi43b08af2010-05-24 15:10:25 -05001351
1352 *of_flat_tree = of_start;
1353 *of_size = of_len;
1354
Kumar Gala54f9c862008-08-15 08:24:39 -05001355 set_working_fdt_addr(*of_flat_tree);
Kumar Gala06a09912008-08-15 08:24:38 -05001356 return 0;
1357
1358error:
1359 return 1;
1360}
Grant Likelyed59e582011-03-28 09:58:49 +00001361#endif /* CONFIG_OF_LIBFDT */
Kumar Gala06a09912008-08-15 08:24:38 -05001362
1363/**
1364 * boot_get_fdt - main fdt handling routine
1365 * @argc: command argument count
1366 * @argv: command argument list
1367 * @images: pointer to the bootm images structure
1368 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1369 * @of_size: pointer to a ulong variable, will hold fdt length
1370 *
1371 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1372 * Curently supported are the following ramdisk sources:
1373 * - multicomponent kernel/ramdisk image,
1374 * - commandline provided address of decicated ramdisk image.
1375 *
1376 * returns:
1377 * 0, if fdt image was found and valid, or skipped
1378 * of_flat_tree and of_size are set to fdt start address and length if
1379 * fdt image is found and valid
1380 *
1381 * 1, if fdt image is found but corrupted
1382 * of_flat_tree and of_size are set to 0 if no fdt exists
1383 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001384int boot_get_fdt(int flag, int argc, char * const argv[],
1385 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001386{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001387 const image_header_t *fdt_hdr;
Kumar Gala06a09912008-08-15 08:24:38 -05001388 ulong fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001389 char *fdt_blob = NULL;
Stephen Warrene37ae402011-11-01 06:28:21 +00001390 ulong image_start, image_data, image_end;
Kumar Gala06a09912008-08-15 08:24:38 -05001391 ulong load_start, load_end;
1392#if defined(CONFIG_FIT)
1393 void *fit_hdr;
1394 const char *fit_uname_config = NULL;
1395 const char *fit_uname_fdt = NULL;
1396 ulong default_addr;
1397 int cfg_noffset;
1398 int fdt_noffset;
1399 const void *data;
1400 size_t size;
1401#endif
1402
1403 *of_flat_tree = NULL;
1404 *of_size = 0;
1405
Stephen Warren712fbcf2011-10-18 11:11:49 +00001406 if (argc > 3 || genimg_has_config(images)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001407#if defined(CONFIG_FIT)
1408 if (argc > 3) {
1409 /*
1410 * If the FDT blob comes from the FIT image and the
1411 * FIT image address is omitted in the command line
1412 * argument, try to use ramdisk or os FIT image
1413 * address or default load address.
1414 */
1415 if (images->fit_uname_rd)
1416 default_addr = (ulong)images->fit_hdr_rd;
1417 else if (images->fit_uname_os)
1418 default_addr = (ulong)images->fit_hdr_os;
1419 else
1420 default_addr = load_addr;
1421
Stephen Warren712fbcf2011-10-18 11:11:49 +00001422 if (fit_parse_conf(argv[3], default_addr,
Kumar Gala06a09912008-08-15 08:24:38 -05001423 &fdt_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001424 debug("* fdt: config '%s' from image at "
1425 "0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001426 fit_uname_config, fdt_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001427 } else if (fit_parse_subimage(argv[3], default_addr,
Kumar Gala06a09912008-08-15 08:24:38 -05001428 &fdt_addr, &fit_uname_fdt)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001429 debug("* fdt: subimage '%s' from image at "
1430 "0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001431 fit_uname_fdt, fdt_addr);
1432 } else
1433#endif
1434 {
1435 fdt_addr = simple_strtoul(argv[3], NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001436 debug("* fdt: cmdline image address = "
1437 "0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001438 fdt_addr);
1439 }
1440#if defined(CONFIG_FIT)
1441 } else {
1442 /* use FIT configuration provided in first bootm
1443 * command argument
1444 */
1445 fdt_addr = (ulong)images->fit_hdr_os;
1446 fit_uname_config = images->fit_uname_cfg;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001447 debug("* fdt: using config '%s' from image "
1448 "at 0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001449 fit_uname_config, fdt_addr);
1450
1451 /*
1452 * Check whether configuration has FDT blob defined,
1453 * if not quit silently.
1454 */
1455 fit_hdr = (void *)fdt_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001456 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001457 fit_uname_config);
1458 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001459 debug("* fdt: no such config\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001460 return 0;
1461 }
1462
Stephen Warren712fbcf2011-10-18 11:11:49 +00001463 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001464 cfg_noffset);
1465 if (fdt_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001466 debug("* fdt: no fdt in config\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001467 return 0;
1468 }
1469 }
1470#endif
1471
Stephen Warren712fbcf2011-10-18 11:11:49 +00001472 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001473 fdt_addr);
1474
1475 /* copy from dataflash if needed */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001476 fdt_addr = genimg_get_image(fdt_addr);
Kumar Gala06a09912008-08-15 08:24:38 -05001477
1478 /*
1479 * Check if there is an FDT image at the
1480 * address provided in the second bootm argument
1481 * check image type, for FIT images get a FIT node.
1482 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001483 switch (genimg_get_format((void *)fdt_addr)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001484 case IMAGE_FORMAT_LEGACY:
1485 /* verify fdt_addr points to a valid image header */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001486 printf("## Flattened Device Tree from Legacy Image "
1487 "at %08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001488 fdt_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001489 fdt_hdr = image_get_fdt(fdt_addr);
Kumar Gala06a09912008-08-15 08:24:38 -05001490 if (!fdt_hdr)
1491 goto error;
1492
1493 /*
1494 * move image data to the load address,
1495 * make sure we don't overwrite initial image
1496 */
1497 image_start = (ulong)fdt_hdr;
Stephen Warrene37ae402011-11-01 06:28:21 +00001498 image_data = (ulong)image_get_data(fdt_hdr);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001499 image_end = image_get_image_end(fdt_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001500
Stephen Warren712fbcf2011-10-18 11:11:49 +00001501 load_start = image_get_load(fdt_hdr);
1502 load_end = load_start + image_get_data_size(fdt_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001503
Stephen Warrene37ae402011-11-01 06:28:21 +00001504 if (load_start == image_start ||
1505 load_start == image_data) {
1506 fdt_blob = (char *)image_data;
1507 break;
1508 }
1509
Kumar Gala06a09912008-08-15 08:24:38 -05001510 if ((load_start < image_end) && (load_end > image_start)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001511 fdt_error("fdt overwritten");
Kumar Gala06a09912008-08-15 08:24:38 -05001512 goto error;
1513 }
1514
Stephen Warren712fbcf2011-10-18 11:11:49 +00001515 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
Stephen Warrene37ae402011-11-01 06:28:21 +00001516 image_data, load_start);
Kumar Gala06a09912008-08-15 08:24:38 -05001517
Stephen Warren712fbcf2011-10-18 11:11:49 +00001518 memmove((void *)load_start,
Stephen Warrene37ae402011-11-01 06:28:21 +00001519 (void *)image_data,
Stephen Warren712fbcf2011-10-18 11:11:49 +00001520 image_get_data_size(fdt_hdr));
Kumar Gala06a09912008-08-15 08:24:38 -05001521
1522 fdt_blob = (char *)load_start;
1523 break;
1524 case IMAGE_FORMAT_FIT:
1525 /*
1526 * This case will catch both: new uImage format
1527 * (libfdt based) and raw FDT blob (also libfdt
1528 * based).
1529 */
1530#if defined(CONFIG_FIT)
1531 /* check FDT blob vs FIT blob */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001532 if (fit_check_format((const void *)fdt_addr)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001533 /*
1534 * FIT image
1535 */
1536 fit_hdr = (void *)fdt_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001537 printf("## Flattened Device Tree from FIT "
1538 "Image at %08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001539 fdt_addr);
1540
1541 if (!fit_uname_fdt) {
1542 /*
1543 * no FDT blob image node unit name,
1544 * try to get config node first. If
1545 * config unit node name is NULL
1546 * fit_conf_get_node() will try to
1547 * find default config node
1548 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001549 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001550 fit_uname_config);
1551
1552 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001553 fdt_error("Could not find "
1554 "configuration "
1555 "node\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001556 goto error;
1557 }
1558
Stephen Warren712fbcf2011-10-18 11:11:49 +00001559 fit_uname_config = fdt_get_name(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001560 cfg_noffset, NULL);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001561 printf(" Using '%s' configuration\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001562 fit_uname_config);
1563
Stephen Warren712fbcf2011-10-18 11:11:49 +00001564 fdt_noffset = fit_conf_get_fdt_node(
1565 fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001566 cfg_noffset);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001567 fit_uname_fdt = fit_get_name(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001568 fdt_noffset, NULL);
1569 } else {
1570 /* get FDT component image node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001571 fdt_noffset = fit_image_get_node(
1572 fit_hdr,
1573 fit_uname_fdt);
Kumar Gala06a09912008-08-15 08:24:38 -05001574 }
1575 if (fdt_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001576 fdt_error("Could not find subimage "
1577 "node\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001578 goto error;
1579 }
1580
Stephen Warren712fbcf2011-10-18 11:11:49 +00001581 printf(" Trying '%s' FDT blob subimage\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001582 fit_uname_fdt);
1583
Stephen Warren712fbcf2011-10-18 11:11:49 +00001584 if (!fit_check_fdt(fit_hdr, fdt_noffset,
Kumar Gala06a09912008-08-15 08:24:38 -05001585 images->verify))
1586 goto error;
1587
1588 /* get ramdisk image data address and length */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001589 if (fit_image_get_data(fit_hdr, fdt_noffset,
Kumar Gala06a09912008-08-15 08:24:38 -05001590 &data, &size)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001591 fdt_error("Could not find FDT "
1592 "subimage data");
Kumar Gala06a09912008-08-15 08:24:38 -05001593 goto error;
1594 }
1595
1596 /* verift that image data is a proper FDT blob */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001597 if (fdt_check_header((char *)data) != 0) {
1598 fdt_error("Subimage data is not a FTD");
Kumar Gala06a09912008-08-15 08:24:38 -05001599 goto error;
1600 }
1601
1602 /*
1603 * move image data to the load address,
1604 * make sure we don't overwrite initial image
1605 */
1606 image_start = (ulong)fit_hdr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001607 image_end = fit_get_end(fit_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001608
Stephen Warren712fbcf2011-10-18 11:11:49 +00001609 if (fit_image_get_load(fit_hdr, fdt_noffset,
Kumar Gala06a09912008-08-15 08:24:38 -05001610 &load_start) == 0) {
1611 load_end = load_start + size;
1612
1613 if ((load_start < image_end) &&
1614 (load_end > image_start)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001615 fdt_error("FDT overwritten");
Kumar Gala06a09912008-08-15 08:24:38 -05001616 goto error;
1617 }
1618
Stephen Warren712fbcf2011-10-18 11:11:49 +00001619 printf(" Loading FDT from 0x%08lx "
1620 "to 0x%08lx\n",
1621 (ulong)data,
1622 load_start);
Kumar Gala06a09912008-08-15 08:24:38 -05001623
Stephen Warren712fbcf2011-10-18 11:11:49 +00001624 memmove((void *)load_start,
Kumar Gala06a09912008-08-15 08:24:38 -05001625 (void *)data, size);
1626
1627 fdt_blob = (char *)load_start;
1628 } else {
1629 fdt_blob = (char *)data;
1630 }
1631
1632 images->fit_hdr_fdt = fit_hdr;
1633 images->fit_uname_fdt = fit_uname_fdt;
1634 images->fit_noffset_fdt = fdt_noffset;
1635 break;
1636 } else
1637#endif
1638 {
1639 /*
1640 * FDT blob
1641 */
1642 fdt_blob = (char *)fdt_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001643 debug("* fdt: raw FDT blob\n");
1644 printf("## Flattened Device Tree blob at "
1645 "%08lx\n", (long)fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001646 }
1647 break;
1648 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001649 puts("ERROR: Did not find a cmdline Flattened Device "
1650 "Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001651 goto error;
1652 }
1653
Simon Glass0ec2ce42011-09-23 06:22:04 +00001654 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001655
1656 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +00001657 image_check_type(&images->legacy_hdr_os_copy,
1658 IH_TYPE_MULTI)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001659
1660 ulong fdt_data, fdt_len;
1661
1662 /*
1663 * Now check if we have a legacy multi-component image,
1664 * get second entry data start address and len.
1665 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001666 printf("## Flattened Device Tree from multi "
Kumar Gala06a09912008-08-15 08:24:38 -05001667 "component Image at %08lX\n",
1668 (ulong)images->legacy_hdr_os);
1669
Stephen Warren712fbcf2011-10-18 11:11:49 +00001670 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1671 &fdt_len);
Kumar Gala06a09912008-08-15 08:24:38 -05001672 if (fdt_len) {
1673
1674 fdt_blob = (char *)fdt_data;
Simon Glass0ec2ce42011-09-23 06:22:04 +00001675 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001676
Stephen Warren712fbcf2011-10-18 11:11:49 +00001677 if (fdt_check_header(fdt_blob) != 0) {
1678 fdt_error("image is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001679 goto error;
1680 }
1681
John Rigbyd1263fc2010-10-13 13:57:32 -06001682 if (fdt_totalsize(fdt_blob) != fdt_len) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001683 fdt_error("fdt size != image size");
Kumar Gala06a09912008-08-15 08:24:38 -05001684 goto error;
1685 }
1686 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001687 debug("## No Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001688 return 0;
1689 }
1690 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001691 debug("## No Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001692 return 0;
1693 }
1694
1695 *of_flat_tree = fdt_blob;
John Rigbyd1263fc2010-10-13 13:57:32 -06001696 *of_size = fdt_totalsize(fdt_blob);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001697 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner52514692008-08-21 07:12:26 -07001698 (ulong)*of_flat_tree, *of_size);
Kumar Gala06a09912008-08-15 08:24:38 -05001699
1700 return 0;
1701
1702error:
1703 *of_flat_tree = 0;
1704 *of_size = 0;
1705 return 1;
1706}
1707#endif /* CONFIG_OF_LIBFDT */
1708
John Rigbyfca43cc2010-10-13 13:57:35 -06001709#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001710/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001711 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001712 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001713 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1714 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1715 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001716 * boot_get_cmdline() allocates space for kernel command line below
Grant Likely590d3ca2011-03-28 09:58:34 +00001717 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001718 * variable is present its contents is copied to allocated kernel
1719 * command line.
1720 *
1721 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001722 * 0 - success
1723 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001724 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001725int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001726{
1727 char *cmdline;
1728 char *s;
1729
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001730 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001731 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001732
1733 if (cmdline == NULL)
1734 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001735
1736 if ((s = getenv("bootargs")) == NULL)
1737 s = "";
1738
1739 strcpy(cmdline, s);
1740
1741 *cmd_start = (ulong) & cmdline[0];
1742 *cmd_end = *cmd_start + strlen(cmdline);
1743
Stephen Warren712fbcf2011-10-18 11:11:49 +00001744 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001745
Kumar Galae822d7f2008-02-27 21:51:49 -06001746 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001747}
John Rigbyfca43cc2010-10-13 13:57:35 -06001748#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001749
John Rigbyfca43cc2010-10-13 13:57:35 -06001750#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001751/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001752 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001753 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001754 * @kbd: double pointer to board info data
1755 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001756 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likely590d3ca2011-03-28 09:58:34 +00001757 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1758 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001759 *
1760 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001761 * 0 - success
1762 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001763 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001764int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001765{
Becky Bruce391fd932008-06-09 20:37:18 -05001766 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001767 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001768 if (*kbd == NULL)
1769 return -1;
1770
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001771 **kbd = *(gd->bd);
1772
Stephen Warren712fbcf2011-10-18 11:11:49 +00001773 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001774
1775#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1776 do_bdinfo(NULL, 0, 0, NULL);
1777#endif
1778
Kumar Galae822d7f2008-02-27 21:51:49 -06001779 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001780}
John Rigbyfca43cc2010-10-13 13:57:35 -06001781#endif /* CONFIG_SYS_BOOT_GET_KBD */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001782#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001783
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001784#if defined(CONFIG_FIT)
1785/*****************************************************************************/
1786/* New uImage format routines */
1787/*****************************************************************************/
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001788#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +00001789static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001790 ulong *addr, const char **name)
1791{
1792 const char *sep;
1793
1794 *addr = addr_curr;
1795 *name = NULL;
1796
Stephen Warren712fbcf2011-10-18 11:11:49 +00001797 sep = strchr(spec, sepc);
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001798 if (sep) {
1799 if (sep - spec > 0)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001800 *addr = simple_strtoul(spec, NULL, 16);
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001801
1802 *name = sep + 1;
1803 return 1;
1804 }
1805
1806 return 0;
1807}
1808
1809/**
1810 * fit_parse_conf - parse FIT configuration spec
1811 * @spec: input string, containing configuration spec
1812 * @add_curr: current image address (to be used as a possible default)
1813 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1814 * configuration
1815 * @conf_name double pointer to a char, will hold pointer to a configuration
1816 * unit name
1817 *
1818 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1819 * where <addr> is a FIT image address that contains configuration
1820 * with a <conf> unit name.
1821 *
1822 * Address part is optional, and if omitted default add_curr will
1823 * be used instead.
1824 *
1825 * returns:
1826 * 1 if spec is a valid configuration string,
1827 * addr and conf_name are set accordingly
1828 * 0 otherwise
1829 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001830inline int fit_parse_conf(const char *spec, ulong addr_curr,
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001831 ulong *addr, const char **conf_name)
1832{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001833 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001834}
1835
1836/**
1837 * fit_parse_subimage - parse FIT subimage spec
1838 * @spec: input string, containing subimage spec
1839 * @add_curr: current image address (to be used as a possible default)
1840 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1841 * subimage
1842 * @image_name: double pointer to a char, will hold pointer to a subimage name
1843 *
1844 * fit_parse_subimage() expects subimage spec in the for of
1845 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1846 * subimage with a <subimg> unit name.
1847 *
1848 * Address part is optional, and if omitted default add_curr will
1849 * be used instead.
1850 *
1851 * returns:
1852 * 1 if spec is a valid subimage string,
1853 * addr and image_name are set accordingly
1854 * 0 otherwise
1855 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001856inline int fit_parse_subimage(const char *spec, ulong addr_curr,
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001857 ulong *addr, const char **image_name)
1858{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001859 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001860}
Marian Balakowicz570abb02008-02-29 15:59:59 +01001861#endif /* !USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001862
Stephen Warren712fbcf2011-10-18 11:11:49 +00001863static void fit_get_debug(const void *fit, int noffset,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001864 char *prop_name, int err)
1865{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001866 debug("Can't get '%s' property from FIT 0x%08lx, "
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001867 "node: offset %d, name %s (%s)\n",
1868 prop_name, (ulong)fit, noffset,
Stephen Warren712fbcf2011-10-18 11:11:49 +00001869 fit_get_name(fit, noffset, NULL),
1870 fdt_strerror(err));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001871}
1872
1873/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001874 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001875 * @fit: pointer to the FIT format image header
1876 * @p: pointer to prefix string
1877 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001878 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001879 * The routine prints out FIT image properties (root node level) follwed by
1880 * the details of each component image.
1881 *
1882 * returns:
1883 * no returned results
1884 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001885void fit_print_contents(const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001886{
1887 char *desc;
1888 char *uname;
1889 int images_noffset;
1890 int confs_noffset;
1891 int noffset;
1892 int ndepth;
1893 int count = 0;
1894 int ret;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001895 const char *p;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001896#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1897 time_t timestamp;
1898#endif
1899
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001900#ifdef USE_HOSTCC
1901 p = "";
1902#else
1903 p = " ";
1904#endif
1905
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001906 /* Root node properties */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001907 ret = fit_get_desc(fit, 0, &desc);
1908 printf("%sFIT description: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001909 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001910 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001911 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001912 printf("%s\n", desc);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001913
1914#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001915 ret = fit_get_timestamp(fit, 0, &timestamp);
1916 printf("%sCreated: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001917 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001918 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001919 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001920 genimg_print_time(timestamp);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001921#endif
1922
1923 /* Find images parent node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001924 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001925 if (images_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001926 printf("Can't find images parent node '%s' (%s)\n",
1927 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001928 return;
1929 }
1930
1931 /* Process its subnodes, print out component images details */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001932 for (ndepth = 0, count = 0,
1933 noffset = fdt_next_node(fit, images_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001934 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001935 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001936 if (ndepth == 1) {
1937 /*
1938 * Direct child node of the images parent node,
1939 * i.e. component image node.
1940 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001941 printf("%s Image %u (%s)\n", p, count++,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001942 fit_get_name(fit, noffset, NULL));
1943
Stephen Warren712fbcf2011-10-18 11:11:49 +00001944 fit_image_print(fit, noffset, p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001945 }
1946 }
1947
1948 /* Find configurations parent node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001949 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001950 if (confs_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001951 debug("Can't get configurations parent node '%s' (%s)\n",
1952 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001953 return;
1954 }
1955
1956 /* get default configuration unit name from default property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001957 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001958 if (uname)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001959 printf("%s Default Configuration: '%s'\n", p, uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001960
1961 /* Process its subnodes, print out configurations details */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001962 for (ndepth = 0, count = 0,
1963 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001964 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001965 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001966 if (ndepth == 1) {
1967 /*
1968 * Direct child node of the configurations parent node,
1969 * i.e. configuration node.
1970 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001971 printf("%s Configuration %u (%s)\n", p, count++,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001972 fit_get_name(fit, noffset, NULL));
1973
Stephen Warren712fbcf2011-10-18 11:11:49 +00001974 fit_conf_print(fit, noffset, p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001975 }
1976 }
1977}
1978
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001979/**
1980 * fit_image_print - prints out the FIT component image details
1981 * @fit: pointer to the FIT format image header
1982 * @image_noffset: offset of the component image node
1983 * @p: pointer to prefix string
1984 *
1985 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001986 * image. If present, hash nodes are printed out as well. Load
1987 * address for images of type firmware is also printed out. Since the load
1988 * address is not mandatory for firmware images, it will be output as
1989 * "unavailable" when not present.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001990 *
1991 * returns:
1992 * no returned results
1993 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001994void fit_image_print(const void *fit, int image_noffset, const char *p)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001995{
1996 char *desc;
1997 uint8_t type, arch, os, comp;
1998 size_t size;
1999 ulong load, entry;
2000 const void *data;
2001 int noffset;
2002 int ndepth;
2003 int ret;
2004
2005 /* Mandatory properties */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002006 ret = fit_get_desc(fit, image_noffset, &desc);
2007 printf("%s Description: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002008 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002009 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002010 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002011 printf("%s\n", desc);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002012
Stephen Warren712fbcf2011-10-18 11:11:49 +00002013 fit_image_get_type(fit, image_noffset, &type);
2014 printf("%s Type: %s\n", p, genimg_get_type_name(type));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002015
Stephen Warren712fbcf2011-10-18 11:11:49 +00002016 fit_image_get_comp(fit, image_noffset, &comp);
2017 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002018
Stephen Warren712fbcf2011-10-18 11:11:49 +00002019 ret = fit_image_get_data(fit, image_noffset, &data, &size);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002020
2021#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +00002022 printf("%s Data Start: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002023 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002024 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002025 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002026 printf("0x%08lx\n", (ulong)data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002027#endif
2028
Stephen Warren712fbcf2011-10-18 11:11:49 +00002029 printf("%s Data Size: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002030 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002031 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002032 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002033 genimg_print_size(size);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002034
2035 /* Remaining, type dependent properties */
2036 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2037 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2038 (type == IH_TYPE_FLATDT)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002039 fit_image_get_arch(fit, image_noffset, &arch);
2040 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002041 }
2042
2043 if (type == IH_TYPE_KERNEL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002044 fit_image_get_os(fit, image_noffset, &os);
2045 printf("%s OS: %s\n", p, genimg_get_os_name(os));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002046 }
2047
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002048 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2049 (type == IH_TYPE_FIRMWARE)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002050 ret = fit_image_get_load(fit, image_noffset, &load);
2051 printf("%s Load Address: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002052 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002053 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002054 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002055 printf("0x%08lx\n", load);
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002056 }
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002057
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002058 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002059 fit_image_get_entry(fit, image_noffset, &entry);
2060 printf("%s Entry Point: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002061 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002062 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002063 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00002064 printf("0x%08lx\n", entry);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002065 }
2066
2067 /* Process all hash subnodes of the component image node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002068 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002069 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002070 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002071 if (ndepth == 1) {
2072 /* Direct child node of the component image node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002073 fit_image_print_hash(fit, noffset, p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002074 }
2075 }
2076}
2077
2078/**
2079 * fit_image_print_hash - prints out the hash node details
2080 * @fit: pointer to the FIT format image header
2081 * @noffset: offset of the hash node
2082 * @p: pointer to prefix string
2083 *
2084 * fit_image_print_hash() lists properies for the processed hash node
2085 *
2086 * returns:
2087 * no returned results
2088 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002089void fit_image_print_hash(const void *fit, int noffset, const char *p)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002090{
2091 char *algo;
2092 uint8_t *value;
2093 int value_len;
2094 int i, ret;
2095
2096 /*
2097 * Check subnode name, must be equal to "hash".
2098 * Multiple hash nodes require unique unit node
2099 * names, e.g. hash@1, hash@2, etc.
2100 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002101 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002102 FIT_HASH_NODENAME,
2103 strlen(FIT_HASH_NODENAME)) != 0)
2104 return;
2105
Stephen Warren712fbcf2011-10-18 11:11:49 +00002106 debug("%s Hash node: '%s'\n", p,
2107 fit_get_name(fit, noffset, NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002108
Stephen Warren712fbcf2011-10-18 11:11:49 +00002109 printf("%s Hash algo: ", p);
2110 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2111 printf("invalid/unsupported\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002112 return;
2113 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00002114 printf("%s\n", algo);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002115
Stephen Warren712fbcf2011-10-18 11:11:49 +00002116 ret = fit_image_hash_get_value(fit, noffset, &value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002117 &value_len);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002118 printf("%s Hash value: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002119 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002120 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002121 } else {
2122 for (i = 0; i < value_len; i++)
Stephen Warren712fbcf2011-10-18 11:11:49 +00002123 printf("%02x", value[i]);
2124 printf("\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002125 }
2126
Stephen Warren712fbcf2011-10-18 11:11:49 +00002127 debug("%s Hash len: %d\n", p, value_len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002128}
2129
2130/**
2131 * fit_get_desc - get node description property
2132 * @fit: pointer to the FIT format image header
2133 * @noffset: node offset
2134 * @desc: double pointer to the char, will hold pointer to the descrption
2135 *
2136 * fit_get_desc() reads description property from a given node, if
2137 * description is found pointer to it is returened in third call argument.
2138 *
2139 * returns:
2140 * 0, on success
2141 * -1, on failure
2142 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002143int fit_get_desc(const void *fit, int noffset, char **desc)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002144{
2145 int len;
2146
Stephen Warren712fbcf2011-10-18 11:11:49 +00002147 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002148 if (*desc == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002149 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002150 return -1;
2151 }
2152
2153 return 0;
2154}
2155
2156/**
2157 * fit_get_timestamp - get node timestamp property
2158 * @fit: pointer to the FIT format image header
2159 * @noffset: node offset
2160 * @timestamp: pointer to the time_t, will hold read timestamp
2161 *
2162 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2163 * is found and has a correct size its value is retured in third call
2164 * argument.
2165 *
2166 * returns:
2167 * 0, on success
2168 * -1, on property read failure
2169 * -2, on wrong timestamp size
2170 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002171int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002172{
2173 int len;
2174 const void *data;
2175
Stephen Warren712fbcf2011-10-18 11:11:49 +00002176 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002177 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002178 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002179 return -1;
2180 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00002181 if (len != sizeof(uint32_t)) {
2182 debug("FIT timestamp with incorrect size of (%u)\n", len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002183 return -2;
2184 }
2185
Stephen Warren712fbcf2011-10-18 11:11:49 +00002186 *timestamp = uimage_to_cpu(*((uint32_t *)data));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002187 return 0;
2188}
2189
2190/**
2191 * fit_image_get_node - get node offset for component image of a given unit name
2192 * @fit: pointer to the FIT format image header
2193 * @image_uname: component image node unit name
2194 *
2195 * fit_image_get_node() finds a component image (withing the '/images'
2196 * node) of a provided unit name. If image is found its node offset is
2197 * returned to the caller.
2198 *
2199 * returns:
2200 * image node offset when found (>=0)
2201 * negative number on failure (FDT_ERR_* code)
2202 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002203int fit_image_get_node(const void *fit, const char *image_uname)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002204{
2205 int noffset, images_noffset;
2206
Stephen Warren712fbcf2011-10-18 11:11:49 +00002207 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002208 if (images_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002209 debug("Can't find images parent node '%s' (%s)\n",
2210 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002211 return images_noffset;
2212 }
2213
Stephen Warren712fbcf2011-10-18 11:11:49 +00002214 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002215 if (noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002216 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2217 image_uname, fdt_strerror(noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002218 }
2219
2220 return noffset;
2221}
2222
2223/**
2224 * fit_image_get_os - get os id for a given component image node
2225 * @fit: pointer to the FIT format image header
2226 * @noffset: component image node offset
2227 * @os: pointer to the uint8_t, will hold os numeric id
2228 *
2229 * fit_image_get_os() finds os property in a given component image node.
2230 * If the property is found, its (string) value is translated to the numeric
2231 * id which is returned to the caller.
2232 *
2233 * returns:
2234 * 0, on success
2235 * -1, on failure
2236 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002237int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002238{
2239 int len;
2240 const void *data;
2241
2242 /* Get OS name from property data */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002243 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002244 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002245 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002246 *os = -1;
2247 return -1;
2248 }
2249
2250 /* Translate OS name to id */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002251 *os = genimg_get_os_id(data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002252 return 0;
2253}
2254
2255/**
2256 * fit_image_get_arch - get arch id for a given component image node
2257 * @fit: pointer to the FIT format image header
2258 * @noffset: component image node offset
2259 * @arch: pointer to the uint8_t, will hold arch numeric id
2260 *
2261 * fit_image_get_arch() finds arch property in a given component image node.
2262 * If the property is found, its (string) value is translated to the numeric
2263 * id which is returned to the caller.
2264 *
2265 * returns:
2266 * 0, on success
2267 * -1, on failure
2268 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002269int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002270{
2271 int len;
2272 const void *data;
2273
2274 /* Get architecture name from property data */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002275 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002276 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002277 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002278 *arch = -1;
2279 return -1;
2280 }
2281
2282 /* Translate architecture name to id */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002283 *arch = genimg_get_arch_id(data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002284 return 0;
2285}
2286
2287/**
2288 * fit_image_get_type - get type id for a given component image node
2289 * @fit: pointer to the FIT format image header
2290 * @noffset: component image node offset
2291 * @type: pointer to the uint8_t, will hold type numeric id
2292 *
2293 * fit_image_get_type() finds type property in a given component image node.
2294 * If the property is found, its (string) value is translated to the numeric
2295 * id which is returned to the caller.
2296 *
2297 * returns:
2298 * 0, on success
2299 * -1, on failure
2300 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002301int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002302{
2303 int len;
2304 const void *data;
2305
2306 /* Get image type name from property data */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002307 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002308 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002309 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002310 *type = -1;
2311 return -1;
2312 }
2313
2314 /* Translate image type name to id */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002315 *type = genimg_get_type_id(data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002316 return 0;
2317}
2318
2319/**
2320 * fit_image_get_comp - get comp id for a given component image node
2321 * @fit: pointer to the FIT format image header
2322 * @noffset: component image node offset
2323 * @comp: pointer to the uint8_t, will hold comp numeric id
2324 *
2325 * fit_image_get_comp() finds comp property in a given component image node.
2326 * If the property is found, its (string) value is translated to the numeric
2327 * id which is returned to the caller.
2328 *
2329 * returns:
2330 * 0, on success
2331 * -1, on failure
2332 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002333int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002334{
2335 int len;
2336 const void *data;
2337
2338 /* Get compression name from property data */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002339 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002340 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002341 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002342 *comp = -1;
2343 return -1;
2344 }
2345
2346 /* Translate compression name to id */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002347 *comp = genimg_get_comp_id(data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002348 return 0;
2349}
2350
2351/**
2352 * fit_image_get_load - get load address property for a given component image node
2353 * @fit: pointer to the FIT format image header
2354 * @noffset: component image node offset
2355 * @load: pointer to the uint32_t, will hold load address
2356 *
2357 * fit_image_get_load() finds load address property in a given component image node.
2358 * If the property is found, its value is returned to the caller.
2359 *
2360 * returns:
2361 * 0, on success
2362 * -1, on failure
2363 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002364int fit_image_get_load(const void *fit, int noffset, ulong *load)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002365{
2366 int len;
2367 const uint32_t *data;
2368
Stephen Warren712fbcf2011-10-18 11:11:49 +00002369 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002370 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002371 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002372 return -1;
2373 }
2374
Stephen Warren712fbcf2011-10-18 11:11:49 +00002375 *load = uimage_to_cpu(*data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002376 return 0;
2377}
2378
2379/**
2380 * fit_image_get_entry - get entry point address property for a given component image node
2381 * @fit: pointer to the FIT format image header
2382 * @noffset: component image node offset
2383 * @entry: pointer to the uint32_t, will hold entry point address
2384 *
2385 * fit_image_get_entry() finds entry point address property in a given component image node.
2386 * If the property is found, its value is returned to the caller.
2387 *
2388 * returns:
2389 * 0, on success
2390 * -1, on failure
2391 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002392int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002393{
2394 int len;
2395 const uint32_t *data;
2396
Stephen Warren712fbcf2011-10-18 11:11:49 +00002397 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002398 if (data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002399 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002400 return -1;
2401 }
2402
Stephen Warren712fbcf2011-10-18 11:11:49 +00002403 *entry = uimage_to_cpu(*data);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002404 return 0;
2405}
2406
2407/**
2408 * fit_image_get_data - get data property and its size for a given component image node
2409 * @fit: pointer to the FIT format image header
2410 * @noffset: component image node offset
2411 * @data: double pointer to void, will hold data property's data address
2412 * @size: pointer to size_t, will hold data property's data size
2413 *
2414 * fit_image_get_data() finds data property in a given component image node.
2415 * If the property is found its data start address and size are returned to
2416 * the caller.
2417 *
2418 * returns:
2419 * 0, on success
2420 * -1, on failure
2421 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002422int fit_image_get_data(const void *fit, int noffset,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002423 const void **data, size_t *size)
2424{
2425 int len;
2426
Stephen Warren712fbcf2011-10-18 11:11:49 +00002427 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002428 if (*data == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002429 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002430 *size = 0;
2431 return -1;
2432 }
2433
2434 *size = len;
2435 return 0;
2436}
2437
2438/**
2439 * fit_image_hash_get_algo - get hash algorithm name
2440 * @fit: pointer to the FIT format image header
2441 * @noffset: hash node offset
2442 * @algo: double pointer to char, will hold pointer to the algorithm name
2443 *
2444 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2445 * If the property is found its data start address is returned to the caller.
2446 *
2447 * returns:
2448 * 0, on success
2449 * -1, on failure
2450 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002451int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002452{
2453 int len;
2454
Stephen Warren712fbcf2011-10-18 11:11:49 +00002455 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002456 if (*algo == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002457 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002458 return -1;
2459 }
2460
2461 return 0;
2462}
2463
2464/**
2465 * fit_image_hash_get_value - get hash value and length
2466 * @fit: pointer to the FIT format image header
2467 * @noffset: hash node offset
2468 * @value: double pointer to uint8_t, will hold address of a hash value data
2469 * @value_len: pointer to an int, will hold hash data length
2470 *
2471 * fit_image_hash_get_value() finds hash value property in a given hash node.
2472 * If the property is found its data start address and size are returned to
2473 * the caller.
2474 *
2475 * returns:
2476 * 0, on success
2477 * -1, on failure
2478 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002479int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002480 int *value_len)
2481{
2482 int len;
2483
Stephen Warren712fbcf2011-10-18 11:11:49 +00002484 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002485 if (*value == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002486 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002487 *value_len = 0;
2488 return -1;
2489 }
2490
2491 *value_len = len;
2492 return 0;
2493}
2494
2495/**
2496 * fit_set_timestamp - set node timestamp property
2497 * @fit: pointer to the FIT format image header
2498 * @noffset: node offset
2499 * @timestamp: timestamp value to be set
2500 *
2501 * fit_set_timestamp() attempts to set timestamp property in the requested
2502 * node and returns operation status to the caller.
2503 *
2504 * returns:
2505 * 0, on success
2506 * -1, on property read failure
2507 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002508int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002509{
2510 uint32_t t;
2511 int ret;
2512
Stephen Warren712fbcf2011-10-18 11:11:49 +00002513 t = cpu_to_uimage(timestamp);
2514 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2515 sizeof(uint32_t));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002516 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002517 printf("Can't set '%s' property for '%s' node (%s)\n",
2518 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2519 fdt_strerror(ret));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002520 return -1;
2521 }
2522
2523 return 0;
2524}
2525
2526/**
2527 * calculate_hash - calculate and return hash for provided input data
2528 * @data: pointer to the input data
2529 * @data_len: data length
2530 * @algo: requested hash algorithm
2531 * @value: pointer to the char, will hold hash value data (caller must
2532 * allocate enough free space)
2533 * value_len: length of the calculated hash
2534 *
2535 * calculate_hash() computes input data hash according to the requested algorithm.
2536 * Resulting hash value is placed in caller provided 'value' buffer, length
2537 * of the calculated hash is returned via value_len pointer argument.
2538 *
2539 * returns:
2540 * 0, on success
2541 * -1, when algo is unsupported
2542 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002543static int calculate_hash(const void *data, int data_len, const char *algo,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002544 uint8_t *value, int *value_len)
2545{
Stephen Warren712fbcf2011-10-18 11:11:49 +00002546 if (strcmp(algo, "crc32") == 0) {
2547 *((uint32_t *)value) = crc32_wd(0, data, data_len,
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002548 CHUNKSZ_CRC32);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002549 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002550 *value_len = 4;
Stephen Warren712fbcf2011-10-18 11:11:49 +00002551 } else if (strcmp(algo, "sha1") == 0) {
2552 sha1_csum_wd((unsigned char *) data, data_len,
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002553 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002554 *value_len = 20;
Stephen Warren712fbcf2011-10-18 11:11:49 +00002555 } else if (strcmp(algo, "md5") == 0) {
2556 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka766529f2008-03-14 16:22:34 +01002557 *value_len = 16;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002558 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002559 debug("Unsupported hash alogrithm\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002560 return -1;
2561 }
2562 return 0;
2563}
2564
2565#ifdef USE_HOSTCC
2566/**
2567 * fit_set_hashes - process FIT component image nodes and calculate hashes
2568 * @fit: pointer to the FIT format image header
2569 *
2570 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2571 * Hashes are calculated for all component images which have hash subnodes
2572 * with algorithm property set to one of the supported hash algorithms.
2573 *
2574 * returns
2575 * 0, on success
2576 * libfdt error code, on failure
2577 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002578int fit_set_hashes(void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002579{
2580 int images_noffset;
2581 int noffset;
2582 int ndepth;
2583 int ret;
2584
2585 /* Find images parent node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002586 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002587 if (images_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002588 printf("Can't find images parent node '%s' (%s)\n",
2589 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002590 return images_noffset;
2591 }
2592
2593 /* Process its subnodes, print out component images details */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002594 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002595 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002596 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002597 if (ndepth == 1) {
2598 /*
2599 * Direct child node of the images parent node,
2600 * i.e. component image node.
2601 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002602 ret = fit_image_set_hashes(fit, noffset);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002603 if (ret)
2604 return ret;
2605 }
2606 }
2607
2608 return 0;
2609}
2610
2611/**
2612 * fit_image_set_hashes - calculate/set hashes for given component image node
2613 * @fit: pointer to the FIT format image header
2614 * @image_noffset: requested component image node
2615 *
2616 * fit_image_set_hashes() adds hash values for an component image node. All
2617 * existing hash subnodes are checked, if algorithm property is set to one of
2618 * the supported hash algorithms, hash value is computed and corresponding
2619 * hash node property is set, for example:
2620 *
2621 * Input component image node structure:
2622 *
2623 * o image@1 (at image_noffset)
2624 * | - data = [binary data]
2625 * o hash@1
2626 * |- algo = "sha1"
2627 *
2628 * Output component image node structure:
2629 *
2630 * o image@1 (at image_noffset)
2631 * | - data = [binary data]
2632 * o hash@1
2633 * |- algo = "sha1"
2634 * |- value = sha1(data)
2635 *
2636 * returns:
2637 * 0 on sucess
2638 * <0 on failure
2639 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002640int fit_image_set_hashes(void *fit, int image_noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002641{
2642 const void *data;
2643 size_t size;
2644 char *algo;
2645 uint8_t value[FIT_MAX_HASH_LEN];
2646 int value_len;
2647 int noffset;
2648 int ndepth;
2649
2650 /* Get image data and data length */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002651 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2652 printf("Can't get image data/size\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002653 return -1;
2654 }
2655
2656 /* Process all hash subnodes of the component image node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002657 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002658 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002659 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002660 if (ndepth == 1) {
2661 /* Direct child node of the component image node */
2662
2663 /*
2664 * Check subnode name, must be equal to "hash".
2665 * Multiple hash nodes require unique unit node
2666 * names, e.g. hash@1, hash@2, etc.
2667 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002668 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002669 FIT_HASH_NODENAME,
2670 strlen(FIT_HASH_NODENAME)) != 0) {
2671 /* Not a hash subnode, skip it */
2672 continue;
2673 }
2674
Stephen Warren712fbcf2011-10-18 11:11:49 +00002675 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2676 printf("Can't get hash algo property for "
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002677 "'%s' hash node in '%s' image node\n",
Stephen Warren712fbcf2011-10-18 11:11:49 +00002678 fit_get_name(fit, noffset, NULL),
2679 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002680 return -1;
2681 }
2682
Stephen Warren712fbcf2011-10-18 11:11:49 +00002683 if (calculate_hash(data, size, algo, value,
2684 &value_len)) {
2685 printf("Unsupported hash algorithm (%s) for "
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002686 "'%s' hash node in '%s' image node\n",
Stephen Warren712fbcf2011-10-18 11:11:49 +00002687 algo, fit_get_name(fit, noffset, NULL),
2688 fit_get_name(fit, image_noffset,
2689 NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002690 return -1;
2691 }
2692
Stephen Warren712fbcf2011-10-18 11:11:49 +00002693 if (fit_image_hash_set_value(fit, noffset, value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002694 value_len)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002695 printf("Can't set hash value for "
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002696 "'%s' hash node in '%s' image node\n",
Stephen Warren712fbcf2011-10-18 11:11:49 +00002697 fit_get_name(fit, noffset, NULL),
2698 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002699 return -1;
2700 }
2701 }
2702 }
2703
2704 return 0;
2705}
2706
2707/**
2708 * fit_image_hash_set_value - set hash value in requested has node
2709 * @fit: pointer to the FIT format image header
2710 * @noffset: hash node offset
2711 * @value: hash value to be set
2712 * @value_len: hash value length
2713 *
2714 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2715 * given and returns operation status to the caller.
2716 *
2717 * returns
2718 * 0, on success
2719 * -1, on failure
2720 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002721int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002722 int value_len)
2723{
2724 int ret;
2725
Stephen Warren712fbcf2011-10-18 11:11:49 +00002726 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002727 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002728 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2729 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2730 fdt_strerror(ret));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002731 return -1;
2732 }
2733
2734 return 0;
2735}
2736#endif /* USE_HOSTCC */
2737
2738/**
2739 * fit_image_check_hashes - verify data intergity
2740 * @fit: pointer to the FIT format image header
2741 * @image_noffset: component image node offset
2742 *
2743 * fit_image_check_hashes() goes over component image hash nodes,
2744 * re-calculates each data hash and compares with the value stored in hash
2745 * node.
2746 *
2747 * returns:
2748 * 1, if all hashes are valid
2749 * 0, otherwise (or on error)
2750 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002751int fit_image_check_hashes(const void *fit, int image_noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002752{
2753 const void *data;
2754 size_t size;
2755 char *algo;
2756 uint8_t *fit_value;
2757 int fit_value_len;
2758 uint8_t value[FIT_MAX_HASH_LEN];
2759 int value_len;
2760 int noffset;
2761 int ndepth;
2762 char *err_msg = "";
2763
2764 /* Get image data and data length */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002765 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2766 printf("Can't get image data/size\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002767 return 0;
2768 }
2769
2770 /* Process all hash subnodes of the component image node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002771 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002772 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002773 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002774 if (ndepth == 1) {
2775 /* Direct child node of the component image node */
2776
2777 /*
2778 * Check subnode name, must be equal to "hash".
2779 * Multiple hash nodes require unique unit node
2780 * names, e.g. hash@1, hash@2, etc.
2781 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002782 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002783 FIT_HASH_NODENAME,
2784 strlen(FIT_HASH_NODENAME)) != 0)
2785 continue;
2786
Stephen Warren712fbcf2011-10-18 11:11:49 +00002787 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002788 err_msg = " error!\nCan't get hash algo "
2789 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002790 goto error;
2791 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00002792 printf("%s", algo);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002793
Stephen Warren712fbcf2011-10-18 11:11:49 +00002794 if (fit_image_hash_get_value(fit, noffset, &fit_value,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002795 &fit_value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002796 err_msg = " error!\nCan't get hash value "
2797 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002798 goto error;
2799 }
2800
Stephen Warren712fbcf2011-10-18 11:11:49 +00002801 if (calculate_hash(data, size, algo, value,
2802 &value_len)) {
2803 err_msg = " error!\n"
2804 "Unsupported hash algorithm";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002805 goto error;
2806 }
2807
2808 if (value_len != fit_value_len) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002809 err_msg = " error !\nBad hash value len";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002810 goto error;
Stephen Warren712fbcf2011-10-18 11:11:49 +00002811 } else if (memcmp(value, fit_value, value_len) != 0) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002812 err_msg = " error!\nBad hash value";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002813 goto error;
2814 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00002815 printf("+ ");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002816 }
2817 }
2818
2819 return 1;
2820
2821error:
Stephen Warren712fbcf2011-10-18 11:11:49 +00002822 printf("%s for '%s' hash node in '%s' image node\n",
2823 err_msg, fit_get_name(fit, noffset, NULL),
2824 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002825 return 0;
2826}
2827
2828/**
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002829 * fit_all_image_check_hashes - verify data intergity for all images
2830 * @fit: pointer to the FIT format image header
2831 *
2832 * fit_all_image_check_hashes() goes over all images in the FIT and
2833 * for every images checks if all it's hashes are valid.
2834 *
2835 * returns:
2836 * 1, if all hashes of all images are valid
2837 * 0, otherwise (or on error)
2838 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002839int fit_all_image_check_hashes(const void *fit)
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002840{
2841 int images_noffset;
2842 int noffset;
2843 int ndepth;
2844 int count;
2845
2846 /* Find images parent node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002847 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002848 if (images_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00002849 printf("Can't find images parent node '%s' (%s)\n",
2850 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002851 return 0;
2852 }
2853
2854 /* Process all image subnodes, check hashes for each */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002855 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002856 (ulong)fit);
2857 for (ndepth = 0, count = 0,
Stephen Warren712fbcf2011-10-18 11:11:49 +00002858 noffset = fdt_next_node(fit, images_noffset, &ndepth);
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002859 (noffset >= 0) && (ndepth > 0);
Stephen Warren712fbcf2011-10-18 11:11:49 +00002860 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002861 if (ndepth == 1) {
2862 /*
2863 * Direct child node of the images parent node,
2864 * i.e. component image node.
2865 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002866 printf(" Hash(es) for Image %u (%s): ", count++,
2867 fit_get_name(fit, noffset, NULL));
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002868
Stephen Warren712fbcf2011-10-18 11:11:49 +00002869 if (!fit_image_check_hashes(fit, noffset))
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002870 return 0;
Stephen Warren712fbcf2011-10-18 11:11:49 +00002871 printf("\n");
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002872 }
2873 }
2874 return 1;
2875}
2876
2877/**
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002878 * fit_image_check_os - check whether image node is of a given os type
2879 * @fit: pointer to the FIT format image header
2880 * @noffset: component image node offset
2881 * @os: requested image os
2882 *
2883 * fit_image_check_os() reads image os property and compares its numeric
2884 * id with the requested os. Comparison result is returned to the caller.
2885 *
2886 * returns:
2887 * 1 if image is of given os type
2888 * 0 otherwise (or on error)
2889 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002890int fit_image_check_os(const void *fit, int noffset, uint8_t os)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002891{
2892 uint8_t image_os;
2893
Stephen Warren712fbcf2011-10-18 11:11:49 +00002894 if (fit_image_get_os(fit, noffset, &image_os))
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002895 return 0;
2896 return (os == image_os);
2897}
2898
2899/**
2900 * fit_image_check_arch - check whether image node is of a given arch
2901 * @fit: pointer to the FIT format image header
2902 * @noffset: component image node offset
2903 * @arch: requested imagearch
2904 *
2905 * fit_image_check_arch() reads image arch property and compares its numeric
2906 * id with the requested arch. Comparison result is returned to the caller.
2907 *
2908 * returns:
2909 * 1 if image is of given arch
2910 * 0 otherwise (or on error)
2911 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002912int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002913{
2914 uint8_t image_arch;
2915
Stephen Warren712fbcf2011-10-18 11:11:49 +00002916 if (fit_image_get_arch(fit, noffset, &image_arch))
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002917 return 0;
2918 return (arch == image_arch);
2919}
2920
2921/**
2922 * fit_image_check_type - check whether image node is of a given type
2923 * @fit: pointer to the FIT format image header
2924 * @noffset: component image node offset
2925 * @type: requested image type
2926 *
2927 * fit_image_check_type() reads image type property and compares its numeric
2928 * id with the requested type. Comparison result is returned to the caller.
2929 *
2930 * returns:
2931 * 1 if image is of given type
2932 * 0 otherwise (or on error)
2933 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002934int fit_image_check_type(const void *fit, int noffset, uint8_t type)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002935{
2936 uint8_t image_type;
2937
Stephen Warren712fbcf2011-10-18 11:11:49 +00002938 if (fit_image_get_type(fit, noffset, &image_type))
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002939 return 0;
2940 return (type == image_type);
2941}
2942
2943/**
2944 * fit_image_check_comp - check whether image node uses given compression
2945 * @fit: pointer to the FIT format image header
2946 * @noffset: component image node offset
2947 * @comp: requested image compression type
2948 *
2949 * fit_image_check_comp() reads image compression property and compares its
2950 * numeric id with the requested compression type. Comparison result is
2951 * returned to the caller.
2952 *
2953 * returns:
2954 * 1 if image uses requested compression
2955 * 0 otherwise (or on error)
2956 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002957int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002958{
2959 uint8_t image_comp;
2960
Stephen Warren712fbcf2011-10-18 11:11:49 +00002961 if (fit_image_get_comp(fit, noffset, &image_comp))
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002962 return 0;
2963 return (comp == image_comp);
2964}
2965
2966/**
2967 * fit_check_format - sanity check FIT image format
2968 * @fit: pointer to the FIT format image header
2969 *
2970 * fit_check_format() runs a basic sanity FIT image verification.
2971 * Routine checks for mandatory properties, nodes, etc.
2972 *
2973 * returns:
2974 * 1, on success
2975 * 0, on failure
2976 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002977int fit_check_format(const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002978{
2979 /* mandatory / node 'description' property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002980 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2981 debug("Wrong FIT format: no description\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002982 return 0;
2983 }
2984
2985#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2986 /* mandatory / node 'timestamp' property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002987 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2988 debug("Wrong FIT format: no timestamp\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002989 return 0;
2990 }
2991#endif
2992
2993 /* mandatory subimages parent '/images' node */
Stephen Warren712fbcf2011-10-18 11:11:49 +00002994 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
2995 debug("Wrong FIT format: no images parent node\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002996 return 0;
2997 }
2998
2999 return 1;
3000}
3001
3002/**
3003 * fit_conf_get_node - get node offset for configuration of a given unit name
3004 * @fit: pointer to the FIT format image header
3005 * @conf_uname: configuration node unit name
3006 *
3007 * fit_conf_get_node() finds a configuration (withing the '/configurations'
3008 * parant node) of a provided unit name. If configuration is found its node offset
3009 * is returned to the caller.
3010 *
3011 * When NULL is provided in second argument fit_conf_get_node() will search
3012 * for a default configuration node instead. Default configuration node unit name
3013 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
3014 *
3015 * returns:
3016 * configuration node offset when found (>=0)
3017 * negative number on failure (FDT_ERR_* code)
3018 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003019int fit_conf_get_node(const void *fit, const char *conf_uname)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003020{
3021 int noffset, confs_noffset;
3022 int len;
3023
Stephen Warren712fbcf2011-10-18 11:11:49 +00003024 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003025 if (confs_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00003026 debug("Can't find configurations parent node '%s' (%s)\n",
3027 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003028 return confs_noffset;
3029 }
3030
3031 if (conf_uname == NULL) {
3032 /* get configuration unit name from the default property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003033 debug("No configuration specified, trying default...\n");
3034 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3035 FIT_DEFAULT_PROP, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003036 if (conf_uname == NULL) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00003037 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3038 len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003039 return len;
3040 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00003041 debug("Found default configuration: '%s'\n", conf_uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003042 }
3043
Stephen Warren712fbcf2011-10-18 11:11:49 +00003044 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003045 if (noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00003046 debug("Can't get node offset for configuration unit name: "
3047 "'%s' (%s)\n",
3048 conf_uname, fdt_strerror(noffset));
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003049 }
3050
3051 return noffset;
3052}
3053
Stephen Warren712fbcf2011-10-18 11:11:49 +00003054static int __fit_conf_get_prop_node(const void *fit, int noffset,
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003055 const char *prop_name)
3056{
3057 char *uname;
3058 int len;
3059
3060 /* get kernel image unit name from configuration kernel property */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003061 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003062 if (uname == NULL)
3063 return len;
3064
Stephen Warren712fbcf2011-10-18 11:11:49 +00003065 return fit_image_get_node(fit, uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003066}
3067
3068/**
3069 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3070 * a given configuration
3071 * @fit: pointer to the FIT format image header
3072 * @noffset: configuration node offset
3073 *
3074 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3075 * configuration FIT_KERNEL_PROP property and translates it to the node
3076 * offset.
3077 *
3078 * returns:
3079 * image node offset when found (>=0)
3080 * negative number on failure (FDT_ERR_* code)
3081 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003082int fit_conf_get_kernel_node(const void *fit, int noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003083{
Stephen Warren712fbcf2011-10-18 11:11:49 +00003084 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003085}
3086
3087/**
3088 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3089 * a given configuration
3090 * @fit: pointer to the FIT format image header
3091 * @noffset: configuration node offset
3092 *
3093 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3094 * configuration FIT_KERNEL_PROP property and translates it to the node
3095 * offset.
3096 *
3097 * returns:
3098 * image node offset when found (>=0)
3099 * negative number on failure (FDT_ERR_* code)
3100 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003101int fit_conf_get_ramdisk_node(const void *fit, int noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003102{
Stephen Warren712fbcf2011-10-18 11:11:49 +00003103 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003104}
3105
3106/**
3107 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3108 * a given configuration
3109 * @fit: pointer to the FIT format image header
3110 * @noffset: configuration node offset
3111 *
3112 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3113 * configuration FIT_KERNEL_PROP property and translates it to the node
3114 * offset.
3115 *
3116 * returns:
3117 * image node offset when found (>=0)
3118 * negative number on failure (FDT_ERR_* code)
3119 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003120int fit_conf_get_fdt_node(const void *fit, int noffset)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003121{
Stephen Warren712fbcf2011-10-18 11:11:49 +00003122 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003123}
3124
3125/**
3126 * fit_conf_print - prints out the FIT configuration details
3127 * @fit: pointer to the FIT format image header
Marian Balakowiczf773bea2008-03-12 10:35:46 +01003128 * @noffset: offset of the configuration node
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003129 * @p: pointer to prefix string
3130 *
3131 * fit_conf_print() lists all mandatory properies for the processed
3132 * configuration node.
3133 *
3134 * returns:
3135 * no returned results
3136 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003137void fit_conf_print(const void *fit, int noffset, const char *p)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003138{
3139 char *desc;
3140 char *uname;
3141 int ret;
3142
3143 /* Mandatory properties */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003144 ret = fit_get_desc(fit, noffset, &desc);
3145 printf("%s Description: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003146 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +00003147 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003148 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00003149 printf("%s\n", desc);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003150
Stephen Warren712fbcf2011-10-18 11:11:49 +00003151 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3152 printf("%s Kernel: ", p);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003153 if (uname == NULL)
Stephen Warren712fbcf2011-10-18 11:11:49 +00003154 printf("unavailable\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003155 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00003156 printf("%s\n", uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003157
3158 /* Optional properties */
Stephen Warren712fbcf2011-10-18 11:11:49 +00003159 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003160 if (uname)
Stephen Warren712fbcf2011-10-18 11:11:49 +00003161 printf("%s Init Ramdisk: %s\n", p, uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003162
Stephen Warren712fbcf2011-10-18 11:11:49 +00003163 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003164 if (uname)
Stephen Warren712fbcf2011-10-18 11:11:49 +00003165 printf("%s FDT: %s\n", p, uname);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003166}
Marian Balakowiczc8779642008-03-12 10:12:37 +01003167
3168/**
3169 * fit_check_ramdisk - verify FIT format ramdisk subimage
3170 * @fit_hdr: pointer to the FIT ramdisk header
3171 * @rd_noffset: ramdisk subimage node offset within FIT image
3172 * @arch: requested ramdisk image architecture type
3173 * @verify: data CRC verification flag
3174 *
3175 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3176 * specified FIT image.
3177 *
3178 * returns:
3179 * 1, on success
3180 * 0, on failure
3181 */
3182#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +00003183static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3184 int verify)
Marian Balakowiczc8779642008-03-12 10:12:37 +01003185{
Stephen Warren712fbcf2011-10-18 11:11:49 +00003186 fit_image_print(fit, rd_noffset, " ");
Marian Balakowiczc8779642008-03-12 10:12:37 +01003187
3188 if (verify) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00003189 puts(" Verifying Hash Integrity ... ");
3190 if (!fit_image_check_hashes(fit, rd_noffset)) {
3191 puts("Bad Data Hash\n");
Simon Glass770605e2012-02-13 13:51:18 +00003192 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003193 return 0;
3194 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00003195 puts("OK\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +01003196 }
3197
Simon Glass770605e2012-02-13 13:51:18 +00003198 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
Stephen Warren712fbcf2011-10-18 11:11:49 +00003199 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3200 !fit_image_check_arch(fit, rd_noffset, arch) ||
3201 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3202 printf("No Linux %s Ramdisk Image\n",
Marian Balakowiczc8779642008-03-12 10:12:37 +01003203 genimg_get_arch_name(arch));
Simon Glass770605e2012-02-13 13:51:18 +00003204 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003205 return 0;
3206 }
3207
Simon Glass770605e2012-02-13 13:51:18 +00003208 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003209 return 1;
3210}
3211#endif /* USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003212#endif /* CONFIG_FIT */