blob: 95c7a15775f411384a899cddb5b87a4c54023667 [file] [log] [blame]
Marian Balakowicz41d71ed2008-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 Balakowicz9c701e82008-01-31 13:57:17 +010025
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010026#ifndef USE_HOSTCC
Marian Balakowicz95418502008-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 Balakowicz902ce702008-05-13 15:53:29 +020038#ifdef CONFIG_LOGBUFFER
39#include <logbuff.h>
40#endif
41
Marian Balakowicza1cc1472008-02-21 17:27:41 +010042#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
43#include <rtc.h>
44#endif
45
Marian Balakowicze227fbb2008-02-29 21:24:06 +010046#include <image.h>
47
Stephen Warren6904b6e2011-10-18 11:11:49 +000048#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010049#include <fdt.h>
50#include <libfdt.h>
51#include <fdt_support.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010052#endif
53
54#if defined(CONFIG_FIT)
Andy Fleming72c23be2008-04-02 16:19:07 -050055#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010056#include <sha1.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010057
Stephen Warren6904b6e2011-10-18 11:11:49 +000058static int fit_check_ramdisk(const void *fit, int os_noffset,
Marian Balakowicza50d5152008-03-12 10:12:37 +010059 uint8_t arch, int verify);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010060#endif
61
Marian Balakowicz2efc2642008-01-31 13:58:13 +010062#ifdef CONFIG_CMD_BDI
Wolfgang Denk6262d0212010-06-28 22:00:46 +020063extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Marian Balakowicz2efc2642008-01-31 13:58:13 +010064#endif
65
66DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz61fde552008-02-27 11:01:04 +010067
Stephen Warren6904b6e2011-10-18 11:11:49 +000068static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +010069 int verify);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010070#else
Marian Balakowicz95418502008-01-31 13:55:39 +010071#include "mkimage.h"
Andy Fleming72c23be2008-04-02 16:19:07 -050072#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010073#include <time.h>
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010074#include <image.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010075#endif /* !USE_HOSTCC*/
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010076
Mike Frysingercf2feb12010-10-20 07:17:39 -040077static const table_entry_t uimage_arch[] = {
Marian Balakowiczc3c7eb22008-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 Balakowiczc3c7eb22008-02-29 15:59:59 +010087 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Erickson80f51b82008-05-04 16:45:01 -070088 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowiczc3c7eb22008-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 Lin354b4e32011-10-19 20:41:09 +000096 { IH_ARCH_NDS32, "nds32", "NDS32", },
Stefan Kristiansson15c669c2011-11-26 19:04:50 +000097 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010098 { -1, "", "", },
99};
100
Mike Frysingercf2feb12010-10-20 07:17:39 -0400101static const table_entry_t uimage_os[] = {
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100102 { IH_OS_INVALID, NULL, "Invalid OS", },
Marian Balakowiczc3c7eb22008-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 Lundgren29a58662010-09-28 11:05:36 +0200108 { IH_OS_OSE, "ose", "Enea OSE", },
Marian Balakowiczc3c7eb22008-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 Tyser56b8dd12008-09-08 14:56:49 -0500115#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
116 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
117#endif
Marian Balakowiczc3c7eb22008-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 Frysingercf2feb12010-10-20 07:17:39 -0400134static const table_entry_t uimage_type[] = {
Stefano Babic38855512011-10-17 00:07:43 +0000135 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100136 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
137 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby34d12a12011-07-21 09:10:30 -0400138 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100139 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stephen Warren8c87eba2011-11-10 13:17:53 -0700140 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
Stefano Babic38855512011-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 Balakowiczc3c7eb22008-02-29 15:59:59 +0100144 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic38855512011-10-17 00:07:43 +0000145 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Marian Balakowiczc3c7eb22008-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 Schocher58cb84d2011-07-16 00:06:42 +0000149 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100150 { -1, "", "", },
151};
152
Mike Frysingercf2feb12010-10-20 07:17:39 -0400153static const table_entry_t uimage_comp[] = {
Marian Balakowiczc3c7eb22008-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' Mantellini35afc062008-09-08 02:46:13 +0200157 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100158 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100159 { -1, "", "", },
160};
161
Stephen Warren6904b6e2011-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 Balakowiczc3c7eb22008-02-29 15:59:59 +0100164#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000165static void genimg_print_time(time_t timestamp);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100166#endif
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100167
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100168/*****************************************************************************/
169/* Legacy format routines */
170/*****************************************************************************/
Stephen Warren6904b6e2011-10-18 11:11:49 +0000171int image_check_hcrc(const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100172{
173 ulong hcrc;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000174 ulong len = image_get_header_size();
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100175 image_header_t header;
176
177 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000178 memmove(&header, (char *)hdr, image_get_header_size());
179 image_set_hcrc(&header, 0);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100180
Stephen Warren6904b6e2011-10-18 11:11:49 +0000181 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100182
Stephen Warren6904b6e2011-10-18 11:11:49 +0000183 return (hcrc == image_get_hcrc(hdr));
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100184}
185
Stephen Warren6904b6e2011-10-18 11:11:49 +0000186int image_check_dcrc(const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100187{
Stephen Warren6904b6e2011-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 Balakowicz41d71ed2008-01-08 18:14:09 +0100191
Stephen Warren6904b6e2011-10-18 11:11:49 +0000192 return (dcrc == image_get_dcrc(hdr));
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100193}
194
Marian Balakowiczb4a12a92008-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 Warren6904b6e2011-10-18 11:11:49 +0000208ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100209{
210 ulong i, count = 0;
Marian Balakowicza1474212008-02-29 16:00:06 +0100211 uint32_t *size;
Marian Balakowiczb4a12a92008-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 Warren6904b6e2011-10-18 11:11:49 +0000215 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczb4a12a92008-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 Warren6904b6e2011-10-18 11:11:49 +0000241void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100242 ulong *data, ulong *len)
243{
244 int i;
Marian Balakowicza1474212008-02-29 16:00:06 +0100245 uint32_t *size;
Nick Spence143a1592008-05-10 14:02:04 -0700246 ulong offset, count, img_data;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100247
248 /* get number of component */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000249 count = image_multi_count(hdr);
Marian Balakowiczb4a12a92008-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 Warren6904b6e2011-10-18 11:11:49 +0000253 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczb4a12a92008-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 Warren6904b6e2011-10-18 11:11:49 +0000257 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100258
259 if (idx < count) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000260 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100261 offset = 0;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100262
263 /* go over all indices preceding requested component idx */
264 for (i = 0; i < idx; i++) {
Nick Spence143a1592008-05-10 14:02:04 -0700265 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000266 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100267 }
268
269 /* calculate idx-th component data address */
Nick Spence143a1592008-05-10 14:02:04 -0700270 *data = img_data + offset;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100271 } else {
272 *len = 0;
273 *data = 0;
274 }
275}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100276
Stephen Warren6904b6e2011-10-18 11:11:49 +0000277static void image_print_type(const image_header_t *hdr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100278{
279 const char *os, *arch, *type, *comp;
280
Stephen Warren6904b6e2011-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100285
Stephen Warren6904b6e2011-10-18 11:11:49 +0000286 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100287}
288
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100289/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200290 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3b506772009-08-19 11:42:56 +0200291 * @ptr: pointer to the legacy format image header
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100292 * @p: pointer to prefix string
293 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200294 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicze227fbb2008-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 Warren6904b6e2011-10-18 11:11:49 +0000301void image_print_contents(const void *ptr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100302{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200303 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200304 const char *p;
305
306#ifdef USE_HOSTCC
307 p = "";
308#else
309 p = " ";
310#endif
311
Stephen Warren6904b6e2011-10-18 11:11:49 +0000312 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100313#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000314 printf("%sCreated: ", p);
315 genimg_print_time((time_t)image_get_time(hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100316#endif
Stephen Warren6904b6e2011-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100323
Stephen Warren6904b6e2011-10-18 11:11:49 +0000324 if (image_check_type(hdr, IH_TYPE_MULTI) ||
325 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100326 int i;
327 ulong data, len;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000328 ulong count = image_multi_count(hdr);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100329
Stephen Warren6904b6e2011-10-18 11:11:49 +0000330 printf("%sContents:\n", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100331 for (i = 0; i < count; i++) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000332 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100333
Stephen Warren6904b6e2011-10-18 11:11:49 +0000334 printf("%s Image %d: ", p, i);
335 genimg_print_size(len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100336
Stephen Warren6904b6e2011-10-18 11:11:49 +0000337 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowiczc3c7eb22008-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 Warren6904b6e2011-10-18 11:11:49 +0000343 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100344 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100345 }
346 }
347}
348
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100349
350#ifndef USE_HOSTCC
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100351/**
352 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowiczd7c88a42008-02-29 14:58:34 +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
367 * otherwise, return NULL
368 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000369static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100370 int verify)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100371{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200372 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100373
Stephen Warren6904b6e2011-10-18 11:11:49 +0000374 if (!image_check_magic(rd_hdr)) {
375 puts("Bad Magic Number\n");
376 show_boot_progress(-10);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100377 return NULL;
378 }
379
Stephen Warren6904b6e2011-10-18 11:11:49 +0000380 if (!image_check_hcrc(rd_hdr)) {
381 puts("Bad Header Checksum\n");
382 show_boot_progress(-11);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100383 return NULL;
384 }
385
Stephen Warren6904b6e2011-10-18 11:11:49 +0000386 show_boot_progress(10);
387 image_print_contents(rd_hdr);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100388
389 if (verify) {
390 puts(" Verifying Checksum ... ");
Stephen Warren6904b6e2011-10-18 11:11:49 +0000391 if (!image_check_dcrc(rd_hdr)) {
392 puts("Bad Data CRC\n");
393 show_boot_progress(-12);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100394 return NULL;
395 }
396 puts("OK\n");
397 }
398
Stephen Warren6904b6e2011-10-18 11:11:49 +0000399 show_boot_progress(11);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100400
Stephen Warren6904b6e2011-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100405 genimg_get_arch_name(arch));
Stephen Warren6904b6e2011-10-18 11:11:49 +0000406 show_boot_progress(-13);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100407 return NULL;
408 }
409
410 return rd_hdr;
411}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100412#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100413
414/*****************************************************************************/
415/* Shared dual-format routines */
416/*****************************************************************************/
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100417#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +0000418int getenv_yesno(char *var)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100419{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000420 char *s = getenv(var);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100421 return (s && (*s == 'n')) ? 0 : 1;
422}
423
424ulong getenv_bootm_low(void)
425{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000426 char *s = getenv("bootm_low");
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100427 if (s) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000428 ulong tmp = simple_strtoul(s, NULL, 16);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100429 return tmp;
430 }
431
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200432#if defined(CONFIG_SYS_SDRAM_BASE)
433 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100434#elif defined(CONFIG_ARM)
435 return gd->bd->bi_dram[0].start;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100436#else
437 return 0;
438#endif
439}
440
Becky Bruced26d67c2008-06-09 20:37:18 -0500441phys_size_t getenv_bootm_size(void)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100442{
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500443 phys_size_t tmp;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000444 char *s = getenv("bootm_size");
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100445 if (s) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000446 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100447 return tmp;
448 }
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500449 s = getenv("bootm_low");
450 if (s)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000451 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500452 else
453 tmp = 0;
454
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100455
Marian Balakowicz2499b042008-03-12 12:14:15 +0100456#if defined(CONFIG_ARM)
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500457 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100458#else
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500459 return gd->bd->bi_memsize - tmp;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100460#endif
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100461}
462
Grant Likely26396382011-03-28 09:58:43 +0000463phys_size_t getenv_bootm_mapsize(void)
464{
465 phys_size_t tmp;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000466 char *s = getenv("bootm_mapsize");
Grant Likely26396382011-03-28 09:58:43 +0000467 if (s) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000468 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likely26396382011-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 Warren6904b6e2011-10-18 11:11:49 +0000479void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100480{
Larry Johnsone4551002010-04-20 08:09:43 -0400481 if (to == from)
482 return;
483
Marian Balakowiczd7c88a42008-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 Warren6904b6e2011-10-18 11:11:49 +0000487 WATCHDOG_RESET();
488 memmove(to, from, tail);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100489 to += tail;
490 from += tail;
491 len -= tail;
492 }
493#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000494 memmove(to, from, len);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100495#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
496}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100497#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100498
Stephen Warren6904b6e2011-10-18 11:11:49 +0000499void genimg_print_size(uint32_t size)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100500{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100501#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +0000502 printf("%d Bytes = ", size);
503 print_size(size, "\n");
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100504#else
Stephen Warren6904b6e2011-10-18 11:11:49 +0000505 printf("%d Bytes = %.2f kB = %.2f MB\n",
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100506 size, (double)size / 1.024e3,
507 (double)size / 1.048576e6);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100508#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100509}
510
511#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000512static void genimg_print_time(time_t timestamp)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100513{
514#ifndef USE_HOSTCC
515 struct rtc_time tm;
516
Stephen Warren6904b6e2011-10-18 11:11:49 +0000517 to_tm(timestamp, &tm);
518 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowiczc3c7eb22008-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 Warren6904b6e2011-10-18 11:11:49 +0000522 printf("%s", ctime(&timestamp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100523#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100524}
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 Frysingercf2feb12010-10-20 07:17:39 -0400541char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100542{
543 for (; table->id >= 0; ++table) {
544 if (table->id == id)
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200545#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Wood68498ab2009-04-02 16:15:10 -0500546 return table->lname;
547#else
548 return table->lname + gd->reloc_off;
549#endif
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100550 }
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100551 return (msg);
552}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100553
Stephen Warren6904b6e2011-10-18 11:11:49 +0000554const char *genimg_get_os_name(uint8_t os)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100555{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000556 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100557}
558
Stephen Warren6904b6e2011-10-18 11:11:49 +0000559const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100560{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000561 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
562 arch));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100563}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100564
Stephen Warren6904b6e2011-10-18 11:11:49 +0000565const char *genimg_get_type_name(uint8_t type)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100566{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000567 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100568}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100569
Stephen Warren6904b6e2011-10-18 11:11:49 +0000570const char *genimg_get_comp_name(uint8_t comp)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100571{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000572 return (get_table_entry_name(uimage_comp, "Unknown Compression",
573 comp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100574}
575
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100576/**
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 Frysingercf2feb12010-10-20 07:17:39 -0400590int get_table_entry_id(const table_entry_t *table,
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100591 const char *table_name, const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100592{
Mike Frysingercf2feb12010-10-20 07:17:39 -0400593 const table_entry_t *t;
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100594#ifdef USE_HOSTCC
595 int first = 1;
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100596
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100597 for (t = table; t->id >= 0; ++t) {
598 if (t->sname && strcasecmp(t->sname, name) == 0)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000599 return(t->id);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100600 }
601
Stephen Warren6904b6e2011-10-18 11:11:49 +0000602 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100603 for (t = table; t->id >= 0; ++t) {
604 if (t->sname == NULL)
605 continue;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000606 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100607 first = 0;
608 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000609 fprintf(stderr, "\n");
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100610#else
611 for (t = table; t->id >= 0; ++t) {
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200612#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Wood68498ab2009-04-02 16:15:10 -0500613 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200614#else
615 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser9057cbf2009-09-21 11:20:36 -0500616#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100617 return (t->id);
618 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000619 debug("Invalid %s Type: %s\n", table_name, name);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100620#endif /* USE_HOSTCC */
621 return (-1);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100622}
623
Stephen Warren6904b6e2011-10-18 11:11:49 +0000624int genimg_get_os_id(const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100625{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000626 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100627}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100628
Stephen Warren6904b6e2011-10-18 11:11:49 +0000629int genimg_get_arch_id(const char *name)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100630{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000631 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100632}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100633
Stephen Warren6904b6e2011-10-18 11:11:49 +0000634int genimg_get_type_id(const char *name)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100635{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000636 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100637}
Marian Balakowicz95418502008-01-31 13:55:39 +0100638
Stephen Warren6904b6e2011-10-18 11:11:49 +0000639int genimg_get_comp_id(const char *name)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100640{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000641 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100642}
643
644#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +0100645/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100646 * genimg_get_format - get image format type
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100647 * @img_addr: image start address
648 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100649 * genimg_get_format() checks whether provided address points to a valid
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100650 * legacy or FIT image.
651 *
Marian Balakowicz12560682008-02-27 11:02:26 +0100652 * 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
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100654 * genimg_get_format() must be able to dectect libfdt header.
Marian Balakowicz12560682008-02-27 11:02:26 +0100655 *
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100656 * returns:
657 * image format type or IMAGE_FORMAT_INVALID if no image is present
658 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000659int genimg_get_format(void *img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100660{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200661 ulong format = IMAGE_FORMAT_INVALID;
662 const image_header_t *hdr;
Marian Balakowicz12560682008-02-27 11:02:26 +0100663#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3b506772009-08-19 11:42:56 +0200664 char *fit_hdr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100665#endif
666
Wolfgang Denk3b506772009-08-19 11:42:56 +0200667 hdr = (const image_header_t *)img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100668 if (image_check_magic(hdr))
669 format = IMAGE_FORMAT_LEGACY;
Marian Balakowicz12560682008-02-27 11:02:26 +0100670#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100671 else {
672 fit_hdr = (char *)img_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000673 if (fdt_check_header(fit_hdr) == 0)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100674 format = IMAGE_FORMAT_FIT;
675 }
676#endif
677
678 return format;
679}
680
681/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100682 * genimg_get_image - get image from special storage (if necessary)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100683 * @img_addr: image start address
684 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100685 * genimg_get_image() checks if provided image start adddress is located
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100686 * 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 Warren6904b6e2011-10-18 11:11:49 +0000691ulong genimg_get_image(ulong img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100692{
Marian Balakowicz535b9102008-02-27 11:00:47 +0100693 ulong ram_addr = img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100694
695#ifdef CONFIG_HAS_DATAFLASH
Marian Balakowicz535b9102008-02-27 11:00:47 +0100696 ulong h_size, d_size;
697
Stephen Warren6904b6e2011-10-18 11:11:49 +0000698 if (addr_dataflash(img_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100699 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200700 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100701
702 /* get header size */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000703 h_size = image_get_header_size();
Marian Balakowicz535b9102008-02-27 11:00:47 +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 Warren6904b6e2011-10-18 11:11:49 +0000710 debug(" Reading image header from dataflash address "
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100711 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100712
Stephen Warren6904b6e2011-10-18 11:11:49 +0000713 read_dataflash(img_addr, h_size, (char *)ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100714
Marian Balakowicz535b9102008-02-27 11:00:47 +0100715 /* get data size */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000716 switch (genimg_get_format((void *)ram_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100717 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-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 Balakowicz535b9102008-02-27 11:00:47 +0100722 ram_addr, d_size);
723 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100724#if defined(CONFIG_FIT)
Marian Balakowicz535b9102008-02-27 11:00:47 +0100725 case IMAGE_FORMAT_FIT:
Stephen Warren6904b6e2011-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 Balakowicz535b9102008-02-27 11:00:47 +0100729 ram_addr, d_size);
730 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100731#endif
Marian Balakowicz535b9102008-02-27 11:00:47 +0100732 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000733 printf(" No valid image found at 0x%08lx\n",
734 img_addr);
Marian Balakowicz535b9102008-02-27 11:00:47 +0100735 return ram_addr;
736 }
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100737
Marian Balakowicz535b9102008-02-27 11:00:47 +0100738 /* read in image data */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000739 debug(" Reading image remaining data from dataflash address "
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100740 "%08lx to RAM address %08lx\n", img_addr + h_size,
741 ram_addr + h_size);
742
Stephen Warren6904b6e2011-10-18 11:11:49 +0000743 read_dataflash(img_addr + h_size, d_size,
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100744 (char *)(ram_addr + h_size));
Marian Balakowicz535b9102008-02-27 11:00:47 +0100745
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100746 }
Marian Balakowicz535b9102008-02-27 11:00:47 +0100747#endif /* CONFIG_HAS_DATAFLASH */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100748
749 return ram_addr;
750}
751
752/**
Marian Balakowicz0cd4f3d2008-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 Warren6904b6e2011-10-18 11:11:49 +0000763int genimg_has_config(bootm_headers_t *images)
Marian Balakowicz0cd4f3d2008-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100773 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz95418502008-01-31 13:55:39 +0100774 * @argc: command argument count
775 * @argv: command argument list
Marian Balakowicz61fde552008-02-27 11:01:04 +0100776 * @images: pointer to the bootm images structure
Marian Balakowicz95418502008-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100781 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz95418502008-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 Balakowicza0ffb422008-03-12 10:14:38 +0100787 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz95418502008-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 Balakowicza0ffb422008-03-12 10:14:38 +0100790 *
Kumar Galab8ca7342008-08-29 19:08:29 -0500791 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz95418502008-01-31 13:55:39 +0100792 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz95418502008-01-31 13:55:39 +0100793 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000794int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100795 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz95418502008-01-31 13:55:39 +0100796{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100797 ulong rd_addr, rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100798 ulong rd_data, rd_len;
Wolfgang Denk3b506772009-08-19 11:42:56 +0200799 const image_header_t *rd_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100800#if defined(CONFIG_FIT)
801 void *fit_hdr;
802 const char *fit_uname_config = NULL;
803 const char *fit_uname_ramdisk = NULL;
804 ulong default_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100805 int rd_noffset;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100806 int cfg_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100807 const void *data;
808 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100809#endif
Marian Balakowicz95418502008-01-31 13:55:39 +0100810
Marian Balakowicza50d5152008-03-12 10:12:37 +0100811 *rd_start = 0;
812 *rd_end = 0;
813
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100814 /*
815 * Look for a '-' which indicates to ignore the
816 * ramdisk argument
817 */
818 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000819 debug("## Skipping init Ramdisk\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100820 rd_len = rd_data = 0;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000821 } else if (argc >= 3 || genimg_has_config(images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100822#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100823 if (argc >= 3) {
824 /*
825 * If the init ramdisk comes from the FIT image and
826 * the FIT image address is omitted in the command
827 * line argument, try to use os FIT image address or
828 * default load address.
829 */
830 if (images->fit_uname_os)
831 default_addr = (ulong)images->fit_hdr_os;
832 else
833 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100834
Stephen Warren6904b6e2011-10-18 11:11:49 +0000835 if (fit_parse_conf(argv[2], default_addr,
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100836 &rd_addr, &fit_uname_config)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000837 debug("* ramdisk: config '%s' from image at "
838 "0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100839 fit_uname_config, rd_addr);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000840 } else if (fit_parse_subimage(argv[2], default_addr,
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100841 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000842 debug("* ramdisk: subimage '%s' from image at "
843 "0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100844 fit_uname_ramdisk, rd_addr);
845 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100846#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100847 {
848 rd_addr = simple_strtoul(argv[2], NULL, 16);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000849 debug("* ramdisk: cmdline image address = "
850 "0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100851 rd_addr);
852 }
853#if defined(CONFIG_FIT)
854 } else {
855 /* use FIT configuration provided in first bootm
856 * command argument
857 */
858 rd_addr = (ulong)images->fit_hdr_os;
859 fit_uname_config = images->fit_uname_cfg;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000860 debug("* ramdisk: using config '%s' from image "
861 "at 0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100862 fit_uname_config, rd_addr);
863
864 /*
865 * Check whether configuration has ramdisk defined,
866 * if not, don't try to use it, quit silently.
867 */
868 fit_hdr = (void *)rd_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000869 cfg_noffset = fit_conf_get_node(fit_hdr,
870 fit_uname_config);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100871 if (cfg_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000872 debug("* ramdisk: no such config\n");
Michal Simekb2474002008-07-11 10:43:13 +0200873 return 1;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100874 }
875
Stephen Warren6904b6e2011-10-18 11:11:49 +0000876 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
877 cfg_noffset);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100878 if (rd_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000879 debug("* ramdisk: no ramdisk in config\n");
Peter Tyser190ee482008-08-05 10:51:57 -0500880 return 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100881 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100882 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100883#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100884
885 /* copy from dataflash if needed */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000886 rd_addr = genimg_get_image(rd_addr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100887
888 /*
889 * Check if there is an initrd image at the
890 * address provided in the second bootm argument
891 * check image type, for FIT images get FIT node.
892 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000893 switch (genimg_get_format((void *)rd_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100894 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000895 printf("## Loading init Ramdisk from Legacy "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100896 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz95418502008-01-31 13:55:39 +0100897
Stephen Warren6904b6e2011-10-18 11:11:49 +0000898 show_boot_progress(9);
899 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100900 images->verify);
Marian Balakowicz95418502008-01-31 13:55:39 +0100901
Marian Balakowicza50d5152008-03-12 10:12:37 +0100902 if (rd_hdr == NULL)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600903 return 1;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600904
Stephen Warren6904b6e2011-10-18 11:11:49 +0000905 rd_data = image_get_data(rd_hdr);
906 rd_len = image_get_data_size(rd_hdr);
907 rd_load = image_get_load(rd_hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100908 break;
909#if defined(CONFIG_FIT)
910 case IMAGE_FORMAT_FIT:
911 fit_hdr = (void *)rd_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000912 printf("## Loading init Ramdisk from FIT "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100913 "Image at %08lx ...\n", rd_addr);
914
Stephen Warren6904b6e2011-10-18 11:11:49 +0000915 show_boot_progress(120);
916 if (!fit_check_format(fit_hdr)) {
917 puts("Bad FIT ramdisk image format!\n");
918 show_boot_progress(-120);
Michal Simekb2474002008-07-11 10:43:13 +0200919 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100920 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000921 show_boot_progress(121);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100922
923 if (!fit_uname_ramdisk) {
924 /*
925 * no ramdisk image node unit name, try to get config
926 * node first. If config unit node name is NULL
927 * fit_conf_get_node() will try to find default config node
928 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000929 show_boot_progress(122);
930 cfg_noffset = fit_conf_get_node(fit_hdr,
931 fit_uname_config);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100932 if (cfg_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000933 puts("Could not find configuration "
934 "node\n");
935 show_boot_progress(-122);
Michal Simekb2474002008-07-11 10:43:13 +0200936 return 1;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100937 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000938 fit_uname_config = fdt_get_name(fit_hdr,
939 cfg_noffset, NULL);
940 printf(" Using '%s' configuration\n",
941 fit_uname_config);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100942
Stephen Warren6904b6e2011-10-18 11:11:49 +0000943 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
944 cfg_noffset);
945 fit_uname_ramdisk = fit_get_name(fit_hdr,
946 rd_noffset, NULL);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100947 } else {
948 /* get ramdisk component image node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000949 show_boot_progress(123);
950 rd_noffset = fit_image_get_node(fit_hdr,
951 fit_uname_ramdisk);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100952 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100953 if (rd_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000954 puts("Could not find subimage node\n");
955 show_boot_progress(-124);
Michal Simekb2474002008-07-11 10:43:13 +0200956 return 1;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100957 }
Marian Balakowicza50d5152008-03-12 10:12:37 +0100958
Stephen Warren6904b6e2011-10-18 11:11:49 +0000959 printf(" Trying '%s' ramdisk subimage\n",
960 fit_uname_ramdisk);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100961
Stephen Warren6904b6e2011-10-18 11:11:49 +0000962 show_boot_progress(125);
963 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
964 images->verify))
Michal Simekb2474002008-07-11 10:43:13 +0200965 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100966
967 /* get ramdisk image data address and length */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000968 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
969 &size)) {
970 puts("Could not find ramdisk subimage data!\n");
971 show_boot_progress(-127);
Michal Simekb2474002008-07-11 10:43:13 +0200972 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100973 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000974 show_boot_progress(128);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100975
976 rd_data = (ulong)data;
977 rd_len = size;
978
Stephen Warren6904b6e2011-10-18 11:11:49 +0000979 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
980 puts("Can't get ramdisk subimage load "
981 "address!\n");
982 show_boot_progress(-129);
Michal Simekb2474002008-07-11 10:43:13 +0200983 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100984 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000985 show_boot_progress(129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100986
987 images->fit_hdr_rd = fit_hdr;
988 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100989 images->fit_noffset_rd = rd_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100990 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100991#endif
992 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000993 puts("Wrong Ramdisk Image Format\n");
Marian Balakowicza50d5152008-03-12 10:12:37 +0100994 rd_data = rd_len = rd_load = 0;
Kumar Galab8ca7342008-08-29 19:08:29 -0500995 return 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100996 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100997 } else if (images->legacy_hdr_valid &&
Stephen Warren6904b6e2011-10-18 11:11:49 +0000998 image_check_type(&images->legacy_hdr_os_copy,
999 IH_TYPE_MULTI)) {
1000
Marian Balakowicz95418502008-01-31 13:55:39 +01001001 /*
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001002 * Now check if we have a legacy mult-component image,
1003 * get second entry data start address and len.
Marian Balakowicz95418502008-01-31 13:55:39 +01001004 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001005 show_boot_progress(13);
1006 printf("## Loading init Ramdisk from multi component "
Marian Balakowicza50d5152008-03-12 10:12:37 +01001007 "Legacy Image at %08lx ...\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001008 (ulong)images->legacy_hdr_os);
1009
Stephen Warren6904b6e2011-10-18 11:11:49 +00001010 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz95418502008-01-31 13:55:39 +01001011 } else {
1012 /*
1013 * no initrd image
1014 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001015 show_boot_progress(14);
Marian Balakowicz95418502008-01-31 13:55:39 +01001016 rd_len = rd_data = 0;
1017 }
1018
1019 if (!rd_data) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001020 debug("## No init Ramdisk\n");
Marian Balakowicz95418502008-01-31 13:55:39 +01001021 } else {
1022 *rd_start = rd_data;
1023 *rd_end = rd_data + rd_len;
1024 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001025 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz95418502008-01-31 13:55:39 +01001026 *rd_start, *rd_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -06001027
1028 return 0;
Marian Balakowicz95418502008-01-31 13:55:39 +01001029}
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001030
John Rigbyeea8e692010-10-13 13:57:35 -06001031#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001032/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001033 * boot_ramdisk_high - relocate init ramdisk
Kumar Galab937bb72008-02-27 21:51:49 -06001034 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001035 * @rd_data: ramdisk data start address
1036 * @rd_len: ramdisk data length
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001037 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1038 * start address (after possible relocation)
1039 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1040 * end address (after possible relocation)
1041 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001042 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001043 * variable and if requested ramdisk data is moved to a specified location.
1044 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001045 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1046 * start/end addresses if ramdisk image start and len were provided,
1047 * otherwise set initrd_start and initrd_end set to zeros.
1048 *
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001049 * returns:
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001050 * 0 - success
1051 * -1 - failure
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001052 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001053int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galab937bb72008-02-27 21:51:49 -06001054 ulong *initrd_start, ulong *initrd_end)
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001055{
1056 char *s;
1057 ulong initrd_high;
1058 int initrd_copy_to_ram = 1;
1059
Stephen Warren6904b6e2011-10-18 11:11:49 +00001060 if ((s = getenv("initrd_high")) != NULL) {
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001061 /* a value of "no" or a similar string will act like 0,
1062 * turning the "load high" feature off. This is intentional.
1063 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001064 initrd_high = simple_strtoul(s, NULL, 16);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001065 if (initrd_high == ~0)
1066 initrd_copy_to_ram = 0;
1067 } else {
1068 /* not set, no restrictions to load high */
1069 initrd_high = ~0;
1070 }
1071
Marian Balakowicz902ce702008-05-13 15:53:29 +02001072
1073#ifdef CONFIG_LOGBUFFER
1074 /* Prevent initrd from overwriting logbuffer */
1075 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1076#endif
1077
Stephen Warren6904b6e2011-10-18 11:11:49 +00001078 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001079 initrd_high, initrd_copy_to_ram);
1080
1081 if (rd_data) {
1082 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001083 debug(" in-place initrd\n");
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001084 *initrd_start = rd_data;
1085 *initrd_end = rd_data + rd_len;
Kumar Galab937bb72008-02-27 21:51:49 -06001086 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001087 } else {
Kumar Galab937bb72008-02-27 21:51:49 -06001088 if (initrd_high)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001089 *initrd_start = (ulong)lmb_alloc_base(lmb,
1090 rd_len, 0x1000, initrd_high);
Kumar Galab937bb72008-02-27 21:51:49 -06001091 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00001092 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1093 0x1000);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001094
Kumar Galab937bb72008-02-27 21:51:49 -06001095 if (*initrd_start == 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001096 puts("ramdisk - allocation error\n");
Kumar Galab937bb72008-02-27 21:51:49 -06001097 goto error;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001098 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001099 show_boot_progress(12);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001100
1101 *initrd_end = *initrd_start + rd_len;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001102 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001103 *initrd_start, *initrd_end);
1104
Stephen Warren6904b6e2011-10-18 11:11:49 +00001105 memmove_wd((void *)*initrd_start,
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001106 (void *)rd_data, rd_len, CHUNKSZ);
1107
Kumar Gala180c5812011-12-07 04:42:58 +00001108#ifdef CONFIG_MP
1109 /*
1110 * Ensure the image is flushed to memory to handle
1111 * AMP boot scenarios in which we might not be
1112 * HW cache coherent
1113 */
1114 flush_cache((unsigned long)*initrd_start, rd_len);
1115#endif
Stephen Warren6904b6e2011-10-18 11:11:49 +00001116 puts("OK\n");
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001117 }
1118 } else {
1119 *initrd_start = 0;
1120 *initrd_end = 0;
1121 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001122 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001123 *initrd_start, *initrd_end);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001124
Kumar Galab937bb72008-02-27 21:51:49 -06001125 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001126
Kumar Galab937bb72008-02-27 21:51:49 -06001127error:
1128 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001129}
John Rigbyeea8e692010-10-13 13:57:35 -06001130#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001131
Kumar Galaa5130ad2008-08-15 08:24:38 -05001132#ifdef CONFIG_OF_LIBFDT
Stephen Warren6904b6e2011-10-18 11:11:49 +00001133static void fdt_error(const char *msg)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001134{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001135 puts("ERROR: ");
1136 puts(msg);
1137 puts(" - must RESET the board to recover.\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001138}
1139
Stephen Warren6904b6e2011-10-18 11:11:49 +00001140static const image_header_t *image_get_fdt(ulong fdt_addr)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001141{
Wolfgang Denk3b506772009-08-19 11:42:56 +02001142 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001143
Stephen Warren6904b6e2011-10-18 11:11:49 +00001144 image_print_contents(fdt_hdr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001145
Stephen Warren6904b6e2011-10-18 11:11:49 +00001146 puts(" Verifying Checksum ... ");
1147 if (!image_check_hcrc(fdt_hdr)) {
1148 fdt_error("fdt header checksum invalid");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001149 return NULL;
1150 }
1151
Stephen Warren6904b6e2011-10-18 11:11:49 +00001152 if (!image_check_dcrc(fdt_hdr)) {
1153 fdt_error("fdt checksum invalid");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001154 return NULL;
1155 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001156 puts("OK\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001157
Stephen Warren6904b6e2011-10-18 11:11:49 +00001158 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1159 fdt_error("uImage is not a fdt");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001160 return NULL;
1161 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001162 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1163 fdt_error("uImage is compressed");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001164 return NULL;
1165 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001166 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1167 fdt_error("uImage data is not a fdt");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001168 return NULL;
1169 }
1170 return fdt_hdr;
1171}
1172
1173/**
1174 * fit_check_fdt - verify FIT format FDT subimage
1175 * @fit_hdr: pointer to the FIT header
1176 * fdt_noffset: FDT subimage node offset within FIT image
1177 * @verify: data CRC verification flag
1178 *
1179 * fit_check_fdt() verifies integrity of the FDT subimage and from
1180 * specified FIT image.
1181 *
1182 * returns:
1183 * 1, on success
1184 * 0, on failure
1185 */
1186#if defined(CONFIG_FIT)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001187static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001188{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001189 fit_image_print(fit, fdt_noffset, " ");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001190
1191 if (verify) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001192 puts(" Verifying Hash Integrity ... ");
1193 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1194 fdt_error("Bad Data Hash");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001195 return 0;
1196 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001197 puts("OK\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001198 }
1199
Stephen Warren6904b6e2011-10-18 11:11:49 +00001200 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1201 fdt_error("Not a FDT image");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001202 return 0;
1203 }
1204
Stephen Warren6904b6e2011-10-18 11:11:49 +00001205 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1206 fdt_error("FDT image is compressed");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001207 return 0;
1208 }
1209
1210 return 1;
1211}
1212#endif /* CONFIG_FIT */
1213
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001214#ifndef CONFIG_SYS_FDT_PAD
1215#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Galaa5130ad2008-08-15 08:24:38 -05001216#endif
1217
Grant Likely20f54922011-03-28 09:59:01 +00001218#if defined(CONFIG_OF_LIBFDT)
1219/**
1220 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1221 * @lmb: pointer to lmb handle, will be used for memory mgmt
1222 * @fdt_blob: pointer to fdt blob base address
1223 *
1224 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1225 * memreserve regions prevents u-boot from using them to store the initrd
1226 * or the fdt blob.
1227 */
1228void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1229{
1230 uint64_t addr, size;
1231 int i, total;
1232
Stephen Warren6904b6e2011-10-18 11:11:49 +00001233 if (fdt_check_header(fdt_blob) != 0)
Grant Likely20f54922011-03-28 09:59:01 +00001234 return;
1235
1236 total = fdt_num_mem_rsv(fdt_blob);
1237 for (i = 0; i < total; i++) {
1238 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1239 continue;
1240 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1241 (unsigned long long)addr, (unsigned long long)size);
1242 lmb_reserve(lmb, addr, size);
1243 }
1244}
1245
Kumar Galaa5130ad2008-08-15 08:24:38 -05001246/**
1247 * boot_relocate_fdt - relocate flat device tree
1248 * @lmb: pointer to lmb handle, will be used for memory mgmt
Kumar Galaa5130ad2008-08-15 08:24:38 -05001249 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1250 * @of_size: pointer to a ulong variable, will hold fdt length
1251 *
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001252 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1253 * relocates the of_flat_tree into that region, even if the fdt is already in
1254 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1255 * bytes.
Kumar Galaa5130ad2008-08-15 08:24:38 -05001256 *
1257 * of_flat_tree and of_size are set to final (after relocation) values
1258 *
1259 * returns:
1260 * 0 - success
1261 * 1 - failure
1262 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001263int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001264{
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001265 void *fdt_blob = *of_flat_tree;
1266 void *of_start = 0;
David A. Longd558a4e2011-07-09 16:40:19 -04001267 char *fdt_high;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001268 ulong of_len = 0;
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001269 int err;
David A. Longd558a4e2011-07-09 16:40:19 -04001270 int disable_relocation = 0;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001271
1272 /* nothing to do */
1273 if (*of_size == 0)
1274 return 0;
1275
Stephen Warren6904b6e2011-10-18 11:11:49 +00001276 if (fdt_check_header(fdt_blob) != 0) {
1277 fdt_error("image is not a fdt");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001278 goto error;
1279 }
1280
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001281 /* position on a 4K boundary before the alloc_current */
1282 /* Pad the FDT by a specified amount */
1283 of_len = *of_size + CONFIG_SYS_FDT_PAD;
David A. Longd558a4e2011-07-09 16:40:19 -04001284
1285 /* If fdt_high is set use it to select the relocation address */
1286 fdt_high = getenv("fdt_high");
1287 if (fdt_high) {
1288 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1289
1290 if (((ulong) desired_addr) == ~0UL) {
1291 /* All ones means use fdt in place */
Shawn Guo0ca9e982012-01-09 21:54:08 +00001292 of_start = fdt_blob;
1293 lmb_reserve(lmb, (ulong)of_start, of_len);
David A. Longd558a4e2011-07-09 16:40:19 -04001294 disable_relocation = 1;
Shawn Guo0ca9e982012-01-09 21:54:08 +00001295 } else if (desired_addr) {
David A. Longd558a4e2011-07-09 16:40:19 -04001296 of_start =
1297 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
Shawn Guo0ca9e982012-01-09 21:54:08 +00001298 (ulong)desired_addr);
1299 if (of_start == 0) {
David A. Longd558a4e2011-07-09 16:40:19 -04001300 puts("Failed using fdt_high value for Device Tree");
1301 goto error;
1302 }
1303 } else {
1304 of_start =
Matthew McClintockdcf6fd72011-07-18 13:08:05 +00001305 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
David A. Longd558a4e2011-07-09 16:40:19 -04001306 }
1307 } else {
1308 of_start =
1309 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1310 getenv_bootm_mapsize()
1311 + getenv_bootm_low());
1312 }
Kumar Galaa5130ad2008-08-15 08:24:38 -05001313
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001314 if (of_start == 0) {
1315 puts("device tree - allocation error\n");
1316 goto error;
1317 }
Kumar Galaa5130ad2008-08-15 08:24:38 -05001318
David A. Longd558a4e2011-07-09 16:40:19 -04001319 if (disable_relocation) {
1320 /* We assume there is space after the existing fdt to use for padding */
1321 fdt_set_totalsize(of_start, of_len);
1322 printf(" Using Device Tree in place at %p, end %p\n",
1323 of_start, of_start + of_len - 1);
1324 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001325 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
David A. Longd558a4e2011-07-09 16:40:19 -04001326 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001327
Stephen Warren6904b6e2011-10-18 11:11:49 +00001328 printf(" Loading Device Tree to %p, end %p ... ",
David A. Longd558a4e2011-07-09 16:40:19 -04001329 of_start, of_start + of_len - 1);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001330
Stephen Warren6904b6e2011-10-18 11:11:49 +00001331 err = fdt_open_into(fdt_blob, of_start, of_len);
David A. Longd558a4e2011-07-09 16:40:19 -04001332 if (err != 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001333 fdt_error("fdt move failed");
David A. Longd558a4e2011-07-09 16:40:19 -04001334 goto error;
1335 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001336 puts("OK\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001337 }
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001338
1339 *of_flat_tree = of_start;
1340 *of_size = of_len;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001341
Kumar Gala9f4e7e42008-08-15 08:24:39 -05001342 set_working_fdt_addr(*of_flat_tree);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001343 return 0;
1344
1345error:
1346 return 1;
1347}
Grant Likely50fb22e2011-03-28 09:58:49 +00001348#endif /* CONFIG_OF_LIBFDT */
Kumar Galaa5130ad2008-08-15 08:24:38 -05001349
1350/**
1351 * boot_get_fdt - main fdt handling routine
1352 * @argc: command argument count
1353 * @argv: command argument list
1354 * @images: pointer to the bootm images structure
1355 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1356 * @of_size: pointer to a ulong variable, will hold fdt length
1357 *
1358 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1359 * Curently supported are the following ramdisk sources:
1360 * - multicomponent kernel/ramdisk image,
1361 * - commandline provided address of decicated ramdisk image.
1362 *
1363 * returns:
1364 * 0, if fdt image was found and valid, or skipped
1365 * of_flat_tree and of_size are set to fdt start address and length if
1366 * fdt image is found and valid
1367 *
1368 * 1, if fdt image is found but corrupted
1369 * of_flat_tree and of_size are set to 0 if no fdt exists
1370 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001371int boot_get_fdt(int flag, int argc, char * const argv[],
1372 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001373{
Wolfgang Denk3b506772009-08-19 11:42:56 +02001374 const image_header_t *fdt_hdr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001375 ulong fdt_addr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001376 char *fdt_blob = NULL;
Stephen Warren84d62282011-11-01 06:28:21 +00001377 ulong image_start, image_data, image_end;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001378 ulong load_start, load_end;
1379#if defined(CONFIG_FIT)
1380 void *fit_hdr;
1381 const char *fit_uname_config = NULL;
1382 const char *fit_uname_fdt = NULL;
1383 ulong default_addr;
1384 int cfg_noffset;
1385 int fdt_noffset;
1386 const void *data;
1387 size_t size;
1388#endif
1389
1390 *of_flat_tree = NULL;
1391 *of_size = 0;
1392
Stephen Warren6904b6e2011-10-18 11:11:49 +00001393 if (argc > 3 || genimg_has_config(images)) {
Kumar Galaa5130ad2008-08-15 08:24:38 -05001394#if defined(CONFIG_FIT)
1395 if (argc > 3) {
1396 /*
1397 * If the FDT blob comes from the FIT image and the
1398 * FIT image address is omitted in the command line
1399 * argument, try to use ramdisk or os FIT image
1400 * address or default load address.
1401 */
1402 if (images->fit_uname_rd)
1403 default_addr = (ulong)images->fit_hdr_rd;
1404 else if (images->fit_uname_os)
1405 default_addr = (ulong)images->fit_hdr_os;
1406 else
1407 default_addr = load_addr;
1408
Stephen Warren6904b6e2011-10-18 11:11:49 +00001409 if (fit_parse_conf(argv[3], default_addr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001410 &fdt_addr, &fit_uname_config)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001411 debug("* fdt: config '%s' from image at "
1412 "0x%08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001413 fit_uname_config, fdt_addr);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001414 } else if (fit_parse_subimage(argv[3], default_addr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001415 &fdt_addr, &fit_uname_fdt)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001416 debug("* fdt: subimage '%s' from image at "
1417 "0x%08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001418 fit_uname_fdt, fdt_addr);
1419 } else
1420#endif
1421 {
1422 fdt_addr = simple_strtoul(argv[3], NULL, 16);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001423 debug("* fdt: cmdline image address = "
1424 "0x%08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001425 fdt_addr);
1426 }
1427#if defined(CONFIG_FIT)
1428 } else {
1429 /* use FIT configuration provided in first bootm
1430 * command argument
1431 */
1432 fdt_addr = (ulong)images->fit_hdr_os;
1433 fit_uname_config = images->fit_uname_cfg;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001434 debug("* fdt: using config '%s' from image "
1435 "at 0x%08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001436 fit_uname_config, fdt_addr);
1437
1438 /*
1439 * Check whether configuration has FDT blob defined,
1440 * if not quit silently.
1441 */
1442 fit_hdr = (void *)fdt_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001443 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001444 fit_uname_config);
1445 if (cfg_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001446 debug("* fdt: no such config\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001447 return 0;
1448 }
1449
Stephen Warren6904b6e2011-10-18 11:11:49 +00001450 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001451 cfg_noffset);
1452 if (fdt_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001453 debug("* fdt: no fdt in config\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001454 return 0;
1455 }
1456 }
1457#endif
1458
Stephen Warren6904b6e2011-10-18 11:11:49 +00001459 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001460 fdt_addr);
1461
1462 /* copy from dataflash if needed */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001463 fdt_addr = genimg_get_image(fdt_addr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001464
1465 /*
1466 * Check if there is an FDT image at the
1467 * address provided in the second bootm argument
1468 * check image type, for FIT images get a FIT node.
1469 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001470 switch (genimg_get_format((void *)fdt_addr)) {
Kumar Galaa5130ad2008-08-15 08:24:38 -05001471 case IMAGE_FORMAT_LEGACY:
1472 /* verify fdt_addr points to a valid image header */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001473 printf("## Flattened Device Tree from Legacy Image "
1474 "at %08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001475 fdt_addr);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001476 fdt_hdr = image_get_fdt(fdt_addr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001477 if (!fdt_hdr)
1478 goto error;
1479
1480 /*
1481 * move image data to the load address,
1482 * make sure we don't overwrite initial image
1483 */
1484 image_start = (ulong)fdt_hdr;
Stephen Warren84d62282011-11-01 06:28:21 +00001485 image_data = (ulong)image_get_data(fdt_hdr);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001486 image_end = image_get_image_end(fdt_hdr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001487
Stephen Warren6904b6e2011-10-18 11:11:49 +00001488 load_start = image_get_load(fdt_hdr);
1489 load_end = load_start + image_get_data_size(fdt_hdr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001490
Stephen Warren84d62282011-11-01 06:28:21 +00001491 if (load_start == image_start ||
1492 load_start == image_data) {
1493 fdt_blob = (char *)image_data;
1494 break;
1495 }
1496
Kumar Galaa5130ad2008-08-15 08:24:38 -05001497 if ((load_start < image_end) && (load_end > image_start)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001498 fdt_error("fdt overwritten");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001499 goto error;
1500 }
1501
Stephen Warren6904b6e2011-10-18 11:11:49 +00001502 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
Stephen Warren84d62282011-11-01 06:28:21 +00001503 image_data, load_start);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001504
Stephen Warren6904b6e2011-10-18 11:11:49 +00001505 memmove((void *)load_start,
Stephen Warren84d62282011-11-01 06:28:21 +00001506 (void *)image_data,
Stephen Warren6904b6e2011-10-18 11:11:49 +00001507 image_get_data_size(fdt_hdr));
Kumar Galaa5130ad2008-08-15 08:24:38 -05001508
1509 fdt_blob = (char *)load_start;
1510 break;
1511 case IMAGE_FORMAT_FIT:
1512 /*
1513 * This case will catch both: new uImage format
1514 * (libfdt based) and raw FDT blob (also libfdt
1515 * based).
1516 */
1517#if defined(CONFIG_FIT)
1518 /* check FDT blob vs FIT blob */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001519 if (fit_check_format((const void *)fdt_addr)) {
Kumar Galaa5130ad2008-08-15 08:24:38 -05001520 /*
1521 * FIT image
1522 */
1523 fit_hdr = (void *)fdt_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001524 printf("## Flattened Device Tree from FIT "
1525 "Image at %08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001526 fdt_addr);
1527
1528 if (!fit_uname_fdt) {
1529 /*
1530 * no FDT blob image node unit name,
1531 * try to get config node first. If
1532 * config unit node name is NULL
1533 * fit_conf_get_node() will try to
1534 * find default config node
1535 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001536 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001537 fit_uname_config);
1538
1539 if (cfg_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001540 fdt_error("Could not find "
1541 "configuration "
1542 "node\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001543 goto error;
1544 }
1545
Stephen Warren6904b6e2011-10-18 11:11:49 +00001546 fit_uname_config = fdt_get_name(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001547 cfg_noffset, NULL);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001548 printf(" Using '%s' configuration\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001549 fit_uname_config);
1550
Stephen Warren6904b6e2011-10-18 11:11:49 +00001551 fdt_noffset = fit_conf_get_fdt_node(
1552 fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001553 cfg_noffset);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001554 fit_uname_fdt = fit_get_name(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001555 fdt_noffset, NULL);
1556 } else {
1557 /* get FDT component image node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001558 fdt_noffset = fit_image_get_node(
1559 fit_hdr,
1560 fit_uname_fdt);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001561 }
1562 if (fdt_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001563 fdt_error("Could not find subimage "
1564 "node\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001565 goto error;
1566 }
1567
Stephen Warren6904b6e2011-10-18 11:11:49 +00001568 printf(" Trying '%s' FDT blob subimage\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001569 fit_uname_fdt);
1570
Stephen Warren6904b6e2011-10-18 11:11:49 +00001571 if (!fit_check_fdt(fit_hdr, fdt_noffset,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001572 images->verify))
1573 goto error;
1574
1575 /* get ramdisk image data address and length */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001576 if (fit_image_get_data(fit_hdr, fdt_noffset,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001577 &data, &size)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001578 fdt_error("Could not find FDT "
1579 "subimage data");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001580 goto error;
1581 }
1582
1583 /* verift that image data is a proper FDT blob */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001584 if (fdt_check_header((char *)data) != 0) {
1585 fdt_error("Subimage data is not a FTD");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001586 goto error;
1587 }
1588
1589 /*
1590 * move image data to the load address,
1591 * make sure we don't overwrite initial image
1592 */
1593 image_start = (ulong)fit_hdr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001594 image_end = fit_get_end(fit_hdr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001595
Stephen Warren6904b6e2011-10-18 11:11:49 +00001596 if (fit_image_get_load(fit_hdr, fdt_noffset,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001597 &load_start) == 0) {
1598 load_end = load_start + size;
1599
1600 if ((load_start < image_end) &&
1601 (load_end > image_start)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001602 fdt_error("FDT overwritten");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001603 goto error;
1604 }
1605
Stephen Warren6904b6e2011-10-18 11:11:49 +00001606 printf(" Loading FDT from 0x%08lx "
1607 "to 0x%08lx\n",
1608 (ulong)data,
1609 load_start);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001610
Stephen Warren6904b6e2011-10-18 11:11:49 +00001611 memmove((void *)load_start,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001612 (void *)data, size);
1613
1614 fdt_blob = (char *)load_start;
1615 } else {
1616 fdt_blob = (char *)data;
1617 }
1618
1619 images->fit_hdr_fdt = fit_hdr;
1620 images->fit_uname_fdt = fit_uname_fdt;
1621 images->fit_noffset_fdt = fdt_noffset;
1622 break;
1623 } else
1624#endif
1625 {
1626 /*
1627 * FDT blob
1628 */
1629 fdt_blob = (char *)fdt_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001630 debug("* fdt: raw FDT blob\n");
1631 printf("## Flattened Device Tree blob at "
1632 "%08lx\n", (long)fdt_blob);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001633 }
1634 break;
1635 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +00001636 puts("ERROR: Did not find a cmdline Flattened Device "
1637 "Tree\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001638 goto error;
1639 }
1640
Simon Glass00aa9882011-09-23 06:22:04 +00001641 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001642
1643 } else if (images->legacy_hdr_valid &&
Stephen Warren6904b6e2011-10-18 11:11:49 +00001644 image_check_type(&images->legacy_hdr_os_copy,
1645 IH_TYPE_MULTI)) {
Kumar Galaa5130ad2008-08-15 08:24:38 -05001646
1647 ulong fdt_data, fdt_len;
1648
1649 /*
1650 * Now check if we have a legacy multi-component image,
1651 * get second entry data start address and len.
1652 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001653 printf("## Flattened Device Tree from multi "
Kumar Galaa5130ad2008-08-15 08:24:38 -05001654 "component Image at %08lX\n",
1655 (ulong)images->legacy_hdr_os);
1656
Stephen Warren6904b6e2011-10-18 11:11:49 +00001657 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1658 &fdt_len);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001659 if (fdt_len) {
1660
1661 fdt_blob = (char *)fdt_data;
Simon Glass00aa9882011-09-23 06:22:04 +00001662 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001663
Stephen Warren6904b6e2011-10-18 11:11:49 +00001664 if (fdt_check_header(fdt_blob) != 0) {
1665 fdt_error("image is not a fdt");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001666 goto error;
1667 }
1668
John Rigbyb763f932010-10-13 13:57:32 -06001669 if (fdt_totalsize(fdt_blob) != fdt_len) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001670 fdt_error("fdt size != image size");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001671 goto error;
1672 }
1673 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001674 debug("## No Flattened Device Tree\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001675 return 0;
1676 }
1677 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001678 debug("## No Flattened Device Tree\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001679 return 0;
1680 }
1681
1682 *of_flat_tree = fdt_blob;
John Rigbyb763f932010-10-13 13:57:32 -06001683 *of_size = fdt_totalsize(fdt_blob);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001684 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner7ddfafc2008-08-21 07:12:26 -07001685 (ulong)*of_flat_tree, *of_size);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001686
1687 return 0;
1688
1689error:
1690 *of_flat_tree = 0;
1691 *of_size = 0;
1692 return 1;
1693}
1694#endif /* CONFIG_OF_LIBFDT */
1695
John Rigbyeea8e692010-10-13 13:57:35 -06001696#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001697/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001698 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galab937bb72008-02-27 21:51:49 -06001699 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001700 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1701 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1702 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001703 * boot_get_cmdline() allocates space for kernel command line below
Grant Likelydfff7a22011-03-28 09:58:34 +00001704 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001705 * variable is present its contents is copied to allocated kernel
1706 * command line.
1707 *
1708 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001709 * 0 - success
1710 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001711 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001712int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001713{
1714 char *cmdline;
1715 char *s;
1716
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001717 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likely26396382011-03-28 09:58:43 +00001718 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galab937bb72008-02-27 21:51:49 -06001719
1720 if (cmdline == NULL)
1721 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001722
1723 if ((s = getenv("bootargs")) == NULL)
1724 s = "";
1725
1726 strcpy(cmdline, s);
1727
1728 *cmd_start = (ulong) & cmdline[0];
1729 *cmd_end = *cmd_start + strlen(cmdline);
1730
Stephen Warren6904b6e2011-10-18 11:11:49 +00001731 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001732
Kumar Galab937bb72008-02-27 21:51:49 -06001733 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001734}
John Rigbyeea8e692010-10-13 13:57:35 -06001735#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001736
John Rigbyeea8e692010-10-13 13:57:35 -06001737#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001738/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001739 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galab937bb72008-02-27 21:51:49 -06001740 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001741 * @kbd: double pointer to board info data
1742 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001743 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likelydfff7a22011-03-28 09:58:34 +00001744 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1745 * with the current u-boot board info data.
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001746 *
1747 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001748 * 0 - success
1749 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001750 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001751int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001752{
Becky Bruced26d67c2008-06-09 20:37:18 -05001753 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likely26396382011-03-28 09:58:43 +00001754 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galab937bb72008-02-27 21:51:49 -06001755 if (*kbd == NULL)
1756 return -1;
1757
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001758 **kbd = *(gd->bd);
1759
Stephen Warren6904b6e2011-10-18 11:11:49 +00001760 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001761
1762#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1763 do_bdinfo(NULL, 0, 0, NULL);
1764#endif
1765
Kumar Galab937bb72008-02-27 21:51:49 -06001766 return 0;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001767}
John Rigbyeea8e692010-10-13 13:57:35 -06001768#endif /* CONFIG_SYS_BOOT_GET_KBD */
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001769#endif /* !USE_HOSTCC */
Marian Balakowicz95418502008-01-31 13:55:39 +01001770
Marian Balakowicz3413e222008-02-21 17:20:20 +01001771#if defined(CONFIG_FIT)
1772/*****************************************************************************/
1773/* New uImage format routines */
1774/*****************************************************************************/
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001775#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +00001776static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
Marian Balakowicz3413e222008-02-21 17:20:20 +01001777 ulong *addr, const char **name)
1778{
1779 const char *sep;
1780
1781 *addr = addr_curr;
1782 *name = NULL;
1783
Stephen Warren6904b6e2011-10-18 11:11:49 +00001784 sep = strchr(spec, sepc);
Marian Balakowicz3413e222008-02-21 17:20:20 +01001785 if (sep) {
1786 if (sep - spec > 0)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001787 *addr = simple_strtoul(spec, NULL, 16);
Marian Balakowicz3413e222008-02-21 17:20:20 +01001788
1789 *name = sep + 1;
1790 return 1;
1791 }
1792
1793 return 0;
1794}
1795
1796/**
1797 * fit_parse_conf - parse FIT configuration spec
1798 * @spec: input string, containing configuration spec
1799 * @add_curr: current image address (to be used as a possible default)
1800 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1801 * configuration
1802 * @conf_name double pointer to a char, will hold pointer to a configuration
1803 * unit name
1804 *
1805 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1806 * where <addr> is a FIT image address that contains configuration
1807 * with a <conf> unit name.
1808 *
1809 * Address part is optional, and if omitted default add_curr will
1810 * be used instead.
1811 *
1812 * returns:
1813 * 1 if spec is a valid configuration string,
1814 * addr and conf_name are set accordingly
1815 * 0 otherwise
1816 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001817inline int fit_parse_conf(const char *spec, ulong addr_curr,
Marian Balakowicz3413e222008-02-21 17:20:20 +01001818 ulong *addr, const char **conf_name)
1819{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001820 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
Marian Balakowicz3413e222008-02-21 17:20:20 +01001821}
1822
1823/**
1824 * fit_parse_subimage - parse FIT subimage spec
1825 * @spec: input string, containing subimage spec
1826 * @add_curr: current image address (to be used as a possible default)
1827 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1828 * subimage
1829 * @image_name: double pointer to a char, will hold pointer to a subimage name
1830 *
1831 * fit_parse_subimage() expects subimage spec in the for of
1832 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1833 * subimage with a <subimg> unit name.
1834 *
1835 * Address part is optional, and if omitted default add_curr will
1836 * be used instead.
1837 *
1838 * returns:
1839 * 1 if spec is a valid subimage string,
1840 * addr and image_name are set accordingly
1841 * 0 otherwise
1842 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001843inline int fit_parse_subimage(const char *spec, ulong addr_curr,
Marian Balakowicz3413e222008-02-21 17:20:20 +01001844 ulong *addr, const char **image_name)
1845{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001846 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
Marian Balakowicz3413e222008-02-21 17:20:20 +01001847}
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001848#endif /* !USE_HOSTCC */
1849
Stephen Warren6904b6e2011-10-18 11:11:49 +00001850static void fit_get_debug(const void *fit, int noffset,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001851 char *prop_name, int err)
1852{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001853 debug("Can't get '%s' property from FIT 0x%08lx, "
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001854 "node: offset %d, name %s (%s)\n",
1855 prop_name, (ulong)fit, noffset,
Stephen Warren6904b6e2011-10-18 11:11:49 +00001856 fit_get_name(fit, noffset, NULL),
1857 fdt_strerror(err));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001858}
1859
1860/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001861 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001862 * @fit: pointer to the FIT format image header
1863 * @p: pointer to prefix string
1864 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001865 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001866 * The routine prints out FIT image properties (root node level) follwed by
1867 * the details of each component image.
1868 *
1869 * returns:
1870 * no returned results
1871 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001872void fit_print_contents(const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001873{
1874 char *desc;
1875 char *uname;
1876 int images_noffset;
1877 int confs_noffset;
1878 int noffset;
1879 int ndepth;
1880 int count = 0;
1881 int ret;
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001882 const char *p;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001883#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1884 time_t timestamp;
1885#endif
1886
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001887#ifdef USE_HOSTCC
1888 p = "";
1889#else
1890 p = " ";
1891#endif
1892
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001893 /* Root node properties */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001894 ret = fit_get_desc(fit, 0, &desc);
1895 printf("%sFIT description: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001896 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001897 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001898 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00001899 printf("%s\n", desc);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001900
1901#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001902 ret = fit_get_timestamp(fit, 0, &timestamp);
1903 printf("%sCreated: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001904 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001905 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001906 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00001907 genimg_print_time(timestamp);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001908#endif
1909
1910 /* Find images parent node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001911 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001912 if (images_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001913 printf("Can't find images parent node '%s' (%s)\n",
1914 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001915 return;
1916 }
1917
1918 /* Process its subnodes, print out component images details */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001919 for (ndepth = 0, count = 0,
1920 noffset = fdt_next_node(fit, images_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001921 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001922 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001923 if (ndepth == 1) {
1924 /*
1925 * Direct child node of the images parent node,
1926 * i.e. component image node.
1927 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001928 printf("%s Image %u (%s)\n", p, count++,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001929 fit_get_name(fit, noffset, NULL));
1930
Stephen Warren6904b6e2011-10-18 11:11:49 +00001931 fit_image_print(fit, noffset, p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001932 }
1933 }
1934
1935 /* Find configurations parent node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001936 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001937 if (confs_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001938 debug("Can't get configurations parent node '%s' (%s)\n",
1939 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001940 return;
1941 }
1942
1943 /* get default configuration unit name from default property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001944 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001945 if (uname)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001946 printf("%s Default Configuration: '%s'\n", p, uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001947
1948 /* Process its subnodes, print out configurations details */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001949 for (ndepth = 0, count = 0,
1950 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001951 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001952 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001953 if (ndepth == 1) {
1954 /*
1955 * Direct child node of the configurations parent node,
1956 * i.e. configuration node.
1957 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001958 printf("%s Configuration %u (%s)\n", p, count++,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001959 fit_get_name(fit, noffset, NULL));
1960
Stephen Warren6904b6e2011-10-18 11:11:49 +00001961 fit_conf_print(fit, noffset, p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001962 }
1963 }
1964}
1965
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001966/**
1967 * fit_image_print - prints out the FIT component image details
1968 * @fit: pointer to the FIT format image header
1969 * @image_noffset: offset of the component image node
1970 * @p: pointer to prefix string
1971 *
1972 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001973 * image. If present, hash nodes are printed out as well. Load
1974 * address for images of type firmware is also printed out. Since the load
1975 * address is not mandatory for firmware images, it will be output as
1976 * "unavailable" when not present.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001977 *
1978 * returns:
1979 * no returned results
1980 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001981void fit_image_print(const void *fit, int image_noffset, const char *p)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001982{
1983 char *desc;
1984 uint8_t type, arch, os, comp;
1985 size_t size;
1986 ulong load, entry;
1987 const void *data;
1988 int noffset;
1989 int ndepth;
1990 int ret;
1991
1992 /* Mandatory properties */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001993 ret = fit_get_desc(fit, image_noffset, &desc);
1994 printf("%s Description: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001995 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001996 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001997 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00001998 printf("%s\n", desc);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001999
Stephen Warren6904b6e2011-10-18 11:11:49 +00002000 fit_image_get_type(fit, image_noffset, &type);
2001 printf("%s Type: %s\n", p, genimg_get_type_name(type));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002002
Stephen Warren6904b6e2011-10-18 11:11:49 +00002003 fit_image_get_comp(fit, image_noffset, &comp);
2004 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002005
Stephen Warren6904b6e2011-10-18 11:11:49 +00002006 ret = fit_image_get_data(fit, image_noffset, &data, &size);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002007
2008#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +00002009 printf("%s Data Start: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002010 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002011 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002012 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00002013 printf("0x%08lx\n", (ulong)data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002014#endif
2015
Stephen Warren6904b6e2011-10-18 11:11:49 +00002016 printf("%s Data Size: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002017 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002018 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002019 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00002020 genimg_print_size(size);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002021
2022 /* Remaining, type dependent properties */
2023 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2024 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2025 (type == IH_TYPE_FLATDT)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002026 fit_image_get_arch(fit, image_noffset, &arch);
2027 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002028 }
2029
2030 if (type == IH_TYPE_KERNEL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002031 fit_image_get_os(fit, image_noffset, &os);
2032 printf("%s OS: %s\n", p, genimg_get_os_name(os));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002033 }
2034
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02002035 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2036 (type == IH_TYPE_FIRMWARE)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002037 ret = fit_image_get_load(fit, image_noffset, &load);
2038 printf("%s Load Address: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002039 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002040 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002041 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00002042 printf("0x%08lx\n", load);
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02002043 }
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002044
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02002045 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002046 fit_image_get_entry(fit, image_noffset, &entry);
2047 printf("%s Entry Point: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002048 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002049 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002050 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00002051 printf("0x%08lx\n", entry);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002052 }
2053
2054 /* Process all hash subnodes of the component image node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002055 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002056 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002057 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002058 if (ndepth == 1) {
2059 /* Direct child node of the component image node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002060 fit_image_print_hash(fit, noffset, p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002061 }
2062 }
2063}
2064
2065/**
2066 * fit_image_print_hash - prints out the hash node details
2067 * @fit: pointer to the FIT format image header
2068 * @noffset: offset of the hash node
2069 * @p: pointer to prefix string
2070 *
2071 * fit_image_print_hash() lists properies for the processed hash node
2072 *
2073 * returns:
2074 * no returned results
2075 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002076void fit_image_print_hash(const void *fit, int noffset, const char *p)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002077{
2078 char *algo;
2079 uint8_t *value;
2080 int value_len;
2081 int i, ret;
2082
2083 /*
2084 * Check subnode name, must be equal to "hash".
2085 * Multiple hash nodes require unique unit node
2086 * names, e.g. hash@1, hash@2, etc.
2087 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002088 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002089 FIT_HASH_NODENAME,
2090 strlen(FIT_HASH_NODENAME)) != 0)
2091 return;
2092
Stephen Warren6904b6e2011-10-18 11:11:49 +00002093 debug("%s Hash node: '%s'\n", p,
2094 fit_get_name(fit, noffset, NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002095
Stephen Warren6904b6e2011-10-18 11:11:49 +00002096 printf("%s Hash algo: ", p);
2097 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2098 printf("invalid/unsupported\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002099 return;
2100 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00002101 printf("%s\n", algo);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002102
Stephen Warren6904b6e2011-10-18 11:11:49 +00002103 ret = fit_image_hash_get_value(fit, noffset, &value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002104 &value_len);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002105 printf("%s Hash value: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002106 if (ret) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002107 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002108 } else {
2109 for (i = 0; i < value_len; i++)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002110 printf("%02x", value[i]);
2111 printf("\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002112 }
2113
Stephen Warren6904b6e2011-10-18 11:11:49 +00002114 debug("%s Hash len: %d\n", p, value_len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002115}
2116
2117/**
2118 * fit_get_desc - get node description property
2119 * @fit: pointer to the FIT format image header
2120 * @noffset: node offset
2121 * @desc: double pointer to the char, will hold pointer to the descrption
2122 *
2123 * fit_get_desc() reads description property from a given node, if
2124 * description is found pointer to it is returened in third call argument.
2125 *
2126 * returns:
2127 * 0, on success
2128 * -1, on failure
2129 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002130int fit_get_desc(const void *fit, int noffset, char **desc)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002131{
2132 int len;
2133
Stephen Warren6904b6e2011-10-18 11:11:49 +00002134 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002135 if (*desc == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002136 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002137 return -1;
2138 }
2139
2140 return 0;
2141}
2142
2143/**
2144 * fit_get_timestamp - get node timestamp property
2145 * @fit: pointer to the FIT format image header
2146 * @noffset: node offset
2147 * @timestamp: pointer to the time_t, will hold read timestamp
2148 *
2149 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2150 * is found and has a correct size its value is retured in third call
2151 * argument.
2152 *
2153 * returns:
2154 * 0, on success
2155 * -1, on property read failure
2156 * -2, on wrong timestamp size
2157 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002158int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002159{
2160 int len;
2161 const void *data;
2162
Stephen Warren6904b6e2011-10-18 11:11:49 +00002163 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002164 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002165 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002166 return -1;
2167 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00002168 if (len != sizeof(uint32_t)) {
2169 debug("FIT timestamp with incorrect size of (%u)\n", len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002170 return -2;
2171 }
2172
Stephen Warren6904b6e2011-10-18 11:11:49 +00002173 *timestamp = uimage_to_cpu(*((uint32_t *)data));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002174 return 0;
2175}
2176
2177/**
2178 * fit_image_get_node - get node offset for component image of a given unit name
2179 * @fit: pointer to the FIT format image header
2180 * @image_uname: component image node unit name
2181 *
2182 * fit_image_get_node() finds a component image (withing the '/images'
2183 * node) of a provided unit name. If image is found its node offset is
2184 * returned to the caller.
2185 *
2186 * returns:
2187 * image node offset when found (>=0)
2188 * negative number on failure (FDT_ERR_* code)
2189 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002190int fit_image_get_node(const void *fit, const char *image_uname)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002191{
2192 int noffset, images_noffset;
2193
Stephen Warren6904b6e2011-10-18 11:11:49 +00002194 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002195 if (images_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002196 debug("Can't find images parent node '%s' (%s)\n",
2197 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002198 return images_noffset;
2199 }
2200
Stephen Warren6904b6e2011-10-18 11:11:49 +00002201 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002202 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002203 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2204 image_uname, fdt_strerror(noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002205 }
2206
2207 return noffset;
2208}
2209
2210/**
2211 * fit_image_get_os - get os id for a given component image node
2212 * @fit: pointer to the FIT format image header
2213 * @noffset: component image node offset
2214 * @os: pointer to the uint8_t, will hold os numeric id
2215 *
2216 * fit_image_get_os() finds os property in a given component image node.
2217 * If the property is found, its (string) value is translated to the numeric
2218 * id which is returned to the caller.
2219 *
2220 * returns:
2221 * 0, on success
2222 * -1, on failure
2223 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002224int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002225{
2226 int len;
2227 const void *data;
2228
2229 /* Get OS name from property data */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002230 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002231 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002232 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002233 *os = -1;
2234 return -1;
2235 }
2236
2237 /* Translate OS name to id */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002238 *os = genimg_get_os_id(data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002239 return 0;
2240}
2241
2242/**
2243 * fit_image_get_arch - get arch id for a given component image node
2244 * @fit: pointer to the FIT format image header
2245 * @noffset: component image node offset
2246 * @arch: pointer to the uint8_t, will hold arch numeric id
2247 *
2248 * fit_image_get_arch() finds arch property in a given component image node.
2249 * If the property is found, its (string) value is translated to the numeric
2250 * id which is returned to the caller.
2251 *
2252 * returns:
2253 * 0, on success
2254 * -1, on failure
2255 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002256int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002257{
2258 int len;
2259 const void *data;
2260
2261 /* Get architecture name from property data */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002262 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002263 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002264 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002265 *arch = -1;
2266 return -1;
2267 }
2268
2269 /* Translate architecture name to id */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002270 *arch = genimg_get_arch_id(data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002271 return 0;
2272}
2273
2274/**
2275 * fit_image_get_type - get type id for a given component image node
2276 * @fit: pointer to the FIT format image header
2277 * @noffset: component image node offset
2278 * @type: pointer to the uint8_t, will hold type numeric id
2279 *
2280 * fit_image_get_type() finds type property in a given component image node.
2281 * If the property is found, its (string) value is translated to the numeric
2282 * id which is returned to the caller.
2283 *
2284 * returns:
2285 * 0, on success
2286 * -1, on failure
2287 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002288int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002289{
2290 int len;
2291 const void *data;
2292
2293 /* Get image type name from property data */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002294 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002295 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002296 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002297 *type = -1;
2298 return -1;
2299 }
2300
2301 /* Translate image type name to id */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002302 *type = genimg_get_type_id(data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002303 return 0;
2304}
2305
2306/**
2307 * fit_image_get_comp - get comp id for a given component image node
2308 * @fit: pointer to the FIT format image header
2309 * @noffset: component image node offset
2310 * @comp: pointer to the uint8_t, will hold comp numeric id
2311 *
2312 * fit_image_get_comp() finds comp property in a given component image node.
2313 * If the property is found, its (string) value is translated to the numeric
2314 * id which is returned to the caller.
2315 *
2316 * returns:
2317 * 0, on success
2318 * -1, on failure
2319 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002320int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002321{
2322 int len;
2323 const void *data;
2324
2325 /* Get compression name from property data */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002326 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002327 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002328 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002329 *comp = -1;
2330 return -1;
2331 }
2332
2333 /* Translate compression name to id */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002334 *comp = genimg_get_comp_id(data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002335 return 0;
2336}
2337
2338/**
2339 * fit_image_get_load - get load address property for a given component image node
2340 * @fit: pointer to the FIT format image header
2341 * @noffset: component image node offset
2342 * @load: pointer to the uint32_t, will hold load address
2343 *
2344 * fit_image_get_load() finds load address property in a given component image node.
2345 * If the property is found, its value is returned to the caller.
2346 *
2347 * returns:
2348 * 0, on success
2349 * -1, on failure
2350 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002351int fit_image_get_load(const void *fit, int noffset, ulong *load)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002352{
2353 int len;
2354 const uint32_t *data;
2355
Stephen Warren6904b6e2011-10-18 11:11:49 +00002356 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002357 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002358 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002359 return -1;
2360 }
2361
Stephen Warren6904b6e2011-10-18 11:11:49 +00002362 *load = uimage_to_cpu(*data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002363 return 0;
2364}
2365
2366/**
2367 * fit_image_get_entry - get entry point address property for a given component image node
2368 * @fit: pointer to the FIT format image header
2369 * @noffset: component image node offset
2370 * @entry: pointer to the uint32_t, will hold entry point address
2371 *
2372 * fit_image_get_entry() finds entry point address property in a given component image node.
2373 * If the property is found, its value is returned to the caller.
2374 *
2375 * returns:
2376 * 0, on success
2377 * -1, on failure
2378 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002379int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002380{
2381 int len;
2382 const uint32_t *data;
2383
Stephen Warren6904b6e2011-10-18 11:11:49 +00002384 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002385 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002386 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002387 return -1;
2388 }
2389
Stephen Warren6904b6e2011-10-18 11:11:49 +00002390 *entry = uimage_to_cpu(*data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002391 return 0;
2392}
2393
2394/**
2395 * fit_image_get_data - get data property and its size for a given component image node
2396 * @fit: pointer to the FIT format image header
2397 * @noffset: component image node offset
2398 * @data: double pointer to void, will hold data property's data address
2399 * @size: pointer to size_t, will hold data property's data size
2400 *
2401 * fit_image_get_data() finds data property in a given component image node.
2402 * If the property is found its data start address and size are returned to
2403 * the caller.
2404 *
2405 * returns:
2406 * 0, on success
2407 * -1, on failure
2408 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002409int fit_image_get_data(const void *fit, int noffset,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002410 const void **data, size_t *size)
2411{
2412 int len;
2413
Stephen Warren6904b6e2011-10-18 11:11:49 +00002414 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002415 if (*data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002416 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002417 *size = 0;
2418 return -1;
2419 }
2420
2421 *size = len;
2422 return 0;
2423}
2424
2425/**
2426 * fit_image_hash_get_algo - get hash algorithm name
2427 * @fit: pointer to the FIT format image header
2428 * @noffset: hash node offset
2429 * @algo: double pointer to char, will hold pointer to the algorithm name
2430 *
2431 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2432 * If the property is found its data start address is returned to the caller.
2433 *
2434 * returns:
2435 * 0, on success
2436 * -1, on failure
2437 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002438int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002439{
2440 int len;
2441
Stephen Warren6904b6e2011-10-18 11:11:49 +00002442 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002443 if (*algo == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002444 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002445 return -1;
2446 }
2447
2448 return 0;
2449}
2450
2451/**
2452 * fit_image_hash_get_value - get hash value and length
2453 * @fit: pointer to the FIT format image header
2454 * @noffset: hash node offset
2455 * @value: double pointer to uint8_t, will hold address of a hash value data
2456 * @value_len: pointer to an int, will hold hash data length
2457 *
2458 * fit_image_hash_get_value() finds hash value property in a given hash node.
2459 * If the property is found its data start address and size are returned to
2460 * the caller.
2461 *
2462 * returns:
2463 * 0, on success
2464 * -1, on failure
2465 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002466int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002467 int *value_len)
2468{
2469 int len;
2470
Stephen Warren6904b6e2011-10-18 11:11:49 +00002471 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002472 if (*value == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002473 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002474 *value_len = 0;
2475 return -1;
2476 }
2477
2478 *value_len = len;
2479 return 0;
2480}
2481
2482/**
2483 * fit_set_timestamp - set node timestamp property
2484 * @fit: pointer to the FIT format image header
2485 * @noffset: node offset
2486 * @timestamp: timestamp value to be set
2487 *
2488 * fit_set_timestamp() attempts to set timestamp property in the requested
2489 * node and returns operation status to the caller.
2490 *
2491 * returns:
2492 * 0, on success
2493 * -1, on property read failure
2494 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002495int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002496{
2497 uint32_t t;
2498 int ret;
2499
Stephen Warren6904b6e2011-10-18 11:11:49 +00002500 t = cpu_to_uimage(timestamp);
2501 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2502 sizeof(uint32_t));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002503 if (ret) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002504 printf("Can't set '%s' property for '%s' node (%s)\n",
2505 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2506 fdt_strerror(ret));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002507 return -1;
2508 }
2509
2510 return 0;
2511}
2512
2513/**
2514 * calculate_hash - calculate and return hash for provided input data
2515 * @data: pointer to the input data
2516 * @data_len: data length
2517 * @algo: requested hash algorithm
2518 * @value: pointer to the char, will hold hash value data (caller must
2519 * allocate enough free space)
2520 * value_len: length of the calculated hash
2521 *
2522 * calculate_hash() computes input data hash according to the requested algorithm.
2523 * Resulting hash value is placed in caller provided 'value' buffer, length
2524 * of the calculated hash is returned via value_len pointer argument.
2525 *
2526 * returns:
2527 * 0, on success
2528 * -1, when algo is unsupported
2529 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002530static int calculate_hash(const void *data, int data_len, const char *algo,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002531 uint8_t *value, int *value_len)
2532{
Stephen Warren6904b6e2011-10-18 11:11:49 +00002533 if (strcmp(algo, "crc32") == 0) {
2534 *((uint32_t *)value) = crc32_wd(0, data, data_len,
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002535 CHUNKSZ_CRC32);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002536 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002537 *value_len = 4;
Stephen Warren6904b6e2011-10-18 11:11:49 +00002538 } else if (strcmp(algo, "sha1") == 0) {
2539 sha1_csum_wd((unsigned char *) data, data_len,
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002540 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002541 *value_len = 20;
Stephen Warren6904b6e2011-10-18 11:11:49 +00002542 } else if (strcmp(algo, "md5") == 0) {
2543 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka2e6f2852008-03-14 16:22:34 +01002544 *value_len = 16;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002545 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002546 debug("Unsupported hash alogrithm\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002547 return -1;
2548 }
2549 return 0;
2550}
2551
2552#ifdef USE_HOSTCC
2553/**
2554 * fit_set_hashes - process FIT component image nodes and calculate hashes
2555 * @fit: pointer to the FIT format image header
2556 *
2557 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2558 * Hashes are calculated for all component images which have hash subnodes
2559 * with algorithm property set to one of the supported hash algorithms.
2560 *
2561 * returns
2562 * 0, on success
2563 * libfdt error code, on failure
2564 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002565int fit_set_hashes(void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002566{
2567 int images_noffset;
2568 int noffset;
2569 int ndepth;
2570 int ret;
2571
2572 /* Find images parent node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002573 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002574 if (images_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002575 printf("Can't find images parent node '%s' (%s)\n",
2576 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002577 return images_noffset;
2578 }
2579
2580 /* Process its subnodes, print out component images details */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002581 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002582 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002583 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002584 if (ndepth == 1) {
2585 /*
2586 * Direct child node of the images parent node,
2587 * i.e. component image node.
2588 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002589 ret = fit_image_set_hashes(fit, noffset);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002590 if (ret)
2591 return ret;
2592 }
2593 }
2594
2595 return 0;
2596}
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01002597
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002598/**
2599 * fit_image_set_hashes - calculate/set hashes for given component image node
2600 * @fit: pointer to the FIT format image header
2601 * @image_noffset: requested component image node
2602 *
2603 * fit_image_set_hashes() adds hash values for an component image node. All
2604 * existing hash subnodes are checked, if algorithm property is set to one of
2605 * the supported hash algorithms, hash value is computed and corresponding
2606 * hash node property is set, for example:
2607 *
2608 * Input component image node structure:
2609 *
2610 * o image@1 (at image_noffset)
2611 * | - data = [binary data]
2612 * o hash@1
2613 * |- algo = "sha1"
2614 *
2615 * Output component image node structure:
2616 *
2617 * o image@1 (at image_noffset)
2618 * | - data = [binary data]
2619 * o hash@1
2620 * |- algo = "sha1"
2621 * |- value = sha1(data)
2622 *
2623 * returns:
2624 * 0 on sucess
2625 * <0 on failure
2626 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002627int fit_image_set_hashes(void *fit, int image_noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002628{
2629 const void *data;
2630 size_t size;
2631 char *algo;
2632 uint8_t value[FIT_MAX_HASH_LEN];
2633 int value_len;
2634 int noffset;
2635 int ndepth;
2636
2637 /* Get image data and data length */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002638 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2639 printf("Can't get image data/size\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002640 return -1;
2641 }
2642
2643 /* Process all hash subnodes of the component image node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002644 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002645 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002646 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002647 if (ndepth == 1) {
2648 /* Direct child node of the component image node */
2649
2650 /*
2651 * Check subnode name, must be equal to "hash".
2652 * Multiple hash nodes require unique unit node
2653 * names, e.g. hash@1, hash@2, etc.
2654 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002655 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002656 FIT_HASH_NODENAME,
2657 strlen(FIT_HASH_NODENAME)) != 0) {
2658 /* Not a hash subnode, skip it */
2659 continue;
2660 }
2661
Stephen Warren6904b6e2011-10-18 11:11:49 +00002662 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2663 printf("Can't get hash algo property for "
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002664 "'%s' hash node in '%s' image node\n",
Stephen Warren6904b6e2011-10-18 11:11:49 +00002665 fit_get_name(fit, noffset, NULL),
2666 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002667 return -1;
2668 }
2669
Stephen Warren6904b6e2011-10-18 11:11:49 +00002670 if (calculate_hash(data, size, algo, value,
2671 &value_len)) {
2672 printf("Unsupported hash algorithm (%s) for "
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002673 "'%s' hash node in '%s' image node\n",
Stephen Warren6904b6e2011-10-18 11:11:49 +00002674 algo, fit_get_name(fit, noffset, NULL),
2675 fit_get_name(fit, image_noffset,
2676 NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002677 return -1;
2678 }
2679
Stephen Warren6904b6e2011-10-18 11:11:49 +00002680 if (fit_image_hash_set_value(fit, noffset, value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002681 value_len)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002682 printf("Can't set hash value for "
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002683 "'%s' hash node in '%s' image node\n",
Stephen Warren6904b6e2011-10-18 11:11:49 +00002684 fit_get_name(fit, noffset, NULL),
2685 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002686 return -1;
2687 }
2688 }
2689 }
2690
2691 return 0;
2692}
2693
2694/**
2695 * fit_image_hash_set_value - set hash value in requested has node
2696 * @fit: pointer to the FIT format image header
2697 * @noffset: hash node offset
2698 * @value: hash value to be set
2699 * @value_len: hash value length
2700 *
2701 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2702 * given and returns operation status to the caller.
2703 *
2704 * returns
2705 * 0, on success
2706 * -1, on failure
2707 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002708int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002709 int value_len)
2710{
2711 int ret;
2712
Stephen Warren6904b6e2011-10-18 11:11:49 +00002713 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002714 if (ret) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002715 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2716 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2717 fdt_strerror(ret));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002718 return -1;
2719 }
2720
2721 return 0;
2722}
2723#endif /* USE_HOSTCC */
2724
2725/**
2726 * fit_image_check_hashes - verify data intergity
2727 * @fit: pointer to the FIT format image header
2728 * @image_noffset: component image node offset
2729 *
2730 * fit_image_check_hashes() goes over component image hash nodes,
2731 * re-calculates each data hash and compares with the value stored in hash
2732 * node.
2733 *
2734 * returns:
2735 * 1, if all hashes are valid
2736 * 0, otherwise (or on error)
2737 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002738int fit_image_check_hashes(const void *fit, int image_noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002739{
2740 const void *data;
2741 size_t size;
2742 char *algo;
2743 uint8_t *fit_value;
2744 int fit_value_len;
2745 uint8_t value[FIT_MAX_HASH_LEN];
2746 int value_len;
2747 int noffset;
2748 int ndepth;
2749 char *err_msg = "";
2750
2751 /* Get image data and data length */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002752 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2753 printf("Can't get image data/size\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002754 return 0;
2755 }
2756
2757 /* Process all hash subnodes of the component image node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002758 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002759 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002760 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002761 if (ndepth == 1) {
2762 /* Direct child node of the component image node */
2763
2764 /*
2765 * Check subnode name, must be equal to "hash".
2766 * Multiple hash nodes require unique unit node
2767 * names, e.g. hash@1, hash@2, etc.
2768 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002769 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002770 FIT_HASH_NODENAME,
2771 strlen(FIT_HASH_NODENAME)) != 0)
2772 continue;
2773
Stephen Warren6904b6e2011-10-18 11:11:49 +00002774 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002775 err_msg = " error!\nCan't get hash algo "
2776 "property";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002777 goto error;
2778 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00002779 printf("%s", algo);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002780
Stephen Warren6904b6e2011-10-18 11:11:49 +00002781 if (fit_image_hash_get_value(fit, noffset, &fit_value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002782 &fit_value_len)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002783 err_msg = " error!\nCan't get hash value "
2784 "property";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002785 goto error;
2786 }
2787
Stephen Warren6904b6e2011-10-18 11:11:49 +00002788 if (calculate_hash(data, size, algo, value,
2789 &value_len)) {
2790 err_msg = " error!\n"
2791 "Unsupported hash algorithm";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002792 goto error;
2793 }
2794
2795 if (value_len != fit_value_len) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002796 err_msg = " error !\nBad hash value len";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002797 goto error;
Stephen Warren6904b6e2011-10-18 11:11:49 +00002798 } else if (memcmp(value, fit_value, value_len) != 0) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002799 err_msg = " error!\nBad hash value";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002800 goto error;
2801 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00002802 printf("+ ");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002803 }
2804 }
2805
2806 return 1;
2807
2808error:
Stephen Warren6904b6e2011-10-18 11:11:49 +00002809 printf("%s for '%s' hash node in '%s' image node\n",
2810 err_msg, fit_get_name(fit, noffset, NULL),
2811 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002812 return 0;
2813}
2814
2815/**
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002816 * fit_all_image_check_hashes - verify data intergity for all images
2817 * @fit: pointer to the FIT format image header
2818 *
2819 * fit_all_image_check_hashes() goes over all images in the FIT and
2820 * for every images checks if all it's hashes are valid.
2821 *
2822 * returns:
2823 * 1, if all hashes of all images are valid
2824 * 0, otherwise (or on error)
2825 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002826int fit_all_image_check_hashes(const void *fit)
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002827{
2828 int images_noffset;
2829 int noffset;
2830 int ndepth;
2831 int count;
2832
2833 /* Find images parent node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002834 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002835 if (images_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002836 printf("Can't find images parent node '%s' (%s)\n",
2837 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002838 return 0;
2839 }
2840
2841 /* Process all image subnodes, check hashes for each */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002842 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002843 (ulong)fit);
2844 for (ndepth = 0, count = 0,
Stephen Warren6904b6e2011-10-18 11:11:49 +00002845 noffset = fdt_next_node(fit, images_noffset, &ndepth);
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002846 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002847 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002848 if (ndepth == 1) {
2849 /*
2850 * Direct child node of the images parent node,
2851 * i.e. component image node.
2852 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002853 printf(" Hash(es) for Image %u (%s): ", count++,
2854 fit_get_name(fit, noffset, NULL));
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002855
Stephen Warren6904b6e2011-10-18 11:11:49 +00002856 if (!fit_image_check_hashes(fit, noffset))
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002857 return 0;
Stephen Warren6904b6e2011-10-18 11:11:49 +00002858 printf("\n");
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002859 }
2860 }
2861 return 1;
2862}
2863
2864/**
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002865 * fit_image_check_os - check whether image node is of a given os type
2866 * @fit: pointer to the FIT format image header
2867 * @noffset: component image node offset
2868 * @os: requested image os
2869 *
2870 * fit_image_check_os() reads image os property and compares its numeric
2871 * id with the requested os. Comparison result is returned to the caller.
2872 *
2873 * returns:
2874 * 1 if image is of given os type
2875 * 0 otherwise (or on error)
2876 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002877int fit_image_check_os(const void *fit, int noffset, uint8_t os)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002878{
2879 uint8_t image_os;
2880
Stephen Warren6904b6e2011-10-18 11:11:49 +00002881 if (fit_image_get_os(fit, noffset, &image_os))
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002882 return 0;
2883 return (os == image_os);
2884}
2885
2886/**
2887 * fit_image_check_arch - check whether image node is of a given arch
2888 * @fit: pointer to the FIT format image header
2889 * @noffset: component image node offset
2890 * @arch: requested imagearch
2891 *
2892 * fit_image_check_arch() reads image arch property and compares its numeric
2893 * id with the requested arch. Comparison result is returned to the caller.
2894 *
2895 * returns:
2896 * 1 if image is of given arch
2897 * 0 otherwise (or on error)
2898 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002899int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002900{
2901 uint8_t image_arch;
2902
Stephen Warren6904b6e2011-10-18 11:11:49 +00002903 if (fit_image_get_arch(fit, noffset, &image_arch))
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002904 return 0;
2905 return (arch == image_arch);
2906}
2907
2908/**
2909 * fit_image_check_type - check whether image node is of a given type
2910 * @fit: pointer to the FIT format image header
2911 * @noffset: component image node offset
2912 * @type: requested image type
2913 *
2914 * fit_image_check_type() reads image type property and compares its numeric
2915 * id with the requested type. Comparison result is returned to the caller.
2916 *
2917 * returns:
2918 * 1 if image is of given type
2919 * 0 otherwise (or on error)
2920 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002921int fit_image_check_type(const void *fit, int noffset, uint8_t type)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002922{
2923 uint8_t image_type;
2924
Stephen Warren6904b6e2011-10-18 11:11:49 +00002925 if (fit_image_get_type(fit, noffset, &image_type))
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002926 return 0;
2927 return (type == image_type);
2928}
2929
2930/**
2931 * fit_image_check_comp - check whether image node uses given compression
2932 * @fit: pointer to the FIT format image header
2933 * @noffset: component image node offset
2934 * @comp: requested image compression type
2935 *
2936 * fit_image_check_comp() reads image compression property and compares its
2937 * numeric id with the requested compression type. Comparison result is
2938 * returned to the caller.
2939 *
2940 * returns:
2941 * 1 if image uses requested compression
2942 * 0 otherwise (or on error)
2943 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002944int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002945{
2946 uint8_t image_comp;
2947
Stephen Warren6904b6e2011-10-18 11:11:49 +00002948 if (fit_image_get_comp(fit, noffset, &image_comp))
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002949 return 0;
2950 return (comp == image_comp);
2951}
2952
2953/**
2954 * fit_check_format - sanity check FIT image format
2955 * @fit: pointer to the FIT format image header
2956 *
2957 * fit_check_format() runs a basic sanity FIT image verification.
2958 * Routine checks for mandatory properties, nodes, etc.
2959 *
2960 * returns:
2961 * 1, on success
2962 * 0, on failure
2963 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002964int fit_check_format(const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002965{
2966 /* mandatory / node 'description' property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002967 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2968 debug("Wrong FIT format: no description\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002969 return 0;
2970 }
2971
2972#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2973 /* mandatory / node 'timestamp' property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002974 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2975 debug("Wrong FIT format: no timestamp\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002976 return 0;
2977 }
2978#endif
2979
2980 /* mandatory subimages parent '/images' node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002981 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
2982 debug("Wrong FIT format: no images parent node\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002983 return 0;
2984 }
2985
2986 return 1;
2987}
2988
2989/**
2990 * fit_conf_get_node - get node offset for configuration of a given unit name
2991 * @fit: pointer to the FIT format image header
2992 * @conf_uname: configuration node unit name
2993 *
2994 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2995 * parant node) of a provided unit name. If configuration is found its node offset
2996 * is returned to the caller.
2997 *
2998 * When NULL is provided in second argument fit_conf_get_node() will search
2999 * for a default configuration node instead. Default configuration node unit name
3000 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
3001 *
3002 * returns:
3003 * configuration node offset when found (>=0)
3004 * negative number on failure (FDT_ERR_* code)
3005 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003006int fit_conf_get_node(const void *fit, const char *conf_uname)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003007{
3008 int noffset, confs_noffset;
3009 int len;
3010
Stephen Warren6904b6e2011-10-18 11:11:49 +00003011 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003012 if (confs_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00003013 debug("Can't find configurations parent node '%s' (%s)\n",
3014 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003015 return confs_noffset;
3016 }
3017
3018 if (conf_uname == NULL) {
3019 /* get configuration unit name from the default property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003020 debug("No configuration specified, trying default...\n");
3021 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3022 FIT_DEFAULT_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003023 if (conf_uname == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00003024 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3025 len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003026 return len;
3027 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00003028 debug("Found default configuration: '%s'\n", conf_uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003029 }
3030
Stephen Warren6904b6e2011-10-18 11:11:49 +00003031 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003032 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00003033 debug("Can't get node offset for configuration unit name: "
3034 "'%s' (%s)\n",
3035 conf_uname, fdt_strerror(noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003036 }
3037
3038 return noffset;
3039}
3040
Stephen Warren6904b6e2011-10-18 11:11:49 +00003041static int __fit_conf_get_prop_node(const void *fit, int noffset,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003042 const char *prop_name)
3043{
3044 char *uname;
3045 int len;
3046
3047 /* get kernel image unit name from configuration kernel property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003048 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003049 if (uname == NULL)
3050 return len;
3051
Stephen Warren6904b6e2011-10-18 11:11:49 +00003052 return fit_image_get_node(fit, uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003053}
3054
3055/**
3056 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3057 * a given configuration
3058 * @fit: pointer to the FIT format image header
3059 * @noffset: configuration node offset
3060 *
3061 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3062 * configuration FIT_KERNEL_PROP property and translates it to the node
3063 * offset.
3064 *
3065 * returns:
3066 * image node offset when found (>=0)
3067 * negative number on failure (FDT_ERR_* code)
3068 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003069int fit_conf_get_kernel_node(const void *fit, int noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003070{
Stephen Warren6904b6e2011-10-18 11:11:49 +00003071 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003072}
3073
3074/**
3075 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3076 * a given configuration
3077 * @fit: pointer to the FIT format image header
3078 * @noffset: configuration node offset
3079 *
3080 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3081 * configuration FIT_KERNEL_PROP property and translates it to the node
3082 * offset.
3083 *
3084 * returns:
3085 * image node offset when found (>=0)
3086 * negative number on failure (FDT_ERR_* code)
3087 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003088int fit_conf_get_ramdisk_node(const void *fit, int noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003089{
Stephen Warren6904b6e2011-10-18 11:11:49 +00003090 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003091}
3092
3093/**
3094 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3095 * a given configuration
3096 * @fit: pointer to the FIT format image header
3097 * @noffset: configuration node offset
3098 *
3099 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3100 * configuration FIT_KERNEL_PROP property and translates it to the node
3101 * offset.
3102 *
3103 * returns:
3104 * image node offset when found (>=0)
3105 * negative number on failure (FDT_ERR_* code)
3106 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003107int fit_conf_get_fdt_node(const void *fit, int noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003108{
Stephen Warren6904b6e2011-10-18 11:11:49 +00003109 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003110}
3111
3112/**
3113 * fit_conf_print - prints out the FIT configuration details
3114 * @fit: pointer to the FIT format image header
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +01003115 * @noffset: offset of the configuration node
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003116 * @p: pointer to prefix string
3117 *
3118 * fit_conf_print() lists all mandatory properies for the processed
3119 * configuration node.
3120 *
3121 * returns:
3122 * no returned results
3123 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003124void fit_conf_print(const void *fit, int noffset, const char *p)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003125{
3126 char *desc;
3127 char *uname;
3128 int ret;
3129
3130 /* Mandatory properties */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003131 ret = fit_get_desc(fit, noffset, &desc);
3132 printf("%s Description: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003133 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00003134 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003135 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00003136 printf("%s\n", desc);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003137
Stephen Warren6904b6e2011-10-18 11:11:49 +00003138 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3139 printf("%s Kernel: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003140 if (uname == NULL)
Stephen Warren6904b6e2011-10-18 11:11:49 +00003141 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003142 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00003143 printf("%s\n", uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003144
3145 /* Optional properties */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003146 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003147 if (uname)
Stephen Warren6904b6e2011-10-18 11:11:49 +00003148 printf("%s Init Ramdisk: %s\n", p, uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003149
Stephen Warren6904b6e2011-10-18 11:11:49 +00003150 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003151 if (uname)
Stephen Warren6904b6e2011-10-18 11:11:49 +00003152 printf("%s FDT: %s\n", p, uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003153}
Marian Balakowicza50d5152008-03-12 10:12:37 +01003154
3155/**
3156 * fit_check_ramdisk - verify FIT format ramdisk subimage
3157 * @fit_hdr: pointer to the FIT ramdisk header
3158 * @rd_noffset: ramdisk subimage node offset within FIT image
3159 * @arch: requested ramdisk image architecture type
3160 * @verify: data CRC verification flag
3161 *
3162 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3163 * specified FIT image.
3164 *
3165 * returns:
3166 * 1, on success
3167 * 0, on failure
3168 */
3169#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +00003170static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3171 int verify)
Marian Balakowicza50d5152008-03-12 10:12:37 +01003172{
Stephen Warren6904b6e2011-10-18 11:11:49 +00003173 fit_image_print(fit, rd_noffset, " ");
Marian Balakowicza50d5152008-03-12 10:12:37 +01003174
3175 if (verify) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00003176 puts(" Verifying Hash Integrity ... ");
3177 if (!fit_image_check_hashes(fit, rd_noffset)) {
3178 puts("Bad Data Hash\n");
3179 show_boot_progress(-125);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003180 return 0;
3181 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00003182 puts("OK\n");
Marian Balakowicza50d5152008-03-12 10:12:37 +01003183 }
3184
Stephen Warren6904b6e2011-10-18 11:11:49 +00003185 show_boot_progress(126);
3186 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3187 !fit_image_check_arch(fit, rd_noffset, arch) ||
3188 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3189 printf("No Linux %s Ramdisk Image\n",
Marian Balakowicza50d5152008-03-12 10:12:37 +01003190 genimg_get_arch_name(arch));
Stephen Warren6904b6e2011-10-18 11:11:49 +00003191 show_boot_progress(-126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003192 return 0;
3193 }
3194
Stephen Warren6904b6e2011-10-18 11:11:49 +00003195 show_boot_progress(127);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003196 return 1;
3197}
3198#endif /* USE_HOSTCC */
Marian Balakowicz3413e222008-02-21 17:20:20 +01003199#endif /* CONFIG_FIT */