blob: 555d9d9d42ba01b89faafa94e83843433c298914 [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", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010097 { -1, "", "", },
98};
99
Mike Frysingercf2feb12010-10-20 07:17:39 -0400100static const table_entry_t uimage_os[] = {
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100101 { IH_OS_INVALID, NULL, "Invalid OS", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100102 { IH_OS_LINUX, "linux", "Linux", },
103#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
104 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
105#endif
106 { IH_OS_NETBSD, "netbsd", "NetBSD", },
Torkel Lundgren29a58662010-09-28 11:05:36 +0200107 { IH_OS_OSE, "ose", "Enea OSE", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100108 { IH_OS_RTEMS, "rtems", "RTEMS", },
109 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
110#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
111 { IH_OS_QNX, "qnx", "QNX", },
112 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
113#endif
Peter Tyser56b8dd12008-09-08 14:56:49 -0500114#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
115 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
116#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100117#ifdef USE_HOSTCC
118 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
119 { IH_OS_DELL, "dell", "Dell", },
120 { IH_OS_ESIX, "esix", "Esix", },
121 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
122 { IH_OS_IRIX, "irix", "Irix", },
123 { IH_OS_NCR, "ncr", "NCR", },
124 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
125 { IH_OS_PSOS, "psos", "pSOS", },
126 { IH_OS_SCO, "sco", "SCO", },
127 { IH_OS_SOLARIS, "solaris", "Solaris", },
128 { IH_OS_SVR4, "svr4", "SVR4", },
129#endif
130 { -1, "", "", },
131};
132
Mike Frysingercf2feb12010-10-20 07:17:39 -0400133static const table_entry_t uimage_type[] = {
Stefano Babic38855512011-10-17 00:07:43 +0000134 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100135 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
136 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby34d12a12011-07-21 09:10:30 -0400137 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100138 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stefano Babic38855512011-10-17 00:07:43 +0000139 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
140 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
141 { IH_TYPE_INVALID, NULL, "Invalid Image", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100142 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic38855512011-10-17 00:07:43 +0000143 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100144 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
145 { IH_TYPE_SCRIPT, "script", "Script", },
146 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Heiko Schocher58cb84d2011-07-16 00:06:42 +0000147 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100148 { -1, "", "", },
149};
150
Mike Frysingercf2feb12010-10-20 07:17:39 -0400151static const table_entry_t uimage_comp[] = {
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100152 { IH_COMP_NONE, "none", "uncompressed", },
153 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
154 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellini35afc062008-09-08 02:46:13 +0200155 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100156 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100157 { -1, "", "", },
158};
159
Stephen Warren6904b6e2011-10-18 11:11:49 +0000160uint32_t crc32(uint32_t, const unsigned char *, uint);
161uint32_t crc32_wd(uint32_t, const unsigned char *, uint, uint);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100162#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000163static void genimg_print_time(time_t timestamp);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100164#endif
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100165
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100166/*****************************************************************************/
167/* Legacy format routines */
168/*****************************************************************************/
Stephen Warren6904b6e2011-10-18 11:11:49 +0000169int image_check_hcrc(const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100170{
171 ulong hcrc;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000172 ulong len = image_get_header_size();
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100173 image_header_t header;
174
175 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000176 memmove(&header, (char *)hdr, image_get_header_size());
177 image_set_hcrc(&header, 0);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100178
Stephen Warren6904b6e2011-10-18 11:11:49 +0000179 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100180
Stephen Warren6904b6e2011-10-18 11:11:49 +0000181 return (hcrc == image_get_hcrc(hdr));
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100182}
183
Stephen Warren6904b6e2011-10-18 11:11:49 +0000184int image_check_dcrc(const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100185{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000186 ulong data = image_get_data(hdr);
187 ulong len = image_get_data_size(hdr);
188 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100189
Stephen Warren6904b6e2011-10-18 11:11:49 +0000190 return (dcrc == image_get_dcrc(hdr));
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100191}
192
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100193/**
194 * image_multi_count - get component (sub-image) count
195 * @hdr: pointer to the header of the multi component image
196 *
197 * image_multi_count() returns number of components in a multi
198 * component image.
199 *
200 * Note: no checking of the image type is done, caller must pass
201 * a valid multi component image.
202 *
203 * returns:
204 * number of components
205 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000206ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100207{
208 ulong i, count = 0;
Marian Balakowicza1474212008-02-29 16:00:06 +0100209 uint32_t *size;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100210
211 /* get start of the image payload, which in case of multi
212 * component images that points to a table of component sizes */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000213 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100214
215 /* count non empty slots */
216 for (i = 0; size[i]; ++i)
217 count++;
218
219 return count;
220}
221
222/**
223 * image_multi_getimg - get component data address and size
224 * @hdr: pointer to the header of the multi component image
225 * @idx: index of the requested component
226 * @data: pointer to a ulong variable, will hold component data address
227 * @len: pointer to a ulong variable, will hold component size
228 *
229 * image_multi_getimg() returns size and data address for the requested
230 * component in a multi component image.
231 *
232 * Note: no checking of the image type is done, caller must pass
233 * a valid multi component image.
234 *
235 * returns:
236 * data address and size of the component, if idx is valid
237 * 0 in data and len, if idx is out of range
238 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000239void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100240 ulong *data, ulong *len)
241{
242 int i;
Marian Balakowicza1474212008-02-29 16:00:06 +0100243 uint32_t *size;
Nick Spence143a1592008-05-10 14:02:04 -0700244 ulong offset, count, img_data;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100245
246 /* get number of component */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000247 count = image_multi_count(hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100248
249 /* get start of the image payload, which in case of multi
250 * component images that points to a table of component sizes */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000251 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100252
253 /* get address of the proper component data start, which means
254 * skipping sizes table (add 1 for last, null entry) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000255 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100256
257 if (idx < count) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000258 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100259 offset = 0;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100260
261 /* go over all indices preceding requested component idx */
262 for (i = 0; i < idx; i++) {
Nick Spence143a1592008-05-10 14:02:04 -0700263 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000264 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100265 }
266
267 /* calculate idx-th component data address */
Nick Spence143a1592008-05-10 14:02:04 -0700268 *data = img_data + offset;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100269 } else {
270 *len = 0;
271 *data = 0;
272 }
273}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100274
Stephen Warren6904b6e2011-10-18 11:11:49 +0000275static void image_print_type(const image_header_t *hdr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100276{
277 const char *os, *arch, *type, *comp;
278
Stephen Warren6904b6e2011-10-18 11:11:49 +0000279 os = genimg_get_os_name(image_get_os(hdr));
280 arch = genimg_get_arch_name(image_get_arch(hdr));
281 type = genimg_get_type_name(image_get_type(hdr));
282 comp = genimg_get_comp_name(image_get_comp(hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100283
Stephen Warren6904b6e2011-10-18 11:11:49 +0000284 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100285}
286
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100287/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200288 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3b506772009-08-19 11:42:56 +0200289 * @ptr: pointer to the legacy format image header
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100290 * @p: pointer to prefix string
291 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200292 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100293 * The routine prints out all header fields followed by the size/offset data
294 * for MULTI/SCRIPT images.
295 *
296 * returns:
297 * no returned results
298 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000299void image_print_contents(const void *ptr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100300{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200301 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200302 const char *p;
303
304#ifdef USE_HOSTCC
305 p = "";
306#else
307 p = " ";
308#endif
309
Stephen Warren6904b6e2011-10-18 11:11:49 +0000310 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100311#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000312 printf("%sCreated: ", p);
313 genimg_print_time((time_t)image_get_time(hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100314#endif
Stephen Warren6904b6e2011-10-18 11:11:49 +0000315 printf("%sImage Type: ", p);
316 image_print_type(hdr);
317 printf("%sData Size: ", p);
318 genimg_print_size(image_get_data_size(hdr));
319 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
320 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100321
Stephen Warren6904b6e2011-10-18 11:11:49 +0000322 if (image_check_type(hdr, IH_TYPE_MULTI) ||
323 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100324 int i;
325 ulong data, len;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000326 ulong count = image_multi_count(hdr);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100327
Stephen Warren6904b6e2011-10-18 11:11:49 +0000328 printf("%sContents:\n", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100329 for (i = 0; i < count; i++) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000330 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100331
Stephen Warren6904b6e2011-10-18 11:11:49 +0000332 printf("%s Image %d: ", p, i);
333 genimg_print_size(len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100334
Stephen Warren6904b6e2011-10-18 11:11:49 +0000335 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100336 /*
337 * the user may need to know offsets
338 * if planning to do something with
339 * multiple files
340 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000341 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100342 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100343 }
344 }
345}
346
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100347
348#ifndef USE_HOSTCC
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100349/**
350 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100351 * @rd_addr: ramdisk image start address
352 * @arch: expected ramdisk architecture
353 * @verify: checksum verification flag
354 *
355 * image_get_ramdisk() returns a pointer to the verified ramdisk image
356 * header. Routine receives image start address and expected architecture
357 * flag. Verification done covers data and header integrity and os/type/arch
358 * fields checking.
359 *
360 * If dataflash support is enabled routine checks for dataflash addresses
361 * and handles required dataflash reads.
362 *
363 * returns:
364 * pointer to a ramdisk image header, if image was found and valid
365 * otherwise, return NULL
366 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000367static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100368 int verify)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100369{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200370 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100371
Stephen Warren6904b6e2011-10-18 11:11:49 +0000372 if (!image_check_magic(rd_hdr)) {
373 puts("Bad Magic Number\n");
374 show_boot_progress(-10);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100375 return NULL;
376 }
377
Stephen Warren6904b6e2011-10-18 11:11:49 +0000378 if (!image_check_hcrc(rd_hdr)) {
379 puts("Bad Header Checksum\n");
380 show_boot_progress(-11);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100381 return NULL;
382 }
383
Stephen Warren6904b6e2011-10-18 11:11:49 +0000384 show_boot_progress(10);
385 image_print_contents(rd_hdr);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100386
387 if (verify) {
388 puts(" Verifying Checksum ... ");
Stephen Warren6904b6e2011-10-18 11:11:49 +0000389 if (!image_check_dcrc(rd_hdr)) {
390 puts("Bad Data CRC\n");
391 show_boot_progress(-12);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100392 return NULL;
393 }
394 puts("OK\n");
395 }
396
Stephen Warren6904b6e2011-10-18 11:11:49 +0000397 show_boot_progress(11);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100398
Stephen Warren6904b6e2011-10-18 11:11:49 +0000399 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
400 !image_check_arch(rd_hdr, arch) ||
401 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
402 printf("No Linux %s Ramdisk Image\n",
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100403 genimg_get_arch_name(arch));
Stephen Warren6904b6e2011-10-18 11:11:49 +0000404 show_boot_progress(-13);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100405 return NULL;
406 }
407
408 return rd_hdr;
409}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100410#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100411
412/*****************************************************************************/
413/* Shared dual-format routines */
414/*****************************************************************************/
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100415#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +0000416int getenv_yesno(char *var)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100417{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000418 char *s = getenv(var);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100419 return (s && (*s == 'n')) ? 0 : 1;
420}
421
422ulong getenv_bootm_low(void)
423{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000424 char *s = getenv("bootm_low");
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100425 if (s) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000426 ulong tmp = simple_strtoul(s, NULL, 16);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100427 return tmp;
428 }
429
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200430#if defined(CONFIG_SYS_SDRAM_BASE)
431 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100432#elif defined(CONFIG_ARM)
433 return gd->bd->bi_dram[0].start;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100434#else
435 return 0;
436#endif
437}
438
Becky Bruced26d67c2008-06-09 20:37:18 -0500439phys_size_t getenv_bootm_size(void)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100440{
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500441 phys_size_t tmp;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000442 char *s = getenv("bootm_size");
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100443 if (s) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000444 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100445 return tmp;
446 }
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500447 s = getenv("bootm_low");
448 if (s)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000449 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500450 else
451 tmp = 0;
452
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100453
Marian Balakowicz2499b042008-03-12 12:14:15 +0100454#if defined(CONFIG_ARM)
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500455 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100456#else
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500457 return gd->bd->bi_memsize - tmp;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100458#endif
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100459}
460
Grant Likely26396382011-03-28 09:58:43 +0000461phys_size_t getenv_bootm_mapsize(void)
462{
463 phys_size_t tmp;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000464 char *s = getenv("bootm_mapsize");
Grant Likely26396382011-03-28 09:58:43 +0000465 if (s) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000466 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likely26396382011-03-28 09:58:43 +0000467 return tmp;
468 }
469
470#if defined(CONFIG_SYS_BOOTMAPSZ)
471 return CONFIG_SYS_BOOTMAPSZ;
472#else
473 return getenv_bootm_size();
474#endif
475}
476
Stephen Warren6904b6e2011-10-18 11:11:49 +0000477void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100478{
Larry Johnsone4551002010-04-20 08:09:43 -0400479 if (to == from)
480 return;
481
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100482#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
483 while (len > 0) {
484 size_t tail = (len > chunksz) ? chunksz : len;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000485 WATCHDOG_RESET();
486 memmove(to, from, tail);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100487 to += tail;
488 from += tail;
489 len -= tail;
490 }
491#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000492 memmove(to, from, len);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100493#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
494}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100495#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100496
Stephen Warren6904b6e2011-10-18 11:11:49 +0000497void genimg_print_size(uint32_t size)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100498{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100499#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +0000500 printf("%d Bytes = ", size);
501 print_size(size, "\n");
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100502#else
Stephen Warren6904b6e2011-10-18 11:11:49 +0000503 printf("%d Bytes = %.2f kB = %.2f MB\n",
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100504 size, (double)size / 1.024e3,
505 (double)size / 1.048576e6);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100506#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100507}
508
509#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000510static void genimg_print_time(time_t timestamp)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100511{
512#ifndef USE_HOSTCC
513 struct rtc_time tm;
514
Stephen Warren6904b6e2011-10-18 11:11:49 +0000515 to_tm(timestamp, &tm);
516 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100517 tm.tm_year, tm.tm_mon, tm.tm_mday,
518 tm.tm_hour, tm.tm_min, tm.tm_sec);
519#else
Stephen Warren6904b6e2011-10-18 11:11:49 +0000520 printf("%s", ctime(&timestamp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100521#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100522}
523#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
524
525/**
526 * get_table_entry_name - translate entry id to long name
527 * @table: pointer to a translation table for entries of a specific type
528 * @msg: message to be returned when translation fails
529 * @id: entry id to be translated
530 *
531 * get_table_entry_name() will go over translation table trying to find
532 * entry that matches given id. If matching entry is found, its long
533 * name is returned to the caller.
534 *
535 * returns:
536 * long entry name if translation succeeds
537 * msg otherwise
538 */
Mike Frysingercf2feb12010-10-20 07:17:39 -0400539char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100540{
541 for (; table->id >= 0; ++table) {
542 if (table->id == id)
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200543#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Wood68498ab2009-04-02 16:15:10 -0500544 return table->lname;
545#else
546 return table->lname + gd->reloc_off;
547#endif
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100548 }
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100549 return (msg);
550}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100551
Stephen Warren6904b6e2011-10-18 11:11:49 +0000552const char *genimg_get_os_name(uint8_t os)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100553{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000554 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100555}
556
Stephen Warren6904b6e2011-10-18 11:11:49 +0000557const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100558{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000559 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
560 arch));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100561}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100562
Stephen Warren6904b6e2011-10-18 11:11:49 +0000563const char *genimg_get_type_name(uint8_t type)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100564{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000565 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100566}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100567
Stephen Warren6904b6e2011-10-18 11:11:49 +0000568const char *genimg_get_comp_name(uint8_t comp)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100569{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000570 return (get_table_entry_name(uimage_comp, "Unknown Compression",
571 comp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100572}
573
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100574/**
575 * get_table_entry_id - translate short entry name to id
576 * @table: pointer to a translation table for entries of a specific type
577 * @table_name: to be used in case of error
578 * @name: entry short name to be translated
579 *
580 * get_table_entry_id() will go over translation table trying to find
581 * entry that matches given short name. If matching entry is found,
582 * its id returned to the caller.
583 *
584 * returns:
585 * entry id if translation succeeds
586 * -1 otherwise
587 */
Mike Frysingercf2feb12010-10-20 07:17:39 -0400588int get_table_entry_id(const table_entry_t *table,
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100589 const char *table_name, const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100590{
Mike Frysingercf2feb12010-10-20 07:17:39 -0400591 const table_entry_t *t;
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100592#ifdef USE_HOSTCC
593 int first = 1;
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100594
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100595 for (t = table; t->id >= 0; ++t) {
596 if (t->sname && strcasecmp(t->sname, name) == 0)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000597 return(t->id);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100598 }
599
Stephen Warren6904b6e2011-10-18 11:11:49 +0000600 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100601 for (t = table; t->id >= 0; ++t) {
602 if (t->sname == NULL)
603 continue;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000604 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100605 first = 0;
606 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000607 fprintf(stderr, "\n");
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100608#else
609 for (t = table; t->id >= 0; ++t) {
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200610#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Wood68498ab2009-04-02 16:15:10 -0500611 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200612#else
613 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser9057cbf2009-09-21 11:20:36 -0500614#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100615 return (t->id);
616 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000617 debug("Invalid %s Type: %s\n", table_name, name);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100618#endif /* USE_HOSTCC */
619 return (-1);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100620}
621
Stephen Warren6904b6e2011-10-18 11:11:49 +0000622int genimg_get_os_id(const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100623{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000624 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100625}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100626
Stephen Warren6904b6e2011-10-18 11:11:49 +0000627int genimg_get_arch_id(const char *name)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100628{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000629 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100630}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100631
Stephen Warren6904b6e2011-10-18 11:11:49 +0000632int genimg_get_type_id(const char *name)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100633{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000634 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100635}
Marian Balakowicz95418502008-01-31 13:55:39 +0100636
Stephen Warren6904b6e2011-10-18 11:11:49 +0000637int genimg_get_comp_id(const char *name)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100638{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000639 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100640}
641
642#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +0100643/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100644 * genimg_get_format - get image format type
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100645 * @img_addr: image start address
646 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100647 * genimg_get_format() checks whether provided address points to a valid
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100648 * legacy or FIT image.
649 *
Marian Balakowicz12560682008-02-27 11:02:26 +0100650 * New uImage format and FDT blob are based on a libfdt. FDT blob
651 * may be passed directly or embedded in a FIT image. In both situations
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100652 * genimg_get_format() must be able to dectect libfdt header.
Marian Balakowicz12560682008-02-27 11:02:26 +0100653 *
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100654 * returns:
655 * image format type or IMAGE_FORMAT_INVALID if no image is present
656 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000657int genimg_get_format(void *img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100658{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200659 ulong format = IMAGE_FORMAT_INVALID;
660 const image_header_t *hdr;
Marian Balakowicz12560682008-02-27 11:02:26 +0100661#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3b506772009-08-19 11:42:56 +0200662 char *fit_hdr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100663#endif
664
Wolfgang Denk3b506772009-08-19 11:42:56 +0200665 hdr = (const image_header_t *)img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100666 if (image_check_magic(hdr))
667 format = IMAGE_FORMAT_LEGACY;
Marian Balakowicz12560682008-02-27 11:02:26 +0100668#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100669 else {
670 fit_hdr = (char *)img_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000671 if (fdt_check_header(fit_hdr) == 0)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100672 format = IMAGE_FORMAT_FIT;
673 }
674#endif
675
676 return format;
677}
678
679/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100680 * genimg_get_image - get image from special storage (if necessary)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100681 * @img_addr: image start address
682 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100683 * genimg_get_image() checks if provided image start adddress is located
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100684 * in a dataflash storage. If so, image is moved to a system RAM memory.
685 *
686 * returns:
687 * image start address after possible relocation from special storage
688 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000689ulong genimg_get_image(ulong img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100690{
Marian Balakowicz535b9102008-02-27 11:00:47 +0100691 ulong ram_addr = img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100692
693#ifdef CONFIG_HAS_DATAFLASH
Marian Balakowicz535b9102008-02-27 11:00:47 +0100694 ulong h_size, d_size;
695
Stephen Warren6904b6e2011-10-18 11:11:49 +0000696 if (addr_dataflash(img_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100697 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200698 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100699
700 /* get header size */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000701 h_size = image_get_header_size();
Marian Balakowicz535b9102008-02-27 11:00:47 +0100702#if defined(CONFIG_FIT)
703 if (sizeof(struct fdt_header) > h_size)
704 h_size = sizeof(struct fdt_header);
705#endif
706
707 /* read in header */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000708 debug(" Reading image header from dataflash address "
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100709 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100710
Stephen Warren6904b6e2011-10-18 11:11:49 +0000711 read_dataflash(img_addr, h_size, (char *)ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100712
Marian Balakowicz535b9102008-02-27 11:00:47 +0100713 /* get data size */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000714 switch (genimg_get_format((void *)ram_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100715 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000716 d_size = image_get_data_size(
717 (const image_header_t *)ram_addr);
718 debug(" Legacy format image found at 0x%08lx, "
719 "size 0x%08lx\n",
Marian Balakowicz535b9102008-02-27 11:00:47 +0100720 ram_addr, d_size);
721 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100722#if defined(CONFIG_FIT)
Marian Balakowicz535b9102008-02-27 11:00:47 +0100723 case IMAGE_FORMAT_FIT:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000724 d_size = fit_get_size((const void *)ram_addr) - h_size;
725 debug(" FIT/FDT format image found at 0x%08lx, "
726 "size 0x%08lx\n",
Marian Balakowicz535b9102008-02-27 11:00:47 +0100727 ram_addr, d_size);
728 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100729#endif
Marian Balakowicz535b9102008-02-27 11:00:47 +0100730 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000731 printf(" No valid image found at 0x%08lx\n",
732 img_addr);
Marian Balakowicz535b9102008-02-27 11:00:47 +0100733 return ram_addr;
734 }
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100735
Marian Balakowicz535b9102008-02-27 11:00:47 +0100736 /* read in image data */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000737 debug(" Reading image remaining data from dataflash address "
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100738 "%08lx to RAM address %08lx\n", img_addr + h_size,
739 ram_addr + h_size);
740
Stephen Warren6904b6e2011-10-18 11:11:49 +0000741 read_dataflash(img_addr + h_size, d_size,
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100742 (char *)(ram_addr + h_size));
Marian Balakowicz535b9102008-02-27 11:00:47 +0100743
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100744 }
Marian Balakowicz535b9102008-02-27 11:00:47 +0100745#endif /* CONFIG_HAS_DATAFLASH */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100746
747 return ram_addr;
748}
749
750/**
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100751 * fit_has_config - check if there is a valid FIT configuration
752 * @images: pointer to the bootm command headers structure
753 *
754 * fit_has_config() checks if there is a FIT configuration in use
755 * (if FTI support is present).
756 *
757 * returns:
758 * 0, no FIT support or no configuration found
759 * 1, configuration found
760 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000761int genimg_has_config(bootm_headers_t *images)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100762{
763#if defined(CONFIG_FIT)
764 if (images->fit_uname_cfg)
765 return 1;
766#endif
767 return 0;
768}
769
770/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100771 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz95418502008-01-31 13:55:39 +0100772 * @argc: command argument count
773 * @argv: command argument list
Marian Balakowicz61fde552008-02-27 11:01:04 +0100774 * @images: pointer to the bootm images structure
Marian Balakowicz95418502008-01-31 13:55:39 +0100775 * @arch: expected ramdisk architecture
776 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
777 * @rd_end: pointer to a ulong variable, will hold ramdisk end
778 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100779 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz95418502008-01-31 13:55:39 +0100780 * Curently supported are the following ramdisk sources:
781 * - multicomponent kernel/ramdisk image,
782 * - commandline provided address of decicated ramdisk image.
783 *
784 * returns:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100785 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz95418502008-01-31 13:55:39 +0100786 * rd_start and rd_end are set to ramdisk start/end addresses if
787 * ramdisk image is found and valid
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100788 *
Kumar Galab8ca7342008-08-29 19:08:29 -0500789 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz95418502008-01-31 13:55:39 +0100790 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz95418502008-01-31 13:55:39 +0100791 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000792int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100793 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz95418502008-01-31 13:55:39 +0100794{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100795 ulong rd_addr, rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100796 ulong rd_data, rd_len;
Wolfgang Denk3b506772009-08-19 11:42:56 +0200797 const image_header_t *rd_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100798#if defined(CONFIG_FIT)
799 void *fit_hdr;
800 const char *fit_uname_config = NULL;
801 const char *fit_uname_ramdisk = NULL;
802 ulong default_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100803 int rd_noffset;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100804 int cfg_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100805 const void *data;
806 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100807#endif
Marian Balakowicz95418502008-01-31 13:55:39 +0100808
Marian Balakowicza50d5152008-03-12 10:12:37 +0100809 *rd_start = 0;
810 *rd_end = 0;
811
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100812 /*
813 * Look for a '-' which indicates to ignore the
814 * ramdisk argument
815 */
816 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000817 debug("## Skipping init Ramdisk\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100818 rd_len = rd_data = 0;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000819 } else if (argc >= 3 || genimg_has_config(images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100820#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100821 if (argc >= 3) {
822 /*
823 * If the init ramdisk comes from the FIT image and
824 * the FIT image address is omitted in the command
825 * line argument, try to use os FIT image address or
826 * default load address.
827 */
828 if (images->fit_uname_os)
829 default_addr = (ulong)images->fit_hdr_os;
830 else
831 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100832
Stephen Warren6904b6e2011-10-18 11:11:49 +0000833 if (fit_parse_conf(argv[2], default_addr,
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100834 &rd_addr, &fit_uname_config)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000835 debug("* ramdisk: config '%s' from image at "
836 "0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100837 fit_uname_config, rd_addr);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000838 } else if (fit_parse_subimage(argv[2], default_addr,
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100839 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000840 debug("* ramdisk: subimage '%s' from image at "
841 "0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100842 fit_uname_ramdisk, rd_addr);
843 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100844#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100845 {
846 rd_addr = simple_strtoul(argv[2], NULL, 16);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000847 debug("* ramdisk: cmdline image address = "
848 "0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100849 rd_addr);
850 }
851#if defined(CONFIG_FIT)
852 } else {
853 /* use FIT configuration provided in first bootm
854 * command argument
855 */
856 rd_addr = (ulong)images->fit_hdr_os;
857 fit_uname_config = images->fit_uname_cfg;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000858 debug("* ramdisk: using config '%s' from image "
859 "at 0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100860 fit_uname_config, rd_addr);
861
862 /*
863 * Check whether configuration has ramdisk defined,
864 * if not, don't try to use it, quit silently.
865 */
866 fit_hdr = (void *)rd_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000867 cfg_noffset = fit_conf_get_node(fit_hdr,
868 fit_uname_config);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100869 if (cfg_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000870 debug("* ramdisk: no such config\n");
Michal Simekb2474002008-07-11 10:43:13 +0200871 return 1;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100872 }
873
Stephen Warren6904b6e2011-10-18 11:11:49 +0000874 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
875 cfg_noffset);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100876 if (rd_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000877 debug("* ramdisk: no ramdisk in config\n");
Peter Tyser190ee482008-08-05 10:51:57 -0500878 return 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100879 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100880 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100881#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100882
883 /* copy from dataflash if needed */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000884 rd_addr = genimg_get_image(rd_addr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100885
886 /*
887 * Check if there is an initrd image at the
888 * address provided in the second bootm argument
889 * check image type, for FIT images get FIT node.
890 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000891 switch (genimg_get_format((void *)rd_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100892 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000893 printf("## Loading init Ramdisk from Legacy "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100894 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz95418502008-01-31 13:55:39 +0100895
Stephen Warren6904b6e2011-10-18 11:11:49 +0000896 show_boot_progress(9);
897 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100898 images->verify);
Marian Balakowicz95418502008-01-31 13:55:39 +0100899
Marian Balakowicza50d5152008-03-12 10:12:37 +0100900 if (rd_hdr == NULL)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600901 return 1;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600902
Stephen Warren6904b6e2011-10-18 11:11:49 +0000903 rd_data = image_get_data(rd_hdr);
904 rd_len = image_get_data_size(rd_hdr);
905 rd_load = image_get_load(rd_hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100906 break;
907#if defined(CONFIG_FIT)
908 case IMAGE_FORMAT_FIT:
909 fit_hdr = (void *)rd_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000910 printf("## Loading init Ramdisk from FIT "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100911 "Image at %08lx ...\n", rd_addr);
912
Stephen Warren6904b6e2011-10-18 11:11:49 +0000913 show_boot_progress(120);
914 if (!fit_check_format(fit_hdr)) {
915 puts("Bad FIT ramdisk image format!\n");
916 show_boot_progress(-120);
Michal Simekb2474002008-07-11 10:43:13 +0200917 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100918 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000919 show_boot_progress(121);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100920
921 if (!fit_uname_ramdisk) {
922 /*
923 * no ramdisk image node unit name, try to get config
924 * node first. If config unit node name is NULL
925 * fit_conf_get_node() will try to find default config node
926 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000927 show_boot_progress(122);
928 cfg_noffset = fit_conf_get_node(fit_hdr,
929 fit_uname_config);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100930 if (cfg_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000931 puts("Could not find configuration "
932 "node\n");
933 show_boot_progress(-122);
Michal Simekb2474002008-07-11 10:43:13 +0200934 return 1;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100935 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000936 fit_uname_config = fdt_get_name(fit_hdr,
937 cfg_noffset, NULL);
938 printf(" Using '%s' configuration\n",
939 fit_uname_config);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100940
Stephen Warren6904b6e2011-10-18 11:11:49 +0000941 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
942 cfg_noffset);
943 fit_uname_ramdisk = fit_get_name(fit_hdr,
944 rd_noffset, NULL);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100945 } else {
946 /* get ramdisk component image node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000947 show_boot_progress(123);
948 rd_noffset = fit_image_get_node(fit_hdr,
949 fit_uname_ramdisk);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100950 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100951 if (rd_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000952 puts("Could not find subimage node\n");
953 show_boot_progress(-124);
Michal Simekb2474002008-07-11 10:43:13 +0200954 return 1;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100955 }
Marian Balakowicza50d5152008-03-12 10:12:37 +0100956
Stephen Warren6904b6e2011-10-18 11:11:49 +0000957 printf(" Trying '%s' ramdisk subimage\n",
958 fit_uname_ramdisk);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100959
Stephen Warren6904b6e2011-10-18 11:11:49 +0000960 show_boot_progress(125);
961 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
962 images->verify))
Michal Simekb2474002008-07-11 10:43:13 +0200963 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100964
965 /* get ramdisk image data address and length */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000966 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
967 &size)) {
968 puts("Could not find ramdisk subimage data!\n");
969 show_boot_progress(-127);
Michal Simekb2474002008-07-11 10:43:13 +0200970 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100971 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000972 show_boot_progress(128);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100973
974 rd_data = (ulong)data;
975 rd_len = size;
976
Stephen Warren6904b6e2011-10-18 11:11:49 +0000977 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
978 puts("Can't get ramdisk subimage load "
979 "address!\n");
980 show_boot_progress(-129);
Michal Simekb2474002008-07-11 10:43:13 +0200981 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100982 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000983 show_boot_progress(129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100984
985 images->fit_hdr_rd = fit_hdr;
986 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100987 images->fit_noffset_rd = rd_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100988 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100989#endif
990 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000991 puts("Wrong Ramdisk Image Format\n");
Marian Balakowicza50d5152008-03-12 10:12:37 +0100992 rd_data = rd_len = rd_load = 0;
Kumar Galab8ca7342008-08-29 19:08:29 -0500993 return 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100994 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100995 } else if (images->legacy_hdr_valid &&
Stephen Warren6904b6e2011-10-18 11:11:49 +0000996 image_check_type(&images->legacy_hdr_os_copy,
997 IH_TYPE_MULTI)) {
998
Marian Balakowicz95418502008-01-31 13:55:39 +0100999 /*
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001000 * Now check if we have a legacy mult-component image,
1001 * get second entry data start address and len.
Marian Balakowicz95418502008-01-31 13:55:39 +01001002 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001003 show_boot_progress(13);
1004 printf("## Loading init Ramdisk from multi component "
Marian Balakowicza50d5152008-03-12 10:12:37 +01001005 "Legacy Image at %08lx ...\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001006 (ulong)images->legacy_hdr_os);
1007
Stephen Warren6904b6e2011-10-18 11:11:49 +00001008 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz95418502008-01-31 13:55:39 +01001009 } else {
1010 /*
1011 * no initrd image
1012 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001013 show_boot_progress(14);
Marian Balakowicz95418502008-01-31 13:55:39 +01001014 rd_len = rd_data = 0;
1015 }
1016
1017 if (!rd_data) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001018 debug("## No init Ramdisk\n");
Marian Balakowicz95418502008-01-31 13:55:39 +01001019 } else {
1020 *rd_start = rd_data;
1021 *rd_end = rd_data + rd_len;
1022 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001023 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz95418502008-01-31 13:55:39 +01001024 *rd_start, *rd_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -06001025
1026 return 0;
Marian Balakowicz95418502008-01-31 13:55:39 +01001027}
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001028
John Rigbyeea8e692010-10-13 13:57:35 -06001029#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001030/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001031 * boot_ramdisk_high - relocate init ramdisk
Kumar Galab937bb72008-02-27 21:51:49 -06001032 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001033 * @rd_data: ramdisk data start address
1034 * @rd_len: ramdisk data length
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001035 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1036 * start address (after possible relocation)
1037 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1038 * end address (after possible relocation)
1039 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001040 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001041 * variable and if requested ramdisk data is moved to a specified location.
1042 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001043 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1044 * start/end addresses if ramdisk image start and len were provided,
1045 * otherwise set initrd_start and initrd_end set to zeros.
1046 *
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001047 * returns:
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001048 * 0 - success
1049 * -1 - failure
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001050 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001051int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galab937bb72008-02-27 21:51:49 -06001052 ulong *initrd_start, ulong *initrd_end)
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001053{
1054 char *s;
1055 ulong initrd_high;
1056 int initrd_copy_to_ram = 1;
1057
Stephen Warren6904b6e2011-10-18 11:11:49 +00001058 if ((s = getenv("initrd_high")) != NULL) {
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001059 /* a value of "no" or a similar string will act like 0,
1060 * turning the "load high" feature off. This is intentional.
1061 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001062 initrd_high = simple_strtoul(s, NULL, 16);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001063 if (initrd_high == ~0)
1064 initrd_copy_to_ram = 0;
1065 } else {
1066 /* not set, no restrictions to load high */
1067 initrd_high = ~0;
1068 }
1069
Marian Balakowicz902ce702008-05-13 15:53:29 +02001070
1071#ifdef CONFIG_LOGBUFFER
1072 /* Prevent initrd from overwriting logbuffer */
1073 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1074#endif
1075
Stephen Warren6904b6e2011-10-18 11:11:49 +00001076 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001077 initrd_high, initrd_copy_to_ram);
1078
1079 if (rd_data) {
1080 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001081 debug(" in-place initrd\n");
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001082 *initrd_start = rd_data;
1083 *initrd_end = rd_data + rd_len;
Kumar Galab937bb72008-02-27 21:51:49 -06001084 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001085 } else {
Kumar Galab937bb72008-02-27 21:51:49 -06001086 if (initrd_high)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001087 *initrd_start = (ulong)lmb_alloc_base(lmb,
1088 rd_len, 0x1000, initrd_high);
Kumar Galab937bb72008-02-27 21:51:49 -06001089 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00001090 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1091 0x1000);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001092
Kumar Galab937bb72008-02-27 21:51:49 -06001093 if (*initrd_start == 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001094 puts("ramdisk - allocation error\n");
Kumar Galab937bb72008-02-27 21:51:49 -06001095 goto error;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001096 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001097 show_boot_progress(12);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001098
1099 *initrd_end = *initrd_start + rd_len;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001100 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001101 *initrd_start, *initrd_end);
1102
Stephen Warren6904b6e2011-10-18 11:11:49 +00001103 memmove_wd((void *)*initrd_start,
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001104 (void *)rd_data, rd_len, CHUNKSZ);
1105
Stephen Warren6904b6e2011-10-18 11:11:49 +00001106 puts("OK\n");
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001107 }
1108 } else {
1109 *initrd_start = 0;
1110 *initrd_end = 0;
1111 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001112 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001113 *initrd_start, *initrd_end);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001114
Kumar Galab937bb72008-02-27 21:51:49 -06001115 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001116
Kumar Galab937bb72008-02-27 21:51:49 -06001117error:
1118 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001119}
John Rigbyeea8e692010-10-13 13:57:35 -06001120#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001121
Kumar Galaa5130ad2008-08-15 08:24:38 -05001122#ifdef CONFIG_OF_LIBFDT
Stephen Warren6904b6e2011-10-18 11:11:49 +00001123static void fdt_error(const char *msg)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001124{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001125 puts("ERROR: ");
1126 puts(msg);
1127 puts(" - must RESET the board to recover.\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001128}
1129
Stephen Warren6904b6e2011-10-18 11:11:49 +00001130static const image_header_t *image_get_fdt(ulong fdt_addr)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001131{
Wolfgang Denk3b506772009-08-19 11:42:56 +02001132 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001133
Stephen Warren6904b6e2011-10-18 11:11:49 +00001134 image_print_contents(fdt_hdr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001135
Stephen Warren6904b6e2011-10-18 11:11:49 +00001136 puts(" Verifying Checksum ... ");
1137 if (!image_check_hcrc(fdt_hdr)) {
1138 fdt_error("fdt header checksum invalid");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001139 return NULL;
1140 }
1141
Stephen Warren6904b6e2011-10-18 11:11:49 +00001142 if (!image_check_dcrc(fdt_hdr)) {
1143 fdt_error("fdt checksum invalid");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001144 return NULL;
1145 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001146 puts("OK\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001147
Stephen Warren6904b6e2011-10-18 11:11:49 +00001148 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1149 fdt_error("uImage is not a fdt");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001150 return NULL;
1151 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001152 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1153 fdt_error("uImage is compressed");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001154 return NULL;
1155 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001156 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1157 fdt_error("uImage data is not a fdt");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001158 return NULL;
1159 }
1160 return fdt_hdr;
1161}
1162
1163/**
1164 * fit_check_fdt - verify FIT format FDT subimage
1165 * @fit_hdr: pointer to the FIT header
1166 * fdt_noffset: FDT subimage node offset within FIT image
1167 * @verify: data CRC verification flag
1168 *
1169 * fit_check_fdt() verifies integrity of the FDT subimage and from
1170 * specified FIT image.
1171 *
1172 * returns:
1173 * 1, on success
1174 * 0, on failure
1175 */
1176#if defined(CONFIG_FIT)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001177static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001178{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001179 fit_image_print(fit, fdt_noffset, " ");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001180
1181 if (verify) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001182 puts(" Verifying Hash Integrity ... ");
1183 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1184 fdt_error("Bad Data Hash");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001185 return 0;
1186 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001187 puts("OK\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001188 }
1189
Stephen Warren6904b6e2011-10-18 11:11:49 +00001190 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1191 fdt_error("Not a FDT image");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001192 return 0;
1193 }
1194
Stephen Warren6904b6e2011-10-18 11:11:49 +00001195 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1196 fdt_error("FDT image is compressed");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001197 return 0;
1198 }
1199
1200 return 1;
1201}
1202#endif /* CONFIG_FIT */
1203
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001204#ifndef CONFIG_SYS_FDT_PAD
1205#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Galaa5130ad2008-08-15 08:24:38 -05001206#endif
1207
Grant Likely20f54922011-03-28 09:59:01 +00001208#if defined(CONFIG_OF_LIBFDT)
1209/**
1210 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1211 * @lmb: pointer to lmb handle, will be used for memory mgmt
1212 * @fdt_blob: pointer to fdt blob base address
1213 *
1214 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1215 * memreserve regions prevents u-boot from using them to store the initrd
1216 * or the fdt blob.
1217 */
1218void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1219{
1220 uint64_t addr, size;
1221 int i, total;
1222
Stephen Warren6904b6e2011-10-18 11:11:49 +00001223 if (fdt_check_header(fdt_blob) != 0)
Grant Likely20f54922011-03-28 09:59:01 +00001224 return;
1225
1226 total = fdt_num_mem_rsv(fdt_blob);
1227 for (i = 0; i < total; i++) {
1228 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1229 continue;
1230 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1231 (unsigned long long)addr, (unsigned long long)size);
1232 lmb_reserve(lmb, addr, size);
1233 }
1234}
1235
Kumar Galaa5130ad2008-08-15 08:24:38 -05001236/**
1237 * boot_relocate_fdt - relocate flat device tree
1238 * @lmb: pointer to lmb handle, will be used for memory mgmt
Kumar Galaa5130ad2008-08-15 08:24:38 -05001239 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1240 * @of_size: pointer to a ulong variable, will hold fdt length
1241 *
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001242 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1243 * relocates the of_flat_tree into that region, even if the fdt is already in
1244 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1245 * bytes.
Kumar Galaa5130ad2008-08-15 08:24:38 -05001246 *
1247 * of_flat_tree and of_size are set to final (after relocation) values
1248 *
1249 * returns:
1250 * 0 - success
1251 * 1 - failure
1252 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001253int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001254{
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001255 void *fdt_blob = *of_flat_tree;
1256 void *of_start = 0;
David A. Longd558a4e2011-07-09 16:40:19 -04001257 char *fdt_high;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001258 ulong of_len = 0;
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001259 int err;
David A. Longd558a4e2011-07-09 16:40:19 -04001260 int disable_relocation = 0;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001261
1262 /* nothing to do */
1263 if (*of_size == 0)
1264 return 0;
1265
Stephen Warren6904b6e2011-10-18 11:11:49 +00001266 if (fdt_check_header(fdt_blob) != 0) {
1267 fdt_error("image is not a fdt");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001268 goto error;
1269 }
1270
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001271 /* position on a 4K boundary before the alloc_current */
1272 /* Pad the FDT by a specified amount */
1273 of_len = *of_size + CONFIG_SYS_FDT_PAD;
David A. Longd558a4e2011-07-09 16:40:19 -04001274
1275 /* If fdt_high is set use it to select the relocation address */
1276 fdt_high = getenv("fdt_high");
1277 if (fdt_high) {
1278 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1279
1280 if (((ulong) desired_addr) == ~0UL) {
1281 /* All ones means use fdt in place */
1282 desired_addr = fdt_blob;
1283 disable_relocation = 1;
1284 }
1285 if (desired_addr) {
1286 of_start =
1287 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1288 ((ulong)
1289 desired_addr)
1290 + of_len);
1291 if (desired_addr && of_start != desired_addr) {
1292 puts("Failed using fdt_high value for Device Tree");
1293 goto error;
1294 }
1295 } else {
1296 of_start =
Matthew McClintockdcf6fd72011-07-18 13:08:05 +00001297 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
David A. Longd558a4e2011-07-09 16:40:19 -04001298 }
1299 } else {
1300 of_start =
1301 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1302 getenv_bootm_mapsize()
1303 + getenv_bootm_low());
1304 }
Kumar Galaa5130ad2008-08-15 08:24:38 -05001305
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001306 if (of_start == 0) {
1307 puts("device tree - allocation error\n");
1308 goto error;
1309 }
Kumar Galaa5130ad2008-08-15 08:24:38 -05001310
David A. Longd558a4e2011-07-09 16:40:19 -04001311 if (disable_relocation) {
1312 /* We assume there is space after the existing fdt to use for padding */
1313 fdt_set_totalsize(of_start, of_len);
1314 printf(" Using Device Tree in place at %p, end %p\n",
1315 of_start, of_start + of_len - 1);
1316 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001317 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
David A. Longd558a4e2011-07-09 16:40:19 -04001318 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001319
Stephen Warren6904b6e2011-10-18 11:11:49 +00001320 printf(" Loading Device Tree to %p, end %p ... ",
David A. Longd558a4e2011-07-09 16:40:19 -04001321 of_start, of_start + of_len - 1);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001322
Stephen Warren6904b6e2011-10-18 11:11:49 +00001323 err = fdt_open_into(fdt_blob, of_start, of_len);
David A. Longd558a4e2011-07-09 16:40:19 -04001324 if (err != 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001325 fdt_error("fdt move failed");
David A. Longd558a4e2011-07-09 16:40:19 -04001326 goto error;
1327 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001328 puts("OK\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001329 }
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001330
1331 *of_flat_tree = of_start;
1332 *of_size = of_len;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001333
Kumar Gala9f4e7e42008-08-15 08:24:39 -05001334 set_working_fdt_addr(*of_flat_tree);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001335 return 0;
1336
1337error:
1338 return 1;
1339}
Grant Likely50fb22e2011-03-28 09:58:49 +00001340#endif /* CONFIG_OF_LIBFDT */
Kumar Galaa5130ad2008-08-15 08:24:38 -05001341
1342/**
1343 * boot_get_fdt - main fdt handling routine
1344 * @argc: command argument count
1345 * @argv: command argument list
1346 * @images: pointer to the bootm images structure
1347 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1348 * @of_size: pointer to a ulong variable, will hold fdt length
1349 *
1350 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1351 * Curently supported are the following ramdisk sources:
1352 * - multicomponent kernel/ramdisk image,
1353 * - commandline provided address of decicated ramdisk image.
1354 *
1355 * returns:
1356 * 0, if fdt image was found and valid, or skipped
1357 * of_flat_tree and of_size are set to fdt start address and length if
1358 * fdt image is found and valid
1359 *
1360 * 1, if fdt image is found but corrupted
1361 * of_flat_tree and of_size are set to 0 if no fdt exists
1362 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001363int boot_get_fdt(int flag, int argc, char * const argv[],
1364 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001365{
Wolfgang Denk3b506772009-08-19 11:42:56 +02001366 const image_header_t *fdt_hdr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001367 ulong fdt_addr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001368 char *fdt_blob = NULL;
1369 ulong image_start, image_end;
1370 ulong load_start, load_end;
1371#if defined(CONFIG_FIT)
1372 void *fit_hdr;
1373 const char *fit_uname_config = NULL;
1374 const char *fit_uname_fdt = NULL;
1375 ulong default_addr;
1376 int cfg_noffset;
1377 int fdt_noffset;
1378 const void *data;
1379 size_t size;
1380#endif
1381
1382 *of_flat_tree = NULL;
1383 *of_size = 0;
1384
Stephen Warren6904b6e2011-10-18 11:11:49 +00001385 if (argc > 3 || genimg_has_config(images)) {
Kumar Galaa5130ad2008-08-15 08:24:38 -05001386#if defined(CONFIG_FIT)
1387 if (argc > 3) {
1388 /*
1389 * If the FDT blob comes from the FIT image and the
1390 * FIT image address is omitted in the command line
1391 * argument, try to use ramdisk or os FIT image
1392 * address or default load address.
1393 */
1394 if (images->fit_uname_rd)
1395 default_addr = (ulong)images->fit_hdr_rd;
1396 else if (images->fit_uname_os)
1397 default_addr = (ulong)images->fit_hdr_os;
1398 else
1399 default_addr = load_addr;
1400
Stephen Warren6904b6e2011-10-18 11:11:49 +00001401 if (fit_parse_conf(argv[3], default_addr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001402 &fdt_addr, &fit_uname_config)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001403 debug("* fdt: config '%s' from image at "
1404 "0x%08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001405 fit_uname_config, fdt_addr);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001406 } else if (fit_parse_subimage(argv[3], default_addr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001407 &fdt_addr, &fit_uname_fdt)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001408 debug("* fdt: subimage '%s' from image at "
1409 "0x%08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001410 fit_uname_fdt, fdt_addr);
1411 } else
1412#endif
1413 {
1414 fdt_addr = simple_strtoul(argv[3], NULL, 16);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001415 debug("* fdt: cmdline image address = "
1416 "0x%08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001417 fdt_addr);
1418 }
1419#if defined(CONFIG_FIT)
1420 } else {
1421 /* use FIT configuration provided in first bootm
1422 * command argument
1423 */
1424 fdt_addr = (ulong)images->fit_hdr_os;
1425 fit_uname_config = images->fit_uname_cfg;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001426 debug("* fdt: using config '%s' from image "
1427 "at 0x%08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001428 fit_uname_config, fdt_addr);
1429
1430 /*
1431 * Check whether configuration has FDT blob defined,
1432 * if not quit silently.
1433 */
1434 fit_hdr = (void *)fdt_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001435 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001436 fit_uname_config);
1437 if (cfg_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001438 debug("* fdt: no such config\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001439 return 0;
1440 }
1441
Stephen Warren6904b6e2011-10-18 11:11:49 +00001442 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001443 cfg_noffset);
1444 if (fdt_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001445 debug("* fdt: no fdt in config\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001446 return 0;
1447 }
1448 }
1449#endif
1450
Stephen Warren6904b6e2011-10-18 11:11:49 +00001451 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001452 fdt_addr);
1453
1454 /* copy from dataflash if needed */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001455 fdt_addr = genimg_get_image(fdt_addr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001456
1457 /*
1458 * Check if there is an FDT image at the
1459 * address provided in the second bootm argument
1460 * check image type, for FIT images get a FIT node.
1461 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001462 switch (genimg_get_format((void *)fdt_addr)) {
Kumar Galaa5130ad2008-08-15 08:24:38 -05001463 case IMAGE_FORMAT_LEGACY:
1464 /* verify fdt_addr points to a valid image header */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001465 printf("## Flattened Device Tree from Legacy Image "
1466 "at %08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001467 fdt_addr);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001468 fdt_hdr = image_get_fdt(fdt_addr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001469 if (!fdt_hdr)
1470 goto error;
1471
1472 /*
1473 * move image data to the load address,
1474 * make sure we don't overwrite initial image
1475 */
1476 image_start = (ulong)fdt_hdr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001477 image_end = image_get_image_end(fdt_hdr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001478
Stephen Warren6904b6e2011-10-18 11:11:49 +00001479 load_start = image_get_load(fdt_hdr);
1480 load_end = load_start + image_get_data_size(fdt_hdr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001481
1482 if ((load_start < image_end) && (load_end > image_start)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001483 fdt_error("fdt overwritten");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001484 goto error;
1485 }
1486
Stephen Warren6904b6e2011-10-18 11:11:49 +00001487 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
1488 image_get_data(fdt_hdr), load_start);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001489
Stephen Warren6904b6e2011-10-18 11:11:49 +00001490 memmove((void *)load_start,
1491 (void *)image_get_data(fdt_hdr),
1492 image_get_data_size(fdt_hdr));
Kumar Galaa5130ad2008-08-15 08:24:38 -05001493
1494 fdt_blob = (char *)load_start;
1495 break;
1496 case IMAGE_FORMAT_FIT:
1497 /*
1498 * This case will catch both: new uImage format
1499 * (libfdt based) and raw FDT blob (also libfdt
1500 * based).
1501 */
1502#if defined(CONFIG_FIT)
1503 /* check FDT blob vs FIT blob */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001504 if (fit_check_format((const void *)fdt_addr)) {
Kumar Galaa5130ad2008-08-15 08:24:38 -05001505 /*
1506 * FIT image
1507 */
1508 fit_hdr = (void *)fdt_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001509 printf("## Flattened Device Tree from FIT "
1510 "Image at %08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001511 fdt_addr);
1512
1513 if (!fit_uname_fdt) {
1514 /*
1515 * no FDT blob image node unit name,
1516 * try to get config node first. If
1517 * config unit node name is NULL
1518 * fit_conf_get_node() will try to
1519 * find default config node
1520 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001521 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001522 fit_uname_config);
1523
1524 if (cfg_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001525 fdt_error("Could not find "
1526 "configuration "
1527 "node\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001528 goto error;
1529 }
1530
Stephen Warren6904b6e2011-10-18 11:11:49 +00001531 fit_uname_config = fdt_get_name(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001532 cfg_noffset, NULL);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001533 printf(" Using '%s' configuration\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001534 fit_uname_config);
1535
Stephen Warren6904b6e2011-10-18 11:11:49 +00001536 fdt_noffset = fit_conf_get_fdt_node(
1537 fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001538 cfg_noffset);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001539 fit_uname_fdt = fit_get_name(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001540 fdt_noffset, NULL);
1541 } else {
1542 /* get FDT component image node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001543 fdt_noffset = fit_image_get_node(
1544 fit_hdr,
1545 fit_uname_fdt);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001546 }
1547 if (fdt_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001548 fdt_error("Could not find subimage "
1549 "node\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001550 goto error;
1551 }
1552
Stephen Warren6904b6e2011-10-18 11:11:49 +00001553 printf(" Trying '%s' FDT blob subimage\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001554 fit_uname_fdt);
1555
Stephen Warren6904b6e2011-10-18 11:11:49 +00001556 if (!fit_check_fdt(fit_hdr, fdt_noffset,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001557 images->verify))
1558 goto error;
1559
1560 /* get ramdisk image data address and length */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001561 if (fit_image_get_data(fit_hdr, fdt_noffset,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001562 &data, &size)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001563 fdt_error("Could not find FDT "
1564 "subimage data");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001565 goto error;
1566 }
1567
1568 /* verift that image data is a proper FDT blob */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001569 if (fdt_check_header((char *)data) != 0) {
1570 fdt_error("Subimage data is not a FTD");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001571 goto error;
1572 }
1573
1574 /*
1575 * move image data to the load address,
1576 * make sure we don't overwrite initial image
1577 */
1578 image_start = (ulong)fit_hdr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001579 image_end = fit_get_end(fit_hdr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001580
Stephen Warren6904b6e2011-10-18 11:11:49 +00001581 if (fit_image_get_load(fit_hdr, fdt_noffset,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001582 &load_start) == 0) {
1583 load_end = load_start + size;
1584
1585 if ((load_start < image_end) &&
1586 (load_end > image_start)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001587 fdt_error("FDT overwritten");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001588 goto error;
1589 }
1590
Stephen Warren6904b6e2011-10-18 11:11:49 +00001591 printf(" Loading FDT from 0x%08lx "
1592 "to 0x%08lx\n",
1593 (ulong)data,
1594 load_start);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001595
Stephen Warren6904b6e2011-10-18 11:11:49 +00001596 memmove((void *)load_start,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001597 (void *)data, size);
1598
1599 fdt_blob = (char *)load_start;
1600 } else {
1601 fdt_blob = (char *)data;
1602 }
1603
1604 images->fit_hdr_fdt = fit_hdr;
1605 images->fit_uname_fdt = fit_uname_fdt;
1606 images->fit_noffset_fdt = fdt_noffset;
1607 break;
1608 } else
1609#endif
1610 {
1611 /*
1612 * FDT blob
1613 */
1614 fdt_blob = (char *)fdt_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001615 debug("* fdt: raw FDT blob\n");
1616 printf("## Flattened Device Tree blob at "
1617 "%08lx\n", (long)fdt_blob);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001618 }
1619 break;
1620 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +00001621 puts("ERROR: Did not find a cmdline Flattened Device "
1622 "Tree\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001623 goto error;
1624 }
1625
Simon Glass00aa9882011-09-23 06:22:04 +00001626 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001627
1628 } else if (images->legacy_hdr_valid &&
Stephen Warren6904b6e2011-10-18 11:11:49 +00001629 image_check_type(&images->legacy_hdr_os_copy,
1630 IH_TYPE_MULTI)) {
Kumar Galaa5130ad2008-08-15 08:24:38 -05001631
1632 ulong fdt_data, fdt_len;
1633
1634 /*
1635 * Now check if we have a legacy multi-component image,
1636 * get second entry data start address and len.
1637 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001638 printf("## Flattened Device Tree from multi "
Kumar Galaa5130ad2008-08-15 08:24:38 -05001639 "component Image at %08lX\n",
1640 (ulong)images->legacy_hdr_os);
1641
Stephen Warren6904b6e2011-10-18 11:11:49 +00001642 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1643 &fdt_len);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001644 if (fdt_len) {
1645
1646 fdt_blob = (char *)fdt_data;
Simon Glass00aa9882011-09-23 06:22:04 +00001647 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001648
Stephen Warren6904b6e2011-10-18 11:11:49 +00001649 if (fdt_check_header(fdt_blob) != 0) {
1650 fdt_error("image is not a fdt");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001651 goto error;
1652 }
1653
John Rigbyb763f932010-10-13 13:57:32 -06001654 if (fdt_totalsize(fdt_blob) != fdt_len) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001655 fdt_error("fdt size != image size");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001656 goto error;
1657 }
1658 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001659 debug("## No Flattened Device Tree\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001660 return 0;
1661 }
1662 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001663 debug("## No Flattened Device Tree\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001664 return 0;
1665 }
1666
1667 *of_flat_tree = fdt_blob;
John Rigbyb763f932010-10-13 13:57:32 -06001668 *of_size = fdt_totalsize(fdt_blob);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001669 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner7ddfafc2008-08-21 07:12:26 -07001670 (ulong)*of_flat_tree, *of_size);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001671
1672 return 0;
1673
1674error:
1675 *of_flat_tree = 0;
1676 *of_size = 0;
1677 return 1;
1678}
1679#endif /* CONFIG_OF_LIBFDT */
1680
John Rigbyeea8e692010-10-13 13:57:35 -06001681#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001682/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001683 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galab937bb72008-02-27 21:51:49 -06001684 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001685 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1686 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1687 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001688 * boot_get_cmdline() allocates space for kernel command line below
Grant Likelydfff7a22011-03-28 09:58:34 +00001689 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001690 * variable is present its contents is copied to allocated kernel
1691 * command line.
1692 *
1693 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001694 * 0 - success
1695 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001696 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001697int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001698{
1699 char *cmdline;
1700 char *s;
1701
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001702 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likely26396382011-03-28 09:58:43 +00001703 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galab937bb72008-02-27 21:51:49 -06001704
1705 if (cmdline == NULL)
1706 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001707
1708 if ((s = getenv("bootargs")) == NULL)
1709 s = "";
1710
1711 strcpy(cmdline, s);
1712
1713 *cmd_start = (ulong) & cmdline[0];
1714 *cmd_end = *cmd_start + strlen(cmdline);
1715
Stephen Warren6904b6e2011-10-18 11:11:49 +00001716 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001717
Kumar Galab937bb72008-02-27 21:51:49 -06001718 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001719}
John Rigbyeea8e692010-10-13 13:57:35 -06001720#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001721
John Rigbyeea8e692010-10-13 13:57:35 -06001722#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001723/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001724 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galab937bb72008-02-27 21:51:49 -06001725 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001726 * @kbd: double pointer to board info data
1727 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001728 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likelydfff7a22011-03-28 09:58:34 +00001729 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1730 * with the current u-boot board info data.
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001731 *
1732 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001733 * 0 - success
1734 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001735 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001736int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001737{
Becky Bruced26d67c2008-06-09 20:37:18 -05001738 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likely26396382011-03-28 09:58:43 +00001739 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galab937bb72008-02-27 21:51:49 -06001740 if (*kbd == NULL)
1741 return -1;
1742
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001743 **kbd = *(gd->bd);
1744
Stephen Warren6904b6e2011-10-18 11:11:49 +00001745 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001746
1747#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1748 do_bdinfo(NULL, 0, 0, NULL);
1749#endif
1750
Kumar Galab937bb72008-02-27 21:51:49 -06001751 return 0;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001752}
John Rigbyeea8e692010-10-13 13:57:35 -06001753#endif /* CONFIG_SYS_BOOT_GET_KBD */
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001754#endif /* !USE_HOSTCC */
Marian Balakowicz95418502008-01-31 13:55:39 +01001755
Marian Balakowicz3413e222008-02-21 17:20:20 +01001756#if defined(CONFIG_FIT)
1757/*****************************************************************************/
1758/* New uImage format routines */
1759/*****************************************************************************/
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001760#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +00001761static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
Marian Balakowicz3413e222008-02-21 17:20:20 +01001762 ulong *addr, const char **name)
1763{
1764 const char *sep;
1765
1766 *addr = addr_curr;
1767 *name = NULL;
1768
Stephen Warren6904b6e2011-10-18 11:11:49 +00001769 sep = strchr(spec, sepc);
Marian Balakowicz3413e222008-02-21 17:20:20 +01001770 if (sep) {
1771 if (sep - spec > 0)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001772 *addr = simple_strtoul(spec, NULL, 16);
Marian Balakowicz3413e222008-02-21 17:20:20 +01001773
1774 *name = sep + 1;
1775 return 1;
1776 }
1777
1778 return 0;
1779}
1780
1781/**
1782 * fit_parse_conf - parse FIT configuration spec
1783 * @spec: input string, containing configuration spec
1784 * @add_curr: current image address (to be used as a possible default)
1785 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1786 * configuration
1787 * @conf_name double pointer to a char, will hold pointer to a configuration
1788 * unit name
1789 *
1790 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1791 * where <addr> is a FIT image address that contains configuration
1792 * with a <conf> unit name.
1793 *
1794 * Address part is optional, and if omitted default add_curr will
1795 * be used instead.
1796 *
1797 * returns:
1798 * 1 if spec is a valid configuration string,
1799 * addr and conf_name are set accordingly
1800 * 0 otherwise
1801 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001802inline int fit_parse_conf(const char *spec, ulong addr_curr,
Marian Balakowicz3413e222008-02-21 17:20:20 +01001803 ulong *addr, const char **conf_name)
1804{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001805 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
Marian Balakowicz3413e222008-02-21 17:20:20 +01001806}
1807
1808/**
1809 * fit_parse_subimage - parse FIT subimage spec
1810 * @spec: input string, containing subimage spec
1811 * @add_curr: current image address (to be used as a possible default)
1812 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1813 * subimage
1814 * @image_name: double pointer to a char, will hold pointer to a subimage name
1815 *
1816 * fit_parse_subimage() expects subimage spec in the for of
1817 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1818 * subimage with a <subimg> unit name.
1819 *
1820 * Address part is optional, and if omitted default add_curr will
1821 * be used instead.
1822 *
1823 * returns:
1824 * 1 if spec is a valid subimage string,
1825 * addr and image_name are set accordingly
1826 * 0 otherwise
1827 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001828inline int fit_parse_subimage(const char *spec, ulong addr_curr,
Marian Balakowicz3413e222008-02-21 17:20:20 +01001829 ulong *addr, const char **image_name)
1830{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001831 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
Marian Balakowicz3413e222008-02-21 17:20:20 +01001832}
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001833#endif /* !USE_HOSTCC */
1834
Stephen Warren6904b6e2011-10-18 11:11:49 +00001835static void fit_get_debug(const void *fit, int noffset,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001836 char *prop_name, int err)
1837{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001838 debug("Can't get '%s' property from FIT 0x%08lx, "
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001839 "node: offset %d, name %s (%s)\n",
1840 prop_name, (ulong)fit, noffset,
Stephen Warren6904b6e2011-10-18 11:11:49 +00001841 fit_get_name(fit, noffset, NULL),
1842 fdt_strerror(err));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001843}
1844
1845/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001846 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001847 * @fit: pointer to the FIT format image header
1848 * @p: pointer to prefix string
1849 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001850 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001851 * The routine prints out FIT image properties (root node level) follwed by
1852 * the details of each component image.
1853 *
1854 * returns:
1855 * no returned results
1856 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001857void fit_print_contents(const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001858{
1859 char *desc;
1860 char *uname;
1861 int images_noffset;
1862 int confs_noffset;
1863 int noffset;
1864 int ndepth;
1865 int count = 0;
1866 int ret;
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001867 const char *p;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001868#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1869 time_t timestamp;
1870#endif
1871
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001872#ifdef USE_HOSTCC
1873 p = "";
1874#else
1875 p = " ";
1876#endif
1877
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001878 /* Root node properties */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001879 ret = fit_get_desc(fit, 0, &desc);
1880 printf("%sFIT description: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001881 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001882 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001883 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00001884 printf("%s\n", desc);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001885
1886#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001887 ret = fit_get_timestamp(fit, 0, &timestamp);
1888 printf("%sCreated: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001889 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001890 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001891 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00001892 genimg_print_time(timestamp);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001893#endif
1894
1895 /* Find images parent node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001896 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001897 if (images_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001898 printf("Can't find images parent node '%s' (%s)\n",
1899 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001900 return;
1901 }
1902
1903 /* Process its subnodes, print out component images details */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001904 for (ndepth = 0, count = 0,
1905 noffset = fdt_next_node(fit, images_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001906 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001907 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001908 if (ndepth == 1) {
1909 /*
1910 * Direct child node of the images parent node,
1911 * i.e. component image node.
1912 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001913 printf("%s Image %u (%s)\n", p, count++,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001914 fit_get_name(fit, noffset, NULL));
1915
Stephen Warren6904b6e2011-10-18 11:11:49 +00001916 fit_image_print(fit, noffset, p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001917 }
1918 }
1919
1920 /* Find configurations parent node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001921 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001922 if (confs_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001923 debug("Can't get configurations parent node '%s' (%s)\n",
1924 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001925 return;
1926 }
1927
1928 /* get default configuration unit name from default property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001929 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001930 if (uname)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001931 printf("%s Default Configuration: '%s'\n", p, uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001932
1933 /* Process its subnodes, print out configurations details */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001934 for (ndepth = 0, count = 0,
1935 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001936 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001937 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001938 if (ndepth == 1) {
1939 /*
1940 * Direct child node of the configurations parent node,
1941 * i.e. configuration node.
1942 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001943 printf("%s Configuration %u (%s)\n", p, count++,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001944 fit_get_name(fit, noffset, NULL));
1945
Stephen Warren6904b6e2011-10-18 11:11:49 +00001946 fit_conf_print(fit, noffset, p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001947 }
1948 }
1949}
1950
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001951/**
1952 * fit_image_print - prints out the FIT component image details
1953 * @fit: pointer to the FIT format image header
1954 * @image_noffset: offset of the component image node
1955 * @p: pointer to prefix string
1956 *
1957 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001958 * image. If present, hash nodes are printed out as well. Load
1959 * address for images of type firmware is also printed out. Since the load
1960 * address is not mandatory for firmware images, it will be output as
1961 * "unavailable" when not present.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001962 *
1963 * returns:
1964 * no returned results
1965 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001966void fit_image_print(const void *fit, int image_noffset, const char *p)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001967{
1968 char *desc;
1969 uint8_t type, arch, os, comp;
1970 size_t size;
1971 ulong load, entry;
1972 const void *data;
1973 int noffset;
1974 int ndepth;
1975 int ret;
1976
1977 /* Mandatory properties */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001978 ret = fit_get_desc(fit, image_noffset, &desc);
1979 printf("%s Description: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001980 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001981 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001982 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00001983 printf("%s\n", desc);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001984
Stephen Warren6904b6e2011-10-18 11:11:49 +00001985 fit_image_get_type(fit, image_noffset, &type);
1986 printf("%s Type: %s\n", p, genimg_get_type_name(type));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001987
Stephen Warren6904b6e2011-10-18 11:11:49 +00001988 fit_image_get_comp(fit, image_noffset, &comp);
1989 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001990
Stephen Warren6904b6e2011-10-18 11:11:49 +00001991 ret = fit_image_get_data(fit, image_noffset, &data, &size);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001992
1993#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +00001994 printf("%s Data Start: ", 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("0x%08lx\n", (ulong)data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001999#endif
2000
Stephen Warren6904b6e2011-10-18 11:11:49 +00002001 printf("%s Data Size: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002002 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002003 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002004 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00002005 genimg_print_size(size);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002006
2007 /* Remaining, type dependent properties */
2008 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2009 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2010 (type == IH_TYPE_FLATDT)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002011 fit_image_get_arch(fit, image_noffset, &arch);
2012 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002013 }
2014
2015 if (type == IH_TYPE_KERNEL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002016 fit_image_get_os(fit, image_noffset, &os);
2017 printf("%s OS: %s\n", p, genimg_get_os_name(os));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002018 }
2019
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02002020 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2021 (type == IH_TYPE_FIRMWARE)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002022 ret = fit_image_get_load(fit, image_noffset, &load);
2023 printf("%s Load Address: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002024 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002025 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002026 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00002027 printf("0x%08lx\n", load);
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02002028 }
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002029
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02002030 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002031 fit_image_get_entry(fit, image_noffset, &entry);
2032 printf("%s Entry Point: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002033 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002034 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002035 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00002036 printf("0x%08lx\n", entry);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002037 }
2038
2039 /* Process all hash subnodes of the component image node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002040 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002041 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002042 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002043 if (ndepth == 1) {
2044 /* Direct child node of the component image node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002045 fit_image_print_hash(fit, noffset, p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002046 }
2047 }
2048}
2049
2050/**
2051 * fit_image_print_hash - prints out the hash node details
2052 * @fit: pointer to the FIT format image header
2053 * @noffset: offset of the hash node
2054 * @p: pointer to prefix string
2055 *
2056 * fit_image_print_hash() lists properies for the processed hash node
2057 *
2058 * returns:
2059 * no returned results
2060 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002061void fit_image_print_hash(const void *fit, int noffset, const char *p)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002062{
2063 char *algo;
2064 uint8_t *value;
2065 int value_len;
2066 int i, ret;
2067
2068 /*
2069 * Check subnode name, must be equal to "hash".
2070 * Multiple hash nodes require unique unit node
2071 * names, e.g. hash@1, hash@2, etc.
2072 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002073 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002074 FIT_HASH_NODENAME,
2075 strlen(FIT_HASH_NODENAME)) != 0)
2076 return;
2077
Stephen Warren6904b6e2011-10-18 11:11:49 +00002078 debug("%s Hash node: '%s'\n", p,
2079 fit_get_name(fit, noffset, NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002080
Stephen Warren6904b6e2011-10-18 11:11:49 +00002081 printf("%s Hash algo: ", p);
2082 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2083 printf("invalid/unsupported\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002084 return;
2085 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00002086 printf("%s\n", algo);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002087
Stephen Warren6904b6e2011-10-18 11:11:49 +00002088 ret = fit_image_hash_get_value(fit, noffset, &value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002089 &value_len);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002090 printf("%s Hash value: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002091 if (ret) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002092 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002093 } else {
2094 for (i = 0; i < value_len; i++)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002095 printf("%02x", value[i]);
2096 printf("\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002097 }
2098
Stephen Warren6904b6e2011-10-18 11:11:49 +00002099 debug("%s Hash len: %d\n", p, value_len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002100}
2101
2102/**
2103 * fit_get_desc - get node description property
2104 * @fit: pointer to the FIT format image header
2105 * @noffset: node offset
2106 * @desc: double pointer to the char, will hold pointer to the descrption
2107 *
2108 * fit_get_desc() reads description property from a given node, if
2109 * description is found pointer to it is returened in third call argument.
2110 *
2111 * returns:
2112 * 0, on success
2113 * -1, on failure
2114 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002115int fit_get_desc(const void *fit, int noffset, char **desc)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002116{
2117 int len;
2118
Stephen Warren6904b6e2011-10-18 11:11:49 +00002119 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002120 if (*desc == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002121 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002122 return -1;
2123 }
2124
2125 return 0;
2126}
2127
2128/**
2129 * fit_get_timestamp - get node timestamp property
2130 * @fit: pointer to the FIT format image header
2131 * @noffset: node offset
2132 * @timestamp: pointer to the time_t, will hold read timestamp
2133 *
2134 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2135 * is found and has a correct size its value is retured in third call
2136 * argument.
2137 *
2138 * returns:
2139 * 0, on success
2140 * -1, on property read failure
2141 * -2, on wrong timestamp size
2142 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002143int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002144{
2145 int len;
2146 const void *data;
2147
Stephen Warren6904b6e2011-10-18 11:11:49 +00002148 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002149 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002150 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002151 return -1;
2152 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00002153 if (len != sizeof(uint32_t)) {
2154 debug("FIT timestamp with incorrect size of (%u)\n", len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002155 return -2;
2156 }
2157
Stephen Warren6904b6e2011-10-18 11:11:49 +00002158 *timestamp = uimage_to_cpu(*((uint32_t *)data));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002159 return 0;
2160}
2161
2162/**
2163 * fit_image_get_node - get node offset for component image of a given unit name
2164 * @fit: pointer to the FIT format image header
2165 * @image_uname: component image node unit name
2166 *
2167 * fit_image_get_node() finds a component image (withing the '/images'
2168 * node) of a provided unit name. If image is found its node offset is
2169 * returned to the caller.
2170 *
2171 * returns:
2172 * image node offset when found (>=0)
2173 * negative number on failure (FDT_ERR_* code)
2174 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002175int fit_image_get_node(const void *fit, const char *image_uname)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002176{
2177 int noffset, images_noffset;
2178
Stephen Warren6904b6e2011-10-18 11:11:49 +00002179 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002180 if (images_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002181 debug("Can't find images parent node '%s' (%s)\n",
2182 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002183 return images_noffset;
2184 }
2185
Stephen Warren6904b6e2011-10-18 11:11:49 +00002186 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002187 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002188 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2189 image_uname, fdt_strerror(noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002190 }
2191
2192 return noffset;
2193}
2194
2195/**
2196 * fit_image_get_os - get os id for a given component image node
2197 * @fit: pointer to the FIT format image header
2198 * @noffset: component image node offset
2199 * @os: pointer to the uint8_t, will hold os numeric id
2200 *
2201 * fit_image_get_os() finds os property in a given component image node.
2202 * If the property is found, its (string) value is translated to the numeric
2203 * id which is returned to the caller.
2204 *
2205 * returns:
2206 * 0, on success
2207 * -1, on failure
2208 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002209int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002210{
2211 int len;
2212 const void *data;
2213
2214 /* Get OS name from property data */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002215 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002216 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002217 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002218 *os = -1;
2219 return -1;
2220 }
2221
2222 /* Translate OS name to id */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002223 *os = genimg_get_os_id(data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002224 return 0;
2225}
2226
2227/**
2228 * fit_image_get_arch - get arch id for a given component image node
2229 * @fit: pointer to the FIT format image header
2230 * @noffset: component image node offset
2231 * @arch: pointer to the uint8_t, will hold arch numeric id
2232 *
2233 * fit_image_get_arch() finds arch property in a given component image node.
2234 * If the property is found, its (string) value is translated to the numeric
2235 * id which is returned to the caller.
2236 *
2237 * returns:
2238 * 0, on success
2239 * -1, on failure
2240 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002241int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002242{
2243 int len;
2244 const void *data;
2245
2246 /* Get architecture name from property data */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002247 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002248 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002249 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002250 *arch = -1;
2251 return -1;
2252 }
2253
2254 /* Translate architecture name to id */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002255 *arch = genimg_get_arch_id(data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002256 return 0;
2257}
2258
2259/**
2260 * fit_image_get_type - get type id for a given component image node
2261 * @fit: pointer to the FIT format image header
2262 * @noffset: component image node offset
2263 * @type: pointer to the uint8_t, will hold type numeric id
2264 *
2265 * fit_image_get_type() finds type property in a given component image node.
2266 * If the property is found, its (string) value is translated to the numeric
2267 * id which is returned to the caller.
2268 *
2269 * returns:
2270 * 0, on success
2271 * -1, on failure
2272 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002273int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002274{
2275 int len;
2276 const void *data;
2277
2278 /* Get image type name from property data */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002279 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002280 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002281 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002282 *type = -1;
2283 return -1;
2284 }
2285
2286 /* Translate image type name to id */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002287 *type = genimg_get_type_id(data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002288 return 0;
2289}
2290
2291/**
2292 * fit_image_get_comp - get comp id for a given component image node
2293 * @fit: pointer to the FIT format image header
2294 * @noffset: component image node offset
2295 * @comp: pointer to the uint8_t, will hold comp numeric id
2296 *
2297 * fit_image_get_comp() finds comp property in a given component image node.
2298 * If the property is found, its (string) value is translated to the numeric
2299 * id which is returned to the caller.
2300 *
2301 * returns:
2302 * 0, on success
2303 * -1, on failure
2304 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002305int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002306{
2307 int len;
2308 const void *data;
2309
2310 /* Get compression name from property data */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002311 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002312 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002313 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002314 *comp = -1;
2315 return -1;
2316 }
2317
2318 /* Translate compression name to id */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002319 *comp = genimg_get_comp_id(data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002320 return 0;
2321}
2322
2323/**
2324 * fit_image_get_load - get load address property for a given component image node
2325 * @fit: pointer to the FIT format image header
2326 * @noffset: component image node offset
2327 * @load: pointer to the uint32_t, will hold load address
2328 *
2329 * fit_image_get_load() finds load address property in a given component image node.
2330 * If the property is found, its value is returned to the caller.
2331 *
2332 * returns:
2333 * 0, on success
2334 * -1, on failure
2335 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002336int fit_image_get_load(const void *fit, int noffset, ulong *load)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002337{
2338 int len;
2339 const uint32_t *data;
2340
Stephen Warren6904b6e2011-10-18 11:11:49 +00002341 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002342 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002343 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002344 return -1;
2345 }
2346
Stephen Warren6904b6e2011-10-18 11:11:49 +00002347 *load = uimage_to_cpu(*data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002348 return 0;
2349}
2350
2351/**
2352 * fit_image_get_entry - get entry point address property for a given component image node
2353 * @fit: pointer to the FIT format image header
2354 * @noffset: component image node offset
2355 * @entry: pointer to the uint32_t, will hold entry point address
2356 *
2357 * fit_image_get_entry() finds entry point address property in a given component image node.
2358 * If the property is found, its value is returned to the caller.
2359 *
2360 * returns:
2361 * 0, on success
2362 * -1, on failure
2363 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002364int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002365{
2366 int len;
2367 const uint32_t *data;
2368
Stephen Warren6904b6e2011-10-18 11:11:49 +00002369 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002370 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002371 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002372 return -1;
2373 }
2374
Stephen Warren6904b6e2011-10-18 11:11:49 +00002375 *entry = uimage_to_cpu(*data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002376 return 0;
2377}
2378
2379/**
2380 * fit_image_get_data - get data property and its size for a given component image node
2381 * @fit: pointer to the FIT format image header
2382 * @noffset: component image node offset
2383 * @data: double pointer to void, will hold data property's data address
2384 * @size: pointer to size_t, will hold data property's data size
2385 *
2386 * fit_image_get_data() finds data property in a given component image node.
2387 * If the property is found its data start address and size are returned to
2388 * the caller.
2389 *
2390 * returns:
2391 * 0, on success
2392 * -1, on failure
2393 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002394int fit_image_get_data(const void *fit, int noffset,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002395 const void **data, size_t *size)
2396{
2397 int len;
2398
Stephen Warren6904b6e2011-10-18 11:11:49 +00002399 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002400 if (*data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002401 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002402 *size = 0;
2403 return -1;
2404 }
2405
2406 *size = len;
2407 return 0;
2408}
2409
2410/**
2411 * fit_image_hash_get_algo - get hash algorithm name
2412 * @fit: pointer to the FIT format image header
2413 * @noffset: hash node offset
2414 * @algo: double pointer to char, will hold pointer to the algorithm name
2415 *
2416 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2417 * If the property is found its data start address is returned to the caller.
2418 *
2419 * returns:
2420 * 0, on success
2421 * -1, on failure
2422 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002423int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002424{
2425 int len;
2426
Stephen Warren6904b6e2011-10-18 11:11:49 +00002427 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002428 if (*algo == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002429 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002430 return -1;
2431 }
2432
2433 return 0;
2434}
2435
2436/**
2437 * fit_image_hash_get_value - get hash value and length
2438 * @fit: pointer to the FIT format image header
2439 * @noffset: hash node offset
2440 * @value: double pointer to uint8_t, will hold address of a hash value data
2441 * @value_len: pointer to an int, will hold hash data length
2442 *
2443 * fit_image_hash_get_value() finds hash value property in a given hash node.
2444 * If the property is found its data start address and size are returned to
2445 * the caller.
2446 *
2447 * returns:
2448 * 0, on success
2449 * -1, on failure
2450 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002451int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002452 int *value_len)
2453{
2454 int len;
2455
Stephen Warren6904b6e2011-10-18 11:11:49 +00002456 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002457 if (*value == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002458 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002459 *value_len = 0;
2460 return -1;
2461 }
2462
2463 *value_len = len;
2464 return 0;
2465}
2466
2467/**
2468 * fit_set_timestamp - set node timestamp property
2469 * @fit: pointer to the FIT format image header
2470 * @noffset: node offset
2471 * @timestamp: timestamp value to be set
2472 *
2473 * fit_set_timestamp() attempts to set timestamp property in the requested
2474 * node and returns operation status to the caller.
2475 *
2476 * returns:
2477 * 0, on success
2478 * -1, on property read failure
2479 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002480int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002481{
2482 uint32_t t;
2483 int ret;
2484
Stephen Warren6904b6e2011-10-18 11:11:49 +00002485 t = cpu_to_uimage(timestamp);
2486 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2487 sizeof(uint32_t));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002488 if (ret) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002489 printf("Can't set '%s' property for '%s' node (%s)\n",
2490 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2491 fdt_strerror(ret));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002492 return -1;
2493 }
2494
2495 return 0;
2496}
2497
2498/**
2499 * calculate_hash - calculate and return hash for provided input data
2500 * @data: pointer to the input data
2501 * @data_len: data length
2502 * @algo: requested hash algorithm
2503 * @value: pointer to the char, will hold hash value data (caller must
2504 * allocate enough free space)
2505 * value_len: length of the calculated hash
2506 *
2507 * calculate_hash() computes input data hash according to the requested algorithm.
2508 * Resulting hash value is placed in caller provided 'value' buffer, length
2509 * of the calculated hash is returned via value_len pointer argument.
2510 *
2511 * returns:
2512 * 0, on success
2513 * -1, when algo is unsupported
2514 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002515static int calculate_hash(const void *data, int data_len, const char *algo,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002516 uint8_t *value, int *value_len)
2517{
Stephen Warren6904b6e2011-10-18 11:11:49 +00002518 if (strcmp(algo, "crc32") == 0) {
2519 *((uint32_t *)value) = crc32_wd(0, data, data_len,
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002520 CHUNKSZ_CRC32);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002521 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002522 *value_len = 4;
Stephen Warren6904b6e2011-10-18 11:11:49 +00002523 } else if (strcmp(algo, "sha1") == 0) {
2524 sha1_csum_wd((unsigned char *) data, data_len,
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002525 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002526 *value_len = 20;
Stephen Warren6904b6e2011-10-18 11:11:49 +00002527 } else if (strcmp(algo, "md5") == 0) {
2528 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka2e6f2852008-03-14 16:22:34 +01002529 *value_len = 16;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002530 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002531 debug("Unsupported hash alogrithm\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002532 return -1;
2533 }
2534 return 0;
2535}
2536
2537#ifdef USE_HOSTCC
2538/**
2539 * fit_set_hashes - process FIT component image nodes and calculate hashes
2540 * @fit: pointer to the FIT format image header
2541 *
2542 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2543 * Hashes are calculated for all component images which have hash subnodes
2544 * with algorithm property set to one of the supported hash algorithms.
2545 *
2546 * returns
2547 * 0, on success
2548 * libfdt error code, on failure
2549 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002550int fit_set_hashes(void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002551{
2552 int images_noffset;
2553 int noffset;
2554 int ndepth;
2555 int ret;
2556
2557 /* Find images parent node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002558 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002559 if (images_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002560 printf("Can't find images parent node '%s' (%s)\n",
2561 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002562 return images_noffset;
2563 }
2564
2565 /* Process its subnodes, print out component images details */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002566 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002567 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002568 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002569 if (ndepth == 1) {
2570 /*
2571 * Direct child node of the images parent node,
2572 * i.e. component image node.
2573 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002574 ret = fit_image_set_hashes(fit, noffset);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002575 if (ret)
2576 return ret;
2577 }
2578 }
2579
2580 return 0;
2581}
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01002582
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002583/**
2584 * fit_image_set_hashes - calculate/set hashes for given component image node
2585 * @fit: pointer to the FIT format image header
2586 * @image_noffset: requested component image node
2587 *
2588 * fit_image_set_hashes() adds hash values for an component image node. All
2589 * existing hash subnodes are checked, if algorithm property is set to one of
2590 * the supported hash algorithms, hash value is computed and corresponding
2591 * hash node property is set, for example:
2592 *
2593 * Input component image node structure:
2594 *
2595 * o image@1 (at image_noffset)
2596 * | - data = [binary data]
2597 * o hash@1
2598 * |- algo = "sha1"
2599 *
2600 * Output component image node structure:
2601 *
2602 * o image@1 (at image_noffset)
2603 * | - data = [binary data]
2604 * o hash@1
2605 * |- algo = "sha1"
2606 * |- value = sha1(data)
2607 *
2608 * returns:
2609 * 0 on sucess
2610 * <0 on failure
2611 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002612int fit_image_set_hashes(void *fit, int image_noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002613{
2614 const void *data;
2615 size_t size;
2616 char *algo;
2617 uint8_t value[FIT_MAX_HASH_LEN];
2618 int value_len;
2619 int noffset;
2620 int ndepth;
2621
2622 /* Get image data and data length */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002623 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2624 printf("Can't get image data/size\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002625 return -1;
2626 }
2627
2628 /* Process all hash subnodes of the component image node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002629 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002630 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002631 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002632 if (ndepth == 1) {
2633 /* Direct child node of the component image node */
2634
2635 /*
2636 * Check subnode name, must be equal to "hash".
2637 * Multiple hash nodes require unique unit node
2638 * names, e.g. hash@1, hash@2, etc.
2639 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002640 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002641 FIT_HASH_NODENAME,
2642 strlen(FIT_HASH_NODENAME)) != 0) {
2643 /* Not a hash subnode, skip it */
2644 continue;
2645 }
2646
Stephen Warren6904b6e2011-10-18 11:11:49 +00002647 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2648 printf("Can't get hash algo property for "
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002649 "'%s' hash node in '%s' image node\n",
Stephen Warren6904b6e2011-10-18 11:11:49 +00002650 fit_get_name(fit, noffset, NULL),
2651 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002652 return -1;
2653 }
2654
Stephen Warren6904b6e2011-10-18 11:11:49 +00002655 if (calculate_hash(data, size, algo, value,
2656 &value_len)) {
2657 printf("Unsupported hash algorithm (%s) for "
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002658 "'%s' hash node in '%s' image node\n",
Stephen Warren6904b6e2011-10-18 11:11:49 +00002659 algo, fit_get_name(fit, noffset, NULL),
2660 fit_get_name(fit, image_noffset,
2661 NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002662 return -1;
2663 }
2664
Stephen Warren6904b6e2011-10-18 11:11:49 +00002665 if (fit_image_hash_set_value(fit, noffset, value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002666 value_len)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002667 printf("Can't set hash value for "
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002668 "'%s' hash node in '%s' image node\n",
Stephen Warren6904b6e2011-10-18 11:11:49 +00002669 fit_get_name(fit, noffset, NULL),
2670 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002671 return -1;
2672 }
2673 }
2674 }
2675
2676 return 0;
2677}
2678
2679/**
2680 * fit_image_hash_set_value - set hash value in requested has node
2681 * @fit: pointer to the FIT format image header
2682 * @noffset: hash node offset
2683 * @value: hash value to be set
2684 * @value_len: hash value length
2685 *
2686 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2687 * given and returns operation status to the caller.
2688 *
2689 * returns
2690 * 0, on success
2691 * -1, on failure
2692 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002693int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002694 int value_len)
2695{
2696 int ret;
2697
Stephen Warren6904b6e2011-10-18 11:11:49 +00002698 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002699 if (ret) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002700 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2701 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2702 fdt_strerror(ret));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002703 return -1;
2704 }
2705
2706 return 0;
2707}
2708#endif /* USE_HOSTCC */
2709
2710/**
2711 * fit_image_check_hashes - verify data intergity
2712 * @fit: pointer to the FIT format image header
2713 * @image_noffset: component image node offset
2714 *
2715 * fit_image_check_hashes() goes over component image hash nodes,
2716 * re-calculates each data hash and compares with the value stored in hash
2717 * node.
2718 *
2719 * returns:
2720 * 1, if all hashes are valid
2721 * 0, otherwise (or on error)
2722 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002723int fit_image_check_hashes(const void *fit, int image_noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002724{
2725 const void *data;
2726 size_t size;
2727 char *algo;
2728 uint8_t *fit_value;
2729 int fit_value_len;
2730 uint8_t value[FIT_MAX_HASH_LEN];
2731 int value_len;
2732 int noffset;
2733 int ndepth;
2734 char *err_msg = "";
2735
2736 /* Get image data and data length */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002737 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2738 printf("Can't get image data/size\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002739 return 0;
2740 }
2741
2742 /* Process all hash subnodes of the component image node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002743 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002744 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002745 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002746 if (ndepth == 1) {
2747 /* Direct child node of the component image node */
2748
2749 /*
2750 * Check subnode name, must be equal to "hash".
2751 * Multiple hash nodes require unique unit node
2752 * names, e.g. hash@1, hash@2, etc.
2753 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002754 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002755 FIT_HASH_NODENAME,
2756 strlen(FIT_HASH_NODENAME)) != 0)
2757 continue;
2758
Stephen Warren6904b6e2011-10-18 11:11:49 +00002759 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002760 err_msg = " error!\nCan't get hash algo "
2761 "property";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002762 goto error;
2763 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00002764 printf("%s", algo);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002765
Stephen Warren6904b6e2011-10-18 11:11:49 +00002766 if (fit_image_hash_get_value(fit, noffset, &fit_value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002767 &fit_value_len)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002768 err_msg = " error!\nCan't get hash value "
2769 "property";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002770 goto error;
2771 }
2772
Stephen Warren6904b6e2011-10-18 11:11:49 +00002773 if (calculate_hash(data, size, algo, value,
2774 &value_len)) {
2775 err_msg = " error!\n"
2776 "Unsupported hash algorithm";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002777 goto error;
2778 }
2779
2780 if (value_len != fit_value_len) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002781 err_msg = " error !\nBad hash value len";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002782 goto error;
Stephen Warren6904b6e2011-10-18 11:11:49 +00002783 } else if (memcmp(value, fit_value, value_len) != 0) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002784 err_msg = " error!\nBad hash value";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002785 goto error;
2786 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00002787 printf("+ ");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002788 }
2789 }
2790
2791 return 1;
2792
2793error:
Stephen Warren6904b6e2011-10-18 11:11:49 +00002794 printf("%s for '%s' hash node in '%s' image node\n",
2795 err_msg, fit_get_name(fit, noffset, NULL),
2796 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002797 return 0;
2798}
2799
2800/**
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002801 * fit_all_image_check_hashes - verify data intergity for all images
2802 * @fit: pointer to the FIT format image header
2803 *
2804 * fit_all_image_check_hashes() goes over all images in the FIT and
2805 * for every images checks if all it's hashes are valid.
2806 *
2807 * returns:
2808 * 1, if all hashes of all images are valid
2809 * 0, otherwise (or on error)
2810 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002811int fit_all_image_check_hashes(const void *fit)
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002812{
2813 int images_noffset;
2814 int noffset;
2815 int ndepth;
2816 int count;
2817
2818 /* Find images parent node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002819 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002820 if (images_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002821 printf("Can't find images parent node '%s' (%s)\n",
2822 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002823 return 0;
2824 }
2825
2826 /* Process all image subnodes, check hashes for each */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002827 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002828 (ulong)fit);
2829 for (ndepth = 0, count = 0,
Stephen Warren6904b6e2011-10-18 11:11:49 +00002830 noffset = fdt_next_node(fit, images_noffset, &ndepth);
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002831 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002832 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002833 if (ndepth == 1) {
2834 /*
2835 * Direct child node of the images parent node,
2836 * i.e. component image node.
2837 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002838 printf(" Hash(es) for Image %u (%s): ", count++,
2839 fit_get_name(fit, noffset, NULL));
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002840
Stephen Warren6904b6e2011-10-18 11:11:49 +00002841 if (!fit_image_check_hashes(fit, noffset))
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002842 return 0;
Stephen Warren6904b6e2011-10-18 11:11:49 +00002843 printf("\n");
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002844 }
2845 }
2846 return 1;
2847}
2848
2849/**
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002850 * fit_image_check_os - check whether image node is of a given os type
2851 * @fit: pointer to the FIT format image header
2852 * @noffset: component image node offset
2853 * @os: requested image os
2854 *
2855 * fit_image_check_os() reads image os property and compares its numeric
2856 * id with the requested os. Comparison result is returned to the caller.
2857 *
2858 * returns:
2859 * 1 if image is of given os type
2860 * 0 otherwise (or on error)
2861 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002862int fit_image_check_os(const void *fit, int noffset, uint8_t os)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002863{
2864 uint8_t image_os;
2865
Stephen Warren6904b6e2011-10-18 11:11:49 +00002866 if (fit_image_get_os(fit, noffset, &image_os))
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002867 return 0;
2868 return (os == image_os);
2869}
2870
2871/**
2872 * fit_image_check_arch - check whether image node is of a given arch
2873 * @fit: pointer to the FIT format image header
2874 * @noffset: component image node offset
2875 * @arch: requested imagearch
2876 *
2877 * fit_image_check_arch() reads image arch property and compares its numeric
2878 * id with the requested arch. Comparison result is returned to the caller.
2879 *
2880 * returns:
2881 * 1 if image is of given arch
2882 * 0 otherwise (or on error)
2883 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002884int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002885{
2886 uint8_t image_arch;
2887
Stephen Warren6904b6e2011-10-18 11:11:49 +00002888 if (fit_image_get_arch(fit, noffset, &image_arch))
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002889 return 0;
2890 return (arch == image_arch);
2891}
2892
2893/**
2894 * fit_image_check_type - check whether image node is of a given type
2895 * @fit: pointer to the FIT format image header
2896 * @noffset: component image node offset
2897 * @type: requested image type
2898 *
2899 * fit_image_check_type() reads image type property and compares its numeric
2900 * id with the requested type. Comparison result is returned to the caller.
2901 *
2902 * returns:
2903 * 1 if image is of given type
2904 * 0 otherwise (or on error)
2905 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002906int fit_image_check_type(const void *fit, int noffset, uint8_t type)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002907{
2908 uint8_t image_type;
2909
Stephen Warren6904b6e2011-10-18 11:11:49 +00002910 if (fit_image_get_type(fit, noffset, &image_type))
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002911 return 0;
2912 return (type == image_type);
2913}
2914
2915/**
2916 * fit_image_check_comp - check whether image node uses given compression
2917 * @fit: pointer to the FIT format image header
2918 * @noffset: component image node offset
2919 * @comp: requested image compression type
2920 *
2921 * fit_image_check_comp() reads image compression property and compares its
2922 * numeric id with the requested compression type. Comparison result is
2923 * returned to the caller.
2924 *
2925 * returns:
2926 * 1 if image uses requested compression
2927 * 0 otherwise (or on error)
2928 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002929int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002930{
2931 uint8_t image_comp;
2932
Stephen Warren6904b6e2011-10-18 11:11:49 +00002933 if (fit_image_get_comp(fit, noffset, &image_comp))
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002934 return 0;
2935 return (comp == image_comp);
2936}
2937
2938/**
2939 * fit_check_format - sanity check FIT image format
2940 * @fit: pointer to the FIT format image header
2941 *
2942 * fit_check_format() runs a basic sanity FIT image verification.
2943 * Routine checks for mandatory properties, nodes, etc.
2944 *
2945 * returns:
2946 * 1, on success
2947 * 0, on failure
2948 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002949int fit_check_format(const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002950{
2951 /* mandatory / node 'description' property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002952 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2953 debug("Wrong FIT format: no description\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002954 return 0;
2955 }
2956
2957#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2958 /* mandatory / node 'timestamp' property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002959 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2960 debug("Wrong FIT format: no timestamp\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002961 return 0;
2962 }
2963#endif
2964
2965 /* mandatory subimages parent '/images' node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002966 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
2967 debug("Wrong FIT format: no images parent node\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002968 return 0;
2969 }
2970
2971 return 1;
2972}
2973
2974/**
2975 * fit_conf_get_node - get node offset for configuration of a given unit name
2976 * @fit: pointer to the FIT format image header
2977 * @conf_uname: configuration node unit name
2978 *
2979 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2980 * parant node) of a provided unit name. If configuration is found its node offset
2981 * is returned to the caller.
2982 *
2983 * When NULL is provided in second argument fit_conf_get_node() will search
2984 * for a default configuration node instead. Default configuration node unit name
2985 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2986 *
2987 * returns:
2988 * configuration node offset when found (>=0)
2989 * negative number on failure (FDT_ERR_* code)
2990 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002991int fit_conf_get_node(const void *fit, const char *conf_uname)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002992{
2993 int noffset, confs_noffset;
2994 int len;
2995
Stephen Warren6904b6e2011-10-18 11:11:49 +00002996 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002997 if (confs_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002998 debug("Can't find configurations parent node '%s' (%s)\n",
2999 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003000 return confs_noffset;
3001 }
3002
3003 if (conf_uname == NULL) {
3004 /* get configuration unit name from the default property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003005 debug("No configuration specified, trying default...\n");
3006 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3007 FIT_DEFAULT_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003008 if (conf_uname == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00003009 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3010 len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003011 return len;
3012 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00003013 debug("Found default configuration: '%s'\n", conf_uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003014 }
3015
Stephen Warren6904b6e2011-10-18 11:11:49 +00003016 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003017 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00003018 debug("Can't get node offset for configuration unit name: "
3019 "'%s' (%s)\n",
3020 conf_uname, fdt_strerror(noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003021 }
3022
3023 return noffset;
3024}
3025
Stephen Warren6904b6e2011-10-18 11:11:49 +00003026static int __fit_conf_get_prop_node(const void *fit, int noffset,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003027 const char *prop_name)
3028{
3029 char *uname;
3030 int len;
3031
3032 /* get kernel image unit name from configuration kernel property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003033 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003034 if (uname == NULL)
3035 return len;
3036
Stephen Warren6904b6e2011-10-18 11:11:49 +00003037 return fit_image_get_node(fit, uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003038}
3039
3040/**
3041 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3042 * a given configuration
3043 * @fit: pointer to the FIT format image header
3044 * @noffset: configuration node offset
3045 *
3046 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3047 * configuration FIT_KERNEL_PROP property and translates it to the node
3048 * offset.
3049 *
3050 * returns:
3051 * image node offset when found (>=0)
3052 * negative number on failure (FDT_ERR_* code)
3053 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003054int fit_conf_get_kernel_node(const void *fit, int noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003055{
Stephen Warren6904b6e2011-10-18 11:11:49 +00003056 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003057}
3058
3059/**
3060 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3061 * a given configuration
3062 * @fit: pointer to the FIT format image header
3063 * @noffset: configuration node offset
3064 *
3065 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3066 * configuration FIT_KERNEL_PROP property and translates it to the node
3067 * offset.
3068 *
3069 * returns:
3070 * image node offset when found (>=0)
3071 * negative number on failure (FDT_ERR_* code)
3072 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003073int fit_conf_get_ramdisk_node(const void *fit, int noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003074{
Stephen Warren6904b6e2011-10-18 11:11:49 +00003075 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003076}
3077
3078/**
3079 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3080 * a given configuration
3081 * @fit: pointer to the FIT format image header
3082 * @noffset: configuration node offset
3083 *
3084 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3085 * configuration FIT_KERNEL_PROP property and translates it to the node
3086 * offset.
3087 *
3088 * returns:
3089 * image node offset when found (>=0)
3090 * negative number on failure (FDT_ERR_* code)
3091 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003092int fit_conf_get_fdt_node(const void *fit, int noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003093{
Stephen Warren6904b6e2011-10-18 11:11:49 +00003094 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003095}
3096
3097/**
3098 * fit_conf_print - prints out the FIT configuration details
3099 * @fit: pointer to the FIT format image header
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +01003100 * @noffset: offset of the configuration node
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003101 * @p: pointer to prefix string
3102 *
3103 * fit_conf_print() lists all mandatory properies for the processed
3104 * configuration node.
3105 *
3106 * returns:
3107 * no returned results
3108 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003109void fit_conf_print(const void *fit, int noffset, const char *p)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003110{
3111 char *desc;
3112 char *uname;
3113 int ret;
3114
3115 /* Mandatory properties */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003116 ret = fit_get_desc(fit, noffset, &desc);
3117 printf("%s Description: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003118 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00003119 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003120 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00003121 printf("%s\n", desc);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003122
Stephen Warren6904b6e2011-10-18 11:11:49 +00003123 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3124 printf("%s Kernel: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003125 if (uname == NULL)
Stephen Warren6904b6e2011-10-18 11:11:49 +00003126 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003127 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00003128 printf("%s\n", uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003129
3130 /* Optional properties */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003131 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003132 if (uname)
Stephen Warren6904b6e2011-10-18 11:11:49 +00003133 printf("%s Init Ramdisk: %s\n", p, uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003134
Stephen Warren6904b6e2011-10-18 11:11:49 +00003135 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003136 if (uname)
Stephen Warren6904b6e2011-10-18 11:11:49 +00003137 printf("%s FDT: %s\n", p, uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003138}
Marian Balakowicza50d5152008-03-12 10:12:37 +01003139
3140/**
3141 * fit_check_ramdisk - verify FIT format ramdisk subimage
3142 * @fit_hdr: pointer to the FIT ramdisk header
3143 * @rd_noffset: ramdisk subimage node offset within FIT image
3144 * @arch: requested ramdisk image architecture type
3145 * @verify: data CRC verification flag
3146 *
3147 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3148 * specified FIT image.
3149 *
3150 * returns:
3151 * 1, on success
3152 * 0, on failure
3153 */
3154#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +00003155static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3156 int verify)
Marian Balakowicza50d5152008-03-12 10:12:37 +01003157{
Stephen Warren6904b6e2011-10-18 11:11:49 +00003158 fit_image_print(fit, rd_noffset, " ");
Marian Balakowicza50d5152008-03-12 10:12:37 +01003159
3160 if (verify) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00003161 puts(" Verifying Hash Integrity ... ");
3162 if (!fit_image_check_hashes(fit, rd_noffset)) {
3163 puts("Bad Data Hash\n");
3164 show_boot_progress(-125);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003165 return 0;
3166 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00003167 puts("OK\n");
Marian Balakowicza50d5152008-03-12 10:12:37 +01003168 }
3169
Stephen Warren6904b6e2011-10-18 11:11:49 +00003170 show_boot_progress(126);
3171 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3172 !fit_image_check_arch(fit, rd_noffset, arch) ||
3173 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3174 printf("No Linux %s Ramdisk Image\n",
Marian Balakowicza50d5152008-03-12 10:12:37 +01003175 genimg_get_arch_name(arch));
Stephen Warren6904b6e2011-10-18 11:11:49 +00003176 show_boot_progress(-126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003177 return 0;
3178 }
3179
Stephen Warren6904b6e2011-10-18 11:11:49 +00003180 show_boot_progress(127);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003181 return 1;
3182}
3183#endif /* USE_HOSTCC */
Marian Balakowicz3413e222008-02-21 17:20:20 +01003184#endif /* CONFIG_FIT */